-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathaudioReceiver.js
56 lines (42 loc) · 1.12 KB
/
audioReceiver.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
var audioReceiver = (function () {
var audiostream = new AudioStream()
var player;
function str2ab(str) {
var buf = new ArrayBuffer(str.length);
var bufView = new Uint8Array(buf);
for (var i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
bufView = null;
return buf;
}
function receivedStarted() {
console.log("Disable button?");
}
function receivedStopped() {
player.stop();
}
function receivedMetadata(metadata) {
player = audiostream.getNewPlayer(metadata);
}
function receivedAudioData(audioData) {
let byteCharacters = atob(audioData);
let byteArray = str2ab(byteCharacters);
player.play(byteArray);
}
function receivedEvent(data) {
if (data.event == 'started') {
receivedStarted()
} else if (data.event == 'stopped') {
receivedStopped()
} else if (data.event == 'metadata') {
var metadata = JSON.parse(data.data);
receivedMetadata(metadata);
} else if (data.event == 'binary') {
receivedAudioData(data.data)
}
}
return {
receive: receivedEvent
};
})();