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

Contents of /linuxsampler/trunk/src/engines/AbstractEngineChannel.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2879 - (show annotations) (download)
Tue Apr 19 14:07:53 2016 UTC (2 years, 5 months ago) by schoenebeck
File size: 48489 byte(s)
* All engines: Active voices are now internally grouped to "Note" objects,
  instead of being directly assigned to a keyboard key. This allows more
  fine graded processing of voices, which is i.e. required for certain
  instrument script features.
* Built-in script function "play_note()": Added support for passing
  special value -1 for "duration-us" argument, which will cause the
  triggered note to be released once the original note was released.
* Bumped version (2.0.0.svn3).

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck *
6 * Copyright (C) 2005-2008 Christian Schoenebeck *
7 * Copyright (C) 2009-2012 Christian Schoenebeck and Grigor Iliev *
8 * Copyright (C) 2012-2016 Christian Schoenebeck and Andreas Persson *
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 * This program is distributed in the hope that it will be useful, *
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
18 * GNU General Public License for more details. *
19 * *
20 * You should have received a copy of the GNU General Public License *
21 * along with this program; if not, write to the Free Software *
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
23 * MA 02111-1307 USA *
24 ***************************************************************************/
25
26 #include "AbstractEngineChannel.h"
27 #include "../common/global_private.h"
28 #include "../Sampler.h"
29
30 namespace LinuxSampler {
31
32 AbstractEngineChannel::AbstractEngineChannel() :
33 virtualMidiDevicesReader_AudioThread(virtualMidiDevices),
34 virtualMidiDevicesReader_MidiThread(virtualMidiDevices)
35 {
36 pEngine = NULL;
37 pEvents = NULL; // we allocate when we retrieve the right Engine object
38 delayedEvents.pList = NULL;
39 pEventQueue = new RingBuffer<Event,false>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0);
40 InstrumentIdx = -1;
41 InstrumentStat = -1;
42 pChannelLeft = NULL;
43 pChannelRight = NULL;
44 AudioDeviceChannelLeft = -1;
45 AudioDeviceChannelRight = -1;
46 midiChannel = midi_chan_all;
47 ResetControllers();
48 PortamentoMode = false;
49 PortamentoTime = CONFIG_PORTAMENTO_TIME_DEFAULT;
50 pScript = NULL;
51 }
52
53 AbstractEngineChannel::~AbstractEngineChannel() {
54 delete pEventQueue;
55 DeleteGroupEventLists();
56 RemoveAllFxSends();
57 }
58
59 Engine* AbstractEngineChannel::GetEngine() {
60 return pEngine;
61 }
62
63 uint AbstractEngineChannel::Channels() {
64 return 2;
65 }
66
67 /**
68 * More or less a workaround to set the instrument name, index and load
69 * status variable to zero percent immediately, that is without blocking
70 * the calling thread. It might be used in future for other preparations
71 * as well though.
72 *
73 * @param FileName - file name of the instrument file
74 * @param Instrument - index of the instrument in the file
75 * @see LoadInstrument()
76 */
77 void AbstractEngineChannel::PrepareLoadInstrument(const char* FileName, uint Instrument) {
78 InstrumentFile = FileName;
79 InstrumentIdx = Instrument;
80 InstrumentStat = 0;
81 }
82
83 String AbstractEngineChannel::InstrumentFileName() {
84 return InstrumentFile;
85 }
86
87 String AbstractEngineChannel::InstrumentName() {
88 return InstrumentIdxName;
89 }
90
91 int AbstractEngineChannel::InstrumentIndex() {
92 return InstrumentIdx;
93 }
94
95 int AbstractEngineChannel::InstrumentStatus() {
96 return InstrumentStat;
97 }
98
99 String AbstractEngineChannel::EngineName() {
100 return AbstractEngine::GetFormatString(GetEngineFormat());
101 }
102
103 void AbstractEngineChannel::Reset() {
104 if (pEngine) pEngine->DisableAndLock();
105 ResetInternal(false/*don't reset engine*/);
106 ResetControllers();
107 if (pEngine) {
108 pEngine->Enable();
109 pEngine->Reset();
110 }
111 }
112
113 void AbstractEngineChannel::ResetControllers() {
114 Pitch = 0;
115 GlobalVolume = 1.0f;
116 MidiVolume = 1.0;
117 iLastPanRequest = 64;
118 GlobalTranspose = 0;
119 // set all MIDI controller values to zero
120 memset(ControllerTable, 0x00, 129);
121 // reset all FX Send levels
122 for (
123 std::vector<FxSend*>::iterator iter = fxSends.begin();
124 iter != fxSends.end(); iter++
125 ) {
126 (*iter)->Reset();
127 }
128 }
129
130 /**
131 * This method is not thread safe!
132 */
133 void AbstractEngineChannel::ResetInternal(bool bResetEngine) {
134 CurrentKeyDimension = 0;
135 PortamentoPos = -1.0f; // no portamento active yet
136
137 // delete all active instrument script events
138 if (pScript) pScript->resetEvents();
139
140 // free all delayed MIDI events
141 delayedEvents.clear();
142
143 // delete all input events
144 pEventQueue->init();
145
146 if (bResetEngine && pEngine) pEngine->ResetInternal();
147
148 // status of engine channel has changed, so set notify flag
149 bStatusChanged = true;
150 }
151
152 /**
153 * Implementation of virtual method from abstract EngineChannel interface.
154 * This method will periodically be polled (e.g. by the LSCP server) to
155 * check if some engine channel parameter has changed since the last
156 * StatusChanged() call.
157 *
158 * This method can also be used to mark the engine channel as changed
159 * from outside, e.g. by a MIDI input device. The optional argument
160 * \a nNewStatus can be used for this.
161 *
162 * TODO: This "poll method" is just a lazy solution and might be
163 * replaced in future.
164 * @param bNewStatus - (optional, default: false) sets the new status flag
165 * @returns true if engine channel status has changed since last
166 * StatusChanged() call
167 */
168 bool AbstractEngineChannel::StatusChanged(bool bNewStatus) {
169 bool b = bStatusChanged;
170 bStatusChanged = bNewStatus;
171 return b;
172 }
173
174 float AbstractEngineChannel::Volume() {
175 return GlobalVolume;
176 }
177
178 void AbstractEngineChannel::Volume(float f) {
179 GlobalVolume = f;
180 bStatusChanged = true; // status of engine channel has changed, so set notify flag
181 }
182
183 float AbstractEngineChannel::Pan() {
184 return float(iLastPanRequest - 64) / 64.0f;
185 }
186
187 void AbstractEngineChannel::Pan(float f) {
188 int iMidiPan = int(f * 64.0f) + 64;
189 if (iMidiPan > 127) iMidiPan = 127;
190 else if (iMidiPan < 0) iMidiPan = 0;
191 iLastPanRequest = iMidiPan;
192 }
193
194 AudioOutputDevice* AbstractEngineChannel::GetAudioOutputDevice() {
195 return (pEngine) ? pEngine->pAudioOutputDevice : NULL;
196 }
197
198 /**
199 * Gets thread safe access to the currently connected audio output
200 * device from other threads than the lscp thread.
201 */
202 AudioOutputDevice* AbstractEngineChannel::GetAudioOutputDeviceSafe() {
203 LockGuard lock(EngineMutex);
204 return GetAudioOutputDevice();
205 }
206
207 void AbstractEngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) {
208 if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet.");
209
210 AudioChannel* pChannel = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannel);
211 if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel));
212 switch (EngineAudioChannel) {
213 case 0: // left output channel
214 if (fxSends.empty()) pChannelLeft = pChannel;
215 AudioDeviceChannelLeft = AudioDeviceChannel;
216 break;
217 case 1: // right output channel
218 if (fxSends.empty()) pChannelRight = pChannel;
219 AudioDeviceChannelRight = AudioDeviceChannel;
220 break;
221 default:
222 throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel));
223 }
224
225 bStatusChanged = true;
226 }
227
228 int AbstractEngineChannel::OutputChannel(uint EngineAudioChannel) {
229 switch (EngineAudioChannel) {
230 case 0: // left channel
231 return AudioDeviceChannelLeft;
232 case 1: // right channel
233 return AudioDeviceChannelRight;
234 default:
235 throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel));
236 }
237 }
238
239 void AbstractEngineChannel::Connect(MidiInputPort* pMidiPort) {
240 if (!pMidiPort) return;
241
242 Sync< ArrayList<MidiInputPort*> > connections = midiInputs.back();
243
244 // check if connection already exists
245 for (int i = 0; i < connections->size(); ++i)
246 if ((*connections)[i] == pMidiPort)
247 return; // to avoid endless recursion
248
249 connections->add(pMidiPort);
250
251 // inform MIDI port about this new connection
252 pMidiPort->Connect(this, MidiChannel());
253 }
254
255 void AbstractEngineChannel::Disconnect(MidiInputPort* pMidiPort) {
256 if (!pMidiPort) return;
257
258 Sync< ArrayList<MidiInputPort*> > connections = midiInputs.back();
259
260 for (int i = 0; i < connections->size(); ++i) {
261 if ((*connections)[i] == pMidiPort) {
262 connections->remove(i);
263 // inform MIDI port about this disconnection
264 pMidiPort->Disconnect(this);
265 return;
266 }
267 }
268 }
269
270 void AbstractEngineChannel::DisconnectAllMidiInputPorts() {
271 Sync< ArrayList<MidiInputPort*> > connections = midiInputs.back();
272 ArrayList<MidiInputPort*> clonedList = *connections;
273 connections->clear();
274 for (int i = 0; i < clonedList.size(); ++i) clonedList[i]->Disconnect(this);
275 }
276
277 uint AbstractEngineChannel::GetMidiInputPortCount() {
278 Sync< ArrayList<MidiInputPort*> > connections = midiInputs.back();
279 return connections->size();
280 }
281
282 MidiInputPort* AbstractEngineChannel::GetMidiInputPort(uint index) {
283 Sync< ArrayList<MidiInputPort*> > connections = midiInputs.back();
284 return (index < connections->size()) ? (*connections)[index] : NULL;
285 }
286
287 // deprecated (just for API backward compatibility) - may be removed in future
288 void AbstractEngineChannel::Connect(MidiInputPort* pMidiPort, midi_chan_t MidiChannel) {
289 if (!pMidiPort) return;
290
291 Sync< ArrayList<MidiInputPort*> > connections = midiInputs.back();
292
293 // check against endless recursion
294 if (connections->size() == 1 && (*connections)[0] == pMidiPort && this->midiChannel == MidiChannel)
295 return;
296
297 if (!isValidMidiChan(MidiChannel))
298 throw MidiInputException("Invalid MIDI channel (" + ToString(int(MidiChannel)) + ")");
299
300 this->midiChannel = MidiChannel;
301
302 // disconnect all currently connected MIDI ports
303 ArrayList<MidiInputPort*> clonedList = *connections;
304 connections->clear();
305 for (int i = 0; i < clonedList.size(); ++i)
306 clonedList[i]->Disconnect(this);
307
308 // connect the new port
309 connections->add(pMidiPort);
310 pMidiPort->Connect(this, MidiChannel);
311 }
312
313 // deprecated (just for API backward compatibility) - may be removed in future
314 void AbstractEngineChannel::DisconnectMidiInputPort() {
315 DisconnectAllMidiInputPorts();
316 }
317
318 // deprecated (just for API backward compatibility) - may be removed in future
319 MidiInputPort* AbstractEngineChannel::GetMidiInputPort() {
320 return GetMidiInputPort(0);
321 }
322
323 midi_chan_t AbstractEngineChannel::MidiChannel() {
324 return midiChannel;
325 }
326
327 void AbstractEngineChannel::SetMidiChannel(midi_chan_t MidiChannel) {
328 if (this->midiChannel == MidiChannel) return;
329 if (!isValidMidiChan(MidiChannel))
330 throw MidiInputException("Invalid MIDI channel (" + ToString(int(MidiChannel)) + ")");
331
332 this->midiChannel = MidiChannel;
333
334 Sync< ArrayList<MidiInputPort*> > connections = midiInputs.back();
335 ArrayList<MidiInputPort*> clonedList = *connections;
336
337 DisconnectAllMidiInputPorts();
338
339 for (int i = 0; i < clonedList.size(); ++i) Connect(clonedList[i]);
340 }
341
342 void AbstractEngineChannel::Connect(VirtualMidiDevice* pDevice) {
343 // double buffer ... double work ...
344 {
345 ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.GetConfigForUpdate();
346 devices.add(pDevice);
347 }
348 {
349 ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.SwitchConfig();
350 devices.add(pDevice);
351 }
352 }
353
354 void AbstractEngineChannel::Disconnect(VirtualMidiDevice* pDevice) {
355 // double buffer ... double work ...
356 {
357 ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.GetConfigForUpdate();
358 devices.remove(pDevice);
359 }
360 {
361 ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.SwitchConfig();
362 devices.remove(pDevice);
363 }
364 }
365
366 /**
367 * Will be called by the MIDIIn Thread to let the audio thread trigger a new
368 * voice for the given key. This method is meant for real time rendering,
369 * that is an event will immediately be created with the current system
370 * time as time stamp.
371 *
372 * @param Key - MIDI key number of the triggered key
373 * @param Velocity - MIDI velocity value of the triggered key
374 */
375 void AbstractEngineChannel::SendNoteOn(uint8_t Key, uint8_t Velocity, uint8_t MidiChannel) {
376 if (pEngine) {
377 // protection in case there are more than 1 MIDI input threads sending MIDI events to this EngineChannel
378 LockGuard g;
379 if (hasMultipleMIDIInputs()) g = LockGuard(MidiInputMutex);
380
381 Event event = pEngine->pEventGenerator->CreateEvent();
382 event.Type = Event::type_note_on;
383 event.Param.Note.Key = Key;
384 event.Param.Note.Velocity = Velocity;
385 event.Param.Note.Channel = MidiChannel;
386 event.pEngineChannel = this;
387 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
388 else dmsg(1,("EngineChannel: Input event queue full!"));
389 // inform connected virtual MIDI devices if any ...
390 // (e.g. virtual MIDI keyboard in instrument editor(s))
391 ArrayList<VirtualMidiDevice*>& devices =
392 const_cast<ArrayList<VirtualMidiDevice*>&>(
393 virtualMidiDevicesReader_MidiThread.Lock()
394 );
395 for (int i = 0; i < devices.size(); i++) {
396 devices[i]->SendNoteOnToDevice(Key, Velocity);
397 }
398 virtualMidiDevicesReader_MidiThread.Unlock();
399 }
400 }
401
402 /**
403 * Will be called by the MIDIIn Thread to let the audio thread trigger a new
404 * voice for the given key. This method is meant for offline rendering
405 * and / or for cases where the exact position of the event in the current
406 * audio fragment is already known.
407 *
408 * @param Key - MIDI key number of the triggered key
409 * @param Velocity - MIDI velocity value of the triggered key
410 * @param FragmentPos - sample point position in the current audio
411 * fragment to which this event belongs to
412 */
413 void AbstractEngineChannel::SendNoteOn(uint8_t Key, uint8_t Velocity, uint8_t MidiChannel, int32_t FragmentPos) {
414 if (FragmentPos < 0) {
415 dmsg(1,("EngineChannel::SendNoteOn(): negative FragmentPos! Seems MIDI driver is buggy!"));
416 }
417 else if (pEngine) {
418 // protection in case there are more than 1 MIDI input threads sending MIDI events to this EngineChannel
419 LockGuard g;
420 if (hasMultipleMIDIInputs()) g = LockGuard(MidiInputMutex);
421
422 Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos);
423 event.Type = Event::type_note_on;
424 event.Param.Note.Key = Key;
425 event.Param.Note.Velocity = Velocity;
426 event.Param.Note.Channel = MidiChannel;
427 event.pEngineChannel = this;
428 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
429 else dmsg(1,("EngineChannel: Input event queue full!"));
430 // inform connected virtual MIDI devices if any ...
431 // (e.g. virtual MIDI keyboard in instrument editor(s))
432 ArrayList<VirtualMidiDevice*>& devices =
433 const_cast<ArrayList<VirtualMidiDevice*>&>(
434 virtualMidiDevicesReader_MidiThread.Lock()
435 );
436 for (int i = 0; i < devices.size(); i++) {
437 devices[i]->SendNoteOnToDevice(Key, Velocity);
438 }
439 virtualMidiDevicesReader_MidiThread.Unlock();
440 }
441 }
442
443 /**
444 * Will be called by the MIDIIn Thread to signal the audio thread to release
445 * voice(s) on the given key. This method is meant for real time rendering,
446 * that is an event will immediately be created with the current system
447 * time as time stamp.
448 *
449 * @param Key - MIDI key number of the released key
450 * @param Velocity - MIDI release velocity value of the released key
451 */
452 void AbstractEngineChannel::SendNoteOff(uint8_t Key, uint8_t Velocity, uint8_t MidiChannel) {
453 if (pEngine) {
454 // protection in case there are more than 1 MIDI input threads sending MIDI events to this EngineChannel
455 LockGuard g;
456 if (hasMultipleMIDIInputs()) g = LockGuard(MidiInputMutex);
457
458 Event event = pEngine->pEventGenerator->CreateEvent();
459 event.Type = Event::type_note_off;
460 event.Param.Note.Key = Key;
461 event.Param.Note.Velocity = Velocity;
462 event.Param.Note.Channel = MidiChannel;
463 event.pEngineChannel = this;
464 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
465 else dmsg(1,("EngineChannel: Input event queue full!"));
466 // inform connected virtual MIDI devices if any ...
467 // (e.g. virtual MIDI keyboard in instrument editor(s))
468 ArrayList<VirtualMidiDevice*>& devices =
469 const_cast<ArrayList<VirtualMidiDevice*>&>(
470 virtualMidiDevicesReader_MidiThread.Lock()
471 );
472 for (int i = 0; i < devices.size(); i++) {
473 devices[i]->SendNoteOffToDevice(Key, Velocity);
474 }
475 virtualMidiDevicesReader_MidiThread.Unlock();
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 offline rendering
482 * and / or for cases where the exact position of the event in the current
483 * audio fragment is already known.
484 *
485 * @param Key - MIDI key number of the released key
486 * @param Velocity - MIDI release velocity value of the released key
487 * @param FragmentPos - sample point position in the current audio
488 * fragment to which this event belongs to
489 */
490 void AbstractEngineChannel::SendNoteOff(uint8_t Key, uint8_t Velocity, uint8_t MidiChannel, int32_t FragmentPos) {
491 if (FragmentPos < 0) {
492 dmsg(1,("EngineChannel::SendNoteOff(): negative FragmentPos! Seems MIDI driver is buggy!"));
493 }
494 else if (pEngine) {
495 // protection in case there are more than 1 MIDI input threads sending MIDI events to this EngineChannel
496 LockGuard g;
497 if (hasMultipleMIDIInputs()) g = LockGuard(MidiInputMutex);
498
499 Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos);
500 event.Type = Event::type_note_off;
501 event.Param.Note.Key = Key;
502 event.Param.Note.Velocity = Velocity;
503 event.Param.Note.Channel = MidiChannel;
504 event.pEngineChannel = this;
505 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
506 else dmsg(1,("EngineChannel: Input event queue full!"));
507 // inform connected virtual MIDI devices if any ...
508 // (e.g. virtual MIDI keyboard in instrument editor(s))
509 ArrayList<VirtualMidiDevice*>& devices =
510 const_cast<ArrayList<VirtualMidiDevice*>&>(
511 virtualMidiDevicesReader_MidiThread.Lock()
512 );
513 for (int i = 0; i < devices.size(); i++) {
514 devices[i]->SendNoteOffToDevice(Key, Velocity);
515 }
516 virtualMidiDevicesReader_MidiThread.Unlock();
517 }
518 }
519
520 /**
521 * Will be called by the MIDIIn Thread to signal the audio thread to change
522 * the pitch value for all voices. This method is meant for real time
523 * rendering, that is an event will immediately be created with the
524 * current system time as time stamp.
525 *
526 * @param Pitch - MIDI pitch value (-8192 ... +8191)
527 */
528 void AbstractEngineChannel::SendPitchbend(int Pitch, uint8_t MidiChannel) {
529 if (pEngine) {
530 // protection in case there are more than 1 MIDI input threads sending MIDI events to this EngineChannel
531 LockGuard g;
532 if (hasMultipleMIDIInputs()) g = LockGuard(MidiInputMutex);
533
534 Event event = pEngine->pEventGenerator->CreateEvent();
535 event.Type = Event::type_pitchbend;
536 event.Param.Pitch.Pitch = Pitch;
537 event.Param.Pitch.Channel = MidiChannel;
538 event.pEngineChannel = this;
539 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
540 else dmsg(1,("EngineChannel: Input event queue full!"));
541 }
542 }
543
544 /**
545 * Will be called by the MIDIIn Thread to signal the audio thread to change
546 * the pitch value for all voices. This method is meant for offline
547 * rendering and / or for cases where the exact position of the event in
548 * the current audio fragment is already known.
549 *
550 * @param Pitch - MIDI pitch value (-8192 ... +8191)
551 * @param FragmentPos - sample point position in the current audio
552 * fragment to which this event belongs to
553 */
554 void AbstractEngineChannel::SendPitchbend(int Pitch, uint8_t MidiChannel, int32_t FragmentPos) {
555 if (FragmentPos < 0) {
556 dmsg(1,("AbstractEngineChannel::SendPitchBend(): negative FragmentPos! Seems MIDI driver is buggy!"));
557 }
558 else if (pEngine) {
559 // protection in case there are more than 1 MIDI input threads sending MIDI events to this EngineChannel
560 LockGuard g;
561 if (hasMultipleMIDIInputs()) g = LockGuard(MidiInputMutex);
562
563 Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos);
564 event.Type = Event::type_pitchbend;
565 event.Param.Pitch.Pitch = Pitch;
566 event.Param.Pitch.Channel = MidiChannel;
567 event.pEngineChannel = this;
568 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
569 else dmsg(1,("AbstractEngineChannel: Input event queue full!"));
570 }
571 }
572
573 /**
574 * Will be called by the MIDIIn Thread to signal the audio thread that a
575 * continuous controller value has changed. This method is meant for real
576 * time rendering, that is an event will immediately be created with the
577 * current system time as time stamp.
578 *
579 * @param Controller - MIDI controller number of the occured control change
580 * @param Value - value of the control change
581 */
582 void AbstractEngineChannel::SendControlChange(uint8_t Controller, uint8_t Value, uint8_t MidiChannel) {
583 if (pEngine) {
584 // protection in case there are more than 1 MIDI input threads sending MIDI events to this EngineChannel
585 LockGuard g;
586 if (hasMultipleMIDIInputs()) g = LockGuard(MidiInputMutex);
587
588 Event event = pEngine->pEventGenerator->CreateEvent();
589 event.Type = Event::type_control_change;
590 event.Param.CC.Controller = Controller;
591 event.Param.CC.Value = Value;
592 event.Param.CC.Channel = MidiChannel;
593 event.pEngineChannel = this;
594 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
595 else dmsg(1,("AbstractEngineChannel: Input event queue full!"));
596 }
597 }
598
599 /**
600 * Will be called by the MIDIIn Thread to signal the audio thread that a
601 * continuous controller value has changed. This method is meant for
602 * offline rendering and / or for cases where the exact position of the
603 * event in the current audio fragment is already known.
604 *
605 * @param Controller - MIDI controller number of the occured control change
606 * @param Value - value of the control change
607 * @param FragmentPos - sample point position in the current audio
608 * fragment to which this event belongs to
609 */
610 void AbstractEngineChannel::SendControlChange(uint8_t Controller, uint8_t Value, uint8_t MidiChannel, int32_t FragmentPos) {
611 if (FragmentPos < 0) {
612 dmsg(1,("AbstractEngineChannel::SendControlChange(): negative FragmentPos! Seems MIDI driver is buggy!"));
613 }
614 else if (pEngine) {
615 // protection in case there are more than 1 MIDI input threads sending MIDI events to this EngineChannel
616 LockGuard g;
617 if (hasMultipleMIDIInputs()) g = LockGuard(MidiInputMutex);
618
619 Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos);
620 event.Type = Event::type_control_change;
621 event.Param.CC.Controller = Controller;
622 event.Param.CC.Value = Value;
623 event.Param.CC.Channel = MidiChannel;
624 event.pEngineChannel = this;
625 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
626 else dmsg(1,("AbstractEngineChannel: Input event queue full!"));
627 }
628 }
629
630 void AbstractEngineChannel::SendChannelPressure(uint8_t Value, uint8_t MidiChannel) {
631 if (pEngine) {
632 // protection in case there are more than 1 MIDI input threads sending MIDI events to this EngineChannel
633 LockGuard g;
634 if (hasMultipleMIDIInputs()) g = LockGuard(MidiInputMutex);
635
636 Event event = pEngine->pEventGenerator->CreateEvent();
637 event.Type = Event::type_channel_pressure;
638 event.Param.ChannelPressure.Controller = CTRL_TABLE_IDX_AFTERTOUCH; // required for instrument scripts
639 event.Param.ChannelPressure.Value = Value;
640 event.Param.ChannelPressure.Channel = MidiChannel;
641 event.pEngineChannel = this;
642 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
643 else dmsg(1,("AbstractEngineChannel: Input event queue full!"));
644 }
645 }
646
647 void AbstractEngineChannel::SendChannelPressure(uint8_t Value, uint8_t MidiChannel, int32_t FragmentPos) {
648 if (pEngine) {
649 // protection in case there are more than 1 MIDI input threads sending MIDI events to this EngineChannel
650 LockGuard g;
651 if (hasMultipleMIDIInputs()) g = LockGuard(MidiInputMutex);
652
653 Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos);
654 event.Type = Event::type_channel_pressure;
655 event.Param.ChannelPressure.Controller = CTRL_TABLE_IDX_AFTERTOUCH; // required for instrument scripts
656 event.Param.ChannelPressure.Value = Value;
657 event.Param.ChannelPressure.Channel = MidiChannel;
658 event.pEngineChannel = this;
659 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
660 else dmsg(1,("AbstractEngineChannel: Input event queue full!"));
661 }
662 }
663
664 void AbstractEngineChannel::SendPolyphonicKeyPressure(uint8_t Key, uint8_t Value, uint8_t MidiChannel) {
665 if (pEngine) {
666 // protection in case there are more than 1 MIDI input threads sending MIDI events to this EngineChannel
667 LockGuard g;
668 if (hasMultipleMIDIInputs()) g = LockGuard(MidiInputMutex);
669
670 Event event = pEngine->pEventGenerator->CreateEvent();
671 event.Type = Event::type_note_pressure;
672 event.Param.NotePressure.Key = Key;
673 event.Param.NotePressure.Value = Value;
674 event.Param.NotePressure.Channel = MidiChannel;
675 event.pEngineChannel = this;
676 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
677 else dmsg(1,("AbstractEngineChannel: Input event queue full!"));
678 }
679 }
680
681 void AbstractEngineChannel::SendPolyphonicKeyPressure(uint8_t Key, uint8_t Value, uint8_t MidiChannel, int32_t FragmentPos) {
682 if (pEngine) {
683 // protection in case there are more than 1 MIDI input threads sending MIDI events to this EngineChannel
684 LockGuard g;
685 if (hasMultipleMIDIInputs()) g = LockGuard(MidiInputMutex);
686
687 Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos);
688 event.Type = Event::type_note_pressure;
689 event.Param.NotePressure.Key = Key;
690 event.Param.NotePressure.Value = Value;
691 event.Param.NotePressure.Channel = MidiChannel;
692 event.pEngineChannel = this;
693 if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event);
694 else dmsg(1,("AbstractEngineChannel: Input event queue full!"));
695 }
696 }
697
698 bool AbstractEngineChannel::applyTranspose(Event* event) {
699 if (event->Type != Event::type_note_on && event->Type != Event::type_note_off)
700 return true; // event OK (not a note event, nothing to do with it here)
701
702 //HACK: we should better add the transpose value only to the most mandatory places (like for retrieving the region and calculating the tuning), because otherwise voices will unintendedly survive when changing transpose while playing
703 const int k = event->Param.Note.Key + GlobalTranspose;
704 if (k < 0 || k > 127)
705 return false; // bad event, drop it
706
707 event->Param.Note.Key = k;
708
709 return true; // event OK
710 }
711
712 /**
713 * Copy all events from the engine channel's input event queue buffer to
714 * the internal event list. This will be done at the beginning of each
715 * audio cycle (that is each RenderAudio() call) to distinguish all
716 * events which have to be processed in the current audio cycle. Each
717 * EngineChannel has it's own input event queue for the common channel
718 * specific events (like NoteOn, NoteOff and ControlChange events).
719 * Beside that, the engine also has a input event queue for global
720 * events (usually SysEx messages).
721 *
722 * @param Samples - number of sample points to be processed in the
723 * current audio cycle
724 */
725 void AbstractEngineChannel::ImportEvents(uint Samples) {
726 // import events from pure software MIDI "devices"
727 // (e.g. virtual keyboard in instrument editor)
728 {
729 const uint8_t channel = MidiChannel() == midi_chan_all ? 0 : MidiChannel();
730 const int FragmentPos = 0; // randomly chosen, we don't care about jitter for virtual MIDI devices
731 Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos);
732 VirtualMidiDevice::event_t devEvent; // the event format we get from the virtual MIDI device
733 // as we're going to (carefully) write some status to the
734 // synchronized struct, we cast away the const
735 ArrayList<VirtualMidiDevice*>& devices =
736 const_cast<ArrayList<VirtualMidiDevice*>&>(virtualMidiDevicesReader_AudioThread.Lock());
737 // iterate through all virtual MIDI devices
738 for (int i = 0; i < devices.size(); i++) {
739 VirtualMidiDevice* pDev = devices[i];
740 // I think we can simply flush the whole FIFO(s), the user shouldn't be so fast ;-)
741 while (pDev->GetMidiEventFromDevice(devEvent)) {
742 switch (devEvent.Type) {
743 case VirtualMidiDevice::EVENT_TYPE_NOTEON:
744 event.Type = Event::type_note_on;
745 event.Param.Note.Key = devEvent.Arg1;
746 event.Param.Note.Velocity = devEvent.Arg2;
747 event.Param.Note.Channel = channel;
748 // apply transpose setting to (note on/off) event
749 if (!applyTranspose(&event))
750 continue; // note value is out of range, so drop this event
751 // assign a new note to this note-on event
752 if (!pEngine->LaunchNewNote(this, &event))
753 continue; // failed launching new note, so drop this event
754 break;
755 case VirtualMidiDevice::EVENT_TYPE_NOTEOFF:
756 event.Type = Event::type_note_off;
757 event.Param.Note.Key = devEvent.Arg1;
758 event.Param.Note.Velocity = devEvent.Arg2;
759 event.Param.Note.Channel = channel;
760 if (!applyTranspose(&event))
761 continue; // note value is out of range, so drop this event
762 break;
763 case VirtualMidiDevice::EVENT_TYPE_CC:
764 switch (devEvent.Arg1) {
765 case 0: // bank select MSB ...
766 SetMidiBankMsb(devEvent.Arg2);
767 continue; // don't push this event into FIFO
768 case 32: // bank select LSB ...
769 SetMidiBankLsb(devEvent.Arg2);
770 continue; // don't push this event into FIFO
771 default: // regular MIDI CC ...
772 event.Type = Event::type_control_change;
773 event.Param.CC.Controller = devEvent.Arg1;
774 event.Param.CC.Value = devEvent.Arg2;
775 event.Param.CC.Channel = channel;
776 }
777 break;
778 case VirtualMidiDevice::EVENT_TYPE_PITCHBEND:
779 event.Type = Event::type_pitchbend;
780 event.Param.Pitch.Pitch = int(devEvent.Arg2 << 7 | devEvent.Arg1) - 8192;
781 event.Param.Pitch.Channel = channel;
782 break;
783 case VirtualMidiDevice::EVENT_TYPE_PROGRAM:
784 SendProgramChange(devEvent.Arg1);
785 continue; // don't push this event into FIFO
786 default:
787 std::cerr << "AbstractEngineChannel::ImportEvents() ERROR: unknown event type ("
788 << devEvent.Type << "). This is a bug!";
789 continue;
790 }
791 event.pEngineChannel = this;
792 // copy event to internal event list
793 if (pEvents->poolIsEmpty()) {
794 dmsg(1,("Event pool emtpy!\n"));
795 goto exitVirtualDevicesLoop;
796 }
797 *pEvents->allocAppend() = event;
798 }
799 }
800 }
801 exitVirtualDevicesLoop:
802 virtualMidiDevicesReader_AudioThread.Unlock();
803
804 // import events from the regular MIDI devices
805 RingBuffer<Event,false>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader();
806 Event* pEvent;
807 while (true) {
808 // get next event from input event queue
809 if (!(pEvent = eventQueueReader.pop())) break;
810 // if younger event reached, ignore that and all subsequent ones for now
811 if (pEvent->FragmentPos() >= Samples) {
812 eventQueueReader--;
813 dmsg(2,("Younger Event, pos=%d ,Samples=%d!\n",pEvent->FragmentPos(),Samples));
814 pEvent->ResetFragmentPos();
815 break;
816 }
817 if (pEvents->poolIsEmpty()) {
818 dmsg(1,("Event pool emtpy!\n"));
819 break;
820 }
821 // apply transpose setting to (note on/off) event
822 if (!applyTranspose(pEvent))
823 continue; // it's a note event which has a note value out of range, so drop this event
824 // assign a new note to this event (if its a note-on event)
825 if (pEvent->Type == Event::type_note_on)
826 if (!pEngine->LaunchNewNote(this, pEvent))
827 continue; // failed launching new note, so drop this event
828 // copy event to internal event list
829 *pEvents->allocAppend() = *pEvent;
830 }
831 eventQueueReader.free(); // free all copied events from input queue
832 }
833
834 /**
835 * Called by real-time instrument script functions to schedule a new event
836 * @a delay microseconds in future.
837 *
838 * @b IMPORTANT: for the supplied @a delay to be scheduled correctly, the
839 * passed @a pEvent must be assigned a valid fragment time within the
840 * current audio fragment boundaries. That fragment time will be used by
841 * this method as basis for interpreting what "now" acutally is, and thus
842 * it will be used as basis for calculating the precise scheduling time
843 * for @a delay. The easiest way to achieve this is by copying a recent
844 * event which happened within the current audio fragment cycle: i.e. the
845 * original event which caused calling this method here.
846 *
847 * @param pEvent - event to be scheduled in future (event data will be copied)
848 * @param delay - amount of microseconds in future (from now) when event shall be processed
849 * @returns unique event ID of scheduled new event, or NULL on error
850 */
851 event_id_t AbstractEngineChannel::ScheduleEventMicroSec(const Event* pEvent, int delay) {
852 dmsg(3,("AbstractEngineChannel::ScheduleEventMicroSec(Event.Type=%d,delay=%d)\n", pEvent->Type, delay));
853 RTList<Event>::Iterator itEvent = pEvents->allocAppend();
854 if (!itEvent) {
855 dmsg(1,("AbstractEngineChannel::ScheduleEventMicroSec(): Event pool emtpy!\n"));
856 return 0;
857 }
858 RTList<ScheduledEvent>::Iterator itNode = delayedEvents.schedulerNodes.allocAppend();
859 if (!itNode) { // scheduler node pool empty ...
860 dmsg(1,("AbstractEngineChannel::ScheduleEventMicroSec(): ScheduledEvent pool empty!\n"));
861 pEvents->free(itEvent);
862 return 0;
863 }
864 // copy passed event
865 *itEvent = *pEvent;
866 // move copied event to list of delayed events
867 itEvent = itEvent.moveToEndOf(delayedEvents.pList);
868 // connect scheduler node with the copied event
869 itNode->itEvent = itEvent;
870 // add entry to time sorted scheduler queue for copied event
871 pEngine->pEventGenerator->scheduleAheadMicroSec(
872 delayedEvents.queue, *itNode, itEvent->FragmentPos(), delay
873 );
874 //dmsg(5,("ScheduledEvent queue size: %d\n", delayedEvents.queue.size()));
875 return pEvents->getID(itEvent);
876 }
877
878 /**
879 * Called by real-time instrument script functions to ignore the event
880 * reflected by given event ID. The event will be freed immediately to its
881 * pool and cannot be dereferenced by its old ID anymore. Even if its
882 * allocated back from the Pool later on, it will have a different ID.
883 *
884 * @param id - unique ID of event to be dropped
885 */
886 void AbstractEngineChannel::IgnoreEvent(event_id_t id) {
887 RTList<Event>::Iterator it = pEvents->fromID(id);
888 if (it) pEvents->free(it);
889 }
890
891 /**
892 * Called by real-time instrument script functions to ignore the note
893 * reflected by given note ID. The note's event will be freed immediately
894 * to its event pool and this will prevent voices to be launched for the
895 * note.
896 *
897 * NOTE: preventing a note by calling this method works only if the note
898 * was launched within the current audio fragment cycle.
899 *
900 * @param id - unique ID of note to be dropped
901 */
902 void AbstractEngineChannel::IgnoreNote(note_id_t id) {
903 NoteBase* pNote = pEngine->NoteByID(id);
904 if (!pNote) return;
905 IgnoreEvent(pNote->eventID);
906 }
907
908 /** @brief Drop the requested event.
909 *
910 * Called by real-time instrument script functions to ignore the event
911 * reflected by the given event @a id. This method detects whether the
912 * passed ID is actually a @c Note ID or a regular @c Event ID and act
913 * accordingly.
914 *
915 * @param id - event id (from script scope)
916 * @see ScriptID
917 */
918 void AbstractEngineChannel::IgnoreEventByScriptID(const ScriptID& id) {
919 switch (id.type()) {
920 case ScriptID::EVENT:
921 IgnoreEvent( id.eventID() );
922 break;
923 case ScriptID::NOTE:
924 IgnoreNote( id.noteID() );
925 break;
926 }
927 }
928
929 FxSend* AbstractEngineChannel::AddFxSend(uint8_t MidiCtrl, String Name) throw (Exception) {
930 if (pEngine) pEngine->DisableAndLock();
931 FxSend* pFxSend = new FxSend(this, MidiCtrl, Name);
932 if (fxSends.empty()) {
933 if (pEngine && pEngine->pAudioOutputDevice) {
934 AudioOutputDevice* pDevice = pEngine->pAudioOutputDevice;
935 // create local render buffers
936 pChannelLeft = new AudioChannel(0, pDevice->MaxSamplesPerCycle());
937 pChannelRight = new AudioChannel(1, pDevice->MaxSamplesPerCycle());
938 } else {
939 // postpone local render buffer creation until audio device is assigned
940 pChannelLeft = NULL;
941 pChannelRight = NULL;
942 }
943 }
944 fxSends.push_back(pFxSend);
945 if (pEngine) pEngine->Enable();
946 fireFxSendCountChanged(GetSamplerChannel()->Index(), GetFxSendCount());
947
948 return pFxSend;
949 }
950
951 FxSend* AbstractEngineChannel::GetFxSend(uint FxSendIndex) {
952 return (FxSendIndex < fxSends.size()) ? fxSends[FxSendIndex] : NULL;
953 }
954
955 uint AbstractEngineChannel::GetFxSendCount() {
956 return fxSends.size();
957 }
958
959 void AbstractEngineChannel::RemoveFxSend(FxSend* pFxSend) {
960 if (pEngine) pEngine->DisableAndLock();
961 for (
962 std::vector<FxSend*>::iterator iter = fxSends.begin();
963 iter != fxSends.end(); iter++
964 ) {
965 if (*iter == pFxSend) {
966 delete pFxSend;
967 fxSends.erase(iter);
968 if (fxSends.empty()) {
969 // destroy local render buffers
970 if (pChannelLeft) delete pChannelLeft;
971 if (pChannelRight) delete pChannelRight;
972 // fallback to render directly into AudioOutputDevice's buffers
973 if (pEngine && pEngine->pAudioOutputDevice) {
974 pChannelLeft = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft);
975 pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight);
976 } else { // we update the pointers later
977 pChannelLeft = NULL;
978 pChannelRight = NULL;
979 }
980 }
981 break;
982 }
983 }
984 if (pEngine) pEngine->Enable();
985 fireFxSendCountChanged(GetSamplerChannel()->Index(), GetFxSendCount());
986 }
987
988 void AbstractEngineChannel::RemoveAllFxSends() {
989 if (pEngine) pEngine->DisableAndLock();
990 if (!fxSends.empty()) { // free local render buffers
991 if (pChannelLeft) {
992 delete pChannelLeft;
993 if (pEngine && pEngine->pAudioOutputDevice) {
994 // fallback to render directly to the AudioOutputDevice's buffer
995 pChannelLeft = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft);
996 } else pChannelLeft = NULL;
997 }
998 if (pChannelRight) {
999 delete pChannelRight;
1000 if (pEngine && pEngine->pAudioOutputDevice) {
1001 // fallback to render directly to the AudioOutputDevice's buffer
1002 pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight);
1003 } else pChannelRight = NULL;
1004 }
1005 }
1006 for (int i = 0; i < fxSends.size(); i++) delete fxSends[i];
1007 fxSends.clear();
1008 if (pEngine) pEngine->Enable();
1009 }
1010
1011 /**
1012 * Add a group number to the set of key groups. Should be called
1013 * when an instrument is loaded to make sure there are event lists
1014 * for all key groups.
1015 */
1016 void AbstractEngineChannel::AddGroup(uint group) {
1017 if (group) {
1018 std::pair<ActiveKeyGroupMap::iterator, bool> p =
1019 ActiveKeyGroups.insert(ActiveKeyGroupMap::value_type(group, 0));
1020 if (p.second) {
1021 // If the engine channel is pending deletion (see bug
1022 // #113), pEngine will be null, so we can't use
1023 // pEngine->pEventPool here. Instead we're using a
1024 // specialized RTList that allows specifying the pool
1025 // later.
1026 (*p.first).second = new LazyList<Event>;
1027 }
1028 }
1029 }
1030
1031 /**
1032 * Handle key group (a.k.a. exclusive group) conflicts.
1033 */
1034 void AbstractEngineChannel::HandleKeyGroupConflicts(uint KeyGroup, Pool<Event>::Iterator& itNoteOnEvent) {
1035 dmsg(4,("HandelKeyGroupConflicts KeyGroup=%d\n", KeyGroup));
1036 if (KeyGroup) {
1037 // send a release event to all active voices in the group
1038 RTList<Event>::Iterator itEvent = ActiveKeyGroups[KeyGroup]->allocAppend(pEngine->pEventPool);
1039 *itEvent = *itNoteOnEvent;
1040 }
1041 }
1042
1043 /**
1044 * Empty the lists of group events. Should be called from the
1045 * audio thread, after all voices have been rendered.
1046 */
1047 void AbstractEngineChannel::ClearGroupEventLists() {
1048 for (ActiveKeyGroupMap::iterator iter = ActiveKeyGroups.begin();
1049 iter != ActiveKeyGroups.end(); iter++) {
1050 if (iter->second) {
1051 iter->second->clear();
1052 } else {
1053 dmsg(1,("EngineChannel: group event list was NULL"));
1054 }
1055 }
1056 }
1057
1058 /**
1059 * Remove all lists with group events.
1060 */
1061 void AbstractEngineChannel::DeleteGroupEventLists() {
1062 for (ActiveKeyGroupMap::iterator iter = ActiveKeyGroups.begin();
1063 iter != ActiveKeyGroups.end(); iter++) {
1064 delete iter->second;
1065 }
1066 ActiveKeyGroups.clear();
1067 }
1068
1069 } // namespace LinuxSampler

  ViewVC Help
Powered by ViewVC