23 |
|
|
24 |
#include "EngineChannel.h" |
#include "EngineChannel.h" |
25 |
|
|
26 |
namespace LinuxSampler { namespace gig { |
namespace LinuxSampler { namespace gig { |
27 |
|
|
28 |
EngineChannel::EngineChannel() { |
EngineChannel::EngineChannel() { |
29 |
pMIDIKeyInfo = new midi_key_info_t[128]; |
pMIDIKeyInfo = new midi_key_info_t[128]; |
30 |
pEngine = NULL; |
pEngine = NULL; |
31 |
pInstrument = NULL; |
pInstrument = NULL; |
32 |
pEventQueue = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT, 0); |
pEvents = NULL; // we allocate when we retrieve the right Engine object |
33 |
|
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++) { |
36 |
pMIDIKeyInfo[i].pActiveVoices = NULL; // we allocate when we retrieve the right Engine object |
pMIDIKeyInfo[i].pActiveVoices = NULL; // we allocate when we retrieve the right Engine object |
38 |
pMIDIKeyInfo[i].Active = false; |
pMIDIKeyInfo[i].Active = false; |
39 |
pMIDIKeyInfo[i].ReleaseTrigger = false; |
pMIDIKeyInfo[i].ReleaseTrigger = false; |
40 |
pMIDIKeyInfo[i].pEvents = NULL; // we allocate when we retrieve the right Engine object |
pMIDIKeyInfo[i].pEvents = NULL; // we allocate when we retrieve the right Engine object |
41 |
|
pMIDIKeyInfo[i].VoiceTheftsQueued = 0; |
42 |
|
pMIDIKeyInfo[i].RoundRobinIndex = 0; |
43 |
} |
} |
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(); |
51 |
} |
} |
52 |
|
|
53 |
EngineChannel::~EngineChannel() { |
EngineChannel::~EngineChannel() { |
54 |
|
DisconnectAudioOutputDevice(); |
55 |
if (pInstrument) Engine::instruments.HandBack(pInstrument, this); |
if (pInstrument) Engine::instruments.HandBack(pInstrument, this); |
|
for (uint i = 0; i < 128; i++) { |
|
|
if (pMIDIKeyInfo[i].pActiveVoices) { |
|
|
pMIDIKeyInfo[i].pActiveVoices->clear(); |
|
|
delete pMIDIKeyInfo[i].pActiveVoices; |
|
|
} |
|
|
if (pMIDIKeyInfo[i].pEvents) { |
|
|
pMIDIKeyInfo[i].pEvents->clear(); |
|
|
delete pMIDIKeyInfo[i].pEvents; |
|
|
} |
|
|
} |
|
56 |
if (pEventQueue) delete pEventQueue; |
if (pEventQueue) delete pEventQueue; |
57 |
if (pActiveKeys) delete pActiveKeys; |
if (pActiveKeys) delete pActiveKeys; |
58 |
if (pMIDIKeyInfo) delete[] pMIDIKeyInfo; |
if (pMIDIKeyInfo) delete[] pMIDIKeyInfo; |
59 |
} |
} |
60 |
|
|
61 |
/** |
/** |
62 |
|
* Implementation of virtual method from abstract EngineChannel interface. |
63 |
|
* This method will periodically be polled (e.g. by the LSCP server) to |
64 |
|
* check if some engine channel parameter has changed since the last |
65 |
|
* StatusChanged() call. |
66 |
|
* |
67 |
|
* This method can also be used to mark the engine channel as changed |
68 |
|
* from outside, e.g. by a MIDI input device. The optional argument |
69 |
|
* \a nNewStatus can be used for this. |
70 |
|
* |
71 |
|
* TODO: This "poll method" is just a lazy solution and might be |
72 |
|
* replaced in future. |
73 |
|
* @param bNewStatus - (optional, default: false) sets the new status flag |
74 |
|
* @returns true if engine channel status has changed since last |
75 |
|
* StatusChanged() call |
76 |
|
*/ |
77 |
|
bool EngineChannel::StatusChanged(bool bNewStatus) { |
78 |
|
bool b = bStatusChanged; |
79 |
|
bStatusChanged = bNewStatus; |
80 |
|
return b; |
81 |
|
} |
82 |
|
|
83 |
|
void EngineChannel::Reset() { |
84 |
|
if (pEngine) pEngine->DisableAndLock(); |
85 |
|
ResetInternal(); |
86 |
|
ResetControllers(); |
87 |
|
if (pEngine) { |
88 |
|
pEngine->Enable(); |
89 |
|
pEngine->Reset(); |
90 |
|
} |
91 |
|
} |
92 |
|
|
93 |
|
/** |
94 |
* This method is not thread safe! |
* This method is not thread safe! |
95 |
*/ |
*/ |
96 |
void EngineChannel::ResetInternal() { |
void EngineChannel::ResetInternal() { |
|
Pitch = 0; |
|
|
SustainPedal = false; |
|
|
GlobalVolume = 1.0; |
|
97 |
CurrentKeyDimension = 0; |
CurrentKeyDimension = 0; |
98 |
|
|
|
// set all MIDI controller values to zero |
|
|
memset(ControllerTable, 0x00, 128); |
|
|
|
|
|
// reset voice stealing parameters |
|
|
itLastStolenVoice = RTList<Voice>::Iterator(); |
|
|
iuiLastStolenKey = RTList<uint>::Iterator(); |
|
|
|
|
99 |
// reset key info |
// reset key info |
100 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
101 |
if (pMIDIKeyInfo[i].pActiveVoices) |
if (pMIDIKeyInfo[i].pActiveVoices) |
106 |
pMIDIKeyInfo[i].Active = false; |
pMIDIKeyInfo[i].Active = false; |
107 |
pMIDIKeyInfo[i].ReleaseTrigger = false; |
pMIDIKeyInfo[i].ReleaseTrigger = false; |
108 |
pMIDIKeyInfo[i].itSelf = Pool<uint>::Iterator(); |
pMIDIKeyInfo[i].itSelf = Pool<uint>::Iterator(); |
109 |
|
pMIDIKeyInfo[i].VoiceTheftsQueued = 0; |
110 |
} |
} |
111 |
|
|
112 |
// reset all key groups |
// reset all key groups |
120 |
pEventQueue->init(); |
pEventQueue->init(); |
121 |
|
|
122 |
if (pEngine) pEngine->ResetInternal(); |
if (pEngine) pEngine->ResetInternal(); |
123 |
|
|
124 |
|
// status of engine channel has changed, so set notify flag |
125 |
|
bStatusChanged = true; |
126 |
} |
} |
127 |
|
|
128 |
LinuxSampler::Engine* EngineChannel::GetEngine() { |
LinuxSampler::Engine* EngineChannel::GetEngine() { |
157 |
void EngineChannel::LoadInstrument() { |
void EngineChannel::LoadInstrument() { |
158 |
|
|
159 |
if (pEngine) pEngine->DisableAndLock(); |
if (pEngine) pEngine->DisableAndLock(); |
160 |
|
|
161 |
ResetInternal(); |
ResetInternal(); |
162 |
|
|
163 |
// free old instrument |
// free old instrument |
164 |
if (pInstrument) { |
if (pInstrument) { |
165 |
// give old instrument back to instrument manager |
// give old instrument back to instrument manager |
218 |
|
|
219 |
/** |
/** |
220 |
* Will be called by the InstrumentResourceManager when the instrument |
* Will be called by the InstrumentResourceManager when the instrument |
221 |
* we are currently using in this engine is going to be updated, so we |
* we are currently using on this EngineChannel is going to be updated, |
222 |
* can stop playback before that happens. |
* so we can stop playback before that happens. |
223 |
*/ |
*/ |
224 |
void EngineChannel::ResourceToBeUpdated(::gig::Instrument* pResource, void*& pUpdateArg) { |
void EngineChannel::ResourceToBeUpdated(::gig::Instrument* pResource, void*& pUpdateArg) { |
225 |
dmsg(3,("gig::Engine: Received instrument update message.\n")); |
dmsg(3,("gig::Engine: Received instrument update message.\n")); |
235 |
void EngineChannel::ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) { |
void EngineChannel::ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) { |
236 |
this->pInstrument = pNewResource; //TODO: there are couple of engine parameters we should update here as well if the instrument was updated (see LoadInstrument()) |
this->pInstrument = pNewResource; //TODO: there are couple of engine parameters we should update here as well if the instrument was updated (see LoadInstrument()) |
237 |
if (pEngine) pEngine->Enable(); |
if (pEngine) pEngine->Enable(); |
238 |
|
bStatusChanged = true; // status of engine has changed, so set notify flag |
239 |
|
} |
240 |
|
|
241 |
|
/** |
242 |
|
* Will be called by the InstrumentResourceManager on progress changes |
243 |
|
* while loading or realoading an instrument for this EngineChannel. |
244 |
|
* |
245 |
|
* @param fProgress - current progress as value between 0.0 and 1.0 |
246 |
|
*/ |
247 |
|
void EngineChannel::OnResourceProgress(float fProgress) { |
248 |
|
this->InstrumentStat = int(fProgress * 100.0f); |
249 |
|
dmsg(7,("gig::EngineChannel: progress %d%", InstrumentStat)); |
250 |
|
bStatusChanged = true; // status of engine has changed, so set notify flag |
251 |
} |
} |
252 |
|
|
253 |
void EngineChannel::Connect(AudioOutputDevice* pAudioOut) { |
void EngineChannel::Connect(AudioOutputDevice* pAudioOut) { |
254 |
if (pEngine && pEngine->pAudioOutputDevice != pAudioOut) { |
if (pEngine) { |
255 |
|
if (pEngine->pAudioOutputDevice == pAudioOut) return; |
256 |
DisconnectAudioOutputDevice(); |
DisconnectAudioOutputDevice(); |
257 |
} |
} |
258 |
pEngine = Engine::AcquireEngine(this, pAudioOut); |
pEngine = Engine::AcquireEngine(this, pAudioOut); |
259 |
ResetInternal(); |
ResetInternal(); |
260 |
|
pEvents = new RTList<Event>(pEngine->pEventPool); |
261 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
262 |
pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool); |
pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool); |
263 |
pMIDIKeyInfo[i].pEvents = new RTList<Event>(pEngine->pEventPool); |
pMIDIKeyInfo[i].pEvents = new RTList<Event>(pEngine->pEventPool); |
271 |
void EngineChannel::DisconnectAudioOutputDevice() { |
void EngineChannel::DisconnectAudioOutputDevice() { |
272 |
if (pEngine) { // if clause to prevent disconnect loops |
if (pEngine) { // if clause to prevent disconnect loops |
273 |
ResetInternal(); |
ResetInternal(); |
274 |
|
if (pEvents) { |
275 |
|
delete pEvents; |
276 |
|
pEvents = NULL; |
277 |
|
} |
278 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
279 |
if (pMIDIKeyInfo[i].pActiveVoices) { |
if (pMIDIKeyInfo[i].pActiveVoices) { |
280 |
delete pMIDIKeyInfo[i].pActiveVoices; |
delete pMIDIKeyInfo[i].pActiveVoices; |
290 |
pEngine = NULL; |
pEngine = NULL; |
291 |
Engine::FreeEngine(this, oldAudioDevice); |
Engine::FreeEngine(this, oldAudioDevice); |
292 |
AudioDeviceChannelLeft = -1; |
AudioDeviceChannelLeft = -1; |
293 |
AudioDeviceChannelRight = -1; |
AudioDeviceChannelRight = -1; |
294 |
} |
} |
295 |
} |
} |
296 |
|
|
297 |
void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) { |
void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) { |
298 |
if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet."); |
if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet."); |
299 |
|
|
300 |
AudioChannel* pChannel = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannel); |
AudioChannel* pChannel = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannel); |
301 |
if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel)); |
if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel)); |
302 |
switch (EngineAudioChannel) { |
switch (EngineAudioChannel) { |
324 |
} |
} |
325 |
} |
} |
326 |
|
|
327 |
|
void EngineChannel::Connect(MidiInputPort* pMidiPort, midi_chan_t MidiChannel) { |
328 |
|
if (!pMidiPort || pMidiPort == this->pMidiInputPort) return; |
329 |
|
DisconnectMidiInputPort(); |
330 |
|
this->pMidiInputPort = pMidiPort; |
331 |
|
this->midiChannel = MidiChannel; |
332 |
|
pMidiPort->Connect(this, MidiChannel); |
333 |
|
} |
334 |
|
|
335 |
|
void EngineChannel::DisconnectMidiInputPort() { |
336 |
|
MidiInputPort* pOldPort = this->pMidiInputPort; |
337 |
|
this->pMidiInputPort = NULL; |
338 |
|
if (pOldPort) pOldPort->Disconnect(this); |
339 |
|
} |
340 |
|
|
341 |
|
MidiInputPort* EngineChannel::GetMidiInputPort() { |
342 |
|
return pMidiInputPort; |
343 |
|
} |
344 |
|
|
345 |
|
midi_chan_t EngineChannel::MidiChannel() { |
346 |
|
return midiChannel; |
347 |
|
} |
348 |
|
|
349 |
/** |
/** |
350 |
* 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 |
351 |
* voice for the given key. |
* voice for the given key. |
359 |
event.Type = Event::type_note_on; |
event.Type = Event::type_note_on; |
360 |
event.Param.Note.Key = Key; |
event.Param.Note.Key = Key; |
361 |
event.Param.Note.Velocity = Velocity; |
event.Param.Note.Velocity = Velocity; |
362 |
event.pEngineChannel = this; |
event.pEngineChannel = this; |
363 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
364 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
else dmsg(1,("EngineChannel: Input event queue full!")); |
365 |
} |
} |
391 |
* @param Pitch - MIDI pitch value (-8192 ... +8191) |
* @param Pitch - MIDI pitch value (-8192 ... +8191) |
392 |
*/ |
*/ |
393 |
void EngineChannel::SendPitchbend(int Pitch) { |
void EngineChannel::SendPitchbend(int Pitch) { |
394 |
if (pEngine) { |
if (pEngine) { |
395 |
Event event = pEngine->pEventGenerator->CreateEvent(); |
Event event = pEngine->pEventGenerator->CreateEvent(); |
396 |
event.Type = Event::type_pitchbend; |
event.Type = Event::type_pitchbend; |
397 |
event.Param.Pitch.Pitch = Pitch; |
event.Param.Pitch.Pitch = Pitch; |
420 |
} |
} |
421 |
} |
} |
422 |
|
|
423 |
|
void EngineChannel::ClearEventLists() { |
424 |
|
pEvents->clear(); |
425 |
|
// empty MIDI key specific event lists |
426 |
|
{ |
427 |
|
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
428 |
|
RTList<uint>::Iterator end = pActiveKeys->end(); |
429 |
|
for(; iuiKey != end; ++iuiKey) { |
430 |
|
pMIDIKeyInfo[*iuiKey].pEvents->clear(); // free all events on the key |
431 |
|
} |
432 |
|
} |
433 |
|
} |
434 |
|
|
435 |
|
void EngineChannel::ResetControllers() { |
436 |
|
Pitch = 0; |
437 |
|
SustainPedal = false; |
438 |
|
SostenutoPedal = false; |
439 |
|
GlobalVolume = 1.0; |
440 |
|
GlobalPanLeft = 1.0f; |
441 |
|
GlobalPanRight = 1.0f; |
442 |
|
// set all MIDI controller values to zero |
443 |
|
memset(ControllerTable, 0x00, 128); |
444 |
|
} |
445 |
|
|
446 |
|
/** |
447 |
|
* Copy all events from the engine channel's input event queue buffer to |
448 |
|
* the internal event list. This will be done at the beginning of each |
449 |
|
* audio cycle (that is each RenderAudio() call) to distinguish all |
450 |
|
* events which have to be processed in the current audio cycle. Each |
451 |
|
* EngineChannel has it's own input event queue for the common channel |
452 |
|
* specific events (like NoteOn, NoteOff and ControlChange events). |
453 |
|
* Beside that, the engine also has a input event queue for global |
454 |
|
* events (usually SysEx messages). |
455 |
|
* |
456 |
|
* @param Samples - number of sample points to be processed in the |
457 |
|
* current audio cycle |
458 |
|
*/ |
459 |
|
void EngineChannel::ImportEvents(uint Samples) { |
460 |
|
RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
461 |
|
Event* pEvent; |
462 |
|
while (true) { |
463 |
|
// get next event from input event queue |
464 |
|
if (!(pEvent = eventQueueReader.pop())) break; |
465 |
|
// if younger event reached, ignore that and all subsequent ones for now |
466 |
|
if (pEvent->FragmentPos() >= Samples) { |
467 |
|
eventQueueReader--; |
468 |
|
dmsg(2,("Younger Event, pos=%d ,Samples=%d!\n",pEvent->FragmentPos(),Samples)); |
469 |
|
pEvent->ResetFragmentPos(); |
470 |
|
break; |
471 |
|
} |
472 |
|
// copy event to internal event list |
473 |
|
if (pEvents->poolIsEmpty()) { |
474 |
|
dmsg(1,("Event pool emtpy!\n")); |
475 |
|
break; |
476 |
|
} |
477 |
|
*pEvents->allocAppend() = *pEvent; |
478 |
|
} |
479 |
|
eventQueueReader.free(); // free all copied events from input queue |
480 |
|
} |
481 |
|
|
482 |
float EngineChannel::Volume() { |
float EngineChannel::Volume() { |
483 |
return GlobalVolume; |
return GlobalVolume; |
484 |
} |
} |
485 |
|
|
486 |
void EngineChannel::Volume(float f) { |
void EngineChannel::Volume(float f) { |
487 |
GlobalVolume = f; |
GlobalVolume = f; |
488 |
|
bStatusChanged = true; // status of engine channel has changed, so set notify flag |
489 |
} |
} |
490 |
|
|
491 |
uint EngineChannel::Channels() { |
uint EngineChannel::Channels() { |
506 |
|
|
507 |
int EngineChannel::InstrumentStatus() { |
int EngineChannel::InstrumentStatus() { |
508 |
return InstrumentStat; |
return InstrumentStat; |
509 |
} |
} |
510 |
|
|
511 |
|
String EngineChannel::EngineName() { |
512 |
|
return LS_GIG_ENGINE_NAME; |
513 |
|
} |
514 |
|
|
515 |
}} // namespace LinuxSampler::gig |
}} // namespace LinuxSampler::gig |