/[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 1041 - (show annotations) (download)
Wed Feb 7 17:45:19 2007 UTC (12 years, 8 months ago) by schoenebeck
File size: 31600 byte(s)
* handle MIDI coarse tuning messages (MIDI RPN #0 MSB, #2 LSB),
  currently lazy implementation, transpose value is simply added on the
  note on/off values instead only at the mandatory places, thus when
  altering transpose while playing, voices can unintendedly survive

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

  ViewVC Help
Powered by ViewVC