src/audio/emscripten/SDL_emscriptenaudio.c
changeset 12580 e5c1992fcd8b
parent 12503 806492103856
child 12581 62f26ad46749
equal deleted inserted replaced
12579:8dc4e45fbe2b 12580:e5c1992fcd8b
    33 static void
    33 static void
    34 FeedAudioDevice(_THIS, const void *buf, const int buflen)
    34 FeedAudioDevice(_THIS, const void *buf, const int buflen)
    35 {
    35 {
    36     const int framelen = (SDL_AUDIO_BITSIZE(this->spec.format) / 8) * this->spec.channels;
    36     const int framelen = (SDL_AUDIO_BITSIZE(this->spec.format) / 8) * this->spec.channels;
    37     EM_ASM_ARGS({
    37     EM_ASM_ARGS({
       
    38         var SDL2 = Module['SDL2'];
    38         var numChannels = SDL2.audio.currentOutputBuffer['numberOfChannels'];
    39         var numChannels = SDL2.audio.currentOutputBuffer['numberOfChannels'];
    39         for (var c = 0; c < numChannels; ++c) {
    40         for (var c = 0; c < numChannels; ++c) {
    40             var channelData = SDL2.audio.currentOutputBuffer['getChannelData'](c);
    41             var channelData = SDL2.audio.currentOutputBuffer['getChannelData'](c);
    41             if (channelData.length != $1) {
    42             if (channelData.length != $1) {
    42                 throw 'Web Audio output buffer length mismatch! Destination size: ' + channelData.length + ' samples vs expected ' + $1 + ' samples!';
    43                 throw 'Web Audio output buffer length mismatch! Destination size: ' + channelData.length + ' samples vs expected ' + $1 + ' samples!';
    98         SDL_AudioStreamClear(this->stream);
    99         SDL_AudioStreamClear(this->stream);
    99         return;
   100         return;
   100     }
   101     }
   101 
   102 
   102     EM_ASM_ARGS({
   103     EM_ASM_ARGS({
       
   104         var SDL2 = Module['SDL2'];
   103         var numChannels = SDL2.capture.currentCaptureBuffer.numberOfChannels;
   105         var numChannels = SDL2.capture.currentCaptureBuffer.numberOfChannels;
   104         for (var c = 0; c < numChannels; ++c) {
   106         for (var c = 0; c < numChannels; ++c) {
   105             var channelData = SDL2.capture.currentCaptureBuffer.getChannelData(c);
   107             var channelData = SDL2.capture.currentCaptureBuffer.getChannelData(c);
   106             if (channelData.length != $1) {
   108             if (channelData.length != $1) {
   107                 throw 'Web Audio capture buffer length mismatch! Destination size: ' + channelData.length + ' samples vs expected ' + $1 + ' samples!';
   109                 throw 'Web Audio capture buffer length mismatch! Destination size: ' + channelData.length + ' samples vs expected ' + $1 + ' samples!';
   143 
   145 
   144 static void
   146 static void
   145 EMSCRIPTENAUDIO_CloseDevice(_THIS)
   147 EMSCRIPTENAUDIO_CloseDevice(_THIS)
   146 {
   148 {
   147     EM_ASM_({
   149     EM_ASM_({
       
   150         var SDL2 = Module['SDL2'];
   148         if ($0) {
   151         if ($0) {
   149             if (SDL2.capture.silenceTimer !== undefined) {
   152             if (SDL2.capture.silenceTimer !== undefined) {
   150                 clearTimeout(SDL2.capture.silenceTimer);
   153                 clearTimeout(SDL2.capture.silenceTimer);
   151             }
   154             }
   152             if (SDL2.capture.stream !== undefined) {
   155             if (SDL2.capture.stream !== undefined) {
   194     SDL_AudioFormat test_format;
   197     SDL_AudioFormat test_format;
   195     int result;
   198     int result;
   196 
   199 
   197     /* based on parts of library_sdl.js */
   200     /* based on parts of library_sdl.js */
   198 
   201 
   199     /* create context (TODO: this puts stuff in the global namespace...)*/
   202     /* create context */
   200     result = EM_ASM_INT({
   203     result = EM_ASM_INT({
   201         if(typeof(SDL2) === 'undefined') {
   204         if(typeof(Module['SDL2']) === 'undefined') {
   202             SDL2 = {};
   205             Module['SDL2'] = {};
   203         }
   206         }
       
   207         var SDL2 = Module['SDL2'];
   204         if (!$0) {
   208         if (!$0) {
   205             SDL2.audio = {};
   209             SDL2.audio = {};
   206         } else {
   210         } else {
   207             SDL2.capture = {};
   211             SDL2.capture = {};
   208         }
   212         }
   247     SDL_zerop(this->hidden);
   251     SDL_zerop(this->hidden);
   248 #endif
   252 #endif
   249     this->hidden = (struct SDL_PrivateAudioData *)0x1;
   253     this->hidden = (struct SDL_PrivateAudioData *)0x1;
   250 
   254 
   251     /* limit to native freq */
   255     /* limit to native freq */
   252     this->spec.freq = EM_ASM_INT_V({ return SDL2.audioContext.sampleRate; });
   256     this->spec.freq = EM_ASM_INT_V({
       
   257       var SDL2 = Module['SDL2'];
       
   258       return SDL2.audioContext.sampleRate;
       
   259     });
   253 
   260 
   254     SDL_CalculateAudioSpec(&this->spec);
   261     SDL_CalculateAudioSpec(&this->spec);
   255 
   262 
   256     if (iscapture) {
   263     if (iscapture) {
   257         /* The idea is to take the capture media stream, hook it up to an
   264         /* The idea is to take the capture media stream, hook it up to an
   269            the browser, they're probably SIMD code, too. MediaRecorder
   276            the browser, they're probably SIMD code, too. MediaRecorder
   270            feels like it's a pretty inefficient tapdance in similar ways,
   277            feels like it's a pretty inefficient tapdance in similar ways,
   271            to be honest. */
   278            to be honest. */
   272 
   279 
   273         EM_ASM_({
   280         EM_ASM_({
       
   281             var SDL2 = Module['SDL2'];
   274             var have_microphone = function(stream) {
   282             var have_microphone = function(stream) {
   275                 //console.log('SDL audio capture: we have a microphone! Replacing silence callback.');
   283                 //console.log('SDL audio capture: we have a microphone! Replacing silence callback.');
   276                 if (SDL2.capture.silenceTimer !== undefined) {
   284                 if (SDL2.capture.silenceTimer !== undefined) {
   277                     clearTimeout(SDL2.capture.silenceTimer);
   285                     clearTimeout(SDL2.capture.silenceTimer);
   278                     SDL2.capture.silenceTimer = undefined;
   286                     SDL2.capture.silenceTimer = undefined;
   311             }
   319             }
   312         }, this->spec.channels, this->spec.samples, HandleCaptureProcess, this);
   320         }, this->spec.channels, this->spec.samples, HandleCaptureProcess, this);
   313     } else {
   321     } else {
   314         /* setup a ScriptProcessorNode */
   322         /* setup a ScriptProcessorNode */
   315         EM_ASM_ARGS({
   323         EM_ASM_ARGS({
       
   324             var SDL2 = Module['SDL2'];
   316             SDL2.audio.scriptProcessorNode = SDL2.audioContext['createScriptProcessor']($1, 0, $0);
   325             SDL2.audio.scriptProcessorNode = SDL2.audioContext['createScriptProcessor']($1, 0, $0);
   317             SDL2.audio.scriptProcessorNode['onaudioprocess'] = function (e) {
   326             SDL2.audio.scriptProcessorNode['onaudioprocess'] = function (e) {
   318                 if ((SDL2 === undefined) || (SDL2.audio === undefined)) { return; }
   327                 if ((SDL2 === undefined) || (SDL2.audio === undefined)) { return; }
   319                 SDL2.audio.currentOutputBuffer = e['outputBuffer'];
   328                 SDL2.audio.currentOutputBuffer = e['outputBuffer'];
   320                 Runtime.dynCall('vi', $2, [$3]);
   329                 Runtime.dynCall('vi', $2, [$3]);