/[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 668 by schoenebeck, Mon Jun 20 15:30:47 2005 UTC
# Line 360  namespace LinuxSampler { namespace gig { Line 360  namespace LinuxSampler { namespace gig {
360          // 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)
361          pEventGenerator->UpdateFragmentTime(Samples);          pEventGenerator->UpdateFragmentTime(Samples);
362    
363            // We only allow a maximum of CONFIG_MAX_VOICES voices to be spawned
364            // in each audio fragment. All subsequent request for spawning new
365            // voices in the same audio fragment will be ignored.
366            VoiceSpawnsLeft = CONFIG_MAX_VOICES;
367    
368          // 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
369          // (these are usually just SysEx messages)          // (these are usually just SysEx messages)
370          ImportEvents(Samples);          ImportEvents(Samples);
# Line 378  namespace LinuxSampler { namespace gig { Line 383  namespace LinuxSampler { namespace gig {
383              }              }
384          }          }
385    
         // 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;  
   
386          // reset internal voice counter (just for statistic of active voices)          // reset internal voice counter (just for statistic of active voices)
387          ActiveVoiceCountTemp = 0;          ActiveVoiceCountTemp = 0;
388    
   
389          // handle events on all engine channels          // handle events on all engine channels
390          for (int i = 0; i < engineChannels.size(); i++) {          for (int i = 0; i < engineChannels.size(); i++) {
391              if (!engineChannels[i]->pInstrument) continue; // ignore if no instrument loaded              if (!engineChannels[i]->pInstrument) continue; // ignore if no instrument loaded
# Line 520  namespace LinuxSampler { namespace gig { Line 519  namespace LinuxSampler { namespace gig {
519          for (; itVoiceStealEvent != end; ++itVoiceStealEvent) {          for (; itVoiceStealEvent != end; ++itVoiceStealEvent) {
520              EngineChannel* pEngineChannel = (EngineChannel*) itVoiceStealEvent->pEngineChannel;              EngineChannel* pEngineChannel = (EngineChannel*) itVoiceStealEvent->pEngineChannel;
521              Pool<Voice>::Iterator itNewVoice =              Pool<Voice>::Iterator itNewVoice =
522                  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);
523              if (itNewVoice) {              if (itNewVoice) {
524                  itNewVoice->Render(Samples);                  itNewVoice->Render(Samples);
525                  if (itNewVoice->IsActive()) ActiveVoiceCountTemp++; // still active                  if (itNewVoice->IsActive()) ActiveVoiceCountTemp++; // still active
# Line 648  namespace LinuxSampler { namespace gig { Line 647  namespace LinuxSampler { namespace gig {
647                  int voicesRequired = pRegion->Layers;                  int voicesRequired = pRegion->Layers;
648                  // now launch the required amount of voices                  // now launch the required amount of voices
649                  for (int i = 0; i < voicesRequired; i++)                  for (int i = 0; i < voicesRequired; i++)
650                      LaunchVoice(pEngineChannel, itNoteOnEventOnKeyList, i, false, true);                      LaunchVoice(pEngineChannel, itNoteOnEventOnKeyList, i, false, true, true);
651              }              }
652          }          }
653    
# Line 692  namespace LinuxSampler { namespace gig { Line 691  namespace LinuxSampler { namespace gig {
691    
692                      // now launch the required amount of voices                      // now launch the required amount of voices
693                      for (int i = 0; i < voicesRequired; i++)                      for (int i = 0; i < voicesRequired; i++)
694                          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
695                  }                  }
696                  pKey->ReleaseTrigger = false;                  pKey->ReleaseTrigger = false;
697              }              }
# Line 729  namespace LinuxSampler { namespace gig { Line 728  namespace LinuxSampler { namespace gig {
728       *  @param VoiceStealing       - if voice stealing should be performed       *  @param VoiceStealing       - if voice stealing should be performed
729       *                               when there is no free voice       *                               when there is no free voice
730       *                               (optional, default = true)       *                               (optional, default = true)
731         *  @param HandleKeyGroupConflicts - if voices should be killed due to a
732         *                                   key group conflict
733       *  @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
734       *           if the voice wasn't triggered (for example when no region is       *           if the voice wasn't triggered (for example when no region is
735       *           defined for the given key).       *           defined for the given key).
736       */       */
737      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) {
738          midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key];          midi_key_info_t* pKey  = &pEngineChannel->pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key];
739            ::gig::Region* pRegion = pEngineChannel->pInstrument->GetRegion(itNoteOnEvent->Param.Note.Key);
740    
741            // if nothing defined for this key
742            if (!pRegion) return Pool<Voice>::Iterator(); // nothing to do
743    
744            // handle key group (a.k.a. exclusive group) conflicts
745            if (HandleKeyGroupConflicts) {
746                // only mark the first voice of a layered voice (group) to be in a
747                // key group, so the layered voices won't kill each other
748                int iKeyGroup = (iLayer == 0 && !ReleaseTriggerVoice) ? pRegion->KeyGroup : 0;
749                if (iKeyGroup) { // if this voice / key belongs to a key group
750                    uint** ppKeyGroup = &pEngineChannel->ActiveKeyGroups[iKeyGroup];
751                    if (*ppKeyGroup) { // if there's already an active key in that key group
752                        midi_key_info_t* pOtherKey = &pEngineChannel->pMIDIKeyInfo[**ppKeyGroup];
753                        // kill all voices on the (other) key
754                        RTList<Voice>::Iterator itVoiceToBeKilled = pOtherKey->pActiveVoices->first();
755                        RTList<Voice>::Iterator end               = pOtherKey->pActiveVoices->end();
756                        for (; itVoiceToBeKilled != end; ++itVoiceToBeKilled) {
757                            if (itVoiceToBeKilled->Type != Voice::type_release_trigger) {
758                                itVoiceToBeKilled->Kill(itNoteOnEvent);
759                                --VoiceSpawnsLeft; //FIXME: just a hack, we should better check in StealVoice() if the voice was killed due to key conflict
760                            }
761                        }
762                    }
763                }
764            }
765    
766          // allocate a new voice for the key          // allocate a new voice for the key
767          Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend();          Pool<Voice>::Iterator itNewVoice = pKey->pActiveVoices->allocAppend();
# Line 745  namespace LinuxSampler { namespace gig { Line 772  namespace LinuxSampler { namespace gig {
772                  pKey->pActiveVoices->free(itNewVoice);                  pKey->pActiveVoices->free(itNewVoice);
773              }              }
774              else { // on success              else { // on success
775                  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);  
                         }  
                     }  
                 }  
