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 * |
24 |
#include <sstream> |
#include <sstream> |
25 |
#include "DiskThread.h" |
#include "DiskThread.h" |
26 |
#include "Voice.h" |
#include "Voice.h" |
27 |
|
#include "EGADSR.h" |
28 |
|
|
29 |
#include "Engine.h" |
#include "Engine.h" |
30 |
|
|
31 |
|
#if defined(__APPLE__) |
32 |
|
# include <stdlib.h> |
33 |
|
#else |
34 |
|
# include <malloc.h> |
35 |
|
#endif |
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 |
|
Engine* Engine::AcquireEngine(LinuxSampler::gig::EngineChannel* pChannel, AudioOutputDevice* pDevice) { |
44 |
|
Engine* pEngine; |
45 |
|
if (engines.count(pDevice)) { |
46 |
|
pEngine = engines[pDevice]; |
47 |
|
} else { |
48 |
|
pEngine = new Engine; |
49 |
|
pEngine->Connect(pDevice); |
50 |
|
} |
51 |
|
pEngine->samplerChannels.push_back(pChannel); |
52 |
|
return pEngine; |
53 |
|
} |
54 |
|
|
55 |
|
void Engine::FreeEngine(LinuxSampler::gig::EngineChannel* pChannel, AudioOutputDevice* pDevice) { |
56 |
|
Engine* pEngine = engines[pDevice]; |
57 |
|
pEngine->samplerChannels.remove(pChannel); |
58 |
|
if (pEngine->samplerChannels.empty()) delete pEngine; |
59 |
|
} |
60 |
|
|
61 |
Engine::Engine() { |
Engine::Engine() { |
|
pRIFF = NULL; |
|
|
pGig = NULL; |
|
|
pInstrument = NULL; |
|
62 |
pAudioOutputDevice = NULL; |
pAudioOutputDevice = NULL; |
63 |
pDiskThread = NULL; |
pDiskThread = NULL; |
64 |
pEventGenerator = NULL; |
pEventGenerator = NULL; |
65 |
pEventQueue = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT); |
pSysexBuffer = new RingBuffer<uint8_t>(SYSEX_BUFFER_SIZE, 0); |
66 |
pEventPool = new RTELMemoryPool<Event>(MAX_EVENTS_PER_FRAGMENT); |
pEventQueue = new RingBuffer<Event>(MAX_EVENTS_PER_FRAGMENT, 0); |
67 |
pVoicePool = new RTELMemoryPool<Voice>(MAX_AUDIO_VOICES); |
pEventPool = new Pool<Event>(MAX_EVENTS_PER_FRAGMENT); |
68 |
pActiveKeys = new RTELMemoryPool<uint>(128); |
pVoicePool = new Pool<Voice>(MAX_AUDIO_VOICES); |
69 |
pEvents = new RTEList<Event>(pEventPool); |
pVoiceStealingQueue = new RTList<Event>(pEventPool); |
70 |
pCCEvents = new RTEList<Event>(pEventPool); |
pEvents = new RTList<Event>(pEventPool); |
71 |
|
pCCEvents = new RTList<Event>(pEventPool); |
72 |
|
|
73 |
for (uint i = 0; i < Event::destination_count; i++) { |
for (uint i = 0; i < Event::destination_count; i++) { |
74 |
pSynthesisEvents[i] = new RTEList<Event>(pEventPool); |
pSynthesisEvents[i] = new RTList<Event>(pEventPool); |
75 |
} |
} |
76 |
for (uint i = 0; i < 128; i++) { |
for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) { |
77 |
pMIDIKeyInfo[i].pActiveVoices = new RTEList<Voice>(pVoicePool); |
iterVoice->SetEngine(this); |
|
pMIDIKeyInfo[i].KeyPressed = false; |
|
|
pMIDIKeyInfo[i].Active = false; |
|
|
pMIDIKeyInfo[i].pSelf = NULL; |
|
|
pMIDIKeyInfo[i].pEvents = new RTEList<Event>(pEventPool); |
|
|
} |
|
|
for (Voice* pVoice = pVoicePool->alloc(); pVoice; pVoice = pVoicePool->alloc()) { |
|
|
pVoice->SetEngine(this); |
|
78 |
} |
} |
79 |
pVoicePool->clear(); |
pVoicePool->clear(); |
80 |
|
|
82 |
pBasicFilterParameters = NULL; |
pBasicFilterParameters = NULL; |
83 |
pMainFilterParameters = NULL; |
pMainFilterParameters = NULL; |
84 |
|
|
|
InstrumentIdx = -1; |
|
|
InstrumentStat = -1; |
|
|
|
|
85 |
ResetInternal(); |
ResetInternal(); |
86 |
} |
} |
87 |
|
|
88 |
Engine::~Engine() { |
Engine::~Engine() { |
89 |
if (pDiskThread) { |
if (pDiskThread) { |
90 |
|
dmsg(1,("Stopping disk thread...")); |
91 |
pDiskThread->StopThread(); |
pDiskThread->StopThread(); |
92 |
delete pDiskThread; |
delete pDiskThread; |
93 |
} |
dmsg(1,("OK\n")); |
|
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; |
|
94 |
} |
} |
95 |
for (uint i = 0; i < Event::destination_count; i++) { |
for (uint i = 0; i < Event::destination_count; i++) { |
96 |
if (pSynthesisEvents[i]) delete pSynthesisEvents[i]; |
if (pSynthesisEvents[i]) delete pSynthesisEvents[i]; |
97 |
} |
} |
|
delete[] pSynthesisEvents; |
|
98 |
if (pEvents) delete pEvents; |
if (pEvents) delete pEvents; |
99 |
if (pCCEvents) delete pCCEvents; |
if (pCCEvents) delete pCCEvents; |
100 |
if (pEventQueue) delete pEventQueue; |
if (pEventQueue) delete pEventQueue; |
101 |
if (pEventPool) delete pEventPool; |
if (pEventPool) delete pEventPool; |
102 |
if (pVoicePool) delete pVoicePool; |
if (pVoicePool) { |
103 |
if (pActiveKeys) delete pActiveKeys; |
pVoicePool->clear(); |
104 |
|
delete pVoicePool; |
105 |
|
} |
106 |
if (pEventGenerator) delete pEventGenerator; |
if (pEventGenerator) delete pEventGenerator; |
107 |
if (pMainFilterParameters) delete[] pMainFilterParameters; |
if (pMainFilterParameters) delete[] pMainFilterParameters; |
108 |
if (pBasicFilterParameters) delete[] pBasicFilterParameters; |
if (pBasicFilterParameters) delete[] pBasicFilterParameters; |
109 |
if (pSynthesisParameters[0]) delete[] pSynthesisParameters[0]; |
if (pSynthesisParameters[0]) free(pSynthesisParameters[0]); |
110 |
|
if (pVoiceStealingQueue) delete pVoiceStealingQueue; |
111 |
|
if (pSysexBuffer) delete pSysexBuffer; |
112 |
} |
} |
113 |
|
|
114 |
void Engine::Enable() { |
void Engine::Enable() { |
135 |
*/ |
*/ |
136 |
void Engine::Reset() { |
void Engine::Reset() { |
137 |
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(); |
|
|
|
|
138 |
ResetInternal(); |
ResetInternal(); |
|
|
|
|
// signal audio thread to continue with rendering |
|
|
//SuspensionRequested = false; |
|
139 |
Enable(); |
Enable(); |
140 |
} |
} |
141 |
|
|
144 |
* control and status variables. This method is not thread safe! |
* control and status variables. This method is not thread safe! |
145 |
*/ |
*/ |
146 |
void Engine::ResetInternal() { |
void Engine::ResetInternal() { |
|
Pitch = 0; |
|
|
SustainPedal = false; |
|
147 |
ActiveVoiceCount = 0; |
ActiveVoiceCount = 0; |
148 |
ActiveVoiceCountMax = 0; |
ActiveVoiceCountMax = 0; |
149 |
|
|
150 |
// set all MIDI controller values to zero |
// reset voice stealing parameters |
151 |
memset(ControllerTable, 0x00, 128); |
itLastStolenVoice = RTList<Voice>::Iterator(); |
152 |
|
iuiLastStolenKey = RTList<uint>::Iterator(); |
153 |
|
pVoiceStealingQueue->clear(); |
154 |
|
|
155 |
// reset key info |
// reset to normal chromatic scale (means equal temper) |
156 |
for (uint i = 0; i < 128; i++) { |
memset(&ScaleTuning[0], 0x00, 12); |
|
pMIDIKeyInfo[i].pActiveVoices->clear(); |
|
|
pMIDIKeyInfo[i].pEvents->clear(); |
|
|
pMIDIKeyInfo[i].KeyPressed = false; |
|
|
pMIDIKeyInfo[i].Active = false; |
|
|
pMIDIKeyInfo[i].pSelf = NULL; |
|
|
} |
|
157 |
|
|
158 |
// reset all voices |
// reset all voices |
159 |
for (Voice* pVoice = pVoicePool->alloc(); pVoice; pVoice = pVoicePool->alloc()) { |
for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) { |
160 |
pVoice->Reset(); |
iterVoice->Reset(); |
161 |
} |
} |
162 |
pVoicePool->clear(); |
pVoicePool->clear(); |
163 |
|
|
|
// free all active keys |
|
|
pActiveKeys->clear(); |
|
|
|
|
164 |
// reset disk thread |
// reset disk thread |
165 |
if (pDiskThread) pDiskThread->Reset(); |
if (pDiskThread) pDiskThread->Reset(); |
166 |
|
|
168 |
pEventQueue->init(); |
pEventQueue->init(); |
169 |
} |
} |
170 |
|
|
|
/** |
|
|
* 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; |
|
|
|
|
|
// 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."); |
|
|
} |
|
|
|
|
|
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; |
|
|
Enable(); |
|
|
} |
|
|
|
|
171 |
void Engine::Connect(AudioOutputDevice* pAudioOut) { |
void Engine::Connect(AudioOutputDevice* pAudioOut) { |
172 |
pAudioOutputDevice = pAudioOut; |
pAudioOutputDevice = pAudioOut; |
173 |
|
|
181 |
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(); |
182 |
throw LinuxSamplerException(msg); |
throw LinuxSamplerException(msg); |
183 |
} |
} |
184 |
|
|
185 |
|
this->MaxSamplesPerCycle = pAudioOutputDevice->MaxSamplesPerCycle(); |
186 |
|
this->SampleRate = pAudioOutputDevice->SampleRate(); |
187 |
|
|
188 |
|
// FIXME: audio drivers with varying fragment sizes might be a problem here |
189 |
|
MaxFadeOutPos = MaxSamplesPerCycle - int(double(SampleRate) * EG_MIN_RELEASE_TIME) - 1; |
190 |
|
if (MaxFadeOutPos < 0) |
191 |
|
throw LinuxSamplerException("EG_MIN_RELEASE_TIME in EGADSR.h too big for current audio fragment size / sampling rate!"); |
192 |
|
|
193 |
// (re)create disk thread |
// (re)create disk thread |
194 |
if (this->pDiskThread) { |
if (this->pDiskThread) { |
195 |
|
dmsg(1,("Stopping disk thread...")); |
196 |
this->pDiskThread->StopThread(); |
this->pDiskThread->StopThread(); |
197 |
delete this->pDiskThread; |
delete this->pDiskThread; |
198 |
|
dmsg(1,("OK\n")); |
199 |
} |
} |
200 |
this->pDiskThread = new DiskThread(((pAudioOut->MaxSamplesPerCycle() << MAX_PITCH) << 1) + 6); //FIXME: assuming stereo |
this->pDiskThread = new DiskThread(((pAudioOut->MaxSamplesPerCycle() << MAX_PITCH) << 1) + 6); //FIXME: assuming stereo |
201 |
if (!pDiskThread) { |
if (!pDiskThread) { |
203 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
204 |
} |
} |
205 |
|
|
206 |
for (Voice* pVoice = pVoicePool->alloc(); pVoice; pVoice = pVoicePool->alloc()) { |
for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) { |
207 |
pVoice->pDiskThread = this->pDiskThread; |
iterVoice->pDiskThread = this->pDiskThread; |
|
pVoice->SetOutput(pAudioOut); |
|
208 |
dmsg(3,("d")); |
dmsg(3,("d")); |
209 |
} |
} |
210 |
pVoicePool->clear(); |
pVoicePool->clear(); |
214 |
pEventGenerator = new EventGenerator(pAudioOut->SampleRate()); |
pEventGenerator = new EventGenerator(pAudioOut->SampleRate()); |
215 |
|
|
216 |
// (re)allocate synthesis parameter matrix |
// (re)allocate synthesis parameter matrix |
217 |
if (pSynthesisParameters[0]) delete[] pSynthesisParameters[0]; |
if (pSynthesisParameters[0]) free(pSynthesisParameters[0]); |
218 |
pSynthesisParameters[0] = new float[Event::destination_count * pAudioOut->MaxSamplesPerCycle()]; |
|
219 |
|
#if defined(__APPLE__) |
220 |
|
pSynthesisParameters[0] = (float *) malloc(Event::destination_count * sizeof(float) * pAudioOut->MaxSamplesPerCycle()); |
221 |
|
#else |
222 |
|
pSynthesisParameters[0] = (float *) memalign(16,(Event::destination_count * sizeof(float) * pAudioOut->MaxSamplesPerCycle())); |
223 |
|
#endif |
224 |
for (int dst = 1; dst < Event::destination_count; dst++) |
for (int dst = 1; dst < Event::destination_count; dst++) |
225 |
pSynthesisParameters[dst] = pSynthesisParameters[dst - 1] + pAudioOut->MaxSamplesPerCycle(); |
pSynthesisParameters[dst] = pSynthesisParameters[dst - 1] + pAudioOut->MaxSamplesPerCycle(); |
226 |
|
|
234 |
pDiskThread->StartThread(); |
pDiskThread->StartThread(); |
235 |
dmsg(1,("OK\n")); |
dmsg(1,("OK\n")); |
236 |
|
|
237 |
for (Voice* pVoice = pVoicePool->first(); pVoice; pVoice = pVoicePool->next()) { |
for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) { |
238 |
if (!pVoice->pDiskThread) { |
if (!iterVoice->pDiskThread) { |
239 |
dmsg(0,("Engine -> voice::trigger: !pDiskThread\n")); |
dmsg(0,("Engine -> voice::trigger: !pDiskThread\n")); |
240 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
241 |
} |
} |
242 |
} |
} |
243 |
} |
} |
244 |
|
|
|
void Engine::DisconnectAudioOutputDevice() { |
|
|
if (pAudioOutputDevice) { // if clause to prevent disconnect loops |
|
|
AudioOutputDevice* olddevice = pAudioOutputDevice; |
|
|
pAudioOutputDevice = NULL; |
|
|
olddevice->Disconnect(this); |
|
|
} |
|
|
} |
|
|
|
|
245 |
/** |
/** |
246 |
* 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 |
247 |
* 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 |
249 |
* converted to the appropriate value range by the audio output class (e.g. |
* converted to the appropriate value range by the audio output class (e.g. |
250 |
* AlsaIO or JackIO) right after. |
* AlsaIO or JackIO) right after. |
251 |
* |
* |
252 |
|
* @param pEngineChannel - the engine's channel to be rendered |
253 |
* @param Samples - number of sample points to be rendered |
* @param Samples - number of sample points to be rendered |
254 |
* @returns 0 on success |
* @returns 0 on success |
255 |
*/ |
*/ |
256 |
int Engine::RenderAudio(uint Samples) { |
int Engine::RenderAudio(LinuxSampler::gig::EngineChannel* pEngineChannel, uint Samples) { |
257 |
dmsg(5,("RenderAudio(Samples=%d)\n", Samples)); |
dmsg(5,("RenderAudio(Samples=%d)\n", Samples)); |
258 |
|
|
259 |
// return if no instrument loaded or engine disabled |
// return if no instrument loaded or engine disabled |
261 |
dmsg(5,("gig::Engine: engine disabled (val=%d)\n",EngineDisabled.GetUnsafe())); |
dmsg(5,("gig::Engine: engine disabled (val=%d)\n",EngineDisabled.GetUnsafe())); |
262 |
return 0; |
return 0; |
263 |
} |
} |
264 |
if (!pInstrument) { |
if (!pEngineChannel->pInstrument) { |
265 |
dmsg(5,("gig::Engine: no instrument loaded\n")); |
dmsg(5,("gig::Engine: no instrument loaded\n")); |
266 |
return 0; |
return 0; |
267 |
} |
} |
268 |
|
|
269 |
|
|
270 |
|
// update time of start and end of this audio fragment (as events' time stamps relate to this) |
271 |
|
pEventGenerator->UpdateFragmentTime(Samples); |
272 |
|
|
273 |
|
|
274 |
// empty the event lists for the new fragment |
// empty the event lists for the new fragment |
275 |
pEvents->clear(); |
pEvents->clear(); |
276 |
pCCEvents->clear(); |
pCCEvents->clear(); |
277 |
for (uint i = 0; i < Event::destination_count; i++) { |
for (uint i = 0; i < Event::destination_count; i++) { |
278 |
pSynthesisEvents[i]->clear(); |
pSynthesisEvents[i]->clear(); |
279 |
} |
} |
280 |
|
{ |
281 |
// read and copy events from input queue |
RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first(); |
282 |
Event event = pEventGenerator->CreateEvent(); |
RTList<uint>::Iterator end = pEngineChannel->pActiveKeys->end(); |
283 |
while (true) { |
for(; iuiKey != end; ++iuiKey) { |
284 |
if (!pEventQueue->pop(&event)) break; |
pEngineChannel->pMIDIKeyInfo[*iuiKey].pEvents->clear(); // free all events on the key |
285 |
pEvents->alloc_assign(event); |
} |
286 |
} |
} |
287 |
|
|
288 |
|
|
289 |
// update time of start and end of this audio fragment (as events' time stamps relate to this) |
// get all events from the input event queue which belong to the current fragment |
290 |
pEventGenerator->UpdateFragmentTime(Samples); |
{ |
291 |
|
RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
292 |
|
Event* pEvent; |
293 |
|
while (true) { |
294 |
|
// get next event from input event queue |
295 |
|
if (!(pEvent = eventQueueReader.pop())) break; |
296 |
|
// if younger event reached, ignore that and all subsequent ones for now |
297 |
|
if (pEvent->FragmentPos() >= Samples) { |
298 |
|
eventQueueReader--; |
299 |
|
dmsg(2,("Younger Event, pos=%d ,Samples=%d!\n",pEvent->FragmentPos(),Samples)); |
300 |
|
pEvent->ResetFragmentPos(); |
301 |
|
break; |
302 |
|
} |
303 |
|
// copy event to internal event list |
304 |
|
if (pEvents->poolIsEmpty()) { |
305 |
|
dmsg(1,("Event pool emtpy!\n")); |
306 |
|
break; |
307 |
|
} |
308 |
|
*pEvents->allocAppend() = *pEvent; |
309 |
|
} |
310 |
|
eventQueueReader.free(); // free all copied events from input queue |
311 |
|
} |
312 |
|
|
313 |
|
|
314 |
// process events |
// process events |
315 |
Event* pNextEvent = pEvents->first(); |
{ |
316 |
while (pNextEvent) { |
RTList<Event>::Iterator itEvent = pEvents->first(); |
317 |
Event* pEvent = pNextEvent; |
RTList<Event>::Iterator end = pEvents->end(); |
318 |
pEvents->set_current(pEvent); |
for (; itEvent != end; ++itEvent) { |
319 |
pNextEvent = pEvents->next(); |
switch (itEvent->Type) { |
320 |
switch (pEvent->Type) { |
case Event::type_note_on: |
321 |
case Event::type_note_on: |
dmsg(5,("Engine: Note on received\n")); |
322 |
dmsg(5,("Audio Thread: Note on received\n")); |
ProcessNoteOn(pEngineChannel, itEvent); |
323 |
ProcessNoteOn(pEvent); |
break; |
324 |
break; |
case Event::type_note_off: |
325 |
case Event::type_note_off: |
dmsg(5,("Engine: Note off received\n")); |
326 |
dmsg(5,("Audio Thread: Note off received\n")); |
ProcessNoteOff(pEngineChannel, itEvent); |
327 |
ProcessNoteOff(pEvent); |
break; |
328 |
break; |
case Event::type_control_change: |
329 |
case Event::type_control_change: |
dmsg(5,("Engine: MIDI CC received\n")); |
330 |
dmsg(5,("Audio Thread: MIDI CC received\n")); |
ProcessControlChange(pEngineChannel, itEvent); |
331 |
ProcessControlChange(pEvent); |
break; |
332 |
break; |
case Event::type_pitchbend: |
333 |
case Event::type_pitchbend: |
dmsg(5,("Engine: Pitchbend received\n")); |
334 |
dmsg(5,("Audio Thread: Pitchbend received\n")); |
ProcessPitchbend(pEngineChannel, itEvent); |
335 |
ProcessPitchbend(pEvent); |
break; |
336 |
break; |
case Event::type_sysex: |
337 |
|
dmsg(5,("Engine: Sysex received\n")); |
338 |
|
ProcessSysex(itEvent); |
339 |
|
break; |
340 |
|
} |
341 |
} |
} |
342 |
} |
} |
343 |
|
|
344 |
|
|
|
// render audio from all active voices |
|
345 |
int active_voices = 0; |
int active_voices = 0; |
346 |
uint* piKey = pActiveKeys->first(); |
|
347 |
while (piKey) { // iterate through all active keys |
// render audio from all active voices |
348 |
midi_key_info_t* pKey = &pMIDIKeyInfo[*piKey]; |
{ |
349 |
pActiveKeys->set_current(piKey); |
RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first(); |
350 |
piKey = pActiveKeys->next(); |
RTList<uint>::Iterator end = pEngineChannel->pActiveKeys->end(); |
351 |
|
while (iuiKey != end) { // iterate through all active keys |
352 |
Voice* pVoiceNext = pKey->pActiveVoices->first(); |
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey]; |
353 |
while (pVoiceNext) { // iterate through all voices on this key |
++iuiKey; |
354 |
// already get next voice on key |
|
355 |
Voice* pVoice = pVoiceNext; |
RTList<Voice>::Iterator itVoice = pKey->pActiveVoices->first(); |
356 |
pKey->pActiveVoices->set_current(pVoice); |
RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end(); |
357 |
pVoiceNext = pKey->pActiveVoices->next(); |
for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key |
358 |
|
// now render current voice |
359 |
// now render current voice |
itVoice->Render(Samples); |
360 |
pVoice->Render(Samples); |
if (itVoice->IsActive()) active_voices++; // still active |
361 |
if (pVoice->IsActive()) active_voices++; // still active |
else { // voice reached end, is now inactive |
362 |
else { // voice reached end, is now inactive |
FreeVoice(pEngineChannel, itVoice); // remove voice from the list of active voices |
363 |
KillVoice(pVoice); // remove voice from the list of active voices |
} |
364 |
|
} |
365 |
|
} |
366 |
|
} |
367 |
|
|
368 |
|
|
369 |
|
// now render all postponed voices from voice stealing |
370 |
|
{ |
371 |
|
RTList<Event>::Iterator itVoiceStealEvent = pVoiceStealingQueue->first(); |
372 |
|
RTList<Event>::Iterator end = pVoiceStealingQueue->end(); |
373 |
|
for (; itVoiceStealEvent != end; ++itVoiceStealEvent) { |
374 |
|
Pool<Voice>::Iterator itNewVoice = |
375 |
|
LaunchVoice(pEngineChannel, itVoiceStealEvent, itVoiceStealEvent->Param.Note.Layer, itVoiceStealEvent->Param.Note.ReleaseTrigger, false); |
376 |
|
if (itNewVoice) { |
377 |
|
for (; itNewVoice; itNewVoice = itNewVoice->itChildVoice) { |
378 |
|
itNewVoice->Render(Samples); |
379 |
|
if (itNewVoice->IsActive()) active_voices++; // still active |
380 |
|
else { // voice reached end, is now inactive |
381 |
|
FreeVoice(pEngineChannel, itNewVoice); // remove voice from the list of active voices |
382 |
|
} |
383 |
|
} |
384 |
|
} |
385 |
|
else dmsg(1,("gig::Engine: ERROR, voice stealing didn't work out!\n")); |
386 |
|
} |
387 |
|
} |
388 |
|
// reset voice stealing for the new fragment |
389 |
|
pVoiceStealingQueue->clear(); |
390 |
|
itLastStolenVoice = RTList<Voice>::Iterator(); |
391 |
|
iuiLastStolenKey = RTList<uint>::Iterator(); |
392 |
|
|
393 |
|
|
394 |
|
// free all keys which have no active voices left |
395 |
|
{ |
396 |
|
RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first(); |
397 |
|
RTList<uint>::Iterator end = pEngineChannel->pActiveKeys->end(); |
398 |
|
while (iuiKey != end) { // iterate through all active keys |
399 |
|
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey]; |
400 |
|
++iuiKey; |
401 |
|
if (pKey->pActiveVoices->isEmpty()) FreeKey(pEngineChannel, pKey); |
402 |
|
#if DEVMODE |
403 |
|
else { // FIXME: should be removed before the final release (purpose: just a sanity check for debugging) |
404 |
|
RTList<Voice>::Iterator itVoice = pKey->pActiveVoices->first(); |
405 |
|
RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end(); |
406 |
|
for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key |
407 |
|
if (itVoice->itKillEvent) { |
408 |
|
dmsg(1,("gig::Engine: ERROR, killed voice survived !!!\n")); |
409 |
|
} |
410 |
|
} |
411 |
} |
} |
412 |
|
#endif // DEVMODE |
413 |
} |
} |
|
pKey->pEvents->clear(); // free all events on the key |
|
414 |
} |
} |
415 |
|
|
416 |
|
|
421 |
|
|
422 |
|
|
423 |
return 0; |
return 0; |
424 |
} |
} |
|
|
|
|
/** |
|
|
* 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.Key = Key; |
|
|
event.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.Key = Key; |
|
|
event.Velocity = Velocity; |
|
|
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
|
|
else dmsg(1,("Engine: Input event queue full!")); |
|
|
} |
|
425 |
|
|
426 |
/** |
/** |
427 |
* Will be called by the MIDIIn Thread to signal the audio thread to change |
* Will be called by the MIDI input device whenever a MIDI system |
428 |
* the pitch value for all voices. |
* exclusive message has arrived. |
429 |
* |
* |
430 |
* @param Pitch - MIDI pitch value (-8192 ... +8191) |
* @param pData - pointer to sysex data |
431 |
|
* @param Size - lenght of sysex data (in bytes) |
432 |
*/ |
*/ |
433 |
void Engine::SendPitchbend(int Pitch) { |
void Engine::SendSysex(void* pData, uint Size) { |
434 |
Event event = pEventGenerator->CreateEvent(); |
Event event = pEventGenerator->CreateEvent(); |
435 |
event.Type = Event::type_pitchbend; |
event.Type = Event::type_sysex; |
436 |
event.Pitch = Pitch; |
event.Param.Sysex.Size = Size; |
437 |
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
if (pEventQueue->write_space() > 0) { |
438 |
else dmsg(1,("Engine: Input event queue full!")); |
if (pSysexBuffer->write_space() >= Size) { |
439 |
} |
// copy sysex data to input buffer |
440 |
|
uint toWrite = Size; |
441 |
|
uint8_t* pPos = (uint8_t*) pData; |
442 |
|
while (toWrite) { |
443 |
|
const uint writeNow = RTMath::Min(toWrite, pSysexBuffer->write_space_to_end()); |
444 |
|
pSysexBuffer->write(pPos, writeNow); |
445 |
|
toWrite -= writeNow; |
446 |
|
pPos += writeNow; |
447 |
|
|
448 |
/** |
} |
449 |
* Will be called by the MIDIIn Thread to signal the audio thread that a |
// finally place sysex event into input event queue |
450 |
* continuous controller value has changed. |
pEventQueue->push(&event); |
451 |
* |
} |
452 |
* @param Controller - MIDI controller number of the occured control change |
else dmsg(1,("Engine: Sysex message too large (%d byte) for input buffer (%d byte)!",Size,SYSEX_BUFFER_SIZE)); |
453 |
* @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.Controller = Controller; |
|
|
event.Value = Value; |
|
|
if (this->pEventQueue->write_space() > 0) this->pEventQueue->push(&event); |
|
454 |
else dmsg(1,("Engine: Input event queue full!")); |
else dmsg(1,("Engine: Input event queue full!")); |
455 |
} |
} |
456 |
|
|
457 |
/** |
/** |
458 |
* Assigns and triggers a new voice for the respective MIDI key. |
* Assigns and triggers a new voice for the respective MIDI key. |
459 |
* |
* |
460 |
* @param pNoteOnEvent - key, velocity and time stamp of the event |
* @param pEngineChannel - engine channel on which this event occured on |
461 |
|
* @param itNoteOnEvent - key, velocity and time stamp of the event |
462 |
*/ |
*/ |
463 |
void Engine::ProcessNoteOn(Event* pNoteOnEvent) { |
void Engine::ProcessNoteOn(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) { |
464 |
midi_key_info_t* pKey = &pMIDIKeyInfo[pNoteOnEvent->Key]; |
|
465 |
|
const int key = itNoteOnEvent->Param.Note.Key; |
466 |
|
|
467 |
|
// Change key dimension value if key is in keyswitching area |
468 |
|
{ |
469 |
|
const ::gig::Instrument* pInstrument = pEngineChannel->pInstrument; |
470 |
|
if (key >= pInstrument->DimensionKeyRange.low && key <= pInstrument->DimensionKeyRange.high) |
471 |
|
pEngineChannel->CurrentKeyDimension = ((key - pInstrument->DimensionKeyRange.low) * 128) / |
472 |
|
(pInstrument->DimensionKeyRange.high - pInstrument->DimensionKeyRange.low + 1); |
473 |
|
} |
474 |
|
|
475 |
|
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[key]; |
476 |
|
|
477 |
pKey->KeyPressed = true; // the MIDI key was now pressed down |
pKey->KeyPressed = true; // the MIDI key was now pressed down |
478 |
|
|
479 |
// cancel release process of voices on this key if needed |
// cancel release process of voices on this key if needed |
480 |
if (pKey->Active && !SustainPedal) { |
if (pKey->Active && !pEngineChannel->SustainPedal) { |
481 |
pNoteOnEvent->Type = Event::type_cancel_release; // transform event type |
RTList<Event>::Iterator itCancelReleaseEvent = pKey->pEvents->allocAppend(); |
482 |
pEvents->move(pNoteOnEvent, pKey->pEvents); // move event to the key's own event list |
if (itCancelReleaseEvent) { |
483 |
} |
*itCancelReleaseEvent = *itNoteOnEvent; // copy event |
484 |
|
itCancelReleaseEvent->Type = Event::type_cancel_release; // transform event type |
|
// allocate a new voice for the key |
|
|
Voice* pNewVoice = pKey->pActiveVoices->alloc(); |
|
|
if (pNewVoice) { |
|
|
// launch the new voice |
|
|
if (pNewVoice->Trigger(pNoteOnEvent, this->Pitch, this->pInstrument) < 0) { |
|
|
dmsg(1,("Triggering new voice failed!\n")); |
|
|
pKey->pActiveVoices->free(pNewVoice); |
|
|
} |
|
|
else if (!pKey->Active) { // mark as active key |
|
|
pKey->Active = true; |
|
|
pKey->pSelf = pActiveKeys->alloc(); |
|
|
*pKey->pSelf = pNoteOnEvent->Key; |
|
485 |
} |
} |
486 |
|
else dmsg(1,("Event pool emtpy!\n")); |
487 |
} |
} |
488 |
else std::cerr << "No free voice!" << std::endl << std::flush; |
|
489 |
|
// move note on event to the key's own event list |
490 |
|
RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents); |
491 |
|
|
492 |
|
// allocate and trigger a new voice for the key |
493 |
|
LaunchVoice(pEngineChannel, itNoteOnEventOnKeyList, 0, false, true); |
494 |
} |
} |
495 |
|
|
496 |
/** |
/** |
499 |
* 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. |
500 |
* due to completion of sample playback). |
* due to completion of sample playback). |
501 |
* |
* |
502 |
* @param pNoteOffEvent - key, velocity and time stamp of the event |
* @param pEngineChannel - engine channel on which this event occured on |
503 |
|
* @param itNoteOffEvent - key, velocity and time stamp of the event |
504 |
*/ |
*/ |
505 |
void Engine::ProcessNoteOff(Event* pNoteOffEvent) { |
void Engine::ProcessNoteOff(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOffEvent) { |
506 |
midi_key_info_t* pKey = &pMIDIKeyInfo[pNoteOffEvent->Key]; |
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itNoteOffEvent->Param.Note.Key]; |
507 |
|
|
508 |
pKey->KeyPressed = false; // the MIDI key was now released |
pKey->KeyPressed = false; // the MIDI key was now released |
509 |
|
|
510 |
// release voices on this key if needed |
// release voices on this key if needed |
511 |
if (pKey->Active && !SustainPedal) { |
if (pKey->Active && !pEngineChannel->SustainPedal) { |
512 |
pNoteOffEvent->Type = Event::type_release; // transform event type |
itNoteOffEvent->Type = Event::type_release; // transform event type |
513 |
pEvents->move(pNoteOffEvent, pKey->pEvents); // move event to the key's own event list |
} |
514 |
|
|
515 |
|
// move event to the key's own event list |
516 |
|
RTList<Event>::Iterator itNoteOffEventOnKeyList = itNoteOffEvent.moveToEndOf(pKey->pEvents); |
517 |
|
|
518 |
|
// spawn release triggered voice(s) if needed |
519 |
|
if (pKey->ReleaseTrigger) { |
520 |
|
LaunchVoice(pEngineChannel, itNoteOffEventOnKeyList, 0, true, false); //FIXME: for the moment we don't perform voice stealing for release triggered samples |
521 |
|
pKey->ReleaseTrigger = false; |
522 |
} |
} |
523 |
} |
} |
524 |
|
|
526 |
* Moves pitchbend event from the general (input) event list to the pitch |
* Moves pitchbend event from the general (input) event list to the pitch |
527 |
* event list. |
* event list. |
528 |
* |
* |
529 |
* @param pPitchbendEvent - absolute pitch value and time stamp of the event |
* @param pEngineChannel - engine channel on which this event occured on |
530 |
|
* @param itPitchbendEvent - absolute pitch value and time stamp of the event |
531 |
*/ |
*/ |
532 |
void Engine::ProcessPitchbend(Event* pPitchbendEvent) { |
void Engine::ProcessPitchbend(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itPitchbendEvent) { |
533 |
this->Pitch = pPitchbendEvent->Pitch; // store current pitch value |
pEngineChannel->Pitch = itPitchbendEvent->Param.Pitch.Pitch; // store current pitch value |
534 |
pEvents->move(pPitchbendEvent, pSynthesisEvents[Event::destination_vco]); |
itPitchbendEvent.moveToEndOf(pSynthesisEvents[Event::destination_vco]); |
535 |
} |
} |
536 |
|
|
537 |
/** |
/** |
538 |
* Immediately kills the voice given with pVoice (no matter if sustain is |
* Allocates and triggers a new voice. This method will usually be |
539 |
* pressed or not) and removes it from the MIDI key's list of active voice. |
* called by the ProcessNoteOn() method and by the voices itself |
540 |
* This method will e.g. be called if a voice went inactive by itself. |
* (e.g. to spawn further voices on the same key for layered sounds). |
541 |
|
* |
542 |
|
* @param pEngineChannel - engine channel on which this event occured on |
543 |
|
* @param itNoteOnEvent - key, velocity and time stamp of the event |
544 |
|
* @param iLayer - layer index for the new voice (optional - only |
545 |
|
* in case of layered sounds of course) |
546 |
|
* @param ReleaseTriggerVoice - if new voice is a release triggered voice |
547 |
|
* (optional, default = false) |
548 |
|
* @param VoiceStealing - if voice stealing should be performed |
549 |
|
* when there is no free voice |
550 |
|
* (optional, default = true) |
551 |
|
* @returns pointer to new voice or NULL if there was no free voice or |
552 |
|
* if the voice wasn't triggered (for example when no region is |
553 |
|
* defined for the given key). |
554 |
|
*/ |
555 |
|
Pool<Voice>::Iterator Engine::LaunchVoice(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent, int iLayer, bool ReleaseTriggerVoice, bool VoiceStealing) { |
556 |
|
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key]; |
557 |
|
|
558 |
|
// allocate a new voice for the key |
559 |
|
Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend(); |
560 |
|
if (itNewVoice) { |
561 |
|
// launch the new voice |
562 |
|
if (itNewVoice->Trigger(pEngineChannel, itNoteOnEvent, pEngineChannel->Pitch, pEngineChannel->pInstrument, iLayer, ReleaseTriggerVoice, VoiceStealing) < 0) { |
563 |
|
dmsg(4,("Voice not triggered\n")); |
564 |
|
pKey->pActiveVoices->free(itNewVoice); |
565 |
|
} |
566 |
|
else { // on success |
567 |
|
uint** ppKeyGroup = NULL; |
568 |
|
if (itNewVoice->KeyGroup) { // if this voice / key belongs to a key group |
569 |
|
ppKeyGroup = &pEngineChannel->ActiveKeyGroups[itNewVoice->KeyGroup]; |
570 |
|
if (*ppKeyGroup) { // if there's already an active key in that key group |
571 |
|
midi_key_info_t* pOtherKey = &pEngineChannel->pMIDIKeyInfo[**ppKeyGroup]; |
572 |
|
// kill all voices on the (other) key |
573 |
|
RTList<Voice>::Iterator itVoiceToBeKilled = pOtherKey->pActiveVoices->first(); |
574 |
|
RTList<Voice>::Iterator end = pOtherKey->pActiveVoices->end(); |
575 |
|
for (; itVoiceToBeKilled != end; ++itVoiceToBeKilled) { |
576 |
|
if (itVoiceToBeKilled->Type != Voice::type_release_trigger) itVoiceToBeKilled->Kill(itNoteOnEvent); |
577 |
|
} |
578 |
|
} |
579 |
|
} |
580 |
|
if (!pKey->Active) { // mark as active key |
581 |
|
pKey->Active = true; |
582 |
|
pKey->itSelf = pEngineChannel->pActiveKeys->allocAppend(); |
583 |
|
*pKey->itSelf = itNoteOnEvent->Param.Note.Key; |
584 |
|
} |
585 |
|
if (itNewVoice->KeyGroup) { |
586 |
|
*ppKeyGroup = &*pKey->itSelf; // put key as the (new) active key to its key group |
587 |
|
} |
588 |
|
if (itNewVoice->Type == Voice::type_release_trigger_required) pKey->ReleaseTrigger = true; // mark key for the need of release triggered voice(s) |
589 |
|
return itNewVoice; // success |
590 |
|
} |
591 |
|
} |
592 |
|
else if (VoiceStealing) { |
593 |
|
// first, get total amount of required voices (dependant on amount of layers) |
594 |
|
::gig::Region* pRegion = pEngineChannel->pInstrument->GetRegion(itNoteOnEvent->Param.Note.Key); |
595 |
|
if (!pRegion) return Pool<Voice>::Iterator(); // nothing defined for this MIDI key, so no voice needed |
596 |
|
int voicesRequired = pRegion->Layers; |
597 |
|
|
598 |
|
// now steal the (remaining) amount of voices |
599 |
|
for (int i = iLayer; i < voicesRequired; i++) |
600 |
|
StealVoice(pEngineChannel, itNoteOnEvent); |
601 |
|
|
602 |
|
// put note-on event into voice-stealing queue, so it will be reprocessed after killed voice died |
603 |
|
RTList<Event>::Iterator itStealEvent = pVoiceStealingQueue->allocAppend(); |
604 |
|
if (itStealEvent) { |
605 |
|
*itStealEvent = *itNoteOnEvent; // copy event |
606 |
|
itStealEvent->Param.Note.Layer = iLayer; |
607 |
|
itStealEvent->Param.Note.ReleaseTrigger = ReleaseTriggerVoice; |
608 |
|
} |
609 |
|
else dmsg(1,("Voice stealing queue full!\n")); |
610 |
|
} |
611 |
|
|
612 |
|
return Pool<Voice>::Iterator(); // no free voice or error |
613 |
|
} |
614 |
|
|
615 |
|
/** |
616 |
|
* Will be called by LaunchVoice() method in case there are no free |
617 |
|
* voices left. This method will select and kill one old voice for |
618 |
|
* voice stealing and postpone the note-on event until the selected |
619 |
|
* voice actually died. |
620 |
* |
* |
621 |
* @param pVoice - points to the voice to be killed |
* @param pEngineChannel - engine channel on which this event occured on |
622 |
|
* @param itNoteOnEvent - key, velocity and time stamp of the event |
623 |
*/ |
*/ |
624 |
void Engine::KillVoice(Voice* pVoice) { |
void Engine::StealVoice(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) { |
625 |
if (pVoice) { |
if (!pEventPool->poolIsEmpty()) { |
626 |
if (pVoice->IsActive()) pVoice->Kill(); |
|
627 |
|
RTList<uint>::Iterator iuiOldestKey; |
628 |
|
RTList<Voice>::Iterator itOldestVoice; |
629 |
|
|
630 |
|
// Select one voice for voice stealing |
631 |
|
switch (VOICE_STEAL_ALGORITHM) { |
632 |
|
|
633 |
|
// try to pick the oldest voice on the key where the new |
634 |
|
// voice should be spawned, if there is no voice on that |
635 |
|
// key, or no voice left to kill there, then procceed with |
636 |
|
// 'oldestkey' algorithm |
637 |
|
case voice_steal_algo_keymask: { |
638 |
|
midi_key_info_t* pOldestKey = &pEngineChannel->pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key]; |
639 |
|
if (itLastStolenVoice) { |
640 |
|
itOldestVoice = itLastStolenVoice; |
641 |
|
++itOldestVoice; |
642 |
|
} |
643 |
|
else { // no voice stolen in this audio fragment cycle yet |
644 |
|
itOldestVoice = pOldestKey->pActiveVoices->first(); |
645 |
|
} |
646 |
|
if (itOldestVoice) { |
647 |
|
iuiOldestKey = pOldestKey->itSelf; |
648 |
|
break; // selection succeeded |
649 |
|
} |
650 |
|
} // no break - intentional ! |
651 |
|
|
652 |
|
// try to pick the oldest voice on the oldest active key |
653 |
|
// (caution: must stay after 'keymask' algorithm !) |
654 |
|
case voice_steal_algo_oldestkey: { |
655 |
|
if (itLastStolenVoice) { |
656 |
|
midi_key_info_t* pOldestKey = &pEngineChannel->pMIDIKeyInfo[*iuiLastStolenKey]; |
657 |
|
itOldestVoice = itLastStolenVoice; |
658 |
|
++itOldestVoice; |
659 |
|
if (!itOldestVoice) { |
660 |
|
iuiOldestKey = iuiLastStolenKey; |
661 |
|
++iuiOldestKey; |
662 |
|
if (iuiOldestKey) { |
663 |
|
midi_key_info_t* pOldestKey = &pEngineChannel->pMIDIKeyInfo[*iuiOldestKey]; |
664 |
|
itOldestVoice = pOldestKey->pActiveVoices->first(); |
665 |
|
} |
666 |
|
else { |
667 |
|
dmsg(1,("gig::Engine: Warning, too less voices, even for voice stealing! - Better recompile with higher MAX_AUDIO_VOICES.\n")); |
668 |
|
return; |
669 |
|
} |
670 |
|
} |
671 |
|
else iuiOldestKey = iuiLastStolenKey; |
672 |
|
} |
673 |
|
else { // no voice stolen in this audio fragment cycle yet |
674 |
|
iuiOldestKey = pEngineChannel->pActiveKeys->first(); |
675 |
|
midi_key_info_t* pOldestKey = &pEngineChannel->pMIDIKeyInfo[*iuiOldestKey]; |
676 |
|
itOldestVoice = pOldestKey->pActiveVoices->first(); |
677 |
|
} |
678 |
|
break; |
679 |
|
} |
680 |
|
|
681 |
|
// don't steal anything |
682 |
|
case voice_steal_algo_none: |
683 |
|
default: { |
684 |
|
dmsg(1,("No free voice (voice stealing disabled)!\n")); |
685 |
|
return; |
686 |
|
} |
687 |
|
} |
688 |
|
|
689 |
midi_key_info_t* pKey = &pMIDIKeyInfo[pVoice->MIDIKey]; |
//FIXME: can be removed, just a sanity check for debugging |
690 |
|
if (!itOldestVoice->IsActive()) dmsg(1,("gig::Engine: ERROR, tried to steal a voice which was not active !!!\n")); |
691 |
|
|
692 |
|
// now kill the selected voice |
693 |
|
itOldestVoice->Kill(itNoteOnEvent); |
694 |
|
// remember which voice on which key we stole, so we can simply proceed for the next voice stealing |
695 |
|
this->itLastStolenVoice = itOldestVoice; |
696 |
|
this->iuiLastStolenKey = iuiOldestKey; |
697 |
|
} |
698 |
|
else dmsg(1,("Event pool emtpy!\n")); |
699 |
|
} |
700 |
|
|
701 |
|
/** |
702 |
|
* Removes the given voice from the MIDI key's list of active voices. |
703 |
|
* This method will be called when a voice went inactive, e.g. because |
704 |
|
* it finished to playback its sample, finished its release stage or |
705 |
|
* just was killed. |
706 |
|
* |
707 |
|
* @param pEngineChannel - engine channel on which this event occured on |
708 |
|
* @param itVoice - points to the voice to be freed |
709 |
|
*/ |
710 |
|
void Engine::FreeVoice(EngineChannel* pEngineChannel, Pool<Voice>::Iterator& itVoice) { |
711 |
|
if (itVoice) { |
712 |
|
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itVoice->MIDIKey]; |
713 |
|
|
714 |
|
uint keygroup = itVoice->KeyGroup; |
715 |
|
|
716 |
// free the voice object |
// free the voice object |
717 |
pVoicePool->free(pVoice); |
pVoicePool->free(itVoice); |
718 |
|
|
719 |
// check if there are no voices left on the MIDI key and update the key info if so |
// if no other voices left and member of a key group, remove from key group |
720 |
if (pKey->pActiveVoices->is_empty()) { |
if (pKey->pActiveVoices->isEmpty() && keygroup) { |
721 |
pKey->Active = false; |
uint** ppKeyGroup = &pEngineChannel->ActiveKeyGroups[keygroup]; |
722 |
pActiveKeys->free(pKey->pSelf); // remove key from list of active keys |
if (*ppKeyGroup == &*pKey->itSelf) *ppKeyGroup = NULL; // remove key from key group |
|
pKey->pSelf = NULL; |
|
|
dmsg(3,("Key has no more voices now\n")); |
|
723 |
} |
} |
724 |
} |
} |
725 |
else std::cerr << "Couldn't release voice! (pVoice == NULL)\n" << std::flush; |
else std::cerr << "Couldn't release voice! (!itVoice)\n" << std::flush; |
726 |
|
} |
727 |
|
|
728 |
|
/** |
729 |
|
* Called when there's no more voice left on a key, this call will |
730 |
|
* update the key info respectively. |
731 |
|
* |
732 |
|
* @param pEngineChannel - engine channel on which this event occured on |
733 |
|
* @param pKey - key which is now inactive |
734 |
|
*/ |
735 |
|
void Engine::FreeKey(EngineChannel* pEngineChannel, midi_key_info_t* pKey) { |
736 |
|
if (pKey->pActiveVoices->isEmpty()) { |
737 |
|
pKey->Active = false; |
738 |
|
pEngineChannel->pActiveKeys->free(pKey->itSelf); // remove key from list of active keys |
739 |
|
pKey->itSelf = RTList<uint>::Iterator(); |
740 |
|
pKey->ReleaseTrigger = false; |
741 |
|
pKey->pEvents->clear(); |
742 |
|
dmsg(3,("Key has no more voices now\n")); |
743 |
|
} |
744 |
|
else dmsg(1,("gig::Engine: Oops, tried to free a key which contains voices.\n")); |
745 |
} |
} |
746 |
|
|
747 |
/** |
/** |
748 |
* Reacts on supported control change commands (e.g. pitch bend wheel, |
* Reacts on supported control change commands (e.g. pitch bend wheel, |
749 |
* modulation wheel, aftertouch). |
* modulation wheel, aftertouch). |
750 |
* |
* |
751 |
* @param pControlChangeEvent - controller, value and time stamp of the event |
* @param pEngineChannel - engine channel on which this event occured on |
752 |
|
* @param itControlChangeEvent - controller, value and time stamp of the event |
753 |
*/ |
*/ |
754 |
void Engine::ProcessControlChange(Event* pControlChangeEvent) { |
void Engine::ProcessControlChange(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itControlChangeEvent) { |
755 |
dmsg(4,("Engine::ContinuousController cc=%d v=%d\n", pControlChangeEvent->Controller, pControlChangeEvent->Value)); |
dmsg(4,("Engine::ContinuousController cc=%d v=%d\n", itControlChangeEvent->Param.CC.Controller, itControlChangeEvent->Param.CC.Value)); |
756 |
|
|
757 |
switch (pControlChangeEvent->Controller) { |
switch (itControlChangeEvent->Param.CC.Controller) { |
758 |
case 64: { |
case 64: { |
759 |
if (pControlChangeEvent->Value >= 64 && !SustainPedal) { |
if (itControlChangeEvent->Param.CC.Value >= 64 && !pEngineChannel->SustainPedal) { |
760 |
dmsg(4,("PEDAL DOWN\n")); |
dmsg(4,("PEDAL DOWN\n")); |
761 |
SustainPedal = true; |
pEngineChannel->SustainPedal = true; |
762 |
|
|
763 |
// cancel release process of voices if necessary |
// cancel release process of voices if necessary |
764 |
uint* piKey = pActiveKeys->first(); |
RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first(); |
765 |
if (piKey) { |
if (iuiKey) { |
766 |
pControlChangeEvent->Type = Event::type_cancel_release; // transform event type |
itControlChangeEvent->Type = Event::type_cancel_release; // transform event type |
767 |
while (piKey) { |
while (iuiKey) { |
768 |
midi_key_info_t* pKey = &pMIDIKeyInfo[*piKey]; |
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey]; |
769 |
pActiveKeys->set_current(piKey); |
++iuiKey; |
|
piKey = pActiveKeys->next(); |
|
770 |
if (!pKey->KeyPressed) { |
if (!pKey->KeyPressed) { |
771 |
Event* pNewEvent = pKey->pEvents->alloc(); |
RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend(); |
772 |
if (pNewEvent) *pNewEvent = *pControlChangeEvent; // copy event to the key's own event list |
if (itNewEvent) *itNewEvent = *itControlChangeEvent; // copy event to the key's own event list |
773 |
else dmsg(1,("Event pool emtpy!\n")); |
else dmsg(1,("Event pool emtpy!\n")); |
774 |
} |
} |
775 |
} |
} |
776 |
} |
} |
777 |
} |
} |
778 |
if (pControlChangeEvent->Value < 64 && SustainPedal) { |
if (itControlChangeEvent->Param.CC.Value < 64 && pEngineChannel->SustainPedal) { |
779 |
dmsg(4,("PEDAL UP\n")); |
dmsg(4,("PEDAL UP\n")); |
780 |
SustainPedal = false; |
pEngineChannel->SustainPedal = false; |
781 |
|
|
782 |
// release voices if their respective key is not pressed |
// release voices if their respective key is not pressed |
783 |
uint* piKey = pActiveKeys->first(); |
RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first(); |
784 |
if (piKey) { |
if (iuiKey) { |
785 |
pControlChangeEvent->Type = Event::type_release; // transform event type |
itControlChangeEvent->Type = Event::type_release; // transform event type |
786 |
while (piKey) { |
while (iuiKey) { |
787 |
midi_key_info_t* pKey = &pMIDIKeyInfo[*piKey]; |
midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey]; |
788 |
pActiveKeys->set_current(piKey); |
++iuiKey; |
|
piKey = pActiveKeys->next(); |
|
789 |
if (!pKey->KeyPressed) { |
if (!pKey->KeyPressed) { |
790 |
Event* pNewEvent = pKey->pEvents->alloc(); |
RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend(); |
791 |
if (pNewEvent) *pNewEvent = *pControlChangeEvent; // copy event to the key's own event list |
if (itNewEvent) *itNewEvent = *itControlChangeEvent; // copy event to the key's own event list |
792 |
else dmsg(1,("Event pool emtpy!\n")); |
else dmsg(1,("Event pool emtpy!\n")); |
793 |
} |
} |
794 |
} |
} |
799 |
} |
} |
800 |
|
|
801 |
// update controller value in the engine's controller table |
// update controller value in the engine's controller table |
802 |
ControllerTable[pControlChangeEvent->Controller] = pControlChangeEvent->Value; |
pEngineChannel->ControllerTable[itControlChangeEvent->Param.CC.Controller] = itControlChangeEvent->Param.CC.Value; |
803 |
|
|
804 |
// 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 |
805 |
pEvents->move(pControlChangeEvent, pCCEvents); |
itControlChangeEvent.moveToEndOf(pCCEvents); |
806 |
|
} |
807 |
|
|
808 |
|
/** |
809 |
|
* Reacts on MIDI system exclusive messages. |
810 |
|
* |
811 |
|
* @param itSysexEvent - sysex data size and time stamp of the sysex event |
812 |
|
*/ |
813 |
|
void Engine::ProcessSysex(Pool<Event>::Iterator& itSysexEvent) { |
814 |
|
RingBuffer<uint8_t>::NonVolatileReader reader = pSysexBuffer->get_non_volatile_reader(); |
815 |
|
|
816 |
|
uint8_t exclusive_status, id; |
817 |
|
if (!reader.pop(&exclusive_status)) goto free_sysex_data; |
818 |
|
if (!reader.pop(&id)) goto free_sysex_data; |
819 |
|
if (exclusive_status != 0xF0) goto free_sysex_data; |
820 |
|
|
821 |
|
switch (id) { |
822 |
|
case 0x41: { // Roland |
823 |
|
uint8_t device_id, model_id, cmd_id; |
824 |
|
if (!reader.pop(&device_id)) goto free_sysex_data; |
825 |
|
if (!reader.pop(&model_id)) goto free_sysex_data; |
826 |
|
if (!reader.pop(&cmd_id)) goto free_sysex_data; |
827 |
|
if (model_id != 0x42 /*GS*/) goto free_sysex_data; |
828 |
|
if (cmd_id != 0x12 /*DT1*/) goto free_sysex_data; |
829 |
|
|
830 |
|
// command address |
831 |
|
uint8_t addr[3]; // 2 byte addr MSB, followed by 1 byte addr LSB) |
832 |
|
const RingBuffer<uint8_t>::NonVolatileReader checksum_reader = reader; // so we can calculate the check sum later |
833 |
|
if (reader.read(&addr[0], 3) != 3) goto free_sysex_data; |
834 |
|
if (addr[0] == 0x40 && addr[1] == 0x00) { // System Parameters |
835 |
|
} |
836 |
|
else if (addr[0] == 0x40 && addr[1] == 0x01) { // Common Parameters |
837 |
|
} |
838 |
|
else if (addr[0] == 0x40 && (addr[1] & 0xf0) == 0x10) { // Part Parameters (1) |
839 |
|
switch (addr[3]) { |
840 |
|
case 0x40: { // scale tuning |
841 |
|
uint8_t scale_tunes[12]; // detuning of all 12 semitones of an octave |
842 |
|
if (reader.read(&scale_tunes[0], 12) != 12) goto free_sysex_data; |
843 |
|
uint8_t checksum; |
844 |
|
if (!reader.pop(&checksum)) goto free_sysex_data; |
845 |
|
if (GSCheckSum(checksum_reader, 12) != checksum) goto free_sysex_data; |
846 |
|
for (int i = 0; i < 12; i++) scale_tunes[i] -= 64; |
847 |
|
AdjustScale((int8_t*) scale_tunes); |
848 |
|
break; |
849 |
|
} |
850 |
|
} |
851 |
|
} |
852 |
|
else if (addr[0] == 0x40 && (addr[1] & 0xf0) == 0x20) { // Part Parameters (2) |
853 |
|
} |
854 |
|
else if (addr[0] == 0x41) { // Drum Setup Parameters |
855 |
|
} |
856 |
|
break; |
857 |
|
} |
858 |
|
} |
859 |
|
|
860 |
|
free_sysex_data: // finally free sysex data |
861 |
|
pSysexBuffer->increment_read_ptr(itSysexEvent->Param.Sysex.Size); |
862 |
|
} |
863 |
|
|
864 |
|
/** |
865 |
|
* Calculates the Roland GS sysex check sum. |
866 |
|
* |
867 |
|
* @param AddrReader - reader which currently points to the first GS |
868 |
|
* command address byte of the GS sysex message in |
869 |
|
* question |
870 |
|
* @param DataSize - size of the GS message data (in bytes) |
871 |
|
*/ |
872 |
|
uint8_t Engine::GSCheckSum(const RingBuffer<uint8_t>::NonVolatileReader AddrReader, uint DataSize) { |
873 |
|
RingBuffer<uint8_t>::NonVolatileReader reader = AddrReader; |
874 |
|
uint bytes = 3 /*addr*/ + DataSize; |
875 |
|
uint8_t addr_and_data[bytes]; |
876 |
|
reader.read(&addr_and_data[0], bytes); |
877 |
|
uint8_t sum = 0; |
878 |
|
for (uint i = 0; i < bytes; i++) sum += addr_and_data[i]; |
879 |
|
return 128 - sum % 128; |
880 |
|
} |
881 |
|
|
882 |
|
/** |
883 |
|
* Allows to tune each of the twelve semitones of an octave. |
884 |
|
* |
885 |
|
* @param ScaleTunes - detuning of all twelve semitones (in cents) |
886 |
|
*/ |
887 |
|
void Engine::AdjustScale(int8_t ScaleTunes[12]) { |
888 |
|
memcpy(&this->ScaleTuning[0], &ScaleTunes[0], 12); //TODO: currently not sample accurate |
889 |
} |
} |
890 |
|
|
891 |
/** |
/** |
901 |
m[i+2] = val; |
m[i+2] = val; |
902 |
m[i+3] = val; |
m[i+3] = val; |
903 |
} |
} |
904 |
} |
} |
|
|
|
|
float Engine::Volume() { |
|
|
return GlobalVolume; |
|
|
} |
|
|
|
|
|
void Engine::Volume(float f) { |
|
|
GlobalVolume = f; |
|
|
} |
|
905 |
|
|
906 |
uint Engine::VoiceCount() { |
uint Engine::VoiceCount() { |
907 |
return ActiveVoiceCount; |
return ActiveVoiceCount; |
935 |
return "GigEngine"; |
return "GigEngine"; |
936 |
} |
} |
937 |
|
|
|
String Engine::InstrumentFileName() { |
|
|
return InstrumentFile; |
|
|
} |
|
|
|
|
|
int Engine::InstrumentIndex() { |
|
|
return InstrumentIdx; |
|
|
} |
|
|
|
|
|
int Engine::InstrumentStatus() { |
|
|
return InstrumentStat; |
|
|
} |
|
|
|
|
938 |
String Engine::Description() { |
String Engine::Description() { |
939 |
return "Gigasampler Engine"; |
return "Gigasampler Engine"; |
940 |
} |
} |
941 |
|
|
942 |
String Engine::Version() { |
String Engine::Version() { |
943 |
String s = "$Revision: 1.7 $"; |
String s = "$Revision: 1.26 $"; |
944 |
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 |
945 |
} |
} |
946 |
|
|