@echogarden/gnuspeech-wasm
Version:
WebAssembly port of the GnuSpeech speech synthesizer.
44 lines • 2.1 kB
JavaScript
import { wrapEmscriptenModuleHeap } from 'wasm-heap-manager';
import { defaultTrmControlModelFileContent } from './DefaultTrmControlModel.js';
export async function synthesize(text, options) {
options = { ...defaultGnuSpeechOptions, ...options };
let modifiedConfigFileContent = defaultTrmControlModelFileContent;
modifiedConfigFileContent = modifiedConfigFileContent.replace('voice_name = male', `voice_name = ${options.voice}`);
modifiedConfigFileContent = modifiedConfigFileContent.replace('tempo = 1.0', `tempo = ${options.tempo}`);
modifiedConfigFileContent = modifiedConfigFileContent.replace('control_rate = 250.0', `control_rate = ${options.controlRate}`);
const m = await getWasmModule();
const dataPath = '/data/en';
m.FS.writeFile(`${dataPath}/trm_control_model.txt`, modifiedConfigFileContent);
const manager = wrapEmscriptenModuleHeap(m);
const textRef = manager.allocNullTerminatedUtf8String(text);
const outputFilePath = manager.allocNullTerminatedUtf8String('./out.wav');
const configDirPathRef = manager.allocNullTerminatedUtf8String(dataPath);
const trmParamFilePathRef = manager.allocNullTerminatedUtf8String('./out-params.txt');
m._GnuSpeech_synthesizeToFile(textRef.address, outputFilePath.address, configDirPathRef.address, trmParamFilePathRef.address, options.debug);
const outputFileContent = m.FS.readFile(outputFilePath.value);
const outputParamsFileContent = m.FS.readFile(trmParamFilePathRef.value);
textRef.free();
outputFilePath.free();
configDirPathRef.free();
trmParamFilePathRef.free();
return {
audioData: outputFileContent,
params: outputParamsFileContent
};
}
let wasmModule;
async function getWasmModule() {
if (wasmModule) {
return wasmModule;
}
const { default: initializer } = await import('../wasm/gnuspeech.js');
wasmModule = await initializer();
return wasmModule;
}
export const defaultGnuSpeechOptions = {
voice: 'male',
tempo: 1.0,
controlRate: 250.0,
debug: false,
};
//# sourceMappingURL=GnuSpeech.js.map