From e84500bb3e5314c47adfaf9c98e73e4113f86a9b Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Mon, 18 Sep 2017 16:05:46 -0700 Subject: [PATCH] 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 | 1 + external/smpeg2-2.0.0/audio/MPEGaudio.cpp | 3 +++ external/smpeg2-2.0.0/audio/mpegtoraw.cpp | 14 +++++++------- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/external/smpeg2-2.0.0/MPEGaudio.h b/external/smpeg2-2.0.0/MPEGaudio.h index 3f315d2a..54c340e1 100644 --- a/external/smpeg2-2.0.0/MPEGaudio.h +++ b/external/smpeg2-2.0.0/MPEGaudio.h @@ -186,6 +186,7 @@ class MPEGaudio : public MPEGerror, public MPEGaudioaction { double rate_in_s; Uint32 frags_playing; Uint32 frag_time; + SDL_AudioFormat format; #ifdef THREADED_AUDIO bool decoding; SDL_Thread *decode_thread; diff --git a/external/smpeg2-2.0.0/audio/MPEGaudio.cpp b/external/smpeg2-2.0.0/audio/MPEGaudio.cpp index 9728d4c9..45c69dc9 100644 --- a/external/smpeg2-2.0.0/audio/MPEGaudio.cpp +++ b/external/smpeg2-2.0.0/audio/MPEGaudio.cpp @@ -34,6 +34,7 @@ MPEGaudio:: MPEGaudio(MPEGstream *stream, bool initSDL) , rate_in_s(0.0) , frags_playing(0) , frag_time(0) + , format(0) #ifdef THREADED_AUDIO , decoding(false) , decode_thread(NULL) @@ -155,6 +156,8 @@ MPEGaudio:: ActualSpec(const SDL_AudioSpec *actual) } rate_in_s=((double)((actual->format&0xFF)/8)*actual->channels*actual->freq); stereo=((actual->channels-1) > 0); + + format = actual->format; } #ifdef THREADED_AUDIO diff --git a/external/smpeg2-2.0.0/audio/mpegtoraw.cpp b/external/smpeg2-2.0.0/audio/mpegtoraw.cpp index 493c8709..22dcf9fe 100644 --- a/external/smpeg2-2.0.0/audio/mpegtoraw.cpp +++ b/external/smpeg2-2.0.0/audio/mpegtoraw.cpp @@ -429,7 +429,7 @@ int Play_MPEGaudio(MPEGaudio *audio, Uint8 *stream, int len) quite right */ copylen = audio->ring->NextReadBuffer(&rbuf); if ( copylen > len ) { - SDL_MixAudio(stream, rbuf, len, volume); + SDL_MixAudioFormat(stream, rbuf, audio->format, len, volume); mixed += len; audio->ring->ReadSome(len); len = 0; @@ -437,7 +437,7 @@ int Play_MPEGaudio(MPEGaudio *audio, Uint8 *stream, int len) audio->timestamp[i] = audio->timestamp[i+1]; audio->timestamp[N_TIMESTAMPS-1] = audio->ring->ReadTimeStamp(); } else { - SDL_MixAudio(stream, rbuf, copylen, volume); + SDL_MixAudioFormat(stream, rbuf, audio->format, copylen, volume); mixed += copylen; ++audio->currentframe; audio->ring->ReadDone(); @@ -477,14 +477,14 @@ int Play_MPEGaudio(MPEGaudio *audio, Uint8 *stream, int len) copylen = (audio->rawdatawriteoffset-audio->rawdatareadoffset); assert(copylen >= 0); if ( copylen >= len ) { - SDL_MixAudio(stream, (Uint8 *)&audio->spillover[audio->rawdatareadoffset], - len*2, volume); + SDL_MixAudioFormat(stream, (Uint8 *)&audio->spillover[audio->rawdatareadoffset], + audio->format, len*2, volume); mixed += len*2; audio->rawdatareadoffset += len; goto finished_mixing; } - SDL_MixAudio(stream, (Uint8 *)&audio->spillover[audio->rawdatareadoffset], - copylen*2, volume); + SDL_MixAudioFormat(stream, (Uint8 *)&audio->spillover[audio->rawdatareadoffset], + audio->format, copylen*2, volume); mixed += copylen*2; len -= copylen; stream += copylen*2; @@ -503,7 +503,7 @@ int Play_MPEGaudio(MPEGaudio *audio, Uint8 *stream, int len) audio->rawdatawriteoffset = 0; if ( audio->run(1) ) { assert(audio->rawdatawriteoffset > len); - SDL_MixAudio(stream, (Uint8 *) audio->spillover, len*2, volume); + SDL_MixAudioFormat(stream, (Uint8 *) audio->spillover, audio->format, len*2, volume); mixed += len*2; audio->rawdatareadoffset = len; } else {