icculus@9278
|
1 |
/*
|
icculus@9278
|
2 |
Simple DirectMedia Layer
|
slouken@9998
|
3 |
Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
|
icculus@9278
|
4 |
|
icculus@9278
|
5 |
This software is provided 'as-is', without any express or implied
|
icculus@9278
|
6 |
warranty. In no event will the authors be held liable for any damages
|
icculus@9278
|
7 |
arising from the use of this software.
|
icculus@9278
|
8 |
|
icculus@9278
|
9 |
Permission is granted to anyone to use this software for any purpose,
|
icculus@9278
|
10 |
including commercial applications, and to alter it and redistribute it
|
icculus@9278
|
11 |
freely, subject to the following restrictions:
|
icculus@9278
|
12 |
|
icculus@9278
|
13 |
1. The origin of this software must not be misrepresented; you must not
|
icculus@9278
|
14 |
claim that you wrote the original software. If you use this software
|
icculus@9278
|
15 |
in a product, an acknowledgment in the product documentation would be
|
icculus@9278
|
16 |
appreciated but is not required.
|
icculus@9278
|
17 |
2. Altered source versions must be plainly marked as such, and must not be
|
icculus@9278
|
18 |
misrepresented as being the original software.
|
icculus@9278
|
19 |
3. This notice may not be removed or altered from any source distribution.
|
icculus@9278
|
20 |
*/
|
icculus@9278
|
21 |
#include "../../SDL_internal.h"
|
icculus@9278
|
22 |
|
icculus@9278
|
23 |
#if SDL_AUDIO_DRIVER_EMSCRIPTEN
|
icculus@9278
|
24 |
|
icculus@9278
|
25 |
#include "SDL_audio.h"
|
icculus@9278
|
26 |
#include "SDL_log.h"
|
icculus@9278
|
27 |
#include "../SDL_audio_c.h"
|
icculus@9278
|
28 |
#include "SDL_emscriptenaudio.h"
|
icculus@9278
|
29 |
|
icculus@9278
|
30 |
#include <emscripten/emscripten.h>
|
icculus@9278
|
31 |
|
icculus@9278
|
32 |
static int
|
icculus@9278
|
33 |
copyData(_THIS)
|
icculus@9278
|
34 |
{
|
icculus@9278
|
35 |
int byte_len;
|
icculus@9278
|
36 |
|
icculus@9278
|
37 |
if (this->hidden->write_off + this->convert.len_cvt > this->hidden->mixlen) {
|
icculus@9278
|
38 |
if (this->hidden->write_off > this->hidden->read_off) {
|
icculus@9278
|
39 |
SDL_memmove(this->hidden->mixbuf,
|
icculus@9278
|
40 |
this->hidden->mixbuf + this->hidden->read_off,
|
icculus@9278
|
41 |
this->hidden->mixlen - this->hidden->read_off);
|
icculus@9278
|
42 |
this->hidden->write_off = this->hidden->write_off - this->hidden->read_off;
|
icculus@9278
|
43 |
} else {
|
icculus@9278
|
44 |
this->hidden->write_off = 0;
|
icculus@9278
|
45 |
}
|
icculus@9278
|
46 |
this->hidden->read_off = 0;
|
icculus@9278
|
47 |
}
|
icculus@9278
|
48 |
|
icculus@9278
|
49 |
SDL_memcpy(this->hidden->mixbuf + this->hidden->write_off,
|
icculus@9278
|
50 |
this->convert.buf,
|
icculus@9278
|
51 |
this->convert.len_cvt);
|
icculus@9278
|
52 |
this->hidden->write_off += this->convert.len_cvt;
|
icculus@9278
|
53 |
byte_len = this->hidden->write_off - this->hidden->read_off;
|
icculus@9278
|
54 |
|
icculus@9278
|
55 |
return byte_len;
|
icculus@9278
|
56 |
}
|
icculus@9278
|
57 |
|
icculus@9278
|
58 |
static void
|
icculus@9278
|
59 |
HandleAudioProcess(_THIS)
|
icculus@9278
|
60 |
{
|
icculus@9278
|
61 |
Uint8 *buf = NULL;
|
icculus@9278
|
62 |
int byte_len = 0;
|
icculus@9278
|
63 |
int bytes = SDL_AUDIO_BITSIZE(this->spec.format) / 8;
|
icculus@9278
|
64 |
|
icculus@10238
|
65 |
/* Only do something if audio is enabled */
|
icculus@10238
|
66 |
if (!SDL_AtomicGet(&this->enabled) || SDL_AtomicGet(&this->paused)) {
|
icculus@9278
|
67 |
return;
|
icculus@10238
|
68 |
}
|
icculus@9278
|
69 |
|
icculus@9278
|
70 |
if (this->convert.needed) {
|
icculus@10274
|
71 |
const int bytes_in = SDL_AUDIO_BITSIZE(this->convert.src_format) / 8;
|
icculus@10274
|
72 |
|
icculus@9278
|
73 |
if (this->hidden->conv_in_len != 0) {
|
icculus@9278
|
74 |
this->convert.len = this->hidden->conv_in_len * bytes_in * this->spec.channels;
|
icculus@9278
|
75 |
}
|
icculus@9278
|
76 |
|
icculus@9278
|
77 |
(*this->spec.callback) (this->spec.userdata,
|
icculus@9278
|
78 |
this->convert.buf,
|
icculus@9278
|
79 |
this->convert.len);
|
icculus@9278
|
80 |
SDL_ConvertAudio(&this->convert);
|
icculus@9278
|
81 |
buf = this->convert.buf;
|
icculus@9278
|
82 |
byte_len = this->convert.len_cvt;
|
icculus@9278
|
83 |
|
icculus@9278
|
84 |
/* size mismatch*/
|
icculus@9278
|
85 |
if (byte_len != this->spec.size) {
|
icculus@9278
|
86 |
if (!this->hidden->mixbuf) {
|
icculus@9278
|
87 |
this->hidden->mixlen = this->spec.size > byte_len ? this->spec.size * 2 : byte_len * 2;
|
icculus@9278
|
88 |
this->hidden->mixbuf = SDL_malloc(this->hidden->mixlen);
|
icculus@9278
|
89 |
}
|
icculus@9278
|
90 |
|
icculus@9278
|
91 |
/* copy existing data */
|
icculus@9278
|
92 |
byte_len = copyData(this);
|
icculus@9278
|
93 |
|
icculus@9278
|
94 |
/* read more data*/
|
icculus@9278
|
95 |
while (byte_len < this->spec.size) {
|
icculus@9278
|
96 |
(*this->spec.callback) (this->spec.userdata,
|
icculus@9278
|
97 |
this->convert.buf,
|
icculus@9278
|
98 |
this->convert.len);
|
icculus@9278
|
99 |
SDL_ConvertAudio(&this->convert);
|
icculus@9278
|
100 |
byte_len = copyData(this);
|
icculus@9278
|
101 |
}
|
icculus@9278
|
102 |
|
icculus@9278
|
103 |
byte_len = this->spec.size;
|
icculus@9278
|
104 |
buf = this->hidden->mixbuf + this->hidden->read_off;
|
icculus@9278
|
105 |
this->hidden->read_off += byte_len;
|
icculus@9278
|
106 |
}
|
icculus@9278
|
107 |
|
icculus@9278
|
108 |
} else {
|
icculus@9278
|
109 |
if (!this->hidden->mixbuf) {
|
icculus@9278
|
110 |
this->hidden->mixlen = this->spec.size;
|
icculus@9278
|
111 |
this->hidden->mixbuf = SDL_malloc(this->hidden->mixlen);
|
icculus@9278
|
112 |
}
|
icculus@9278
|
113 |
(*this->spec.callback) (this->spec.userdata,
|
icculus@9278
|
114 |
this->hidden->mixbuf,
|
icculus@9278
|
115 |
this->hidden->mixlen);
|
icculus@9278
|
116 |
buf = this->hidden->mixbuf;
|
icculus@9278
|
117 |
byte_len = this->hidden->mixlen;
|
icculus@9278
|
118 |
}
|
icculus@9278
|
119 |
|
icculus@9278
|
120 |
if (buf) {
|
icculus@9278
|
121 |
EM_ASM_ARGS({
|
icculus@9278
|
122 |
var numChannels = SDL2.audio.currentOutputBuffer['numberOfChannels'];
|
icculus@9278
|
123 |
for (var c = 0; c < numChannels; ++c) {
|
icculus@9278
|
124 |
var channelData = SDL2.audio.currentOutputBuffer['getChannelData'](c);
|
icculus@9278
|
125 |
if (channelData.length != $1) {
|
icculus@9278
|
126 |
throw 'Web Audio output buffer length mismatch! Destination size: ' + channelData.length + ' samples vs expected ' + $1 + ' samples!';
|
icculus@9278
|
127 |
}
|
icculus@9278
|
128 |
|
icculus@9278
|
129 |
for (var j = 0; j < $1; ++j) {
|
icculus@9278
|
130 |
channelData[j] = getValue($0 + (j*numChannels + c)*4, 'float');
|
icculus@9278
|
131 |
}
|
icculus@9278
|
132 |
}
|
icculus@9278
|
133 |
}, buf, byte_len / bytes / this->spec.channels);
|
icculus@9278
|
134 |
}
|
icculus@9278
|
135 |
}
|
icculus@9278
|
136 |
|
icculus@9278
|
137 |
static void
|
icculus@10274
|
138 |
HandleCaptureProcess(_THIS)
|
icculus@10274
|
139 |
{
|
icculus@10274
|
140 |
Uint8 *buf;
|
icculus@10274
|
141 |
int buflen;
|
icculus@10274
|
142 |
|
icculus@10274
|
143 |
/* Only do something if audio is enabled */
|
icculus@10274
|
144 |
if (!SDL_AtomicGet(&this->enabled) || SDL_AtomicGet(&this->paused)) {
|
icculus@10274
|
145 |
return;
|
icculus@10274
|
146 |
}
|
icculus@10274
|
147 |
|
icculus@10274
|
148 |
if (this->convert.needed) {
|
icculus@10274
|
149 |
buf = this->convert.buf;
|
icculus@10274
|
150 |
buflen = this->convert.len_cvt;
|
icculus@10274
|
151 |
} else {
|
icculus@10274
|
152 |
if (!this->hidden->mixbuf) {
|
icculus@10274
|
153 |
this->hidden->mixbuf = (Uint8 *) SDL_malloc(this->spec.size);
|
icculus@10274
|
154 |
if (!this->hidden->mixbuf) {
|
icculus@10274
|
155 |
return; /* oh well. */
|
icculus@10274
|
156 |
}
|
icculus@10274
|
157 |
}
|
icculus@10274
|
158 |
buf = this->hidden->mixbuf;
|
icculus@10274
|
159 |
buflen = this->spec.size;
|
icculus@10274
|
160 |
}
|
icculus@10274
|
161 |
|
icculus@10274
|
162 |
EM_ASM_ARGS({
|
icculus@10274
|
163 |
var numChannels = SDL2.capture.currentCaptureBuffer.numberOfChannels;
|
icculus@10274
|
164 |
if (numChannels == 1) { /* fastpath this a little for the common (mono) case. */
|
icculus@10274
|
165 |
var channelData = SDL2.capture.currentCaptureBuffer.getChannelData(0);
|
icculus@10274
|
166 |
if (channelData.length != $1) {
|
icculus@10274
|
167 |
throw 'Web Audio capture buffer length mismatch! Destination size: ' + channelData.length + ' samples vs expected ' + $1 + ' samples!';
|
icculus@10274
|
168 |
}
|
icculus@10274
|
169 |
for (var j = 0; j < $1; ++j) {
|
icculus@10274
|
170 |
setValue($0 + (j * 4), channelData[j], 'float');
|
icculus@10274
|
171 |
}
|
icculus@10274
|
172 |
} else {
|
icculus@10274
|
173 |
for (var c = 0; c < numChannels; ++c) {
|
icculus@10274
|
174 |
var channelData = SDL2.capture.currentCaptureBuffer.getChannelData(c);
|
icculus@10274
|
175 |
if (channelData.length != $1) {
|
icculus@10274
|
176 |
throw 'Web Audio capture buffer length mismatch! Destination size: ' + channelData.length + ' samples vs expected ' + $1 + ' samples!';
|
icculus@10274
|
177 |
}
|
icculus@10274
|
178 |
|
icculus@10274
|
179 |
for (var j = 0; j < $1; ++j) {
|
icculus@10274
|
180 |
setValue($0 + (((j * numChannels) + c) * 4), channelData[j], 'float');
|
icculus@10274
|
181 |
}
|
icculus@10274
|
182 |
}
|
icculus@10274
|
183 |
}
|
icculus@10274
|
184 |
}, buf, (this->spec.size / sizeof (float)) / this->spec.channels);
|
icculus@10274
|
185 |
|
icculus@10274
|
186 |
/* okay, we've got an interleaved float32 array in C now. */
|
icculus@10274
|
187 |
|
icculus@10274
|
188 |
if (this->convert.needed) {
|
icculus@10274
|
189 |
SDL_ConvertAudio(&this->convert);
|
icculus@10274
|
190 |
}
|
icculus@10274
|
191 |
|
icculus@10274
|
192 |
/* Send it to the app. */
|
icculus@10274
|
193 |
(*this->spec.callback) (this->spec.userdata, buf, buflen);
|
icculus@10274
|
194 |
}
|
icculus@10274
|
195 |
|
icculus@10274
|
196 |
|
icculus@10274
|
197 |
|
icculus@10274
|
198 |
static void
|
icculus@10281
|
199 |
EMSCRIPTENAUDIO_CloseDevice(_THIS)
|
icculus@9278
|
200 |
{
|
icculus@10274
|
201 |
EM_ASM_({
|
icculus@10274
|
202 |
if ($0) {
|
icculus@10274
|
203 |
if (SDL2.capture.silenceTimer !== undefined) {
|
icculus@10274
|
204 |
clearTimeout(SDL2.capture.silenceTimer);
|
icculus@10274
|
205 |
}
|
icculus@10301
|
206 |
if (SDL2.capture.stream !== undefined) {
|
icculus@10301
|
207 |
var tracks = SDL2.capture.stream.getAudioTracks();
|
icculus@10301
|
208 |
for (var i = 0; i < tracks.length; i++) {
|
icculus@10301
|
209 |
SDL2.capture.stream.removeTrack(tracks[i]);
|
icculus@10301
|
210 |
}
|
icculus@10301
|
211 |
SDL2.capture.stream = undefined;
|
icculus@10301
|
212 |
}
|
icculus@10274
|
213 |
if (SDL2.capture.scriptProcessorNode !== undefined) {
|
icculus@10301
|
214 |
SDL2.capture.scriptProcessorNode.onaudioprocess = function(audioProcessingEvent) {};
|
icculus@10274
|
215 |
SDL2.capture.scriptProcessorNode.disconnect();
|
icculus@10274
|
216 |
SDL2.capture.scriptProcessorNode = undefined;
|
icculus@10274
|
217 |
}
|
icculus@10274
|
218 |
if (SDL2.capture.mediaStreamNode !== undefined) {
|
icculus@10274
|
219 |
SDL2.capture.mediaStreamNode.disconnect();
|
icculus@10274
|
220 |
SDL2.capture.mediaStreamNode = undefined;
|
icculus@10274
|
221 |
}
|
icculus@10274
|
222 |
if (SDL2.capture.silenceBuffer !== undefined) {
|
icculus@10274
|
223 |
SDL2.capture.silenceBuffer = undefined
|
icculus@10274
|
224 |
}
|
icculus@10274
|
225 |
SDL2.capture = undefined;
|
icculus@10274
|
226 |
} else {
|
icculus@10274
|
227 |
if (SDL2.audio.scriptProcessorNode != undefined) {
|
icculus@10274
|
228 |
SDL2.audio.scriptProcessorNode.disconnect();
|
icculus@10274
|
229 |
SDL2.audio.scriptProcessorNode = undefined;
|
icculus@10274
|
230 |
}
|
icculus@10274
|
231 |
SDL2.audio = undefined;
|
icculus@10274
|
232 |
}
|
icculus@10274
|
233 |
if ((SDL2.audioContext !== undefined) && (SDL2.audio === undefined) && (SDL2.capture === undefined)) {
|
icculus@10274
|
234 |
SDL2.audioContext.close();
|
icculus@10274
|
235 |
SDL2.audioContext = undefined;
|
icculus@10274
|
236 |
}
|
icculus@10274
|
237 |
}, this->iscapture);
|
icculus@10274
|
238 |
|
icculus@10255
|
239 |
SDL_free(this->hidden->mixbuf);
|
icculus@10255
|
240 |
SDL_free(this->hidden);
|
icculus@9278
|
241 |
}
|
icculus@9278
|
242 |
|
icculus@9278
|
243 |
static int
|
icculus@10281
|
244 |
EMSCRIPTENAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
|
icculus@9278
|
245 |
{
|
icculus@9278
|
246 |
SDL_bool valid_format = SDL_FALSE;
|
icculus@10274
|
247 |
SDL_AudioFormat test_format;
|
icculus@9278
|
248 |
int i;
|
icculus@9278
|
249 |
float f;
|
philipp@9344
|
250 |
int result;
|
icculus@9278
|
251 |
|
icculus@10274
|
252 |
/* based on parts of library_sdl.js */
|
icculus@10274
|
253 |
|
icculus@10274
|
254 |
/* create context (TODO: this puts stuff in the global namespace...)*/
|
icculus@10274
|
255 |
result = EM_ASM_INT({
|
icculus@10274
|
256 |
if(typeof(SDL2) === 'undefined') {
|
icculus@10274
|
257 |
SDL2 = {};
|
icculus@10274
|
258 |
}
|
icculus@10274
|
259 |
if (!$0) {
|
icculus@10274
|
260 |
SDL2.audio = {};
|
icculus@10274
|
261 |
} else {
|
icculus@10274
|
262 |
SDL2.capture = {};
|
icculus@10274
|
263 |
}
|
icculus@10274
|
264 |
|
icculus@10274
|
265 |
if (!SDL2.audioContext) {
|
icculus@10274
|
266 |
if (typeof(AudioContext) !== 'undefined') {
|
icculus@10274
|
267 |
SDL2.audioContext = new AudioContext();
|
icculus@10274
|
268 |
} else if (typeof(webkitAudioContext) !== 'undefined') {
|
icculus@10274
|
269 |
SDL2.audioContext = new webkitAudioContext();
|
icculus@10274
|
270 |
}
|
icculus@10274
|
271 |
}
|
icculus@10274
|
272 |
return SDL2.audioContext === undefined ? -1 : 0;
|
icculus@10274
|
273 |
}, iscapture);
|
icculus@10274
|
274 |
if (result < 0) {
|
icculus@10274
|
275 |
return SDL_SetError("Web Audio API is not available!");
|
icculus@10274
|
276 |
}
|
icculus@10274
|
277 |
|
icculus@10274
|
278 |
test_format = SDL_FirstAudioFormat(this->spec.format);
|
icculus@9278
|
279 |
while ((!valid_format) && (test_format)) {
|
icculus@9278
|
280 |
switch (test_format) {
|
icculus@9278
|
281 |
case AUDIO_F32: /* web audio only supports floats */
|
icculus@9278
|
282 |
this->spec.format = test_format;
|
icculus@9278
|
283 |
|
icculus@9278
|
284 |
valid_format = SDL_TRUE;
|
icculus@9278
|
285 |
break;
|
icculus@9278
|
286 |
}
|
icculus@9278
|
287 |
test_format = SDL_NextAudioFormat();
|
icculus@9278
|
288 |
}
|
icculus@9278
|
289 |
|
icculus@9278
|
290 |
if (!valid_format) {
|
icculus@9278
|
291 |
/* Didn't find a compatible format :( */
|
icculus@9278
|
292 |
return SDL_SetError("No compatible audio format!");
|
icculus@9278
|
293 |
}
|
icculus@9278
|
294 |
|
icculus@9278
|
295 |
/* Initialize all variables that we clean on shutdown */
|
icculus@9278
|
296 |
this->hidden = (struct SDL_PrivateAudioData *)
|
icculus@9278
|
297 |
SDL_malloc((sizeof *this->hidden));
|
icculus@9278
|
298 |
if (this->hidden == NULL) {
|
icculus@9278
|
299 |
return SDL_OutOfMemory();
|
icculus@9278
|
300 |
}
|
icculus@10257
|
301 |
SDL_zerop(this->hidden);
|
icculus@9278
|
302 |
|
icculus@9278
|
303 |
/* limit to native freq */
|
icculus@10274
|
304 |
const int sampleRate = EM_ASM_INT_V({
|
icculus@10274
|
305 |
return SDL2.audioContext.sampleRate;
|
icculus@9278
|
306 |
});
|
icculus@9278
|
307 |
|
icculus@9278
|
308 |
if(this->spec.freq != sampleRate) {
|
icculus@9278
|
309 |
for (i = this->spec.samples; i > 0; i--) {
|
icculus@9278
|
310 |
f = (float)i / (float)sampleRate * (float)this->spec.freq;
|
icculus@9278
|
311 |
if (SDL_floor(f) == f) {
|
icculus@9278
|
312 |
this->hidden->conv_in_len = SDL_floor(f);
|
icculus@9278
|
313 |
break;
|
icculus@9278
|
314 |
}
|
icculus@9278
|
315 |
}
|
icculus@9278
|
316 |
|
icculus@9278
|
317 |
this->spec.freq = sampleRate;
|
icculus@9278
|
318 |
}
|
icculus@9278
|
319 |
|
icculus@9278
|
320 |
SDL_CalculateAudioSpec(&this->spec);
|
icculus@9278
|
321 |
|
icculus@10274
|
322 |
if (iscapture) {
|
icculus@10274
|
323 |
/* The idea is to take the capture media stream, hook it up to an
|
icculus@10274
|
324 |
audio graph where we can pass it through a ScriptProcessorNode
|
icculus@10274
|
325 |
to access the raw PCM samples and push them to the SDL app's
|
icculus@10274
|
326 |
callback. From there, we "process" the audio data into silence
|
icculus@10274
|
327 |
and forget about it. */
|
icculus@10274
|
328 |
|
icculus@10274
|
329 |
/* This should, strictly speaking, use MediaRecorder for capture, but
|
icculus@10274
|
330 |
this API is cleaner to use and better supported, and fires a
|
icculus@10274
|
331 |
callback whenever there's enough data to fire down into the app.
|
icculus@10274
|
332 |
The downside is that we are spending CPU time silencing a buffer
|
icculus@10274
|
333 |
that the audiocontext uselessly mixes into any output. On the
|
icculus@10274
|
334 |
upside, both of those things are not only run in native code in
|
icculus@10274
|
335 |
the browser, they're probably SIMD code, too. MediaRecorder
|
icculus@10274
|
336 |
feels like it's a pretty inefficient tapdance in similar ways,
|
icculus@10274
|
337 |
to be honest. */
|
icculus@10274
|
338 |
|
icculus@10274
|
339 |
EM_ASM_({
|
icculus@10274
|
340 |
var have_microphone = function(stream) {
|
icculus@10283
|
341 |
//console.log('SDL audio capture: we have a microphone! Replacing silence callback.');
|
icculus@10283
|
342 |
if (SDL2.capture.silenceTimer !== undefined) {
|
icculus@10283
|
343 |
clearTimeout(SDL2.capture.silenceTimer);
|
icculus@10283
|
344 |
SDL2.capture.silenceTimer = undefined;
|
icculus@10283
|
345 |
}
|
icculus@10274
|
346 |
SDL2.capture.mediaStreamNode = SDL2.audioContext.createMediaStreamSource(stream);
|
icculus@10274
|
347 |
SDL2.capture.scriptProcessorNode = SDL2.audioContext.createScriptProcessor($1, $0, 1);
|
icculus@10274
|
348 |
SDL2.capture.scriptProcessorNode.onaudioprocess = function(audioProcessingEvent) {
|
icculus@10274
|
349 |
audioProcessingEvent.outputBuffer.getChannelData(0).fill(0.0);
|
icculus@10274
|
350 |
SDL2.capture.currentCaptureBuffer = audioProcessingEvent.inputBuffer;
|
icculus@10274
|
351 |
Runtime.dynCall('vi', $2, [$3]);
|
icculus@10274
|
352 |
};
|
icculus@10274
|
353 |
SDL2.capture.mediaStreamNode.connect(SDL2.capture.scriptProcessorNode);
|
icculus@10274
|
354 |
SDL2.capture.scriptProcessorNode.connect(SDL2.audioContext.destination);
|
icculus@10301
|
355 |
SDL2.capture.stream = stream;
|
icculus@10274
|
356 |
};
|
icculus@10274
|
357 |
|
icculus@10274
|
358 |
var no_microphone = function(error) {
|
icculus@10283
|
359 |
//console.log('SDL audio capture: we DO NOT have a microphone! (' + error.name + ')...leaving silence callback running.');
|
icculus@10274
|
360 |
};
|
icculus@10274
|
361 |
|
icculus@10274
|
362 |
/* we write silence to the audio callback until the microphone is available (user approves use, etc). */
|
icculus@10274
|
363 |
SDL2.capture.silenceBuffer = SDL2.audioContext.createBuffer($0, $1, SDL2.audioContext.sampleRate);
|
icculus@10274
|
364 |
SDL2.capture.silenceBuffer.getChannelData(0).fill(0.0);
|
icculus@10274
|
365 |
var silence_callback = function() {
|
icculus@10274
|
366 |
SDL2.capture.currentCaptureBuffer = SDL2.capture.silenceBuffer;
|
icculus@10274
|
367 |
Runtime.dynCall('vi', $2, [$3]);
|
icculus@10274
|
368 |
};
|
icculus@10274
|
369 |
|
icculus@10276
|
370 |
SDL2.capture.silenceTimer = setTimeout(silence_callback, ($1 / SDL2.audioContext.sampleRate) * 1000);
|
icculus@10274
|
371 |
|
icculus@10274
|
372 |
if ((navigator.mediaDevices !== undefined) && (navigator.mediaDevices.getUserMedia !== undefined)) {
|
icculus@10274
|
373 |
navigator.mediaDevices.getUserMedia({ audio: true, video: false }).then(have_microphone).catch(no_microphone);
|
icculus@10274
|
374 |
} else if (navigator.webkitGetUserMedia !== undefined) {
|
icculus@10274
|
375 |
navigator.webkitGetUserMedia({ audio: true, video: false }, have_microphone, no_microphone);
|
icculus@10274
|
376 |
}
|
icculus@10274
|
377 |
}, this->spec.channels, this->spec.samples, HandleCaptureProcess, this);
|
icculus@10274
|
378 |
} else {
|
icculus@10274
|
379 |
/* setup a ScriptProcessorNode */
|
icculus@10274
|
380 |
EM_ASM_ARGS({
|
icculus@10274
|
381 |
SDL2.audio.scriptProcessorNode = SDL2.audioContext['createScriptProcessor']($1, 0, $0);
|
icculus@10274
|
382 |
SDL2.audio.scriptProcessorNode['onaudioprocess'] = function (e) {
|
icculus@10274
|
383 |
SDL2.audio.currentOutputBuffer = e['outputBuffer'];
|
icculus@10274
|
384 |
Runtime.dynCall('vi', $2, [$3]);
|
icculus@10274
|
385 |
};
|
icculus@10274
|
386 |
SDL2.audio.scriptProcessorNode['connect'](SDL2.audioContext['destination']);
|
icculus@10274
|
387 |
}, this->spec.channels, this->spec.samples, HandleAudioProcess, this);
|
icculus@10274
|
388 |
}
|
icculus@10274
|
389 |
|
icculus@9278
|
390 |
return 0;
|
icculus@9278
|
391 |
}
|
icculus@9278
|
392 |
|
icculus@9278
|
393 |
static int
|
icculus@10281
|
394 |
EMSCRIPTENAUDIO_Init(SDL_AudioDriverImpl * impl)
|
icculus@9278
|
395 |
{
|
icculus@9278
|
396 |
/* Set the function pointers */
|
icculus@10281
|
397 |
impl->OpenDevice = EMSCRIPTENAUDIO_OpenDevice;
|
icculus@10281
|
398 |
impl->CloseDevice = EMSCRIPTENAUDIO_CloseDevice;
|
icculus@9278
|
399 |
|
icculus@9278
|
400 |
impl->OnlyHasDefaultOutputDevice = 1;
|
icculus@9278
|
401 |
|
icculus@9278
|
402 |
/* no threads here */
|
icculus@9278
|
403 |
impl->SkipMixerLock = 1;
|
icculus@9278
|
404 |
impl->ProvidesOwnCallbackThread = 1;
|
icculus@9278
|
405 |
|
icculus@9278
|
406 |
/* check availability */
|
icculus@10274
|
407 |
const int available = EM_ASM_INT_V({
|
icculus@9278
|
408 |
if (typeof(AudioContext) !== 'undefined') {
|
icculus@9278
|
409 |
return 1;
|
icculus@9278
|
410 |
} else if (typeof(webkitAudioContext) !== 'undefined') {
|
icculus@9278
|
411 |
return 1;
|
icculus@9278
|
412 |
}
|
icculus@9278
|
413 |
return 0;
|
icculus@9278
|
414 |
});
|
icculus@9278
|
415 |
|
philipp@9831
|
416 |
if (!available) {
|
philipp@9831
|
417 |
SDL_SetError("No audio context available");
|
philipp@9831
|
418 |
}
|
philipp@9831
|
419 |
|
icculus@10274
|
420 |
const int capture_available = available && EM_ASM_INT_V({
|
icculus@10274
|
421 |
if ((typeof(navigator.mediaDevices) !== 'undefined') && (typeof(navigator.mediaDevices.getUserMedia) !== 'undefined')) {
|
icculus@10274
|
422 |
return 1;
|
icculus@10274
|
423 |
} else if (typeof(navigator.webkitGetUserMedia) !== 'undefined') {
|
icculus@10274
|
424 |
return 1;
|
icculus@10274
|
425 |
}
|
icculus@10274
|
426 |
return 0;
|
icculus@10274
|
427 |
});
|
icculus@10274
|
428 |
|
icculus@10274
|
429 |
impl->HasCaptureSupport = capture_available ? SDL_TRUE : SDL_FALSE;
|
icculus@10274
|
430 |
impl->OnlyHasDefaultCaptureDevice = capture_available ? SDL_TRUE : SDL_FALSE;
|
icculus@10274
|
431 |
|
icculus@9278
|
432 |
return available;
|
icculus@9278
|
433 |
}
|
icculus@9278
|
434 |
|
icculus@10281
|
435 |
AudioBootStrap EMSCRIPTENAUDIO_bootstrap = {
|
icculus@10281
|
436 |
"emscripten", "SDL emscripten audio driver", EMSCRIPTENAUDIO_Init, 0
|
icculus@9278
|
437 |
};
|
icculus@9278
|
438 |
|
icculus@9278
|
439 |
#endif /* SDL_AUDIO_DRIVER_EMSCRIPTEN */
|
icculus@9278
|
440 |
|
icculus@9278
|
441 |
/* vi: set ts=4 sw=4 expandtab: */
|