From 067cf7e0211a3fbce80614a42b33abfc67c7f338 Mon Sep 17 00:00:00 2001 From: Thomas Jentzsch Date: Wed, 14 Sep 2022 17:38:47 +0200 Subject: [PATCH] fixed interrupted WAV continuing to play at restart --- src/common/SoundSDL2.cxx | 19 +++++++++++++++++++ src/common/SoundSDL2.hxx | 5 +++++ src/emucore/Console.cxx | 1 + 3 files changed, 25 insertions(+) diff --git a/src/common/SoundSDL2.cxx b/src/common/SoundSDL2.cxx index 1c894e924..61647f39f 100644 --- a/src/common/SoundSDL2.cxx +++ b/src/common/SoundSDL2.cxx @@ -441,7 +441,26 @@ bool SoundSDL2::playWav(const string& fileName, const uInt32 position, myWavLen = length ? std::min(length, myWavLength - position) : myWavLength; +#ifndef RESAMPLE_WAV myWavPos = myWavBuffer + position; +#else + SDL_free(myCvt.buf); + + //const double speed = BSPF::clamp(262 * 60 * 2. / myEmulationTiming->audioSampleRate(), 0.5, 2.0); + const double speed = 262 * 60 * 2. / myEmulationTiming->audioSampleRate(); + + SDL_BuildAudioCVT(&myCvt, myWavSpec.format, myWavSpec.channels, myWavSpec.freq, + myWavSpec.format, myWavSpec.channels, myWavSpec.freq * speed); + SDL_assert(myCvt.needed); // Obviously, this one is always needed. + myCvt.len = myWavLen * myWavSpec.channels; // Mono 8 bit sample frames + myCvt.buf = (uInt8*)SDL_malloc(myCvt.len * myCvt.len_mult * 2); // Double buffer size to avoid memory access exception + // Read original data into conversion buffer + SDL_memcpy(myCvt.buf, myWavBuffer + position, myCvt.len); + SDL_ConvertAudio(&myCvt); + + myWavPos = myCvt.buf; + myWavLen = myCvt.len_cvt; +#endif // Open audio device if(!myWavDevice) diff --git a/src/common/SoundSDL2.hxx b/src/common/SoundSDL2.hxx index e5fd57fb6..7b5bdf673 100644 --- a/src/common/SoundSDL2.hxx +++ b/src/common/SoundSDL2.hxx @@ -20,6 +20,8 @@ #ifndef SOUND_SDL2_HXX #define SOUND_SDL2_HXX +//#define RESAMPLE_WAV + class OSystem; class AudioQueue; class EmulationTiming; @@ -192,6 +194,9 @@ class SoundSDL2 : public Sound uInt32 myWavLength{0}; SDL_AudioDeviceID myWavDevice{0}; uInt8* myWavBuffer{nullptr}; +#ifdef RESAMPLE_WAV + SDL_AudioCVT myCvt{0}; +#endif static float myWavVolumeFactor; static SDL_AudioSpec myWavSpec; // audio output format diff --git a/src/emucore/Console.cxx b/src/emucore/Console.cxx index e0af1daf8..8b0615fc6 100644 --- a/src/emucore/Console.cxx +++ b/src/emucore/Console.cxx @@ -253,6 +253,7 @@ Console::~Console() // Close audio to prevent invalid access to myConsoleTiming from the audio // callback myOSystem.sound().close(); + myOSystem.sound().stopWav(); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -