src/audio/emscripten/SDL_emscriptenaudio.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 05 Jan 2017 21:31:02 -0500
changeset 10760 0333e86613d5
parent 10737 3406a0f8b041
child 10761 6afae547c925
permissions -rw-r--r--
emscriptenaudio: Reworked to use SDL_AudioStream.
icculus@9278
     1
/*
icculus@9278
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 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@10760
    29
#include "SDL_assert.h"
icculus@9278
    30
icculus@9278
    31
#include <emscripten/emscripten.h>
icculus@9278
    32
icculus@10760
    33
static void
icculus@10760
    34
FeedAudioDevice(_THIS, const void *buf, const int buflen)
icculus@9278
    35
{
icculus@10760
    36
    const int framelen = (SDL_AUDIO_BITSIZE(this->spec.format) / 8) * this->spec.channels;
icculus@10760
    37
    EM_ASM_ARGS({
icculus@10760
    38
        var numChannels = SDL2.audio.currentOutputBuffer['numberOfChannels'];
icculus@10760
    39
        for (var c = 0; c < numChannels; ++c) {
icculus@10760
    40
            var channelData = SDL2.audio.currentOutputBuffer['getChannelData'](c);
icculus@10760
    41
            if (channelData.length != $1) {
icculus@10760
    42
                throw 'Web Audio output buffer length mismatch! Destination size: ' + channelData.length + ' samples vs expected ' + $1 + ' samples!';
icculus@10760
    43
            }
icculus@9278
    44
icculus@10760
    45
            for (var j = 0; j < $1; ++j) {
icculus@10760
    46
                channelData[j] = HEAPF32[$0 + ((j*numChannels + c) << 2) >> 2];  /* !!! FIXME: why are these shifts here? */
icculus@10760
    47
            }
icculus@9278
    48
        }
icculus@10760
    49
    }, buf, buflen / framelen);
icculus@9278
    50
}
icculus@9278
    51
icculus@9278
    52
static void
icculus@9278
    53
HandleAudioProcess(_THIS)
icculus@9278
    54
{
icculus@10760
    55
    SDL_AudioCallback callback = this->spec.callback;
icculus@10760
    56
    const int stream_len = this->callbackspec.size;
icculus@9278
    57
icculus@10238
    58
    /* Only do something if audio is enabled */
icculus@10238
    59
    if (!SDL_AtomicGet(&this->enabled) || SDL_AtomicGet(&this->paused)) {
icculus@10760
    60
        if (this->stream) {
icculus@10760
    61
            SDL_AudioStreamClear(this->stream);
icculus@10760
    62
        }
icculus@9278
    63
        return;
icculus@10238
    64
    }
icculus@9278
    65
icculus@10760
    66
    if (this->stream == NULL) {  /* no conversion necessary. */
icculus@10760
    67
        SDL_assert(this->spec.size == stream_len);
icculus@10760
    68
        callback(this->spec.userdata, this->fake_stream, stream_len);
icculus@10760
    69
    } else {  /* streaming/converting */
icculus@10760
    70
        int got;
icculus@10760
    71
        while (SDL_AudioStreamAvailable(this->stream) < ((int) this->spec.size)) {
icculus@10760
    72
            callback(this->spec.userdata, this->fake_stream, stream_len);
icculus@10760
    73
            if (SDL_AudioStreamPut(this->stream, this->fake_stream, stream_len) == -1) {
icculus@10760
    74
                SDL_AudioStreamClear(this->stream);
icculus@10760
    75
                SDL_AtomicSet(&this->enabled, 0);
icculus@10760
    76
            }
icculus@9278
    77
        }
icculus@9278
    78
icculus@10760
    79
        got = SDL_AudioStreamGet(this->stream, this->spec.size, this->fake_stream, this->spec.size);
icculus@10760
    80
        SDL_assert((got < 0) || (got == this->spec.size));
icculus@10760
    81
        if (got != this->spec.size) {
icculus@10760
    82
            SDL_memset(this->fake_stream, this->spec.silence, this->spec.size);
icculus@9278
    83
        }
icculus@9278
    84
    }
icculus@9278
    85
icculus@10760
    86
    FeedAudioDevice(this, this->fake_stream, this->spec.size);
icculus@9278
    87
}
icculus@9278
    88
