src/audio/emscripten/SDL_emscriptenaudio.c
changeset 9344 83f4ab8fb0b8
parent 9291 02b47b8164da
child 9394 bb28e5281770
equal deleted inserted replaced
9343:7f58a328445c 9344:83f4ab8fb0b8
   155 {
   155 {
   156     SDL_bool valid_format = SDL_FALSE;
   156     SDL_bool valid_format = SDL_FALSE;
   157     SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
   157     SDL_AudioFormat test_format = SDL_FirstAudioFormat(this->spec.format);
   158     int i;
   158     int i;
   159     float f;
   159     float f;
       
   160     int result;
   160 
   161 
   161     while ((!valid_format) && (test_format)) {
   162     while ((!valid_format) && (test_format)) {
   162         switch (test_format) {
   163         switch (test_format) {
   163         case AUDIO_F32: /* web audio only supports floats */
   164         case AUDIO_F32: /* web audio only supports floats */
   164             this->spec.format = test_format;
   165             this->spec.format = test_format;
   183     SDL_memset(this->hidden, 0, (sizeof *this->hidden));
   184     SDL_memset(this->hidden, 0, (sizeof *this->hidden));
   184 
   185 
   185     /* based on parts of library_sdl.js */
   186     /* based on parts of library_sdl.js */
   186 
   187 
   187     /* create context (TODO: this puts stuff in the global namespace...)*/
   188     /* create context (TODO: this puts stuff in the global namespace...)*/
   188     EM_ASM({
   189     result = EM_ASM_INT_V({
   189         if(typeof(SDL2) === 'undefined')
   190         if(typeof(SDL2) === 'undefined')
   190             SDL2 = {};
   191             SDL2 = {};
   191 
   192 
   192         if(typeof(SDL2.audio) === 'undefined')
   193         if(typeof(SDL2.audio) === 'undefined')
   193             SDL2.audio = {};
   194             SDL2.audio = {};
   196             if (typeof(AudioContext) !== 'undefined') {
   197             if (typeof(AudioContext) !== 'undefined') {
   197                 SDL2.audioContext = new AudioContext();
   198                 SDL2.audioContext = new AudioContext();
   198             } else if (typeof(webkitAudioContext) !== 'undefined') {
   199             } else if (typeof(webkitAudioContext) !== 'undefined') {
   199                 SDL2.audioContext = new webkitAudioContext();
   200                 SDL2.audioContext = new webkitAudioContext();
   200             } else {
   201             } else {
   201                 throw 'Web Audio API is not available!';
   202                 return -1;
   202             }
   203             }
   203         }
   204         }
       
   205         return 0;
   204     });
   206     });
       
   207     if (result < 0) {
       
   208         return SDL_SetError("Web Audio API is not available!");
       
   209     }
   205 
   210 
   206     /* limit to native freq */
   211     /* limit to native freq */
   207     int sampleRate = EM_ASM_INT_V({
   212     int sampleRate = EM_ASM_INT_V({
   208         return SDL2.audioContext['sampleRate'];
   213         return SDL2.audioContext['sampleRate'];
   209     });
   214     });