-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio.js
100 lines (89 loc) · 2.53 KB
/
audio.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
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
var audioContext = new AudioContext();
var isAudioCaptured = false;
var isRecording = false;
var audioStream = null,
audioInput = null,
realAudioInput = null,
inputPoint = null,
audioRecorder = null;
var recIndex = 0;
var takes = {};
function reset() {
if (isRecording) {
audioRecorder.stop();
}
if (isAudioCaptured) {
audioStream.getTracks().forEach(track => track.stop());
}
isAudioCaptured = false;
isRecording = false;
audioStream = null;
audioInput = null;
realAudioInput = null;
inputPoint = null;
audioRecorder = null;
recIndex = 0;
takes = {};
}
function capture() {
if (!isAudioCaptured)
{
// https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia
navigator.mediaDevices
.getUserMedia({ audio: true, video: false })
.then(gotStream)
.catch(function(err) {
console.log(err);
});
} else {
stopRecording();
audioStream.getTracks().forEach(track => track.stop());
isAudioCaptured = false;
}
}
// https://github.com/cwilso/AudioRecorder/blob/master/js/main.js
function gotStream(stream) {
audioStream = stream;
inputPoint = audioContext.createGain();
// Create an AudioNode from the stream.
realAudioInput = audioContext.createMediaStreamSource(stream);
audioInput = realAudioInput;
audioInput.connect(inputPoint);
audioRecorder = new Recorder( inputPoint );
zeroGain = audioContext.createGain();
zeroGain.gain.value = 0.0;
inputPoint.connect( zeroGain );
zeroGain.connect( audioContext.destination );
isAudioCaptured = true;
startRecording();
}
function startRecording() {
audioRecorder.clear();
console.log("starting recording");
isRecording = true;
audioRecorder.record();
}
function stopRecording() {
audioRecorder.stop();
console.log("stopping recording");
isRecording = false;
audioRecorder.getBuffers(buffers => audioRecorder.exportWAV( doneEncoding ));
}
function doneEncoding( blob ) {
var title = $("h1").text();
var url = (window.URL || window.webkitURL).createObjectURL(blob);
takes[title+recIndex] = {
"kind": "audioReady",
"title": title,
"sequence": recIndex,
"extension": ".wav",
"downloadUrl": url
};
chrome.runtime.sendMessage({
"kind": "audioReady",
"title": title,
"sequence": recIndex,
"extension": ".wav",
"downloadUrl": url });
recIndex++;
}