From 4b184e1a77ebdb071fa142311c9ff6c9a16ddd68 Mon Sep 17 00:00:00 2001 From: Vitaly Novichkov Date: Tue, 17 Dec 2019 15:55:02 +0300 Subject: [PATCH] music_mpg123.c: seek fix, and initialize the stream on file open - Fixed bug seek going into wrong position if sample rate of the file and the stream don't match. - Initialize the stream in the MPG123_CreateFromRW(). --- CHANGES.txt | 5 ++++- src/codecs/music_mpg123.c | 40 ++++++++++++++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 953939ac..44fd23cd 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,5 +1,8 @@ 2.0.5: -Ozkan Sezer - Sun Dec 17 14:56:04 2019 +Vitaly Novichkov - Thu Dec 17 15:50:02 2019 + * Fixed music_mpg123 seek bug when sample rate of the file and + the stream don't match +Ozkan Sezer - Thu Dec 17 14:56:04 2019 * Timidity improvements. New Mix_SetTimidityCfg() public api. Ozkan Sezer - Sun Dec 08 10:20:40 2019 * Improved mp3 tag detection/skipping diff --git a/src/codecs/music_mpg123.c b/src/codecs/music_mpg123.c index c59b717d..653a2f9e 100644 --- a/src/codecs/music_mpg123.c +++ b/src/codecs/music_mpg123.c @@ -131,12 +131,14 @@ typedef struct SDL_AudioStream *stream; unsigned char *buffer; size_t buffer_size; + long sample_rate; } MPG123_Music; static int MPG123_Seek(void *context, double secs); static void MPG123_Delete(void *context); + static int mpg123_format_to_sdl(int fmt) { switch (fmt) @@ -151,7 +153,8 @@ static int mpg123_format_to_sdl(int fmt) } } -/* +/*#define DEBUG_MPG123*/ +#ifdef DEBUG_MPG123 static const char *mpg123_format_str(int fmt) { switch (fmt) @@ -167,7 +170,7 @@ static const char *mpg123_format_str(int fmt) } return "unknown"; } -*/ +#endif static char const* mpg_err(mpg123_handle* mpg, int result) { @@ -212,7 +215,8 @@ static int MPG123_Open(const SDL_AudioSpec *spec) static void *MPG123_CreateFromRW(SDL_RWops *src, int freesrc) { MPG123_Music *music; - int result; + int result, format, channels, encoding; + long rate; const long *rates; size_t i, num_rates; @@ -283,6 +287,28 @@ static void *MPG123_CreateFromRW(SDL_RWops *src, int freesrc) return NULL; } + result = mpg123.mpg123_getformat(music->handle, &rate, &channels, &encoding); + if (result != MPG123_OK) { + MPG123_Delete(music); + Mix_SetError("mpg123_getformat: %s", mpg_err(music->handle, result)); + return NULL; + } +#ifdef DEBUG_MPG123 + printf("MPG123 format: %s, channels: %d, rate: %ld\n", + mpg123_format_str(encoding), channels, rate); +#endif + + format = mpg123_format_to_sdl(encoding); + SDL_assert(format != -1); + music->sample_rate = rate; + + music->stream = SDL_NewAudioStream((SDL_AudioFormat)format, (Uint8)channels, (int)rate, + music_spec.format, music_spec.channels, music_spec.freq); + if (!music->stream) { + MPG123_Delete(music); + return NULL; + } + music->freesrc = freesrc; return music; } @@ -336,7 +362,10 @@ static int MPG123_GetSome(void *context, void *data, int bytes, SDL_bool *done) Mix_SetError("mpg123_getformat: %s", mpg_err(music->handle, result)); return -1; } -/*printf("MPG123 format: %s, channels = %d, rate = %ld\n", mpg123_format_str(encoding), channels, rate);*/ +#ifdef DEBUG_MPG123 + printf("MPG123 format: %s, channels: %d, rate: %ld\n", + mpg123_format_str(encoding), channels, rate); +#endif format = mpg123_format_to_sdl(encoding); SDL_assert(format != -1); @@ -346,6 +375,7 @@ static int MPG123_GetSome(void *context, void *data, int bytes, SDL_bool *done) if (!music->stream) { return -1; } + music->sample_rate = rate; break; case MPG123_DONE: @@ -377,7 +407,7 @@ static int MPG123_GetAudio(void *context, void *data, int bytes) static int MPG123_Seek(void *context, double secs) { MPG123_Music *music = (MPG123_Music *)context; - off_t offset = (off_t)(music_spec.freq * secs); + off_t offset = (off_t)(music->sample_rate * secs); if ((offset = mpg123.mpg123_seek(music->handle, offset, SEEK_SET)) < 0) { return Mix_SetError("mpg123_seek: %s", mpg_err(music->handle, (int)-offset));