/[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 659 by schoenebeck, Thu Jun 16 21:35:30 2005 UTC revision 775 by schoenebeck, Wed Sep 21 14:30:43 2005 UTC
# Line 29  Line 29 
29    
30  #include "Engine.h"  #include "Engine.h"
31    
 #if defined(__APPLE__)  
 # include <stdlib.h>  
 #else  
 # include <malloc.h>  
 #endif  
   
32  namespace LinuxSampler { namespace gig {  namespace LinuxSampler { namespace gig {
33    
34      InstrumentResourceManager Engine::instruments;      InstrumentResourceManager Engine::instruments;
# Line 114  namespace LinuxSampler { namespace gig { Line 108  namespace LinuxSampler { namespace gig {
108          }          }
109          pVoicePool->clear();          pVoicePool->clear();
110    
         pSynthesisParameters[0] = NULL; // we allocate when an audio device is connected  
         pBasicFilterParameters  = NULL;  
         pMainFilterParameters   = NULL;  
   
111          ResetInternal();          ResetInternal();
112          ResetScaleTuning();          ResetScaleTuning();
113      }      }
# Line 139  namespace LinuxSampler { namespace gig { Line 129  namespace LinuxSampler { namespace gig {
129              delete pVoicePool;              delete pVoicePool;
130          }          }
131          if (pEventGenerator) delete pEventGenerator;          if (pEventGenerator) delete pEventGenerator;
         if (pMainFilterParameters) delete[] pMainFilterParameters;  
         if (pBasicFilterParameters) delete[] pBasicFilterParameters;  
         if (pSynthesisParameters[0]) free(pSynthesisParameters[0]);  
