feat: add TTS module
This commit is contained in:
@@ -0,0 +1,74 @@
|
|||||||
|
import path from 'path';
|
||||||
|
import fs from 'fs';
|
||||||
|
|
||||||
|
import { Logger } from '../utils/log';
|
||||||
|
import { isModule } from '../utils/misc';
|
||||||
|
|
||||||
|
export interface TTSResponse {
|
||||||
|
error?: string;
|
||||||
|
data?: Buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface TTSModule {
|
||||||
|
name: string;
|
||||||
|
getVoices: () => Promise<Array<string> | undefined>;
|
||||||
|
generate: (voice: string, text: string) => Promise<TTSResponse>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class TTSManager {
|
||||||
|
private readonly modules: Array<TTSModule> = [];
|
||||||
|
private readonly log: Logger;
|
||||||
|
|
||||||
|
private constructor() {
|
||||||
|
this.log = new Logger('TTS Manager');
|
||||||
|
const modesFolder = path.resolve(__dirname, './tts-modes');
|
||||||
|
|
||||||
|
fs.readdirSync(modesFolder).map(
|
||||||
|
async (file) => await this.loadModule(path.join(modesFolder, file))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
public
|
||||||
|
*/
|
||||||
|
public getModule(name: string): TTSModule | undefined {
|
||||||
|
return this.modules.find((mod) => mod.name == name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public getModules(): TTSModule[] {
|
||||||
|
return this.modules;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
internal
|
||||||
|
*/
|
||||||
|
private async loadModule(filePath: string): Promise<void> {
|
||||||
|
try {
|
||||||
|
if (!isModule(filePath)) return;
|
||||||
|
|
||||||
|
const modRaw = await import(`file://${filePath}`);
|
||||||
|
|
||||||
|
if (!modRaw || !modRaw.default) {
|
||||||
|
this.log.warning('Invalid module format in %s', filePath);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const mod = modRaw.default as TTSModule;
|
||||||
|
|
||||||
|
this.log.verbose(`Loaded TTS mode: ${mod.name}`);
|
||||||
|
this.modules.push(mod);
|
||||||
|
} catch (err) {
|
||||||
|
this.log.error('Failed to load TTS Module at %s (%s)', filePath, err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
singleton logic
|
||||||
|
*/
|
||||||
|
static #instance: TTSManager | null = null;
|
||||||
|
|
||||||
|
public static get get(): TTSManager {
|
||||||
|
if (!TTSManager.#instance) TTSManager.#instance = new TTSManager();
|
||||||
|
return TTSManager.#instance;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user