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
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)
00022 #define MIDI_THRU NULL
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
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;
00065 unsigned data1;
00066 unsigned data2;
00067 long time;
00068
00069 CSL_MIDIMessage();
00070
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 class MIDIIO {
00101 public:
00102 MIDIIO();
00103 virtual ~MIDIIO();
00104
00105 virtual status open() { return cslErr; };
00106 bool is_open();
00107 status close();
00108 status dump_device_info();
00109 void dump_count_devices();
00110
00112 int count_devices();
00113 int get_default_input_id();
00114 int get_default_output_id();
00115 const PmDeviceInfo* get_device_info( int deviceID );
00116
00117 protected:
00118 PmStream * mMIDIStream;
00119 PmDeviceID mDeviceID;
00120
00122 static bool mIsInitialized;
00123 static unsigned mNumInstantiated;
00124 static bool mIsPortTimeStarted;
00125
00127 bool mIsInput;
00128 bool mIsOutput;
00129 bool mIsOpen;
00130
00131 void handle_error(PmError err);
00132 };
00133
00137 class MIDIIn : public MIDIIO {
00138 public:
00139 MIDIIn();
00140
00141 unsigned buffer_size();
00142 void set_buffer_size( unsigned bufferSize );
00143 status open();
00144 status open(int deviceID);
00145 bool poll();
00146 status read();
00147 status read_interpret();
00148
00149 PmEvent buffer() { return mBuffer[0]; }
00150 void dump_buffer();
00151
00152 CSL_MIDIMessage message() { return mMsg; }
00153 void dump_CSL_MIDIMessage();
00154
00155 bool is_NoteOn_received();
00156 bool is_NoteOff_received();
00157 bool is_PolyTouch_received();
00158 bool is_ControlChange_received();
00159 bool is_ProgramChange_received();
00160 bool is_Aftertouch_received();
00161 bool is_PitchWheel_received();
00162 bool is_SysEX_received();
00163
00164 unsigned get_note();
00165 unsigned get_velocity();
00166 unsigned get_PolyAftertouch();
00167 unsigned get_ControlChange_function();
00168 unsigned get_ControlChange_value();
00169 unsigned get_ProgramNumber();
00170 unsigned get_Aftertouch();
00171 unsigned get_PitchWheel();
00172 float get_frequency();
00173 float get_velocity_float();
00174
00175 status setFilter();
00176 status filter_active_sensing( bool flag );
00177 status filter_sysex( bool flag );
00178 status filter_clock_msg( bool flag );
00179
00180
00181 protected:
00182
00183 long mBufferSize;
00184 long mFilterFlag;
00185
00186 CSL_MIDIMessage mMsg;
00187
00188 PmEvent mBuffer[1];
00189 PmError mLength;
00190
00191 };
00192
00193
00194
00195 class MIDIOut : public MIDIIO {
00196 public:
00197 MIDIOut();
00198 ~MIDIOut();
00199
00200 unsigned buffer_size();
00201 void set_buffer_size( unsigned bufferSize );
00202 long latency();
00203 void set_latency( long latency );
00204
00205 status open();
00206 status open( int deviceID );
00207 void set_message( CSL_MIDIMessage msg, long when );
00208 status write();
00209 status write( CSL_MIDIMessage* msg, long length );
00210 status write_short( CSL_MIDIMessage msg );
00211 status write_SysEX( long when, unsigned char *msg );
00212
00213
00214
00215 status write_NoteOn( unsigned channel, unsigned pitch, unsigned velocity );
00216 status write_NoteOn( unsigned channel, float frequency, float amplitude );
00217 status write_NoteOff( unsigned channel, unsigned pitch, unsigned velocity );
00218 status write_NoteOff( unsigned channel, float frequency, float amplitude );
00219 status write_PolyTouch( unsigned channel, unsigned pitch, unsigned amount );
00220 status write_ControlChange( unsigned channel, unsigned function, unsigned value );
00221 status write_ProgramChange( unsigned channel, unsigned programNum );
00222 status write_Aftertouch( unsigned channel, unsigned amount );
00223 status write_PitchWheel( unsigned channel, unsigned amount );
00224
00225 protected:
00226 long mBufferSize;
00227 long mLatency;
00228
00229 CSL_MIDIMessage mMsg;
00230 };
00231
00232 }
00233
00234 #endif
00235