forked from rhulha/StreamingKokoroJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButtonHandler.js
More file actions
237 lines (196 loc) · 7.29 KB
/
Copy pathButtonHandler.js
File metadata and controls
237 lines (196 loc) · 7.29 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
import { updateProgress } from "./updateProgress.js";
export class ButtonHandler {
constructor(worker, audioPlayer, audioDiskSaver, getRealSpeedFunc) {
this.worker = worker;
this.audioPlayer = audioPlayer;
this.audioDiskSaver = audioDiskSaver;
this.getRealSpeed = getRealSpeedFunc;
this.mode = "none";
this.isStreaming = false;
this.queueManager = null; // Will be set externally
this.handleStreamButtonClick = this.handleStreamButtonClick.bind(this);
this.handleDiskButtonClick = this.handleDiskButtonClick.bind(this);
}
init() {
document.getElementById("streamAudioContext").addEventListener("click", this.handleStreamButtonClick);
document.getElementById("streamDisk").addEventListener("click", this.handleDiskButtonClick);
}
setQueueManager(queueManager) {
this.queueManager = queueManager;
}
getTtsOptions() {
const text = document.getElementById("ta").value;
const voice = document.getElementById("voiceSelector").value;
const sliderValue = parseFloat(document.getElementById('speed-slider').value);
const speed = this.getRealSpeed(sliderValue);
return { text, voice, speed };
}
isQueueModeEnabled() {
const queueModeCheckbox = document.getElementById("queueMode");
return queueModeCheckbox && queueModeCheckbox.checked;
}
showButtonContent(button, contentType) {
const allContents = button.querySelectorAll('.btn-content');
allContents.forEach(content => {
content.style.display = 'none';
});
let contentClass;
switch (contentType) {
case 'play': contentClass = '.play-content'; break;
case 'stop': contentClass = '.stop-content'; break;
case 'loading': contentClass = '.loading-content'; break;
case 'download': contentClass = '.download-content'; break;
case 'download-loading': contentClass = '.download-loading-content'; break;
case 'stop-download': contentClass = '.stop-content'; break;
default: console.error('Unknown content type:', contentType); return;
}
const contentToShow = button.querySelector(contentClass);
if (contentToShow) {
contentToShow.style.display = 'inline-flex';
}
}
enableButtons() {
const streamBtn = document.getElementById("streamAudioContext");
const diskBtn = document.getElementById("streamDisk");
if (this.isStreaming) return;
streamBtn.disabled = false;
diskBtn.disabled = false;
streamBtn.classList.remove("loading", "stop-streaming", "has-content");
diskBtn.classList.remove("loading", "stop-saving", "has-content");
this.showButtonContent(streamBtn, "play");
this.showButtonContent(diskBtn, "download");
}
updateStreamButtonToStop() {
const streamBtn = document.getElementById("streamAudioContext");
if (streamBtn.classList.contains("loading")) {
streamBtn.disabled = false;
streamBtn.classList.remove("loading");
streamBtn.classList.add("stop-streaming", "has-content");
this.showButtonContent(streamBtn, "stop");
}
}
resetStreamButton() {
this.resetStreamingState();
}
resetDiskButton() {
this.resetStreamingState();
}
resetStreamingState() {
this.isStreaming = false;
this.mode = "none";
this.enableButtons();
}
async handleStreamButtonClick() {
// Check if queue mode is enabled
if (this.isQueueModeEnabled() && this.queueManager) {
const { text, voice, speed } = this.getTtsOptions();
if (text.trim().length === 0) {
alert('Please enter text to convert');
return;
}
// Add to queue
const jobId = await this.queueManager.addJob(text, voice, speed, 'stream');
alert(`Job #${jobId} added to queue!\n\nYou can close this tab. You'll get a notification when it's complete.`);
// Clear text area if user wants
if (confirm('Clear the text area?')) {
document.getElementById("ta").value = '';
}
return;
}
// Normal stream mode (existing logic)
if (this.isStreaming) {
this.audioPlayer.stop();
this.resetStreamingState();
updateProgress(100, "Streaming stopped");
return;
}
this.setStreamingState("stream");
const { text, voice, speed } = this.getTtsOptions();
if (text.trim().length === 0) {
this.resetStreamingState();
return;
}
updateProgress(0, "Initializing audio streaming...");
this.audioPlayer.setTotalChunks(text.length / 300);
this.worker.postMessage({ type: "generate", text: text, voice: voice, speed: speed });
}
async handleDiskButtonClick() {
// Check if queue mode is enabled
if (this.isQueueModeEnabled() && this.queueManager) {
const { text, voice, speed } = this.getTtsOptions();
if (text.trim().length === 0) {
alert('Please enter text to convert');
return;
}
// Add to queue
const jobId = await this.queueManager.addJob(text, voice, speed, 'disk');
alert(`Job #${jobId} added to queue!\n\nYou can close this tab. You'll get a notification when it's complete.\n\nYou can download the audio from the queue list when it's done.`);
// Clear text area if user wants
if (confirm('Clear the text area?')) {
document.getElementById("ta").value = '';
}
return;
}
// Normal disk mode (existing logic)
if (this.isStreaming) {
this.worker.postMessage({ type: "stop" });
await this.audioDiskSaver.stopSave();
this.resetStreamingState();
updateProgress(100, "Disk save stopped");
return;
}
this.setStreamingState("disk");
try {
updateProgress(0, "Preparing to save audio...");
await this.audioDiskSaver.initSave();
const { text, voice, speed } = this.getTtsOptions();
if (text.trim().length === 0) {
this.resetStreamingState();
return;
}
this.audioDiskSaver.setTotalChunks(text.length / 100);
updateProgress(0, "Processing audio for saving...");
this.worker.postMessage({ type: "generate", text: text, voice: voice, speed: speed });
} catch (error) {
console.error("Error initializing disk save:", error);
updateProgress(100, "File save error!");
this.resetStreamingState();
}
}
setStreamingState(mode) {
this.isStreaming = true;
this.mode = mode;
const streamBtn = document.getElementById("streamAudioContext");
const diskBtn = document.getElementById("streamDisk");
if (mode === "stream") {
this.showButtonContent(streamBtn, "loading");
streamBtn.disabled = false;
diskBtn.disabled = true;
} else if (mode === "disk") {
this.showButtonContent(diskBtn, "download-loading");
diskBtn.disabled = false;
streamBtn.disabled = true;
}
}
updateDiskButtonToStop() {
const diskBtn = document.getElementById("streamDisk");
if (diskBtn.classList.contains("loading")) {
diskBtn.disabled = false;
diskBtn.classList.remove("loading");
diskBtn.classList.add("stop-saving", "has-content");
this.showButtonContent(diskBtn, "stop-download");
}
}
getMode() {
return this.mode;
}
setMode(newMode) {
this.mode = newMode;
}
isCurrentlyStreaming() {
return this.isStreaming;
}
setStreaming(state) {
this.isStreaming = state;
}
}