/[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 649 by schoenebeck, Wed Jun 15 00:01:28 2005 UTC revision 716 by iliev, Sun Jul 24 06:57:30 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 190  namespace LinuxSampler { namespace gig { Line 192  namespace LinuxSampler { namespace gig {
192          iuiLastStolenKeyGlobally   = RTList<uint>::Iterator();          iuiLastStolenKeyGlobally   = RTList<uint>::Iterator();
193          pLastStolenChannel         = NULL;          pLastStolenChannel         = NULL;
194    
         // reset to normal chromatic scale (means equal temper)  
         memset(&ScaleTuning[0], 0x00, 12);  
   
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()) {
197              iterVoice->Reset();              iterVoice->Reset();
# Line 207  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 234  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 354  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 372  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 476  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 !CONFIG_PROCESS_MUTED_CHANNELS
496            if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted
497            #endif
498    
499          RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();          RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();
500          RTList<uint>::Iterator end    = pEngineChannel->pActiveKeys->end();          RTList<uint>::Iterator end    = pEngineChannel->pActiveKeys->end();
501          while (iuiKey != end) { // iterate through all active keys          while (iuiKey != end) { // iterate through all active keys
# Line 514  namespace LinuxSampler { namespace gig { Line 534  namespace LinuxSampler { namespace gig {
534          for (; itVoiceStealEvent != end; ++itVoiceStealEvent) {          for (; itVoiceStealEvent != end; ++itVoiceStealEvent) {
535              EngineChannel* pEngineChannel = (EngineChannel*) itVoiceStealEvent->pEngineChannel;              EngineChannel* pEngineChannel = (EngineChannel*) itVoiceStealEvent->pEngineChannel;
536              Pool<Voice>::Iterator itNewVoice =              Pool<Voice>::Iterator itNewVoice =
537                  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);
538              if (itNewVoice) {              if (itNewVoice) {
539                  itNewVoice->Render(Samples);                  itNewVoice->Render(Samples);
540                  if (itNewVoice->IsActive()) ActiveVoiceCountTemp++; // still active                  if (itNewVoice->IsActive()) ActiveVoiceCountTemp++; // still active
# Line 604  namespace LinuxSampler { namespace gig { Line 624  namespace LinuxSampler { namespace gig {
624       *  @param itNoteOnEvent - key, velocity and time stamp of the event       *  @param itNoteOnEvent - key, velocity and time stamp of the event
625       */       */
626      void Engine::ProcessNoteOn(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) {      void Engine::ProcessNoteOn(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) {
627            #if !CONFIG_PROCESS_MUTED_CHANNELS
628            if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted
629            #endif
630    
631          const int key = itNoteOnEvent->Param.Note.Key;          const int key = itNoteOnEvent->Param.Note.Key;
632    
# Line 642  namespace LinuxSampler { namespace gig { Line 665  namespace LinuxSampler { namespace gig {
665                  int voicesRequired = pRegion->Layers;                  int voicesRequired = pRegion->Layers;
666                  // now launch the required amount of voices                  // now launch the required amount of voices
667                  for (int i = 0; i < voicesRequired; i++)                  for (int i = 0; i < voicesRequired; i++)
668                      LaunchVoice(pEngineChannel, itNoteOnEventOnKeyList, i, false, true);                      LaunchVoice(pEngineChannel, itNoteOnEventOnKeyList, i, false, true, true);
669              }              }
670          }          }
671    
# Line 663  namespace LinuxSampler { namespace gig { Line 686  namespace LinuxSampler { namespace gig {
686       *  @param itNoteOffEvent - key, velocity and time stamp of the event       *  @param itNoteOffEvent - key, velocity and time stamp of the event
687       */       */
688      void Engine::ProcessNoteOff(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOffEvent) {      void Engine::ProcessNoteOff(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOffEvent) {
689          midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itNoteOffEvent->Param.Note.Key];          #if !CONFIG_PROCESS_MUTED_CHANNELS
690            if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted
691            #endif
692    
693            midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itNoteOffEvent->Param.Note.Key];
694          pKey->KeyPressed = false; // the MIDI key was now released          pKey->KeyPressed = false; // the MIDI key was now released
695    
696          // release voices on this key if needed          // release voices on this key if needed
# Line 686  namespace LinuxSampler { namespace gig { Line 712  namespace LinuxSampler { namespace gig {
712    
713                      // now launch the required amount of voices                      // now launch the required amount of voices
714                      for (int i = 0; i < voicesRequired; i++)                      for (int i = 0; i < voicesRequired; i++)
715                          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
716                  }                  }
717                  pKey->ReleaseTrigger = false;                  pKey->ReleaseTrigger = false;
718              }              }
# Line 723  namespace LinuxSampler { namespace gig { Line 749  namespace LinuxSampler { namespace gig {
749       *  @param VoiceStealing       - if voice stealing should be performed       *  @param VoiceStealing       - if voice stealing should be performed
750       *                               when there is no free voice       *                               when there is no free voice
751       *                               (optional, default = true)       *                               (optional, default = true)
752         *  @param HandleKeyGroupConflicts - if voices should be killed due to a
753         *                                   key group conflict
754       *  @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
755       *           if the voice wasn't triggered (for example when no region is       *           if the voice wasn't triggered (for example when no region is
756       *           defined for the given key).       *           defined for the given key).
757       */       */
758      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) {
759          midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key];          int MIDIKey            = itNoteOnEvent->Param.Note.Key;
760            midi_key_info_t* pKey  = &pEngineChannel->pMIDIKeyInfo[MIDIKey];
761            ::gig::Region* pRegion = pEngineChannel->pInstrument->GetRegion(MIDIKey);
762    
763            // if nothing defined for this key
764            if (!pRegion) return Pool<Voice>::Iterator(); // nothing to do
765    
766            // only mark the first voice of a layered voice (group) to be in a
767            // key group, so the layered voices won't kill each other
768            int iKeyGroup = (iLayer == 0 && !ReleaseTriggerVoice) ? pRegion->KeyGroup : 0;
769    
770            // handle key group (a.k.a. exclusive group) conflicts
771            if (HandleKeyGroupConflicts) {
772                if (iKeyGroup) { // if this voice / key belongs to a key group
773                    uint** ppKeyGroup = &pEngineChannel->ActiveKeyGroups[iKeyGroup];
774                    if (*ppKeyGroup) { // if there's already an active key in that key group
775                        midi_key_info_t* pOtherKey = &pEngineChannel->pMIDIKeyInfo[**ppKeyGroup];
776                        // kill all voices on the (other) key
777                        RTList<Voice>::Iterator itVoiceToBeKilled = pOtherKey->pActiveVoices->first();
778                        RTList<Voice>::Iterator end               = pOtherKey->pActiveVoices->end();
779                        for (; itVoiceToBeKilled != end; ++itVoiceToBeKilled) {
780                            if (itVoiceToBeKilled->Type != Voice::type_release_trigger) {
781                                itVoiceToBeKilled->Kill(itNoteOnEvent);
782                                --VoiceSpawnsLeft; //FIXME: just a hack, we should better check in StealVoice() if the voice was killed due to key conflict
783                            }
784                        }
785                    }
786                }
787            }
788    
789            Voice::type_t VoiceType = Voice::type_normal;
790    
791            // get current dimension values to select the right dimension region
792            //TODO: for stolen voices this dimension region selection block is processed twice, this should be changed
793            //FIXME: controller values for selecting the dimension region here are currently not sample accurate
794            uint DimValues[8] = { 0 };
795            for (int i = pRegion->Dimensions - 1; i >= 0; i--) {
796                switch (pRegion->pDimensionDefinitions[i].dimension) {
797                    case ::gig::dimension_samplechannel:
798                        DimValues[i] = 0; //TODO: we currently ignore this dimension
799                        break;
800                    case ::gig::dimension_layer:
801                        DimValues[i] = iLayer;
802                        break;
803                    case ::gig::dimension_velocity:
804                        DimValues[i] = itNoteOnEvent->Param.Note.Velocity;
805                        break;
806                    case ::gig::dimension_channelaftertouch:
807                        DimValues[i] = 0; //TODO: we currently ignore this dimension
808                        break;
809                    case ::gig::dimension_releasetrigger:
810                        VoiceType = (ReleaseTriggerVoice) ? Voice::type_release_trigger : (!iLayer) ? Voice::type_release_trigger_required : Voice::type_normal;
811                        DimValues[i] = (uint) ReleaseTriggerVoice;
812                        break;
813                    case ::gig::dimension_keyboard:
814                        DimValues[i] = (uint) pEngineChannel->CurrentKeyDimension;
815                        break;
816                    case ::gig::dimension_roundrobin:
817                        DimValues[i] = (uint) pEngineChannel->pMIDIKeyInfo[MIDIKey].RoundRobinIndex; // incremented for each note on
818                        break;
819                    case ::gig::dimension_random:
820                        RandomSeed   = RandomSeed * 1103515245 + 12345; // classic pseudo random number generator
821                        DimValues[i] = (uint) RandomSeed >> (32 - pRegion->pDimensionDefinitions[i].bits); // highest bits are most random
822                        break;
823                    case ::gig::dimension_modwheel:
824                        DimValues[i] = pEngineChannel->ControllerTable[1];
825                        break;
826                    case ::gig::dimension_breath:
827                        DimValues[i] = pEngineChannel->ControllerTable[2];
828                        break;
829                    case ::gig::dimension_foot:
830                        DimValues[i] = pEngineChannel->ControllerTable[4];
831                        break;
832                    case ::gig::dimension_portamentotime:
833                        DimValues[i] = pEngineChannel->ControllerTable[5];
834                        break;
835                    case ::gig::dimension_effect1:
836                        DimValues[i] = pEngineChannel->ControllerTable[12];
837                        break;
838                    case ::gig::dimension_effect2:
839                        DimValues[i] = pEngineChannel->ControllerTable[13];
840                        break;
841                    case ::gig::dimension_genpurpose1:
842                        DimValues[i] = pEngineChannel->ControllerTable[16];
843                        break;
844                    case ::gig::dimension_genpurpose2:
845                        DimValues[i] = pEngineChannel->ControllerTable[17];
846                        break;
847                    case ::gig::dimension_genpurpose3:
848                        DimValues[i] = pEngineChannel->ControllerTable[18];
849                        break;
850                    case ::gig::dimension_genpurpose4:
851                        DimValues[i] = pEngineChannel->ControllerTable[19];
852                        break;
853                    case ::gig::dimension_sustainpedal:
854                        DimValues[i] = pEngineChannel->ControllerTable[64];
855                        break;
856                    case ::gig::dimension_portamento:
857                        DimValues[i] = pEngineChannel->ControllerTable[65];
858                        break;
859                    case ::gig::dimension_sostenutopedal:
860                        DimValues[i] = pEngineChannel->ControllerTable[66];
861                        break;
862                    case ::gig::dimension_softpedal:
863                        DimValues[i] = pEngineChannel->ControllerTable[67];
864                        break;
865                    case ::gig::dimension_genpurpose5:
866                        DimValues[i] = pEngineChannel->ControllerTable[80];
867                        break;
868                    case ::gig::dimension_genpurpose6:
869                        DimValues[i] = pEngineChannel->ControllerTable[81];
870                        break;
871                    case ::gig::dimension_genpurpose7:
872                        DimValues[i] = pEngineChannel->ControllerTable[82];
873                        break;
874                    case ::gig::dimension_genpurpose8:
875                        DimValues[i] = pEngineChannel->ControllerTable[83];
876                        break;
877                    case ::gig::dimension_effect1depth:
878                        DimValues[i] = pEngineChannel->ControllerTable[91];
879                        break;
880                    case ::gig::dimension_effect2depth:
881                        DimValues[i] = pEngineChannel->ControllerTable[92];
882                        break;
883                    case ::gig::dimension_effect3depth:
884                        DimValues[i] = pEngineChannel->ControllerTable[93];
885                        break;
886                    case ::gig::dimension_effect4depth:
887                        DimValues[i] = pEngineChannel->ControllerTable[94];
888                        break;
889                    case ::gig::dimension_effect5depth:
890                        DimValues[i] = pEngineChannel->ControllerTable[95];
891                        break;
892                    case ::gig::dimension_none:
893                        std::cerr << "gig::Engine::LaunchVoice() Error: dimension=none\n" << std::flush;
894                        break;
895                    default:
896                        std::cerr << "gig::Engine::LaunchVoice() Error: Unknown dimension\n" << std::flush;
897                }
898            }
899            ::gig::DimensionRegion* pDimRgn = pRegion->GetDimensionRegionByValue(DimValues);
900    
901            // no need to continue if sample is silent
902            if (!pDimRgn->pSample || !pDimRgn->pSample->SamplesTotal) return Pool<Voice>::Iterator();
903    
904          // allocate a new voice for the key          // allocate a new voice for the key
905          Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend();          Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend();
906          if (itNewVoice) {          if (itNewVoice) {
907              // launch the new voice              // launch the new voice
908              if (itNewVoice->Trigger(pEngineChannel, itNoteOnEvent, pEngineChannel->Pitch, pEngineChannel->pInstrument, iLayer, ReleaseTriggerVoice, VoiceStealing) < 0) {              if (itNewVoice->Trigger(pEngineChannel, itNoteOnEvent, pEngineChannel->Pitch, pDimRgn, VoiceType, iKeyGroup) < 0) {
909                  dmsg(4,("Voice not triggered\n"));                  dmsg(4,("Voice not triggered\n"));
910                  pKey->pActiveVoices->free(itNewVoice);                  pKey->pActiveVoices->free(itNewVoice);
911              }              }
912              else { // on success              else { // on success
913                  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);  
                         }  
                     }  
                 }  
