src/audio/emscripten/SDL_emscriptenaudio.c
changeset 10283 1b8594db77f1
parent 10281 2a002e96888f
child 10301 a25d9c643cfb
equal deleted inserted replaced
10282:8bd5eb9d96c7 10283:1b8594db77f1
   328            feels like it's a pretty inefficient tapdance in similar ways,
   328            feels like it's a pretty inefficient tapdance in similar ways,
   329            to be honest. */
   329            to be honest. */
   330 
   330 
   331         EM_ASM_({
   331         EM_ASM_({
   332             var have_microphone = function(stream) {
   332             var have_microphone = function(stream) {
   333                 clearTimeout(SDL2.capture.silenceTimer);
   333                 //console.log('SDL audio capture: we have a microphone! Replacing silence callback.');
   334                 SDL2.capture.silenceTimer = undefined;
   334                 if (SDL2.capture.silenceTimer !== undefined) {
       
   335                     clearTimeout(SDL2.capture.silenceTimer);
       
   336                     SDL2.capture.silenceTimer = undefined;
       
   337                 }
   335                 SDL2.capture.mediaStreamNode = SDL2.audioContext.createMediaStreamSource(stream);
   338                 SDL2.capture.mediaStreamNode = SDL2.audioContext.createMediaStreamSource(stream);
   336                 SDL2.capture.scriptProcessorNode = SDL2.audioContext.createScriptProcessor($1, $0, 1);
   339                 SDL2.capture.scriptProcessorNode = SDL2.audioContext.createScriptProcessor($1, $0, 1);
   337                 SDL2.capture.scriptProcessorNode.onaudioprocess = function(audioProcessingEvent) {
   340                 SDL2.capture.scriptProcessorNode.onaudioprocess = function(audioProcessingEvent) {
   338                     audioProcessingEvent.outputBuffer.getChannelData(0).fill(0.0);
   341                     audioProcessingEvent.outputBuffer.getChannelData(0).fill(0.0);
   339                     SDL2.capture.currentCaptureBuffer = audioProcessingEvent.inputBuffer;
   342                     SDL2.capture.currentCaptureBuffer = audioProcessingEvent.inputBuffer;
   342                 SDL2.capture.mediaStreamNode.connect(SDL2.capture.scriptProcessorNode);
   345                 SDL2.capture.mediaStreamNode.connect(SDL2.capture.scriptProcessorNode);
   343                 SDL2.capture.scriptProcessorNode.connect(SDL2.audioContext.destination);
   346                 SDL2.capture.scriptProcessorNode.connect(SDL2.audioContext.destination);
   344             };
   347             };
   345 
   348 
   346             var no_microphone = function(error) {
   349             var no_microphone = function(error) {
   347                 console.log('we DO NOT have a microphone! (' + error.name + ')...leaving silence callback running.');
   350                 //console.log('SDL audio capture: we DO NOT have a microphone! (' + error.name + ')...leaving silence callback running.');
   348             };
   351             };
   349 
   352 
   350             /* we write silence to the audio callback until the microphone is available (user approves use, etc). */
   353             /* we write silence to the audio callback until the microphone is available (user approves use, etc). */
   351             SDL2.capture.silenceBuffer = SDL2.audioContext.createBuffer($0, $1, SDL2.audioContext.sampleRate);
   354             SDL2.capture.silenceBuffer = SDL2.audioContext.createBuffer($0, $1, SDL2.audioContext.sampleRate);
   352             SDL2.capture.silenceBuffer.getChannelData(0).fill(0.0);
   355             SDL2.capture.silenceBuffer.getChannelData(0).fill(0.0);
   353 
       
   354             var silence_callback = function() {
   356             var silence_callback = function() {
   355                 SDL2.capture.currentCaptureBuffer = SDL2.capture.silenceBuffer;
   357                 SDL2.capture.currentCaptureBuffer = SDL2.capture.silenceBuffer;
   356                 Runtime.dynCall('vi', $2, [$3]);
   358                 Runtime.dynCall('vi', $2, [$3]);
   357             };
   359             };
   358 
   360