30 |
pEngine = NULL; |
pEngine = NULL; |
31 |
pInstrument = NULL; |
pInstrument = NULL; |
32 |
pEvents = NULL; // we allocate when we retrieve the right Engine object |
pEvents = NULL; // we allocate when we retrieve the right Engine object |
33 |
pEventQueue = new RingBuffer<Event>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0); |
pEventQueue = new RingBuffer<Event,false>(CONFIG_MAX_EVENTS_PER_FRAGMENT, 0); |
34 |
pActiveKeys = new Pool<uint>(128); |
pActiveKeys = new Pool<uint>(128); |
35 |
for (uint i = 0; i < 128; i++) { |
for (uint i = 0; i < 128; i++) { |
36 |
pMIDIKeyInfo[i].pActiveVoices = NULL; // we allocate when we retrieve the right Engine object |
pMIDIKeyInfo[i].pActiveVoices = NULL; // we allocate when we retrieve the right Engine object |
43 |
} |
} |
44 |
InstrumentIdx = -1; |
InstrumentIdx = -1; |
45 |
InstrumentStat = -1; |
InstrumentStat = -1; |
46 |
|
pChannelLeft = NULL; |
47 |
|
pChannelRight = NULL; |
48 |
AudioDeviceChannelLeft = -1; |
AudioDeviceChannelLeft = -1; |
49 |
AudioDeviceChannelRight = -1; |
AudioDeviceChannelRight = -1; |
50 |
pMidiInputPort = NULL; |
pMidiInputPort = NULL; |
61 |
if (pEventQueue) delete pEventQueue; |
if (pEventQueue) delete pEventQueue; |
62 |
if (pActiveKeys) delete pActiveKeys; |
if (pActiveKeys) delete pActiveKeys; |
63 |
if (pMIDIKeyInfo) delete[] pMIDIKeyInfo; |
if (pMIDIKeyInfo) delete[] pMIDIKeyInfo; |
64 |
|
RemoveAllFxSends(); |
65 |
} |
} |
66 |
|
|
67 |
/** |
/** |
163 |
* @see PrepareLoadInstrument() |
* @see PrepareLoadInstrument() |
164 |
*/ |
*/ |
165 |
void EngineChannel::LoadInstrument() { |
void EngineChannel::LoadInstrument() { |
166 |
|
::gig::Instrument* oldInstrument = pInstrument; |
|
if (pEngine) pEngine->DisableAndLock(); |
|
|
|
|
|
ResetInternal(); |
|
167 |
|
|
168 |
// free old instrument |
// free old instrument |
169 |
if (pInstrument) { |
if (oldInstrument) { |
170 |
// give old instrument back to instrument manager |
if (pEngine) { |
171 |
Engine::instruments.HandBack(pInstrument, this); |
// make sure we don't trigger any new notes with the |
172 |
|
// old instrument |
173 |
|
::gig::DimensionRegion** dimRegionsInUse = pEngine->ChangeInstrument(this, 0); |
174 |
|
|
175 |
|
// give old instrument back to instrument manager, but |
176 |
|
// keep the dimension regions and samples that are in |
177 |
|
// use |
178 |
|
Engine::instruments.HandBackInstrument(oldInstrument, this, dimRegionsInUse); |
179 |
|
} else { |
180 |
|
Engine::instruments.HandBack(oldInstrument, this); |
181 |
|
} |
182 |
} |
} |
183 |
|
|
184 |
// delete all key groups |
// delete all key groups |
185 |
ActiveKeyGroups.clear(); |
ActiveKeyGroups.clear(); |
186 |
|
|
187 |
// request gig instrument from instrument manager |
// request gig instrument from instrument manager |
188 |
|
::gig::Instrument* newInstrument; |
189 |
try { |
try { |
190 |
InstrumentManager::instrument_id_t instrid; |
InstrumentManager::instrument_id_t instrid; |
191 |
instrid.FileName = InstrumentFile; |
instrid.FileName = InstrumentFile; |
192 |
instrid.Index = InstrumentIdx; |
instrid.Index = InstrumentIdx; |
193 |
pInstrument = Engine::instruments.Borrow(instrid, this); |
newInstrument = Engine::instruments.Borrow(instrid, this); |
194 |
if (!pInstrument) { |
if (!newInstrument) { |
195 |
InstrumentStat = -1; |
InstrumentStat = -1; |
196 |
dmsg(1,("no instrument loaded!!!\n")); |
dmsg(1,("no instrument loaded!!!\n")); |
197 |
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
213 |
} |
} |
214 |
|
|
215 |
// rebuild ActiveKeyGroups map with key groups of current instrument |
// rebuild ActiveKeyGroups map with key groups of current instrument |
216 |
for (::gig::Region* pRegion = pInstrument->GetFirstRegion(); pRegion; pRegion = pInstrument->GetNextRegion()) |
for (::gig::Region* pRegion = newInstrument->GetFirstRegion(); pRegion; pRegion = newInstrument->GetNextRegion()) |
217 |
if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL; |
if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL; |
218 |
|
|
219 |
InstrumentIdxName = pInstrument->pInfo->Name; |
InstrumentIdxName = newInstrument->pInfo->Name; |
220 |
InstrumentStat = 100; |
InstrumentStat = 100; |
221 |
|
|
222 |
// inform audio driver for the need of two channels |
if (pEngine) pEngine->ChangeInstrument(this, newInstrument); |
223 |
try { |
else pInstrument = newInstrument; |
|
if (pEngine && pEngine->pAudioOutputDevice) |
|
|
pEngine->pAudioOutputDevice->AcquireChannels(2); // gig Engine only stereo |
|
|
} |
|
|
catch (AudioOutputException e) { |
|
|
String msg = "Audio output device unable to provide 2 audio channels, cause: " + e.Message(); |
|
|
throw Exception(msg); |
|
|
} |
|
|
|
|
|
if (pEngine) pEngine->Enable(); |
|
224 |
} |
} |
225 |
|
|
226 |
/** |
/** |
271 |
} |
} |
272 |
AudioDeviceChannelLeft = 0; |
AudioDeviceChannelLeft = 0; |
273 |
AudioDeviceChannelRight = 1; |
AudioDeviceChannelRight = 1; |
274 |
pOutputLeft = pAudioOut->Channel(0)->Buffer(); |
if (fxSends.empty()) { // render directly into the AudioDevice's output buffers |
275 |
pOutputRight = pAudioOut->Channel(1)->Buffer(); |
pChannelLeft = pAudioOut->Channel(AudioDeviceChannelLeft); |
276 |
|
pChannelRight = pAudioOut->Channel(AudioDeviceChannelRight); |
277 |
|
} else { // use local buffers for rendering and copy later |
278 |
|
// ensure the local buffers have the correct size |
279 |
|
if (pChannelLeft) delete pChannelLeft; |
280 |
|
if (pChannelRight) delete pChannelRight; |
281 |
|
pChannelLeft = new AudioChannel(0, pAudioOut->MaxSamplesPerCycle()); |
282 |
|
pChannelRight = new AudioChannel(1, pAudioOut->MaxSamplesPerCycle()); |
283 |
|
} |
284 |
|
if (pEngine->EngineDisabled.GetUnsafe()) pEngine->Enable(); |
285 |
MidiInputPort::AddSysexListener(pEngine); |
MidiInputPort::AddSysexListener(pEngine); |
286 |
} |
} |
287 |
|
|
308 |
Engine::FreeEngine(this, oldAudioDevice); |
Engine::FreeEngine(this, oldAudioDevice); |
309 |
AudioDeviceChannelLeft = -1; |
AudioDeviceChannelLeft = -1; |
310 |
AudioDeviceChannelRight = -1; |
AudioDeviceChannelRight = -1; |
311 |
|
if (!fxSends.empty()) { // free the local rendering buffers |
312 |
|
if (pChannelLeft) delete pChannelLeft; |
313 |
|
if (pChannelRight) delete pChannelRight; |
314 |
|
} |
315 |
|
pChannelLeft = NULL; |
316 |
|
pChannelRight = NULL; |
317 |
} |
} |
318 |
} |
} |
319 |
|
|
320 |
|
AudioOutputDevice* EngineChannel::GetAudioOutputDevice() { |
321 |
|
return (pEngine) ? pEngine->pAudioOutputDevice : NULL; |
322 |
|
} |
323 |
|
|
324 |
void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) { |
void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) { |
325 |
if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet."); |
if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet."); |
326 |
|
|
328 |
if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel)); |
if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel)); |
329 |
switch (EngineAudioChannel) { |
switch (EngineAudioChannel) { |
330 |
case 0: // left output channel |
case 0: // left output channel |
331 |
pOutputLeft = pChannel->Buffer(); |
if (fxSends.empty()) pChannelLeft = pChannel; |
332 |
AudioDeviceChannelLeft = AudioDeviceChannel; |
AudioDeviceChannelLeft = AudioDeviceChannel; |
333 |
break; |
break; |
334 |
case 1: // right output channel |
case 1: // right output channel |
335 |
pOutputRight = pChannel->Buffer(); |
if (fxSends.empty()) pChannelRight = pChannel; |
336 |
AudioDeviceChannelRight = AudioDeviceChannel; |
AudioDeviceChannelRight = AudioDeviceChannel; |
337 |
break; |
break; |
338 |
default: |
default: |
373 |
return midiChannel; |
return midiChannel; |
374 |
} |
} |
375 |
|
|
376 |
|
FxSend* EngineChannel::AddFxSend(uint8_t MidiCtrl, String Name) throw (Exception) { |
377 |
|
if (pEngine) pEngine->DisableAndLock(); |
378 |
|
FxSend* pFxSend = new FxSend(this, MidiCtrl, Name); |
379 |
|
if (fxSends.empty()) { |
380 |
|
if (pEngine && pEngine->pAudioOutputDevice) { |
381 |
|
AudioOutputDevice* pDevice = pEngine->pAudioOutputDevice; |
382 |
|
// create local render buffers |
383 |
|
pChannelLeft = new AudioChannel(0, pDevice->MaxSamplesPerCycle()); |
384 |
|
pChannelRight = new AudioChannel(1, pDevice->MaxSamplesPerCycle()); |
385 |
|
} else { |
386 |
|
// postpone local render buffer creation until audio device is assigned |
387 |
|
pChannelLeft = NULL; |
388 |
|
pChannelRight = NULL; |
389 |
|
} |
390 |
|
} |
391 |
|
fxSends.push_back(pFxSend); |
392 |
|
if (pEngine) pEngine->Enable(); |
393 |
|
return pFxSend; |
394 |
|
} |
395 |
|
|
396 |
|
FxSend* EngineChannel::GetFxSend(uint FxSendIndex) { |
397 |
|
return (FxSendIndex < fxSends.size()) ? fxSends[FxSendIndex] : NULL; |
398 |
|
} |
399 |
|
|
400 |
|
uint EngineChannel::GetFxSendCount() { |
401 |
|
return fxSends.size(); |
402 |
|
} |
403 |
|
|
404 |
|
void EngineChannel::RemoveFxSend(FxSend* pFxSend) { |
405 |
|
if (pEngine) pEngine->DisableAndLock(); |
406 |
|
for ( |
407 |
|
std::vector<FxSend*>::iterator iter = fxSends.begin(); |
408 |
|
iter != fxSends.end(); iter++ |
409 |
|
) { |
410 |
|
if (*iter == pFxSend) { |
411 |
|
delete pFxSend; |
412 |
|
fxSends.erase(iter); |
413 |
|
if (fxSends.empty()) { |
414 |
|
// destroy local render buffers |
415 |
|
if (pChannelLeft) delete pChannelLeft; |
416 |
|
if (pChannelRight) delete pChannelRight; |
417 |
|
// fallback to render directly into AudioOutputDevice's buffers |
418 |
|
if (pEngine && pEngine->pAudioOutputDevice) { |
419 |
|
pChannelLeft = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft); |
420 |
|
pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight); |
421 |
|
} else { // we update the pointers later |
422 |
|
pChannelLeft = NULL; |
423 |
|
pChannelRight = NULL; |
424 |
|
} |
425 |
|
} |
426 |
|
break; |
427 |
|
} |
428 |
|
} |
429 |
|
if (pEngine) pEngine->Enable(); |
430 |
|
} |
431 |
|
|
432 |
/** |
/** |
433 |
* Will be called by the MIDIIn Thread to let the audio thread trigger a new |
* Will be called by the MIDIIn Thread to let the audio thread trigger a new |
434 |
* voice for the given key. This method is meant for real time rendering, |
* voice for the given key. This method is meant for real time rendering, |
629 |
Pitch = 0; |
Pitch = 0; |
630 |
SustainPedal = false; |
SustainPedal = false; |
631 |
SostenutoPedal = false; |
SostenutoPedal = false; |
632 |
GlobalVolume = CONFIG_GLOBAL_ATTENUATION; |
GlobalVolume = 1.0f; |
633 |
MidiVolume = 1.0; |
MidiVolume = 1.0; |
634 |
GlobalPanLeft = 1.0f; |
GlobalPanLeft = 1.0f; |
635 |
GlobalPanRight = 1.0f; |
GlobalPanRight = 1.0f; |
651 |
* current audio cycle |
* current audio cycle |
652 |
*/ |
*/ |
653 |
void EngineChannel::ImportEvents(uint Samples) { |
void EngineChannel::ImportEvents(uint Samples) { |
654 |
RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
RingBuffer<Event,false>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
655 |
Event* pEvent; |
Event* pEvent; |
656 |
while (true) { |
while (true) { |
657 |
// get next event from input event queue |
// get next event from input event queue |
673 |
eventQueueReader.free(); // free all copied events from input queue |
eventQueueReader.free(); // free all copied events from input queue |
674 |
} |
} |
675 |
|
|
676 |
|
void EngineChannel::RemoveAllFxSends() { |
677 |
|
if (pEngine) pEngine->DisableAndLock(); |
678 |
|
if (!fxSends.empty()) { // free local render buffers |
679 |
|
if (pChannelLeft) { |
680 |
|
delete pChannelLeft; |
681 |
|
if (pEngine && pEngine->pAudioOutputDevice) { |
682 |
|
// fallback to render directly to the AudioOutputDevice's buffer |
683 |
|
pChannelLeft = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft); |
684 |
|
} else pChannelLeft = NULL; |
685 |
|
} |
686 |
|
if (pChannelRight) { |
687 |
|
delete pChannelRight; |
688 |
|
if (pEngine && pEngine->pAudioOutputDevice) { |
689 |
|
// fallback to render directly to the AudioOutputDevice's buffer |
690 |
|
pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight); |
691 |
|
} else pChannelRight = NULL; |
692 |
|
} |
693 |
|
} |
694 |
|
for (int i = 0; i < fxSends.size(); i++) delete fxSends[i]; |
695 |
|
fxSends.clear(); |
696 |
|
if (pEngine) pEngine->Enable(); |
697 |
|
} |
698 |
|
|
699 |
float EngineChannel::Volume() { |
float EngineChannel::Volume() { |
700 |
return GlobalVolume; |
return GlobalVolume; |
701 |
} |
} |