2006-12-18 15:39:33 +01:00
|
|
|
/******************************************************************************\
|
2009-02-15 19:11:15 +01:00
|
|
|
* Copyright (c) 2004-2009
|
2006-12-18 15:39:33 +01:00
|
|
|
*
|
|
|
|
* Author(s):
|
|
|
|
* Volker Fischer, Alexander Kurpiers
|
|
|
|
*
|
|
|
|
* This code is based on the Open-Source sound interface implementation of
|
2009-03-10 21:01:46 +01:00
|
|
|
* the Dream DRM Receiver project and on the simple_client example of the
|
|
|
|
* Jack audio interface.
|
2006-12-18 15:39:33 +01:00
|
|
|
*
|
|
|
|
\******************************************************************************/
|
|
|
|
|
|
|
|
#include "sound.h"
|
|
|
|
|
|
|
|
#ifdef WITH_SOUND
|
2009-03-08 09:10:10 +01:00
|
|
|
# if USE_JACK
|
2009-03-28 10:05:56 +01:00
|
|
|
void CSound::OpenJack()
|
2009-03-08 09:10:10 +01:00
|
|
|
{
|
|
|
|
jack_status_t JackStatus;
|
|
|
|
|
|
|
|
// try to become a client of the JACK server
|
|
|
|
pJackClient = jack_client_open ( "llcon", JackNullOption, &JackStatus );
|
|
|
|
if ( pJackClient == NULL )
|
|
|
|
{
|
|
|
|
throw CGenErr ( "Jack server not running" );
|
|
|
|
}
|
|
|
|
|
|
|
|
// tell the JACK server to call "process()" whenever
|
|
|
|
// there is work to be done
|
|
|
|
jack_set_process_callback ( pJackClient, process, this );
|
2009-03-08 22:38:38 +01:00
|
|
|
|
2009-03-10 18:33:12 +01:00
|
|
|
// register a "buffer size changed" callback function
|
|
|
|
jack_set_buffer_size_callback ( pJackClient, bufferSizeCallback, this );
|
|
|
|
|
2009-03-12 12:56:15 +01:00
|
|
|
// register shutdown callback function
|
|
|
|
jack_on_shutdown ( pJackClient, shutdownCallback, this );
|
|
|
|
|
2009-03-08 22:38:38 +01:00
|
|
|
// TEST check sample rate, if not correct, just fire error
|
2009-07-26 09:29:32 +02:00
|
|
|
if ( jack_get_sample_rate ( pJackClient ) != SYSTEM_SAMPLE_RATE )
|
2009-03-08 22:38:38 +01:00
|
|
|
{
|
|
|
|
throw CGenErr ( "Jack server sample rate is different from "
|
|
|
|
"required one" );
|
2009-03-08 09:10:10 +01:00
|
|
|
}
|
2009-03-28 10:05:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSound::CloseJack()
|
|
|
|
{
|
|
|
|
// close client connection to jack server
|
|
|
|
jack_client_close ( pJackClient );
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSound::Start()
|
|
|
|
{
|
|
|
|
const char** ports;
|
2009-03-08 09:10:10 +01:00
|
|
|
|
2009-03-09 23:52:28 +01:00
|
|
|
// create four ports (two for input, two for output -> stereo)
|
2009-03-08 22:38:38 +01:00
|
|
|
input_port_left = jack_port_register ( pJackClient, "input left",
|
|
|
|
JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0 );
|
2009-03-10 19:39:29 +01:00
|
|
|
|
2009-03-08 22:38:38 +01:00
|
|
|
input_port_right = jack_port_register ( pJackClient, "input right",
|
|
|
|
JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0 );
|
2009-03-08 09:10:10 +01:00
|
|
|
|
2009-03-08 22:38:38 +01:00
|
|
|
output_port_left = jack_port_register ( pJackClient, "output left",
|
|
|
|
JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0 );
|
2009-03-10 19:39:29 +01:00
|
|
|
|
2009-03-08 22:38:38 +01:00
|
|
|
output_port_right = jack_port_register ( pJackClient, "output right",
|
|
|
|
JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0 );
|
2009-03-08 09:10:10 +01:00
|
|
|
|
2009-03-08 22:38:38 +01:00
|
|
|
// tell the JACK server that we are ready to roll
|
|
|
|
if ( jack_activate ( pJackClient ) )
|
|
|
|
{
|
|
|
|
throw CGenErr ( "Cannot activate client" );
|
|
|
|
}
|
2009-03-08 09:10:10 +01:00
|
|
|
|
2009-03-08 22:38:38 +01:00
|
|
|
// connect the ports, note: you cannot do this before
|
|
|
|
// the client is activated, because we cannot allow
|
|
|
|
// connections to be made to clients that are not
|
|
|
|
// running
|
|
|
|
if ( ( ports = jack_get_ports ( pJackClient, NULL, NULL,
|
|
|
|
JackPortIsPhysical | JackPortIsOutput ) ) == NULL )
|
|
|
|
{
|
|
|
|
throw CGenErr ( "Cannot find any physical capture ports" );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !ports[1] )
|
|
|
|
{
|
|
|
|
throw CGenErr ( "Cannot find enough physical capture ports" );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( jack_connect ( pJackClient, ports[0], jack_port_name ( input_port_left ) ) )
|
|
|
|
{
|
|
|
|
throw CGenErr ( "Cannot connect input ports" );
|
|
|
|
}
|
|
|
|
if ( jack_connect ( pJackClient, ports[1], jack_port_name ( input_port_right ) ) )
|
|
|
|
{
|
|
|
|
throw CGenErr ( "Cannot connect input ports" );
|
|
|
|
}
|
|
|
|
|
|
|
|
free ( ports );
|
|
|
|
|
|
|
|
if ( ( ports = jack_get_ports ( pJackClient, NULL, NULL,
|
|
|
|
JackPortIsPhysical | JackPortIsInput ) ) == NULL )
|
|
|
|
{
|
|
|
|
throw CGenErr ( "Cannot find any physical playback ports" );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !ports[1] )
|
|
|
|
{
|
|
|
|
throw CGenErr ( "Cannot find enough physical playback ports" );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( jack_connect ( pJackClient, jack_port_name ( output_port_left ), ports[0] ) )
|
|
|
|
{
|
|
|
|
throw CGenErr ( "Cannot connect output ports" );
|
|
|
|
}
|
|
|
|
if ( jack_connect ( pJackClient, jack_port_name ( output_port_right ), ports[1] ) )
|
|
|
|
{
|
|
|
|
throw CGenErr ( "Cannot connect output ports" );
|
|
|
|
}
|
|
|
|
|
|
|
|
free ( ports );
|
|
|
|
|
|
|
|
// call base class
|
|
|
|
CSoundBase::Start();
|
2009-03-08 09:10:10 +01:00
|
|
|
}
|
|
|
|
|
2009-03-08 22:38:38 +01:00
|
|
|
void CSound::Stop()
|
2009-03-08 09:10:10 +01:00
|
|
|
{
|
2009-03-08 22:38:38 +01:00
|
|
|
// deactivate client
|
|
|
|
jack_deactivate ( pJackClient );
|
2009-03-08 09:10:10 +01:00
|
|
|
|
2009-03-28 10:05:56 +01:00
|
|
|
// unregister ports
|
|
|
|
jack_port_unregister ( pJackClient, input_port_left );
|
|
|
|
jack_port_unregister ( pJackClient, input_port_right );
|
|
|
|
jack_port_unregister ( pJackClient, output_port_left );
|
|
|
|
jack_port_unregister ( pJackClient, output_port_right );
|
|
|
|
|
2009-03-08 22:38:38 +01:00
|
|
|
// call base class
|
|
|
|
CSoundBase::Stop();
|
2009-03-08 09:10:10 +01:00
|
|
|
}
|
|
|
|
|
2009-03-08 22:38:38 +01:00
|
|
|
int CSound::Init ( const int iNewPrefMonoBufferSize )
|
2009-03-08 09:10:10 +01:00
|
|
|
{
|
2009-03-10 19:39:29 +01:00
|
|
|
// try setting buffer size
|
|
|
|
// TODO seems not to work! -> no audio after this operation!
|
|
|
|
//jack_set_buffer_size ( pJackClient, iNewPrefMonoBufferSize );
|
2009-03-08 09:10:10 +01:00
|
|
|
|
2009-03-09 19:06:27 +01:00
|
|
|
// get actual buffer size
|
|
|
|
iJACKBufferSizeMono = jack_get_buffer_size ( pJackClient );
|
2009-03-08 09:10:10 +01:00
|
|
|
|
2009-03-08 22:38:38 +01:00
|
|
|
// init base clasee
|
|
|
|
CSoundBase::Init ( iJACKBufferSizeMono );
|
|
|
|
|
|
|
|
// set internal buffer size value and calculate stereo buffer size
|
|
|
|
iJACKBufferSizeStero = 2 * iJACKBufferSizeMono;
|
|
|
|
|
|
|
|
// create memory for intermediate audio buffer
|
|
|
|
vecsTmpAudioSndCrdStereo.Init ( iJACKBufferSizeStero );
|
|
|
|
|
2009-03-09 23:52:28 +01:00
|
|
|
return iJACKBufferSizeMono;
|
2009-03-08 09:10:10 +01:00
|
|
|
}
|
|
|
|
|
2009-03-08 22:38:38 +01:00
|
|
|
|
2009-03-08 09:10:10 +01:00
|
|
|
// JACK callbacks --------------------------------------------------------------
|
|
|
|
int CSound::process ( jack_nframes_t nframes, void* arg )
|
|
|
|
{
|
2009-03-08 22:38:38 +01:00
|
|
|
int i;
|
2009-03-08 09:10:10 +01:00
|
|
|
CSound* pSound = reinterpret_cast<CSound*> ( arg );
|
|
|
|
|
2009-03-10 18:33:12 +01:00
|
|
|
// get input data pointer
|
2009-03-08 22:38:38 +01:00
|
|
|
jack_default_audio_sample_t* in_left =
|
2009-03-10 18:33:12 +01:00
|
|
|
(jack_default_audio_sample_t*) jack_port_get_buffer (
|
|
|
|
pSound->input_port_left, nframes );
|
2009-03-08 09:10:10 +01:00
|
|
|
|
2009-03-08 22:38:38 +01:00
|
|
|
jack_default_audio_sample_t* in_right =
|
2009-03-10 18:33:12 +01:00
|
|
|
(jack_default_audio_sample_t*) jack_port_get_buffer (
|
|
|
|
pSound->input_port_right, nframes );
|
2009-03-08 22:38:38 +01:00
|
|
|
|
|
|
|
// copy input data
|
|
|
|
for ( i = 0; i < pSound->iJACKBufferSizeMono; i++ )
|
|
|
|
{
|
2009-03-09 23:52:28 +01:00
|
|
|
pSound->vecsTmpAudioSndCrdStereo[2 * i] = (short) ( in_left[i] * _MAXSHORT );
|
|
|
|
pSound->vecsTmpAudioSndCrdStereo[2 * i + 1] = (short) ( in_right[i] * _MAXSHORT );
|
2009-03-08 22:38:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// call processing callback function
|
2009-03-10 18:33:12 +01:00
|
|
|
pSound->ProcessCallback ( pSound->vecsTmpAudioSndCrdStereo );
|
2009-03-08 09:10:10 +01:00
|
|
|
|
2009-03-10 18:33:12 +01:00
|
|
|
// get output data pointer
|
2009-03-08 22:38:38 +01:00
|
|
|
jack_default_audio_sample_t* out_left =
|
2009-03-10 18:33:12 +01:00
|
|
|
(jack_default_audio_sample_t*) jack_port_get_buffer (
|
|
|
|
pSound->output_port_left, nframes );
|
2009-03-08 09:10:10 +01:00
|
|
|
|
2009-03-08 22:38:38 +01:00
|
|
|
jack_default_audio_sample_t* out_right =
|
2009-03-10 18:33:12 +01:00
|
|
|
(jack_default_audio_sample_t*) jack_port_get_buffer (
|
|
|
|
pSound->output_port_right, nframes );
|
2009-03-08 22:38:38 +01:00
|
|
|
|
|
|
|
// copy output data
|
|
|
|
for ( i = 0; i < pSound->iJACKBufferSizeMono; i++ )
|
|
|
|
{
|
2009-03-09 23:52:28 +01:00
|
|
|
out_left[i] = (jack_default_audio_sample_t)
|
|
|
|
pSound->vecsTmpAudioSndCrdStereo[2 * i] / _MAXSHORT;
|
|
|
|
|
|
|
|
out_right[i] = (jack_default_audio_sample_t)
|
|
|
|
pSound->vecsTmpAudioSndCrdStereo[2 * i + 1] / _MAXSHORT;
|
2009-03-08 22:38:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0; // zero on success, non-zero on error
|
|
|
|
}
|
2009-03-10 18:33:12 +01:00
|
|
|
|
|
|
|
int CSound::bufferSizeCallback ( jack_nframes_t nframes, void *arg )
|
|
|
|
{
|
|
|
|
CSound* pSound = reinterpret_cast<CSound*> ( arg );
|
|
|
|
|
2009-03-12 12:56:15 +01:00
|
|
|
pSound->EmitReinitRequestSignal();
|
2009-03-10 18:33:12 +01:00
|
|
|
|
|
|
|
return 0; // zero on success, non-zero on error
|
|
|
|
}
|
2009-03-12 12:56:15 +01:00
|
|
|
|
|
|
|
void CSound::shutdownCallback ( void *arg )
|
|
|
|
{
|
|
|
|
// without a Jack server, our software makes no sense to run, throw
|
|
|
|
// error message
|
|
|
|
throw CGenErr ( "Jack server was shut down" );
|
|
|
|
}
|
2009-03-08 09:10:10 +01:00
|
|
|
# else
|
2009-02-15 19:11:15 +01:00
|
|
|
// Wave in *********************************************************************
|
2009-02-23 21:13:03 +01:00
|
|
|
void CSound::InitRecording()
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
2009-02-15 19:11:15 +01:00
|
|
|
// if recording device was already open, close it first
|
2006-12-07 19:57:26 +01:00
|
|
|
if ( rhandle != NULL )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2006-12-07 19:57:26 +01:00
|
|
|
snd_pcm_close ( rhandle );
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 */
|
2009-02-15 19:11:15 +01:00
|
|
|
// either "hw:0,0" or "plughw:0,0"
|
2006-12-18 15:39:33 +01:00
|
|
|
if ( err = snd_pcm_open ( &rhandle, "hw:0,0", SND_PCM_STREAM_CAPTURE, 0 ) != 0 )
|
|
|
|
{
|
|
|
|
qDebug ( "open error: %s", snd_strerror ( err ) );
|
|
|
|
}
|
|
|
|
|
2009-02-15 19:11:15 +01:00
|
|
|
// recording should be blocking
|
2006-12-18 15:39:33 +01:00
|
|
|
if ( err = snd_pcm_nonblock ( rhandle, FALSE ) != 0 )
|
|
|
|
{
|
|
|
|
qDebug ( "cannot set blocking: %s", snd_strerror ( err ) );
|
|
|
|
}
|
|
|
|
|
2009-02-15 19:11:15 +01:00
|
|
|
// set hardware parameters
|
2006-12-18 15:39:33 +01:00
|
|
|
SetHWParams ( rhandle, iBufferSizeIn, iCurPeriodSizeIn );
|
|
|
|
|
|
|
|
// start record
|
|
|
|
snd_pcm_reset ( rhandle );
|
|
|
|
snd_pcm_start ( rhandle );
|
|
|
|
|
|
|
|
qDebug ( "alsa init record done" );
|
|
|
|
}
|
|
|
|
|
2008-10-31 21:27:55 +01:00
|
|
|
bool CSound::Read ( CVector<short>& psData )
|
2006-10-10 21:26:36 +02:00
|
|
|
{
|
2006-12-07 19:57:26 +01:00
|
|
|
int ret;
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2009-02-15 19:11:15 +01:00
|
|
|
// check if device must be opened or reinitialized
|
2008-10-31 21:27:55 +01:00
|
|
|
if ( bChangParamIn == true )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2009-02-23 21:13:03 +01:00
|
|
|
InitRecording();
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2009-02-15 19:11:15 +01:00
|
|
|
// reset flag
|
2006-12-18 15:39:33 +01:00
|
|
|
bChangParamIn = false;
|
|
|
|
}
|
|
|
|
|
2008-10-31 21:27:55 +01:00
|
|
|
ret = snd_pcm_readi ( rhandle, &psData[0], iBufferSizeIn );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
|
|
|
if ( ret < 0 )
|
|
|
|
{
|
|
|
|
if ( ret == -EPIPE )
|
|
|
|
{
|
|
|
|
// under-run
|
2006-12-07 19:57:26 +01:00
|
|
|
qDebug ( "rprepare" );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
|
|
|
ret = snd_pcm_prepare ( rhandle );
|
|
|
|
|
2006-12-07 19:57:26 +01:00
|
|
|
if ( ret < 0 )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2006-12-07 19:57:26 +01:00
|
|
|
qDebug ( "Can't recover from underrun, prepare failed: %s", snd_strerror ( ret ) );
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = snd_pcm_start ( rhandle );
|
|
|
|
|
2008-10-31 21:27:55 +01:00
|
|
|
if ( ret < 0 )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2006-12-07 19:57:26 +01:00
|
|
|
qDebug ( "Can't recover from underrun, start failed: %s", snd_strerror ( ret ) );
|
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
else if ( ret == -ESTRPIPE )
|
|
|
|
{
|
|
|
|
qDebug ( "strpipe" );
|
|
|
|
|
|
|
|
// wait until the suspend flag is released
|
2006-12-07 19:57:26 +01:00
|
|
|
while ( ( ret = snd_pcm_resume ( rhandle ) ) == -EAGAIN )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2006-12-07 19:57:26 +01:00
|
|
|
sleep ( 1 );
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ret < 0 )
|
|
|
|
{
|
|
|
|
ret = snd_pcm_prepare ( rhandle );
|
|
|
|
|
2009-02-16 21:11:35 +01:00
|
|
|
if ( ret < 0 )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2006-12-07 19:57:26 +01:00
|
|
|
qDebug ( "Can't recover from suspend, prepare failed: %s", snd_strerror ( ret ) );
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
throw CGenErr ( "CSound:Read" );
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qDebug ( "CSound::Read: %s", snd_strerror ( ret ) );
|
|
|
|
throw CGenErr ( "CSound:Read" );
|
|
|
|
}
|
|
|
|
}
|
2006-12-07 19:57:26 +01:00
|
|
|
else
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2006-12-07 19:57:26 +01:00
|
|
|
return false;
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-15 19:11:15 +01:00
|
|
|
// Wave out ********************************************************************
|
2009-02-23 21:13:03 +01:00
|
|
|
void CSound::InitPlayback()
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
// if playback device was already open, close it first
|
2006-12-07 19:57:26 +01:00
|
|
|
if ( phandle != NULL )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2006-12-07 19:57:26 +01:00
|
|
|
snd_pcm_close ( phandle );
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// playback device (either "hw:0,0" or "plughw:0,0")
|
2006-12-07 19:57:26 +01:00
|
|
|
if ( err = snd_pcm_open ( &phandle, "hw:0,0",
|
2009-02-16 21:11:35 +01:00
|
|
|
SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK ) != 0 )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
|
|
|
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 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// set hardware parameters
|
2006-12-07 19:57:26 +01:00
|
|
|
SetHWParams ( phandle, iBufferSizeOut, iCurPeriodSizeOut );
|
2006-10-03 23:16:23 +02:00
|
|
|
|
2006-12-18 15:39:33 +01:00
|
|
|
// start playback
|
2006-12-07 19:57:26 +01:00
|
|
|
snd_pcm_start ( phandle );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
|
|
|
qDebug ( "alsa init playback done" );
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSound::Write ( CVector<short>& psData )
|
|
|
|
{
|
2008-10-31 21:27:55 +01:00
|
|
|
int size = iBufferSizeOut;
|
2006-12-07 19:57:26 +01:00
|
|
|
int start = 0;
|
2006-12-18 15:39:33 +01:00
|
|
|
int ret;
|
|
|
|
|
2008-10-31 21:27:55 +01:00
|
|
|
// check if device must be opened or reinitialized
|
2006-12-18 15:39:33 +01:00
|
|
|
if ( bChangParamOut == true )
|
|
|
|
{
|
2009-02-23 21:13:03 +01:00
|
|
|
InitPlayback();
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2008-10-31 21:27:55 +01:00
|
|
|
// reset flag
|
2006-12-18 15:39:33 +01:00
|
|
|
bChangParamOut = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ( size )
|
|
|
|
{
|
|
|
|
ret = snd_pcm_writei ( phandle, &psData[start], size );
|
|
|
|
|
|
|
|
if ( ret < 0 )
|
|
|
|
{
|
|
|
|
if ( ret == -EPIPE )
|
2006-12-07 19:57:26 +01:00
|
|
|
{
|
2008-10-31 21:27:55 +01:00
|
|
|
// under-run
|
2006-12-07 19:57:26 +01:00
|
|
|
qDebug ( "wunderrun" );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2006-12-07 19:57:26 +01:00
|
|
|
ret = snd_pcm_prepare ( phandle );
|
2006-10-03 23:16:23 +02:00
|
|
|
|
2006-12-07 19:57:26 +01:00
|
|
|
if ( ret < 0 )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2006-12-07 19:57:26 +01:00
|
|
|
qDebug ( "Can't recover from underrun, prepare failed: %s", snd_strerror ( ret ) );
|
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
continue;
|
|
|
|
}
|
2006-12-07 19:57:26 +01:00
|
|
|
else if ( ret == -EAGAIN )
|
|
|
|
{
|
2009-02-16 21:11:35 +01:00
|
|
|
if ( ( ret = snd_pcm_wait ( phandle, 1000 ) ) < 0 )
|
2006-12-07 19:57:26 +01:00
|
|
|
{
|
|
|
|
qDebug ( "poll failed (%s)", snd_strerror ( ret ) );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
else if ( ret == -ESTRPIPE )
|
|
|
|
{
|
2009-02-16 21:11:35 +01:00
|
|
|
qDebug ( "wstrpipe" );
|
2006-01-28 12:29:22 +01:00
|
|
|
|
2008-10-31 21:27:55 +01:00
|
|
|
// wait until the suspend flag is released
|
2009-02-16 21:11:35 +01:00
|
|
|
while ( ( ret = snd_pcm_resume ( phandle ) ) == -EAGAIN )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2009-02-16 21:11:35 +01:00
|
|
|
sleep ( 1 );
|
2006-12-07 19:57:26 +01:00
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
|
|
|
|
if ( ret < 0 )
|
|
|
|
{
|
2006-12-07 19:57:26 +01:00
|
|
|
ret = snd_pcm_prepare ( phandle );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
2006-12-07 19:57:26 +01:00
|
|
|
if ( ret < 0 )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2006-12-07 19:57:26 +01:00
|
|
|
qDebug ( "Can't recover from suspend, prepare failed: %s", snd_strerror ( ret ) );
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qDebug ( "Write error: %s", snd_strerror ( ret ) );
|
|
|
|
}
|
|
|
|
break; // skip one period
|
|
|
|
}
|
|
|
|
|
2009-02-16 21:11:35 +01:00
|
|
|
if ( ret > 0 )
|
|
|
|
{
|
|
|
|
size -= ret;
|
|
|
|
start += ret;
|
|
|
|
}
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-15 19:11:15 +01:00
|
|
|
// Common ***********************************************************************
|
2009-03-03 14:37:28 +01:00
|
|
|
bool CSound::SetHWParams ( snd_pcm_t* handle, const int iDesiredBufferSize,
|
2008-10-31 21:27:55 +01:00
|
|
|
const int iNumPeriodBlocks )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2008-10-31 21:27:55 +01:00
|
|
|
int err;
|
|
|
|
snd_pcm_hw_params_t* hwparams;
|
2006-12-18 15:39:33 +01:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2006-12-07 19:57:26 +01:00
|
|
|
// restrict a configuration space to contain only one access type:
|
2006-12-18 15:39:33 +01:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2006-12-07 19:57:26 +01:00
|
|
|
// restrict a configuration space to contain only one format:
|
2006-12-18 15:39:33 +01:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2006-12-07 19:57:26 +01:00
|
|
|
// restrict a configuration space to contain only one channels count:
|
2006-12-18 15:39:33 +01:00
|
|
|
// set the count of channels (usually stereo, 2 channels)
|
2009-02-16 21:11:35 +01:00
|
|
|
if ( err = snd_pcm_hw_params_set_channels ( handle, hwparams, NUM_IN_OUT_CHANNELS ) < 0 )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
|
|
|
qDebug ( "Channels count (%i) not available s: %s", NUM_IN_OUT_CHANNELS, snd_strerror ( err ) );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-12-07 19:57:26 +01:00
|
|
|
// restrict a configuration space to have rate nearest to a target:
|
2006-12-18 15:39:33 +01:00
|
|
|
// set the sample-rate
|
2009-07-26 09:29:32 +02:00
|
|
|
unsigned int rrate = SYSTEM_SAMPLE_RATE;
|
2006-12-18 15:39:33 +01:00
|
|
|
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;
|
|
|
|
}
|
2009-07-26 09:29:32 +02:00
|
|
|
if ( rrate != SYSTEM_SAMPLE_RATE ) // check if rate is possible
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
|
|
|
qDebug ( "Rate doesn't match (requested %iHz, get %iHz)", rrate, err );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-12-07 19:57:26 +01:00
|
|
|
// set the period size
|
2009-03-03 14:37:28 +01:00
|
|
|
snd_pcm_uframes_t PeriodSize = iDesiredBufferSize;
|
2006-12-18 15:39:33 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-03-03 14:37:28 +01:00
|
|
|
// set the buffer size and period size
|
|
|
|
snd_pcm_uframes_t BufferFrames = iDesiredBufferSize * iNumPeriodBlocks;
|
|
|
|
if ( err = snd_pcm_hw_params_set_buffer_size_near ( handle, hwparams, &BufferFrames ) < 0 )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2009-03-03 14:37:28 +01:00
|
|
|
qDebug ( "cannot set buffer size (%s)\n", snd_strerror ( err ) );
|
2006-12-18 15:39:33 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-10-31 21:27:55 +01:00
|
|
|
// check period and buffer size
|
2006-12-18 15:39:33 +01:00
|
|
|
snd_pcm_uframes_t period_size;
|
2008-10-31 21:27:55 +01:00
|
|
|
err = snd_pcm_hw_params_get_period_size ( hwparams, &period_size, 0 );
|
|
|
|
if ( err < 0 )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2009-03-03 14:37:28 +01:00
|
|
|
qDebug ( "Unable to get period size: %s\n", snd_strerror ( err ) );
|
|
|
|
}
|
|
|
|
qDebug ( "frame size: %d (desired: %d)", (int) period_size, iDesiredBufferSize );
|
|
|
|
|
|
|
|
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: %s\n", snd_strerror ( err ) );
|
2006-10-03 23:16:23 +02:00
|
|
|
}
|
2009-03-03 14:37:28 +01:00
|
|
|
qDebug ( "buffer size: %d (desired: %d)", (int) buffer_size, iDesiredBufferSize * iNumPeriodBlocks );
|
2006-12-18 15:39:33 +01:00
|
|
|
|
|
|
|
|
2009-03-03 14:37:28 +01:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2008-10-31 21:27:55 +01:00
|
|
|
// clean-up
|
2006-12-18 15:39:33 +01:00
|
|
|
snd_pcm_hw_params_free ( hwparams );
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-10-31 21:27:55 +01:00
|
|
|
void CSound::Close()
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2008-10-31 21:27:55 +01:00
|
|
|
// read
|
2006-12-07 19:57:26 +01:00
|
|
|
if ( rhandle != NULL )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2006-12-07 19:57:26 +01:00
|
|
|
snd_pcm_close ( rhandle );
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rhandle = NULL;
|
|
|
|
|
2008-10-31 21:27:55 +01:00
|
|
|
// playback
|
2006-12-07 19:57:26 +01:00
|
|
|
if ( phandle != NULL )
|
2006-12-18 15:39:33 +01:00
|
|
|
{
|
2006-12-07 19:57:26 +01:00
|
|
|
snd_pcm_close ( phandle );
|
2006-12-18 15:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
phandle = NULL;
|
2006-01-28 12:29:22 +01:00
|
|
|
}
|
2009-03-08 09:10:10 +01:00
|
|
|
# endif // USE_JACK
|
|
|
|
#endif // WITH_SOUND
|