Fixed bug 3833 - Silent MP3 playback using included smpeg2 library
authorSam Lantinga
Mon, 18 Sep 2017 16:05:46 -0700
changeset 736f04d39d0b63c
parent 735 03a56a2f580a
child 737 d3fa63933df2
Fixed bug 3833 - Silent MP3 playback using included smpeg2 library

Holger Schemel

Summary: When playing MP3 files using the included smpeg2 library, audio output is silent.

How to reproduce the problem: Build latest SDL2_mixer and smpeg2 (from directory "external/smpeg2-2.0.0") and play MP3 file using included music player "playmus". (Tested on Ubuntu 12.04 and Mac OS X 10.11.)

Expected behaviour: "playmus" plays MP3 music through the computer's audio device.

Observed behaviour: "playmus" appears to be playing, but the computer's audio device remains silent.

The problem was introduced with changeset 718:fb0562cc1559, which replaces legacy functions like SDL_OpenAudio(), SDL_LockAudio(), SDL_PauseAudio(), SDL_MixAudio() etc. with their non-legacy counterparts like SDL_OpenAudioDevice(), SDL_LockAudioDevice(), SDL_PauseAudioDevice() and SDL_MixAudioFormat() in the SDL2_mixer code, while the smpeg2 code still uses the legacy functions.

Replacing SDL_MixAudio() with SDL_MixAudioFormat() in the smpeg2 code fixes the problem, and MP3 playback using SDL2_mixer with smpeg2 works fine again (from both "playmus" and from my SDL2/SDL2_mixer/smpeg2 based games).

The attached patch fixes the problem.
external/smpeg2-2.0.0/MPEGaudio.h
external/smpeg2-2.0.0/audio/MPEGaudio.cpp
external/smpeg2-2.0.0/audio/mpegtoraw.cpp
     1.1 --- a/external/smpeg2-2.0.0/MPEGaudio.h	Sat Sep 09 18:57:35 2017 -0700
     1.2 +++ b/external/smpeg2-2.0.0/MPEGaudio.h	Mon Sep 18 16:05:46 2017 -0700
     1.3 @@ -186,6 +186,7 @@
     1.4      double rate_in_s;
     1.5      Uint32 frags_playing;
     1.6      Uint32 frag_time;
     1.7 +    SDL_AudioFormat format;
     1.8  #ifdef THREADED_AUDIO
     1.9      bool decoding;
    1.10      SDL_Thread *decode_thread;
     2.1 --- a/external/smpeg2-2.0.0/audio/MPEGaudio.cpp	Sat Sep 09 18:57:35 2017 -0700
     2.2 +++ b/external/smpeg2-2.0.0/audio/MPEGaudio.cpp	Mon Sep 18 16:05:46 2017 -0700
     2.3 @@ -34,6 +34,7 @@
     2.4      , rate_in_s(0.0)
     2.5      , frags_playing(0)
     2.6      , frag_time(0)
     2.7 +    , format(0)
     2.8  #ifdef THREADED_AUDIO
     2.9      , decoding(false)
    2.10      , decode_thread(NULL)
    2.11 @@ -155,6 +156,8 @@
    2.12      }
    2.13      rate_in_s=((double)((actual->format&0xFF)/8)*actual->channels*actual->freq);
    2.14      stereo=((actual->channels-1) > 0);
    2.15 +
    2.16 +    format = actual->format;
    2.17  }
    2.18  
    2.19  #ifdef THREADED_AUDIO
     3.1 --- a/external/smpeg2-2.0.0/audio/mpegtoraw.cpp	Sat Sep 09 18:57:35 2017 -0700
     3.2 +++ b/external/smpeg2-2.0.0/audio/mpegtoraw.cpp	Mon Sep 18 16:05:46 2017 -0700
     3.3 @@ -429,7 +429,7 @@
     3.4  	   quite right */
     3.5          copylen = audio->ring->NextReadBuffer(&rbuf);
     3.6          if ( copylen > len ) {
     3.7 -            SDL_MixAudio(stream, rbuf, len, volume);
     3.8 +	    SDL_MixAudioFormat(stream, rbuf, audio->format, len, volume);
     3.9              mixed += len;
    3.10              audio->ring->ReadSome(len);
    3.11              len = 0;
    3.12 @@ -437,7 +437,7 @@
    3.13  		audio->timestamp[i] = audio->timestamp[i+1];
    3.14  	    audio->timestamp[N_TIMESTAMPS-1] = audio->ring->ReadTimeStamp();
    3.15          } else {
    3.16 -            SDL_MixAudio(stream, rbuf, copylen, volume);
    3.17 +	    SDL_MixAudioFormat(stream, rbuf, audio->format, copylen, volume);
    3.18              mixed += copylen;
    3.19              ++audio->currentframe;
    3.20              audio->ring->ReadDone();
    3.21 @@ -477,14 +477,14 @@
    3.22          copylen = (audio->rawdatawriteoffset-audio->rawdatareadoffset);
    3.23          assert(copylen >= 0);
    3.24          if ( copylen >= len ) {
    3.25 -            SDL_MixAudio(stream, (Uint8 *)&audio->spillover[audio->rawdatareadoffset],
    3.26 -                                                       len*2, volume);
    3.27 +            SDL_MixAudioFormat(stream, (Uint8 *)&audio->spillover[audio->rawdatareadoffset],
    3.28 +                                                       audio->format, len*2, volume);
    3.29              mixed += len*2;
    3.30              audio->rawdatareadoffset += len;
    3.31              goto finished_mixing;
    3.32          }
    3.33 -        SDL_MixAudio(stream, (Uint8 *)&audio->spillover[audio->rawdatareadoffset],
    3.34 -                                                       copylen*2, volume);
    3.35 +        SDL_MixAudioFormat(stream, (Uint8 *)&audio->spillover[audio->rawdatareadoffset],
    3.36 +                                                       audio->format, copylen*2, volume);
    3.37          mixed += copylen*2;
    3.38          len -= copylen;
    3.39          stream += copylen*2;
    3.40 @@ -503,7 +503,7 @@
    3.41      audio->rawdatawriteoffset = 0;
    3.42      if ( audio->run(1) ) {
    3.43          assert(audio->rawdatawriteoffset > len);
    3.44 -        SDL_MixAudio(stream, (Uint8 *) audio->spillover, len*2, volume);
    3.45 +        SDL_MixAudioFormat(stream, (Uint8 *) audio->spillover, audio->format, len*2, volume);
    3.46          mixed += len*2;
    3.47          audio->rawdatareadoffset = len;
    3.48      } else {