132          if (pVoiceStealingQueue) delete pVoiceStealingQueue;          if (pVoiceStealingQueue) delete pVoiceStealingQueue;
133          if (pSysexBuffer) delete pSysexBuffer;          if (pSysexBuffer) delete pSysexBuffer;
134          EngineFactory::Destroy(this);          EngineFactory::Destroy(this);
# Line 203  namespace LinuxSampler { namespace gig { Line 190  namespace LinuxSampler { namespace gig {
190    
191          // delete all input events          // delete all input events
192          pEventQueue->init();          pEventQueue->init();
193            pSysexBuffer->init();
194      }      }
195    
196      /**      /**
# Line 240  namespace LinuxSampler { namespace gig { Line 228  namespace LinuxSampler { namespace gig {
228    
229          // FIXME: audio drivers with varying fragment sizes might be a problem here          // FIXME: audio drivers with varying fragment sizes might be a problem here
230          MaxFadeOutPos = MaxSamplesPerCycle - int(double(SampleRate) * CONFIG_EG_MIN_RELEASE_TIME) - 1;          MaxFadeOutPos = MaxSamplesPerCycle - int(double(SampleRate) * CONFIG_EG_MIN_RELEASE_TIME) - 1;
231          if (MaxFadeOutPos < 0)          if (MaxFadeOutPos < 0) {
232              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 "
233                          << "too big for current audio fragment size & sampling rate! "
234                          << "May lead to click sounds if voice stealing chimes in!\n" << std::flush;
235                // force volume ramp downs at the beginning of each fragment
236                MaxFadeOutPos = 0;
237                // lower minimum release time
238                const float minReleaseTime = (float) MaxSamplesPerCycle / (float) SampleRate;
239                for (RTList<Voice>::Iterator iterVoice = pVoicePool->allocAppend(); iterVoice == pVoicePool->last(); iterVoice = pVoicePool->allocAppend()) {
240                    iterVoice->EG1.CalculateFadeOutCoeff(minReleaseTime, SampleRate);
241                }
242                pVoicePool->clear();
243            }
244    
245          // (re)create disk thread          // (re)create disk thread
246          if (this->pDiskThread) {          if (this->pDiskThread) {
# Line 266  namespace LinuxSampler { namespace gig { Line 265  namespace LinuxSampler { namespace gig {
265          if (pEventGenerator) delete pEventGenerator;          if (pEventGenerator) delete pEventGenerator;
266          pEventGenerator = new EventGenerator(pAudioOut->SampleRate());          pEventGenerator = new EventGenerator(pAudioOut->SampleRate());
267    
         // (re)allocate synthesis parameter matrix  
         if (pSynthesisParameters[0]) free(pSynthesisParameters[0]);  
   
         #if defined(__APPLE__)  
         pSynthesisParameters[0] = (float *) malloc(Event::destination_count * sizeof(float) * pAudioOut->MaxSamplesPerCycle());  
         #else  
         pSynthesisParameters[0] = (float *) memalign(16,(Event::destination_count * sizeof(float) * pAudioOut->MaxSamplesPerCycle()));  
         #endif  
         for (int dst = 1; dst < Event::destination_count; dst++)  
             pSynthesisParameters[dst] = pSynthesisParameters[dst - 1] + pAudioOut->MaxSamplesPerCycle();  
   
         // (re)allocate biquad filter parameter sequence  
         if (pBasicFilterParameters) delete[] pBasicFilterParameters;  
         if (pMainFilterParameters)  delete[] pMainFilterParameters;  
         pBasicFilterParameters = new biquad_param_t[pAudioOut->MaxSamplesPerCycle()];  
         pMainFilterParameters  = new biquad_param_t[pAudioOut->MaxSamplesPerCycle()];  
   
268          dmsg(1,("Starting disk thread..."));          dmsg(1,("Starting disk thread..."));
269          pDiskThread->StartThread();          pDiskThread->StartThread();
270          dmsg(1,("OK\n"));          dmsg(1,("OK\n"));
# Line 360  namespace LinuxSampler { namespace gig { Line 342  namespace LinuxSampler { namespace gig {
342          // 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)
343          pEventGenerator->UpdateFragmentTime(Samples);          pEventGenerator->UpdateFragmentTime(Samples);
344    
345            // We only allow a maximum of CONFIG_MAX_VOICES voices to be spawned
346            // in each audio fragment. All subsequent request for spawning new
347            // voices in the same audio fragment will be ignored.
348            VoiceSpawnsLeft = CONFIG_MAX_VOICES;
349    
350          // 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
351          // (these are usually just SysEx messages)          // (these are usually just SysEx messages)
352          ImportEvents(Samples);          ImportEvents(Samples);
# Line 378  namespace LinuxSampler { namespace gig { Line 365  namespace LinuxSampler { namespace gig {
365              }              }
366          }          }
367    
         // 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;  
   
368          // reset internal voice counter (just for statistic of active voices)          // reset internal voice counter (just for statistic of active voices)
369          ActiveVoiceCountTemp = 0;          ActiveVoiceCountTemp = 0;
370    
   
371          // handle events on all engine channels          // handle events on all engine channels
372          for (int i = 0; i < engineChannels.size(); i++) {          for (int i = 0; i < engineChannels.size(); i++) {
373              if (!engineChannels[i]->pInstrument) continue; // ignore if no instrument loaded              if (!engineChannels[i]->pInstrument) continue; // ignore if no instrument loaded
# Line 482  namespace LinuxSampler { namespace gig { Line 463  namespace LinuxSampler { namespace gig {
463       *                         this audio fragment cycle       *                         this audio fragment cycle
464       */       */
465      void Engine::RenderActiveVoices(EngineChannel* pEngineChannel, uint Samples) {      void Engine::RenderActiveVoices(EngineChannel* pEngineChannel, uint Samples) {
466            #if !CONFIG_PROCESS_MUTED_CHANNELS
467            if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted
468            #endif
469    
470          RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();          RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();
471          RTList<uint>::Iterator end    = pEngineChannel->pActiveKeys->end();          RTList<uint>::Iterator end    = pEngineChannel->pActiveKeys->end();
472          while (iuiKey != end) { // iterate through all active keys          while (iuiKey != end) { // iterate through all active keys
# Line 520  namespace LinuxSampler { namespace gig { Line 505  namespace LinuxSampler { namespace gig {
505          for (; itVoiceStealEvent != end; ++itVoiceStealEvent) {          for (; itVoiceStealEvent != end; ++itVoiceStealEvent) {
506              EngineChannel* pEngineChannel = (EngineChannel*) itVoiceStealEvent->pEngineChannel;              EngineChannel* pEngineChannel = (EngineChannel*) itVoiceStealEvent->pEngineChannel;
507              Pool<Voice>::Iterator itNewVoice =              Pool<Voice>::Iterator itNewVoice =
508                  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);
509              if (itNewVoice) {              if (itNewVoice) {
510                  itNewVoice->Render(Samples);                  itNewVoice->Render(Samples);
511                  if (itNewVoice->IsActive()) ActiveVoiceCountTemp++; // still active                  if (itNewVoice->IsActive()) ActiveVoiceCountTemp++; // still active
# Line 610  namespace LinuxSampler { namespace gig { Line 595  namespace LinuxSampler { namespace gig {
595       *  @param itNoteOnEvent - key, velocity and time stamp of the event       *  @param itNoteOnEvent - key, velocity and time stamp of the event
596       */       */
597      void Engine::ProcessNoteOn(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) {      void Engine::ProcessNoteOn(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) {
598            #if !CONFIG_PROCESS_MUTED_CHANNELS
599            if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted
600            #endif
601    
602          const int key = itNoteOnEvent->Param.Note.Key;          const int key = itNoteOnEvent->Param.Note.Key;
603    
# Line 648  namespace LinuxSampler { namespace gig { Line 636  namespace LinuxSampler { namespace gig {
636                  int voicesRequired = pRegion->Layers;                  int voicesRequired = pRegion->Layers;
637                  // now launch the required amount of voices                  // now launch the required amount of voices
638                  for (int i = 0; i < voicesRequired; i++)                  for (int i = 0; i < voicesRequired; i++)
639                      LaunchVoice(pEngineChannel, itNoteOnEventOnKeyList, i, false, true);                      LaunchVoice(pEngineChannel, itNoteOnEventOnKeyList, i, false, true, true);
640              }              }
641          }          }
642    
# Line 669  namespace LinuxSampler { namespace gig { Line 657  namespace LinuxSampler { namespace gig {
657       *  @param itNoteOffEvent - key, velocity and time stamp of the event       *  @param itNoteOffEvent - key, velocity and time stamp of the event
658       */       */
659      void Engine::ProcessNoteOff(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOffEvent) {      void Engine::ProcessNoteOff(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOffEvent) {
660          midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itNoteOffEvent->Param.Note.Key];          #if !CONFIG_PROCESS_MUTED_CHANNELS
661            if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted
662            #endif
663    
664            midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itNoteOffEvent->Param.Note.Key];
665          pKey->KeyPressed = false; // the MIDI key was now released          pKey->KeyPressed = false; // the MIDI key was now released
666    
667          // release voices on this key if needed          // release voices on this key if needed
# Line 692  namespace LinuxSampler { namespace gig { Line 683  namespace LinuxSampler { namespace gig {
683    
684                      // now launch the required amount of voices                      // now launch the required amount of voices
685                      for (int i = 0; i < voicesRequired; i++)                      for (int i = 0; i < voicesRequired; i++)
686                          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
687                  }                  }
688                  pKey->ReleaseTrigger = false;                  pKey->ReleaseTrigger = false;
689              }              }
# Line 704  namespace LinuxSampler { namespace gig { Line 695  namespace LinuxSampler { namespace gig {
695      }      }
696    
697      /**      /**
698       *  Moves pitchbend event from the general (input) event list to the pitch       *  Moves pitchbend event from the general (input) event list to the engine
699       *  event list.       *  channel's event list. It will actually processed later by the
700         *  respective voice.
701       *       *
702       *  @param pEngineChannel - engine channel on which this event occured on       *  @param pEngineChannel - engine channel on which this event occured on
703       *  @param itPitchbendEvent - absolute pitch value and time stamp of the event       *  @param itPitchbendEvent - absolute pitch value and time stamp of the event
704       */       */
705      void Engine::ProcessPitchbend(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itPitchbendEvent) {      void Engine::ProcessPitchbend(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itPitchbendEvent) {
706          pEngineChannel->Pitch = itPitchbendEvent->Param.Pitch.Pitch; // store current pitch value          pEngineChannel->Pitch = itPitchbendEvent->Param.Pitch.Pitch; // store current pitch value
         itPitchbendEvent.moveToEndOf(pEngineChannel->pSynthesisEvents[Event::destination_vco]);  
707      }      }
708    
709      /**      /**
# Line 729  namespace LinuxSampler { namespace gig { Line 720  namespace LinuxSampler { namespace gig {
720       *  @param VoiceStealing       - if voice stealing should be performed       *  @param VoiceStealing       - if voice stealing should be performed
721       *                               when there is no free voice       *                               when there is no free voice
722       *                               (optional, default = true)       *                               (optional, default = true)
723         *  @param HandleKeyGroupConflicts - if voices should be killed due to a
724         *                                   key group conflict
725       *  @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
726       *           if the voice wasn't triggered (for example when no region is       *           if the voice wasn't triggered (for example when no region is
727       *           defined for the given key).       *           defined for the given key).
728       */       */
729      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) {
730          midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key];          int MIDIKey            = itNoteOnEvent->Param.Note.Key;
731            midi_key_info_t* pKey  = &pEngineChannel->pMIDIKeyInfo[MIDIKey];
732            ::gig::Region* pRegion = pEngineChannel->pInstrument->GetRegion(MIDIKey);
733    
734            // if nothing defined for this key
735            if (!pRegion) return Pool<Voice>::Iterator(); // nothing to do
736    
737            // only mark the first voice of a layered voice (group) to be in a
738            // key group, so the layered voices won't kill each other
739            int iKeyGroup = (iLayer == 0 && !ReleaseTriggerVoice) ? pRegion->KeyGroup : 0;
740    
741            // handle key group (a.k.a. exclusive group) conflicts
742            if (HandleKeyGroupConflicts) {
743                if (iKeyGroup) { // if this voice / key belongs to a key group
744                    uint** ppKeyGroup = &pEngineChannel->ActiveKeyGroups[iKeyGroup];
745                    if (*ppKeyGroup) { // if there's already an active key in that key group
746                        midi_key_info_t* pOtherKey = &pEngineChannel->pMIDIKeyInfo[**ppKeyGroup];
747                        // kill all voices on the (other) key
748                        RTList<Voice>::Iterator itVoiceToBeKilled = pOtherKey->pActiveVoices->first();
749                        RTList<Voice>::Iterator end               = pOtherKey->pActiveVoices->end();
750                        for (; itVoiceToBeKilled != end; ++itVoiceToBeKilled) {
751                            if (itVoiceToBeKilled->Type != Voice::type_release_trigger) {
752                                itVoiceToBeKilled->Kill(itNoteOnEvent);
753                                --VoiceSpawnsLeft; //FIXME: just a hack, we should better check in StealVoice() if the voice was killed due to key conflict
754                            }
755                        }
756                    }
757                }
758            }
759    
760            Voice::type_t VoiceType = Voice::type_normal;
761    
762            // get current dimension values to select the right dimension region
763            //TODO: for stolen voices this dimension region selection block is processed twice, this should be changed
764            //FIXME: controller values for selecting the dimension region here are currently not sample accurate
765            uint DimValues[8] = { 0 };
766            for (int i = pRegion->Dimensions - 1; i >= 0; i--) {
767                switch (pRegion->pDimensionDefinitions[i].dimension) {
768                    case ::gig::dimension_samplechannel:
769                        DimValues[i] = 0; //TODO: we currently ignore this dimension
770                        break;
771                    case ::gig::dimension_layer:
772                        DimValues[i] = iLayer;
773                        break;
774                    case ::gig::dimension_velocity:
775                        DimValues[i] = itNoteOnEvent->Param.Note.Velocity;
776                        break;
777                    case ::gig::dimension_channelaftertouch:
778                        DimValues[i] = 0; //TODO: we currently ignore this dimension
779                        break;
780                    case ::gig::dimension_releasetrigger:
781                        VoiceType = (ReleaseTriggerVoice) ? Voice::type_release_trigger : (!iLayer) ? Voice::type_release_trigger_required : Voice::type_normal;
782                        DimValues[i] = (uint) ReleaseTriggerVoice;
783                        break;
784                    case ::gig::dimension_keyboard:
785                        DimValues[i] = (uint) pEngineChannel->CurrentKeyDimension;
786                        break;
787                    case ::gig::dimension_roundrobin:
788                        DimValues[i] = (uint) pEngineChannel->pMIDIKeyInfo[MIDIKey].RoundRobinIndex; // incremented for each note on
789                        break;
790                    case ::gig::dimension_random:
791                        RandomSeed   = RandomSeed * 1103515245 + 12345; // classic pseudo random number generator
792                        DimValues[i] = (uint) RandomSeed >> (32 - pRegion->pDimensionDefinitions[i].bits); // highest bits are most random
793                        break;
794                    case ::gig::dimension_modwheel:
795                        DimValues[i] = pEngineChannel->ControllerTable[1];
796                        break;
797                    case ::gig::dimension_breath:
798                        DimValues[i] = pEngineChannel->ControllerTable[2];
799                        break;
800                    case ::gig::dimension_foot:
801                        DimValues[i] = pEngineChannel->ControllerTable[4];
802                        break;
803                    case ::gig::dimension_portamentotime:
804                        DimValues[i] = pEngineChannel->ControllerTable[5];
805                        break;
806                    case ::gig::dimension_effect1:
807                        DimValues[i] = pEngineChannel->ControllerTable[12];
808                        break;
809                    case ::gig::dimension_effect2:
810                        DimValues[i] = pEngineChannel->ControllerTable[13];
811                        break;
812                    case ::gig::dimension_genpurpose1:
813                        DimValues[i] = pEngineChannel->ControllerTable[16];
814                        break;
815                    case ::gig::dimension_genpurpose2:
816                        DimValues[i] = pEngineChannel->ControllerTable[17];
817                        break;
818                    case ::gig::dimension_genpurpose3:
819                        DimValues[i] = pEngineChannel->ControllerTable[18];
820                        break;
821                    case ::gig::dimension_genpurpose4:
822                        DimValues[i] = pEngineChannel->ControllerTable[19];
823                        break;
824                    case ::gig::dimension_sustainpedal:
825                        DimValues[i] = pEngineChannel->ControllerTable[64];
826                        break;
827                    case ::gig::dimension_portamento:
828                        DimValues[i] = pEngineChannel->ControllerTable[65];
829                        break;
830                    case ::gig::dimension_sostenutopedal:
831                        DimValues[i] = pEngineChannel->ControllerTable[66];
832                        break;
833                    case ::gig::dimension_softpedal:
834                        DimValues[i] = pEngineChannel->ControllerTable[67];
835                        break;
836                    case ::gig::dimension_genpurpose5:
837                        DimValues[i] = pEngineChannel->ControllerTable[80];
838                        break;
839                    case ::gig::dimension_genpurpose6:
840                        DimValues[i] = pEngineChannel->ControllerTable[81];
841                        break;
842                    case ::gig::dimension_genpurpose7:
843                        DimValues[i] = pEngineChannel->ControllerTable[82];
844                        break;
845                    case ::gig::dimension_genpurpose8:
846                        DimValues[i] = pEngineChannel->ControllerTable[83];
847                        break;
848                    case ::gig::dimension_effect1depth:
849                        DimValues[i] = pEngineChannel->ControllerTable[91];
850                        break;
851                    case ::gig::dimension_effect2depth:
852                        DimValues[i] = pEngineChannel->ControllerTable[92];
853                        break;
854                    case ::gig::dimension_effect3depth:
855                        DimValues[i] = pEngineChannel->ControllerTable[93];
856                        break;
857                    case ::gig::dimension_effect4depth:
858                        DimValues[i] = pEngineChannel->ControllerTable[94];
859                        break;
860                    case ::gig::dimension_effect5depth:
861                        DimValues[i] = pEngineChannel->ControllerTable[95];
862                        break;
863                    case ::gig::dimension_none:
864                        std::cerr << "gig::Engine::LaunchVoice() Error: dimension=none\n" << std::flush;
865                        break;
866                    default:
867                        std::cerr << "gig::Engine::LaunchVoice() Error: Unknown dimension\n" << std::flush;
868                }
869            }
870            ::gig::DimensionRegion* pDimRgn = pRegion->GetDimensionRegionByValue(DimValues);
871    
872            // no need to continue if sample is silent
873            if (!pDimRgn->pSample || !pDimRgn->pSample->SamplesTotal) return Pool<Voice>::Iterator();
874    
875          // allocate a new voice for the key          // allocate a new voice for the key
876          Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend();          Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend();
877          if (itNewVoice) {          if (itNewVoice) {
878              // launch the new voice              // launch the new voice
879              if (itNewVoice->Trigger(pEngineChannel, itNoteOnEvent, pEngineChannel->Pitch, pEngineChannel->pInstrument, iLayer, ReleaseTriggerVoice, VoiceStealing) < 0) {              if (itNewVoice->Trigger(pEngineChannel, itNoteOnEvent, pEngineChannel->Pitch, pDimRgn, VoiceType, iKeyGroup) < 0) {
880                  dmsg(4,("Voice not triggered\n"));                  dmsg(4,("Voice not triggered\n"));
881                  pKey->pActiveVoices->free(itNewVoice);                  pKey->pActiveVoices->free(itNewVoice);
882              }              }
883              else { // on success              else { // on success
884                  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);  
                         }  
                     }  
                 }  
