Add JavaScript API (WebAssembly) for Kokoro TTS 1.0 (#1809)

This commit is contained in:
Fangjun Kuang
2025-02-07 16:46:03 +08:00
committed by GitHub
parent 19513af0ae
commit 0610679539
4 changed files with 73 additions and 3 deletions

View File

@@ -141,12 +141,15 @@ function initSherpaOnnxOfflineTtsKokoroModelConfig(config, Module) {
const voicesLen = Module.lengthBytesUTF8(config.voices) + 1;
const tokensLen = Module.lengthBytesUTF8(config.tokens || '') + 1;
const dataDirLen = Module.lengthBytesUTF8(config.dataDir || '') + 1;
const dictDirLen = Module.lengthBytesUTF8(config.dictDir || '') + 1;
const lexiconLen = Module.lengthBytesUTF8(config.lexicon || '') + 1;
const n = modelLen + voicesLen + tokensLen + dataDirLen;
const n =
modelLen + voicesLen + tokensLen + dataDirLen + dictDirLen + lexiconLen;
const buffer = Module._malloc(n);
const len = 5 * 4;
const len = 7 * 4;
const ptr = Module._malloc(len);
let offset = 0;
@@ -162,6 +165,12 @@ function initSherpaOnnxOfflineTtsKokoroModelConfig(config, Module) {
Module.stringToUTF8(config.dataDir || '', buffer + offset, dataDirLen);
offset += dataDirLen;
Module.stringToUTF8(config.dictDir || '', buffer + offset, dictDirLen);
offset += dictDirLen;
Module.stringToUTF8(config.lexicon || '', buffer + offset, lexiconLen);
offset += lexiconLen;
offset = 0;
Module.setValue(ptr, buffer + offset, 'i8*');
offset += modelLen;
@@ -177,6 +186,12 @@ function initSherpaOnnxOfflineTtsKokoroModelConfig(config, Module) {
Module.setValue(ptr + 16, config.lengthScale || 1.0, 'float');
Module.setValue(ptr + 20, buffer + offset, 'i8*');
offset += dictDirLen;
Module.setValue(ptr + 24, buffer + offset, 'i8*');
offset += lexiconLen;
return {
buffer: buffer, ptr: ptr, len: len,
}
@@ -216,6 +231,8 @@ function initSherpaOnnxOfflineTtsModelConfig(config, Module) {
tokens: '',
lengthScale: 1.0,
dataDir: '',
dictDir: '',
lexicon: '',
};
}
@@ -382,6 +399,8 @@ function createOfflineTts(Module, myConfig) {
tokens: '',
dataDir: '',
lengthScale: 1.0,
dictDir: '',
lexicon: '',
};
const offlineTtsModelConfig = {