Commit a1b08947 by Nick N. Sukharev

Обновлена версия модуля customTts.js до шаблонной

1 parent ccfca291
Showing with 69 additions and 65 deletions
...@@ -3,79 +3,83 @@ const path = require("path"); ...@@ -3,79 +3,83 @@ const path = require("path");
class AudioResources class AudioResources
{ {
constructor() constructor()
{ {
this.resources = {}; this.resources = {};
} }
getResourceKey(text, voiceInfo) { getResourceKey(text, voiceInfo) {
return [ return [
voiceInfo.lang ?? "", voiceInfo.lang ?? "",
voiceInfo.speaker ?? "", voiceInfo.speaker ?? "",
voiceInfo.emotion ?? "Neutral", voiceInfo.emotion ?? "Neutral",
voiceInfo.speed ?? 1, voiceInfo.speed ?? 1,
voiceInfo.variation ?? 0, voiceInfo.variation ?? 0,
text, text,
].join("|"); ].join("|");
} }
async appendJson(folder, pack) async appendJson(folder, pack)
{ {
const errors = []; const errors = [];
let i = 0; let i = 0;
for (const phrase of pack.phrases ?? []) { for (const phrase of pack.phrases ?? []) {
i++; i++;
// валидация // валидация
if (phrase.phrase === undefined) { if (phrase.phrase === undefined) {
errors.push(`For ${i} phrase field 'phrase' is undefined`); errors.push(`For ${i} phrase field 'phrase' is undefined`);
continue; continue;
} }
if (phrase.audio === undefined) { if (phrase.audio === undefined) {
errors.push(`For ${i} phrase field 'audio' is undefined`); errors.push(`For ${i} phrase field 'audio' is undefined`);
continue; continue;
} }
const audioFile = path.join(folder, phrase.audio); const audioFile = path.join(folder, phrase.audio);
if (!fs.existsSync(audioFile)) { if (!fs.existsSync(audioFile)) {
errors.push(`For ${i} phrase "${phrase.phrase}" file not found`); errors.push(`For ${i} phrase '${phrase.phrase}' file not found`);
continue; continue;
} }
phrase.voice = { ...pack.voice, ...phrase.voice }; phrase.voice = { ...pack.voice, ...phrase.voice };
const resourceKey = this.getResourceKey(phrase.phrase, phrase.voice ?? {}); const resourceKey = this.getResourceKey(phrase.phrase, phrase.voice ?? {});
if (resourceKey in this.resources) { if (resourceKey in this.resources) {
// дубликат - не ошибка // дубликат - не ошибка
console.warn("Skip", i, "phrase because it's duplicate"); console.warn("Skip", i, "phrase because it's duplicate");
continue; continue;
} }
this.resources[resourceKey] = audioFile; this.resources[resourceKey] = audioFile;
}
if(errors.length > 0)
{
console.error(`Was errors ${JSON.stringify(errors)}`)
}
} }
if (errors.length > 0)
async addFolder(folder)
{ {
const files = fs.readdirSync(folder); console.error(`Was errors: ${JSON.stringify(errors, undefined, 2)}`);
for (const fileName of files) return false;
{
if (path.extname(fileName) === ".json")
{
const fname = path.join(folder, fileName);
console.log(`Parsing ${fname}`);
await this.appendJson(folder, JSON.parse(fs.readFileSync(fname).toString()));
}
}
} }
return true;
}
GetPath(text, voiceInfo) async addFolder(folder)
{
const files = fs.readdirSync(folder);
let result = false;
for (const fileName of files)
{ {
const key = this.getResourceKey(text, voiceInfo); if (path.extname(fileName) === ".json")
const fpath = this.resources[key]; {
if (fpath === undefined) const fname = path.join(folder, fileName);
throw new Error(`Failed to get ${key}`); console.log(`Parsing ${fname}`);
return fpath; result = await this.appendJson(folder, JSON.parse(fs.readFileSync(fname).toString()));
}
} }
return result;
}
GetPath(text, voiceInfo)
{
const key = this.getResourceKey(text, voiceInfo);
const fpath = this.resources[key];
if (fpath === undefined)
throw new Error(`Failed to get ${key}`);
return fpath;
}
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!