3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005 Christian Schoenebeck * |
* Copyright (C) 2005 - 2007 Christian Schoenebeck * |
7 |
* * |
* * |
8 |
* This program is free software; you can redistribute it and/or modify * |
* 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 * |
* it under the terms of the GNU General Public License as published by * |
30 |
pEngine = NULL; |
pEngine = NULL; |
31 |
pInstrument = NULL; |
pInstrument = NULL; |
32 |
pEvents = NULL; // we allocate when we retrieve the right Engine object |
pEvents = NULL; // we allocate when we retrieve the right Engine object |
33 |
pCCEvents = NULL; // we allocate when we retrieve the right Engine object |
pEventQueue = new RingBuffer<Event,false>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0); |
|
pEventQueue = new RingBuffer<Event>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0); |
|
34 |
pActiveKeys = new Pool<uint>(128); |
pActiveKeys = new Pool<uint>(128); |
35 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
36 |
pMIDIKeyInfo[i].pActiveVoices = NULL; // we allocate when we retrieve the right Engine object |
pMIDIKeyInfo[i].pActiveVoices = NULL; // we allocate when we retrieve the right Engine object |
41 |
pMIDIKeyInfo[i].VoiceTheftsQueued = 0; |
pMIDIKeyInfo[i].VoiceTheftsQueued = 0; |
42 |
pMIDIKeyInfo[i].RoundRobinIndex = 0; |
pMIDIKeyInfo[i].RoundRobinIndex = 0; |
43 |
} |
} |
|
for (uint i = 0; i < Event::destination_count; i++) { |
|
|
pSynthesisEvents[i] = NULL; // we allocate when we retrieve the right Engine object |
|
|
} |
|
44 |
InstrumentIdx = -1; |
InstrumentIdx = -1; |
45 |
InstrumentStat = -1; |
InstrumentStat = -1; |
46 |
|
pChannelLeft = NULL; |
47 |
|
pChannelRight = NULL; |
48 |
AudioDeviceChannelLeft = -1; |
AudioDeviceChannelLeft = -1; |
49 |
AudioDeviceChannelRight = -1; |
AudioDeviceChannelRight = -1; |
50 |
|
pMidiInputPort = NULL; |
51 |
|
midiChannel = midi_chan_all; |
52 |
|
ResetControllers(); |
53 |
|
SoloMode = false; |
54 |
|
PortamentoMode = false; |
55 |
|
PortamentoTime = CONFIG_PORTAMENTO_TIME_DEFAULT; |
56 |
} |
} |
57 |
|
|
58 |
EngineChannel::~EngineChannel() { |
EngineChannel::~EngineChannel() { |
61 |
if (pEventQueue) delete pEventQueue; |
if (pEventQueue) delete pEventQueue; |
62 |
if (pActiveKeys) delete pActiveKeys; |
if (pActiveKeys) delete pActiveKeys; |
63 |
if (pMIDIKeyInfo) delete[] pMIDIKeyInfo; |
if (pMIDIKeyInfo) delete[] pMIDIKeyInfo; |
64 |
|
RemoveAllFxSends(); |
65 |
} |
} |
66 |
|
|
67 |
/** |
/** |
70 |
* check if some engine channel parameter has changed since the last |
* check if some engine channel parameter has changed since the last |
71 |
* StatusChanged() call. |
* StatusChanged() call. |
72 |
* |
* |
73 |
|
* This method can also be used to mark the engine channel as changed |
74 |
|
* from outside, e.g. by a MIDI input device. The optional argument |
75 |
|
* \a nNewStatus can be used for this. |
76 |
|
* |
77 |
* TODO: This "poll method" is just a lazy solution and might be |
* TODO: This "poll method" is just a lazy solution and might be |
78 |
* replaced in future. |
* replaced in future. |
79 |
* |
* @param bNewStatus - (optional, default: false) sets the new status flag |
80 |
* @returns true if engine channel status has changed since last |
* @returns true if engine channel status has changed since last |
81 |
* StatusChanged() call |
* StatusChanged() call |
82 |
*/ |
*/ |
83 |
bool EngineChannel::StatusChanged() { |
bool EngineChannel::StatusChanged(bool bNewStatus) { |
84 |
bool b = bStatusChanged; |
bool b = bStatusChanged; |
85 |
bStatusChanged = false; |
bStatusChanged = bNewStatus; |
86 |
return b; |
return b; |
87 |
} |
} |
88 |
|
|
89 |
|
void EngineChannel::Reset() { |
90 |
|
if (pEngine) pEngine->DisableAndLock(); |
91 |
|
ResetInternal(); |
92 |
|
ResetControllers(); |
93 |
|
if (pEngine) { |
94 |
|
pEngine->Enable(); |
95 |
|
pEngine->Reset(); |
96 |
|
} |
97 |
|
} |
98 |
|
|
99 |
/** |
/** |
100 |
* This method is not thread safe! |
* This method is not thread safe! |
101 |
*/ |
*/ |
102 |
void EngineChannel::ResetInternal() { |
void EngineChannel::ResetInternal() { |
|
Pitch = 0; |
|
|
SustainPedal = false; |
|
|
GlobalVolume = 1.0; |
|
|
GlobalPanLeft = 1.0f; |
|
|
GlobalPanRight = 1.0f; |
|
103 |
CurrentKeyDimension = 0; |
CurrentKeyDimension = 0; |
104 |
|
|
|
ResetControllers(); |
|
|
|
|
105 |
// reset key info |
// reset key info |
106 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
107 |
if (pMIDIKeyInfo[i].pActiveVoices) |
if (pMIDIKeyInfo[i].pActiveVoices) |
114 |
pMIDIKeyInfo[i].itSelf = Pool<uint>::Iterator(); |
pMIDIKeyInfo[i].itSelf = Pool<uint>::Iterator(); |
115 |
pMIDIKeyInfo[i].VoiceTheftsQueued = 0; |
pMIDIKeyInfo[i].VoiceTheftsQueued = 0; |
116 |
} |
} |
117 |
|
SoloKey = -1; // no solo key active yet |
118 |
|
PortamentoPos = -1.0f; // no portamento active yet |
119 |
|
|
120 |
// reset all key groups |
// reset all key groups |
121 |
std::map<uint,uint*>::iterator iter = ActiveKeyGroups.begin(); |
std::map<uint,uint*>::iterator iter = ActiveKeyGroups.begin(); |
159 |
* This method will then actually start to load the instrument and block |
* This method will then actually start to load the instrument and block |
160 |
* the calling thread until loading was completed. |
* the calling thread until loading was completed. |
161 |
* |
* |
|
* @returns detailed description of the method call result |
|
162 |
* @see PrepareLoadInstrument() |
* @see PrepareLoadInstrument() |
163 |
*/ |
*/ |
164 |
void EngineChannel::LoadInstrument() { |
void EngineChannel::LoadInstrument() { |
165 |
|
::gig::Instrument* oldInstrument = pInstrument; |
|
if (pEngine) pEngine->DisableAndLock(); |
|
|
|
|
|
ResetInternal(); |
|
166 |
|
|
167 |
// free old instrument |
// free old instrument |
168 |
if (pInstrument) { |
if (oldInstrument) { |
169 |
// give old instrument back to instrument manager |
if (pEngine) { |
170 |
Engine::instruments.HandBack(pInstrument, this); |
// make sure we don't trigger any new notes with the |
171 |
|
// old instrument |
172 |
|
::gig::DimensionRegion** dimRegionsInUse = pEngine->ChangeInstrument(this, 0); |
173 |
|
|
174 |
|
// give old instrument back to instrument manager, but |
175 |
|
// keep the dimension regions and samples that are in |
176 |
|
// use |
177 |
|
Engine::instruments.HandBackInstrument(oldInstrument, this, dimRegionsInUse); |
178 |
|
} else { |
179 |
|
Engine::instruments.HandBack(oldInstrument, this); |
180 |
|
} |
181 |
} |
} |
182 |
|
|
183 |
// delete all key groups |
// delete all key groups |
184 |
ActiveKeyGroups.clear(); |
ActiveKeyGroups.clear(); |
185 |
|
|
186 |
// request gig instrument from instrument manager |
// request gig instrument from instrument manager |
187 |
|
::gig::Instrument* newInstrument; |
188 |
try { |
try { |
189 |
instrument_id_t instrid; |
InstrumentManager::instrument_id_t instrid; |
190 |
instrid.FileName = InstrumentFile; |
instrid.FileName = InstrumentFile; |
191 |
instrid.iInstrument = InstrumentIdx; |
instrid.Index = InstrumentIdx; |
192 |
pInstrument = Engine::instruments.Borrow(instrid, this); |
newInstrument = Engine::instruments.Borrow(instrid, this); |
193 |
if (!pInstrument) { |
if (!newInstrument) { |
194 |
InstrumentStat = -1; |
throw InstrumentManagerException("resource was not created"); |
|
dmsg(1,("no instrument loaded!!!\n")); |
|
|
exit(EXIT_FAILURE); |
|
195 |
} |
} |
196 |
} |
} |
197 |
catch (RIFF::Exception e) { |
catch (RIFF::Exception e) { |
198 |
InstrumentStat = -2; |
InstrumentStat = -2; |
199 |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message; |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message; |
200 |
throw LinuxSamplerException(msg); |
throw Exception(msg); |
201 |
} |
} |
202 |
catch (InstrumentResourceManagerException e) { |
catch (InstrumentManagerException e) { |
203 |
InstrumentStat = -3; |
InstrumentStat = -3; |
204 |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message(); |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message(); |
205 |
throw LinuxSamplerException(msg); |
throw Exception(msg); |
206 |
} |
} |
207 |
catch (...) { |
catch (...) { |
208 |
InstrumentStat = -4; |
InstrumentStat = -4; |
209 |
throw LinuxSamplerException("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file."); |
throw Exception("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file."); |
210 |
} |
} |
211 |
|
|
212 |
// rebuild ActiveKeyGroups map with key groups of current instrument |
// rebuild ActiveKeyGroups map with key groups of current instrument |
213 |
for (::gig::Region* pRegion = pInstrument->GetFirstRegion(); pRegion; pRegion = pInstrument->GetNextRegion()) |
for (::gig::Region* pRegion = newInstrument->GetFirstRegion(); pRegion; pRegion = newInstrument->GetNextRegion()) |
214 |
if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL; |
if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL; |
215 |
|
|
216 |
InstrumentIdxName = pInstrument->pInfo->Name; |
InstrumentIdxName = newInstrument->pInfo->Name; |
217 |
InstrumentStat = 100; |
InstrumentStat = 100; |
218 |
|
|
219 |
// inform audio driver for the need of two channels |
if (pEngine) pEngine->ChangeInstrument(this, newInstrument); |
220 |
try { |
else pInstrument = newInstrument; |
|
if (pEngine && pEngine->pAudioOutputDevice) |
|
|
pEngine->pAudioOutputDevice->AcquireChannels(2); // gig Engine only stereo |
|
|
} |
|
|
catch (AudioOutputException e) { |
|
|
String msg = "Audio output device unable to provide 2 audio channels, cause: " + e.Message(); |
|
|
throw LinuxSamplerException(msg); |
|
|
} |
|
|
|
|
|
if (pEngine) pEngine->Enable(); |
|
221 |
} |
} |
222 |
|
|
223 |
/** |
/** |
261 |
} |
} |
262 |
pEngine = Engine::AcquireEngine(this, pAudioOut); |
pEngine = Engine::AcquireEngine(this, pAudioOut); |
263 |
ResetInternal(); |
ResetInternal(); |
264 |
pEvents = new RTList<Event>(pEngine->pEventPool); |
pEvents = new RTList<Event>(pEngine->pEventPool); |
|
pCCEvents = new RTList<Event>(pEngine->pEventPool); |
|
|
for (uint i = 0; i < Event::destination_count; i++) { |
|
|
pSynthesisEvents[i] = new RTList<Event>(pEngine->pEventPool); |
|
|
} |
|
265 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
266 |
pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool); |
pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pEngine->pVoicePool); |
267 |
pMIDIKeyInfo[i].pEvents = new RTList<Event>(pEngine->pEventPool); |
pMIDIKeyInfo[i].pEvents = new RTList<Event>(pEngine->pEventPool); |
268 |
} |
} |
269 |
AudioDeviceChannelLeft = 0; |
AudioDeviceChannelLeft = 0; |
270 |
AudioDeviceChannelRight = 1; |
AudioDeviceChannelRight = 1; |
271 |
pOutputLeft = pAudioOut->Channel(0)->Buffer(); |
if (fxSends.empty()) { // render directly into the AudioDevice's output buffers |
272 |
pOutputRight = pAudioOut->Channel(1)->Buffer(); |
pChannelLeft = pAudioOut->Channel(AudioDeviceChannelLeft); |
273 |
|
pChannelRight = pAudioOut->Channel(AudioDeviceChannelRight); |
274 |
|
} else { // use local buffers for rendering and copy later |
275 |
|
// ensure the local buffers have the correct size |
276 |
|
if (pChannelLeft) delete pChannelLeft; |
277 |
|
if (pChannelRight) delete pChannelRight; |
278 |
|
pChannelLeft = new AudioChannel(0, pAudioOut->MaxSamplesPerCycle()); |
279 |
|
pChannelRight = new AudioChannel(1, pAudioOut->MaxSamplesPerCycle()); |
280 |
|
} |
281 |
|
if (pEngine->EngineDisabled.GetUnsafe()) pEngine->Enable(); |
282 |
|
MidiInputPort::AddSysexListener(pEngine); |
283 |
} |
} |
284 |
|
|
285 |
void EngineChannel::DisconnectAudioOutputDevice() { |
void EngineChannel::DisconnectAudioOutputDevice() { |
289 |
delete pEvents; |
delete pEvents; |
290 |
pEvents = NULL; |
pEvents = NULL; |
291 |
} |
} |
|
if (pCCEvents) { |
|
|
delete pCCEvents; |
|
|
pCCEvents = NULL; |
|
|
} |
|
292 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
293 |
if (pMIDIKeyInfo[i].pActiveVoices) { |
if (pMIDIKeyInfo[i].pActiveVoices) { |
294 |
delete pMIDIKeyInfo[i].pActiveVoices; |
delete pMIDIKeyInfo[i].pActiveVoices; |
299 |
pMIDIKeyInfo[i].pEvents = NULL; |
pMIDIKeyInfo[i].pEvents = NULL; |
300 |
} |
} |
301 |
} |
} |
|
for (uint i = 0; i < Event::destination_count; i++) { |
|
|
if (pSynthesisEvents[i]) { |
|
|
delete pSynthesisEvents[i]; |
|
|
pSynthesisEvents[i] = NULL; |
|
|
} |
|
|
} |
|
302 |
Engine* oldEngine = pEngine; |
Engine* oldEngine = pEngine; |
303 |
AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice; |
AudioOutputDevice* oldAudioDevice = pEngine->pAudioOutputDevice; |
304 |
pEngine = NULL; |
pEngine = NULL; |
305 |
Engine::FreeEngine(this, oldAudioDevice); |
Engine::FreeEngine(this, oldAudioDevice); |
306 |
AudioDeviceChannelLeft = -1; |
AudioDeviceChannelLeft = -1; |
307 |
AudioDeviceChannelRight = -1; |
AudioDeviceChannelRight = -1; |
308 |
|
if (!fxSends.empty()) { // free the local rendering buffers |
309 |
|
if (pChannelLeft) delete pChannelLeft; |
310 |
|
if (pChannelRight) delete pChannelRight; |
311 |
|
} |
312 |
|
pChannelLeft = NULL; |
313 |
|
pChannelRight = NULL; |
314 |
} |
} |
315 |
} |
} |
316 |
|
|
317 |
|
AudioOutputDevice* EngineChannel::GetAudioOutputDevice() { |
318 |
|
return (pEngine) ? pEngine->pAudioOutputDevice : NULL; |
319 |
|
} |
320 |
|
|
321 |
void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) { |
void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) { |
322 |
if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet."); |
if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet."); |
323 |
|
|
325 |
if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel)); |
if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel)); |
326 |
switch (EngineAudioChannel) { |
switch (EngineAudioChannel) { |
327 |
case 0: // left output channel |
case 0: // left output channel |
328 |
pOutputLeft = pChannel->Buffer(); |
if (fxSends.empty()) pChannelLeft = pChannel; |
329 |
AudioDeviceChannelLeft = AudioDeviceChannel; |
AudioDeviceChannelLeft = AudioDeviceChannel; |
330 |
break; |
break; |
331 |
case 1: // right output channel |
case 1: // right output channel |
332 |
pOutputRight = pChannel->Buffer(); |
if (fxSends.empty()) pChannelRight = pChannel; |
333 |
AudioDeviceChannelRight = AudioDeviceChannel; |
AudioDeviceChannelRight = AudioDeviceChannel; |
334 |
break; |
break; |
335 |
default: |
default: |
336 |
throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel)); |
throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel)); |
337 |
} |
} |
338 |
|
|
339 |
|
bStatusChanged = true; |
340 |
} |
} |
341 |
|
|
342 |
int EngineChannel::OutputChannel(uint EngineAudioChannel) { |
int EngineChannel::OutputChannel(uint EngineAudioChannel) { |
350 |
} |
} |
351 |
} |
} |
352 |
|
|
353 |
|
void EngineChannel::Connect(MidiInputPort* pMidiPort, midi_chan_t MidiChannel) { |
354 |
|
if (!pMidiPort || pMidiPort == this->pMidiInputPort) return; |
355 |
|
DisconnectMidiInputPort(); |
356 |
|
this->pMidiInputPort = pMidiPort; |
357 |
|
this->midiChannel = MidiChannel; |
358 |
|
pMidiPort->Connect(this, MidiChannel); |
359 |
|
} |
360 |
|
|
361 |
|
void EngineChannel::DisconnectMidiInputPort() { |
362 |
|
MidiInputPort* pOldPort = this->pMidiInputPort; |
363 |
|
this->pMidiInputPort = NULL; |
364 |
|
if (pOldPort) pOldPort->Disconnect(this); |
365 |
|
} |
366 |
|
|
367 |
|
MidiInputPort* EngineChannel::GetMidiInputPort() { |
368 |
|
return pMidiInputPort; |
369 |
|
} |
370 |
|
|
371 |
|
midi_chan_t EngineChannel::MidiChannel() { |
372 |
|
return midiChannel; |
373 |
|
} |
374 |
|
|
375 |
|
FxSend* EngineChannel::AddFxSend(uint8_t MidiCtrl, String Name) throw (Exception) { |
376 |
|
if (pEngine) pEngine->DisableAndLock(); |
377 |
|
FxSend* pFxSend = new FxSend(this, MidiCtrl, Name); |
378 |
|
if (fxSends.empty()) { |
379 |
|
if (pEngine && pEngine->pAudioOutputDevice) { |
380 |
|
AudioOutputDevice* pDevice = pEngine->pAudioOutputDevice; |
381 |
|
// create local render buffers |
382 |
|
pChannelLeft = new AudioChannel(0, pDevice->MaxSamplesPerCycle()); |
383 |
|
pChannelRight = new AudioChannel(1, pDevice->MaxSamplesPerCycle()); |
384 |
|
} else { |
385 |
|
// postpone local render buffer creation until audio device is assigned |
386 |
|
pChannelLeft = NULL; |
387 |
|
pChannelRight = NULL; |
388 |
|
} |
389 |
|
} |
390 |
|
fxSends.push_back(pFxSend); |
391 |
|
if (pEngine) pEngine->Enable(); |
392 |
|
fireFxSendCountChanged(iSamplerChannelIndex, GetFxSendCount()); |
393 |
|
|
394 |
|
return pFxSend; |
395 |
|
} |
396 |
|
|
397 |
|
FxSend* EngineChannel::GetFxSend(uint FxSendIndex) { |
398 |
|
return (FxSendIndex < fxSends.size()) ? fxSends[FxSendIndex] : NULL; |
399 |
|
} |
400 |
|
|
401 |
|
uint EngineChannel::GetFxSendCount() { |
402 |
|
return fxSends.size(); |
403 |
|
} |
404 |
|
|
405 |
|
void EngineChannel::RemoveFxSend(FxSend* pFxSend) { |
406 |
|
if (pEngine) pEngine->DisableAndLock(); |
407 |
|
for ( |
408 |
|
std::vector<FxSend*>::iterator iter = fxSends.begin(); |
409 |
|
iter != fxSends.end(); iter++ |
410 |
|
) { |
411 |
|
if (*iter == pFxSend) { |
412 |
|
delete pFxSend; |
413 |
|
fxSends.erase(iter); |
414 |
|
if (fxSends.empty()) { |
415 |
|
// destroy local render buffers |
416 |
|
if (pChannelLeft) delete pChannelLeft; |
417 |
|
if (pChannelRight) delete pChannelRight; |
418 |
|
// fallback to render directly into AudioOutputDevice's buffers |
419 |
|
if (pEngine && pEngine->pAudioOutputDevice) { |
420 |
|
pChannelLeft = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft); |
421 |
|
pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight); |
422 |
|
} else { // we update the pointers later |
423 |
|
pChannelLeft = NULL; |
424 |
|
pChannelRight = NULL; |
425 |
|
} |
426 |
|
} |
427 |
|
break; |
428 |
|
} |
429 |
|
} |
430 |
|
if (pEngine) pEngine->Enable(); |
431 |
|
fireFxSendCountChanged(iSamplerChannelIndex, GetFxSendCount()); |
432 |
|
} |
433 |
|
|
434 |
/** |
/** |
435 |
* Will be called by the MIDIIn Thread to let the audio thread trigger a new |
* Will be called by the MIDIIn Thread to let the audio thread trigger a new |
436 |
* voice for the given key. |
* voice for the given key. This method is meant for real time rendering, |
437 |
|
* that is an event will immediately be created with the current system |
438 |
|
* time as time stamp. |
439 |
* |
* |
440 |
* @param Key - MIDI key number of the triggered key |
* @param Key - MIDI key number of the triggered key |
441 |
* @param Velocity - MIDI velocity value of the triggered key |
* @param Velocity - MIDI velocity value of the triggered key |
453 |
} |
} |
454 |
|
|
455 |
/** |
/** |
456 |
|
* Will be called by the MIDIIn Thread to let the audio thread trigger a new |
457 |
|
* voice for the given key. This method is meant for offline rendering |
458 |
|
* and / or for cases where the exact position of the event in the current |
459 |
|
* audio fragment is already known. |
460 |
|
* |
461 |
|
* @param Key - MIDI key number of the triggered key |
462 |
|
* @param Velocity - MIDI velocity value of the triggered key |
463 |
|
* @param FragmentPos - sample point position in the current audio |
464 |
|
* fragment to which this event belongs to |
465 |
|
*/ |
466 |
|
void EngineChannel::SendNoteOn(uint8_t Key, uint8_t Velocity, int32_t FragmentPos) { |
467 |
|
if (FragmentPos < 0) { |
468 |
|
dmsg(1,("EngineChannel::SendNoteOn(): negative FragmentPos! Seems MIDI driver is buggy!")); |
469 |
|
} |
470 |
|
else if (pEngine) { |
471 |
|
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
472 |
|
event.Type = Event::type_note_on; |
473 |
|
event.Param.Note.Key = Key; |
474 |
|
event.Param.Note.Velocity = Velocity; |
475 |
|
event.pEngineChannel = this; |
476 |
|
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
477 |
|
else dmsg(1,("EngineChannel: Input event queue full!")); |
478 |
|
} |
479 |
|
} |
480 |
|
|
481 |
|
/** |
482 |
* Will be called by the MIDIIn Thread to signal the audio thread to release |
* Will be called by the MIDIIn Thread to signal the audio thread to release |
483 |
* voice(s) on the given key. |
* voice(s) on the given key. This method is meant for real time rendering, |
484 |
|
* that is an event will immediately be created with the current system |
485 |
|
* time as time stamp. |
486 |
* |
* |
487 |
* @param Key - MIDI key number of the released key |
* @param Key - MIDI key number of the released key |
488 |
* @param Velocity - MIDI release velocity value of the released key |
* @param Velocity - MIDI release velocity value of the released key |
500 |
} |
} |
501 |
|
|
502 |
/** |
/** |
503 |
|
* Will be called by the MIDIIn Thread to signal the audio thread to release |
504 |
|
* voice(s) on the given key. This method is meant for offline rendering |
505 |
|
* and / or for cases where the exact position of the event in the current |
506 |
|
* audio fragment is already known. |
507 |
|
* |
508 |
|
* @param Key - MIDI key number of the released key |
509 |
|
* @param Velocity - MIDI release velocity value of the released key |
510 |
|
* @param FragmentPos - sample point position in the current audio |
511 |
|
* fragment to which this event belongs to |
512 |
|
*/ |
513 |
|
void EngineChannel::SendNoteOff(uint8_t Key, uint8_t Velocity, int32_t FragmentPos) { |
514 |
|
if (FragmentPos < 0) { |
515 |
|
dmsg(1,("EngineChannel::SendNoteOff(): negative FragmentPos! Seems MIDI driver is buggy!")); |
516 |
|
} |
517 |
|
else if (pEngine) { |
518 |
|
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
519 |
|
event.Type = Event::type_note_off; |
520 |
|
event.Param.Note.Key = Key; |
521 |
|
event.Param.Note.Velocity = Velocity; |
522 |
|
event.pEngineChannel = this; |
523 |
|
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
524 |
|
else dmsg(1,("EngineChannel: Input event queue full!")); |
525 |
|
} |
526 |
|
} |
527 |
|
|
528 |
|
/** |
529 |
* Will be called by the MIDIIn Thread to signal the audio thread to change |
* Will be called by the MIDIIn Thread to signal the audio thread to change |
530 |
* the pitch value for all voices. |
* the pitch value for all voices. This method is meant for real time |
531 |
|
* rendering, that is an event will immediately be created with the |
532 |
|
* current system time as time stamp. |
533 |
* |
* |
534 |
* @param Pitch - MIDI pitch value (-8192 ... +8191) |
* @param Pitch - MIDI pitch value (-8192 ... +8191) |
535 |
*/ |
*/ |
545 |
} |
} |
546 |
|
|
547 |
/** |
/** |
548 |
|
* Will be called by the MIDIIn Thread to signal the audio thread to change |
549 |
|
* the pitch value for all voices. This method is meant for offline |
550 |
|
* rendering and / or for cases where the exact position of the event in |
551 |
|
* the current audio fragment is already known. |
552 |
|
* |
553 |
|
* @param Pitch - MIDI pitch value (-8192 ... +8191) |
554 |
|
* @param FragmentPos - sample point position in the current audio |
555 |
|
* fragment to which this event belongs to |
556 |
|
*/ |
557 |
|
void EngineChannel::SendPitchbend(int Pitch, int32_t FragmentPos) { |
558 |
|
if (FragmentPos < 0) { |
559 |
|
dmsg(1,("EngineChannel::SendPitchBend(): negative FragmentPos! Seems MIDI driver is buggy!")); |
560 |
|
} |
561 |
|
else if (pEngine) { |
562 |
|
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
563 |
|
event.Type = Event::type_pitchbend; |
564 |
|
event.Param.Pitch.Pitch = Pitch; |
565 |
|
event.pEngineChannel = this; |
566 |
|
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
567 |
|
else dmsg(1,("EngineChannel: Input event queue full!")); |
568 |
|
} |
569 |
|
} |
570 |
|
|
571 |
|
/** |
572 |
* Will be called by the MIDIIn Thread to signal the audio thread that a |
* Will be called by the MIDIIn Thread to signal the audio thread that a |
573 |
* continuous controller value has changed. |
* continuous controller value has changed. This method is meant for real |
574 |
|
* time rendering, that is an event will immediately be created with the |
575 |
|
* current system time as time stamp. |
576 |
* |
* |
577 |
* @param Controller - MIDI controller number of the occured control change |
* @param Controller - MIDI controller number of the occured control change |
578 |
* @param Value - value of the control change |
* @param Value - value of the control change |
589 |
} |
} |
590 |
} |
} |
591 |
|
|
592 |
|
/** |
593 |
|
* Will be called by the MIDIIn Thread to signal the audio thread that a |
594 |
|
* continuous controller value has changed. This method is meant for |
595 |
|
* offline rendering and / or for cases where the exact position of the |
596 |
|
* event in the current audio fragment is already known. |
597 |
|
* |
598 |
|
* @param Controller - MIDI controller number of the occured control change |
599 |
|
* @param Value - value of the control change |
600 |
|
* @param FragmentPos - sample point position in the current audio |
601 |
|
* fragment to which this event belongs to |
602 |
|
*/ |
603 |
|
void EngineChannel::SendControlChange(uint8_t Controller, uint8_t Value, int32_t FragmentPos) { |
604 |
|
if (FragmentPos < 0) { |
605 |
|
dmsg(1,("EngineChannel::SendControlChange(): negative FragmentPos! Seems MIDI driver is buggy!")); |
606 |
|
} |
607 |
|
else if (pEngine) { |
608 |
|
Event event = pEngine->pEventGenerator->CreateEvent(FragmentPos); |
609 |
|
event.Type = Event::type_control_change; |
610 |
|
event.Param.CC.Controller = Controller; |
611 |
|
event.Param.CC.Value = Value; |
612 |
|
event.pEngineChannel = this; |
613 |
|
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
614 |
|
else dmsg(1,("EngineChannel: Input event queue full!")); |
615 |
|
} |
616 |
|
} |
617 |
|
|
618 |
void EngineChannel::ClearEventLists() { |
void EngineChannel::ClearEventLists() { |
619 |
pEvents->clear(); |
pEvents->clear(); |
|
pCCEvents->clear(); |
|
|
for (uint i = 0; i < Event::destination_count; i++) { |
|
|
pSynthesisEvents[i]->clear(); |
|
|
} |
|
620 |
// empty MIDI key specific event lists |
// empty MIDI key specific event lists |
621 |
{ |
{ |
622 |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
628 |
} |
} |
629 |
|
|
630 |
void EngineChannel::ResetControllers() { |
void EngineChannel::ResetControllers() { |
631 |
|
Pitch = 0; |
632 |
|
SustainPedal = false; |
633 |
|
SostenutoPedal = false; |
634 |
|
GlobalVolume = 1.0f; |
635 |
|
MidiVolume = 1.0; |
636 |
|
GlobalPanLeft = 1.0f; |
637 |
|
GlobalPanRight = 1.0f; |
638 |
|
GlobalTranspose = 0; |
639 |
// set all MIDI controller values to zero |
// set all MIDI controller values to zero |
640 |
memset(ControllerTable, 0x00, 128); |
memset(ControllerTable, 0x00, 129); |
641 |
|
// reset all FX Send levels |
642 |
|
for ( |
643 |
|
std::vector<FxSend*>::iterator iter = fxSends.begin(); |
644 |
|
iter != fxSends.end(); iter++ |
645 |
|
) { |
646 |
|
(*iter)->Reset(); |
647 |
|
} |
648 |
} |
} |
649 |
|
|
650 |
/** |
/** |
661 |
* current audio cycle |
* current audio cycle |
662 |
*/ |
*/ |
663 |
void EngineChannel::ImportEvents(uint Samples) { |
void EngineChannel::ImportEvents(uint Samples) { |
664 |
RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
RingBuffer<Event,false>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
665 |
Event* pEvent; |
Event* pEvent; |
666 |
while (true) { |
while (true) { |
667 |
// get next event from input event queue |
// get next event from input event queue |
683 |
eventQueueReader.free(); // free all copied events from input queue |
eventQueueReader.free(); // free all copied events from input queue |
684 |
} |
} |
685 |
|
|
686 |
|
void EngineChannel::RemoveAllFxSends() { |
687 |
|
if (pEngine) pEngine->DisableAndLock(); |
688 |
|
if (!fxSends.empty()) { // free local render buffers |
689 |
|
if (pChannelLeft) { |
690 |
|
delete pChannelLeft; |
691 |
|
if (pEngine && pEngine->pAudioOutputDevice) { |
692 |
|
// fallback to render directly to the AudioOutputDevice's buffer |
693 |
|
pChannelLeft = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft); |
694 |
|
} else pChannelLeft = NULL; |
695 |
|
} |
696 |
|
if (pChannelRight) { |
697 |
|
delete pChannelRight; |
698 |
|
if (pEngine && pEngine->pAudioOutputDevice) { |
699 |
|
// fallback to render directly to the AudioOutputDevice's buffer |
700 |
|
pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight); |
701 |
|
} else pChannelRight = NULL; |
702 |
|
} |
703 |
|
} |
704 |
|
for (int i = 0; i < fxSends.size(); i++) delete fxSends[i]; |
705 |
|
fxSends.clear(); |
706 |
|
if (pEngine) pEngine->Enable(); |
707 |
|
} |
708 |
|
|
709 |
float EngineChannel::Volume() { |
float EngineChannel::Volume() { |
710 |
return GlobalVolume; |
return GlobalVolume; |
711 |
} |
} |