Commit ·
e1364a2
1
Parent(s): d0d0f2d
update model
Browse files- app-tts.js +20 -3
- index.html +3 -2
- sherpa-onnx-tts.worker.js +82 -0
app-tts.js
CHANGED
|
@@ -10,13 +10,14 @@ const speedInput = document.getElementById('speed');
|
|
| 10 |
const speedValue = document.getElementById('speedValue');
|
| 11 |
const textArea = document.getElementById('text');
|
| 12 |
const soundClips = document.getElementById('sound-clips');
|
|
|
|
| 13 |
|
| 14 |
speedValue.innerHTML = speedInput.value;
|
| 15 |
|
| 16 |
let index = 0;
|
| 17 |
|
| 18 |
let audioCtx = null;
|
| 19 |
-
const worker = new Worker("
|
| 20 |
let ttsInstanceInfo = {
|
| 21 |
modelType: 0,
|
| 22 |
numSpeakers: 0,
|
|
@@ -25,6 +26,7 @@ let ttsInstanceInfo = {
|
|
| 25 |
worker.onmessage = (e) => {
|
| 26 |
if (e.data.type === "sherpa-onnx-tts-progress") {
|
| 27 |
Module.setStatus(e.data.status);
|
|
|
|
| 28 |
}
|
| 29 |
if (e.data.type === "sherpa-onnx-tts-ready") {
|
| 30 |
ttsInstanceInfo.modelType = e.data.modelType ?? 0;
|
|
@@ -33,6 +35,11 @@ worker.onmessage = (e) => {
|
|
| 33 |
generateBtn.disabled = false;
|
| 34 |
speakerIdLabel.innerHTML = `Speaker ID (0 - ${e.data.numSpeakers - 1}):`;
|
| 35 |
updateUiForModelType();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
return;
|
| 37 |
}
|
| 38 |
if (e.data.type === "sherpa-onnx-tts-result") {
|
|
@@ -65,7 +72,6 @@ Module = {};
|
|
| 65 |
// https://emscripten.org/docs/api_reference/module.html#Module.locateFile
|
| 66 |
Module.setStatus = function(status) {
|
| 67 |
console.log(`status ${status}`);
|
| 68 |
-
const statusElement = document.getElementById('status');
|
| 69 |
if (status == 'Running...') {
|
| 70 |
status = 'Model downloaded. Initializing text to speech model...'
|
| 71 |
}
|
|
@@ -141,6 +147,11 @@ async function readReferenceAudio(file) {
|
|
| 141 |
}
|
| 142 |
}
|
| 143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
generateBtn.onclick = async function() {
|
| 145 |
const isZipVoice = ttsInstanceInfo.modelType === 4;
|
| 146 |
|
|
@@ -180,13 +191,19 @@ generateBtn.onclick = async function() {
|
|
| 180 |
return;
|
| 181 |
}
|
| 182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
const referenceText = referenceTextInput.value.trim();
|
| 184 |
if (referenceText.length === 0) {
|
| 185 |
alert('Please input the reference text');
|
| 186 |
return;
|
| 187 |
}
|
| 188 |
|
| 189 |
-
const referenceAudio = await readReferenceAudio(
|
| 190 |
const genConfig = {
|
| 191 |
speed: parseFloat(speedInput.value),
|
| 192 |
referenceAudio: referenceAudio.samples,
|
|
|
|
| 10 |
const speedValue = document.getElementById('speedValue');
|
| 11 |
const textArea = document.getElementById('text');
|
| 12 |
const soundClips = document.getElementById('sound-clips');
|
| 13 |
+
const statusElement = document.getElementById('status');
|
| 14 |
|
| 15 |
speedValue.innerHTML = speedInput.value;
|
| 16 |
|
| 17 |
let index = 0;
|
| 18 |
|
| 19 |
let audioCtx = null;
|
| 20 |
+
const worker = new Worker("sherpa-onnx-tts.worker.js");
|
| 21 |
let ttsInstanceInfo = {
|
| 22 |
modelType: 0,
|
| 23 |
numSpeakers: 0,
|
|
|
|
| 26 |
worker.onmessage = (e) => {
|
| 27 |
if (e.data.type === "sherpa-onnx-tts-progress") {
|
| 28 |
Module.setStatus(e.data.status);
|
| 29 |
+
return;
|
| 30 |
}
|
| 31 |
if (e.data.type === "sherpa-onnx-tts-ready") {
|
| 32 |
ttsInstanceInfo.modelType = e.data.modelType ?? 0;
|
|
|
|
| 35 |
generateBtn.disabled = false;
|
| 36 |
speakerIdLabel.innerHTML = `Speaker ID (0 - ${e.data.numSpeakers - 1}):`;
|
| 37 |
updateUiForModelType();
|
| 38 |
+
Module.setStatus('');
|
| 39 |
+
return;
|
| 40 |
+
}
|
| 41 |
+
if (e.data.type === "error") {
|
| 42 |
+
Module.setStatus(e.data.message);
|
| 43 |
return;
|
| 44 |
}
|
| 45 |
if (e.data.type === "sherpa-onnx-tts-result") {
|
|
|
|
| 72 |
// https://emscripten.org/docs/api_reference/module.html#Module.locateFile
|
| 73 |
Module.setStatus = function(status) {
|
| 74 |
console.log(`status ${status}`);
|
|
|
|
| 75 |
if (status == 'Running...') {
|
| 76 |
status = 'Model downloaded. Initializing text to speech model...'
|
| 77 |
}
|
|
|
|
| 147 |
}
|
| 148 |
}
|
| 149 |
|
| 150 |
+
function isWaveFile(file) {
|
| 151 |
+
const name = file.name || '';
|
| 152 |
+
return name.toLowerCase().endsWith('.wav');
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
generateBtn.onclick = async function() {
|
| 156 |
const isZipVoice = ttsInstanceInfo.modelType === 4;
|
| 157 |
|
|
|
|
| 191 |
return;
|
| 192 |
}
|
| 193 |
|
| 194 |
+
const referenceFile = referenceAudioInput.files[0];
|
| 195 |
+
if (!isWaveFile(referenceFile)) {
|
| 196 |
+
alert('Please select a .wav reference audio file');
|
| 197 |
+
return;
|
| 198 |
+
}
|
| 199 |
+
|
| 200 |
const referenceText = referenceTextInput.value.trim();
|
| 201 |
if (referenceText.length === 0) {
|
| 202 |
alert('Please input the reference text');
|
| 203 |
return;
|
| 204 |
}
|
| 205 |
|
| 206 |
+
const referenceAudio = await readReferenceAudio(referenceFile);
|
| 207 |
const genConfig = {
|
| 208 |
speed: parseFloat(speedInput.value),
|
| 209 |
referenceAudio: referenceAudio.samples,
|
index.html
CHANGED
|
@@ -37,8 +37,9 @@
|
|
| 37 |
<br/>
|
| 38 |
</div>
|
| 39 |
<div id="referenceAudioSection" class="hidden">
|
| 40 |
-
<label for="referenceAudio">Reference audio: </label>
|
| 41 |
-
<input type="file" id="referenceAudio" name="referenceAudio" accept="audio/
|
|
|
|
| 42 |
<br/>
|
| 43 |
<br/>
|
| 44 |
</div>
|
|
|
|
| 37 |
<br/>
|
| 38 |
</div>
|
| 39 |
<div id="referenceAudioSection" class="hidden">
|
| 40 |
+
<label for="referenceAudio">Reference audio (.wav): </label>
|
| 41 |
+
<input type="file" id="referenceAudio" name="referenceAudio" accept=".wav,audio/wav" />
|
| 42 |
+
<div style="font-size: 0.9rem; color: #6c757d;">Only `.wav` files are supported.</div>
|
| 43 |
<br/>
|
| 44 |
<br/>
|
| 45 |
</div>
|
sherpa-onnx-tts.worker.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
let tts = null;
|
| 2 |
+
self.Module = {
|
| 3 |
+
// https://emscripten.org/docs/api_reference/module.html#Module.locateFile
|
| 4 |
+
locateFile: function (path, scriptDirectory = "") {
|
| 5 |
+
return scriptDirectory + path;
|
| 6 |
+
},
|
| 7 |
+
// https://emscripten.org/docs/api_reference/module.html#Module.locateFile
|
| 8 |
+
setStatus: function (status) {
|
| 9 |
+
self.postMessage({ type: "sherpa-onnx-tts-progress", status });
|
| 10 |
+
},
|
| 11 |
+
onRuntimeInitialized: function () {
|
| 12 |
+
console.log("Model files downloaded!");
|
| 13 |
+
console.log("Initializing tts ......");
|
| 14 |
+
try {
|
| 15 |
+
tts = createOfflineTts(self.Module);
|
| 16 |
+
self.postMessage({
|
| 17 |
+
type: "sherpa-onnx-tts-ready",
|
| 18 |
+
modelType: getDefaultOfflineTtsModelType(),
|
| 19 |
+
numSpeakers: tts.numSpeakers,
|
| 20 |
+
});
|
| 21 |
+
} catch (e) {
|
| 22 |
+
self.postMessage({
|
| 23 |
+
type: "error",
|
| 24 |
+
message: "TTS Initialization failed: " + e.message,
|
| 25 |
+
});
|
| 26 |
+
}
|
| 27 |
+
},
|
| 28 |
+
};
|
| 29 |
+
importScripts("sherpa-onnx-wasm-main-tts.js");
|
| 30 |
+
importScripts("sherpa-onnx-tts.js");
|
| 31 |
+
self.onmessage = async (e) => {
|
| 32 |
+
const { type, text, sid, speed, genConfig } = e.data;
|
| 33 |
+
if (type === "generate") {
|
| 34 |
+
if (!tts) {
|
| 35 |
+
return;
|
| 36 |
+
}
|
| 37 |
+
try {
|
| 38 |
+
const audio = tts.generate({
|
| 39 |
+
text: text,
|
| 40 |
+
sid: sid || 0,
|
| 41 |
+
speed: speed || 1.0,
|
| 42 |
+
});
|
| 43 |
+
const samples = audio.samples;
|
| 44 |
+
const sampleRate = tts.sampleRate;
|
| 45 |
+
self.postMessage(
|
| 46 |
+
{
|
| 47 |
+
type: "sherpa-onnx-tts-result",
|
| 48 |
+
samples: samples,
|
| 49 |
+
sampleRate: sampleRate,
|
| 50 |
+
},
|
| 51 |
+
[samples.buffer],
|
| 52 |
+
);
|
| 53 |
+
} catch (err) {
|
| 54 |
+
self.postMessage({
|
| 55 |
+
type: "error",
|
| 56 |
+
message: "Generation failed: " + err.message,
|
| 57 |
+
});
|
| 58 |
+
}
|
| 59 |
+
} else if (type === "generateWithConfig") {
|
| 60 |
+
if (!tts) {
|
| 61 |
+
return;
|
| 62 |
+
}
|
| 63 |
+
try {
|
| 64 |
+
const audio = tts.generateWithConfig(text, genConfig || {});
|
| 65 |
+
const samples = audio.samples;
|
| 66 |
+
const sampleRate = audio.sampleRate;
|
| 67 |
+
self.postMessage(
|
| 68 |
+
{
|
| 69 |
+
type: "sherpa-onnx-tts-result",
|
| 70 |
+
samples: samples,
|
| 71 |
+
sampleRate: sampleRate,
|
| 72 |
+
},
|
| 73 |
+
[samples.buffer],
|
| 74 |
+
);
|
| 75 |
+
} catch (err) {
|
| 76 |
+
self.postMessage({
|
| 77 |
+
type: "error",
|
| 78 |
+
message: "Generation failed: " + err.message,
|
| 79 |
+
});
|
| 80 |
+
}
|
| 81 |
+
}
|
| 82 |
+
};
|