2012-01-28 12:51:14 +01:00
|
|
|
/******************************************************************************\
|
2015-02-04 19:17:23 +01:00
|
|
|
* Copyright (c) 2004-2015
|
2012-01-28 12:51:14 +01:00
|
|
|
*
|
|
|
|
* Author(s):
|
|
|
|
* Volker Fischer
|
|
|
|
*
|
|
|
|
* This code is based on the simple_client example of the Jack audio interface.
|
2013-03-28 08:52:46 +01:00
|
|
|
*
|
2012-01-28 12:51:14 +01:00
|
|
|
******************************************************************************
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU General Public License as published by the Free Software
|
|
|
|
* Foundation; either version 2 of the License, or (at your option) any later
|
|
|
|
* version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
\******************************************************************************/
|
|
|
|
|
|
|
|
#include "sound.h"
|
|
|
|
|
|
|
|
#ifdef WITH_SOUND
|
|
|
|
void CSound::OpenJack()
|
|
|
|
{
|
|
|
|
jack_status_t JackStatus;
|
|
|
|
|
|
|
|
// try to become a client of the JACK server
|
2013-02-17 22:14:19 +01:00
|
|
|
pJackClient = jack_client_open ( APP_NAME, JackNullOption, &JackStatus );
|
2013-03-27 22:32:47 +01:00
|
|
|
|
2012-01-28 12:51:14 +01:00
|
|
|
if ( pJackClient == NULL )
|
|
|
|
{
|
2013-03-28 08:52:46 +01:00
|
|
|
throw CGenErr ( tr ( "The Jack server is not running. This software "
|
|
|
|
"requires a Jack server to run. Normally if the Jack server is "
|
|
|
|
"not running this software will automatically start the Jack server. "
|
|
|
|
"It seems that this auto start has not worked. Try to start the Jack "
|
|
|
|
"server manually." ) );
|
2012-01-28 12:51:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// tell the JACK server to call "process()" whenever
|
|
|
|
// there is work to be done
|
|
|
|
jack_set_process_callback ( pJackClient, process, this );
|
|
|
|
|
|
|
|
// register a "buffer size changed" callback function
|
|
|
|
jack_set_buffer_size_callback ( pJackClient, bufferSizeCallback, this );
|
|
|
|
|
|
|
|
// register shutdown callback function
|
|
|
|
jack_on_shutdown ( pJackClient, shutdownCallback, this );
|
|
|
|
|
2013-03-28 08:52:46 +01:00
|
|
|
// check sample rate, if not correct, just fire error
|
|
|
|
if ( jack_get_sample_rate ( pJackClient ) != SYSTEM_SAMPLE_RATE_HZ )
|
|
|
|
{
|
|
|
|
throw CGenErr ( tr ( "The Jack server sample rate is different from "
|
|
|
|
"the required one. The required sample rate is: <b>" ) +
|
|
|
|
QString().setNum ( SYSTEM_SAMPLE_RATE_HZ ) + tr ( " Hz</b>. You can "
|
|
|
|
"use a tool like <i><a href=""http://qjackctl.sourceforge.net"">QJackCtl</a></i> "
|
|
|
|
"to adjust the Jack server sample rate.<br>Make sure to set the "
|
|
|
|
"<b>Frames/Period</b> to a low value like <b>" ) +
|
|
|
|
QString().setNum ( SYSTEM_FRAME_SIZE_SAMPLES * FRAME_SIZE_FACTOR_PREFERRED ) +
|
|
|
|
tr ( "</b> to achieve a low delay." ) );
|
|
|
|
}
|
2012-01-28 12:51:14 +01:00
|
|
|
|
|
|
|
// create four ports (two for input, two for output -> stereo)
|
|
|
|
input_port_left = jack_port_register ( pJackClient, "input left",
|
|
|
|
JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0 );
|
|
|
|
|
|
|
|
input_port_right = jack_port_register ( pJackClient, "input right",
|
|
|
|
JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0 );
|
|
|
|
|
|
|
|
output_port_left = jack_port_register ( pJackClient, "output left",
|
|
|
|
JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0 );
|
|
|
|
|
|
|
|
output_port_right = jack_port_register ( pJackClient, "output right",
|
|
|
|
JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0 );
|
|
|
|
|
|
|
|
if ( ( input_port_left == NULL ) ||
|
|
|
|
( input_port_right == NULL ) ||
|
|
|
|
( output_port_left == NULL ) ||
|
|
|
|
( output_port_right == NULL ) )
|
|
|
|
{
|
2013-03-28 08:52:46 +01:00
|
|
|
throw CGenErr ( tr ( "The Jack port registering failed." ) );
|
2012-01-28 12:51:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const char** ports;
|
|
|
|
|
|
|
|
// tell the JACK server that we are ready to roll
|
|
|
|
if ( jack_activate ( pJackClient ) )
|
|
|
|
{
|
2013-03-28 08:52:46 +01:00
|
|
|
throw CGenErr ( tr ( "Cannot activate the Jack client." ) );
|
2012-01-28 12:51:14 +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
|
2013-12-26 16:59:59 +01:00
|
|
|
|
2013-12-27 16:34:37 +01:00
|
|
|
// try to connect physical input ports
|
2015-03-15 08:24:11 +01:00
|
|
|
if ( ( ports = jack_get_ports ( pJackClient,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
JackPortIsPhysical | JackPortIsOutput ) ) != NULL )
|
2012-01-28 12:51:14 +01:00
|
|
|
{
|
2013-12-27 16:34:37 +01:00
|
|
|
if ( jack_connect ( pJackClient, ports[0], jack_port_name ( input_port_left ) ) )
|
2013-12-26 16:59:59 +01:00
|
|
|
{
|
|
|
|
throw CGenErr ( tr ( "Cannot connect the Jack input ports" ) );
|
|
|
|
}
|
2012-01-28 12:51:14 +01:00
|
|
|
|
2013-12-27 16:34:37 +01:00
|
|
|
// before connecting the second stereo channel, check if the input is not
|
|
|
|
// mono
|
2015-03-15 08:24:11 +01:00
|
|
|
|
|
|
|
// TODO who checks if ports[1] actually exists??? I assume that if we have mono, the
|
|
|
|
// ports array is only one item long...?
|
|
|
|
|
2013-12-27 16:34:37 +01:00
|
|
|
if ( ports[1] )
|
|
|
|
{
|
|
|
|
if ( jack_connect ( pJackClient, ports[1], jack_port_name ( input_port_right ) ) )
|
|
|
|
{
|
|
|
|
throw CGenErr ( tr ( "Cannot connect the Jack input ports" ) );
|
|
|
|
}
|
|
|
|
}
|
2012-01-28 12:51:14 +01:00
|
|
|
|
2015-03-15 08:24:11 +01:00
|
|
|
// TODO shouldn't we call jack_free() function instead?
|
2013-12-27 16:34:37 +01:00
|
|
|
free ( ports );
|
2012-01-28 12:51:14 +01:00
|
|
|
}
|
2013-12-26 16:59:59 +01:00
|
|
|
|
2013-12-27 16:34:37 +01:00
|
|
|
// try to connect physical output ports
|
2015-03-15 08:24:11 +01:00
|
|
|
if ( ( ports = jack_get_ports ( pJackClient,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
JackPortIsPhysical | JackPortIsInput ) ) != NULL )
|
2012-01-28 12:51:14 +01:00
|
|
|
{
|
2013-12-27 16:34:37 +01:00
|
|
|
if ( jack_connect ( pJackClient, jack_port_name ( output_port_left ), ports[0] ) )
|
2013-12-26 16:59:59 +01:00
|
|
|
{
|
|
|
|
throw CGenErr ( tr ( "Cannot connect the Jack output ports." ) );
|
|
|
|
}
|
2012-01-28 12:51:14 +01:00
|
|
|
|
2013-12-27 16:34:37 +01:00
|
|
|
// before connecting the second stereo channel, check if the output is not
|
|
|
|
// mono
|
2015-03-15 08:24:11 +01:00
|
|
|
|
|
|
|
// TODO who checks if ports[1] actually exists??? I assume that if we have mono, the
|
|
|
|
// ports array is only one item long...?
|
|
|
|
|
2013-12-27 16:34:37 +01:00
|
|
|
if ( ports[1] )
|
|
|
|
{
|
|
|
|
if ( jack_connect ( pJackClient, jack_port_name ( output_port_right ), ports[1] ) )
|
|
|
|
{
|
|
|
|
throw CGenErr ( tr ( "Cannot connect the Jack output ports." ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-15 08:24:11 +01:00
|
|
|
// TODO shouldn't we call jack_free() function instead?
|
2013-12-27 16:34:37 +01:00
|
|
|
free ( ports );
|
|
|
|
}
|
2012-01-28 12:51:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CSound::CloseJack()
|
|
|
|
{
|
|
|
|
// deactivate client
|
|
|
|
jack_deactivate ( pJackClient );
|
|
|
|
|
|
|
|
// 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 );
|
|
|
|
|
|
|
|
// close client connection to jack server
|
|
|
|
jack_client_close ( pJackClient );
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSound::Start()
|
|
|
|
{
|
|
|
|
// call base class
|
|
|
|
CSoundBase::Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSound::Stop()
|
|
|
|
{
|
|
|
|
// call base class
|
|
|
|
CSoundBase::Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
int CSound::Init ( const int /* iNewPrefMonoBufferSize */ )
|
|
|
|
{
|
2015-03-15 08:24:11 +01:00
|
|
|
|
|
|
|
|
2012-01-28 12:51:14 +01:00
|
|
|
// try setting buffer size
|
|
|
|
// TODO seems not to work! -> no audio after this operation!
|
2015-03-15 08:24:11 +01:00
|
|
|
|
|
|
|
// Doesn't this give an infinite loop? The set buffer size function will call our
|
|
|
|
// registerd callback which calls "EmitReinitRequestSignal()". In that function
|
|
|
|
// this CSound::Init() function is called...
|
|
|
|
|
2012-01-28 12:51:14 +01:00
|
|
|
//jack_set_buffer_size ( pJackClient, iNewPrefMonoBufferSize );
|
|
|
|
|
2015-03-15 08:24:11 +01:00
|
|
|
|
2012-01-28 12:51:14 +01:00
|
|
|
// get actual buffer size
|
|
|
|
iJACKBufferSizeMono = jack_get_buffer_size ( pJackClient );
|
|
|
|
|
|
|
|
// init base class
|
|
|
|
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 );
|
|
|
|
|
|
|
|
return iJACKBufferSizeMono;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// JACK callbacks --------------------------------------------------------------
|
|
|
|
int CSound::process ( jack_nframes_t nframes, void* arg )
|
|
|
|
{
|
2014-01-03 09:56:31 +01:00
|
|
|
CSound* pSound = static_cast<CSound*> ( arg );
|
2012-01-28 12:51:14 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if ( pSound->IsRunning() )
|
|
|
|
{
|
|
|
|
// get input data pointer
|
|
|
|
jack_default_audio_sample_t* in_left =
|
|
|
|
(jack_default_audio_sample_t*) jack_port_get_buffer (
|
|
|
|
pSound->input_port_left, nframes );
|
|
|
|
|
|
|
|
jack_default_audio_sample_t* in_right =
|
|
|
|
(jack_default_audio_sample_t*) jack_port_get_buffer (
|
|
|
|
pSound->input_port_right, nframes );
|
|
|
|
|
|
|
|
// copy input data
|
2015-02-15 17:53:17 +01:00
|
|
|
if ( in_left != 0 && in_right != 0 )
|
2012-01-28 12:51:14 +01:00
|
|
|
{
|
2015-02-15 17:53:17 +01:00
|
|
|
for ( i = 0; i < pSound->iJACKBufferSizeMono; i++ )
|
|
|
|
{
|
|
|
|
pSound->vecsTmpAudioSndCrdStereo[2 * i] =
|
|
|
|
(short) ( in_left[i] * _MAXSHORT );
|
2012-01-28 12:51:14 +01:00
|
|
|
|
2015-02-15 17:53:17 +01:00
|
|
|
pSound->vecsTmpAudioSndCrdStereo[2 * i + 1] =
|
|
|
|
(short) ( in_right[i] * _MAXSHORT );
|
|
|
|
}
|
2012-01-28 12:51:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// call processing callback function
|
|
|
|
pSound->ProcessCallback ( pSound->vecsTmpAudioSndCrdStereo );
|
|
|
|
|
|
|
|
// get output data pointer
|
|
|
|
jack_default_audio_sample_t* out_left =
|
|
|
|
(jack_default_audio_sample_t*) jack_port_get_buffer (
|
|
|
|
pSound->output_port_left, nframes );
|
|
|
|
|
|
|
|
jack_default_audio_sample_t* out_right =
|
|
|
|
(jack_default_audio_sample_t*) jack_port_get_buffer (
|
|
|
|
pSound->output_port_right, nframes );
|
|
|
|
|
|
|
|
// copy output data
|
2015-02-15 17:53:17 +01:00
|
|
|
if ( out_left != 0 && out_right != 0 )
|
2012-01-28 12:51:14 +01:00
|
|
|
{
|
2015-02-15 17:53:17 +01:00
|
|
|
for ( i = 0; i < pSound->iJACKBufferSizeMono; i++ )
|
|
|
|
{
|
|
|
|
out_left[i] = (jack_default_audio_sample_t)
|
|
|
|
pSound->vecsTmpAudioSndCrdStereo[2 * i] / _MAXSHORT;
|
2012-01-28 12:51:14 +01:00
|
|
|
|
2015-02-15 17:53:17 +01:00
|
|
|
out_right[i] = (jack_default_audio_sample_t)
|
|
|
|
pSound->vecsTmpAudioSndCrdStereo[2 * i + 1] / _MAXSHORT;
|
|
|
|
}
|
2012-01-28 12:51:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// get output data pointer
|
|
|
|
jack_default_audio_sample_t* out_left =
|
|
|
|
(jack_default_audio_sample_t*) jack_port_get_buffer (
|
|
|
|
pSound->output_port_left, nframes );
|
|
|
|
|
|
|
|
jack_default_audio_sample_t* out_right =
|
|
|
|
(jack_default_audio_sample_t*) jack_port_get_buffer (
|
|
|
|
pSound->output_port_right, nframes );
|
|
|
|
|
|
|
|
// clear output data
|
2015-02-15 17:53:17 +01:00
|
|
|
if ( out_left != 0 && out_right != 0 )
|
|
|
|
{
|
|
|
|
memset ( out_left,
|
2015-03-15 08:24:11 +01:00
|
|
|
0,
|
|
|
|
sizeof ( jack_default_audio_sample_t ) * nframes );
|
2012-01-28 12:51:14 +01:00
|
|
|
|
2015-02-15 17:53:17 +01:00
|
|
|
memset ( out_right,
|
2015-03-15 08:24:11 +01:00
|
|
|
0,
|
|
|
|
sizeof ( jack_default_audio_sample_t ) * nframes );
|
2015-02-15 17:53:17 +01:00
|
|
|
}
|
2012-01-28 12:51:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0; // zero on success, non-zero on error
|
|
|
|
}
|
|
|
|
|
|
|
|
int CSound::bufferSizeCallback ( jack_nframes_t, void *arg )
|
|
|
|
{
|
2014-01-03 09:56:31 +01:00
|
|
|
CSound* pSound = static_cast<CSound*> ( arg );
|
2012-01-28 12:51:14 +01:00
|
|
|
|
|
|
|
pSound->EmitReinitRequestSignal ( RS_ONLY_RESTART_AND_INIT );
|
|
|
|
|
|
|
|
return 0; // zero on success, non-zero on error
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSound::shutdownCallback ( void* )
|
|
|
|
{
|
|
|
|
// without a Jack server, our software makes no sense to run, throw
|
|
|
|
// error message
|
2013-03-28 08:52:46 +01:00
|
|
|
throw CGenErr ( tr ( "The Jack server was shut down. This software "
|
|
|
|
"requires a Jack server to run. Try to restart the software to "
|
|
|
|
"solve the issue." ) );
|
2012-01-28 12:51:14 +01:00
|
|
|
}
|
|
|
|
#endif // WITH_SOUND
|