mirror of https://github.com/stella-emu/stella.git
added on-the-fly WAV resampling (disabled, causes echo)
This commit is contained in:
parent
1263f28e49
commit
5e09f486af
|
@ -175,6 +175,9 @@ void SoundSDL2::open(shared_ptr<AudioQueue> audioQueue,
|
||||||
openDevice();
|
openDevice();
|
||||||
|
|
||||||
myEmulationTiming = emulationTiming;
|
myEmulationTiming = emulationTiming;
|
||||||
|
#ifdef RESAMPLE_WAV_CB
|
||||||
|
myWavSpeed = 262 * 60 * 2. / myEmulationTiming->audioSampleRate();
|
||||||
|
#endif
|
||||||
|
|
||||||
Logger::debug("SoundSDL2::open started ...");
|
Logger::debug("SoundSDL2::open started ...");
|
||||||
mute(true);
|
mute(true);
|
||||||
|
@ -503,6 +506,34 @@ void SoundSDL2::wavCallback(void* udata, uInt8* stream, int len)
|
||||||
{
|
{
|
||||||
SDL_memset(stream, myWavSpec.silence, len);
|
SDL_memset(stream, myWavSpec.silence, len);
|
||||||
if(myWavLen)
|
if(myWavLen)
|
||||||
|
{
|
||||||
|
#ifdef RESAMPLE_WAV_CB
|
||||||
|
if(myWavSpeed != 1.0)
|
||||||
|
{
|
||||||
|
int newLen = std::round(len / myWavSpeed);
|
||||||
|
const int newFreq = std::round(static_cast<double>(myWavSpec.freq) * len / newLen);
|
||||||
|
|
||||||
|
if(static_cast<uInt32>(newLen) > myWavLen)
|
||||||
|
newLen = myWavLen;
|
||||||
|
|
||||||
|
SDL_AudioCVT cvt;
|
||||||
|
SDL_BuildAudioCVT(&cvt, myWavSpec.format, myWavSpec.channels, myWavSpec.freq,
|
||||||
|
myWavSpec.format, myWavSpec.channels, newFreq);
|
||||||
|
SDL_assert(cvt.needed); // Obviously, this one is always needed.
|
||||||
|
cvt.len = newLen * myWavSpec.channels; // Mono 8 bit sample frames
|
||||||
|
cvt.buf = static_cast<uInt8*>(SDL_malloc(cvt.len * cvt.len_mult * 2)); // Double buffer size to avoid memory access exception
|
||||||
|
// Read original data into conversion buffer
|
||||||
|
SDL_memcpy(cvt.buf, myWavPos, cvt.len);
|
||||||
|
SDL_ConvertAudio(&cvt);
|
||||||
|
// Mix volume adjusted WAV data into silent buffer
|
||||||
|
SDL_MixAudioFormat(stream, cvt.buf, myWavSpec.format, cvt.len_cvt,
|
||||||
|
SDL_MIX_MAXVOLUME * myWavVolumeFactor);
|
||||||
|
SDL_free(cvt.buf);
|
||||||
|
|
||||||
|
cerr << cvt.len_cvt << " ";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
#endif
|
||||||
{
|
{
|
||||||
if(static_cast<uInt32>(len) > myWavLen)
|
if(static_cast<uInt32>(len) > myWavLen)
|
||||||
len = myWavLen;
|
len = myWavLen;
|
||||||
|
@ -510,6 +541,7 @@ void SoundSDL2::wavCallback(void* udata, uInt8* stream, int len)
|
||||||
// Mix volume adjusted WAV data into silent buffer
|
// Mix volume adjusted WAV data into silent buffer
|
||||||
SDL_MixAudioFormat(stream, myWavPos, myWavSpec.format, len,
|
SDL_MixAudioFormat(stream, myWavPos, myWavSpec.format, len,
|
||||||
SDL_MIX_MAXVOLUME * myWavVolumeFactor);
|
SDL_MIX_MAXVOLUME * myWavVolumeFactor);
|
||||||
|
}
|
||||||
myWavPos += len;
|
myWavPos += len;
|
||||||
myWavLen -= len;
|
myWavLen -= len;
|
||||||
}
|
}
|
||||||
|
@ -520,5 +552,8 @@ float SoundSDL2::myWavVolumeFactor = 0xffff;
|
||||||
SDL_AudioSpec SoundSDL2::myWavSpec; // audio output format
|
SDL_AudioSpec SoundSDL2::myWavSpec; // audio output format
|
||||||
uInt8* SoundSDL2::myWavPos = nullptr; // pointer to the audio buffer to be played
|
uInt8* SoundSDL2::myWavPos = nullptr; // pointer to the audio buffer to be played
|
||||||
uInt32 SoundSDL2::myWavLen = 0; // remaining length of the sample we have to play
|
uInt32 SoundSDL2::myWavLen = 0; // remaining length of the sample we have to play
|
||||||
|
#ifdef RESAMPLE_WAV_CB
|
||||||
|
double SoundSDL2::myWavSpeed = 1.0;
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif // SOUND_SUPPORT
|
#endif // SOUND_SUPPORT
|
||||||
|
|
|
@ -20,7 +20,8 @@
|
||||||
#ifndef SOUND_SDL2_HXX
|
#ifndef SOUND_SDL2_HXX
|
||||||
#define SOUND_SDL2_HXX
|
#define SOUND_SDL2_HXX
|
||||||
|
|
||||||
//#define RESAMPLE_WAV
|
//#define RESAMPLE_WAV // does not adjust to speed immediately
|
||||||
|
//#define RESAMPLE_WAV_CB // causes echo in in-the-fly resampling
|
||||||
|
|
||||||
class OSystem;
|
class OSystem;
|
||||||
class AudioQueue;
|
class AudioQueue;
|
||||||
|
@ -197,6 +198,9 @@ class SoundSDL2 : public Sound
|
||||||
#ifdef RESAMPLE_WAV
|
#ifdef RESAMPLE_WAV
|
||||||
SDL_AudioCVT myCvt{0};
|
SDL_AudioCVT myCvt{0};
|
||||||
#endif
|
#endif
|
||||||
|
#ifdef RESAMPLE_WAV_CB
|
||||||
|
static double myWavSpeed;
|
||||||
|
#endif
|
||||||
|
|
||||||
static float myWavVolumeFactor;
|
static float myWavVolumeFactor;
|
||||||
static SDL_AudioSpec myWavSpec; // audio output format
|
static SDL_AudioSpec myWavSpec; // audio output format
|
||||||
|
|
Loading…
Reference in New Issue