From 8144caa02cc2ae2eee262b1812143c715c2c5577 Mon Sep 17 00:00:00 2001 From: moret Date: Thu, 21 Aug 2008 16:05:29 +0000 Subject: [PATCH] Switch producer/consumer to host/adapted in AdapterInterface git-svn-id: http://subversion.jackaudio.org/jack/jack2/trunk/jackmp@2794 0c269be4-1314-0410-8aa9-9f06e86f4224 --- common/JackAudioAdapterInterface.h | 289 ++++++++++++++++------------- common/JackNetAdapter.cpp | 49 +++-- 2 files changed, 182 insertions(+), 156 deletions(-) diff --git a/common/JackAudioAdapterInterface.h b/common/JackAudioAdapterInterface.h index d6c09f5f..68d6cc8f 100644 --- a/common/JackAudioAdapterInterface.h +++ b/common/JackAudioAdapterInterface.h @@ -38,145 +38,174 @@ namespace Jack #define TABLE_MAX 100000 -struct Measure -{ - int delta; - int time1; - int time2; - float r1; - float r2; - int pos1; - int pos2; -}; - -struct MeasureTable -{ - - Measure fTable[TABLE_MAX]; - int fCount; - - MeasureTable():fCount(0) - {} + struct Measure + { + int delta; + int time1; + int time2; + float r1; + float r2; + int pos1; + int pos2; + }; + + struct MeasureTable + { + + Measure fTable[TABLE_MAX]; + int fCount; + + MeasureTable() :fCount ( 0 ) + {} - void Write(int time1, int time2, float r1, float r2, int pos1, int pos2); - void Save(); + void Write ( int time1, int time2, float r1, float r2, int pos1, int pos2 ); + void Save(); -}; + }; #endif -/*! -\brief Base class for audio adapters. -*/ - -class JackAudioAdapterInterface -{ - - protected: + /*! + \brief Base class for audio adapters. + */ - #ifdef JACK_MONITOR - MeasureTable fTable; - #endif + class JackAudioAdapterInterface + { - int fCaptureChannels; - int fPlaybackChannels; + protected: - jack_nframes_t fBufferSize; - jack_nframes_t fSampleRate; - - // DLL - JackAtomicDelayLockedLoop fProducerDLL; - JackAtomicDelayLockedLoop fConsumerDLL; - - JackResampler** fCaptureRingBuffer; - JackResampler** fPlaybackRingBuffer; - - bool fRunning; - - public: - - JackAudioAdapterInterface(jack_nframes_t buffer_size, jack_nframes_t sample_rate) - :fCaptureChannels(0), - fPlaybackChannels(0), - fBufferSize(buffer_size), - fSampleRate(sample_rate), - fProducerDLL(buffer_size, sample_rate), - fConsumerDLL(buffer_size, sample_rate), - fRunning(false) - {} - virtual ~JackAudioAdapterInterface() - {} +#ifdef JACK_MONITOR + MeasureTable fTable; +#endif - void SetRingBuffers(JackResampler** input, JackResampler** output) - { - fCaptureRingBuffer = input; - fPlaybackRingBuffer = output; - } - - bool IsRunning() {return fRunning;} - - virtual void Reset() {fRunning = false;} - void ResetRingBuffers(); - - virtual int Open(); - virtual int Close(); - - virtual int SetBufferSize(jack_nframes_t buffer_size) - { - fBufferSize = buffer_size; - fConsumerDLL.Init(fBufferSize, fSampleRate); - fProducerDLL.Init(fBufferSize, fSampleRate); - return 0; - } - - virtual int SetSampleRate(jack_nframes_t sample_rate) - { - fSampleRate = sample_rate; - fConsumerDLL.Init(fBufferSize, fSampleRate); - // Producer (Audio) keeps the same SR - return 0; - } - - virtual int SetAudioSampleRate(jack_nframes_t sample_rate) - { - fSampleRate = sample_rate; - // Consumer keeps the same SR - fProducerDLL.Init(fBufferSize, fSampleRate); - return 0; - } - - virtual void SetCallbackTime(jack_time_t callback_usec) - { - fConsumerDLL.IncFrame(callback_usec); - } - - void ResampleFactor(jack_nframes_t& frame1, jack_nframes_t& frame2); - - void SetInputs ( int inputs ) - { - jack_log ( "JackAudioAdapterInterface::SetInputs %d", inputs ); - fCaptureChannels = inputs; - } - - void SetOutputs ( int outputs ) - { - jack_log ( "JackAudioAdapterInterface::SetOutputs %d", outputs ); - fPlaybackChannels = outputs; - } - - int GetInputs() - { - jack_log ( "JackAudioAdapterInterface::GetInputs %d", fCaptureChannels ); - return fCaptureChannels; - } - - int GetOutputs() - { - jack_log ( "JackAudioAdapterInterface::GetOutputs %d", fPlaybackChannels ); - return fPlaybackChannels; - } - -}; + int fCaptureChannels; + int fPlaybackChannels; + + jack_nframes_t fBufferSize; + jack_nframes_t fSampleRate; + + // DLL + JackAtomicDelayLockedLoop fProducerDLL; + JackAtomicDelayLockedLoop fConsumerDLL; + + JackResampler** fCaptureRingBuffer; + JackResampler** fPlaybackRingBuffer; + + bool fRunning; + + public: + + JackAudioAdapterInterface ( jack_nframes_t buffer_size, jack_nframes_t sample_rate ) + :fCaptureChannels ( 0 ), + fPlaybackChannels ( 0 ), + fBufferSize ( buffer_size ), + fSampleRate ( sample_rate ), + fProducerDLL ( buffer_size, sample_rate ), + fConsumerDLL ( buffer_size, sample_rate ), + 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 int Open(); + virtual int Close(); + + virtual int SetBufferSize ( jack_nframes_t buffer_size ) + { + fBufferSize = buffer_size; + fConsumerDLL.Init ( fBufferSize, fSampleRate ); + fProducerDLL.Init ( fBufferSize, fSampleRate ); + return 0; + } + + virtual int SetHostBufferSize ( jack_nframes_t buffer_size ) + { + fBufferSize = buffer_size; + fConsumerDLL.Init ( fBufferSize, fSampleRate ); + return 0; + } + + virtual int SetAdaptedBufferSize ( jack_nframes_t buffer_size ) + { + fProducerDLL.Init ( buffer_size, fSampleRate ); + return 0; + } + + //TODO : switch the two next methods to SetHost/AdaptedBufferSize in adapters + virtual int SetSampleRate ( jack_nframes_t sample_rate ) + { + fSampleRate = sample_rate; + fConsumerDLL.Init ( fBufferSize, fSampleRate ); + // Producer (Audio) keeps the same SR + return 0; + } + + virtual int SetAudioSampleRate ( jack_nframes_t sample_rate ) + { + fSampleRate = sample_rate; + // Consumer keeps the same SR + fProducerDLL.Init ( fBufferSize, fSampleRate ); + return 0; + } + + //host = driver that hosts the adapter + virtual int SetHostSampleRate ( jack_nframes_t sample_rate ) + { + fSampleRate = sample_rate; + fConsumerDLL.Init ( fBufferSize, fSampleRate ); + return 0; + } + + //adapted = driver hosted by the adapter + virtual int SetAdaptedSampleRate ( jack_nframes_t sample_rate ) + { + fProducerDLL.Init ( fBufferSize, fSampleRate ); + return 0; + } + + virtual void SetCallbackTime ( jack_time_t callback_usec ) + { + fConsumerDLL.IncFrame ( callback_usec ); + } + + void ResampleFactor ( jack_nframes_t& frame1, jack_nframes_t& frame2 ); + + void SetInputs ( int inputs ) + { + jack_log ( "JackAudioAdapterInterface::SetInputs %d", inputs ); + fCaptureChannels = inputs; + } + + void SetOutputs ( int outputs ) + { + jack_log ( "JackAudioAdapterInterface::SetOutputs %d", outputs ); + fPlaybackChannels = outputs; + } + + int GetInputs() + { + jack_log ( "JackAudioAdapterInterface::GetInputs %d", fCaptureChannels ); + return fCaptureChannels; + } + + int GetOutputs() + { + jack_log ( "JackAudioAdapterInterface::GetOutputs %d", fPlaybackChannels ); + return fPlaybackChannels; + } + + }; } diff --git a/common/JackNetAdapter.cpp b/common/JackNetAdapter.cpp index 15cce024..54efa601 100644 --- a/common/JackNetAdapter.cpp +++ b/common/JackNetAdapter.cpp @@ -178,7 +178,7 @@ namespace Jack int JackNetAdapter::SetBufferSize ( jack_nframes_t buffer_size ) { - fParams.fPeriodSize = buffer_size; + JackAudioAdapterInterface::SetHostBufferSize ( buffer_size ); return 0; } @@ -209,6 +209,9 @@ namespace Jack fNetAudioPlaybackBuffer->SetBuffer ( port_index, fSoftPlaybackBuffer[port_index] ); } + //set audio adapter parameters + JackAudioAdapterInterface::SetAdaptedBufferSize ( fParams.fPeriodSize ); + //init done, display parameters SessionParamsDisplay ( &fParams ); @@ -261,39 +264,33 @@ namespace Jack { bool failure = false; int port_index; - int rx_bytes, tx_bytes; //read data from the network - //in case of fatal network error, definitely stop the process - rx_bytes = Read(); - if ( rx_bytes == SOCKET_ERROR ) + //in case of fatal network error, stop the process + if ( Read() == SOCKET_ERROR ) return SOCKET_ERROR; - //if there is data to resample, - if ( rx_bytes ) - { - //get the resample factor, - jack_nframes_t time1, time2; - ResampleFactor ( time1, time2 ); + //get the resample factor, + jack_nframes_t time1, time2; + ResampleFactor ( time1, time2 ); - //resample input data, - for ( port_index = 0; port_index < fCaptureChannels; port_index++ ) - { - fCaptureRingBuffer[port_index]->SetRatio ( time1, time2 ); - if ( fCaptureRingBuffer[port_index]->WriteResample ( fSoftCaptureBuffer[port_index], fBufferSize ) < fBufferSize ) - failure = true; - } - //and output data, - for ( port_index = 0; port_index < fPlaybackChannels; port_index++ ) - { - fPlaybackRingBuffer[port_index]->SetRatio ( time2, time1 ); - if ( fPlaybackRingBuffer[port_index]->ReadResample ( fSoftPlaybackBuffer[port_index], fBufferSize ) < fBufferSize ) - failure = true; - } + //resample input data, + for ( port_index = 0; port_index < fCaptureChannels; port_index++ ) + { + fCaptureRingBuffer[port_index]->SetRatio ( time1, time2 ); + if ( fCaptureRingBuffer[port_index]->WriteResample ( fSoftCaptureBuffer[port_index], fParams.fPeriodSize ) < fParams.fPeriodSize ) + failure = true; + } + //and output data, + for ( port_index = 0; port_index < fPlaybackChannels; port_index++ ) + { + fPlaybackRingBuffer[port_index]->SetRatio ( time2, time1 ); + if ( fPlaybackRingBuffer[port_index]->ReadResample ( fSoftPlaybackBuffer[port_index], fParams.fPeriodSize ) < fParams.fPeriodSize ) + failure = true; } //then write data to network - //in case of failure, definitely stop process + //in case of failure, stop process if ( Write() == SOCKET_ERROR ) return SOCKET_ERROR;