-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathlibrary_html5audio.js
More file actions
216 lines (181 loc) · 7.08 KB
/
library_html5audio.js
File metadata and controls
216 lines (181 loc) · 7.08 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
var LibraryHTML5Audio = {
$AUDIO: {
players: [],
lastSoundID: 0,
},
html5audio_list_devices: function(){
if (!navigator.mediaDevices.enumerateDevices) {
console.log("enumerateDevices() not supported.");
} else {
// List cameras and microphones.
navigator.mediaDevices.enumerateDevices()
.then((devices) => {
devices.forEach((device) => {
if(device.kind == "audioinput"){
console.log(`${device.kind}: ${device.label} id = ${device.deviceId}`);
}
});
return devices;
})
.catch((err) => {
console.error(`${err.name}: ${err.message}`);
});
}
},
html5audio_context_create: function () {
try {
// Fix up for prefixing
window.AudioContext = window.AudioContext || window.webkitAudioContext;
var context = new AudioContext({});
var mediaStreamAudioDestinationNode = new MediaStreamAudioDestinationNode(context);
// Fix issue with chrome autoplay policy
document.addEventListener('mousedown', function cb(event) {
context.resume();
event.currentTarget.removeEventListener(event.type, cb);
});
AUDIO.context = context;
AUDIO.contextStream = mediaStreamAudioDestinationNode;
var fft = context.createAnalyser();
fft.smoothingTimeConstant = 0;
fft.connect(AUDIO.context.destination);
fft.connect(AUDIO.contextStream);
fft.maxDecibels = 0;
fft.minDecibels = -100;
AUDIO.fft = fft;
return 0;
} catch (e) {
console.log('Web Audio API is not supported in this browser', e);
return -1;
}
},
html5audio_context_start: function () {
AUDIO.context.resume();
},
html5audio_context_stop: function () {
AUDIO.context.suspend();
},
html5audio_context_spectrum: function (bands, spectrum) {
AUDIO.fft.fftSize = bands * 2;
var spectrumArray = Module.HEAPF32.subarray(spectrum >> 2, (spectrum >> 2) + bands);
AUDIO.fft.getFloatFrequencyData(spectrumArray);
},
html5audio_context_samplerate: function () {
return AUDIO.context.sampleRate.value;
},
html5audio_sound_load: function (url) {
var audio = document.createElement('audio');
var sound_id = AUDIO.lastSoundID++;
AUDIO.players[sound_id] = audio;
AUDIO.players[sound_id].src = UTF8ToString(url);
var source = AUDIO.context.createMediaElementSource(AUDIO.players[sound_id]);
AUDIO.players[sound_id].soundPan = AUDIO.context.createStereoPanner();
source.connect(AUDIO.players[sound_id].soundPan).connect(AUDIO.fft);
return sound_id;
},
html5audio_sound_play: function (sound_id, offset) {
AUDIO.players[sound_id].play(offset);
},
html5audio_sound_stop: function (sound_id) {
AUDIO.players[sound_id].currentTime = 0;
AUDIO.players[sound_id].pause();
},
html5audio_sound_pause: function (sound_id) {
AUDIO.players[sound_id].pause();
},
html5audio_sound_rate: function (sound_id) {
return AUDIO.players[sound_id].playbackRate;
},
html5audio_sound_set_rate: function (sound_id, rate) {
AUDIO.players[sound_id].playbackRate = rate;
},
html5audio_sound_done: function (sound_id) {
return AUDIO.players[sound_id].done;
},
html5audio_sound_duration: function (sound_id) {
return AUDIO.players[sound_id].duration;
},
html5audio_sound_position: function (sound_id) {
return AUDIO.players[sound_id].currentTime;
},
html5audio_sound_set_position: function (sound_id, position) {
AUDIO.players[sound_id].currentTime = position * AUDIO.players[sound_id].duration;
},
html5audio_sound_set_loop: function (sound_id, loop) {
AUDIO.players[sound_id].loop = true;
},
html5audio_sound_set_volume: function (sound_id, volume) {
AUDIO.players[sound_id].volume = volume;
},
html5audio_sound_volume: function (sound_id) {
return AUDIO.players[sound_id].volume;
},
html5audio_sound_set_pan: function (sound_id, pan) {
AUDIO.players[sound_id].soundPan.pan.value = pan;
},
html5audio_sound_pan: function (sound_id) {
return AUDIO.players[sound_id].soundPan.pan.value;
},
html5audio_sound_free: function (sound_id) {
if(AUDIO.players[sound_id] != undefined){
AUDIO.players[sound_id].pause();
URL.revokeObjectURL(AUDIO.players[sound_id].src);
}
},
html5audio_stream_create: function(bufferSize, inputChannels, outputChannels, inbuffer, outbuffer, callback, userData){
var stream = AUDIO.context.createScriptProcessor(bufferSize,inputChannels,outputChannels);
var inbufferArray = Module.HEAPF32.subarray(inbuffer>>2,(inbuffer>>2)+bufferSize*inputChannels);
var outbufferArray = Module.HEAPF32.subarray(outbuffer>>2,(outbuffer>>2)+bufferSize*outputChannels);
stream.onaudioprocess = function(event){
var i, j, c;
if (inputChannels > 0){
for (c = 0; c < inputChannels; ++c){
var inChannel = event.inputBuffer.getChannelData(c);
for (i = 0, j = c; i < bufferSize; ++i, j += inputChannels){
inbufferArray[j] = inChannel[i];
}
}
}
{{{ makeDynCall('viiii', 'callback') }}}(bufferSize, inputChannels, outputChannels, userData);
if (outputChannels > 0){
for (c = 0;c < outputChannels; ++c){
var outChannel = event.outputBuffer.getChannelData(c);
for (i = 0,j = c;i<bufferSize; ++i, j += outputChannels){
outChannel[i] = outbufferArray[j];
}
}
}
};
if (inputChannels > 0){
navigator.getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia;
if (navigator.getUserMedia){
navigator.getUserMedia(
{audio: true},
function (audioIn){
var mediaElement = AUDIO.context.createMediaStreamSource(audioIn);
mediaElement.connect(stream);
AUDIO.mediaElement = mediaElement;
},
function (error){
console.log("error creating audio in",error);
});
}
}
stream.connect(AUDIO.fft);
AUDIO.stream = stream;
},
html5audio_stream_free: function () {
return AUDIO.stream = null;
return AUDIO.mediaElement = null;
},
html5audio_sound_is_loaded: function (sound_id) {
if (AUDIO.players[sound_id].src != undefined) {
return true;
}
return false;
}
}
autoAddDeps(LibraryHTML5Audio, '$AUDIO');
mergeInto(LibraryManager.library, LibraryHTML5Audio);