776                  if (!pKey->Active) { // mark as active key                  if (!pKey->Active) { // mark as active key
777                      pKey->Active = true;                      pKey->Active = true;
778                      pKey->itSelf = pEngineChannel->pActiveKeys->allocAppend();                      pKey->itSelf = pEngineChannel->pActiveKeys->allocAppend();
779                      *pKey->itSelf = itNoteOnEvent->Param.Note.Key;                      *pKey->itSelf = itNoteOnEvent->Param.Note.Key;
780                  }                  }
781                  if (itNewVoice->KeyGroup) {                  if (itNewVoice->KeyGroup) {
782                        uint** ppKeyGroup = &pEngineChannel->ActiveKeyGroups[itNewVoice->KeyGroup];
783                      *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
784                  }                  }
785                  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 816  namespace LinuxSampler { namespace gig {
816       *  @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
817       */       */
818      int Engine::StealVoice(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) {      int Engine::StealVoice(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent) {
819          if (!VoiceTheftsLeft) {          if (VoiceSpawnsLeft <= 0) {
820              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"));
821              return -1;              return -1;
822          }          }
# Line 819  namespace LinuxSampler { namespace gig { Line 835  namespace LinuxSampler { namespace gig {
835                      midi_key_info_t* pSelectedKey = &pEngineChannel->pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key];                      midi_key_info_t* pSelectedKey = &pEngineChannel->pMIDIKeyInfo[itNoteOnEvent->Param.Note.Key];
836                      itSelectedVoice = pSelectedKey->pActiveVoices->first();                      itSelectedVoice = pSelectedKey->pActiveVoices->first();
837                      // proceed iterating if voice was created in this fragment cycle                      // proceed iterating if voice was created in this fragment cycle
838                      while (itSelectedVoice && !itSelectedVoice->hasRendered()) ++itSelectedVoice;                      while (itSelectedVoice && !itSelectedVoice->IsStealable()) ++itSelectedVoice;
839                      // if we haven't found a voice then proceed with algorithm 'oldestkey'                      // if we haven't found a voice then proceed with algorithm 'oldestkey'
840                      if (itSelectedVoice && itSelectedVoice->hasRendered()) break;                      if (itSelectedVoice && itSelectedVoice->IsStealable()) break;
841                  } // no break - intentional !                  } // no break - intentional !
842    
843                  // 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 849  namespace LinuxSampler { namespace gig {
849                          itSelectedVoice = this->itLastStolenVoice;                          itSelectedVoice = this->itLastStolenVoice;
850                          do {                          do {
851                              ++itSelectedVoice;                              ++itSelectedVoice;
852                          } 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
853                          // found a "stealable" voice ?                          // found a "stealable" voice ?
854                          if (itSelectedVoice && itSelectedVoice->hasRendered()) {                          if (itSelectedVoice && itSelectedVoice->IsStealable()) {
855                              // 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
856                              this->itLastStolenVoice = itSelectedVoice;                              this->itLastStolenVoice = itSelectedVoice;
857                              break; // selection succeeded                              break; // selection succeeded
# Line 847  namespace LinuxSampler { namespace gig { Line 863  namespace LinuxSampler { namespace gig {
863                          midi_key_info_t* pSelectedKey = &pEngineChannel->pMIDIKeyInfo[*iuiSelectedKey];                          midi_key_info_t* pSelectedKey = &pEngineChannel->pMIDIKeyInfo[*iuiSelectedKey];
864                          itSelectedVoice = pSelectedKey->pActiveVoices->first();                          itSelectedVoice = pSelectedKey->pActiveVoices->first();
865                          // proceed iterating if voice was created in this fragment cycle                          // proceed iterating if voice was created in this fragment cycle
866                          while (itSelectedVoice && !itSelectedVoice->hasRendered()) ++itSelectedVoice;                          while (itSelectedVoice && !itSelectedVoice->IsStealable()) ++itSelectedVoice;
867                          // found a "stealable" voice ?                          // found a "stealable" voice ?
868                          if (itSelectedVoice && itSelectedVoice->hasRendered()) {                          if (itSelectedVoice && itSelectedVoice->IsStealable()) {
869                              // 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
870                              this->iuiLastStolenKey  = iuiSelectedKey;                              this->iuiLastStolenKey  = iuiSelectedKey;
871                              this->itLastStolenVoice = itSelectedVoice;                              this->itLastStolenVoice = itSelectedVoice;
# Line 871  namespace LinuxSampler { namespace gig { Line 887  namespace LinuxSampler { namespace gig {
887              // 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
888              // steal oldest voice on the oldest key from any other engine channel              // steal oldest voice on the oldest key from any other engine channel
889              // (the smaller engine channel number, the higher priority)              // (the smaller engine channel number, the higher priority)
890              if (!itSelectedVoice || !itSelectedVoice->hasRendered()) {              if (!itSelectedVoice || !itSelectedVoice->IsStealable()) {
891                  EngineChannel* pSelectedChannel;                  EngineChannel* pSelectedChannel;
892                  int            iChannelIndex;                  int            iChannelIndex;
893                  // select engine channel                  // select engine channel
# Line 882  namespace LinuxSampler { namespace gig { Line 898  namespace LinuxSampler { namespace gig {
898                      iChannelIndex    = (pEngineChannel->iEngineIndexSelf + 1) % engineChannels.size();                      iChannelIndex    = (pEngineChannel->iEngineIndexSelf + 1) % engineChannels.size();
899                      pSelectedChannel = engineChannels[iChannelIndex];                      pSelectedChannel = engineChannels[iChannelIndex];
900                  }                  }
901                  // iterate through engine channels  
902                  while (true) {                  // if we already stole in this fragment, try to proceed on same key
903                      // if we already stole in this fragment, try to proceed on same key                  if (this->itLastStolenVoiceGlobally) {
904                      if (this->itLastStolenVoiceGlobally) {                      itSelectedVoice = this->itLastStolenVoiceGlobally;
905                          itSelectedVoice = this->itLastStolenVoiceGlobally;                      do {
906                          do {                          ++itSelectedVoice;
907                              ++itSelectedVoice;                      } while (itSelectedVoice && !itSelectedVoice->IsStealable()); // proceed iterating if voice was created in this fragment cycle
908                          } while (itSelectedVoice && !itSelectedVoice->hasRendered()); // proceed iterating if voice was created in this fragment cycle                  }
909                          // break if selection succeeded  
910                          if (itSelectedVoice && itSelectedVoice->hasRendered()) {                  #if CONFIG_DEVMODE
911                              // remember which voice we stole, so we can simply proceed on next voice stealing                  EngineChannel* pBegin = pSelectedChannel; // to detect endless loop
912                              this->itLastStolenVoiceGlobally = itSelectedVoice;                  #endif // CONFIG_DEVMODE
913                              break; // selection succeeded  
914                          }                  // did we find a 'stealable' voice?
915                      }                  if (itSelectedVoice && itSelectedVoice->IsStealable()) {
916                        // remember which voice we stole, so we can simply proceed on next voice stealing
917                        this->itLastStolenVoiceGlobally = itSelectedVoice;
918                    } else while (true) { // iterate through engine channels
919                      // get (next) oldest key                      // get (next) oldest key
920                      RTList<uint>::Iterator iuiSelectedKey = (this->iuiLastStolenKey) ? ++this->iuiLastStolenKey : pSelectedChannel->pActiveKeys->first();                      RTList<uint>::Iterator iuiSelectedKey = (this->iuiLastStolenKeyGlobally) ? ++this->iuiLastStolenKeyGlobally : pSelectedChannel->pActiveKeys->first();
921                        this->iuiLastStolenKeyGlobally = RTList<uint>::Iterator(); // to prevent endless loop (see line above)
922                      while (iuiSelectedKey) {                      while (iuiSelectedKey) {
923                          midi_key_info_t* pSelectedKey = &pEngineChannel->pMIDIKeyInfo[*iuiSelectedKey];                          midi_key_info_t* pSelectedKey = &pSelectedChannel->pMIDIKeyInfo[*iuiSelectedKey];
924                          itSelectedVoice = pSelectedKey->pActiveVoices->first();                          itSelectedVoice = pSelectedKey->pActiveVoices->first();
925                          // proceed iterating if voice was created in this fragment cycle                          // proceed iterating if voice was created in this fragment cycle
926                          while (itSelectedVoice && !itSelectedVoice->hasRendered()) ++itSelectedVoice;                          while (itSelectedVoice && !itSelectedVoice->IsStealable()) ++itSelectedVoice;
927                          // found a "stealable" voice ?                          // found a "stealable" voice ?
928                          if (itSelectedVoice && itSelectedVoice->hasRendered()) {                          if (itSelectedVoice && itSelectedVoice->IsStealable()) {
929                              // 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
930                              this->iuiLastStolenKeyGlobally  = iuiSelectedKey;                              this->iuiLastStolenKeyGlobally  = iuiSelectedKey;
931                              this->itLastStolenVoiceGlobally = itSelectedVoice;                              this->itLastStolenVoiceGlobally = itSelectedVoice;
932                              this->pLastStolenChannel        = pSelectedChannel;                              this->pLastStolenChannel        = pSelectedChannel;
933                              break; // selection succeeded                              goto stealable_voice_found; // selection succeeded
934                          }                          }
935                          ++iuiSelectedKey; // get next key on current engine channel                          ++iuiSelectedKey; // get next key on current engine channel
936                      }                      }
937                      // get next engine channel                      // get next engine channel
938                      iChannelIndex    = (iChannelIndex + 1) % engineChannels.size();                      iChannelIndex    = (iChannelIndex + 1) % engineChannels.size();
939                      pSelectedChannel = engineChannels[iChannelIndex];                      pSelectedChannel = engineChannels[iChannelIndex];
940    
941                        #if CONFIG_DEVMODE
942                        if (pSelectedChannel == pBegin) {
943                            dmsg(1,("FATAL ERROR: voice stealing endless loop!\n"));
944                            dmsg(1,("VoiceSpawnsLeft=%d.\n", VoiceSpawnsLeft));
945                            dmsg(1,("Exiting.\n"));
946                            exit(-1);
947                        }
948                        #endif // CONFIG_DEVMODE
949                  }                  }
950              }              }
951    
952                // jump point if a 'stealable' voice was found
953                stealable_voice_found:
954    
955              #if CONFIG_DEVMODE              #if CONFIG_DEVMODE
956              if (!itSelectedVoice->IsActive()) {              if (!itSelectedVoice->IsActive()) {
957                  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 962  namespace LinuxSampler { namespace gig {
962              // now kill the selected voice              // now kill the selected voice
963              itSelectedVoice->Kill(itNoteOnEvent);              itSelectedVoice->Kill(itNoteOnEvent);
964    
965              --VoiceTheftsLeft;              --VoiceSpawnsLeft;
966    
967              return 0; // success              return 0; // success
968          }          }
# Line 1006  namespace LinuxSampler { namespace gig { Line 1038  namespace LinuxSampler { namespace gig {
1038              case 7: { // volume              case 7: { // volume
1039                  //TODO: not sample accurate yet                  //TODO: not sample accurate yet
1040                  pEngineChannel->GlobalVolume = (float) itControlChangeEventOnCCList->Param.CC.Value / 127.0f;                  pEngineChannel->GlobalVolume = (float) itControlChangeEventOnCCList->Param.CC.Value / 127.0f;
1041                    pEngineChannel->bStatusChanged = true; // engine channel status has changed, so set notify flag
1042                  break;                  break;
1043              }              }
1044              case 10: { // panpot              case 10: { // panpot
# Line 1205  namespace LinuxSampler { namespace gig { Line 1238  namespace LinuxSampler { namespace gig {
1238              RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end();              RTList<Voice>::Iterator itVoicesEnd = pKey->pActiveVoices->end();
1239              for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key              for (; itVoice != itVoicesEnd; ++itVoice) { // iterate through all voices on this key
1240                  itVoice->Kill(itKillEvent);                  itVoice->Kill(itKillEvent);
1241                    --VoiceSpawnsLeft; //FIXME: just a temporary workaround, we should check the cause in StealVoice() instead
1242              }              }
1243          }          }
1244      }      }
# Line 1261  namespace LinuxSampler { namespace gig { Line 1295  namespace LinuxSampler { namespace gig {
1295      }      }
1296    
1297      String Engine::Version() {      String Engine::Version() {
1298          String s = "$Revision: 1.42 $";          String s = "$Revision: 1.46 $";
1299          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
1300      }      }
1301    

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

  ViewVC Help
Powered by ViewVC