MIDIIO.h

Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 #ifndef CSL_MIDIIO_H
00007 #define CSL_MIDIIO_H
00008 
00009 #include "math.h"
00010 #include "portmidi.h"
00011 #include "porttime.h"
00012 
00013 #include "CSL_Types.h"
00014 #include "CGestalt.h"
00015 
00016 // copied from 240_test.c. need to sort out where it belongs.
00017 #define OUTPUT_BUFFER_SIZE 0
00018 #define DRIVER_INFO NULL
00019 #define TIME_PROC Pt_Time
00020 #define TIME_INFO NULL
00021 #define TIME_START Pt_Start(1, 0, 0) /* timer started w/millisecond accuracy */
00022 #define MIDI_THRU NULL
00023 
00024 // PmDeviceInfo data structure taken from portmidi.h
00025 //
00026 //  typedef struct {
00027 //      int structVersion; 
00028 //      const char *interf; /* underlying MIDI API, e.g. MMSystem or DirectX */
00029 //      const char *name;   /* device name, e.g. USB MidiSport 1x1 */
00030 //      int input;          /* true iff input is available */
00031 //      int output;         /* true iff output is available */
00032 //      int opened;         /* used by generic PortMidi code to do error checking on arguments */       
00033 //  } PmDeviceInfo;
00034 
00035 // PmEvent data structure taken from portmidi.h
00036 //  typedef long PmMessage;
00037 //  typedef struct {
00038 //      PmMessage      message;
00039 //      PmTimestamp    timestamp;
00040 //  } PmEvent;
00041 
00042 namespace csl {
00043     
00045 
00046 typedef enum {
00047     kNone           = 0,
00048     kNoteOff        = 8,
00049     kNoteOn         = 9,
00050     kPolyTouch      = 10,
00051     kControlChange  = 11,
00052     kProgramChange  = 12,
00053     kAftertouch     = 13,
00054     kPitchWheel     = 14,
00055     kSysEX          = 15
00056 } CSL_MIDIMessageType;
00057 
00059 
00060 class CSL_MIDIMessage {
00061 
00062 public:
00063     CSL_MIDIMessageType message;
00064     unsigned channel;   // 0-indexed, so from 0 to 15 
00065     unsigned data1;
00066     unsigned data2;
00067     long time;
00068     
00069     CSL_MIDIMessage();
00070 //  CSL_MIDIMessage( CSL_MIDIMessageType, ch, d1, d2 );
00071 }; 
00072 
00074 
00075 void copy_CSL_MIDIMessage( CSL_MIDIMessage* source, CSL_MIDIMessage* dest );
00076 
00078 
00079 void CSL_MIDIMessageToPmEvent( CSL_MIDIMessage* cslMIDI, PmEvent* event );
00080 
00082 
00083 void PmEventToCSL_MIDIMessage(  PmEvent* event, CSL_MIDIMessage* cslMIDI );
00084 
00086 
00087 unsigned Message_ChannelToStatus( CSL_MIDIMessageType message, unsigned channel );
00088 
00090 
00091 float MIDINoteToFreq( unsigned midiNote );
00092 
00094 
00095 unsigned FreqToMIDINote( float frequency );
00096 
00100 
00101 class MIDIIO {
00102 public: 
00103     MIDIIO();
00104     virtual ~MIDIIO();
00105     
00106     virtual status open() { return cslErr; };       
00107     bool is_open();                 
00108     status close();                 
00109     status dump_device_info();      
00110     void dump_count_devices();      
00111 
00113     int count_devices();
00114     int get_default_input_id();
00115     int get_default_output_id();    
00116     const PmDeviceInfo* get_device_info( int deviceID );
00117     
00118 protected:
00119     PmStream * mMIDIStream;         
00120     PmDeviceID mDeviceID;           
00121 
00123     static bool mIsInitialized; 
00124     static unsigned mNumInstantiated;
00125     static bool mIsPortTimeStarted;
00126     
00128     bool mIsInput;
00129     bool mIsOutput;
00130     bool mIsOpen;
00131 
00132     void handle_error(PmError err);
00133 };
00134 
00138 
00139 class MIDIIn : public MIDIIO {
00140 public:
00141     MIDIIn();   
00142     
00143     unsigned buffer_size();
00144     void     set_buffer_size( unsigned bufferSize );
00145     status open();              
00146     status open(int deviceID);
00147     bool poll();
00148     status read();              // generic read method. gets MIDI message and store it in mBuffer
00149     status read_interpret();        // read method and sets internal flag for which message was received
00150     
00151     PmEvent buffer() { return mBuffer[0]; }
00152     void    dump_buffer();
00153 
00154     CSL_MIDIMessage message() { return mMsg; }
00155     void     dump_CSL_MIDIMessage();
00156     
00157     bool is_NoteOn_received();
00158     bool is_NoteOff_received();
00159     bool is_PolyTouch_received();
00160     bool is_ControlChange_received();
00161     bool is_ProgramChange_received();
00162     bool is_Aftertouch_received();
00163     bool is_PitchWheel_received();
00164     bool is_SysEX_received();
00165     
00166     unsigned get_note();
00167     unsigned get_velocity();
00168     unsigned get_PolyAftertouch();
00169     unsigned get_ControlChange_function();
00170     unsigned get_ControlChange_value();
00171     unsigned get_ProgramNumber();
00172     unsigned get_Aftertouch();
00173     unsigned get_PitchWheel();
00174     float    get_frequency();
00175     float    get_velocity_float();  // has range of [0.0 1.0] mapped to [0 127]
00176     
00177     status  setFilter();
00178     status  filter_active_sensing( bool flag );
00179     status  filter_sysex( bool flag );
00180     status  filter_clock_msg( bool flag );
00181     
00182 
00183 protected:
00184     // fundamental members
00185     long     mBufferSize;
00186     long     mFilterFlag;
00187     
00188     CSL_MIDIMessage mMsg;
00189     
00190     PmEvent mBuffer[1]; // buffer which gets filled by read()
00191     PmError mLength;    // length 
00192 
00193 };
00194 
00198 
00199 class MIDIOut : public MIDIIO {
00200 public:
00201     MIDIOut();
00202     ~MIDIOut();
00203 
00204     unsigned buffer_size();                         
00205     void     set_buffer_size( unsigned bufferSize ); // TODO: should not be called after opening.
00206     long     latency();
00207     void     set_latency( long latency );           // TODO: should not be called after opening.
00208     
00209     status open();
00210     status open( int deviceID );
00211     void     set_message( CSL_MIDIMessage msg, long when ); 
00212     status write();
00213     status write( CSL_MIDIMessage* msg, long length );  // thin wrapper for Pm_Write
00214     status write_short( CSL_MIDIMessage msg );
00215     status write_SysEX( long when, unsigned char *msg );
00216     
00217                 // convenience method for each MIDI messages
00218                 // writes directly and doesn't use member mMsg for temporal storage.
00219     status write_NoteOn( unsigned channel, unsigned pitch, unsigned velocity );     // MIDINote#, [0, 127]
00220     status write_NoteOn( unsigned channel, float frequency, float amplitude );      // [Hz], [0.0 1.0];
00221     status write_NoteOff( unsigned channel, unsigned pitch, unsigned velocity );    // MIDINote#, [0, 127]
00222     status write_NoteOff( unsigned channel, float frequency, float amplitude );     // [Hz], [0.0 1.0];
00223     status write_PolyTouch( unsigned channel, unsigned pitch, unsigned amount );
00224     status write_ControlChange( unsigned channel, unsigned function, unsigned value );
00225     status write_ProgramChange( unsigned channel, unsigned programNum );
00226     status write_Aftertouch( unsigned channel, unsigned amount );                   // [0, 127]
00227     status write_PitchWheel( unsigned channel, unsigned amount );               // [0, 16384] 
00228 
00229 protected:
00230     long        mBufferSize;
00231     long        mLatency;
00232     
00233     CSL_MIDIMessage mMsg;
00234 };
00235 
00236 }   // csl namespace
00237 
00238 #endif
00239 

Generated on Thu Sep 17 23:14:15 2009 for CSL by  doxygen 1.5.8