icculus@9278
    89
static void
icculus@10274
    90
HandleCaptureProcess(_THIS)
icculus@10274
    91
{
icculus@10760
    92
    SDL_AudioCallback callback = this->spec.callback;
icculus@10760
    93
    const int stream_len = this->callbackspec.size;
icculus@10274
    94
icculus@10274
    95
    /* Only do something if audio is enabled */
icculus@10274
    96
    if (!SDL_AtomicGet(&this->enabled) || SDL_AtomicGet(&this->paused)) {
icculus@10760
    97
        SDL_AudioStreamClear(this->stream);
icculus@10274
    98
        return;
icculus@10274
    99
    }
icculus@10274
   100
icculus@10274
   101
    EM_ASM_ARGS({
icculus@10274
   102
        var numChannels = SDL2.capture.currentCaptureBuffer.numberOfChannels;
icculus@10760
   103
        for (var c = 0; c < numChannels; ++c) {
icculus@10760
   104
            var channelData = SDL2.capture.currentCaptureBuffer.getChannelData(c);
icculus@10274
   105
            if (channelData.length != $1) {
icculus@10274
   106
                throw 'Web Audio capture buffer length mismatch! Destination size: ' + channelData.length + ' samples vs expected ' + $1 + ' samples!';
icculus@10274
   107
            }
icculus@10760
   108
icculus@10760
   109
            if (numChannels == 1) {  /* fastpath this a little for the common (mono) case. */
icculus@10760
   110
                for (var j = 0; j < $1; ++j) {
icculus@10760
   111
                    setValue($0 + (j * 4), channelData[j], 'float');
icculus@10274
   112
                }
icculus@10760
   113
            } else {
icculus@10274
   114
                for (var j = 0; j < $1; ++j) {
icculus@10274
   115
                    setValue($0 + (((j * numChannels) + c) * 4), channelData[j], 'float');
icculus@10274
   116
                }
icculus@10274
   117
            }
icculus@10274
   118
        }
icculus@10760
   119
    }, this->fake_stream, (this->spec.size / sizeof (float)) / this->spec.channels);
icculus@10274
   120
icculus@10274
   121
    /* okay, we've got an interleaved float32 array in C now. */
icculus@10274
   122
icculus@10760
   123
    if (this->stream == NULL) {  /* no conversion necessary. */
icculus@10760
   124
        SDL_assert(this->spec.size == stream_len);
icculus@10760
   125
        callback(this->spec.userdata, this->fake_stream, stream_len);
icculus@10760
   126
    } else {  /* streaming/converting */
icculus@10760
   127
        if (SDL_AudioStreamPut(this->stream, this->fake_stream, this->spec.size) == -1) {
icculus@10760
   128
            SDL_AtomicSet(&this->enabled, 0);
icculus@10760
   129
        }
icculus@10760
   130
icculus@10760
   131
        while (SDL_AudioStreamAvailable(this->stream) >= stream_len) {
icculus@10760
   132
            const int got = SDL_AudioStreamGet(this->stream, stream_len, this->fake_stream, stream_len);
icculus@10760
   133
            SDL_assert((got < 0) || (got == stream_len));
icculus@10760
   134
            if (got != stream_len) {
icculus@10760
   135
                SDL_memset(this->fake_stream, this->callbackspec.silence, stream_len);
icculus@10760
   136
            }
icculus@10760
   137
            callback(this->spec.userdata, this->fake_stream, stream_len);  /* Send it to the app. */
icculus@10760
   138
        }
icculus@10274
   139
    }
icculus@10274
   140
}
icculus@10274
   141
