-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMuRecorder.cpp
More file actions
executable file
·485 lines (427 loc) · 12.4 KB
/
MuRecorder.cpp
File metadata and controls
executable file
·485 lines (427 loc) · 12.4 KB
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//
// MuRecoder.cpp
// MuMRT
//
// Created by Carlos Eduardo Mello on 3/4/19.
// Copyright © 2019 Carlos Eduardo Mello. All rights reserved.
//
#include "MuRecorder.h"
MuRecorder::MuRecorder(void)
{
#ifdef MUM_MACOSX
// CoreAudio...
midiClient = 0;
midiInPort = 0;
midiSource = 0;
#endif
#ifdef MUM_LINUX
// RtMidi...
midiIn = 0;
selectedPort = -1;
#endif
// Common
buff1.data = NULL;
buff1.max = 0;
buff1.count = 0;
buff2.data = NULL;
buff2.max = 0;
buff2.count = 0;
currentBuffer = NULL;
initialStamp = 0;
}
MuRecorder::~MuRecorder(void)
{
#ifdef MUM_MACOSX
if(midiInPort != 0 && midiSource != 0)
MIDIPortDisconnectSource(midiInPort,midiSource);
#endif
#ifdef MUM_LINUX
if(midiIn != 0)
{
midiIn->closePort();
}
#endif
if(buff1.data != NULL)
delete [] buff1.data;
if(buff2.data != NULL)
delete [] buff2.data;
}
bool MuRecorder::Init(long buffSize = DEFAULT_BUFFER_SIZE)
{
// remember when the Recorder started to run...
initialStamp = ClockStamp();
// ALLOCATE MIDI BUFFERS...
if(buff1.data == NULL)
{
buff1.data = new MuMIDIMessage[buffSize];
if(buff1.data != NULL)
{
buff1.max = buffSize;
buff1.count = 0;
}
}
if(buff2.data == NULL)
{
buff2.data = new MuMIDIMessage[buffSize];
if(buff2.data != NULL)
{
buff2.max = buffSize;
buff2.count = 0;
}
}
currentBuffer = &buff1;
#ifdef MUM_MACOSX
// INITIALIZE MIDI PORTS...
long n,i;
OSStatus err = noErr;
// create Client...
if(midiClient == 0)
{
err = MIDIClientCreate(CFSTR("MuM Recorder"), NULL, NULL, &midiClient);
if(err == noErr)
{
// Create Input Port...
err = MIDIInputPortCreate(midiClient, CFSTR("MuM Input"), MIDIInputCallback, this, &midiInPort); if(err == noErr)
{
// Count Available MIDI Sources...
n = MIDIGetNumberOfSources();
if(n != 0)
{
CFStringRef name;
char cname[64];
MIDIEndpointRef source;
// List Possible Sources...
for(i = 0; i < n; i++)
{
source = MIDIGetSource(i);
if (source != 0)
{
MIDIObjectGetStringProperty(source, kMIDIPropertyName, &name);
CFStringGetCString(name, cname, sizeof(cname), 0);
CFRelease(name);
cout << "[Source " << i << "]: " << cname << endl << endl;
}
}
// Choose the first MIDI source to get input from...
midiSource = MIDIGetSource(0);
OSStatus result;
result = MIDIPortConnectSource(midiInPort, midiSource, NULL);
if(result == noErr)
return true;
}
else
{
cout << "No MIDI destinations present!\n" << endl;
}
}
else
{
cout << "Failed to open output port!\n" << endl;
}
}
else
{
cout << "Failed to create MIDI client!\n" << endl;
}
}
else
{
cout << "Client already initialized! (call reset MIDI)\n" << endl;
}
#endif
#ifdef MUM_LINUX
// RtMidiIn constructor
try
{
midiIn = new RtMidiIn();
}
catch ( RtMidiError &error )
{
error.printMessage();
exit( EXIT_FAILURE );
}
// Check inputs.
string portName;
unsigned int nPorts = midiIn->getPortCount();
cout << "\nAvailable input sources: " << nPorts << endl;
for ( unsigned int i = 0; i<nPorts; i++ )
{
try
{
portName = midiIn->getPortName(i);
}
catch ( RtMidiError &error )
{
error.printMessage();
}
cout << " Input Port #" << i << ": " << portName << '\n';
}
midiIn->openPort( 0, "MuM Input" );
selectedPort= 0;
midiIn->setCallback( &MIDIInputCallback, (void*)this );
#endif
return false;
}
bool MuRecorder::SelectMIDISource(int sourceNumber)
{
#ifdef MUM_MACOSX
OSStatus result;
if(midiSource != 0)
{
result = MIDIPortDisconnectSource(midiInPort,midiSource);
if(result != noErr)
cout << "Couldn't disconnect from previously selected source..." << endl;
}
if(sourceNumber >= 0)
{
midiSource = MIDIGetSource(sourceNumber);
if (midiSource != 0)
{
result = MIDIPortConnectSource(midiInPort, midiSource, NULL);
if(result == noErr)
return true;
else
cout << endl << "MIDI Source Connection Failed!" << endl;
}
}
#endif
#ifdef MUM_LINUX
if(sourceNumber >= 0)
{
midiIn->closePort();
midiIn->openPort(sourceNumber,"MuM Input");
}
#endif
return false;
}
void MuRecorder::ToggleCurrentBuffer(void)
{
if (currentBuffer == &buff1)
currentBuffer = &buff2;
else
currentBuffer = &buff1;
}
MuMIDIBuffer MuRecorder::GetData(void)
{
MuMIDIBuffer outBuffer;
outBuffer.data = NULL;
outBuffer.max = 0;
outBuffer.count = 0;
MuMIDIBuffer * previous;
// Keep the address of where the data is...
previous = currentBuffer;
// redirect input to the other buffer...
ToggleCurrentBuffer();
// allocate memory to copy data...
long i;
long n = previous->count;
if(n > 0)
{
outBuffer.data = new MuMIDIMessage[n];
if(outBuffer.data != NULL)
{
for(i = 0; i < n; i++)
outBuffer.data[i] = previous->data[i];
outBuffer.max = n;
outBuffer.count = n;
// flush previous buffer, so we don't run out of space
previous->count = 0;
}
}
return outBuffer;
}
#ifdef MUM_MACOSX
void MuRecorder::MIDIInputCallback (const MIDIPacketList *list, void *procRef,void *srcRef)
{
//cout << "MIDIInputCallback was called" << endl;
unsigned int i;
MuRecorder * recorder = (MuRecorder *)procRef;
const MIDIPacket *packet = &(list->packet[0]);
UInt16 nBytes,j;
MuMIDIMessage msg;
msg.time = ((ClockStamp() - recorder->initialStamp)/ (float)ONE_SECOND);
for ( i = 0; i < list->numPackets; i++)
{
nBytes = packet->length;
j = 0;
while(j < nBytes)
{
Byte next = packet->data[j];
Byte mask = (next & 0xF0);
// if this is a voice message...
if( (mask >= MU_NOTE_OFF) && (mask <= MU_PITCH_BEND))
{
// extract and store it...
msg.status = next;
msg.data1 = packet->data[j+1];
if(mask == MU_PROGRAM_CHANGE)
{
msg.data2 = msg.data1; // either this or 0
j += 2;
}
else
{
msg.data2 = packet->data[j+2];
j += 3;
}
recorder->AddMessageToBuffer(msg);
}
else // otherwise just ignore it and move to the next byte...
{
j++;
}
}
// when done with this packet, move to the next...
packet = MIDIPacketNext(packet);
}
}
#endif
#ifdef MUM_LINUX
void MuRecorder::MIDIInputCallback(double timeStamp, vector< unsigned char > * message, void *userData)
{
//cout << "MIDIInputCallback was called" << endl;
unsigned int i;
MuRecorder * recorder = (MuRecorder *)userData;
MuMIDIMessage msg;
msg.time = ((ClockStamp() - recorder->initialStamp)/ (float)ONE_SECOND);
unsigned int nBytes = message->size();
if(nBytes >= 2 && nBytes <= 3)
{
unsigned char next = (unsigned char)message->at(0);
unsigned char mask = (next & 0xF0);
// if this is a voice message...
if( (mask >= MU_NOTE_OFF) && (mask <= MU_PITCH_BEND))
{
// extract and store it...
msg.status = next;
msg.data1 = (unsigned char)message->at(1);
if(mask == MU_PROGRAM_CHANGE)
{
msg.data2 = msg.data1; // either this or 0
}
else
{
msg.data2 = (unsigned char)message->at(2);
}
recorder->AddMessageToBuffer(msg);
}
}
}
#endif
void MuRecorder::AddMessageToBuffer(MuMIDIMessage msg)
{
if(currentBuffer->count < (currentBuffer->max - 1))
{
currentBuffer->data[currentBuffer->count] = msg;
currentBuffer->count++;
}
}
// Obs.:
// 1) upon return, if .count == 0 or .data == NULL, join operation failed
// 2) calling code is responsible for releasing buffer memory.
MuMIDIBuffer MuRecorder::JoinMIDIBuffers(MuMIDIBuffer buff1, MuMIDIBuffer buff2)
{
MuMIDIBuffer res;
res.data = NULL;
res.max = 0;
res.count = 0;
long i;
long n = buff1.count + buff2.count;
if(n > 0)
{
// allocate memory enough to put data from both buffers...
MuMIDIMessage * temp = new MuMIDIMessage[n];
if(temp != NULL)
{
// copy data from buff1...
if((buff1.data != NULL) && (buff1.count > 0))
{
for (i = 0; i < buff1.count; i++)
temp[i] = buff1.data[i];
}
// copy data from buff2...
if((buff2.data != NULL) && (buff2.count > 0))
{
for(i = 0; i < buff2.count; i++)
temp[i+buff1.count] = buff2.data[i];
}
res.data = temp;
res.max = n;
res.count = n;
}
}
return res;
}
MuMIDIBuffer MuRecorder::ExtractInvalidNotes(MuMIDIBuffer buff)
{
MuMIDIBuffer newBuff;
MuMIDIMessage first,second;
long i,j,k,n;
k = 0;
bool matchFound = false;
n = buff.count;
newBuff.data = new MuMIDIMessage[n];
if(newBuff.data)
{
newBuff.max = n;
newBuff.count = n;
// for each noteOn event in the input buffer, check remaining events for a matching noteOff
for(i = 0; i < n; i++)
{
first = buff.data[i];
matchFound = false;
if(((first.status & 0xF0) == 0x90) && (first.data2 != 0))
{
for(j = i+1; j < n; j++)
{
second = buff.data[j];
if( // this is a noteOff or...
(((second.status & 0xF0) == 0x80) ||
// a noteOn with key velocity zero...
(((second.status & 0xF0) == 0x90) && (second.data2 == 0))) &&
// and the event is for the same channel and same pitch...
(((second.status & 0x0F) == (first.status & 0x0F)) && (second.data1 == first.data1))
)
{
matchFound = true;
break;
}
}
if(!matchFound)
{
newBuff.data[k] = first;
k++;
}
}
}
}
newBuff.count = k;
return newBuff;
}
MuMIDIBuffer MuRecorder::ExtractEventsOfType(unsigned char eventType, MuMIDIBuffer buff )
{
MuMIDIBuffer newBuff;
MuMIDIMessage event;
long i,j,n;
j = 0;
n = buff.count;
newBuff.data = new MuMIDIMessage[n];
if(newBuff.data)
{
newBuff.max = n;
newBuff.count = n;
// for each event in the input buffer, check to see if
// its type corresponds to the requested type...
for(i = 0; i < n; i++)
{
event = buff.data[i];
if( (event.status & 0xF0) == eventType)
{
newBuff.data[j] = event;
j++;
}
}
}
newBuff.count = j;
return newBuff;
}