/[svn]/linuxsampler/trunk/src/engines/gig/EngineChannel.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/engines/gig/EngineChannel.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1309 - (show annotations) (download)
Wed Aug 29 10:36:32 2007 UTC (16 years, 7 months ago) by iliev
File size: 31843 byte(s)
* A notification event is now sent when the
  instrument loading on a sampler channel failed

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 * Copyright (C) 2005 - 2007 Christian Schoenebeck *
7 * *
8 * 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 *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the Free Software *
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21 * MA 02111-1307 USA *
22 ***************************************************************************/
23
24 #include "EngineChannel.h"
25
26 namespace LinuxSampler { namespace gig {
27
28 EngineChannel::EngineChannel() {
29 pMIDIKeyInfo = new midi_key_info_t[128];
30 pEngine = NULL;
31 pInstrument = NULL;
32 pEvents = NULL; // we allocate when we retrieve the right Engine object
33 pEventQueue = new RingBuffer<Event,false>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0);
34 pActiveKeys = new Pool<uint>(128);
35 for (uint i = 0; i < 128; i++) {
36 pMIDIKeyInfo[i].pActiveVoices = NULL; // we allocate when we retrieve the right Engine object
37 pMIDIKeyInfo[i].KeyPressed = false;
38 pMIDIKeyInfo[i].Active = false;
39 pMIDIKeyInfo[i].ReleaseTrigger = false;
40 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;
45 InstrumentStat = -1;
46 pChannelLeft = NULL;
47 pChannelRight = NULL;
48 AudioDeviceChannelLeft = -1;
49 AudioDeviceChannelRight = -1;
50 pMidiInputPort = NULL;
51 midiChannel = midi_chan_all;
52 ResetControllers();
53 SoloMode = false;
54 PortamentoMode = false;
55 PortamentoTime = CONFIG_PORTAMENTO_TIME_DEFAULT;
56 }
57
58 EngineChannel::~EngineChannel() {
59 DisconnectAudioOutputDevice();
60 if (pInstrument) Engine::instruments.HandBack(pInstrument, this);
61 if (pEventQueue) delete pEventQueue;
62 if (pActiveKeys) delete pActiveKeys;
63 if (pMIDIKeyInfo) delete[] pMIDIKeyInfo;
64 RemoveAllFxSends();
65 }
66
67 /**
68 * Implementation of virtual method from abstract EngineChannel interface.
69 * This method will periodically be polled (e.g. by the LSCP server) to
70 * check if some engine channel parameter has changed since the last
71 * StatusChanged() call.
72 *
73 * This method can also be used to mark the engine channel as changed
74 * from outside, e.g. by a MIDI input device. The optional argument
75 * \a nNewStatus can be used for this.
76 *
77 * TODO: This "poll method" is just a lazy solution and might be
78 * replaced in future.
79 * @param bNewStatus - (optional, default: false) sets the new status flag
80 * @returns true if engine channel status has changed since last
81 * StatusChanged() call
82 */
83 bool EngineChannel::StatusChanged(bool bNewStatus) {
84 bool b = bStatusChanged;
85 bStatusChanged = bNewStatus;
86 return b;
87 }
88
89 void EngineChannel::Reset() {
90 if (pEngine) pEngine->DisableAndLock();
91 ResetInternal();
92 ResetControllers();
93 if (pEngine) {
94 pEngine->Enable();
95 pEngine->Reset();
96 }
97 }
98
99 /**
100 * This method is not thread safe!
101 */
102 void EngineChannel::ResetInternal() {
103 CurrentKeyDimension = 0;
104
105 // reset key info
106 for (uint i = 0; i < 128; i++) {
107 if (pMIDIKeyInfo[i].pActiveVoices)
108 pMIDIKeyInfo[i].pActiveVoices->clear();
109 if (pMIDIKeyInfo[i].pEvents)
110 pMIDIKeyInfo[i].pEvents->clear();
111 pMIDIKeyInfo[i].KeyPressed = false;
112 pMIDIKeyInfo[i].Active = false;
113 pMIDIKeyInfo[i].ReleaseTrigger = false;
114 pMIDIKeyInfo[i].itSelf = Pool<uint>::Iterator();
115 pMIDIKeyInfo[i].VoiceTheftsQueued = 0;
116 }
117 SoloKey = -1; // no solo key active yet
118 PortamentoPos = -1.0f; // no portamento active yet
119
120 // reset all key groups
121 std::map<uint,uint*>::iterator iter = ActiveKeyGroups.begin();
122 for (; iter != ActiveKeyGroups.end(); iter++) iter->second = NULL;
123
124 // free all active keys
125 pActiveKeys->clear();
126
127 // delete all input events
128 pEventQueue->init();
129
130 if (pEngine) pEngine->ResetInternal();
131
132 // status of engine channel has changed, so set notify flag
133 bStatusChanged = true;
134 }
135
136 LinuxSampler::Engine* EngineChannel::GetEngine() {
137 return pEngine;
138 }
139
140 /**
141 * More or less a workaround to set the instrument name, index and load
142 * status variable to zero percent immediately, that is without blocking
143 * the calling thread. It might be used in future for other preparations
144 * as well though.
145 *
146 * @param FileName - file name of the Gigasampler instrument file
147 * @param Instrument - index of the instrument in the .gig file
148 * @see LoadInstrument()
149 */
150 void EngineChannel::PrepareLoadInstrument(const char* FileName, uint Instrument) {
151 InstrumentFile = FileName;
152 InstrumentIdx = Instrument;
153 InstrumentStat = 0;
154 }
155
156 /**
157 * Load an instrument from a .gig file. PrepareLoadInstrument() has to
158 * be called first to provide the information which instrument to load.
159 * This method will then actually start to load the instrument and block
160 * the calling thread until loading was completed.
161 *
162 * @see PrepareLoadInstrument()
163 */
164 void EngineChannel::LoadInstrument() {
165 ::gig::Instrument* oldInstrument = pInstrument;
166
167 // free old instrument
168 if (oldInstrument) {
169 if (pEngine) {
170 // make sure we don't trigger any new notes with the
171 // old instrument
172 ::gig::DimensionRegion** dimRegionsInUse = pEngine->ChangeInstrument(this, 0);
173
174 // give old instrument back to instrument manager, but
175 // keep the dimension regions and samples that are in
176 // use
177 Engine::instruments.HandBackInstrument(oldInstrument, this, dimRegionsInUse);
178 } else {
179 Engine::instruments.HandBack(oldInstrument, this);
180 }
181 }
182
183 // delete all key groups
184 ActiveKeyGroups.clear();
185
186 // request gig instrument from instrument manager
187 ::gig::Instrument* newInstrument;
188 try {
189 InstrumentManager::instrument_id_t instrid;
190 instrid.FileName = InstrumentFile;
191 instrid.Index = InstrumentIdx;
192 newInstrument = Engine::instruments.Borrow(instrid, this);
193 if (!newInstrument) {
194 throw InstrumentManagerException("resource was not created");
195 }
196 }
197 catch (RIFF::Exception e) {
198 InstrumentStat = -2;
199 StatusChanged(true);
200 String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message;
201 throw Exception(msg);
202 }
203 catch (InstrumentManagerException e) {
204 InstrumentStat = -3;
205 StatusChanged(true);
206 String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message();
207 throw Exception(msg);
208 }
209 catch (...) {
210 InstrumentStat = -4;
211 StatusChanged(true);
212 throw Exception("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file.");
213 }
214
215 // rebuild ActiveKeyGroups map with key groups of current instrument
216 for (::gig::Region* pRegion = newInstrument->GetFirstRegion(); pRegion; pRegion = newInstrument->GetNextRegion())
217 if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL;
218
219 InstrumentIdxName = newInstrument->pInfo->Name;
220 InstrumentStat = 100;
221
222 if (pEngine) pEngine->ChangeInstrument(this, newInstrument);
223 else pInstrument = newInstrument;
224
225 StatusChanged(true);
226 }
227
228 /**
229 * Will be called by the InstrumentResourceManager when the instrument
230 * we are currently using on this EngineChannel is going to be updated,
231 * so we can stop playback before that happens.
232 */
233 void EngineChannel::ResourceToBeUpdated(::gig::Instrument* pResource, void*& pUpdateArg) {
234 dmsg(3,("gig::Engine: Received instrument update message.\n"));
235 if (pEngine) pEngine->DisableAndLock();
236 ResetInternal();
237 this->pInstrument = NULL;
238 }
239
240 /**
241 * Will be called by the InstrumentResourceManager when the instrument
242 * update process was completed, so we can continue with playback.
243 */
244 void EngineChannel::ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) {
245 this->pInstrument = pNewResource; //TODO: there are couple of engine parameters we should update here as well if the instrument was updated (see LoadInstrument())
246 if (pEngine) pEngine->Enable();
247 bStatusChanged = true; // status of engine has changed, so set notify flag
248 }
249
250 /**
251 * Will be called by the InstrumentResourceManager on progress changes
252 * while loading or realoading an instrument for this EngineChannel.
253 *
254 * @param fProgress - current progress as value between 0.0 and 1.0
255 */
256 void EngineChannel::OnResourceProgress(float fProgress) {
257 this->InstrumentStat = int(fProgress * 100.0f);
258 dmsg(7,("gig::EngineChannel: progress %d%", InstrumentStat));
259 bStatusChanged = true; // status of engine has changed, so set notify flag
260 }
261
262 void EngineChannel::Connect(AudioOutputDevice* pAudioOut) {
263 if (pEngine) {
264 if (pEngine->pAudioOutputDevice == pAudioOut) return;
265 DisconnectAudioOutputDevice();
266 }
267 pEngine = Engine::AcquireEngine(this, pAudioOut);
268 ResetInternal();
269 pEvents = new RTList<Event>(pEngine->pEventPool);
270 for (uint i = 0; i < 128; i++) {
271 pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool);
272 pMIDIKeyInfo[i].pEvents = new RTList<Event>(pEngine->pEventPool);
273 }
274 AudioDeviceChannelLeft = 0;
275 AudioDeviceChannelRight = 1;
276 if (fxSends.empty()) { // render directly into the AudioDevice's output buffers
277 pChannelLeft = pAudioOut->Channel(AudioDeviceChannelLeft);
278 pChannelRight = pAudioOut->Channel(AudioDeviceChannelRight);
279 } else { // use local buffers for rendering and copy later
280 // ensure the local buffers have the correct size
281 if (pChannelLeft) delete pChannelLeft;
282 if (pChannelRight) delete pChannelRight;
283 pChannelLeft = new AudioChannel(0, pAudioOut->MaxSamplesPerCycle());
284 pChannelRight = new AudioChannel(1, pAudioOut->MaxSamplesPerCycle());
285 }
286 if (pEngine->EngineDisabled.GetUnsafe()) pEngine->Enable();
287 MidiInputPort::AddSysexListener(pEngine);
288 }
289
290 void EngineChannel::DisconnectAudioOutputDevice() {
291 if (pEngine) { // if clause to prevent disconnect loops
292 ResetInternal();
293 if (pEvents) {
294 delete pEvents;
295 pEvents = NULL;
296 }
297 for (uint i = 0; i < 128; i++) {
298 if (pMIDIKeyInfo[i].pActiveVoices) {
299 delete pMIDIKeyInfo[i].pActiveVoices;
300 pMIDIKeyInfo[i].pActiveVoices = NULL;
301 }
302 if (pMIDIKeyInfo[i].pEvents) {
303 delete pMIDIKeyInfo[i].pEvents;
304 pMIDIKeyInfo[i].pEvents = NULL;
305 }
306 }
307 Engine* oldEngine = pEngine;
308 AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice;
309 pEngine = NULL;
310 Engine::FreeEngine(this, oldAudioDevice);
311 AudioDeviceChannelLeft = -1;
312 AudioDeviceChannelRight = -1;
313 if (!fxSends.empty()) { // free the local rendering buffers
314 if (pChannelLeft) delete pChannelLeft;
315 if (pChannelRight) delete pChannelRight;
316 }
317 pChannelLeft = NULL;
318 pChannelRight = NULL;
319 }
320 }
321
322 AudioOutputDevice* EngineChannel::GetAudioOutputDevice() {
323 return (pEngine) ? pEngine->pAudioOutputDevice : NULL;
324 }
325
326 void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) {
327 if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet.");
328
329 AudioChannel* pChannel = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannel);
330 if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel));
331 switch (EngineAudioChannel) {
332 case 0: // left output channel
333 if (fxSends.empty()) pChannelLeft = pChannel;
334 AudioDeviceChannelLeft = AudioDeviceChannel;
335 break;
336 case 1: // right output channel
337 if (fxSends.empty()) pChannelRight = pChannel;
338 AudioDeviceChannelRight = AudioDeviceChannel;
339 break;
340 default:
341 throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel));
342 }
343
344 bStatusChanged = true;
345 }
346
347 int EngineChannel::OutputChannel(uint EngineAudioChannel) {
348 switch (EngineAudioChannel) {
349 case 0: // left channel
350 return AudioDeviceChannelLeft;
351 case 1: // right channel
352 return AudioDeviceChannelRight;
353 default:
354 throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel));
355 }
356 }
357
358 void EngineChannel::Connect(MidiInputPort* pMidiPort, midi_chan_t MidiChannel) {
359 if (!pMidiPort || pMidiPort == this->pMidiInputPort) return;
360 DisconnectMidiInputPort();
361 this->pMidiInputPort = pMidiPort;
362 this->midiChannel = MidiChannel;
363 pMidiPort->Connect(this, MidiChannel);
364 }
365
366 void EngineChannel::DisconnectMidiInputPort() {
367 MidiInputPort* pOldPort = this->pMidiInputPort;
368 this->pMidiInputPort = NULL;
369 if (pOldPort) pOldPort->Disconnect(this);
370 }
371
372 MidiInputPort* EngineChannel::GetMidiInputPort() {
373 return pMidiInputPort;
374 }
375
376 midi_chan_t EngineChannel::MidiChannel() {
377 return midiChannel;
378 }
379
380 FxSend* EngineChannel::AddFxSend(uint8_t MidiCtrl, String Name) throw (Exception) {
381 if (pEngine) pEngine->DisableAndLock();
382 FxSend* pFxSend = new FxSend(this, MidiCtrl, Name);
383 if (fxSends.empty()) {
384 if (pEngine && pEngine->pAudioOutputDevice) {
385 AudioOutputDevice* pDevice = pEngine->pAudioOutputDevice;
386 // create local render buffers
387 pChannelLeft = new AudioChannel(0, pDevice->MaxSamplesPerCycle());
388 pChannelRight = new AudioChannel(1, pDevice->MaxSamplesPerCycle());
389 } else {
390 // postpone local render buffer creation until audio device is assigned
391 pChannelLeft = NULL;
392 pChannelRight = NULL;
393 }
394 }
395 fxSends.push_back(pFxSend);
396 if (pEngine) pEngine->Enable();
397 fireFxSendCountChanged(iSamplerChannelIndex, GetFxSendCount());
398
399 return pFxSend;
400 }
401
402 FxSend* EngineChannel::GetFxSend(uint FxSendIndex) {
403 return (FxSendIndex < fxSends.size()) ? fxSends[FxSendIndex] : NULL;
404 }
405
406 uint EngineChannel::GetFxSendCount() {
407 return fxSends.size();
408 }
409
410 void EngineChannel::RemoveFxSend(FxSend* pFxSend) {
411 if (pEngine) pEngine->DisableAndLock();
412 for (
413 std::vector<FxSend*>::iterator iter = fxSends.begin();
414 iter != fxSends.end(); iter++
415 ) {
416 if (*iter == pFxSend) {
417 delete pFxSend;
418 fxSends.erase(iter);
419 if (fxSends.empty()) {
420 // destroy local render buffers
421 if (pChannelLeft) delete pChannelLeft;
422 if (pChannelRight) delete pChannelRight;
423 // fallback to render directly into AudioOutputDevice's buffers
424 if (pEngine && pEngine->pAudioOutputDevice) {
425 pChannelLeft = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft);
426 pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight);
427 } else { // we update the pointers later
428 pChannelLeft = NULL;
429 pChannelRight = NULL;
430 }
431 }
432 break;
433 }
434 }
435 if (pEngine) pEngine->Enable();
436 fireFxSendCountChanged(iSamplerChannelIndex, GetFxSendCount());
437 }
438
439 /**
440 * Will be called by the MIDIIn Thread to let the audio thread trigger a new
441 * voice for the given key. This method is meant for real time rendering,
442 * that is an event will immediately be created with the current system
443 * time as time stamp.
444 *
445 * @param Key - MIDI key number of the triggered key
446 * @param Velocity - MIDI velocity value of the triggered key
447 */
448 void EngineChannel::SendNoteOn(uint8_t Key, uint8_t Velocity) {
449 if (pEngine) {
450 Event event = pEngine->pEventGenerator->CreateEvent();
451 event.Type = Event::type_note_on;
452 event.Param.Note.Key = Key;
453 event.Param.Note.Velocity = Velocity;
454 event.pEngineChannel = this;
455 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
456 else dmsg(1,("EngineChannel: Input event queue full!"));
457 }
458 }
459
460 /**
461 * Will be called by the MIDIIn Thread to let the audio thread trigger a new
462 * voice for the given key. This method is meant for offline rendering
463 * and / or for cases where the exact position of the event in the current
464 * audio fragment is already known.
465 *
466 * @param Key - MIDI key number of the triggered key
467 * @param Velocity - MIDI velocity value of the triggered key
468 * @param FragmentPos - sample point position in the current audio
469 * fragment to which this event belongs to
470 */
471 void EngineChannel::SendNoteOn(uint8_t Key, uint8_t Velocity, int32_t FragmentPos) {
472 if (FragmentPos < 0) {
473 dmsg(1,("EngineChannel::SendNoteOn(): negative FragmentPos! Seems MIDI driver is buggy!"));
474 }
475 else if (pEngine) {
476 Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos);
477 event.Type = Event::type_note_on;
478 event.Param.Note.Key = Key;
479 event.Param.Note.Velocity = Velocity;
480 event.pEngineChannel = this;
481 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
482 else dmsg(1,("EngineChannel: Input event queue full!"));
483 }
484 }
485
486 /**
487 * Will be called by the MIDIIn Thread to signal the audio thread to release
488 * voice(s) on the given key. This method is meant for real time rendering,
489 * that is an event will immediately be created with the current system
490 * time as time stamp.
491 *
492 * @param Key - MIDI key number of the released key
493 * @param Velocity - MIDI release velocity value of the released key
494 */
495 void EngineChannel::SendNoteOff(uint8_t Key, uint8_t Velocity) {
496 if (pEngine) {
497 Event event = pEngine->pEventGenerator->CreateEvent();
498 event.Type = Event::type_note_off;
499 event.Param.Note.Key = Key;
500 event.Param.Note.Velocity = Velocity;
501 event.pEngineChannel = this;
502 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
503 else dmsg(1,("EngineChannel: Input event queue full!"));
504 }
505 }
506
507 /**
508 * Will be called by the MIDIIn Thread to signal the audio thread to release
509 * voice(s) on the given key. This method is meant for offline rendering
510 * and / or for cases where the exact position of the event in the current
511 * audio fragment is already known.
512 *
513 * @param Key - MIDI key number of the released key
514 * @param Velocity - MIDI release velocity value of the released key
515 * @param FragmentPos - sample point position in the current audio
516 * fragment to which this event belongs to
517 */
518 void EngineChannel::SendNoteOff(uint8_t Key, uint8_t Velocity, int32_t FragmentPos) {
519 if (FragmentPos < 0) {
520 dmsg(1,("EngineChannel::SendNoteOff(): negative FragmentPos! Seems MIDI driver is buggy!"));
521 }
522 else if (pEngine) {
523 Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos);
524 event.Type = Event::type_note_off;
525 event.Param.Note.Key = Key;
526 event.Param.Note.Velocity = Velocity;
527 event.pEngineChannel = this;
528 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
529 else dmsg(1,("EngineChannel: Input event queue full!"));
530 }
531 }
532
533 /**
534 * Will be called by the MIDIIn Thread to signal the audio thread to change
535 * the pitch value for all voices. This method is meant for real time
536 * rendering, that is an event will immediately be created with the
537 * current system time as time stamp.
538 *
539 * @param Pitch - MIDI pitch value (-8192 ... +8191)
540 */
541 void EngineChannel::SendPitchbend(int Pitch) {
542 if (pEngine) {
543 Event event = pEngine->pEventGenerator->CreateEvent();
544 event.Type = Event::type_pitchbend;
545 event.Param.Pitch.Pitch = Pitch;
546 event.pEngineChannel = this;
547 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
548 else dmsg(1,("EngineChannel: Input event queue full!"));
549 }
550 }
551
552 /**
553 * Will be called by the MIDIIn Thread to signal the audio thread to change
554 * the pitch value for all voices. This method is meant for offline
555 * rendering and / or for cases where the exact position of the event in
556 * the current audio fragment is already known.
557 *
558 * @param Pitch - MIDI pitch value (-8192 ... +8191)
559 * @param FragmentPos - sample point position in the current audio
560 * fragment to which this event belongs to
561 */
562 void EngineChannel::SendPitchbend(int Pitch, int32_t FragmentPos) {
563 if (FragmentPos < 0) {
564 dmsg(1,("EngineChannel::SendPitchBend(): negative FragmentPos! Seems MIDI driver is buggy!"));
565 }
566 else if (pEngine) {
567 Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos);
568 event.Type = Event::type_pitchbend;
569 event.Param.Pitch.Pitch = Pitch;
570 event.pEngineChannel = this;
571 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
572 else dmsg(1,("EngineChannel: Input event queue full!"));
573 }
574 }
575
576 /**
577 * Will be called by the MIDIIn Thread to signal the audio thread that a
578 * continuous controller value has changed. This method is meant for real
579 * time rendering, that is an event will immediately be created with the
580 * current system time as time stamp.
581 *
582 * @param Controller - MIDI controller number of the occured control change
583 * @param Value - value of the control change
584 */
585 void EngineChannel::SendControlChange(uint8_t Controller, uint8_t Value) {
586 if (pEngine) {
587 Event event = pEngine->pEventGenerator->CreateEvent();
588 event.Type = Event::type_control_change;
589 event.Param.CC.Controller = Controller;
590 event.Param.CC.Value = Value;
591 event.pEngineChannel = this;
592 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
593 else dmsg(1,("EngineChannel: Input event queue full!"));
594 }
595 }
596
597 /**
598 * Will be called by the MIDIIn Thread to signal the audio thread that a
599 * continuous controller value has changed. This method is meant for
600 * offline rendering and / or for cases where the exact position of the
601 * event in the current audio fragment is already known.
602 *
603 * @param Controller - MIDI controller number of the occured control change
604 * @param Value - value of the control change
605 * @param FragmentPos - sample point position in the current audio
606 * fragment to which this event belongs to
607 */
608 void EngineChannel::SendControlChange(uint8_t Controller, uint8_t Value, int32_t FragmentPos) {
609 if (FragmentPos < 0) {
610 dmsg(1,("EngineChannel::SendControlChange(): negative FragmentPos! Seems MIDI driver is buggy!"));
611 }
612 else if (pEngine) {
613 Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos);
614 event.Type = Event::type_control_change;
615 event.Param.CC.Controller = Controller;
616 event.Param.CC.Value = Value;
617 event.pEngineChannel = this;
618 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
619 else dmsg(1,("EngineChannel: Input event queue full!"));
620 }
621 }
622
623 void EngineChannel::ClearEventLists() {
624 pEvents->clear();
625 // empty MIDI key specific event lists
626 {
627 RTList<uint>::Iterator iuiKey = pActiveKeys->first();
628 RTList<uint>::Iterator end = pActiveKeys->end();
629 for(; iuiKey != end; ++iuiKey) {
630 pMIDIKeyInfo[*iuiKey].pEvents->clear(); // free all events on the key
631 }
632 }
633 }
634
635 void EngineChannel::ResetControllers() {
636 Pitch = 0;
637 SustainPedal = false;
638 SostenutoPedal = false;
639 GlobalVolume = 1.0f;
640 MidiVolume = 1.0;
641 GlobalPanLeft = 1.0f;
642 GlobalPanRight = 1.0f;
643 GlobalTranspose = 0;
644 // set all MIDI controller values to zero
645 memset(ControllerTable, 0x00, 129);
646 // reset all FX Send levels
647 for (
648 std::vector<FxSend*>::iterator iter = fxSends.begin();
649 iter != fxSends.end(); iter++
650 ) {
651 (*iter)->Reset();
652 }
653 }
654
655 /**
656 * Copy all events from the engine channel's input event queue buffer to
657 * the internal event list. This will be done at the beginning of each
658 * audio cycle (that is each RenderAudio() call) to distinguish all
659 * events which have to be processed in the current audio cycle. Each
660 * EngineChannel has it's own input event queue for the common channel
661 * specific events (like NoteOn, NoteOff and ControlChange events).
662 * Beside that, the engine also has a input event queue for global
663 * events (usually SysEx messages).
664 *
665 * @param Samples - number of sample points to be processed in the
666 * current audio cycle
667 */
668 void EngineChannel::ImportEvents(uint Samples) {
669 RingBuffer<Event,false>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader();
670 Event* pEvent;
671 while (true) {
672 // get next event from input event queue
673 if (!(pEvent = eventQueueReader.pop())) break;
674 // if younger event reached, ignore that and all subsequent ones for now
675 if (pEvent->FragmentPos() >= Samples) {
676 eventQueueReader--;
677 dmsg(2,("Younger Event, pos=%d ,Samples=%d!\n",pEvent->FragmentPos(),Samples));
678 pEvent->ResetFragmentPos();
679 break;
680 }
681 // copy event to internal event list
682 if (pEvents->poolIsEmpty()) {
683 dmsg(1,("Event pool emtpy!\n"));
684 break;
685 }
686 *pEvents->allocAppend() = *pEvent;
687 }
688 eventQueueReader.free(); // free all copied events from input queue
689 }
690
691 void EngineChannel::RemoveAllFxSends() {
692 if (pEngine) pEngine->DisableAndLock();
693 if (!fxSends.empty()) { // free local render buffers
694 if (pChannelLeft) {
695 delete pChannelLeft;
696 if (pEngine && pEngine->pAudioOutputDevice) {
697 // fallback to render directly to the AudioOutputDevice's buffer
698 pChannelLeft = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft);
699 } else pChannelLeft = NULL;
700 }
701 if (pChannelRight) {
702 delete pChannelRight;
703 if (pEngine && pEngine->pAudioOutputDevice) {
704 // fallback to render directly to the AudioOutputDevice's buffer
705 pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight);
706 } else pChannelRight = NULL;
707 }
708 }
709 for (int i = 0; i < fxSends.size(); i++) delete fxSends[i];
710 fxSends.clear();
711 if (pEngine) pEngine->Enable();
712 }
713
714 float EngineChannel::Volume() {
715 return GlobalVolume;
716 }
717
718 void EngineChannel::Volume(float f) {
719 GlobalVolume = f;
720 bStatusChanged = true; // status of engine channel has changed, so set notify flag
721 }
722
723 uint EngineChannel::Channels() {
724 return 2;
725 }
726
727 String EngineChannel::InstrumentFileName() {
728 return InstrumentFile;
729 }
730
731 String EngineChannel::InstrumentName() {
732 return InstrumentIdxName;
733 }
734
735 int EngineChannel::InstrumentIndex() {
736 return InstrumentIdx;
737 }
738
739 int EngineChannel::InstrumentStatus() {
740 return InstrumentStat;
741 }
742
743 String EngineChannel::EngineName() {
744 return LS_GIG_ENGINE_NAME;
745 }
746
747 }} // namespace LinuxSampler::gig

  ViewVC Help
Powered by ViewVC