00001 //***************************************************************** 00002 /* 00003 JackTrip: A System for High-Quality Audio Network Performance 00004 over the Internet 00005 00006 Copyright (c) 2008 Juan-Pablo Caceres, Chris Chafe. 00007 SoundWIRE group at CCRMA, Stanford University. 00008 00009 Permission is hereby granted, free of charge, to any person 00010 obtaining a copy of this software and associated documentation 00011 files (the "Software"), to deal in the Software without 00012 restriction, including without limitation the rights to use, 00013 copy, modify, merge, publish, distribute, sublicense, and/or sell 00014 copies of the Software, and to permit persons to whom the 00015 Software is furnished to do so, subject to the following 00016 conditions: 00017 00018 The above copyright notice and this permission notice shall be 00019 included in all copies or substantial portions of the Software. 00020 00021 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00022 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 00023 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00024 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 00025 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 00026 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00027 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 00028 OTHER DEALINGS IN THE SOFTWARE. 00029 */ 00030 //***************************************************************** 00031 00039 #ifndef __JACKAUDIOINTERFACE_H__ 00040 #define __JACKAUDIOINTERFACE_H__ 00041 00042 #include <iostream> 00043 #include <tr1/memory> //for shared_ptr 00044 #include <functional> //for mem_fun_ref 00045 #include <jack/jack.h> 00046 00047 #include <QVector> 00048 #include <QVarLengthArray> 00049 #include <QMutex> 00050 00051 00052 #include "jacktrip_types.h" 00053 #include "ProcessPlugin.h" 00054 #include "AudioInterface.h" 00055 00056 //class JackTrip; //forward declaration 00057 00058 00064 class JackAudioInterface : public AudioInterface 00065 { 00066 public: 00067 00075 JackAudioInterface(JackTrip* jacktrip, 00076 int NumInChans, int NumOutChans, 00077 AudioInterface::audioBitResolutionT AudioBitResolution = AudioInterface::BIT16, 00078 const char* ClientName = "JackTrip"); 00079 00082 virtual ~JackAudioInterface(); 00083 00086 virtual void setup(); 00087 00090 virtual void close(); 00091 00094 virtual uint32_t getSampleRate() const; 00095 00099 virtual samplingRateT getSampleRateType() const; 00100 00106 static int getSampleRateFromType(samplingRateT rate_type); 00107 00110 virtual uint32_t getBufferSizeInSamples() const; 00111 00114 virtual uint32_t getBufferSizeInBytes() const 00115 { 00116 return (getBufferSizeInSamples() * getAudioBitResolution()/8); 00117 } 00118 00120 virtual size_t getSizeInBytesPerChannel() const; 00121 00126 virtual int startProcess() const; 00127 00131 virtual int stopProcess() const; 00132 00143 /* 00144 void setRingBuffers(const std::tr1::shared_ptr<RingBuffer> InRingBuffer, 00145 const std::tr1::shared_ptr<RingBuffer> OutRingBuffer); 00146 */ 00147 00154 //void appendProcessPlugin(const std::tr1::shared_ptr<ProcessPlugin> plugin); 00155 /* 00156 virtual void appendProcessPlugin(ProcessPlugin* plugin); 00157 */ 00158 00160 void connectDefaultPorts(); 00161 00163 virtual void setClientName(const char* ClientName) 00164 { mClientName = ClientName; } 00165 00166 private: 00167 00176 void setupClient(); 00177 00180 void createChannels(); 00181 00185 static void jackShutdown(void*); 00186 00188 //void computeNetworkProcess(); 00189 00191 //void computeNetworkProcessFromNetwork(); 00192 00194 //void computeNetworkProcessToNetwork(); 00195 00199 void setProcessCallback(); 00200 00211 int processCallback(jack_nframes_t nframes); 00212 00224 // reference : http://article.gmane.org/gmane.comp.audio.jackit/12873 00225 static int wrapperProcessCallback(jack_nframes_t nframes, void *arg) ; 00226 00227 int mNumInChans; 00228 int mNumOutChans; 00229 int mNumFrames; 00230 //int mAudioBitResolution; ///< Bit resolution in audio samples 00231 AudioInterface::audioBitResolutionT mBitResolutionMode; 00232 00233 jack_client_t* mClient; 00234 const char* mClientName; 00235 QVarLengthArray<jack_port_t*> mInPorts; 00236 QVarLengthArray<jack_port_t*> mOutPorts; 00237 //jack_port_t** mInPorts; ///< Vector of Input Ports (Channels) 00238 //jack_port_t** mOutPorts; ///< Vector of Output Ports (Channels) 00239 QVarLengthArray<sample_t*> mInBuffer; 00240 //QVarLengthArray<sample_t*>* mInBuffer; ///< Vector of Input buffers/channel read from JACK 00241 QVarLengthArray<sample_t*> mOutBuffer; 00242 00243 QVarLengthArray<sample_t*> mInProcessBuffer; 00244 QVarLengthArray<sample_t*> mOutProcessBuffer; 00245 00246 int8_t* mInputPacket; 00247 int8_t* mOutputPacket; 00248 size_t mSizeInBytesPerChannel; 00249 00250 QVector<ProcessPlugin*> mProcessPlugins; 00251 00252 JackTrip* mJackTrip; 00253 static QMutex sJackMutex; 00254 }; 00255 00256 00257 #endif