• Main Page
  • Classes
  • Files
  • File List

jackaudioio.hpp

00001 //C++ Classes that wrap JACK
00002 //Copyright 2007 Alex Norman
00003 //
00004 //This file is part of JACKC++.
00005 //
00006 //JACKC++ is free software: you can redistribute it and/or modify
00007 //it under the terms of the GNU General Public License as published by
00008 //the Free Software Foundation, either version 3 of the License, or
00009 //(at your option) any later version.
00010 //
00011 //JACKC++ is distributed in the hope that it will be useful,
00012 //but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //GNU General Public License for more details.
00015 //
00016 //You should have received a copy of the GNU General Public License
00017 //along with JACKC++.  If not, see <http://www.gnu.org/licenses/>.
00018 
00019 #ifndef JACK_AUDIO_IO_H
00020 #define JACK_AUDIO_IO_H
00021 
00022 extern "C" {
00023 #include <jack/jack.h>
00024 #include <jack/types.h>
00025 #include <jack/ringbuffer.h>
00026 }
00027 #include <string>
00028 #include <vector>
00029 #include <stdexcept>
00030 #include "jackringbuffer.hpp"
00031 
00032 namespace JackCpp {
00033 
00048         class AudioIO {
00049                 public:
00051                         enum jack_state_t {notActive,active,closed};
00053                         typedef std::vector<jack_default_audio_sample_t *> audioBufVector;
00054                 private:
00055                         //commands
00056                         enum cmd_t {add_in_port, add_out_port};
00057                         RingBuffer<cmd_t> mCmdBuffer;
00058                         /* the client */
00059                         jack_client_t *mJackClient;
00060                         // an vector of i/o ports
00061                         std::vector<jack_port_t *> mOutputPorts;
00062                         std::vector<jack_port_t *> mInputPorts;
00063 
00064                         //these are only accessed by the callback [once it is activated]
00065                         //they will usually be equal mOutputPorts.size() etc, except when
00066                         //a new port is added, before the callback
00067                         unsigned int mNumOutputPorts;
00068                         unsigned int mNumInputPorts;
00069 
00070                         //these items are used for grabbing data for the jack callback
00071                         audioBufVector mJackInBuf;
00072                         audioBufVector mJackOutBuf;
00073                         //this stores the state of this jack process [active,notActive,closed]
00074                         jack_state_t mJackState;
00075                         //this prepares the input/output buffers to be passed 
00076                         //to the callback function that a user writes
00077                         //XXX should this be virtual?
00078                         inline int jackToClassAudioCallback(jack_nframes_t nframes);
00079                         std::vector<std::string> mPortNames;
00080                 protected:
00088                         virtual int audioCallback(jack_nframes_t nframes, 
00089                                         audioBufVector inBufs,
00090                                         audioBufVector outBufs) = 0;
00095                         jack_client_t * client();
00096                 public:
00105                         AudioIO(std::string name, 
00106                                         unsigned int inPorts = 0, 
00107                                         unsigned int outPorts = 2, 
00108 #ifdef __APPLE__
00109                                         bool startServer = false)
00110 #else
00111                                         bool startServer = true)
00112 #endif
00113                                 throw(std::runtime_error);
00114 
00116                         virtual ~AudioIO();
00117 
00129                         static int jackProcessCallback(jack_nframes_t nframes, void *arg);
00130 
00132                         bool portExists(std::string name);
00133 
00146                         virtual void reserveOutPorts(unsigned int num)
00147                                 throw(std::runtime_error);
00160                         virtual void reserveInPorts(unsigned int num)
00161                                 throw(std::runtime_error);
00162 
00164                         void start()
00165                                 throw(std::runtime_error);
00167                         void stop()
00168                                 throw(std::runtime_error);
00170                         void close()
00171                                 throw(std::runtime_error);
00172 
00174                         unsigned int inPorts();
00176                         unsigned int outPorts();
00177 
00183                         virtual unsigned int addInPort(std::string name)
00184                                 throw(std::runtime_error);
00190                         virtual unsigned int addOutPort(std::string name)
00191                                 throw(std::runtime_error);
00192 
00198                         void connectTo(unsigned int index, std::string sourcePortName) 
00199                                 throw(std::range_error, std::runtime_error);
00205                         void connectFrom(unsigned int index, std::string destPortName)
00206                                 throw(std::range_error, std::runtime_error);
00212                         void connectToPhysical(unsigned int index, unsigned physical_index)
00213                                 throw(std::range_error, std::runtime_error);
00219                         void connectFromPhysical(unsigned int index, unsigned physical_index)
00220                                 throw(std::range_error, std::runtime_error);
00222                         void disconnectInPort(unsigned int index)
00223                                 throw(std::range_error, std::runtime_error);
00225                         void disconnectOutPort(unsigned int index)
00226                                 throw(std::range_error, std::runtime_error);
00227 
00229                         unsigned int numConnectionsInPort(unsigned int index)
00230                                 throw(std::range_error);
00232                         unsigned int numConnectionsOutPort(unsigned int index)
00233                                 throw(std::range_error);
00234 
00239                         unsigned int numPhysicalSourcePorts();
00244                         unsigned int numPhysicalDestinationPorts();
00245 
00247                         std::string getInputPortName(unsigned int index)
00248                                 throw(std::range_error);
00250                         std::string getOutputPortName(unsigned int index)
00251                                 throw(std::range_error);
00252 
00257                         virtual void jackShutdownCallback();
00265                         float getCpuLoad();
00267                         jack_nframes_t getSampleRate();
00269                         jack_nframes_t getBufferSize();
00271                         bool isRealTime(){return jack_is_realtime(mJackClient);}
00280                         std::string getName(){return std::string(jack_get_client_name(mJackClient));}
00282                         jack_state_t getState(){return mJackState;}
00283 
00291                         jack_nframes_t getFrameTime(){return jack_frame_time(mJackClient);}
00292 
00298                         jack_nframes_t getFramesSinceCycleStart(){return jack_frames_since_cycle_start(mJackClient);}
00299         };
00300 
00301 }
00302 
00303 #endif

Generated on Wed Apr 20 2011 13:46:10 for JackCpp by  doxygen 1.7.1