Commit 734e4509 authored by Stephane Letz's avatar Stephane Letz
Browse files

NetJack2 code cleanup.

parent 2b7864ad
......@@ -32,172 +32,188 @@ using namespace std;
namespace Jack
{
//static methods ***********************************************************
int JackAudioAdapter::Process (jack_nframes_t frames, void* arg)
{
JackAudioAdapter* adapter = static_cast<JackAudioAdapter*>(arg);
jack_default_audio_sample_t* inputBuffer[adapter->fAudioAdapter->GetInputs()];
jack_default_audio_sample_t* outputBuffer[adapter->fAudioAdapter->GetOutputs()];
// Always clear output
for (int i = 0; i < adapter->fAudioAdapter->GetInputs(); i++) {
#ifdef OPTIMIZED_PROTOCOL
inputBuffer[i] = (jack_port_connected(adapter->fCapturePortList[i]) > 0)
? (jack_default_audio_sample_t*)(adapter->fCapturePortList[i], frames)
: NULL;
if (inputBuffer[i]) {
memset(inputBuffer[i], 0, frames * sizeof(jack_default_audio_sample_t));
}
#else
inputBuffer[i] = (jack_default_audio_sample_t*)jack_port_get_buffer(adapter->fCapturePortList[i], frames);
memset(inputBuffer[i], 0, frames * sizeof(jack_default_audio_sample_t));
#endif
}
for (int i = 0; i < adapter->fAudioAdapter->GetOutputs(); i++) {
#ifdef OPTIMIZED_PROTOCOL
outputBuffer[i] = (jack_port_connected(fAudioCapturePorts[audio_port_index] > 0)
? (jack_default_audio_sample_t*)jack_port_get_buffer(adapter->fPlaybackPortList[i], frames)
: NULL;
#else
outputBuffer[i] = (jack_default_audio_sample_t*)jack_port_get_buffer(adapter->fPlaybackPortList[i], frames);
#endif
}
adapter->fAudioAdapter->PullAndPush(inputBuffer, outputBuffer, frames);
return 0;
int JackAudioAdapter::Process(jack_nframes_t frames, void* arg)
{
JackAudioAdapter* adapter = static_cast<JackAudioAdapter*>(arg);
jack_default_audio_sample_t* inputBuffer[adapter->fAudioAdapter->GetInputs()];
jack_default_audio_sample_t* outputBuffer[adapter->fAudioAdapter->GetOutputs()];
// Always clear output
for (int i = 0; i < adapter->fAudioAdapter->GetInputs(); i++) {
inputBuffer[i] = (jack_default_audio_sample_t*)jack_port_get_buffer(adapter->fCapturePortList[i], frames);
memset(inputBuffer[i], 0, frames * sizeof(jack_default_audio_sample_t));
}
int JackAudioAdapter::BufferSize ( jack_nframes_t buffer_size, void* arg )
{
JackAudioAdapter* adapter = static_cast<JackAudioAdapter*> ( arg );
adapter->Reset();
adapter->fAudioAdapter->SetHostBufferSize ( buffer_size );
return 0;
for (int i = 0; i < adapter->fAudioAdapter->GetOutputs(); i++) {
outputBuffer[i] = (jack_default_audio_sample_t*)jack_port_get_buffer(adapter->fPlaybackPortList[i], frames);
}
int JackAudioAdapter::SampleRate ( jack_nframes_t sample_rate, void* arg )
{
JackAudioAdapter* adapter = static_cast<JackAudioAdapter*> ( arg );
adapter->Reset();
adapter->fAudioAdapter->SetHostSampleRate ( sample_rate );
return 0;
}
adapter->fAudioAdapter->PullAndPush(inputBuffer, outputBuffer, frames);
return 0;
}
int JackAudioAdapter::BufferSize(jack_nframes_t buffer_size, void* arg)
{
JackAudioAdapter* adapter = static_cast<JackAudioAdapter*>(arg);
adapter->Reset();
adapter->fAudioAdapter->SetHostBufferSize(buffer_size);
return 0;
}
//JackAudioAdapter *********************************************************
int JackAudioAdapter::SampleRate(jack_nframes_t sample_rate, void* arg)
{
JackAudioAdapter* adapter = static_cast<JackAudioAdapter*>(arg);
adapter->Reset();
adapter->fAudioAdapter->SetHostSampleRate(sample_rate);
return 0;
}
JackAudioAdapter::JackAudioAdapter (jack_client_t* jack_client, JackAudioAdapterInterface* audio_io, const JSList* params, bool system)
:fJackClient(jack_client), fAudioAdapter(audio_io)
{
const JSList* node;
const jack_driver_param_t* param;
fAutoConnect = false;
void JackAudioAdapter::Latency(jack_latency_callback_mode_t mode, void* arg)
{
JackAudioAdapter* adapter = static_cast<JackAudioAdapter*>(arg);
for (node = params; node; node = jack_slist_next(node)) {
param = (const jack_driver_param_t*) node->data;
switch (param->character) {
case 'c':
fAutoConnect = true;
break;
}
if (mode == JackCaptureLatency) {
for (int i = 0; i < adapter->fAudioAdapter->GetInputs(); i++) {
jack_latency_range_t range;
range.min = range.max = adapter->fAudioAdapter->GetInputLatency(i);
jack_port_set_latency_range(adapter->fCapturePortList[i], JackCaptureLatency, &range);
}
} else {
for (int i = 0; i < adapter->fAudioAdapter->GetOutputs(); i++) {
jack_latency_range_t range;
range.min = range.max = adapter->fAudioAdapter->GetOutputLatency(i);
jack_port_set_latency_range(adapter->fPlaybackPortList[i], JackPlaybackLatency, &range);
}
}
}
JackAudioAdapter::~JackAudioAdapter()
{
// When called, Close has already been used for the client, thus ports are already unregistered.
delete fAudioAdapter;
JackAudioAdapter::JackAudioAdapter(jack_client_t* client, JackAudioAdapterInterface* audio_io, const JSList* params)
:fClient(client), fAudioAdapter(audio_io)
{
const JSList* node;
const jack_driver_param_t* param;
fAutoConnect = false;
for (node = params; node; node = jack_slist_next(node)) {
param = (const jack_driver_param_t*)node->data;
switch (param->character) {
case 'c':
fAutoConnect = true;
break;
}
}
}
void JackAudioAdapter::FreePorts()
{
for (int i = 0; i < fAudioAdapter->GetInputs(); i++ )
if ( fCapturePortList[i] )
jack_port_unregister ( fJackClient, fCapturePortList[i] );
for (int i = 0; i < fAudioAdapter->GetOutputs(); i++ )
if ( fPlaybackPortList[i] )
jack_port_unregister ( fJackClient, fPlaybackPortList[i] );
JackAudioAdapter::~JackAudioAdapter()
{
// When called, Close has already been used for the client, thus ports are already unregistered.
delete fAudioAdapter;
}
delete[] fCapturePortList;
delete[] fPlaybackPortList;
void JackAudioAdapter::FreePorts()
{
for (int i = 0; i < fAudioAdapter->GetInputs(); i++) {
if (fCapturePortList[i]) {
jack_port_unregister(fClient, fCapturePortList[i]);
}
}
for (int i = 0; i < fAudioAdapter->GetOutputs(); i++) {
if (fPlaybackPortList[i]) {
jack_port_unregister(fClient, fPlaybackPortList[i]);
}
}
void JackAudioAdapter::ConnectPorts()
{
const char **ports;
delete[] fCapturePortList;
delete[] fPlaybackPortList;
}
ports = jack_get_ports(fJackClient, NULL, NULL, JackPortIsPhysical | JackPortIsInput);
if (ports != NULL) {
for (int i = 0; i < fAudioAdapter->GetInputs() && ports[i]; i++) {
jack_connect(fJackClient, jack_port_name(fCapturePortList[i]), ports[i]);
}
free(ports);
}
void JackAudioAdapter::ConnectPorts()
{
const char** ports;
ports = jack_get_ports(fJackClient, NULL, NULL, JackPortIsPhysical | JackPortIsOutput);
if (ports != NULL) {
for (int i = 0; i < fAudioAdapter->GetOutputs() && ports[i]; i++) {
jack_connect(fJackClient, ports[i], jack_port_name(fPlaybackPortList[i]));
}
free(ports);
ports = jack_get_ports(fClient, NULL, NULL, JackPortIsPhysical | JackPortIsInput);
if (ports != NULL) {
for (int i = 0; i < fAudioAdapter->GetInputs() && ports[i]; i++) {
jack_connect(fClient,jack_port_name(fCapturePortList[i]), ports[i]);
}
jack_free(ports);
}
void JackAudioAdapter::Reset()
{
fAudioAdapter->Reset();
ports = jack_get_ports(fClient, NULL, NULL, JackPortIsPhysical | JackPortIsOutput);
if (ports != NULL) {
for (int i = 0; i < fAudioAdapter->GetOutputs() && ports[i]; i++) {
jack_connect(fClient, ports[i], jack_port_name(fPlaybackPortList[i]));
}
jack_free(ports);
}
}
int JackAudioAdapter::Open()
{
char name[32];
jack_log("JackAudioAdapter::Open fCaptureChannels %d fPlaybackChannels %d", fAudioAdapter->GetInputs(), fAudioAdapter->GetOutputs());
fAudioAdapter->Create();
void JackAudioAdapter::Reset()
{
fAudioAdapter->Reset();
}
//jack ports
fCapturePortList = new jack_port_t*[fAudioAdapter->GetInputs()];
fPlaybackPortList = new jack_port_t*[fAudioAdapter->GetOutputs()];
int JackAudioAdapter::Open()
{
char name[32];
jack_log("JackAudioAdapter::Open fCaptureChannels %d fPlaybackChannels %d", fAudioAdapter->GetInputs(), fAudioAdapter->GetOutputs());
fAudioAdapter->Create();
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)
goto fail;
}
//jack ports
fCapturePortList = new jack_port_t*[fAudioAdapter->GetInputs()];
fPlaybackPortList = new jack_port_t*[fAudioAdapter->GetOutputs()];
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)
goto fail;
for (int i = 0; i < fAudioAdapter->GetInputs(); i++) {
sprintf(name, "capture_%d", i + 1);
if ((fCapturePortList[i] = jack_port_register(fClient, name, JACK_DEFAULT_AUDIO_TYPE, CaptureDriverFlags, 0)) == NULL) {
goto fail;
}
}
//callbacks and activation
if (jack_set_process_callback(fJackClient, Process, this) < 0)
goto fail;
if (jack_set_buffer_size_callback(fJackClient, BufferSize, this) < 0)
for (int i = 0; i < fAudioAdapter->GetOutputs(); i++) {
sprintf(name, "playback_%d", i + 1);
if ((fPlaybackPortList[i] = jack_port_register(fClient, name, JACK_DEFAULT_AUDIO_TYPE, PlaybackDriverFlags, 0)) == NULL) {
goto fail;
if (jack_set_sample_rate_callback(fJackClient, SampleRate, this) < 0)
goto fail;
if (jack_activate(fJackClient) < 0)
goto fail;
if (fAutoConnect)
ConnectPorts();
// Ring buffers are now allocated..
return fAudioAdapter->Open();
return 0;
}
}
fail:
FreePorts();
fAudioAdapter->Destroy();
return -1;
//callbacks and activation
if (jack_set_process_callback(fClient, Process, this) < 0) {
goto fail;
}
if (jack_set_buffer_size_callback(fClient, BufferSize, this) < 0) {
goto fail;
}
if (jack_set_sample_rate_callback(fClient, SampleRate, this) < 0) {
goto fail;
}
if (jack_set_latency_callback(fClient, Latency, this) < 0) {
goto fail;
}
if (jack_activate(fClient) < 0) {
goto fail;
}
int JackAudioAdapter::Close()
{
fAudioAdapter->Close();
fAudioAdapter->Destroy();
return 0;
if (fAutoConnect) {
ConnectPorts();
}
// Ring buffers are now allocated...
return fAudioAdapter->Open();
return 0;
fail:
FreePorts();
fAudioAdapter->Destroy();
return -1;
}
int JackAudioAdapter::Close()
{
fAudioAdapter->Close();
fAudioAdapter->Destroy();
return 0;
}
} //namespace
......@@ -37,11 +37,12 @@ namespace Jack
static int Process(jack_nframes_t, void* arg);
static int BufferSize(jack_nframes_t buffer_size, void* arg);
static int SampleRate(jack_nframes_t sample_rate, void* arg);
static void Latency(jack_latency_callback_mode_t mode, void* arg);
jack_port_t** fCapturePortList;
jack_port_t** fPlaybackPortList;
jack_client_t* fJackClient;
jack_client_t* fClient;
JackAudioAdapterInterface* fAudioAdapter;
bool fAutoConnect;
......@@ -51,7 +52,7 @@ namespace Jack
public:
JackAudioAdapter(jack_client_t* jack_client, JackAudioAdapterInterface* audio_io, const JSList* params = NULL, bool system = false);
JackAudioAdapter(jack_client_t* client, JackAudioAdapterInterface* audio_io, const JSList* params = NULL);
~JackAudioAdapter();
int Open();
......@@ -60,4 +61,7 @@ namespace Jack
}
#define CaptureDriverFlags static_cast<JackPortFlags>(JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal)
#define PlaybackDriverFlags static_cast<JackPortFlags>(JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal)
#endif
......@@ -49,8 +49,7 @@ namespace Jack
FILE* file = fopen("JackAudioAdapter.log", "w");
int max = (fCount) % TABLE_MAX - 1;
for (int i = 1; i < max; i++)
{
for (int i = 1; i < max; i++) {
fprintf(file, "%d \t %d \t %d \t %f \t %f \t %d \t %d \n",
fTable[i].delta, fTable[i].time1, fTable[i].time2,
fTable[i].r1, fTable[i].r2, fTable[i].pos1, fTable[i].pos2);
......@@ -158,21 +157,25 @@ namespace Jack
void JackAudioAdapterInterface::AdaptRingBufferSize()
{
if (fHostBufferSize > fAdaptedBufferSize)
if (fHostBufferSize > fAdaptedBufferSize) {
fRingbufferCurSize = 4 * fHostBufferSize;
else
} else {
fRingbufferCurSize = 4 * fAdaptedBufferSize;
}
}
void JackAudioAdapterInterface::ResetRingBuffers()
{
if (fRingbufferCurSize > DEFAULT_RB_SIZE)
if (fRingbufferCurSize > DEFAULT_RB_SIZE) {
fRingbufferCurSize = DEFAULT_RB_SIZE;
}
for (int i = 0; i < fCaptureChannels; i++)
for (int i = 0; i < fCaptureChannels; i++) {
fCaptureRingBuffer[i]->Reset(fRingbufferCurSize);
for (int i = 0; i < fPlaybackChannels; i++)
}
for (int i = 0; i < fPlaybackChannels; i++) {
fPlaybackRingBuffer[i]->Reset(fRingbufferCurSize);
}
}
void JackAudioAdapterInterface::Reset()
......@@ -195,8 +198,9 @@ namespace Jack
AdaptRingBufferSize();
jack_info("Ringbuffer automatic adaptative mode size = %d frames", fRingbufferCurSize);
} else {
if (fRingbufferCurSize > DEFAULT_RB_SIZE)
if (fRingbufferCurSize > DEFAULT_RB_SIZE) {
fRingbufferCurSize = DEFAULT_RB_SIZE;
}
jack_info("Fixed ringbuffer size = %d frames", fRingbufferCurSize);
}
......@@ -209,19 +213,23 @@ namespace Jack
fPlaybackRingBuffer[i]->Reset(fRingbufferCurSize);
}
if (fCaptureChannels > 0)
if (fCaptureChannels > 0) {
jack_log("ReadSpace = %ld", fCaptureRingBuffer[0]->ReadSpace());
if (fPlaybackChannels > 0)
}
if (fPlaybackChannels > 0) {
jack_log("WriteSpace = %ld", fPlaybackRingBuffer[0]->WriteSpace());
}
}
#endif
void JackAudioAdapterInterface::Destroy()
{
for (int i = 0; i < fCaptureChannels; i++ )
delete ( fCaptureRingBuffer[i] );
for (int i = 0; i < fPlaybackChannels; i++ )
delete ( fPlaybackRingBuffer[i] );
for (int i = 0; i < fCaptureChannels; i++) {
delete(fCaptureRingBuffer[i]);
}
for (int i = 0; i < fPlaybackChannels; i++) {
delete (fPlaybackRingBuffer[i]);
}
delete[] fCaptureRingBuffer;
delete[] fPlaybackRingBuffer;
......@@ -238,10 +246,11 @@ namespace Jack
double ratio = 1;
// TODO : done like this just to avoid crash when input only or output only...
if (fCaptureChannels > 0)
if (fCaptureChannels > 0) {
ratio = fPIControler.GetRatio(fCaptureRingBuffer[0]->GetError() - delta_frames);
else if (fPlaybackChannels > 0)
} else if (fPlaybackChannels > 0) {
ratio = fPIControler.GetRatio(fPlaybackRingBuffer[0]->GetError() - delta_frames);
}
#ifdef JACK_MONITOR
if (fCaptureRingBuffer && fCaptureRingBuffer[0] != NULL)
......
......@@ -49,7 +49,7 @@ namespace Jack
Measure fTable[TABLE_MAX];
int fCount;
MeasureTable() :fCount ( 0 )
MeasureTable() :fCount(0)
{}
void Write(int time1, int time2, float r1, float r2, int pos1, int pos2);
......@@ -102,13 +102,13 @@ namespace Jack
public:
JackAudioAdapterInterface ( jack_nframes_t buffer_size, jack_nframes_t sample_rate, jack_nframes_t ring_buffer_size = DEFAULT_ADAPTATIVE_SIZE):
fCaptureChannels ( 0 ),
fPlaybackChannels ( 0 ),
fHostBufferSize ( buffer_size ),
fHostSampleRate ( sample_rate ),
fAdaptedBufferSize ( buffer_size),
fAdaptedSampleRate ( sample_rate ),
JackAudioAdapterInterface(jack_nframes_t buffer_size, jack_nframes_t sample_rate, jack_nframes_t ring_buffer_size = DEFAULT_ADAPTATIVE_SIZE):
fCaptureChannels(0),
fPlaybackChannels(0),
fHostBufferSize(buffer_size),
fHostSampleRate(sample_rate),
fAdaptedBufferSize(buffer_size),
fAdaptedSampleRate(sample_rate),
fPIControler(sample_rate / sample_rate, 256),
fCaptureRingBuffer(NULL), fPlaybackRingBuffer(NULL),
fQuality(0),
......@@ -117,23 +117,23 @@ namespace Jack
fRunning(false),
fAdaptative(true)
{}
JackAudioAdapterInterface ( jack_nframes_t host_buffer_size,
JackAudioAdapterInterface(jack_nframes_t host_buffer_size,
jack_nframes_t host_sample_rate,
jack_nframes_t adapted_buffer_size,
jack_nframes_t adapted_sample_rate,
jack_nframes_t ring_buffer_size = DEFAULT_ADAPTATIVE_SIZE ) :
fCaptureChannels ( 0 ),
fPlaybackChannels ( 0 ),
fHostBufferSize ( host_buffer_size ),
fHostSampleRate ( host_sample_rate ),
fAdaptedBufferSize ( adapted_buffer_size),
fAdaptedSampleRate ( adapted_sample_rate ),
fPIControler(host_sample_rate / host_sample_rate, 256),
fQuality(0),
fRingbufferCurSize(ring_buffer_size),
fPullAndPushTime(0),
fRunning(false),
fAdaptative(true)
jack_nframes_t ring_buffer_size = DEFAULT_ADAPTATIVE_SIZE) :
fCaptureChannels(0),
fPlaybackChannels(0),
fHostBufferSize(host_buffer_size),
fHostSampleRate(host_sample_rate),
fAdaptedBufferSize(adapted_buffer_size),
fAdaptedSampleRate(adapted_sample_rate),
fPIControler(host_sample_rate / host_sample_rate, 256),
fQuality(0),
fRingbufferCurSize(ring_buffer_size),
fPullAndPushTime(0),
fRunning(false),
fAdaptative(true)
{}
virtual ~JackAudioAdapterInterface()
......@@ -154,59 +154,61 @@ namespace Jack
return 0;
}
virtual int SetHostBufferSize ( jack_nframes_t buffer_size )
virtual int SetHostBufferSize(jack_nframes_t buffer_size)
{
fHostBufferSize = buffer_size;
if (fAdaptative)
if (fAdaptative) {
AdaptRingBufferSize();
}
return 0;
}
virtual int SetAdaptedBufferSize ( jack_nframes_t buffer_size )
virtual int SetAdaptedBufferSize(jack_nframes_t buffer_size)
{
fAdaptedBufferSize = buffer_size;
if (fAdaptative)
if (fAdaptative) {
AdaptRingBufferSize();
}
return 0;
}
virtual int SetBufferSize ( jack_nframes_t buffer_size )
virtual int SetBufferSize(jack_nframes_t buffer_size)
{
SetHostBufferSize ( buffer_size );
SetAdaptedBufferSize ( buffer_size );
SetHostBufferSize(buffer_size);
SetAdaptedBufferSize(buffer_size);
return 0;
}
virtual int SetHostSampleRate ( jack_nframes_t sample_rate )
virtual int SetHostSampleRate(jack_nframes_t sample_rate)
{
fHostSampleRate = sample_rate;
fPIControler.Init(double(fHostSampleRate) / double(fAdaptedSampleRate));
return 0;
}
virtual int SetAdaptedSampleRate ( jack_nframes_t sample_rate )
virtual int SetAdaptedSampleRate(jack_nframes_t sample_rate)
{
fAdaptedSampleRate = sample_rate;
fPIControler.Init(double(fHostSampleRate) / double(fAdaptedSampleRate));
return 0;
}
virtual int SetSampleRate ( jack_nframes_t sample_rate )
virtual int SetSampleRate(jack_nframes_t sample_rate)
{
SetHostSampleRate ( sample_rate );
SetAdaptedSampleRate ( sample_rate );
SetHostSampleRate(sample_rate);
SetAdaptedSampleRate(sample_rate);
return 0;
}
void SetInputs ( int inputs )
void SetInputs(int inputs)
{
jack_log ( "JackAudioAdapterInterface::SetInputs %d", inputs );
jack_log("JackAudioAdapterInterface::SetInputs %d", inputs);
fCaptureChannels = inputs;
}
void SetOutputs ( int outputs )
void SetOutputs(int outputs)
{
jack_log ( "JackAudioAdapterInterface::SetOutputs %d", outputs );
jack_log("JackAudioAdapterInterface::SetOutputs %d", outputs);
fPlaybackChannels = outputs;
}
......@@ -222,6 +224,9 @@ namespace Jack
return fPlaybackChannels;