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-2010 Christian Schoenebeck and Grigor Iliev * |
8 |
* * |
9 |
* This program is free software; you can redistribute it and/or modify * |
10 |
* it under the terms of the GNU General Public License as published by * |
11 |
* the Free Software Foundation; either version 2 of the License, or * |
12 |
* (at your option) any later version. * |
13 |
* * |
14 |
* This program is distributed in the hope that it will be useful, * |
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
17 |
* GNU General Public License for more details. * |
18 |
* * |
19 |
* You should have received a copy of the GNU General Public License * |
20 |
* along with this program; if not, write to the Free Software * |
21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, * |
22 |
* MA 02111-1307 USA * |
23 |
***************************************************************************/ |
24 |
|
25 |
#include "AbstractEngineChannel.h" |
26 |
#include "../common/global_private.h" |
27 |
#include "../Sampler.h" |
28 |
|
29 |
namespace LinuxSampler { |
30 |
|
31 |
AbstractEngineChannel::AbstractEngineChannel() : |
32 |
virtualMidiDevicesReader_AudioThread(virtualMidiDevices), |
33 |
virtualMidiDevicesReader_MidiThread(virtualMidiDevices) |
34 |
{ |
35 |
pEngine = NULL; |
36 |
pEvents = NULL; // we allocate when we retrieve the right Engine object |
37 |
pEventQueue = new RingBuffer<Event,false>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0); |
38 |
InstrumentIdx = -1; |
39 |
InstrumentStat = -1; |
40 |
pChannelLeft = NULL; |
41 |
pChannelRight = NULL; |
42 |
AudioDeviceChannelLeft = -1; |
43 |
AudioDeviceChannelRight = -1; |
44 |
pMidiInputPort = NULL; |
45 |
midiChannel = midi_chan_all; |
46 |
ResetControllers(); |
47 |
PortamentoMode = false; |
48 |
PortamentoTime = CONFIG_PORTAMENTO_TIME_DEFAULT; |
49 |
} |
50 |
|
51 |
AbstractEngineChannel::~AbstractEngineChannel() { |
52 |
delete pEventQueue; |
53 |
DeleteGroupEventLists(); |
54 |
RemoveAllFxSends(); |
55 |
} |
56 |
|
57 |
Engine* AbstractEngineChannel::GetEngine() { |
58 |
return pEngine; |
59 |
} |
60 |
|
61 |
uint AbstractEngineChannel::Channels() { |
62 |
return 2; |
63 |
} |
64 |
|
65 |
/** |
66 |
* More or less a workaround to set the instrument name, index and load |
67 |
* status variable to zero percent immediately, that is without blocking |
68 |
* the calling thread. It might be used in future for other preparations |
69 |
* as well though. |
70 |
* |
71 |
* @param FileName - file name of the instrument file |
72 |
* @param Instrument - index of the instrument in the file |
73 |
* @see LoadInstrument() |
74 |
*/ |
75 |
void AbstractEngineChannel::PrepareLoadInstrument(const char* FileName, uint Instrument) { |
76 |
InstrumentFile = FileName; |
77 |
InstrumentIdx = Instrument; |
78 |
InstrumentStat = 0; |
79 |
} |
80 |
|
81 |
String AbstractEngineChannel::InstrumentFileName() { |
82 |
return InstrumentFile; |
83 |
} |
84 |
|
85 |
String AbstractEngineChannel::InstrumentName() { |
86 |
return InstrumentIdxName; |
87 |
} |
88 |
|
89 |
int AbstractEngineChannel::InstrumentIndex() { |
90 |
return InstrumentIdx; |
91 |
} |
92 |
|
93 |
int AbstractEngineChannel::InstrumentStatus() { |
94 |
return InstrumentStat; |
95 |
} |
96 |
|
97 |
String AbstractEngineChannel::EngineName() { |
98 |
return AbstractEngine::GetFormatString(GetEngineFormat()); |
99 |
} |
100 |
|
101 |
void AbstractEngineChannel::Reset() { |
102 |
if (pEngine) pEngine->DisableAndLock(); |
103 |
ResetInternal(); |
104 |
ResetControllers(); |
105 |
if (pEngine) { |
106 |
pEngine->Enable(); |
107 |
pEngine->Reset(); |
108 |
} |
109 |
} |
110 |
|
111 |
void AbstractEngineChannel::ResetControllers() { |
112 |
Pitch = 0; |
113 |
GlobalVolume = 1.0f; |
114 |
MidiVolume = 1.0; |
115 |
GlobalPanLeft = 1.0f; |
116 |
GlobalPanRight = 1.0f; |
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() { |
134 |
CurrentKeyDimension = 0; |
135 |
PortamentoPos = -1.0f; // no portamento active yet |
136 |
|
137 |
// delete all input events |
138 |
pEventQueue->init(); |
139 |
|
140 |
if (pEngine) pEngine->ResetInternal(); |
141 |
|
142 |
// status of engine channel has changed, so set notify flag |
143 |
bStatusChanged = true; |
144 |
} |
145 |
|
146 |
/** |
147 |
* Implementation of virtual method from abstract EngineChannel interface. |
148 |
* This method will periodically be polled (e.g. by the LSCP server) to |
149 |
* check if some engine channel parameter has changed since the last |
150 |
* StatusChanged() call. |
151 |
* |
152 |
* This method can also be used to mark the engine channel as changed |
153 |
* from outside, e.g. by a MIDI input device. The optional argument |
154 |
* \a nNewStatus can be used for this. |
155 |
* |
156 |
* TODO: This "poll method" is just a lazy solution and might be |
157 |
* replaced in future. |
158 |
* @param bNewStatus - (optional, default: false) sets the new status flag |
159 |
* @returns true if engine channel status has changed since last |
160 |
* StatusChanged() call |
161 |
*/ |
162 |
bool AbstractEngineChannel::StatusChanged(bool bNewStatus) { |
163 |
bool b = bStatusChanged; |
164 |
bStatusChanged = bNewStatus; |
165 |
return b; |
166 |
} |
167 |
|
168 |
float AbstractEngineChannel::Volume() { |
169 |
return GlobalVolume; |
170 |
} |
171 |
|
172 |
void AbstractEngineChannel::Volume(float f) { |
173 |
GlobalVolume = f; |
174 |
bStatusChanged = true; // status of engine channel has changed, so set notify flag |
175 |
} |
176 |
|
177 |
float AbstractEngineChannel::Pan() { |
178 |
return float(iLastPanRequest - 64) / 64.0f; |
179 |
} |
180 |
|
181 |
void AbstractEngineChannel::Pan(float f) { |
182 |
int iMidiPan = int(f * 64.0f) + 64; |
183 |
if (iMidiPan > 127) iMidiPan = 127; |
184 |
else if (iMidiPan < 0) iMidiPan = 0; |
185 |
GlobalPanLeft = AbstractEngine::PanCurve[128 - iMidiPan]; |
186 |
GlobalPanRight = AbstractEngine::PanCurve[iMidiPan]; |
187 |
iLastPanRequest = iMidiPan; |
188 |
} |
189 |
|
190 |
AudioOutputDevice* AbstractEngineChannel::GetAudioOutputDevice() { |
191 |
return (pEngine) ? pEngine->pAudioOutputDevice : NULL; |
192 |
} |
193 |
|
194 |
void AbstractEngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) { |
195 |
if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet."); |
196 |
|
197 |
AudioChannel* pChannel = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannel); |
198 |
if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel)); |
199 |
switch (EngineAudioChannel) { |
200 |
case 0: // left output channel |
201 |
if (fxSends.empty()) pChannelLeft = pChannel; |
202 |
AudioDeviceChannelLeft = AudioDeviceChannel; |
203 |
break; |
204 |
case 1: // right output channel |
205 |
if (fxSends.empty()) pChannelRight = pChannel; |
206 |
AudioDeviceChannelRight = AudioDeviceChannel; |
207 |
break; |
208 |
default: |
209 |
throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel)); |
210 |
} |
211 |
|
212 |
bStatusChanged = true; |
213 |
} |
214 |
|
215 |
int AbstractEngineChannel::OutputChannel(uint EngineAudioChannel) { |
216 |
switch (EngineAudioChannel) { |
217 |
case 0: // left channel |
218 |
return AudioDeviceChannelLeft; |
219 |
case 1: // right channel |
220 |
return AudioDeviceChannelRight; |
221 |
default: |
222 |
throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel)); |
223 |
} |
224 |
} |
225 |
|
226 |
void AbstractEngineChannel::Connect(MidiInputPort* pMidiPort, midi_chan_t MidiChannel) { |
227 |
if (!pMidiPort || pMidiPort == this->pMidiInputPort) return; |
228 |
DisconnectMidiInputPort(); |
229 |
this->pMidiInputPort = pMidiPort; |
230 |
this->midiChannel = MidiChannel; |
231 |
pMidiPort->Connect(this, MidiChannel); |
232 |
} |
233 |
|
234 |
void AbstractEngineChannel::DisconnectMidiInputPort() { |
235 |
MidiInputPort* pOldPort = this->pMidiInputPort; |
236 |
this->pMidiInputPort = NULL; |
237 |
if (pOldPort) pOldPort->Disconnect(this); |
238 |
} |
239 |
|
240 |
MidiInputPort* AbstractEngineChannel::GetMidiInputPort() { |
241 |
return pMidiInputPort; |
242 |
} |
243 |
|
244 |
midi_chan_t AbstractEngineChannel::MidiChannel() { |
245 |
return midiChannel; |
246 |
} |
247 |
|
248 |
void AbstractEngineChannel::Connect(VirtualMidiDevice* pDevice) { |
249 |
// double buffer ... double work ... |
250 |
{ |
251 |
ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.GetConfigForUpdate(); |
252 |
devices.add(pDevice); |
253 |
} |
254 |
{ |
255 |
ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.SwitchConfig(); |
256 |
devices.add(pDevice); |
257 |
} |
258 |
} |
259 |
|
260 |
void AbstractEngineChannel::Disconnect(VirtualMidiDevice* pDevice) { |
261 |
// double buffer ... double work ... |
262 |
{ |
263 |
ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.GetConfigForUpdate(); |
264 |
devices.remove(pDevice); |
265 |
} |
266 |
{ |
267 |
ArrayList<VirtualMidiDevice*>& devices = virtualMidiDevices.SwitchConfig(); |
268 |
devices.remove(pDevice); |
269 |
} |
270 |
} |
271 |
|
272 |
/** |
273 |
* Will be called by the MIDIIn Thread to let the audio thread trigger a new |
274 |
* voice for the given key. This method is meant for real time rendering, |
275 |
* that is an event will immediately be created with the current system |
276 |
* time as time stamp. |
277 |
* |
278 |
* @param Key - MIDI key number of the triggered key |
279 |
* @param Velocity - MIDI velocity value of the triggered key |
280 |
*/ |
281 |
void AbstractEngineChannel::SendNoteOn(uint8_t Key, uint8_t Velocity) { |
282 |
if (pEngine) { |
283 |
Event event = pEngine->pEventGenerator->CreateEvent(); |
284 |
event.Type = Event::type_note_on; |
285 |
event.Param.Note.Key = Key; |
286 |
event.Param.Note.Velocity = Velocity; |
287 |
event.pEngineChannel = this; |
288 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
289 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
290 |
// inform connected virtual MIDI devices if any ... |
291 |
// (e.g. virtual MIDI keyboard in instrument editor(s)) |
292 |
ArrayList<VirtualMidiDevice*>& devices = |
293 |
const_cast<ArrayList<VirtualMidiDevice*>&>( |
294 |
virtualMidiDevicesReader_MidiThread.Lock() |
295 |
); |
296 |
for (int i = 0; i < devices.size(); i++) { |
297 |
devices[i]->SendNoteOnToDevice(Key, Velocity); |
298 |
} |
299 |
virtualMidiDevicesReader_MidiThread.Unlock(); |
300 |
} |
301 |
} |
302 |
|
303 |
/** |
304 |
* Will be called by the MIDIIn Thread to let the audio thread trigger a new |
305 |
* voice for the given key. This method is meant for offline rendering |
306 |
* and / or for cases where the exact position of the event in the current |
307 |
* audio fragment is already known. |
308 |
* |
309 |
* @param Key - MIDI key number of the triggered key |
310 |
* @param Velocity - MIDI velocity value of the triggered key |
311 |
* @param FragmentPos - sample point position in the current audio |
312 |
* fragment to which this event belongs to |
313 |
*/ |
314 |
void AbstractEngineChannel::SendNoteOn(uint8_t Key, uint8_t Velocity, int32_t FragmentPos) { |
315 |
if (FragmentPos < 0) { |
316 |
dmsg(1,("EngineChannel::SendNoteOn(): negative FragmentPos! Seems MIDI driver is buggy!")); |
317 |
} |
318 |
else if (pEngine) { |
319 |
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
320 |
event.Type = Event::type_note_on; |
321 |
event.Param.Note.Key = Key; |
322 |
event.Param.Note.Velocity = Velocity; |
323 |
event.pEngineChannel = this; |
324 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
325 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
326 |
// inform connected virtual MIDI devices if any ... |
327 |
// (e.g. virtual MIDI keyboard in instrument editor(s)) |
328 |
ArrayList<VirtualMidiDevice*>& devices = |
329 |
const_cast<ArrayList<VirtualMidiDevice*>&>( |
330 |
virtualMidiDevicesReader_MidiThread.Lock() |
331 |
); |
332 |
for (int i = 0; i < devices.size(); i++) { |
333 |
devices[i]->SendNoteOnToDevice(Key, Velocity); |
334 |
} |
335 |
virtualMidiDevicesReader_MidiThread.Unlock(); |
336 |
} |
337 |
} |
338 |
|
339 |
/** |
340 |
* Will be called by the MIDIIn Thread to signal the audio thread to release |
341 |
* voice(s) on the given key. This method is meant for real time rendering, |
342 |
* that is an event will immediately be created with the current system |
343 |
* time as time stamp. |
344 |
* |
345 |
* @param Key - MIDI key number of the released key |
346 |
* @param Velocity - MIDI release velocity value of the released key |
347 |
*/ |
348 |
void AbstractEngineChannel::SendNoteOff(uint8_t Key, uint8_t Velocity) { |
349 |
if (pEngine) { |
350 |
Event event = pEngine->pEventGenerator->CreateEvent(); |
351 |
event.Type = Event::type_note_off; |
352 |
event.Param.Note.Key = Key; |
353 |
event.Param.Note.Velocity = Velocity; |
354 |
event.pEngineChannel = this; |
355 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
356 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
357 |
// inform connected virtual MIDI devices if any ... |
358 |
// (e.g. virtual MIDI keyboard in instrument editor(s)) |
359 |
ArrayList<VirtualMidiDevice*>& devices = |
360 |
const_cast<ArrayList<VirtualMidiDevice*>&>( |
361 |
virtualMidiDevicesReader_MidiThread.Lock() |
362 |
); |
363 |
for (int i = 0; i < devices.size(); i++) { |
364 |
devices[i]->SendNoteOffToDevice(Key, Velocity); |
365 |
} |
366 |
virtualMidiDevicesReader_MidiThread.Unlock(); |
367 |
} |
368 |
} |
369 |
|
370 |
/** |
371 |
* Will be called by the MIDIIn Thread to signal the audio thread to release |
372 |
* voice(s) on the given key. This method is meant for offline rendering |
373 |
* and / or for cases where the exact position of the event in the current |
374 |
* audio fragment is already known. |
375 |
* |
376 |
* @param Key - MIDI key number of the released key |
377 |
* @param Velocity - MIDI release velocity value of the released key |
378 |
* @param FragmentPos - sample point position in the current audio |
379 |
* fragment to which this event belongs to |
380 |
*/ |
381 |
void AbstractEngineChannel::SendNoteOff(uint8_t Key, uint8_t Velocity, int32_t FragmentPos) { |
382 |
if (FragmentPos < 0) { |
383 |
dmsg(1,("EngineChannel::SendNoteOff(): negative FragmentPos! Seems MIDI driver is buggy!")); |
384 |
} |
385 |
else if (pEngine) { |
386 |
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
387 |
event.Type = Event::type_note_off; |
388 |
event.Param.Note.Key = Key; |
389 |
event.Param.Note.Velocity = Velocity; |
390 |
event.pEngineChannel = this; |
391 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
392 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
393 |
// inform connected virtual MIDI devices if any ... |
394 |
// (e.g. virtual MIDI keyboard in instrument editor(s)) |
395 |
ArrayList<VirtualMidiDevice*>& devices = |
396 |
const_cast<ArrayList<VirtualMidiDevice*>&>( |
397 |
virtualMidiDevicesReader_MidiThread.Lock() |
398 |
); |
399 |
for (int i = 0; i < devices.size(); i++) { |
400 |
devices[i]->SendNoteOffToDevice(Key, Velocity); |
401 |
} |
402 |
virtualMidiDevicesReader_MidiThread.Unlock(); |
403 |
} |
404 |
} |
405 |
|
406 |
/** |
407 |
* Will be called by the MIDIIn Thread to signal the audio thread to change |
408 |
* the pitch value for all voices. This method is meant for real time |
409 |
* rendering, that is an event will immediately be created with the |
410 |
* current system time as time stamp. |
411 |
* |
412 |
* @param Pitch - MIDI pitch value (-8192 ... +8191) |
413 |
*/ |
414 |
void AbstractEngineChannel::SendPitchbend(int Pitch) { |
415 |
if (pEngine) { |
416 |
Event event = pEngine->pEventGenerator->CreateEvent(); |
417 |
event.Type = Event::type_pitchbend; |
418 |
event.Param.Pitch.Pitch = Pitch; |
419 |
event.pEngineChannel = this; |
420 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
421 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
422 |
} |
423 |
} |
424 |
|
425 |
/** |
426 |
* Will be called by the MIDIIn Thread to signal the audio thread to change |
427 |
* the pitch value for all voices. This method is meant for offline |
428 |
* rendering and / or for cases where the exact position of the event in |
429 |
* the current audio fragment is already known. |
430 |
* |
431 |
* @param Pitch - MIDI pitch value (-8192 ... +8191) |
432 |
* @param FragmentPos - sample point position in the current audio |
433 |
* fragment to which this event belongs to |
434 |
*/ |
435 |
void AbstractEngineChannel::SendPitchbend(int Pitch, int32_t FragmentPos) { |
436 |
if (FragmentPos < 0) { |
437 |
dmsg(1,("AbstractEngineChannel::SendPitchBend(): negative FragmentPos! Seems MIDI driver is buggy!")); |
438 |
} |
439 |
else if (pEngine) { |
440 |
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
441 |
event.Type = Event::type_pitchbend; |
442 |
event.Param.Pitch.Pitch = Pitch; |
443 |
event.pEngineChannel = this; |
444 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
445 |
else dmsg(1,("AbstractEngineChannel: Input event queue full!")); |
446 |
} |
447 |
} |
448 |
|
449 |
/** |
450 |
* Will be called by the MIDIIn Thread to signal the audio thread that a |
451 |
* continuous controller value has changed. This method is meant for real |
452 |
* time rendering, that is an event will immediately be created with the |
453 |
* current system time as time stamp. |
454 |
* |
455 |
* @param Controller - MIDI controller number of the occured control change |
456 |
* @param Value - value of the control change |
457 |
*/ |
458 |
void AbstractEngineChannel::SendControlChange(uint8_t Controller, uint8_t Value) { |
459 |
if (pEngine) { |
460 |
Event event = pEngine->pEventGenerator->CreateEvent(); |
461 |
event.Type = Event::type_control_change; |
462 |
event.Param.CC.Controller = Controller; |
463 |
event.Param.CC.Value = Value; |
464 |
event.pEngineChannel = this; |
465 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
466 |
else dmsg(1,("AbstractEngineChannel: Input event queue full!")); |
467 |
} |
468 |
} |
469 |
|
470 |
/** |
471 |
* Will be called by the MIDIIn Thread to signal the audio thread that a |
472 |
* continuous controller value has changed. This method is meant for |
473 |
* offline rendering and / or for cases where the exact position of the |
474 |
* event in the current audio fragment is already known. |
475 |
* |
476 |
* @param Controller - MIDI controller number of the occured control change |
477 |
* @param Value - value of the control change |
478 |
* @param FragmentPos - sample point position in the current audio |
479 |
* fragment to which this event belongs to |
480 |
*/ |
481 |
void AbstractEngineChannel::SendControlChange(uint8_t Controller, uint8_t Value, int32_t FragmentPos) { |
482 |
if (FragmentPos < 0) { |
483 |
dmsg(1,("AbstractEngineChannel::SendControlChange(): negative FragmentPos! Seems MIDI driver is buggy!")); |
484 |
} |
485 |
else if (pEngine) { |
486 |
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
487 |
event.Type = Event::type_control_change; |
488 |
event.Param.CC.Controller = Controller; |
489 |
event.Param.CC.Value = Value; |
490 |
event.pEngineChannel = this; |
491 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
492 |
else dmsg(1,("AbstractEngineChannel: Input event queue full!")); |
493 |
} |
494 |
} |
495 |
|
496 |
/** |
497 |
* Copy all events from the engine channel's input event queue buffer to |
498 |
* the internal event list. This will be done at the beginning of each |
499 |
* audio cycle (that is each RenderAudio() call) to distinguish all |
500 |
* events which have to be processed in the current audio cycle. Each |
501 |
* EngineChannel has it's own input event queue for the common channel |
502 |
* specific events (like NoteOn, NoteOff and ControlChange events). |
503 |
* Beside that, the engine also has a input event queue for global |
504 |
* events (usually SysEx messages). |
505 |
* |
506 |
* @param Samples - number of sample points to be processed in the |
507 |
* current audio cycle |
508 |
*/ |
509 |
void AbstractEngineChannel::ImportEvents(uint Samples) { |
510 |
// import events from pure software MIDI "devices" |
511 |
// (e.g. virtual keyboard in instrument editor) |
512 |
{ |
513 |
const int FragmentPos = 0; // randomly chosen, we don't care about jitter for virtual MIDI devices |
514 |
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
515 |
VirtualMidiDevice::event_t devEvent; // the event format we get from the virtual MIDI device |
516 |
// as we're going to (carefully) write some status to the |
517 |
// synchronized struct, we cast away the const |
518 |
ArrayList<VirtualMidiDevice*>& devices = |
519 |
const_cast<ArrayList<VirtualMidiDevice*>&>(virtualMidiDevicesReader_AudioThread.Lock()); |
520 |
// iterate through all virtual MIDI devices |
521 |
for (int i = 0; i < devices.size(); i++) { |
522 |
VirtualMidiDevice* pDev = devices[i]; |
523 |
// I think we can simply flush the whole FIFO(s), the user shouldn't be so fast ;-) |
524 |
while (pDev->GetMidiEventFromDevice(devEvent)) { |
525 |
switch (devEvent.Type) { |
526 |
case VirtualMidiDevice::EVENT_TYPE_NOTEON: |
527 |
event.Type = Event::type_note_on; |
528 |
event.Param.Note.Key = devEvent.Arg1; |
529 |
event.Param.Note.Velocity = devEvent.Arg2; |
530 |
break; |
531 |
case VirtualMidiDevice::EVENT_TYPE_NOTEOFF: |
532 |
event.Type = Event::type_note_off; |
533 |
event.Param.Note.Key = devEvent.Arg1; |
534 |
event.Param.Note.Velocity = devEvent.Arg2; |
535 |
break; |
536 |
case VirtualMidiDevice::EVENT_TYPE_CC: |
537 |
event.Type = Event::type_control_change; |
538 |
event.Param.CC.Controller = devEvent.Arg1; |
539 |
event.Param.CC.Value = devEvent.Arg2; |
540 |
break; |
541 |
default: |
542 |
std::cerr << "AbstractEngineChannel::ImportEvents() ERROR: unknown event type (" |
543 |
<< devEvent.Type << "). This is a bug!"; |
544 |
continue; |
545 |
} |
546 |
event.pEngineChannel = this; |
547 |
// copy event to internal event list |
548 |
if (pEvents->poolIsEmpty()) { |
549 |
dmsg(1,("Event pool emtpy!\n")); |
550 |
goto exitVirtualDevicesLoop; |
551 |
} |
552 |
*pEvents->allocAppend() = event; |
553 |
} |
554 |
} |
555 |
} |
556 |
exitVirtualDevicesLoop: |
557 |
virtualMidiDevicesReader_AudioThread.Unlock(); |
558 |
|
559 |
// import events from the regular MIDI devices |
560 |
RingBuffer<Event,false>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
561 |
Event* pEvent; |
562 |
while (true) { |
563 |
// get next event from input event queue |
564 |
if (!(pEvent = eventQueueReader.pop())) break; |
565 |
// if younger event reached, ignore that and all subsequent ones for now |
566 |
if (pEvent->FragmentPos() >= Samples) { |
567 |
eventQueueReader--; |
568 |
dmsg(2,("Younger Event, pos=%d ,Samples=%d!\n",pEvent->FragmentPos(),Samples)); |
569 |
pEvent->ResetFragmentPos(); |
570 |
break; |
571 |
} |
572 |
// copy event to internal event list |
573 |
if (pEvents->poolIsEmpty()) { |
574 |
dmsg(1,("Event pool emtpy!\n")); |
575 |
break; |
576 |
} |
577 |
*pEvents->allocAppend() = *pEvent; |
578 |
} |
579 |
eventQueueReader.free(); // free all copied events from input queue |
580 |
} |
581 |
|
582 |
FxSend* AbstractEngineChannel::AddFxSend(uint8_t MidiCtrl, String Name) throw (Exception) { |
583 |
if (pEngine) pEngine->DisableAndLock(); |
584 |
FxSend* pFxSend = new FxSend(this, MidiCtrl, Name); |
585 |
if (fxSends.empty()) { |
586 |
if (pEngine && pEngine->pAudioOutputDevice) { |
587 |
AudioOutputDevice* pDevice = pEngine->pAudioOutputDevice; |
588 |
// create local render buffers |
589 |
pChannelLeft = new AudioChannel(0, pDevice->MaxSamplesPerCycle()); |
590 |
pChannelRight = new AudioChannel(1, pDevice->MaxSamplesPerCycle()); |
591 |
} else { |
592 |
// postpone local render buffer creation until audio device is assigned |
593 |
pChannelLeft = NULL; |
594 |
pChannelRight = NULL; |
595 |
} |
596 |
} |
597 |
fxSends.push_back(pFxSend); |
598 |
if (pEngine) pEngine->Enable(); |
599 |
fireFxSendCountChanged(GetSamplerChannel()->Index(), GetFxSendCount()); |
600 |
|
601 |
return pFxSend; |
602 |
} |
603 |
|
604 |
FxSend* AbstractEngineChannel::GetFxSend(uint FxSendIndex) { |
605 |
return (FxSendIndex < fxSends.size()) ? fxSends[FxSendIndex] : NULL; |
606 |
} |
607 |
|
608 |
uint AbstractEngineChannel::GetFxSendCount() { |
609 |
return fxSends.size(); |
610 |
} |
611 |
|
612 |
void AbstractEngineChannel::RemoveFxSend(FxSend* pFxSend) { |
613 |
if (pEngine) pEngine->DisableAndLock(); |
614 |
for ( |
615 |
std::vector<FxSend*>::iterator iter = fxSends.begin(); |
616 |
iter != fxSends.end(); iter++ |
617 |
) { |
618 |
if (*iter == pFxSend) { |
619 |
delete pFxSend; |
620 |
fxSends.erase(iter); |
621 |
if (fxSends.empty()) { |
622 |
// destroy local render buffers |
623 |
if (pChannelLeft) delete pChannelLeft; |
624 |
if (pChannelRight) delete pChannelRight; |
625 |
// fallback to render directly into AudioOutputDevice's buffers |
626 |
if (pEngine && pEngine->pAudioOutputDevice) { |
627 |
pChannelLeft = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft); |
628 |
pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight); |
629 |
} else { // we update the pointers later |
630 |
pChannelLeft = NULL; |
631 |
pChannelRight = NULL; |
632 |
} |
633 |
} |
634 |
break; |
635 |
} |
636 |
} |
637 |
if (pEngine) pEngine->Enable(); |
638 |
fireFxSendCountChanged(GetSamplerChannel()->Index(), GetFxSendCount()); |
639 |
} |
640 |
|
641 |
void AbstractEngineChannel::RemoveAllFxSends() { |
642 |
if (pEngine) pEngine->DisableAndLock(); |
643 |
if (!fxSends.empty()) { // free local render buffers |
644 |
if (pChannelLeft) { |
645 |
delete pChannelLeft; |
646 |
if (pEngine && pEngine->pAudioOutputDevice) { |
647 |
// fallback to render directly to the AudioOutputDevice's buffer |
648 |
pChannelLeft = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft); |
649 |
} else pChannelLeft = NULL; |
650 |
} |
651 |
if (pChannelRight) { |
652 |
delete pChannelRight; |
653 |
if (pEngine && pEngine->pAudioOutputDevice) { |
654 |
// fallback to render directly to the AudioOutputDevice's buffer |
655 |
pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight); |
656 |
} else pChannelRight = NULL; |
657 |
} |
658 |
} |
659 |
for (int i = 0; i < fxSends.size(); i++) delete fxSends[i]; |
660 |
fxSends.clear(); |
661 |
if (pEngine) pEngine->Enable(); |
662 |
} |
663 |
|
664 |
/** |
665 |
* Add a group number to the set of key groups. Should be called |
666 |
* when an instrument is loaded to make sure there are event lists |
667 |
* for all key groups. |
668 |
*/ |
669 |
void AbstractEngineChannel::AddGroup(uint group) { |
670 |
if (group) { |
671 |
std::pair<ActiveKeyGroupMap::iterator, bool> p = |
672 |
ActiveKeyGroups.insert(ActiveKeyGroupMap::value_type(group, 0)); |
673 |
if (p.second) { |
674 |
// If the engine channel is pending deletion (see bug |
675 |
// #113), pEngine will be null, so we can't use |
676 |
// pEngine->pEventPool here. Instead we're using a |
677 |
// specialized RTList that allows specifying the pool |
678 |
// later. |
679 |
(*p.first).second = new LazyList<Event>; |
680 |
} |
681 |
} |
682 |
} |
683 |
|
684 |
/** |
685 |
* Handle key group (a.k.a. exclusive group) conflicts. |
686 |
*/ |
687 |
void AbstractEngineChannel::HandleKeyGroupConflicts(uint KeyGroup, Pool<Event>::Iterator& itNoteOnEvent) { |
688 |
dmsg(4,("HandelKeyGroupConflicts KeyGroup=%d\n", KeyGroup)); |
689 |
if (KeyGroup) { |
690 |
// send a release event to all active voices in the group |
691 |
RTList<Event>::Iterator itEvent = ActiveKeyGroups[KeyGroup]->allocAppend(pEngine->pEventPool); |
692 |
*itEvent = *itNoteOnEvent; |
693 |
} |
694 |
} |
695 |
|
696 |
/** |
697 |
* Empty the lists of group events. Should be called from the |
698 |
* audio thread, after all voices have been rendered. |
699 |
*/ |
700 |
void AbstractEngineChannel::ClearGroupEventLists() { |
701 |
for (ActiveKeyGroupMap::iterator iter = ActiveKeyGroups.begin(); |
702 |
iter != ActiveKeyGroups.end(); iter++) { |
703 |
if (iter->second) { |
704 |
iter->second->clear(); |
705 |
} else { |
706 |
dmsg(1,("EngineChannel: group event list was NULL")); |
707 |
} |
708 |
} |
709 |
} |
710 |
|
711 |
/** |
712 |
* Remove all lists with group events. |
713 |
*/ |
714 |
void AbstractEngineChannel::DeleteGroupEventLists() { |
715 |
for (ActiveKeyGroupMap::iterator iter = ActiveKeyGroups.begin(); |
716 |
iter != ActiveKeyGroups.end(); iter++) { |
717 |
delete iter->second; |
718 |
} |
719 |
ActiveKeyGroups.clear(); |
720 |
} |
721 |
|
722 |
} // namespace LinuxSampler |