icculus@10274
   142
icculus@10274
   143
static void
icculus@10281
   144
EMSCRIPTENAUDIO_CloseDevice(_THIS)
icculus@9278
   145
{
icculus@10274
   146
    EM_ASM_({
icculus@10274
   147
        if ($0) {
icculus@10274
   148
            if (SDL2.capture.silenceTimer !== undefined) {
icculus@10274
   149
                clearTimeout(SDL2.capture.silenceTimer);
icculus@10274
   150
            }
icculus@10301
   151
            if (SDL2.capture.stream !== undefined) {
icculus@10301
   152
                var tracks = SDL2.capture.stream.getAudioTracks();
icculus@10301
   153
                for (var i = 0; i < tracks.length; i++) {
icculus@10301
   154
                    SDL2.capture.stream.removeTrack(tracks[i]);
icculus@10301
   155
                }
icculus@10301
   156
                SDL2.capture.stream = undefined;
icculus@10301
   157
            }
icculus@10274
   158
            if (SDL2.capture.scriptProcessorNode !== undefined) {
icculus@10301
   159
                SDL2.capture.scriptProcessorNode.onaudioprocess = function(audioProcessingEvent) {};
icculus@10274
   160
                SDL2.capture.scriptProcessorNode.disconnect();
icculus@10274
   161
                SDL2.capture.scriptProcessorNode = undefined;
icculus@10274
   162
            }
icculus@10274
   163
            if (SDL2.capture.mediaStreamNode !== undefined) {
icculus@10274
   164
                SDL2.capture.mediaStreamNode.disconnect();
icculus@10274
   165
                SDL2.capture.mediaStreamNode = undefined;
icculus@10274
   166
            }
icculus@10274
   167
            if (SDL2.capture.silenceBuffer !== undefined) {
icculus@10274
   168
                SDL2.capture.silenceBuffer = undefined
icculus@10274
   169
            }
icculus@10274
   170
            SDL2.capture = undefined;
icculus@10274
   171
        } else {
icculus@10274
   172
            if (SDL2.audio.scriptProcessorNode != undefined) {
icculus@10274
   173
                SDL2.audio.scriptProcessorNode.disconnect();
icculus@10274
   174
                SDL2.audio.scriptProcessorNode = undefined;
icculus@10274
   175
            }
icculus@10274
   176
            SDL2.audio = undefined;
icculus@10274
   177
        }
icculus@10274
   178
        if ((SDL2.audioContext !== undefined) && (SDL2.audio === undefined) && (SDL2.capture === undefined)) {
icculus@10274
   179
            SDL2.audioContext.close();
icculus@10274
   180
            SDL2.audioContext = undefined;
icculus@10274
   181
        }
icculus@10274
   182
    }, this->iscapture);
icculus@10274
   183
icculus@10760
   184
#if 0  /* !!! FIXME: currently not used. Can we move some stuff off the SDL2 namespace? --ryan. */
icculus@10255
   185
    SDL_free(this->hidden);
icculus@10760
   186
#endif
icculus@9278
   187
}
icculus@9278
   188
icculus@9278
   189
static int
icculus@10281
   190
