JackTrip
JackAudioInterface.h
Go to the documentation of this file.
1 //*****************************************************************
2 /*
3  JackTrip: A System for High-Quality Audio Network Performance
4  over the Internet
5 
6  Copyright (c) 2008 Juan-Pablo Caceres, Chris Chafe.
7  SoundWIRE group at CCRMA, Stanford University.
8 
9  Permission is hereby granted, free of charge, to any person
10  obtaining a copy of this software and associated documentation
11  files (the "Software"), to deal in the Software without
12  restriction, including without limitation the rights to use,
13  copy, modify, merge, publish, distribute, sublicense, and/or sell
14  copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following
16  conditions:
17 
18  The above copyright notice and this permission notice shall be
19  included in all copies or substantial portions of the Software.
20 
21  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
23  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28  OTHER DEALINGS IN THE SOFTWARE.
29 */
30 //*****************************************************************
31 
39 #ifndef __JACKAUDIOINTERFACE_H__
40 #define __JACKAUDIOINTERFACE_H__
41 
42 #include <iostream>
43 #include <tr1/memory> //for shared_ptr
44 #include <functional> //for mem_fun_ref
45 #include <jack/jack.h>
46 
47 #include <QVector>
48 #include <QVarLengthArray>
49 #include <QMutex>
50 
51 
52 #include "jacktrip_types.h"
53 #include "ProcessPlugin.h"
54 
55 class JackTrip; //forward declaration
56 
57 
64 {
65 public:
66 
70  BIT8 = 1,
71  BIT16 = 2,
72  BIT24 = 3,
73  BIT32 = 4
74  };
75 
78  SR22,
79  SR32,
80  SR44,
81  SR48,
82  SR88,
83  SR96,
86  };
87 
95  JackAudioInterface(JackTrip* jacktrip,
96  int NumInChans, int NumOutChans,
97  audioBitResolutionT AudioBitResolution = BIT16,
98  const char* ClientName = "JackTrip");
99 
102  virtual ~JackAudioInterface();
103 
106  void setup();
107 
110  uint32_t getSampleRate() const;
111 
116 
122  static int getSampleRateFromType(samplingRateT rate_type);
123 
127 
131  {
133  }
134 
139  int getAudioBitResolution() const;
140 
142  int getNumInputChannels() const;
143 
145  int getNumOutputChannels() const;
146 
148  size_t getSizeInBytesPerChannel() const;
149 
154  int startProcess() const;
155 
159  int stopProcess() const;
160 
171  /*
172  void setRingBuffers(const std::tr1::shared_ptr<RingBuffer> InRingBuffer,
173  const std::tr1::shared_ptr<RingBuffer> OutRingBuffer);
174  */
175 
182  //void appendProcessPlugin(const std::tr1::shared_ptr<ProcessPlugin> plugin);
183  void appendProcessPlugin(ProcessPlugin* plugin);
184 
192  static void fromSampleToBitConversion(const sample_t* const input,
193  int8_t* output,
194  const audioBitResolutionT targetBitResolution);
195 
203  static void fromBitToSampleConversion(const int8_t* const input,
204  sample_t* output,
205  const audioBitResolutionT sourceBitResolution);
206 
208  void connectDefaultPorts();
209 
211  void setClientName(const char* ClientName)
212  { mClientName = ClientName; }
213 
214 private:
215 
224  void setupClient();
225 
228  void createChannels();
229 
233  static void jackShutdown(void*);
234 
236  //void computeNetworkProcess();
237 
240 
243 
247  void setProcessCallback();
248 
259  int processCallback(jack_nframes_t nframes);
260 
272  // reference : http://article.gmane.org/gmane.comp.audio.jackit/12873
273  static int wrapperProcessCallback(jack_nframes_t nframes, void *arg) ;
274 
275 
281 
282  jack_client_t* mClient;
283  const char* mClientName;
284  QVarLengthArray<jack_port_t*> mInPorts;
285  QVarLengthArray<jack_port_t*> mOutPorts;
286  //jack_port_t** mInPorts; ///< Vector of Input Ports (Channels)
287  //jack_port_t** mOutPorts; ///< Vector of Output Ports (Channels)
288  QVarLengthArray<sample_t*> mInBuffer;
289  QVarLengthArray<sample_t*> mOutBuffer;
290 
291  QVarLengthArray<sample_t*> mInProcessBuffer;
292  QVarLengthArray<sample_t*> mOutProcessBuffer;
293 
297 
298  QVector<ProcessPlugin*> mProcessPlugins;
300 
301  static QMutex sJackMutex;
302 };
303 
304 
305 #endif
samplingRateT getSampleRateType() const
Get the Jack Server Sampling Rate Enum Type samplingRateT.
Definition: JackAudioInterface.cpp:211
22050 Hz
Definition: JackAudioInterface.h:78
const char * mClientName
Jack Client Name.
Definition: JackAudioInterface.h:283
int startProcess() const
Tell the JACK server that we are ready to roll. The process-callback will start running. This runs on its own thread.
Definition: JackAudioInterface.cpp:331
static void jackShutdown(void *)
JACK calls this shutdown_callback if the server ever shuts down or decides to disconnect the client...
Definition: JackAudioInterface.cpp:358
192000 Hz
Definition: JackAudioInterface.h:84
int getAudioBitResolution() const
Get the Audio Bit Resolution, in bits.
Definition: JackAudioInterface.cpp:284
Undefined.
Definition: JackAudioInterface.h:85
QVarLengthArray< sample_t * > mOutProcessBuffer
Vector of Output buffers/channel for ProcessPlugin.
Definition: JackAudioInterface.h:292
uint32_t getBufferSizeInBytes() const
Get the Jack Server Buffer Size, in bytes.
Definition: JackAudioInterface.h:130
size_t mSizeInBytesPerChannel
Size in bytes per audio channel.
Definition: JackAudioInterface.h:296
static int wrapperProcessCallback(jack_nframes_t nframes, void *arg)
Wrapper to cast the member processCallback to a static function pointer that can be used with jack_se...
Definition: JackAudioInterface.cpp:522
static int getSampleRateFromType(samplingRateT rate_type)
Helper function to get the sample rate (in Hz) for a JackAudioInterface::samplingRateT.
Definition: JackAudioInterface.cpp:235
int8_t * mInputPacket
Packet containing all the channels to read from the RingBuffer.
Definition: JackAudioInterface.h:294
int mNumOutChans
Number of Output Channels.
Definition: JackAudioInterface.h:277
virtual ~JackAudioInterface()
The class destructor.
Definition: JackAudioInterface.cpp:80
44100 Hz
Definition: JackAudioInterface.h:80
Class that provides an interface with the Jack Audio Server.
Definition: JackAudioInterface.h:63
quint32 uint32_t
Typedef for unsigned int. This type is guaranteed to be 32-bit.
Definition: jacktrip_types.h:69
void computeNetworkProcessToNetwork()
Compute the process from JACK to send packets.
Definition: JackAudioInterface.cpp:415
48000 Hz
Definition: JackAudioInterface.h:81
int mAudioBitResolution
Bit resolution in audio samples.
Definition: JackAudioInterface.h:279
static void fromBitToSampleConversion(const int8_t *const input, sample_t *output, const audioBitResolutionT sourceBitResolution)
Convert a audioBitResolutionT bit resolution number into a 32bit number (sample_t) ...
Definition: JackAudioInterface.cpp:580
int8_t * mOutputPacket
Packet containing all the channels to send to the RingBuffer.
Definition: JackAudioInterface.h:295
void setClientName(const char *ClientName)
Set Client Name to something different that the default (JackTrip)
Definition: JackAudioInterface.h:211
QVarLengthArray< sample_t * > mInBuffer
Vector of Input buffers/channel read from JACK.
Definition: JackAudioInterface.h:288
int getNumInputChannels() const
Get Number of Input Channels.
Definition: JackAudioInterface.cpp:291
QVector< ProcessPlugin * > mProcessPlugins
Vector of ProcesPlugins
Definition: JackAudioInterface.h:298
QVarLengthArray< jack_port_t * > mOutPorts
Vector of Output Ports (Channels)
Definition: JackAudioInterface.h:285
jack_default_audio_sample_t sample_t
Audio sample type.
Definition: jacktrip_types.h:52
32000 Hz
Definition: JackAudioInterface.h:79
Interface for the process plugins to add to the JACK callback process in JackAudioInterface.
Definition: ProcessPlugin.h:52
jack_client_t * mClient
Jack Client.
Definition: JackAudioInterface.h:282
int getNumOutputChannels() const
Get Number of Output Channels.
Definition: JackAudioInterface.cpp:298
audioBitResolutionT
Enum for Audio Resolution in bits.
Definition: JackAudioInterface.h:69
void appendProcessPlugin(ProcessPlugin *plugin)
Set the pointer to the Input and Output RingBuffer that'll be use to read and write audio...
Definition: JackAudioInterface.cpp:622
16 bits (default)
Definition: JackAudioInterface.h:71
int mNumInChans
Number of Input Channels.
Definition: JackAudioInterface.h:276
QVarLengthArray< jack_port_t * > mInPorts
Vector of Input Ports (Channels)
Definition: JackAudioInterface.h:284
static QMutex sJackMutex
Mutex to make thread safe jack functions that are not.
Definition: JackAudioInterface.h:301
qint8 int8_t
Typedef for unsigned long long int. This type is guaranteed to be 64-bit.
Definition: jacktrip_types.h:74
8 bits
Definition: JackAudioInterface.h:70
int processCallback(jack_nframes_t nframes)
JACK process callback.
Definition: JackAudioInterface.cpp:448
JackTrip * mJackTrip
JackTrip mediator class.
Definition: JackAudioInterface.h:299
static void fromSampleToBitConversion(const sample_t *const input, int8_t *output, const audioBitResolutionT targetBitResolution)
Convert a 32bit number (sample_t) into one of the bit resolution supported (audioBitResolutionT).
Definition: JackAudioInterface.cpp:531
int mNumFrames
Buffer block size, in samples.
Definition: JackAudioInterface.h:278
uint32_t getSampleRate() const
Get the Jack Server Sampling Rate, in samples/second.
Definition: JackAudioInterface.cpp:204
32 bits
Definition: JackAudioInterface.h:73
void setProcessCallback()
Set the process callback of the member function processCallback. This process will be called by the J...
Definition: JackAudioInterface.cpp:311
void createChannels()
Creates input and output channels in the Jack client.
Definition: JackAudioInterface.cpp:177
void computeNetworkProcessFromNetwork()
Sets the part of the process callback that sends and receive packets.
Definition: JackAudioInterface.cpp:383
24 bits
Definition: JackAudioInterface.h:72
88200 Hz
Definition: JackAudioInterface.h:82
samplingRateT
Sampling Rates supported by JACK.
Definition: JackAudioInterface.h:77
uint32_t getBufferSizeInSamples() const
Get the Jack Server Buffer Size, in samples.
Definition: JackAudioInterface.cpp:277
QVarLengthArray< sample_t * > mOutBuffer
Vector of Output buffer/channel to write to JACK.
Definition: JackAudioInterface.h:289
int stopProcess() const
Stops the process-callback thread.
Definition: JackAudioInterface.cpp:345
Main class to creates a SERVER (to listen) or a CLIENT (to connect to a listening server) to send aud...
Definition: JackTrip.h:59
size_t getSizeInBytesPerChannel() const
Get size of each audio per channel, in bytes.
Definition: JackAudioInterface.cpp:305
void setupClient()
Private method to setup a client of the Jack server. Can't start Jack.
Definition: JackAudioInterface.cpp:104
audioBitResolutionT mBitResolutionMode
Bit resolution (audioBitResolutionT) mode.
Definition: JackAudioInterface.h:280
96000 Hz
Definition: JackAudioInterface.h:83
JackAudioInterface(JackTrip *jacktrip, int NumInChans, int NumOutChans, audioBitResolutionT AudioBitResolution=BIT16, const char *ClientName="JackTrip")
The class constructor.
Definition: JackAudioInterface.cpp:64
QVarLengthArray< sample_t * > mInProcessBuffer
Vector of Input buffers/channel for ProcessPlugin.
Definition: JackAudioInterface.h:291
void setup()
Setup the client.
Definition: JackAudioInterface.cpp:96
void connectDefaultPorts()
Connect the default ports, capture to sends, and receives to playback.
Definition: JackAudioInterface.cpp:632