/[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 775 by schoenebeck, Wed Sep 21 14:30:43 2005 UTC revision 832 by persson, Sun Feb 5 10:24:05 2006 UTC
# Line 3  Line 3 
3   *   LinuxSampler - modular, streaming capable sampler                     *   *   LinuxSampler - modular, streaming capable sampler                     *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *
6   *   Copyright (C) 2005 Christian Schoenebeck                              *   *   Copyright (C) 2005, 2006 Christian Schoenebeck                        *
7   *                                                                         *   *                                                                         *
8   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
9   *   it under the terms of the GNU General Public License as published by  *   *   it under the terms of the GNU General Public License as published by  *
# Line 600  namespace LinuxSampler { namespace gig { Line 600  namespace LinuxSampler { namespace gig {
600          #endif          #endif
601    
602          const int key = itNoteOnEvent->Param.Note.Key;          const int key = itNoteOnEvent->Param.Note.Key;
603            midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[key];
604    
605            // move note on event to the key's own event list
606            RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents);
607    
608            // if Solo Mode then kill all already active voices
609            if (pEngineChannel->SoloMode) {
610                Pool<uint>::Iterator itYoungestKey = pEngineChannel->pActiveKeys->last();
611                if (itYoungestKey) {
612                    const int iYoungestKey = *itYoungestKey;
613                    const midi_key_info_t* pOtherKey = &pEngineChannel->pMIDIKeyInfo[iYoungestKey];
614                    if (pOtherKey->Active) {
615                        // get final portamento position of currently active voice
616                        if (pEngineChannel->PortamentoMode) {
617                            RTList<Voice>::Iterator itVoice = pOtherKey->pActiveVoices->last();
618                            if (itVoice) itVoice->UpdatePortamentoPos(itNoteOnEventOnKeyList);
619                        }
620                        // kill all voices on the (other) key
621                        RTList<Voice>::Iterator itVoiceToBeKilled = pOtherKey->pActiveVoices->first();
622                        RTList<Voice>::Iterator end               = pOtherKey->pActiveVoices->end();
623                        for (; itVoiceToBeKilled != end; ++itVoiceToBeKilled) {
624                            if (itVoiceToBeKilled->Type != Voice::type_release_trigger)
625                                itVoiceToBeKilled->Kill(itNoteOnEventOnKeyList);
626                        }
627                    }
628                }
629                // set this key as 'currently active solo key'
630                pEngineChannel->SoloKey = key;
631            }
632    
633          // Change key dimension value if key is in keyswitching area          // Change key dimension value if key is in keyswitching area
634          {          {
# Line 609  namespace LinuxSampler { namespace gig { Line 638  namespace LinuxSampler { namespace gig {
638                      (pInstrument->DimensionKeyRange.high - pInstrument->DimensionKeyRange.low + 1);                      (pInstrument->DimensionKeyRange.high - pInstrument->DimensionKeyRange.low + 1);
639          }          }
640    
         midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[key];  
   
641          pKey->KeyPressed = true; // the MIDI key was now pressed down          pKey->KeyPressed = true; // the MIDI key was now pressed down
642          pKey->Velocity   = itNoteOnEvent->Param.Note.Velocity;          pKey->Velocity   = itNoteOnEventOnKeyList->Param.Note.Velocity;
643          pKey->NoteOnTime = FrameTime + itNoteOnEvent->FragmentPos(); // will be used to calculate note length          pKey->NoteOnTime = FrameTime + itNoteOnEventOnKeyList->FragmentPos(); // will be used to calculate note length
644    
645          // cancel release process of voices on this key if needed          // cancel release process of voices on this key if needed
646          if (pKey->Active && !pEngineChannel->SustainPedal) {          if (pKey->Active && !pEngineChannel->SustainPedal) {
647              RTList<Event>::Iterator itCancelReleaseEvent = pKey->pEvents->allocAppend();              RTList<Event>::Iterator itCancelReleaseEvent = pKey->pEvents->allocAppend();
648              if (itCancelReleaseEvent) {              if (itCancelReleaseEvent) {
649                  *itCancelReleaseEvent = *itNoteOnEvent;                  // copy event                  *itCancelReleaseEvent = *itNoteOnEventOnKeyList;         // copy event
650                  itCancelReleaseEvent->Type = Event::type_cancel_release; // transform event type                  itCancelReleaseEvent->Type = Event::type_cancel_release; // transform event type
651              }              }
652              else dmsg(1,("Event pool emtpy!\n"));              else dmsg(1,("Event pool emtpy!\n"));
653          }          }
654    
         // move note on event to the key's own event list  
         RTList<Event>::Iterator itNoteOnEventOnKeyList = itNoteOnEvent.moveToEndOf(pKey->pEvents);  
   