EMSCRIPTENAUDIO_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
icculus@9278
   191
{
icculus@9278
   192
    SDL_bool valid_format = SDL_FALSE;
icculus@10274
   193
    SDL_AudioFormat test_format;
philipp@9344
   194
    int result;
icculus@9278
   195
icculus@10274
   196
    /* based on parts of library_sdl.js */
icculus@10274
   197
icculus@10274
   198
    /* create context (TODO: this puts stuff in the global namespace...)*/
icculus@10274
   199
    result = EM_ASM_INT({
icculus@10274
   200
        if(typeof(SDL2) === 'undefined') {
icculus@10274
   201
            SDL2 = {};
icculus@10274
   202
        }
icculus@10274
   203
        if (!$0) {
icculus@10274
   204
            SDL2.audio = {};
icculus@10274
   205
        } else {
icculus@10274
   206
            SDL2.capture = {};
icculus@10274
   207
        }
icculus@10274
   208
icculus@10274
   209
        if (!SDL2.audioContext) {
icculus@10274
   210
            if (typeof(AudioContext) !== 'undefined') {
icculus@10274
   211
                SDL2.audioContext = new AudioContext();
icculus@10274
   212
            } else if (typeof(webkitAudioContext) !== 'undefined') {
icculus@10274
   213
                SDL2.audioContext = new webkitAudioContext();
icculus@10274
   214
            }
icculus@10274
   215
        }
icculus@10274
   216
        return SDL2.audioContext === undefined ? -1 : 0;
icculus@10274
   217
    }, iscapture);
icculus@10274
   218
    if (result < 0) {
icculus@10274
   219
        return SDL_SetError("Web Audio API is not available!");
icculus@10274
   220
    }
icculus@10274
   221
icculus@10274
   222
    test_format = SDL_FirstAudioFormat(this->spec.format);
icculus@9278
   223
    while ((!valid_format) && (test_format)) {
icculus@9278
   224
        switch (test_format) {
icculus@9278
   225
        case AUDIO_F32: /* web audio only supports floats */
icculus@9278
   226
            this->spec.format = test_format;
icculus@9278
   227
icculus@9278
   228
            valid_format = SDL_TRUE;
icculus@9278
   229
            break;
icculus@9278
   230
        }
icculus@9278
   231
        test_format = SDL_NextAudioFormat();
icculus@9278
   232
    }
icculus@9278
   233
icculus@9278
   234
    if (!valid_format) {
icculus@9278
   235
        /* Didn't find a compatible format :( */
icculus@9278
   236
        return SDL_SetError("No compatible audio format!");
icculus@9278
   237
    }
icculus@9278
   238
icculus@9278
   239
    /* Initialize all variables that we clean on shutdown */
icculus@10760
   240
#if 0  /* !!! FIXME: currently not used. Can we move some stuff off the SDL2 namespace? --ryan. */
icculus@9278
   241
    this->hidden = (struct SDL_PrivateAudioData *)
icculus@9278
   242
        SDL_malloc((sizeof *this->hidden));
icculus@9278
   243
    if (this->hidden == NULL) {
icculus@9278
   244
        return SDL_OutOfMemory();
icculus@9278
   245
    }
icculus@10257
   246
    SDL_zerop(this->hidden);
icculus@10760
   247
#endif
icculus@9278
   248
icculus@9278
   249
    /* limit to native freq */
icculus@10760
   250
    this->spec.freq = EM_ASM_INT_V({ return SDL2.audioContext.sampleRate; });
icculus@9278
   251
icculus@9278
   252
    SDL_CalculateAudioSpec(&this->spec);
icculus@9278
   253
icculus@10274
   254
    if (iscapture) {
icculus@10274
   255
        /* The idea is to take the capture media stream, hook it up to an
icculus@10274
   256
           audio graph where we can pass it through a ScriptProcessorNode
icculus@10274
   257
           to access the raw PCM samples and push them to the SDL app's
icculus@10274
   258
           callback. From there, we "process" the audio data into silence
icculus@10274
   259
           and forget about it. */
icculus@10274
   260
icculus@10274
   261
        /* This should, strictly speaking, use MediaRecorder for capture, but
icculus@10274
   262
           this API is cleaner to use and better supported, and fires a
icculus@10274
   263
           callback whenever there's enough data to fire down into the app.
icculus@10274
   264
           The downside is that we are spending CPU time silencing a buffer
icculus@10274
   265
           that the audiocontext uselessly mixes into any output. On the
icculus@10274
   266
           upside, both of those things are not only run in native code in
icculus@10274
   267
           the browser, they're probably SIMD code, too. MediaRecorder
icculus@10274
   268
           feels like it's a pretty inefficient tapdance in similar ways,
icculus@10274
   269
           to be honest. */
icculus@10274
   270
icculus@10274
   271
        EM_ASM_({
icculus@10274
   272
            var have_microphone = function(stream) {
icculus@10283
   273
                //console.log('SDL audio capture: we have a microphone! Replacing silence callback.');
icculus@10283
   274
                if (SDL2.capture.silenceTimer !== undefined) {
icculus@10283
   275
                    clearTimeout(SDL2.capture.silenceTimer);
icculus@10283
   276
                    SDL2.capture.silenceTimer = undefined;
icculus@10283
   277
                }
icculus@10274
   278
                SDL2.capture.mediaStreamNode = SDL2.audioContext.createMediaStreamSource(stream);
icculus@10274
   279
                SDL2.capture.scriptProcessorNode = SDL2.audioContext.createScriptProcessor($1, $0, 1);
icculus@10274
   280
                SDL2.capture.scriptProcessorNode.onaudioprocess = function(audioProcessingEvent) {
icculus@10355
   281
                    if ((SDL2 === undefined) || (SDL2.capture === undefined)) { return; }
icculus@10274
   282
                    audioProcessingEvent.outputBuffer.getChannelData(0).fill(0.0);
icculus@10274
   283
                    SDL2.capture.currentCaptureBuffer = audioProcessingEvent.inputBuffer;
icculus@10274
   284
                    Runtime.dynCall('vi', $2, [$3]);
icculus@10274
   285
                };
icculus@10274
   286
                SDL2.capture.mediaStreamNode.connect(SDL2.capture.scriptProcessorNode);
icculus@10274
   287
                SDL2.capture.scriptProcessorNode.connect(SDL2.audioContext.destination);
icculus@10301
   288
                SDL2.capture.stream = stream;
icculus@10274
   289
            };
icculus@10274
   290
icculus@10274
   291
            var no_microphone = function(error) {
icculus@10283
   292
                //console.log('SDL audio capture: we DO NOT have a microphone! (' + error.name + ')...leaving silence callback running.');
icculus@10274
   293
            };
icculus@10274
   294
icculus@10274
   295
            /* we write silence to the audio callback until the microphone is available (user approves use, etc). */
icculus@10274
   296
            SDL2.capture.silenceBuffer = SDL2.audioContext.createBuffer($0, $1, SDL2.audioContext.sampleRate);
icculus@10274
   297
            SDL2.capture.silenceBuffer.getChannelData(0).fill(0.0);
icculus@10274
   298
            var silence_callback = function() {
icculus@10274
   299
                SDL2.capture.currentCaptureBuffer = SDL2.capture.silenceBuffer;
icculus@10274
   300
                Runtime.dynCall('vi', $2, [$3]);
icculus@10274
   301
            };
icculus@10274
   302
icculus@10276
   303
            SDL2.capture.silenceTimer = setTimeout(silence_callback, ($1 / SDL2.audioContext.sampleRate) * 1000);
icculus@10274
   304
icculus@10274
   305
            if ((navigator.mediaDevices !== undefined) && (navigator.mediaDevices.getUserMedia !== undefined)) {
icculus@10274
   306
                navigator.mediaDevices.getUserMedia({ audio: true, video: false }).then(have_microphone).catch(no_microphone);
icculus@10274
   307
            } else if (navigator.webkitGetUserMedia !== undefined) {
icculus@10274
   308
                navigator.webkitGetUserMedia({ audio: true, video: false }, have_microphone, no_microphone);
icculus@10274
   309
            }
icculus@10274
   310
        }, this->spec.channels, this->spec.samples, HandleCaptureProcess, this);
icculus@10274
   311
    } else {
icculus@10274
   312
        /* setup a ScriptProcessorNode */
icculus@10274
   313
        EM_ASM_ARGS({
icculus@10274
   314
            SDL2.audio.scriptProcessorNode = SDL2.audioContext['createScriptProcessor']($1, 0, $0);
icculus@10274
   315
            SDL2.audio.scriptProcessorNode['onaudioprocess'] = function (e) {
icculus@10355
   316
                if ((SDL2 === undefined) || (SDL2.audio === undefined)) { return; }
icculus@10274
   317
                SDL2.audio.currentOutputBuffer = e['outputBuffer'];
icculus@10274
   318
                Runtime.dynCall('vi', $2, [$3]);
icculus@10274
   319
            };
icculus@10274
   320
            SDL2.audio.scriptProcessorNode['connect'](SDL2.audioContext['destination']);
icculus@10274
   321
        }, this->spec.channels, this->spec.samples, HandleAudioProcess, this);
icculus@10274
   322
    }
icculus@10274
   323
icculus@9278
   324
    return 0;
icculus@9278
   325
}
icculus@9278
   326
