-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathElevenLabsClient.js
More file actions
239 lines (187 loc) · 8.48 KB
/
Copy pathElevenLabsClient.js
File metadata and controls
239 lines (187 loc) · 8.48 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
238
239
// A class that manages speech synthesis through an external API
class SpeechManager {
// The constructor initializes the class with the API domain and sets up some state variables
constructor(apiDomain, status) {
this.apiDomain = apiDomain; // The domain of the API to fetch speech from
this.currentAudio = null; // The Audio object of the currently playing speech
this.queue = []; // A queue of speech tasks
this.voicing = true; // A flag indicating whether speech synthesis is currently allowed
this.isSpeaking = false; // A flag indicating whether speech synthesis is currently happening
this.audioContext = null;//new (window.AudioContext || window.webkitAudioContext)();
this.unlocked = false;
if(status)
this.status = status;
this.cache = new Map(); // Cache to store audio blob URLs for text-voice combinations
}
async unlockAudioContext() {
if (this.unlocked) return;
this.AddToStatus("Unlocking", true);
this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
const buffer = this.audioContext.createBuffer(1, 1, 22050);
const source = this.audioContext.createBufferSource();
source.buffer = buffer;
source.connect(this.audioContext.destination);
source.start(0);
// Older browsers might require 'noteOn' instead of 'start'.
// source.noteOn(0);
// By checking the play state after some time, we can see if we're really unlocked
setTimeout(() => {
if((source.playbackState === source.PLAYING_STATE || source.playbackState === source.FINISHED_STATE)) {
this.unlocked = true;
this.AddToStatus("Unlocked");
}
}, 0);
}
async resumeAudioContext() {
if (this.audioContext && this.audioContext.state === 'suspended') {
this.AddToStatus("resuming");
await this.audioContext.resume();
}
}
// The AddToStatus function updates the status text
// newLine is the text to be added
// reset is a boolean indicating whether the status text should be cleared before adding newLine
AddToStatus(newLine, reset)
{
console.log(newLine);
// If reset is true, clear the status text
if(reset)
if (this.status != null)
this.status.innerText = "";
// Add newLine to the status text
if (this.status != null) this.status.innerText += newLine + "\n";
}
// The Speak function initiates a speech task
async Speak(text, voice, callBack)
{
this.AddToStatus("Speak text:" + text + " voice:" + voice);
if (!this.unlocked)
await buttonPressHandler();
// If the system is not currently allowed to speak, or if there is an audio currently playing, or if a speech task is already happening, return immediately
if (!this.voicing)
return;
//if new thing to be said
if (text != null && voice != null) {
console.log("Adding to queue");
const cacheKey = `${text}-${voice}`; // Create a unique key for text-voice combination
let blobUrl;
if (this.cache.has(cacheKey)) {
blobUrl = this.cache.get(cacheKey); // Retrieve the blob URL from the cache
} else {
blobUrl = await this.fetchSpeech(text, voice);
this.cache.set(cacheKey, blobUrl); // Store the blob URL in the cache
}
this.queue.push({ text: text, voice: voice, callBack: callBack, blobUrl: blobUrl });
// If it's not currently speaking, start to play
if (!this.isSpeaking) {
await this.PlayNextInQueue();
}
}
}
async PlayNextInQueue() {
// If the system is currently speaking or there's no speech task in the queue, return immediately
if (this.isSpeaking || this.queue.length === 0)
return;
this.isSpeaking = true; // Set the isSpeaking flag to true
console.log("getting message to play");
// Dequeue the next speech task
let message = this.queue.shift();
// Update the status text
this.AddToStatus("Speak: " + message.text, true);
try {
// Fetch and decode the audio for the given text
const audioBuffer = await this.GetAudio(message.blobUrl);
// Play the decoded audio
this.PlayAudio(audioBuffer, message.callBack);
} catch (error) {
// Log the error and update the status text
console.error('Error:', error);
this.AddToStatus('Error: ' + error.message);
}
}
async GetAudio(blobUrl) {
const response = await fetch(blobUrl);
if (!response.ok) {
throw new Error(`Failed to fetch audio from blob URL. Status: ${response.status}`);
}
const arrayBuffer = await response.arrayBuffer();
return this.audioContext.decodeAudioData(arrayBuffer);
}
PlayAudio(audioBuffer, callBack) {
// Create an AudioBufferSourceNode from the AudioBuffer
const source = this.audioContext.createBufferSource();
source.buffer = audioBuffer;
// Connect the AudioBufferSourceNode to the AudioContext's destination (the speakers)
source.connect(this.audioContext.destination);
// Start playing the audio immediately
source.start(0);
// Keep a reference to the AudioBufferSourceNode that's currently playing
this.currentSource = source;
// Handle the end of the audio
this.handleAudioEnd(source, callBack);
}
// The fetchSpeech function fetches the speech audio from the API
async fetchSpeech(text, voice) {
const response = await fetch(this.apiDomain + '/Speak', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ text: text, voiceId: voice })
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
console.log(`Generated blob URL: ${url}`);
return url;
}
/*
* The handleAudioEnd function handles the end of the audio.
* It sets up a callback to be called when the audio finishes playing.
*
* @param {AudioBufferSourceNode} source - The AudioBufferSourceNode that is playing the audio.
* @param {function} callBack - A function to be called when the audio finishes playing.
*/
handleAudioEnd(source, callBack) {
// Set up a callback to be called when the audio finishes playing
source.onended = () => {
// Update the status text
this.AddToStatus('Audio has finished playing!');
// If a callback function was provided, call it
if (callBack != null)
callBack();
// Set the isSpeaking flag to false, since the audio has finished playing
this.isSpeaking = false;
// If there are more speech tasks in the queue, start the next one
if (this.queue.length > 0) {
this.PlayNextInQueue();
}
};
}
// The StopSpeaking function stops the currently playing audio, clears the queue of speech tasks, and pauses any further speech synthesis
StopSpeaking() {
if (this.currentSource) {
this.currentSource.stop(); // Stop the currently playing audio
this.currentSource = null; // Clear the currently playing audio
}
this.queue = []; // Clear the queue of speech tasks
this.isSpeaking = false; // Set the isSpeaking flag to false
this.voicing = false; // Set the voicing flag to false
}
// The ResumeSpeaking function resumes speech synthesis
ResumeSpeaking() {
this.voicing = true; // Set the voicing flag to true
if(this.queue.length > 0) {
this.PlayNextInQueue(); // Start the next speech task if there are any in the queue
}
}
}
// Create a new SpeechManager object
let speechManager = new SpeechManager('https://brennan.games:3000', document.getElementById('SpeechManagerStatus'));
// Use this function to unlock the audio context after a user interaction (e.g., button press)
async function buttonPressHandler() {
await speechManager.unlockAudioContext();
await speechManager.resumeAudioContext();
}