--- linuxsampler/trunk/src/engines/gig/Engine.cpp 2004/06/18 14:29:02 133 +++ linuxsampler/trunk/src/engines/gig/Engine.cpp 2004/09/07 09:32:21 233 @@ -65,6 +65,9 @@ InstrumentIdx = -1; InstrumentStat = -1; + AudioDeviceChannelLeft = -1; + AudioDeviceChannelRight = -1; + ResetInternal(); } @@ -149,6 +152,7 @@ SustainPedal = false; ActiveVoiceCount = 0; ActiveVoiceCountMax = 0; + GlobalVolume = 1.0; // set all MIDI controller values to zero memset(ControllerTable, 0x00, 128); @@ -278,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(); @@ -291,7 +302,6 @@ for (Voice* pVoice = pVoicePool->alloc(); pVoice; pVoice = pVoicePool->alloc()) { pVoice->pDiskThread = this->pDiskThread; - pVoice->SetOutput(pAudioOut); dmsg(3,("d")); } pVoicePool->clear(); @@ -329,6 +339,8 @@ AudioOutputDevice* olddevice = pAudioOutputDevice; pAudioOutputDevice = NULL; olddevice->Disconnect(this); + AudioDeviceChannelLeft = -1; + AudioDeviceChannelRight = -1; } } @@ -515,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); } /** @@ -564,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. @@ -675,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; } @@ -724,7 +784,7 @@ } String Engine::Version() { - String s = "$Revision: 1.7 $"; + String s = "$Revision: 1.9 $"; return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword }