diff --git a/src/stb_vorbis.h b/src/stb_vorbis.h index 8686bdf..7d2418d 100644 --- a/src/stb_vorbis.h +++ b/src/stb_vorbis.h @@ -5512,6 +5512,21 @@ int stb_vorbis_get_frame_short_interleaved(stb_vorbis *f, int num_c, short *buff return len; } +static int fixup_current_playback_loc(stb_vorbis *f, int n) +{ + unsigned int lgs; + + f->current_playback_loc += n; + lgs = stb_vorbis_stream_length_in_samples(f); + if (f->current_playback_loc > lgs && lgs > 0 && lgs != SAMPLE_unknown) { + int r = n - (f->current_playback_loc - (int)lgs); + f->current_playback_loc = lgs; + return r; + } + + return n; +} + int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short *buffer, int num_shorts) { float **outputs; @@ -5528,8 +5543,7 @@ int stb_vorbis_get_samples_short_interleaved(stb_vorbis *f, int channels, short if (n == len) break; if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) break; } - f->current_playback_loc += n; - return n; + return fixup_current_playback_loc(f, n); } int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, int len) @@ -5546,8 +5560,7 @@ int stb_vorbis_get_samples_short(stb_vorbis *f, int channels, short **buffer, in if (n == len) break; if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) break; } - f->current_playback_loc += n; - return n; + return fixup_current_playback_loc(f, n); } #ifndef STB_VORBIS_NO_STDIO @@ -5655,8 +5668,7 @@ int stb_vorbis_get_samples_float_interleaved(stb_vorbis *f, int channels, float if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) break; } - f->current_playback_loc += n; - return n; + return fixup_current_playback_loc(f, n); } int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, int num_samples) @@ -5682,8 +5694,7 @@ int stb_vorbis_get_samples_float(stb_vorbis *f, int channels, float **buffer, in if (!stb_vorbis_get_frame_float(f, NULL, &outputs)) break; } - f->current_playback_loc += n; - return n; + return fixup_current_playback_loc(f, n); } #endif // STB_VORBIS_NO_PULLDATA_API