2 |
* * |
* * |
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-2008 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 * |
29 |
|
|
30 |
#include "Engine.h" |
#include "Engine.h" |
31 |
|
|
32 |
#if defined(__APPLE__) |
#include "../../common/global_private.h" |
|
# include <stdlib.h> |
|
|
#else |
|
|
# include <malloc.h> |
|
|
#endif |
|
33 |
|
|
34 |
namespace LinuxSampler { namespace gig { |
namespace LinuxSampler { namespace gig { |
35 |
|
|
53 |
if (engines.count(pDevice)) { |
if (engines.count(pDevice)) { |
54 |
dmsg(4,("Using existing gig::Engine.\n")); |
dmsg(4,("Using existing gig::Engine.\n")); |
55 |
pEngine = engines[pDevice]; |
pEngine = engines[pDevice]; |
56 |
|
|
57 |
|
// Disable the engine while the new engine channel is |
58 |
|
// added and initialized. The engine will be enabled again |
59 |
|
// in EngineChannel::Connect. |
60 |
|
pEngine->DisableAndLock(); |
61 |
} else { // create a new engine (and disk thread) instance for the given audio output device |
} else { // create a new engine (and disk thread) instance for the given audio output device |
62 |
dmsg(4,("Creating new gig::Engine.\n")); |
dmsg(4,("Creating new gig::Engine.\n")); |
63 |
pEngine = (Engine*) EngineFactory::Create("gig"); |
pEngine = (Engine*) EngineFactory::Create("gig"); |
74 |
|
|
75 |
/** |
/** |
76 |
* Once an engine channel is disconnected from an audio output device, |
* Once an engine channel is disconnected from an audio output device, |
77 |
* it wil immediately call this method to unregister itself from the |
* it will immediately call this method to unregister itself from the |
78 |
* engine instance and if that engine instance is not used by any other |
* engine instance and if that engine instance is not used by any other |
79 |
* engine channel anymore, then that engine instance will be destroyed. |
* engine channel anymore, then that engine instance will be destroyed. |
80 |
* |
* |
100 |
/** |
/** |
101 |
* Constructor |
* Constructor |
102 |
*/ |
*/ |
103 |
Engine::Engine() { |
Engine::Engine() : SuspendedRegions(128) { |
104 |
pAudioOutputDevice = NULL; |
pAudioOutputDevice = NULL; |
105 |
pDiskThread = NULL; |
pDiskThread = NULL; |
106 |
pEventGenerator = NULL; |
pEventGenerator = NULL; |
107 |
pSysexBuffer = new RingBuffer<uint8_t>(CONFIG_SYSEX_BUFFER_SIZE, 0); |
pSysexBuffer = new RingBuffer<uint8_t,false>(CONFIG_SYSEX_BUFFER_SIZE, 0); |
108 |
pEventQueue = new RingBuffer<Event>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0); |
pEventQueue = new RingBuffer<Event,false>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0); |
109 |
pEventPool = new Pool<Event>(CONFIG_MAX_EVENTS_PER_FRAGMENT); |
pEventPool = new Pool<Event>(CONFIG_MAX_EVENTS_PER_FRAGMENT); |
110 |
pVoicePool = new Pool<Voice>(CONFIG_MAX_VOICES); |
pVoicePool = new Pool<Voice>(CONFIG_MAX_VOICES); |
111 |
|
pDimRegionPool[0] = new Pool< ::gig::DimensionRegion*>(CONFIG_MAX_VOICES); |
112 |
|
pDimRegionPool[1] = new Pool< ::gig::DimensionRegion*>(CONFIG_MAX_VOICES); |
113 |
pVoiceStealingQueue = new RTList<Event>(pEventPool); |
pVoiceStealingQueue = new RTList<Event>(pEventPool); |
114 |
pGlobalEvents = new RTList<Event>(pEventPool); |
pGlobalEvents = new RTList<Event>(pEventPool); |
115 |
|
|
116 |
for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) { |
for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) { |
117 |
iterVoice->SetEngine(this); |
iterVoice->SetEngine(this); |
118 |
} |
} |
119 |
pVoicePool->clear(); |
pVoicePool->clear(); |
120 |
|
|
|
pSynthesisParameters[0] = NULL; // we allocate when an audio device is connected |
|
|
pBasicFilterParameters = NULL; |
|
|
pMainFilterParameters = NULL; |
|
|
|
|
121 |
ResetInternal(); |
ResetInternal(); |
122 |
ResetScaleTuning(); |
ResetScaleTuning(); |
123 |
|
ResetSuspendedRegions(); |
124 |
} |
} |
125 |
|
|
126 |
/** |
/** |
127 |
* Destructor |
* Destructor |
128 |
*/ |
*/ |
129 |
Engine::~Engine() { |
Engine::~Engine() { |
130 |
|
MidiInputPort::RemoveSysexListener(this); |
131 |
if (pDiskThread) { |
if (pDiskThread) { |
132 |
dmsg(1,("Stopping disk thread...")); |
dmsg(1,("Stopping disk thread...")); |
133 |
pDiskThread->StopThread(); |
pDiskThread->StopThread(); |
141 |
delete pVoicePool; |
delete pVoicePool; |
142 |
} |
} |
143 |
if (pEventGenerator) delete pEventGenerator; |
if (pEventGenerator) delete pEventGenerator; |
|
if (pMainFilterParameters) delete[] pMainFilterParameters; |
|
|
if (pBasicFilterParameters) delete[] pBasicFilterParameters; |
|
|
if (pSynthesisParameters[0]) free(pSynthesisParameters[0]); |
|
144 |
if (pVoiceStealingQueue) delete pVoiceStealingQueue; |
if (pVoiceStealingQueue) delete pVoiceStealingQueue; |
145 |
if (pSysexBuffer) delete pSysexBuffer; |
if (pSysexBuffer) delete pSysexBuffer; |
146 |
EngineFactory::Destroy(this); |
if (pGlobalEvents) delete pGlobalEvents; |
147 |
|
if (pDimRegionPool[0]) delete pDimRegionPool[0]; |
148 |
|
if (pDimRegionPool[1]) delete pDimRegionPool[1]; |
149 |
|
ResetSuspendedRegions(); |
150 |
|
Unregister(); |
151 |
} |
} |
152 |
|
|
153 |
void Engine::Enable() { |
void Engine::Enable() { |
156 |
dmsg(3,("gig::Engine: enabled (val=%d)\n", EngineDisabled.GetUnsafe())); |
dmsg(3,("gig::Engine: enabled (val=%d)\n", EngineDisabled.GetUnsafe())); |
157 |
} |
} |
158 |
|
|
159 |
|
/** |
160 |
|
* Temporarily stop the engine to not do anything. The engine will just be |
161 |
|
* frozen during that time, that means after enabling it again it will |
162 |
|
* continue where it was, with all its voices and playback state it had at |
163 |
|
* the point of disabling. Notice that the engine's (audio) thread will |
164 |
|
* continue to run, it just remains in an inactive loop during that time. |
165 |
|
* |
166 |
|
* If you need to be sure that all voices and disk streams are killed as |
167 |
|
* well, use @c SuspendAll() instead. |
168 |
|
* |
169 |
|
* @see Enable(), SuspendAll() |
170 |
|
*/ |
171 |
void Engine::Disable() { |
void Engine::Disable() { |
172 |
dmsg(3,("gig::Engine: disabling\n")); |
dmsg(3,("gig::Engine: disabling\n")); |
173 |
bool* pWasDisabled = EngineDisabled.PushAndUnlock(true, 2); // wait max. 2s |
bool* pWasDisabled = EngineDisabled.PushAndUnlock(true, 2); // wait max. 2s |
181 |
} |
} |
182 |
|
|
183 |
/** |
/** |
184 |
|
* Similar to @c Disable() but this method additionally kills all voices |
185 |
|
* and disk streams and blocks until all voices and disk streams are actually |
186 |
|
* killed / deleted. |
187 |
|
* |
188 |
|
* @e Note: only the original calling thread is able to re-enable the |
189 |
|
* engine afterwards by calling @c ResumeAll() later on! |
190 |
|
*/ |
191 |
|
void Engine::SuspendAll() { |
192 |
|
dmsg(2,("gig::Engine: Suspending all ...\n")); |
193 |
|
// stop the engine, so we can safely modify the engine's |
194 |
|
// data structures from this foreign thread |
195 |
|
DisableAndLock(); |
196 |
|
// we could also use the respective class member variable here, |
197 |
|
// but this is probably safer and cleaner |
198 |
|
int iPendingStreamDeletions = 0; |
199 |
|
// kill all voices on all engine channels the *die hard* way |
200 |
|
for (int iChannel = 0; iChannel < engineChannels.size(); iChannel++) { |
201 |
|
EngineChannel* pEngineChannel = engineChannels[iChannel]; |
202 |
|
RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first(); |
203 |
|
RTList<uint>::Iterator end = pEngineChannel->pActiveKeys->end(); |
204 |
|
for (; iuiKey != end; ++iuiKey) { // iterate through all active keys |
205 |
|
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey]; |
206 |
|
RTList<Voice>::Iterator itVoice = pKey->pActiveVoices->first(); |
207 |
|
RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end(); |
208 |
|
for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key |
209 |
|
// request a notification from disk thread side for stream deletion |
210 |
|
const Stream::Handle hStream = itVoice->KillImmediately(true); |
211 |
|
if (hStream != Stream::INVALID_HANDLE) { // voice actually used a stream |
212 |
|
iPendingStreamDeletions++; |
213 |
|
} |
214 |
|
} |
215 |
|
} |
216 |
|
} |
217 |
|
// wait until all streams were actually deleted by the disk thread |
218 |
|
while (iPendingStreamDeletions) { |
219 |
|
while ( |
220 |
|
iPendingStreamDeletions && |
221 |
|
pDiskThread->AskForDeletedStream() != Stream::INVALID_HANDLE |
222 |
|
) iPendingStreamDeletions--; |
223 |
|
if (!iPendingStreamDeletions) break; |
224 |
|
usleep(10000); // sleep for 10ms |
225 |
|
} |
226 |
|
dmsg(2,("gig::Engine: Everything suspended.\n")); |
227 |
|
} |
228 |
|
|
229 |
|
/** |
230 |
|
* At the moment same as calling @c Enable() directly, but this might |
231 |
|
* change in future, so better call this method as counterpart to |
232 |
|
* @c SuspendAll() instead of @c Enable() ! |
233 |
|
*/ |
234 |
|
void Engine::ResumeAll() { |
235 |
|
Enable(); |
236 |
|
} |
237 |
|
|
238 |
|
/** |
239 |
|
* Order the engine to stop rendering audio for the given region. |
240 |
|
* Additionally this method will block until all voices and their disk |
241 |
|
* streams associated with that region are actually killed / deleted, so |
242 |
|
* one can i.e. safely modify the region with an instrument editor after |
243 |
|
* returning from this method. |
244 |
|
* |
245 |
|
* @param pRegion - region the engine shall stop using |
246 |
|
*/ |
247 |
|
void Engine::Suspend(::gig::Region* pRegion) { |
248 |
|
dmsg(2,("gig::Engine: Suspending Region %x ...\n",pRegion)); |
249 |
|
SuspendedRegionsMutex.Lock(); |
250 |
|
SuspensionChangeOngoing.Set(true); |
251 |
|
pPendingRegionSuspension = pRegion; |
252 |
|
SuspensionChangeOngoing.WaitAndUnlockIf(true); |
253 |
|
SuspendedRegionsMutex.Unlock(); |
254 |
|
dmsg(2,("gig::Engine: Region %x suspended.",pRegion)); |
255 |
|
} |
256 |
|
|
257 |
|
/** |
258 |
|
* Orders the engine to resume playing back the given region, previously |
259 |
|
* suspended with @c Suspend() . |
260 |
|
* |
261 |
|
* @param pRegion - region the engine shall be allowed to use again |
262 |
|
*/ |
263 |
|
void Engine::Resume(::gig::Region* pRegion) { |
264 |
|
dmsg(2,("gig::Engine: Resuming Region %x ...\n",pRegion)); |
265 |
|
SuspendedRegionsMutex.Lock(); |
266 |
|
SuspensionChangeOngoing.Set(true); |
267 |
|
pPendingRegionResumption = pRegion; |
268 |
|
SuspensionChangeOngoing.WaitAndUnlockIf(true); |
269 |
|
SuspendedRegionsMutex.Unlock(); |
270 |
|
dmsg(2,("gig::Engine: Region %x resumed.\n",pRegion)); |
271 |
|
} |
272 |
|
|
273 |
|
/** |
274 |
* Reset all voices and disk thread and clear input event queue and all |
* Reset all voices and disk thread and clear input event queue and all |
275 |
* control and status variables. |
* control and status variables. |
276 |
*/ |
*/ |
283 |
|
|
284 |
/** |
/** |
285 |
* Reset all voices and disk thread and clear input event queue and all |
* Reset all voices and disk thread and clear input event queue and all |
286 |
* control and status variables. This method is not thread safe! |
* control and status variables. This method is protected by a mutex. |
287 |
*/ |
*/ |
288 |
void Engine::ResetInternal() { |
void Engine::ResetInternal() { |
289 |
|
ResetInternalMutex.Lock(); |
290 |
|
|
291 |
|
// make sure that the engine does not get any sysex messages |
292 |
|
// while it's reseting |
293 |
|
bool sysexDisabled = MidiInputPort::RemoveSysexListener(this); |
294 |
ActiveVoiceCount = 0; |
ActiveVoiceCount = 0; |
295 |
ActiveVoiceCountMax = 0; |
ActiveVoiceCountMax = 0; |
296 |
|
|
313 |
|
|
314 |
// delete all input events |
// delete all input events |
315 |
pEventQueue->init(); |
pEventQueue->init(); |
316 |
|
pSysexBuffer->init(); |
317 |
|
if (sysexDisabled) MidiInputPort::AddSysexListener(this); |
318 |
|
ResetInternalMutex.Unlock(); |
319 |
} |
} |
320 |
|
|
321 |
/** |
/** |
325 |
memset(&ScaleTuning[0], 0x00, 12); |
memset(&ScaleTuning[0], 0x00, 12); |
326 |
} |
} |
327 |
|
|
328 |
|
void Engine::ResetSuspendedRegions() { |
329 |
|
SuspendedRegions.clear(); |
330 |
|
iPendingStreamDeletions = 0; |
331 |
|
pPendingRegionSuspension = pPendingRegionResumption = NULL; |
332 |
|
SuspensionChangeOngoing.Set(false); |
333 |
|
} |
334 |
|
|
335 |
/** |
/** |
336 |
* Connect this engine instance with the given audio output device. |
* Connect this engine instance with the given audio output device. |
337 |
* This method will be called when an Engine instance is created. |
* This method will be called when an Engine instance is created. |
352 |
} |
} |
353 |
catch (AudioOutputException e) { |
catch (AudioOutputException e) { |
354 |
String msg = "Audio output device unable to provide 2 audio channels, cause: " + e.Message(); |
String msg = "Audio output device unable to provide 2 audio channels, cause: " + e.Message(); |
355 |
throw LinuxSamplerException(msg); |
throw Exception(msg); |
356 |
} |
} |
357 |
|
|
358 |
this->MaxSamplesPerCycle = pAudioOutputDevice->MaxSamplesPerCycle(); |
this->MaxSamplesPerCycle = pAudioOutputDevice->MaxSamplesPerCycle(); |
359 |
this->SampleRate = pAudioOutputDevice->SampleRate(); |
this->SampleRate = pAudioOutputDevice->SampleRate(); |
360 |
|
|
361 |
// FIXME: audio drivers with varying fragment sizes might be a problem here |
MinFadeOutSamples = int(double(SampleRate) * CONFIG_EG_MIN_RELEASE_TIME) - 1; |
362 |
MaxFadeOutPos = MaxSamplesPerCycle - int(double(SampleRate) * CONFIG_EG_MIN_RELEASE_TIME) - 1; |
if (MaxSamplesPerCycle < MinFadeOutSamples) { |
|
if (MaxFadeOutPos < 0) { |
|
363 |
std::cerr << "gig::Engine: WARNING, CONFIG_EG_MIN_RELEASE_TIME " |
std::cerr << "gig::Engine: WARNING, CONFIG_EG_MIN_RELEASE_TIME " |
364 |
<< "too big for current audio fragment size & sampling rate! " |
<< "too big for current audio fragment size & sampling rate! " |
365 |
<< "May lead to click sounds if voice stealing chimes in!\n" << std::flush; |
<< "May lead to click sounds if voice stealing chimes in!\n" << std::flush; |
366 |
// force volume ramp downs at the beginning of each fragment |
// force volume ramp downs at the beginning of each fragment |
367 |
MaxFadeOutPos = 0; |
MinFadeOutSamples = MaxSamplesPerCycle; |
368 |
// lower minimum release time |
// lower minimum release time |
369 |
const float minReleaseTime = (float) MaxSamplesPerCycle / (float) SampleRate; |
const float minReleaseTime = (float) MaxSamplesPerCycle / (float) SampleRate; |
370 |
for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) { |
for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) { |
371 |
iterVoice->pEG1->CalculateFadeOutCoeff(minReleaseTime, SampleRate); |
iterVoice->EG1.CalculateFadeOutCoeff(minReleaseTime, SampleRate); |
372 |
} |
} |
373 |
pVoicePool->clear(); |
pVoicePool->clear(); |
374 |
} |
} |
380 |
delete this->pDiskThread; |
delete this->pDiskThread; |
381 |
dmsg(1,("OK\n")); |
dmsg(1,("OK\n")); |
382 |
} |
} |
383 |
this->pDiskThread = new DiskThread(((pAudioOut->MaxSamplesPerCycle() << CONFIG_MAX_PITCH) << 1) + 6); //FIXME: assuming stereo |
this->pDiskThread = new DiskThread(((pAudioOut->MaxSamplesPerCycle() << CONFIG_MAX_PITCH) << 1) + 6, //FIXME: assuming stereo |
384 |
|
&instruments); |
385 |
if (!pDiskThread) { |
if (!pDiskThread) { |
386 |
dmsg(0,("gig::Engine new diskthread = NULL\n")); |
dmsg(0,("gig::Engine new diskthread = NULL\n")); |
387 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
397 |
if (pEventGenerator) delete pEventGenerator; |
if (pEventGenerator) delete pEventGenerator; |
398 |
pEventGenerator = new EventGenerator(pAudioOut->SampleRate()); |
pEventGenerator = new EventGenerator(pAudioOut->SampleRate()); |
399 |
|
|
|
// (re)allocate synthesis parameter matrix |
|
|
if (pSynthesisParameters[0]) free(pSynthesisParameters[0]); |
|
|
|
|
|
#if defined(__APPLE__) |
|
|
pSynthesisParameters[0] = (float *) malloc(Event::destination_count * sizeof(float) * pAudioOut->MaxSamplesPerCycle()); |
|
|
#else |
|
|
pSynthesisParameters[0] = (float *) memalign(16,(Event::destination_count * sizeof(float) * pAudioOut->MaxSamplesPerCycle())); |
|
|
#endif |
|
|
for (int dst = 1; dst < Event::destination_count; dst++) |
|
|
pSynthesisParameters[dst] = pSynthesisParameters[dst - 1] + pAudioOut->MaxSamplesPerCycle(); |
|
|
|
|
|
// (re)allocate biquad filter parameter sequence |
|
|
if (pBasicFilterParameters) delete[] pBasicFilterParameters; |
|
|
if (pMainFilterParameters) delete[] pMainFilterParameters; |
|
|
pBasicFilterParameters = new biquad_param_t[pAudioOut->MaxSamplesPerCycle()]; |
|
|
pMainFilterParameters = new biquad_param_t[pAudioOut->MaxSamplesPerCycle()]; |
|
|
|
|
400 |
dmsg(1,("Starting disk thread...")); |
dmsg(1,("Starting disk thread...")); |
401 |
pDiskThread->StartThread(); |
pDiskThread->StartThread(); |
402 |
dmsg(1,("OK\n")); |
dmsg(1,("OK\n")); |
410 |
} |
} |
411 |
|
|
412 |
/** |
/** |
413 |
|
* Called by the engine's (audio) thread once per cycle to process requests |
414 |
|
* from the outer world to suspend or resume a given @c gig::Region . |
415 |
|
*/ |
416 |
|
void Engine::ProcessSuspensionsChanges() { |
417 |
|
// process request for suspending one region |
418 |
|
if (pPendingRegionSuspension) { |
419 |
|
// kill all voices on all engine channels that use this region |
420 |
|
for (int iChannel = 0; iChannel < engineChannels.size(); iChannel++) { |
421 |
|
EngineChannel* pEngineChannel = engineChannels[iChannel]; |
422 |
|
RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first(); |
423 |
|
RTList<uint>::Iterator end = pEngineChannel->pActiveKeys->end(); |
424 |
|
for (; iuiKey != end; ++iuiKey) { // iterate through all active keys |
425 |
|
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey]; |
426 |
|
RTList<Voice>::Iterator itVoice = pKey->pActiveVoices->first(); |
427 |
|
// if current key is not associated with this region, skip this key |
428 |
|
if (itVoice->pDimRgn->GetParent() != pPendingRegionSuspension) continue; |
429 |
|
RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end(); |
430 |
|
for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key |
431 |
|
// request a notification from disk thread side for stream deletion |
432 |
|
const Stream::Handle hStream = itVoice->KillImmediately(true); |
433 |
|
if (hStream != Stream::INVALID_HANDLE) { // voice actually used a stream |
434 |
|
iPendingStreamDeletions++; |
435 |
|
} |
436 |
|
} |
437 |
|
} |
438 |
|
} |
439 |
|
// make sure the region is not yet on the list |
440 |
|
bool bAlreadySuspended = false; |
441 |
|
RTList< ::gig::Region*>::Iterator iter = SuspendedRegions.first(); |
442 |
|
RTList< ::gig::Region*>::Iterator end = SuspendedRegions.end(); |
443 |
|
for (; iter != end; ++iter) { // iterate through all suspended regions |
444 |
|
if (*iter == pPendingRegionSuspension) { // found |
445 |
|
bAlreadySuspended = true; |
446 |
|
dmsg(1,("gig::Engine: attempt to suspend an already suspended region !!!\n")); |
447 |
|
break; |
448 |
|
} |
449 |
|
} |
450 |
|
if (!bAlreadySuspended) { |
451 |
|
// put the region on the list of suspended regions |
452 |
|
RTList< ::gig::Region*>::Iterator iter = SuspendedRegions.allocAppend(); |
453 |
|
if (iter) { |
454 |
|
*iter = pPendingRegionSuspension; |
455 |
|
} else std::cerr << "gig::Engine: Could not suspend Region, list is full. This is a bug!!!\n" << std::flush; |
456 |
|
} |
457 |
|
// free request slot for next caller (and to make sure that |
458 |
|
// we're not going to process the same request in the next cycle) |
459 |
|
pPendingRegionSuspension = NULL; |
460 |
|
// if no disk stream deletions are pending, awaken other side, as |
461 |
|
// we're done in this case |
462 |
|
if (!iPendingStreamDeletions) SuspensionChangeOngoing.Set(false); |
463 |
|
} |
464 |
|
|
465 |
|
// process request for resuming one region |
466 |
|
if (pPendingRegionResumption) { |
467 |
|
// remove region from the list of suspended regions |
468 |
|
RTList< ::gig::Region*>::Iterator iter = SuspendedRegions.first(); |
469 |
|
RTList< ::gig::Region*>::Iterator end = SuspendedRegions.end(); |
470 |
|
for (; iter != end; ++iter) { // iterate through all suspended regions |
471 |
|
if (*iter == pPendingRegionResumption) { // found |
472 |
|
SuspendedRegions.free(iter); |
473 |
|
break; // done |
474 |
|
} |
475 |
|
} |
476 |
|
// free request slot for next caller |
477 |
|
pPendingRegionResumption = NULL; |
478 |
|
// awake other side as we're done |
479 |
|
SuspensionChangeOngoing.Set(false); |
480 |
|
} |
481 |
|
} |
482 |
|
|
483 |
|
/** |
484 |
|
* Called by the engine's (audio) thread once per cycle to check if |
485 |
|
* streams of voices that were killed due to suspension request have |
486 |
|
* finally really been deleted by the disk thread. |
487 |
|
*/ |
488 |
|
void Engine::ProcessPendingStreamDeletions() { |
489 |
|
if (!iPendingStreamDeletions) return; |
490 |
|
//TODO: or shall we better store a list with stream handles instead of a scalar amount of streams to be deleted? might be safer |
491 |
|
while ( |
492 |
|
iPendingStreamDeletions && |
493 |
|
pDiskThread->AskForDeletedStream() != Stream::INVALID_HANDLE |
494 |
|
) iPendingStreamDeletions--; |
495 |
|
// just for safety ... |
496 |
|
while (pDiskThread->AskForDeletedStream() != Stream::INVALID_HANDLE); |
497 |
|
// now that all disk streams are deleted, awake other side as |
498 |
|
// we're finally done with suspending the requested region |
499 |
|
if (!iPendingStreamDeletions) SuspensionChangeOngoing.Set(false); |
500 |
|
} |
501 |
|
|
502 |
|
/** |
503 |
|
* Returns @c true if the given region is currently set to be suspended |
504 |
|
* from being used, @c false otherwise. |
505 |
|
*/ |
506 |
|
bool Engine::RegionSuspended(::gig::Region* pRegion) { |
507 |
|
if (SuspendedRegions.isEmpty()) return false; |
508 |
|
//TODO: or shall we use a sorted container instead of the RTList? might be faster ... or trivial ;-) |
509 |
|
RTList< ::gig::Region*>::Iterator iter = SuspendedRegions.first(); |
510 |
|
RTList< ::gig::Region*>::Iterator end = SuspendedRegions.end(); |
511 |
|
for (; iter != end; ++iter) // iterate through all suspended regions |
512 |
|
if (*iter == pRegion) return true; |
513 |
|
return false; |
514 |
|
} |
515 |
|
|
516 |
|
/** |
517 |
* Clear all engine global event lists. |
* Clear all engine global event lists. |
518 |
*/ |
*/ |
519 |
void Engine::ClearEventLists() { |
void Engine::ClearEventLists() { |
534 |
* current audio cycle |
* current audio cycle |
535 |
*/ |
*/ |
536 |
void Engine::ImportEvents(uint Samples) { |
void Engine::ImportEvents(uint Samples) { |
537 |
RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
RingBuffer<Event,false>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
538 |
Event* pEvent; |
Event* pEvent; |
539 |
while (true) { |
while (true) { |
540 |
// get next event from input event queue |
// get next event from input event queue |
557 |
} |
} |
558 |
|
|
559 |
/** |
/** |
560 |
* Let this engine proceed to render the given amount of sample points. The |
* Let this engine proceed to render the given amount of sample points. |
561 |
* calculated audio data of all voices of this engine will be placed into |
* The engine will iterate through all engine channels and render audio |
562 |
* the engine's audio sum buffer which has to be copied and eventually be |
* for each engine channel independently. The calculated audio data of |
563 |
* converted to the appropriate value range by the audio output class (e.g. |
* all voices of each engine channel will be placed into the audio sum |
564 |
* AlsaIO or JackIO) right after. |
* buffers of the respective audio output device, connected to the |
565 |
|
* respective engine channel. |
566 |
* |
* |
567 |
* @param Samples - number of sample points to be rendered |
* @param Samples - number of sample points to be rendered |
568 |
* @returns 0 on success |
* @returns 0 on success |
569 |
*/ |
*/ |
570 |
int Engine::RenderAudio(uint Samples) { |
int Engine::RenderAudio(uint Samples) { |
571 |
dmsg(5,("RenderAudio(Samples=%d)\n", Samples)); |
dmsg(8,("RenderAudio(Samples=%d)\n", Samples)); |
572 |
|
|
573 |
// return if engine disabled |
// return if engine disabled |
574 |
if (EngineDisabled.Pop()) { |
if (EngineDisabled.Pop()) { |
576 |
return 0; |
return 0; |
577 |
} |
} |
578 |
|
|
579 |
|
// process requests for suspending / resuming regions (i.e. to avoid |
580 |
|
// crashes while these regions are modified by an instrument editor) |
581 |
|
ProcessSuspensionsChanges(); |
582 |
|
|
583 |
// update time of start and end of this audio fragment (as events' time stamps relate to this) |
// update time of start and end of this audio fragment (as events' time stamps relate to this) |
584 |
pEventGenerator->UpdateFragmentTime(Samples); |
pEventGenerator->UpdateFragmentTime(Samples); |
585 |
|
|
609 |
// reset internal voice counter (just for statistic of active voices) |
// reset internal voice counter (just for statistic of active voices) |
610 |
ActiveVoiceCountTemp = 0; |
ActiveVoiceCountTemp = 0; |
611 |
|
|
612 |
|
// handle instrument change commands |
613 |
|
bool instrumentChanged = false; |
614 |
|
for (int i = 0; i < engineChannels.size(); i++) { |
615 |
|
EngineChannel* pEngineChannel = engineChannels[i]; |
616 |
|
|
617 |
|
// as we're going to (carefully) write some status to the |
618 |
|
// synchronized struct, we cast away the const |
619 |
|
EngineChannel::instrument_change_command_t& cmd = |
620 |
|
const_cast<EngineChannel::instrument_change_command_t&>(pEngineChannel->InstrumentChangeCommandReader.Lock()); |
621 |
|
|
622 |
|
pEngineChannel->pDimRegionsInUse = cmd.pDimRegionsInUse; |
623 |
|
pEngineChannel->pDimRegionsInUse->clear(); |
624 |
|
|
625 |
|
if (cmd.bChangeInstrument) { |
626 |
|
// change instrument |
627 |
|
dmsg(5,("Engine: instrument change command received\n")); |
628 |
|
cmd.bChangeInstrument = false; |
629 |
|
pEngineChannel->pInstrument = cmd.pInstrument; |
630 |
|
instrumentChanged = true; |
631 |
|
|
632 |
|
// Iterate through all active voices and mark them as |
633 |
|
// "orphans", which means that the dimension regions |
634 |
|
// and samples they use should be released to the |
635 |
|
// instrument resource manager when the voices die. |
636 |
|
int i = 0; |
637 |
|
RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first(); |
638 |
|
RTList<uint>::Iterator end = pEngineChannel->pActiveKeys->end(); |
639 |
|
while (iuiKey != end) { // iterate through all active keys |
640 |
|
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey]; |
641 |
|
++iuiKey; |
642 |
|
|
643 |
|
RTList<Voice>::Iterator itVoice = pKey->pActiveVoices->first(); |
644 |
|
RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end(); |
645 |
|
for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key |
646 |
|
itVoice->Orphan = true; |
647 |
|
} |
648 |
|
} |
649 |
|
} |
650 |
|
} |
651 |
|
if (instrumentChanged) { |
652 |
|
//TODO: this is a lazy solution ATM and not safe in case somebody is currently editing the instrument we're currently switching to (we should store all suspended regions on instrument manager side and when switching to another instrument copy that list to the engine's local list of suspensions |
653 |
|
ResetSuspendedRegions(); |
654 |
|
} |
655 |
|
|
656 |
// handle events on all engine channels |
// handle events on all engine channels |
657 |
for (int i = 0; i < engineChannels.size(); i++) { |
for (int i = 0; i < engineChannels.size(); i++) { |
|
if (!engineChannels[i]->pInstrument) continue; // ignore if no instrument loaded |
|
658 |
ProcessEvents(engineChannels[i], Samples); |
ProcessEvents(engineChannels[i], Samples); |
659 |
} |
} |
660 |
|
|
661 |
// render all 'normal', active voices on all engine channels |
// render all 'normal', active voices on all engine channels |
662 |
for (int i = 0; i < engineChannels.size(); i++) { |
for (int i = 0; i < engineChannels.size(); i++) { |
|
if (!engineChannels[i]->pInstrument) continue; // ignore if no instrument loaded |
|
663 |
RenderActiveVoices(engineChannels[i], Samples); |
RenderActiveVoices(engineChannels[i], Samples); |
664 |
} |
} |
665 |
|
|
666 |
// now that all ordinary voices on ALL engine channels are rendered, render new stolen voices |
// now that all ordinary voices on ALL engine channels are rendered, render new stolen voices |
667 |
RenderStolenVoices(Samples); |
RenderStolenVoices(Samples); |
668 |
|
|
669 |
|
// handle audio routing for engine channels with FX sends |
670 |
|
for (int i = 0; i < engineChannels.size(); i++) { |
671 |
|
if (engineChannels[i]->fxSends.empty()) continue; // ignore if no FX sends |
672 |
|
RouteAudio(engineChannels[i], Samples); |
673 |
|
} |
674 |
|
|
675 |
// handle cleanup on all engine channels for the next audio fragment |
// handle cleanup on all engine channels for the next audio fragment |
676 |
for (int i = 0; i < engineChannels.size(); i++) { |
for (int i = 0; i < engineChannels.size(); i++) { |
|
if (!engineChannels[i]->pInstrument) continue; // ignore if no instrument loaded |
|
677 |
PostProcess(engineChannels[i]); |
PostProcess(engineChannels[i]); |
678 |
} |
} |
679 |
|
|
688 |
ActiveVoiceCount = ActiveVoiceCountTemp; |
ActiveVoiceCount = ActiveVoiceCountTemp; |
689 |
if (ActiveVoiceCount > ActiveVoiceCountMax) ActiveVoiceCountMax = ActiveVoiceCount; |
if (ActiveVoiceCount > ActiveVoiceCountMax) ActiveVoiceCountMax = ActiveVoiceCount; |
690 |
|
|
691 |
|
// in case regions were previously suspended and we killed voices |
692 |
|
// with disk streams due to that, check if those streams have finally |
693 |
|
// been deleted by the disk thread |
694 |
|
if (iPendingStreamDeletions) ProcessPendingStreamDeletions(); |
695 |
|
|
696 |
|
for (int i = 0; i < engineChannels.size(); i++) { |
697 |
|
engineChannels[i]->InstrumentChangeCommandReader.Unlock(); |
698 |
|
} |
699 |
FrameTime += Samples; |
FrameTime += Samples; |
700 |
|
|
701 |
return 0; |
return 0; |
763 |
if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted |
if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted |
764 |
#endif |
#endif |
765 |
|
|
766 |
|
uint voiceCount = 0; |
767 |
|
uint streamCount = 0; |
768 |
RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first(); |
RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first(); |
769 |
RTList<uint>::Iterator end = pEngineChannel->pActiveKeys->end(); |
RTList<uint>::Iterator end = pEngineChannel->pActiveKeys->end(); |
770 |
while (iuiKey != end) { // iterate through all active keys |
while (iuiKey != end) { // iterate through all active keys |
776 |
for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key |
for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key |
777 |
// now render current voice |
// now render current voice |
778 |
itVoice->Render(Samples); |
itVoice->Render(Samples); |
779 |
if (itVoice->IsActive()) ActiveVoiceCountTemp++; // still active |
if (itVoice->IsActive()) { // still active |
780 |
else { // voice reached end, is now inactive |
if (!itVoice->Orphan) { |
781 |
|
*(pEngineChannel->pDimRegionsInUse->allocAppend()) = itVoice->pDimRgn; |
782 |
|
} |
783 |
|
ActiveVoiceCountTemp++; |
784 |
|
voiceCount++; |
785 |
|
|
786 |
|
if (itVoice->PlaybackState == Voice::playback_state_disk) { |
787 |
|
if ((itVoice->DiskStreamRef).State == Stream::state_active) streamCount++; |
788 |
|
} |
789 |
|
} else { // voice reached end, is now inactive |
790 |
FreeVoice(pEngineChannel, itVoice); // remove voice from the list of active voices |
FreeVoice(pEngineChannel, itVoice); // remove voice from the list of active voices |
791 |
} |
} |
792 |
} |
} |
793 |
} |
} |
794 |
|
|
795 |
|
pEngineChannel->SetVoiceCount(voiceCount); |
796 |
|
pEngineChannel->SetDiskStreamCount(streamCount); |
797 |
} |
} |
798 |
|
|
799 |
/** |
/** |
814 |
RTList<Event>::Iterator end = pVoiceStealingQueue->end(); |
RTList<Event>::Iterator end = pVoiceStealingQueue->end(); |
815 |
for (; itVoiceStealEvent != end; ++itVoiceStealEvent) { |
for (; itVoiceStealEvent != end; ++itVoiceStealEvent) { |
816 |
EngineChannel* pEngineChannel = (EngineChannel*) itVoiceStealEvent->pEngineChannel; |
EngineChannel* pEngineChannel = (EngineChannel*) itVoiceStealEvent->pEngineChannel; |
817 |
|
if (!pEngineChannel->pInstrument) continue; // ignore if no instrument loaded |
818 |
Pool<Voice>::Iterator itNewVoice = |
Pool<Voice>::Iterator itNewVoice = |
819 |
LaunchVoice(pEngineChannel, itVoiceStealEvent, itVoiceStealEvent->Param.Note.Layer, itVoiceStealEvent->Param.Note.ReleaseTrigger, false, false); |
LaunchVoice(pEngineChannel, itVoiceStealEvent, itVoiceStealEvent->Param.Note.Layer, itVoiceStealEvent->Param.Note.ReleaseTrigger, false, false); |
820 |
if (itNewVoice) { |
if (itNewVoice) { |
821 |
itNewVoice->Render(Samples); |
itNewVoice->Render(Samples); |
822 |
if (itNewVoice->IsActive()) ActiveVoiceCountTemp++; // still active |
if (itNewVoice->IsActive()) { // still active |
823 |
else { // voice reached end, is now inactive |
*(pEngineChannel->pDimRegionsInUse->allocAppend()) = itNewVoice->pDimRgn; |
824 |
|
ActiveVoiceCountTemp++; |
825 |
|
pEngineChannel->SetVoiceCount(pEngineChannel->GetVoiceCount() + 1); |
826 |
|
|
827 |
|
if (itNewVoice->PlaybackState == Voice::playback_state_disk) { |
828 |
|
if (itNewVoice->DiskStreamRef.State == Stream::state_active) { |
829 |
|
pEngineChannel->SetDiskStreamCount(pEngineChannel->GetDiskStreamCount() + 1); |
830 |
|
} |
831 |
|
} |
832 |
|
} else { // voice reached end, is now inactive |
833 |
FreeVoice(pEngineChannel, itNewVoice); // remove voice from the list of active voices |
FreeVoice(pEngineChannel, itNewVoice); // remove voice from the list of active voices |
834 |
} |
} |
835 |
} |
} |
843 |
} |
} |
844 |
|
|
845 |
/** |
/** |
846 |
|
* Will be called in case the respective engine channel sports FX send |
847 |
|
* channels. In this particular case, engine channel local buffers are |
848 |
|
* used to render and mix all voices to. This method is responsible for |
849 |
|
* copying the audio data from those local buffers to the master audio |
850 |
|
* output channels as well as to the FX send audio output channels with |
851 |
|
* their respective FX send levels. |
852 |
|
* |
853 |
|
* @param pEngineChannel - engine channel from which audio should be |
854 |
|
* routed |
855 |
|
* @param Samples - amount of sample points to be routed in |
856 |
|
* this audio fragment cycle |
857 |
|
*/ |
858 |
|
void Engine::RouteAudio(EngineChannel* pEngineChannel, uint Samples) { |
859 |
|
// route dry signal |
860 |
|
{ |
861 |
|
AudioChannel* pDstL = pAudioOutputDevice->Channel(pEngineChannel->AudioDeviceChannelLeft); |
862 |
|
AudioChannel* pDstR = pAudioOutputDevice->Channel(pEngineChannel->AudioDeviceChannelRight); |
863 |
|
pEngineChannel->pChannelLeft->MixTo(pDstL, Samples); |
864 |
|
pEngineChannel->pChannelRight->MixTo(pDstR, Samples); |
865 |
|
} |
866 |
|
// route FX send signal |
867 |
|
{ |
868 |
|
for (int iFxSend = 0; iFxSend < pEngineChannel->GetFxSendCount(); iFxSend++) { |
869 |
|
FxSend* pFxSend = pEngineChannel->GetFxSend(iFxSend); |
870 |
|
for (int iChan = 0; iChan < 2; ++iChan) { |
871 |
|
AudioChannel* pSource = |
872 |
|
(iChan) |
873 |
|
? pEngineChannel->pChannelRight |
874 |
|
: pEngineChannel->pChannelLeft; |
875 |
|
const int iDstChan = pFxSend->DestinationChannel(iChan); |
876 |
|
if (iDstChan < 0) { |
877 |
|
dmsg(1,("Engine::RouteAudio() Error: invalid FX send (%s) destination channel (%d->%d)", ((iChan) ? "R" : "L"), iChan, iDstChan)); |
878 |
|
goto channel_cleanup; |
879 |
|
} |
880 |
|
AudioChannel* pDstChan = NULL; |
881 |
|
if (pFxSend->DestinationMasterEffectChain() >= 0) { // fx send routed to an internal master effect |
882 |
|
EffectChain* pEffectChain = |
883 |
|
pAudioOutputDevice->MasterEffectChain( |
884 |
|
pFxSend->DestinationMasterEffectChain() |
885 |
|
); |
886 |
|
if (!pEffectChain) { |
887 |
|
dmsg(1,("Engine::RouteAudio() Error: invalid FX send (%s) destination effect chain %d", ((iChan) ? "R" : "L"), pFxSend->DestinationMasterEffectChain())); |
888 |
|
goto channel_cleanup; |
889 |
|
} |
890 |
|
Effect* pEffect = |
891 |
|
pEffectChain->GetEffect( |
892 |
|
pFxSend->DestinationMasterEffect() |
893 |
|
); |
894 |
|
if (!pEffect) { |
895 |
|
dmsg(1,("Engine::RouteAudio() Error: invalid FX send (%s) destination effect %d of effect chain %d", ((iChan) ? "R" : "L"), pFxSend->DestinationMasterEffect(), pFxSend->DestinationMasterEffectChain())); |
896 |
|
goto channel_cleanup; |
897 |
|
} |
898 |
|
pDstChan = pEffect->InputChannel(iDstChan); |
899 |
|
} else { // FX send routed directly to an audio output channel |
900 |
|
pDstChan = pAudioOutputDevice->Channel(iDstChan); |
901 |
|
} |
902 |
|
if (!pDstChan) { |
903 |
|
dmsg(1,("Engine::RouteAudio() Error: invalid FX send (%s) destination channel (%d->%d)", ((iChan) ? "R" : "L"), iChan, iDstChan)); |
904 |
|
goto channel_cleanup; |
905 |
|
} |
906 |
|
pSource->MixTo(pDstChan, Samples, pFxSend->Level()); |
907 |
|
} |
908 |
|
} |
909 |
|
} |
910 |
|
channel_cleanup: |
911 |
|
// reset buffers with silence (zero out) for the next audio cycle |
912 |
|
pEngineChannel->pChannelLeft->Clear(); |
913 |
|
pEngineChannel->pChannelRight->Clear(); |
914 |
|
} |
915 |
|
|
916 |
|
/** |
917 |
* Free all keys which have turned inactive in this audio fragment, from |
* Free all keys which have turned inactive in this audio fragment, from |
918 |
* the list of active keys and clear all event lists on that engine |
* the list of active keys and clear all event lists on that engine |
919 |
* channel. |
* channel. |
953 |
* |
* |
954 |
* @param pData - pointer to sysex data |
* @param pData - pointer to sysex data |
955 |
* @param Size - lenght of sysex data (in bytes) |
* @param Size - lenght of sysex data (in bytes) |
956 |
|
* @param pSender - the MIDI input port on which the SysEx message was |
957 |
|
* received |
958 |
*/ |
*/ |
959 |
void Engine::SendSysex(void* pData, uint Size) { |
void Engine::SendSysex(void* pData, uint Size, MidiInputPort* pSender) { |
960 |
Event event = pEventGenerator->CreateEvent(); |
Event event = pEventGenerator->CreateEvent(); |
961 |
event.Type = Event::type_sysex; |
event.Type = Event::type_sysex; |
962 |
event.Param.Sysex.Size = Size; |
event.Param.Sysex.Size = Size; |
963 |
event.pEngineChannel = NULL; // as Engine global event |
event.pEngineChannel = NULL; // as Engine global event |
964 |
|
event.pMidiInputPort = pSender; |
965 |
if (pEventQueue->write_space() > 0) { |
if (pEventQueue->write_space() > 0) { |
966 |
if (pSysexBuffer->write_space() >= Size) { |
if (pSysexBuffer->write_space() >= Size) { |
967 |
// copy sysex data to input buffer |
// copy sysex data to input buffer |
993 |
if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted |
if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted |
994 |
#endif |
#endif |
995 |
|
|
996 |
|
if (!pEngineChannel->pInstrument) return; // ignore if no instrument loaded |
997 |
|
|
998 |
|
//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 |
999 |
|
itNoteOnEvent->Param.Note.Key += pEngineChannel->GlobalTranspose; |
1000 |
|
|
1001 |
const int key = itNoteOnEvent->Param.Note.Key; |
const int key = itNoteOnEvent->Param.Note.Key; |
1002 |
|
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[key]; |
1003 |
|
|
1004 |
|
// move note on event to the key's own event list |
1005 |
|
RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents); |
1006 |
|
|
1007 |
|
// if Solo Mode then kill all already active voices |
1008 |
|
if (pEngineChannel->SoloMode) { |
1009 |
|
Pool<uint>::Iterator itYoungestKey = pEngineChannel->pActiveKeys->last(); |
1010 |
|
if (itYoungestKey) { |
1011 |
|
const int iYoungestKey = *itYoungestKey; |
1012 |
|
const midi_key_info_t* pOtherKey = &pEngineChannel->pMIDIKeyInfo[iYoungestKey]; |
1013 |
|
if (pOtherKey->Active) { |
1014 |
|
// get final portamento position of currently active voice |
1015 |
|
if (pEngineChannel->PortamentoMode) { |
1016 |
|
RTList<Voice>::Iterator itVoice = pOtherKey->pActiveVoices->last(); |
1017 |
|
if (itVoice) itVoice->UpdatePortamentoPos(itNoteOnEventOnKeyList); |
1018 |
|
} |
1019 |
|
// kill all voices on the (other) key |
1020 |
|
RTList<Voice>::Iterator itVoiceToBeKilled = pOtherKey->pActiveVoices->first(); |
1021 |
|
RTList<Voice>::Iterator end = pOtherKey->pActiveVoices->end(); |
1022 |
|
for (; itVoiceToBeKilled != end; ++itVoiceToBeKilled) { |
1023 |
|
if (itVoiceToBeKilled->Type != Voice::type_release_trigger) |
1024 |
|
itVoiceToBeKilled->Kill(itNoteOnEventOnKeyList); |
1025 |
|
} |
1026 |
|
} |
1027 |
|
} |
1028 |
|
// set this key as 'currently active solo key' |
1029 |
|
pEngineChannel->SoloKey = key; |
1030 |
|
} |
1031 |
|
|
1032 |
// Change key dimension value if key is in keyswitching area |
// Change key dimension value if key is in keyswitching area |
1033 |
{ |
{ |
1034 |
const ::gig::Instrument* pInstrument = pEngineChannel->pInstrument; |
const ::gig::Instrument* pInstrument = pEngineChannel->pInstrument; |
1035 |
if (key >= pInstrument->DimensionKeyRange.low && key <= pInstrument->DimensionKeyRange.high) |
if (key >= pInstrument->DimensionKeyRange.low && key <= pInstrument->DimensionKeyRange.high) |
1036 |
pEngineChannel->CurrentKeyDimension = ((key - pInstrument->DimensionKeyRange.low) * 128) / |
pEngineChannel->CurrentKeyDimension = float(key - pInstrument->DimensionKeyRange.low) / |
1037 |
(pInstrument->DimensionKeyRange.high - pInstrument->DimensionKeyRange.low + 1); |
(pInstrument->DimensionKeyRange.high - pInstrument->DimensionKeyRange.low + 1); |
1038 |
} |
} |
1039 |
|
|
|
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[key]; |
|
|
|
|
1040 |
pKey->KeyPressed = true; // the MIDI key was now pressed down |
pKey->KeyPressed = true; // the MIDI key was now pressed down |
1041 |
pKey->Velocity = itNoteOnEvent->Param.Note.Velocity; |
pKey->Velocity = itNoteOnEventOnKeyList->Param.Note.Velocity; |
1042 |
pKey->NoteOnTime = FrameTime + itNoteOnEvent->FragmentPos(); // will be used to calculate note length |
pKey->NoteOnTime = FrameTime + itNoteOnEventOnKeyList->FragmentPos(); // will be used to calculate note length |
1043 |
|
|
1044 |
// cancel release process of voices on this key if needed |
// cancel release process of voices on this key if needed |
1045 |
if (pKey->Active && !pEngineChannel->SustainPedal) { |
if (pKey->Active && !pEngineChannel->SustainPedal) { |
1046 |
RTList<Event>::Iterator itCancelReleaseEvent = pKey->pEvents->allocAppend(); |
RTList<Event>::Iterator itCancelReleaseEvent = pKey->pEvents->allocAppend(); |
1047 |
if (itCancelReleaseEvent) { |
if (itCancelReleaseEvent) { |
1048 |
*itCancelReleaseEvent = *itNoteOnEvent; // copy event |
*itCancelReleaseEvent = *itNoteOnEventOnKeyList; // copy event |
1049 |
itCancelReleaseEvent->Type = Event::type_cancel_release; // transform event type |
itCancelReleaseEvent->Type = Event::type_cancel_release; // transform event type |
1050 |
} |
} |
1051 |
else dmsg(1,("Event pool emtpy!\n")); |
else dmsg(1,("Event pool emtpy!\n")); |
1052 |
} |
} |
1053 |
|
|
|
// move note on event to the key's own event list |
|
|
RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents); |
|
|
|
|
1054 |
// allocate and trigger new voice(s) for the key |
// allocate and trigger new voice(s) for the key |
1055 |
{ |
{ |
1056 |
// first, get total amount of required voices (dependant on amount of layers) |
// first, get total amount of required voices (dependant on amount of layers) |
1057 |
::gig::Region* pRegion = pEngineChannel->pInstrument->GetRegion(itNoteOnEventOnKeyList->Param.Note.Key); |
::gig::Region* pRegion = pEngineChannel->pInstrument->GetRegion(itNoteOnEventOnKeyList->Param.Note.Key); |
1058 |
if (pRegion) { |
if (pRegion && !RegionSuspended(pRegion)) { |
1059 |
int voicesRequired = pRegion->Layers; |
int voicesRequired = pRegion->Layers; |
1060 |
// now launch the required amount of voices |
// now launch the required amount of voices |
1061 |
for (int i = 0; i < voicesRequired; i++) |
for (int i = 0; i < voicesRequired; i++) |
1067 |
if (!pKey->Active && !pKey->VoiceTheftsQueued) |
if (!pKey->Active && !pKey->VoiceTheftsQueued) |
1068 |
pKey->pEvents->free(itNoteOnEventOnKeyList); |
pKey->pEvents->free(itNoteOnEventOnKeyList); |
1069 |
|
|
1070 |
|
if (!pEngineChannel->SoloMode || pEngineChannel->PortamentoPos < 0.0f) pEngineChannel->PortamentoPos = (float) key; |
1071 |
pKey->RoundRobinIndex++; |
pKey->RoundRobinIndex++; |
1072 |
} |
} |
1073 |
|
|
1085 |
if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted |
if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted |
1086 |
#endif |
#endif |
1087 |
|
|
1088 |
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itNoteOffEvent->Param.Note.Key]; |
//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 |
1089 |
|
itNoteOffEvent->Param.Note.Key += pEngineChannel->GlobalTranspose; |
1090 |
|
|
1091 |
|
const int iKey = itNoteOffEvent->Param.Note.Key; |
1092 |
|
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[iKey]; |
1093 |
pKey->KeyPressed = false; // the MIDI key was now released |
pKey->KeyPressed = false; // the MIDI key was now released |
1094 |
|
|
1095 |
// release voices on this key if needed |
// move event to the key's own event list |
1096 |
if (pKey->Active && !pEngineChannel->SustainPedal) { |
RTList<Event>::Iterator itNoteOffEventOnKeyList = itNoteOffEvent.moveToEndOf(pKey->pEvents); |
|
itNoteOffEvent->Type = Event::type_release; // transform event type |
|
1097 |
|
|
1098 |
// move event to the key's own event list |
bool bShouldRelease = pKey->Active && ShouldReleaseVoice(pEngineChannel, itNoteOffEventOnKeyList->Param.Note.Key); |
1099 |
RTList<Event>::Iterator itNoteOffEventOnKeyList = itNoteOffEvent.moveToEndOf(pKey->pEvents); |
|
1100 |
|
// in case Solo Mode is enabled, kill all voices on this key and respawn a voice on the highest pressed key (if any) |
1101 |
|
if (pEngineChannel->SoloMode && pEngineChannel->pInstrument) { //TODO: this feels like too much code just for handling solo mode :P |
1102 |
|
bool bOtherKeysPressed = false; |
1103 |
|
if (iKey == pEngineChannel->SoloKey) { |
1104 |
|
pEngineChannel->SoloKey = -1; |
1105 |
|
// if there's still a key pressed down, respawn a voice (group) on the highest key |
1106 |
|
for (int i = 127; i > 0; i--) { |
1107 |
|
midi_key_info_t* pOtherKey = &pEngineChannel->pMIDIKeyInfo[i]; |
1108 |
|
if (pOtherKey->KeyPressed) { |
1109 |
|
bOtherKeysPressed = true; |
1110 |
|
// make the other key the new 'currently active solo key' |
1111 |
|
pEngineChannel->SoloKey = i; |
1112 |
|
// get final portamento position of currently active voice |
1113 |
|
if (pEngineChannel->PortamentoMode) { |
1114 |
|
RTList<Voice>::Iterator itVoice = pKey->pActiveVoices->first(); |
1115 |
|
if (itVoice) itVoice->UpdatePortamentoPos(itNoteOffEventOnKeyList); |
1116 |
|
} |
1117 |
|
// create a pseudo note on event |
1118 |
|
RTList<Event>::Iterator itPseudoNoteOnEvent = pOtherKey->pEvents->allocAppend(); |
1119 |
|
if (itPseudoNoteOnEvent) { |
1120 |
|
// copy event |
1121 |
|
*itPseudoNoteOnEvent = *itNoteOffEventOnKeyList; |
1122 |
|
// transform event to a note on event |
1123 |
|
itPseudoNoteOnEvent->Type = Event::type_note_on; |
1124 |
|
itPseudoNoteOnEvent->Param.Note.Key = i; |
1125 |
|
itPseudoNoteOnEvent->Param.Note.Velocity = pOtherKey->Velocity; |
1126 |
|
// allocate and trigger new voice(s) for the other key |
1127 |
|
{ |
1128 |
|
// first, get total amount of required voices (dependant on amount of layers) |
1129 |
|
::gig::Region* pRegion = pEngineChannel->pInstrument->GetRegion(i); |
1130 |
|
if (pRegion) { |
1131 |
|
int voicesRequired = pRegion->Layers; |
1132 |
|
// now launch the required amount of voices |
1133 |
|
for (int iLayer = 0; iLayer < voicesRequired; iLayer++) |
1134 |
|
LaunchVoice(pEngineChannel, itPseudoNoteOnEvent, iLayer, false, true, false); |
1135 |
|
} |
1136 |
|
} |
1137 |
|
// if neither a voice was spawned or postponed then remove note on event from key again |
1138 |
|
if (!pOtherKey->Active && !pOtherKey->VoiceTheftsQueued) |
1139 |
|
pOtherKey->pEvents->free(itPseudoNoteOnEvent); |
1140 |
|
|
1141 |
|
} else dmsg(1,("Could not respawn voice, no free event left\n")); |
1142 |
|
break; // done |
1143 |
|
} |
1144 |
|
} |
1145 |
|
} |
1146 |
|
if (bOtherKeysPressed) { |
1147 |
|
if (pKey->Active) { // kill all voices on this key |
1148 |
|
bShouldRelease = false; // no need to release, as we kill it here |
1149 |
|
RTList<Voice>::Iterator itVoiceToBeKilled = pKey->pActiveVoices->first(); |
1150 |
|
RTList<Voice>::Iterator end = pKey->pActiveVoices->end(); |
1151 |
|
for (; itVoiceToBeKilled != end; ++itVoiceToBeKilled) { |
1152 |
|
if (itVoiceToBeKilled->Type != Voice::type_release_trigger) |
1153 |
|
itVoiceToBeKilled->Kill(itNoteOffEventOnKeyList); |
1154 |
|
} |
1155 |
|
} |
1156 |
|
} else pEngineChannel->PortamentoPos = -1.0f; |
1157 |
|
} |
1158 |
|
|
1159 |
|
// if no solo mode (the usual case) or if solo mode and no other key pressed, then release voices on this key if needed |
1160 |
|
if (bShouldRelease) { |
1161 |
|
itNoteOffEventOnKeyList->Type = Event::type_release; // transform event type |
1162 |
|
|
1163 |
// spawn release triggered voice(s) if needed |
// spawn release triggered voice(s) if needed |
1164 |
if (pKey->ReleaseTrigger) { |
if (pKey->ReleaseTrigger && pEngineChannel->pInstrument) { |
1165 |
// first, get total amount of required voices (dependant on amount of layers) |
// first, get total amount of required voices (dependant on amount of layers) |
1166 |
::gig::Region* pRegion = pEngineChannel->pInstrument->GetRegion(itNoteOffEventOnKeyList->Param.Note.Key); |
::gig::Region* pRegion = pEngineChannel->pInstrument->GetRegion(itNoteOffEventOnKeyList->Param.Note.Key); |
1167 |
if (pRegion) { |
if (pRegion) { |
1176 |
} |
} |
1177 |
pKey->ReleaseTrigger = false; |
pKey->ReleaseTrigger = false; |
1178 |
} |
} |
|
|
|
|
// if neither a voice was spawned or postponed then remove note off event from key again |
|
|
if (!pKey->Active && !pKey->VoiceTheftsQueued) |
|
|
pKey->pEvents->free(itNoteOffEventOnKeyList); |
|
1179 |
} |
} |
1180 |
|
|
1181 |
|
// if neither a voice was spawned or postponed on this key then remove note off event from key again |
1182 |
|
if (!pKey->Active && !pKey->VoiceTheftsQueued) |
1183 |
|
pKey->pEvents->free(itNoteOffEventOnKeyList); |
1184 |
} |
} |
1185 |
|
|
1186 |
/** |
/** |
1187 |
* Moves pitchbend event from the general (input) event list to the pitch |
* Moves pitchbend event from the general (input) event list to the engine |
1188 |
* event list. |
* channel's event list. It will actually processed later by the |
1189 |
|
* respective voice. |
1190 |
* |
* |
1191 |
* @param pEngineChannel - engine channel on which this event occured on |
* @param pEngineChannel - engine channel on which this event occured on |
1192 |
* @param itPitchbendEvent - absolute pitch value and time stamp of the event |
* @param itPitchbendEvent - absolute pitch value and time stamp of the event |
1193 |
*/ |
*/ |
1194 |
void Engine::ProcessPitchbend(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itPitchbendEvent) { |
void Engine::ProcessPitchbend(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itPitchbendEvent) { |
1195 |
pEngineChannel->Pitch = itPitchbendEvent->Param.Pitch.Pitch; // store current pitch value |
pEngineChannel->Pitch = itPitchbendEvent->Param.Pitch.Pitch; // store current pitch value |
|
itPitchbendEvent.moveToEndOf(pEngineChannel->pSynthesisEvents[Event::destination_vco]); |
|
1196 |
} |
} |
1197 |
|
|
1198 |
/** |
/** |
1264 |
DimValues[i] = itNoteOnEvent->Param.Note.Velocity; |
DimValues[i] = itNoteOnEvent->Param.Note.Velocity; |
1265 |
break; |
break; |
1266 |
case ::gig::dimension_channelaftertouch: |
case ::gig::dimension_channelaftertouch: |
1267 |
DimValues[i] = 0; //TODO: we currently ignore this dimension |
DimValues[i] = pEngineChannel->ControllerTable[128]; |
1268 |
break; |
break; |
1269 |
case ::gig::dimension_releasetrigger: |
case ::gig::dimension_releasetrigger: |
1270 |
VoiceType = (ReleaseTriggerVoice) ? Voice::type_release_trigger : (!iLayer) ? Voice::type_release_trigger_required : Voice::type_normal; |
VoiceType = (ReleaseTriggerVoice) ? Voice::type_release_trigger : (!iLayer) ? Voice::type_release_trigger_required : Voice::type_normal; |
1271 |
DimValues[i] = (uint) ReleaseTriggerVoice; |
DimValues[i] = (uint) ReleaseTriggerVoice; |
1272 |
break; |
break; |
1273 |
case ::gig::dimension_keyboard: |
case ::gig::dimension_keyboard: |
1274 |
DimValues[i] = (uint) pEngineChannel->CurrentKeyDimension; |
DimValues[i] = (uint) (pEngineChannel->CurrentKeyDimension * pRegion->pDimensionDefinitions[i].zones); |
1275 |
break; |
break; |
1276 |
case ::gig::dimension_roundrobin: |
case ::gig::dimension_roundrobin: |
1277 |
DimValues[i] = (uint) pEngineChannel->pMIDIKeyInfo[MIDIKey].RoundRobinIndex; // incremented for each note on |
DimValues[i] = (uint) pEngineChannel->pMIDIKeyInfo[MIDIKey].RoundRobinIndex; // incremented for each note on |
1356 |
std::cerr << "gig::Engine::LaunchVoice() Error: Unknown dimension\n" << std::flush; |
std::cerr << "gig::Engine::LaunchVoice() Error: Unknown dimension\n" << std::flush; |
1357 |
} |
} |
1358 |
} |
} |
1359 |
|
|
1360 |
|
// return if this is a release triggered voice and there is no |
1361 |
|
// releasetrigger dimension (could happen if an instrument |
1362 |
|
// change has occured between note on and off) |
1363 |
|
if (ReleaseTriggerVoice && VoiceType != Voice::type_release_trigger) return Pool<Voice>::Iterator(); |
1364 |
|
|
1365 |
::gig::DimensionRegion* pDimRgn = pRegion->GetDimensionRegionByValue(DimValues); |
::gig::DimensionRegion* pDimRgn = pRegion->GetDimensionRegionByValue(DimValues); |
1366 |
|
|
1367 |
// no need to continue if sample is silent |
// no need to continue if sample is silent |
1591 |
|
|
1592 |
uint keygroup = itVoice->KeyGroup; |
uint keygroup = itVoice->KeyGroup; |
1593 |
|
|
1594 |
|
// if the sample and dimension region belong to an |
1595 |
|
// instrument that is unloaded, tell the disk thread to |
1596 |
|
// release them |
1597 |
|
if (itVoice->Orphan) { |
1598 |
|
pDiskThread->OrderDeletionOfDimreg(itVoice->pDimRgn); |
1599 |
|
} |
1600 |
|
|
1601 |
// free the voice object |
// free the voice object |
1602 |
pVoicePool->free(itVoice); |
pVoicePool->free(itVoice); |
1603 |
|
|
1639 |
void Engine::ProcessControlChange(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itControlChangeEvent) { |
void Engine::ProcessControlChange(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itControlChangeEvent) { |
1640 |
dmsg(4,("Engine::ContinuousController cc=%d v=%d\n", itControlChangeEvent->Param.CC.Controller, itControlChangeEvent->Param.CC.Value)); |
dmsg(4,("Engine::ContinuousController cc=%d v=%d\n", itControlChangeEvent->Param.CC.Controller, itControlChangeEvent->Param.CC.Value)); |
1641 |
|
|
1642 |
|
// handle the "control triggered" MIDI rule: a control change |
1643 |
|
// event can trigger a new note on or note off event |
1644 |
|
if (pEngineChannel->pInstrument) { |
1645 |
|
|
1646 |
|
::gig::MidiRule* rule; |
1647 |
|
for (int i = 0 ; (rule = pEngineChannel->pInstrument->GetMidiRule(i)) ; i++) { |
1648 |
|
|
1649 |
|
if (::gig::MidiRuleCtrlTrigger* ctrlTrigger = |
1650 |
|
dynamic_cast< ::gig::MidiRuleCtrlTrigger*>(rule)) { |
1651 |
|
if (itControlChangeEvent->Param.CC.Controller == |
1652 |
|
ctrlTrigger->ControllerNumber) { |
1653 |
|
|
1654 |
|
uint8_t oldCCValue = pEngineChannel->ControllerTable[ |
1655 |
|
itControlChangeEvent->Param.CC.Controller]; |
1656 |
|
uint8_t newCCValue = itControlChangeEvent->Param.CC.Value; |
1657 |
|
|
1658 |
|
for (int i = 0 ; i < ctrlTrigger->Triggers ; i++) { |
1659 |
|
::gig::MidiRuleCtrlTrigger::trigger_t* pTrigger = |
1660 |
|
&ctrlTrigger->pTriggers[i]; |
1661 |
|
|
1662 |
|
// check if the controller has passed the |
1663 |
|
// trigger point in the right direction |
1664 |
|
if ((pTrigger->Descending && |
1665 |
|
oldCCValue > pTrigger->TriggerPoint && |
1666 |
|
newCCValue <= pTrigger->TriggerPoint) || |
1667 |
|
(!pTrigger->Descending && |
1668 |
|
oldCCValue < pTrigger->TriggerPoint && |
1669 |
|
newCCValue >= pTrigger->TriggerPoint)) { |
1670 |
|
|
1671 |
|
RTList<Event>::Iterator itNewEvent = pGlobalEvents->allocAppend(); |
1672 |
|
if (itNewEvent) { |
1673 |
|
*itNewEvent = *itControlChangeEvent; |
1674 |
|
itNewEvent->Param.Note.Key = pTrigger->Key; |
1675 |
|
|
1676 |
|
if (pTrigger->NoteOff || pTrigger->Velocity == 0) { |
1677 |
|
itNewEvent->Type = Event::type_note_off; |
1678 |
|
itNewEvent->Param.Note.Velocity = 100; |
1679 |
|
|
1680 |
|
ProcessNoteOff(pEngineChannel, itNewEvent); |
1681 |
|
} else { |
1682 |
|
itNewEvent->Type = Event::type_note_on; |
1683 |
|
//TODO: if Velocity is 255, the triggered velocity should |
1684 |
|
// depend on how fast the controller is moving |
1685 |
|
itNewEvent->Param.Note.Velocity = |
1686 |
|
pTrigger->Velocity == 255 ? 100 : |
1687 |
|
pTrigger->Velocity; |
1688 |
|
|
1689 |
|
ProcessNoteOn(pEngineChannel, itNewEvent); |
1690 |
|
} |
1691 |
|
} |
1692 |
|
else dmsg(1,("Event pool emtpy!\n")); |
1693 |
|
} |
1694 |
|
} |
1695 |
|
} |
1696 |
|
} |
1697 |
|
} |
1698 |
|
} |
1699 |
|
|
1700 |
// update controller value in the engine channel's controller table |
// update controller value in the engine channel's controller table |
1701 |
pEngineChannel->ControllerTable[itControlChangeEvent->Param.CC.Controller] = itControlChangeEvent->Param.CC.Value; |
pEngineChannel->ControllerTable[itControlChangeEvent->Param.CC.Controller] = itControlChangeEvent->Param.CC.Value; |
1702 |
|
|
1703 |
// move event from the unsorted event list to the control change event list |
// handle hard coded MIDI controllers |
1704 |
Pool<Event>::Iterator itControlChangeEventOnCCList = itControlChangeEvent.moveToEndOf(pEngineChannel->pCCEvents); |
switch (itControlChangeEvent->Param.CC.Controller) { |
1705 |
|
case 5: { // portamento time |
1706 |
switch (itControlChangeEventOnCCList->Param.CC.Controller) { |
pEngineChannel->PortamentoTime = (float) itControlChangeEvent->Param.CC.Value / 127.0f * (float) CONFIG_PORTAMENTO_TIME_MAX + (float) CONFIG_PORTAMENTO_TIME_MIN; |
1707 |
|
break; |
1708 |
|
} |
1709 |
|
case 6: { // data entry (currently only used for RPN controllers) |
1710 |
|
if (pEngineChannel->GetMidiRpnController() == 2) { // coarse tuning in half tones |
1711 |
|
int transpose = (int) itControlChangeEvent->Param.CC.Value - 64; |
1712 |
|
// limit to +- two octaves for now |
1713 |
|
transpose = RTMath::Min(transpose, 24); |
1714 |
|
transpose = RTMath::Max(transpose, -24); |
1715 |
|
pEngineChannel->GlobalTranspose = transpose; |
1716 |
|
// workaround, so we won't have hanging notes |
1717 |
|
ReleaseAllVoices(pEngineChannel, itControlChangeEvent); |
1718 |
|
} |
1719 |
|
// to avoid other MIDI CC #6 messages to be misenterpreted as RPN controller data |
1720 |
|
pEngineChannel->ResetMidiRpnController(); |
1721 |
|
break; |
1722 |
|
} |
1723 |
case 7: { // volume |
case 7: { // volume |
1724 |
//TODO: not sample accurate yet |
//TODO: not sample accurate yet |
1725 |
pEngineChannel->GlobalVolume = (float) itControlChangeEventOnCCList->Param.CC.Value / 127.0f; |
pEngineChannel->MidiVolume = VolumeCurve[itControlChangeEvent->Param.CC.Value]; |
1726 |
pEngineChannel->bStatusChanged = true; // engine channel status has changed, so set notify flag |
pEngineChannel->bStatusChanged = true; // engine channel status has changed, so set notify flag |
1727 |
break; |
break; |
1728 |
} |
} |
1729 |
case 10: { // panpot |
case 10: { // panpot |
1730 |
//TODO: not sample accurate yet |
//TODO: not sample accurate yet |
1731 |
const int pan = (int) itControlChangeEventOnCCList->Param.CC.Value - 64; |
pEngineChannel->GlobalPanLeft = PanCurve[128 - itControlChangeEvent->Param.CC.Value]; |
1732 |
pEngineChannel->GlobalPanLeft = 1.0f - float(RTMath::Max(pan, 0)) / 63.0f; |
pEngineChannel->GlobalPanRight = PanCurve[itControlChangeEvent->Param.CC.Value]; |
1733 |
pEngineChannel->GlobalPanRight = 1.0f - float(RTMath::Min(pan, 0)) / -64.0f; |
pEngineChannel->iLastPanRequest = itControlChangeEvent->Param.CC.Value; |
1734 |
break; |
break; |
1735 |
} |
} |
1736 |
case 64: { // sustain |
case 64: { // sustain |
1737 |
if (itControlChangeEventOnCCList->Param.CC.Value >= 64 && !pEngineChannel->SustainPedal) { |
if (itControlChangeEvent->Param.CC.Value >= 64 && !pEngineChannel->SustainPedal) { |
1738 |
dmsg(4,("PEDAL DOWN\n")); |
dmsg(4,("DAMPER (RIGHT) PEDAL DOWN\n")); |
1739 |
pEngineChannel->SustainPedal = true; |
pEngineChannel->SustainPedal = true; |
1740 |
|
|
1741 |
#if !CONFIG_PROCESS_MUTED_CHANNELS |
#if !CONFIG_PROCESS_MUTED_CHANNELS |
1749 |
if (!pKey->KeyPressed) { |
if (!pKey->KeyPressed) { |
1750 |
RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend(); |
RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend(); |
1751 |
if (itNewEvent) { |
if (itNewEvent) { |
1752 |
*itNewEvent = *itControlChangeEventOnCCList; // copy event to the key's own event list |
*itNewEvent = *itControlChangeEvent; // copy event to the key's own event list |
1753 |
itNewEvent->Type = Event::type_cancel_release; // transform event type |
itNewEvent->Type = Event::type_cancel_release; // transform event type |
1754 |
} |
} |
1755 |
else dmsg(1,("Event pool emtpy!\n")); |
else dmsg(1,("Event pool emtpy!\n")); |
1756 |
} |
} |
1757 |
} |
} |
1758 |
} |
} |
1759 |
if (itControlChangeEventOnCCList->Param.CC.Value < 64 && pEngineChannel->SustainPedal) { |
if (itControlChangeEvent->Param.CC.Value < 64 && pEngineChannel->SustainPedal) { |
1760 |
dmsg(4,("PEDAL UP\n")); |
dmsg(4,("DAMPER (RIGHT) PEDAL UP\n")); |
1761 |
pEngineChannel->SustainPedal = false; |
pEngineChannel->SustainPedal = false; |
1762 |
|
|
1763 |
#if !CONFIG_PROCESS_MUTED_CHANNELS |
#if !CONFIG_PROCESS_MUTED_CHANNELS |
1768 |
RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first(); |
RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first(); |
1769 |
for (; iuiKey; ++iuiKey) { |
for (; iuiKey; ++iuiKey) { |
1770 |
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey]; |
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey]; |
1771 |
if (!pKey->KeyPressed) { |
if (!pKey->KeyPressed && ShouldReleaseVoice(pEngineChannel, *iuiKey)) { |
1772 |
|
RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend(); |
1773 |
|
if (itNewEvent) { |
1774 |
|
*itNewEvent = *itControlChangeEvent; // copy event to the key's own event list |
1775 |
|
itNewEvent->Type = Event::type_release; // transform event type |
1776 |
|
} |
1777 |
|
else dmsg(1,("Event pool emtpy!\n")); |
1778 |
|
} |
1779 |
|
} |
1780 |
|
} |
1781 |
|
break; |
1782 |
|
} |
1783 |
|
case 65: { // portamento on / off |
1784 |
|
const bool bPortamento = itControlChangeEvent->Param.CC.Value >= 64; |
1785 |
|
if (bPortamento != pEngineChannel->PortamentoMode) |
1786 |
|
KillAllVoices(pEngineChannel, itControlChangeEvent); |
1787 |
|
pEngineChannel->PortamentoMode = bPortamento; |
1788 |
|
break; |
1789 |
|
} |
1790 |
|
case 66: { // sostenuto |
1791 |
|
if (itControlChangeEvent->Param.CC.Value >= 64 && !pEngineChannel->SostenutoPedal) { |
1792 |
|
dmsg(4,("SOSTENUTO (CENTER) PEDAL DOWN\n")); |
1793 |
|
pEngineChannel->SostenutoPedal = true; |
1794 |
|
|
1795 |
|
#if !CONFIG_PROCESS_MUTED_CHANNELS |
1796 |
|
if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted |
1797 |
|
#endif |
1798 |
|
|
1799 |
|
SostenutoKeyCount = 0; |
1800 |
|
// Remeber the pressed keys |
1801 |
|
RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first(); |
1802 |
|
for (; iuiKey; ++iuiKey) { |
1803 |
|
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey]; |
1804 |
|
if (pKey->KeyPressed && SostenutoKeyCount < 128) SostenutoKeys[SostenutoKeyCount++] = *iuiKey; |
1805 |
|
} |
1806 |
|
} |
1807 |
|
if (itControlChangeEvent->Param.CC.Value < 64 && pEngineChannel->SostenutoPedal) { |
1808 |
|
dmsg(4,("SOSTENUTO (CENTER) PEDAL UP\n")); |
1809 |
|
pEngineChannel->SostenutoPedal = false; |
1810 |
|
|
1811 |
|
#if !CONFIG_PROCESS_MUTED_CHANNELS |
1812 |
|
if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted |
1813 |
|
#endif |
1814 |
|
|
1815 |
|
// release voices if the damper pedal is up and their respective key is not pressed |
1816 |
|
for (int i = 0; i < SostenutoKeyCount; i++) { |
1817 |
|
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[SostenutoKeys[i]]; |
1818 |
|
if (!pKey->KeyPressed && !pEngineChannel->SustainPedal) { |
1819 |
RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend(); |
RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend(); |
1820 |
if (itNewEvent) { |
if (itNewEvent) { |
1821 |
*itNewEvent = *itControlChangeEventOnCCList; // copy event to the key's own event list |
*itNewEvent = *itControlChangeEvent; // copy event to the key's own event list |
1822 |
itNewEvent->Type = Event::type_release; // transform event type |
itNewEvent->Type = Event::type_release; // transform event type |
1823 |
} |
} |
1824 |
else dmsg(1,("Event pool emtpy!\n")); |
else dmsg(1,("Event pool emtpy!\n")); |
1827 |
} |
} |
1828 |
break; |
break; |
1829 |
} |
} |
1830 |
|
case 100: { // RPN controller LSB |
1831 |
|
pEngineChannel->SetMidiRpnControllerLsb(itControlChangeEvent->Param.CC.Value); |
1832 |
|
break; |
1833 |
|
} |
1834 |
|
case 101: { // RPN controller MSB |
1835 |
|
pEngineChannel->SetMidiRpnControllerMsb(itControlChangeEvent->Param.CC.Value); |
1836 |
|
break; |
1837 |
|
} |
1838 |
|
|
1839 |
|
|
1840 |
// Channel Mode Messages |
// Channel Mode Messages |
1841 |
|
|
1842 |
case 120: { // all sound off |
case 120: { // all sound off |
1843 |
KillAllVoices(pEngineChannel, itControlChangeEventOnCCList); |
KillAllVoices(pEngineChannel, itControlChangeEvent); |
1844 |
break; |
break; |
1845 |
} |
} |
1846 |
case 121: { // reset all controllers |
case 121: { // reset all controllers |
1848 |
break; |
break; |
1849 |
} |
} |
1850 |
case 123: { // all notes off |
case 123: { // all notes off |
1851 |
ReleaseAllVoices(pEngineChannel, itControlChangeEventOnCCList); |
#if CONFIG_PROCESS_ALL_NOTES_OFF |
1852 |
|
ReleaseAllVoices(pEngineChannel, itControlChangeEvent); |
1853 |
|
#endif // CONFIG_PROCESS_ALL_NOTES_OFF |
1854 |
|
break; |
1855 |
|
} |
1856 |
|
case 126: { // mono mode on |
1857 |
|
if (!pEngineChannel->SoloMode) |
1858 |
|
KillAllVoices(pEngineChannel, itControlChangeEvent); |
1859 |
|
pEngineChannel->SoloMode = true; |
1860 |
|
break; |
1861 |
|
} |
1862 |
|
case 127: { // poly mode on |
1863 |
|
if (pEngineChannel->SoloMode) |
1864 |
|
KillAllVoices(pEngineChannel, itControlChangeEvent); |
1865 |
|
pEngineChannel->SoloMode = false; |
1866 |
break; |
break; |
1867 |
} |
} |
1868 |
} |
} |
1869 |
|
|
1870 |
|
// handle FX send controllers |
1871 |
|
if (!pEngineChannel->fxSends.empty()) { |
1872 |
|
for (int iFxSend = 0; iFxSend < pEngineChannel->GetFxSendCount(); iFxSend++) { |
1873 |
|
FxSend* pFxSend = pEngineChannel->GetFxSend(iFxSend); |
1874 |
|
if (pFxSend->MidiController() == itControlChangeEvent->Param.CC.Controller) { |
1875 |
|
pFxSend->SetLevel(itControlChangeEvent->Param.CC.Value); |
1876 |
|
pFxSend->SetInfoChanged(true); |
1877 |
|
} |
1878 |
|
} |
1879 |
|
} |
1880 |
} |
} |
1881 |
|
|
1882 |
/** |
/** |
1885 |
* @param itSysexEvent - sysex data size and time stamp of the sysex event |
* @param itSysexEvent - sysex data size and time stamp of the sysex event |
1886 |
*/ |
*/ |
1887 |
void Engine::ProcessSysex(Pool<Event>::Iterator& itSysexEvent) { |
void Engine::ProcessSysex(Pool<Event>::Iterator& itSysexEvent) { |
1888 |
RingBuffer<uint8_t>::NonVolatileReader reader = pSysexBuffer->get_non_volatile_reader(); |
RingBuffer<uint8_t,false>::NonVolatileReader reader = pSysexBuffer->get_non_volatile_reader(); |
1889 |
|
|
1890 |
uint8_t exclusive_status, id; |
uint8_t exclusive_status, id; |
1891 |
if (!reader.pop(&exclusive_status)) goto free_sysex_data; |
if (!reader.pop(&exclusive_status)) goto free_sysex_data; |
1893 |
if (exclusive_status != 0xF0) goto free_sysex_data; |
if (exclusive_status != 0xF0) goto free_sysex_data; |
1894 |
|
|
1895 |
switch (id) { |
switch (id) { |
1896 |
|
case 0x7f: { // (Realtime) Universal Sysex (GM Standard) |
1897 |
|
uint8_t sysex_channel, sub_id1, sub_id2, val_msb, val_lsb;; |
1898 |
|
if (!reader.pop(&sysex_channel)) goto free_sysex_data; |
1899 |
|
if (!reader.pop(&sub_id1)) goto free_sysex_data; |
1900 |
|
if (!reader.pop(&sub_id2)) goto free_sysex_data; |
1901 |
|
if (!reader.pop(&val_lsb)) goto free_sysex_data; |
1902 |
|
if (!reader.pop(&val_msb)) goto free_sysex_data; |
1903 |
|
//TODO: for now we simply ignore the sysex channel, seldom used anyway |
1904 |
|
switch (sub_id1) { |
1905 |
|
case 0x04: // Device Control |
1906 |
|
switch (sub_id2) { |
1907 |
|
case 0x01: // Master Volume |
1908 |
|
GLOBAL_VOLUME = |
1909 |
|
double((uint(val_msb)<<7) | uint(val_lsb)) / 16383.0; |
1910 |
|
break; |
1911 |
|
} |
1912 |
|
break; |
1913 |
|
} |
1914 |
|
break; |
1915 |
|
} |
1916 |
case 0x41: { // Roland |
case 0x41: { // Roland |
1917 |
dmsg(3,("Roland Sysex\n")); |
dmsg(3,("Roland Sysex\n")); |
1918 |
uint8_t device_id, model_id, cmd_id; |
uint8_t device_id, model_id, cmd_id; |
1924 |
|
|
1925 |
// command address |
// command address |
1926 |
uint8_t addr[3]; // 2 byte addr MSB, followed by 1 byte addr LSB) |
uint8_t addr[3]; // 2 byte addr MSB, followed by 1 byte addr LSB) |
1927 |
const RingBuffer<uint8_t>::NonVolatileReader checksum_reader = reader; // so we can calculate the check sum later |
const RingBuffer<uint8_t,false>::NonVolatileReader checksum_reader = reader; // so we can calculate the check sum later |
1928 |
if (reader.read(&addr[0], 3) != 3) goto free_sysex_data; |
if (reader.read(&addr[0], 3) != 3) goto free_sysex_data; |
1929 |
if (addr[0] == 0x40 && addr[1] == 0x00) { // System Parameters |
if (addr[0] == 0x40 && addr[1] == 0x00) { // System Parameters |
1930 |
dmsg(3,("\tSystem Parameter\n")); |
dmsg(3,("\tSystem Parameter\n")); |
1949 |
dmsg(3,("\t\t\tNew scale applied.\n")); |
dmsg(3,("\t\t\tNew scale applied.\n")); |
1950 |
break; |
break; |
1951 |
} |
} |
1952 |
|
case 0x15: { // chromatic / drumkit mode |
1953 |
|
dmsg(3,("\t\tMIDI Instrument Map Switch\n")); |
1954 |
|
uint8_t part = addr[1] & 0x0f; |
1955 |
|
uint8_t map; |
1956 |
|
if (!reader.pop(&map)) goto free_sysex_data; |
1957 |
|
for (int i = 0; i < engineChannels.size(); ++i) { |
1958 |
|
EngineChannel* pEngineChannel = engineChannels[i]; |
1959 |
|
if ( |
1960 |
|
(pEngineChannel->midiChannel == part || |
1961 |
|
pEngineChannel->midiChannel == midi_chan_all) && |
1962 |
|
pEngineChannel->GetMidiInputPort() == itSysexEvent->pMidiInputPort |
1963 |
|
) { |
1964 |
|
try { |
1965 |
|
pEngineChannel->SetMidiInstrumentMap(map); |
1966 |
|
} catch (Exception e) { |
1967 |
|
dmsg(2,("\t\t\tCould not apply MIDI instrument map %d to part %d: %s\n", map, part, e.Message().c_str())); |
1968 |
|
goto free_sysex_data; |
1969 |
|
} catch (...) { |
1970 |
|
dmsg(2,("\t\t\tCould not apply MIDI instrument map %d to part %d (unknown exception)\n", map, part)); |
1971 |
|
goto free_sysex_data; |
1972 |
|
} |
1973 |
|
} |
1974 |
|
} |
1975 |
|
dmsg(3,("\t\t\tApplied MIDI instrument map %d to part %d.\n", map, part)); |
1976 |
|
break; |
1977 |
|
} |
1978 |
} |
} |
1979 |
} |
} |
1980 |
else if (addr[0] == 0x40 && (addr[1] & 0xf0) == 0x20) { // Part Parameters (2) |
else if (addr[0] == 0x40 && (addr[1] & 0xf0) == 0x20) { // Part Parameters (2) |
1997 |
* question |
* question |
1998 |
* @param DataSize - size of the GS message data (in bytes) |
* @param DataSize - size of the GS message data (in bytes) |
1999 |
*/ |
*/ |
2000 |
uint8_t Engine::GSCheckSum(const RingBuffer<uint8_t>::NonVolatileReader AddrReader, uint DataSize) { |
uint8_t Engine::GSCheckSum(const RingBuffer<uint8_t,false>::NonVolatileReader AddrReader, uint DataSize) { |
2001 |
RingBuffer<uint8_t>::NonVolatileReader reader = AddrReader; |
RingBuffer<uint8_t,false>::NonVolatileReader reader = AddrReader; |
2002 |
uint bytes = 3 /*addr*/ + DataSize; |
uint bytes = 3 /*addr*/ + DataSize; |
2003 |
uint8_t addr_and_data[bytes]; |
uint8_t addr_and_data[bytes]; |
2004 |
reader.read(&addr_and_data[0], bytes); |
reader.read(&addr_and_data[0], bytes); |
2063 |
} |
} |
2064 |
|
|
2065 |
/** |
/** |
2066 |
* Initialize the parameter sequence for the modulation destination given by |
* Determines whether the specified voice should be released. |
2067 |
* by 'dst' with the constant value given by val. |
* |
2068 |
*/ |
* @param pEngineChannel - The engine channel on which the voice should be checked |
2069 |
void Engine::ResetSynthesisParameters(Event::destination_t dst, float val) { |
* @param Key - The key number |
2070 |
int maxsamples = pAudioOutputDevice->MaxSamplesPerCycle(); |
* @returns true if the specified should be released, false otherwise. |
2071 |
float* m = &pSynthesisParameters[dst][0]; |
*/ |
2072 |
for (int i = 0; i < maxsamples; i += 4) { |
bool Engine::ShouldReleaseVoice(EngineChannel* pEngineChannel, int Key) { |
2073 |
m[i] = val; |
if (pEngineChannel->SustainPedal) return false; |
2074 |
m[i+1] = val; |
|
2075 |
m[i+2] = val; |
if (pEngineChannel->SostenutoPedal) { |
2076 |
m[i+3] = val; |
for (int i = 0; i < SostenutoKeyCount; i++) |
2077 |
|
if (Key == SostenutoKeys[i]) return false; |
2078 |
} |
} |
2079 |
|
|
2080 |
|
return true; |
2081 |
} |
} |
2082 |
|
|
2083 |
uint Engine::VoiceCount() { |
uint Engine::VoiceCount() { |
2113 |
} |
} |
2114 |
|
|
2115 |
String Engine::Description() { |
String Engine::Description() { |
2116 |
return "Gigasampler Engine"; |
return "Gigasampler Format Engine"; |
2117 |
} |
} |
2118 |
|
|
2119 |
String Engine::Version() { |
String Engine::Version() { |
2120 |
String s = "$Revision: 1.51 $"; |
String s = "$Revision: 1.95 $"; |
2121 |
return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword |
return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword |
2122 |
} |
} |
2123 |
|
|
2124 |
|
InstrumentManager* Engine::GetInstrumentManager() { |
2125 |
|
return &instruments; |
2126 |
|
} |
2127 |
|
|
2128 |
|
// static constant initializers |
2129 |
|
const Engine::FloatTable Engine::VolumeCurve(InitVolumeCurve()); |
2130 |
|
const Engine::FloatTable Engine::PanCurve(InitPanCurve()); |
2131 |
|
const Engine::FloatTable Engine::CrossfadeCurve(InitCrossfadeCurve()); |
2132 |
|
|
2133 |
|
float* Engine::InitVolumeCurve() { |
2134 |
|
// line-segment approximation |
2135 |
|
const float segments[] = { |
2136 |
|
0, 0, 2, 0.0046, 16, 0.016, 31, 0.051, 45, 0.115, 54.5, 0.2, |
2137 |
|
64.5, 0.39, 74, 0.74, 92, 1.03, 114, 1.94, 119.2, 2.2, 127, 2.2 |
2138 |
|
}; |
2139 |
|
return InitCurve(segments); |
2140 |
|
} |
2141 |
|
|
2142 |
|
float* Engine::InitPanCurve() { |
2143 |
|
// line-segment approximation |
2144 |
|
const float segments[] = { |
2145 |
|
0, 0, 1, 0, |
2146 |
|
2, 0.05, 31.5, 0.7, 51, 0.851, 74.5, 1.12, |
2147 |
|
127, 1.41, 128, 1.41 |
2148 |
|
}; |
2149 |
|
return InitCurve(segments, 129); |
2150 |
|
} |
2151 |
|
|
2152 |
|
float* Engine::InitCrossfadeCurve() { |
2153 |
|
// line-segment approximation |
2154 |
|
const float segments[] = { |
2155 |
|
0, 0, 1, 0.03, 10, 0.1, 51, 0.58, 127, 1 |
2156 |
|
}; |
2157 |
|
return InitCurve(segments); |
2158 |
|
} |
2159 |
|
|
2160 |
|
float* Engine::InitCurve(const float* segments, int size) { |
2161 |
|
float* y = new float[size]; |
2162 |
|
for (int x = 0 ; x < size ; x++) { |
2163 |
|
if (x > segments[2]) segments += 2; |
2164 |
|
y[x] = segments[1] + (x - segments[0]) * |
2165 |
|
(segments[3] - segments[1]) / (segments[2] - segments[0]); |
2166 |
|
} |
2167 |
|
return y; |
2168 |
|
} |
2169 |
|
|
2170 |
}} // namespace LinuxSampler::gig |
}} // namespace LinuxSampler::gig |