forked from dliganov/Chaotic-DAW
-
Notifications
You must be signed in to change notification settings - Fork 0
/
awful_midi.cpp
428 lines (355 loc) · 12.1 KB
/
awful_midi.cpp
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
/*==================================================================================================
Module Name: awful_midi.cpp
General Description: Helper MIDI host implementation. Midi host class is responsible for
interaction with system MIDI devices.
====================================================================================================
Revision History:
Author Date Major Changes description
--------------------- ------------- ------------------------------------------------------------
Anonymous 02/18/2009 Initial creation
====================================================================================================
INCLUDE FILES
==================================================================================================*/
#include "awful_midi.h"
#include "Awful_audio.h"
#include "Awful_preview.h"
//#include "Awful_utils_common.h"
/*==================================================================================================
FUNCTIONS AND CLASS MEMBER IMPLEMENTATION SECTION
==================================================================================================*/
/*==================================================================================================
BRIEF:
This is the main MIDI in callback routine. The function is got called by midi-in devices for
all non SysEx midi messages.
PARAMETERS:
[in]
source - pointer to a midi source called callback
message - the message to handle
[out]
none
OUTPUT:
none
==================================================================================================*/
void AwfulMidiInputCallback::handleIncomingMidiMessage (MidiInput* source, const MidiMessage& message)
{
unsigned char* midi_data = message.getRawData();
MidiEventType_t evt = (MidiEventType_t)(midi_data[0] & 0xf0);
/* Some stupid lousy Synths like casio CTK-481, which doesn't even have pitch wheel,
they don't send NoteOff when a key is released. They send note-on with 0-velosity instead. */
if ((evt == MidiNoteOn) && (message.getFloatVelocity() == 0))
{
evt = MidiNoteOff;
}
switch (evt)
{
case MidiNoteOn:
{
//go through the all instruments of the source and send note evt to 'em
unsigned char src_idx = owner->GetSrcIndex(source); // find the source in the array
MidiToHost_AddNoteOn(current_instr,
message.getNoteNumber(), message.getVelocity());
/*
for(unsigned char i = 0; i < MAX_NUM_INSTR_PER_MIDI_IN_SRC; ++i)
{
if (owner->m_InstrSlots[src_idx][i] != NULL)
{
MidiToHost_AddNoteOn(owner->m_InstrSlots[src_idx][i],
message.getNoteNumber(), message.getFloatVelocity());
}
}
*/
}break;
case MidiNoteOff:
{
//TODO: we got to create fake samplent/longnote of the length equal to key preassure time
//go through the all instruments of the source and send note evt to 'em
unsigned char src_idx = owner->GetSrcIndex(source); // find the source in the array
MidiToHost_AddNoteOff(current_instr, message.getNoteNumber());
/*
for(unsigned char i = 0; i < MAX_NUM_INSTR_PER_MIDI_IN_SRC; ++i)
{
if (owner->m_InstrSlots[src_idx][i] != NULL)
{
MidiToHost_AddNoteOff(owner->m_InstrSlots[src_idx][i], message.getNoteNumber());
}
}
*/
}break;
case MidiKeyAftertouch:
{
unsigned char src_idx = owner->GetSrcIndex(source); // find the source in the array
for(unsigned char i = 0; i < MAX_NUM_INSTR_PER_MIDI_IN_SRC; ++i)
{
if (owner->m_InstrSlots[src_idx][i] != NULL)
{
//Change the volume of the particular note
Preview_SetNoteVolume(message.getNoteNumber(), message.getFloatVelocity());
}
}
}break;
case MidiChannelAftertouch:
{
unsigned char src_idx = owner->GetSrcIndex(source); // find the source in the array
for(unsigned char i = 0; i < MAX_NUM_INSTR_PER_MIDI_IN_SRC; ++i)
{
if (owner->m_InstrSlots[src_idx][i] != NULL)
{
//Change the volume of the particular note
Preview_SetVolume(message.getFloatVelocity());
}
}
}break;
case MidiControlChange:
{
unsigned int num = message.getControllerNumber();
unsigned int val = message.getControllerValue();
switch(num)
{
case MidiCAllSoundOff:
case MidiCAllNotesOff:
{
//MidiToHost_StopAllNotes();
}break;
case MidiCPan:
default:
break;
}
}break;
case MidiPitchBend:
{
//MidiToHost_PitchBend();
}break;
case MidiProgramChange:
default:
break;
}
}
/*==================================================================================================
BRIEF:
This is the MIDI in callback routine for System Exclusive messages. The function is got called
by midi-in devices for SysEx midi messages.
PARAMETERS:
[in]
source - pointer to a midi source called callback
messageData - pointer to a data field of the message
numBytesSoFar - num bytes received so far
timestamp - message timestamp
[out]
none
OUTPUT:
none
==================================================================================================*/
void AwfulMidiInputCallback::handlePartialSysexMessage (MidiInput* source,
const uint8* messageData,
const int numBytesSoFar,
const double timestamp)
{
//just a stub. Don't think we really need to support SysEx messages in our sequensor
}
AwfulMidiWrapper::AwfulMidiWrapper()
{
unsigned char index = 0;
unsigned char j = 0;
m_MidiInputCallback = new AwfulMidiInputCallback(this);
for (index = 0; index < MAX_NUM_MIDI_IN_SRC; ++index)
{
m_MidiInputs[index].source = NULL;
m_MidiInputs[index].index = -1;
m_DevList[index].index = -1;
m_DevList[index].name = NULL;
for (j = 0; j < MAX_NUM_INSTR_PER_MIDI_IN_SRC; ++j)
{
m_InstrSlots[index][j] = NULL;
}
m_InstrCount[index] = 0;
}
/* Now fill the device list. Re-convert juce structures to old good C-based buffers with pointers.
Yeah, I know, I'm of old school programmer ;) */
StringArray devs = MidiInput::getDevices();
String str;
for (index = 0; index < devs.size(); ++index)
{
m_DevList[index].index = index;
str = devs[index];
m_DevList[index].name = (char*) malloc((str.length() + 1) * sizeof(char));
if (m_DevList[index].name != NULL)
{
memset(m_DevList[index].name, 0, str.length() + 1);
str.copyToBuffer(m_DevList[index].name, str.length());
}
}
m_DeviceCount = devs.size();
}
AwfulMidiWrapper::~AwfulMidiWrapper()
{
unsigned char index = 0;
if (m_MidiInputCallback != NULL)
{
delete m_MidiInputCallback;
m_MidiInputCallback = NULL;
}
for (index = 0; index < MAX_NUM_MIDI_IN_SRC; ++index)
{
if (m_MidiInputs[index].source != NULL)
{
this->CloseMidiSource(index);
}
if (m_DevList[index].name != NULL)
{
free(m_DevList[index].name);
m_DevList[index].name = NULL;
}
}
}
unsigned char AwfulMidiWrapper::GetDeviceCount() const
{
return m_DeviceCount;
}
const MIDEVICE_DESCR_T* AwfulMidiWrapper::GetDeviceDescr(unsigned char index) const
{
return &(m_DevList[index]);
}
unsigned char AwfulMidiWrapper::Attach(Instrument* instr, unsigned char juce_index)
{
unsigned int ret_val = -1;
char i = 0, j = 0, pos = -1, index = -1;
/* First of all, convert juce_index to an index of the source in internal array */
for (i = 0; i < MAX_NUM_MIDI_IN_SRC; ++i)
{
if (m_MidiInputs[i].index == juce_index)
{
index = i;
break;
}
else if ((m_MidiInputs[i].index == -1) &&
(index == -1))
{
index = i;
}
}
if (index == -1)
{
/* there are no free slots. MAX_NUM_MIDI_IN_SRC is not enough */
return index;
}
/* first of all, go through the all slots and detach the instrument if any */
for (i = 0; i < MAX_NUM_MIDI_IN_SRC; ++i)
{
/* exclude from the search the source being attached */
if (i != index)
{
for(j = 0; j < MAX_NUM_INSTR_PER_MIDI_IN_SRC; ++j)
{
if (m_InstrSlots[i][j] == instr)
{
m_InstrSlots[i][j] = NULL;
--(m_InstrCount[i]);
if (m_InstrCount[i] <= 0)
{
this->CloseMidiSource(index);
}
break;
}
}
}
}
for (j = 0; j < MAX_NUM_INSTR_PER_MIDI_IN_SRC; ++j)
{
if (m_InstrSlots[index][j] == instr)
{
/* If the instrument already attached to the source, just return the index */
return j;
}
else if ((m_InstrSlots[index][j] == NULL) &&
(pos == -1))
{
pos = j;
}
}
if (pos != -1)
{
/* went through the slots and didn't find any match, attach then */
if (true == this->OpenMidiSource(juce_index))
{
m_InstrSlots[index][pos] = instr;
++(m_InstrCount[index]);
}
else
{
/* Error happened. Can't open MidiSource */
pos = -1;
}
}
return pos;
}
void AwfulMidiWrapper::Detach(Instrument* instr)
{
unsigned char i,j;
for (i = 0; i < MAX_NUM_MIDI_IN_SRC; ++i)
{
for(j = 0; j < MAX_NUM_INSTR_PER_MIDI_IN_SRC; ++j)
{
if (m_InstrSlots[i][j] == instr)
{
m_InstrSlots[i][j] = NULL;
--(m_InstrCount[i]);
if (m_InstrCount[i] <= 0)
{
this->CloseMidiSource(i);
}
break;
}
}
}
}
bool AwfulMidiWrapper::OpenMidiSource(unsigned char juce_index)
{
bool ret_val = true;
bool open = false;
char index = -1, i = 0;
/* First of all, convert juce_index to an index of the source in internal array */
for (i = 0; i < MAX_NUM_MIDI_IN_SRC; ++i)
{
if (m_MidiInputs[i].index == juce_index)
{
index = i;
open = true;
break;
}
else if ((m_MidiInputs[i].index == -1) &&
(index == -1))
{
index = i;
}
}
if (index != -1)
{
if (open == false)
{
if (NULL != (m_MidiInputs[index].source = MidiInput::openDevice(juce_index, m_MidiInputCallback)))
{
m_MidiInputs[index].index = juce_index;
m_MidiInputs[index].source->start();
}
else
{
ret_val = false;
}
}
}
else
{
ret_val = false;
}
return ret_val;
}
void AwfulMidiWrapper::CloseMidiSource(unsigned char index)
{
if (m_MidiInputs[index].source != NULL)
{
m_MidiInputs[index].source->stop();
delete m_MidiInputs[index].source;
m_MidiInputs[index].source = NULL;
m_MidiInputs[index].index = -1;
}
}