Directly use sound sample rate instead of "quality"

git-svn-id: https://svn.code.sf.net/p/vbam/code/trunk@827 a31d4220-a93d-0410-bf67-fe4944624d44
This commit is contained in:
bgk 2008-12-30 10:42:34 +00:00
parent b7197a8a92
commit 49b5ee430a
10 changed files with 34 additions and 42 deletions

View File

@ -41,7 +41,7 @@ extern bool stopState; // TODO: silence sound when true
int const SOUND_CLOCK_TICKS_ = 167772; // 1/100 second int const SOUND_CLOCK_TICKS_ = 167772; // 1/100 second
static u16 soundFinalWave [1470]; static u16 soundFinalWave [1470];
int soundQuality = 1; long soundSampleRate = 44100;
bool soundInterpolation = true; bool soundInterpolation = true;
bool soundPaused = true; bool soundPaused = true;
float soundFiltering = 0.5f; float soundFiltering = 0.5f;
@ -448,8 +448,7 @@ static void remake_stereo_buffer()
stereo_buffer = 0; stereo_buffer = 0;
stereo_buffer = new Stereo_Buffer; // TODO: handle out of memory stereo_buffer = new Stereo_Buffer; // TODO: handle out of memory
long const sample_rate = 44100 / soundQuality; stereo_buffer->set_sample_rate( soundSampleRate ); // TODO: handle out of memory
stereo_buffer->set_sample_rate( sample_rate ); // TODO: handle out of memory
stereo_buffer->clock_rate( gb_apu->clock_rate ); stereo_buffer->clock_rate( gb_apu->clock_rate );
// PCM // PCM
@ -534,7 +533,7 @@ bool soundInit()
if ( !soundDriver ) if ( !soundDriver )
return false; return false;
if (!soundDriver->init(soundQuality)) if (!soundDriver->init(soundSampleRate))
return false; return false;
soundPaused = true; soundPaused = true;
@ -543,22 +542,24 @@ bool soundInit()
int soundGetQuality() int soundGetQuality()
{ {
return soundQuality; return 44100 / soundSampleRate;
} }
void soundSetQuality(int quality) void soundSetQuality(int quality)
{ {
if ( soundQuality != quality ) long newSampleRate = 44100 / quality;
if ( soundSampleRate != newSampleRate )
{ {
if ( systemCanChangeSoundQuality() ) if ( systemCanChangeSoundQuality() )
{ {
soundShutdown(); soundShutdown();
soundQuality = quality; soundSampleRate = newSampleRate;
soundInit(); soundInit();
} }
else else
{ {
soundQuality = quality; soundSampleRate = newSampleRate;
} }
remake_stereo_buffer(); remake_stereo_buffer();

View File

@ -33,9 +33,9 @@ public:
/** /**
* Initialize the sound driver. * 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 * Tell the driver that the sound stream has paused

View File

@ -94,25 +94,13 @@ void SoundSDL::write(const u16 * finalWave, int length)
SDL_mutexV(_mutex); SDL_mutexV(_mutex);
} }
bool SoundSDL::init(int quality) bool SoundSDL::init(long sampleRate)
{ {
SDL_AudioSpec audio; SDL_AudioSpec audio;
switch(quality) { _bufferLen = sampleRate / 15;
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;
}
audio.freq = sampleRate;
audio.format = AUDIO_S16SYS; audio.format = AUDIO_S16SYS;
audio.channels = 2; audio.channels = 2;
audio.samples = 1024; audio.samples = 1024;

View File

@ -25,7 +25,7 @@ class SoundSDL: public SoundDriver
public: public:
virtual ~SoundSDL(); virtual ~SoundSDL();
virtual bool init(int quality); virtual bool init(long sampleRate);
virtual void pause(); virtual void pause();
virtual void reset(); virtual void reset();
virtual void resume(); virtual void resume();

View File

@ -9,6 +9,7 @@
#include "gb_apu/Effects_Buffer.h" #include "gb_apu/Effects_Buffer.h"
extern int gbHardware; extern int gbHardware;
extern long soundSampleRate; // current sound quality
gb_effects_config_t gb_effects_config = { false, 0.20f, 0.15f, false }; 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 = 0;
stereo_buffer = new Simple_Effects_Buffer; // TODO: handle out of memory 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 ); stereo_buffer->clock_rate( gb_apu->clock_rate );
// APU // APU
@ -217,17 +218,19 @@ void gbSoundReset()
void gbSoundSetQuality(int quality) void gbSoundSetQuality(int quality)
{ {
if ( soundQuality != quality ) long newSampleRate = 44100 / quality;
if ( soundSampleRate != newSampleRate )
{ {
if ( systemCanChangeSoundQuality() ) if ( systemCanChangeSoundQuality() )
{ {
soundShutdown(); soundShutdown();
soundQuality = quality; soundSampleRate = newSampleRate;
soundInit(); soundInit();
} }
else else
{ {
soundQuality = quality; soundSampleRate = newSampleRate;
} }
remake_stereo_buffer(); remake_stereo_buffer();

View File

@ -9,7 +9,6 @@
// Sets sample rate to 44100 / quality // Sets sample rate to 44100 / quality
void gbSoundSetQuality( int quality ); void gbSoundSetQuality( int quality );
extern int soundQuality; // current sound quality
// Manages declicking mode. When enabled, clicks are reduced. Note that clicks // Manages declicking mode. When enabled, clicks are reduced. Note that clicks
// are normal for GB and GBC sound hardware. // are normal for GB and GBC sound hardware.

View File

@ -721,7 +721,7 @@ void sdlReadPreferences(FILE *f)
} else if(!strcmp(key, "captureFormat")) { } else if(!strcmp(key, "captureFormat")) {
captureFormat = sdlFromHex(value); captureFormat = sdlFromHex(value);
} else if(!strcmp(key, "soundQuality")) { } else if(!strcmp(key, "soundQuality")) {
soundQuality = sdlFromHex(value); int soundQuality = sdlFromHex(value);
switch(soundQuality) { switch(soundQuality) {
case 1: case 1:
case 2: case 2:
@ -733,6 +733,7 @@ void sdlReadPreferences(FILE *f)
soundQuality = 2; soundQuality = 2;
break; break;
} }
soundSetQuality(soundQuality);
} else if(!strcmp(key, "soundEnable")) { } else if(!strcmp(key, "soundEnable")) {
int res = sdlFromHex(value) & 0x30f; int res = sdlFromHex(value) & 0x30f;
soundSetEnable(res); soundSetEnable(res);

View File

@ -34,7 +34,7 @@ public:
DirectSound(); DirectSound();
virtual ~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 pause(); // pause the secondary sound buffer
void reset(); // stop and reset the secondary sound buffer void reset(); // stop and reset the secondary sound buffer
void resume(); // resume 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; HRESULT hr;
DWORD freq; DWORD freq;
@ -123,7 +123,7 @@ bool DirectSound::init(int quality)
return false; return false;
} }
freq = 44100 / quality; freq = sampleRate;
// calculate the number of samples per frame first // calculate the number of samples per frame first
// then multiply it with the size of a sample frame (16 bit * stereo) // then multiply it with the size of a sample frame (16 bit * stereo)
soundBufferLen = ( freq / 60 ) * 4; soundBufferLen = ( freq / 60 ) * 4;

View File

@ -37,7 +37,7 @@ public:
OpenAL(); OpenAL();
virtual ~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 pause(); // pause the secondary sound buffer
void reset(); // stop and reset the secondary sound buffer void reset(); // stop and reset the secondary sound buffer
void resume(); // play/resume the secondary sound buffer void resume(); // play/resume the secondary sound buffer
@ -145,7 +145,7 @@ void OpenAL::debugState()
#endif #endif
bool OpenAL::init(int quality) bool OpenAL::init(long sampleRate)
{ {
winlog( "OpenAL::init\n" ); winlog( "OpenAL::init\n" );
assert( initialized == false ); assert( initialized == false );
@ -174,7 +174,7 @@ bool OpenAL::init(int quality)
ALFunction.alGenSources( 1, &source ); ALFunction.alGenSources( 1, &source );
ASSERT_SUCCESS; ASSERT_SUCCESS;
freq = 44100 / quality; freq = sampleRate;
// calculate the number of samples per frame first // calculate the number of samples per frame first
// then multiply it with the size of a sample frame (16 bit * stereo) // then multiply it with the size of a sample frame (16 bit * stereo)

View File

@ -56,7 +56,7 @@ public:
~XAudio2_Output(); ~XAudio2_Output();
// Initialization // Initialization
bool init(int quality); bool init(long sampleRate);
// Sound Data Feed // Sound Data Feed
void write(const u16 * finalWave, int length); 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; 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 // calculate the number of samples per frame first
// then multiply it with the size of a sample frame (16 bit * stereo) // then multiply it with the size of a sample frame (16 bit * stereo)
@ -360,7 +360,7 @@ void XAudio2_Output::reset()
sVoice->FlushSourceBuffers(); sVoice->FlushSourceBuffers();
sVoice->Start( 0 ); sVoice->Start( 0 );
playing = true; playing = true;
} }