This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
audioCapture.js
74 lines (71 loc) · 2.23 KB
/
audioCapture.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
class AudioObject {
constructor(tabCapture) {
this.capture = tabCapture;
this.audioContext = new AudioContext();
this.audioStream = this.audioContext.createMediaStreamSource(this.capture);
this.audioAnalyser = this.audioContext.createAnalyser();
this.audioAnalyser.fftSize = 1024;
this.audioStream.connect(this.audioContext.destination);
this.audioStream.connect(this.audioAnalyser);
this.data = {
bufferSize: this.audioAnalyser.frequencyBinCount
}
this.data.frequencyData = new Uint8Array(this.data.bufferSize);
this.data.timeData = new Uint8Array(this.data.bufferSize);
}
update() {
this.audioAnalyser.getByteFrequencyData(this.data.frequencyData);
this.audioAnalyser.getByteTimeDomainData(this.data.timeData);
}
close() {
var tracks = this.capture.getTracks();
tracks.forEach((track)=>{
track.stop();
});
this.audioContext.close().then(function() {
delete this.audioContext;
}).bind(this);
}
closeRaw(capture) {
var tracks = capture.getTracks();
tracks.forEach((track)=>{
track.stop();
});
}
}
var audioCapture = {
captures: {
content: [],
add: function(audioObject) {
audioCapture.captures.content.push(audioObject);
for (i=0;i<audioCapture.captures.onAdd.listeners.length;i++) {
audioCapture.captures.onAdd.listeners[i]({event:"update",object:audioObject});
}
},
onAdd: {
addListener: function(listener) {
audioCapture.captures.onAdd.listeners.push(listener);
},
listeners: []
},
getById: function(id) {
for (i=0;i<audioCapture.captures.content.length;i++) {
if (audioCapture.captures.content[i].capture.id == id) {
return audioCapture.captures.content[i];
}
}
return undefined;
}
},
startCapture: function() {
chrome.tabCapture.capture({audio:!0,video:!1},function(tab) {
if (chrome.runtime.lastError != undefined) {
error = chrome.runtime.lastError.message
chrome.runtime.lastError = undefined;
return error;
}
audioCapture.captures.add(new AudioObject(tab));
return undefined;
})
}
}