/[svn]/linuxsampler/trunk/src/engines/gig/Engine.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/engines/gig/Engine.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 554 by schoenebeck, Thu May 19 19:25:14 2005 UTC revision 705 by schoenebeck, Wed Jul 20 21:43:23 2005 UTC
# Line 119  namespace LinuxSampler { namespace gig { Line 119  namespace LinuxSampler { namespace gig {
119          pMainFilterParameters   = NULL;          pMainFilterParameters   = NULL;
120    
121          ResetInternal();          ResetInternal();
122            ResetScaleTuning();
123      }      }
124    
125      /**      /**
# Line 171  namespace LinuxSampler { namespace gig { Line 172  namespace LinuxSampler { namespace gig {
172      void Engine::Reset() {      void Engine::Reset() {
173          DisableAndLock();          DisableAndLock();
174          ResetInternal();          ResetInternal();
175            ResetScaleTuning();
176          Enable();          Enable();
177      }      }
178    
# Line 184  namespace LinuxSampler { namespace gig { Line 186  namespace LinuxSampler { namespace gig {
186    
187          // reset voice stealing parameters          // reset voice stealing parameters
188          pVoiceStealingQueue->clear();          pVoiceStealingQueue->clear();
189          itLastStolenVoice  = RTList<Voice>::Iterator();          itLastStolenVoice          = RTList<Voice>::Iterator();
190          iuiLastStolenKey   = RTList<uint>::Iterator();          itLastStolenVoiceGlobally  = RTList<Voice>::Iterator();
191          pLastStolenChannel = NULL;          iuiLastStolenKey           = RTList<uint>::Iterator();
192            iuiLastStolenKeyGlobally   = RTList<uint>::Iterator();
193          // reset to normal chromatic scale (means equal temper)          pLastStolenChannel         = NULL;
         memset(&ScaleTuning[0], 0x00, 12);  
194    
195          // reset all voices          // reset all voices
196          for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) {          for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) {
# Line 205  namespace LinuxSampler { namespace gig { Line 206  namespace LinuxSampler { namespace gig {
206      }      }
207    
208      /**      /**
209         * Reset to normal, chromatic scale (means equal tempered).
210         */
211        void Engine::ResetScaleTuning() {
212            memset(&ScaleTuning[0], 0x00, 12);
213        }
214    
215        /**
216       * Connect this engine instance with the given audio output device.       * Connect this engine instance with the given audio output device.
217       * This method will be called when an Engine instance is created.       * This method will be called when an Engine instance is created.
218       * All of the engine's data structures which are dependant to the used       * All of the engine's data structures which are dependant to the used
# Line 232  namespace LinuxSampler { namespace gig { Line 240  namespace LinuxSampler { namespace gig {
240    
241          // FIXME: audio drivers with varying fragment sizes might be a problem here          // FIXME: audio drivers with varying fragment sizes might be a problem here
242          MaxFadeOutPos = MaxSamplesPerCycle - int(double(SampleRate) * CONFIG_EG_MIN_RELEASE_TIME) - 1;          MaxFadeOutPos = MaxSamplesPerCycle - int(double(SampleRate) * CONFIG_EG_MIN_RELEASE_TIME) - 1;
243          if (MaxFadeOutPos < 0)          if (MaxFadeOutPos < 0) {
244              throw LinuxSamplerException("CONFIG_EG_MIN_RELEASE_TIME too big for current audio fragment size / sampling rate!");              std::cerr << "gig::Engine: WARNING, CONFIG_EG_MIN_RELEASE_TIME "
245                          << "too big for current audio fragment size & sampling rate! "
246                          << "May lead to click sounds if voice stealing chimes in!\n" << std::flush;
247                // force volume ramp downs at the beginning of each fragment
248                MaxFadeOutPos = 0;
249                // lower minimum release time
250                const float minReleaseTime = (float) MaxSamplesPerCycle / (float) SampleRate;
251                for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) {
252                    iterVoice->pEG1->CalculateFadeOutCoeff(minReleaseTime, SampleRate);
253                }
254                pVoicePool->clear();
255            }
256    
257          // (re)create disk thread          // (re)create disk thread
258          if (this->pDiskThread) {          if (this->pDiskThread) {
# Line 352  namespace LinuxSampler { namespace gig { Line 371  namespace LinuxSampler { namespace gig {
371          // update time of start and end of this audio fragment (as events' time stamps relate to this)          // update time of start and end of this audio fragment (as events' time stamps relate to this)
372          pEventGenerator->UpdateFragmentTime(Samples);          pEventGenerator->UpdateFragmentTime(Samples);
373    
374            // We only allow a maximum of CONFIG_MAX_VOICES voices to be spawned
375            // in each audio fragment. All subsequent request for spawning new
376            // voices in the same audio fragment will be ignored.
377            VoiceSpawnsLeft = CONFIG_MAX_VOICES;
378    
379          // get all events from the engine's global input event queue which belong to the current fragment          // get all events from the engine's global input event queue which belong to the current fragment
380          // (these are usually just SysEx messages)          // (these are usually just SysEx messages)
381          ImportEvents(Samples);          ImportEvents(Samples);
# Line 370  namespace LinuxSampler { namespace gig { Line 394  namespace LinuxSampler { namespace gig {
394              }              }
395          }          }
396    
         // We only allow a maximum of CONFIG_MAX_VOICES voices to be stolen  
         // in each audio fragment. All subsequent request for spawning new  
         // voices in the same audio fragment will be ignored.  
         VoiceTheftsLeft = CONFIG_MAX_VOICES;  
   
397          // reset internal voice counter (just for statistic of active voices)          // reset internal voice counter (just for statistic of active voices)
398          ActiveVoiceCountTemp = 0;          ActiveVoiceCountTemp = 0;
399    
   
400          // handle events on all engine channels          // handle events on all engine channels
401          for (int i = 0; i < engineChannels.size(); i++) {          for (int i = 0; i < engineChannels.size(); i++) {
402              if (!engineChannels[i]->pInstrument) continue; // ignore if no instrument loaded              if (!engineChannels[i]->pInstrument) continue; // ignore if no instrument loaded
# Line 406  namespace LinuxSampler { namespace gig { Line 424  namespace LinuxSampler { namespace gig {
424    
425          // reset voice stealing for the next audio fragment          // reset voice stealing for the next audio fragment
426          pVoiceStealingQueue->clear();          pVoiceStealingQueue->clear();
         itLastStolenVoice  = RTList<Voice>::Iterator();  
         iuiLastStolenKey   = RTList<uint>::Iterator();  
         pLastStolenChannel = NULL;  
427    
428          // just some statistics about this engine instance          // just some statistics about this engine instance
429          ActiveVoiceCount = ActiveVoiceCountTemp;          ActiveVoiceCount = ActiveVoiceCountTemp;
430          if (ActiveVoiceCount > ActiveVoiceCountMax) ActiveVoiceCountMax = ActiveVoiceCount;          if (ActiveVoiceCount > ActiveVoiceCountMax) ActiveVoiceCountMax = ActiveVoiceCount;
431    
432            FrameTime += Samples;
433    
434          return 0;          return 0;
435      }      }
436    
# Line 456  namespace LinuxSampler { namespace gig { Line 473  namespace LinuxSampler { namespace gig {
473                  }                  }
474              }              }
475          }          }
476    
477            // reset voice stealing for the next engine channel (or next audio fragment)
478            itLastStolenVoice         = RTList<Voice>::Iterator();
479            itLastStolenVoiceGlobally = RTList<Voice>::Iterator();
480            iuiLastStolenKey          = RTList<uint>::Iterator();
481            iuiLastStolenKeyGlobally  = RTList<uint>::Iterator();
482            pLastStolenChannel        = NULL;
483      }      }
484    
485      /**      /**
# Line 468  namespace LinuxSampler { namespace gig { Line 492  namespace LinuxSampler { namespace gig {
492       *                         this audio fragment cycle       *                         this audio fragment cycle
493       */       */
494      void Engine::RenderActiveVoices(EngineChannel* pEngineChannel, uint Samples) {      void Engine::RenderActiveVoices(EngineChannel* pEngineChannel, uint Samples) {
495            if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted
496    
497          RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();          RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();
498          RTList<uint>::Iterator end    = pEngineChannel->pActiveKeys->end();          RTList<uint>::Iterator end    = pEngineChannel->pActiveKeys->end();
499          while (iuiKey != end) { // iterate through all active keys          while (iuiKey != end) { // iterate through all active keys
# Line 506  namespace LinuxSampler { namespace gig { Line 532  namespace LinuxSampler { namespace gig {
532          for (; itVoiceStealEvent != end; ++itVoiceStealEvent) {          for (; itVoiceStealEvent != end; ++itVoiceStealEvent) {
533              EngineChannel* pEngineChannel = (EngineChannel*) itVoiceStealEvent->pEngineChannel;              EngineChannel* pEngineChannel = (EngineChannel*) itVoiceStealEvent->pEngineChannel;
534              Pool<Voice>::Iterator itNewVoice =              Pool<Voice>::Iterator itNewVoice =
535                  LaunchVoice(pEngineChannel, itVoiceStealEvent, itVoiceStealEvent->Param.Note.Layer, itVoiceStealEvent->Param.Note.ReleaseTrigger, false);                  LaunchVoice(pEngineChannel, itVoiceStealEvent, itVoiceStealEvent->Param.Note.Layer, itVoiceStealEvent->Param.Note.ReleaseTrigger, false, false);
536              if (itNewVoice) {              if (itNewVoice) {
537                  itNewVoice->Render(Samples);                  itNewVoice->Render(Samples);
538                  if (itNewVoice->IsActive()) ActiveVoiceCountTemp++; // still active                  if (itNewVoice->IsActive()) ActiveVoiceCountTemp++; // still active
# Line 540  namespace LinuxSampler { namespace gig { Line 566  namespace LinuxSampler { namespace gig {
566                  ++iuiKey;                  ++iuiKey;
567                  if (pKey->pActiveVoices->isEmpty()) FreeKey(pEngineChannel, pKey);                  if (pKey->pActiveVoices->isEmpty()) FreeKey(pEngineChannel, pKey);
568                  #if CONFIG_DEVMODE                  #if CONFIG_DEVMODE
569                  else { // FIXME: should be removed before the final release (purpose: just a sanity check for debugging)                  else { // just a sanity check for debugging
570                      RTList<Voice>::Iterator itVoice     = pKey->pActiveVoices->first();                      RTList<Voice>::Iterator itVoice     = pKey->pActiveVoices->first();
571                      RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end();                      RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end();
572                      for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key                      for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key
# Line 596  namespace LinuxSampler { namespace gig { Line 622  namespace LinuxSampler { namespace gig {
622       *  @param itNoteOnEvent - key, velocity and time stamp of the event       *  @param itNoteOnEvent - key, velocity and time stamp of the event
623       */       */
624      void Engine::ProcessNoteOn(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) {      void Engine::ProcessNoteOn(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) {
625           if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted
626    
627          const int key = itNoteOnEvent->Param.Note.Key;          const int key = itNoteOnEvent->Param.Note.Key;
628    
# Line 610  namespace LinuxSampler { namespace gig { Line 637  namespace LinuxSampler { namespace gig {
637          midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[key];          midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[key];
638    
639          pKey->KeyPressed = true; // the MIDI key was now pressed down          pKey->KeyPressed = true; // the MIDI key was now pressed down
640            pKey->Velocity   = itNoteOnEvent->Param.Note.Velocity;
641            pKey->NoteOnTime = FrameTime + itNoteOnEvent->FragmentPos(); // will be used to calculate note length
642    
643          // cancel release process of voices on this key if needed          // cancel release process of voices on this key if needed
644          if (pKey->Active && !pEngineChannel->SustainPedal) {          if (pKey->Active && !pEngineChannel->SustainPedal) {
# Line 632  namespace LinuxSampler { namespace gig { Line 661  namespace LinuxSampler { namespace gig {
661                  int voicesRequired = pRegion->Layers;                  int voicesRequired = pRegion->Layers;
662                  // now launch the required amount of voices                  // now launch the required amount of voices
663                  for (int i = 0; i < voicesRequired; i++)                  for (int i = 0; i < voicesRequired; i++)
664                      LaunchVoice(pEngineChannel, itNoteOnEventOnKeyList, i, false, true);                      LaunchVoice(pEngineChannel, itNoteOnEventOnKeyList, i, false, true, true);
665              }              }
666          }          }
667    
# Line 653  namespace LinuxSampler { namespace gig { Line 682  namespace LinuxSampler { namespace gig {
682       *  @param itNoteOffEvent - key, velocity and time stamp of the event       *  @param itNoteOffEvent - key, velocity and time stamp of the event
683       */       */
684      void Engine::ProcessNoteOff(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOffEvent) {      void Engine::ProcessNoteOff(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOffEvent) {
685          midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itNoteOffEvent->Param.Note.Key];          if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted
686    
687            midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itNoteOffEvent->Param.Note.Key];
688          pKey->KeyPressed = false; // the MIDI key was now released          pKey->KeyPressed = false; // the MIDI key was now released
689    
690          // release voices on this key if needed          // release voices on this key if needed
# Line 670  namespace LinuxSampler { namespace gig { Line 700  namespace LinuxSampler { namespace gig {
700                  ::gig::Region* pRegion = pEngineChannel->pInstrument->GetRegion(itNoteOffEventOnKeyList->Param.Note.Key);                  ::gig::Region* pRegion = pEngineChannel->pInstrument->GetRegion(itNoteOffEventOnKeyList->Param.Note.Key);
701                  if (pRegion) {                  if (pRegion) {
702                      int voicesRequired = pRegion->Layers;                      int voicesRequired = pRegion->Layers;
703    
704                        // MIDI note-on velocity is used instead of note-off velocity
705                        itNoteOffEventOnKeyList->Param.Note.Velocity = pKey->Velocity;
706    
707                      // now launch the required amount of voices                      // now launch the required amount of voices
708                      for (int i = 0; i < voicesRequired; i++)                      for (int i = 0; i < voicesRequired; i++)
709                          LaunchVoice(pEngineChannel, itNoteOffEventOnKeyList, i, true, false); //FIXME: for the moment we don't perform voice stealing for release triggered samples                          LaunchVoice(pEngineChannel, itNoteOffEventOnKeyList, i, true, false, false); //FIXME: for the moment we don't perform voice stealing for release triggered samples
710                  }                  }
711                  pKey->ReleaseTrigger = false;                  pKey->ReleaseTrigger = false;
712              }              }
# Line 709  namespace LinuxSampler { namespace gig { Line 743  namespace LinuxSampler { namespace gig {
743       *  @param VoiceStealing       - if voice stealing should be performed       *  @param VoiceStealing       - if voice stealing should be performed
744       *                               when there is no free voice       *                               when there is no free voice
745       *                               (optional, default = true)       *                               (optional, default = true)
746         *  @param HandleKeyGroupConflicts - if voices should be killed due to a
747         *                                   key group conflict
748       *  @returns pointer to new voice or NULL if there was no free voice or       *  @returns pointer to new voice or NULL if there was no free voice or
749       *           if the voice wasn't triggered (for example when no region is       *           if the voice wasn't triggered (for example when no region is
750       *           defined for the given key).       *           defined for the given key).
751       */       */
752      Pool<Voice>::Iterator Engine::LaunchVoice(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent, int iLayer, bool ReleaseTriggerVoice, bool VoiceStealing) {      Pool<Voice>::Iterator Engine::LaunchVoice(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent, int iLayer, bool ReleaseTriggerVoice, bool VoiceStealing, bool HandleKeyGroupConflicts) {
753          midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key];          int MIDIKey            = itNoteOnEvent->Param.Note.Key;
754            midi_key_info_t* pKey  = &pEngineChannel->pMIDIKeyInfo[MIDIKey];
755            ::gig::Region* pRegion = pEngineChannel->pInstrument->GetRegion(MIDIKey);
756    
757            // if nothing defined for this key
758            if (!pRegion) return Pool<Voice>::Iterator(); // nothing to do
759    
760            // only mark the first voice of a layered voice (group) to be in a
761            // key group, so the layered voices won't kill each other
762            int iKeyGroup = (iLayer == 0 && !ReleaseTriggerVoice) ? pRegion->KeyGroup : 0;
763    
764            // handle key group (a.k.a. exclusive group) conflicts
765            if (HandleKeyGroupConflicts) {
766                if (iKeyGroup) { // if this voice / key belongs to a key group
767                    uint** ppKeyGroup = &pEngineChannel->ActiveKeyGroups[iKeyGroup];
768                    if (*ppKeyGroup) { // if there's already an active key in that key group
769                        midi_key_info_t* pOtherKey = &pEngineChannel->pMIDIKeyInfo[**ppKeyGroup];
770                        // kill all voices on the (other) key
771                        RTList<Voice>::Iterator itVoiceToBeKilled = pOtherKey->pActiveVoices->first();
772                        RTList<Voice>::Iterator end               = pOtherKey->pActiveVoices->end();
773                        for (; itVoiceToBeKilled != end; ++itVoiceToBeKilled) {
774                            if (itVoiceToBeKilled->Type != Voice::type_release_trigger) {
775                                itVoiceToBeKilled->Kill(itNoteOnEvent);
776                                --VoiceSpawnsLeft; //FIXME: just a hack, we should better check in StealVoice() if the voice was killed due to key conflict
777                            }
778                        }
779                    }
780                }
781            }
782    
783            Voice::type_t VoiceType = Voice::type_normal;
784    
785            // get current dimension values to select the right dimension region
786            //TODO: for stolen voices this dimension region selection block is processed twice, this should be changed
787            //FIXME: controller values for selecting the dimension region here are currently not sample accurate
788            uint DimValues[8] = { 0 };
789            for (int i = pRegion->Dimensions - 1; i >= 0; i--) {
790                switch (pRegion->pDimensionDefinitions[i].dimension) {
791                    case ::gig::dimension_samplechannel:
792                        DimValues[i] = 0; //TODO: we currently ignore this dimension
793                        break;
794                    case ::gig::dimension_layer:
795                        DimValues[i] = iLayer;
796                        break;
797                    case ::gig::dimension_velocity:
798                        DimValues[i] = itNoteOnEvent->Param.Note.Velocity;
799                        break;
800                    case ::gig::dimension_channelaftertouch:
801                        DimValues[i] = 0; //TODO: we currently ignore this dimension
802                        break;
803                    case ::gig::dimension_releasetrigger:
804                        VoiceType = (ReleaseTriggerVoice) ? Voice::type_release_trigger : (!iLayer) ? Voice::type_release_trigger_required : Voice::type_normal;
805                        DimValues[i] = (uint) ReleaseTriggerVoice;
806                        break;
807                    case ::gig::dimension_keyboard:
808                        DimValues[i] = (uint) pEngineChannel->CurrentKeyDimension;
809                        break;
810                    case ::gig::dimension_roundrobin:
811                        DimValues[i] = (uint) pEngineChannel->pMIDIKeyInfo[MIDIKey].RoundRobinIndex; // incremented for each note on
812                        break;
813                    case ::gig::dimension_random:
814                        RandomSeed   = RandomSeed * 1103515245 + 12345; // classic pseudo random number generator
815                        DimValues[i] = (uint) RandomSeed >> (32 - pRegion->pDimensionDefinitions[i].bits); // highest bits are most random
816                        break;
817                    case ::gig::dimension_modwheel:
818                        DimValues[i] = pEngineChannel->ControllerTable[1];
819                        break;
820                    case ::gig::dimension_breath:
821                        DimValues[i] = pEngineChannel->ControllerTable[2];
822                        break;
823                    case ::gig::dimension_foot:
824                        DimValues[i] = pEngineChannel->ControllerTable[4];
825                        break;
826                    case ::gig::dimension_portamentotime:
827                        DimValues[i] = pEngineChannel->ControllerTable[5];
828                        break;
829                    case ::gig::dimension_effect1:
830                        DimValues[i] = pEngineChannel->ControllerTable[12];
831                        break;
832                    case ::gig::dimension_effect2:
833                        DimValues[i] = pEngineChannel->ControllerTable[13];
834                        break;
835                    case ::gig::dimension_genpurpose1:
836                        DimValues[i] = pEngineChannel->ControllerTable[16];
837                        break;
838                    case ::gig::dimension_genpurpose2:
839                        DimValues[i] = pEngineChannel->ControllerTable[17];
840                        break;
841                    case ::gig::dimension_genpurpose3:
842                        DimValues[i] = pEngineChannel->ControllerTable[18];
843                        break;
844                    case ::gig::dimension_genpurpose4:
845                        DimValues[i] = pEngineChannel->ControllerTable[19];
846                        break;
847                    case ::gig::dimension_sustainpedal:
848                        DimValues[i] = pEngineChannel->ControllerTable[64];
849                        break;
850                    case ::gig::dimension_portamento:
851                        DimValues[i] = pEngineChannel->ControllerTable[65];
852                        break;
853                    case ::gig::dimension_sostenutopedal:
854                        DimValues[i] = pEngineChannel->ControllerTable[66];
855                        break;
856                    case ::gig::dimension_softpedal:
857                        DimValues[i] = pEngineChannel->ControllerTable[67];
858                        break;
859                    case ::gig::dimension_genpurpose5:
860                        DimValues[i] = pEngineChannel->ControllerTable[80];
861                        break;
862                    case ::gig::dimension_genpurpose6:
863                        DimValues[i] = pEngineChannel->ControllerTable[81];
864                        break;
865                    case ::gig::dimension_genpurpose7:
866                        DimValues[i] = pEngineChannel->ControllerTable[82];
867                        break;
868                    case ::gig::dimension_genpurpose8:
869                        DimValues[i] = pEngineChannel->ControllerTable[83];
870                        break;
871                    case ::gig::dimension_effect1depth:
872                        DimValues[i] = pEngineChannel->ControllerTable[91];
873                        break;
874                    case ::gig::dimension_effect2depth:
875                        DimValues[i] = pEngineChannel->ControllerTable[92];
876                        break;
877                    case ::gig::dimension_effect3depth:
878                        DimValues[i] = pEngineChannel->ControllerTable[93];
879                        break;
880                    case ::gig::dimension_effect4depth:
881                        DimValues[i] = pEngineChannel->ControllerTable[94];
882                        break;
883                    case ::gig::dimension_effect5depth:
884                        DimValues[i] = pEngineChannel->ControllerTable[95];
885                        break;
886                    case ::gig::dimension_none:
887                        std::cerr << "gig::Engine::LaunchVoice() Error: dimension=none\n" << std::flush;
888                        break;
889                    default:
890                        std::cerr << "gig::Engine::LaunchVoice() Error: Unknown dimension\n" << std::flush;
891                }
892            }
893            ::gig::DimensionRegion* pDimRgn = pRegion->GetDimensionRegionByValue(DimValues);
894    
895            // no need to continue if sample is silent
896            if (!pDimRgn->pSample || !pDimRgn->pSample->SamplesTotal) return Pool<Voice>::Iterator();
897    
898          // allocate a new voice for the key          // allocate a new voice for the key
899          Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend();          Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend();
900          if (itNewVoice) {          if (itNewVoice) {
901              // launch the new voice              // launch the new voice
902              if (itNewVoice->Trigger(pEngineChannel, itNoteOnEvent, pEngineChannel->Pitch, pEngineChannel->pInstrument, iLayer, ReleaseTriggerVoice, VoiceStealing) < 0) {              if (itNewVoice->Trigger(pEngineChannel, itNoteOnEvent, pEngineChannel->Pitch, pDimRgn, VoiceType, iKeyGroup) < 0) {
903                  dmsg(4,("Voice not triggered\n"));                  dmsg(4,("Voice not triggered\n"));
904                  pKey->pActiveVoices->free(itNewVoice);                  pKey->pActiveVoices->free(itNewVoice);
905              }              }
906              else { // on success              else { // on success
907                  uint** ppKeyGroup = NULL;                  --VoiceSpawnsLeft;
                 if (itNewVoice->KeyGroup) { // if this voice / key belongs to a key group  
                     ppKeyGroup = &pEngineChannel->ActiveKeyGroups[itNewVoice->KeyGroup];  
                     if (*ppKeyGroup) { // if there's already an active key in that key group  
                         midi_key_info_t* pOtherKey = &pEngineChannel->pMIDIKeyInfo[**ppKeyGroup];  
                         // kill all voices on the (other) key  
                         RTList<Voice>::Iterator itVoiceToBeKilled = pOtherKey->pActiveVoices->first();  
                         RTList<Voice>::Iterator end               = pOtherKey->pActiveVoices->end();  
                         for (; itVoiceToBeKilled != end; ++itVoiceToBeKilled) {  
                             if (itVoiceToBeKilled->Type != Voice::type_release_trigger) itVoiceToBeKilled->Kill(itNoteOnEvent);  
                         }  
                     }  
                 }  
908                  if (!pKey->Active) { // mark as active key                  if (!pKey->Active) { // mark as active key
909                      pKey->Active = true;                      pKey->Active = true;
910                      pKey->itSelf = pEngineChannel->pActiveKeys->allocAppend();                      pKey->itSelf = pEngineChannel->pActiveKeys->allocAppend();
911                      *pKey->itSelf = itNoteOnEvent->Param.Note.Key;                      *pKey->itSelf = itNoteOnEvent->Param.Note.Key;
912                  }                  }
913                  if (itNewVoice->KeyGroup) {                  if (itNewVoice->KeyGroup) {
914                        uint** ppKeyGroup = &pEngineChannel->ActiveKeyGroups[itNewVoice->KeyGroup];
915                      *ppKeyGroup = &*pKey->itSelf; // put key as the (new) active key to its key group                      *ppKeyGroup = &*pKey->itSelf; // put key as the (new) active key to its key group
916                  }                  }
917                  if (itNewVoice->Type == Voice::type_release_trigger_required) pKey->ReleaseTrigger = true; // mark key for the need of release triggered voice(s)                  if (itNewVoice->Type == Voice::type_release_trigger_required) pKey->ReleaseTrigger = true; // mark key for the need of release triggered voice(s)
# Line 780  namespace LinuxSampler { namespace gig { Line 948  namespace LinuxSampler { namespace gig {
948       *  @returns 0 on success, a value < 0 if no active voice could be picked for voice stealing       *  @returns 0 on success, a value < 0 if no active voice could be picked for voice stealing
949       */       */
950      int Engine::StealVoice(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) {      int Engine::StealVoice(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) {
951          if (!VoiceTheftsLeft) {          if (VoiceSpawnsLeft <= 0) {
952              dmsg(1,("Max. voice thefts per audio fragment reached (you may raise CONFIG_MAX_VOICES).\n"));              dmsg(1,("Max. voice thefts per audio fragment reached (you may raise CONFIG_MAX_VOICES).\n"));
953              return -1;              return -1;
954          }          }
# Line 793  namespace LinuxSampler { namespace gig { Line 961  namespace LinuxSampler { namespace gig {
961    
962                  // try to pick the oldest voice on the key where the new                  // try to pick the oldest voice on the key where the new
963                  // voice should be spawned, if there is no voice on that                  // voice should be spawned, if there is no voice on that
964                  // key, or no voice left to kill there, then procceed with                  // key, or no voice left to kill, then procceed with
965                  // 'oldestkey' algorithm                  // 'oldestkey' algorithm
966                  case voice_steal_algo_oldestvoiceonkey: {                  case voice_steal_algo_oldestvoiceonkey: {
                 #if 0 // FIXME: broken  
967                      midi_key_info_t* pSelectedKey = &pEngineChannel->pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key];                      midi_key_info_t* pSelectedKey = &pEngineChannel->pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key];
968                      if (this->itLastStolenVoice) {                      itSelectedVoice = pSelectedKey->pActiveVoices->first();
969                          itSelectedVoice = this->itLastStolenVoice;                      // proceed iterating if voice was created in this fragment cycle
970                          ++itSelectedVoice;                      while (itSelectedVoice && !itSelectedVoice->IsStealable()) ++itSelectedVoice;
971                      }                      // if we haven't found a voice then proceed with algorithm 'oldestkey'
972                      else { // no voice stolen in this audio fragment cycle yet                      if (itSelectedVoice && itSelectedVoice->IsStealable()) break;
                         itSelectedVoice = pSelectedKey->pActiveVoices->first();  
                     }  
                     if (itSelectedVoice) {  
                         iuiSelectedKey = pSelectedKey->itSelf;  
                         break; // selection succeeded  
                     }  
                 #endif  
973                  } // no break - intentional !                  } // no break - intentional !
974    
975                  // try to pick the oldest voice on the oldest active key                  // try to pick the oldest voice on the oldest active key
976                    // from the same engine channel
977                  // (caution: must stay after 'oldestvoiceonkey' algorithm !)                  // (caution: must stay after 'oldestvoiceonkey' algorithm !)
978                  case voice_steal_algo_oldestkey: {                  case voice_steal_algo_oldestkey: {
979                        // if we already stole in this fragment, try to proceed on same key
980                      if (this->itLastStolenVoice) {                      if (this->itLastStolenVoice) {
981                          itSelectedVoice = this->itLastStolenVoice;                          itSelectedVoice = this->itLastStolenVoice;
982                          ++itSelectedVoice;                          do {
983                          if (itSelectedVoice) break; // selection succeeded                              ++itSelectedVoice;
984                          RTList<uint>::Iterator iuiSelectedKey = this->iuiLastStolenKey;                          } while (itSelectedVoice && !itSelectedVoice->IsStealable()); // proceed iterating if voice was created in this fragment cycle
985                          ++iuiSelectedKey;                          // found a "stealable" voice ?
986                          if (iuiSelectedKey) {                          if (itSelectedVoice && itSelectedVoice->IsStealable()) {
987                              this->iuiLastStolenKey = iuiSelectedKey;                              // remember which voice we stole, so we can simply proceed on next voice stealing
988                              midi_key_info_t* pSelectedKey = &pEngineChannel->pMIDIKeyInfo[*iuiSelectedKey];                              this->itLastStolenVoice = itSelectedVoice;
989                              itSelectedVoice = pSelectedKey->pActiveVoices->first();                              break; // selection succeeded
990                            }
991                        }
992                        // get (next) oldest key
993                        RTList<uint>::Iterator iuiSelectedKey = (this->iuiLastStolenKey) ? ++this->iuiLastStolenKey : pEngineChannel->pActiveKeys->first();
994                        while (iuiSelectedKey) {
995                            midi_key_info_t* pSelectedKey = &pEngineChannel->pMIDIKeyInfo[*iuiSelectedKey];
996                            itSelectedVoice = pSelectedKey->pActiveVoices->first();
997                            // proceed iterating if voice was created in this fragment cycle
998                            while (itSelectedVoice && !itSelectedVoice->IsStealable()) ++itSelectedVoice;
999                            // found a "stealable" voice ?
1000                            if (itSelectedVoice && itSelectedVoice->IsStealable()) {
1001                                // remember which voice on which key we stole, so we can simply proceed on next voice stealing
1002                                this->iuiLastStolenKey  = iuiSelectedKey;
1003                                this->itLastStolenVoice = itSelectedVoice;
1004                              break; // selection succeeded                              break; // selection succeeded
1005                          }                          }
1006                            ++iuiSelectedKey; // get next oldest key
1007                      }                      }
1008                      break;                      break;
1009                  }                  }
# Line 839  namespace LinuxSampler { namespace gig { Line 1016  namespace LinuxSampler { namespace gig {
1016                  }                  }
1017              }              }
1018    
1019              // steal oldest voice on the oldest key from this or any other engine channel              // if we couldn't steal a voice from the same engine channel then
1020              if (!itSelectedVoice) {              // steal oldest voice on the oldest key from any other engine channel
1021                  EngineChannel* pSelectedChannel = (pLastStolenChannel) ? pLastStolenChannel : pEngineChannel;              // (the smaller engine channel number, the higher priority)
1022                  int iChannelIndex = pSelectedChannel->iEngineIndexSelf;              if (!itSelectedVoice || !itSelectedVoice->IsStealable()) {
1023                  while (true) {                  EngineChannel* pSelectedChannel;
1024                      RTList<uint>::Iterator iuiSelectedKey = pSelectedChannel->pActiveKeys->first();                  int            iChannelIndex;
1025                      if (iuiSelectedKey) {                  // select engine channel
1026                    if (pLastStolenChannel) {
1027                        pSelectedChannel = pLastStolenChannel;
1028                        iChannelIndex    = pSelectedChannel->iEngineIndexSelf;
1029                    } else { // pick the engine channel followed by this engine channel
1030                        iChannelIndex    = (pEngineChannel->iEngineIndexSelf + 1) % engineChannels.size();
1031                        pSelectedChannel = engineChannels[iChannelIndex];
1032                    }
1033    
1034                    // if we already stole in this fragment, try to proceed on same key
1035                    if (this->itLastStolenVoiceGlobally) {
1036                        itSelectedVoice = this->itLastStolenVoiceGlobally;
1037                        do {
1038                            ++itSelectedVoice;
1039                        } while (itSelectedVoice && !itSelectedVoice->IsStealable()); // proceed iterating if voice was created in this fragment cycle
1040                    }
1041    
1042                    #if CONFIG_DEVMODE
1043                    EngineChannel* pBegin = pSelectedChannel; // to detect endless loop
1044                    #endif // CONFIG_DEVMODE
1045    
1046                    // did we find a 'stealable' voice?
1047                    if (itSelectedVoice && itSelectedVoice->IsStealable()) {
1048                        // remember which voice we stole, so we can simply proceed on next voice stealing
1049                        this->itLastStolenVoiceGlobally = itSelectedVoice;
1050                    } else while (true) { // iterate through engine channels
1051                        // get (next) oldest key
1052                        RTList<uint>::Iterator iuiSelectedKey = (this->iuiLastStolenKeyGlobally) ? ++this->iuiLastStolenKeyGlobally : pSelectedChannel->pActiveKeys->first();
1053                        this->iuiLastStolenKeyGlobally = RTList<uint>::Iterator(); // to prevent endless loop (see line above)
1054                        while (iuiSelectedKey) {
1055                          midi_key_info_t* pSelectedKey = &pSelectedChannel->pMIDIKeyInfo[*iuiSelectedKey];                          midi_key_info_t* pSelectedKey = &pSelectedChannel->pMIDIKeyInfo[*iuiSelectedKey];
1056                          itSelectedVoice    = pSelectedKey->pActiveVoices->first();                          itSelectedVoice = pSelectedKey->pActiveVoices->first();
1057                          iuiLastStolenKey   = iuiSelectedKey;                          // proceed iterating if voice was created in this fragment cycle
1058                          pLastStolenChannel = pSelectedChannel;                          while (itSelectedVoice && !itSelectedVoice->IsStealable()) ++itSelectedVoice;
1059                          break; // selection succeeded                          // found a "stealable" voice ?
1060                            if (itSelectedVoice && itSelectedVoice->IsStealable()) {
1061                                // remember which voice on which key on which engine channel we stole, so we can simply proceed on next voice stealing
1062                                this->iuiLastStolenKeyGlobally  = iuiSelectedKey;
1063                                this->itLastStolenVoiceGlobally = itSelectedVoice;
1064                                this->pLastStolenChannel        = pSelectedChannel;
1065                                goto stealable_voice_found; // selection succeeded
1066                            }
1067                            ++iuiSelectedKey; // get next key on current engine channel
1068                      }                      }
1069                        // get next engine channel
1070                      iChannelIndex    = (iChannelIndex + 1) % engineChannels.size();                      iChannelIndex    = (iChannelIndex + 1) % engineChannels.size();
1071                      pSelectedChannel =  engineChannels[iChannelIndex];                      pSelectedChannel = engineChannels[iChannelIndex];
1072    
1073                        #if CONFIG_DEVMODE
1074                        if (pSelectedChannel == pBegin) {
1075                            dmsg(1,("FATAL ERROR: voice stealing endless loop!\n"));
1076                            dmsg(1,("VoiceSpawnsLeft=%d.\n", VoiceSpawnsLeft));
1077                            dmsg(1,("Exiting.\n"));
1078                            exit(-1);
1079                        }
1080                        #endif // CONFIG_DEVMODE
1081                  }                  }
1082              }              }
1083    
1084              //FIXME: can be removed, just a sanity check for debugging              // jump point if a 'stealable' voice was found
1085                stealable_voice_found:
1086    
1087                #if CONFIG_DEVMODE
1088              if (!itSelectedVoice->IsActive()) {              if (!itSelectedVoice->IsActive()) {
1089                  dmsg(1,("gig::Engine: ERROR, tried to steal a voice which was not active !!!\n"));                  dmsg(1,("gig::Engine: ERROR, tried to steal a voice which was not active !!!\n"));
1090                  return -1;                  return -1;
1091              }              }
1092                #endif // CONFIG_DEVMODE
1093    
1094              // now kill the selected voice              // now kill the selected voice
1095              itSelectedVoice->Kill(itNoteOnEvent);              itSelectedVoice->Kill(itNoteOnEvent);
1096    
1097              // remember which voice we stole, so we can simply proceed for the next voice stealing              --VoiceSpawnsLeft;
             itLastStolenVoice = itSelectedVoice;  
   
             --VoiceTheftsLeft;  
1098    
1099              return 0; // success              return 0; // success
1100          }          }
# Line 945  namespace LinuxSampler { namespace gig { Line 1170  namespace LinuxSampler { namespace gig {
1170              case 7: { // volume              case 7: { // volume
1171                  //TODO: not sample accurate yet                  //TODO: not sample accurate yet
1172                  pEngineChannel->GlobalVolume = (float) itControlChangeEventOnCCList->Param.CC.Value / 127.0f;                  pEngineChannel->GlobalVolume = (float) itControlChangeEventOnCCList->Param.CC.Value / 127.0f;
1173                    pEngineChannel->bStatusChanged = true; // engine channel status has changed, so set notify flag
1174                  break;                  break;
1175              }              }
1176              case 10: { // panpot              case 10: { // panpot
# Line 959  namespace LinuxSampler { namespace gig { Line 1185  namespace LinuxSampler { namespace gig {
1185                      dmsg(4,("PEDAL DOWN\n"));                      dmsg(4,("PEDAL DOWN\n"));
1186                      pEngineChannel->SustainPedal = true;                      pEngineChannel->SustainPedal = true;
1187    
1188                        if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted
1189    
1190                      // cancel release process of voices if necessary                      // cancel release process of voices if necessary
1191                      RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();                      RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();
1192                      for (; iuiKey; ++iuiKey) {                      for (; iuiKey; ++iuiKey) {
# Line 977  namespace LinuxSampler { namespace gig { Line 1205  namespace LinuxSampler { namespace gig {
1205                      dmsg(4,("PEDAL UP\n"));                      dmsg(4,("PEDAL UP\n"));
1206                      pEngineChannel->SustainPedal = false;                      pEngineChannel->SustainPedal = false;
1207    
1208                        if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted
1209    
1210                      // release voices if their respective key is not pressed                      // release voices if their respective key is not pressed
1211                      RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();                      RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();
1212                      for (; iuiKey; ++iuiKey) {                      for (; iuiKey; ++iuiKey) {
# Line 1054  namespace LinuxSampler { namespace gig { Line 1284  namespace LinuxSampler { namespace gig {
1284                              if (reader.read(&scale_tunes[0], 12) != 12) goto free_sysex_data;                              if (reader.read(&scale_tunes[0], 12) != 12) goto free_sysex_data;
1285                              uint8_t checksum;                              uint8_t checksum;
1286                              if (!reader.pop(&checksum)) goto free_sysex_data;                              if (!reader.pop(&checksum)) goto free_sysex_data;
1287                              // some are not sending a GS checksum, so we ignore it for now                              #if CONFIG_ASSERT_GS_SYSEX_CHECKSUM
1288                              //if (GSCheckSum(checksum_reader, 12)) goto free_sysex_data;                              if (GSCheckSum(checksum_reader, 12)) goto free_sysex_data;
1289                                #endif // CONFIG_ASSERT_GS_SYSEX_CHECKSUM
1290                              for (int i = 0; i < 12; i++) scale_tunes[i] -= 64;                              for (int i = 0; i < 12; i++) scale_tunes[i] -= 64;
1291                              AdjustScale((int8_t*) scale_tunes);                              AdjustScale((int8_t*) scale_tunes);
1292                              dmsg(3,("\t\t\tNew scale applied.\n"));                              dmsg(3,("\t\t\tNew scale applied.\n"));
# Line 1143  namespace LinuxSampler { namespace gig { Line 1374  namespace LinuxSampler { namespace gig {
1374              RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end();              RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end();
1375              for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key              for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key
1376                  itVoice->Kill(itKillEvent);                  itVoice->Kill(itKillEvent);
1377                    --VoiceSpawnsLeft; //FIXME: just a temporary workaround, we should check the cause in StealVoice() instead
1378              }              }
1379          }          }
1380      }      }
# Line 1199  namespace LinuxSampler { namespace gig { Line 1431  namespace LinuxSampler { namespace gig {
1431      }      }
1432    
1433      String Engine::Version() {      String Engine::Version() {
1434          String s = "$Revision: 1.37 $";          String s = "$Revision: 1.50 $";
1435          return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword          return s.substr(11, s.size() - 13); // cut dollar signs, spaces and CVS macro keyword
1436      }      }
1437    

Legend:
Removed from v.554  
changed lines
  Added in v.705

  ViewVC Help
Powered by ViewVC