3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005-2009 Christian Schoenebeck * |
* Copyright (C) 2005-2008 Christian Schoenebeck * |
7 |
* Copyright (C) 2009 Grigor Iliev * |
* Copyright (C) 2009-2012 Christian Schoenebeck and Grigor Iliev * |
8 |
|
* Copyright (C) 2013-2014 Christian Schoenebeck and Andreas Persson * |
9 |
* * |
* * |
10 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
11 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
42 |
pChannelRight = NULL; |
pChannelRight = NULL; |
43 |
AudioDeviceChannelLeft = -1; |
AudioDeviceChannelLeft = -1; |
44 |
AudioDeviceChannelRight = -1; |
AudioDeviceChannelRight = -1; |
|
pMidiInputPort = NULL; |
|
45 |
midiChannel = midi_chan_all; |
midiChannel = midi_chan_all; |
46 |
ResetControllers(); |
ResetControllers(); |
47 |
PortamentoMode = false; |
PortamentoMode = false; |
48 |
PortamentoTime = CONFIG_PORTAMENTO_TIME_DEFAULT; |
PortamentoTime = CONFIG_PORTAMENTO_TIME_DEFAULT; |
49 |
|
pScript = NULL; |
50 |
} |
} |
51 |
|
|
52 |
AbstractEngineChannel::~AbstractEngineChannel() { |
AbstractEngineChannel::~AbstractEngineChannel() { |
53 |
|
delete pEventQueue; |
54 |
if (pEventQueue) delete pEventQueue; |
DeleteGroupEventLists(); |
55 |
RemoveAllFxSends(); |
RemoveAllFxSends(); |
56 |
} |
} |
57 |
|
|
113 |
Pitch = 0; |
Pitch = 0; |
114 |
GlobalVolume = 1.0f; |
GlobalVolume = 1.0f; |
115 |
MidiVolume = 1.0; |
MidiVolume = 1.0; |
|
GlobalPanLeft = 1.0f; |
|
|
GlobalPanRight = 1.0f; |
|
116 |
iLastPanRequest = 64; |
iLastPanRequest = 64; |
117 |
GlobalTranspose = 0; |
GlobalTranspose = 0; |
118 |
// set all MIDI controller values to zero |
// set all MIDI controller values to zero |
181 |
int iMidiPan = int(f * 64.0f) + 64; |
int iMidiPan = int(f * 64.0f) + 64; |
182 |
if (iMidiPan > 127) iMidiPan = 127; |
if (iMidiPan > 127) iMidiPan = 127; |
183 |
else if (iMidiPan < 0) iMidiPan = 0; |
else if (iMidiPan < 0) iMidiPan = 0; |
|
GlobalPanLeft = AbstractEngine::PanCurve[128 - iMidiPan]; |
|
|
GlobalPanRight = AbstractEngine::PanCurve[iMidiPan]; |
|
184 |
iLastPanRequest = iMidiPan; |
iLastPanRequest = iMidiPan; |
185 |
} |
} |
186 |
|
|
188 |
return (pEngine) ? pEngine->pAudioOutputDevice : NULL; |
return (pEngine) ? pEngine->pAudioOutputDevice : NULL; |
189 |
} |
} |
190 |
|
|
191 |
|
/** |
192 |
|
* Gets thread safe access to the currently connected audio output |
193 |
|
* device from other threads than the lscp thread. |
194 |
|
*/ |
195 |
|
AudioOutputDevice* AbstractEngineChannel::GetAudioOutputDeviceSafe() { |
196 |
|
LockGuard lock(EngineMutex); |
197 |
|
return GetAudioOutputDevice(); |
198 |
|
} |
199 |
|
|
200 |
void AbstractEngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) { |
void AbstractEngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) { |
201 |
if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet."); |
if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet."); |
202 |
|
|
229 |
} |
} |
230 |
} |
} |
231 |
|
|
232 |
|
void AbstractEngineChannel::Connect(MidiInputPort* pMidiPort) { |
233 |
|
if (!pMidiPort) return; |
234 |
|
|
235 |
|
Sync< ArrayList<MidiInputPort*> > connections = midiInputs.back(); |
236 |
|
|
237 |
|
// check if connection already exists |
238 |
|
for (int i = 0; i < connections->size(); ++i) |
239 |
|
if ((*connections)[i] == pMidiPort) |
240 |
|
return; // to avoid endless recursion |
241 |
|
|
242 |
|
connections->add(pMidiPort); |
243 |
|
|
244 |
|
// inform MIDI port about this new connection |
245 |
|
pMidiPort->Connect(this, MidiChannel()); |
246 |
|
} |
247 |
|
|
248 |
|
void AbstractEngineChannel::Disconnect(MidiInputPort* pMidiPort) { |
249 |
|
if (!pMidiPort) return; |
250 |
|
|
251 |
|
Sync< ArrayList<MidiInputPort*> > connections = midiInputs.back(); |
252 |
|
|
253 |
|
for (int i = 0; i < connections->size(); ++i) { |
254 |
|
if ((*connections)[i] == pMidiPort) { |
255 |
|
connections->remove(i); |
256 |
|
// inform MIDI port about this disconnection |
257 |
|
pMidiPort->Disconnect(this); |
258 |
|
return; |
259 |
|
} |
260 |
|
} |
261 |
|
} |
262 |
|
|
263 |
|
void AbstractEngineChannel::DisconnectAllMidiInputPorts() { |
264 |
|
Sync< ArrayList<MidiInputPort*> > connections = midiInputs.back(); |
265 |
|
ArrayList<MidiInputPort*> clonedList = *connections; |
266 |
|
connections->clear(); |
267 |
|
for (int i = 0; i < clonedList.size(); ++i) clonedList[i]->Disconnect(this); |
268 |
|
} |
269 |
|
|
270 |
|
uint AbstractEngineChannel::GetMidiInputPortCount() { |
271 |
|
Sync< ArrayList<MidiInputPort*> > connections = midiInputs.back(); |
272 |
|
return connections->size(); |
273 |
|
} |
274 |
|
|
275 |
|
MidiInputPort* AbstractEngineChannel::GetMidiInputPort(uint index) { |
276 |
|
Sync< ArrayList<MidiInputPort*> > connections = midiInputs.back(); |
277 |
|
return (index < connections->size()) ? (*connections)[index] : NULL; |
278 |
|
} |
279 |
|
|
280 |
|
// deprecated (just for API backward compatibility) - may be removed in future |
281 |
void AbstractEngineChannel::Connect(MidiInputPort* pMidiPort, midi_chan_t MidiChannel) { |
void AbstractEngineChannel::Connect(MidiInputPort* pMidiPort, midi_chan_t MidiChannel) { |
282 |
if (!pMidiPort || pMidiPort == this->pMidiInputPort) return; |
if (!pMidiPort) return; |
283 |
DisconnectMidiInputPort(); |
|
284 |
this->pMidiInputPort = pMidiPort; |
Sync< ArrayList<MidiInputPort*> > connections = midiInputs.back(); |
285 |
this->midiChannel = MidiChannel; |
|
286 |
|
// check against endless recursion |
287 |
|
if (connections->size() == 1 && (*connections)[0] == pMidiPort && this->midiChannel == MidiChannel) |
288 |
|
return; |
289 |
|
|
290 |
|
if (!isValidMidiChan(MidiChannel)) |
291 |
|
throw MidiInputException("Invalid MIDI channel (" + ToString(int(MidiChannel)) + ")"); |
292 |
|
|
293 |
|
this->midiChannel = MidiChannel; |
294 |
|
|
295 |
|
// disconnect all currently connected MIDI ports |
296 |
|
ArrayList<MidiInputPort*> clonedList = *connections; |
297 |
|
connections->clear(); |
298 |
|
for (int i = 0; i < clonedList.size(); ++i) |
299 |
|
clonedList[i]->Disconnect(this); |
300 |
|
|
301 |
|
// connect the new port |
302 |
|
connections->add(pMidiPort); |
303 |
pMidiPort->Connect(this, MidiChannel); |
pMidiPort->Connect(this, MidiChannel); |
304 |
} |
} |
305 |
|
|
306 |
|
// deprecated (just for API backward compatibility) - may be removed in future |
307 |
void AbstractEngineChannel::DisconnectMidiInputPort() { |
void AbstractEngineChannel::DisconnectMidiInputPort() { |
308 |
MidiInputPort* pOldPort = this->pMidiInputPort; |
DisconnectAllMidiInputPorts(); |
|
this->pMidiInputPort = NULL; |
|
|
if (pOldPort) pOldPort->Disconnect(this); |
|
309 |
} |
} |
310 |
|
|
311 |
|
// deprecated (just for API backward compatibility) - may be removed in future |
312 |
MidiInputPort* AbstractEngineChannel::GetMidiInputPort() { |
MidiInputPort* AbstractEngineChannel::GetMidiInputPort() { |
313 |
return pMidiInputPort; |
return GetMidiInputPort(0); |
314 |
} |
} |
315 |
|
|
316 |
midi_chan_t AbstractEngineChannel::MidiChannel() { |
midi_chan_t AbstractEngineChannel::MidiChannel() { |
317 |
return midiChannel; |
return midiChannel; |
318 |
} |
} |
319 |
|
|
320 |
|
void AbstractEngineChannel::SetMidiChannel(midi_chan_t MidiChannel) { |
321 |
|
if (this->midiChannel == MidiChannel) return; |
322 |
|
if (!isValidMidiChan(MidiChannel)) |
323 |
|
throw MidiInputException("Invalid MIDI channel (" + ToString(int(MidiChannel)) + ")"); |
324 |
|
|
325 |
|
this->midiChannel = MidiChannel; |
326 |
|
|
327 |
|
Sync< ArrayList<MidiInputPort*> > connections = midiInputs.back(); |
328 |
|
ArrayList<MidiInputPort*> clonedList = *connections; |
329 |
|
|
330 |
|
DisconnectAllMidiInputPorts(); |
331 |
|
|
332 |
|
for (int i = 0; i < clonedList.size(); ++i) Connect(clonedList[i]); |
333 |
|
} |
334 |
|
|
335 |
void AbstractEngineChannel::Connect(VirtualMidiDevice* pDevice) { |
void AbstractEngineChannel::Connect(VirtualMidiDevice* pDevice) { |
336 |
// double buffer ... double work ... |
// double buffer ... double work ... |
337 |
{ |
{ |
365 |
* @param Key - MIDI key number of the triggered key |
* @param Key - MIDI key number of the triggered key |
366 |
* @param Velocity - MIDI velocity value of the triggered key |
* @param Velocity - MIDI velocity value of the triggered key |
367 |
*/ |
*/ |
368 |
void AbstractEngineChannel::SendNoteOn(uint8_t Key, uint8_t Velocity) { |
void AbstractEngineChannel::SendNoteOn(uint8_t Key, uint8_t Velocity, uint8_t MidiChannel) { |
369 |
if (pEngine) { |
if (pEngine) { |
370 |
|
// protection in case there are more than 1 MIDI input threads sending MIDI events to this EngineChannel |
371 |
|
LockGuard g; |
372 |
|
if (hasMultipleMIDIInputs()) g = LockGuard(MidiInputMutex); |
373 |
|
|
374 |
Event event = pEngine->pEventGenerator->CreateEvent(); |
Event event = pEngine->pEventGenerator->CreateEvent(); |
375 |
event.Type = Event::type_note_on; |
event.Type = Event::type_note_on; |
376 |
event.Param.Note.Key = Key; |
event.Param.Note.Key = Key; |
377 |
event.Param.Note.Velocity = Velocity; |
event.Param.Note.Velocity = Velocity; |
378 |
|
event.Param.Note.Channel = MidiChannel; |
379 |
|
memset(&event.Format, 0, sizeof(event.Format)); // init format speific stuff with zeroes |
380 |
event.pEngineChannel = this; |
event.pEngineChannel = this; |
381 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
382 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
else dmsg(1,("EngineChannel: Input event queue full!")); |
404 |
* @param FragmentPos - sample point position in the current audio |
* @param FragmentPos - sample point position in the current audio |
405 |
* fragment to which this event belongs to |
* fragment to which this event belongs to |
406 |
*/ |
*/ |
407 |
void AbstractEngineChannel::SendNoteOn(uint8_t Key, uint8_t Velocity, int32_t FragmentPos) { |
void AbstractEngineChannel::SendNoteOn(uint8_t Key, uint8_t Velocity, uint8_t MidiChannel, int32_t FragmentPos) { |
408 |
if (FragmentPos < 0) { |
if (FragmentPos < 0) { |
409 |
dmsg(1,("EngineChannel::SendNoteOn(): negative FragmentPos! Seems MIDI driver is buggy!")); |
dmsg(1,("EngineChannel::SendNoteOn(): negative FragmentPos! Seems MIDI driver is buggy!")); |
410 |
} |
} |
411 |
else if (pEngine) { |
else if (pEngine) { |
412 |
|
// protection in case there are more than 1 MIDI input threads sending MIDI events to this EngineChannel |
413 |
|
LockGuard g; |
414 |
|
if (hasMultipleMIDIInputs()) g = LockGuard(MidiInputMutex); |
415 |
|
|
416 |
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
417 |
event.Type = Event::type_note_on; |
event.Type = Event::type_note_on; |
418 |
event.Param.Note.Key = Key; |
event.Param.Note.Key = Key; |
419 |
event.Param.Note.Velocity = Velocity; |
event.Param.Note.Velocity = Velocity; |
420 |
|
event.Param.Note.Channel = MidiChannel; |
421 |
|
memset(&event.Format, 0, sizeof(event.Format)); // init format speific stuff with zeroes |
422 |
event.pEngineChannel = this; |
event.pEngineChannel = this; |
423 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
424 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
else dmsg(1,("EngineChannel: Input event queue full!")); |
444 |
* @param Key - MIDI key number of the released key |
* @param Key - MIDI key number of the released key |
445 |
* @param Velocity - MIDI release velocity value of the released key |
* @param Velocity - MIDI release velocity value of the released key |
446 |
*/ |
*/ |
447 |
void AbstractEngineChannel::SendNoteOff(uint8_t Key, uint8_t Velocity) { |
void AbstractEngineChannel::SendNoteOff(uint8_t Key, uint8_t Velocity, uint8_t MidiChannel) { |
448 |
if (pEngine) { |
if (pEngine) { |
449 |
|
// protection in case there are more than 1 MIDI input threads sending MIDI events to this EngineChannel |
450 |
|
LockGuard g; |
451 |
|
if (hasMultipleMIDIInputs()) g = LockGuard(MidiInputMutex); |
452 |
|
|
453 |
Event event = pEngine->pEventGenerator->CreateEvent(); |
Event event = pEngine->pEventGenerator->CreateEvent(); |
454 |
event.Type = Event::type_note_off; |
event.Type = Event::type_note_off; |
455 |
event.Param.Note.Key = Key; |
event.Param.Note.Key = Key; |
456 |
event.Param.Note.Velocity = Velocity; |
event.Param.Note.Velocity = Velocity; |
457 |
|
event.Param.Note.Channel = MidiChannel; |
458 |
|
memset(&event.Format, 0, sizeof(event.Format)); // init format speific stuff with zeroes |
459 |
event.pEngineChannel = this; |
event.pEngineChannel = this; |
460 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
461 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
else dmsg(1,("EngineChannel: Input event queue full!")); |
483 |
* @param FragmentPos - sample point position in the current audio |
* @param FragmentPos - sample point position in the current audio |
484 |
* fragment to which this event belongs to |
* fragment to which this event belongs to |
485 |
*/ |
*/ |
486 |
void AbstractEngineChannel::SendNoteOff(uint8_t Key, uint8_t Velocity, int32_t FragmentPos) { |
void AbstractEngineChannel::SendNoteOff(uint8_t Key, uint8_t Velocity, uint8_t MidiChannel, int32_t FragmentPos) { |
487 |
if (FragmentPos < 0) { |
if (FragmentPos < 0) { |
488 |
dmsg(1,("EngineChannel::SendNoteOff(): negative FragmentPos! Seems MIDI driver is buggy!")); |
dmsg(1,("EngineChannel::SendNoteOff(): negative FragmentPos! Seems MIDI driver is buggy!")); |
489 |
} |
} |
490 |
else if (pEngine) { |
else if (pEngine) { |
491 |
|
// protection in case there are more than 1 MIDI input threads sending MIDI events to this EngineChannel |
492 |
|
LockGuard g; |
493 |
|
if (hasMultipleMIDIInputs()) g = LockGuard(MidiInputMutex); |
494 |
|
|
495 |
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
496 |
event.Type = Event::type_note_off; |
event.Type = Event::type_note_off; |
497 |
event.Param.Note.Key = Key; |
event.Param.Note.Key = Key; |
498 |
event.Param.Note.Velocity = Velocity; |
event.Param.Note.Velocity = Velocity; |
499 |
|
event.Param.Note.Channel = MidiChannel; |
500 |
|
memset(&event.Format, 0, sizeof(event.Format)); // init format speific stuff with zeroes |
501 |
event.pEngineChannel = this; |
event.pEngineChannel = this; |
502 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
503 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
else dmsg(1,("EngineChannel: Input event queue full!")); |
522 |
* |
* |
523 |
* @param Pitch - MIDI pitch value (-8192 ... +8191) |
* @param Pitch - MIDI pitch value (-8192 ... +8191) |
524 |
*/ |
*/ |
525 |
void AbstractEngineChannel::SendPitchbend(int Pitch) { |
void AbstractEngineChannel::SendPitchbend(int Pitch, uint8_t MidiChannel) { |
526 |
if (pEngine) { |
if (pEngine) { |
527 |
|
// protection in case there are more than 1 MIDI input threads sending MIDI events to this EngineChannel |
528 |
|
LockGuard g; |
529 |
|
if (hasMultipleMIDIInputs()) g = LockGuard(MidiInputMutex); |
530 |
|
|
531 |
Event event = pEngine->pEventGenerator->CreateEvent(); |
Event event = pEngine->pEventGenerator->CreateEvent(); |
532 |
event.Type = Event::type_pitchbend; |
event.Type = Event::type_pitchbend; |
533 |
event.Param.Pitch.Pitch = Pitch; |
event.Param.Pitch.Pitch = Pitch; |
534 |
|
event.Param.Pitch.Channel = MidiChannel; |
535 |
|
memset(&event.Format, 0, sizeof(event.Format)); // init format speific stuff with zeroes |
536 |
event.pEngineChannel = this; |
event.pEngineChannel = this; |
537 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
538 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
else dmsg(1,("EngineChannel: Input event queue full!")); |
549 |
* @param FragmentPos - sample point position in the current audio |
* @param FragmentPos - sample point position in the current audio |
550 |
* fragment to which this event belongs to |
* fragment to which this event belongs to |
551 |
*/ |
*/ |
552 |
void AbstractEngineChannel::SendPitchbend(int Pitch, int32_t FragmentPos) { |
void AbstractEngineChannel::SendPitchbend(int Pitch, uint8_t MidiChannel, int32_t FragmentPos) { |
553 |
if (FragmentPos < 0) { |
if (FragmentPos < 0) { |
554 |
dmsg(1,("AbstractEngineChannel::SendPitchBend(): negative FragmentPos! Seems MIDI driver is buggy!")); |
dmsg(1,("AbstractEngineChannel::SendPitchBend(): negative FragmentPos! Seems MIDI driver is buggy!")); |
555 |
} |
} |
556 |
else if (pEngine) { |
else if (pEngine) { |
557 |
|
// protection in case there are more than 1 MIDI input threads sending MIDI events to this EngineChannel |
558 |
|
LockGuard g; |
559 |
|
if (hasMultipleMIDIInputs()) g = LockGuard(MidiInputMutex); |
560 |
|
|
561 |
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
562 |
event.Type = Event::type_pitchbend; |
event.Type = Event::type_pitchbend; |
563 |
event.Param.Pitch.Pitch = Pitch; |
event.Param.Pitch.Pitch = Pitch; |
564 |
|
event.Param.Pitch.Channel = MidiChannel; |
565 |
|
memset(&event.Format, 0, sizeof(event.Format)); // init format speific stuff with zeroes |
566 |
event.pEngineChannel = this; |
event.pEngineChannel = this; |
567 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
568 |
else dmsg(1,("AbstractEngineChannel: Input event queue full!")); |
else dmsg(1,("AbstractEngineChannel: Input event queue full!")); |
578 |
* @param Controller - MIDI controller number of the occured control change |
* @param Controller - MIDI controller number of the occured control change |
579 |
* @param Value - value of the control change |
* @param Value - value of the control change |
580 |
*/ |
*/ |
581 |
void AbstractEngineChannel::SendControlChange(uint8_t Controller, uint8_t Value) { |
void AbstractEngineChannel::SendControlChange(uint8_t Controller, uint8_t Value, uint8_t MidiChannel) { |
582 |
if (pEngine) { |
if (pEngine) { |
583 |
|
// protection in case there are more than 1 MIDI input threads sending MIDI events to this EngineChannel |
584 |
|
LockGuard g; |
585 |
|
if (hasMultipleMIDIInputs()) g = LockGuard(MidiInputMutex); |
586 |
|
|
587 |
Event event = pEngine->pEventGenerator->CreateEvent(); |
Event event = pEngine->pEventGenerator->CreateEvent(); |
588 |
event.Type = Event::type_control_change; |
event.Type = Event::type_control_change; |
589 |
event.Param.CC.Controller = Controller; |
event.Param.CC.Controller = Controller; |
590 |
event.Param.CC.Value = Value; |
event.Param.CC.Value = Value; |
591 |
|
event.Param.CC.Channel = MidiChannel; |
592 |
|
memset(&event.Format, 0, sizeof(event.Format)); // init format speific stuff with zeroes |
593 |
event.pEngineChannel = this; |
event.pEngineChannel = this; |
594 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
595 |
else dmsg(1,("AbstractEngineChannel: Input event queue full!")); |
else dmsg(1,("AbstractEngineChannel: Input event queue full!")); |
607 |
* @param FragmentPos - sample point position in the current audio |
* @param FragmentPos - sample point position in the current audio |
608 |
* fragment to which this event belongs to |
* fragment to which this event belongs to |
609 |
*/ |
*/ |
610 |
void AbstractEngineChannel::SendControlChange(uint8_t Controller, uint8_t Value, int32_t FragmentPos) { |
void AbstractEngineChannel::SendControlChange(uint8_t Controller, uint8_t Value, uint8_t MidiChannel, int32_t FragmentPos) { |
611 |
if (FragmentPos < 0) { |
if (FragmentPos < 0) { |
612 |
dmsg(1,("AbstractEngineChannel::SendControlChange(): negative FragmentPos! Seems MIDI driver is buggy!")); |
dmsg(1,("AbstractEngineChannel::SendControlChange(): negative FragmentPos! Seems MIDI driver is buggy!")); |
613 |
} |
} |
614 |
else if (pEngine) { |
else if (pEngine) { |
615 |
|
// protection in case there are more than 1 MIDI input threads sending MIDI events to this EngineChannel |
616 |
|
LockGuard g; |
617 |
|
if (hasMultipleMIDIInputs()) g = LockGuard(MidiInputMutex); |
618 |
|
|
619 |
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
620 |
event.Type = Event::type_control_change; |
event.Type = Event::type_control_change; |
621 |
event.Param.CC.Controller = Controller; |
event.Param.CC.Controller = Controller; |
622 |
event.Param.CC.Value = Value; |
event.Param.CC.Value = Value; |
623 |
|
event.Param.CC.Channel = MidiChannel; |
624 |
|
memset(&event.Format, 0, sizeof(event.Format)); // init format speific stuff with zeroes |
625 |
event.pEngineChannel = this; |
event.pEngineChannel = this; |
626 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
627 |
else dmsg(1,("AbstractEngineChannel: Input event queue full!")); |
else dmsg(1,("AbstractEngineChannel: Input event queue full!")); |
628 |
} |
} |
629 |
} |
} |
630 |
|
|
631 |
|
void AbstractEngineChannel::SendChannelPressure(uint8_t Value, uint8_t MidiChannel) { |
632 |
|
if (pEngine) { |
633 |
|
// protection in case there are more than 1 MIDI input threads sending MIDI events to this EngineChannel |
634 |
|
LockGuard g; |
635 |
|
if (hasMultipleMIDIInputs()) g = LockGuard(MidiInputMutex); |
636 |
|
|
637 |
|
Event event = pEngine->pEventGenerator->CreateEvent(); |
638 |
|
event.Type = Event::type_channel_pressure; |
639 |
|
event.Param.ChannelPressure.Controller = CTRL_TABLE_IDX_AFTERTOUCH; // required for instrument scripts |
640 |
|
event.Param.ChannelPressure.Value = Value; |
641 |
|
event.Param.ChannelPressure.Channel = MidiChannel; |
642 |
|
memset(&event.Format, 0, sizeof(event.Format)); // init format speific stuff with zeroes |
643 |
|
event.pEngineChannel = this; |
644 |
|
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
645 |
|
else dmsg(1,("AbstractEngineChannel: Input event queue full!")); |
646 |
|
} |
647 |
|
} |
648 |
|
|
649 |
|
void AbstractEngineChannel::SendChannelPressure(uint8_t Value, uint8_t MidiChannel, int32_t FragmentPos) { |
650 |
|
if (pEngine) { |
651 |
|
// protection in case there are more than 1 MIDI input threads sending MIDI events to this EngineChannel |
652 |
|
LockGuard g; |
653 |
|
if (hasMultipleMIDIInputs()) g = LockGuard(MidiInputMutex); |
654 |
|
|
655 |
|
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
656 |
|
event.Type = Event::type_channel_pressure; |
657 |
|
event.Param.ChannelPressure.Controller = CTRL_TABLE_IDX_AFTERTOUCH; // required for instrument scripts |
658 |
|
event.Param.ChannelPressure.Value = Value; |
659 |
|
event.Param.ChannelPressure.Channel = MidiChannel; |
660 |
|
memset(&event.Format, 0, sizeof(event.Format)); // init format speific stuff with zeroes |
661 |
|
event.pEngineChannel = this; |
662 |
|
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
663 |
|
else dmsg(1,("AbstractEngineChannel: Input event queue full!")); |
664 |
|
} |
665 |
|
} |
666 |
|
|
667 |
|
void AbstractEngineChannel::SendPolyphonicKeyPressure(uint8_t Key, uint8_t Value, uint8_t MidiChannel) { |
668 |
|
if (pEngine) { |
669 |
|
// protection in case there are more than 1 MIDI input threads sending MIDI events to this EngineChannel |
670 |
|
LockGuard g; |
671 |
|
if (hasMultipleMIDIInputs()) g = LockGuard(MidiInputMutex); |
672 |
|
|
673 |
|
Event event = pEngine->pEventGenerator->CreateEvent(); |
674 |
|
event.Type = Event::type_note_pressure; |
675 |
|
event.Param.NotePressure.Key = Key; |
676 |
|
event.Param.NotePressure.Value = Value; |
677 |
|
event.Param.NotePressure.Channel = MidiChannel; |
678 |
|
memset(&event.Format, 0, sizeof(event.Format)); // init format speific stuff with zeroes |
679 |
|
event.pEngineChannel = this; |
680 |
|
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
681 |
|
else dmsg(1,("AbstractEngineChannel: Input event queue full!")); |
682 |
|
} |
683 |
|
} |
684 |
|
|
685 |
|
void AbstractEngineChannel::SendPolyphonicKeyPressure(uint8_t Key, uint8_t Value, uint8_t MidiChannel, int32_t FragmentPos) { |
686 |
|
if (pEngine) { |
687 |
|
// protection in case there are more than 1 MIDI input threads sending MIDI events to this EngineChannel |
688 |
|
LockGuard g; |
689 |
|
if (hasMultipleMIDIInputs()) g = LockGuard(MidiInputMutex); |
690 |
|
|
691 |
|
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
692 |
|
event.Type = Event::type_note_pressure; |
693 |
|
event.Param.NotePressure.Key = Key; |
694 |
|
event.Param.NotePressure.Value = Value; |
695 |
|
event.Param.NotePressure.Channel = MidiChannel; |
696 |
|
memset(&event.Format, 0, sizeof(event.Format)); // init format speific stuff with zeroes |
697 |
|
event.pEngineChannel = this; |
698 |
|
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
699 |
|
else dmsg(1,("AbstractEngineChannel: Input event queue full!")); |
700 |
|
} |
701 |
|
} |
702 |
|
|
703 |
/** |
/** |
704 |
* Copy all events from the engine channel's input event queue buffer to |
* Copy all events from the engine channel's input event queue buffer to |
705 |
* the internal event list. This will be done at the beginning of each |
* the internal event list. This will be done at the beginning of each |
717 |
// import events from pure software MIDI "devices" |
// import events from pure software MIDI "devices" |
718 |
// (e.g. virtual keyboard in instrument editor) |
// (e.g. virtual keyboard in instrument editor) |
719 |
{ |
{ |
720 |
|
const uint8_t channel = MidiChannel() == midi_chan_all ? 0 : MidiChannel(); |
721 |
const int FragmentPos = 0; // randomly chosen, we don't care about jitter for virtual MIDI devices |
const int FragmentPos = 0; // randomly chosen, we don't care about jitter for virtual MIDI devices |
722 |
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
723 |
VirtualMidiDevice::event_t devEvent; // the event format we get from the virtual MIDI device |
VirtualMidiDevice::event_t devEvent; // the event format we get from the virtual MIDI device |
730 |
VirtualMidiDevice* pDev = devices[i]; |
VirtualMidiDevice* pDev = devices[i]; |
731 |
// I think we can simply flush the whole FIFO(s), the user shouldn't be so fast ;-) |
// I think we can simply flush the whole FIFO(s), the user shouldn't be so fast ;-) |
732 |
while (pDev->GetMidiEventFromDevice(devEvent)) { |
while (pDev->GetMidiEventFromDevice(devEvent)) { |
733 |
event.Type = |
switch (devEvent.Type) { |
734 |
(devEvent.Type == VirtualMidiDevice::EVENT_TYPE_NOTEON) ? |
case VirtualMidiDevice::EVENT_TYPE_NOTEON: |
735 |
Event::type_note_on : Event::type_note_off; |
event.Type = Event::type_note_on; |
736 |
event.Param.Note.Key = devEvent.Key; |
event.Param.Note.Key = devEvent.Arg1; |
737 |
event.Param.Note.Velocity = devEvent.Velocity; |
event.Param.Note.Velocity = devEvent.Arg2; |
738 |
event.pEngineChannel = this; |
event.Param.Note.Channel = channel; |
739 |
|
break; |
740 |
|
case VirtualMidiDevice::EVENT_TYPE_NOTEOFF: |
741 |
|
event.Type = Event::type_note_off; |
742 |
|
event.Param.Note.Key = devEvent.Arg1; |
743 |
|
event.Param.Note.Velocity = devEvent.Arg2; |
744 |
|
event.Param.Note.Channel = channel; |
745 |
|
break; |
746 |
|
case VirtualMidiDevice::EVENT_TYPE_CC: |
747 |
|
switch (devEvent.Arg1) { |
748 |
|
case 0: // bank select MSB ... |
749 |
|
SetMidiBankMsb(devEvent.Arg2); |
750 |
|
continue; // don't push this event into FIFO |
751 |
|
case 32: // bank select LSB ... |
752 |
|
SetMidiBankLsb(devEvent.Arg2); |
753 |
|
continue; // don't push this event into FIFO |
754 |
|
default: // regular MIDI CC ... |
755 |
|
event.Type = Event::type_control_change; |
756 |
|
event.Param.CC.Controller = devEvent.Arg1; |
757 |
|
event.Param.CC.Value = devEvent.Arg2; |
758 |
|
event.Param.CC.Channel = channel; |
759 |
|
} |
760 |
|
break; |
761 |
|
case VirtualMidiDevice::EVENT_TYPE_PITCHBEND: |
762 |
|
event.Type = Event::type_pitchbend; |
763 |
|
event.Param.Pitch.Pitch = int(devEvent.Arg2 << 7 | devEvent.Arg1) - 8192; |
764 |
|
event.Param.Pitch.Channel = channel; |
765 |
|
break; |
766 |
|
case VirtualMidiDevice::EVENT_TYPE_PROGRAM: |
767 |
|
SendProgramChange(devEvent.Arg1); |
768 |
|
continue; // don't push this event into FIFO |
769 |
|
default: |
770 |
|
std::cerr << "AbstractEngineChannel::ImportEvents() ERROR: unknown event type (" |
771 |
|
<< devEvent.Type << "). This is a bug!"; |
772 |
|
continue; |
773 |
|
} |
774 |
|
memset(&event.Format, 0, sizeof(event.Format)); // init format specific stuff with zeroes |
775 |
|
event.pEngineChannel = this; |
776 |
// copy event to internal event list |
// copy event to internal event list |
777 |
if (pEvents->poolIsEmpty()) { |
if (pEvents->poolIsEmpty()) { |
778 |
dmsg(1,("Event pool emtpy!\n")); |
dmsg(1,("Event pool emtpy!\n")); |
808 |
eventQueueReader.free(); // free all copied events from input queue |
eventQueueReader.free(); // free all copied events from input queue |
809 |
} |
} |
810 |
|
|
811 |
|
/** |
812 |
|
* Called by real-time instrument script functions to schedule a new event |
813 |
|
* somewhere in future. |
814 |
|
* |
815 |
|
* @returns unique event ID of scheduled new event |
816 |
|
*/ |
817 |
|
int AbstractEngineChannel::ScheduleEvent(const Event* pEvent, int delay) { //TODO: delay not implemented yet |
818 |
|
// since delay is not implemented yet, we simply add the new event |
819 |
|
// to the event list of the current audio fragmet cycle for now |
820 |
|
RTList<Event>::Iterator itEvent = pEvents->allocAppend(); |
821 |
|
if (itEvent) *itEvent = *pEvent; // copy event |
822 |
|
return pEvents->getID(itEvent); |
823 |
|
} |
824 |
|
|
825 |
|
/** |
826 |
|
* Called by real-time instrument script functions to ignore the event |
827 |
|
* reflected by given event ID. The event will be freed immediately to its |
828 |
|
* pool and cannot be dereferenced by its old ID anymore. Even if its |
829 |
|
* allocated back from the Pool later on, it will have a different ID. |
830 |
|
*/ |
831 |
|
void AbstractEngineChannel::IgnoreEvent(int id) { |
832 |
|
RTList<Event>::Iterator it = pEvents->fromID(id); |
833 |
|
if (it) pEvents->free(it); |
834 |
|
} |
835 |
|
|
836 |
FxSend* AbstractEngineChannel::AddFxSend(uint8_t MidiCtrl, String Name) throw (Exception) { |
FxSend* AbstractEngineChannel::AddFxSend(uint8_t MidiCtrl, String Name) throw (Exception) { |
837 |
if (pEngine) pEngine->DisableAndLock(); |
if (pEngine) pEngine->DisableAndLock(); |
838 |
FxSend* pFxSend = new FxSend(this, MidiCtrl, Name); |
FxSend* pFxSend = new FxSend(this, MidiCtrl, Name); |
915 |
if (pEngine) pEngine->Enable(); |
if (pEngine) pEngine->Enable(); |
916 |
} |
} |
917 |
|
|
918 |
|
/** |
919 |
|
* Add a group number to the set of key groups. Should be called |
920 |
|
* when an instrument is loaded to make sure there are event lists |
921 |
|
* for all key groups. |
922 |
|
*/ |
923 |
|
void AbstractEngineChannel::AddGroup(uint group) { |
924 |
|
if (group) { |
925 |
|
std::pair<ActiveKeyGroupMap::iterator, bool> p = |
926 |
|
ActiveKeyGroups.insert(ActiveKeyGroupMap::value_type(group, 0)); |
927 |
|
if (p.second) { |
928 |
|
// If the engine channel is pending deletion (see bug |
929 |
|
// #113), pEngine will be null, so we can't use |
930 |
|
// pEngine->pEventPool here. Instead we're using a |
931 |
|
// specialized RTList that allows specifying the pool |
932 |
|
// later. |
933 |
|
(*p.first).second = new LazyList<Event>; |
934 |
|
} |
935 |
|
} |
936 |
|
} |
937 |
|
|
938 |
|
/** |
939 |
|
* Handle key group (a.k.a. exclusive group) conflicts. |
940 |
|
*/ |
941 |
|
void AbstractEngineChannel::HandleKeyGroupConflicts(uint KeyGroup, Pool<Event>::Iterator& itNoteOnEvent) { |
942 |
|
dmsg(4,("HandelKeyGroupConflicts KeyGroup=%d\n", KeyGroup)); |
943 |
|
if (KeyGroup) { |
944 |
|
// send a release event to all active voices in the group |
945 |
|
RTList<Event>::Iterator itEvent = ActiveKeyGroups[KeyGroup]->allocAppend(pEngine->pEventPool); |
946 |
|
*itEvent = *itNoteOnEvent; |
947 |
|
} |
948 |
|
} |
949 |
|
|
950 |
|
/** |
951 |
|
* Empty the lists of group events. Should be called from the |
952 |
|
* audio thread, after all voices have been rendered. |
953 |
|
*/ |
954 |
|
void AbstractEngineChannel::ClearGroupEventLists() { |
955 |
|
for (ActiveKeyGroupMap::iterator iter = ActiveKeyGroups.begin(); |
956 |
|
iter != ActiveKeyGroups.end(); iter++) { |
957 |
|
if (iter->second) { |
958 |
|
iter->second->clear(); |
959 |
|
} else { |
960 |
|
dmsg(1,("EngineChannel: group event list was NULL")); |
961 |
|
} |
962 |
|
} |
963 |
|
} |
964 |
|
|
965 |
|
/** |
966 |
|
* Remove all lists with group events. |
967 |
|
*/ |
968 |
|
void AbstractEngineChannel::DeleteGroupEventLists() { |
969 |
|
for (ActiveKeyGroupMap::iterator iter = ActiveKeyGroups.begin(); |
970 |
|
iter != ActiveKeyGroups.end(); iter++) { |
971 |
|
delete iter->second; |
972 |
|
} |
973 |
|
ActiveKeyGroups.clear(); |
974 |
|
} |
975 |
|
|
976 |
} // namespace LinuxSampler |
} // namespace LinuxSampler |