set the soundcard buffer size in the init function

This commit is contained in:
Volker Fischer 2009-02-24 22:56:19 +00:00
parent 2cd15abb8a
commit 8843e94822
8 changed files with 69 additions and 47 deletions

View file

@ -46,20 +46,14 @@
class CSound : public CSoundBase class CSound : public CSoundBase
{ {
public: public:
CSound ( const int iNewStereoBufferSize, CSound ( void (*fpNewCallback) ( CVector<short>& psData, void* arg ), void* arg ) :
void (*fpNewCallback) ( CVector<short>& psData, void* arg ), void* arg ) :
#if WITH_SOUND #if WITH_SOUND
CSoundBase ( iNewStereoBufferSize, fpNewCallback, arg ), rhandle ( NULL ), CSoundBase ( fpNewCallback, arg ), rhandle ( NULL ),
phandle ( NULL ), iCurPeriodSizeIn ( NUM_PERIOD_BLOCKS_IN ), phandle ( NULL ), iCurPeriodSizeIn ( NUM_PERIOD_BLOCKS_IN ),
iCurPeriodSizeOut ( NUM_PERIOD_BLOCKS_OUT ), bChangParamIn ( true ), iCurPeriodSizeOut ( NUM_PERIOD_BLOCKS_OUT ), bChangParamIn ( true ),
bChangParamOut ( true ) bChangParamOut ( true ) {}
{
// set internal buffer size for read and write
iBufferSizeIn = iNewStereoBufferSize / NUM_IN_OUT_CHANNELS; // mono size
iBufferSizeOut = iNewStereoBufferSize / NUM_IN_OUT_CHANNELS; // mono size
}
#else #else
CSoundBase ( iNewStereoBufferSize, fpNewCallback, arg ) {} CSoundBase ( fpNewCallback, arg ) {}
#endif #endif
virtual ~CSound() { Close(); } virtual ~CSound() { Close(); }
@ -75,7 +69,18 @@ public:
void SetOutNumBuf ( int iNewNum ); void SetOutNumBuf ( int iNewNum );
int GetOutNumBuf() { return iCurPeriodSizeOut; } int GetOutNumBuf() { return iCurPeriodSizeOut; }
virtual void Init() { InitRecording(); InitPlayback(); } virtual void Init ( const int iNewStereoBufferSize )
{
// init base class
CSoundBase::Init ( iNewStereoBufferSize );
// set internal buffer size for read and write
iBufferSizeIn = iNewStereoBufferSize / NUM_IN_OUT_CHANNELS; // mono size
iBufferSizeOut = iNewStereoBufferSize / NUM_IN_OUT_CHANNELS; // mono size
InitRecording();
InitPlayback();
}
virtual bool Read ( CVector<short>& psData ); virtual bool Read ( CVector<short>& psData );
virtual bool Write ( CVector<short>& psData ); virtual bool Write ( CVector<short>& psData );
virtual void Close(); virtual void Close();
@ -102,7 +107,7 @@ protected:
int GetInNumBuf() { return 1; } int GetInNumBuf() { return 1; }
void SetOutNumBuf ( int iNewNum ) {} void SetOutNumBuf ( int iNewNum ) {}
int GetOutNumBuf() { return 1; } int GetOutNumBuf() { return 1; }
virtual void Init() { printf ( "no sound!" ); } virtual void Init ( const int iNewStereoBufferSize ) { CSoundBase::Init ( iNewStereoBufferSize ); }
virtual bool Read ( CVector<short>& psData ) { printf ( "no sound!" ); return false; } virtual bool Read ( CVector<short>& psData ) { printf ( "no sound!" ); return false; }
virtual bool Write ( CVector<short>& psData ) { printf ( "no sound!" ); return false; } virtual bool Write ( CVector<short>& psData ) { printf ( "no sound!" ); return false; }
virtual void Close() {} virtual void Close() {}

View file

@ -541,15 +541,29 @@ CChannel::CChannel() : sName ( "" ),
// query all possible network in buffer sizes for determining if an // query all possible network in buffer sizes for determining if an
// audio packet was received (the following code only works if all // audio packet was received (the following code only works if all
// possible network buffer sizes are different!) // possible network buffer sizes are different!)
// we add a special entry for network modes which are managed via the
// protocol -> "+ 1"
const int iNumSupportedAudComprTypes = 3; const int iNumSupportedAudComprTypes = 3;
vecNetwBufferInProps.Init ( iNumSupportedAudComprTypes * vecNetwBufferInProps.Init ( iNumSupportedAudComprTypes *
MAX_NET_BLOCK_SIZE_FACTOR ); MAX_NET_BLOCK_SIZE_FACTOR + 1 );
// init special mode
// TEST -> 64
vecNetwBufferInProps[0].iBlockSizeFactor = 1;
vecNetwBufferInProps[0].eAudComprType = CAudioCompression::CT_NONE;
vecNetwBufferInProps[0].iNetwInBufSize = AudioCompressionIn.Init (
vecNetwBufferInProps[0].iBlockSizeFactor * 64,
vecNetwBufferInProps[0].eAudComprType );
for ( int i = 0; i < MAX_NET_BLOCK_SIZE_FACTOR; i++ ) for ( int i = 0; i < MAX_NET_BLOCK_SIZE_FACTOR; i++ )
{ {
const int iNoneIdx = iNumSupportedAudComprTypes * i; // (consider the special mode -> "1 +")
const int iIMAIdx = iNumSupportedAudComprTypes * i + 1; const int iNoneIdx = 1 + iNumSupportedAudComprTypes * i;
const int iMSIdx = iNumSupportedAudComprTypes * i + 2; const int iIMAIdx = 1 + iNumSupportedAudComprTypes * i + 1;
const int iMSIdx = 1 + iNumSupportedAudComprTypes * i + 2;
// network block size factor must start from 1 -> i + 1 // network block size factor must start from 1 -> i + 1
const int iCurNetBlockSizeFact = i + 1; const int iCurNetBlockSizeFact = i + 1;

View file

@ -27,9 +27,7 @@
/* Implementation *************************************************************/ /* Implementation *************************************************************/
CClient::CClient ( const quint16 iPortNumber ) : CClient::CClient ( const quint16 iPortNumber ) :
iSndCrdMonoBlockSizeSam ( MIN_SND_CRD_BLOCK_SIZE_SAMPLES ), Sound ( AudioCallback, this ),
iSndCrdStereoBlockSizeSam ( 2 * MIN_SND_CRD_BLOCK_SIZE_SAMPLES ),
Sound ( MIN_SND_CRD_BLOCK_SIZE_SAMPLES * 2 /* stereo */, AudioCallback, this ),
Socket ( &Channel, iPortNumber ), Socket ( &Channel, iPortNumber ),
iAudioInFader ( AUD_FADER_IN_MIDDLE ), iAudioInFader ( AUD_FADER_IN_MIDDLE ),
iReverbLevel ( 0 ), iReverbLevel ( 0 ),
@ -192,7 +190,10 @@ void CClient::Init()
{ {
// set block size (in samples) // set block size (in samples)
iMonoBlockSizeSam = MIN_BLOCK_SIZE_SAMPLES; iMonoBlockSizeSam = MIN_BLOCK_SIZE_SAMPLES;
iStereoBlockSizeSam = 2 * MIN_BLOCK_SIZE_SAMPLES; iStereoBlockSizeSam = 2 * iMonoBlockSizeSam;
iSndCrdMonoBlockSizeSam = MIN_BLOCK_DURATION_MS * SND_CRD_SAMPLE_RATE / 1000;
iSndCrdStereoBlockSizeSam = 2 * iSndCrdMonoBlockSizeSam;
vecsAudioSndCrdStereo.Init ( iSndCrdStereoBlockSizeSam ); vecsAudioSndCrdStereo.Init ( iSndCrdStereoBlockSizeSam );
vecdAudioSndCrdMono.Init ( iSndCrdMonoBlockSizeSam ); vecdAudioSndCrdMono.Init ( iSndCrdMonoBlockSizeSam );
@ -200,7 +201,7 @@ void CClient::Init()
vecdAudioStereo.Init ( iStereoBlockSizeSam ); vecdAudioStereo.Init ( iStereoBlockSizeSam );
Sound.Init(); Sound.Init ( iSndCrdStereoBlockSizeSam );
// resample objects are always initialized with the input block size // resample objects are always initialized with the input block size
// record // record

View file

@ -67,7 +67,6 @@
#define MIN_BLOCK_DURATION_MS 2 // ms #define MIN_BLOCK_DURATION_MS 2 // ms
#define MIN_BLOCK_SIZE_SAMPLES ( MIN_BLOCK_DURATION_MS * SYSTEM_SAMPLE_RATE / 1000 ) #define MIN_BLOCK_SIZE_SAMPLES ( MIN_BLOCK_DURATION_MS * SYSTEM_SAMPLE_RATE / 1000 )
#define MIN_SND_CRD_BLOCK_SIZE_SAMPLES ( MIN_BLOCK_DURATION_MS * SND_CRD_SAMPLE_RATE / 1000 )
// maximum value of factor for network block size // maximum value of factor for network block size
#define MAX_NET_BLOCK_SIZE_FACTOR 3 #define MAX_NET_BLOCK_SIZE_FACTOR 3

View file

@ -26,6 +26,12 @@
/* Implementation *************************************************************/ /* Implementation *************************************************************/
void CSoundBase::Init ( const int iNewStereoBufferSize )
{
// init audio sound card buffer
vecsAudioSndCrdStereo.Init ( iNewStereoBufferSize );
}
void CSoundBase::Start() void CSoundBase::Start()
{ {
@ -71,9 +77,6 @@ void CSoundBase::run()
bRun = true; bRun = true;
while ( bRun ) while ( bRun )
{ {
// TEST
CVector<short> vecsAudioSndCrdStereo ( iStereoBufferSize );
// get audio from sound card (blocking function) // get audio from sound card (blocking function)
if ( Read ( vecsAudioSndCrdStereo ) ) if ( Read ( vecsAudioSndCrdStereo ) )
{ {

View file

@ -34,13 +34,12 @@
class CSoundBase : public QThread class CSoundBase : public QThread
{ {
public: public:
CSoundBase ( const int iNewStereoBufferSize, CSoundBase ( void (*fpNewCallback) ( CVector<short>& psData, void* arg ),
void (*fpNewCallback) ( CVector<short>& psData, void* arg ), void* arg ) : fpCallback ( fpNewCallback ), pCallbackArg ( arg ),
void* arg ) : iStereoBufferSize ( iNewStereoBufferSize ), bRun ( false ) {}
fpCallback ( fpNewCallback ), pCallbackArg ( arg ), bRun ( false ) {}
virtual ~CSoundBase() {} virtual ~CSoundBase() {}
virtual void Init() = 0; virtual void Init ( const int iNewStereoBufferSize );
virtual void Start(); virtual void Start();
virtual void Stop(); virtual void Stop();
bool IsRunning() const { return bRun; } bool IsRunning() const { return bRun; }
@ -59,7 +58,7 @@ protected:
void run(); void run();
bool bRun; bool bRun;
int iStereoBufferSize; CVector<short> vecsAudioSndCrdStereo;
}; };
#endif /* !defined ( SOUNDBASE_HOIHGEH8_3_4344456456345634565KJIUHF1912__INCLUDED_ ) */ #endif /* !defined ( SOUNDBASE_HOIHGEH8_3_4344456456345634565KJIUHF1912__INCLUDED_ ) */

View file

@ -89,7 +89,7 @@ bool CSound::Read ( CVector<short>& psData )
if ( bChangParamIn ) if ( bChangParamIn )
{ {
// reinit sound interface // reinit sound interface
Init(); Init ( iBufferSizeStereo );
// reset flag // reset flag
bChangParamIn = false; bChangParamIn = false;
@ -181,7 +181,7 @@ bool CSound::Write ( CVector<short>& psData )
if ( bChangParamOut ) if ( bChangParamOut )
{ {
// reinit sound interface // reinit sound interface
Init(); Init ( iBufferSizeStereo );
// reset flag // reset flag
bChangParamOut = false; bChangParamOut = false;
@ -284,11 +284,11 @@ void CSound::SetDev ( const int iNewDev )
// loading and initializing the new driver failed, go back to original // loading and initializing the new driver failed, go back to original
// driver and display error message // driver and display error message
LoadAndInitializeDriver ( lCurDev ); LoadAndInitializeDriver ( lCurDev );
Init(); Init ( iBufferSizeStereo );
throw CGenErr ( strErrorMessage.c_str() ); throw CGenErr ( strErrorMessage.c_str() );
} }
Init(); Init ( iBufferSizeStereo );
} }
else else
{ {
@ -607,13 +607,20 @@ if ( iASIOBufferSizeMono == 256 )
return ""; return "";
} }
void CSound::Init() void CSound::Init ( const int iNewStereoBufferSize )
{ {
// first, stop audio and dispose ASIO buffers // first, stop audio and dispose ASIO buffers
ASIOStop(); ASIOStop();
ASIOMutex.lock(); // get mutex lock ASIOMutex.lock(); // get mutex lock
{ {
// init base clasee
CSoundBase::Init ( iNewStereoBufferSize );
// set internal buffer size value and calculate mono buffer size
iBufferSizeStereo = iNewStereoBufferSize;
iBufferSizeMono = iBufferSizeStereo / 2;
// store new buffer number values // store new buffer number values
iCurNumSndBufIn = iNewNumSndBufIn; iCurNumSndBufIn = iNewNumSndBufIn;
iCurNumSndBufOut = iNewNumSndBufOut; iCurNumSndBufOut = iNewNumSndBufOut;
@ -674,14 +681,9 @@ void CSound::Close()
bChangParamOut = true; bChangParamOut = true;
} }
CSound::CSound ( const int iNewBufferSizeStereo, CSound::CSound ( void (*fpNewCallback) ( CVector<short>& psData, void* arg ), void* arg ) :
void (*fpNewCallback) ( CVector<short>& psData, void* arg ), void* arg ) : CSoundBase ( fpNewCallback, arg )
CSoundBase ( iNewBufferSizeStereo, fpNewCallback, arg )
{ {
// set internal buffer size value and calculate mono buffer size
iBufferSizeStereo = iNewBufferSizeStereo;
iBufferSizeMono = iBufferSizeStereo / 2;
// init number of sound buffers // init number of sound buffers
iNewNumSndBufIn = NUM_SOUND_BUFFERS_IN; iNewNumSndBufIn = NUM_SOUND_BUFFERS_IN;
iCurNumSndBufIn = NUM_SOUND_BUFFERS_IN; iCurNumSndBufIn = NUM_SOUND_BUFFERS_IN;

View file

@ -55,11 +55,10 @@
class CSound : public CSoundBase class CSound : public CSoundBase
{ {
public: public:
CSound ( const int iNewBufferSizeStereo, CSound ( void (*fpNewCallback) ( CVector<short>& psData, void* arg ), void* arg );
void (*fpNewCallback) ( CVector<short>& psData, void* arg ), void* arg );
virtual ~CSound(); virtual ~CSound();
virtual void Init(); virtual void Init ( const int iNewStereoBufferSize );
virtual bool Read ( CVector<short>& psData ); virtual bool Read ( CVector<short>& psData );
virtual bool Write ( CVector<short>& psData ); virtual bool Write ( CVector<short>& psData );
virtual void Close(); virtual void Close();