aboutsummaryrefslogtreecommitdiff
path: root/src/midiio/src/MidiMessage.cpp
blob: 5f3df193983ef70a29e81f0aeb7fa44036a02437 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//
// Copyright 1997-1998 by Craig Stuart Sapp, All Rights Reserved.
// Programmer:    Craig Stuart Sapp <craig@ccrma.stanford.edu>
// Creation Date: 19 December 1997
// Last Modified: Fri Jan 23 00:26:12 GMT-0800 1998
// Last Modified: Sun Sep 20 12:17:26 PDT 1998
// Last Modified: Mon Oct 15 14:29:12 PDT 2001 (added is_note functions)
// Filename:      ...sig/src/sigControl/MidiMessage.cpp
// Web Address:   http://sig.sapp.org/src/sigControl/MidiMessage.cpp
// Syntax:        C++ 
//
// Description:   A structure for handling MIDI input messages.
//                This class stores a time stamp plus up to 
//                four MIDI message bytes.  System Exclusive messages
//                are stored in a separate array in the MidiInPort
//                class, and their storage index is passed to the
//                user through a MIDI message for later extraction
//                of the full sysex message.
//

#include "MidiMessage.h"


//////////////////////////////
//
// MidiMessage::MidiMessage
//

MidiMessage::MidiMessage(void) {
   // no initialization.  Note that the default contents are undefined.
}


// default value aTime = 0
MidiMessage::MidiMessage(int aCommand, int aP1, int aP2, int aTime) {
   time = aTime;
   command() = (uchar)aCommand;
   p1() = (uchar)aP1;
   p2() = (uchar)aP2;
}


MidiMessage::MidiMessage(const MidiMessage& aMessage) {
   time = aMessage.time;
   data = aMessage.data;
}



//////////////////////////////
//
// MidiMessage::~MidiMessage -- Destructor.
//

MidiMessage::~MidiMessage() {
   // do nothing
}



//////////////////////////////
//
// MidiMessage::command -- returns the MIDI command byte
//

uchar& MidiMessage::command(void) {
   return p0();
}



//////////////////////////////
//
// MidiMessage::operator= -- defines how objects are to be copied
//

MidiMessage& MidiMessage::operator=(const MidiMessage& aMessage) {
   time = aMessage.time;
   data = aMessage.data;
   return *this;
}



//////////////////////////////
//
// MidiMessage::operator[] -- access to byte data
//     can only access index 0 to 3, other number will be
//     chopped.
//

uchar& MidiMessage::operator[](int index) {
   return *(((uchar*)&data)+(index & 0x3));
}



//////////////////////////////
//
// MidiMessage::p0 -- returns the command byte of the
//    midi message.  Same as the command() function.
//

uchar& MidiMessage::p0(void) {
   return *(((uchar*)&data)+0);
}



//////////////////////////////
//
// MidiMessage::p1 -- returns the first parameter of the
//    midi message.  Valid if the command requires a parameter.
//

uchar& MidiMessage::p1(void) {
   return *(((uchar*)&data)+1);
}



//////////////////////////////
//
// MidiMessage::p2 -- returns the second parameter of the
//    midi message.  Valid if the command requires two parameters.
//

uchar& MidiMessage::p2(void) {
   return *(((uchar*)&data)+2);
}


//////////////////////////////
//
// MidiMessage::p3 -- returns the third parameter of the
//    midi message.  Valid if the command requires three parameters
//    (but none of the MIDI command do).
//

uchar& MidiMessage::p3(void) {
   return *(((uchar*)&data)+3);
}



//////////////////////////////
//
// MidiMessage:getArgCount -- 
//

int MidiMessage::getArgCount(void) const {
   return getParameterCount();
}



//////////////////////////////
//
// MidiMessage::getParameterCount -- returns the number
//	of valid parameters for the assiciated MIDI command
//	stored in p0.  Returns -1 if MIDI command is invalid,
//	or the number of valid parameters is unknown.
//

