Spaces:
Sleeping
Sleeping
Yurii Paniv
commited on
Commit
•
bca4ff8
1
Parent(s):
98a1ca0
Move script to dedicated folder
Browse files- static/main.js +74 -0
- templates/hello.html +1 -75
static/main.js
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// part of script inspired by https://github.com/addpipe/simple-recorderjs-demo
|
2 |
+
var gumStream; //stream from getUserMedia()
|
3 |
+
var rec; //Recorder.js object
|
4 |
+
var input; //MediaStreamAudioSourceNode we'll be recording
|
5 |
+
|
6 |
+
// shim for AudioContext when it's not avb.
|
7 |
+
var AudioContext = window.AudioContext || window.webkitAudioContext;
|
8 |
+
var audioContext; //audio context to help us record
|
9 |
+
const resultNode = document.getElementById('result');
|
10 |
+
const actionButton = document.getElementById('action');
|
11 |
+
|
12 |
+
function resultProcess(data) {
|
13 |
+
resultNode.textContent = `Довжина тексту: ${data.length} \n
|
14 |
+
Текст: ${data}
|
15 |
+
`;
|
16 |
+
actionButton.textContent = "Start recording (3 sec)";
|
17 |
+
actionButton.disabled = false;
|
18 |
+
}
|
19 |
+
|
20 |
+
function exportWAV(blob) {
|
21 |
+
var data = new FormData()
|
22 |
+
data.append('file', blob);
|
23 |
+
fetch(`./recognize`, { method: "POST", body: data })
|
24 |
+
.then(response => response.text())
|
25 |
+
.then(resultProcess);
|
26 |
+
}
|
27 |
+
function record() {
|
28 |
+
|
29 |
+
var constraints = { audio: true, video: false }
|
30 |
+
navigator.mediaDevices.getUserMedia(constraints).then(function (stream) {
|
31 |
+
actionButton.textContent = "Recording..."
|
32 |
+
actionButton.disabled = true;
|
33 |
+
/*
|
34 |
+
create an audio context after getUserMedia is called
|
35 |
+
sampleRate might change after getUserMedia is called, like it does on macOS when recording through AirPods
|
36 |
+
the sampleRate defaults to the one set in your OS for your playback device
|
37 |
+
*/
|
38 |
+
audioContext = new AudioContext();
|
39 |
+
|
40 |
+
/* assign to gumStream for later use */
|
41 |
+
gumStream = stream;
|
42 |
+
|
43 |
+
/* use the stream */
|
44 |
+
input = audioContext.createMediaStreamSource(stream);
|
45 |
+
|
46 |
+
/*
|
47 |
+
Create the Recorder object and configure to record mono sound (1 channel)
|
48 |
+
Recording 2 channels will double the file size
|
49 |
+
*/
|
50 |
+
rec = new Recorder(input, { numChannels: 1 })
|
51 |
+
|
52 |
+
//start the recording process
|
53 |
+
rec.record()
|
54 |
+
sleep(3000).then(stop);
|
55 |
+
})
|
56 |
+
}
|
57 |
+
|
58 |
+
|
59 |
+
function stop() {
|
60 |
+
rec.stop();
|
61 |
+
|
62 |
+
//stop microphone access
|
63 |
+
gumStream.getAudioTracks()[0].stop();
|
64 |
+
|
65 |
+
//create the wav blob and pass it on to createDownloadLink
|
66 |
+
rec.exportWAV(exportWAV);
|
67 |
+
}
|
68 |
+
|
69 |
+
|
70 |
+
const sleep = time => new Promise(resolve => setTimeout(resolve, time));
|
71 |
+
|
72 |
+
async function handleAction() {
|
73 |
+
record();
|
74 |
+
}
|
templates/hello.html
CHANGED
@@ -15,81 +15,7 @@
|
|
15 |
<button class="btn btn-primary" id="action" onclick="handleAction()">Start recording (3 sec)</button>
|
16 |
<div id="result"></div>
|
17 |
<script src="https://cdn.rawgit.com/mattdiamond/Recorderjs/08e7abd9/dist/recorder.js"></script>
|
18 |
-
<script>
|
19 |
-
var gumStream; //stream from getUserMedia()
|
20 |
-
var rec; //Recorder.js object
|
21 |
-
var input; //MediaStreamAudioSourceNode we'll be recording
|
22 |
-
|
23 |
-
// shim for AudioContext when it's not avb.
|
24 |
-
var AudioContext = window.AudioContext || window.webkitAudioContext;
|
25 |
-
var audioContext; //audio context to help us record
|
26 |
-
const resultNode = document.getElementById('result');
|
27 |
-
const actionButton = document.getElementById('action');
|
28 |
-
|
29 |
-
function resultProcess(data) {
|
30 |
-
resultNode.textContent = `Довжина тексту: ${data.length} \n
|
31 |
-
Текст: ${data}
|
32 |
-
`;
|
33 |
-
actionButton.textContent = "Start recording (3 sec)";
|
34 |
-
actionButton.disabled = false;
|
35 |
-
}
|
36 |
-
|
37 |
-
function exportWAV(blob) {
|
38 |
-
var data = new FormData()
|
39 |
-
data.append('file', blob);
|
40 |
-
fetch(`./recognize`, { method: "POST", body: data })
|
41 |
-
.then(response => response.text())
|
42 |
-
.then(resultProcess);
|
43 |
-
}
|
44 |
-
function record() {
|
45 |
-
|
46 |
-
var constraints = { audio: true, video: false }
|
47 |
-
navigator.mediaDevices.getUserMedia(constraints).then(function (stream) {
|
48 |
-
actionButton.textContent = "Recording..."
|
49 |
-
actionButton.disabled = true;
|
50 |
-
/*
|
51 |
-
create an audio context after getUserMedia is called
|
52 |
-
sampleRate might change after getUserMedia is called, like it does on macOS when recording through AirPods
|
53 |
-
the sampleRate defaults to the one set in your OS for your playback device
|
54 |
-
*/
|
55 |
-
audioContext = new AudioContext();
|
56 |
-
|
57 |
-
/* assign to gumStream for later use */
|
58 |
-
gumStream = stream;
|
59 |
-
|
60 |
-
/* use the stream */
|
61 |
-
input = audioContext.createMediaStreamSource(stream);
|
62 |
-
|
63 |
-
/*
|
64 |
-
Create the Recorder object and configure to record mono sound (1 channel)
|
65 |
-
Recording 2 channels will double the file size
|
66 |
-
*/
|
67 |
-
rec = new Recorder(input, { numChannels: 1 })
|
68 |
-
|
69 |
-
//start the recording process
|
70 |
-
rec.record()
|
71 |
-
sleep(3000).then(stop);
|
72 |
-
})
|
73 |
-
}
|
74 |
-
|
75 |
-
|
76 |
-
function stop() {
|
77 |
-
rec.stop();
|
78 |
-
|
79 |
-
//stop microphone access
|
80 |
-
gumStream.getAudioTracks()[0].stop();
|
81 |
-
|
82 |
-
//create the wav blob and pass it on to createDownloadLink
|
83 |
-
rec.exportWAV(exportWAV);
|
84 |
-
}
|
85 |
-
|
86 |
-
|
87 |
-
const sleep = time => new Promise(resolve => setTimeout(resolve, time));
|
88 |
-
|
89 |
-
async function handleAction() {
|
90 |
-
record();
|
91 |
-
}
|
92 |
-
</script>
|
93 |
</body>
|
94 |
|
95 |
</html>
|
|
|
15 |
<button class="btn btn-primary" id="action" onclick="handleAction()">Start recording (3 sec)</button>
|
16 |
<div id="result"></div>
|
17 |
<script src="https://cdn.rawgit.com/mattdiamond/Recorderjs/08e7abd9/dist/recorder.js"></script>
|
18 |
+
<script src="/static/main.js"></script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
</body>
|
20 |
|
21 |
</html>
|