--- linuxsampler/trunk/src/engines/gig/Engine.cpp 2004/04/26 17:15:51 53 +++ linuxsampler/trunk/src/engines/gig/Engine.cpp 2004/09/07 09:32:21 233 @@ -2,7 +2,7 @@ * * * LinuxSampler - modular, streaming capable sampler * * * - * Copyright (C) 2003 by Benno Senoner and Christian Schoenebeck * + * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * @@ -59,6 +59,14 @@ pVoicePool->clear(); pSynthesisParameters[0] = NULL; // we allocate when an audio device is connected + pBasicFilterParameters = NULL; + pMainFilterParameters = NULL; + + InstrumentIdx = -1; + InstrumentStat = -1; + + AudioDeviceChannelLeft = -1; + AudioDeviceChannelRight = -1; ResetInternal(); } @@ -85,13 +93,15 @@ if (pVoicePool) delete pVoicePool; if (pActiveKeys) delete pActiveKeys; if (pEventGenerator) delete pEventGenerator; + if (pMainFilterParameters) delete[] pMainFilterParameters; + if (pBasicFilterParameters) delete[] pBasicFilterParameters; if (pSynthesisParameters[0]) delete[] pSynthesisParameters[0]; } void Engine::Enable() { dmsg(3,("gig::Engine: enabling\n")); EngineDisabled.PushAndUnlock(false, 2); // set condition object 'EngineDisabled' to false (wait max. 2s) - dmsg(1,("gig::Engine: enabled (val=%d)\n", EngineDisabled.GetUnsafe())); + dmsg(3,("gig::Engine: enabled (val=%d)\n", EngineDisabled.GetUnsafe())); } void Engine::Disable() { @@ -142,6 +152,7 @@ SustainPedal = false; ActiveVoiceCount = 0; ActiveVoiceCountMax = 0; + GlobalVolume = 1.0; // set all MIDI controller values to zero memset(ControllerTable, 0x00, 128); @@ -191,6 +202,10 @@ Instruments.HandBack(pInstrument, this); } + InstrumentFile = FileName; + InstrumentIdx = Instrument; + InstrumentStat = 0; + // request gig instrument from instrument manager try { instrument_id_t instrid; @@ -198,22 +213,28 @@ 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 @@ -261,6 +282,13 @@ throw LinuxSamplerException(msg); } + this->AudioDeviceChannelLeft = 0; + this->AudioDeviceChannelRight = 1; + this->pOutputLeft = pAudioOutputDevice->Channel(0)->Buffer(); + this->pOutputRight = pAudioOutputDevice->Channel(1)->Buffer(); + this->MaxSamplesPerCycle = pAudioOutputDevice->MaxSamplesPerCycle(); + this->SampleRate = pAudioOutputDevice->SampleRate(); + // (re)create disk thread if (this->pDiskThread) { this->pDiskThread->StopThread(); @@ -274,7 +302,6 @@ for (Voice* pVoice = pVoicePool->alloc(); pVoice; pVoice = pVoicePool->alloc()) { pVoice->pDiskThread = this->pDiskThread; - pVoice->SetOutput(pAudioOut); dmsg(3,("d")); } pVoicePool->clear(); @@ -289,6 +316,12 @@ for (int dst = 1; dst < Event::destination_count; dst++) pSynthesisParameters[dst] = pSynthesisParameters[dst - 1] + pAudioOut->MaxSamplesPerCycle(); + // (re)allocate biquad filter parameter sequence + if (pBasicFilterParameters) delete[] pBasicFilterParameters; + if (pMainFilterParameters) delete[] pMainFilterParameters; + pBasicFilterParameters = new biquad_param_t[pAudioOut->MaxSamplesPerCycle()]; + pMainFilterParameters = new biquad_param_t[pAudioOut->MaxSamplesPerCycle()]; + dmsg(1,("Starting disk thread...")); pDiskThread->StartThread(); dmsg(1,("OK\n")); @@ -306,6 +339,8 @@ AudioOutputDevice* olddevice = pAudioOutputDevice; pAudioOutputDevice = NULL; olddevice->Disconnect(this); + AudioDeviceChannelLeft = -1; + AudioDeviceChannelRight = -1; } } @@ -492,21 +527,8 @@ pEvents->move(pNoteOnEvent, pKey->pEvents); // move event to the key's own event list } - // 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; - } - } - else std::cerr << "No free voice!" << std::endl << std::flush; + // allocate and trigger a new voice for the key + LaunchVoice(pNoteOnEvent); } /** @@ -541,6 +563,35 @@ } /** + * Allocates and triggers a new voice. This method will usually be + * called by the ProcessNoteOn() method and by the voices itself + * (e.g. to spawn further voices on the same key for layered sounds). + * + * @param pNoteOnEvent - key, velocity and time stamp of the event + * @param iLayer - layer index for the new voice (optional - only + * in case of layered sounds of course) + */ + void Engine::LaunchVoice(Event* pNoteOnEvent, int iLayer) { + midi_key_info_t* pKey = &pMIDIKeyInfo[pNoteOnEvent->Key]; + + // 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, iLayer) < 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; + } + } + else std::cerr << "No free voice!" << std::endl << std::flush; + } + + /** * Immediately kills the voice given with pVoice (no matter if sustain is * pressed or not) and removes it from the MIDI key's list of active voice. * This method will e.g. be called if a voice went inactive by itself. @@ -635,7 +686,13 @@ */ void Engine::ResetSynthesisParameters(Event::destination_t dst, float val) { int maxsamples = pAudioOutputDevice->MaxSamplesPerCycle(); - for (int i = 0; i < maxsamples; i++) pSynthesisParameters[dst][i] = val; + float* m = &pSynthesisParameters[dst][0]; + for (int i = 0; i < maxsamples; i += 4) { + m[i] = val; + m[i+1] = val; + m[i+2] = val; + m[i+3] = val; + } } float Engine::Volume() { @@ -646,6 +703,38 @@ 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)); + } + } + uint Engine::VoiceCount() { return ActiveVoiceCount; } @@ -674,12 +763,29 @@ return pDiskThread->GetBufferFillPercentage(); } + String Engine::EngineName() { + return "GigEngine"; + } + + String Engine::InstrumentFileName() { + return InstrumentFile; + } + + int Engine::InstrumentIndex() { + return InstrumentIdx; + } + + int Engine::InstrumentStatus() { + return InstrumentStat; + } + String Engine::Description() { return "Gigasampler Engine"; } String Engine::Version() { - return "0.0.1-0cvs20040423"; + String s = "$Revision: 1.9 $"; + return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword } }} // namespace LinuxSampler::gig