Commit 5d8b564a authored by sletz's avatar sletz
Browse files

Major cleanup in adapter code.

git-svn-id: http://subversion.jackaudio.org/jack/jack2/trunk/jackmp@3360 0c269be4-1314-0410-8aa9-9f06e86f4224
parent 6eda6fb3
......@@ -22,6 +22,10 @@ Michael Voigt
---------------------------
Jackdmp changes log
---------------------------
2009-02-25 Stephane Letz <letz@grame.fr>
* Major cleanup in adapter code.
2009-02-25 Stephane Letz <letz@grame.fr>
......@@ -35,7 +39,7 @@ Michael Voigt
* Another fix in systemdeps.h and types.h: jack_time_t now uniquely defined in types.h.
* Move generic code and data in JackNetInterface and JackNetMasterInterface classes.
* First version of D-Bus based audio device rerservation.
* First version of D-Bus based audio device reservation.
2009-02-20 Stephane Letz <letz@grame.fr>
......
......@@ -18,7 +18,6 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "JackAudioAdapter.h"
#include "JackLibSampleRateResampler.h"
#include "JackError.h"
#include "JackCompilerDeps.h"
#include "JackTools.h"
......@@ -34,40 +33,22 @@ namespace Jack
{
//static methods ***********************************************************
int JackAudioAdapter::Process ( jack_nframes_t frames, void* arg )
int JackAudioAdapter::Process (jack_nframes_t frames, void* arg)
{
JackAudioAdapter* adapter = static_cast<JackAudioAdapter*> ( arg );
float* buffer;
bool failure = false;
int i;
if ( !adapter->fAudioAdapter->IsRunning() )
JackAudioAdapter* adapter = static_cast<JackAudioAdapter*>(arg);
if (!adapter->fAudioAdapter->IsRunning())
return 0;
// DLL
adapter->fAudioAdapter->SetCallbackTime (GetMicroSeconds());
// Push/pull from ringbuffer
for ( i = 0; i < adapter->fCaptureChannels; i++ )
{
buffer = static_cast<float*> ( jack_port_get_buffer ( adapter->fCapturePortList[i], frames ) );
if ( adapter->fCaptureRingBuffer[i]->Read ( buffer, frames ) < frames )
failure = true;
float* inputBuffer[adapter->fAudioAdapter->GetInputs()];
float* outputBuffer[adapter->fAudioAdapter->GetOutputs()];
for (int i = 0; i < adapter->fAudioAdapter->GetInputs(); i++) {
inputBuffer[i] = (float*)jack_port_get_buffer(adapter->fCapturePortList[i], frames);
}
for ( i = 0; i < adapter->fPlaybackChannels; i++ )
{
buffer = static_cast<float*> ( jack_port_get_buffer ( adapter->fPlaybackPortList[i], frames ) );
if ( adapter->fPlaybackRingBuffer[i]->Write ( buffer, frames ) < frames )
failure = true;
}
// Reset all ringbuffers in case of failure
if ( failure )
{
jack_error ( "JackCallbackAudioAdapter::Process ringbuffer failure... reset" );
adapter->Reset();
for (int i = 0; i < adapter->fAudioAdapter->GetOutputs(); i++) {
outputBuffer[i] = (float*)jack_port_get_buffer(adapter->fPlaybackPortList[i], frames);
}
adapter->fAudioAdapter->PullAndPush(inputBuffer, outputBuffer, frames);
return 0;
}
......@@ -91,24 +72,15 @@ namespace Jack
JackAudioAdapter::~JackAudioAdapter()
{
// When called, Close has already been used for the client, thus ports are already unregistered.
int i;
for ( i = 0; i < fCaptureChannels; i++ )
delete ( fCaptureRingBuffer[i] );
for ( i = 0; i < fPlaybackChannels; i++ )
delete ( fPlaybackRingBuffer[i] );
delete[] fCaptureRingBuffer;
delete[] fPlaybackRingBuffer;
delete fAudioAdapter;
}
void JackAudioAdapter::FreePorts()
{
int i;
for ( i = 0; i < fCaptureChannels; i++ )
for (int i = 0; i < fAudioAdapter->GetInputs(); i++ )
if ( fCapturePortList[i] )
jack_port_unregister ( fJackClient, fCapturePortList[i] );
for ( i = 0; i < fCaptureChannels; i++ )
for (int i = 0; i < fAudioAdapter->GetOutputs(); i++ )
if ( fPlaybackPortList[i] )
jack_port_unregister ( fJackClient, fPlaybackPortList[i] );
......@@ -118,52 +90,30 @@ namespace Jack
void JackAudioAdapter::Reset()
{
int i;
for ( i = 0; i < fCaptureChannels; i++ )
fCaptureRingBuffer[i]->Reset();
for ( i = 0; i < fPlaybackChannels; i++ )
fPlaybackRingBuffer[i]->Reset();
fAudioAdapter->Reset();
}
int JackAudioAdapter::Open()
{
int i;
char name[32];
fCaptureChannels = fAudioAdapter->GetInputs();
fPlaybackChannels = fAudioAdapter->GetOutputs();
jack_log ( "JackAudioAdapter::Open fCaptureChannels %d fPlaybackChannels %d", fCaptureChannels, fPlaybackChannels );
//ringbuffers
fCaptureRingBuffer = new JackResampler*[fCaptureChannels];
fPlaybackRingBuffer = new JackResampler*[fPlaybackChannels];
for ( i = 0; i < fCaptureChannels; i++ )
fCaptureRingBuffer[i] = new JackLibSampleRateResampler(fAudioAdapter->GetQuality(), fAudioAdapter->GetRingbufferSize());
for ( i = 0; i < fPlaybackChannels; i++ )
fPlaybackRingBuffer[i] = new JackLibSampleRateResampler(fAudioAdapter->GetQuality(), fAudioAdapter->GetRingbufferSize());
fAudioAdapter->SetRingBuffers ( fCaptureRingBuffer, fPlaybackRingBuffer );
if (fCaptureChannels > 0)
jack_log ( "ReadSpace = %ld", fCaptureRingBuffer[0]->ReadSpace() );
if (fPlaybackChannels > 0)
jack_log ( "WriteSpace = %ld", fPlaybackRingBuffer[0]->WriteSpace() );
jack_log("JackAudioAdapter::Open fCaptureChannels %d fPlaybackChannels %d", fAudioAdapter->GetInputs(), fAudioAdapter->GetOutputs());
fAudioAdapter->Create();
//jack ports
fCapturePortList = new jack_port_t* [fCaptureChannels];
fPlaybackPortList = new jack_port_t* [fPlaybackChannels];
fCapturePortList = new jack_port_t*[fAudioAdapter->GetInputs()];
fPlaybackPortList = new jack_port_t*[fAudioAdapter->GetOutputs()];
for ( i = 0; i < fCaptureChannels; i++ )
for (int i = 0; i < fAudioAdapter->GetInputs(); i++)
{
sprintf ( name, "capture_%d", i+1 );
if ( ( fCapturePortList[i] = jack_port_register ( fJackClient, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0 ) ) == NULL )
sprintf(name, "capture_%d", i + 1);
if ((fCapturePortList[i] = jack_port_register(fJackClient, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0)) == NULL)
goto fail;
}
for ( i = 0; i < fPlaybackChannels; i++ )
for (int i = 0; i < fAudioAdapter->GetOutputs(); i++)
{
sprintf ( name, "playback_%d", i+1 );
if ( ( fPlaybackPortList[i] = jack_port_register ( fJackClient, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0 ) ) == NULL )
sprintf(name, "playback_%d", i + 1);
if ((fPlaybackPortList[i] = jack_port_register(fJackClient, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0 )) == NULL)
goto fail;
}
......@@ -177,17 +127,20 @@ namespace Jack
if ( jack_activate ( fJackClient ) < 0 )
goto fail;
//ringbuffers and jack clients are ok, we can now open the adapter driver interface
// Ring buffer are now allocated..
return fAudioAdapter->Open();
fail:
FreePorts();
fAudioAdapter->Destroy();
return -1;
}
int JackAudioAdapter::Close()
{
return fAudioAdapter->Close();
fAudioAdapter->Close();
fAudioAdapter->Destroy();
return 0;
}
} //namespace
......@@ -37,12 +37,6 @@ namespace Jack
static int BufferSize ( jack_nframes_t buffer_size, void *arg );
static int SampleRate ( jack_nframes_t sample_rate, void *arg );
int fCaptureChannels;
int fPlaybackChannels;
JackResampler** fCaptureRingBuffer;
JackResampler** fPlaybackRingBuffer;
jack_port_t** fCapturePortList;
jack_port_t** fPlaybackPortList;
......
......@@ -18,6 +18,7 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "JackAudioAdapter.h"
#include "JackLibSampleRateResampler.h"
#include "JackTime.h"
#include <stdio.h>
......@@ -96,13 +97,12 @@ namespace Jack
}
#endif
void JackAudioAdapterInterface::ResetRingBuffers()
{
int i;
for (i = 0; i < fCaptureChannels; i++)
for (int i = 0; i < fCaptureChannels; i++)
fCaptureRingBuffer[i]->Reset();
for (i = 0; i < fPlaybackChannels; i++)
for (int i = 0; i < fPlaybackChannels; i++)
fPlaybackRingBuffer[i]->Reset();
}
......@@ -110,17 +110,14 @@ namespace Jack
{
jack_time_t time = GetMicroSeconds();
if ( !fRunning )
{
if (!fRunning) {
// Init DLL
fRunning = true;
fHostDLL.Init ( time );
fAdaptedDLL.Init ( time );
fHostDLL.Init(time);
fAdaptedDLL.Init(time);
frame1 = 1;
frame2 = 1;
}
else
{
} else {
// DLL
fAdaptedDLL.IncFrame(time);
jack_nframes_t time1 = fHostDLL.Time2Frames(time);
......@@ -131,24 +128,47 @@ namespace Jack
long(time1), long(time2), double(time1) / double(time2), double(time2) / double(time1));
}
}
void JackAudioAdapterInterface::Reset()
{
ResetRingBuffers();
fRunning = false;
}
int JackAudioAdapterInterface::Open()
void JackAudioAdapterInterface::Create()
{
return 0;
//ringbuffers
fCaptureRingBuffer = new JackResampler*[fCaptureChannels];
fPlaybackRingBuffer = new JackResampler*[fPlaybackChannels];
for (int i = 0; i < fCaptureChannels; i++ )
fCaptureRingBuffer[i] = new JackLibSampleRateResampler(fQuality, fRingbufferSize);
for (int i = 0; i < fPlaybackChannels; i++ )
fPlaybackRingBuffer[i] = new JackLibSampleRateResampler(fQuality, fRingbufferSize);
if (fCaptureChannels > 0)
jack_log("ReadSpace = %ld", fCaptureRingBuffer[0]->ReadSpace());
if (fPlaybackChannels > 0)
jack_log("WriteSpace = %ld", fPlaybackRingBuffer[0]->WriteSpace());
}
int JackAudioAdapterInterface::Close()
void JackAudioAdapterInterface::Destroy()
{
return 0;
for (int i = 0; i < fCaptureChannels; i++ )
delete ( fCaptureRingBuffer[i] );
for (int i = 0; i < fPlaybackChannels; i++ )
delete ( fPlaybackRingBuffer[i] );
delete[] fCaptureRingBuffer;
delete[] fPlaybackRingBuffer;
}
void JackAudioAdapterInterface::PushAndPull(float** inputBuffer, float** outputBuffer, unsigned int inNumberFrames)
{
bool failure = false;
jack_time_t time1, time2;
ResampleFactor(time1, time2);
// Push/pull from ringbuffer
for (int i = 0; i < fCaptureChannels; i++) {
fCaptureRingBuffer[i]->SetRatio(time1, time2);
if (fCaptureRingBuffer[i]->WriteResample(inputBuffer[i], inNumberFrames) < inNumberFrames)
......@@ -173,6 +193,27 @@ namespace Jack
}
}
void JackAudioAdapterInterface::PullAndPush(float** inputBuffer, float** outputBuffer, unsigned int inNumberFrames)
{
bool failure = false;
SetCallbackTime(GetMicroSeconds());
// Push/pull from ringbuffer
for (int i = 0; i < fCaptureChannels; i++) {
if (fCaptureRingBuffer[i]->Read(inputBuffer[i], inNumberFrames) < inNumberFrames)
failure = true;
}
for (int i = 0; i < fPlaybackChannels; i++) {
if (fPlaybackRingBuffer[i]->Write(outputBuffer[i], inNumberFrames) < inNumberFrames)
failure = true;
}
// Reset all ringbuffers in case of failure
if (failure) {
jack_error("JackCallbackAudioAdapter::PullAndPush ringbuffer failure... reset");
Reset();
}
}
} // namespace
......@@ -93,56 +93,54 @@ namespace Jack
unsigned int fRingbufferSize;
bool fRunning;
void ResetRingBuffers();
void ResampleFactor ( jack_time_t& frame1, jack_time_t& frame2 );
virtual void SetCallbackTime ( jack_time_t callback_usec )
{
fHostDLL.IncFrame ( callback_usec );
}
public:
JackAudioAdapterInterface ( jack_nframes_t buffer_size, jack_nframes_t sample_rate ) :
fCaptureChannels ( 0 ),
fPlaybackChannels ( 0 ),
fHostBufferSize ( buffer_size ),
fHostSampleRate ( sample_rate ),
fAdaptedBufferSize ( buffer_size),
fAdaptedSampleRate ( sample_rate ),
fHostDLL ( buffer_size, sample_rate ),
fAdaptedDLL ( buffer_size, sample_rate ),
fQuality(0), fRingbufferSize(DEFAULT_RB_SIZE),
fRunning(false)
JackAudioAdapterInterface ( jack_nframes_t buffer_size, jack_nframes_t sample_rate ):
fCaptureChannels ( 0 ),
fPlaybackChannels ( 0 ),
fHostBufferSize ( buffer_size ),
fHostSampleRate ( sample_rate ),
fAdaptedBufferSize ( buffer_size),
fAdaptedSampleRate ( sample_rate ),
fHostDLL ( buffer_size, sample_rate ),
fAdaptedDLL ( buffer_size, sample_rate ),
fCaptureRingBuffer(NULL), fPlaybackRingBuffer(NULL),
fQuality(0), fRingbufferSize(DEFAULT_RB_SIZE),
fRunning(false)
{}
virtual ~JackAudioAdapterInterface()
{}
void SetRingBuffers ( JackResampler** input, JackResampler** output )
{
fCaptureRingBuffer = input;
fPlaybackRingBuffer = output;
}
bool IsRunning()
{
return fRunning;
}
virtual void Reset()
{
fRunning = false;
}
void ResetRingBuffers();
virtual void Reset();
void Create();
void Destroy();
unsigned int GetRingbufferSize()
virtual int Open()
{
return fRingbufferSize;
return 0;
}
unsigned int GetQuality()
virtual int Close()
{
return fQuality;
return 0;
}
virtual int Open();
virtual int Close();
virtual int SetHostBufferSize ( jack_nframes_t buffer_size )
{
fHostBufferSize = buffer_size;
......@@ -184,14 +182,7 @@ namespace Jack
SetAdaptedSampleRate ( sample_rate );
return 0;
}
virtual void SetCallbackTime ( jack_time_t callback_usec )
{
fHostDLL.IncFrame ( callback_usec );
}
void ResampleFactor ( jack_time_t& frame1, jack_time_t& frame2 );
void SetInputs ( int inputs )
{
jack_log ( "JackAudioAdapterInterface::SetInputs %d", inputs );
......@@ -217,6 +208,7 @@ namespace Jack
}
void PushAndPull(float** inputBuffer, float** outputBuffer, unsigned int inNumberFrames);
void PullAndPush(float** inputBuffer, float** outputBuffer, unsigned int inNumberFrames);
};
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment