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 Christian Schoenebeck * |
* Copyright (C) 2005, 2006 Christian Schoenebeck * |
7 |
* * |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
9 |
* 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 * |
30 |
pEngine = NULL; |
pEngine = NULL; |
31 |
pInstrument = NULL; |
pInstrument = NULL; |
32 |
pEvents = NULL; // we allocate when we retrieve the right Engine object |
pEvents = NULL; // we allocate when we retrieve the right Engine object |
|
pCCEvents = NULL; // we allocate when we retrieve the right Engine object |
|
33 |
pEventQueue = new RingBuffer<Event>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0); |
pEventQueue = new RingBuffer<Event>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0); |
34 |
pActiveKeys = new Pool<uint>(128); |
pActiveKeys = new Pool<uint>(128); |
35 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
41 |
pMIDIKeyInfo[i].VoiceTheftsQueued = 0; |
pMIDIKeyInfo[i].VoiceTheftsQueued = 0; |
42 |
pMIDIKeyInfo[i].RoundRobinIndex = 0; |
pMIDIKeyInfo[i].RoundRobinIndex = 0; |
43 |
} |
} |
|
for (uint i = 0; i < Event::destination_count; i++) { |
|
|
pSynthesisEvents[i] = NULL; // we allocate when we retrieve the right Engine object |
|
|
} |
|
44 |
InstrumentIdx = -1; |
InstrumentIdx = -1; |
45 |
InstrumentStat = -1; |
InstrumentStat = -1; |
46 |
AudioDeviceChannelLeft = -1; |
AudioDeviceChannelLeft = -1; |
47 |
AudioDeviceChannelRight = -1; |
AudioDeviceChannelRight = -1; |
48 |
|
pMidiInputPort = NULL; |
49 |
|
midiChannel = midi_chan_all; |
50 |
ResetControllers(); |
ResetControllers(); |
51 |
|
SoloMode = false; |
52 |
|
PortamentoMode = false; |
53 |
|
PortamentoTime = CONFIG_PORTAMENTO_TIME_DEFAULT; |
54 |
} |
} |
55 |
|
|
56 |
EngineChannel::~EngineChannel() { |
EngineChannel::~EngineChannel() { |
111 |
pMIDIKeyInfo[i].itSelf = Pool<uint>::Iterator(); |
pMIDIKeyInfo[i].itSelf = Pool<uint>::Iterator(); |
112 |
pMIDIKeyInfo[i].VoiceTheftsQueued = 0; |
pMIDIKeyInfo[i].VoiceTheftsQueued = 0; |
113 |
} |
} |
114 |
|
SoloKey = -1; // no solo key active yet |
115 |
|
PortamentoPos = -1.0f; // no portamento active yet |
116 |
|
|
117 |
// reset all key groups |
// reset all key groups |
118 |
std::map<uint,uint*>::iterator iter = ActiveKeyGroups.begin(); |
std::map<uint,uint*>::iterator iter = ActiveKeyGroups.begin(); |
176 |
|
|
177 |
// request gig instrument from instrument manager |
// request gig instrument from instrument manager |
178 |
try { |
try { |
179 |
instrument_id_t instrid; |
InstrumentManager::instrument_id_t instrid; |
180 |
instrid.FileName = InstrumentFile; |
instrid.FileName = InstrumentFile; |
181 |
instrid.iInstrument = InstrumentIdx; |
instrid.Index = InstrumentIdx; |
182 |
pInstrument = Engine::instruments.Borrow(instrid, this); |
pInstrument = Engine::instruments.Borrow(instrid, this); |
183 |
if (!pInstrument) { |
if (!pInstrument) { |
184 |
InstrumentStat = -1; |
InstrumentStat = -1; |
189 |
catch (RIFF::Exception e) { |
catch (RIFF::Exception e) { |
190 |
InstrumentStat = -2; |
InstrumentStat = -2; |
191 |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message; |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message; |
192 |
throw LinuxSamplerException(msg); |
throw Exception(msg); |
193 |
} |
} |
194 |
catch (InstrumentResourceManagerException e) { |
catch (InstrumentResourceManagerException e) { |
195 |
InstrumentStat = -3; |
InstrumentStat = -3; |
196 |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message(); |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message(); |
197 |
throw LinuxSamplerException(msg); |
throw Exception(msg); |
198 |
} |
} |
199 |
catch (...) { |
catch (...) { |
200 |
InstrumentStat = -4; |
InstrumentStat = -4; |
201 |
throw LinuxSamplerException("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file."); |
throw Exception("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file."); |
202 |
} |
} |
203 |
|
|
204 |
// rebuild ActiveKeyGroups map with key groups of current instrument |
// rebuild ActiveKeyGroups map with key groups of current instrument |
215 |
} |
} |
216 |
catch (AudioOutputException e) { |
catch (AudioOutputException e) { |
217 |
String msg = "Audio output device unable to provide 2 audio channels, cause: " + e.Message(); |
String msg = "Audio output device unable to provide 2 audio channels, cause: " + e.Message(); |
218 |
throw LinuxSamplerException(msg); |
throw Exception(msg); |
219 |
} |
} |
220 |
|
|
221 |
if (pEngine) pEngine->Enable(); |
if (pEngine) pEngine->Enable(); |
262 |
} |
} |
263 |
pEngine = Engine::AcquireEngine(this, pAudioOut); |
pEngine = Engine::AcquireEngine(this, pAudioOut); |
264 |
ResetInternal(); |
ResetInternal(); |
265 |
pEvents = new RTList<Event>(pEngine->pEventPool); |
pEvents = new RTList<Event>(pEngine->pEventPool); |
|
pCCEvents = new RTList<Event>(pEngine->pEventPool); |
|
|
for (uint i = 0; i < Event::destination_count; i++) { |
|
|
pSynthesisEvents[i] = new RTList<Event>(pEngine->pEventPool); |
|
|
} |
|
266 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
267 |
pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool); |
pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool); |
268 |
pMIDIKeyInfo[i].pEvents = new RTList<Event>(pEngine->pEventPool); |
pMIDIKeyInfo[i].pEvents = new RTList<Event>(pEngine->pEventPool); |
271 |
AudioDeviceChannelRight = 1; |
AudioDeviceChannelRight = 1; |
272 |
pOutputLeft = pAudioOut->Channel(0)->Buffer(); |
pOutputLeft = pAudioOut->Channel(0)->Buffer(); |
273 |
pOutputRight = pAudioOut->Channel(1)->Buffer(); |
pOutputRight = pAudioOut->Channel(1)->Buffer(); |
274 |
|
MidiInputPort::AddSysexListener(pEngine); |
275 |
} |
} |
276 |
|
|
277 |
void EngineChannel::DisconnectAudioOutputDevice() { |
void EngineChannel::DisconnectAudioOutputDevice() { |
281 |
delete pEvents; |
delete pEvents; |
282 |
pEvents = NULL; |
pEvents = NULL; |
283 |
} |
} |
|
if (pCCEvents) { |
|
|
delete pCCEvents; |
|
|
pCCEvents = NULL; |
|
|
} |
|
284 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
285 |
if (pMIDIKeyInfo[i].pActiveVoices) { |
if (pMIDIKeyInfo[i].pActiveVoices) { |
286 |
delete pMIDIKeyInfo[i].pActiveVoices; |
delete pMIDIKeyInfo[i].pActiveVoices; |
291 |
pMIDIKeyInfo[i].pEvents = NULL; |
pMIDIKeyInfo[i].pEvents = NULL; |
292 |
} |
} |
293 |
} |
} |
|
for (uint i = 0; i < Event::destination_count; i++) { |
|
|
if (pSynthesisEvents[i]) { |
|
|
delete pSynthesisEvents[i]; |
|
|
pSynthesisEvents[i] = NULL; |
|
|
} |
|
|
} |
|
294 |
Engine* oldEngine = pEngine; |
Engine* oldEngine = pEngine; |
295 |
AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice; |
AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice; |
296 |
pEngine = NULL; |
pEngine = NULL; |
330 |
} |
} |
331 |
} |
} |
332 |
|
|
333 |
|
void EngineChannel::Connect(MidiInputPort* pMidiPort, midi_chan_t MidiChannel) { |
334 |
|
if (!pMidiPort || pMidiPort == this->pMidiInputPort) return; |
335 |
|
DisconnectMidiInputPort(); |
336 |
|
this->pMidiInputPort = pMidiPort; |
337 |
|
this->midiChannel = MidiChannel; |
338 |
|
pMidiPort->Connect(this, MidiChannel); |
339 |
|
} |
340 |
|
|
341 |
|
void EngineChannel::DisconnectMidiInputPort() { |
342 |
|
MidiInputPort* pOldPort = this->pMidiInputPort; |
343 |
|
this->pMidiInputPort = NULL; |
344 |
|
if (pOldPort) pOldPort->Disconnect(this); |
345 |
|
} |
346 |
|
|
347 |
|
MidiInputPort* EngineChannel::GetMidiInputPort() { |
348 |
|
return pMidiInputPort; |
349 |
|
} |
350 |
|
|
351 |
|
midi_chan_t EngineChannel::MidiChannel() { |
352 |
|
return midiChannel; |
353 |
|
} |
354 |
|
|
355 |
/** |
/** |
356 |
* Will be called by the MIDIIn Thread to let the audio thread trigger a new |
* Will be called by the MIDIIn Thread to let the audio thread trigger a new |
357 |
* voice for the given key. |
* voice for the given key. This method is meant for real time rendering, |
358 |
|
* that is an event will immediately be created with the current system |
359 |
|
* time as time stamp. |
360 |
* |
* |
361 |
* @param Key - MIDI key number of the triggered key |
* @param Key - MIDI key number of the triggered key |
362 |
* @param Velocity - MIDI velocity value of the triggered key |
* @param Velocity - MIDI velocity value of the triggered key |
374 |
} |
} |
375 |
|
|
376 |
/** |
/** |
377 |
|
* Will be called by the MIDIIn Thread to let the audio thread trigger a new |
378 |
|
* voice for the given key. This method is meant for offline rendering |
379 |
|
* and / or for cases where the exact position of the event in the current |
380 |
|
* audio fragment is already known. |
381 |
|
* |
382 |
|
* @param Key - MIDI key number of the triggered key |
383 |
|
* @param Velocity - MIDI velocity value of the triggered key |
384 |
|
* @param FragmentPos - sample point position in the current audio |
385 |
|
* fragment to which this event belongs to |
386 |
|
*/ |
387 |
|
void EngineChannel::SendNoteOn(uint8_t Key, uint8_t Velocity, int32_t FragmentPos) { |
388 |
|
if (FragmentPos < 0) { |
389 |
|
dmsg(1,("EngineChannel::SendNoteOn(): negative FragmentPos! Seems MIDI driver is buggy!")); |
390 |
|
} |
391 |
|
else if (pEngine) { |
392 |
|
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
393 |
|
event.Type = Event::type_note_on; |
394 |
|
event.Param.Note.Key = Key; |
395 |
|
event.Param.Note.Velocity = Velocity; |
396 |
|
event.pEngineChannel = this; |
397 |
|
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
398 |
|
else dmsg(1,("EngineChannel: Input event queue full!")); |
399 |
|
} |
400 |
|
} |
401 |
|
|
402 |
|
/** |
403 |
* Will be called by the MIDIIn Thread to signal the audio thread to release |
* Will be called by the MIDIIn Thread to signal the audio thread to release |
404 |
* voice(s) on the given key. |
* voice(s) on the given key. This method is meant for real time rendering, |
405 |
|
* that is an event will immediately be created with the current system |
406 |
|
* time as time stamp. |
407 |
* |
* |
408 |
* @param Key - MIDI key number of the released key |
* @param Key - MIDI key number of the released key |
409 |
* @param Velocity - MIDI release velocity value of the released key |
* @param Velocity - MIDI release velocity value of the released key |
421 |
} |
} |
422 |
|
|
423 |
/** |
/** |
424 |
|
* Will be called by the MIDIIn Thread to signal the audio thread to release |
425 |
|
* voice(s) on the given key. This method is meant for offline rendering |
426 |
|
* and / or for cases where the exact position of the event in the current |
427 |
|
* audio fragment is already known. |
428 |
|
* |
429 |
|
* @param Key - MIDI key number of the released key |
430 |
|
* @param Velocity - MIDI release velocity value of the released key |
431 |
|
* @param FragmentPos - sample point position in the current audio |
432 |
|
* fragment to which this event belongs to |
433 |
|
*/ |
434 |
|
void EngineChannel::SendNoteOff(uint8_t Key, uint8_t Velocity, int32_t FragmentPos) { |
435 |
|
if (FragmentPos < 0) { |
436 |
|
dmsg(1,("EngineChannel::SendNoteOff(): negative FragmentPos! Seems MIDI driver is buggy!")); |
437 |
|
} |
438 |
|
else if (pEngine) { |
439 |
|
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
440 |
|
event.Type = Event::type_note_off; |
441 |
|
event.Param.Note.Key = Key; |
442 |
|
event.Param.Note.Velocity = Velocity; |
443 |
|
event.pEngineChannel = this; |
444 |
|
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
445 |
|
else dmsg(1,("EngineChannel: Input event queue full!")); |
446 |
|
} |
447 |
|
} |
448 |
|
|
449 |
|
/** |
450 |
* Will be called by the MIDIIn Thread to signal the audio thread to change |
* Will be called by the MIDIIn Thread to signal the audio thread to change |
451 |
* the pitch value for all voices. |
* the pitch value for all voices. This method is meant for real time |
452 |
|
* rendering, that is an event will immediately be created with the |
453 |
|
* current system time as time stamp. |
454 |
* |
* |
455 |
* @param Pitch - MIDI pitch value (-8192 ... +8191) |
* @param Pitch - MIDI pitch value (-8192 ... +8191) |
456 |
*/ |
*/ |
466 |
} |
} |
467 |
|
|
468 |
/** |
/** |
469 |
|
* Will be called by the MIDIIn Thread to signal the audio thread to change |
470 |
|
* the pitch value for all voices. This method is meant for offline |
471 |
|
* rendering and / or for cases where the exact position of the event in |
472 |
|
* the current audio fragment is already known. |
473 |
|
* |
474 |
|
* @param Pitch - MIDI pitch value (-8192 ... +8191) |
475 |
|
* @param FragmentPos - sample point position in the current audio |
476 |
|
* fragment to which this event belongs to |
477 |
|
*/ |
478 |
|
void EngineChannel::SendPitchbend(int Pitch, int32_t FragmentPos) { |
479 |
|
if (FragmentPos < 0) { |
480 |
|
dmsg(1,("EngineChannel::SendPitchBend(): negative FragmentPos! Seems MIDI driver is buggy!")); |
481 |
|
} |
482 |
|
else if (pEngine) { |
483 |
|
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
484 |
|
event.Type = Event::type_pitchbend; |
485 |
|
event.Param.Pitch.Pitch = Pitch; |
486 |
|
event.pEngineChannel = this; |
487 |
|
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
488 |
|
else dmsg(1,("EngineChannel: Input event queue full!")); |
489 |
|
} |
490 |
|
} |
491 |
|
|
492 |
|
/** |
493 |
* Will be called by the MIDIIn Thread to signal the audio thread that a |
* Will be called by the MIDIIn Thread to signal the audio thread that a |
494 |
* continuous controller value has changed. |
* continuous controller value has changed. This method is meant for real |
495 |
|
* time rendering, that is an event will immediately be created with the |
496 |
|
* current system time as time stamp. |
497 |
* |
* |
498 |
* @param Controller - MIDI controller number of the occured control change |
* @param Controller - MIDI controller number of the occured control change |
499 |
* @param Value - value of the control change |
* @param Value - value of the control change |
510 |
} |
} |
511 |
} |
} |
512 |
|
|
513 |
|
/** |
514 |
|
* Will be called by the MIDIIn Thread to signal the audio thread that a |
515 |
|
* continuous controller value has changed. This method is meant for |
516 |
|
* offline rendering and / or for cases where the exact position of the |
517 |
|
* event in the current audio fragment is already known. |
518 |
|
* |
519 |
|
* @param Controller - MIDI controller number of the occured control change |
520 |
|
* @param Value - value of the control change |
521 |
|
* @param FragmentPos - sample point position in the current audio |
522 |
|
* fragment to which this event belongs to |
523 |
|
*/ |
524 |
|
void EngineChannel::SendControlChange(uint8_t Controller, uint8_t Value, int32_t FragmentPos) { |
525 |
|
if (FragmentPos < 0) { |
526 |
|
dmsg(1,("EngineChannel::SendControlChange(): negative FragmentPos! Seems MIDI driver is buggy!")); |
527 |
|
} |
528 |
|
else if (pEngine) { |
529 |
|
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
530 |
|
event.Type = Event::type_control_change; |
531 |
|
event.Param.CC.Controller = Controller; |
532 |
|
event.Param.CC.Value = Value; |
533 |
|
event.pEngineChannel = this; |
534 |
|
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
535 |
|
else dmsg(1,("EngineChannel: Input event queue full!")); |
536 |
|
} |
537 |
|
} |
538 |
|
|
539 |
void EngineChannel::ClearEventLists() { |
void EngineChannel::ClearEventLists() { |
540 |
pEvents->clear(); |
pEvents->clear(); |
|
pCCEvents->clear(); |
|
|
for (uint i = 0; i < Event::destination_count; i++) { |
|
|
pSynthesisEvents[i]->clear(); |
|
|
} |
|
541 |
// empty MIDI key specific event lists |
// empty MIDI key specific event lists |
542 |
{ |
{ |
543 |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
551 |
void EngineChannel::ResetControllers() { |
void EngineChannel::ResetControllers() { |
552 |
Pitch = 0; |
Pitch = 0; |
553 |
SustainPedal = false; |
SustainPedal = false; |
554 |
GlobalVolume = 1.0; |
SostenutoPedal = false; |
555 |
|
GlobalVolume = CONFIG_GLOBAL_ATTENUATION; |
556 |
|
MidiVolume = 1.0; |
557 |
GlobalPanLeft = 1.0f; |
GlobalPanLeft = 1.0f; |
558 |
GlobalPanRight = 1.0f; |
GlobalPanRight = 1.0f; |
559 |
// set all MIDI controller values to zero |
// set all MIDI controller values to zero |
560 |
memset(ControllerTable, 0x00, 128); |
memset(ControllerTable, 0x00, 129); |
561 |
} |
} |
562 |
|
|
563 |
/** |
/** |