/[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 665 by schoenebeck, Sun Jun 19 15:11:20 2005 UTC revision 716 by iliev, Sun Jul 24 06:57:30 2005 UTC
# Line 240  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 481  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 519  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 609  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 647  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 668  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 691  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 728  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                  --VoiceSpawnsLeft;                  --VoiceSpawnsLeft;
                 uint** ppKeyGroup = NULL;  
                 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);  
                                 --VoiceSpawnsLeft; //FIXME: just a hack, we should better check in StealVoice() if the voice was killed due to key conflict  
                             }  
                         }  
                     }  
                 }  
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 1040  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 1058  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 1282  namespace LinuxSampler { namespace gig { Line 1441  namespace LinuxSampler { namespace gig {
1441      }      }
1442    
1443      String Engine::Version() {      String Engine::Version() {
1444          String s = "$Revision: 1.45 $";          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.665  
changed lines
  Added in v.716

  ViewVC Help
Powered by ViewVC