int MidiMessage::getParameterCount(void) const {
   int output = -1;
   switch (*(((uchar*)&data)+0) & 0xf0) {
      case 0x80:                // note-off
      case 0x90:                // note-on
      case 0xa0:                // aftertouch
      case 0xb0:                // continuous controller
      case 0xe0:                // pitch wheel
         output = 2;
         break;
      case 0xc0:                // patch change
      case 0xd0:                // channel pressure
         output = 1;
         break;
      case 0xf0:
         switch (*(((uchar*)&data)+0)) {
            // System Common Messages
            case 0xf0: return -1;     // variable for sysex
            case 0xf1: return  1;     // MIDI Time Code Quarter Frame
            case 0xf2: return  2;     // Song Position Pointer
            case 0xf3: return  1;     // Song Select
            case 0xf4: return  0;     // Undefined
            case 0xf5: return  0;     // Undefined
            case 0xf6: return  0;     // Tune Request
            case 0xf7: return  0;     // End of System exclusive
            // System Real-Time Messages
            case 0xf8: return  0;     // Timing Clock
            case 0xf9: return  0;     // Undefined
            case 0xfa: return  0;     // Start
            case 0xfb: return  0;     // Continue
            case 0xfc: return  0;     // Stop
            case 0xfd: return  0;     // Undefined
            case 0xfe: return  0;     // Active Sensing
            case 0xff: return  0;     // System Reset
         }
         return -1;
         break;
      default:                  // don't know or invalid command
         output = -1;
         break;
   }

   return output;
}



//////////////////////////////
//
// MidiMessage::getCommand -- same as command().
//

uchar MidiMessage::getCommand(void) const {
   return getP0();
}



//////////////////////////////
//
// MidiMessage::getP0 -- same as p0().
//

uchar MidiMessage::getP0(void) const {
   return *(((uchar*)&data)+0);
}



//////////////////////////////
//
// MidiMessage::getP1 -- same as p1().
//

uchar MidiMessage::getP1(void) const {
   return *(((uchar*)&data)+1);
}



//////////////////////////////
//
// MidiMessage::getP2 -- same as p2().
//

uchar MidiMessage::getP2(void) const {
   return *(((uchar*)&data)+2);
}



//////////////////////////////
//
// MidiMessage::getP3 -- same as p3().
//

uchar MidiMessage::getP3(void) const {
   return *(((uchar*)&data)+3);
}



//////////////////////////////
//
// MidiMessage::setCommand -- same as command().
//

void MidiMessage::setCommand(uchar aCommand) {
   command() = aCommand;
}



//////////////////////////////
//
// MidiMessage::setData -- sets the message bytes
//	default values: aP1 = 0, aP2 = 0, aP3 = 0.
//

void MidiMessage::setData(uchar aCommand, uchar aP1, uchar aP2, uchar aP3) {
   setCommand(aCommand);
   setP1(aP1);
   setP2(aP2);
   setP3(aP3);
}



//////////////////////////////
//
// MidiMessage::setP0 -- same as p0().
//

void MidiMessage::setP0(uchar aP0) {
   p0() = aP0;
}



//////////////////////////////
//
// MidiMessage::setP1 -- same as p1().
//

void MidiMessage::setP1(uchar aP1) {
   p1() = aP1;
}



//////////////////////////////
//
// MidiMessage::setP2 -- same as p2().
//

void MidiMessage::setP2(uchar aP2) {
   p2() = aP2;
}



//////////////////////////////
//
// MidiMessage::setP3 -- same as p3().
//

void MidiMessage::setP3(uchar aP3) {
   p3() = aP3;
}



//////////////////////////////
//
// MidiMessage::is_note -- Returns true if the MIDI command is 0x80 or 0x90.
//

int MidiMessage::is_note(void) {
   if ((p0() & 0xf0) == 0x90) {         // note-on or note-off
      return 1;
   } else if ((p0() & 0xf0) == 0x80) {   // note-off
      return 1;
   } else {
      return 0;
   }
}



//////////////////////////////
//
// MidiMessage::is_note_on -- Returns true if the MIDI command is a note
//     on message (0x90 series with p2() > 0).
//

int MidiMessage::is_note_on(void) {
   if (((p0() & 0xf0) == 0x90) &&  p2() > 0) {
      return 1;
   } else {
      return 0;
   }
}



//////////////////////////////
//
// MidiMessage::is_note_off -- Returns true if the MIDI command is a note
//     off message (0x80 series or 0x90 series with p2() == 0).
//

int MidiMessage::is_note_off(void) {
   if ((p0() & 0xf0) == 0x80) { 
      return 1;
   } else if (((p0() & 0xf0) == 0x90) && (p2() == 0)) {
      return 1;
   } else {
      return 0;
   }
}


///////////////////////////////////////////////////////////////////////////

//////////////////////////////
//
// operator<< MidiMessage
//

std::ostream& operator<<(std::ostream& out, MidiMessage& aMessage) {
   out << "(" << aMessage.time << ") " 
       << std::hex << (int)aMessage.getP0() << ": ";
   for (int i=1; i<=aMessage.getArgCount(); i++) {
      out << std::dec << (int)aMessage[i] << ' ';
   }

   return out;
}



// md5sum:	487f0fddeb8db20d81f9c039e2a460c9  - MidiMessage.cpp =css= 20030102