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 * |
25 |
#include "DiskThread.h" |
#include "DiskThread.h" |
26 |
#include "Voice.h" |
#include "Voice.h" |
27 |
#include "EGADSR.h" |
#include "EGADSR.h" |
28 |
|
#include "../EngineFactory.h" |
29 |
|
|
30 |
#include "Engine.h" |
#include "Engine.h" |
31 |
|
|
37 |
|
|
38 |
namespace LinuxSampler { namespace gig { |
namespace LinuxSampler { namespace gig { |
39 |
|
|
40 |
InstrumentResourceManager Engine::Instruments; |
InstrumentResourceManager Engine::instruments; |
41 |
|
|
42 |
|
std::map<AudioOutputDevice*,Engine*> Engine::engines; |
43 |
|
|
44 |
|
/** |
45 |
|
* Get a gig::Engine object for the given gig::EngineChannel and the |
46 |
|
* given AudioOutputDevice. All engine channels which are connected to |
47 |
|
* the same audio output device will use the same engine instance. This |
48 |
|
* method will be called by a gig::EngineChannel whenever it's |
49 |
|
* connecting to a audio output device. |
50 |
|
* |
51 |
|
* @param pChannel - engine channel which acquires an engine object |
52 |
|
* @param pDevice - the audio output device \a pChannel is connected to |
53 |
|
*/ |
54 |
|
Engine* Engine::AcquireEngine(LinuxSampler::gig::EngineChannel* pChannel, AudioOutputDevice* pDevice) { |
55 |
|
Engine* pEngine = NULL; |
56 |
|
// check if there's already an engine for the given audio output device |
57 |
|
if (engines.count(pDevice)) { |
58 |
|
dmsg(4,("Using existing gig::Engine.\n")); |
59 |
|
pEngine = engines[pDevice]; |
60 |
|
} else { // create a new engine (and disk thread) instance for the given audio output device |
61 |
|
dmsg(4,("Creating new gig::Engine.\n")); |
62 |
|
pEngine = (Engine*) EngineFactory::Create("gig"); |
63 |
|
pEngine->Connect(pDevice); |
64 |
|
engines[pDevice] = pEngine; |
65 |
|
} |
66 |
|
// register engine channel to the engine instance |
67 |
|
pEngine->engineChannels.push_back(pChannel); |
68 |
|
dmsg(4,("This gig::Engine has now %d EngineChannels.\n",pEngine->engineChannels.size())); |
69 |
|
return pEngine; |
70 |
|
} |
71 |
|
|
72 |
|
/** |
73 |
|
* Once an engine channel is disconnected from an audio output device, |
74 |
|
* it wil immediately call this method to unregister itself from the |
75 |
|
* engine instance and if that engine instance is not used by any other |
76 |
|
* engine channel anymore, then that engine instance will be destroyed. |
77 |
|
* |
78 |
|
* @param pChannel - engine channel which wants to disconnect from it's |
79 |
|
* engine instance |
80 |
|
* @param pDevice - audio output device \a pChannel was connected to |
81 |
|
*/ |
82 |
|
void Engine::FreeEngine(LinuxSampler::gig::EngineChannel* pChannel, AudioOutputDevice* pDevice) { |
83 |
|
dmsg(4,("Disconnecting EngineChannel from gig::Engine.\n")); |
84 |
|
Engine* pEngine = engines[pDevice]; |
85 |
|
// unregister EngineChannel from the Engine instance |
86 |
|
pEngine->engineChannels.remove(pChannel); |
87 |
|
// if the used Engine instance is not used anymore, then destroy it |
88 |
|
if (pEngine->engineChannels.empty()) { |
89 |
|
pDevice->Disconnect(pEngine); |
90 |
|
engines.erase(pDevice); |
91 |
|
delete pEngine; |
92 |
|
dmsg(4,("Destroying gig::Engine.\n")); |
93 |
|
} |
94 |
|
else dmsg(4,("This gig::Engine has now %d EngineChannels.\n",pEngine->engineChannels.size())); |
95 |
|
} |
96 |
|
|
97 |
Engine::Engine() { |
Engine::Engine() { |
|
pRIFF = NULL; |
|
|
pGig = NULL; |
|
|
pInstrument = NULL; |
|
98 |
pAudioOutputDevice = NULL; |
pAudioOutputDevice = NULL; |
99 |
pDiskThread = NULL; |
pDiskThread = NULL; |
100 |
pEventGenerator = NULL; |
pEventGenerator = NULL; |
101 |
pSysexBuffer = new RingBuffer<uint8_t>(SYSEX_BUFFER_SIZE, 0); |
pSysexBuffer = new RingBuffer<uint8_t>(SYSEX_BUFFER_SIZE, 0); |
102 |
pEventQueue = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT, 0); |
pEventQueue = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT, 0); |
103 |
pEventPool = new Pool<Event>(MAX_EVENTS_PER_FRAGMENT); |
pEventPool = new Pool<Event>(MAX_EVENTS_PER_FRAGMENT); |
104 |
pVoicePool = new Pool<Voice>(MAX_AUDIO_VOICES); |
pVoicePool = new Pool<Voice>(MAX_AUDIO_VOICES); |
|
pActiveKeys = new Pool<uint>(128); |
|
105 |
pVoiceStealingQueue = new RTList<Event>(pEventPool); |
pVoiceStealingQueue = new RTList<Event>(pEventPool); |
106 |
pEvents = new RTList<Event>(pEventPool); |
pEvents = new RTList<Event>(pEventPool); |
107 |
pCCEvents = new RTList<Event>(pEventPool); |
pCCEvents = new RTList<Event>(pEventPool); |
108 |
|
|
109 |
for (uint i = 0; i < Event::destination_count; i++) { |
for (uint i = 0; i < Event::destination_count; i++) { |
110 |
pSynthesisEvents[i] = new RTList<Event>(pEventPool); |
pSynthesisEvents[i] = new RTList<Event>(pEventPool); |
111 |
} |
} |
|
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); |
|
|
} |
|
112 |
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()) { |
113 |
iterVoice->SetEngine(this); |
iterVoice->SetEngine(this); |
114 |
} |
} |
118 |
pBasicFilterParameters = NULL; |
pBasicFilterParameters = NULL; |
119 |
pMainFilterParameters = NULL; |
pMainFilterParameters = NULL; |
120 |
|
|
|
InstrumentIdx = -1; |
|
|
InstrumentStat = -1; |
|
|
|
|
|
AudioDeviceChannelLeft = -1; |
|
|
AudioDeviceChannelRight = -1; |
|
|
|
|
121 |
ResetInternal(); |
ResetInternal(); |
122 |
} |
} |
123 |
|
|
128 |
delete pDiskThread; |
delete pDiskThread; |
129 |
dmsg(1,("OK\n")); |
dmsg(1,("OK\n")); |
130 |
} |
} |
|
|
|
|
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; |
|
|
} |
|
131 |
for (uint i = 0; i < Event::destination_count; i++) { |
for (uint i = 0; i < Event::destination_count; i++) { |
132 |
if (pSynthesisEvents[i]) delete pSynthesisEvents[i]; |
if (pSynthesisEvents[i]) delete pSynthesisEvents[i]; |
133 |
} |
} |
135 |
if (pCCEvents) delete pCCEvents; |
if (pCCEvents) delete pCCEvents; |
136 |
if (pEventQueue) delete pEventQueue; |
if (pEventQueue) delete pEventQueue; |
137 |
if (pEventPool) delete pEventPool; |
if (pEventPool) delete pEventPool; |
138 |
if (pVoicePool) { |
if (pVoicePool) { |
139 |
pVoicePool->clear(); |
pVoicePool->clear(); |
140 |
delete pVoicePool; |
delete pVoicePool; |
141 |
} |
} |
|
if (pActiveKeys) delete pActiveKeys; |
|
|
if (pSysexBuffer) delete pSysexBuffer; |
|
142 |
if (pEventGenerator) delete pEventGenerator; |
if (pEventGenerator) delete pEventGenerator; |
143 |
if (pMainFilterParameters) delete[] pMainFilterParameters; |
if (pMainFilterParameters) delete[] pMainFilterParameters; |
144 |
if (pBasicFilterParameters) delete[] pBasicFilterParameters; |
if (pBasicFilterParameters) delete[] pBasicFilterParameters; |
145 |
if (pSynthesisParameters[0]) free(pSynthesisParameters[0]); |
if (pSynthesisParameters[0]) free(pSynthesisParameters[0]); |
146 |
if (pVoiceStealingQueue) delete pVoiceStealingQueue; |
if (pVoiceStealingQueue) delete pVoiceStealingQueue; |
147 |
|
if (pSysexBuffer) delete pSysexBuffer; |
148 |
|
EngineFactory::Destroy(this); |
149 |
} |
} |
150 |
|
|
151 |
void Engine::Enable() { |
void Engine::Enable() { |
172 |
*/ |
*/ |
173 |
void Engine::Reset() { |
void Engine::Reset() { |
174 |
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(); |
|
|
|
|
175 |
ResetInternal(); |
ResetInternal(); |
|
|
|
|
// signal audio thread to continue with rendering |
|
|
//SuspensionRequested = false; |
|
176 |
Enable(); |
Enable(); |
177 |
} |
} |
178 |
|
|
181 |
* control and status variables. This method is not thread safe! |
* control and status variables. This method is not thread safe! |
182 |
*/ |
*/ |
183 |
void Engine::ResetInternal() { |
void Engine::ResetInternal() { |
|
Pitch = 0; |
|
|
SustainPedal = false; |
|
184 |
ActiveVoiceCount = 0; |
ActiveVoiceCount = 0; |
185 |
ActiveVoiceCountMax = 0; |
ActiveVoiceCountMax = 0; |
|
GlobalVolume = 1.0; |
|
|
CurrentKeyDimension = 0; |
|
186 |
|
|
187 |
// reset voice stealing parameters |
// reset voice stealing parameters |
|
itLastStolenVoice = RTList<Voice>::Iterator(); |
|
|
iuiLastStolenKey = RTList<uint>::Iterator(); |
|
188 |
pVoiceStealingQueue->clear(); |
pVoiceStealingQueue->clear(); |
189 |
|
|
190 |
// reset to normal chromatic scale (means equal temper) |
// reset to normal chromatic scale (means equal temper) |
191 |
memset(&ScaleTuning[0], 0x00, 12); |
memset(&ScaleTuning[0], 0x00, 12); |
192 |
|
|
|
// 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; |
|
|
|
|
193 |
// reset all voices |
// reset all voices |
194 |
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()) { |
195 |
iterVoice->Reset(); |
iterVoice->Reset(); |
196 |
} |
} |
197 |
pVoicePool->clear(); |
pVoicePool->clear(); |
198 |
|
|
|
// free all active keys |
|
|
pActiveKeys->clear(); |
|
|
|
|
199 |
// reset disk thread |
// reset disk thread |
200 |
if (pDiskThread) pDiskThread->Reset(); |
if (pDiskThread) pDiskThread->Reset(); |
201 |
|
|
202 |
// delete all input events |
// delete all input events |
203 |
pEventQueue->init(); |
pEventQueue->init(); |
204 |
} |
} |
|
|
|
|
/** |
|
|
* Load an instrument from a .gig file. |
|
|
* |
|
|
* @param FileName - file name of the Gigasampler instrument file |
|
|
* @param Instrument - index of the instrument in the .gig file |
|
|
* @throws LinuxSamplerException on error |
|
|
* @returns detailed description of the method call result |
|
|
*/ |
|
|
void Engine::LoadInstrument(const char* FileName, uint Instrument) { |
|
|
|
|
|
DisableAndLock(); |
|
|
|
|
|
ResetInternal(); // reset engine |
|
|
|
|
|
// free old instrument |
|
|
if (pInstrument) { |
|
|
// give old instrument back to instrument manager |
|
|
Instruments.HandBack(pInstrument, this); |
|
|
} |
|
|
|
|
|
InstrumentFile = FileName; |
|
|
InstrumentIdx = Instrument; |
|
|
InstrumentStat = 0; |
|
|
|
|
|
// delete all key groups |
|
|
ActiveKeyGroups.clear(); |
|
|
|
|
|
// request gig instrument from instrument manager |
|
|
try { |
|
|
instrument_id_t instrid; |
|
|
instrid.FileName = FileName; |
|
|
instrid.iInstrument = Instrument; |
|
|
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; |
|
|
|
|
|
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(); |
|
|
} |
|
205 |
|
|
206 |
void Engine::Connect(AudioOutputDevice* pAudioOut) { |
void Engine::Connect(AudioOutputDevice* pAudioOut) { |
207 |
pAudioOutputDevice = pAudioOut; |
pAudioOutputDevice = pAudioOut; |
216 |
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(); |
217 |
throw LinuxSamplerException(msg); |
throw LinuxSamplerException(msg); |
218 |
} |
} |
219 |
|
|
|
this->AudioDeviceChannelLeft = 0; |
|
|
this->AudioDeviceChannelRight = 1; |
|
|
this->pOutputLeft = pAudioOutputDevice->Channel(0)->Buffer(); |
|
|
this->pOutputRight = pAudioOutputDevice->Channel(1)->Buffer(); |
|
220 |
this->MaxSamplesPerCycle = pAudioOutputDevice->MaxSamplesPerCycle(); |
this->MaxSamplesPerCycle = pAudioOutputDevice->MaxSamplesPerCycle(); |
221 |
this->SampleRate = pAudioOutputDevice->SampleRate(); |
this->SampleRate = pAudioOutputDevice->SampleRate(); |
222 |
|
|
223 |
// FIXME: audio drivers with varying fragment sizes might be a problem here |
// FIXME: audio drivers with varying fragment sizes might be a problem here |
224 |
MaxFadeOutPos = MaxSamplesPerCycle - int(double(SampleRate) * EG_MIN_RELEASE_TIME) - 1; |
MaxFadeOutPos = MaxSamplesPerCycle - int(double(SampleRate) * EG_MIN_RELEASE_TIME) - 1; |
225 |
if (MaxFadeOutPos < 0) |
if (MaxFadeOutPos < 0) |
226 |
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!"); |
227 |
|
|
228 |
// (re)create disk thread |
// (re)create disk thread |
229 |
if (this->pDiskThread) { |
if (this->pDiskThread) { |
277 |
} |
} |
278 |
} |
} |
279 |
|
|
280 |
void Engine::DisconnectAudioOutputDevice() { |
void Engine::ClearEventLists() { |
281 |
if (pAudioOutputDevice) { // if clause to prevent disconnect loops |
pEvents->clear(); |
282 |
AudioOutputDevice* olddevice = pAudioOutputDevice; |
pCCEvents->clear(); |
283 |
pAudioOutputDevice = NULL; |
for (uint i = 0; i < Event::destination_count; i++) { |
284 |
olddevice->Disconnect(this); |
pSynthesisEvents[i]->clear(); |
|
AudioDeviceChannelLeft = -1; |
|
|
AudioDeviceChannelRight = -1; |
|
285 |
} |
} |
286 |
} |
} |
287 |
|
|
288 |
/** |
/** |
289 |
|
* Copy all events from the given input queue buffer to the engine's |
290 |
|
* internal event list. This will be done at the beginning of each audio |
291 |
|
* cycle (that is each RenderAudio() call) to get all events which have |
292 |
|
* to be processed in the current audio cycle. Each EngineChannel has |
293 |
|
* it's own input event queue for the common channel specific events |
294 |
|
* (like NoteOn, NoteOff and ControlChange events). Beside that, the |
295 |
|
* engine also has a input event queue for global events (usually SysEx |
296 |
|
* message). |
297 |
|
* |
298 |
|
* @param pEventQueue - input event buffer to read from |
299 |
|
* @param Samples - number of sample points to be processed in the |
300 |
|
* current audio cycle |
301 |
|
*/ |
302 |
|
void Engine::ImportEvents(RingBuffer<Event>* pEventQueue, uint Samples) { |
303 |
|
RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
304 |
|
Event* pEvent; |
305 |
|
while (true) { |
306 |
|
// get next event from input event queue |
307 |
|
if (!(pEvent = eventQueueReader.pop())) break; |
308 |
|
// if younger event reached, ignore that and all subsequent ones for now |
309 |
|
if (pEvent->FragmentPos() >= Samples) { |
310 |
|
eventQueueReader--; |
311 |
|
dmsg(2,("Younger Event, pos=%d ,Samples=%d!\n",pEvent->FragmentPos(),Samples)); |
312 |
|
pEvent->ResetFragmentPos(); |
313 |
|
break; |
314 |
|
} |
315 |
|
// copy event to internal event list |
316 |
|
if (pEvents->poolIsEmpty()) { |
317 |
|
dmsg(1,("Event pool emtpy!\n")); |
318 |
|
break; |
319 |
|
} |
320 |
|
*pEvents->allocAppend() = *pEvent; |
321 |
|
} |
322 |
|
eventQueueReader.free(); // free all copied events from input queue |
323 |
|
} |
324 |
|
|
325 |
|
/** |
326 |
* 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 |
327 |
* 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 |
328 |
* 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 |
335 |
int Engine::RenderAudio(uint Samples) { |
int Engine::RenderAudio(uint Samples) { |
336 |
dmsg(5,("RenderAudio(Samples=%d)\n", Samples)); |
dmsg(5,("RenderAudio(Samples=%d)\n", Samples)); |
337 |
|
|
338 |
// return if no instrument loaded or engine disabled |
// return if engine disabled |
339 |
if (EngineDisabled.Pop()) { |
if (EngineDisabled.Pop()) { |
340 |
dmsg(5,("gig::Engine: engine disabled (val=%d)\n",EngineDisabled.GetUnsafe())); |
dmsg(5,("gig::Engine: engine disabled (val=%d)\n",EngineDisabled.GetUnsafe())); |
341 |
return 0; |
return 0; |
342 |
} |
} |
|
if (!pInstrument) { |
|
|
dmsg(5,("gig::Engine: no instrument loaded\n")); |
|
|
return 0; |
|
|
} |
|
|
|
|
343 |
|
|
344 |
// 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) |
345 |
pEventGenerator->UpdateFragmentTime(Samples); |
pEventGenerator->UpdateFragmentTime(Samples); |
346 |
|
|
347 |
|
// empty the engine's event lists for the new fragment |
348 |
|
ClearEventLists(); |
349 |
|
|
350 |
// 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 |
351 |
pEvents->clear(); |
// (these are usually just SysEx messages) |
352 |
pCCEvents->clear(); |
ImportEvents(this->pEventQueue, Samples); |
353 |
for (uint i = 0; i < Event::destination_count; i++) { |
|
354 |
pSynthesisEvents[i]->clear(); |
// process engine global events (these are currently only MIDI System Exclusive messages) |
355 |
|
{ |
356 |
|
RTList<Event>::Iterator itEvent = pEvents->first(); |
357 |
|
RTList<Event>::Iterator end = pEvents->end(); |
358 |
|
for (; itEvent != end; ++itEvent) { |
359 |
|
switch (itEvent->Type) { |
360 |
|
case Event::type_sysex: |
361 |
|
dmsg(5,("Engine: Sysex received\n")); |
362 |
|
ProcessSysex(itEvent); |
363 |
|
break; |
364 |
|
} |
365 |
|
} |
366 |
} |
} |
367 |
|
|
368 |
|
// reset internal voice counter (just for statistic of active voices) |
369 |
|
ActiveVoiceCountTemp = 0; |
370 |
|
|
371 |
|
// render audio for all engine channels |
372 |
|
// 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 |
373 |
{ |
{ |
374 |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
std::list<EngineChannel*>::iterator itChannel = engineChannels.begin(); |
375 |
RTList<uint>::Iterator end = pActiveKeys->end(); |
std::list<EngineChannel*>::iterator end = engineChannels.end(); |
376 |
for(; iuiKey != end; ++iuiKey) { |
for (; itChannel != end; itChannel++) { |
377 |
pMIDIKeyInfo[*iuiKey].pEvents->clear(); // free all events on the key |
if (!(*itChannel)->pInstrument) continue; // ignore if no instrument loaded |
378 |
|
RenderAudio(*itChannel, Samples); |
379 |
} |
} |
380 |
} |
} |
381 |
|
|
382 |
|
// just some statistics about this engine instance |
383 |
|
ActiveVoiceCount = ActiveVoiceCountTemp; |
384 |
|
if (ActiveVoiceCount > ActiveVoiceCountMax) ActiveVoiceCountMax = ActiveVoiceCount; |
385 |
|
|
386 |
// get all events from the input event queue which belong to the current fragment |
return 0; |
387 |
|
} |
388 |
|
|
389 |
|
void Engine::RenderAudio(EngineChannel* pEngineChannel, uint Samples) { |
390 |
|
// empty the engine's event lists for the new fragment |
391 |
|
ClearEventLists(); |
392 |
|
// empty the engine channel's, MIDI key specific event lists |
393 |
{ |
{ |
394 |
RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first(); |
395 |
Event* pEvent; |
RTList<uint>::Iterator end = pEngineChannel->pActiveKeys->end(); |
396 |
while (true) { |
for(; iuiKey != end; ++iuiKey) { |
397 |
// 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; |
|
398 |
} |
} |
|
eventQueueReader.free(); // free all copied events from input queue |
|
399 |
} |
} |
400 |
|
|
401 |
|
|
402 |
|
// get all events from the engine channels's input event queue which belong to the current fragment |
403 |
|
// (these are the common events like NoteOn, NoteOff, ControlChange, etc.) |
404 |
|
ImportEvents(pEngineChannel->pEventQueue, Samples); |
405 |
|
|
406 |
|
|
407 |
// process events |
// process events |
408 |
{ |
{ |
409 |
RTList<Event>::Iterator itEvent = pEvents->first(); |
RTList<Event>::Iterator itEvent = pEvents->first(); |
412 |
switch (itEvent->Type) { |
switch (itEvent->Type) { |
413 |
case Event::type_note_on: |
case Event::type_note_on: |
414 |
dmsg(5,("Engine: Note on received\n")); |
dmsg(5,("Engine: Note on received\n")); |
415 |
ProcessNoteOn(itEvent); |
ProcessNoteOn((EngineChannel*)itEvent->pEngineChannel, itEvent); |
416 |
break; |
break; |
417 |
case Event::type_note_off: |
case Event::type_note_off: |
418 |
dmsg(5,("Engine: Note off received\n")); |
dmsg(5,("Engine: Note off received\n")); |
419 |
ProcessNoteOff(itEvent); |
ProcessNoteOff((EngineChannel*)itEvent->pEngineChannel, itEvent); |
420 |
break; |
break; |
421 |
case Event::type_control_change: |
case Event::type_control_change: |
422 |
dmsg(5,("Engine: MIDI CC received\n")); |
dmsg(5,("Engine: MIDI CC received\n")); |
423 |
ProcessControlChange(itEvent); |
ProcessControlChange((EngineChannel*)itEvent->pEngineChannel, itEvent); |
424 |
break; |
break; |
425 |
case Event::type_pitchbend: |
case Event::type_pitchbend: |
426 |
dmsg(5,("Engine: Pitchbend received\n")); |
dmsg(5,("Engine: Pitchbend received\n")); |
427 |
ProcessPitchbend(itEvent); |
ProcessPitchbend((EngineChannel*)itEvent->pEngineChannel, itEvent); |
|
break; |
|
|
case Event::type_sysex: |
|
|
dmsg(5,("Engine: Sysex received\n")); |
|
|
ProcessSysex(itEvent); |
|
428 |
break; |
break; |
429 |
} |
} |
430 |
} |
} |
431 |
} |
} |
432 |
|
|
433 |
|
|
|
int active_voices = 0; |
|
|
|
|
434 |
// render audio from all active voices |
// render audio from all active voices |
435 |
{ |
{ |
436 |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first(); |
437 |
RTList<uint>::Iterator end = pActiveKeys->end(); |
RTList<uint>::Iterator end = pEngineChannel->pActiveKeys->end(); |
438 |
while (iuiKey != end) { // iterate through all active keys |
while (iuiKey != end) { // iterate through all active keys |
439 |
midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey]; |
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey]; |
440 |
++iuiKey; |
++iuiKey; |
441 |
|
|
442 |
RTList<Voice>::Iterator itVoice = pKey->pActiveVoices->first(); |
RTList<Voice>::Iterator itVoice = pKey->pActiveVoices->first(); |
444 |
for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key |
for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key |
445 |
// now render current voice |
// now render current voice |
446 |
itVoice->Render(Samples); |
itVoice->Render(Samples); |
447 |
if (itVoice->IsActive()) active_voices++; // still active |
if (itVoice->IsActive()) ActiveVoiceCountTemp++; // still active |
448 |
else { // voice reached end, is now inactive |
else { // voice reached end, is now inactive |
449 |
FreeVoice(itVoice); // remove voice from the list of active voices |
FreeVoice(pEngineChannel, itVoice); // remove voice from the list of active voices |
450 |
} |
} |
451 |
} |
} |
452 |
} |
} |
453 |
} |
} |
454 |
|
|
455 |
|
|
456 |
// now render all postponed voices from voice stealing |
// now render all postponed voices from voice stealing |
457 |
{ |
{ |
458 |
RTList<Event>::Iterator itVoiceStealEvent = pVoiceStealingQueue->first(); |
RTList<Event>::Iterator itVoiceStealEvent = pVoiceStealingQueue->first(); |
459 |
RTList<Event>::Iterator end = pVoiceStealingQueue->end(); |
RTList<Event>::Iterator end = pVoiceStealingQueue->end(); |
460 |
for (; itVoiceStealEvent != end; ++itVoiceStealEvent) { |
for (; itVoiceStealEvent != end; ++itVoiceStealEvent) { |
461 |
Pool<Voice>::Iterator itNewVoice = LaunchVoice(itVoiceStealEvent, itVoiceStealEvent->Param.Note.Layer, itVoiceStealEvent->Param.Note.ReleaseTrigger, false); |
Pool<Voice>::Iterator itNewVoice = |
462 |
|
LaunchVoice(pEngineChannel, itVoiceStealEvent, itVoiceStealEvent->Param.Note.Layer, itVoiceStealEvent->Param.Note.ReleaseTrigger, false); |
463 |
if (itNewVoice) { |
if (itNewVoice) { |
464 |
for (; itNewVoice; itNewVoice = itNewVoice->itChildVoice) { |
for (; itNewVoice; itNewVoice = itNewVoice->itChildVoice) { |
465 |
itNewVoice->Render(Samples); |
itNewVoice->Render(Samples); |
466 |
if (itNewVoice->IsActive()) active_voices++; // still active |
if (itNewVoice->IsActive()) ActiveVoiceCountTemp++; // still active |
467 |
else { // voice reached end, is now inactive |
else { // voice reached end, is now inactive |
468 |
FreeVoice(itNewVoice); // remove voice from the list of active voices |
FreeVoice(pEngineChannel, itNewVoice); // remove voice from the list of active voices |
469 |
} |
} |
470 |
} |
} |
471 |
} |
} |
474 |
} |
} |
475 |
// reset voice stealing for the new fragment |
// reset voice stealing for the new fragment |
476 |
pVoiceStealingQueue->clear(); |
pVoiceStealingQueue->clear(); |
477 |
itLastStolenVoice = RTList<Voice>::Iterator(); |
pEngineChannel->itLastStolenVoice = RTList<Voice>::Iterator(); |
478 |
iuiLastStolenKey = RTList<uint>::Iterator(); |
pEngineChannel->iuiLastStolenKey = RTList<uint>::Iterator(); |
479 |
|
|
480 |
|
|
481 |
// free all keys which have no active voices left |
// free all keys which have no active voices left |
482 |
{ |
{ |
483 |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first(); |
484 |
RTList<uint>::Iterator end = pActiveKeys->end(); |
RTList<uint>::Iterator end = pEngineChannel->pActiveKeys->end(); |
485 |
while (iuiKey != end) { // iterate through all active keys |
while (iuiKey != end) { // iterate through all active keys |
486 |
midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey]; |
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey]; |
487 |
++iuiKey; |
++iuiKey; |
488 |
if (pKey->pActiveVoices->isEmpty()) FreeKey(pKey); |
if (pKey->pActiveVoices->isEmpty()) FreeKey(pEngineChannel, pKey); |
489 |
#if DEVMODE |
#if DEVMODE |
490 |
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) |
491 |
RTList<Voice>::Iterator itVoice = pKey->pActiveVoices->first(); |
RTList<Voice>::Iterator itVoice = pKey->pActiveVoices->first(); |
499 |
#endif // DEVMODE |
#endif // DEVMODE |
500 |
} |
} |
501 |
} |
} |
|
|
|
|
|
|
|
// 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!")); |
|
502 |
} |
} |
503 |
|
|
504 |
/** |
/** |
512 |
Event event = pEventGenerator->CreateEvent(); |
Event event = pEventGenerator->CreateEvent(); |
513 |
event.Type = Event::type_sysex; |
event.Type = Event::type_sysex; |
514 |
event.Param.Sysex.Size = Size; |
event.Param.Sysex.Size = Size; |
515 |
|
event.pEngineChannel = NULL; // as Engine global event |
516 |
if (pEventQueue->write_space() > 0) { |
if (pEventQueue->write_space() > 0) { |
517 |
if (pSysexBuffer->write_space() >= Size) { |
if (pSysexBuffer->write_space() >= Size) { |
518 |
// copy sysex data to input buffer |
// copy sysex data to input buffer |
536 |
/** |
/** |
537 |
* Assigns and triggers a new voice for the respective MIDI key. |
* Assigns and triggers a new voice for the respective MIDI key. |
538 |
* |
* |
539 |
|
* @param pEngineChannel - engine channel on which this event occured on |
540 |
* @param itNoteOnEvent - key, velocity and time stamp of the event |
* @param itNoteOnEvent - key, velocity and time stamp of the event |
541 |
*/ |
*/ |
542 |
void Engine::ProcessNoteOn(Pool<Event>::Iterator& itNoteOnEvent) { |
void Engine::ProcessNoteOn(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) { |
543 |
|
|
544 |
const int key = itNoteOnEvent->Param.Note.Key; |
const int key = itNoteOnEvent->Param.Note.Key; |
545 |
|
|
546 |
// Change key dimension value if key is in keyswitching area |
// Change key dimension value if key is in keyswitching area |
547 |
if (key >= pInstrument->DimensionKeyRange.low && key <= pInstrument->DimensionKeyRange.high) |
{ |
548 |
CurrentKeyDimension = ((key - pInstrument->DimensionKeyRange.low) * 128) / |
const ::gig::Instrument* pInstrument = pEngineChannel->pInstrument; |
549 |
(pInstrument->DimensionKeyRange.high - pInstrument->DimensionKeyRange.low + 1); |
if (key >= pInstrument->DimensionKeyRange.low && key <= pInstrument->DimensionKeyRange.high) |
550 |
|
pEngineChannel->CurrentKeyDimension = ((key - pInstrument->DimensionKeyRange.low) * 128) / |
551 |
|
(pInstrument->DimensionKeyRange.high - pInstrument->DimensionKeyRange.low + 1); |
552 |
|
} |
553 |
|
|
554 |
midi_key_info_t* pKey = &pMIDIKeyInfo[key]; |
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[key]; |
555 |
|
|
556 |
pKey->KeyPressed = true; // the MIDI key was now pressed down |
pKey->KeyPressed = true; // the MIDI key was now pressed down |
557 |
|
|
558 |
// cancel release process of voices on this key if needed |
// cancel release process of voices on this key if needed |
559 |
if (pKey->Active && !SustainPedal) { |
if (pKey->Active && !pEngineChannel->SustainPedal) { |
560 |
RTList<Event>::Iterator itCancelReleaseEvent = pKey->pEvents->allocAppend(); |
RTList<Event>::Iterator itCancelReleaseEvent = pKey->pEvents->allocAppend(); |
561 |
if (itCancelReleaseEvent) { |
if (itCancelReleaseEvent) { |
562 |
*itCancelReleaseEvent = *itNoteOnEvent; // copy event |
*itCancelReleaseEvent = *itNoteOnEvent; // copy event |
569 |
RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents); |
RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents); |
570 |
|
|
571 |
// allocate and trigger a new voice for the key |
// allocate and trigger a new voice for the key |
572 |
LaunchVoice(itNoteOnEventOnKeyList, 0, false, true); |
LaunchVoice(pEngineChannel, itNoteOnEventOnKeyList, 0, false, true); |
573 |
} |
} |
574 |
|
|
575 |
/** |
/** |
578 |
* 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. |
579 |
* due to completion of sample playback). |
* due to completion of sample playback). |
580 |
* |
* |
581 |
|
* @param pEngineChannel - engine channel on which this event occured on |
582 |
* @param itNoteOffEvent - key, velocity and time stamp of the event |
* @param itNoteOffEvent - key, velocity and time stamp of the event |
583 |
*/ |
*/ |
584 |
void Engine::ProcessNoteOff(Pool<Event>::Iterator& itNoteOffEvent) { |
void Engine::ProcessNoteOff(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOffEvent) { |
585 |
midi_key_info_t* pKey = &pMIDIKeyInfo[itNoteOffEvent->Param.Note.Key]; |
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itNoteOffEvent->Param.Note.Key]; |
586 |
|
|
587 |
pKey->KeyPressed = false; // the MIDI key was now released |
pKey->KeyPressed = false; // the MIDI key was now released |
588 |
|
|
589 |
// release voices on this key if needed |
// release voices on this key if needed |
590 |
if (pKey->Active && !SustainPedal) { |
if (pKey->Active && !pEngineChannel->SustainPedal) { |
591 |
itNoteOffEvent->Type = Event::type_release; // transform event type |
itNoteOffEvent->Type = Event::type_release; // transform event type |
592 |
} |
} |
593 |
|
|
596 |
|
|
597 |
// spawn release triggered voice(s) if needed |
// spawn release triggered voice(s) if needed |
598 |
if (pKey->ReleaseTrigger) { |
if (pKey->ReleaseTrigger) { |
599 |
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 |
600 |
pKey->ReleaseTrigger = false; |
pKey->ReleaseTrigger = false; |
601 |
} |
} |
602 |
} |
} |
605 |
* Moves pitchbend event from the general (input) event list to the pitch |
* Moves pitchbend event from the general (input) event list to the pitch |
606 |
* event list. |
* event list. |
607 |
* |
* |
608 |
|
* @param pEngineChannel - engine channel on which this event occured on |
609 |
* @param itPitchbendEvent - absolute pitch value and time stamp of the event |
* @param itPitchbendEvent - absolute pitch value and time stamp of the event |
610 |
*/ |
*/ |
611 |
void Engine::ProcessPitchbend(Pool<Event>::Iterator& itPitchbendEvent) { |
void Engine::ProcessPitchbend(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itPitchbendEvent) { |
612 |
this->Pitch = itPitchbendEvent->Param.Pitch.Pitch; // store current pitch value |
pEngineChannel->Pitch = itPitchbendEvent->Param.Pitch.Pitch; // store current pitch value |
613 |
itPitchbendEvent.moveToEndOf(pSynthesisEvents[Event::destination_vco]); |
itPitchbendEvent.moveToEndOf(pSynthesisEvents[Event::destination_vco]); |
614 |
} |
} |
615 |
|
|
618 |
* called by the ProcessNoteOn() method and by the voices itself |
* called by the ProcessNoteOn() method and by the voices itself |
619 |
* (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). |
620 |
* |
* |
621 |
|
* @param pEngineChannel - engine channel on which this event occured on |
622 |
* @param itNoteOnEvent - key, velocity and time stamp of the event |
* @param itNoteOnEvent - key, velocity and time stamp of the event |
623 |
* @param iLayer - layer index for the new voice (optional - only |
* @param iLayer - layer index for the new voice (optional - only |
624 |
* in case of layered sounds of course) |
* in case of layered sounds of course) |
631 |
* if the voice wasn't triggered (for example when no region is |
* if the voice wasn't triggered (for example when no region is |
632 |
* defined for the given key). |
* defined for the given key). |
633 |
*/ |
*/ |
634 |
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) { |
635 |
midi_key_info_t* pKey = &pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key]; |
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key]; |
636 |
|
|
637 |
// allocate a new voice for the key |
// allocate a new voice for the key |
638 |
Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend(); |
Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend(); |
639 |
if (itNewVoice) { |
if (itNewVoice) { |
640 |
// launch the new voice |
// launch the new voice |
641 |
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) { |
642 |
dmsg(4,("Voice not triggered\n")); |
dmsg(4,("Voice not triggered\n")); |
643 |
pKey->pActiveVoices->free(itNewVoice); |
pKey->pActiveVoices->free(itNewVoice); |
644 |
} |
} |
645 |
else { // on success |
else { // on success |
646 |
uint** ppKeyGroup = NULL; |
uint** ppKeyGroup = NULL; |
647 |
if (itNewVoice->KeyGroup) { // if this voice / key belongs to a key group |
if (itNewVoice->KeyGroup) { // if this voice / key belongs to a key group |
648 |
ppKeyGroup = &ActiveKeyGroups[itNewVoice->KeyGroup]; |
ppKeyGroup = &pEngineChannel->ActiveKeyGroups[itNewVoice->KeyGroup]; |
649 |
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 |
650 |
midi_key_info_t* pOtherKey = &pMIDIKeyInfo[**ppKeyGroup]; |
midi_key_info_t* pOtherKey = &pEngineChannel->pMIDIKeyInfo[**ppKeyGroup]; |
651 |
// kill all voices on the (other) key |
// kill all voices on the (other) key |
652 |
RTList<Voice>::Iterator itVoiceToBeKilled = pOtherKey->pActiveVoices->first(); |
RTList<Voice>::Iterator itVoiceToBeKilled = pOtherKey->pActiveVoices->first(); |
653 |
RTList<Voice>::Iterator end = pOtherKey->pActiveVoices->end(); |
RTList<Voice>::Iterator end = pOtherKey->pActiveVoices->end(); |
658 |
} |
} |
659 |
if (!pKey->Active) { // mark as active key |
if (!pKey->Active) { // mark as active key |
660 |
pKey->Active = true; |
pKey->Active = true; |
661 |
pKey->itSelf = pActiveKeys->allocAppend(); |
pKey->itSelf = pEngineChannel->pActiveKeys->allocAppend(); |
662 |
*pKey->itSelf = itNoteOnEvent->Param.Note.Key; |
*pKey->itSelf = itNoteOnEvent->Param.Note.Key; |
663 |
} |
} |
664 |
if (itNewVoice->KeyGroup) { |
if (itNewVoice->KeyGroup) { |
670 |
} |
} |
671 |
else if (VoiceStealing) { |
else if (VoiceStealing) { |
672 |
// first, get total amount of required voices (dependant on amount of layers) |
// first, get total amount of required voices (dependant on amount of layers) |
673 |
::gig::Region* pRegion = pInstrument->GetRegion(itNoteOnEvent->Param.Note.Key); |
::gig::Region* pRegion = pEngineChannel->pInstrument->GetRegion(itNoteOnEvent->Param.Note.Key); |
674 |
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 |
675 |
int voicesRequired = pRegion->Layers; |
int voicesRequired = pRegion->Layers; |
676 |
|
|
677 |
// now steal the (remaining) amount of voices |
// now steal the (remaining) amount of voices |
678 |
for (int i = iLayer; i < voicesRequired; i++) |
for (int i = iLayer; i < voicesRequired; i++) |
679 |
StealVoice(itNoteOnEvent); |
StealVoice(pEngineChannel, itNoteOnEvent); |
680 |
|
|
681 |
// 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 |
682 |
RTList<Event>::Iterator itStealEvent = pVoiceStealingQueue->allocAppend(); |
RTList<Event>::Iterator itStealEvent = pVoiceStealingQueue->allocAppend(); |
697 |
* voice stealing and postpone the note-on event until the selected |
* voice stealing and postpone the note-on event until the selected |
698 |
* voice actually died. |
* voice actually died. |
699 |
* |
* |
700 |
|
* @param pEngineChannel - engine channel on which this event occured on |
701 |
* @param itNoteOnEvent - key, velocity and time stamp of the event |
* @param itNoteOnEvent - key, velocity and time stamp of the event |
702 |
*/ |
*/ |
703 |
void Engine::StealVoice(Pool<Event>::Iterator& itNoteOnEvent) { |
void Engine::StealVoice(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) { |
704 |
if (!pEventPool->poolIsEmpty()) { |
if (!pEventPool->poolIsEmpty()) { |
705 |
|
|
706 |
RTList<uint>::Iterator iuiOldestKey; |
RTList<uint>::Iterator iuiOldestKey; |
714 |
// key, or no voice left to kill there, then procceed with |
// key, or no voice left to kill there, then procceed with |
715 |
// 'oldestkey' algorithm |
// 'oldestkey' algorithm |
716 |
case voice_steal_algo_keymask: { |
case voice_steal_algo_keymask: { |
717 |
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key]; |
midi_key_info_t* pOldestKey = &pEngineChannel->pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key]; |
718 |
if (itLastStolenVoice) { |
if (pEngineChannel->itLastStolenVoice) { |
719 |
itOldestVoice = itLastStolenVoice; |
itOldestVoice = pEngineChannel->itLastStolenVoice; |
720 |
++itOldestVoice; |
++itOldestVoice; |
721 |
} |
} |
722 |
else { // no voice stolen in this audio fragment cycle yet |
else { // no voice stolen in this audio fragment cycle yet |
731 |
// try to pick the oldest voice on the oldest active key |
// try to pick the oldest voice on the oldest active key |
732 |
// (caution: must stay after 'keymask' algorithm !) |
// (caution: must stay after 'keymask' algorithm !) |
733 |
case voice_steal_algo_oldestkey: { |
case voice_steal_algo_oldestkey: { |
734 |
if (itLastStolenVoice) { |
if (pEngineChannel->itLastStolenVoice) { |
735 |
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiLastStolenKey]; |
midi_key_info_t* pOldestKey = &pEngineChannel->pMIDIKeyInfo[*pEngineChannel->iuiLastStolenKey]; |
736 |
itOldestVoice = itLastStolenVoice; |
itOldestVoice = pEngineChannel->itLastStolenVoice; |
737 |
++itOldestVoice; |
++itOldestVoice; |
738 |
if (!itOldestVoice) { |
if (!itOldestVoice) { |
739 |
iuiOldestKey = iuiLastStolenKey; |
iuiOldestKey = pEngineChannel->iuiLastStolenKey; |
740 |
++iuiOldestKey; |
++iuiOldestKey; |
741 |
if (iuiOldestKey) { |
if (iuiOldestKey) { |
742 |
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiOldestKey]; |
midi_key_info_t* pOldestKey = &pEngineChannel->pMIDIKeyInfo[*iuiOldestKey]; |
743 |
itOldestVoice = pOldestKey->pActiveVoices->first(); |
itOldestVoice = pOldestKey->pActiveVoices->first(); |
744 |
} |
} |
745 |
else { |
else { |
747 |
return; |
return; |
748 |
} |
} |
749 |
} |
} |
750 |
else iuiOldestKey = iuiLastStolenKey; |
else iuiOldestKey = pEngineChannel->iuiLastStolenKey; |
751 |
} |
} |
752 |
else { // no voice stolen in this audio fragment cycle yet |
else { // no voice stolen in this audio fragment cycle yet |
753 |
iuiOldestKey = pActiveKeys->first(); |
iuiOldestKey = pEngineChannel->pActiveKeys->first(); |
754 |
midi_key_info_t* pOldestKey = &pMIDIKeyInfo[*iuiOldestKey]; |
midi_key_info_t* pOldestKey = &pEngineChannel->pMIDIKeyInfo[*iuiOldestKey]; |
755 |
itOldestVoice = pOldestKey->pActiveVoices->first(); |
itOldestVoice = pOldestKey->pActiveVoices->first(); |
756 |
} |
} |
757 |
break; |
break; |
771 |
// now kill the selected voice |
// now kill the selected voice |
772 |
itOldestVoice->Kill(itNoteOnEvent); |
itOldestVoice->Kill(itNoteOnEvent); |
773 |
// 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 |
774 |
this->itLastStolenVoice = itOldestVoice; |
pEngineChannel->itLastStolenVoice = itOldestVoice; |
775 |
this->iuiLastStolenKey = iuiOldestKey; |
pEngineChannel->iuiLastStolenKey = iuiOldestKey; |
776 |
} |
} |
777 |
else dmsg(1,("Event pool emtpy!\n")); |
else dmsg(1,("Event pool emtpy!\n")); |
778 |
} |
} |
783 |
* it finished to playback its sample, finished its release stage or |
* it finished to playback its sample, finished its release stage or |
784 |
* just was killed. |
* just was killed. |
785 |
* |
* |
786 |
|
* @param pEngineChannel - engine channel on which this event occured on |
787 |
* @param itVoice - points to the voice to be freed |
* @param itVoice - points to the voice to be freed |
788 |
*/ |
*/ |
789 |
void Engine::FreeVoice(Pool<Voice>::Iterator& itVoice) { |
void Engine::FreeVoice(EngineChannel* pEngineChannel, Pool<Voice>::Iterator& itVoice) { |
790 |
if (itVoice) { |
if (itVoice) { |
791 |
midi_key_info_t* pKey = &pMIDIKeyInfo[itVoice->MIDIKey]; |
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itVoice->MIDIKey]; |
792 |
|
|
793 |
uint keygroup = itVoice->KeyGroup; |
uint keygroup = itVoice->KeyGroup; |
794 |
|
|
797 |
|
|
798 |
// 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 |
799 |
if (pKey->pActiveVoices->isEmpty() && keygroup) { |
if (pKey->pActiveVoices->isEmpty() && keygroup) { |
800 |
uint** ppKeyGroup = &ActiveKeyGroups[keygroup]; |
uint** ppKeyGroup = &pEngineChannel->ActiveKeyGroups[keygroup]; |
801 |
if (*ppKeyGroup == &*pKey->itSelf) *ppKeyGroup = NULL; // remove key from key group |
if (*ppKeyGroup == &*pKey->itSelf) *ppKeyGroup = NULL; // remove key from key group |
802 |
} |
} |
803 |
} |
} |
808 |
* 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 |
809 |
* update the key info respectively. |
* update the key info respectively. |
810 |
* |
* |
811 |
|
* @param pEngineChannel - engine channel on which this event occured on |
812 |
* @param pKey - key which is now inactive |
* @param pKey - key which is now inactive |
813 |
*/ |
*/ |
814 |
void Engine::FreeKey(midi_key_info_t* pKey) { |
void Engine::FreeKey(EngineChannel* pEngineChannel, midi_key_info_t* pKey) { |
815 |
if (pKey->pActiveVoices->isEmpty()) { |
if (pKey->pActiveVoices->isEmpty()) { |
816 |
pKey->Active = false; |
pKey->Active = false; |
817 |
pActiveKeys->free(pKey->itSelf); // remove key from list of active keys |
pEngineChannel->pActiveKeys->free(pKey->itSelf); // remove key from list of active keys |
818 |
pKey->itSelf = RTList<uint>::Iterator(); |
pKey->itSelf = RTList<uint>::Iterator(); |
819 |
pKey->ReleaseTrigger = false; |
pKey->ReleaseTrigger = false; |
820 |
pKey->pEvents->clear(); |
pKey->pEvents->clear(); |
827 |
* Reacts on supported control change commands (e.g. pitch bend wheel, |
* Reacts on supported control change commands (e.g. pitch bend wheel, |
828 |
* modulation wheel, aftertouch). |
* modulation wheel, aftertouch). |
829 |
* |
* |
830 |
|
* @param pEngineChannel - engine channel on which this event occured on |
831 |
* @param itControlChangeEvent - controller, value and time stamp of the event |
* @param itControlChangeEvent - controller, value and time stamp of the event |
832 |
*/ |
*/ |
833 |
void Engine::ProcessControlChange(Pool<Event>::Iterator& itControlChangeEvent) { |
void Engine::ProcessControlChange(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itControlChangeEvent) { |
834 |
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)); |
835 |
|
|
836 |
switch (itControlChangeEvent->Param.CC.Controller) { |
switch (itControlChangeEvent->Param.CC.Controller) { |
837 |
case 64: { |
case 64: { |
838 |
if (itControlChangeEvent->Param.CC.Value >= 64 && !SustainPedal) { |
if (itControlChangeEvent->Param.CC.Value >= 64 && !pEngineChannel->SustainPedal) { |
839 |
dmsg(4,("PEDAL DOWN\n")); |
dmsg(4,("PEDAL DOWN\n")); |
840 |
SustainPedal = true; |
pEngineChannel->SustainPedal = true; |
841 |
|
|
842 |
// cancel release process of voices if necessary |
// cancel release process of voices if necessary |
843 |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first(); |
844 |
if (iuiKey) { |
if (iuiKey) { |
845 |
itControlChangeEvent->Type = Event::type_cancel_release; // transform event type |
itControlChangeEvent->Type = Event::type_cancel_release; // transform event type |
846 |
while (iuiKey) { |
while (iuiKey) { |
847 |
midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey]; |
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey]; |
848 |
++iuiKey; |
++iuiKey; |
849 |
if (!pKey->KeyPressed) { |
if (!pKey->KeyPressed) { |
850 |
RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend(); |
RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend(); |
854 |
} |
} |
855 |
} |
} |
856 |
} |
} |
857 |
if (itControlChangeEvent->Param.CC.Value < 64 && SustainPedal) { |
if (itControlChangeEvent->Param.CC.Value < 64 && pEngineChannel->SustainPedal) { |
858 |
dmsg(4,("PEDAL UP\n")); |
dmsg(4,("PEDAL UP\n")); |
859 |
SustainPedal = false; |
pEngineChannel->SustainPedal = false; |
860 |
|
|
861 |
// release voices if their respective key is not pressed |
// release voices if their respective key is not pressed |
862 |
RTList<uint>::Iterator iuiKey = pActiveKeys->first(); |
RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first(); |
863 |
if (iuiKey) { |
if (iuiKey) { |
864 |
itControlChangeEvent->Type = Event::type_release; // transform event type |
itControlChangeEvent->Type = Event::type_release; // transform event type |
865 |
while (iuiKey) { |
while (iuiKey) { |
866 |
midi_key_info_t* pKey = &pMIDIKeyInfo[*iuiKey]; |
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey]; |
867 |
++iuiKey; |
++iuiKey; |
868 |
if (!pKey->KeyPressed) { |
if (!pKey->KeyPressed) { |
869 |
RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend(); |
RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend(); |
878 |
} |
} |
879 |
|
|
880 |
// update controller value in the engine's controller table |
// update controller value in the engine's controller table |
881 |
ControllerTable[itControlChangeEvent->Param.CC.Controller] = itControlChangeEvent->Param.CC.Value; |
pEngineChannel->ControllerTable[itControlChangeEvent->Param.CC.Controller] = itControlChangeEvent->Param.CC.Value; |
882 |
|
|
883 |
// 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 |
884 |
itControlChangeEvent.moveToEndOf(pCCEvents); |
itControlChangeEvent.moveToEndOf(pCCEvents); |
980 |
m[i+2] = val; |
m[i+2] = val; |
981 |
m[i+3] = val; |
m[i+3] = val; |
982 |
} |
} |
983 |
} |
} |
|
|
|
|
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)); |
|
|
} |
|
|
} |
|
984 |
|
|
985 |
uint Engine::VoiceCount() { |
uint Engine::VoiceCount() { |
986 |
return ActiveVoiceCount; |
return ActiveVoiceCount; |
1014 |
return "GigEngine"; |
return "GigEngine"; |
1015 |
} |
} |
1016 |
|
|
|
String Engine::InstrumentFileName() { |
|
|
return InstrumentFile; |
|
|
} |
|
|
|
|
|
int Engine::InstrumentIndex() { |
|
|
return InstrumentIdx; |
|
|
} |
|
|
|
|
|
int Engine::InstrumentStatus() { |
|
|
return InstrumentStat; |
|
|
} |
|
|
|
|
1017 |
String Engine::Description() { |
String Engine::Description() { |
1018 |
return "Gigasampler Engine"; |
return "Gigasampler Engine"; |
1019 |
} |
} |
1020 |
|
|
1021 |
String Engine::Version() { |
String Engine::Version() { |
1022 |
String s = "$Revision: 1.23 $"; |
String s = "$Revision: 1.28 $"; |
1023 |
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 |
1024 |
} |
} |
1025 |
|
|