some code style changes, added first implementation of mixer in client window

This commit is contained in:
Volker Fischer 2006-12-07 18:57:26 +00:00
parent 63adf15507
commit 7f4bea94eb
5 changed files with 531 additions and 490 deletions

View File

@ -2,7 +2,7 @@
* Copyright (c) 2004-2005
*
* Author(s):
* Volker Fischer, Alexander Kurpiers
* Volker Fischer, Alexander Kurpiers
*
* This code is based on the Open-Source sound interface implementation of
* the Dream DRM Receiver project.
@ -15,412 +15,412 @@
/* Wave in ********************************************************************/
void CSound::InitRecording(int iNewBufferSize, bool bNewBlocking)
{
int err;
int err;
/* set internal buffer size for read */
iBufferSizeIn = iNewBufferSize / NUM_IN_OUT_CHANNELS; /* mono size */
/* 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 )
{
snd_pcm_close ( rhandle );
}
/* if recording device was already open, close it first */
if ( rhandle != NULL )
{
snd_pcm_close ( rhandle );
}
/* record device: The most important ALSA interfaces to the PCM devices are
the "plughw" and the "hw" interface. If you use the "plughw" interface,
you need not care much about the sound hardware. If your soundcard does
not support the sample rate or sample format you specify, your data will
be automatically converted. This also applies to the access type and the
number of channels. With the "hw" interface, you have to check whether
your hardware supports the configuration you would like to use */
/* either "hw:0,0" or "plughw:0,0" */
if ( err = snd_pcm_open ( &rhandle, "hw:0,0", SND_PCM_STREAM_CAPTURE, 0 ) != 0 )
{
qDebug ( "open error: %s", snd_strerror ( err ) );
}
/* record device: The most important ALSA interfaces to the PCM devices are
the "plughw" and the "hw" interface. If you use the "plughw" interface,
you need not care much about the sound hardware. If your soundcard does
not support the sample rate or sample format you specify, your data will
be automatically converted. This also applies to the access type and the
number of channels. With the "hw" interface, you have to check whether
your hardware supports the configuration you would like to use */
/* either "hw:0,0" or "plughw:0,0" */
if ( err = snd_pcm_open ( &rhandle, "hw:0,0", SND_PCM_STREAM_CAPTURE, 0 ) != 0 )
{
qDebug ( "open error: %s", snd_strerror ( err ) );
}
/* recording should be blocking */
if ( err = snd_pcm_nonblock ( rhandle, FALSE ) != 0 )
{
qDebug ( "cannot set blocking: %s", snd_strerror ( err ) );
}
/* recording should be blocking */
if ( err = snd_pcm_nonblock ( rhandle, FALSE ) != 0 )
{
qDebug ( "cannot set blocking: %s", snd_strerror ( err ) );
}
/* set hardware parameters */
SetHWParams ( rhandle, iBufferSizeIn, iCurPeriodSizeIn );
/* set hardware parameters */
SetHWParams ( rhandle, iBufferSizeIn, iCurPeriodSizeIn );
/* sw parameters --------------------------------------------------------- */
snd_pcm_sw_params_t* swparams;
/* sw parameters --------------------------------------------------------- */
snd_pcm_sw_params_t* swparams;
// allocate an invalid snd_pcm_sw_params_t using standard malloc
if ( err = snd_pcm_sw_params_malloc ( &swparams ) != 0 )
{
qDebug ( "snd_pcm_sw_params_malloc: %s", snd_strerror ( err ) );
}
// allocate an invalid snd_pcm_sw_params_t using standard malloc
if ( err = snd_pcm_sw_params_malloc ( &swparams ) != 0 )
{
qDebug ( "snd_pcm_sw_params_malloc: %s", snd_strerror ( err ) );
}
// get the current swparams
if ( err = snd_pcm_sw_params_current ( rhandle, swparams ) < 0 )
{
qDebug ( "Unable to determine current swparams : %s", snd_strerror ( err ) );
}
// get the current swparams
if ( err = snd_pcm_sw_params_current ( rhandle, swparams ) < 0 )
{
qDebug ( "Unable to determine current swparams : %s", snd_strerror ( err ) );
}
// start the transfer when the buffer immediately -> no threshold, val: 0
err = snd_pcm_sw_params_set_start_threshold ( rhandle, swparams, 0 );
if ( err < 0 )
{
qDebug ( "Unable to set start threshold mode : %s", snd_strerror ( err ) );
}
// start the transfer when the buffer immediately -> no threshold, val: 0
err = snd_pcm_sw_params_set_start_threshold ( rhandle, swparams, 0 );
if ( err < 0 )
{
qDebug ( "Unable to set start threshold mode : %s", snd_strerror ( err ) );
}
// align all transfers to 1 sample
err = snd_pcm_sw_params_set_xfer_align ( rhandle, swparams, 1 );
if ( err < 0 )
{
qDebug ( "Unable to set transfer align : %s", snd_strerror ( err ) );
}
// align all transfers to 1 sample
err = snd_pcm_sw_params_set_xfer_align ( rhandle, swparams, 1 );
if ( err < 0 )
{
qDebug ( "Unable to set transfer align : %s", snd_strerror ( err ) );
}
// set avail min inside a software configuration container
/* Note: This is similar to setting an OSS wakeup point. The valid values
for 'val' are determined by the specific hardware. Most PC sound cards
can only accept power of 2 frame counts (i.e. 512, 1024, 2048). You
cannot use this as a high resolution timer - it is limited to how often
the sound card hardware raises an interrupt. Note that you can greatly
improve the reponses using snd_pcm_sw_params_set_sleep_min where another
timing source is used */
snd_pcm_uframes_t period_size = iBufferSizeIn;
// set avail min inside a software configuration container
/* Note: This is similar to setting an OSS wakeup point. The valid values
for 'val' are determined by the specific hardware. Most PC sound cards
can only accept power of 2 frame counts (i.e. 512, 1024, 2048). You
cannot use this as a high resolution timer - it is limited to how often
the sound card hardware raises an interrupt. Note that you can greatly
improve the reponses using snd_pcm_sw_params_set_sleep_min where another
timing source is used */
snd_pcm_uframes_t period_size = iBufferSizeIn;
err = snd_pcm_sw_params_set_avail_min ( rhandle, swparams, period_size );
if ( err < 0 )
{
qDebug ( "Unable to set avail min : %s", snd_strerror ( err ) );
}
err = snd_pcm_sw_params_set_avail_min ( rhandle, swparams, period_size );
if ( err < 0 )
{
qDebug ( "Unable to set avail min : %s", snd_strerror ( err ) );
}
// write the parameters to the record/playback device
err = snd_pcm_sw_params ( rhandle, swparams );
if ( err < 0 )
{
qDebug ( "Unable to set sw params : %s", snd_strerror ( err ) );
}
// write the parameters to the record/playback device
err = snd_pcm_sw_params ( rhandle, swparams );
if ( err < 0 )
{
qDebug ( "Unable to set sw params : %s", snd_strerror ( err ) );
}
// clean-up
snd_pcm_sw_params_free ( swparams );
// clean-up
snd_pcm_sw_params_free ( swparams );
// start record
snd_pcm_reset ( rhandle );
snd_pcm_start ( rhandle );
// start record
snd_pcm_reset ( rhandle );
snd_pcm_start ( rhandle );
qDebug ( "alsa init record done" );
qDebug ( "alsa init record done" );
}
bool CSound::Read(CVector<short>& psData)
{
int ret;
int ret;
/* Check if device must be opened or reinitialized */
if (bChangParamIn == true)
{
InitRecording ( iBufferSizeIn * NUM_IN_OUT_CHANNELS );
/* Check if device must be opened or reinitialized */
if (bChangParamIn == true)
{
InitRecording ( iBufferSizeIn * NUM_IN_OUT_CHANNELS );
/* Reset flag */
bChangParamIn = false;
}
/* Reset flag */
bChangParamIn = false;
}
ret = snd_pcm_readi(rhandle, &psData[0], iBufferSizeIn);
ret = snd_pcm_readi(rhandle, &psData[0], iBufferSizeIn);
if ( ret < 0 )
{
if ( ret == -EPIPE )
{
// under-run
qDebug ( "rprepare" );
if ( ret < 0 )
{
if ( ret == -EPIPE )
{
// under-run
qDebug ( "rprepare" );
ret = snd_pcm_prepare ( rhandle );
ret = snd_pcm_prepare ( rhandle );
if ( ret < 0 )
{
qDebug ( "Can't recover from underrun, prepare failed: %s", snd_strerror ( ret ) );
}
if ( ret < 0 )
{
qDebug ( "Can't recover from underrun, prepare failed: %s", snd_strerror ( ret ) );
}
ret = snd_pcm_start ( rhandle );
ret = snd_pcm_start ( rhandle );
if (ret < 0)
{
qDebug ( "Can't recover from underrun, start failed: %s", snd_strerror ( ret ) );
}
if (ret < 0)
{
qDebug ( "Can't recover from underrun, start failed: %s", snd_strerror ( ret ) );
}
return true;
return true;
}
else if ( ret == -ESTRPIPE )
{
qDebug ( "strpipe" );
}
else if ( ret == -ESTRPIPE )
{
qDebug ( "strpipe" );
// wait until the suspend flag is released
while ( ( ret = snd_pcm_resume ( rhandle ) ) == -EAGAIN )
{
sleep ( 1 );
}
// wait until the suspend flag is released
while ( ( ret = snd_pcm_resume ( rhandle ) ) == -EAGAIN )
{
sleep ( 1 );
}
if ( ret < 0 )
{
ret = snd_pcm_prepare ( rhandle );
if ( ret < 0 )
{
ret = snd_pcm_prepare ( rhandle );
if (ret < 0)
{
qDebug ( "Can't recover from suspend, prepare failed: %s", snd_strerror ( ret ) );
}
throw CGenErr ( "CSound:Read" );
}
if (ret < 0)
{
qDebug ( "Can't recover from suspend, prepare failed: %s", snd_strerror ( ret ) );
}
throw CGenErr ( "CSound:Read" );
}
return true;
}
else
{
qDebug ( "CSound::Read: %s", snd_strerror ( ret ) );
throw CGenErr ( "CSound:Read" );
}
}
else
{
return false;
}
return true;
}
else
{
qDebug ( "CSound::Read: %s", snd_strerror ( ret ) );
throw CGenErr ( "CSound:Read" );
}
}
else
{
return false;
}
}
void CSound::SetInNumBuf ( int iNewNum )
{
/* check new parameter */
if ( ( iNewNum >= MAX_SND_BUF_IN ) || ( iNewNum < 1 ) )
{
iNewNum = NUM_PERIOD_BLOCKS_IN;
}
/* check new parameter */
if ( ( iNewNum >= MAX_SND_BUF_IN ) || ( iNewNum < 1 ) )
{
iNewNum = NUM_PERIOD_BLOCKS_IN;
}
/* Change only if parameter is different */
if ( iNewNum != iCurPeriodSizeIn )
{
iCurPeriodSizeIn = iNewNum;
bChangParamIn = true;
}
/* Change only if parameter is different */
if ( iNewNum != iCurPeriodSizeIn )
{
iCurPeriodSizeIn = iNewNum;
bChangParamIn = true;
}
}
/* Wave out *******************************************************************/
void CSound::InitPlayback ( int iNewBufferSize, bool bNewBlocking )
{
int err;
int err;
// save buffer size
iBufferSizeOut = iNewBufferSize / NUM_IN_OUT_CHANNELS; // mono size
// save buffer size
iBufferSizeOut = iNewBufferSize / NUM_IN_OUT_CHANNELS; // mono size
// if playback device was already open, close it first
if ( phandle != NULL )
{
snd_pcm_close ( phandle );
}
// if playback device was already open, close it first
if ( phandle != NULL )
{
snd_pcm_close ( phandle );
}
// playback device (either "hw:0,0" or "plughw:0,0")
if ( err = snd_pcm_open ( &phandle, "hw:0,0",
SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK ) != 0)
{
qDebug ( "open error: %s", snd_strerror ( err ) );
}
// playback device (either "hw:0,0" or "plughw:0,0")
if ( err = snd_pcm_open ( &phandle, "hw:0,0",
SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK ) != 0)
{
qDebug ( "open error: %s", snd_strerror ( err ) );
}
// non-blocking playback
if ( err = snd_pcm_nonblock ( phandle, TRUE ) != 0 )
{
qDebug ( "cannot set blocking: %s", snd_strerror ( err ) );
}
// non-blocking playback
if ( err = snd_pcm_nonblock ( phandle, TRUE ) != 0 )
{
qDebug ( "cannot set blocking: %s", snd_strerror ( err ) );
}
// set hardware parameters
SetHWParams ( phandle, iBufferSizeOut, iCurPeriodSizeOut );
// set hardware parameters
SetHWParams ( phandle, iBufferSizeOut, iCurPeriodSizeOut );
// start playback
snd_pcm_start ( phandle );
// start playback
snd_pcm_start ( phandle );
qDebug ( "alsa init playback done" );
qDebug ( "alsa init playback done" );
}
bool CSound::Write ( CVector<short>& psData )
{
int size = iBufferSizeIn;
int start = 0;
int ret;
int size = iBufferSizeIn;
int start = 0;
int ret;
/* Check if device must be opened or reinitialized */
if ( bChangParamOut == true )
{
InitPlayback ( iBufferSizeOut * NUM_IN_OUT_CHANNELS );
/* Check if device must be opened or reinitialized */
if ( bChangParamOut == true )
{
InitPlayback ( iBufferSizeOut * NUM_IN_OUT_CHANNELS );
/* Reset flag */
bChangParamOut = false;
}
/* Reset flag */
bChangParamOut = false;
}
while ( size )
{
ret = snd_pcm_writei ( phandle, &psData[start], size );
while ( size )
{
ret = snd_pcm_writei ( phandle, &psData[start], size );
if ( ret < 0 )
{
if ( ret == -EPIPE )
{
/* under-run */
qDebug ( "wunderrun" );
if ( ret < 0 )
{
if ( ret == -EPIPE )
{
/* under-run */
qDebug ( "wunderrun" );
ret = snd_pcm_prepare ( phandle );
ret = snd_pcm_prepare ( phandle );
if ( ret < 0 )
{
qDebug ( "Can't recover from underrun, prepare failed: %s", snd_strerror ( ret ) );
}
continue;
}
else if ( ret == -EAGAIN )
{
if ( ( ret = snd_pcm_wait ( phandle, 1 ) ) < 0 )
{
qDebug ( "poll failed (%s)", snd_strerror ( ret ) );
break;
}
continue;
}
else if ( ret == -ESTRPIPE )
{
qDebug("wstrpipe");
if ( ret < 0 )
{
qDebug ( "Can't recover from underrun, prepare failed: %s", snd_strerror ( ret ) );
}
continue;
}
else if ( ret == -EAGAIN )
{
if ( ( ret = snd_pcm_wait ( phandle, 1 ) ) < 0 )
{
qDebug ( "poll failed (%s)", snd_strerror ( ret ) );
break;
}
continue;
}
else if ( ret == -ESTRPIPE )
{
qDebug("wstrpipe");
/* wait until the suspend flag is released */
while ( (ret = snd_pcm_resume ( phandle ) ) == -EAGAIN )
{
sleep(1);
}
/* wait until the suspend flag is released */
while ( (ret = snd_pcm_resume ( phandle ) ) == -EAGAIN )
{
sleep(1);
}
if ( ret < 0 )
{
ret = snd_pcm_prepare ( phandle );
if ( ret < 0 )
{
ret = snd_pcm_prepare ( phandle );
if ( ret < 0 )
{
qDebug ( "Can't recover from suspend, prepare failed: %s", snd_strerror ( ret ) );
}
}
continue;
}
else
{
qDebug ( "Write error: %s", snd_strerror ( ret ) );
}
break; // skip one period
}
if ( ret < 0 )
{
qDebug ( "Can't recover from suspend, prepare failed: %s", snd_strerror ( ret ) );
}
}
continue;
}
else
{
qDebug ( "Write error: %s", snd_strerror ( ret ) );
}
break; // skip one period
}
size -= ret;
start += ret;
}
size -= ret;
start += ret;
}
return false;
return false;
}
void CSound::SetOutNumBuf(int iNewNum)
{
/* check new parameter */
if ( ( iNewNum >= MAX_SND_BUF_OUT ) || ( iNewNum < 1 ) )
{
iNewNum = NUM_PERIOD_BLOCKS_OUT;
}
/* check new parameter */
if ( ( iNewNum >= MAX_SND_BUF_OUT ) || ( iNewNum < 1 ) )
{
iNewNum = NUM_PERIOD_BLOCKS_OUT;
}
/* Change only if parameter is different */
if ( iNewNum != iCurPeriodSizeOut )
{
iCurPeriodSizeOut = iNewNum;
bChangParamOut = true;
}
/* Change only if parameter is different */
if ( iNewNum != iCurPeriodSizeOut )
{
iCurPeriodSizeOut = iNewNum;
bChangParamOut = true;
}
}
/* common **********************************************************************/
bool CSound::SetHWParams(snd_pcm_t* handle, const int iBufferSizeIn,
const int iNumPeriodBlocks)
const int iNumPeriodBlocks)
{
int err;
snd_pcm_hw_params_t* hwparams;
int err;
snd_pcm_hw_params_t* hwparams;
// allocate an invalid snd_pcm_hw_params_t using standard malloc
if ( err = snd_pcm_hw_params_malloc ( &hwparams ) < 0 )
{
qDebug ( "cannot allocate hardware parameter structure (%s)\n", snd_strerror ( err ) );
return true;
}
// allocate an invalid snd_pcm_hw_params_t using standard malloc
if ( err = snd_pcm_hw_params_malloc ( &hwparams ) < 0 )
{
qDebug ( "cannot allocate hardware parameter structure (%s)\n", snd_strerror ( err ) );
return true;
}
// fill params with a full configuration space for a PCM
if ( err = snd_pcm_hw_params_any ( handle, hwparams ) < 0 )
{
qDebug ( "cannot initialize hardware parameter structure (%s)\n", snd_strerror ( err ) );
return true;
}
// fill params with a full configuration space for a PCM
if ( err = snd_pcm_hw_params_any ( handle, hwparams ) < 0 )
{
qDebug ( "cannot initialize hardware parameter structure (%s)\n", snd_strerror ( err ) );
return true;
}
// restrict a configuration space to contain only one access type:
// set the interleaved read/write format
if ( err = snd_pcm_hw_params_set_access ( handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED ) < 0 )
{
qDebug ( "Access type not available : %s", snd_strerror ( err ) );
return true;
}
// restrict a configuration space to contain only one access type:
// set the interleaved read/write format
if ( err = snd_pcm_hw_params_set_access ( handle, hwparams, SND_PCM_ACCESS_RW_INTERLEAVED ) < 0 )
{
qDebug ( "Access type not available : %s", snd_strerror ( err ) );
return true;
}
// restrict a configuration space to contain only one format:
// set the sample format PCM, 16 bit
if ( err = snd_pcm_hw_params_set_format ( handle, hwparams, SND_PCM_FORMAT_S16 ) < 0 )
{
qDebug ( "Sample format not available : %s", snd_strerror ( err ) );
return true;
}
// restrict a configuration space to contain only one format:
// set the sample format PCM, 16 bit
if ( err = snd_pcm_hw_params_set_format ( handle, hwparams, SND_PCM_FORMAT_S16 ) < 0 )
{
qDebug ( "Sample format not available : %s", snd_strerror ( err ) );
return true;
}
// restrict a configuration space to contain only one channels count:
// set the count of channels (usually stereo, 2 channels)
if ( err = snd_pcm_hw_params_set_channels(handle, hwparams, NUM_IN_OUT_CHANNELS ) < 0 )
{
qDebug ( "Channels count (%i) not available s: %s", NUM_IN_OUT_CHANNELS, snd_strerror ( err ) );
return true;
}
// restrict a configuration space to contain only one channels count:
// set the count of channels (usually stereo, 2 channels)
if ( err = snd_pcm_hw_params_set_channels(handle, hwparams, NUM_IN_OUT_CHANNELS ) < 0 )
{
qDebug ( "Channels count (%i) not available s: %s", NUM_IN_OUT_CHANNELS, snd_strerror ( err ) );
return true;
}
// restrict a configuration space to have rate nearest to a target:
// set the sample-rate
unsigned int rrate = SND_CRD_SAMPLE_RATE;
if ( err = snd_pcm_hw_params_set_rate_near ( handle, hwparams, &rrate, 0 ) < 0 )
{
qDebug ( "Rate %iHz not available : %s", rrate, snd_strerror ( err ) );
return true;
}
if ( rrate != SND_CRD_SAMPLE_RATE ) // check if rate is possible
{
qDebug ( "Rate doesn't match (requested %iHz, get %iHz)", rrate, err );
return true;
}
// restrict a configuration space to have rate nearest to a target:
// set the sample-rate
unsigned int rrate = SND_CRD_SAMPLE_RATE;
if ( err = snd_pcm_hw_params_set_rate_near ( handle, hwparams, &rrate, 0 ) < 0 )
{
qDebug ( "Rate %iHz not available : %s", rrate, snd_strerror ( err ) );
return true;
}
if ( rrate != SND_CRD_SAMPLE_RATE ) // check if rate is possible
{
qDebug ( "Rate doesn't match (requested %iHz, get %iHz)", rrate, err );
return true;
}
// set the buffer size and period size
snd_pcm_uframes_t BufferFrames = iBufferSizeIn * iNumPeriodBlocks;
if ( err = snd_pcm_hw_params_set_buffer_size_near ( handle, hwparams, &BufferFrames ) < 0 )
{
qDebug ( "cannot set buffer size (%s)\n", snd_strerror ( err ) );
return true;
}
// set the buffer size and period size
snd_pcm_uframes_t BufferFrames = iBufferSizeIn * iNumPeriodBlocks;
if ( err = snd_pcm_hw_params_set_buffer_size_near ( handle, hwparams, &BufferFrames ) < 0 )
{
qDebug ( "cannot set buffer size (%s)\n", snd_strerror ( err ) );
return true;
}
// set the period size
snd_pcm_uframes_t PeriodSize = iBufferSizeIn;
if ( err = snd_pcm_hw_params_set_period_size_near ( handle, hwparams, &PeriodSize, 0 ) < 0 )
{
qDebug ( "cannot set period size (%s)\n", snd_strerror ( err ) );
return true;
}
// set the period size
snd_pcm_uframes_t PeriodSize = iBufferSizeIn;
if ( err = snd_pcm_hw_params_set_period_size_near ( handle, hwparams, &PeriodSize, 0 ) < 0 )
{
qDebug ( "cannot set period size (%s)\n", snd_strerror ( err ) );
return true;
}
// write the parameters to device
if ( err = snd_pcm_hw_params ( handle, hwparams ) < 0 )
{
qDebug("Unable to set hw params : %s", snd_strerror(err));
return true;
}
// write the parameters to device
if ( err = snd_pcm_hw_params ( handle, hwparams ) < 0 )
{
qDebug("Unable to set hw params : %s", snd_strerror(err));
return true;
}
/* 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));
qDebug("Unable to get buffer size for playback: %s\n", snd_strerror(err));
}
qDebug("buffer size: %d (desired: %d)", buffer_size, iBufferSizeIn * iNumPeriodBlocks);
@ -428,34 +428,34 @@ snd_pcm_uframes_t period_size;
err = snd_pcm_hw_params_get_period_size(hwparams, &period_size, 0);
if (err < 0)
{
qDebug("Unable to get period size for playback: %s\n", snd_strerror(err));
qDebug("Unable to get period size for playback: %s\n", snd_strerror(err));
}
qDebug("frame size: %d (desired: %d)", period_size, iBufferSizeIn);
/* clean-up */
snd_pcm_hw_params_free ( hwparams );
/* clean-up */
snd_pcm_hw_params_free ( hwparams );
return false;
return false;
}
void CSound::Close ()
{
/* read */
if ( rhandle != NULL )
{
snd_pcm_close ( rhandle );
}
/* read */
if ( rhandle != NULL )
{
snd_pcm_close ( rhandle );
}
rhandle = NULL;
rhandle = NULL;
/* playback */
if ( phandle != NULL )
{
snd_pcm_close ( phandle );
}
/* playback */
if ( phandle != NULL )
{
snd_pcm_close ( phandle );
}
phandle = NULL;
phandle = NULL;
}
#endif /* WITH_SOUND */

