src/audio/emscripten/SDL_emscriptenaudio.c
changeset 9278 8900afb78a19
child 9291 02b47b8164da
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/audio/emscripten/SDL_emscriptenaudio.c	Thu Dec 18 00:19:52 2014 -0500
     1.3 @@ -0,0 +1,271 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +#include "../../SDL_internal.h"
    1.25 +
    1.26 +#if SDL_AUDIO_DRIVER_EMSCRIPTEN
    1.27 +
    1.28 +#include "SDL_audio.h"
    1.29 +#include "SDL_log.h"
    1.30 +#include "../SDL_audio_c.h"
    1.31 +#include "SDL_emscriptenaudio.h"
    1.32 +
    1.33 +#include <emscripten/emscripten.h>
    1.34 +
    1.35 +static int
    1.36 +copyData(_THIS)
    1.37 +{
    1.38 +    int byte_len;
    1.39 +
    1.40 +    if (this->hidden->write_off + this->convert.len_cvt > this->hidden->mixlen) {
    1.41 +        if (this->hidden->write_off > this->hidden->read_off) {
    1.42 +            SDL_memmove(this->hidden->mixbuf,
    1.43 +                        this->hidden->mixbuf + this->hidden->read_off,
    1.44 +                        this->hidden->mixlen - this->hidden->read_off);
    1.45 +            this->hidden->write_off = this->hidden->write_off - this->hidden->read_off;
    1.46 +        } else {
    1.47 +            this->hidden->write_off = 0;
    1.48 +        }
    1.49 +        this->hidden->read_off = 0;
    1.50 +    }
    1.51 +
    1.52 +    SDL_memcpy(this->hidden->mixbuf + this->hidden->write_off,
    1.53 +               this->convert.buf,
    1.54 +               this->convert.len_cvt);
    1.55 +    this->hidden->write_off += this->convert.len_cvt;
    1.56 +    byte_len = this->hidden->write_off - this->hidden->read_off;
    1.57 +
    1.58 +    return byte_len;
    1.59 +}
    1.60 +
    1.61 +static void
    1.62 +HandleAudioProcess(_THIS)
    1.63 +{
    1.64 +    Uint8 *buf = NULL;
    1.65 +    int byte_len = 0;
    1.66 +    int bytes = SDL_AUDIO_BITSIZE(this->spec.format) / 8;
    1.67 +    int bytes_in = SDL_AUDIO_BITSIZE(this->convert.src_format) / 8;
    1.68 +    int i;
    1.69 +
    1.70 +    /* Only do soemthing if audio is enabled */
    1.71 +    if (!this->enabled)
    1.72 +        return;
    1.73 +
    1.74 +    if (this->paused)
    1.75 +        return;
    1.76 +
    1.77 +    if (this->convert.needed) {
    1.78 +        if (this->hidden->conv_in_len != 0) {
    1.79 +            this->convert.len = this->hidden->conv_in_len * bytes_in * this->spec.channels;
    1.80 +        }
    1.81 +
    1.82 +        (*this->spec.callback) (this->spec.userdata,
    1.83 +                                 this->convert.buf,
    1.84 +                                 this->convert.len);
    1.85 +        SDL_ConvertAudio(&this->convert);
    1.86 +        buf = this->convert.buf;
    1.87 +        byte_len = this->convert.len_cvt;
    1.88 +
    1.89 +        /* size mismatch*/
    1.90 +        if (byte_len != this->spec.size) {
    1.91 +            if (!this->hidden->mixbuf) {
    1.92 +                this->hidden->mixlen = this->spec.size > byte_len ? this->spec.size * 2 : byte_len * 2;
    1.93 +                this->hidden->mixbuf = SDL_malloc(this->hidden->mixlen);
    1.94 +            }
    1.95 +
    1.96 +            /* copy existing data */
    1.97 +            byte_len = copyData(this);
    1.98 +
    1.99 +            /* read more data*/
   1.100 +            while (byte_len < this->spec.size) {
   1.101 +                (*this->spec.callback) (this->spec.userdata,
   1.102 +                                         this->convert.buf,
   1.103 +                                         this->convert.len);
   1.104 +                SDL_ConvertAudio(&this->convert);
   1.105 +                byte_len = copyData(this);
   1.106 +            }
   1.107 +
   1.108 +            byte_len = this->spec.size;
   1.109 +            buf = this->hidden->mixbuf + this->hidden->read_off;
   1.110 +            this->hidden->read_off += byte_len;
   1.111 +        }
   1.112 +
   1.113 +    } else {
   1.114 +        if (!this->hidden->mixbuf) {
   1.115 +            this->hidden->mixlen = this->spec.size;
   1.116 +            this->hidden->mixbuf = SDL_malloc(this->hidden->mixlen);
   1.117 +        }
   1.118 +        (*this->spec.callback) (this->spec.userdata,
   1.119 +                                 this->hidden->mixbuf,
   1.120 +                                 this->hidden->mixlen);
   1.121 +        buf = this->hidden->mixbuf;
   1.122 +        byte_len = this->hidden->mixlen;
   1.123 +    }
   1.124 +
   1.125 +    if (buf) {
   1.126 +        EM_ASM_ARGS({
   1.127 +            var numChannels = SDL2.audio.currentOutputBuffer['numberOfChannels'];
   1.128 +            for (var c = 0; c < numChannels; ++c) {
   1.129 +                var channelData = SDL2.audio.currentOutputBuffer['getChannelData'](c);
   1.130 +                if (channelData.length != $1) {
   1.131 +                    throw 'Web Audio output buffer length mismatch! Destination size: ' + channelData.length + ' samples vs expected ' + $1 + ' samples!';
   1.132 +                }
   1.133 +
   1.134 +                for (var j = 0; j < $1; ++j) {
   1.135 +                    channelData[j] = getValue($0 + (j*numChannels + c)*4, 'float');
   1.136 +                }
   1.137 +            }
   1.138 +        }, buf, byte_len / bytes / this->spec.channels);
   1.139 +    }
   1.140 +}
   1.141 +
   1.142 +static void
   1.143 +Emscripten_CloseDevice(_THIS)
   1.144 +{
   1.145 +    if (this->hidden != NULL) {
   1.146 +        if (this->hidden->mixbuf != NULL) {
   1.147 +            /* Clean up the audio buffer */
   1.148 +            SDL_free(this->hidden->mixbuf);
   1.149 +            this->hidden->mixbuf = NULL;
   1.150 +        }
   1.151 +
   1.152 +        SDL_free(this->hidden);
   1.153 +        this->hidden = NULL;
   1.154 +    }
   1.155 +}
   1.156 +
   1.157 +static int
   1.158 +Emscripten_OpenDevice(_THIS, const char *devname, int iscapture)
   1.159 +{
   1.160 +    SDL_bool valid_format = SDL_FALSE;
   1.161 +    SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
   1.162 +    int i;
   1.163 +    float f;
   1.164 +
   1.165 +    while ((!valid_format) && (test_format)) {
   1.166 +        switch (test_format) {
   1.167 +        case AUDIO_F32: /* web audio only supports floats */
   1.168 +            this->spec.format = test_format;
   1.169 +
   1.170 +            valid_format = SDL_TRUE;
   1.171 +            break;
   1.172 +        }
   1.173 +        test_format = SDL_NextAudioFormat();
   1.174 +    }
   1.175 +
   1.176 +    if (!valid_format) {
   1.177 +        /* Didn't find a compatible format :( */
   1.178 +        return SDL_SetError("No compatible audio format!");
   1.179 +    }
   1.180 +
   1.181 +    /* Initialize all variables that we clean on shutdown */
   1.182 +    this->hidden = (struct SDL_PrivateAudioData *)
   1.183 +        SDL_malloc((sizeof *this->hidden));
   1.184 +    if (this->hidden == NULL) {
   1.185 +        return SDL_OutOfMemory();
   1.186 +    }
   1.187 +    SDL_memset(this->hidden, 0, (sizeof *this->hidden));
   1.188 +
   1.189 +    /* based on parts of library_sdl.js */
   1.190 +
   1.191 +    /* create context (TODO: this puts stuff in the global namespace...)*/
   1.192 +    EM_ASM({
   1.193 +        if(typeof(SDL2) === 'undefined')
   1.194 +            SDL2 = {};
   1.195 +
   1.196 +        if(typeof(SDL2.audio) === 'undefined')
   1.197 +            SDL2.audio = {};
   1.198 +
   1.199 +        if (!SDL2.audioContext) {
   1.200 +            if (typeof(AudioContext) !== 'undefined') {
   1.201 +                SDL2.audioContext = new AudioContext();
   1.202 +            } else if (typeof(webkitAudioContext) !== 'undefined') {
   1.203 +                SDL2.audioContext = new webkitAudioContext();
   1.204 +            } else {
   1.205 +                throw 'Web Audio API is not available!';
   1.206 +            }
   1.207 +        }
   1.208 +    });
   1.209 +
   1.210 +    /* limit to native freq */
   1.211 +    int sampleRate = EM_ASM_INT_V({
   1.212 +        return SDL2.audioContext['sampleRate'];
   1.213 +    });
   1.214 +
   1.215 +    if(this->spec.freq != sampleRate) {
   1.216 +        for (i = this->spec.samples; i > 0; i--) {
   1.217 +            f = (float)i / (float)sampleRate * (float)this->spec.freq;
   1.218 +            if (SDL_floor(f) == f) {
   1.219 +                this->hidden->conv_in_len = SDL_floor(f);
   1.220 +                break;
   1.221 +            }
   1.222 +        }
   1.223 +
   1.224 +        this->spec.freq = sampleRate;
   1.225 +    }
   1.226 +
   1.227 +    SDL_CalculateAudioSpec(&this->spec);
   1.228 +
   1.229 +    /* setup a ScriptProcessorNode */
   1.230 +    EM_ASM_ARGS({
   1.231 +        SDL2.audio.scriptProcessorNode = SDL2.audioContext['createScriptProcessor']($1, 0, $0);
   1.232 +        SDL2.audio.scriptProcessorNode['onaudioprocess'] = function (e) {
   1.233 +            SDL2.audio.currentOutputBuffer = e['outputBuffer'];
   1.234 +            Runtime.dynCall('vi', $2, [$3]);
   1.235 +        };
   1.236 +        SDL2.audio.scriptProcessorNode['connect'](SDL2.audioContext['destination']);
   1.237 +    }, this->spec.channels, this->spec.samples, HandleAudioProcess, this);
   1.238 +    return 0;
   1.239 +}
   1.240 +
   1.241 +static int
   1.242 +Emscripten_Init(SDL_AudioDriverImpl * impl)
   1.243 +{
   1.244 +    /* Set the function pointers */
   1.245 +    impl->OpenDevice = Emscripten_OpenDevice;
   1.246 +    impl->CloseDevice = Emscripten_CloseDevice;
   1.247 +
   1.248 +    /* only one output */
   1.249 +    impl->OnlyHasDefaultOutputDevice = 1;
   1.250 +
   1.251 +    /* no threads here */
   1.252 +    impl->SkipMixerLock = 1;
   1.253 +    impl->ProvidesOwnCallbackThread = 1;
   1.254 +
   1.255 +    /* check availability */
   1.256 +    int available = EM_ASM_INT_V({
   1.257 +        if (typeof(AudioContext) !== 'undefined') {
   1.258 +            return 1;
   1.259 +        } else if (typeof(webkitAudioContext) !== 'undefined') {
   1.260 +            return 1;
   1.261 +        }
   1.262 +        return 0;
   1.263 +    });
   1.264 +
   1.265 +    return available;
   1.266 +}
   1.267 +
   1.268 +AudioBootStrap EmscriptenAudio_bootstrap = {
   1.269 +    "emscripten", "SDL emscripten audio driver", Emscripten_Init, 0
   1.270 +};
   1.271 +
   1.272 +#endif /* SDL_AUDIO_DRIVER_EMSCRIPTEN */
   1.273 +
   1.274 +/* vi: set ts=4 sw=4 expandtab: */