-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathButtonHandler.js
More file actions
206 lines (176 loc) · 7.5 KB
/
ButtonHandler.js
File metadata and controls
206 lines (176 loc) · 7.5 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
import { updateProgress } from "./updateProgress.js";
export class ButtonHandler {
constructor(worker, audioPlayer, audioDiskSaver) {
this.worker = worker;
this.audioPlayer = audioPlayer;
this.audioDiskSaver = audioDiskSaver;
this.mode = "none";
this.isStreaming = false;
// Bind methods to maintain 'this' context
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);
} showButtonContent(button, contentType) {
// Hide all content spans first
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;
// Remove any state classes
streamBtn.classList.remove("loading", "stop-streaming", "has-content");
diskBtn.classList.remove("loading", "stop-saving", "has-content");
// Show play content, hide stop and loading 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() {
const streamBtn = document.getElementById("streamAudioContext");
streamBtn.disabled = false;
streamBtn.classList.remove("loading", "stop-streaming", "has-content");
// Show play content, hide stop and loading content
this.showButtonContent(streamBtn, "play");
// Log for debugging
console.log("Button reset to play state");
}
handleStreamButtonClick() {
if (this.isStreaming) {
this.mode = "none";
this.isStreaming = false;
this.audioPlayer.stop();
updateProgress(100, "Streaming stopped");
// Use our dedicated function to reset the button
setTimeout(() => {
this.resetStreamButton();
document.getElementById("streamDisk").disabled = false;
}, 50); // Small delay to ensure clean state transition
return;
}
const streamBtn = document.getElementById("streamAudioContext");
streamBtn.classList.add("loading", "has-content");
this.showButtonContent(streamBtn, "loading");
// Only disable the disk button, but NOT the stream button
document.getElementById("streamDisk").disabled = true;
this.mode = "stream";
this.isStreaming = true;
let text = document.getElementById("ta").value;
updateProgress(0, "Initializing audio streaming...");
// Set estimated chunks based on text length, similar to disk mode this.audioPlayer.setTotalChunks(text.length / 300); // rough estimate based on chunk size
// Get the selected voice
const selectedVoice = document.getElementById("voiceSelector").value;
this.worker.postMessage({ type: "generate", text: text, voice: selectedVoice });
}
async handleDiskButtonClick() {
// If already running a disk save, stop it
if (this.mode === "disk") {
this.mode = "none";
// Send stop message to worker first
this.worker.postMessage({ type: "stop" });
this.audioDiskSaver.stopSave();
updateProgress(100, "Disk save stopped");
// Reset buttons after stopping
setTimeout(() => {
this.resetDiskButton();
document.getElementById("streamAudioContext").disabled = false;
}, 50); // Small delay to ensure clean state transition
return;
}
const diskBtn = document.getElementById("streamDisk");
// Show loading animation and disable both buttons
diskBtn.classList.add("loading", "has-content");
this.showButtonContent(diskBtn, "download-loading");
document.getElementById("streamAudioContext").disabled = true;
diskBtn.disabled = true;
this.mode = "disk";
try {
updateProgress(0, "Preparing to save audio...");
await this.audioDiskSaver.initSave();
let text = document.getElementById("ta").value;
// Set estimated chunks based on text length
this.audioDiskSaver.setTotalChunks(text.length / 100); // rough estimate based on chunk size
updateProgress(0, "Processing audio for saving...");
// Get the selected voice
const selectedVoice = document.getElementById("voiceSelector").value;
this.worker.postMessage({ type: "generate", text: text, voice: selectedVoice });
} catch (error) {
console.error("Error initializing disk save:", error);
updateProgress(100, "Error initializing file save!");
this.enableButtons();
}
}
resetDiskButton() {
const diskBtn = document.getElementById("streamDisk");
diskBtn.disabled = false;
diskBtn.classList.remove("loading", "stop-saving", "has-content");
this.showButtonContent(diskBtn, "download");
console.log("Disk button reset to download state");
}
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;
}
}