music_mpg123.c: seek fix, and initialize the stream on file open
authorVitaly Novichkov
Tue, 17 Dec 2019 15:55:02 +0300
changeset 10872019adc079cc
parent 1086 89f446aae9b5
child 1088 840f92752d54
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
src/codecs/music_mpg123.c
     1.1 --- a/CHANGES.txt	Tue Dec 17 14:56:04 2019 +0300
     1.2 +++ b/CHANGES.txt	Tue Dec 17 15:55:02 2019 +0300
     1.3 @@ -1,5 +1,8 @@
     1.4  2.0.5:
     1.5 -Ozkan Sezer - Sun Dec 17 14:56:04 2019
     1.6 +Vitaly Novichkov - Thu Dec 17 15:50:02 2019
     1.7 + * Fixed music_mpg123 seek bug when sample rate of the file and
     1.8 +   the stream don't match
     1.9 +Ozkan Sezer - Thu Dec 17 14:56:04 2019
    1.10   * Timidity improvements. New Mix_SetTimidityCfg() public api.
    1.11  Ozkan Sezer - Sun Dec 08 10:20:40 2019
    1.12   * Improved mp3 tag detection/skipping
     2.1 --- a/src/codecs/music_mpg123.c	Tue Dec 17 14:56:04 2019 +0300
     2.2 +++ b/src/codecs/music_mpg123.c	Tue Dec 17 15:55:02 2019 +0300
     2.3 @@ -131,12 +131,14 @@
     2.4      SDL_AudioStream *stream;
     2.5      unsigned char *buffer;
     2.6      size_t buffer_size;
     2.7 +    long sample_rate;
     2.8  } MPG123_Music;
     2.9  
    2.10  
    2.11  static int MPG123_Seek(void *context, double secs);
    2.12  static void MPG123_Delete(void *context);
    2.13  
    2.14 +
    2.15  static int mpg123_format_to_sdl(int fmt)
    2.16  {
    2.17      switch (fmt)
    2.18 @@ -151,7 +153,8 @@
    2.19      }
    2.20  }
    2.21  
    2.22 -/*
    2.23 +/*#define DEBUG_MPG123*/
    2.24 +#ifdef DEBUG_MPG123
    2.25  static const char *mpg123_format_str(int fmt)
    2.26  {
    2.27      switch (fmt)
    2.28 @@ -167,7 +170,7 @@
    2.29      }
    2.30      return "unknown";
    2.31  }
    2.32 -*/
    2.33 +#endif
    2.34  
    2.35  static char const* mpg_err(mpg123_handle* mpg, int result)
    2.36  {
    2.37 @@ -212,7 +215,8 @@
    2.38  static void *MPG123_CreateFromRW(SDL_RWops *src, int freesrc)
    2.39  {
    2.40      MPG123_Music *music;
    2.41 -    int result;
    2.42 +    int result, format, channels, encoding;
    2.43 +    long rate;
    2.44      const long *rates;
    2.45      size_t i, num_rates;
    2.46  
    2.47 @@ -283,6 +287,28 @@
    2.48          return NULL;
    2.49      }
    2.50  
    2.51 +    result = mpg123.mpg123_getformat(music->handle, &rate, &channels, &encoding);
    2.52 +    if (result != MPG123_OK) {
    2.53 +        MPG123_Delete(music);
    2.54 +        Mix_SetError("mpg123_getformat: %s", mpg_err(music->handle, result));
    2.55 +        return NULL;
    2.56 +    }
    2.57 +#ifdef DEBUG_MPG123
    2.58 +        printf("MPG123 format: %s, channels: %d, rate: %ld\n",
    2.59 +                mpg123_format_str(encoding), channels, rate);
    2.60 +#endif
    2.61 +
    2.62 +    format = mpg123_format_to_sdl(encoding);
    2.63 +    SDL_assert(format != -1);
    2.64 +    music->sample_rate = rate;
    2.65 +
    2.66 +    music->stream = SDL_NewAudioStream((SDL_AudioFormat)format, (Uint8)channels, (int)rate,
    2.67 +                                       music_spec.format, music_spec.channels, music_spec.freq);
    2.68 +    if (!music->stream) {
    2.69 +        MPG123_Delete(music);
    2.70 +        return NULL;
    2.71 +    }
    2.72 +
    2.73      music->freesrc = freesrc;
    2.74      return music;
    2.75  }
    2.76 @@ -336,7 +362,10 @@
    2.77              Mix_SetError("mpg123_getformat: %s", mpg_err(music->handle, result));
    2.78              return -1;
    2.79          }
    2.80 -/*printf("MPG123 format: %s, channels = %d, rate = %ld\n", mpg123_format_str(encoding), channels, rate);*/
    2.81 +#ifdef DEBUG_MPG123
    2.82 +        printf("MPG123 format: %s, channels: %d, rate: %ld\n",
    2.83 +                mpg123_format_str(encoding), channels, rate);
    2.84 +#endif
    2.85  
    2.86          format = mpg123_format_to_sdl(encoding);
    2.87          SDL_assert(format != -1);
    2.88 @@ -346,6 +375,7 @@
    2.89          if (!music->stream) {
    2.90              return -1;
    2.91          }
    2.92 +        music->sample_rate = rate;
    2.93          break;
    2.94  
    2.95      case MPG123_DONE:
    2.96 @@ -377,7 +407,7 @@
    2.97  static int MPG123_Seek(void *context, double secs)
    2.98  {
    2.99      MPG123_Music *music = (MPG123_Music *)context;
   2.100 -    off_t offset = (off_t)(music_spec.freq * secs);
   2.101 +    off_t offset = (off_t)(music->sample_rate * secs);
   2.102  
   2.103      if ((offset = mpg123.mpg123_seek(music->handle, offset, SEEK_SET)) < 0) {
   2.104          return Mix_SetError("mpg123_seek: %s", mpg_err(music->handle, (int)-offset));