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 * |
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 * |
36 |
|
|
37 |
namespace LinuxSampler { namespace gig { |
namespace LinuxSampler { namespace gig { |
38 |
|
|
39 |
InstrumentResourceManager Engine::Instruments; |
InstrumentResourceManager Engine::instruments; |
40 |
|
|
41 |
|
std::map<AudioOutputDevice*,Engine*> Engine::engines; |
42 |
|
|
43 |
|
/** |
44 |
|
* Get a gig::Engine object for the given gig::EngineChannel and the |
45 |
|
* given AudioOutputDevice. All engine channels which are connected to |
46 |
|
* the same audio output device will use the same engine instance. This |
47 |
|
* method will be called by a gig::EngineChannel whenever it's |
48 |
|
* connecting to a audio output device. |
49 |
|
* |
50 |
|
* @param pChannel - engine channel which acquires an engine object |
51 |
|
* @param pDevice - the audio output device \a pChannel is connected to |
52 |
|
*/ |
53 |
|
Engine* Engine::AcquireEngine(LinuxSampler::gig::EngineChannel* pChannel, AudioOutputDevice* pDevice) { |
54 |
|
Engine* pEngine = NULL; |
55 |
|
// check if there's already an engine for the given audio output device |
56 |
|
if (engines.count(pDevice)) { |
57 |
|
dmsg(4,("Using existing gig::Engine.\n")); |
58 |
|
pEngine = engines[pDevice]; |
59 |
|
} else { // create a new engine (and disk thread) instance for the given audio output device |
60 |
|
dmsg(4,("Creating new gig::Engine.\n")); |
61 |
|
pEngine = new Engine; |
62 |
|
pEngine->Connect(pDevice); |
63 |
|
engines[pDevice] = pEngine; |
64 |
|
} |
65 |
|
// register engine channel to the engine instance |
66 |
|
pEngine->engineChannels.push_back(pChannel); |
67 |
|
dmsg(4,("This gig::Engine has now %d EngineChannels.\n",pEngine->engineChannels.size())); |
68 |
|
return pEngine; |
69 |
|
} |
70 |
|
|
71 |
|
/** |
72 |
|
* Once an engine channel is disconnected from an audio output device, |
73 |
|
* it wil immediately call this method to unregister itself from the |
74 |
|
* engine instance and if that engine instance is not used by any other |
75 |
|
* engine channel anymore, then that engine instance will be destroyed. |
76 |
|
* |
77 |
|
* @param pChannel - engine channel which wants to disconnect from it's |
78 |
|
* engine instance |
79 |
|
* @param pDevice - audio output device \a pChannel was connected to |
80 |
|
*/ |
81 |
|
void Engine::FreeEngine(LinuxSampler::gig::EngineChannel* pChannel, AudioOutputDevice* pDevice) { |
82 |
|
dmsg(4,("Disconnecting EngineChannel from gig::Engine.\n")); |
83 |
|
Engine* pEngine = engines[pDevice]; |
84 |
|
// unregister EngineChannel from the Engine instance |
85 |
|
pEngine->engineChannels.remove(pChannel); |
86 |
|
// if the used Engine instance is not used anymore, then destroy it |
87 |
|
if (pEngine->engineChannels.empty()) { |
88 |
|
pDevice->Disconnect(pEngine); |
89 |
|
engines.erase(pDevice); |
90 |
|
delete pEngine; |
91 |
|
dmsg(4,("Destroying gig::Engine.\n")); |
92 |
|
} |
93 |
|
else dmsg(4,("This gig::Engine has now %d EngineChannels.\n",pEngine->engineChannels.size())); |
94 |
|
} |
95 |
|
|
96 |
Engine::Engine() { |
Engine::Engine() { |
|
pRIFF = NULL; |
|
|
pGig = NULL; |
|
|
pInstrument = NULL; |
|
97 |
pAudioOutputDevice = NULL; |
pAudioOutputDevice = NULL; |
98 |
pDiskThread = NULL; |
pDiskThread = NULL; |
99 |
pEventGenerator = NULL; |
pEventGenerator = NULL; |
100 |
pSysexBuffer = new RingBuffer<uint8_t>(SYSEX_BUFFER_SIZE, 0); |
pSysexBuffer = new RingBuffer<uint8_t>(SYSEX_BUFFER_SIZE, 0); |
101 |
pEventQueue = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT, 0); |
pEventQueue = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT, 0); |
102 |
pEventPool = new Pool<Event>(MAX_EVENTS_PER_FRAGMENT); |
pEventPool = new Pool<Event>(MAX_EVENTS_PER_FRAGMENT); |
103 |
pVoicePool = new Pool<Voice>(MAX_AUDIO_VOICES); |
pVoicePool = new Pool<Voice>(MAX_AUDIO_VOICES); |
|
pActiveKeys = new Pool<uint>(128); |
|
104 |
pVoiceStealingQueue = new RTList<Event>(pEventPool); |
pVoiceStealingQueue = new RTList<Event>(pEventPool); |
105 |
pEvents = new RTList<Event>(pEventPool); |
pEvents = new RTList<Event>(pEventPool); |
106 |
pCCEvents = new RTList<Event>(pEventPool); |
pCCEvents = new RTList<Event>(pEventPool); |
107 |
|
|
108 |
for (uint i = 0; i < Event::destination_count; i++) { |
for (uint i = 0; i < Event::destination_count; i++) { |
109 |
pSynthesisEvents[i] = new RTList<Event>(pEventPool); |
pSynthesisEvents[i] = new RTList<Event>(pEventPool); |
110 |
} |
} |
|
for (uint i = 0; i < 128; i++) { |
|
|
pMIDIKeyInfo[i].pActiveVoices = new RTList<Voice>(pVoicePool); |
|
|
pMIDIKeyInfo[i].KeyPressed = false; |
|
|
pMIDIKeyInfo[i].Active = false; |
|
|
pMIDIKeyInfo[i].ReleaseTrigger = false; |
|
|
pMIDIKeyInfo[i].pEvents = new RTList<Event>(pEventPool); |
|
|
} |
|
111 |
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()) { |
112 |
iterVoice->SetEngine(this); |
iterVoice->SetEngine(this); |
113 |
} |
} |
117 |
pBasicFilterParameters = NULL; |
pBasicFilterParameters = NULL; |
118 |
pMainFilterParameters = NULL; |
pMainFilterParameters = NULL; |
119 |
|
|
|
InstrumentIdx = -1; |
|
|
InstrumentStat = -1; |
|
|
|
|
|
AudioDeviceChannelLeft = -1; |
|
|
AudioDeviceChannelRight = -1; |
|
|
|
|
120 |
ResetInternal(); |
ResetInternal(); |
121 |
} |
} |
122 |
|
|
127 |
delete pDiskThread; |
delete pDiskThread; |
128 |
dmsg(1,("OK\n")); |
dmsg(1,("OK\n")); |
129 |
} |
} |
|
|
|
|
if (pInstrument) Instruments.HandBack(pInstrument, this); |
|
|
|
|
|
if (pGig) delete pGig; |
|
|
if (pRIFF) delete pRIFF; |
|
|
for (uint i = 0; i < 128; i++) { |
|
|
if (pMIDIKeyInfo[i].pActiveVoices) delete pMIDIKeyInfo[i].pActiveVoices; |
|
|
if (pMIDIKeyInfo[i].pEvents) delete pMIDIKeyInfo[i].pEvents; |
|
|
} |
|
130 |
for (uint i = 0; i < Event::destination_count; i++) { |
for (uint i = 0; i < Event::destination_count; i++) { |
131 |
if (pSynthesisEvents[i]) delete pSynthesisEvents[i]; |
if (pSynthesisEvents[i]) delete pSynthesisEvents[i]; |
132 |
} |
} |
134 |
if (pCCEvents) delete pCCEvents; |
if (pCCEvents) delete pCCEvents; |
135 |
if (pEventQueue) delete pEventQueue; |
if (pEventQueue) delete pEventQueue; |
136 |
if (pEventPool) delete pEventPool; |
if (pEventPool) delete pEventPool; |
137 |
if (pVoicePool) { |
if (pVoicePool) { |
138 |
pVoicePool->clear(); |
pVoicePool->clear(); |
139 |
delete pVoicePool; |
delete pVoicePool; |
140 |
} |
} |
|
if (pActiveKeys) delete pActiveKeys; |
|
|
if (pSysexBuffer) delete pSysexBuffer; |
|
141 |
if (pEventGenerator) delete pEventGenerator; |
if (pEventGenerator) delete pEventGenerator; |
142 |
if (pMainFilterParameters) delete[] pMainFilterParameters; |
if (pMainFilterParameters) delete[] pMainFilterParameters; |
143 |
if (pBasicFilterParameters) delete[] pBasicFilterParameters; |
if (pBasicFilterParameters) delete[] pBasicFilterParameters; |
144 |
if (pSynthesisParameters[0]) free(pSynthesisParameters[0]); |
if (pSynthesisParameters[0]) free(pSynthesisParameters[0]); |
145 |
if (pVoiceStealingQueue) delete pVoiceStealingQueue; |
if (pVoiceStealingQueue) delete pVoiceStealingQueue; |
146 |
|
if (pSysexBuffer) delete pSysexBuffer; |
147 |
} |
} |
148 |
|
|
149 |
void Engine::Enable() { |
void Engine::Enable() { |
170 |
*/ |
*/ |
171 |
void Engine::Reset() { |
void Engine::Reset() { |
172 |
DisableAndLock(); |
DisableAndLock(); |
|
|
|
|
//if (pAudioOutputDevice->IsPlaying()) { // if already running |
|
|
/* |
|
|
// signal audio thread not to enter render part anymore |
|
|
SuspensionRequested = true; |
|
|
// sleep until wakened by audio thread |
|
|
pthread_mutex_lock(&__render_state_mutex); |
|
|
pthread_cond_wait(&__render_exit_condition, &__render_state_mutex); |
|
|
pthread_mutex_unlock(&__render_state_mutex); |
|
|
*/ |
|
|
//} |
|
|
|
|
|
//if (wasplaying) pAudioOutputDevice->Stop(); |
|
|
|
|
173 |
ResetInternal(); |
ResetInternal(); |
|
|
|
|
// signal audio thread to continue with rendering |
|
|
//SuspensionRequested = false; |
|
174 |
Enable(); |
Enable(); |
175 |
} |
} |
176 |
|
|
179 |
* control and status variables. This method is not thread safe! |
* control and status variables. This method is not thread safe! |
180 |
*/ |
*/ |
181 |
void Engine::ResetInternal() { |
void Engine::ResetInternal() { |
|
Pitch = 0; |
|
|
SustainPedal = false; |
|
182 |
ActiveVoiceCount = 0; |
ActiveVoiceCount = 0; |
183 |
ActiveVoiceCountMax = 0; |
ActiveVoiceCountMax = 0; |
|
GlobalVolume = 1.0; |
|
|
CurrentKeyDimension = 0; |
|
184 |
|
|
185 |
// reset voice stealing parameters |
// reset voice stealing parameters |
|
itLastStolenVoice = RTList<Voice>::Iterator(); |
|
|
iuiLastStolenKey = RTList<uint>::Iterator(); |
|
186 |
pVoiceStealingQueue->clear(); |
pVoiceStealingQueue->clear(); |
187 |
|
|
188 |
// reset to normal chromatic scale (means equal temper) |
// reset to normal chromatic scale (means equal temper) |
189 |
memset(&ScaleTuning[0], 0x00, 12); |
memset(&ScaleTuning[0], 0x00, 12); |
190 |
|
|
|
// set all MIDI controller values to zero |
|
|
memset(ControllerTable, 0x00, 128); |
|
|
|
|
|
// reset key info |
|
|
for (uint i = 0; i < 128; i++) { |
|
|
pMIDIKeyInfo[i].pActiveVoices->clear(); |
|
|
pMIDIKeyInfo[i].pEvents->clear(); |
|
|
pMIDIKeyInfo[i].KeyPressed = false; |
|
|
pMIDIKeyInfo[i].Active = false; |
|
|
pMIDIKeyInfo[i].ReleaseTrigger = false; |
|
|
pMIDIKeyInfo[i].itSelf = Pool<uint>::Iterator(); |
|
|
} |
|
|
|
|
|
// reset all key groups |
|
|
map<uint,uint*>::iterator iter = ActiveKeyGroups.begin(); |
|
|
for (; iter != ActiveKeyGroups.end(); iter++) iter->second = NULL; |
|
|
|
|
191 |
// reset all voices |
// reset all voices |
192 |
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()) { |
193 |
iterVoice->Reset(); |
iterVoice->Reset(); |
194 |
} |
} |
195 |
pVoicePool->clear(); |
pVoicePool->clear(); |
196 |
|
|
|
// free all active keys |
|
|
pActiveKeys->clear(); |
|
|
|
|
197 |
// reset disk thread |
// reset disk thread |
198 |
if (pDiskThread) pDiskThread->Reset(); |
if (pDiskThread) pDiskThread->Reset(); |
199 |
|
|
200 |
// delete all input events |
// delete all input events |
201 |
pEventQueue->init(); |
pEventQueue->init(); |
202 |
} |
} |
|
|
|
|
/** |
|
|
* More or less a workaround to set the instrument name, index and load |
|
|
* status variable to zero percent immediately, that is without blocking |
|
|
* the calling thread. It might be used in future for other preparations |
|
|
* as well though. |
|
|
* |
|
|
* @param FileName - file name of the Gigasampler instrument file |
|
|
* @param Instrument - index of the instrument in the .gig file |
|
|
* @see LoadInstrument() |
|
|
*/ |
|
|
void Engine::PrepareLoadInstrument(const char* FileName, uint Instrument) { |
|
|
InstrumentFile = FileName; |
|
|
InstrumentIdx = Instrument; |
|
|
InstrumentStat = 0; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Load an instrument from a .gig file. PrepareLoadInstrument() has to |
|
|
* be called first to provide the information which instrument to load. |
|
|
* This method will then actually start to load the instrument and block |
|
|
* the calling thread until loading was completed. |
|
|
* |
|
|
* @returns detailed description of the method call result |
|
|
* @see PrepareLoadInstrument() |
|
|
*/ |
|
|
void Engine::LoadInstrument() { |
|
|
|
|
|
DisableAndLock(); |
|
|
|
|
|
ResetInternal(); // reset engine |
|
|
|
|
|
// free old instrument |
|
|
if (pInstrument) { |
|
|
// give old instrument back to instrument manager |
|
|
Instruments.HandBack(pInstrument, this); |
|
|
} |
|
|
|
|
|
// delete all key groups |
|
|
ActiveKeyGroups.clear(); |
|
|
|
|
|
// request gig instrument from instrument manager |
|
|
try { |
|
|
instrument_id_t instrid; |
|
|
instrid.FileName = InstrumentFile; |
|
|
instrid.iInstrument = InstrumentIdx; |
|
|
pInstrument = Instruments.Borrow(instrid, this); |
|
|
if (!pInstrument) { |
|
|
InstrumentStat = -1; |
|
|
dmsg(1,("no instrument loaded!!!\n")); |
|
|
exit(EXIT_FAILURE); |
|
|
} |
|
|
} |
|
|
catch (RIFF::Exception e) { |
|
|
InstrumentStat = -2; |
|
|
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message; |
|
|
throw LinuxSamplerException(msg); |
|
|
} |
|
|
catch (InstrumentResourceManagerException e) { |
|
|
InstrumentStat = -3; |
|
|
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message(); |
|
|
throw LinuxSamplerException(msg); |
|
|
} |
|
|
catch (...) { |
|
|
InstrumentStat = -4; |
|
|
throw LinuxSamplerException("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file."); |
|
|
} |
|
|
|
|
|
// rebuild ActiveKeyGroups map with key groups of current instrument |
|
|
for (::gig::Region* pRegion = pInstrument->GetFirstRegion(); pRegion; pRegion = pInstrument->GetNextRegion()) |
|
|
if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL; |
|
|
|
|
|
InstrumentIdxName = pInstrument->pInfo->Name; |
|
|
InstrumentStat = 100; |
|
|
|
|
|
// inform audio driver for the need of two channels |
|
|
try { |
|
|
if (pAudioOutputDevice) 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); |
|
|
} |
|
|
|
|
|
Enable(); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Will be called by the InstrumentResourceManager when the instrument |
|
|
* we are currently using in this engine is going to be updated, so we |
|
|
* can stop playback before that happens. |
|
|
*/ |
|
|
void Engine::ResourceToBeUpdated(::gig::Instrument* pResource, void*& pUpdateArg) { |
|
|
dmsg(3,("gig::Engine: Received instrument update message.\n")); |
|
|
DisableAndLock(); |
|
|
ResetInternal(); |
|
|
this->pInstrument = NULL; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Will be called by the InstrumentResourceManager when the instrument |
|
|
* update process was completed, so we can continue with playback. |
|
|
*/ |
|
|
void Engine::ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) { |
|
|
this->pInstrument = pNewResource; //TODO: there are couple of engine parameters we should update here as well if the instrument was updated (see LoadInstrument()) |
|
|
Enable(); |
|
|
} |
|
203 |
|
|
204 |
void Engine::Connect(AudioOutputDevice* pAudioOut) { |
void Engine::Connect(AudioOutputDevice* pAudioOut) { |
205 |
pAudioOutputDevice = pAudioOut; |
pAudioOutputDevice = pAudioOut; |
214 |
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(); |
215 |
throw LinuxSamplerException(msg); |
throw LinuxSamplerException(msg); |
216 |
} |
} |
217 |
|
|
|
this->AudioDeviceChannelLeft = 0; |
|
|
this->AudioDeviceChannelRight = 1; |
|
|
this->pOutputLeft = pAudioOutputDevice->Channel(0)->Buffer(); |
|
|
this->pOutputRight = pAudioOutputDevice->Channel(1)->Buffer(); |
|
218 |
this->MaxSamplesPerCycle = pAudioOutputDevice->MaxSamplesPerCycle(); |
this->MaxSamplesPerCycle = pAudioOutputDevice->MaxSamplesPerCycle(); |
219 |
this->SampleRate = pAudioOutputDevice->SampleRate(); |
this->SampleRate = pAudioOutputDevice->SampleRate(); |
220 |
|
|
221 |
// FIXME: audio drivers with varying fragment sizes might be a problem here |
// FIXME: audio drivers with varying fragment sizes might be a problem here |
222 |
MaxFadeOutPos = MaxSamplesPerCycle - int(double(SampleRate) * EG_MIN_RELEASE_TIME) - 1; |
MaxFadeOutPos = MaxSamplesPerCycle - int(double(SampleRate) * EG_MIN_RELEASE_TIME) - 1; |
223 |
if (MaxFadeOutPos < 0) |
if (MaxFadeOutPos < 0) |
224 |
throw LinuxSamplerException("EG_MIN_RELEASE_TIME in EGADSR.h to big for current audio fragment size / sampling rate!"); |
throw LinuxSamplerException("EG_MIN_RELEASE_TIME in EGADSR.h too big for current audio fragment size / sampling rate!"); |
225 |
|
|
226 |
// (re)create disk thread |
// (re)create disk thread |
227 |
if (this->pDiskThread) { |
if (this->pDiskThread) { |
275 |
} |
} |
276 |
} |
} |
277 |
|
|
278 |
void Engine::DisconnectAudioOutputDevice() { |
void Engine::ClearEventLists() { |
279 |
if (pAudioOutputDevice) { // if clause to prevent disconnect loops |
pEvents->clear(); |
280 |
AudioOutputDevice* olddevice = pAudioOutputDevice; |
pCCEvents->clear(); |
281 |
pAudioOutputDevice = NULL; |
for (uint i = 0; i < Event::destination_count; i++) { |
282 |
olddevice->Disconnect(this); |
pSynthesisEvents[i]->clear(); |
|
AudioDeviceChannelLeft = -1; |
|
|
AudioDeviceChannelRight = -1; |
|
283 |
} |
} |
284 |
} |
} |
285 |
|
|
286 |
/** |
/** |
287 |
|
* Copy all events from the given input queue buffer to the engine's |
288 |
|
* internal event list. This will be done at the beginning of each audio |
289 |
|
* cycle (that is each RenderAudio() call) to get all events which have |
290 |
|
* to be processed in the current audio cycle. Each EngineChannel has |
291 |
|
* it's own input event queue for the common channel specific events |
292 |
|
* (like NoteOn, NoteOff and ControlChange events). Beside that, the |
293 |
|
* engine also has a input event queue for global events (usually SysEx |
294 |
|
* message). |
295 |
|
* |
296 |
|
* @param pEventQueue - input event buffer to read from |
297 |
|
* @param Samples - number of sample points to be processed in the |
298 |
|
* current audio cycle |
299 |
|
*/ |
300 |
|
void Engine::ImportEvents(RingBuffer<Event>* pEventQueue, uint Samples) { |
301 |
|
RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
302 |
|
Event* pEvent; |
303 |
|
while (true) { |
304 |
|
// get next event from input event queue |
305 |
|
if (!(pEvent = eventQueueReader.pop())) break; |
306 |
|
// if younger event reached, ignore that and all subsequent ones for now |
307 |
|
if (pEvent->FragmentPos() >= Samples) { |
308 |
|
eventQueueReader--; |
309 |
|
dmsg(2,("Younger Event, pos=%d ,Samples=%d!\n",pEvent->FragmentPos(),Samples)); |
310 |
|
pEvent->ResetFragmentPos(); |
311 |
|
break; |
312 |
|
} |
313 |
|
// copy event to internal event list |
314 |
|
if (pEvents->poolIsEmpty()) { |
315 |
|
dmsg(1,("Event pool emtpy!\n")); |
316 |
|
break; |
317 |
|
} |
318 |
|
*pEvents->allocAppend() = *pEvent; |
319 |
|
} |
320 |
|
eventQueueReader.free(); // free all copied events from input queue |
321 |
|
} |
322 |
|
|
323 |
|
/** |
324 |
* 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. The |
325 |
* calculated audio data of all voices of this engine will be placed into |
* calculated audio data of all voices of this engine will be placed into |
326 |
* the engine's audio sum buffer which has to be copied and eventually be |
* the engine's audio sum buffer which has to be copied and eventually be |
333 |
int Engine::RenderAudio(uint Samples) { |
int Engine::RenderAudio(uint Samples) { |
334 |
dmsg(5,("RenderAudio(Samples=%d)\n", Samples)); |
dmsg(5,("RenderAudio(Samples=%d)\n", Samples)); |
335 |
|
|
336 |
// return if no instrument loaded or engine disabled |
// return if engine disabled |
337 |
if (EngineDisabled.Pop()) { |
if (EngineDisabled.Pop()) { |
338 |
dmsg(5,("gig::Engine: engine disabled (val=%d)\n",EngineDisabled.GetUnsafe())); |
dmsg(5,("gig::Engine: engine disabled (val=%d)\n",EngineDisabled.GetUnsafe())); |
339 |
return 0; |
return 0; |
340 |
} |
} |
|
if (!pInstrument) { |
|
|
dmsg(5,("gig::Engine: no instrument loaded\n")); |
|
|
return 0; |
|
|
} |
|
|
|
|
341 |
|
|
342 |
// 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) |
343 |
pEventGenerator->UpdateFragmentTime(Samples); |
pEventGenerator->UpdateFragmentTime(Samples); |
344 |
|
|
345 |
|
// empty the engine's event lists for the new fragment |
346 |
|
ClearEventLists(); |
347 |
|
|
348 |
// empty the event lists for the new fragment |
// get all events from the engine's global input event queue which belong to the current fragment |
349 |
pEvents->clear(); |
// (these are usually just SysEx messages) |
350 |
pCCEvents->clear(); |
ImportEvents(this->pEventQueue, Samples); |
351 |
for (uint i = 0; i < Event::destination_count; i++) { |
|
352 |
pSynthesisEvents[i]->clear(); |
// process engine global events (these are currently only MIDI System Exclusive messages) |
353 |
|
{ |
354 |
|
RTList<Event>::Iterator itEvent = pEvents->first(); |
355 |
|
RTList<Event>::Iterator end = pEvents->end(); |
356 |
|
for (; itEvent != end; ++itEvent) { |
357 |
|
switch (itEvent->Type) { |
358 |
|
case Event::type_sysex: |
359 |
|
dmsg(5,("Engine: Sysex received\n")); |
360 |
|
ProcessSysex(itEvent); |
361 |
|
break; |
362 |
|
} |
363 |
|
} |
364 |
} |
} |
365 |
|
|
366 |
|
// reset internal voice counter (just for statistic of active voices) |
367 |
|
ActiveVoiceCountTemp = 0; |
368 |
|
|
369 |
|
// render audio for all engine channels |
370 |
|
// TODO: should we make voice stealing engine globally? unfortunately this would mean other disadvantages so I left voice stealing in the engine channel space for now |
371 |
{ |
{ |
372 |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
std::list<EngineChannel*>::iterator itChannel = engineChannels.begin(); |
373 |
RTList<uint>::Iterator end = pActiveKeys->end(); |
std::list<EngineChannel*>::iterator end = engineChannels.end(); |
374 |
for(; iuiKey != end; ++iuiKey) { |
for (; itChannel != end; itChannel++) { |
375 |
pMIDIKeyInfo[*iuiKey].pEvents->clear(); // free all events on the key |
if (!(*itChannel)->pInstrument) continue; // ignore if no instrument loaded |
376 |
|
RenderAudio(*itChannel, Samples); |
377 |
} |
} |
378 |
} |
} |
379 |
|
|
380 |
|
// just some statistics about this engine instance |
381 |
|
ActiveVoiceCount = ActiveVoiceCountTemp; |
382 |
|
if (ActiveVoiceCount > ActiveVoiceCountMax) ActiveVoiceCountMax = ActiveVoiceCount; |
383 |
|
|
384 |
// get all events from the input event queue which belong to the current fragment |
return 0; |
385 |
|
} |
386 |
|
|
387 |
|
void Engine::RenderAudio(EngineChannel* pEngineChannel, uint Samples) { |
388 |
|
// empty the engine's event lists for the new fragment |
389 |
|
ClearEventLists(); |
390 |
|
// empty the engine channel's, MIDI key specific event lists |
391 |
{ |
{ |
392 |
RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first(); |
393 |
Event* pEvent; |
RTList<uint>::Iterator end = pEngineChannel->pActiveKeys->end(); |
394 |
while (true) { |
for(; iuiKey != end; ++iuiKey) { |
395 |
// get next event from input event queue |
pEngineChannel->pMIDIKeyInfo[*iuiKey].pEvents->clear(); // free all events on the key |
|
if (!(pEvent = eventQueueReader.pop())) break; |
|
|
// if younger event reached, ignore that and all subsequent ones for now |
|
|
if (pEvent->FragmentPos() >= Samples) { |
|
|
eventQueueReader--; |
|
|
dmsg(2,("Younger Event, pos=%d ,Samples=%d!\n",pEvent->FragmentPos(),Samples)); |
|
|
pEvent->ResetFragmentPos(); |
|
|
break; |
|
|
} |
|
|
// copy event to internal event list |
|
|
if (pEvents->poolIsEmpty()) { |
|
|
dmsg(1,("Event pool emtpy!\n")); |
|
|
break; |
|
|
} |
|
|
*pEvents->allocAppend() = *pEvent; |
|
396 |
} |
} |
|
eventQueueReader.free(); // free all copied events from input queue |
|
397 |
} |
} |
398 |
|
|
399 |
|
|
400 |
|
// get all events from the engine channels's input event queue which belong to the current fragment |
401 |
|
// (these are the common events like NoteOn, NoteOff, ControlChange, etc.) |
402 |
|
ImportEvents(pEngineChannel->pEventQueue, Samples); |
403 |
|
|
404 |
|
|
405 |
// process events |
// process events |
406 |
{ |
{ |
407 |
RTList<Event>::Iterator itEvent = pEvents->first(); |
RTList<Event>::Iterator itEvent = pEvents->first(); |
410 |
switch (itEvent->Type) { |
switch (itEvent->Type) { |
411 |
case Event::type_note_on: |
case Event::type_note_on: |
412 |
dmsg(5,("Engine: Note on received\n")); |
dmsg(5,("Engine: Note on received\n")); |
413 |
ProcessNoteOn(itEvent); |
ProcessNoteOn((EngineChannel*)itEvent->pEngineChannel, itEvent); |
414 |
break; |
break; |
415 |
case Event::type_note_off: |
case Event::type_note_off: |
416 |
dmsg(5,("Engine: Note off received\n")); |
dmsg(5,("Engine: Note off received\n")); |
417 |
ProcessNoteOff(itEvent); |
ProcessNoteOff((EngineChannel*)itEvent->pEngineChannel, itEvent); |
418 |
break; |
break; |
419 |
case Event::type_control_change: |
case Event::type_control_change: |
420 |
dmsg(5,("Engine: MIDI CC received\n")); |
dmsg(5,("Engine: MIDI CC received\n")); |
421 |
ProcessControlChange(itEvent); |
ProcessControlChange((EngineChannel*)itEvent->pEngineChannel, itEvent); |
422 |
break; |
break; |
423 |
case Event::type_pitchbend: |
case Event::type_pitchbend: |
424 |
dmsg(5,("Engine: Pitchbend received\n")); |
dmsg(5,("Engine: Pitchbend received\n")); |
425 |
ProcessPitchbend(itEvent); |
ProcessPitchbend((EngineChannel*)itEvent->pEngineChannel, itEvent); |
|
break; |
|
|
case Event::type_sysex: |
|
|
dmsg(5,("Engine: Sysex received\n")); |
|
|
ProcessSysex(itEvent); |
|
426 |
break; |
break; |
427 |
} |
} |
428 |
} |
} |
429 |
} |
} |
430 |
|
|
431 |
|
|
|
int active_voices = 0; |
|
|
|
|
432 |
// render audio from all active voices |
// render audio from all active voices |
433 |
{ |
{ |
434 |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first(); |
435 |
RTList<uint>::Iterator end = pActiveKeys->end(); |
RTList<uint>::Iterator end = pEngineChannel->pActiveKeys->end(); |
436 |
while (iuiKey != end) { // iterate through all active keys |
while (iuiKey != end) { // iterate through all active keys |
437 |
midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey]; |
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey]; |
438 |
++iuiKey; |
++iuiKey; |
439 |
|
|
440 |
RTList<Voice>::Iterator itVoice = pKey->pActiveVoices->first(); |
RTList<Voice>::Iterator itVoice = pKey->pActiveVoices->first(); |
442 |
for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key |
for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key |
443 |
// now render current voice |
// now render current voice |
444 |
itVoice->Render(Samples); |
itVoice->Render(Samples); |
445 |
if (itVoice->IsActive()) active_voices++; // still active |
if (itVoice->IsActive()) ActiveVoiceCountTemp++; // still active |
446 |
else { // voice reached end, is now inactive |
else { // voice reached end, is now inactive |
447 |
FreeVoice(itVoice); // remove voice from the list of active voices |
FreeVoice(pEngineChannel, itVoice); // remove voice from the list of active voices |
448 |
} |
} |
449 |
} |
} |
450 |
} |
} |
451 |
} |
} |
452 |
|
|
453 |
|
|
454 |
// now render all postponed voices from voice stealing |
// now render all postponed voices from voice stealing |
455 |
{ |
{ |
456 |
RTList<Event>::Iterator itVoiceStealEvent = pVoiceStealingQueue->first(); |
RTList<Event>::Iterator itVoiceStealEvent = pVoiceStealingQueue->first(); |
457 |
RTList<Event>::Iterator end = pVoiceStealingQueue->end(); |
RTList<Event>::Iterator end = pVoiceStealingQueue->end(); |
458 |
for (; itVoiceStealEvent != end; ++itVoiceStealEvent) { |
for (; itVoiceStealEvent != end; ++itVoiceStealEvent) { |
459 |
Pool<Voice>::Iterator itNewVoice = LaunchVoice(itVoiceStealEvent, itVoiceStealEvent->Param.Note.Layer, itVoiceStealEvent->Param.Note.ReleaseTrigger, false); |
Pool<Voice>::Iterator itNewVoice = |
460 |
|
LaunchVoice(pEngineChannel, itVoiceStealEvent, itVoiceStealEvent->Param.Note.Layer, itVoiceStealEvent->Param.Note.ReleaseTrigger, false); |
461 |
if (itNewVoice) { |
if (itNewVoice) { |
462 |
for (; itNewVoice; itNewVoice = itNewVoice->itChildVoice) { |
for (; itNewVoice; itNewVoice = itNewVoice->itChildVoice) { |
463 |
itNewVoice->Render(Samples); |
itNewVoice->Render(Samples); |
464 |
if (itNewVoice->IsActive()) active_voices++; // still active |
if (itNewVoice->IsActive()) ActiveVoiceCountTemp++; // still active |
465 |
else { // voice reached end, is now inactive |
else { // voice reached end, is now inactive |
466 |
FreeVoice(itNewVoice); // remove voice from the list of active voices |
FreeVoice(pEngineChannel, itNewVoice); // remove voice from the list of active voices |
467 |
} |
} |
468 |
} |
} |
469 |
} |
} |
472 |
} |
} |
473 |
// reset voice stealing for the new fragment |
// reset voice stealing for the new fragment |
474 |
pVoiceStealingQueue->clear(); |
pVoiceStealingQueue->clear(); |
475 |
itLastStolenVoice = RTList<Voice>::Iterator(); |
pEngineChannel->itLastStolenVoice = RTList<Voice>::Iterator(); |
476 |
iuiLastStolenKey = RTList<uint>::Iterator(); |
pEngineChannel->iuiLastStolenKey = RTList<uint>::Iterator(); |
477 |
|
|
478 |
|
|
479 |
// free all keys which have no active voices left |
// free all keys which have no active voices left |
480 |
{ |
{ |
481 |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first(); |
482 |
RTList<uint>::Iterator end = pActiveKeys->end(); |
RTList<uint>::Iterator end = pEngineChannel->pActiveKeys->end(); |
483 |
while (iuiKey != end) { // iterate through all active keys |
while (iuiKey != end) { // iterate through all active keys |
484 |
midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey]; |
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey]; |
485 |
++iuiKey; |
++iuiKey; |
486 |
if (pKey->pActiveVoices->isEmpty()) FreeKey(pKey); |
if (pKey->pActiveVoices->isEmpty()) FreeKey(pEngineChannel, pKey); |
487 |
#if DEVMODE |
#if DEVMODE |
488 |
else { // FIXME: should be removed before the final release (purpose: just a sanity check for debugging) |
else { // FIXME: should be removed before the final release (purpose: just a sanity check for debugging) |
489 |
RTList<Voice>::Iterator itVoice = pKey->pActiveVoices->first(); |
RTList<Voice>::Iterator itVoice = pKey->pActiveVoices->first(); |
497 |
#endif // DEVMODE |
#endif // DEVMODE |
498 |
} |
} |
499 |
} |
} |
|
|
|
|
|
|
|
// write that to the disk thread class so that it can print it |
|
|
// on the console for debugging purposes |
|
|
ActiveVoiceCount = active_voices; |
|
|
if (ActiveVoiceCount > ActiveVoiceCountMax) ActiveVoiceCountMax = ActiveVoiceCount; |
|
|
|
|
|
|
|
|
return 0; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Will be called by the MIDIIn Thread to let the audio thread trigger a new |
|
|
* voice for the given key. |
|
|
* |
|
|
* @param Key - MIDI key number of the triggered key |
|
|
* @param Velocity - MIDI velocity value of the triggered key |
|
|
*/ |
|
|
void Engine::SendNoteOn(uint8_t Key, uint8_t Velocity) { |
|
|
Event event = pEventGenerator->CreateEvent(); |
|
|
event.Type = Event::type_note_on; |
|
|
event.Param.Note.Key = Key; |
|
|
event.Param.Note.Velocity = Velocity; |
|
|
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
|
|
else dmsg(1,("Engine: Input event queue full!")); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Will be called by the MIDIIn Thread to signal the audio thread to release |
|
|
* voice(s) on the given key. |
|
|
* |
|
|
* @param Key - MIDI key number of the released key |
|
|
* @param Velocity - MIDI release velocity value of the released key |
|
|
*/ |
|
|
void Engine::SendNoteOff(uint8_t Key, uint8_t Velocity) { |
|
|
Event event = pEventGenerator->CreateEvent(); |
|
|
event.Type = Event::type_note_off; |
|
|
event.Param.Note.Key = Key; |
|
|
event.Param.Note.Velocity = Velocity; |
|
|
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
|
|
else dmsg(1,("Engine: Input event queue full!")); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Will be called by the MIDIIn Thread to signal the audio thread to change |
|
|
* the pitch value for all voices. |
|
|
* |
|
|
* @param Pitch - MIDI pitch value (-8192 ... +8191) |
|
|
*/ |
|
|
void Engine::SendPitchbend(int Pitch) { |
|
|
Event event = pEventGenerator->CreateEvent(); |
|
|
event.Type = Event::type_pitchbend; |
|
|
event.Param.Pitch.Pitch = Pitch; |
|
|
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
|
|
else dmsg(1,("Engine: Input event queue full!")); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Will be called by the MIDIIn Thread to signal the audio thread that a |
|
|
* continuous controller value has changed. |
|
|
* |
|
|
* @param Controller - MIDI controller number of the occured control change |
|
|
* @param Value - value of the control change |
|
|
*/ |
|
|
void Engine::SendControlChange(uint8_t Controller, uint8_t Value) { |
|
|
Event event = pEventGenerator->CreateEvent(); |
|
|
event.Type = Event::type_control_change; |
|
|
event.Param.CC.Controller = Controller; |
|
|
event.Param.CC.Value = Value; |
|
|
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
|
|
else dmsg(1,("Engine: Input event queue full!")); |
|
500 |
} |
} |
501 |
|
|
502 |
/** |
/** |
510 |
Event event = pEventGenerator->CreateEvent(); |
Event event = pEventGenerator->CreateEvent(); |
511 |
event.Type = Event::type_sysex; |
event.Type = Event::type_sysex; |
512 |
event.Param.Sysex.Size = Size; |
event.Param.Sysex.Size = Size; |
513 |
|
event.pEngineChannel = NULL; // as Engine global event |
514 |
if (pEventQueue->write_space() > 0) { |
if (pEventQueue->write_space() > 0) { |
515 |
if (pSysexBuffer->write_space() >= Size) { |
if (pSysexBuffer->write_space() >= Size) { |
516 |
// copy sysex data to input buffer |
// copy sysex data to input buffer |
534 |
/** |
/** |
535 |
* Assigns and triggers a new voice for the respective MIDI key. |
* Assigns and triggers a new voice for the respective MIDI key. |
536 |
* |
* |
537 |
|
* @param pEngineChannel - engine channel on which this event occured on |
538 |
* @param itNoteOnEvent - key, velocity and time stamp of the event |
* @param itNoteOnEvent - key, velocity and time stamp of the event |
539 |
*/ |
*/ |
540 |
void Engine::ProcessNoteOn(Pool<Event>::Iterator& itNoteOnEvent) { |
void Engine::ProcessNoteOn(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) { |
541 |
|
|
542 |
const int key = itNoteOnEvent->Param.Note.Key; |
const int key = itNoteOnEvent->Param.Note.Key; |
543 |
|
|
544 |
// Change key dimension value if key is in keyswitching area |
// Change key dimension value if key is in keyswitching area |
545 |
if (key >= pInstrument->DimensionKeyRange.low && key <= pInstrument->DimensionKeyRange.high) |
{ |
546 |
CurrentKeyDimension = ((key - pInstrument->DimensionKeyRange.low) * 128) / |
const ::gig::Instrument* pInstrument = pEngineChannel->pInstrument; |
547 |
(pInstrument->DimensionKeyRange.high - pInstrument->DimensionKeyRange.low + 1); |
if (key >= pInstrument->DimensionKeyRange.low && key <= pInstrument->DimensionKeyRange.high) |
548 |
|
pEngineChannel->CurrentKeyDimension = ((key - pInstrument->DimensionKeyRange.low) * 128) / |
549 |
|
(pInstrument->DimensionKeyRange.high - pInstrument->DimensionKeyRange.low + 1); |
550 |
|
} |
551 |
|
|
552 |
midi_key_info_t* pKey = &pMIDIKeyInfo[key]; |
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[key]; |
553 |
|
|
554 |
pKey->KeyPressed = true; // the MIDI key was now pressed down |
pKey->KeyPressed = true; // the MIDI key was now pressed down |
555 |
|
|
556 |
// cancel release process of voices on this key if needed |
// cancel release process of voices on this key if needed |
557 |
if (pKey->Active && !SustainPedal) { |
if (pKey->Active && !pEngineChannel->SustainPedal) { |
558 |
RTList<Event>::Iterator itCancelReleaseEvent = pKey->pEvents->allocAppend(); |
RTList<Event>::Iterator itCancelReleaseEvent = pKey->pEvents->allocAppend(); |
559 |
if (itCancelReleaseEvent) { |
if (itCancelReleaseEvent) { |
560 |
*itCancelReleaseEvent = *itNoteOnEvent; // copy event |
*itCancelReleaseEvent = *itNoteOnEvent; // copy event |
567 |
RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents); |
RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents); |
568 |
|
|
569 |
// allocate and trigger a new voice for the key |
// allocate and trigger a new voice for the key |
570 |
LaunchVoice(itNoteOnEventOnKeyList, 0, false, true); |
LaunchVoice(pEngineChannel, itNoteOnEventOnKeyList, 0, false, true); |
571 |
} |
} |
572 |
|
|
573 |
/** |
/** |
576 |
* sustain pedal will be released or voice turned inactive by itself (e.g. |
* sustain pedal will be released or voice turned inactive by itself (e.g. |
577 |
* due to completion of sample playback). |
* due to completion of sample playback). |
578 |
* |
* |
579 |
|
* @param pEngineChannel - engine channel on which this event occured on |
580 |
* @param itNoteOffEvent - key, velocity and time stamp of the event |
* @param itNoteOffEvent - key, velocity and time stamp of the event |
581 |
*/ |
*/ |
582 |
void Engine::ProcessNoteOff(Pool<Event>::Iterator& itNoteOffEvent) { |
void Engine::ProcessNoteOff(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOffEvent) { |
583 |
midi_key_info_t* pKey = &pMIDIKeyInfo[itNoteOffEvent->Param.Note.Key]; |
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itNoteOffEvent->Param.Note.Key]; |
584 |
|
|
585 |
pKey->KeyPressed = false; // the MIDI key was now released |
pKey->KeyPressed = false; // the MIDI key was now released |
586 |
|
|
587 |
// release voices on this key if needed |
// release voices on this key if needed |
588 |
if (pKey->Active && !SustainPedal) { |
if (pKey->Active && !pEngineChannel->SustainPedal) { |
589 |
itNoteOffEvent->Type = Event::type_release; // transform event type |
itNoteOffEvent->Type = Event::type_release; // transform event type |
590 |
} |
} |
591 |
|
|
594 |
|
|
595 |
// spawn release triggered voice(s) if needed |
// spawn release triggered voice(s) if needed |
596 |
if (pKey->ReleaseTrigger) { |
if (pKey->ReleaseTrigger) { |
597 |
LaunchVoice(itNoteOffEventOnKeyList, 0, true, false); //FIXME: for the moment we don't perform voice stealing for release triggered samples |
LaunchVoice(pEngineChannel, itNoteOffEventOnKeyList, 0, true, false); //FIXME: for the moment we don't perform voice stealing for release triggered samples |
598 |
pKey->ReleaseTrigger = false; |
pKey->ReleaseTrigger = false; |
599 |
} |
} |
600 |
} |
} |
603 |
* Moves pitchbend event from the general (input) event list to the pitch |
* Moves pitchbend event from the general (input) event list to the pitch |
604 |
* event list. |
* event list. |
605 |
* |
* |
606 |
|
* @param pEngineChannel - engine channel on which this event occured on |
607 |
* @param itPitchbendEvent - absolute pitch value and time stamp of the event |
* @param itPitchbendEvent - absolute pitch value and time stamp of the event |
608 |
*/ |
*/ |
609 |
void Engine::ProcessPitchbend(Pool<Event>::Iterator& itPitchbendEvent) { |
void Engine::ProcessPitchbend(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itPitchbendEvent) { |
610 |
this->Pitch = itPitchbendEvent->Param.Pitch.Pitch; // store current pitch value |
pEngineChannel->Pitch = itPitchbendEvent->Param.Pitch.Pitch; // store current pitch value |
611 |
itPitchbendEvent.moveToEndOf(pSynthesisEvents[Event::destination_vco]); |
itPitchbendEvent.moveToEndOf(pSynthesisEvents[Event::destination_vco]); |
612 |
} |
} |
613 |
|
|
616 |
* called by the ProcessNoteOn() method and by the voices itself |
* called by the ProcessNoteOn() method and by the voices itself |
617 |
* (e.g. to spawn further voices on the same key for layered sounds). |
* (e.g. to spawn further voices on the same key for layered sounds). |
618 |
* |
* |
619 |
|
* @param pEngineChannel - engine channel on which this event occured on |
620 |
* @param itNoteOnEvent - key, velocity and time stamp of the event |
* @param itNoteOnEvent - key, velocity and time stamp of the event |
621 |
* @param iLayer - layer index for the new voice (optional - only |
* @param iLayer - layer index for the new voice (optional - only |
622 |
* in case of layered sounds of course) |
* in case of layered sounds of course) |
629 |
* if the voice wasn't triggered (for example when no region is |
* if the voice wasn't triggered (for example when no region is |
630 |
* defined for the given key). |
* defined for the given key). |
631 |
*/ |
*/ |
632 |
Pool<Voice>::Iterator Engine::LaunchVoice(Pool<Event>::Iterator& itNoteOnEvent, int iLayer, bool ReleaseTriggerVoice, bool VoiceStealing) { |
Pool<Voice>::Iterator Engine::LaunchVoice(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent, int iLayer, bool ReleaseTriggerVoice, bool VoiceStealing) { |
633 |
midi_key_info_t* pKey = &pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key]; |
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key]; |
634 |
|
|
635 |
// allocate a new voice for the key |
// allocate a new voice for the key |
636 |
Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend(); |
Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend(); |
637 |
if (itNewVoice) { |
if (itNewVoice) { |
638 |
// launch the new voice |
// launch the new voice |
639 |
if (itNewVoice->Trigger(itNoteOnEvent, this->Pitch, this->pInstrument, iLayer, ReleaseTriggerVoice, VoiceStealing) < 0) { |
if (itNewVoice->Trigger(pEngineChannel, itNoteOnEvent, pEngineChannel->Pitch, pEngineChannel->pInstrument, iLayer, ReleaseTriggerVoice, VoiceStealing) < 0) { |
640 |
dmsg(4,("Voice not triggered\n")); |
dmsg(4,("Voice not triggered\n")); |
641 |
pKey->pActiveVoices->free(itNewVoice); |
pKey->pActiveVoices->free(itNewVoice); |
642 |
} |
} |
643 |
else { // on success |
else { // on success |
644 |
uint** ppKeyGroup = NULL; |
uint** ppKeyGroup = NULL; |
645 |
if (itNewVoice->KeyGroup) { // if this voice / key belongs to a key group |
if (itNewVoice->KeyGroup) { // if this voice / key belongs to a key group |
646 |
ppKeyGroup = &ActiveKeyGroups[itNewVoice->KeyGroup]; |
ppKeyGroup = &pEngineChannel->ActiveKeyGroups[itNewVoice->KeyGroup]; |
647 |
if (*ppKeyGroup) { // if there's already an active key in that key group |
if (*ppKeyGroup) { // if there's already an active key in that key group |
648 |
midi_key_info_t* pOtherKey = &pMIDIKeyInfo[**ppKeyGroup]; |
midi_key_info_t* pOtherKey = &pEngineChannel->pMIDIKeyInfo[**ppKeyGroup]; |
649 |
// kill all voices on the (other) key |
// kill all voices on the (other) key |
650 |
RTList<Voice>::Iterator itVoiceToBeKilled = pOtherKey->pActiveVoices->first(); |
RTList<Voice>::Iterator itVoiceToBeKilled = pOtherKey->pActiveVoices->first(); |
651 |
RTList<Voice>::Iterator end = pOtherKey->pActiveVoices->end(); |
RTList<Voice>::Iterator end = pOtherKey->pActiveVoices->end(); |
656 |
} |
} |
657 |
if (!pKey->Active) { // mark as active key |
if (!pKey->Active) { // mark as active key |
658 |
pKey->Active = true; |
pKey->Active = true; |
659 |
pKey->itSelf = pActiveKeys->allocAppend(); |
pKey->itSelf = pEngineChannel->pActiveKeys->allocAppend(); |
660 |
*pKey->itSelf = itNoteOnEvent->Param.Note.Key; |
*pKey->itSelf = itNoteOnEvent->Param.Note.Key; |
661 |
} |
} |
662 |
if (itNewVoice->KeyGroup) { |
if (itNewVoice->KeyGroup) { |
668 |
} |
} |
669 |
else if (VoiceStealing) { |
else if (VoiceStealing) { |
670 |
// first, get total amount of required voices (dependant on amount of layers) |
// first, get total amount of required voices (dependant on amount of layers) |
671 |
::gig::Region* pRegion = pInstrument->GetRegion(itNoteOnEvent->Param.Note.Key); |
::gig::Region* pRegion = pEngineChannel->pInstrument->GetRegion(itNoteOnEvent->Param.Note.Key); |
672 |
if (!pRegion) return Pool<Voice>::Iterator(); // nothing defined for this MIDI key, so no voice needed |
if (!pRegion) return Pool<Voice>::Iterator(); // nothing defined for this MIDI key, so no voice needed |
673 |
int voicesRequired = pRegion->Layers; |
int voicesRequired = pRegion->Layers; |
674 |
|
|
675 |
// now steal the (remaining) amount of voices |
// now steal the (remaining) amount of voices |
676 |
for (int i = iLayer; i < voicesRequired; i++) |
for (int i = iLayer; i < voicesRequired; i++) |
677 |
StealVoice(itNoteOnEvent); |
StealVoice(pEngineChannel, itNoteOnEvent); |
678 |
|
|
679 |
// put note-on event into voice-stealing queue, so it will be reprocessed after killed voice died |
// put note-on event into voice-stealing queue, so it will be reprocessed after killed voice died |
680 |
RTList<Event>::Iterator itStealEvent = pVoiceStealingQueue->allocAppend(); |
RTList<Event>::Iterator itStealEvent = pVoiceStealingQueue->allocAppend(); |
695 |
* voice stealing and postpone the note-on event until the selected |
* voice stealing and postpone the note-on event until the selected |
696 |
* voice actually died. |
* voice actually died. |
697 |
* |
* |
698 |
|
* @param pEngineChannel - engine channel on which this event occured on |
699 |
* @param itNoteOnEvent - key, velocity and time stamp of the event |
* @param itNoteOnEvent - key, velocity and time stamp of the event |
700 |
*/ |
*/ |
701 |
void Engine::StealVoice(Pool<Event>::Iterator& itNoteOnEvent) { |
void Engine::StealVoice(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) { |
702 |
if (!pEventPool->poolIsEmpty()) { |
if (!pEventPool->poolIsEmpty()) { |
703 |
|
|
704 |
RTList<uint>::Iterator iuiOldestKey; |
RTList<uint>::Iterator iuiOldestKey; |
712 |
// key, or no voice left to kill there, then procceed with |
// key, or no voice left to kill there, then procceed with |
713 |
// 'oldestkey' algorithm |
// 'oldestkey' algorithm |
714 |
case voice_steal_algo_keymask: { |
case voice_steal_algo_keymask: { |
715 |
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key]; |
midi_key_info_t* pOldestKey = &pEngineChannel->pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key]; |
716 |
if (itLastStolenVoice) { |
if (pEngineChannel->itLastStolenVoice) { |
717 |
itOldestVoice = itLastStolenVoice; |
itOldestVoice = pEngineChannel->itLastStolenVoice; |
718 |
++itOldestVoice; |
++itOldestVoice; |
719 |
} |
} |
720 |
else { // no voice stolen in this audio fragment cycle yet |
else { // no voice stolen in this audio fragment cycle yet |
729 |
// try to pick the oldest voice on the oldest active key |
// try to pick the oldest voice on the oldest active key |
730 |
// (caution: must stay after 'keymask' algorithm !) |
// (caution: must stay after 'keymask' algorithm !) |
731 |
case voice_steal_algo_oldestkey: { |
case voice_steal_algo_oldestkey: { |
732 |
if (itLastStolenVoice) { |
if (pEngineChannel->itLastStolenVoice) { |
733 |
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiLastStolenKey]; |
midi_key_info_t* pOldestKey = &pEngineChannel->pMIDIKeyInfo[*pEngineChannel->iuiLastStolenKey]; |
734 |
itOldestVoice = itLastStolenVoice; |
itOldestVoice = pEngineChannel->itLastStolenVoice; |
735 |
++itOldestVoice; |
++itOldestVoice; |
736 |
if (!itOldestVoice) { |
if (!itOldestVoice) { |
737 |
iuiOldestKey = iuiLastStolenKey; |
iuiOldestKey = pEngineChannel->iuiLastStolenKey; |
738 |
++iuiOldestKey; |
++iuiOldestKey; |
739 |
if (iuiOldestKey) { |
if (iuiOldestKey) { |
740 |
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiOldestKey]; |
midi_key_info_t* pOldestKey = &pEngineChannel->pMIDIKeyInfo[*iuiOldestKey]; |
741 |
itOldestVoice = pOldestKey->pActiveVoices->first(); |
itOldestVoice = pOldestKey->pActiveVoices->first(); |
742 |
} |
} |
743 |
else { |
else { |
745 |
return; |
return; |
746 |
} |
} |
747 |
} |
} |
748 |
else iuiOldestKey = iuiLastStolenKey; |
else iuiOldestKey = pEngineChannel->iuiLastStolenKey; |
749 |
} |
} |
750 |
else { // no voice stolen in this audio fragment cycle yet |
else { // no voice stolen in this audio fragment cycle yet |
751 |
iuiOldestKey = pActiveKeys->first(); |
iuiOldestKey = pEngineChannel->pActiveKeys->first(); |
752 |
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiOldestKey]; |
midi_key_info_t* pOldestKey = &pEngineChannel->pMIDIKeyInfo[*iuiOldestKey]; |
753 |
itOldestVoice = pOldestKey->pActiveVoices->first(); |
itOldestVoice = pOldestKey->pActiveVoices->first(); |
754 |
} |
} |
755 |
break; |
break; |
769 |
// now kill the selected voice |
// now kill the selected voice |
770 |
itOldestVoice->Kill(itNoteOnEvent); |
itOldestVoice->Kill(itNoteOnEvent); |
771 |
// remember which voice on which key we stole, so we can simply proceed for the next voice stealing |
// remember which voice on which key we stole, so we can simply proceed for the next voice stealing |
772 |
this->itLastStolenVoice = itOldestVoice; |
pEngineChannel->itLastStolenVoice = itOldestVoice; |
773 |
this->iuiLastStolenKey = iuiOldestKey; |
pEngineChannel->iuiLastStolenKey = iuiOldestKey; |
774 |
} |
} |
775 |
else dmsg(1,("Event pool emtpy!\n")); |
else dmsg(1,("Event pool emtpy!\n")); |
776 |
} |
} |
781 |
* it finished to playback its sample, finished its release stage or |
* it finished to playback its sample, finished its release stage or |
782 |
* just was killed. |
* just was killed. |
783 |
* |
* |
784 |
|
* @param pEngineChannel - engine channel on which this event occured on |
785 |
* @param itVoice - points to the voice to be freed |
* @param itVoice - points to the voice to be freed |
786 |
*/ |
*/ |
787 |
void Engine::FreeVoice(Pool<Voice>::Iterator& itVoice) { |
void Engine::FreeVoice(EngineChannel* pEngineChannel, Pool<Voice>::Iterator& itVoice) { |
788 |
if (itVoice) { |
if (itVoice) { |
789 |
midi_key_info_t* pKey = &pMIDIKeyInfo[itVoice->MIDIKey]; |
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itVoice->MIDIKey]; |
790 |
|
|
791 |
uint keygroup = itVoice->KeyGroup; |
uint keygroup = itVoice->KeyGroup; |
792 |
|
|
795 |
|
|
796 |
// if no other voices left and member of a key group, remove from key group |
// if no other voices left and member of a key group, remove from key group |
797 |
if (pKey->pActiveVoices->isEmpty() && keygroup) { |
if (pKey->pActiveVoices->isEmpty() && keygroup) { |
798 |
uint** ppKeyGroup = &ActiveKeyGroups[keygroup]; |
uint** ppKeyGroup = &pEngineChannel->ActiveKeyGroups[keygroup]; |
799 |
if (*ppKeyGroup == &*pKey->itSelf) *ppKeyGroup = NULL; // remove key from key group |
if (*ppKeyGroup == &*pKey->itSelf) *ppKeyGroup = NULL; // remove key from key group |
800 |
} |
} |
801 |
} |
} |
806 |
* Called when there's no more voice left on a key, this call will |
* Called when there's no more voice left on a key, this call will |
807 |
* update the key info respectively. |
* update the key info respectively. |
808 |
* |
* |
809 |
|
* @param pEngineChannel - engine channel on which this event occured on |
810 |
* @param pKey - key which is now inactive |
* @param pKey - key which is now inactive |
811 |
*/ |
*/ |
812 |
void Engine::FreeKey(midi_key_info_t* pKey) { |
void Engine::FreeKey(EngineChannel* pEngineChannel, midi_key_info_t* pKey) { |
813 |
if (pKey->pActiveVoices->isEmpty()) { |
if (pKey->pActiveVoices->isEmpty()) { |
814 |
pKey->Active = false; |
pKey->Active = false; |
815 |
pActiveKeys->free(pKey->itSelf); // remove key from list of active keys |
pEngineChannel->pActiveKeys->free(pKey->itSelf); // remove key from list of active keys |
816 |
pKey->itSelf = RTList<uint>::Iterator(); |
pKey->itSelf = RTList<uint>::Iterator(); |
817 |
pKey->ReleaseTrigger = false; |
pKey->ReleaseTrigger = false; |
818 |
pKey->pEvents->clear(); |
pKey->pEvents->clear(); |
825 |
* Reacts on supported control change commands (e.g. pitch bend wheel, |
* Reacts on supported control change commands (e.g. pitch bend wheel, |
826 |
* modulation wheel, aftertouch). |
* modulation wheel, aftertouch). |
827 |
* |
* |
828 |
|
* @param pEngineChannel - engine channel on which this event occured on |
829 |
* @param itControlChangeEvent - controller, value and time stamp of the event |
* @param itControlChangeEvent - controller, value and time stamp of the event |
830 |
*/ |
*/ |
831 |
void Engine::ProcessControlChange(Pool<Event>::Iterator& itControlChangeEvent) { |
void Engine::ProcessControlChange(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itControlChangeEvent) { |
832 |
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)); |
833 |
|
|
834 |
switch (itControlChangeEvent->Param.CC.Controller) { |
switch (itControlChangeEvent->Param.CC.Controller) { |
835 |
case 64: { |
case 64: { |
836 |
if (itControlChangeEvent->Param.CC.Value >= 64 && !SustainPedal) { |
if (itControlChangeEvent->Param.CC.Value >= 64 && !pEngineChannel->SustainPedal) { |
837 |
dmsg(4,("PEDAL DOWN\n")); |
dmsg(4,("PEDAL DOWN\n")); |
838 |
SustainPedal = true; |
pEngineChannel->SustainPedal = true; |
839 |
|
|
840 |
// cancel release process of voices if necessary |
// cancel release process of voices if necessary |
841 |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first(); |
842 |
if (iuiKey) { |
if (iuiKey) { |
843 |
itControlChangeEvent->Type = Event::type_cancel_release; // transform event type |
itControlChangeEvent->Type = Event::type_cancel_release; // transform event type |
844 |
while (iuiKey) { |
while (iuiKey) { |
845 |
midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey]; |
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey]; |
846 |
++iuiKey; |
++iuiKey; |
847 |
if (!pKey->KeyPressed) { |
if (!pKey->KeyPressed) { |
848 |
RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend(); |
RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend(); |
852 |
} |
} |
853 |
} |
} |
854 |
} |
} |
855 |
if (itControlChangeEvent->Param.CC.Value < 64 && SustainPedal) { |
if (itControlChangeEvent->Param.CC.Value < 64 && pEngineChannel->SustainPedal) { |
856 |
dmsg(4,("PEDAL UP\n")); |
dmsg(4,("PEDAL UP\n")); |
857 |
SustainPedal = false; |
pEngineChannel->SustainPedal = false; |
858 |
|
|
859 |
// release voices if their respective key is not pressed |
// release voices if their respective key is not pressed |
860 |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first(); |
861 |
if (iuiKey) { |
if (iuiKey) { |
862 |
itControlChangeEvent->Type = Event::type_release; // transform event type |
itControlChangeEvent->Type = Event::type_release; // transform event type |
863 |
while (iuiKey) { |
while (iuiKey) { |
864 |
midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey]; |
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey]; |
865 |
++iuiKey; |
++iuiKey; |
866 |
if (!pKey->KeyPressed) { |
if (!pKey->KeyPressed) { |
867 |
RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend(); |
RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend(); |
876 |
} |
} |
877 |
|
|
878 |
// update controller value in the engine's controller table |
// update controller value in the engine's controller table |
879 |
ControllerTable[itControlChangeEvent->Param.CC.Controller] = itControlChangeEvent->Param.CC.Value; |
pEngineChannel->ControllerTable[itControlChangeEvent->Param.CC.Controller] = itControlChangeEvent->Param.CC.Value; |
880 |
|
|
881 |
// move event from the unsorted event list to the control change event list |
// move event from the unsorted event list to the control change event list |
882 |
itControlChangeEvent.moveToEndOf(pCCEvents); |
itControlChangeEvent.moveToEndOf(pCCEvents); |
978 |
m[i+2] = val; |
m[i+2] = val; |
979 |
m[i+3] = val; |
m[i+3] = val; |
980 |
} |
} |
981 |
} |
} |
|
|
|
|
float Engine::Volume() { |
|
|
return GlobalVolume; |
|
|
} |
|
|
|
|
|
void Engine::Volume(float f) { |
|
|
GlobalVolume = f; |
|
|
} |
|
|
|
|
|
uint Engine::Channels() { |
|
|
return 2; |
|
|
} |
|
|
|
|
|
void Engine::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) { |
|
|
AudioChannel* pChannel = pAudioOutputDevice->Channel(AudioDeviceChannel); |
|
|
if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel)); |
|
|
switch (EngineAudioChannel) { |
|
|
case 0: // left output channel |
|
|
pOutputLeft = pChannel->Buffer(); |
|
|
AudioDeviceChannelLeft = AudioDeviceChannel; |
|
|
break; |
|
|
case 1: // right output channel |
|
|
pOutputRight = pChannel->Buffer(); |
|
|
AudioDeviceChannelRight = AudioDeviceChannel; |
|
|
break; |
|
|
default: |
|
|
throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel)); |
|
|
} |
|
|
} |
|
|
|
|
|
int Engine::OutputChannel(uint EngineAudioChannel) { |
|
|
switch (EngineAudioChannel) { |
|
|
case 0: // left channel |
|
|
return AudioDeviceChannelLeft; |
|
|
case 1: // right channel |
|
|
return AudioDeviceChannelRight; |
|
|
default: |
|
|
throw AudioOutputException("Invalid engine audio channel " + ToString(EngineAudioChannel)); |
|
|
} |
|
|
} |
|
982 |
|
|
983 |
uint Engine::VoiceCount() { |
uint Engine::VoiceCount() { |
984 |
return ActiveVoiceCount; |
return ActiveVoiceCount; |
1012 |
return "GigEngine"; |
return "GigEngine"; |
1013 |
} |
} |
1014 |
|
|
|
String Engine::InstrumentFileName() { |
|
|
return InstrumentFile; |
|
|
} |
|
|
|
|
|
String Engine::InstrumentName() { |
|
|
return InstrumentIdxName; |
|
|
} |
|
|
|
|
|
int Engine::InstrumentIndex() { |
|
|
return InstrumentIdx; |
|
|
} |
|
|
|
|
|
int Engine::InstrumentStatus() { |
|
|
return InstrumentStat; |
|
|
} |
|
|
|
|
1015 |
String Engine::Description() { |
String Engine::Description() { |
1016 |
return "Gigasampler Engine"; |
return "Gigasampler Engine"; |
1017 |
} |
} |
1018 |
|
|
1019 |
String Engine::Version() { |
String Engine::Version() { |
1020 |
String s = "$Revision: 1.25 $"; |
String s = "$Revision: 1.27 $"; |
1021 |
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 |
1022 |
} |
} |
1023 |
|
|