icculus@9278
   327
static int
icculus@10281
   328
EMSCRIPTENAUDIO_Init(SDL_AudioDriverImpl * impl)
icculus@9278
   329
{
icculus@9278
   330
    /* Set the function pointers */
icculus@10281
   331
    impl->OpenDevice = EMSCRIPTENAUDIO_OpenDevice;
icculus@10281
   332
    impl->CloseDevice = EMSCRIPTENAUDIO_CloseDevice;
icculus@9278
   333
icculus@9278
   334
    impl->OnlyHasDefaultOutputDevice = 1;
icculus@9278
   335
icculus@9278
   336
    /* no threads here */
icculus@9278
   337
    impl->SkipMixerLock = 1;
icculus@9278
   338
    impl->ProvidesOwnCallbackThread = 1;
icculus@9278
   339
icculus@9278
   340
    /* check availability */
icculus@10274
   341
    const int available = EM_ASM_INT_V({
icculus@9278
   342
        if (typeof(AudioContext) !== 'undefined') {
icculus@9278
   343
            return 1;
icculus@9278
   344
        } else if (typeof(webkitAudioContext) !== 'undefined') {
icculus@9278
   345
            return 1;
icculus@9278
   346
        }
icculus@9278
   347
        return 0;
icculus@9278
   348
    });
icculus@9278
   349
philipp@9831
   350
    if (!available) {
philipp@9831
   351
        SDL_SetError("No audio context available");
philipp@9831
   352
    }
philipp@9831
   353
icculus@10274
   354
    const int capture_available = available && EM_ASM_INT_V({
icculus@10274
   355
        if ((typeof(navigator.mediaDevices) !== 'undefined') && (typeof(navigator.mediaDevices.getUserMedia) !== 'undefined')) {
icculus@10274
   356
            return 1;
icculus@10274
   357
        } else if (typeof(navigator.webkitGetUserMedia) !== 'undefined') {
icculus@10274
   358
            return 1;
icculus@10274
   359
        }
icculus@10274
   360
        return 0;
icculus@10274
   361
    });
icculus@10274
   362
icculus@10274
   363
    impl->HasCaptureSupport = capture_available ? SDL_TRUE : SDL_FALSE;
icculus@10274
   364
    impl->OnlyHasDefaultCaptureDevice = capture_available ? SDL_TRUE : SDL_FALSE;
icculus@10274
   365
icculus@9278
   366
    return available;
icculus@9278
   367
}
icculus@9278
   368
icculus@10281
   369
AudioBootStrap EMSCRIPTENAUDIO_bootstrap = {
icculus@10281
   370
    "emscripten", "SDL emscripten audio driver", EMSCRIPTENAUDIO_Init, 0
icculus@9278
   371
};
icculus@9278
   372
icculus@9278
   373
#endif /* SDL_AUDIO_DRIVER_EMSCRIPTEN */
icculus@9278
   374
icculus@9278
   375
/* vi: set ts=4 sw=4 expandtab: */