diff --git a/src/internal.c b/src/internal.c index bed8809..1e697ee 100644 --- a/src/internal.c +++ b/src/internal.c @@ -177,6 +177,28 @@ SYMBOL_INTERNAL void ResizeGamestates(struct Game* game) { } } +SYMBOL_INTERNAL int SetupAudio(struct Game* game) { + int samplerate = strtol(GetConfigOptionDefault(game, "SuperDerpy", "samplerate", "44100"), NULL, 10); +#ifdef __EMSCRIPTEN__ + game->audio.v = al_create_voice(samplerate, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2); +#else + game->audio.v = al_create_voice(samplerate, ALLEGRO_AUDIO_DEPTH_INT16, ALLEGRO_CHANNEL_CONF_2); + if (!game->audio.v) { + // fallback + game->audio.v = al_create_voice(samplerate, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2); + } +#endif + al_set_default_voice(game->audio.v); + + if (game->audio.v) { + al_attach_mixer_to_voice(game->audio.mixer, game->audio.v); + } else { + PrintConsole(game, "Could not create audio voice!"); + return 1; + } + return 0; +} + SYMBOL_INTERNAL void DrawConsole(struct Game* game) { double game_time = al_get_time(); if (game->_priv.showconsole) { @@ -572,7 +594,11 @@ SYMBOL_INTERNAL void PauseExecution(struct Game* game) { return; } game->_priv.paused = true; - al_detach_voice(game->audio.v); + if (game->audio.v) { + al_detach_voice(game->audio.v); + } + al_set_default_voice(NULL); + game->audio.v = NULL; FreezeGamestates(game); PrintConsole(game, "Engine halted."); } @@ -606,7 +632,7 @@ SYMBOL_INTERNAL void ResumeExecution(struct Game* game) { return; } UnfreezeGamestates(game); - al_attach_mixer_to_voice(game->audio.mixer, game->audio.v); + SetupAudio(game); game->_priv.paused = false; game->_priv.timestamp = al_get_time(); PrintConsole(game, "Engine resumed."); diff --git a/src/internal.h b/src/internal.h index 3bfeac5..d07705e 100644 --- a/src/internal.h +++ b/src/internal.h @@ -101,6 +101,7 @@ void ReloadGamestates(struct Game* game); void FreezeGamestates(struct Game* game); void UnfreezeGamestates(struct Game* game); void ResizeGamestates(struct Game* game); +int SetupAudio(struct Game* game); void DrawConsole(struct Game* game); void Console_Load(struct Game* game); void Console_Unload(struct Game* game); diff --git a/src/libsuperderpy.c b/src/libsuperderpy.c index b22d40d..4665b8d 100644 --- a/src/libsuperderpy.c +++ b/src/libsuperderpy.c @@ -283,20 +283,10 @@ SYMBOL_EXPORT struct Game* libsuperderpy_init(int argc, char** argv, const char* } int samplerate = strtol(GetConfigOptionDefault(game, "SuperDerpy", "samplerate", "44100"), NULL, 10); -#ifdef __EMSCRIPTEN__ - game->audio.v = al_create_voice(samplerate, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2); -#else - game->audio.v = al_create_voice(samplerate, ALLEGRO_AUDIO_DEPTH_INT16, ALLEGRO_CHANNEL_CONF_2); - if (!game->audio.v) { - // fallback - game->audio.v = al_create_voice(samplerate, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2); - } -#endif game->audio.mixer = al_create_mixer(samplerate, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2); game->audio.fx = al_create_mixer(samplerate, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2); game->audio.music = al_create_mixer(samplerate, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2); game->audio.voice = al_create_mixer(samplerate, ALLEGRO_AUDIO_DEPTH_FLOAT32, ALLEGRO_CHANNEL_CONF_2); - al_attach_mixer_to_voice(game->audio.mixer, game->audio.v); al_attach_mixer_to_mixer(game->audio.fx, game->audio.mixer); al_attach_mixer_to_mixer(game->audio.music, game->audio.mixer); al_attach_mixer_to_mixer(game->audio.voice, game->audio.mixer); @@ -304,6 +294,7 @@ SYMBOL_EXPORT struct Game* libsuperderpy_init(int argc, char** argv, const char* al_set_mixer_gain(game->audio.music, game->config.music / 10.0); al_set_mixer_gain(game->audio.voice, game->config.voice / 10.0); al_set_mixer_gain(game->audio.mixer, game->config.mute ? 0.0 : 1.0); + SetupAudio(game); al_set_default_mixer(game->audio.mixer); setlocale(LC_NUMERIC, "C"); @@ -477,11 +468,12 @@ SYMBOL_EXPORT void libsuperderpy_destroy(struct Game* game) { al_destroy_display(game->display); al_destroy_user_event_source(&(game->event_source)); al_destroy_event_queue(game->_priv.event_queue); + al_set_default_mixer(NULL); // does not destroy anything al_destroy_mixer(game->audio.fx); al_destroy_mixer(game->audio.music); al_destroy_mixer(game->audio.voice); al_destroy_mixer(game->audio.mixer); - al_destroy_voice(game->audio.v); + al_set_default_voice(NULL); // destroys game->audio.v voice al_destroy_cond(game->_priv.texture_sync_cond); al_destroy_mutex(game->_priv.texture_sync_mutex); al_destroy_cond(game->_priv.bsod_cond);