914                  if (!pKey->Active) { // mark as active key                  if (!pKey->Active) { // mark as active key
915                      pKey->Active = true;                      pKey->Active = true;
916                      pKey->itSelf = pEngineChannel->pActiveKeys->allocAppend();                      pKey->itSelf = pEngineChannel->pActiveKeys->allocAppend();
917                      *pKey->itSelf = itNoteOnEvent->Param.Note.Key;                      *pKey->itSelf = itNoteOnEvent->Param.Note.Key;
918                  }                  }
919                  if (itNewVoice->KeyGroup) {                  if (itNewVoice->KeyGroup) {
920                        uint** ppKeyGroup = &pEngineChannel->ActiveKeyGroups[itNewVoice->KeyGroup];
921                      *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
922                  }                  }
923                  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 794  namespace LinuxSampler { namespace gig { Line 954  namespace LinuxSampler { namespace gig {
954       *  @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
955       */       */
956      int Engine::StealVoice(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) {      int Engine::StealVoice(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) {
957          if (!VoiceTheftsLeft) {          if (VoiceSpawnsLeft <= 0) {
958              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"));
959              return -1;              return -1;
960          }          }
# Line 813  namespace LinuxSampler { namespace gig { Line 973  namespace LinuxSampler { namespace gig {
973                      midi_key_info_t* pSelectedKey = &pEngineChannel->pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key];                      midi_key_info_t* pSelectedKey = &pEngineChannel->pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key];
974                      itSelectedVoice = pSelectedKey->pActiveVoices->first();                      itSelectedVoice = pSelectedKey->pActiveVoices->first();
975                      // proceed iterating if voice was created in this fragment cycle                      // proceed iterating if voice was created in this fragment cycle
976                      while (itSelectedVoice && !itSelectedVoice->hasRendered()) ++itSelectedVoice;                      while (itSelectedVoice && !itSelectedVoice->IsStealable()) ++itSelectedVoice;
977                      // if we haven't found a voice then proceed with algorithm 'oldestkey'                      // if we haven't found a voice then proceed with algorithm 'oldestkey'
978                      if (itSelectedVoice && itSelectedVoice->hasRendered()) break;                      if (itSelectedVoice && itSelectedVoice->IsStealable()) break;
979                  } // no break - intentional !                  } // no break - intentional !
980    
981                  // try to pick the oldest voice on the oldest active key                  // try to pick the oldest voice on the oldest active key
# Line 827  namespace LinuxSampler { namespace gig { Line 987  namespace LinuxSampler { namespace gig {
987                          itSelectedVoice = this->itLastStolenVoice;                          itSelectedVoice = this->itLastStolenVoice;
988                          do {                          do {
989                              ++itSelectedVoice;                              ++itSelectedVoice;
990                          } while (itSelectedVoice && !itSelectedVoice->hasRendered()); // proceed iterating if voice was created in this fragment cycle                          } while (itSelectedVoice && !itSelectedVoice->IsStealable()); // proceed iterating if voice was created in this fragment cycle
991                          // found a "stealable" voice ?                          // found a "stealable" voice ?
992                          if (itSelectedVoice && itSelectedVoice->hasRendered()) {                          if (itSelectedVoice && itSelectedVoice->IsStealable()) {
993                              // remember which voice we stole, so we can simply proceed on next voice stealing                              // remember which voice we stole, so we can simply proceed on next voice stealing
994                              this->itLastStolenVoice = itSelectedVoice;                              this->itLastStolenVoice = itSelectedVoice;
995                              break; // selection succeeded                              break; // selection succeeded
# Line 839  namespace LinuxSampler { namespace gig { Line 999  namespace LinuxSampler { namespace gig {
999                      RTList<uint>::Iterator iuiSelectedKey = (this->iuiLastStolenKey) ? ++this->iuiLastStolenKey : pEngineChannel->pActiveKeys->first();                      RTList<uint>::Iterator iuiSelectedKey = (this->iuiLastStolenKey) ? ++this->iuiLastStolenKey : pEngineChannel->pActiveKeys->first();
1000                      while (iuiSelectedKey) {                      while (iuiSelectedKey) {
1001                          midi_key_info_t* pSelectedKey = &pEngineChannel->pMIDIKeyInfo[*iuiSelectedKey];                          midi_key_info_t* pSelectedKey = &pEngineChannel->pMIDIKeyInfo[*iuiSelectedKey];
1002                          itSelectedVoice = pSelectedKey->pActiveVoices->first();                                                  itSelectedVoice = pSelectedKey->pActiveVoices->first();
1003                          // proceed iterating if voice was created in this fragment cycle                          // proceed iterating if voice was created in this fragment cycle
1004                          while (itSelectedVoice && !itSelectedVoice->hasRendered()) ++itSelectedVoice;                          while (itSelectedVoice && !itSelectedVoice->IsStealable()) ++itSelectedVoice;
1005                          // found a "stealable" voice ?                          // found a "stealable" voice ?
1006                          if (itSelectedVoice && itSelectedVoice->hasRendered()) {                          if (itSelectedVoice && itSelectedVoice->IsStealable()) {
1007                              // remember which voice on which key we stole, so we can simply proceed on next voice stealing                              // remember which voice on which key we stole, so we can simply proceed on next voice stealing
1008                              this->iuiLastStolenKey  = iuiSelectedKey;                              this->iuiLastStolenKey  = iuiSelectedKey;
1009                              this->itLastStolenVoice = itSelectedVoice;                              this->itLastStolenVoice = itSelectedVoice;
# Line 865  namespace LinuxSampler { namespace gig { Line 1025  namespace LinuxSampler { namespace gig {
1025              // if we couldn't steal a voice from the same engine channel then              // if we couldn't steal a voice from the same engine channel then
1026              // steal oldest voice on the oldest key from any other engine channel              // steal oldest voice on the oldest key from any other engine channel
1027              // (the smaller engine channel number, the higher priority)              // (the smaller engine channel number, the higher priority)
1028              if (!itSelectedVoice || !itSelectedVoice->hasRendered()) {              if (!itSelectedVoice || !itSelectedVoice->IsStealable()) {
1029                  EngineChannel* pSelectedChannel;                  EngineChannel* pSelectedChannel;
1030                  int            iChannelIndex;                  int            iChannelIndex;
1031                  // select engine channel                  // select engine channel
# Line 876  namespace LinuxSampler { namespace gig { Line 1036  namespace LinuxSampler { namespace gig {
1036                      iChannelIndex    = (pEngineChannel->iEngineIndexSelf + 1) % engineChannels.size();                      iChannelIndex    = (pEngineChannel->iEngineIndexSelf + 1) % engineChannels.size();
1037                      pSelectedChannel = engineChannels[iChannelIndex];                      pSelectedChannel = engineChannels[iChannelIndex];
1038                  }                  }
1039                  // iterate through engine channels  
1040                  while (true) {                  // if we already stole in this fragment, try to proceed on same key
1041                      // if we already stole in this fragment, try to proceed on same key                  if (this->itLastStolenVoiceGlobally) {
1042                      if (this->itLastStolenVoiceGlobally) {                      itSelectedVoice = this->itLastStolenVoiceGlobally;
1043                          itSelectedVoice = this->itLastStolenVoiceGlobally;                      do {
1044                          do {                          ++itSelectedVoice;
1045                              ++itSelectedVoice;                      } while (itSelectedVoice && !itSelectedVoice->IsStealable()); // proceed iterating if voice was created in this fragment cycle
1046                          } while (itSelectedVoice && !itSelectedVoice->hasRendered()); // proceed iterating if voice was created in this fragment cycle                  }
1047                          // break if selection succeeded  
1048                          if (itSelectedVoice && itSelectedVoice->hasRendered()) {                  #if CONFIG_DEVMODE
1049                              // remember which voice we stole, so we can simply proceed on next voice stealing                  EngineChannel* pBegin = pSelectedChannel; // to detect endless loop
1050                              this->itLastStolenVoiceGlobally = itSelectedVoice;                  #endif // CONFIG_DEVMODE
1051                              break; // selection succeeded  
1052                          }                  // did we find a 'stealable' voice?
1053                      }                  if (itSelectedVoice && itSelectedVoice->IsStealable()) {
1054                        // remember which voice we stole, so we can simply proceed on next voice stealing
1055                        this->itLastStolenVoiceGlobally = itSelectedVoice;
1056                    } else while (true) { // iterate through engine channels
1057                      // get (next) oldest key                      // get (next) oldest key
1058                      RTList<uint>::Iterator iuiSelectedKey = (this->iuiLastStolenKey) ? ++this->iuiLastStolenKey : pSelectedChannel->pActiveKeys->first();                      RTList<uint>::Iterator iuiSelectedKey = (this->iuiLastStolenKeyGlobally) ? ++this->iuiLastStolenKeyGlobally : pSelectedChannel->pActiveKeys->first();
1059                        this->iuiLastStolenKeyGlobally = RTList<uint>::Iterator(); // to prevent endless loop (see line above)
1060                      while (iuiSelectedKey) {                      while (iuiSelectedKey) {
1061                          midi_key_info_t* pSelectedKey = &pEngineChannel->pMIDIKeyInfo[*iuiSelectedKey];                          midi_key_info_t* pSelectedKey = &pSelectedChannel->pMIDIKeyInfo[*iuiSelectedKey];
1062                          itSelectedVoice = pSelectedKey->pActiveVoices->first();                          itSelectedVoice = pSelectedKey->pActiveVoices->first();
1063                          // proceed iterating if voice was created in this fragment cycle                          // proceed iterating if voice was created in this fragment cycle
1064                          while (itSelectedVoice && !itSelectedVoice->hasRendered()) ++itSelectedVoice;                          while (itSelectedVoice && !itSelectedVoice->IsStealable()) ++itSelectedVoice;
1065                          // found a "stealable" voice ?                          // found a "stealable" voice ?
1066                          if (itSelectedVoice && itSelectedVoice->hasRendered()) {                          if (itSelectedVoice && itSelectedVoice->IsStealable()) {
1067                              // remember which voice on which key on which engine channel we stole, so we can simply proceed on next voice stealing                              // remember which voice on which key on which engine channel we stole, so we can simply proceed on next voice stealing
1068                              this->iuiLastStolenKeyGlobally  = iuiSelectedKey;                              this->iuiLastStolenKeyGlobally  = iuiSelectedKey;
1069                              this->itLastStolenVoiceGlobally = itSelectedVoice;                              this->itLastStolenVoiceGlobally = itSelectedVoice;
1070                              this->pLastStolenChannel        = pSelectedChannel;                              this->pLastStolenChannel        = pSelectedChannel;
1071                              break; // selection succeeded                              goto stealable_voice_found; // selection succeeded
1072                          }                          }
1073                          ++iuiSelectedKey; // get next key on current engine channel                          ++iuiSelectedKey; // get next key on current engine channel
1074                      }                      }
1075                      // get next engine channel                      // get next engine channel
1076                      iChannelIndex    = (iChannelIndex + 1) % engineChannels.size();                      iChannelIndex    = (iChannelIndex + 1) % engineChannels.size();
1077                      pSelectedChannel = engineChannels[iChannelIndex];                      pSelectedChannel = engineChannels[iChannelIndex];
1078    
1079                        #if CONFIG_DEVMODE
1080                        if (pSelectedChannel == pBegin) {
1081                            dmsg(1,("FATAL ERROR: voice stealing endless loop!\n"));
1082                            dmsg(1,("VoiceSpawnsLeft=%d.\n", VoiceSpawnsLeft));
1083                            dmsg(1,("Exiting.\n"));
1084                            exit(-1);
1085                        }
1086                        #endif // CONFIG_DEVMODE
1087                  }                  }
1088              }              }
1089    
1090                // jump point if a 'stealable' voice was found
1091                stealable_voice_found:
1092    
1093              #if CONFIG_DEVMODE              #if CONFIG_DEVMODE
1094              if (!itSelectedVoice->IsActive()) {              if (!itSelectedVoice->IsActive()) {
1095                  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"));
# Line 922  namespace LinuxSampler { namespace gig { Line 1098  namespace LinuxSampler { namespace gig {
1098              #endif // CONFIG_DEVMODE              #endif // CONFIG_DEVMODE
1099    
1100              // now kill the selected voice              // now kill the selected voice
1101              itSelectedVoice->Kill(itNoteOnEvent);                          itSelectedVoice->Kill(itNoteOnEvent);
1102    
1103              --VoiceTheftsLeft;              --VoiceSpawnsLeft;
1104    
1105              return 0; // success              return 0; // success
1106          }          }
# Line 1000  namespace LinuxSampler { namespace gig { Line 1176  namespace LinuxSampler { namespace gig {
1176              case 7: { // volume              case 7: { // volume
1177                  //TODO: not sample accurate yet                  //TODO: not sample accurate yet
1178                  pEngineChannel->GlobalVolume = (float) itControlChangeEventOnCCList->Param.CC.Value / 127.0f;                  pEngineChannel->GlobalVolume = (float) itControlChangeEventOnCCList->Param.CC.Value / 127.0f;
1179                    pEngineChannel->bStatusChanged = true; // engine channel status has changed, so set notify flag
1180                  break;                  break;
1181              }              }
1182              case 10: { // panpot              case 10: { // panpot
# Line 1014  namespace LinuxSampler { namespace gig { Line 1191  namespace LinuxSampler { namespace gig {
1191                      dmsg(4,("PEDAL DOWN\n"));                      dmsg(4,("PEDAL DOWN\n"));
1192                      pEngineChannel->SustainPedal = true;                      pEngineChannel->SustainPedal = true;
1193    
1194                        #if !CONFIG_PROCESS_MUTED_CHANNELS
1195                        if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted
1196                        #endif
1197    
1198                      // cancel release process of voices if necessary                      // cancel release process of voices if necessary
1199                      RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();                      RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();
1200                      for (; iuiKey; ++iuiKey) {                      for (; iuiKey; ++iuiKey) {
# Line 1032  namespace LinuxSampler { namespace gig { Line 1213  namespace LinuxSampler { namespace gig {
1213                      dmsg(4,("PEDAL UP\n"));                      dmsg(4,("PEDAL UP\n"));
1214                      pEngineChannel->SustainPedal = false;                      pEngineChannel->SustainPedal = false;
1215    
1216                        #if !CONFIG_PROCESS_MUTED_CHANNELS
1217                        if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted
1218                        #endif
1219    
1220                      // release voices if their respective key is not pressed                      // release voices if their respective key is not pressed
1221                      RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();                      RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();
1222                      for (; iuiKey; ++iuiKey) {                      for (; iuiKey; ++iuiKey) {
# Line 1199  namespace LinuxSampler { namespace gig { Line 1384  namespace LinuxSampler { namespace gig {
1384              RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end();              RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end();
1385              for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key              for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key
1386                  itVoice->Kill(itKillEvent);                  itVoice->Kill(itKillEvent);
1387                    --VoiceSpawnsLeft; //FIXME: just a temporary workaround, we should check the cause in StealVoice() instead
1388              }              }
1389          }          }
1390      }      }
# Line 1255  namespace LinuxSampler { namespace gig { Line 1441  namespace LinuxSampler { namespace gig {
1441      }      }
1442    
1443      String Engine::Version() {      String Engine::Version() {
1444          String s = "$Revision: 1.41 $";          String s = "$Revision: 1.51 $";
1445          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
1446      }      }
1447    

Legend:
Removed from v.649  
changed lines
  Added in v.716

  ViewVC Help
Powered by ViewVC