csukuangfj
commited on
Commit
·
63302c5
1
Parent(s):
90becda
update model
Browse files- app-tts.js +1 -1
- sherpa-onnx-tts.js +96 -68
- sherpa-onnx-wasm-main-tts.js +0 -0
- sherpa-onnx-wasm-main-tts.wasm +2 -2
app-tts.js
CHANGED
@@ -22,7 +22,7 @@ Module.onRuntimeInitialized = function() {
|
|
22 |
console.log('Model files downloaded!');
|
23 |
|
24 |
console.log('Initializing tts ......');
|
25 |
-
tts =
|
26 |
if (tts.numSpeakers > 1) {
|
27 |
speakerIdLabel.innerHTML = `Speaker ID (0 - ${tts.numSpeakers - 1}):`;
|
28 |
}
|
|
|
22 |
console.log('Model files downloaded!');
|
23 |
|
24 |
console.log('Initializing tts ......');
|
25 |
+
tts = createOfflineTts(Module)
|
26 |
if (tts.numSpeakers > 1) {
|
27 |
speakerIdLabel.innerHTML = `Speaker ID (0 - ${tts.numSpeakers - 1}):`;
|
28 |
}
|
sherpa-onnx-tts.js
CHANGED
@@ -1,109 +1,109 @@
|
|
1 |
|
2 |
-
function freeConfig(config) {
|
3 |
if ('buffer' in config) {
|
4 |
-
_free(config.buffer);
|
5 |
}
|
6 |
|
7 |
if ('config' in config) {
|
8 |
-
freeConfig(config.config)
|
9 |
}
|
10 |
|
11 |
-
_free(config.ptr);
|
12 |
}
|
13 |
|
14 |
// The user should free the returned pointers
|
15 |
-
function initSherpaOnnxOfflineTtsVitsModelConfig(config) {
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
|
21 |
-
|
22 |
|
23 |
-
|
24 |
|
25 |
-
|
26 |
-
|
27 |
|
28 |
let offset = 0;
|
29 |
-
stringToUTF8(config.model, buffer + offset, modelLen);
|
30 |
offset += modelLen;
|
31 |
|
32 |
-
stringToUTF8(config.lexicon, buffer + offset, lexiconLen);
|
33 |
offset += lexiconLen;
|
34 |
|
35 |
-
stringToUTF8(config.tokens, buffer + offset, tokensLen);
|
36 |
offset += tokensLen;
|
37 |
|
38 |
-
stringToUTF8(config.dataDir, buffer + offset, dataDirLen);
|
39 |
offset += dataDirLen;
|
40 |
|
41 |
offset = 0;
|
42 |
-
setValue(ptr, buffer + offset, 'i8*');
|
43 |
offset += modelLen;
|
44 |
|
45 |
-
setValue(ptr + 4, buffer + offset, 'i8*');
|
46 |
offset += lexiconLen;
|
47 |
|
48 |
-
setValue(ptr + 8, buffer + offset, 'i8*');
|
49 |
offset += tokensLen;
|
50 |
|
51 |
-
setValue(ptr + 12, buffer + offset, 'i8*');
|
52 |
offset += dataDirLen;
|
53 |
|
54 |
-
setValue(ptr + 16, config.noiseScale, 'float');
|
55 |
-
setValue(ptr + 20, config.noiseScaleW, 'float');
|
56 |
-
setValue(ptr + 24, config.lengthScale, 'float');
|
57 |
|
58 |
return {
|
59 |
buffer: buffer, ptr: ptr, len: len,
|
60 |
}
|
61 |
}
|
62 |
|
63 |
-
function initSherpaOnnxOfflineTtsModelConfig(config) {
|
64 |
-
|
65 |
-
|
66 |
|
67 |
-
|
68 |
-
|
69 |
|
70 |
let offset = 0;
|
71 |
-
_CopyHeap(vitsModelConfig.ptr, vitsModelConfig.len, ptr + offset);
|
72 |
offset += vitsModelConfig.len;
|
73 |
|
74 |
-
setValue(ptr + offset, config.numThreads, 'i32');
|
75 |
offset += 4;
|
76 |
|
77 |
-
setValue(ptr + offset, config.debug, 'i32');
|
78 |
offset += 4;
|
79 |
|
80 |
-
|
81 |
-
|
82 |
-
stringToUTF8(config.provider, buffer, providerLen);
|
83 |
-
setValue(ptr + offset, buffer, 'i8*');
|
84 |
|
85 |
return {
|
86 |
buffer: buffer, ptr: ptr, len: len, config: vitsModelConfig,
|
87 |
}
|
88 |
}
|
89 |
|
90 |
-
function initSherpaOnnxOfflineTtsConfig(config) {
|
91 |
-
|
92 |
-
initSherpaOnnxOfflineTtsModelConfig(config.offlineTtsModelConfig);
|
93 |
-
|
94 |
-
|
95 |
|
96 |
let offset = 0;
|
97 |
-
_CopyHeap(modelConfig.ptr, modelConfig.len, ptr + offset);
|
98 |
offset += modelConfig.len;
|
99 |
|
100 |
-
|
101 |
-
|
102 |
-
stringToUTF8(config.ruleFsts, buffer, ruleFstsLen);
|
103 |
-
setValue(ptr + offset, buffer, 'i8*');
|
104 |
offset += 4;
|
105 |
|
106 |
-
setValue(ptr + offset, config.maxNumSentences, 'i32');
|
107 |
|
108 |
return {
|
109 |
buffer: buffer, ptr: ptr, len: len, config: modelConfig,
|
@@ -111,19 +111,21 @@ function initSherpaOnnxOfflineTtsConfig(config) {
|
|
111 |
}
|
112 |
|
113 |
class OfflineTts {
|
114 |
-
constructor(configObj) {
|
115 |
-
|
116 |
-
|
|
|
117 |
|
118 |
-
freeConfig(config);
|
119 |
|
120 |
this.handle = handle;
|
121 |
-
this.sampleRate = _SherpaOnnxOfflineTtsSampleRate(this.handle);
|
122 |
-
this.numSpeakers = _SherpaOnnxOfflineTtsNumSpeakers(this.handle);
|
|
|
123 |
}
|
124 |
|
125 |
free() {
|
126 |
-
_SherpaOnnxDestroyOfflineTts(this.handle);
|
127 |
this.handle = 0
|
128 |
}
|
129 |
|
@@ -133,29 +135,44 @@ class OfflineTts {
|
|
133 |
// speed: 1.0
|
134 |
// }
|
135 |
generate(config) {
|
136 |
-
|
137 |
-
|
138 |
-
stringToUTF8(config.text, textPtr, textLen);
|
139 |
|
140 |
-
|
141 |
this.handle, textPtr, config.sid, config.speed);
|
142 |
|
143 |
-
|
144 |
-
|
145 |
|
146 |
-
|
147 |
-
|
148 |
for (let i = 0; i < numSamples; i++) {
|
149 |
-
samples[i] = HEAPF32[samplesPtr + i];
|
150 |
}
|
151 |
|
152 |
-
_SherpaOnnxDestroyOfflineTtsGeneratedAudio(h);
|
153 |
return {samples: samples, sampleRate: sampleRate};
|
154 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
155 |
}
|
156 |
|
157 |
-
function
|
158 |
-
|
159 |
model: './model.onnx',
|
160 |
lexicon: '',
|
161 |
tokens: './tokens.txt',
|
@@ -164,7 +181,7 @@ function initSherpaOnnxOfflineTts() {
|
|
164 |
noiseScaleW: 0.8,
|
165 |
lengthScale: 1.0,
|
166 |
};
|
167 |
-
|
168 |
offlineTtsVitsModelConfig: offlineTtsVitsModelConfig,
|
169 |
numThreads: 1,
|
170 |
debug: 1,
|
@@ -176,5 +193,16 @@ function initSherpaOnnxOfflineTts() {
|
|
176 |
maxNumSentences: 1,
|
177 |
}
|
178 |
|
179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
}
|
|
|
1 |
|
2 |
+
function freeConfig(config, Module) {
|
3 |
if ('buffer' in config) {
|
4 |
+
Module._free(config.buffer);
|
5 |
}
|
6 |
|
7 |
if ('config' in config) {
|
8 |
+
freeConfig(config.config, Module)
|
9 |
}
|
10 |
|
11 |
+
Module._free(config.ptr);
|
12 |
}
|
13 |
|
14 |
// The user should free the returned pointers
|
15 |
+
function initSherpaOnnxOfflineTtsVitsModelConfig(config, Module) {
|
16 |
+
const modelLen = Module.lengthBytesUTF8(config.model) + 1;
|
17 |
+
const lexiconLen = Module.lengthBytesUTF8(config.lexicon) + 1;
|
18 |
+
const tokensLen = Module.lengthBytesUTF8(config.tokens) + 1;
|
19 |
+
const dataDirLen = Module.lengthBytesUTF8(config.dataDir) + 1;
|
20 |
|
21 |
+
const n = modelLen + lexiconLen + tokensLen + dataDirLen;
|
22 |
|
23 |
+
const buffer = Module._malloc(n);
|
24 |
|
25 |
+
const len = 7 * 4;
|
26 |
+
const ptr = Module._malloc(len);
|
27 |
|
28 |
let offset = 0;
|
29 |
+
Module.stringToUTF8(config.model, buffer + offset, modelLen);
|
30 |
offset += modelLen;
|
31 |
|
32 |
+
Module.stringToUTF8(config.lexicon, buffer + offset, lexiconLen);
|
33 |
offset += lexiconLen;
|
34 |
|
35 |
+
Module.stringToUTF8(config.tokens, buffer + offset, tokensLen);
|
36 |
offset += tokensLen;
|
37 |
|
38 |
+
Module.stringToUTF8(config.dataDir, buffer + offset, dataDirLen);
|
39 |
offset += dataDirLen;
|
40 |
|
41 |
offset = 0;
|
42 |
+
Module.setValue(ptr, buffer + offset, 'i8*');
|
43 |
offset += modelLen;
|
44 |
|
45 |
+
Module.setValue(ptr + 4, buffer + offset, 'i8*');
|
46 |
offset += lexiconLen;
|
47 |
|
48 |
+
Module.setValue(ptr + 8, buffer + offset, 'i8*');
|
49 |
offset += tokensLen;
|
50 |
|
51 |
+
Module.setValue(ptr + 12, buffer + offset, 'i8*');
|
52 |
offset += dataDirLen;
|
53 |
|
54 |
+
Module.setValue(ptr + 16, config.noiseScale, 'float');
|
55 |
+
Module.setValue(ptr + 20, config.noiseScaleW, 'float');
|
56 |
+
Module.setValue(ptr + 24, config.lengthScale, 'float');
|
57 |
|
58 |
return {
|
59 |
buffer: buffer, ptr: ptr, len: len,
|
60 |
}
|
61 |
}
|
62 |
|
63 |
+
function initSherpaOnnxOfflineTtsModelConfig(config, Module) {
|
64 |
+
const vitsModelConfig = initSherpaOnnxOfflineTtsVitsModelConfig(
|
65 |
+
config.offlineTtsVitsModelConfig, Module);
|
66 |
|
67 |
+
const len = vitsModelConfig.len + 3 * 4;
|
68 |
+
const ptr = Module._malloc(len);
|
69 |
|
70 |
let offset = 0;
|
71 |
+
Module._CopyHeap(vitsModelConfig.ptr, vitsModelConfig.len, ptr + offset);
|
72 |
offset += vitsModelConfig.len;
|
73 |
|
74 |
+
Module.setValue(ptr + offset, config.numThreads, 'i32');
|
75 |
offset += 4;
|
76 |
|
77 |
+
Module.setValue(ptr + offset, config.debug, 'i32');
|
78 |
offset += 4;
|
79 |
|
80 |
+
const providerLen = Module.lengthBytesUTF8(config.provider) + 1;
|
81 |
+
const buffer = Module._malloc(providerLen);
|
82 |
+
Module.stringToUTF8(config.provider, buffer, providerLen);
|
83 |
+
Module.setValue(ptr + offset, buffer, 'i8*');
|
84 |
|
85 |
return {
|
86 |
buffer: buffer, ptr: ptr, len: len, config: vitsModelConfig,
|
87 |
}
|
88 |
}
|
89 |
|
90 |
+
function initSherpaOnnxOfflineTtsConfig(config, Module) {
|
91 |
+
const modelConfig =
|
92 |
+
initSherpaOnnxOfflineTtsModelConfig(config.offlineTtsModelConfig, Module);
|
93 |
+
const len = modelConfig.len + 2 * 4;
|
94 |
+
const ptr = Module._malloc(len);
|
95 |
|
96 |
let offset = 0;
|
97 |
+
Module._CopyHeap(modelConfig.ptr, modelConfig.len, ptr + offset);
|
98 |
offset += modelConfig.len;
|
99 |
|
100 |
+
const ruleFstsLen = Module.lengthBytesUTF8(config.ruleFsts) + 1;
|
101 |
+
const buffer = Module._malloc(ruleFstsLen);
|
102 |
+
Module.stringToUTF8(config.ruleFsts, buffer, ruleFstsLen);
|
103 |
+
Module.setValue(ptr + offset, buffer, 'i8*');
|
104 |
offset += 4;
|
105 |
|
106 |
+
Module.setValue(ptr + offset, config.maxNumSentences, 'i32');
|
107 |
|
108 |
return {
|
109 |
buffer: buffer, ptr: ptr, len: len, config: modelConfig,
|
|
|
111 |
}
|
112 |
|
113 |
class OfflineTts {
|
114 |
+
constructor(configObj, Module) {
|
115 |
+
console.log(configObj)
|
116 |
+
const config = initSherpaOnnxOfflineTtsConfig(configObj, Module)
|
117 |
+
const handle = Module._SherpaOnnxCreateOfflineTts(config.ptr);
|
118 |
|
119 |
+
freeConfig(config, Module);
|
120 |
|
121 |
this.handle = handle;
|
122 |
+
this.sampleRate = Module._SherpaOnnxOfflineTtsSampleRate(this.handle);
|
123 |
+
this.numSpeakers = Module._SherpaOnnxOfflineTtsNumSpeakers(this.handle);
|
124 |
+
this.Module = Module
|
125 |
}
|
126 |
|
127 |
free() {
|
128 |
+
this.Module._SherpaOnnxDestroyOfflineTts(this.handle);
|
129 |
this.handle = 0
|
130 |
}
|
131 |
|
|
|
135 |
// speed: 1.0
|
136 |
// }
|
137 |
generate(config) {
|
138 |
+
const textLen = this.Module.lengthBytesUTF8(config.text) + 1;
|
139 |
+
const textPtr = this.Module._malloc(textLen);
|
140 |
+
this.Module.stringToUTF8(config.text, textPtr, textLen);
|
141 |
|
142 |
+
const h = this.Module._SherpaOnnxOfflineTtsGenerate(
|
143 |
this.handle, textPtr, config.sid, config.speed);
|
144 |
|
145 |
+
const numSamples = this.Module.HEAP32[h / 4 + 1];
|
146 |
+
const sampleRate = this.Module.HEAP32[h / 4 + 2];
|
147 |
|
148 |
+
const samplesPtr = this.Module.HEAP32[h / 4] / 4;
|
149 |
+
const samples = new Float32Array(numSamples);
|
150 |
for (let i = 0; i < numSamples; i++) {
|
151 |
+
samples[i] = this.Module.HEAPF32[samplesPtr + i];
|
152 |
}
|
153 |
|
154 |
+
this.Module._SherpaOnnxDestroyOfflineTtsGeneratedAudio(h);
|
155 |
return {samples: samples, sampleRate: sampleRate};
|
156 |
}
|
157 |
+
save(filename, audio) {
|
158 |
+
const samples = audio.samples;
|
159 |
+
const sampleRate = audio.sampleRate;
|
160 |
+
const ptr = this.Module._malloc(samples.length * 4);
|
161 |
+
for (let i = 0; i < samples.length; i++) {
|
162 |
+
this.Module.HEAPF32[ptr / 4 + i] = samples[i];
|
163 |
+
}
|
164 |
+
|
165 |
+
const filenameLen = this.Module.lengthBytesUTF8(filename) + 1;
|
166 |
+
const buffer = this.Module._malloc(filenameLen);
|
167 |
+
this.Module.stringToUTF8(filename, buffer, filenameLen);
|
168 |
+
this.Module._SherpaOnnxWriteWave(ptr, samples.length, sampleRate, buffer);
|
169 |
+
this.Module._free(buffer);
|
170 |
+
this.Module._free(ptr);
|
171 |
+
}
|
172 |
}
|
173 |
|
174 |
+
function createOfflineTts(Module, myConfig) {
|
175 |
+
const offlineTtsVitsModelConfig = {
|
176 |
model: './model.onnx',
|
177 |
lexicon: '',
|
178 |
tokens: './tokens.txt',
|
|
|
181 |
noiseScaleW: 0.8,
|
182 |
lengthScale: 1.0,
|
183 |
};
|
184 |
+
const offlineTtsModelConfig = {
|
185 |
offlineTtsVitsModelConfig: offlineTtsVitsModelConfig,
|
186 |
numThreads: 1,
|
187 |
debug: 1,
|
|
|
193 |
maxNumSentences: 1,
|
194 |
}
|
195 |
|
196 |
+
if (myConfig) {
|
197 |
+
offlineTtsConfig = myConfig;
|
198 |
+
}
|
199 |
+
|
200 |
+
return new OfflineTts(offlineTtsConfig, Module);
|
201 |
+
}
|
202 |
+
|
203 |
+
if (typeof process == 'object' && typeof process.versions == 'object' &&
|
204 |
+
typeof process.versions.node == 'string') {
|
205 |
+
module.exports = {
|
206 |
+
createOfflineTts,
|
207 |
+
};
|
208 |
}
|
sherpa-onnx-wasm-main-tts.js
CHANGED
The diff for this file is too large to render.
See raw diff
|
|
sherpa-onnx-wasm-main-tts.wasm
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
-
size
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:546b1483cc66701fea8ddfd5e3cae9774c0db653f85b2410f41aa84e39d37814
|
3 |
+
size 11442235
|