Centralize fallback sample rate with kFallbackSampleRate

Replaces hardcoded 44100Hz values (used as fallbacks or for the "Default" sample rate preference) with a new constant `constexpr int kFallbackSampleRate = 44100;` defined in RageSound.h.

This constant is now used in:
- RageSoundReader_Silence
- RageSoundManager (for unloaded driver scenarios)
- All sound drivers' initialization logic for the "Default (0)" preference (DSound, WaveOut, AU, PulseAudio, OSS, WDMKS, Null)
- DSound primary buffer setup.
This commit is contained in:
Patrik Nilsson
2025-06-01 08:37:19 -07:00
committed by teejusb
parent c42501ba65
commit 72c7316671
11 changed files with 16 additions and 12 deletions
+1 -1
View File
@@ -147,7 +147,7 @@ public:
int SetPosition( int iFrame ) { return 1; } int SetPosition( int iFrame ) { return 1; }
int Read( float *pBuf, int iFrames ) { return RageSoundReader::END_OF_FILE; } int Read( float *pBuf, int iFrames ) { return RageSoundReader::END_OF_FILE; }
RageSoundReader *Copy() const { return new RageSoundReader_Silence; } RageSoundReader *Copy() const { return new RageSoundReader_Silence; }
int GetSampleRate() const { return 44100; } // Hardcoded to 44100 int GetSampleRate() const { return kFallbackSampleRate; }
unsigned GetNumChannels() const { return 1; } unsigned GetNumChannels() const { return 1; }
int GetNextSourceFrame() const { return 0; } int GetNextSourceFrame() const { return 0; }
float GetStreamToSourceRatio() const { return 1.0f; } float GetStreamToSourceRatio() const { return 1.0f; }
+2
View File
@@ -9,6 +9,8 @@
#include <cstdint> #include <cstdint>
constexpr int kFallbackSampleRate = 44100;
class RageSoundReader; class RageSoundReader;
struct lua_State; struct lua_State;
+1 -1
View File
@@ -133,7 +133,7 @@ float RageSoundManager::GetPlayLatency() const
int RageSoundManager::GetDriverSampleRate() const int RageSoundManager::GetDriverSampleRate() const
{ {
if( m_pDriver == nullptr ) if( m_pDriver == nullptr )
return 44100; // Fallback if no driver is loaded return kFallbackSampleRate;
return m_pDriver->GetSampleRate(); // Returns the *actual* operating rate of the loaded driver return m_pDriver->GetSampleRate(); // Returns the *actual* operating rate of the loaded driver
} }
+2 -1
View File
@@ -1,4 +1,5 @@
#include "global.h" #include "global.h"
#include "RageSound.h"
#include "RageLog.h" #include "RageLog.h"
#include "RageUtil.h" #include "RageUtil.h"
#include "ALSA9Helpers.h" #include "ALSA9Helpers.h"
@@ -229,7 +230,7 @@ RString Alsa9Buf::Init( int channels_,
preferred_writeahead = iWriteahead; preferred_writeahead = iWriteahead;
preferred_chunksize = iChunkSize; preferred_chunksize = iChunkSize;
if( iSampleRate == 0 ) if( iSampleRate == 0 )
samplerate = 44100; samplerate = kFallbackSampleRate;
else else
samplerate = iSampleRate; samplerate = iSampleRate;
+2 -2
View File
@@ -65,7 +65,7 @@ void DSound::SetPrimaryBufferMode()
int preferredSampleRate = PREFSMAN->m_iSoundPreferredSampleRate; int preferredSampleRate = PREFSMAN->m_iSoundPreferredSampleRate;
if (preferredSampleRate == 0) if (preferredSampleRate == 0)
{ {
preferredSampleRate = 44100; // Default to 44100 Hz if preference is 0 preferredSampleRate = kFallbackSampleRate;
} }
waveformat.nSamplesPerSec = preferredSampleRate; waveformat.nSamplesPerSec = preferredSampleRate;
waveformat.nBlockAlign = 4; waveformat.nBlockAlign = 4;
@@ -202,7 +202,7 @@ RString DSoundBuf::Init( DSound &ds, DSoundBuf::hw hardware,
bool bNeedCtrlFrequency = false; bool bNeedCtrlFrequency = false;
if( m_iSampleRate == DYNAMIC_SAMPLERATE ) // DYNAMIC_SAMPLERATE is usually 0 or some special value if( m_iSampleRate == DYNAMIC_SAMPLERATE ) // DYNAMIC_SAMPLERATE is usually 0 or some special value
{ {
m_iSampleRate = 44100; // If dynamic, default to 44100 for now m_iSampleRate = kFallbackSampleRate;
bNeedCtrlFrequency = true; bNeedCtrlFrequency = true;
} }
+3 -2
View File
@@ -6,13 +6,14 @@
#include "RageThreads.h" #include "RageThreads.h"
#include "RageTimer.h" #include "RageTimer.h"
#include "RageUtil_CircularBuffer.h" #include "RageUtil_CircularBuffer.h"
#include "RageSound.h"
#include <cstdint> #include <cstdint>
class RageSoundBase; class RageSoundBase;
class RageTimer; class RageTimer;
class RageSoundMixBuffer; class RageSoundMixBuffer;
static const int samples_per_block = 512; static const int samples_per_block = 512; // This should probably be kSamplesPerBlock or similar if it's truly const and static
class RageSoundDriver: public RageDriver class RageSoundDriver: public RageDriver
{ {
@@ -66,7 +67,7 @@ public:
* hearing it. (This isn't necessarily the same as the buffer latency.) */ * hearing it. (This isn't necessarily the same as the buffer latency.) */
virtual float GetPlayLatency() const { return 0.0f; } virtual float GetPlayLatency() const { return 0.0f; }
virtual int GetSampleRate() const { return 44100; } virtual int GetSampleRate() const { return kFallbackSampleRate; }
protected: protected:
/* Start the decoding. This should be called once the hardware is set up and /* Start the decoding. This should be called once the hardware is set up and
@@ -97,7 +97,7 @@ RString RageSoundDriver_DSound_Software::Init()
m_pPCM = new DSoundBuf; m_pPCM = new DSoundBuf;
m_iSampleRate = PREFSMAN->m_iSoundPreferredSampleRate; m_iSampleRate = PREFSMAN->m_iSoundPreferredSampleRate;
if( m_iSampleRate == 0 ) if( m_iSampleRate == 0 )
m_iSampleRate = 44100; m_iSampleRate = kFallbackSampleRate;
// This m_iSampleRate (driver's) is then passed as the iSampleRate parameter to DSoundBuf::Init() // This m_iSampleRate (driver's) is then passed as the iSampleRate parameter to DSoundBuf::Init()
sError = m_pPCM->Init( ds, DSoundBuf::HW_DONT_CARE, channels, m_iSampleRate, 16, g_iMaxWriteahead ); sError = m_pPCM->Init( ds, DSoundBuf::HW_DONT_CARE, channels, m_iSampleRate, 16, g_iMaxWriteahead );
if( sError != "" ) if( sError != "" )
+1 -1
View File
@@ -32,7 +32,7 @@ RageSoundDriver_Null::RageSoundDriver_Null()
{ {
m_iSampleRate = PREFSMAN->m_iSoundPreferredSampleRate; m_iSampleRate = PREFSMAN->m_iSoundPreferredSampleRate;
if( m_iSampleRate == 0 ) if( m_iSampleRate == 0 )
m_iSampleRate = 44100; m_iSampleRate = kFallbackSampleRate;
m_iLastCursorPos = GetPosition(); m_iLastCursorPos = GetPosition();
StartDecodeThread(); StartDecodeThread();
} }
+1 -1
View File
@@ -196,7 +196,7 @@ RString RageSoundDriver_OSS::Init()
// Determine the target sample rate based on preference // Determine the target sample rate based on preference
int targetSampleRate = PREFSMAN->m_iSoundPreferredSampleRate; int targetSampleRate = PREFSMAN->m_iSoundPreferredSampleRate;
if (targetSampleRate == 0) { if (targetSampleRate == 0) {
targetSampleRate = 44100; // Default to 44100 if preference is 0 targetSampleRate = kFallbackSampleRate;
} }
samplerate = targetSampleRate; // Attempt to set this rate samplerate = targetSampleRate; // Attempt to set this rate
@@ -29,7 +29,7 @@ m_PulseMainLoop(nullptr), m_PulseCtx(nullptr), m_PulseStream(nullptr)
{ {
m_ss.rate = PREFSMAN->m_iSoundPreferredSampleRate; m_ss.rate = PREFSMAN->m_iSoundPreferredSampleRate;
if( m_ss.rate == 0 ) if( m_ss.rate == 0 )
m_ss.rate = 44100; m_ss.rate = kFallbackSampleRate;
} }
RageSoundDriver_PulseAudio::~RageSoundDriver_PulseAudio() RageSoundDriver_PulseAudio::~RageSoundDriver_PulseAudio()
+1 -1
View File
@@ -125,7 +125,7 @@ RString RageSoundDriver_WaveOut::Init()
b_InitSuccess = false; b_InitSuccess = false;
m_iSampleRate = PREFSMAN->m_iSoundPreferredSampleRate; m_iSampleRate = PREFSMAN->m_iSoundPreferredSampleRate;
if( m_iSampleRate == 0 ) if( m_iSampleRate == 0 )
m_iSampleRate = 44100; m_iSampleRate = kFallbackSampleRate;
WAVEFORMATEX fmt; WAVEFORMATEX fmt;
fmt.wFormatTag = WAVE_FORMAT_PCM; fmt.wFormatTag = WAVE_FORMAT_PCM;