Tri4 commited on
Commit
da6ef97
1 Parent(s): 1cbf75a

Create app.js

Browse files
Files changed (1) hide show
  1. app.js +51 -0
app.js ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const record = document.querySelector(".record");
2
+ const output = document.querySelector(".output");
3
+
4
+ if (navigator.mediaDevices.getUserMedia) {
5
+
6
+ let onMediaSetupSuccess = function (stream) {
7
+ const mediaRecorder = new MediaRecorder(stream);
8
+ let chunks = [];
9
+
10
+ record.onclick = function() {
11
+ if (mediaRecorder.state == "recording") {
12
+ mediaRecorder.stop();
13
+ record.classList.remove("btn-danger");
14
+ record.classList.add("btn-primary");
15
+ } else {
16
+ mediaRecorder.start();
17
+ record.classList.remove("btn-primary");
18
+ record.classList.add("btn-danger");
19
+ }
20
+ }
21
+
22
+ mediaRecorder.ondataavailable = function (e) {
23
+ chunks.push(e.data);
24
+ }
25
+
26
+ mediaRecorder.onstop = function () {
27
+ let blob = new Blob(chunks, {type: "audio/webm"});
28
+ chunks = [];
29
+
30
+ let formData = new FormData();
31
+ formData.append("audio", blob);
32
+
33
+ fetch("/transcribe", {
34
+ method: "POST",
35
+ body: formData
36
+ }).then((response) => response.json())
37
+ .then((data) => {
38
+ output.innerHTML = data.output;
39
+ })
40
+ }
41
+ }
42
+
43
+ let onMediaSetupFailure = function(err) {
44
+ alert(err);
45
+ }
46
+
47
+ navigator.mediaDevices.getUserMedia({ audio: true}).then(onMediaSetupSuccess, onMediaSetupFailure);
48
+
49
+ } else {
50
+ alert("getUserMedia is not supported in your browser!")
51
+ }