885                  if (!pKey->Active) { // mark as active key                  if (!pKey->Active) { // mark as active key
886                      pKey->Active = true;                      pKey->Active = true;
887                      pKey->itSelf = pEngineChannel->pActiveKeys->allocAppend();                      pKey->itSelf = pEngineChannel->pActiveKeys->allocAppend();
888                      *pKey->itSelf = itNoteOnEvent->Param.Note.Key;                      *pKey->itSelf = itNoteOnEvent->Param.Note.Key;
889                  }                  }
890                  if (itNewVoice->KeyGroup) {                  if (itNewVoice->KeyGroup) {
891                        uint** ppKeyGroup = &pEngineChannel->ActiveKeyGroups[itNewVoice->KeyGroup];
892                      *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
893                  }                  }
894                  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 800  namespace LinuxSampler { namespace gig { Line 925  namespace LinuxSampler { namespace gig {
925       *  @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
926       */       */
927      int Engine::StealVoice(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) {      int Engine::StealVoice(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) {
928          if (!VoiceTheftsLeft) {          if (VoiceSpawnsLeft <= 0) {
929              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"));
930              return -1;              return -1;
931          }          }
# Line 819  namespace LinuxSampler { namespace gig { Line 944  namespace LinuxSampler { namespace gig {
944                      midi_key_info_t* pSelectedKey = &pEngineChannel->pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key];                      midi_key_info_t* pSelectedKey = &pEngineChannel->pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key];
945                      itSelectedVoice = pSelectedKey->pActiveVoices->first();                      itSelectedVoice = pSelectedKey->pActiveVoices->first();
946                      // proceed iterating if voice was created in this fragment cycle                      // proceed iterating if voice was created in this fragment cycle
947                      while (itSelectedVoice && !itSelectedVoice->hasRendered()) ++itSelectedVoice;                      while (itSelectedVoice && !itSelectedVoice->IsStealable()) ++itSelectedVoice;
948                      // if we haven't found a voice then proceed with algorithm 'oldestkey'                      // if we haven't found a voice then proceed with algorithm 'oldestkey'
949                      if (itSelectedVoice && itSelectedVoice->hasRendered()) break;                      if (itSelectedVoice && itSelectedVoice->IsStealable()) break;
950                  } // no break - intentional !                  } // no break - intentional !
951    
952                  // try to pick the oldest voice on the oldest active key                  // try to pick the oldest voice on the oldest active key
# Line 833  namespace LinuxSampler { namespace gig { Line 958  namespace LinuxSampler { namespace gig {
958                          itSelectedVoice = this->itLastStolenVoice;                          itSelectedVoice = this->itLastStolenVoice;
959                          do {                          do {
960                              ++itSelectedVoice;                              ++itSelectedVoice;
961                          } 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
962                          // found a "stealable" voice ?                          // found a "stealable" voice ?
963                          if (itSelectedVoice && itSelectedVoice->hasRendered()) {                          if (itSelectedVoice && itSelectedVoice->IsStealable()) {
964                              // 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
965                              this->itLastStolenVoice = itSelectedVoice;                              this->itLastStolenVoice = itSelectedVoice;
966                              break; // selection succeeded                              break; // selection succeeded
# Line 847  namespace LinuxSampler { namespace gig { Line 972  namespace LinuxSampler { namespace gig {
972                          midi_key_info_t* pSelectedKey = &pEngineChannel->pMIDIKeyInfo[*iuiSelectedKey];                          midi_key_info_t* pSelectedKey = &pEngineChannel->pMIDIKeyInfo[*iuiSelectedKey];
973                          itSelectedVoice = pSelectedKey->pActiveVoices->first();                          itSelectedVoice = pSelectedKey->pActiveVoices->first();
974                          // proceed iterating if voice was created in this fragment cycle                          // proceed iterating if voice was created in this fragment cycle
975                          while (itSelectedVoice && !itSelectedVoice->hasRendered()) ++itSelectedVoice;                          while (itSelectedVoice && !itSelectedVoice->IsStealable()) ++itSelectedVoice;
976                          // found a "stealable" voice ?                          // found a "stealable" voice ?
977                          if (itSelectedVoice && itSelectedVoice->hasRendered()) {                          if (itSelectedVoice && itSelectedVoice->IsStealable()) {
978                              // 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
979                              this->iuiLastStolenKey  = iuiSelectedKey;                              this->iuiLastStolenKey  = iuiSelectedKey;
980                              this->itLastStolenVoice = itSelectedVoice;                              this->itLastStolenVoice = itSelectedVoice;
# Line 871  namespace LinuxSampler { namespace gig { Line 996  namespace LinuxSampler { namespace gig {
996              // 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
997              // steal oldest voice on the oldest key from any other engine channel              // steal oldest voice on the oldest key from any other engine channel
998              // (the smaller engine channel number, the higher priority)              // (the smaller engine channel number, the higher priority)
999              if (!itSelectedVoice || !itSelectedVoice->hasRendered()) {              if (!itSelectedVoice || !itSelectedVoice->IsStealable()) {
1000                  EngineChannel* pSelectedChannel;                  EngineChannel* pSelectedChannel;
1001                  int            iChannelIndex;                  int            iChannelIndex;
1002                  // select engine channel                  // select engine channel
# Line 882  namespace LinuxSampler { namespace gig { Line 1007  namespace LinuxSampler { namespace gig {
1007                      iChannelIndex    = (pEngineChannel->iEngineIndexSelf + 1) % engineChannels.size();                      iChannelIndex    = (pEngineChannel->iEngineIndexSelf + 1) % engineChannels.size();
1008                      pSelectedChannel = engineChannels[iChannelIndex];                      pSelectedChannel = engineChannels[iChannelIndex];
1009                  }                  }
1010                  // iterate through engine channels  
1011                  while (true) {                  // if we already stole in this fragment, try to proceed on same key
1012                      // if we already stole in this fragment, try to proceed on same key                  if (this->itLastStolenVoiceGlobally) {
1013                      if (this->itLastStolenVoiceGlobally) {                      itSelectedVoice = this->itLastStolenVoiceGlobally;
1014                          itSelectedVoice = this->itLastStolenVoiceGlobally;                      do {
1015                          do {                          ++itSelectedVoice;
1016                              ++itSelectedVoice;                      } while (itSelectedVoice && !itSelectedVoice->IsStealable()); // proceed iterating if voice was created in this fragment cycle
1017                          } while (itSelectedVoice && !itSelectedVoice->hasRendered()); // proceed iterating if voice was created in this fragment cycle                  }
1018                          // break if selection succeeded  
1019                          if (itSelectedVoice && itSelectedVoice->hasRendered()) {                  #if CONFIG_DEVMODE
1020                              // remember which voice we stole, so we can simply proceed on next voice stealing                  EngineChannel* pBegin = pSelectedChannel; // to detect endless loop
1021                              this->itLastStolenVoiceGlobally = itSelectedVoice;                  #endif // CONFIG_DEVMODE
1022                              break; // selection succeeded  
1023                          }                  // did we find a 'stealable' voice?
1024                      }                  if (itSelectedVoice && itSelectedVoice->IsStealable()) {
1025                        // remember which voice we stole, so we can simply proceed on next voice stealing
1026                        this->itLastStolenVoiceGlobally = itSelectedVoice;
1027                    } else while (true) { // iterate through engine channels
1028                      // get (next) oldest key                      // get (next) oldest key
1029                      RTList<uint>::Iterator iuiSelectedKey = (this->iuiLastStolenKey) ? ++this->iuiLastStolenKey : pSelectedChannel->pActiveKeys->first();                      RTList<uint>::Iterator iuiSelectedKey = (this->iuiLastStolenKeyGlobally) ? ++this->iuiLastStolenKeyGlobally : pSelectedChannel->pActiveKeys->first();
1030                        this->iuiLastStolenKeyGlobally = RTList<uint>::Iterator(); // to prevent endless loop (see line above)
1031                      while (iuiSelectedKey) {                      while (iuiSelectedKey) {
1032                          midi_key_info_t* pSelectedKey = &pEngineChannel->pMIDIKeyInfo[*iuiSelectedKey];                          midi_key_info_t* pSelectedKey = &pSelectedChannel->pMIDIKeyInfo[*iuiSelectedKey];
1033                          itSelectedVoice = pSelectedKey->pActiveVoices->first();                          itSelectedVoice = pSelectedKey->pActiveVoices->first();
1034                          // proceed iterating if voice was created in this fragment cycle                          // proceed iterating if voice was created in this fragment cycle
1035                          while (itSelectedVoice && !itSelectedVoice->hasRendered()) ++itSelectedVoice;                          while (itSelectedVoice && !itSelectedVoice->IsStealable()) ++itSelectedVoice;
1036                          // found a "stealable" voice ?                          // found a "stealable" voice ?
1037                          if (itSelectedVoice && itSelectedVoice->hasRendered()) {                          if (itSelectedVoice && itSelectedVoice->IsStealable()) {
1038                              // 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
1039                              this->iuiLastStolenKeyGlobally  = iuiSelectedKey;                              this->iuiLastStolenKeyGlobally  = iuiSelectedKey;
1040                              this->itLastStolenVoiceGlobally = itSelectedVoice;                              this->itLastStolenVoiceGlobally = itSelectedVoice;
1041                              this->pLastStolenChannel        = pSelectedChannel;                              this->pLastStolenChannel        = pSelectedChannel;
1042                              break; // selection succeeded                              goto stealable_voice_found; // selection succeeded
1043                          }                          }
1044                          ++iuiSelectedKey; // get next key on current engine channel                          ++iuiSelectedKey; // get next key on current engine channel
1045                      }                      }
1046                      // get next engine channel                      // get next engine channel
1047                      iChannelIndex    = (iChannelIndex + 1) % engineChannels.size();                      iChannelIndex    = (iChannelIndex + 1) % engineChannels.size();
1048                      pSelectedChannel = engineChannels[iChannelIndex];                      pSelectedChannel = engineChannels[iChannelIndex];
1049    
1050                        #if CONFIG_DEVMODE
1051                        if (pSelectedChannel == pBegin) {
1052                            dmsg(1,("FATAL ERROR: voice stealing endless loop!\n"));
1053                            dmsg(1,("VoiceSpawnsLeft=%d.\n", VoiceSpawnsLeft));
1054                            dmsg(1,("Exiting.\n"));
1055                            exit(-1);
1056                        }
1057                        #endif // CONFIG_DEVMODE
1058                  }                  }
1059              }              }
1060    
1061                // jump point if a 'stealable' voice was found
1062                stealable_voice_found:
1063    
1064              #if CONFIG_DEVMODE              #if CONFIG_DEVMODE
1065              if (!itSelectedVoice->IsActive()) {              if (!itSelectedVoice->IsActive()) {
1066                  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 930  namespace LinuxSampler { namespace gig { Line 1071  namespace LinuxSampler { namespace gig {
1071              // now kill the selected voice              // now kill the selected voice
1072              itSelectedVoice->Kill(itNoteOnEvent);              itSelectedVoice->Kill(itNoteOnEvent);
1073    
1074              --VoiceTheftsLeft;              --VoiceSpawnsLeft;
1075    
1076              return 0; // success              return 0; // success
1077          }          }
# Line 999  namespace LinuxSampler { namespace gig { Line 1140  namespace LinuxSampler { namespace gig {
1140          // update controller value in the engine channel's controller table          // update controller value in the engine channel's controller table
1141          pEngineChannel->ControllerTable[itControlChangeEvent->Param.CC.Controller] = itControlChangeEvent->Param.CC.Value;          pEngineChannel->ControllerTable[itControlChangeEvent->Param.CC.Controller] = itControlChangeEvent->Param.CC.Value;
1142    
1143          // move event from the unsorted event list to the control change event list          switch (itControlChangeEvent->Param.CC.Controller) {
         Pool<Event>::Iterator itControlChangeEventOnCCList = itControlChangeEvent.moveToEndOf(pEngineChannel->pCCEvents);  
   
         switch (itControlChangeEventOnCCList->Param.CC.Controller) {  
1144              case 7: { // volume              case 7: { // volume
1145                  //TODO: not sample accurate yet                  //TODO: not sample accurate yet
1146                  pEngineChannel->GlobalVolume = (float) itControlChangeEventOnCCList->Param.CC.Value / 127.0f;                  pEngineChannel->GlobalVolume = (float) itControlChangeEvent->Param.CC.Value / 127.0f;
1147                    pEngineChannel->bStatusChanged = true; // engine channel status has changed, so set notify flag
1148                  break;                  break;
1149              }              }
1150              case 10: { // panpot              case 10: { // panpot
1151                  //TODO: not sample accurate yet                  //TODO: not sample accurate yet
1152                  const int pan = (int) itControlChangeEventOnCCList->Param.CC.Value - 64;                  const int pan = (int) itControlChangeEvent->Param.CC.Value - 64;
1153                  pEngineChannel->GlobalPanLeft  = 1.0f - float(RTMath::Max(pan, 0)) /  63.0f;                  pEngineChannel->GlobalPanLeft  = 1.0f - float(RTMath::Max(pan, 0)) /  63.0f;
1154                  pEngineChannel->GlobalPanRight = 1.0f - float(RTMath::Min(pan, 0)) / -64.0f;                  pEngineChannel->GlobalPanRight = 1.0f - float(RTMath::Min(pan, 0)) / -64.0f;
1155                  break;                  break;
1156              }              }
1157              case 64: { // sustain              case 64: { // sustain
1158                  if (itControlChangeEventOnCCList->Param.CC.Value >= 64 && !pEngineChannel->SustainPedal) {                  if (itControlChangeEvent->Param.CC.Value >= 64 && !pEngineChannel->SustainPedal) {
1159                      dmsg(4,("PEDAL DOWN\n"));                      dmsg(4,("PEDAL DOWN\n"));
1160                      pEngineChannel->SustainPedal = true;                      pEngineChannel->SustainPedal = true;
1161    
1162                        #if !CONFIG_PROCESS_MUTED_CHANNELS
1163                        if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted
1164                        #endif
1165    
1166                      // cancel release process of voices if necessary                      // cancel release process of voices if necessary
1167                      RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();                      RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();
1168                      for (; iuiKey; ++iuiKey) {                      for (; iuiKey; ++iuiKey) {
# Line 1027  namespace LinuxSampler { namespace gig { Line 1170  namespace LinuxSampler { namespace gig {
1170                          if (!pKey->KeyPressed) {                          if (!pKey->KeyPressed) {
1171                              RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend();                              RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend();
1172                              if (itNewEvent) {                              if (itNewEvent) {
1173                                  *itNewEvent = *itControlChangeEventOnCCList; // copy event to the key's own event list                                  *itNewEvent = *itControlChangeEvent; // copy event to the key's own event list
1174                                  itNewEvent->Type = Event::type_cancel_release; // transform event type                                  itNewEvent->Type = Event::type_cancel_release; // transform event type
1175                              }                              }
1176                              else dmsg(1,("Event pool emtpy!\n"));                              else dmsg(1,("Event pool emtpy!\n"));
1177                          }                          }
1178                      }                      }
1179                  }                  }
1180                  if (itControlChangeEventOnCCList->Param.CC.Value < 64 && pEngineChannel->SustainPedal) {                  if (itControlChangeEvent->Param.CC.Value < 64 && pEngineChannel->SustainPedal) {
1181                      dmsg(4,("PEDAL UP\n"));                      dmsg(4,("PEDAL UP\n"));
1182                      pEngineChannel->SustainPedal = false;                      pEngineChannel->SustainPedal = false;
1183    
1184                        #if !CONFIG_PROCESS_MUTED_CHANNELS
1185                        if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted
1186                        #endif
1187    
1188                      // release voices if their respective key is not pressed                      // release voices if their respective key is not pressed
1189                      RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();                      RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();
1190                      for (; iuiKey; ++iuiKey) {                      for (; iuiKey; ++iuiKey) {
# Line 1045  namespace LinuxSampler { namespace gig { Line 1192  namespace LinuxSampler { namespace gig {
1192                          if (!pKey->KeyPressed) {                          if (!pKey->KeyPressed) {
1193                              RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend();                              RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend();
1194                              if (itNewEvent) {                              if (itNewEvent) {
1195                                  *itNewEvent = *itControlChangeEventOnCCList; // copy event to the key's own event list                                  *itNewEvent = *itControlChangeEvent; // copy event to the key's own event list
1196                                  itNewEvent->Type = Event::type_release; // transform event type                                  itNewEvent->Type = Event::type_release; // transform event type
1197                              }                              }
1198                              else dmsg(1,("Event pool emtpy!\n"));                              else dmsg(1,("Event pool emtpy!\n"));
# Line 1059  namespace LinuxSampler { namespace gig { Line 1206  namespace LinuxSampler { namespace gig {
1206              // Channel Mode Messages              // Channel Mode Messages
1207    
1208              case 120: { // all sound off              case 120: { // all sound off
1209                  KillAllVoices(pEngineChannel, itControlChangeEventOnCCList);                  KillAllVoices(pEngineChannel, itControlChangeEvent);
1210                  break;                  break;
1211              }              }
1212              case 121: { // reset all controllers              case 121: { // reset all controllers
# Line 1067  namespace LinuxSampler { namespace gig { Line 1214  namespace LinuxSampler { namespace gig {
1214                  break;                  break;
1215              }              }
1216              case 123: { // all notes off              case 123: { // all notes off
1217                  ReleaseAllVoices(pEngineChannel, itControlChangeEventOnCCList);                  ReleaseAllVoices(pEngineChannel, itControlChangeEvent);
1218                  break;                  break;
1219              }              }
1220          }          }
# Line 1205  namespace LinuxSampler { namespace gig { Line 1352  namespace LinuxSampler { namespace gig {
1352              RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end();              RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end();
1353              for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key              for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key
1354                  itVoice->Kill(itKillEvent);                  itVoice->Kill(itKillEvent);
1355                    --VoiceSpawnsLeft; //FIXME: just a temporary workaround, we should check the cause in StealVoice() instead
1356              }              }
1357          }          }
1358      }      }
1359    
     /**  
      * Initialize the parameter sequence for the modulation destination given by  
      * by 'dst' with the constant value given by val.  
      */  
     void Engine::ResetSynthesisParameters(Event::destination_t dst, float val) {  
         int maxsamples = pAudioOutputDevice->MaxSamplesPerCycle();  
         float* m = &pSynthesisParameters[dst][0];  
         for (int i = 0; i < maxsamples; i += 4) {  
            m[i]   = val;  
            m[i+1] = val;  
            m[i+2] = val;  
            m[i+3] = val;  
         }  
     }  
   
1360      uint Engine::VoiceCount() {      uint Engine::VoiceCount() {
1361          return ActiveVoiceCount;          return ActiveVoiceCount;
1362      }      }
# Line 1261  namespace LinuxSampler { namespace gig { Line 1394  namespace LinuxSampler { namespace gig {
1394      }      }
1395    
1396      String Engine::Version() {      String Engine::Version() {
1397          String s = "$Revision: 1.42 $";          String s = "$Revision: 1.54 $";
1398          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
1399      }      }
1400    

Legend:
Removed from v.659  
changed lines
  Added in v.775

  ViewVC Help
Powered by ViewVC