From 969d59fbd7b8aa5254bacbac47c64db6a5a42919 Mon Sep 17 00:00:00 2001 From: e Date: Fri, 19 Jan 2024 20:49:36 -0300 Subject: [PATCH 1/2] track total_length in frames --- src/SDL_sound_internal.h | 1 + src/SDL_sound_vorbis.c | 1 + 2 files changed, 2 insertions(+) diff --git a/src/SDL_sound_internal.h b/src/SDL_sound_internal.h index b68d2f7f..fc8d3f01 100644 --- a/src/SDL_sound_internal.h +++ b/src/SDL_sound_internal.h @@ -260,6 +260,7 @@ typedef struct __SOUND_SAMPLEINTERNAL__ Uint32 buffer_size; void *decoder_private; Sint32 total_time; + Uint32 total_length; Uint32 mix_position; MixFunc mix; } Sound_SampleInternal; diff --git a/src/SDL_sound_vorbis.c b/src/SDL_sound_vorbis.c index b427521e..9e66b974 100644 --- a/src/SDL_sound_vorbis.c +++ b/src/SDL_sound_vorbis.c @@ -133,6 +133,7 @@ static int VORBIS_open(Sound_Sample *sample, const char *ext) const unsigned int rate = stb->sample_rate; internal->total_time = (num_frames / rate) * 1000; internal->total_time += (num_frames % rate) * 1000 / rate; + internal->total_length = num_frames; } /* else */ return 1; /* we'll handle this data. */ From 20c2d706e7c1ac10399dd3f7ad532257e2abcf07 Mon Sep 17 00:00:00 2001 From: e Date: Fri, 19 Jan 2024 20:50:30 -0300 Subject: [PATCH 2/2] fix ogg decoding adds silence at end --- src/SDL_sound_vorbis.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/SDL_sound_vorbis.c b/src/SDL_sound_vorbis.c index 9e66b974..6fdd7a36 100644 --- a/src/SDL_sound_vorbis.c +++ b/src/SDL_sound_vorbis.c @@ -153,14 +153,23 @@ static Uint32 VORBIS_read(Sound_Sample *sample) Uint32 retval; int rc; int err; + int delta; Sound_SampleInternal *internal = (Sound_SampleInternal *) sample->opaque; stb_vorbis *stb = (stb_vorbis *) internal->decoder_private; const int channels = (int) sample->actual.channels; const int want_samples = (int) (internal->buffer_size / sizeof (float)); + const int offset = stb_vorbis_get_playback_sample_offset(stb); + + delta = internal->total_length - offset; stb_vorbis_get_error(stb); /* clear any error state */ rc = stb_vorbis_get_samples_float_interleaved(stb, channels, (float *) internal->buffer, want_samples); - retval = (Uint32) (rc * channels * sizeof (float)); /* rc == number of sample frames read */ + if(delta > 0 && delta < rc) { + retval = (Uint32) (delta * channels * sizeof (float)); /* prevents bug in stb_vorbis */ + } else { + retval = (Uint32) (rc * channels * sizeof (float)); /* rc == number of sample frames read */ + } + err = stb_vorbis_get_error(stb); if (retval == 0)