-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
54 lines (45 loc) · 1.32 KB
/
utils.js
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
export async function convertAudioBufferToWavBlob(audioBuffer) {
return new Promise(function (resolve) {
var worker = new Worker('./wave-worker.js');
worker.onmessage = function (e) {
var blob = new Blob([e.data.buffer], { type: 'audio/wav' });
resolve(blob);
};
let pcmArrays = [];
for (let i = 0; i < audioBuffer.numberOfChannels; i++) {
pcmArrays.push(audioBuffer.getChannelData(i));
}
worker.postMessage({
pcmArrays,
config: { sampleRate: audioBuffer.sampleRate },
});
});
}
export function downloadBlob(blob, name) {
const blobUrl = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = blobUrl;
link.download = name;
document.body.appendChild(link);
link.dispatchEvent(
new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: window,
})
);
document.body.removeChild(link);
}
export function initButtonListener(mediaRecorder) {
const recordButton = document.getElementById('record-button');
recordButton.innerHTML = 'Record';
recordButton.addEventListener('click', () => {
if (mediaRecorder.state === 'inactive') {
mediaRecorder.start();
recordButton.innerHTML = 'Recording ...';
} else {
mediaRecorder.stop();
recordButton.innerHTML = 'Record';
}
});
}