CSL  6.0
MIDIIOP.h
Go to the documentation of this file.
1 ///
2 /// MIDIIO.h -- MIDI IO using PortMIDI
3 ///
4 /// See the copyright notice and acknowledgment of authors in the file COPYRIGHT
5 ///
6 
7 #ifndef CSL_MIDIIO_H
8 #define CSL_MIDIIO_H
9 
10 #include "CSL_Types.h"
11 #include "CGestalt.h"
12 
13 #include "math.h"
14 #include "portmidi.h"
15 #include "porttime.h"
16 
17 // copied from 240_test.c. need to sort out where it belongs.
18 
19 #define OUTPUT_BUFFER_SIZE 0
20 #define DRIVER_INFO NULL
21 #define TIME_PROC Pt_Time
22 #define TIME_INFO NULL
23 #define TIME_START Pt_Start(1, 0, 0) /* timer started w/millisecond accuracy */
24 #define MIDI_THRU NULL
25 
26 // PmDeviceInfo data structure taken from portmidi.h
27 //
28 // typedef struct {
29 // int structVersion;
30 // const char *interf; /* underlying MIDI API, e.g. MMSystem or DirectX */
31 // const char *name; /* device name, e.g. USB MidiSport 1x1 */
32 // int input; /* true iff input is available */
33 // int output; /* true iff output is available */
34 // int opened; /* used by generic PortMidi code to do error checking on arguments */
35 // } PmDeviceInfo;
36 
37 // PmEvent data structure taken from portmidi.h
38 // typedef long PmMessage;
39 // typedef struct {
40 // PmMessage message;
41 // PmTimestamp timestamp;
42 // } PmEvent;
43 
44 namespace csl {
45 
46 /// CSL_MIDIMessageType
47 
48 typedef enum {
49  kNone = 0,
50  kNoteOff = 8,
51  kNoteOn = 9,
52  kPolyTouch = 10,
53  kControlChange = 11,
54  kProgramChange = 12,
55  kAftertouch = 13,
56  kPitchWheel = 14,
57  kSysEX = 15
59 
60 /// CSL_MIDIMessage
61 
63 
64 public:
66  unsigned channel; // 0-indexed, so from 0 to 15
67  unsigned data1;
68  unsigned data2;
69  long time;
70 
72 // CSL_MIDIMessage(CSL_MIDIMessageType, ch, d1, d2 );
73 };
74 
75 /// copy_CSL_MIDIMessage -- copies CSL_MIDIMessage
76 
78 
79 /// CSL_MIDIMessageToPmEvent -- converts CSL_MIDIMessage to PmEvent
80 
81 void CSL_MIDIMessageToPmEvent(CSL_MIDIMessage* cslMIDI, PmEvent* event );
82 
83 /// PmEventToCSL_MIDIMessage -- converts PmEvent to CSL_MIDIMessage
84 
85 void PmEventToCSL_MIDIMessage( PmEvent* event, CSL_MIDIMessage* cslMIDI );
86 
87 /// Message_ChannelToStatus -- converts from message and channel to status byte
88 
89 unsigned Message_ChannelToStatus(CSL_MIDIMessageType message, unsigned channel );
90 
91 ///
92 /// MIDIIO class
93 ///
94 
95 class MIDIIO {
96 public:
97  MIDIIO();
98  virtual ~MIDIIO();
99 
100  virtual void open() { }; ///< can't open the abstract class
101  bool is_open(); ///< true if MIDI stream is opened.
102  void close(); ///< closing MIDI stream
103  void dump_device_info(); ///< printing device info for all devices.
104  void dump_count_devices(); ///< printing total number of devices available
105 
106  ///< thin wrapper for PortMidi functions. made available for flexibility.
107  int count_devices();
108  int get_default_input_id();
109  int get_default_output_id();
110  const PmDeviceInfo* get_device_info(int deviceID );
111 
112 protected:
113  PmStream * mMIDIStream; ///< opened stream
114  PmDeviceID mDeviceID; ///< device ID which will/is opened.
115 
116  ///< static to keep track of Pm_Initialize() / Pm_Terminate()
117  static bool mIsInitialized;
118  static unsigned mNumInstantiated;
119  static bool mIsPortTimeStarted;
120 
121  ///< status indicators
122  bool mIsInput;
123  bool mIsOutput;
124  bool mIsOpen;
125 
126  void handle_error(PmError err);
127 };
128 
129 ///
130 /// MIDIIn class
131 ///
132 
133 class MIDIIn : public MIDIIO {
134 public:
135  MIDIIn();
136 
137  unsigned buffer_size();
138  void set_buffer_size(unsigned bufferSize );
139  void open(); ///< method for opening the default stream.
140  void open(int deviceID);
141  bool poll();
142  void read(); ///< generic read method. gets MIDI message and store it in mBuffer
143  void read_interpret(); ///< read method and sets internal flag for which message was received
144 
145  PmEvent buffer() { return mBuffer[0]; }
146  void dump_buffer();
147 
149  void dump_CSL_MIDIMessage();
150 
151  bool is_NoteOn_received();
152  bool is_NoteOff_received();
153  bool is_PolyTouch_received();
156  bool is_Aftertouch_received();
157  bool is_PitchWheel_received();
158  bool is_SysEX_received();
159 
160  unsigned get_note();
161  unsigned get_velocity();
162  unsigned get_PolyAftertouch();
163  unsigned get_ControlChange_function();
164  unsigned get_ControlChange_value();
165  unsigned get_ProgramNumber();
166  unsigned get_Aftertouch();
167  unsigned get_PitchWheel();
168  float get_frequency();
169  float get_velocity_float(); ///< has range of [0.0 1.0] mapped to [0 127]
170 
171  void setFilter();
172  void filter_active_sensing(bool flag );
173  void filter_sysex(bool flag );
174  void filter_clock_msg(bool flag );
175 
176 
177 protected:
178  /// fundamental members
181 
183 
184  PmEvent mBuffer[1]; ///< buffer which gets filled by read()
185  PmError mLength; ///< length
186 
187 };
188 
189 ///
190 /// MIDIOut class
191 ///
192 
193 class MIDIOut : public MIDIIO {
194 public:
195  MIDIOut();
196  ~MIDIOut();
197 
198  unsigned buffer_size();
199  void set_buffer_size(unsigned bufferSize ); // TODO: should not be called after opening.
200  long latency();
201  void set_latency(long latency ); // TODO: should not be called after opening.
202 
203  void open();
204  void open(int deviceID );
205  void set_message(CSL_MIDIMessage msg, long when );
206  void write();
207  void write(CSL_MIDIMessage* msg, long length ); ///< thin wrapper for Pm_Write
208  void write_short(CSL_MIDIMessage msg );
209  void write_SysEX(long when, unsigned char *msg );
210 
211  /// convenience method for each MIDI messages
212  /// writes directly and doesn't use member mMsg for temporal storage.
213  void write_NoteOn(unsigned channel, unsigned pitch, unsigned velocity ); ///< MIDINote#, [0, 127]
214  void write_NoteOn(unsigned channel, float frequency, float amplitude ); ///< [Hz], [0.0 1.0];
215  void write_NoteOff(unsigned channel, unsigned pitch, unsigned velocity ); ///< MIDINote#, [0, 127]
216  void write_NoteOff(unsigned channel, float frequency, float amplitude ); ///< [Hz], [0.0 1.0];
217  void write_PolyTouch(unsigned channel, unsigned pitch, unsigned amount );
218  void write_ControlChange(unsigned channel, unsigned function, unsigned value );
219  void write_ProgramChange(unsigned channel, unsigned programNum );
220  void write_Aftertouch(unsigned channel, unsigned amount ); ///< [0, 127]
221  void write_PitchWheel(unsigned channel, unsigned amount ); ///< [0, 16384]
222 
223 protected:
224  long mBufferSize;
225  long mLatency;
226 
228 };
229 
230 } // csl namespace
231 
232 #endif
void setFilter()
Definition: MIDIIOP.cpp:404
unsigned buffer_size()
Definition: MIDIIOP.cpp:174
MIDIIO class: superclass of in and out; has a message buffer and current messages It's a model so you...
Definition: MIDIIOJ.h:86
bool mIsInput
Definition: MIDIIOP.h:122
void write()
Definition: MIDIIOP.cpp:534
void write_PitchWheel(unsigned channel, unsigned amount)
[0, 16384]
Definition: MIDIIOP.cpp:668
AdditiveInstrument.h – Sum-of-sines synthesis instrument class.
Definition: Accessor.h:17
PmEvent buffer()
Definition: MIDIIOP.h:145
bool is_PitchWheel_received()
Definition: MIDIIOP.cpp:385
unsigned get_PolyAftertouch()
Definition: MIDIIOP.cpp:395
CSL_MIDIMessage message()
Definition: MIDIIOP.h:148
void open()
method for opening the default stream.
Definition: MIDIIOP.cpp:182
bool is_Aftertouch_received()
Definition: MIDIIOP.cpp:381
unsigned data1
Definition: MIDIIOP.h:67
long mBufferSize
fundamental members
Definition: MIDIIOP.h:179
MIDIIn class is-a MidiInputCallback too, and an "input-ready" flag.
Definition: MIDIIOJ.h:130
bool is_NoteOff_received()
Definition: MIDIIOP.cpp:365
unsigned channel
Definition: MIDIIOP.h:66
long mBufferSize
Definition: MIDIIOJ.h:177
CSL_MIDIMessageType
CSL_MIDIMessageType.
Definition: MIDIIOP.h:48
void filter_active_sensing(bool flag)
Definition: MIDIIOP.cpp:411
int count_devices()
Definition: MIDIIOP.cpp:138
virtual void open()
Definition: MIDIIOP.h:100
bool mIsOpen
instance status indicators
Definition: MIDIIOJ.h:113
bool is_SysEX_received()
Definition: MIDIIOP.cpp:389
void set_buffer_size(unsigned bufferSize)
Definition: MIDIIOP.cpp:503
unsigned get_PitchWheel()
Definition: MIDIIOP.cpp:400
void open()
Definition: MIDIIOP.cpp:507
void filter_sysex(bool flag)
Definition: MIDIIOP.cpp:436
MIDIIO()
< It's a model & sends itself "changed"
Definition: MIDIIOJ.cpp:75
float get_velocity_float()
has range of [0.0 1.0] mapped to [0 127]
Definition: MIDIIOP.cpp:402
const PmDeviceInfo * get_device_info(int deviceID)
Definition: MIDIIOP.cpp:150
CSL_MIDIMessage mMsg
Definition: MIDIIOP.h:182
int get_default_input_id()
Definition: MIDIIOP.cpp:142
void filter_clock_msg(bool flag)
Definition: MIDIIOP.cpp:461
CSL_MIDIMessage mMsg
Definition: MIDIIOP.h:227
unsigned buffer_size()
Definition: MIDIIOP.cpp:502
bool is_PolyTouch_received()
Definition: MIDIIOP.cpp:369
void copy_CSL_MIDIMessage(CSL_MIDIMessage *source, CSL_MIDIMessage *dest)
copy_CSL_MIDIMessage – copies CSL_MIDIMessage
Definition: MIDIIOP.cpp:25
unsigned get_velocity()
Definition: MIDIIOP.cpp:394
bool is_ProgramChange_received()
Definition: MIDIIOP.cpp:377
void write_short(CSL_MIDIMessage msg)
Definition: MIDIIOP.cpp:553
unsigned get_ControlChange_function()
Definition: MIDIIOP.cpp:396
PmStream * mMIDIStream
opened stream
Definition: MIDIIOP.h:113
void set_buffer_size(unsigned bufferSize)
Definition: MIDIIOP.cpp:178
virtual ~MIDIIO()
Definition: MIDIIOJ.cpp:83
unsigned get_ControlChange_value()
Definition: MIDIIOP.cpp:397
int get_default_output_id()
Definition: MIDIIOP.cpp:146
void write_PolyTouch(unsigned channel, unsigned pitch, unsigned amount)
Definition: MIDIIOP.cpp:620
float get_frequency()
Definition: MIDIIOP.cpp:401
long mFilterFlag
Definition: MIDIIOP.h:180
unsigned get_note()
Definition: MIDIIOP.cpp:393
bool mIsOutput
Definition: MIDIIOP.h:123
void write_NoteOn(unsigned channel, unsigned pitch, unsigned velocity)
convenience method for each MIDI messages writes directly and doesn't use member mMsg for temporal st...
Definition: MIDIIOP.cpp:572
bool poll()
poll returns a bool (really quickly)
Definition: MIDIIOJ.cpp:263
PmEvent mBuffer[1]
buffer which gets filled by read()
Definition: MIDIIOP.h:184
bool is_ControlChange_received()
Definition: MIDIIOP.cpp:373
void write_NoteOff(unsigned channel, unsigned pitch, unsigned velocity)
MIDINote#, [0, 127].
Definition: MIDIIOP.cpp:596
unsigned get_Aftertouch()
Definition: MIDIIOP.cpp:399
void write_ControlChange(unsigned channel, unsigned function, unsigned value)
Definition: MIDIIOP.cpp:632
PmError mLength
length
Definition: MIDIIOP.h:185
unsigned get_ProgramNumber()
Definition: MIDIIOP.cpp:398
bool is_open()
can't open the abstract class
Definition: MIDIIOP.cpp:102
CSL_MIDIMessageType message
Definition: MIDIIOP.h:65
void write_Aftertouch(unsigned channel, unsigned amount)
[0, 127]
Definition: MIDIIOP.cpp:656
void handle_error(PmError err)
Definition: MIDIIOP.cpp:156
static unsigned mNumInstantiated
Definition: MIDIIOP.h:118
void dump_CSL_MIDIMessage()
Definition: MIDIIOP.cpp:324
PmDeviceID mDeviceID
device ID which will/is opened.
Definition: MIDIIOP.h:114
bool is_NoteOn_received()
Definition: MIDIIOP.cpp:361
void CSL_MIDIMessageToPmEvent(CSL_MIDIMessage *cslMIDI, PmEvent *event)
CSL_MIDIMessageToPmEvent – converts CSL_MIDIMessage to PmEvent.
Definition: MIDIIOP.cpp:37
long mLatency
Definition: MIDIIOJ.h:178
static bool mIsPortTimeStarted
status indicators
Definition: MIDIIOP.h:119
void read_interpret()
read method and sets internal flag for which message was received
Definition: MIDIIOP.cpp:244
long latency()
Definition: MIDIIOP.cpp:504
void write_SysEX(long when, unsigned char *msg)
Definition: MIDIIOP.cpp:565
void dump_buffer()
Definition: MIDIIOP.cpp:313
virtual void close()
closing MIDI stream
Definition: MIDIIOJ.cpp:95
void dump_device_info()
printing device info for all devices.
Definition: MIDIIOP.cpp:113
void dump_count_devices()
printing total number of devices available
Definition: MIDIIOP.cpp:132
void set_message(CSL_MIDIMessage msg, long when)
Definition: MIDIIOP.cpp:530
static bool mIsInitialized
< static flags to keep track of driver state
Definition: MIDIIOJ.h:109
void read()
generic read method. gets MIDI message and store it in mBuffer
Definition: MIDIIOP.cpp:221
unsigned Message_ChannelToStatus(CSL_MIDIMessageType message, unsigned channel)
Message_ChannelToStatus – converts from message and channel to status byte.
Definition: MIDIIOP.cpp:64
void write_ProgramChange(unsigned channel, unsigned programNum)
Definition: MIDIIOP.cpp:644
CSL_MIDIMessage.
Definition: MIDIIOP.h:62
MIDIOut class write msgs out to a device (or file)
Definition: MIDIIOJ.h:156
void PmEventToCSL_MIDIMessage(PmEvent *event, CSL_MIDIMessage *cslMIDI)
PmEventToCSL_MIDIMessage – converts PmEvent to CSL_MIDIMessage.
Definition: MIDIIOP.cpp:49
unsigned bufferSize()
Definition: MIDIIOJ.cpp:209
void set_latency(long latency)
Definition: MIDIIOP.cpp:505
unsigned data2
Definition: MIDIIOP.h:68