-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathmain.js
More file actions
177 lines (158 loc) · 6.55 KB
/
Copy pathmain.js
File metadata and controls
177 lines (158 loc) · 6.55 KB
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
import { updateProgress } from "./updateProgress.js";
import { AudioPlayer } from "./AudioPlayer.js";
import { AudioDiskSaver } from "./AudioDiskSaver.js";
import { ButtonHandler } from "./ButtonHandler.js";
if (window.location.hostname === "localhost") {
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("./service-worker.js").then(() => {
console.log("Service Worker registered.");
});
}
}
let tts_worker = new Worker(new URL("./worker.js", import.meta.url), { type: "module" });
let audioPlayer = new AudioPlayer(tts_worker);
let audioDiskSaver = new AudioDiskSaver();
let buttonHandler = new ButtonHandler(tts_worker, audioPlayer, audioDiskSaver);
function populateVoiceSelector(voices) {
const voiceSelector = document.getElementById("voiceSelector");
// Clear any existing options
while (voiceSelector.options.length > 0) {
voiceSelector.remove(0);
}
// Group voices by category (based on prefix) and gender
const voiceGroups = {};
let heartVoice = null;
for (const [id, voice] of Object.entries(voices)) {
if (id === "af_heart") {
heartVoice = { id, name: voice.name, language: voice.language };
continue;
}
const category = id.split('_')[0];
const groupKey = `${category} - ${voice.gender}`;
if (!voiceGroups[groupKey]) {
voiceGroups[groupKey] = [];
}
voiceGroups[groupKey].push({ id, name: voice.name, language: voice.language });
}
// Sort groups alphabetically
const sortedGroups = Object.keys(voiceGroups).sort();
// Add optgroups and options
for (const groupKey of sortedGroups) {
const [category, gender] = groupKey.split(' - ');
const optgroup = document.createElement('optgroup');
optgroup.label = `${gender} Voices (${category.toUpperCase()})`;
// Sort voices within the group by name
voiceGroups[groupKey].sort((a, b) => a.name.localeCompare(b.name));
// If this is the AF Female group, insert Heart at the top
if (category === "af" && gender === "Female" && heartVoice) {
const option = document.createElement('option');
option.value = heartVoice.id;
option.textContent = `${heartVoice.name} (${heartVoice.language})`;
option.selected = true;
optgroup.appendChild(option);
}
for (const voice of voiceGroups[groupKey]) {
const option = document.createElement('option');
option.value = voice.id;
option.textContent = `${voice.name} (${voice.language})`;
// If Heart wasn't found, select the first option
if (!heartVoice && voiceSelector.options.length === 0) {
option.selected = true;
}
optgroup.appendChild(option);
}
voiceSelector.appendChild(optgroup);
}
voiceSelector.disabled = false;
}
const onMessageReceived = async (e) => {switch (e.data.status) {
case "loading_model_start":
console.log(e.data);
updateProgress(0, "Loading model...");
break;
case "loading_model_ready":
buttonHandler.enableButtons();
updateProgress(100, "Model loaded successfully");
// Populate voice selector if voices are available
if (e.data.voices) {
populateVoiceSelector(e.data.voices);
}
break;
case "loading_model_progress":
let progress = Number(e.data.progress) * 100;
if (isNaN(progress)) progress = 0;
updateProgress(progress, `Loading model: ${Math.round(progress)}%`);
break;
case "stream_audio_data":
if (buttonHandler.getMode() === "disk") {
const percent = await audioDiskSaver.addAudioChunk(e.data.audio);
updateProgress(percent, "Processing audio for saving...");
// Update the disk button to the stop state if it's still loading
buttonHandler.updateDiskButtonToStop();
// Notify worker when buffer has been processed in disk mode
tts_worker.postMessage({ type: "buffer_processed" });
} else if (buttonHandler.getMode() === "stream") {
// Update the stream button to the stop state if it's still loading
buttonHandler.updateStreamButtonToStop();
// In stream mode, the buffer_processed notification is actually handled in AudioPlayer.js.
// as well as the updateProgress() call.
await audioPlayer.queueAudio(e.data.audio);
}
break;
case "complete":
if (buttonHandler.getMode() === "disk") {
try {
updateProgress(99, "Combining audio chunks...");
updateProgress(99.5, "Writing file to disk...");
await audioDiskSaver.finalizeSave();
updateProgress(100, "File saved successfully!");
} catch (error) {
console.error("Error combining audio chunks:", error);
updateProgress(100, "Error saving file!");
}
buttonHandler.setMode("none");
// Reset the disk button state after saving is complete
buttonHandler.resetDiskButton();
document.getElementById("streamAudioContext").disabled = false;
} else if (buttonHandler.getMode() === "stream") {
// Only reset streaming state when complete is received and we're still in streaming mode
buttonHandler.setStreaming(false);
buttonHandler.setMode("none");
updateProgress(100, "Streaming complete");
// Use resetStreamButton instead of enableButtons
buttonHandler.resetStreamButton();
document.getElementById("streamDisk").disabled = false;
} else {
// For any other mode, use the standard enableButtons function
buttonHandler.enableButtons();
}
break;
}
};
const onErrorReceived = (e) => {
console.error("Worker error:", e);
// Get the current mode before resetting it
const currentMode = buttonHandler.getMode();
buttonHandler.setStreaming(false);
buttonHandler.setMode("none");
updateProgress(100, "An error occurred! Please try again.");
// Reset the appropriate button based on the mode we were in
if (currentMode === "disk") {
buttonHandler.resetDiskButton();
document.getElementById("streamAudioContext").disabled = false;
} else {
buttonHandler.resetStreamButton();
document.getElementById("streamDisk").disabled = false;
}
};
tts_worker.addEventListener("message", onMessageReceived);
tts_worker.addEventListener("error", onErrorReceived);
document.addEventListener('DOMContentLoaded', async () => {
updateProgress(0, "Initializing Kokoro model...");
document.getElementById("progressContainer").style.display = "block";
document.getElementById("ta").value = await (await fetch('./end.txt')).text();
buttonHandler.init();
});
window.addEventListener("beforeunload", () => {
audioPlayer.close();
});