1 |
/*************************************************************************** |
2 |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005, 2006 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>(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 |
AudioDeviceChannelLeft = -1; |
47 |
AudioDeviceChannelRight = -1; |
48 |
pMidiInputPort = NULL; |
49 |
midiChannel = midi_chan_all; |
50 |
ResetControllers(); |
51 |
SoloMode = false; |
52 |
PortamentoMode = false; |
53 |
PortamentoTime = CONFIG_PORTAMENTO_TIME_DEFAULT; |
54 |
} |
55 |
|
56 |
EngineChannel::~EngineChannel() { |
57 |
DisconnectAudioOutputDevice(); |
58 |
if (pInstrument) Engine::instruments.HandBack(pInstrument, this); |
59 |
if (pEventQueue) delete pEventQueue; |
60 |
if (pActiveKeys) delete pActiveKeys; |
61 |
if (pMIDIKeyInfo) delete[] pMIDIKeyInfo; |
62 |
} |
63 |
|
64 |
/** |
65 |
* Implementation of virtual method from abstract EngineChannel interface. |
66 |
* This method will periodically be polled (e.g. by the LSCP server) to |
67 |
* check if some engine channel parameter has changed since the last |
68 |
* StatusChanged() call. |
69 |
* |
70 |
* This method can also be used to mark the engine channel as changed |
71 |
* from outside, e.g. by a MIDI input device. The optional argument |
72 |
* \a nNewStatus can be used for this. |
73 |
* |
74 |
* TODO: This "poll method" is just a lazy solution and might be |
75 |
* replaced in future. |
76 |
* @param bNewStatus - (optional, default: false) sets the new status flag |
77 |
* @returns true if engine channel status has changed since last |
78 |
* StatusChanged() call |
79 |
*/ |
80 |
bool EngineChannel::StatusChanged(bool bNewStatus) { |
81 |
bool b = bStatusChanged; |
82 |
bStatusChanged = bNewStatus; |
83 |
return b; |
84 |
} |
85 |
|
86 |
void EngineChannel::Reset() { |
87 |
if (pEngine) pEngine->DisableAndLock(); |
88 |
ResetInternal(); |
89 |
ResetControllers(); |
90 |
if (pEngine) { |
91 |
pEngine->Enable(); |
92 |
pEngine->Reset(); |
93 |
} |
94 |
} |
95 |
|
96 |
/** |
97 |
* This method is not thread safe! |
98 |
*/ |
99 |
void EngineChannel::ResetInternal() { |
100 |
CurrentKeyDimension = 0; |
101 |
|
102 |
// reset key info |
103 |
for (uint i = 0; i < 128; i++) { |
104 |
if (pMIDIKeyInfo[i].pActiveVoices) |
105 |
pMIDIKeyInfo[i].pActiveVoices->clear(); |
106 |
if (pMIDIKeyInfo[i].pEvents) |
107 |
pMIDIKeyInfo[i].pEvents->clear(); |
108 |
pMIDIKeyInfo[i].KeyPressed = false; |
109 |
pMIDIKeyInfo[i].Active = false; |
110 |
pMIDIKeyInfo[i].ReleaseTrigger = false; |
111 |
pMIDIKeyInfo[i].itSelf = Pool<uint>::Iterator(); |
112 |
pMIDIKeyInfo[i].VoiceTheftsQueued = 0; |
113 |
} |
114 |
SoloKey = -1; // no solo key active yet |
115 |
PortamentoPos = -1.0f; // no portamento active yet |
116 |
|
117 |
// reset all key groups |
118 |
std::map<uint,uint*>::iterator iter = ActiveKeyGroups.begin(); |
119 |
for (; iter != ActiveKeyGroups.end(); iter++) iter->second = NULL; |
120 |
|
121 |
// free all active keys |
122 |
pActiveKeys->clear(); |
123 |
|
124 |
// delete all input events |
125 |
pEventQueue->init(); |
126 |
|
127 |
if (pEngine) pEngine->ResetInternal(); |
128 |
|
129 |
// status of engine channel has changed, so set notify flag |
130 |
bStatusChanged = true; |
131 |
} |
132 |
|
133 |
LinuxSampler::Engine* EngineChannel::GetEngine() { |
134 |
return pEngine; |
135 |
} |
136 |
|
137 |
/** |
138 |
* More or less a workaround to set the instrument name, index and load |
139 |
* status variable to zero percent immediately, that is without blocking |
140 |
* the calling thread. It might be used in future for other preparations |
141 |
* as well though. |
142 |
* |
143 |
* @param FileName - file name of the Gigasampler instrument file |
144 |
* @param Instrument - index of the instrument in the .gig file |
145 |
* @see LoadInstrument() |
146 |
*/ |
147 |
void EngineChannel::PrepareLoadInstrument(const char* FileName, uint Instrument) { |
148 |
InstrumentFile = FileName; |
149 |
InstrumentIdx = Instrument; |
150 |
InstrumentStat = 0; |
151 |
} |
152 |
|
153 |
/** |
154 |
* Load an instrument from a .gig file. PrepareLoadInstrument() has to |
155 |
* be called first to provide the information which instrument to load. |
156 |
* This method will then actually start to load the instrument and block |
157 |
* the calling thread until loading was completed. |
158 |
* |
159 |
* @returns detailed description of the method call result |
160 |
* @see PrepareLoadInstrument() |
161 |
*/ |
162 |
void EngineChannel::LoadInstrument() { |
163 |
|
164 |
if (pEngine) pEngine->DisableAndLock(); |
165 |
|
166 |
ResetInternal(); |
167 |
|
168 |
// free old instrument |
169 |
if (pInstrument) { |
170 |
// give old instrument back to instrument manager |
171 |
Engine::instruments.HandBack(pInstrument, this); |
172 |
} |
173 |
|
174 |
// delete all key groups |
175 |
ActiveKeyGroups.clear(); |
176 |
|
177 |
// request gig instrument from instrument manager |
178 |
try { |
179 |
InstrumentManager::instrument_id_t instrid; |
180 |
instrid.FileName = InstrumentFile; |
181 |
instrid.Index = InstrumentIdx; |
182 |
pInstrument = Engine::instruments.Borrow(instrid, this); |
183 |
if (!pInstrument) { |
184 |
InstrumentStat = -1; |
185 |
dmsg(1,("no instrument loaded!!!\n")); |
186 |
exit(EXIT_FAILURE); |
187 |
} |
188 |
} |
189 |
catch (RIFF::Exception e) { |
190 |
InstrumentStat = -2; |
191 |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message; |
192 |
throw Exception(msg); |
193 |
} |
194 |
catch (InstrumentResourceManagerException e) { |
195 |
InstrumentStat = -3; |
196 |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message(); |
197 |
throw Exception(msg); |
198 |
} |
199 |
catch (...) { |
200 |
InstrumentStat = -4; |
201 |
throw Exception("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file."); |
202 |
} |
203 |
|
204 |
// rebuild ActiveKeyGroups map with key groups of current instrument |
205 |
for (::gig::Region* pRegion = pInstrument->GetFirstRegion(); pRegion; pRegion = pInstrument->GetNextRegion()) |
206 |
if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL; |
207 |
|
208 |
InstrumentIdxName = pInstrument->pInfo->Name; |
209 |
InstrumentStat = 100; |
210 |
|
211 |
// inform audio driver for the need of two channels |
212 |
try { |
213 |
if (pEngine && pEngine->pAudioOutputDevice) |
214 |
pEngine->pAudioOutputDevice->AcquireChannels(2); // gig Engine only stereo |
215 |
} |
216 |
catch (AudioOutputException e) { |
217 |
String msg = "Audio output device unable to provide 2 audio channels, cause: " + e.Message(); |
218 |
throw Exception(msg); |
219 |
} |
220 |
|
221 |
if (pEngine) pEngine->Enable(); |
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 |
pOutputLeft = pAudioOut->Channel(0)->Buffer(); |
273 |
pOutputRight = pAudioOut->Channel(1)->Buffer(); |
274 |
MidiInputPort::AddSysexListener(pEngine); |
275 |
} |
276 |
|
277 |
void EngineChannel::DisconnectAudioOutputDevice() { |
278 |
if (pEngine) { // if clause to prevent disconnect loops |
279 |
ResetInternal(); |
280 |
if (pEvents) { |
281 |
delete pEvents; |
282 |
pEvents = NULL; |
283 |
} |
284 |
for (uint i = 0; i < 128; i++) { |
285 |
if (pMIDIKeyInfo[i].pActiveVoices) { |
286 |
delete pMIDIKeyInfo[i].pActiveVoices; |
287 |
pMIDIKeyInfo[i].pActiveVoices = NULL; |
288 |
} |
289 |
if (pMIDIKeyInfo[i].pEvents) { |
290 |
delete pMIDIKeyInfo[i].pEvents; |
291 |
pMIDIKeyInfo[i].pEvents = NULL; |
292 |
} |
293 |
} |
294 |
Engine* oldEngine = pEngine; |
295 |
AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice; |
296 |
pEngine = NULL; |
297 |
Engine::FreeEngine(this, oldAudioDevice); |
298 |
AudioDeviceChannelLeft = -1; |
299 |
AudioDeviceChannelRight = -1; |
300 |
} |
301 |
} |
302 |
|
303 |
void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) { |
304 |
if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet."); |
305 |
|
306 |
AudioChannel* pChannel = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannel); |
307 |
if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel)); |
308 |
switch (EngineAudioChannel) { |
309 |
case 0: // left output channel |
310 |
pOutputLeft = pChannel->Buffer(); |
311 |
AudioDeviceChannelLeft = AudioDeviceChannel; |
312 |
break; |
313 |
case 1: // right output channel |
314 |
pOutputRight = pChannel->Buffer(); |
315 |
AudioDeviceChannelRight = AudioDeviceChannel; |
316 |
break; |
317 |
default: |
318 |
throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel)); |
319 |
} |
320 |
} |
321 |
|
322 |
int EngineChannel::OutputChannel(uint EngineAudioChannel) { |
323 |
switch (EngineAudioChannel) { |
324 |
case 0: // left channel |
325 |
return AudioDeviceChannelLeft; |
326 |
case 1: // right channel |
327 |
return AudioDeviceChannelRight; |
328 |
default: |
329 |
throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel)); |
330 |
} |
331 |
} |
332 |
|
333 |
void EngineChannel::Connect(MidiInputPort* pMidiPort, midi_chan_t MidiChannel) { |
334 |
if (!pMidiPort || pMidiPort == this->pMidiInputPort) return; |
335 |
DisconnectMidiInputPort(); |
336 |
this->pMidiInputPort = pMidiPort; |
337 |
this->midiChannel = MidiChannel; |
338 |
pMidiPort->Connect(this, MidiChannel); |
339 |
} |
340 |
|
341 |
void EngineChannel::DisconnectMidiInputPort() { |
342 |
MidiInputPort* pOldPort = this->pMidiInputPort; |
343 |
this->pMidiInputPort = NULL; |
344 |
if (pOldPort) pOldPort->Disconnect(this); |
345 |
} |
346 |
|
347 |
MidiInputPort* EngineChannel::GetMidiInputPort() { |
348 |
return pMidiInputPort; |
349 |
} |
350 |
|
351 |
midi_chan_t EngineChannel::MidiChannel() { |
352 |
return midiChannel; |
353 |
} |
354 |
|
355 |
/** |
356 |
* Will be called by the MIDIIn Thread to let the audio thread trigger a new |
357 |
* voice for the given key. This method is meant for real time rendering, |
358 |
* that is an event will immediately be created with the current system |
359 |
* time as time stamp. |
360 |
* |
361 |
* @param Key - MIDI key number of the triggered key |
362 |
* @param Velocity - MIDI velocity value of the triggered key |
363 |
*/ |
364 |
void EngineChannel::SendNoteOn(uint8_t Key, uint8_t Velocity) { |
365 |
if (pEngine) { |
366 |
Event event = pEngine->pEventGenerator->CreateEvent(); |
367 |
event.Type = Event::type_note_on; |
368 |
event.Param.Note.Key = Key; |
369 |
event.Param.Note.Velocity = Velocity; |
370 |
event.pEngineChannel = this; |
371 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
372 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
373 |
} |
374 |
} |
375 |
|
376 |
/** |
377 |
* Will be called by the MIDIIn Thread to let the audio thread trigger a new |
378 |
* voice for the given key. This method is meant for offline rendering |
379 |
* and / or for cases where the exact position of the event in the current |
380 |
* audio fragment is already known. |
381 |
* |
382 |
* @param Key - MIDI key number of the triggered key |
383 |
* @param Velocity - MIDI velocity value of the triggered key |
384 |
* @param FragmentPos - sample point position in the current audio |
385 |
* fragment to which this event belongs to |
386 |
*/ |
387 |
void EngineChannel::SendNoteOn(uint8_t Key, uint8_t Velocity, int32_t FragmentPos) { |
388 |
if (FragmentPos < 0) { |
389 |
dmsg(1,("EngineChannel::SendNoteOn(): negative FragmentPos! Seems MIDI driver is buggy!")); |
390 |
} |
391 |
else if (pEngine) { |
392 |
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
393 |
event.Type = Event::type_note_on; |
394 |
event.Param.Note.Key = Key; |
395 |
event.Param.Note.Velocity = Velocity; |
396 |
event.pEngineChannel = this; |
397 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
398 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
399 |
} |
400 |
} |
401 |
|
402 |
/** |
403 |
* Will be called by the MIDIIn Thread to signal the audio thread to release |
404 |
* voice(s) on the given key. This method is meant for real time rendering, |
405 |
* that is an event will immediately be created with the current system |
406 |
* time as time stamp. |
407 |
* |
408 |
* @param Key - MIDI key number of the released key |
409 |
* @param Velocity - MIDI release velocity value of the released key |
410 |
*/ |
411 |
void EngineChannel::SendNoteOff(uint8_t Key, uint8_t Velocity) { |
412 |
if (pEngine) { |
413 |
Event event = pEngine->pEventGenerator->CreateEvent(); |
414 |
event.Type = Event::type_note_off; |
415 |
event.Param.Note.Key = Key; |
416 |
event.Param.Note.Velocity = Velocity; |
417 |
event.pEngineChannel = this; |
418 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
419 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
420 |
} |
421 |
} |
422 |
|
423 |
/** |
424 |
* Will be called by the MIDIIn Thread to signal the audio thread to release |
425 |
* voice(s) on the given key. This method is meant for offline rendering |
426 |
* and / or for cases where the exact position of the event in the current |
427 |
* audio fragment is already known. |
428 |
* |
429 |
* @param Key - MIDI key number of the released key |
430 |
* @param Velocity - MIDI release velocity value of the released key |
431 |
* @param FragmentPos - sample point position in the current audio |
432 |
* fragment to which this event belongs to |
433 |
*/ |
434 |
void EngineChannel::SendNoteOff(uint8_t Key, uint8_t Velocity, int32_t FragmentPos) { |
435 |
if (FragmentPos < 0) { |
436 |
dmsg(1,("EngineChannel::SendNoteOff(): negative FragmentPos! Seems MIDI driver is buggy!")); |
437 |
} |
438 |
else if (pEngine) { |
439 |
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
440 |
event.Type = Event::type_note_off; |
441 |
event.Param.Note.Key = Key; |
442 |
event.Param.Note.Velocity = Velocity; |
443 |
event.pEngineChannel = this; |
444 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
445 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
446 |
} |
447 |
} |
448 |
|
449 |
/** |
450 |
* Will be called by the MIDIIn Thread to signal the audio thread to change |
451 |
* the pitch value for all voices. This method is meant for real time |
452 |
* rendering, that is an event will immediately be created with the |
453 |
* current system time as time stamp. |
454 |
* |
455 |
* @param Pitch - MIDI pitch value (-8192 ... +8191) |
456 |
*/ |
457 |
void EngineChannel::SendPitchbend(int Pitch) { |
458 |
if (pEngine) { |
459 |
Event event = pEngine->pEventGenerator->CreateEvent(); |
460 |
event.Type = Event::type_pitchbend; |
461 |
event.Param.Pitch.Pitch = Pitch; |
462 |
event.pEngineChannel = this; |
463 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
464 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
465 |
} |
466 |
} |
467 |
|
468 |
/** |
469 |
* Will be called by the MIDIIn Thread to signal the audio thread to change |
470 |
* the pitch value for all voices. This method is meant for offline |
471 |
* rendering and / or for cases where the exact position of the event in |
472 |
* the current audio fragment is already known. |
473 |
* |
474 |
* @param Pitch - MIDI pitch value (-8192 ... +8191) |
475 |
* @param FragmentPos - sample point position in the current audio |
476 |
* fragment to which this event belongs to |
477 |
*/ |
478 |
void EngineChannel::SendPitchbend(int Pitch, int32_t FragmentPos) { |
479 |
if (FragmentPos < 0) { |
480 |
dmsg(1,("EngineChannel::SendPitchBend(): negative FragmentPos! Seems MIDI driver is buggy!")); |
481 |
} |
482 |
else if (pEngine) { |
483 |
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
484 |
event.Type = Event::type_pitchbend; |
485 |
event.Param.Pitch.Pitch = Pitch; |
486 |
event.pEngineChannel = this; |
487 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
488 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
489 |
} |
490 |
} |
491 |
|
492 |
/** |
493 |
* Will be called by the MIDIIn Thread to signal the audio thread that a |
494 |
* continuous controller value has changed. This method is meant for real |
495 |
* time rendering, that is an event will immediately be created with the |
496 |
* current system time as time stamp. |
497 |
* |
498 |
* @param Controller - MIDI controller number of the occured control change |
499 |
* @param Value - value of the control change |
500 |
*/ |
501 |
void EngineChannel::SendControlChange(uint8_t Controller, uint8_t Value) { |
502 |
if (pEngine) { |
503 |
Event event = pEngine->pEventGenerator->CreateEvent(); |
504 |
event.Type = Event::type_control_change; |
505 |
event.Param.CC.Controller = Controller; |
506 |
event.Param.CC.Value = Value; |
507 |
event.pEngineChannel = this; |
508 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
509 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
510 |
} |
511 |
} |
512 |
|
513 |
/** |
514 |
* Will be called by the MIDIIn Thread to signal the audio thread that a |
515 |
* continuous controller value has changed. This method is meant for |
516 |
* offline rendering and / or for cases where the exact position of the |
517 |
* event in the current audio fragment is already known. |
518 |
* |
519 |
* @param Controller - MIDI controller number of the occured control change |
520 |
* @param Value - value of the control change |
521 |
* @param FragmentPos - sample point position in the current audio |
522 |
* fragment to which this event belongs to |
523 |
*/ |
524 |
void EngineChannel::SendControlChange(uint8_t Controller, uint8_t Value, int32_t FragmentPos) { |
525 |
if (FragmentPos < 0) { |
526 |
dmsg(1,("EngineChannel::SendControlChange(): negative FragmentPos! Seems MIDI driver is buggy!")); |
527 |
} |
528 |
else if (pEngine) { |
529 |
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
530 |
event.Type = Event::type_control_change; |
531 |
event.Param.CC.Controller = Controller; |
532 |
event.Param.CC.Value = Value; |
533 |
event.pEngineChannel = this; |
534 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
535 |
else dmsg(1,("EngineChannel: Input event queue full!")); |
536 |
} |
537 |
} |
538 |
|
539 |
void EngineChannel::ClearEventLists() { |
540 |
pEvents->clear(); |
541 |
// empty MIDI key specific event lists |
542 |
{ |
543 |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
544 |
RTList<uint>::Iterator end = pActiveKeys->end(); |
545 |
for(; iuiKey != end; ++iuiKey) { |
546 |
pMIDIKeyInfo[*iuiKey].pEvents->clear(); // free all events on the key |
547 |
} |
548 |
} |
549 |
} |
550 |
|
551 |
void EngineChannel::ResetControllers() { |
552 |
Pitch = 0; |
553 |
SustainPedal = false; |
554 |
SostenutoPedal = false; |
555 |
GlobalVolume = CONFIG_GLOBAL_ATTENUATION; |
556 |
MidiVolume = 1.0; |
557 |
GlobalPanLeft = 1.0f; |
558 |
GlobalPanRight = 1.0f; |
559 |
// set all MIDI controller values to zero |
560 |
memset(ControllerTable, 0x00, 129); |
561 |
} |
562 |
|
563 |
/** |
564 |
* Copy all events from the engine channel's input event queue buffer to |
565 |
* the internal event list. This will be done at the beginning of each |
566 |
* audio cycle (that is each RenderAudio() call) to distinguish all |
567 |
* events which have to be processed in the current audio cycle. Each |
568 |
* EngineChannel has it's own input event queue for the common channel |
569 |
* specific events (like NoteOn, NoteOff and ControlChange events). |
570 |
* Beside that, the engine also has a input event queue for global |
571 |
* events (usually SysEx messages). |
572 |
* |
573 |
* @param Samples - number of sample points to be processed in the |
574 |
* current audio cycle |
575 |
*/ |
576 |
void EngineChannel::ImportEvents(uint Samples) { |
577 |
RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
578 |
Event* pEvent; |
579 |
while (true) { |
580 |
// get next event from input event queue |
581 |
if (!(pEvent = eventQueueReader.pop())) break; |
582 |
// if younger event reached, ignore that and all subsequent ones for now |
583 |
if (pEvent->FragmentPos() >= Samples) { |
584 |
eventQueueReader--; |
585 |
dmsg(2,("Younger Event, pos=%d ,Samples=%d!\n",pEvent->FragmentPos(),Samples)); |
586 |
pEvent->ResetFragmentPos(); |
587 |
break; |
588 |
} |
589 |
// copy event to internal event list |
590 |
if (pEvents->poolIsEmpty()) { |
591 |
dmsg(1,("Event pool emtpy!\n")); |
592 |
break; |
593 |
} |
594 |
*pEvents->allocAppend() = *pEvent; |
595 |
} |
596 |
eventQueueReader.free(); // free all copied events from input queue |
597 |
} |
598 |
|
599 |
float EngineChannel::Volume() { |
600 |
return GlobalVolume; |
601 |
} |
602 |
|
603 |
void EngineChannel::Volume(float f) { |
604 |
GlobalVolume = f; |
605 |
bStatusChanged = true; // status of engine channel has changed, so set notify flag |
606 |
} |
607 |
|
608 |
uint EngineChannel::Channels() { |
609 |
return 2; |
610 |
} |
611 |
|
612 |
String EngineChannel::InstrumentFileName() { |
613 |
return InstrumentFile; |
614 |
} |
615 |
|
616 |
String EngineChannel::InstrumentName() { |
617 |
return InstrumentIdxName; |
618 |
} |
619 |
|
620 |
int EngineChannel::InstrumentIndex() { |
621 |
return InstrumentIdx; |
622 |
} |
623 |
|
624 |
int EngineChannel::InstrumentStatus() { |
625 |
return InstrumentStat; |
626 |
} |
627 |
|
628 |
String EngineChannel::EngineName() { |
629 |
return LS_GIG_ENGINE_NAME; |
630 |
} |
631 |
|
632 |
}} // namespace LinuxSampler::gig |