/[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 1212 - (show annotations) (download)
Tue May 29 23:59:36 2007 UTC (16 years, 11 months ago) by schoenebeck
File size: 31674 byte(s)
* added highly experimental support for on-the-fly instrument editing
  within the sampler's process (by using instrument editor plugins),
  you'll notice the new "Registered instrument editors:" message on
  startup, the plugin path can be overridden at compile time with
  ./configure --enable-plugin-dir=/some/dir
* added a new LSCP command "EDIT INSTRUMENT <sampler-channel>" to spawn
  a matching instrument editor for the instrument on the given sampler
  channel (LSCP command syntax might be subject to change soon)
* config.h is not going to be installed along with liblinuxsampler's
  API header files anymore (not necessary anymore)
* take care of $(DESTDIR) when creating the instruments DB on 'make
  install' rule (needed for packaging and cross compilation)
* bumped version to 0.4.0.5cvs

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 String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message;
200 throw Exception(msg);
201 }
202 catch (InstrumentManagerException e) {
203 InstrumentStat = -3;
204 String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message();
205 throw Exception(msg);
206 }
207 catch (...) {
208 InstrumentStat = -4;
209 throw Exception("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file.");
210 }
211
212 // rebuild ActiveKeyGroups map with key groups of current instrument
213 for (::gig::Region* pRegion = newInstrument->GetFirstRegion(); pRegion; pRegion = newInstrument->GetNextRegion())
214 if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL;
215
216 InstrumentIdxName = newInstrument->pInfo->Name;
217 InstrumentStat = 100;
218
219 if (pEngine) pEngine->ChangeInstrument(this, newInstrument);
220 else pInstrument = newInstrument;
221 }
222
223 /**
224 * Will be called by the InstrumentResourceManager when the instrument
225 * we are currently using on this EngineChannel is going to be updated,
226 * so we can stop playback before that happens.
227 */
228 void EngineChannel::ResourceToBeUpdated(::gig::Instrument* pResource, void*& pUpdateArg) {
229 dmsg(3,("gig::Engine: Received instrument update message.\n"));
230 if (pEngine) pEngine->DisableAndLock();
231 ResetInternal();
232 this->pInstrument = NULL;
233 }
234
235 /**
236 * Will be called by the InstrumentResourceManager when the instrument
237 * update process was completed, so we can continue with playback.
238 */
239 void EngineChannel::ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) {
240 this->pInstrument = pNewResource; //TODO: there are couple of engine parameters we should update here as well if the instrument was updated (see LoadInstrument())
241 if (pEngine) pEngine->Enable();
242 bStatusChanged = true; // status of engine has changed, so set notify flag
243 }
244
245 /**
246 * Will be called by the InstrumentResourceManager on progress changes
247 * while loading or realoading an instrument for this EngineChannel.
248 *
249 * @param fProgress - current progress as value between 0.0 and 1.0
250 */
251 void EngineChannel::OnResourceProgress(float fProgress) {
252 this->InstrumentStat = int(fProgress * 100.0f);
253 dmsg(7,("gig::EngineChannel: progress %d%", InstrumentStat));
254 bStatusChanged = true; // status of engine has changed, so set notify flag
255 }
256
257 void EngineChannel::Connect(AudioOutputDevice* pAudioOut) {
258 if (pEngine) {
259 if (pEngine->pAudioOutputDevice == pAudioOut) return;
260 DisconnectAudioOutputDevice();
261 }
262 pEngine = Engine::AcquireEngine(this, pAudioOut);
263 ResetInternal();
264 pEvents = new RTList<Event>(pEngine->pEventPool);
265 for (uint i = 0; i < 128; i++) {
266 pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool);
267 pMIDIKeyInfo[i].pEvents = new RTList<Event>(pEngine->pEventPool);
268 }
269 AudioDeviceChannelLeft = 0;
270 AudioDeviceChannelRight = 1;
271 if (fxSends.empty()) { // render directly into the AudioDevice's output buffers
272 pChannelLeft = pAudioOut->Channel(AudioDeviceChannelLeft);
273 pChannelRight = pAudioOut->Channel(AudioDeviceChannelRight);
274 } else { // use local buffers for rendering and copy later
275 // ensure the local buffers have the correct size
276 if (pChannelLeft) delete pChannelLeft;
277 if (pChannelRight) delete pChannelRight;
278 pChannelLeft = new AudioChannel(0, pAudioOut->MaxSamplesPerCycle());
279 pChannelRight = new AudioChannel(1, pAudioOut->MaxSamplesPerCycle());
280 }
281 if (pEngine->EngineDisabled.GetUnsafe()) pEngine->Enable();
282 MidiInputPort::AddSysexListener(pEngine);
283 }
284
285 void EngineChannel::DisconnectAudioOutputDevice() {
286 if (pEngine) { // if clause to prevent disconnect loops
287 ResetInternal();
288 if (pEvents) {
289 delete pEvents;
290 pEvents = NULL;
291 }
292 for (uint i = 0; i < 128; i++) {
293 if (pMIDIKeyInfo[i].pActiveVoices) {
294 delete pMIDIKeyInfo[i].pActiveVoices;
295 pMIDIKeyInfo[i].pActiveVoices = NULL;
296 }
297 if (pMIDIKeyInfo[i].pEvents) {
298 delete pMIDIKeyInfo[i].pEvents;
299 pMIDIKeyInfo[i].pEvents = NULL;
300 }
301 }
302 Engine* oldEngine = pEngine;
303 AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice;
304 pEngine = NULL;
305 Engine::FreeEngine(this, oldAudioDevice);
306 AudioDeviceChannelLeft = -1;
307 AudioDeviceChannelRight = -1;
308 if (!fxSends.empty()) { // free the local rendering buffers
309 if (pChannelLeft) delete pChannelLeft;
310 if (pChannelRight) delete pChannelRight;
311 }
312 pChannelLeft = NULL;
313 pChannelRight = NULL;
314 }
315 }
316
317 AudioOutputDevice* EngineChannel::GetAudioOutputDevice() {
318 return (pEngine) ? pEngine->pAudioOutputDevice : NULL;
319 }
320
321 void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) {
322 if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet.");
323
324 AudioChannel* pChannel = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannel);
325 if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel));
326 switch (EngineAudioChannel) {
327 case 0: // left output channel
328 if (fxSends.empty()) pChannelLeft = pChannel;
329 AudioDeviceChannelLeft = AudioDeviceChannel;
330 break;
331 case 1: // right output channel
332 if (fxSends.empty()) pChannelRight = pChannel;
333 AudioDeviceChannelRight = AudioDeviceChannel;
334 break;
335 default:
336 throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel));
337 }
338 }
339
340 int EngineChannel::OutputChannel(uint EngineAudioChannel) {
341 switch (EngineAudioChannel) {
342 case 0: // left channel
343 return AudioDeviceChannelLeft;
344 case 1: // right channel
345 return AudioDeviceChannelRight;
346 default:
347 throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel));
348 }
349 }
350
351 void EngineChannel::Connect(MidiInputPort* pMidiPort, midi_chan_t MidiChannel) {
352 if (!pMidiPort || pMidiPort == this->pMidiInputPort) return;
353 DisconnectMidiInputPort();
354 this->pMidiInputPort = pMidiPort;
355 this->midiChannel = MidiChannel;
356 pMidiPort->Connect(this, MidiChannel);
357 }
358
359 void EngineChannel::DisconnectMidiInputPort() {
360 MidiInputPort* pOldPort = this->pMidiInputPort;
361 this->pMidiInputPort = NULL;
362 if (pOldPort) pOldPort->Disconnect(this);
363 }
364
365 MidiInputPort* EngineChannel::GetMidiInputPort() {
366 return pMidiInputPort;
367 }
368
369 midi_chan_t EngineChannel::MidiChannel() {
370 return midiChannel;
371 }
372
373 FxSend* EngineChannel::AddFxSend(uint8_t MidiCtrl, String Name) throw (Exception) {
374 if (pEngine) pEngine->DisableAndLock();
375 FxSend* pFxSend = new FxSend(this, MidiCtrl, Name);
376 if (fxSends.empty()) {
377 if (pEngine && pEngine->pAudioOutputDevice) {
378 AudioOutputDevice* pDevice = pEngine->pAudioOutputDevice;
379 // create local render buffers
380 pChannelLeft = new AudioChannel(0, pDevice->MaxSamplesPerCycle());
381 pChannelRight = new AudioChannel(1, pDevice->MaxSamplesPerCycle());
382 } else {
383 // postpone local render buffer creation until audio device is assigned
384 pChannelLeft = NULL;
385 pChannelRight = NULL;
386 }
387 }
388 fxSends.push_back(pFxSend);
389 if (pEngine) pEngine->Enable();
390 fireFxSendCountChanged(iSamplerChannelIndex, GetFxSendCount());
391
392 return pFxSend;
393 }
394
395 FxSend* EngineChannel::GetFxSend(uint FxSendIndex) {
396 return (FxSendIndex < fxSends.size()) ? fxSends[FxSendIndex] : NULL;
397 }
398
399 uint EngineChannel::GetFxSendCount() {
400 return fxSends.size();
401 }
402
403 void EngineChannel::RemoveFxSend(FxSend* pFxSend) {
404 if (pEngine) pEngine->DisableAndLock();
405 for (
406 std::vector<FxSend*>::iterator iter = fxSends.begin();
407 iter != fxSends.end(); iter++
408 ) {
409 if (*iter == pFxSend) {
410 delete pFxSend;
411 fxSends.erase(iter);
412 if (fxSends.empty()) {
413 // destroy local render buffers
414 if (pChannelLeft) delete pChannelLeft;
415 if (pChannelRight) delete pChannelRight;
416 // fallback to render directly into AudioOutputDevice's buffers
417 if (pEngine && pEngine->pAudioOutputDevice) {
418 pChannelLeft = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft);
419 pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight);
420 } else { // we update the pointers later
421 pChannelLeft = NULL;
422 pChannelRight = NULL;
423 }
424 }
425 break;
426 }
427 }
428 if (pEngine) pEngine->Enable();
429 fireFxSendCountChanged(iSamplerChannelIndex, GetFxSendCount());
430 }
431
432 /**
433 * Will be called by the MIDIIn Thread to let the audio thread trigger a new
434 * voice for the given key. This method is meant for real time rendering,
435 * that is an event will immediately be created with the current system
436 * time as time stamp.
437 *
438 * @param Key - MIDI key number of the triggered key
439 * @param Velocity - MIDI velocity value of the triggered key
440 */
441 void EngineChannel::SendNoteOn(uint8_t Key, uint8_t Velocity) {
442 if (pEngine) {
443 Event event = pEngine->pEventGenerator->CreateEvent();
444 event.Type = Event::type_note_on;
445 event.Param.Note.Key = Key;
446 event.Param.Note.Velocity = Velocity;
447 event.pEngineChannel = this;
448 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
449 else dmsg(1,("EngineChannel: Input event queue full!"));
450 }
451 }
452
453 /**
454 * Will be called by the MIDIIn Thread to let the audio thread trigger a new
455 * voice for the given key. This method is meant for offline rendering
456 * and / or for cases where the exact position of the event in the current
457 * audio fragment is already known.
458 *
459 * @param Key - MIDI key number of the triggered key
460 * @param Velocity - MIDI velocity value of the triggered key
461 * @param FragmentPos - sample point position in the current audio
462 * fragment to which this event belongs to
463 */
464 void EngineChannel::SendNoteOn(uint8_t Key, uint8_t Velocity, int32_t FragmentPos) {
465 if (FragmentPos < 0) {
466 dmsg(1,("EngineChannel::SendNoteOn(): negative FragmentPos! Seems MIDI driver is buggy!"));
467 }
468 else if (pEngine) {
469 Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos);
470 event.Type = Event::type_note_on;
471 event.Param.Note.Key = Key;
472 event.Param.Note.Velocity = Velocity;
473 event.pEngineChannel = this;
474 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
475 else dmsg(1,("EngineChannel: Input event queue full!"));
476 }
477 }
478
479 /**
480 * Will be called by the MIDIIn Thread to signal the audio thread to release
481 * voice(s) on the given key. This method is meant for real time rendering,
482 * that is an event will immediately be created with the current system
483 * time as time stamp.
484 *
485 * @param Key - MIDI key number of the released key
486 * @param Velocity - MIDI release velocity value of the released key
487 */
488 void EngineChannel::SendNoteOff(uint8_t Key, uint8_t Velocity) {
489 if (pEngine) {
490 Event event = pEngine->pEventGenerator->CreateEvent();
491 event.Type = Event::type_note_off;
492 event.Param.Note.Key = Key;
493 event.Param.Note.Velocity = Velocity;
494 event.pEngineChannel = this;
495 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
496 else dmsg(1,("EngineChannel: Input event queue full!"));
497 }
498 }
499
500 /**
501 * Will be called by the MIDIIn Thread to signal the audio thread to release
502 * voice(s) on the given key. This method is meant for offline rendering
503 * and / or for cases where the exact position of the event in the current
504 * audio fragment is already known.
505 *
506 * @param Key - MIDI key number of the released key
507 * @param Velocity - MIDI release velocity value of the released key
508 * @param FragmentPos - sample point position in the current audio
509 * fragment to which this event belongs to
510 */
511 void EngineChannel::SendNoteOff(uint8_t Key, uint8_t Velocity, int32_t FragmentPos) {
512 if (FragmentPos < 0) {
513 dmsg(1,("EngineChannel::SendNoteOff(): negative FragmentPos! Seems MIDI driver is buggy!"));
514 }
515 else if (pEngine) {
516 Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos);
517 event.Type = Event::type_note_off;
518 event.Param.Note.Key = Key;
519 event.Param.Note.Velocity = Velocity;
520 event.pEngineChannel = this;
521 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
522 else dmsg(1,("EngineChannel: Input event queue full!"));
523 }
524 }
525
526 /**
527 * Will be called by the MIDIIn Thread to signal the audio thread to change
528 * the pitch value for all voices. This method is meant for real time
529 * rendering, that is an event will immediately be created with the
530 * current system time as time stamp.
531 *
532 * @param Pitch - MIDI pitch value (-8192 ... +8191)
533 */
534 void EngineChannel::SendPitchbend(int Pitch) {
535 if (pEngine) {
536 Event event = pEngine->pEventGenerator->CreateEvent();
537 event.Type = Event::type_pitchbend;
538 event.Param.Pitch.Pitch = Pitch;
539 event.pEngineChannel = this;
540 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
541 else dmsg(1,("EngineChannel: Input event queue full!"));
542 }
543 }
544
545 /**
546 * Will be called by the MIDIIn Thread to signal the audio thread to change
547 * the pitch value for all voices. This method is meant for offline
548 * rendering and / or for cases where the exact position of the event in
549 * the current audio fragment is already known.
550 *
551 * @param Pitch - MIDI pitch value (-8192 ... +8191)
552 * @param FragmentPos - sample point position in the current audio
553 * fragment to which this event belongs to
554 */
555 void EngineChannel::SendPitchbend(int Pitch, int32_t FragmentPos) {
556 if (FragmentPos < 0) {
557 dmsg(1,("EngineChannel::SendPitchBend(): negative FragmentPos! Seems MIDI driver is buggy!"));
558 }
559 else if (pEngine) {
560 Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos);
561 event.Type = Event::type_pitchbend;
562 event.Param.Pitch.Pitch = Pitch;
563 event.pEngineChannel = this;
564 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
565 else dmsg(1,("EngineChannel: Input event queue full!"));
566 }
567 }
568
569 /**
570 * Will be called by the MIDIIn Thread to signal the audio thread that a
571 * continuous controller value has changed. This method is meant for real
572 * time rendering, that is an event will immediately be created with the
573 * current system time as time stamp.
574 *
575 * @param Controller - MIDI controller number of the occured control change
576 * @param Value - value of the control change
577 */
578 void EngineChannel::SendControlChange(uint8_t Controller, uint8_t Value) {
579 if (pEngine) {
580 Event event = pEngine->pEventGenerator->CreateEvent();
581 event.Type = Event::type_control_change;
582 event.Param.CC.Controller = Controller;
583 event.Param.CC.Value = Value;
584 event.pEngineChannel = this;
585 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
586 else dmsg(1,("EngineChannel: Input event queue full!"));
587 }
588 }
589
590 /**
591 * Will be called by the MIDIIn Thread to signal the audio thread that a
592 * continuous controller value has changed. This method is meant for
593 * offline rendering and / or for cases where the exact position of the
594 * event in the current audio fragment is already known.
595 *
596 * @param Controller - MIDI controller number of the occured control change
597 * @param Value - value of the control change
598 * @param FragmentPos - sample point position in the current audio
599 * fragment to which this event belongs to
600 */
601 void EngineChannel::SendControlChange(uint8_t Controller, uint8_t Value, int32_t FragmentPos) {
602 if (FragmentPos < 0) {
603 dmsg(1,("EngineChannel::SendControlChange(): negative FragmentPos! Seems MIDI driver is buggy!"));
604 }
605 else if (pEngine) {
606 Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos);
607 event.Type = Event::type_control_change;
608 event.Param.CC.Controller = Controller;
609 event.Param.CC.Value = Value;
610 event.pEngineChannel = this;
611 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
612 else dmsg(1,("EngineChannel: Input event queue full!"));
613 }
614 }
615
616 void EngineChannel::ClearEventLists() {
617 pEvents->clear();
618 // empty MIDI key specific event lists
619 {
620 RTList<uint>::Iterator iuiKey = pActiveKeys->first();
621 RTList<uint>::Iterator end = pActiveKeys->end();
622 for(; iuiKey != end; ++iuiKey) {
623 pMIDIKeyInfo[*iuiKey].pEvents->clear(); // free all events on the key
624 }
625 }
626 }
627
628 void EngineChannel::ResetControllers() {
629 Pitch = 0;
630 SustainPedal = false;
631 SostenutoPedal = false;
632 GlobalVolume = 1.0f;
633 MidiVolume = 1.0;
634 GlobalPanLeft = 1.0f;
635 GlobalPanRight = 1.0f;
636 GlobalTranspose = 0;
637 // set all MIDI controller values to zero
638 memset(ControllerTable, 0x00, 129);
639 // reset all FX Send levels
640 for (
641 std::vector<FxSend*>::iterator iter = fxSends.begin();
642 iter != fxSends.end(); iter++
643 ) {
644 (*iter)->Reset();
645 }
646 }
647
648 /**
649 * Copy all events from the engine channel's input event queue buffer to
650 * the internal event list. This will be done at the beginning of each
651 * audio cycle (that is each RenderAudio() call) to distinguish all
652 * events which have to be processed in the current audio cycle. Each
653 * EngineChannel has it's own input event queue for the common channel
654 * specific events (like NoteOn, NoteOff and ControlChange events).
655 * Beside that, the engine also has a input event queue for global
656 * events (usually SysEx messages).
657 *
658 * @param Samples - number of sample points to be processed in the
659 * current audio cycle
660 */
661 void EngineChannel::ImportEvents(uint Samples) {
662 RingBuffer<Event,false>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader();
663 Event* pEvent;
664 while (true) {
665 // get next event from input event queue
666 if (!(pEvent = eventQueueReader.pop())) break;
667 // if younger event reached, ignore that and all subsequent ones for now
668 if (pEvent->FragmentPos() >= Samples) {
669 eventQueueReader--;
670 dmsg(2,("Younger Event, pos=%d ,Samples=%d!\n",pEvent->FragmentPos(),Samples));
671 pEvent->ResetFragmentPos();
672 break;
673 }
674 // copy event to internal event list
675 if (pEvents->poolIsEmpty()) {
676 dmsg(1,("Event pool emtpy!\n"));
677 break;
678 }
679 *pEvents->allocAppend() = *pEvent;
680 }
681 eventQueueReader.free(); // free all copied events from input queue
682 }
683
684 void EngineChannel::RemoveAllFxSends() {
685 if (pEngine) pEngine->DisableAndLock();
686 if (!fxSends.empty()) { // free local render buffers
687 if (pChannelLeft) {
688 delete pChannelLeft;
689 if (pEngine && pEngine->pAudioOutputDevice) {
690 // fallback to render directly to the AudioOutputDevice's buffer
691 pChannelLeft = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft);
692 } else pChannelLeft = NULL;
693 }
694 if (pChannelRight) {
695 delete pChannelRight;
696 if (pEngine && pEngine->pAudioOutputDevice) {
697 // fallback to render directly to the AudioOutputDevice's buffer
698 pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight);
699 } else pChannelRight = NULL;
700 }
701 }
702 for (int i = 0; i < fxSends.size(); i++) delete fxSends[i];
703 fxSends.clear();
704 if (pEngine) pEngine->Enable();
705 }
706
707 float EngineChannel::Volume() {
708 return GlobalVolume;
709 }
710
711 void EngineChannel::Volume(float f) {
712 GlobalVolume = f;
713 bStatusChanged = true; // status of engine channel has changed, so set notify flag
714 }
715
716 uint EngineChannel::Channels() {
717 return 2;
718 }
719
720 String EngineChannel::InstrumentFileName() {
721 return InstrumentFile;
722 }
723
724 String EngineChannel::InstrumentName() {
725 return InstrumentIdxName;
726 }
727
728 int EngineChannel::InstrumentIndex() {
729 return InstrumentIdx;
730 }
731
732 int EngineChannel::InstrumentStatus() {
733 return InstrumentStat;
734 }
735
736 String EngineChannel::EngineName() {
737 return LS_GIG_ENGINE_NAME;
738 }
739
740 }} // namespace LinuxSampler::gig

  ViewVC Help
Powered by ViewVC