655          // allocate and trigger new voice(s) for the key          // allocate and trigger new voice(s) for the key
656          {          {
657              // first, get total amount of required voices (dependant on amount of layers)              // first, get total amount of required voices (dependant on amount of layers)
# Line 644  namespace LinuxSampler { namespace gig { Line 668  namespace LinuxSampler { namespace gig {
668          if (!pKey->Active && !pKey->VoiceTheftsQueued)          if (!pKey->Active && !pKey->VoiceTheftsQueued)
669              pKey->pEvents->free(itNoteOnEventOnKeyList);              pKey->pEvents->free(itNoteOnEventOnKeyList);
670    
671            if (!pEngineChannel->SoloMode || pEngineChannel->PortamentoPos < 0.0f) pEngineChannel->PortamentoPos = (float) key;
672          pKey->RoundRobinIndex++;          pKey->RoundRobinIndex++;
673      }      }
674    
# Line 661  namespace LinuxSampler { namespace gig { Line 686  namespace LinuxSampler { namespace gig {
686          if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted          if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted
687          #endif          #endif
688    
689          midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[itNoteOffEvent->Param.Note.Key];          const int iKey = itNoteOffEvent->Param.Note.Key;
690            midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[iKey];
691          pKey->KeyPressed = false; // the MIDI key was now released          pKey->KeyPressed = false; // the MIDI key was now released
692    
693          // release voices on this key if needed          // move event to the key's own event list
694          if (pKey->Active && !pEngineChannel->SustainPedal) {          RTList<Event>::Iterator itNoteOffEventOnKeyList = itNoteOffEvent.moveToEndOf(pKey->pEvents);
695              itNoteOffEvent->Type = Event::type_release; // transform event type  
696            bool bShouldRelease = pKey->Active && ShouldReleaseVoice(pEngineChannel, itNoteOffEventOnKeyList->Param.Note.Key);
697    
698              // move event to the key's own event list          // in case Solo Mode is enabled, kill all voices on this key and respawn a voice on the highest pressed key (if any)
699              RTList<Event>::Iterator itNoteOffEventOnKeyList = itNoteOffEvent.moveToEndOf(pKey->pEvents);          if (pEngineChannel->SoloMode) { //TODO: this feels like too much code just for handling solo mode :P
700                bool bOtherKeysPressed = false;
701                if (iKey == pEngineChannel->SoloKey) {
702                    pEngineChannel->SoloKey = -1;
703                    // if there's still a key pressed down, respawn a voice (group) on the highest key
704                    for (int i = 127; i > 0; i--) {
705                        midi_key_info_t* pOtherKey = &pEngineChannel->pMIDIKeyInfo[i];
706                        if (pOtherKey->KeyPressed) {
707                            bOtherKeysPressed = true;
708                            // make the other key the new 'currently active solo key'
709                            pEngineChannel->SoloKey = i;
710                            // get final portamento position of currently active voice
711                            if (pEngineChannel->PortamentoMode) {
712                                RTList<Voice>::Iterator itVoice = pKey->pActiveVoices->first();
713                                if (itVoice) itVoice->UpdatePortamentoPos(itNoteOffEventOnKeyList);
714                            }
715                            // create a pseudo note on event
716                            RTList<Event>::Iterator itPseudoNoteOnEvent = pOtherKey->pEvents->allocAppend();
717                            if (itPseudoNoteOnEvent) {
718                                // copy event
719                                *itPseudoNoteOnEvent = *itNoteOffEventOnKeyList;
720                                // transform event to a note on event
721                                itPseudoNoteOnEvent->Type                = Event::type_note_on;
722                                itPseudoNoteOnEvent->Param.Note.Key      = i;
723                                itPseudoNoteOnEvent->Param.Note.Velocity = pOtherKey->Velocity;
724                                // allocate and trigger new voice(s) for the other key
725                                {
726                                    // first, get total amount of required voices (dependant on amount of layers)
727                                    ::gig::Region* pRegion = pEngineChannel->pInstrument->GetRegion(i);
728                                    if (pRegion) {
729                                        int voicesRequired = pRegion->Layers;
730                                        // now launch the required amount of voices
731                                        for (int iLayer = 0; iLayer < voicesRequired; iLayer++)
732                                            LaunchVoice(pEngineChannel, itPseudoNoteOnEvent, iLayer, false, true, false);
733                                    }
734                                }
735                                // if neither a voice was spawned or postponed then remove note on event from key again
736                                if (!pOtherKey->Active && !pOtherKey->VoiceTheftsQueued)
737                                    pOtherKey->pEvents->free(itPseudoNoteOnEvent);
738    
739                            } else dmsg(1,("Could not respawn voice, no free event left\n"));
740                            break; // done
741                        }
742                    }
743                }
744                if (bOtherKeysPressed) {
745                    if (pKey->Active) { // kill all voices on this key
746                        bShouldRelease = false; // no need to release, as we kill it here
747                        RTList<Voice>::Iterator itVoiceToBeKilled = pKey->pActiveVoices->first();
748                        RTList<Voice>::Iterator end               = pKey->pActiveVoices->end();
749                        for (; itVoiceToBeKilled != end; ++itVoiceToBeKilled) {
750                            if (itVoiceToBeKilled->Type != Voice::type_release_trigger)
751                                itVoiceToBeKilled->Kill(itNoteOffEventOnKeyList);
752                        }
753                    }
754                } else pEngineChannel->PortamentoPos = -1.0f;
755            }
756    
757            // if no solo mode (the usual case) or if solo mode and no other key pressed, then release voices on this key if needed
758            if (bShouldRelease) {
759                itNoteOffEventOnKeyList->Type = Event::type_release; // transform event type
760    
761              // spawn release triggered voice(s) if needed              // spawn release triggered voice(s) if needed
762              if (pKey->ReleaseTrigger) {              if (pKey->ReleaseTrigger) {
# Line 687  namespace LinuxSampler { namespace gig { Line 774  namespace LinuxSampler { namespace gig {
774                  }                  }
775                  pKey->ReleaseTrigger = false;                  pKey->ReleaseTrigger = false;
776              }              }
   
             // if neither a voice was spawned or postponed then remove note off event from key again  
             if (!pKey->Active && !pKey->VoiceTheftsQueued)  
                 pKey->pEvents->free(itNoteOffEventOnKeyList);  
