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, 2006 Christian Schoenebeck * |
* Copyright (C) 2005 - 2007 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 * |
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 |
/** |
/** |
159 |
* This method will then actually start to load the instrument and block |
* This method will then actually start to load the instrument and block |
160 |
* the calling thread until loading was completed. |
* the calling thread until loading was completed. |
161 |
* |
* |
|
* @returns detailed description of the method call result |
|
162 |
* @see PrepareLoadInstrument() |
* @see PrepareLoadInstrument() |
163 |
*/ |
*/ |
164 |
void EngineChannel::LoadInstrument() { |
void EngineChannel::LoadInstrument() { |
165 |
|
::gig::Instrument* oldInstrument = pInstrument; |
|
if (pEngine) pEngine->DisableAndLock(); |
|
|
|
|
|
ResetInternal(); |
|
166 |
|
|
167 |
// free old instrument |
// free old instrument |
168 |
if (pInstrument) { |
if (oldInstrument) { |
169 |
// give old instrument back to instrument manager |
if (pEngine) { |
170 |
Engine::instruments.HandBack(pInstrument, this); |
// make sure we don't trigger any new notes with the |
171 |
|
// old instrument |
172 |
|
::gig::DimensionRegion** dimRegionsInUse = pEngine->ChangeInstrument(this, 0); |
173 |
|
|
174 |
|
// give old instrument back to instrument manager, but |
175 |
|
// keep the dimension regions and samples that are in |
176 |
|
// use |
177 |
|
Engine::instruments.HandBackInstrument(oldInstrument, this, dimRegionsInUse); |
178 |
|
} else { |
179 |
|
Engine::instruments.HandBack(oldInstrument, this); |
180 |
|
} |
181 |
} |
} |
182 |
|
|
183 |
// delete all key groups |
// delete all key groups |
184 |
ActiveKeyGroups.clear(); |
ActiveKeyGroups.clear(); |
185 |
|
|
186 |
// request gig instrument from instrument manager |
// request gig instrument from instrument manager |
187 |
|
::gig::Instrument* newInstrument; |
188 |
try { |
try { |
189 |
instrument_id_t instrid; |
InstrumentManager::instrument_id_t instrid; |
190 |
instrid.FileName = InstrumentFile; |
instrid.FileName = InstrumentFile; |
191 |
instrid.iInstrument = InstrumentIdx; |
instrid.Index = InstrumentIdx; |
192 |
pInstrument = Engine::instruments.Borrow(instrid, this); |
newInstrument = Engine::instruments.Borrow(instrid, this); |
193 |
if (!pInstrument) { |
if (!newInstrument) { |
194 |
InstrumentStat = -1; |
throw InstrumentManagerException("resource was not created"); |
|
dmsg(1,("no instrument loaded!!!\n")); |
|
|
exit(EXIT_FAILURE); |
|
195 |
} |
} |
196 |
} |
} |
197 |
catch (RIFF::Exception e) { |
catch (RIFF::Exception e) { |
199 |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message; |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message; |
200 |
throw Exception(msg); |
throw Exception(msg); |
201 |
} |
} |
202 |
catch (InstrumentResourceManagerException e) { |
catch (InstrumentManagerException e) { |
203 |
InstrumentStat = -3; |
InstrumentStat = -3; |
204 |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message(); |
String msg = "gig::Engine error: Failed to load instrument, cause: " + e.Message(); |
205 |
throw Exception(msg); |
throw Exception(msg); |
210 |
} |
} |
211 |
|
|
212 |
// rebuild ActiveKeyGroups map with key groups of current instrument |
// rebuild ActiveKeyGroups map with key groups of current instrument |
213 |
for (::gig::Region* pRegion = pInstrument->GetFirstRegion(); pRegion; pRegion = pInstrument->GetNextRegion()) |
for (::gig::Region* pRegion = newInstrument->GetFirstRegion(); pRegion; pRegion = newInstrument->GetNextRegion()) |
214 |
if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL; |
if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL; |
215 |
|
|
216 |
InstrumentIdxName = pInstrument->pInfo->Name; |
InstrumentIdxName = newInstrument->pInfo->Name; |
217 |
InstrumentStat = 100; |
InstrumentStat = 100; |
218 |
|
|
219 |
// inform audio driver for the need of two channels |
if (pEngine) pEngine->ChangeInstrument(this, newInstrument); |
220 |
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(); |
|
221 |
} |
} |
222 |
|
|
223 |
/** |
/** |
268 |
} |
} |
269 |
AudioDeviceChannelLeft = 0; |
AudioDeviceChannelLeft = 0; |
270 |
AudioDeviceChannelRight = 1; |
AudioDeviceChannelRight = 1; |
271 |
pOutputLeft = pAudioOut->Channel(0)->Buffer(); |
if (fxSends.empty()) { // render directly into the AudioDevice's output buffers |
272 |
pOutputRight = pAudioOut->Channel(1)->Buffer(); |
pChannelLeft = pAudioOut->Channel(AudioDeviceChannelLeft); |
273 |
|
pChannelRight = pAudioOut->Channel(AudioDeviceChannelRight); |
274 |
|
} else { // use local buffers for rendering and copy later |
275 |
|
// ensure the local buffers have the correct size |
276 |
|
if (pChannelLeft) delete pChannelLeft; |
277 |
|
if (pChannelRight) delete pChannelRight; |
278 |
|
pChannelLeft = new AudioChannel(0, pAudioOut->MaxSamplesPerCycle()); |
279 |
|
pChannelRight = new AudioChannel(1, pAudioOut->MaxSamplesPerCycle()); |
280 |
|
} |
281 |
|
if (pEngine->EngineDisabled.GetUnsafe()) pEngine->Enable(); |
282 |
MidiInputPort::AddSysexListener(pEngine); |
MidiInputPort::AddSysexListener(pEngine); |
283 |
} |
} |
284 |
|
|
305 |
Engine::FreeEngine(this, oldAudioDevice); |
Engine::FreeEngine(this, oldAudioDevice); |
306 |
AudioDeviceChannelLeft = -1; |
AudioDeviceChannelLeft = -1; |
307 |
AudioDeviceChannelRight = -1; |
AudioDeviceChannelRight = -1; |
308 |
|
if (!fxSends.empty()) { // free the local rendering buffers |
309 |
|
if (pChannelLeft) delete pChannelLeft; |
310 |
|
if (pChannelRight) delete pChannelRight; |
311 |
|
} |
312 |
|
pChannelLeft = NULL; |
313 |
|
pChannelRight = NULL; |
314 |
} |
} |
315 |
} |
} |
316 |
|
|
317 |
|
AudioOutputDevice* EngineChannel::GetAudioOutputDevice() { |
318 |
|
return (pEngine) ? pEngine->pAudioOutputDevice : NULL; |
319 |
|
} |
320 |
|
|
321 |
void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) { |
void EngineChannel::SetOutputChannel(uint EngineAudioChannel, uint AudioDeviceChannel) { |
322 |
if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet."); |
if (!pEngine || !pEngine->pAudioOutputDevice) throw AudioOutputException("No audio output device connected yet."); |
323 |
|
|
325 |
if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel)); |
if (!pChannel) throw AudioOutputException("Invalid audio output device channel " + ToString(AudioDeviceChannel)); |
326 |
switch (EngineAudioChannel) { |
switch (EngineAudioChannel) { |
327 |
case 0: // left output channel |
case 0: // left output channel |
328 |
pOutputLeft = pChannel->Buffer(); |
if (fxSends.empty()) pChannelLeft = pChannel; |
329 |
AudioDeviceChannelLeft = AudioDeviceChannel; |
AudioDeviceChannelLeft = AudioDeviceChannel; |
330 |
break; |
break; |
331 |
case 1: // right output channel |
case 1: // right output channel |
332 |
pOutputRight = pChannel->Buffer(); |
if (fxSends.empty()) pChannelRight = pChannel; |
333 |
AudioDeviceChannelRight = AudioDeviceChannel; |
AudioDeviceChannelRight = AudioDeviceChannel; |
334 |
break; |
break; |
335 |
default: |
default: |
370 |
return midiChannel; |
return midiChannel; |
371 |
} |
} |
372 |
|
|
373 |
|
FxSend* EngineChannel::AddFxSend(uint8_t MidiCtrl, String Name) throw (Exception) { |
374 |
|
if (pEngine) pEngine->DisableAndLock(); |
375 |
|
FxSend* pFxSend = new FxSend(this, MidiCtrl, Name); |
376 |
|
if (fxSends.empty()) { |
377 |
|
if (pEngine && pEngine->pAudioOutputDevice) { |
378 |
|
AudioOutputDevice* pDevice = pEngine->pAudioOutputDevice; |
379 |
|
// create local render buffers |
380 |
|
pChannelLeft = new AudioChannel(0, pDevice->MaxSamplesPerCycle()); |
381 |
|
pChannelRight = new AudioChannel(1, pDevice->MaxSamplesPerCycle()); |
382 |
|
} else { |
383 |
|
// postpone local render buffer creation until audio device is assigned |
384 |
|
pChannelLeft = NULL; |
385 |
|
pChannelRight = NULL; |
386 |
|
} |
387 |
|
} |
388 |
|
fxSends.push_back(pFxSend); |
389 |
|
if (pEngine) pEngine->Enable(); |
390 |
|
fireFxSendCountChanged(iSamplerChannelIndex, GetFxSendCount()); |
391 |
|
|
392 |
|
return pFxSend; |
393 |
|
} |
394 |
|
|
395 |
|
FxSend* EngineChannel::GetFxSend(uint FxSendIndex) { |
396 |
|
return (FxSendIndex < fxSends.size()) ? fxSends[FxSendIndex] : NULL; |
397 |
|
} |
398 |
|
|
399 |
|
uint EngineChannel::GetFxSendCount() { |
400 |
|
return fxSends.size(); |
401 |
|
} |
402 |
|
|
403 |
|
void EngineChannel::RemoveFxSend(FxSend* pFxSend) { |
404 |
|
if (pEngine) pEngine->DisableAndLock(); |
405 |
|
for ( |
406 |
|
std::vector<FxSend*>::iterator iter = fxSends.begin(); |
407 |
|
iter != fxSends.end(); iter++ |
408 |
|
) { |
409 |
|
if (*iter == pFxSend) { |
410 |
|
delete pFxSend; |
411 |
|
fxSends.erase(iter); |
412 |
|
if (fxSends.empty()) { |
413 |
|
// destroy local render buffers |
414 |
|
if (pChannelLeft) delete pChannelLeft; |
415 |
|
if (pChannelRight) delete pChannelRight; |
416 |
|
// fallback to render directly into AudioOutputDevice's buffers |
417 |
|
if (pEngine && pEngine->pAudioOutputDevice) { |
418 |
|
pChannelLeft = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft); |
419 |
|
pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight); |
420 |
|
} else { // we update the pointers later |
421 |
|
pChannelLeft = NULL; |
422 |
|
pChannelRight = NULL; |
423 |
|
} |
424 |
|
} |
425 |
|
break; |
426 |
|
} |
427 |
|
} |
428 |
|
if (pEngine) pEngine->Enable(); |
429 |
|
fireFxSendCountChanged(iSamplerChannelIndex, GetFxSendCount()); |
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; |
634 |
GlobalPanLeft = 1.0f; |
GlobalPanLeft = 1.0f; |
635 |
GlobalPanRight = 1.0f; |
GlobalPanRight = 1.0f; |
636 |
|
GlobalTranspose = 0; |
637 |
// set all MIDI controller values to zero |
// set all MIDI controller values to zero |
638 |
memset(ControllerTable, 0x00, 129); |
memset(ControllerTable, 0x00, 129); |
639 |
|
// reset all FX Send levels |
640 |
|
for ( |
641 |
|
std::vector<FxSend*>::iterator iter = fxSends.begin(); |
642 |
|
iter != fxSends.end(); iter++ |
643 |
|
) { |
644 |
|
(*iter)->Reset(); |
645 |
|
} |
646 |
} |
} |
647 |
|
|
648 |
/** |
/** |
659 |
* current audio cycle |
* current audio cycle |
660 |
*/ |
*/ |
661 |
void EngineChannel::ImportEvents(uint Samples) { |
void EngineChannel::ImportEvents(uint Samples) { |
662 |
RingBuffer<Event>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
RingBuffer<Event,false>::NonVolatileReader eventQueueReader = pEventQueue->get_non_volatile_reader(); |
663 |
Event* pEvent; |
Event* pEvent; |
664 |
while (true) { |
while (true) { |
665 |
// get next event from input event queue |
// get next event from input event queue |
681 |
eventQueueReader.free(); // free all copied events from input queue |
eventQueueReader.free(); // free all copied events from input queue |
682 |
} |
} |
683 |
|
|
684 |
|
void EngineChannel::RemoveAllFxSends() { |
685 |
|
if (pEngine) pEngine->DisableAndLock(); |
686 |
|
if (!fxSends.empty()) { // free local render buffers |
687 |
|
if (pChannelLeft) { |
688 |
|
delete pChannelLeft; |
689 |
|
if (pEngine && pEngine->pAudioOutputDevice) { |
690 |
|
// fallback to render directly to the AudioOutputDevice's buffer |
691 |
|
pChannelLeft = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelLeft); |
692 |
|
} else pChannelLeft = NULL; |
693 |
|
} |
694 |
|
if (pChannelRight) { |
695 |
|
delete pChannelRight; |
696 |
|
if (pEngine && pEngine->pAudioOutputDevice) { |
697 |
|
// fallback to render directly to the AudioOutputDevice's buffer |
698 |
|
pChannelRight = pEngine->pAudioOutputDevice->Channel(AudioDeviceChannelRight); |
699 |
|
} else pChannelRight = NULL; |
700 |
|
} |
701 |
|
} |
702 |
|
for (int i = 0; i < fxSends.size(); i++) delete fxSends[i]; |
703 |
|
fxSends.clear(); |
704 |
|
if (pEngine) pEngine->Enable(); |
705 |
|
} |
706 |
|
|
707 |
float EngineChannel::Volume() { |
float EngineChannel::Volume() { |
708 |
return GlobalVolume; |
return GlobalVolume; |
709 |
} |
} |