some fixes for selecting different ASIO soundcards

This commit is contained in:
Volker Fischer 2008-10-31 20:27:55 +00:00
parent 1c2535db16
commit 8d77d11ff9
7 changed files with 275 additions and 236 deletions

View file

@ -13,13 +13,10 @@
#ifdef WITH_SOUND
/* Wave in ********************************************************************/
void CSound::InitRecording(int iNewBufferSize, bool bNewBlocking)
void CSound::InitRecording ( const bool bNewBlocking )
{
int err;
/* set internal buffer size for read */
iBufferSizeIn = iNewBufferSize / NUM_IN_OUT_CHANNELS; /* mono size */
/* if recording device was already open, close it first */
if ( rhandle != NULL )
{
@ -204,13 +201,10 @@ void CSound::SetInNumBuf ( int iNewNum )
/* Wave out *******************************************************************/
void CSound::InitPlayback ( int iNewBufferSize, bool bNewBlocking )
void CSound::InitPlayback ( const bool bNewBlocking )
{
int err;
// save buffer size
iBufferSizeOut = iNewBufferSize / NUM_IN_OUT_CHANNELS; // mono size
// if playback device was already open, close it first
if ( phandle != NULL )
{
@ -245,12 +239,12 @@ bool CSound::Write ( CVector<short>& psData )
int start = 0;
int ret;
/* Check if device must be opened or reinitialized */
// check if device must be opened or reinitialized
if ( bChangParamOut == true )
{
InitPlayback ( iBufferSizeOut * NUM_IN_OUT_CHANNELS );
/* Reset flag */
// reset flag
bChangParamOut = false;
}
@ -262,7 +256,7 @@ bool CSound::Write ( CVector<short>& psData )
{
if ( ret == -EPIPE )
{
/* under-run */
// under-run
qDebug ( "wunderrun" );
ret = snd_pcm_prepare ( phandle );
@ -286,7 +280,7 @@ bool CSound::Write ( CVector<short>& psData )
{
qDebug("wstrpipe");
/* wait until the suspend flag is released */
// wait until the suspend flag is released
while ( (ret = snd_pcm_resume ( phandle ) ) == -EAGAIN )
{
sleep(1);
@ -319,13 +313,13 @@ bool CSound::Write ( CVector<short>& psData )
void CSound::SetOutNumBuf ( int iNewNum )
{
/* check new parameter */
// check new parameter
if ( ( iNewNum >= MAX_SND_BUF_OUT ) || ( iNewNum < 1 ) )
{
iNewNum = NUM_PERIOD_BLOCKS_OUT;
}
/* Change only if parameter is different */
// change only if parameter is different
if ( iNewNum != iCurPeriodSizeOut )
{
iCurPeriodSizeOut = iNewNum;
@ -417,7 +411,7 @@ bool CSound::SetHWParams(snd_pcm_t* handle, const int iBufferSizeIn,
}
/* check period and buffer size */
// check period and buffer size
snd_pcm_uframes_t buffer_size;
if ( err = snd_pcm_hw_params_get_buffer_size(hwparams, &buffer_size ) < 0 ) {
qDebug ( "Unable to get buffer size for playback: %s\n", snd_strerror ( err ) );
@ -433,7 +427,7 @@ if (err < 0)
qDebug ( "frame size: %d (desired: %d)", period_size, iBufferSizeIn );
/* clean-up */
// clean-up
snd_pcm_hw_params_free ( hwparams );
return false;
@ -441,7 +435,7 @@ qDebug("frame size: %d (desired: %d)", period_size, iBufferSizeIn);
void CSound::Close()
{
/* read */
// read
if ( rhandle != NULL )
{
snd_pcm_close ( rhandle );
@ -449,7 +443,7 @@ void CSound::Close ()
rhandle = NULL;
/* playback */
// playback
if ( phandle != NULL )
{
snd_pcm_close ( phandle );

View file

@ -31,9 +31,9 @@
/* Definitions ****************************************************************/
#define NUM_IN_OUT_CHANNELS 2 /* always stereo */
#define NUM_IN_OUT_CHANNELS 2 // always stereo
/* the number of periods is critical for latency */
// the number of periods is critical for latency
#define NUM_PERIOD_BLOCKS_IN 2
#define NUM_PERIOD_BLOCKS_OUT 2
@ -44,13 +44,17 @@
class CSound
{
public:
CSound()
CSound ( const int iNewBufferSizeStereo )
#if WITH_SOUND
: rhandle ( NULL ), phandle ( NULL ), iCurPeriodSizeIn ( NUM_PERIOD_BLOCKS_IN ),
iCurPeriodSizeOut ( NUM_PERIOD_BLOCKS_OUT ), bChangParamIn ( true ),
bChangParamOut ( true )
#endif
{}
{
// set internal buffer size for read and write
iBufferSizeIn = iNewBufferSizeStereo / NUM_IN_OUT_CHANNELS; // mono size
iBufferSizeOut = iNewBufferSizeStereo / NUM_IN_OUT_CHANNELS; // mono size
}
virtual ~CSound() { Close(); }
// not implemented yet, always return one device and default string
@ -64,8 +68,8 @@ public:
int GetInNumBuf() { return iCurPeriodSizeIn; }
void SetOutNumBuf ( int iNewNum );
int GetOutNumBuf() { return iCurPeriodSizeOut; }
void InitRecording(int iNewBufferSize, bool bNewBlocking = true);
void InitPlayback(int iNewBufferSize, bool bNewBlocking = false);
void InitRecording ( const bool bNewBlocking = true );
void InitPlayback ( const bool bNewBlocking = false );
bool Read ( CVector<short>& psData );
bool Write ( CVector<short>& psData );
@ -85,13 +89,13 @@ protected:
bool bChangParamOut;
int iCurPeriodSizeOut;
#else
/* Dummy definitions */
// dummy definitions
void SetInNumBuf ( int iNewNum ) {}
int GetInNumBuf() { return 1; }
void SetOutNumBuf ( int iNewNum ) {}
int GetOutNumBuf() { return 1; }
void InitRecording(int iNewBufferSize, bool bNewBlocking = true) {printf("no sound!");}
void InitPlayback(int iNewBufferSize, bool bNewBlocking = false) {printf("no sound!");}
void InitRecording ( const bool bNewBlocking = true ) { printf ( "no sound!" ); }
void InitPlayback ( const bool bNewBlocking = false ) { printf ( "no sound!" ); }
bool Read ( CVector<short>& psData ) { printf ( "no sound!" ); return false; }
bool Write ( CVector<short>& psData ) { printf ( "no sound!" ); return false; }
void Close() {}

View file

@ -27,6 +27,8 @@
/* Implementation *************************************************************/
CClient::CClient ( const quint16 iPortNumber ) : bRun ( false ),
iSndCrdBlockSizeSam ( MIN_SND_CRD_BLOCK_SIZE_SAMPLES ),
Sound ( MIN_SND_CRD_BLOCK_SIZE_SAMPLES * 2 /* stereo */ ),
Socket ( &Channel, iPortNumber ),
iAudioInFader ( AUD_FADER_IN_MAX / 2 ),
iReverbLevel ( AUD_REVERB_MAX / 6 ),
@ -168,9 +170,8 @@ void CClient::OnProtocolStatus ( bool bOk )
void CClient::Init()
{
// set block sizes (in samples)
// set block size (in samples)
iBlockSizeSam = MIN_BLOCK_SIZE_SAMPLES;
iSndCrdBlockSizeSam = MIN_SND_CRD_BLOCK_SIZE_SAMPLES;
vecsAudioSndCrd.Init ( iSndCrdBlockSizeSam * 2 ); // stereo
vecdAudioSndCrdL.Init ( iSndCrdBlockSizeSam );
@ -179,8 +180,8 @@ void CClient::Init()
vecdAudioL.Init ( iBlockSizeSam );
vecdAudioR.Init ( iBlockSizeSam );
Sound.InitRecording ( iSndCrdBlockSizeSam * 2 ); // stereo
Sound.InitPlayback ( iSndCrdBlockSizeSam * 2 ); // stereo
Sound.InitRecording();
Sound.InitPlayback();
// resample objects are always initialized with the input block size
// record

View file

@ -250,7 +250,8 @@ void CClientSettingsDlg::OnSoundCrdSelection ( int iSndDevIdx )
{
QMessageBox::critical ( 0, APP_NAME,
QString ( "The selected audio device could not be used because "
"of the following error: " ) + generr.GetErrorText(), "Ok", 0 );
"of the following error: " ) + generr.GetErrorText() +
QString ( " The previous driver will be selected." ), "Ok", 0 );
// recover old selection
cbSoundcard->setCurrentIndex ( pClient->GetSndInterface()->GetDev() );

View file

@ -138,6 +138,11 @@ int main ( int argc, char** argv )
exit ( 1 );
}
#ifdef _WIN32
// Set application priority class -> high priority
SetPriorityClass ( GetCurrentProcess(), HIGH_PRIORITY_CLASS );
#endif
// Application object
QApplication app ( argc, argv, bUseGUI );

View file

@ -87,8 +87,8 @@ bool CSound::Read ( CVector<short>& psData )
// check if device must be opened or reinitialized
if ( bChangParamIn )
{
// reinit sound interface (init recording requires stereo buffer size)
InitRecordingAndPlayback ( iBufferSizeStereo );
// reinit sound interface
InitRecordingAndPlayback();
// reset flag
bChangParamIn = false;
@ -188,8 +188,8 @@ bool CSound::Write ( CVector<short>& psData )
// check if device must be opened or reinitialized
if ( bChangParamOut )
{
// reinit sound interface (init recording requires stereo buffer size)
InitRecordingAndPlayback ( iBufferSizeStereo );
// reinit sound interface
InitRecordingAndPlayback();
// reset flag
bChangParamOut = false;
@ -256,6 +256,9 @@ void CSound::SetDev ( const int iNewDev )
{
// a device was already been initialized and is used, kill working
// thread and clean up
// stop driver
ASIOStop();
// set event to ensure that thread leaves the waiting function
if ( m_ASIOEvent != NULL )
{
@ -265,8 +268,7 @@ void CSound::SetDev ( const int iNewDev )
// wait for the thread to terminate
Sleep ( 500 );
// stop audio and dispose ASIO buffers
ASIOStop();
// dispose ASIO buffers
ASIODisposeBuffers();
// remove old driver
@ -280,9 +282,11 @@ void CSound::SetDev ( const int iNewDev )
// loading and initializing the new driver failed, go back to original
// driver and display error message
LoadAndInitializeDriver ( lCurDev );
InitRecordingAndPlayback();
throw CGenErr ( strErrorMessage.c_str() );
}
InitRecordingAndPlayback();
}
else
{
@ -305,8 +309,15 @@ void CSound::SetDev ( const int iNewDev )
}
}
std::string CSound::LoadAndInitializeDriver ( const int iDriverIdx )
std::string CSound::LoadAndInitializeDriver ( int iDriverIdx )
{
// first check and correct input parameter
if ( iDriverIdx >= lNumDevs )
{
// we assume here that at least one driver is in the system
iDriverIdx = 0;
}
// load driver
loadAsioDriver ( cDriverNames[iDriverIdx] );
if ( ASIOInit ( &driverInfo ) != ASE_OK )
@ -316,6 +327,62 @@ std::string CSound::LoadAndInitializeDriver ( const int iDriverIdx )
return "The audio driver could not be initialized.";
}
const std::string strStat = GetAndCheckSoundCardProperties();
// store ID of selected driver if initialization was successful
if ( strStat.empty() )
{
lCurDev = iDriverIdx;
}
return strStat;
}
bool CSound::LoadAndInitializeFirstValidDriver()
{
// load and initialize first valid ASIO driver
bool bValidDriverDetected = false;
int iCurDriverIdx = 0;
// try all available drivers in the system ("lNumDevs" devices)
while ( !bValidDriverDetected && iCurDriverIdx < lNumDevs )
{
if ( loadAsioDriver ( cDriverNames[iCurDriverIdx] ) )
{
if ( ASIOInit ( &driverInfo ) == ASE_OK )
{
if ( GetAndCheckSoundCardProperties().empty() )
{
// initialization was successful
bValidDriverDetected = true;
// store ID of selected driver
lCurDev = iCurDriverIdx;
}
else
{
// driver could not be loaded, free memory
asioDrivers->removeCurrentDriver();
}
}
else
{
// driver could not be loaded, free memory
asioDrivers->removeCurrentDriver();
}
}
// try next driver
iCurDriverIdx++;
}
return bValidDriverDetected;
}
std::string CSound::GetAndCheckSoundCardProperties()
{
int i;
// check the number of available channels
long lNumInChan;
long lNumOutChan;
@ -350,63 +417,6 @@ std::string CSound::LoadAndInitializeDriver ( const int iDriverIdx )
&HWBufferInfo.lPreferredSize,
&HWBufferInfo.lGranularity );
// check wether the driver requires the ASIOOutputReady() optimization
// (can be used by the driver to reduce output latency by one block)
bASIOPostOutput = ( ASIOOutputReady() == ASE_OK );
// store ID of selected driver
lCurDev = iDriverIdx;
return "";
}
bool CSound::LoadAndInitializeFirstValidDriver()
{
// load and initialize first valid ASIO driver
bool bValidDriverDetected = false;
int iCurDriverIdx = 0;
// try all available drivers in the system ("lNumDevs" devices)
while ( !bValidDriverDetected && iCurDriverIdx < lNumDevs )
{
if ( loadAsioDriver ( cDriverNames[iCurDriverIdx] ) )
{
if ( ASIOInit ( &driverInfo ) == ASE_OK )
{
// initialization was successful
bValidDriverDetected = true;
// store ID of selected driver
lCurDev = iCurDriverIdx;
}
else
{
// driver could not be loaded, free memory
asioDrivers->removeCurrentDriver();
}
}
// try next driver
iCurDriverIdx++;
}
return bValidDriverDetected;
}
void CSound::InitRecordingAndPlayback ( int iNewBufferSize )
{
int i;
// first, stop audio and dispose ASIO buffers
ASIOStop();
ASIODisposeBuffers();
ASIOMutex.lock(); // get mutex lock
{
// set internal buffer size value and calculate mono buffer size
iBufferSizeStereo = iNewBufferSize;
iBufferSizeMono = iBufferSizeStereo / 2;
// calculate "nearest" buffer size and set internal parameter accordingly
// first check minimum and maximum values
if ( iBufferSizeMono < HWBufferInfo.lMinSize )
@ -499,16 +509,33 @@ void CSound::InitRecordingAndPlayback ( int iNewBufferSize )
channelInfos[i].isInput = bufferInfos[i].isInput;
ASIOGetChannelInfo ( &channelInfos[i] );
// only 16 bit is supported
// only 16/24/32 LSB is supported
if ( ( channelInfos[i].type != ASIOSTInt16LSB ) &&
( channelInfos[i].type != ASIOSTInt24LSB ) &&
( channelInfos[i].type != ASIOSTInt32LSB ) )
{
throw CGenErr ( "Required audio sample format not available (16/24/32 bit LSB)." );
// clean up and return error string
ASIODisposeBuffers();
ASIOExit();
asioDrivers->removeCurrentDriver();
return "Required audio sample format not available (16/24/32 bit LSB).";
}
}
// check wether the driver requires the ASIOOutputReady() optimization
// (can be used by the driver to reduce output latency by one block)
bASIOPostOutput = ( ASIOOutputReady() == ASE_OK );
return "";
}
void CSound::InitRecordingAndPlayback()
{
// first, stop audio and dispose ASIO buffers
ASIOStop();
ASIOMutex.lock(); // get mutex lock
{
// Our buffer management -----------------------------------------------
// store new buffer number values
iCurNumSndBufIn = iNewNumSndBufIn;
@ -537,7 +564,7 @@ void CSound::InitRecordingAndPlayback ( int iNewBufferSize )
psPlayBuffer = new short[iCurNumSndBufOut * iBufferSizeStereo];
// clear new buffer
for ( i = 0; i < iCurNumSndBufOut * iBufferSizeStereo; i++ )
for ( int i = 0; i < iCurNumSndBufOut * iBufferSizeStereo; i++ )
{
psPlayBuffer[i] = 0;
}
@ -553,6 +580,9 @@ void CSound::InitRecordingAndPlayback ( int iNewBufferSize )
void CSound::Close()
{
// stop driver
ASIOStop();
// set event to ensure that thread leaves the waiting function
if ( m_ASIOEvent != NULL )
{
@ -562,8 +592,7 @@ void CSound::Close()
// wait for the thread to terminate
Sleep ( 500 );
// stop audio and dispose ASIO buffers
ASIOStop();
// dispose ASIO buffers
ASIODisposeBuffers();
// set flag to open devices the next time it is initialized
@ -571,8 +600,12 @@ void CSound::Close()
bChangParamOut = true;
}
CSound::CSound()
CSound::CSound ( const int iNewBufferSizeStereo )
{
// set internal buffer size value and calculate mono buffer size
iBufferSizeStereo = iNewBufferSizeStereo;
iBufferSizeMono = iBufferSizeStereo / 2;
// init number of sound buffers
iNewNumSndBufIn = NUM_SOUND_BUFFERS_IN;
iCurNumSndBufIn = NUM_SOUND_BUFFERS_IN;

View file

@ -65,18 +65,18 @@
class CSound
{
public:
CSound();
CSound ( const int iNewBufferSizeStereo );
virtual ~CSound();
void InitRecording ( const int iNewBufferSize, const bool bNewBlocking = true )
void InitRecording ( const bool bNewBlocking = true )
{
bBlockingRec = bNewBlocking;
InitRecordingAndPlayback ( iNewBufferSize );
InitRecordingAndPlayback();
}
void InitPlayback ( const int iNewBufferSize, const bool bNewBlocking = false )
void InitPlayback ( const bool bNewBlocking = false )
{
bBlockingPlay = bNewBlocking;
InitRecordingAndPlayback ( iNewBufferSize );
InitRecordingAndPlayback();
}
bool Read ( CVector<short>& psData );
bool Write ( CVector<short>& psData );
@ -96,8 +96,9 @@ public:
protected:
bool LoadAndInitializeFirstValidDriver();
std::string LoadAndInitializeDriver ( const int iIdx );
void InitRecordingAndPlayback ( const int iNewBufferSize );
std::string LoadAndInitializeDriver ( int iIdx );
std::string GetAndCheckSoundCardProperties();
void InitRecordingAndPlayback();
// audio hardware buffer info
struct sHWBufferInfo