-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
350 lines (315 loc) · 14.8 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Web API Seamlessm4tv2 Demo</title>
<script>
var srcLang = "eng"
var tgtLang = "eng"
let audioData;
let mediaRecorder;
let chunks = [];
let audioURL;
let myAudioData;
let mySampleRate = 16000; //required by seamlessm4tv2 model
let stopButtonId;
// Function to create a dropdown list
function createDropdown(id, options, callback) {
var select = document.createElement("select");
select.id = id;
select.name = id;
// Create options based on the provided array
for (var i = 0; i < options.length; i++) {
var option = document.createElement("option");
option.value = options[i].value;
option.text = options[i].text;
select.appendChild(option);
}
// Set the onchange attribute to trigger the callback function
select.onchange = function() {
callback(id); // Pass the ID of the selected dropdown to the callback function
};
return select;
}
// Function to show selected value
function srcDropdownSelected(selectId) {
var selectedValue = document.getElementById(selectId).value;
srcLang = selectedValue
console.log('you selected srcLang: ', srcLang);
}
function tgtDropdownSelected(selectId) {
var selectedValue = document.getElementById(selectId).value;
tgtLang = selectedValue
console.log('you selected tgtLang: ', tgtLang);
}
// Text 2 Text Translate
function t2tTranslate(selectId) {
// Get the input text value
const inputText = document.getElementById('inputText').value;
console.log("inputText:", inputText);
console.log("kv inputText:", JSON.stringify({ inputText }));
console.log("kv srcLang:", JSON.stringify({ srcLang }));
console.log("kv json ", JSON.stringify({ inputText, srcLang, tgtLang}));
fetch('http://localhost:5000/t2t', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ inputText, srcLang, tgtLang}),
})
.then(response => response.json())
.then(data => {
// Display the processed text
document.getElementById('result').innerText = data.processedText;
console.log("did have data.processedText.", data.processedText);
//check sample_rate if available
if (data.sample_rate) {
console.log("did have sample_rate:", data.sample_rate);
}
// Play audio if available
if (data.audioData) {
console.log("did have audioData.");
playAudio(data.audioData, data.sample_rate);
}
})
.catch(error => console.error('Error:', error));
}
// Text 2 Speech Translate
function t2sTranslate(selectId) {
// Get the input text value
const inputText = document.getElementById('inputText').value;
fetch('http://localhost:5000/t2s', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ inputText, srcLang, tgtLang}),
})
.then(response => response.json())
.then(data => {
//check sample_rate if available
if (data.sample_rate) {
console.log("did have sample_rate:", data.sample_rate);
}
// Play audio if available
if (data.audioData) {
console.log("did have audioData.");
audioData = data.audioData;
sampleRate = data.sample_rate;
playAudio(data.audioData, data.sample_rate);
}
})
.catch(error => console.error('Error:', error));
}
// Speech 2 Text Translate
function s2tTranslate(selectId) {
// button_s2t_start
console.log("s2tTranslate");
// button_s2t_start
document.getElementById('button_s2t_start').disabled = false;
document.getElementById('button_s2t_stop').disabled = true;
}
// Speech 2 Text Translate
function s2sTranslate(selectId) {
// button_s2t_start
console.log("s2sTranslate");
// button_s2t_start
document.getElementById('button_s2s_start').disabled = false;
document.getElementById('button_s2s_stop').disabled = true;
}
// Function to play audio using Web Audio API on the client side
function playAudio(audioData, sample_rate) {
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
const audioBuffer = audioContext.createBuffer(1, audioData.length, sample_rate);
console.log('playAudio audioData.length: sample_rate ', audioData.length, sample_rate);
// console.log('playAudio audioData ', audioData);
// Fill the buffer with the audio data
const audioBufferChannel = audioBuffer.getChannelData(0);
for (let i = 0; i < audioData.length; i++) {
audioBufferChannel[i] = audioData[i];
}
// Create an audio buffer source node
const source = audioContext.createBufferSource();
source.buffer = audioBuffer;
// Connect the source to the audio context's destination (speakers)
source.connect(audioContext.destination);
// Start playing the audio
source.start();
}
// Speech 2 Text Translate
function send_s2tTranslate(audioData, sampleRate) {
// audioSample = Array.from(audioData);
audioSample = audioData;
sampleRate = sampleRate;
console.log('send_s2tTranslate audioSample.length: sampleRate ', audioSample.length, sampleRate);
// playAudio(audioSample, sampleRate);
fetch('http://localhost:5000/s2t', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ audioSample, sampleRate, srcLang, tgtLang}),
})
.then(response => response.json())
.then(data => {
// Display the processed text
document.getElementById('result').innerText = data.processedText;
console.log("did have data.processedText.", data.processedText);
})
.catch(error => console.error('Error:', error));
}
// Speech 2 Speech Translate
function send_s2sTranslate(audioData, sampleRate) {
// audioSample = Array.from(audioData);
audioSample = audioData;
sampleRate = sampleRate;
console.log('send_s2sTranslate audioSample.length: sampleRate ', audioSample.length, sampleRate);
// playAudio(audioSample, sampleRate);
fetch('http://localhost:5000/s2s', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ audioSample, sampleRate, srcLang, tgtLang}),
})
.then(response => response.json())
.then(data => {
//check sample_rate if available
if (data.sample_rate) {
console.log("did have sample_rate:", data.sample_rate);
}
// Play audio if available
if (data.audioData) {
console.log("did have audioData.");
audioData = data.audioData;
sampleRate = data.sample_rate;
playAudio(data.audioData, data.sample_rate);
}
})
.catch(error => console.error('Error:', error));
}
async function startRecording(button) {
console.log('startRecording_s2t function buttonId: ', button.id);
button.disabled = true;
if (button.id == 'button_s2t_start'){
console.log('startRecording function u pressed button button_s2t_start');
button.disabled = true;
// document.getElementById('button_s2t_start').disabled = true;
document.getElementById('button_s2t_stop').disabled = false;
}else {
console.log('startRecording function u pressed button button_s2s_start');
document.getElementById('button_s2s_stop').disabled = false;
}
const stream = await navigator.mediaDevices.getUserMedia({ audio: { sampleRate: 48000 } });
mediaRecorder = new MediaRecorder(stream, { audioBitsPerSecond: 16000 });
mediaRecorder.ondataavailable = (e) => {
chunks.push(e.data);
};
mediaRecorder.onstop = () => {
const blob = new Blob(chunks, { type: 'audio/ogg; codecs=opus' });
chunks = [];
const reader = new FileReader();
reader.onload = () => {
const arrayBuffer = reader.result;
const audioContext = new AudioContext();
audioContext.decodeAudioData(arrayBuffer, (audioBuffer) => {
console.log('audioBuffer.sampleRate: ', audioBuffer.sampleRate);
const sourceBuffer = audioBuffer.getChannelData(0);
const targetBuffer = new Float32Array(Math.round(sourceBuffer.length * mySampleRate / audioBuffer.sampleRate));
const ratio = sourceBuffer.length / targetBuffer.length;
for (let i = 0; i < targetBuffer.length; i++) {
const sourceIndex = Math.round(i * ratio);
targetBuffer[i] = sourceBuffer[sourceIndex];
}
myAudioData = Array.from(targetBuffer);
playAudio(myAudioData, mySampleRate);
if (button.id == 'button_s2t_start'){
console.log('startRecording function u pressed button button_s2t_start');
send_s2tTranslate(myAudioData, mySampleRate);
}else {
console.log('startRecording function u pressed button button_s2s_start');
send_s2sTranslate(myAudioData, mySampleRate);
}
});
};
reader.readAsArrayBuffer(blob);
};
mediaRecorder.start();
};
function stopRecording(button) {
console.log('stopRecording fnc');
button.disabled = true
mediaRecorder.stop();
};
</script>
</head>
<body>
<h2 style="text-align: center; margin: 0;">Web Interface API for facebook/seamless-m4t-v2-large Model Backend Demonstration</h2>
<br><br>
<!-- Insert the dropdown dynamically with a unique ID -->
<div id="dropdownContainer">
<!-- You can create multiple dropdowns by calling the createDropdown function -->
<script>
var langOptions = [
{ value: "eng", text: "English" },
{ value: "cmn", text: "Chinese" },
{ value: "tha", text: "Thai" },
{ value: "deu", text: "German" },
{ value: "rus", text: "Russian" },
{ value: "fra", text: "Franch" },
{ value: "spa", text: "Spanish" },
{ value: "jpn", text: "Japanese" },
{ value: "vie", text: "Vietnamese" }
];
</script>
<label for="inputText">Input Text:</label>
<input type="text" id="inputText" name="inputText" required style="width: 400px;">
<br><br>
<label for="label1">Source Language:</label>
<script>
var srcDropdown = createDropdown("srcLang", langOptions, srcDropdownSelected);
document.getElementById("dropdownContainer").appendChild(srcDropdown);
</script>
<label for="label1">Target Language:</label>
<script>
var tgtDropdown = createDropdown("tgtLang", langOptions, tgtDropdownSelected);
document.getElementById("dropdownContainer").appendChild(tgtDropdown);
</script>
<br><br>
<div class="button-container">
<!-- Button to show selected value for the first dropdown -->
<button type="button" onclick="t2tTranslate('t2t')"> Text to Text </button>
<!-- Button to show selected value for the second dropdown -->
<button type="button" onclick="t2sTranslate('t2s')"> Text to Speech </button>
<!-- Button to show selected value for the second dropdown -->
<button type="button" onclick="s2tTranslate('s2t')"> Speech to Text </button>
<!-- Button to show selected value for the second dropdown -->
<button type="button" onclick="s2sTranslate('s2s')"> Speech to Speech </button>
</div>
<br>
<!-- Two smaller buttons precisely beneath "Speech to Text" -->
<div class="button-container">
<button id="button_s2t_start" disabled type="button" onclick="startRecording(this)">Start</button>
<button id="button_s2t_stop" disabled type="button" onclick="stopRecording(this)">Stop</button>
<button id="button_s2s_start" disabled type="button" onclick="startRecording(this)">Start</button>
<!-- <button id="button_s2s_start" disabled type="button" onclick="startRecording_s2s(this)">Start2</button> -->
<button id="button_s2s_stop" disabled type="button" onclick="stopRecording(this)">Stop</button>
</div>
</div>
<br><br>
<!-- Display the result here -->
<div id="result"></div>
</body>
</html>