/
midi_input.cpp
492 lines (405 loc) · 13.7 KB
/
midi_input.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
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
485
486
487
488
489
490
491
492
/*
* Hydrogen
* Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net]
*
* http://www.hydrogen-music.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY, without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include <hydrogen/IO/MidiInput.h>
#include <hydrogen/event_queue.h>
#include <hydrogen/Preferences.h>
#include <hydrogen/hydrogen.h>
#include <hydrogen/basics/instrument.h>
#include <hydrogen/basics/instrument_list.h>
#include <hydrogen/basics/note.h>
#include <hydrogen/midi_action.h>
#include <hydrogen/audio_engine.h>
#include <hydrogen/midi_map.h>
namespace H2Core
{
MidiInput::MidiInput( const char* class_name )
: Object( class_name )
, m_bActive( false )
, __hihat_cc_openess ( 127 )
{
//INFOLOG( "INIT" );
}
MidiInput::~MidiInput()
{
//INFOLOG( "DESTROY" );
}
void MidiInput::handleMidiMessage( const MidiMessage& msg )
{
EventQueue::get_instance()->push_event( EVENT_MIDI_ACTIVITY, -1 );
INFOLOG( "[start of handleMidiMessage]" );
INFOLOG( QString("[handleMidiMessage] channel: %1").arg(msg.m_nChannel) );
INFOLOG( QString("[handleMidiMessage] val1: %1").arg( msg.m_nData1 ) );
INFOLOG( QString("[handleMidiMessage] val2: %1").arg( msg.m_nData2 ) );
// midi channel filter for all messages
bool bIsChannelValid = true;
Preferences* pPref = Preferences::get_instance();
if ( pPref->m_nMidiChannelFilter != -1
&& pPref->m_nMidiChannelFilter != msg.m_nChannel
) {
bIsChannelValid = false;
}
// exclude all midi channel filter independent messages
int type = msg.m_type;
if ( MidiMessage::SYSEX == type
|| MidiMessage::SYSTEM_EXCLUSIVE == type
|| MidiMessage::START == type
|| MidiMessage::CONTINUE == type
|| MidiMessage::STOP == type
|| MidiMessage::SONG_POS == type
|| MidiMessage::QUARTER_FRAME == type
) {
bIsChannelValid = true;
}
if ( !bIsChannelValid) return;
Hydrogen* pHydrogen = Hydrogen::get_instance();
switch ( type ) {
case MidiMessage::SYSEX:
handleSysexMessage( msg );
break;
case MidiMessage::NOTE_ON:
INFOLOG("This is a NOTE ON message.");
handleNoteOnMessage( msg );
break;
case MidiMessage::NOTE_OFF:
INFOLOG("This is a NOTE OFF message.");
handleNoteOffMessage( msg, false );
break;
case MidiMessage::POLYPHONIC_KEY_PRESSURE:
//ERRORLOG( "POLYPHONIC_KEY_PRESSURE event not handled yet" );
INFOLOG( QString( "[handleMidiMessage] POLYPHONIC_KEY_PRESSURE Parameter: %1, Value: %2")
.arg( msg.m_nData1 ).arg( msg.m_nData2 ) );
handlePolyphonicKeyPressureMessage( msg );
break;
case MidiMessage::CONTROL_CHANGE:
INFOLOG( QString( "[handleMidiMessage] CONTROL_CHANGE Parameter: %1, Value: %2")
.arg( msg.m_nData1 ).arg( msg.m_nData2 ) );
handleControlChangeMessage( msg );
break;
case MidiMessage::PROGRAM_CHANGE:
INFOLOG( QString( "[handleMidiMessage] PROGRAM_CHANGE event, seting next pattern to %1" )
.arg( msg.m_nData1 ) );
// Hydrogen::get_instance()->sequencer_setNextPattern(msg.m_nData1, false, false);
handleProgramChangeMessage( msg );
break;
case MidiMessage::CHANNEL_PRESSURE:
ERRORLOG( "CHANNEL_PRESSURE event not handled yet" );
break;
case MidiMessage::PITCH_WHEEL:
ERRORLOG( "PITCH_WHEEL event not handled yet" );
break;
case MidiMessage::SYSTEM_EXCLUSIVE:
ERRORLOG( "SYSTEM_EXCLUSIVE event not handled yet" );
break;
case MidiMessage::START: /* Start from position 0 */
INFOLOG( "START event" );
if ( pHydrogen->getState() != STATE_PLAYING ) {
pHydrogen->setPatternPos( 0 );
pHydrogen->setTimelineBpm();
pHydrogen->sequencer_play();
}
break;
case MidiMessage::CONTINUE: /* Just start */
ERRORLOG( "CONTINUE event" );
if ( pHydrogen->getState() != STATE_PLAYING )
pHydrogen->sequencer_play();
break;
case MidiMessage::STOP: /* Stop in current position i.e. Pause */
INFOLOG( "STOP event" );
if ( pHydrogen->getState() == STATE_PLAYING )
pHydrogen->sequencer_stop();
break;
case MidiMessage::SONG_POS:
ERRORLOG( "SONG_POS event not handled yet" );
break;
case MidiMessage::QUARTER_FRAME:
WARNINGLOG( "QUARTER_FRAME event not handled yet" );
break;
case MidiMessage::UNKNOWN:
ERRORLOG( "Unknown midi message" );
break;
default:
ERRORLOG( QString( "unhandled midi message type: %1" ).arg( msg.m_type ) );
}
INFOLOG("[end of handleMidiMessage]");
}
void MidiInput::handleControlChangeMessage( const MidiMessage& msg )
{
//INFOLOG( QString( "[handleMidiMessage] CONTROL_CHANGE Parameter: %1, Value: %2" ).arg( msg.m_nData1 ).arg( msg.m_nData2 ) );
Hydrogen *pEngine = Hydrogen::get_instance();
MidiActionManager *aH = MidiActionManager::get_instance();
MidiMap *mM = MidiMap::get_instance();
MidiAction *pAction = mM->getCCAction( msg.m_nData1 );
pAction->setParameter2( QString::number( msg.m_nData2 ) );
aH->handleAction( pAction );
if(msg.m_nData1 == 04)
__hihat_cc_openess = msg.m_nData2;
pEngine->lastMidiEvent = "CC";
pEngine->lastMidiEventParameter = msg.m_nData1;
}
void MidiInput::handleProgramChangeMessage( const MidiMessage& msg )
{
INFOLOG( QString( "[handleMidiMessage] PROGRAM_CHANGE Value: %1" ).arg( msg.m_nData1 ) );
Hydrogen *pEngine = Hydrogen::get_instance();
MidiActionManager *aH = MidiActionManager::get_instance();
MidiMap *mM = MidiMap::get_instance();
MidiAction *pAction = mM->getPCAction();
pAction->setParameter2( QString::number( msg.m_nData1 ) );
aH->handleAction( pAction );
pEngine->lastMidiEvent = "PROGRAM_CHANGE";
pEngine->lastMidiEventParameter = msg.m_nData1;
}
void MidiInput::handleNoteOnMessage( const MidiMessage& msg )
{
// INFOLOG( "handleNoteOnMessage" );
int nNote = msg.m_nData1;
float fVelocity = msg.m_nData2 / 127.0;
if ( fVelocity == 0 ) {
handleNoteOffMessage( msg, false );
return;
}
MidiActionManager * aH = MidiActionManager::get_instance();
MidiMap * mM = MidiMap::get_instance();
Hydrogen *pEngine = Hydrogen::get_instance();
pEngine->lastMidiEvent = "NOTE";
pEngine->lastMidiEventParameter = msg.m_nData1;
bool action = aH->handleAction( mM->getNoteAction( msg.m_nData1 ) );
if ( action && Preferences::get_instance()->m_bMidiDiscardNoteAfterAction)
{
return;
}
bool bPatternSelect = false;
if ( bPatternSelect ) {
int patternNumber = nNote - 36;
//INFOLOG( QString( "next pattern = %1" ).arg( patternNumber ) );
pEngine->sequencer_setNextPattern( patternNumber, false, false );
} else {
static const float fPan_L = 0.5f;
static const float fPan_R = 0.5f;
int nInstrument = nNote - 36;
if ( nInstrument < 0 ) {
if(Preferences::get_instance()->__playselectedinstrument)
{
//we're in instrument mode -> we accept all notes
} else {
//we're in drumkit mode, lets drop everything < 36
nInstrument = 0;
return;
}
}
if ( nInstrument > ( MAX_INSTRUMENTS -1 ) ) {
nInstrument = MAX_INSTRUMENTS - 1;
}
InstrumentList *instrList = pEngine->getSong()->get_instrument_list();
Instrument *instr = instrList->get( nInstrument );
/*
Only look to change instrument if the
current note is actually of hihat and
hihat openess is outside the instrument selected
*/
if ( instr != NULL &&
instr->is_hihat() &&
( __hihat_cc_openess < instr->get_lower_cc() || __hihat_cc_openess > instr->get_higher_cc() ) )
{
for(int i=0 ; i<=instrList->size() ; i++)
{
Instrument *instr_contestant = instrList->get( i );
if( instr_contestant != NULL &&
instr_contestant->is_hihat() &&
__hihat_cc_openess >= instr_contestant->get_lower_cc() &&
__hihat_cc_openess <= instr_contestant->get_higher_cc() )
{
nInstrument = i;
break;
}
}
}
pEngine->addRealtimeNote( nInstrument, fVelocity, fPan_L, fPan_R, 0.0, false, true, nNote );
}
__noteOnTick = pEngine->__getMidiRealtimeNoteTickPosition();
}
/*
EDrums (at least Roland TD-6V) uses PolyphonicKeyPressure
for cymbal choke.
If the message is 127 (choked) we send a NoteOff
*/
void MidiInput::handlePolyphonicKeyPressureMessage( const MidiMessage& msg )
{
if( msg.m_nData2 == 127 )
handleNoteOffMessage( msg, true );
}
void MidiInput::handleNoteOffMessage( const MidiMessage& msg, bool CymbalChoke )
{
// INFOLOG( "handleNoteOffMessage" );
if ( !CymbalChoke && Preferences::get_instance()->m_bMidiNoteOffIgnore ) {
return;
}
Hydrogen *pEngine = Hydrogen::get_instance();
Song *pSong = pEngine->getSong();
__noteOffTick = pEngine->getTickPosition();
unsigned long notelength = computeDeltaNoteOnOfftime();
int nNote = msg.m_nData1;
//float fVelocity = msg.m_nData2 / 127.0; //we need this in future to controll release velocity
int nInstrument = nNote - 36;
if ( nInstrument < 0 ) {
nInstrument = 0;
}
if ( nInstrument > ( MAX_INSTRUMENTS -1 ) ) {
nInstrument = MAX_INSTRUMENTS - 1;
}
Instrument *pInstr = pSong->get_instrument_list()->get( nInstrument );
float fStep = pow( 1.0594630943593, (nNote -36) );
if ( !Preferences::get_instance()->__playselectedinstrument )
fStep = 1;
if ( Preferences::get_instance()->__playselectedinstrument ){
nInstrument = pEngine->getSelectedInstrumentNumber();
pInstr= pEngine->getSong()->get_instrument_list()->get( pEngine->getSelectedInstrumentNumber());
}
bool use_note_off = AudioEngine::get_instance()->get_sampler()->is_instrument_playing( pInstr );
if(use_note_off){
if ( Preferences::get_instance()->__playselectedinstrument ){
AudioEngine::get_instance()->get_sampler()->midi_keyboard_note_off( msg.m_nData1 );
}else
{
if ( pSong->get_instrument_list()->size() < nInstrument +1 )
return;
Note *offnote = new Note( pInstr,
0.0,
0.0,
0.0,
0.0,
-1,
0 );
offnote->set_note_off( true );
AudioEngine::get_instance()->get_sampler()->note_on( offnote );
delete offnote;
}
if(Preferences::get_instance()->getRecordEvents())
AudioEngine::get_instance()->get_sampler()->setPlayingNotelength( pInstr, notelength * fStep, __noteOnTick );
}
}
unsigned long MidiInput::computeDeltaNoteOnOfftime()
{
unsigned long __notelengthTicks = __noteOffTick - __noteOnTick;
return __notelengthTicks;
}
void MidiInput::handleSysexMessage( const MidiMessage& msg )
{
/*
General MMC message
0 1 2 3 4 5
F0 7F id 6 cmd 247
cmd:
1 stop
2 play
3 Deferred play
4 Fast Forward
5 Rewind
6 Record strobe (punch in)
7 Record exit (punch out)
8 Record ready
9 Pause
Goto MMC message
0 1 2 3 4 5 6 7 8 9 10 11 12
240 127 id 6 68 6 1 hr mn sc fr ff 247
*/
MidiActionManager * aH = MidiActionManager::get_instance();
MidiMap * mM = MidiMap::get_instance();
Hydrogen *pEngine = Hydrogen::get_instance();
pEngine->lastMidiEventParameter = msg.m_nData1;
if ( msg.m_sysexData.size() == 6 ) {
if (
( msg.m_sysexData[0] == 240 ) &&
( msg.m_sysexData[1] == 127 ) &&
//( msg.m_sysexData[2] == 0 ) &&
( msg.m_sysexData[3] == 6 ) ) {
switch ( msg.m_sysexData[4] ) {
case 1: // STOP
{
pEngine->lastMidiEvent = "MMC_STOP";
aH->handleAction(mM->getMMCAction("MMC_STOP"));
break;
}
case 2: // PLAY
{
pEngine->lastMidiEvent = "MMC_PLAY";
aH->handleAction(mM->getMMCAction("MMC_PLAY"));
break;
}
case 3: //DEFERRED PLAY
{
pEngine->lastMidiEvent = "MMC_PLAY";
aH->handleAction(mM->getMMCAction("MMC_PLAY"));
break;
}
case 4: // FAST FWD
pEngine->lastMidiEvent = "MMC_FAST_FORWARD";
aH->handleAction(mM->getMMCAction("MMC_FAST_FORWARD"));
break;
case 5: // REWIND
pEngine->lastMidiEvent = "MMC_REWIND";
aH->handleAction(mM->getMMCAction("MMC_REWIND"));
break;
case 6: // RECORD STROBE (PUNCH IN)
pEngine->lastMidiEvent = "MMC_RECORD_STROBE";
aH->handleAction(mM->getMMCAction("MMC_RECORD_STROBE"));
break;
case 7: // RECORD EXIT (PUNCH OUT)
pEngine->lastMidiEvent = "MMC_RECORD_EXIT";
aH->handleAction(mM->getMMCAction("MMC_RECORD_EXIT"));
break;
case 8: // RECORD READY
pEngine->lastMidiEvent = "MMC_RECORD_READY";
aH->handleAction(mM->getMMCAction("MMC_RECORD_READY"));
break;
case 9: //PAUSE
pEngine->lastMidiEvent = "MMC_PAUSE";
aH->handleAction(mM->getMMCAction("MMC_PAUSE"));
break;
default:
WARNINGLOG( "Unknown MMC Command" );
// midiDump( buf, nBytes );
}
}
} else if ( msg.m_sysexData.size() == 13 ) {
ERRORLOG( "MMC GOTO Message not implemented yet" );
// midiDump( buf, nBytes );
//int id = buf[2];
int hr = msg.m_sysexData[7];
int mn = msg.m_sysexData[8];
int sc = msg.m_sysexData[9];
int fr = msg.m_sysexData[10];
int ff = msg.m_sysexData[11];
char tmp[200];
sprintf( tmp, "[handleSysexMessage] GOTO %d:%d:%d:%d:%d", hr, mn, sc, fr, ff );
INFOLOG( tmp );
} else {
// sysex dump
QString sDump;
char tmpChar[64];
for ( int i = 0; i < ( int )msg.m_sysexData.size(); ++i ) {
sprintf( tmpChar, "%X ", ( int )msg.m_sysexData[ i ] );
sDump += tmpChar;
}
WARNINGLOG( QString( "Unknown SysEx message: (%1) [%2]" ).arg( msg.m_sysexData.size() ).arg( sDump ) );
}
}
};