View File

@ -2,7 +2,7 @@
* Copyright (c) 2004-2005
*
* Author(s):
* Volker Fischer, Alexander Kurpiers
* Volker Fischer, Alexander Kurpiers
*
* This code is based on the Open-Source sound interface implementation of
* the Dream DRM Receiver project.
@ -31,74 +31,74 @@
/* 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 */
#define NUM_PERIOD_BLOCKS_IN 2
#define NUM_PERIOD_BLOCKS_OUT 2
#define NUM_PERIOD_BLOCKS_IN 2
#define NUM_PERIOD_BLOCKS_OUT 2
#define MAX_SND_BUF_IN 200
#define MAX_SND_BUF_OUT 200
#define MAX_SND_BUF_IN 200
#define MAX_SND_BUF_OUT 200
/* Classes ********************************************************************/
class CSound
{
public:
CSound()
CSound()
#if WITH_SOUND
: rhandle(NULL), phandle(NULL), iCurPeriodSizeIn(NUM_PERIOD_BLOCKS_IN),
iCurPeriodSizeOut(NUM_PERIOD_BLOCKS_OUT), bChangParamIn(true),
bChangParamOut(true)
: rhandle(NULL), phandle(NULL), iCurPeriodSizeIn(NUM_PERIOD_BLOCKS_IN),
iCurPeriodSizeOut(NUM_PERIOD_BLOCKS_OUT), bChangParamIn(true),
bChangParamOut(true)
#endif
{}
virtual ~CSound() {Close();}
{}
virtual ~CSound() {Close();}
/* Not implemented yet, always return one device and default string */
int GetNumDev() {return 1;}
void SetOutDev(int iNewDev) {}
void SetInDev(int iNewDev) {}
/* Not implemented yet, always return one device and default string */
int GetNumDev() {return 1;}
void SetOutDev(int iNewDev) {}
void SetInDev(int iNewDev) {}
/* Return invalid device ID which is the same as using "wave mapper" which
we assume here to be used */
int GetOutDev() {return 1;}
int GetInDev() {return 1;}
/* Return invalid device ID which is the same as using "wave mapper" which
we assume here to be used */
int GetOutDev() {return 1;}
int GetInDev() {return 1;}
#if WITH_SOUND
void SetInNumBuf(int iNewNum);
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);
bool Read(CVector<short>& psData);
bool Write(CVector<short>& psData);
void SetInNumBuf(int iNewNum);
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);
bool Read(CVector<short>& psData);
bool Write(CVector<short>& psData);
void Close();
void Close();
protected:
snd_pcm_t* rhandle;
snd_pcm_t* phandle;
snd_pcm_t* rhandle;
snd_pcm_t* phandle;
bool SetHWParams(snd_pcm_t* handle, const int iBufferSizeIn,
const int iNumPeriodBlocks);
bool SetHWParams(snd_pcm_t* handle, const int iBufferSizeIn,
const int iNumPeriodBlocks);
int iBufferSizeOut;
int iBufferSizeIn;
bool bChangParamIn;
int iCurPeriodSizeIn;
bool bChangParamOut;
int iCurPeriodSizeOut;
int iBufferSizeOut;
int iBufferSizeIn;
bool bChangParamIn;
int iCurPeriodSizeIn;
bool bChangParamOut;
int iCurPeriodSizeOut;
#else
/* 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!");}
bool Read(CVector<short>& psData) {printf("no sound!"); return false;}
bool Write(CVector<short>& psData) {printf("no sound!"); return false;}
void Close() {}
/* 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!");}
bool Read(CVector<short>& psData) {printf("no sound!"); return false;}
bool Write(CVector<short>& psData) {printf("no sound!"); return false;}
void Close() {}
#endif
};

View File

@ -26,14 +26,15 @@
/* Implementation *************************************************************/
CClient::CClient () : bRun ( false ), Socket ( &Channel ),
CClient::CClient() : bRun ( false ), Socket ( &Channel ),
iAudioInFader ( AUD_FADER_IN_MAX / 2 ),
iReverbLevel ( AUD_REVERB_MAX / 6 ),
bReverbOnLeftChan ( false ),
iNetwBufSizeFactIn ( DEF_NET_BLOCK_SIZE_FACTOR )
{
// connection for protocol
QObject::connect ( &Channel, SIGNAL ( MessReadyForSending ( CVector<uint8_t> ) ),
QObject::connect ( &Channel,
SIGNAL ( MessReadyForSending ( CVector<uint8_t> ) ),
this, SLOT ( OnSendProtMessage ( CVector<uint8_t> ) ) );
QObject::connect ( &Channel, SIGNAL ( ReqJittBufSize() ),
@ -42,7 +43,8 @@ CClient::CClient () : bRun ( false ), Socket ( &Channel ),
QObject::connect ( &Channel, SIGNAL ( ProtocolStatus ( bool ) ),
this, SLOT ( OnProtocolStatus ( bool ) ) );
QObject::connect ( &Channel, SIGNAL ( ConClientListMesReceived ( CVector<CChannelShortInfo> ) ),
QObject::connect ( &Channel,
SIGNAL ( ConClientListMesReceived ( CVector<CChannelShortInfo> ) ),
SIGNAL ( ConClientListMesReceived ( CVector<CChannelShortInfo> ) ) );
}
@ -50,15 +52,15 @@ void CClient::OnSendProtMessage ( CVector<uint8_t> vecMessage )
{
// convert unsigned uint8_t in char, TODO convert all buffers in uint8_t
CVector<unsigned char> vecbyDataConv ( vecMessage.Size () );
for ( int i = 0; i < vecMessage.Size (); i++ ) {
CVector<unsigned char> vecbyDataConv ( vecMessage.Size() );
for ( int i = 0; i < vecMessage.Size(); i++ ) {
vecbyDataConv[i] = static_cast<unsigned char> ( vecMessage[i] );
}
// the protocol queries me to call the function to send the message
// send it through the network
Socket.SendPacket ( vecbyDataConv, Channel.GetAddress () );
Socket.SendPacket ( vecbyDataConv, Channel.GetAddress() );
}
void CClient::OnReqJittBufSize()
@ -102,37 +104,37 @@ void CClient::OnProtocolStatus ( bool bOk )
void CClient::Init()
{
/* set block sizes (in samples) */
// set block sizes (in samples)
iBlockSizeSam = MIN_BLOCK_SIZE_SAMPLES;
iSndCrdBlockSizeSam = MIN_SND_CRD_BLOCK_SIZE_SAMPLES;
vecsAudioSndCrd.Init(iSndCrdBlockSizeSam * 2); /* stereo */
vecdAudioSndCrdL.Init(iSndCrdBlockSizeSam);
vecdAudioSndCrdR.Init(iSndCrdBlockSizeSam);
vecsAudioSndCrd.Init ( iSndCrdBlockSizeSam * 2 ); // stereo
vecdAudioSndCrdL.Init ( iSndCrdBlockSizeSam );
vecdAudioSndCrdR.Init ( iSndCrdBlockSizeSam );
vecdAudioL.Init(iBlockSizeSam);
vecdAudioR.Init(iBlockSizeSam);
vecdAudioL.Init ( iBlockSizeSam );
vecdAudioR.Init ( iBlockSizeSam );
Sound.InitRecording(iSndCrdBlockSizeSam * 2 /* stereo */);
Sound.InitPlayback(iSndCrdBlockSizeSam * 2 /* stereo */);
Sound.InitRecording ( iSndCrdBlockSizeSam * 2 ); // stereo
Sound.InitPlayback ( iSndCrdBlockSizeSam * 2 ); // stereo
/* resample objects are always initialized with the input block size */
/* record */
ResampleObjDownL.Init(iSndCrdBlockSizeSam, SND_CRD_SAMPLE_RATE, SAMPLE_RATE);
ResampleObjDownR.Init(iSndCrdBlockSizeSam, SND_CRD_SAMPLE_RATE, SAMPLE_RATE);
// resample objects are always initialized with the input block size
// record
ResampleObjDownL.Init ( iSndCrdBlockSizeSam, SND_CRD_SAMPLE_RATE, SAMPLE_RATE );
ResampleObjDownR.Init ( iSndCrdBlockSizeSam, SND_CRD_SAMPLE_RATE, SAMPLE_RATE );
/* playback */
ResampleObjUpL.Init(iBlockSizeSam, SAMPLE_RATE, SND_CRD_SAMPLE_RATE);
ResampleObjUpR.Init(iBlockSizeSam, SAMPLE_RATE, SND_CRD_SAMPLE_RATE);
// playback
ResampleObjUpL.Init ( iBlockSizeSam, SAMPLE_RATE, SND_CRD_SAMPLE_RATE );
ResampleObjUpR.Init ( iBlockSizeSam, SAMPLE_RATE, SND_CRD_SAMPLE_RATE );
/* init network buffers */
vecsNetwork.Init(iBlockSizeSam);
vecdNetwData.Init(iBlockSizeSam);
// init network buffers
vecsNetwork.Init ( iBlockSizeSam );
vecdNetwData.Init ( iBlockSizeSam );
/* init moving average buffer for response time evaluation */
RespTimeMoAvBuf.Init(LEN_MOV_AV_RESPONSE);
// init moving average buffer for response time evaluation
RespTimeMoAvBuf.Init ( LEN_MOV_AV_RESPONSE );
/* init time for response time evaluation */
// init time for response time evaluation
TimeLastBlock = QTime::currentTime();
AudioReverb.Clear();
@ -142,111 +144,116 @@ void CClient::run()
{
int i, iInCnt;
/* Set thread priority (The working thread should have a higher
priority than the GUI) */
// Set thread priority (The working thread should have a higher
// priority than the GUI)
#ifdef _WIN32
SetThreadPriority ( GetCurrentThread (), THREAD_PRIORITY_ABOVE_NORMAL );
SetThreadPriority ( GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL );
#else
/* set the process to realtime privs, taken from
"http://www.gardena.net/benno/linux/audio" but does not seem to work,
maybe a problem with user rights */
// set the process to realtime privs, taken from
// "http://www.gardena.net/benno/linux/audio" but does not seem to work,
// maybe a problem with user rights
struct sched_param schp;
memset ( &schp, 0, sizeof ( schp ) );
schp.sched_priority = sched_get_priority_max ( SCHED_FIFO );
sched_setscheduler ( 0, SCHED_FIFO, &schp );
#endif
/* init object */
// init object
Init();
/* runtime phase --------------------------------------------------------- */
// runtime phase ------------------------------------------------------------
bRun = true;
/* main loop of working thread */
while (bRun)
// main loop of working thread
while ( bRun )
{
/* get audio from sound card (blocking function) */
if (Sound.Read(vecsAudioSndCrd))
// get audio from sound card (blocking function)
if ( Sound.Read ( vecsAudioSndCrd ) )
{
PostWinMessage(MS_SOUND_IN, MUL_COL_LED_RED);
PostWinMessage ( MS_SOUND_IN, MUL_COL_LED_RED );
}
else
{
PostWinMessage(MS_SOUND_IN, MUL_COL_LED_GREEN);
PostWinMessage ( MS_SOUND_IN, MUL_COL_LED_GREEN );
}
/* copy data from one stereo buffer in two separate buffers */
// copy data from one stereo buffer in two separate buffers
iInCnt = 0;
for (i = 0; i < iSndCrdBlockSizeSam; i++)
for ( i = 0; i < iSndCrdBlockSizeSam; i++ )
{
vecdAudioSndCrdL[i] = (double) vecsAudioSndCrd[iInCnt++];
vecdAudioSndCrdR[i] = (double) vecsAudioSndCrd[iInCnt++];
}
/* resample data for each channel seaparately */
ResampleObjDownL.Resample(vecdAudioSndCrdL, vecdAudioL);
ResampleObjDownR.Resample(vecdAudioSndCrdR, vecdAudioR);
// resample data for each channel seaparately
ResampleObjDownL.Resample ( vecdAudioSndCrdL, vecdAudioL );
ResampleObjDownR.Resample ( vecdAudioSndCrdR, vecdAudioR );
/* update signal level meters */
SignalLevelMeterL.Update(vecdAudioL);
SignalLevelMeterR.Update(vecdAudioR);
// update signal level meters
SignalLevelMeterL.Update ( vecdAudioL );
SignalLevelMeterR.Update ( vecdAudioR );
/* add reverberation effect if activated */
if (iReverbLevel != 0)
// add reverberation effect if activated
if ( iReverbLevel != 0 )
{
/* first attenuation amplification factor */
// first attenuation amplification factor
const double dRevLev = (double) iReverbLevel / AUD_REVERB_MAX / 2;
if (bReverbOnLeftChan)
if ( bReverbOnLeftChan )
{
for (i = 0; i < iBlockSizeSam; i++)
{
/* left channel */
// left channel
vecdAudioL[i] +=
dRevLev * AudioReverb.ProcessSample(vecdAudioL[i]);
dRevLev * AudioReverb.ProcessSample ( vecdAudioL[i] );
}
}
else
{
for (i = 0; i < iBlockSizeSam; i++)
for ( i = 0; i < iBlockSizeSam; i++ )
{
/* right channel */
// right channel
vecdAudioR[i] +=
dRevLev * AudioReverb.ProcessSample(vecdAudioR[i]);
dRevLev * AudioReverb.ProcessSample ( vecdAudioR[i] );
}
}
}
/* mix both signals depending on the fading setting */
// mix both signals depending on the fading setting
const int iMiddleOfFader = AUD_FADER_IN_MAX / 2;
const double dAttFact =
(double) (iMiddleOfFader - abs(iMiddleOfFader - iAudioInFader)) /
(double) ( iMiddleOfFader - abs ( iMiddleOfFader - iAudioInFader ) ) /
iMiddleOfFader;
for (i = 0; i < iBlockSizeSam; i++)
for ( i = 0; i < iBlockSizeSam; i++ )
{
double dMixedSignal;
if (iAudioInFader > iMiddleOfFader)
if ( iAudioInFader > iMiddleOfFader )
{
dMixedSignal = vecdAudioL[i] + dAttFact * vecdAudioR[i];
}
else
{
dMixedSignal = vecdAudioR[i] + dAttFact * vecdAudioL[i];
}
vecsNetwork[i] = Double2Short(dMixedSignal);
vecsNetwork[i] = Double2Short ( dMixedSignal );
}
/* send it through the network */
// send it through the network
Socket.SendPacket ( Channel.PrepSendPacket ( vecsNetwork ),
Channel.GetAddress () );
/* receive a new block */
if (Channel.GetData(vecdNetwData))
// receive a new block
if ( Channel.GetData ( vecdNetwData ) )
{
PostWinMessage(MS_JIT_BUF_GET, MUL_COL_LED_GREEN);
PostWinMessage ( MS_JIT_BUF_GET, MUL_COL_LED_GREEN );
}
else
{
PostWinMessage(MS_JIT_BUF_GET, MUL_COL_LED_RED);
PostWinMessage ( MS_JIT_BUF_GET, MUL_COL_LED_RED );
}
#ifdef _DEBUG_
@ -311,53 +318,53 @@ fflush(pFileDelay);
*/
/* check if channel is connected */
if (Channel.IsConnected())
// check if channel is connected
if ( Channel.IsConnected() )
{
/* write mono input signal in both sound-card channels */
for (i = 0; i < iBlockSizeSam; i++)
// write mono input signal in both sound-card channels
for ( i = 0; i < iBlockSizeSam; i++ )
{
vecdAudioL[i] = vecdAudioR[i] = vecdNetwData[i];
}
}
else
{
/* if not connected, clear data */
for (i = 0; i < iBlockSizeSam; i++)
// if not connected, clear data
for ( i = 0; i < iBlockSizeSam; i++ )
{
vecdAudioL[i] = vecdAudioR[i] = 0.0;
}
}
/* resample data for each channel separately */
ResampleObjUpL.Resample(vecdAudioL, vecdAudioSndCrdL);
ResampleObjUpR.Resample(vecdAudioR, vecdAudioSndCrdR);
// resample data for each channel separately
ResampleObjUpL.Resample ( vecdAudioL, vecdAudioSndCrdL );
ResampleObjUpR.Resample ( vecdAudioR, vecdAudioSndCrdR );
/* copy data from one stereo buffer in two separate buffers */
// copy data from one stereo buffer in two separate buffers
iInCnt = 0;
for (i = 0; i < iSndCrdBlockSizeSam; i++)
for ( i = 0; i < iSndCrdBlockSizeSam; i++ )
{
vecsAudioSndCrd[iInCnt++] = Double2Short(vecdAudioSndCrdL[i]);
vecsAudioSndCrd[iInCnt++] = Double2Short(vecdAudioSndCrdR[i]);
vecsAudioSndCrd[iInCnt++] = Double2Short ( vecdAudioSndCrdL[i] );
vecsAudioSndCrd[iInCnt++] = Double2Short ( vecdAudioSndCrdR[i] );
}
/* play the new block */
if (Sound.Write(vecsAudioSndCrd))
// play the new block
if ( Sound.Write ( vecsAudioSndCrd ) )
{
PostWinMessage(MS_SOUND_OUT, MUL_COL_LED_RED);
PostWinMessage ( MS_SOUND_OUT, MUL_COL_LED_RED );
}
else
{
PostWinMessage(MS_SOUND_OUT, MUL_COL_LED_GREEN);
PostWinMessage ( MS_SOUND_OUT, MUL_COL_LED_GREEN );
}
/* update response time measurement --------------------------------- */
/* add time difference */
// update response time measurement ------------------------------------
// add time difference
const QTime CurTime = QTime::currentTime();
/* we want to calculate the standard deviation (we assume that the mean
is correct at the block period time) */
// we want to calculate the standard deviation (we assume that the mean
// is correct at the block period time)
const double dCurAddVal =
( (double) TimeLastBlock.msecsTo ( CurTime ) - MIN_BLOCK_DURATION_MS );
@ -368,23 +375,23 @@ fprintf(pFileTest, "%e\n", dCurAddVal);
fflush(pFileTest);
*/
RespTimeMoAvBuf.Add ( dCurAddVal * dCurAddVal ); /* add squared value */
RespTimeMoAvBuf.Add ( dCurAddVal * dCurAddVal ); // add squared value
/* store old time value */
// store old time value
TimeLastBlock = CurTime;
}
/* reset current signal level and LEDs */
// reset current signal level and LEDs
SignalLevelMeterL.Reset();
SignalLevelMeterR.Reset();
PostWinMessage(MS_RESET_ALL, 0);
PostWinMessage ( MS_RESET_ALL, 0 );
}
bool CClient::Stop()
{
/* set flag so that thread can leave the main loop */
// set flag so that thread can leave the main loop
bRun = false;
/* give thread some time to terminate, return status */
return wait(5000);
// give thread some time to terminate, return status
return wait ( 5000 );
}

View File

@ -114,9 +114,13 @@ CLlconClientDlg::CLlconClientDlg ( CClient* pNCliP, QWidget* parent,
/* set radio buttons --- */
// reverb channel
if (pClient->IsReverbOnLeftChan())
{
RadioButtonRevSelL->setChecked(true);
}
else
{
RadioButtonRevSelR->setChecked(true);
}
/* Settings menu ------------------------------------------------------- */
@ -138,6 +142,18 @@ CLlconClientDlg::CLlconClientDlg ( CClient* pNCliP, QWidget* parent,
/* Now tell the layout about the menu */
CLlconClientDlgBaseLayout->setMenuBar ( pMenu );
// mixer board -------------------------------------------------------------
// create all mixer controls and make them invisible
vecpChanFader.Init ( MAX_NUM_CHANNELS );
for ( int i = 0; i < MAX_NUM_CHANNELS; i++ )
{
vecpChanFader[i] = new CLlconClientDlg::CChannelFader ( FrameAudioFaders,
FrameAudioFadersLayout, "test" );
vecpChanFader[i]->Hide();
}
/* connections ---------------------------------------------------------- */
// push-buttons
@ -221,7 +237,20 @@ void CLlconClientDlg::OnConnectDisconBut ()
ProgressBarInputLevelR->setProgress ( 0 );
/* immediately update status bar */
OnTimerStatus ();
OnTimerStatus ();
// TEST
/*
// make old controls invisible
for ( int i = 0; i < MAX_NUM_CHANNELS; i++ )
{
vecpChanFader[i]->Hide();
}
*/
}
else
{
@ -294,32 +323,28 @@ void CLlconClientDlg::OnConClientListMesReceived ( CVector<CChannelShortInfo> ve
int i;
//FrameAudioFadersLayout->addWidget(new QLabel ( "test", FrameAudioFaders ));
// TODO
// remove old controls
for ( i = 0; i < vecpChanFader.Size(); i++ )
// make old controls invisible
for ( i = 0; i < MAX_NUM_CHANNELS; i++ )
{
delete vecpChanFader[i];
vecpChanFader[i]->Hide();
}
// TEST add current faders
vecpChanFader.Init ( vecChanInfo.Size() );
for ( i = 0; i < vecChanInfo.Size(); i++ )
{
QHostAddress addrTest ( vecChanInfo[i].veciIpAddr );
vecpChanFader[i] = new CLlconClientDlg::CChannelFader ( FrameAudioFaders,
FrameAudioFadersLayout, addrTest.toString() );
vecpChanFader[i]->Show();
vecpChanFader[i]->SetText ( addrTest.toString().latin1() );
// vecpChanFader[i] = new CLlconClientDlg::CChannelFader ( FrameAudioFaders,
// FrameAudioFadersLayout, addrTest.toString() );
}
//FrameAudioFadersLayout->addWidget(new QLabel ( "test", FrameAudioFaders ));
}
@ -399,3 +424,8 @@ pFader->setEnabled ( FALSE );
pParentLayout->insertLayout ( 0, pMainGrid );
}
void CLlconClientDlg::CChannelFader::SetText ( const std::string sText )
{
pLabel->setText ( sText.c_str() );
}

View File

@ -85,6 +85,10 @@ protected:
// TODO get rid of pMainGrid
}
void SetText ( const std::string sText );
void Show() { pLabel->show(); pFader->show(); }
void Hide() { pLabel->hide(); pFader->hide(); }
protected:
QGridLayout* pMainGrid;
QSlider* pFader;