777          }          }
778    
779            // if neither a voice was spawned or postponed on this key then remove note off event from key again
780            if (!pKey->Active && !pKey->VoiceTheftsQueued)
781                pKey->pEvents->free(itNoteOffEventOnKeyList);
782      }      }
783    
784      /**      /**
# Line 1141  namespace LinuxSampler { namespace gig { Line 1228  namespace LinuxSampler { namespace gig {
1228          pEngineChannel->ControllerTable[itControlChangeEvent->Param.CC.Controller] = itControlChangeEvent->Param.CC.Value;          pEngineChannel->ControllerTable[itControlChangeEvent->Param.CC.Controller] = itControlChangeEvent->Param.CC.Value;
1229    
1230          switch (itControlChangeEvent->Param.CC.Controller) {          switch (itControlChangeEvent->Param.CC.Controller) {
1231                case 5: { // portamento time
1232                    pEngineChannel->PortamentoTime = (float) itControlChangeEvent->Param.CC.Value / 127.0f * (float) CONFIG_PORTAMENTO_TIME_MAX + (float) CONFIG_PORTAMENTO_TIME_MIN;
1233                    break;
1234                }
1235              case 7: { // volume              case 7: { // volume
1236                  //TODO: not sample accurate yet                  //TODO: not sample accurate yet
1237                  pEngineChannel->GlobalVolume = (float) itControlChangeEvent->Param.CC.Value / 127.0f;                  pEngineChannel->GlobalVolume = VolumeCurve[itControlChangeEvent->Param.CC.Value] *  CONFIG_GLOBAL_ATTENUATION;
1238                  pEngineChannel->bStatusChanged = true; // engine channel status has changed, so set notify flag                  pEngineChannel->bStatusChanged = true; // engine channel status has changed, so set notify flag
1239                  break;                  break;
1240              }              }
1241              case 10: { // panpot              case 10: { // panpot
1242                  //TODO: not sample accurate yet                  //TODO: not sample accurate yet
1243                  const int pan = (int) itControlChangeEvent->Param.CC.Value - 64;                  pEngineChannel->GlobalPanLeft  = PanCurve[128 - itControlChangeEvent->Param.CC.Value];
1244                  pEngineChannel->GlobalPanLeft  = 1.0f - float(RTMath::Max(pan, 0)) /  63.0f;                  pEngineChannel->GlobalPanRight = PanCurve[itControlChangeEvent->Param.CC.Value];
                 pEngineChannel->GlobalPanRight = 1.0f - float(RTMath::Min(pan, 0)) / -64.0f;  
1245                  break;                  break;
1246              }              }
1247              case 64: { // sustain              case 64: { // sustain
1248                  if (itControlChangeEvent->Param.CC.Value >= 64 && !pEngineChannel->SustainPedal) {                  if (itControlChangeEvent->Param.CC.Value >= 64 && !pEngineChannel->SustainPedal) {
1249                      dmsg(4,("PEDAL DOWN\n"));                      dmsg(4,("DAMPER (RIGHT) PEDAL DOWN\n"));
1250                      pEngineChannel->SustainPedal = true;                      pEngineChannel->SustainPedal = true;
1251    
1252                      #if !CONFIG_PROCESS_MUTED_CHANNELS                      #if !CONFIG_PROCESS_MUTED_CHANNELS
# Line 1178  namespace LinuxSampler { namespace gig { Line 1268  namespace LinuxSampler { namespace gig {
1268                      }                      }
1269                  }                  }
1270                  if (itControlChangeEvent->Param.CC.Value < 64 && pEngineChannel->SustainPedal) {                  if (itControlChangeEvent->Param.CC.Value < 64 && pEngineChannel->SustainPedal) {
1271                      dmsg(4,("PEDAL UP\n"));                      dmsg(4,("DAMPER (RIGHT) PEDAL UP\n"));
1272                      pEngineChannel->SustainPedal = false;                      pEngineChannel->SustainPedal = false;
1273    
1274                      #if !CONFIG_PROCESS_MUTED_CHANNELS                      #if !CONFIG_PROCESS_MUTED_CHANNELS
# Line 1189  namespace LinuxSampler { namespace gig { Line 1279  namespace LinuxSampler { namespace gig {
1279                      RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();                      RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();
1280                      for (; iuiKey; ++iuiKey) {                      for (; iuiKey; ++iuiKey) {
1281                          midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey];                          midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey];
1282                          if (!pKey->KeyPressed) {                          if (!pKey->KeyPressed && ShouldReleaseVoice(pEngineChannel, *iuiKey)) {
1283                                RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend();
1284                                if (itNewEvent) {
1285                                    *itNewEvent = *itControlChangeEvent; // copy event to the key's own event list
1286                                    itNewEvent->Type = Event::type_release; // transform event type
1287                                }
1288                                else dmsg(1,("Event pool emtpy!\n"));
1289                            }
1290                        }
1291                    }
1292                    break;
1293                }
1294                case 65: { // portamento on / off
1295                    KillAllVoices(pEngineChannel, itControlChangeEvent);
1296                    pEngineChannel->PortamentoMode = itControlChangeEvent->Param.CC.Value >= 64;
1297                    break;
1298                }
1299                case 66: { // sostenuto
1300                    if (itControlChangeEvent->Param.CC.Value >= 64 && !pEngineChannel->SostenutoPedal) {
1301                        dmsg(4,("SOSTENUTO (CENTER) PEDAL DOWN\n"));
1302                        pEngineChannel->SostenutoPedal = true;
1303    
1304                        #if !CONFIG_PROCESS_MUTED_CHANNELS
1305                        if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted
1306                        #endif
1307    
1308                        SostenutoKeyCount = 0;
1309                        // Remeber the pressed keys
1310                        RTList<uint>::Iterator iuiKey = pEngineChannel->pActiveKeys->first();
1311                        for (; iuiKey; ++iuiKey) {
1312                            midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[*iuiKey];
1313                            if (pKey->KeyPressed && SostenutoKeyCount < 128) SostenutoKeys[SostenutoKeyCount++] = *iuiKey;
1314                        }
1315                    }
1316                    if (itControlChangeEvent->Param.CC.Value < 64 && pEngineChannel->SostenutoPedal) {
1317                        dmsg(4,("SOSTENUTO (CENTER) PEDAL UP\n"));
1318                        pEngineChannel->SostenutoPedal = false;
1319    
1320                        #if !CONFIG_PROCESS_MUTED_CHANNELS
1321                        if (pEngineChannel->GetMute()) return; // skip if sampler channel is muted
1322                        #endif
1323    
1324                        // release voices if the damper pedal is up and their respective key is not pressed
1325                        for (int i = 0; i < SostenutoKeyCount; i++) {
1326                            midi_key_info_t* pKey = &pEngineChannel->pMIDIKeyInfo[SostenutoKeys[i]];
1327                            if (!pKey->KeyPressed && !pEngineChannel->SustainPedal) {
1328                              RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend();                              RTList<Event>::Iterator itNewEvent = pKey->pEvents->allocAppend();
1329                              if (itNewEvent) {                              if (itNewEvent) {
1330                                  *itNewEvent = *itControlChangeEvent; // copy event to the key's own event list                                  *itNewEvent = *itControlChangeEvent; // copy event to the key's own event list
# Line 1217  namespace LinuxSampler { namespace gig { Line 1352  namespace LinuxSampler { namespace gig {
1352                  ReleaseAllVoices(pEngineChannel, itControlChangeEvent);                  ReleaseAllVoices(pEngineChannel, itControlChangeEvent);
1353                  break;                  break;
1354              }              }
1355                case 126: { // mono mode on
1356                    KillAllVoices(pEngineChannel, itControlChangeEvent);
1357                    pEngineChannel->SoloMode = true;
1358                    break;
1359                }
1360                case 127: { // poly mode on
1361                    KillAllVoices(pEngineChannel, itControlChangeEvent);
1362                    pEngineChannel->SoloMode = false;
1363                    break;
1364                }
1365          }          }
1366      }      }
1367    
# Line 1357  namespace LinuxSampler { namespace gig { Line 1502  namespace LinuxSampler { namespace gig {
1502          }          }
1503      }      }
1504    
1505        /**
1506         * Determines whether the specified voice should be released.
1507         *
1508         * @param pEngineChannel - The engine channel on which the voice should be checked
1509         * @param Key - The key number
1510         * @returns true if the specified should be released, false otherwise.
1511         */
1512        bool Engine::ShouldReleaseVoice(EngineChannel* pEngineChannel, int Key) {
1513            if (pEngineChannel->SustainPedal) return false;
1514    
1515            if (pEngineChannel->SostenutoPedal) {
1516                for (int i = 0; i < SostenutoKeyCount; i++)
1517                    if (Key == SostenutoKeys[i]) return false;
1518            }
1519    
1520            return true;
1521        }
1522    
1523      uint Engine::VoiceCount() {      uint Engine::VoiceCount() {
1524          return ActiveVoiceCount;          return ActiveVoiceCount;
1525      }      }
# Line 1394  namespace LinuxSampler { namespace gig { Line 1557  namespace LinuxSampler { namespace gig {
1557      }      }
1558    
1559      String Engine::Version() {      String Engine::Version() {
1560          String s = "$Revision: 1.54 $";          String s = "$Revision: 1.59 $";
1561          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
1562      }      }
1563    
1564        // static constant initializers
1565        const float* Engine::VolumeCurve(InitVolumeCurve());
1566        const float* Engine::PanCurve(InitPanCurve());
1567        const float* Engine::CrossfadeCurve(InitCrossfadeCurve());
1568    
1569        float* Engine::InitVolumeCurve() {
1570            // line-segment approximation
1571            const float segments[] = {
1572                0, 0, 2, 0.0046, 16, 0.016, 31, 0.051, 45, 0.115, 54.5, 0.2,
1573                64.5, 0.39, 74, 0.74, 92, 1.03, 114, 1.94, 119.2, 2.2, 127, 2.2
1574            };
1575            return InitCurve(segments);
1576        }
1577    
1578        float* Engine::InitPanCurve() {
1579            // line-segment approximation
1580            const float segments[] = {
1581                0, 0, 1, 0,
1582                2, 0.05, 31.5, 0.7, 51, 0.851, 74.5, 1.12,
1583                127, 1.41, 128, 1.41
1584            };
1585            return InitCurve(segments, 129);
1586        }
1587    
1588        float* Engine::InitCrossfadeCurve() {
1589            // line-segment approximation
1590            const float segments[] = {
1591                0, 0, 1, 0.03, 10, 0.1, 51, 0.58, 127, 1
1592            };
1593            return InitCurve(segments);
1594        }
1595    
1596        float* Engine::InitCurve(const float* segments, int size) {
1597            float* y = new float[size];
1598            for (int x = 0 ; x < size ; x++) {
1599                if (x > segments[2]) segments += 2;
1600                y[x] = segments[1] + (x - segments[0]) *
1601                    (segments[3] - segments[1]) / (segments[2] - segments[0]);
1602            }
1603            return y;
1604        }
1605    
1606  }} // namespace LinuxSampler::gig  }} // namespace LinuxSampler::gig

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

  ViewVC Help
Powered by ViewVC