From 2adbb131e63101b01195be7a317b3a9516ce0ebc Mon Sep 17 00:00:00 2001 From: Sebastian Krzyszkowiak Date: Mon, 9 Dec 2019 02:50:59 +0100 Subject: [PATCH] emscripten-audio-stream: resume stream from correct position after pausing --- src/emscripten-audio-stream.c | 8 ++++++++ src/emscripten-audio-stream.h | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/emscripten-audio-stream.c b/src/emscripten-audio-stream.c index b7903d1..b0f5e54 100644 --- a/src/emscripten-audio-stream.c +++ b/src/emscripten-audio-stream.c @@ -60,6 +60,7 @@ SYMBOL_EXPORT ALLEGRO_AUDIO_STREAM *emscripten_load_audio_stream(const char* fil stream->sample = al_load_sample(filename); stream->instance = al_create_sample_instance(stream->sample); stream->old_pos = 0.0; + stream->pause_pos = 0.0; al_set_sample_instance_playing(stream->instance, true); return (ALLEGRO_AUDIO_STREAM*)stream; } @@ -69,6 +70,7 @@ SYMBOL_EXPORT ALLEGRO_AUDIO_STREAM *emscripten_load_audio_stream_f(ALLEGRO_FILE stream->sample = al_load_sample_f(file, ident); stream->instance = al_create_sample_instance(stream->sample); stream->old_pos = 0.0; + stream->pause_pos = 0.0; al_set_sample_instance_playing(stream->instance, true); return (ALLEGRO_AUDIO_STREAM*)stream; } @@ -80,6 +82,11 @@ SYMBOL_EXPORT bool emscripten_set_audio_stream_gain(ALLEGRO_AUDIO_STREAM *stream SYMBOL_EXPORT bool emscripten_set_audio_stream_playing(ALLEGRO_AUDIO_STREAM *stream, bool val) { EMSCRIPTEN_AUDIO_STREAM *s = (EMSCRIPTEN_AUDIO_STREAM*)stream; + if (val && s->pause_pos) { + al_set_sample_instance_position(s->instance, s->pause_pos); + } else { + s->pause_pos = al_get_sample_instance_position(s->instance); + } return al_set_sample_instance_playing(s->instance, val); } @@ -101,6 +108,7 @@ SYMBOL_EXPORT bool emscripten_get_audio_stream_playing(ALLEGRO_AUDIO_STREAM *str SYMBOL_EXPORT bool emscripten_rewind_audio_stream(ALLEGRO_AUDIO_STREAM *stream) { EMSCRIPTEN_AUDIO_STREAM *s = (EMSCRIPTEN_AUDIO_STREAM*)stream; + s->pause_pos = 0.0; return al_set_sample_instance_position(s->instance, 0); } diff --git a/src/emscripten-audio-stream.h b/src/emscripten-audio-stream.h index f473f75..09b3645 100644 --- a/src/emscripten-audio-stream.h +++ b/src/emscripten-audio-stream.h @@ -26,7 +26,7 @@ typedef struct { ALLEGRO_SAMPLE* sample; ALLEGRO_SAMPLE_INSTANCE* instance; - double old_pos; + double old_pos, pause_pos; } EMSCRIPTEN_AUDIO_STREAM; ALLEGRO_AUDIO_STREAM* emscripten_load_audio_stream(const char* filename, size_t buffer_count, unsigned int samples);