diff --git a/src/Sound.cpp b/src/Sound.cpp index 91f83ede..2f7b91d9 100644 --- a/src/Sound.cpp +++ b/src/Sound.cpp @@ -41,7 +41,7 @@ extern bool stopState; // TODO: silence sound when true int const SOUND_CLOCK_TICKS_ = 167772; // 1/100 second static u16 soundFinalWave [1470]; -int soundQuality = 1; +long soundSampleRate = 44100; bool soundInterpolation = true; bool soundPaused = true; float soundFiltering = 0.5f; @@ -448,8 +448,7 @@ static void remake_stereo_buffer() stereo_buffer = 0; stereo_buffer = new Stereo_Buffer; // TODO: handle out of memory - long const sample_rate = 44100 / soundQuality; - stereo_buffer->set_sample_rate( sample_rate ); // TODO: handle out of memory + stereo_buffer->set_sample_rate( soundSampleRate ); // TODO: handle out of memory stereo_buffer->clock_rate( gb_apu->clock_rate ); // PCM @@ -534,7 +533,7 @@ bool soundInit() if ( !soundDriver ) return false; - if (!soundDriver->init(soundQuality)) + if (!soundDriver->init(soundSampleRate)) return false; soundPaused = true; @@ -543,22 +542,24 @@ bool soundInit() int soundGetQuality() { - return soundQuality; + return 44100 / soundSampleRate; } void soundSetQuality(int quality) { - if ( soundQuality != quality ) + long newSampleRate = 44100 / quality; + + if ( soundSampleRate != newSampleRate ) { if ( systemCanChangeSoundQuality() ) { soundShutdown(); - soundQuality = quality; + soundSampleRate = newSampleRate; soundInit(); } else { - soundQuality = quality; + soundSampleRate = newSampleRate; } remake_stereo_buffer(); diff --git a/src/common/SoundDriver.h b/src/common/SoundDriver.h index 671e39af..7ecd1182 100644 --- a/src/common/SoundDriver.h +++ b/src/common/SoundDriver.h @@ -33,9 +33,9 @@ public: /** * Initialize the sound driver. - * @param quality Sound frequency : 1 => 44100 Hz, 2 => 22050 Hz, 4 => 11025 Hz + * @param sampleRate In Hertz */ - virtual bool init(int quality) = 0; + virtual bool init(long sampleRate) = 0; /** * Tell the driver that the sound stream has paused diff --git a/src/common/SoundSDL.cpp b/src/common/SoundSDL.cpp index 3f8f2e13..c116e389 100644 --- a/src/common/SoundSDL.cpp +++ b/src/common/SoundSDL.cpp @@ -94,25 +94,13 @@ void SoundSDL::write(const u16 * finalWave, int length) SDL_mutexV(_mutex); } -bool SoundSDL::init(int quality) +bool SoundSDL::init(long sampleRate) { SDL_AudioSpec audio; - switch(quality) { - case 1: - audio.freq = 44100; - _bufferLen = 1470*2; - break; - case 2: - audio.freq = 22050; - _bufferLen = 736*2; - break; - case 4: - audio.freq = 11025; - _bufferLen = 368*2; - break; - } + _bufferLen = sampleRate / 15; + audio.freq = sampleRate; audio.format = AUDIO_S16SYS; audio.channels = 2; audio.samples = 1024; diff --git a/src/common/SoundSDL.h b/src/common/SoundSDL.h index 1612d2b1..e7de9f79 100644 --- a/src/common/SoundSDL.h +++ b/src/common/SoundSDL.h @@ -25,7 +25,7 @@ class SoundSDL: public SoundDriver public: virtual ~SoundSDL(); - virtual bool init(int quality); + virtual bool init(long sampleRate); virtual void pause(); virtual void reset(); virtual void resume(); diff --git a/src/dmg/gbSound.cpp b/src/dmg/gbSound.cpp index 83c6b9ac..13e9ebba 100644 --- a/src/dmg/gbSound.cpp +++ b/src/dmg/gbSound.cpp @@ -9,6 +9,7 @@ #include "gb_apu/Effects_Buffer.h" extern int gbHardware; +extern long soundSampleRate; // current sound quality gb_effects_config_t gb_effects_config = { false, 0.20f, 0.15f, false }; @@ -125,7 +126,7 @@ static void remake_stereo_buffer() stereo_buffer = 0; stereo_buffer = new Simple_Effects_Buffer; // TODO: handle out of memory - if ( stereo_buffer->set_sample_rate( 44100 / soundQuality ) ) { } // TODO: handle out of memory + if ( stereo_buffer->set_sample_rate( soundSampleRate ) ) { } // TODO: handle out of memory stereo_buffer->clock_rate( gb_apu->clock_rate ); // APU @@ -217,17 +218,19 @@ void gbSoundReset() void gbSoundSetQuality(int quality) { - if ( soundQuality != quality ) + long newSampleRate = 44100 / quality; + + if ( soundSampleRate != newSampleRate ) { if ( systemCanChangeSoundQuality() ) { soundShutdown(); - soundQuality = quality; + soundSampleRate = newSampleRate; soundInit(); } else { - soundQuality = quality; + soundSampleRate = newSampleRate; } remake_stereo_buffer(); diff --git a/src/dmg/gbSound.h b/src/dmg/gbSound.h index 5e059c4b..07834093 100644 --- a/src/dmg/gbSound.h +++ b/src/dmg/gbSound.h @@ -9,7 +9,6 @@ // Sets sample rate to 44100 / quality void gbSoundSetQuality( int quality ); -extern int soundQuality; // current sound quality // Manages declicking mode. When enabled, clicks are reduced. Note that clicks // are normal for GB and GBC sound hardware. diff --git a/src/sdl/SDL.cpp b/src/sdl/SDL.cpp index 5e4fb15b..86ebc42b 100644 --- a/src/sdl/SDL.cpp +++ b/src/sdl/SDL.cpp @@ -721,7 +721,7 @@ void sdlReadPreferences(FILE *f) } else if(!strcmp(key, "captureFormat")) { captureFormat = sdlFromHex(value); } else if(!strcmp(key, "soundQuality")) { - soundQuality = sdlFromHex(value); + int soundQuality = sdlFromHex(value); switch(soundQuality) { case 1: case 2: @@ -733,6 +733,7 @@ void sdlReadPreferences(FILE *f) soundQuality = 2; break; } + soundSetQuality(soundQuality); } else if(!strcmp(key, "soundEnable")) { int res = sdlFromHex(value) & 0x30f; soundSetEnable(res); diff --git a/src/win32/DirectSound.cpp b/src/win32/DirectSound.cpp index f45f540e..051d6658 100644 --- a/src/win32/DirectSound.cpp +++ b/src/win32/DirectSound.cpp @@ -34,7 +34,7 @@ public: DirectSound(); virtual ~DirectSound(); - bool init(int quality); // initialize the primary and secondary sound buffer + bool init(long sampleRate); // initialize the primary and secondary sound buffer void pause(); // pause the secondary sound buffer void reset(); // stop and reset the secondary sound buffer void resume(); // resume the secondary sound buffer @@ -85,7 +85,7 @@ DirectSound::~DirectSound() } -bool DirectSound::init(int quality) +bool DirectSound::init(long sampleRate) { HRESULT hr; DWORD freq; @@ -123,7 +123,7 @@ bool DirectSound::init(int quality) return false; } - freq = 44100 / quality; + freq = sampleRate; // calculate the number of samples per frame first // then multiply it with the size of a sample frame (16 bit * stereo) soundBufferLen = ( freq / 60 ) * 4; diff --git a/src/win32/OpenAL.cpp b/src/win32/OpenAL.cpp index b76669a6..765ae303 100644 --- a/src/win32/OpenAL.cpp +++ b/src/win32/OpenAL.cpp @@ -37,7 +37,7 @@ public: OpenAL(); virtual ~OpenAL(); - bool init(int quality); // initialize the sound buffer queue + bool init(long sampleRate); // initialize the sound buffer queue void pause(); // pause the secondary sound buffer void reset(); // stop and reset the secondary sound buffer void resume(); // play/resume the secondary sound buffer @@ -145,7 +145,7 @@ void OpenAL::debugState() #endif -bool OpenAL::init(int quality) +bool OpenAL::init(long sampleRate) { winlog( "OpenAL::init\n" ); assert( initialized == false ); @@ -174,7 +174,7 @@ bool OpenAL::init(int quality) ALFunction.alGenSources( 1, &source ); ASSERT_SUCCESS; - freq = 44100 / quality; + freq = sampleRate; // calculate the number of samples per frame first // then multiply it with the size of a sample frame (16 bit * stereo) diff --git a/src/win32/XAudio2.cpp b/src/win32/XAudio2.cpp index 8a0d48c9..10feca84 100644 --- a/src/win32/XAudio2.cpp +++ b/src/win32/XAudio2.cpp @@ -56,7 +56,7 @@ public: ~XAudio2_Output(); // Initialization - bool init(int quality); + bool init(long sampleRate); // Sound Data Feed void write(const u16 * finalWave, int length); @@ -137,7 +137,7 @@ XAudio2_Output::~XAudio2_Output() } -bool XAudio2_Output::init(int quality) +bool XAudio2_Output::init(long sampleRate) { if( failed || initialized ) return false; @@ -157,7 +157,7 @@ bool XAudio2_Output::init(int quality) } - freq = 44100 / (UINT32)quality; + freq = sampleRate; // calculate the number of samples per frame first // then multiply it with the size of a sample frame (16 bit * stereo) @@ -360,7 +360,7 @@ void XAudio2_Output::reset() sVoice->FlushSourceBuffers(); sVoice->Start( 0 ); - playing = true; + playing = true; }