Showing
1 changed file
with
41 additions
and
0 deletions
client.js
0 → 100644
| 1 | +let rec = null; | ||
| 2 | +let audioStream = null; | ||
| 3 | +const recordButton = document.getElementById("recordButton"); | ||
| 4 | +const transcribeButton = document.getElementById("transcribeButton"); | ||
| 5 | +recordButton.addEventListener("click", startRecording); | ||
| 6 | +transcribeButton.addEventListener("click", transcribeText); | ||
| 7 | +function startRecording() { | ||
| 8 | + let constraints = { audio: true, video:false } | ||
| 9 | + recordButton.disabled = true; | ||
| 10 | + transcribeButton.disabled = false; | ||
| 11 | + navigator.mediaDevices.getUserMedia(constraints).then(function(stream) { | ||
| 12 | + const audioContext = new window.AudioContext(); | ||
| 13 | + audioStream = stream; | ||
| 14 | + const input = audioContext.createMediaStreamSource(stream); | ||
| 15 | + rec = new Recorder(input, { numChannels:1 }) | ||
| 16 | + rec.record() | ||
| 17 | + }).catch(function(err) { | ||
| 18 | + recordButton.disabled = false; | ||
| 19 | + transcribeButton.disabled = true; | ||
| 20 | + }); | ||
| 21 | +} | ||
| 22 | +function transcribeText() { | ||
| 23 | + transcribeButton.disabled = true; | ||
| 24 | + recordButton.disabled = false; | ||
| 25 | + rec.stop(); | ||
| 26 | + audioStream.getAudioTracks()[0].stop(); | ||
| 27 | + rec.exportWAV(uploadSoundData); | ||
| 28 | +} | ||
| 29 | +function uploadSoundData(blob) { | ||
| 30 | + let filename = new Date().toISOString(); | ||
| 31 | + let xhr = new XMLHttpRequest(); | ||
| 32 | + let formData = new FormData(); | ||
| 33 | + xhr.onload = function(e) { | ||
| 34 | + if(this.readyState === 4) { | ||
| 35 | + document.getElementById("output").innerHTML += `<br><br><strong>Result: </strong>${e.target.responseText}` | ||
| 36 | + } | ||
| 37 | + }; | ||
| 38 | + formData.append("audio_data", blob, filename); | ||
| 39 | + xhr.open("POST", "/upload_sound", true); | ||
| 40 | + xhr.send(formData); | ||
| 41 | +} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment