--- linuxsampler/trunk/src/engines/gig/Engine.cpp 2004/08/22 14:46:47 225 +++ linuxsampler/trunk/src/engines/gig/Engine.cpp 2004/09/12 14:48:19 239 @@ -166,6 +166,10 @@ pMIDIKeyInfo[i].pSelf = NULL; } + // reset all key groups + map::iterator iter = ActiveKeyGroups.begin(); + for (; iter != ActiveKeyGroups.end(); iter++) iter->second = NULL; + // reset all voices for (Voice* pVoice = pVoicePool->alloc(); pVoice; pVoice = pVoicePool->alloc()) { pVoice->Reset(); @@ -206,6 +210,9 @@ InstrumentIdx = Instrument; InstrumentStat = 0; + // delete all key groups + ActiveKeyGroups.clear(); + // request gig instrument from instrument manager try { instrument_id_t instrid; @@ -233,6 +240,10 @@ throw LinuxSamplerException("gig::Engine error: Failed to load instrument, cause: Unknown exception while trying to parse gig file."); } + // rebuild ActiveKeyGroups map with key groups of current instrument + for (::gig::Region* pRegion = pInstrument->GetFirstRegion(); pRegion; pRegion = pInstrument->GetNextRegion()) + if (pRegion->KeyGroup) ActiveKeyGroups[pRegion->KeyGroup] = NULL; + InstrumentStat = 100; // inform audio driver for the need of two channels @@ -264,7 +275,7 @@ * update process was completed, so we can continue with playback. */ void Engine::ResourceUpdated(::gig::Instrument* pOldResource, ::gig::Instrument* pNewResource, void* pUpdateArg) { - this->pInstrument = pNewResource; + this->pInstrument = pNewResource; //TODO: there are couple of engine parameters we should update here as well if the instrument was updated (see LoadInstrument()) Enable(); } @@ -433,7 +444,7 @@ pVoice->Render(Samples); if (pVoice->IsActive()) active_voices++; // still active else { // voice reached end, is now inactive - KillVoice(pVoice); // remove voice from the list of active voices + KillVoiceImmediately(pVoice); // remove voice from the list of active voices } } pKey->pEvents->clear(); // free all events on the key @@ -523,25 +534,19 @@ // cancel release process of voices on this key if needed if (pKey->Active && !SustainPedal) { - pNoteOnEvent->Type = Event::type_cancel_release; // transform event type - 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; + Event* pCancelReleaseEvent = pKey->pEvents->alloc(); + if (pCancelReleaseEvent) { + *pCancelReleaseEvent = *pNoteOnEvent; + pCancelReleaseEvent->Type = Event::type_cancel_release; // transform event type } + else dmsg(1,("Event pool emtpy!\n")); } - else std::cerr << "No free voice!" << std::endl << std::flush; + + // allocate and trigger a new voice for the key + LaunchVoice(pNoteOnEvent); + + // finally move note on event to the key's own event list + pEvents->move(pNoteOnEvent, pKey->pEvents); } /** @@ -576,15 +581,60 @@ } /** + * 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 { // on success + uint** ppKeyGroup = NULL; + if (pNewVoice->KeyGroup) { // if this voice / key belongs to a key group + ppKeyGroup = &ActiveKeyGroups[pNewVoice->KeyGroup]; + if (*ppKeyGroup) { // if there's already an active key in that key group + midi_key_info_t* pOtherKey = &pMIDIKeyInfo[**ppKeyGroup]; + // kill all voices on the (other) key + Voice* pVoiceToBeKilled = pOtherKey->pActiveVoices->first(); + for (; pVoiceToBeKilled; pVoiceToBeKilled = pOtherKey->pActiveVoices->next()) + if (pVoiceToBeKilled != pNewVoice) pVoiceToBeKilled->Kill(pNoteOnEvent); + } + } + if (!pKey->Active) { // mark as active key + pKey->Active = true; + pKey->pSelf = pActiveKeys->alloc(); + *pKey->pSelf = pNoteOnEvent->Key; + } + if (pNewVoice->KeyGroup) { + *ppKeyGroup = pKey->pSelf; // put key as the (new) active key to its key group + } + } + } + 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. * * @param pVoice - points to the voice to be killed */ - void Engine::KillVoice(Voice* pVoice) { + void Engine::KillVoiceImmediately(Voice* pVoice) { if (pVoice) { - if (pVoice->IsActive()) pVoice->Kill(); + if (pVoice->IsActive()) pVoice->KillImmediately(); midi_key_info_t* pKey = &pMIDIKeyInfo[pVoice->MIDIKey]; @@ -593,6 +643,10 @@ // check if there are no voices left on the MIDI key and update the key info if so if (pKey->pActiveVoices->is_empty()) { + if (pVoice->KeyGroup) { // if voice / key belongs to a key group + uint** ppKeyGroup = &ActiveKeyGroups[pVoice->KeyGroup]; + if (*ppKeyGroup == pKey->pSelf) *ppKeyGroup = NULL; // remove key from key group + } pKey->Active = false; pActiveKeys->free(pKey->pSelf); // remove key from list of active keys pKey->pSelf = NULL; @@ -768,7 +822,7 @@ } String Engine::Version() { - String s = "$Revision: 1.8 $"; + String s = "$Revision: 1.10 $"; return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword }