Last active
July 26, 2016 14:06
-
-
Save whistlegraph/44075ddf97a5219dd3e5 to your computer and use it in GitHub Desktop.
Emscripten SDL2 Mobile Audio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SDL.h> | |
#include <emscripten/emscripten.h> | |
int eventFilter(void* userdata, SDL_Event* event){ | |
int audio_started; | |
(void)(userdata); | |
switch(event->type){ | |
case SDL_FINGERDOWN: | |
case SDL_MOUSEBUTTONDOWN: | |
case SDL_KEYDOWN: | |
audio_started = EM_ASM_INT_V({ | |
if(SDL2.audioContext && SDL2.audioContext.currentTime == 0){ | |
var buffer = SDL2.audioContext.createBuffer(1, 1, 22050); | |
var source = SDL2.audioContext.createBufferSource(); | |
source.buffer = buffer; | |
source.connect(SDL2.audioContext.destination); | |
source.noteOn(0); | |
} else if(SDL2.audioContext && SDL2.audioContext.currentTime != 0){ | |
return 1; | |
} | |
return 0; | |
}); | |
if (audio_started){ | |
SDL_SetEventFilter(NULL, NULL); | |
return 0; | |
} | |
break; | |
} | |
return 1; | |
} | |
void mobile_audio_start_on_user_input(){ | |
SDL_SetEventFilter(eventFilter, NULL); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void mobile_audio_start_on_user_input(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment