-
Notifications
You must be signed in to change notification settings - Fork 0
/
ibad one.html
830 lines (703 loc) · 27.4 KB
/
ibad one.html
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>WebSocket Chat</title>
<style>
body {
font-family: Inter, sans-serif;
font-size: 15.297px;
font-style: normal;
font-weight: 400;
margin: 0;
padding: 20px;
display: flex;
flex-direction: column;
align-items: center;
}
#chatContainer {
width: 100%;
max-width: 800px;
display: flex;
flex-direction: column;
}
.message {
margin: 5px 0;
padding: 10px;
border-radius: 8px;
}
.transcript {
color: #000;
align-self: flex-start;
border-radius: 7.649px 7.649px 5.068px 0;
background: rgba(48, 78, 141, 0.1);
}
.response {
color: #fff;
align-self: flex-end;
border-radius: 7.649px 7.649px 0 7.649px;
background: #2183c6;
margin-right: 10px;
}
#muteButton {
margin-top: 20px;
padding: 10px 20px;
background-color: #2183c6;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
<div id="status" style="text-align: center">Not Connected</div>
<div id="chatContainer">
<!-- Messages will be appended here -->
</div>
<audio id="audioPlayer" style="display: none"></audio>
<!-- Hidden input to trigger Bubble event -->
<input type="hidden" id="triggerBubbleEvent" value="0" />
<input type="hidden" id="currentTranscript" value="" />
<!-- Mute/Unmute buttons -->
<button id="muteButton" style="display: none">Mute Mic</button>
<button id="muteSpeakerButton" style="display: none">Mute Speaker</button>
<button id="unmuteSpeakerButton" style="display: none">
Unmute Speaker
</button>
<script>
// Function to extract user_id, session_id, and name from the URL query string
function getParameterByName(name) {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get(name);
}
// Map full language names to their short codes
function getLanguageShortCode(language) {
const languageMap = {
English: "en",
French: "fr",
};
return languageMap[language] || language;
}
// Retrieve user_id, session_id, and name from the query string
const userId = getParameterByName("user_id");
const sessionId = getParameterByName("session_id");
const userName = getParameterByName("name");
const languagetest = getParameterByName("language_test");
const interface = getParameterByName("interface_lang");
const countdownValue = getParameterByName("countdown");
if (userId) {
console.log("User ID retrieved from URL:", userId);
} else {
console.error("User ID not found in URL");
}
if (sessionId) {
console.log("Session ID retrieved from URL:", sessionId);
} else {
console.error("Session ID not found in URL");
}
if (userName) {
console.log("User Name retrieved from URL:", userName);
} else {
console.error("User Name not found in URL");
}
if (countdownValue) {
console.log("coutdown retrieved from URL:", countdownValue);
} else {
console.error("countdown not found in URL");
}
if (interface) {
console.log("Interface Langugae retrieved from URL:", interface);
} else {
console.error("Interface Langugae not found in URL");
}
if (languagetest) {
console.log("Language to test retrieved from URL:", languagetest);
} else {
console.error("Language test not found in URL");
}
// Convert full language name to short code
const languageCode = getLanguageShortCode(languagetest);
console.log("Language code to send:", languageCode);
let deepgramSocket;
let mediaRecorder;
let isMuted = false;
let isSpeakerMuted = false; // Track speaker mute status
let heartbeatInterval;
const audioQueue = []; // Queue to store incoming audio chunks
let isPlaying = false; // Track if audio is currently playing
let audioContext; // AudioContext to manage audio playback
let gainNode; // GainNode to control speaker volume
let base64Audio = ""; // addddddddd
let recordedData = [];
var convertedBlob = "";
// addddddd Utility function to convert Blob to Base64
function blobToBase64(blob) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result.split(",")[1]); // Strip out the "data:audio/wav;base64," prefix
reader.onerror = reject;
reader.readAsDataURL(blob);
});
}
async function blobArrayToBase64(blobArray) {
return new Promise((resolve, reject) => {
const finalBlob = new Blob(blobArray, { type: "audio/wav" });
const reader = new FileReader();
reader.onloadend = function () {
const base64String = reader.result.split(",")[1];
resolve(base64String);
};
reader.onerror = (error) => {
console.error("Error converting Blob to Base64", error);
reject(error);
};
reader.readAsDataURL(finalBlob);
});
}
function createDeepgramSocket() {
deepgramSocket = new WebSocket(
`wss://api.deepgram.com/v1/listen?punctuate=true&vad_events=true&stream=true&smart_format=true&utterance_end_ms=1500&interim_results=true&language=${languageCode}`,
["token", "f0b9e93ea008ac588ff38c715cbf2de0524eb3fe"]
);
deepgramSocket.onopen = () => {
console.log("Value of Mic Mute", isMuted);
document.querySelector("#status").textContent = "Connected";
console.log({ event: "onopen" });
// Send the language test value after connection
if (languagetest) {
deepgramSocket.send(
JSON.stringify({ type: "language", value: languagetest })
);
console.log("Sent language test value to Deepgram:", languagetest);
} else {
console.error("Language test value not found");
}
// Trigger Bubble event to start the timer
document.querySelector("#triggerBubbleEvent").value = "1";
document
.querySelector("#triggerBubbleEvent")
.dispatchEvent(new Event("change"));
startHeartbeat(); // Start the heartbeat to keep the connection alive
startMediaRecorder(); // Start the media recorder
};
deepgramSocket.onmessage = async (message) => {
try {
const received = JSON.parse(message.data);
const transcript = received?.channel?.alternatives?.[0]?.transcript;
if (transcript && received.is_final) {
// Update the hidden input and trigger the event
updateBubbleTextElement(transcript);
appendMessage("transcript", transcript);
const currentTime = Date.now();
messageBuffer += transcript + " ";
totalWordCount += transcript
.split(" ")
.filter((word) => word).length; // Count words
if (!isVoiceActive) {
voiceStartTime = currentTime; // Set voice start time when first voice is detected
isVoiceActive = true;
}
if (isVoiceActive) {
// If the voice was active, add the duration since last start time to totalVoiceTime
totalVoiceTime += (currentTime - voiceStartTime) / 1000;
voiceStartTime = currentTime; // Reset start time to current time
}
lastSentTime = currentTime; // Update the last sent time
console.log("error, message buffer", messageBuffer);
if (clientSocket.readyState === WebSocket.OPEN) {
messageId++;
async function createFileFormCurrentRecordedData() {
console.log(
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~Stopped 1~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
);
console.log("recordedData:", recordedData);
convertedBlob = await blobArrayToBase64(recordedData);
console.log("convertedBlob", convertedBlob);
const messageToSend = {
id: messageId,
user_id: userId,
session_id: sessionId,
name: userName,
language_test: languagetest,
interface_lang: interface,
message: messageBuffer.trim(),
voice: convertedBlob,
};
console.log(
"Sending message to server convertedblob:",
messageToSend
);
clientSocket.send(JSON.stringify(messageToSend));
messageBuffer = "";
convertedBlob = "";
recordedData = [];
}
// mediaRecorder.onstop = createFileFormCurrentRecordedData;
// Text-to-Speech generation removed here
}
// Reset current response container for new voice input
currentResponseContainer = null;
}
} catch (error) {
console.error("Error processing message:", error);
}
};
deepgramSocket.onclose = () => {
try {
stopHeartbeat();
if (mediaRecorder && mediaRecorder.stream) {
mediaRecorder.stream.getTracks().forEach((track) => track.stop());
}
// STOP MEDIA RECORDER
if (mediaRecorder) {
mediaRecorder.stop();
mediaRecorder = null; // Important to clear the old instance
}
console.log({ event: "onclose" });
setTimeout(() => {
console.log("Attempting to reconnect...");
createDeepgramSocket();
}, 1000);
} catch (error) {
console.error("Error during socket close:", error);
}
};
deepgramSocket.onerror = (error) => {
console.error("WebSocket Error", error);
};
}
function initializeAudioContext() {
if (!audioContext) {
audioContext = new (window.AudioContext ||
window.webkitAudioContext)();
gainNode = audioContext.createGain(); // Create a GainNode for controlling volume
gainNode.connect(audioContext.destination);
console.log("AudioContext initialized");
}
}
const mergeAudioStreams = (desktopStream, voiceStream) => {
const context = new AudioContext();
const destination = context.createMediaStreamDestination();
let hasDesktop = false;
let hasVoice = false;
if (desktopStream && desktopStream.getAudioTracks().length > 0) {
// If you don't want to share Audio from the desktop it should still work with just the voice.
const source1 = context.createMediaStreamSource(desktopStream);
const desktopGain = context.createGain();
desktopGain.gain.value = 0.7;
source1.connect(desktopGain).connect(destination);
hasDesktop = true;
}
if (voiceStream && voiceStream.getAudioTracks().length > 0) {
const source2 = context.createMediaStreamSource(voiceStream);
const voiceGain = context.createGain();
voiceGain.gain.value = 0.7;
source2.connect(voiceGain).connect(destination);
hasVoice = true;
}
return (hasDesktop || hasVoice) ? destination.stream.getAudioTracks() : [];
};
async function startMediaRecorder() {
try {
console.log("Requesting microphone audio...");
const micStream = await navigator.mediaDevices.getUserMedia({ audio: true });
console.log("Microphone audio stream obtained.");
let systemAudioStream = null;
let combinedStreamNew = null;
// Try to get system audio (with video) if supported
try {
console.log("Requesting system audio...");
systemAudioStream = await navigator.mediaDevices.getDisplayMedia({
audio: true, // Capture system audio
video: true, // Capture screen or application video
});
/**
* Audio Context
*/
const tracks = [
...systemAudioStream.getVideoTracks(),
...mergeAudioStreams(systemAudioStream, micStream)
];
console.log('Tracks to add to stream', tracks);
combinedStreamNew = new MediaStream(tracks);
console.log('Stream', combinedStreamNew)
// console.log("System audio/video stream obtained:", systemAudioStream);
} catch (systemAudioError) {
console.warn("System audio not supported or permission denied:", systemAudioError);
}
if (!systemAudioStream) {
console.error("System audio/video stream not available.");
return;
}
// Choose the appropriate MIME type for recording
let mimeType = "video/webm"; // Use a video format that supports both video and audio
if (MediaRecorder.isTypeSupported("video/webm")) {
mimeType = "video/webm";
} else if (MediaRecorder.isTypeSupported("video/mp4")) {
mimeType = "video/mp4";
} else if (MediaRecorder.isTypeSupported("video/ogg")) {
mimeType = "video/ogg";
} else {
alert("No supported video formats found for recording in this browser.");
throw new Error("No supported video formats for MediaRecorder.");
}
console.log("Permission granted for media recording with mimeType:", mimeType);
let mediaRecorder = new MediaRecorder(micStream, { mimeType: mimeType });
console.log("Audio recording", mediaRecorder)
mediaRecorder.addEventListener("dataavailable", async (event) => {
if (event.data.size > 0 && isMuted === false && deepgramSocket.readyState === WebSocket.OPEN) {
deepgramSocket.send(event.data);
recordedData.push(event.data);
}
if (mediaRecorder.state === "paused") {
mediaRecorder.resume();
} else if (mediaRecorder.state === "inactive") {
mediaRecorder.start(1000);
}
});
mediaRecorder.onstop = async () => {
console.log("recorded --------------------------------", recordedData.length)
if (recordedData.length > 0) {
console.log("recorded inner ", recordedData.length)
const base64Audio = await blobArrayToBase64(recordedData);
console.log("recorded base64 after ", base64Audio)
sendRecordedData(base64Audio);
recordedData = [];
}
}
// Record both system video and audio using a single MediaRecorder
let mediaRecorderVideoAudio = new MediaRecorder(combinedStreamNew, { mimeType });
let recordedVideoAudioData = [];
mediaRecorderVideoAudio.addEventListener("dataavailable", (event) => {
if (event.data.size > 0) {
recordedVideoAudioData.push(event.data);
}
});
mediaRecorderVideoAudio.onstop = async () => {
console.log("Recording of video and audio stopped.");
if (recordedVideoAudioData.length > 0) {
// Convert the recorded data to a blob and send it or process it as needed
const blob = new Blob(recordedVideoAudioData, { type: mimeType });
const base64 = await blobArrayToBase64(recordedVideoAudioData)
console.log("````````````````````base64```````````````", base64)
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = 'combined-video-audio.webm';
document.body.appendChild(a);
a.click();
}
};
// Start the recording
mediaRecorderVideoAudio.start(1000); // Trigger `dataavailable` event every second
mediaRecorder.start(1000); // Trigger `dataavailable` event every second
// Stop the recording after 10 seconds for this example
setTimeout(() => {
mediaRecorderVideoAudio.stop();
mediaRecorder.onstop();
console.log("Stopped recording after timeout.");
}, 10000 * 2);
console.log("Recording started...");
} catch (error) {
console.error("Error accessing media devices:", error.message);
alert(`Error accessing media devices: ${error.message}.`);
}
}
createDeepgramSocket();
const clientSocket = new WebSocket(
`wss://cefrl.octalooptechnologies.com/ws/${userId}/${userName}/${languageCode}`
);
// Function to start sending heartbeat messages to keep the connection alive
function startHeartbeat() {
heartbeatInterval = setInterval(() => {
if (deepgramSocket.readyState === WebSocket.OPEN) {
deepgramSocket.send(JSON.stringify({ type: "ping" }));
}
}, 5000); // Send a ping every 5 seconds
}
// Function to stop sending heartbeat messages
function stopHeartbeat() {
clearInterval(heartbeatInterval);
}
let lastSentTime = Date.now();
let messageBuffer = "";
let messageId = 0;
let totalWordCount = 0;
let voiceStartTime = null;
let totalVoiceTime = 0;
let overallStartTime = null;
let overallEndTime = null;
let isVoiceActive = false;
let currentResponseContainer = null;
const chatContainer = document.getElementById("chatContainer");
// Function to update the Bubble.io text element
function updateBubbleTextElement(transcript) {
const transcriptInput = document.getElementById("currentTranscript");
transcriptInput.value = transcript;
// Manually trigger input event to ensure Bubble workflow detects the change
var event = new Event("input", {
bubbles: true,
cancelable: true,
});
transcriptInput.dispatchEvent(event);
}
// Event listener to handle changes in the currentTranscript input
document
.getElementById("currentTranscript")
.addEventListener("input", (event) => {
const transcript = event.target.value;
// console.log("Transcript updated: ", transcript);
});
clientSocket.onopen = () => {
if (userId && sessionId && userName) {
const initialMessage = JSON.stringify({
type: "init",
user_id: userId,
session_id: sessionId,
name: userName,
language_test: languagetest,
interface_lang: interface,
});
clientSocket.send(initialMessage);
console.log(
"WebSocket connection established and user_id/session_id/name/language_test sent:",
userId,
sessionId,
userName,
languagetest,
interface
);
}
};
clientSocket.onmessage = async (message) => {
const received = JSON.parse(message.data);
// console.log("Message received from server:", received);
if (received.event === "greeting") {
const responseMessage = received.text;
console.log(responseMessage);
appendResponse(responseMessage);
const audioBase64 = received.audio;
const audioBlob = base64ToBlob(audioBase64, "audio/wav");
const audioUrl = URL.createObjectURL(audioBlob);
audioQueue.push(audioUrl);
if (!isPlaying) {
playAudioQueue();
}
} else if (received.message) {
const responseMessage = received.message;
console.log(responseMessage);
appendResponse(responseMessage);
} else if (received.audio) {
const audioBase64 = received.audio;
const audioBlob = base64ToBlob(audioBase64, "audio/wav");
const audioUrl = URL.createObjectURL(audioBlob);
audioQueue.push(audioUrl);
if (!isPlaying) {
playAudioQueue();
}
}
};
async function playAudioQueue() {
isPlaying = true;
initializeAudioContext();
while (audioQueue.length > 0) {
const audioUrl = audioQueue.shift();
await playAudioChunk(audioUrl);
}
isPlaying = false;
}
async function playAudioChunk(audioUrl) {
return new Promise((resolve) => {
fetch(audioUrl)
.then((response) => response.arrayBuffer())
.then((arrayBuffer) => audioContext.decodeAudioData(arrayBuffer))
.then((audioBuffer) => {
const source = audioContext.createBufferSource();
source.buffer = audioBuffer;
source.connect(gainNode);
source.onended = resolve;
source.start(0);
})
.catch((error) => {
console.error("Error playing audio chunk:", error);
resolve();
});
});
}
clientSocket.onclose = () => {
console.log("Disconnected from Python WebSocket client");
if (audioQueue.length > 0) {
playAudioQueue();
}
};
clientSocket.onerror = (error) => {
console.error("Client WebSocket Error", error);
};
setInterval(() => {
if (messageBuffer && Date.now() - lastSentTime >= 3000) {
if (clientSocket.readyState === WebSocket.OPEN) {
messageId++;
const messageToSend = {
id: messageId,
user_id: userId,
session_id: sessionId,
name: userName,
language_test: languagetest,
interface_lang: interface,
message: messageBuffer.trim(),
};
console.log("Sending message to server:", messageToSend);
clientSocket.send(JSON.stringify(messageToSend));
lastSentTime = Date.now();
messageBuffer = "";
}
}
// console.log("recorded Data empty", recordedData);
}, 1000);
function base64ToBlob(base64, mimeType) {
const byteCharacters = atob(base64);
const byteArrays = [];
for (let offset = 0; offset < byteCharacters.length; offset += 512) {
const slice = byteCharacters.slice(offset, offset + 512);
const byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
return new Blob(byteArrays, { type: mimeType });
}
function appendMessage(type, message) {
const messageElement = document.createElement("div");
messageElement.className = `message ${type}`;
messageElement.textContent = message;
chatContainer.appendChild(messageElement);
chatContainer.scrollTop = chatContainer.scrollHeight;
}
function appendResponse(message) {
if (!currentResponseContainer) {
currentResponseContainer = document.createElement("div");
currentResponseContainer.className = "message response";
chatContainer.appendChild(currentResponseContainer);
}
currentResponseContainer.textContent += message;
chatContainer.scrollTop = chatContainer.scrollHeight;
}
// Mute/Unmute Button logic
document
.getElementById("muteButton")
.addEventListener("click", toggleMute);
document
.getElementById("muteSpeakerButton")
.addEventListener("click", muteSpeaker);
document
.getElementById("unmuteSpeakerButton")
.addEventListener("click", unmuteSpeaker);
// Function to handle mute/unmute mic
function toggleMute() {
const muteButton = document.getElementById("muteButton");
if (!muteButton) {
console.error("Mute button element not found");
return; // Exit the function if the element is not found
}
if (isMuted) {
unmuteMic(); // Unmute mic
} else {
muteMic(); // Mute mic
}
}
function muteMic() {
if (mediaRecorder && mediaRecorder.state === "recording") {
mediaRecorder.pause(); // Mute
}
isMuted = true;
document.getElementById("muteButton").textContent = "Unmute Mic";
// Mute speaker when mic is muted
muteSpeaker();
if (deepgramSocket.readyState === WebSocket.OPEN) {
deepgramSocket.send(JSON.stringify({ type: "pause" }));
}
startHeartbeat(); // Ensure heartbeat continues even when muted
}
function unmuteMic() {
if (!mediaRecorder || mediaRecorder.state === "inactive") {
// If the mediaRecorder is inactive, restart it
startMediaRecorder();
} else if (mediaRecorder.state === "paused") {
mediaRecorder.resume(); // Unmute
}
isMuted = false;
document.getElementById("muteButton").textContent = "Mute Mic";
// Unmute speaker when mic is unmuted
unmuteSpeaker();
if (deepgramSocket.readyState === WebSocket.OPEN) {
deepgramSocket.send(JSON.stringify({ type: "resume" }));
}
}
function muteSpeaker() {
if (gainNode) {
gainNode.gain.value = 0; // Mute the audio by setting gain to 0
}
isSpeakerMuted = true;
console.log("Speaker muted");
}
function unmuteSpeaker() {
if (gainNode) {
gainNode.gain.value = 1; // Unmute the audio by restoring gain
}
isSpeakerMuted = false;
console.log("Speaker unmuted");
}
window.muteMic = muteMic;
window.unmuteMic = unmuteMic;
window.muteSpeaker = muteSpeaker;
window.unmuteSpeaker = unmuteSpeaker;
document.body.addEventListener("click", initializeAudioContext, {
once: true,
});
document.body.addEventListener("touchstart", initializeAudioContext, {
once: true,
});
document.addEventListener("visibilitychange", () => {
if (
document.visibilityState === "visible" &&
audioContext.state === "suspended"
) {
audioContext
.resume()
.then(() => {
console.log("AudioContext resumed on visibility change");
})
.catch((error) => {
console.error("Error resuming AudioContext:", error);
});
}
});
async function generateTextToSpeech(text) {
try {
const speechSynthesis = window.speechSynthesis;
if (!speechSynthesis) {
console.error("SpeechSynthesis API not supported in this browser.");
return;
}
const utterance = new SpeechSynthesisUtterance(text);
utterance.lang = "en-US"; // Set the language code
utterance.onstart = () => {
console.log("TTS started for:", text);
};
utterance.onend = () => {
console.log("TTS finished for:", text);
};
speechSynthesis.speak(utterance);
} catch (error) {
console.error("Error generating TTS:", error);
}
}
</script>
</body>
</html>