File size: 6,662 Bytes
1265f08 63302c5 1265f08 63302c5 1265f08 63302c5 1265f08 63302c5 1265f08 63302c5 5567528 33cbc12 1265f08 63302c5 1265f08 33cbc12 63302c5 1265f08 5567528 1265f08 5567528 1265f08 5567528 1265f08 5567528 1265f08 5567528 33cbc12 1265f08 63302c5 1265f08 63302c5 1265f08 63302c5 1265f08 63302c5 1265f08 5567528 33cbc12 1265f08 63302c5 1265f08 63302c5 1265f08 63302c5 1265f08 5567528 1265f08 5567528 1265f08 5567528 63302c5 1265f08 63302c5 2d83373 63302c5 1265f08 63302c5 1265f08 5567528 2d83373 5567528 2d83373 63302c5 1265f08 5567528 2d83373 1265f08 63302c5 1265f08 63302c5 1265f08 63302c5 1265f08 63302c5 1265f08 63302c5 1265f08 63302c5 1265f08 63302c5 1265f08 63302c5 1265f08 63302c5 1265f08 63302c5 1265f08 63302c5 1265f08 63302c5 1265f08 33cbc12 1265f08 63302c5 1265f08 2d83373 1265f08 63302c5 1265f08 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
function freeConfig(config, Module) {
if ('buffer' in config) {
Module._free(config.buffer);
}
if ('config' in config) {
freeConfig(config.config, Module)
}
Module._free(config.ptr);
}
// The user should free the returned pointers
function initSherpaOnnxOfflineTtsVitsModelConfig(config, Module) {
const modelLen = Module.lengthBytesUTF8(config.model) + 1;
const lexiconLen = Module.lengthBytesUTF8(config.lexicon || '') + 1;
const tokensLen = Module.lengthBytesUTF8(config.tokens || '') + 1;
const dataDirLen = Module.lengthBytesUTF8(config.dataDir || '') + 1;
const dictDirLen = Module.lengthBytesUTF8(config.dictDir || '') + 1;
const n = modelLen + lexiconLen + tokensLen + dataDirLen + dictDirLen;
const buffer = Module._malloc(n);
const len = 8 * 4;
const ptr = Module._malloc(len);
let offset = 0;
Module.stringToUTF8(config.model || '', buffer + offset, modelLen);
offset += modelLen;
Module.stringToUTF8(config.lexicon || '', buffer + offset, lexiconLen);
offset += lexiconLen;
Module.stringToUTF8(config.tokens || '', buffer + offset, tokensLen);
offset += tokensLen;
Module.stringToUTF8(config.dataDir || '', buffer + offset, dataDirLen);
offset += dataDirLen;
Module.stringToUTF8(config.dictDir || '', buffer + offset, dictDirLen);
offset += dictDirLen;
offset = 0;
Module.setValue(ptr, buffer + offset, 'i8*');
offset += modelLen;
Module.setValue(ptr + 4, buffer + offset, 'i8*');
offset += lexiconLen;
Module.setValue(ptr + 8, buffer + offset, 'i8*');
offset += tokensLen;
Module.setValue(ptr + 12, buffer + offset, 'i8*');
offset += dataDirLen;
Module.setValue(ptr + 16, config.noiseScale || 0.667, 'float');
Module.setValue(ptr + 20, config.noiseScaleW || 0.8, 'float');
Module.setValue(ptr + 24, config.lengthScale || 1.0, 'float');
Module.setValue(ptr + 28, buffer + offset, 'i8*');
offset += dictDirLen;
return {
buffer: buffer, ptr: ptr, len: len,
}
}
function initSherpaOnnxOfflineTtsModelConfig(config, Module) {
const vitsModelConfig = initSherpaOnnxOfflineTtsVitsModelConfig(
config.offlineTtsVitsModelConfig, Module);
const len = vitsModelConfig.len + 3 * 4;
const ptr = Module._malloc(len);
let offset = 0;
Module._CopyHeap(vitsModelConfig.ptr, vitsModelConfig.len, ptr + offset);
offset += vitsModelConfig.len;
Module.setValue(ptr + offset, config.numThreads || 1, 'i32');
offset += 4;
Module.setValue(ptr + offset, config.debug || 0, 'i32');
offset += 4;
const providerLen = Module.lengthBytesUTF8(config.provider || 'cpu') + 1;
const buffer = Module._malloc(providerLen);
Module.stringToUTF8(config.provider, buffer, providerLen);
Module.setValue(ptr + offset, buffer, 'i8*');
return {
buffer: buffer, ptr: ptr, len: len, config: vitsModelConfig,
}
}
function initSherpaOnnxOfflineTtsConfig(config, Module) {
const modelConfig =
initSherpaOnnxOfflineTtsModelConfig(config.offlineTtsModelConfig, Module);
const len = modelConfig.len + 3 * 4;
const ptr = Module._malloc(len);
let offset = 0;
Module._CopyHeap(modelConfig.ptr, modelConfig.len, ptr + offset);
offset += modelConfig.len;
const ruleFstsLen = Module.lengthBytesUTF8(config.ruleFsts || '') + 1;
const ruleFarsLen = Module.lengthBytesUTF8(config.ruleFars || '') + 1;
const buffer = Module._malloc(ruleFstsLen + ruleFarsLen);
Module.stringToUTF8(config.ruleFsts || '', buffer, ruleFstsLen);
Module.stringToUTF8(config.ruleFars || '', buffer + ruleFstsLen, ruleFarsLen);
Module.setValue(ptr + offset, buffer, 'i8*');
offset += 4;
Module.setValue(ptr + offset, config.maxNumSentences || 1, 'i32');
offset += 4;
Module.setValue(ptr + offset, buffer + ruleFstsLen, 'i8*');
return {
buffer: buffer, ptr: ptr, len: len, config: modelConfig,
}
}
class OfflineTts {
constructor(configObj, Module) {
console.log(configObj)
const config = initSherpaOnnxOfflineTtsConfig(configObj, Module)
const handle = Module._SherpaOnnxCreateOfflineTts(config.ptr);
freeConfig(config, Module);
this.handle = handle;
this.sampleRate = Module._SherpaOnnxOfflineTtsSampleRate(this.handle);
this.numSpeakers = Module._SherpaOnnxOfflineTtsNumSpeakers(this.handle);
this.Module = Module
}
free() {
this.Module._SherpaOnnxDestroyOfflineTts(this.handle);
this.handle = 0
}
// {
// text: "hello",
// sid: 1,
// speed: 1.0
// }
generate(config) {
const textLen = this.Module.lengthBytesUTF8(config.text) + 1;
const textPtr = this.Module._malloc(textLen);
this.Module.stringToUTF8(config.text, textPtr, textLen);
const h = this.Module._SherpaOnnxOfflineTtsGenerate(
this.handle, textPtr, config.sid, config.speed);
const numSamples = this.Module.HEAP32[h / 4 + 1];
const sampleRate = this.Module.HEAP32[h / 4 + 2];
const samplesPtr = this.Module.HEAP32[h / 4] / 4;
const samples = new Float32Array(numSamples);
for (let i = 0; i < numSamples; i++) {
samples[i] = this.Module.HEAPF32[samplesPtr + i];
}
this.Module._SherpaOnnxDestroyOfflineTtsGeneratedAudio(h);
return {samples: samples, sampleRate: sampleRate};
}
save(filename, audio) {
const samples = audio.samples;
const sampleRate = audio.sampleRate;
const ptr = this.Module._malloc(samples.length * 4);
for (let i = 0; i < samples.length; i++) {
this.Module.HEAPF32[ptr / 4 + i] = samples[i];
}
const filenameLen = this.Module.lengthBytesUTF8(filename) + 1;
const buffer = this.Module._malloc(filenameLen);
this.Module.stringToUTF8(filename, buffer, filenameLen);
this.Module._SherpaOnnxWriteWave(ptr, samples.length, sampleRate, buffer);
this.Module._free(buffer);
this.Module._free(ptr);
}
}
function createOfflineTts(Module, myConfig) {
const offlineTtsVitsModelConfig = {
model: './model.onnx',
lexicon: '',
tokens: './tokens.txt',
dataDir: './espeak-ng-data',
dictDir: '',
noiseScale: 0.667,
noiseScaleW: 0.8,
lengthScale: 1.0,
};
const offlineTtsModelConfig = {
offlineTtsVitsModelConfig: offlineTtsVitsModelConfig,
numThreads: 1,
debug: 1,
provider: 'cpu',
};
let offlineTtsConfig = {
offlineTtsModelConfig: offlineTtsModelConfig,
ruleFsts: '',
ruleFars: '',
maxNumSentences: 1,
}
if (myConfig) {
offlineTtsConfig = myConfig;
}
return new OfflineTts(offlineTtsConfig, Module);
}
if (typeof process == 'object' && typeof process.versions == 'object' &&
typeof process.versions.node == 'string') {
module.exports = {
createOfflineTts,
};
}
|