Skip to content

Instantly share code, notes, and snippets.

@whistlegraph
Last active July 26, 2016 14:06
Show Gist options
  • Save whistlegraph/44075ddf97a5219dd3e5 to your computer and use it in GitHub Desktop.
Save whistlegraph/44075ddf97a5219dd3e5 to your computer and use it in GitHub Desktop.
Emscripten SDL2 Mobile Audio
#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);
}
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