/[svn]/linuxsampler/trunk/src/engines/gig/EGADSR.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/engines/gig/EGADSR.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 237 by senkov, Sun Sep 12 01:59:40 2004 UTC revision 286 by schoenebeck, Sat Oct 16 17:29:05 2004 UTC
# Line 24  Line 24 
24    
25  namespace LinuxSampler { namespace gig {  namespace LinuxSampler { namespace gig {
26    
27        const float EGADSR::FadeOutCoeff(CalculateFadeOutCoeff());
28    
29        float EGADSR::CalculateFadeOutCoeff() {
30            const float sampleRate = 44100.0; // even if the sample rate will be 192kHz it won't hurt at all
31            const float killSteps  = EG_MIN_RELEASE_TIME * sampleRate;
32            return -1.0f / killSteps;
33        }
34    
35      EGADSR::EGADSR(gig::Engine* pEngine, Event::destination_t ModulationDestination) {      EGADSR::EGADSR(gig::Engine* pEngine, Event::destination_t ModulationDestination) {
36          this->pEngine = pEngine;          this->pEngine = pEngine;
37          this->ModulationDestination = ModulationDestination;          this->ModulationDestination = ModulationDestination;
# Line 35  namespace LinuxSampler { namespace gig { Line 43  namespace LinuxSampler { namespace gig {
43       * Will be called by the voice for every audio fragment to let the EG       * Will be called by the voice for every audio fragment to let the EG
44       * queue it's modulation changes for the current audio fragment.       * queue it's modulation changes for the current audio fragment.
45       *       *
46       * @param Samples       - total number of sample points to be rendered in this       * @param TotalSamples  - total number of sample points to be rendered in this
47       *                        audio fragment cycle by the audio engine       *                        audio fragment cycle by the audio engine
48       * @param pEvents       - event list with "release" and "cancel release" events       * @param pEvents       - event list with "release" and "cancel release" events
49       * @param pTriggerEvent - event that caused triggering of the voice (only if       * @param itTriggerEvent - event that caused triggering of the voice (only if
50       *                        the voices was triggered in the current audio       *                        the voice was triggered in the current audio
51       *                        fragment, NULL otherwise)       *                        fragment, NULL otherwise)
52       * @param SamplePos     - current playback position       * @param SamplePos     - current playback position
53       * @param CurrentPitch  - current pitch value for playback       * @param CurrentPitch  - current pitch value for playback
54         * @param itKillEvent   - (optional) event which caused this voice to be killed
55       */       */
56      void EGADSR::Process(uint Samples, RTEList<Event>* pEvents, Event* pTriggerEvent, double SamplePos, double CurrentPitch) {      void EGADSR::Process(uint TotalSamples, RTList<Event>* pEvents, RTList<Event>::Iterator itTriggerEvent, double SamplePos, double CurrentPitch, RTList<Event>::Iterator itKillEvent) {
57          Event* pTransitionEvent;          // skip all events which occured before this voice was triggered
58          if (pTriggerEvent) {          RTList<Event>::Iterator itTransitionEvent = (itTriggerEvent) ? ++itTriggerEvent : pEvents->first();
59              pEvents->set_current(pTriggerEvent);  
60              pTransitionEvent = pEvents->next();          // if the voice was killed in this fragment we only process the time before this kill event, then switch to 'stage_fadeout'
61          }          int Samples = (itKillEvent) ? RTMath::Min(itKillEvent->FragmentPos(), pEngine->MaxFadeOutPos) : (int) TotalSamples;
         else {  
             pTransitionEvent = pEvents->first();  
         }  
62    
63          int iSample = TriggerDelay;          int iSample = TriggerDelay;
64          while (iSample < Samples) {  
65            #if DEVMODE
66            if (TriggerDelay > TotalSamples) { // FIXME: should be removed before the final release (purpose: just a sanity check for debugging)
67                dmsg(1,("EGADSR: ERROR, TriggerDelay > Totalsamples\n"));
68                int* i = NULL;
69                (*i)++; // force a segfault
70            }
71            #endif // DEVMODE
72    
73            while (iSample < TotalSamples) {
74    
75                // if the voice was killed in this fragment and we already processed the time before this kill event
76                if (itKillEvent && iSample >= Samples) Stage = stage_fadeout;
77    
78              switch (Stage) {              switch (Stage) {
79                  case stage_attack: {                  case stage_attack: {
80                      TriggerDelay = 0;                      TriggerDelay = 0;
# Line 66  namespace LinuxSampler { namespace gig { Line 85  namespace LinuxSampler { namespace gig {
85                          Level += AttackCoeff;                          Level += AttackCoeff;
86                          pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;                          pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
87                      }                      }
88                      if (iSample == Samples) { // postpone last transition event for the next audio fragment                      if (iSample == TotalSamples) { // postpone last transition event for the next audio fragment
89                          Event* pLastEvent = pEvents->last();                          RTList<Event>::Iterator itLastEvent = pEvents->last();
90                          if (pLastEvent) ReleasePostponed = (pLastEvent->Type == Event::type_release);                          if (itLastEvent) ReleasePostponed = (itLastEvent->Type == Event::type_release);
91                      }                      }
92                      if (!AttackStepsLeft) Stage = (ReleasePostponed) ? stage_release : (HoldAttack) ? stage_attack_hold : stage_decay1;                      if (!AttackStepsLeft) Stage = (ReleasePostponed) ? stage_release : (HoldAttack) ? stage_attack_hold : stage_decay1;
93                      break;                      break;
# Line 81  namespace LinuxSampler { namespace gig { Line 100  namespace LinuxSampler { namespace gig {
100                      int holdstepsleft = (int) (LoopStart - SamplePos / CurrentPitch); // FIXME: just an approximation, inaccuracy grows with higher audio fragment size, sufficient for usual fragment sizes though                      int holdstepsleft = (int) (LoopStart - SamplePos / CurrentPitch); // FIXME: just an approximation, inaccuracy grows with higher audio fragment size, sufficient for usual fragment sizes though
101                      int to_process    = RTMath::Min(holdstepsleft, Samples - iSample);                      int to_process    = RTMath::Min(holdstepsleft, Samples - iSample);
102                      int process_end   = iSample + to_process;                      int process_end   = iSample + to_process;
103                      if (pTransitionEvent && pTransitionEvent->FragmentPos() <= process_end) {                      if (itTransitionEvent && itTransitionEvent->FragmentPos() <= process_end) {
104                          process_end      = pTransitionEvent->FragmentPos();                          process_end       = itTransitionEvent->FragmentPos();
105                          Stage            = (pTransitionEvent->Type == Event::type_release) ? stage_release : (InfiniteSustain) ? stage_sustain : stage_decay2;                          Stage             = (itTransitionEvent->Type == Event::type_release) ? stage_release : (InfiniteSustain) ? stage_sustain : stage_decay2;
106                          pTransitionEvent = pEvents->next();                          ++itTransitionEvent;
107                      }                      }
108                      else if (to_process == holdstepsleft) Stage = stage_decay1;                      else if (to_process == holdstepsleft) Stage = stage_decay1;
109                      while (iSample < process_end) {                      while (iSample < process_end) {
# Line 95  namespace LinuxSampler { namespace gig { Line 114  namespace LinuxSampler { namespace gig {
114                  case stage_decay1: {                  case stage_decay1: {
115                      int to_process   = RTMath::Min(Samples - iSample, Decay1StepsLeft);                      int to_process   = RTMath::Min(Samples - iSample, Decay1StepsLeft);
116                      int process_end  = iSample + to_process;                      int process_end  = iSample + to_process;
117                      if (pTransitionEvent && pTransitionEvent->FragmentPos() <= process_end) {                      if (itTransitionEvent && itTransitionEvent->FragmentPos() <= process_end) {
118                          process_end      = pTransitionEvent->FragmentPos();                          process_end       = itTransitionEvent->FragmentPos();
119                          Stage            = (pTransitionEvent->Type == Event::type_release) ? stage_release : (InfiniteSustain) ? stage_sustain : stage_decay2;                          Stage             = (itTransitionEvent->Type == Event::type_release) ? stage_release : (InfiniteSustain) ? stage_sustain : stage_decay2;
120                          pTransitionEvent = pEvents->next();                          ++itTransitionEvent;
121                      }                      }
122                      else {                      else {
123                          Decay1StepsLeft -= to_process;                          Decay1StepsLeft -= to_process;
# Line 112  namespace LinuxSampler { namespace gig { Line 131  namespace LinuxSampler { namespace gig {
131                  }                  }
132                  case stage_decay2: {                  case stage_decay2: {
133                      int process_end;                      int process_end;
134                      if (pTransitionEvent && pTransitionEvent->Type == Event::type_release && pTransitionEvent->FragmentPos() <= Samples) {                      if (itTransitionEvent && itTransitionEvent->Type == Event::type_release && itTransitionEvent->FragmentPos() <= Samples) {
135                          process_end      = pTransitionEvent->FragmentPos();                          process_end       = itTransitionEvent->FragmentPos();
136                          pTransitionEvent = pEvents->next();                          ++itTransitionEvent;
137                          Stage            = stage_release; // switch to release stage soon                          Stage             = stage_release; // switch to release stage soon
138                      }                      }
139                      else process_end = Samples;                      else process_end = Samples;
140                      while (iSample < process_end) {                      while (iSample < process_end) {
141                          Level += Level * Decay2Coeff;                          Level += Level * Decay2Coeff;
142                          pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;                          pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
143                      }                      }
144                      if (Level <= EG_ENVELOPE_LIMIT) Stage = stage_end;                      if (Level <= EG_ENVELOPE_LIMIT) Stage = stage_fadeout;
145                      break;                      break;
146                  }                  }
147                  case stage_sustain: {                  case stage_sustain: {
148                      int process_end;                      int process_end;
149                      if (pTransitionEvent && pTransitionEvent->Type == Event::type_release && pTransitionEvent->FragmentPos() <= Samples) {                      if (itTransitionEvent && itTransitionEvent->Type == Event::type_release && itTransitionEvent->FragmentPos() <= Samples) {
150                          process_end      = pTransitionEvent->FragmentPos();                          process_end       = itTransitionEvent->FragmentPos();
151                          pTransitionEvent = pEvents->next();                          ++itTransitionEvent;
152                          Stage            = stage_release; // switch to release stage soon                          Stage             = stage_release; // switch to release stage soon
153                      }                      }
154                      else process_end = Samples;                      else process_end = Samples;
155                      while (iSample < process_end) {                      while (iSample < process_end) {
# Line 140  namespace LinuxSampler { namespace gig { Line 159  namespace LinuxSampler { namespace gig {
159                  }                  }
160                  case stage_release: {                  case stage_release: {
161                      int process_end;                      int process_end;
162                      if (pTransitionEvent && pTransitionEvent->Type == Event::type_cancel_release && pTransitionEvent->FragmentPos() <= Samples) {                      if (itTransitionEvent && itTransitionEvent->Type == Event::type_cancel_release && itTransitionEvent->FragmentPos() <= Samples) {
163                          process_end      = pTransitionEvent->FragmentPos();                          process_end       = itTransitionEvent->FragmentPos();
164                          pTransitionEvent = pEvents->next();                          ++itTransitionEvent;
165                          Stage            = (InfiniteSustain) ? stage_sustain : stage_decay2; // switch back to sustain / decay2 stage soon                          Stage             = (InfiniteSustain) ? stage_sustain : stage_decay2; // switch back to sustain / decay2 stage soon
166                      }                      }
167                      else process_end = Samples;                      else process_end = Samples;
168                      while (iSample < process_end) {                      while (iSample < process_end) {
169                          Level += Level * ReleaseCoeff;                          Level += Level * ReleaseCoeff;
170                          pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;                          pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
171                      }                      }
172                      if (Level <= EG_ENVELOPE_LIMIT) Stage = stage_end;                      if (Level <= EG_ENVELOPE_LIMIT) Stage = stage_fadeout;
173                      break;                      break;
174                  }                  }
175                  case stage_end: {                  case stage_fadeout: {
176                      while (iSample < Samples) {                      int to_process   = RTMath::Min(int(Level / (-FadeOutCoeff)), TotalSamples - iSample);
177                          Level += Level * ReleaseCoeff;                      int process_end  = iSample + to_process;
178                        while (iSample < process_end) {
179                            Level += FadeOutCoeff;
180                          pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;                          pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
181                      }                      }
182                        Stage = stage_end;
183                        if (Level > -FadeOutCoeff) dmsg(1,("EGADSR: Warning, final fade out level too high, may result in click sound!\n"));
184                    } //Fall through here instead of breaking otherwise we can get back into stage_fadeout and loop forever!
185                    case stage_end: {
186                        while (iSample < TotalSamples) {
187                            pEngine->pSynthesisParameters[ModulationDestination][iSample++] = 0.0f;
188                        }
189                      break;                      break;
190                  }                  }
191              }              }
192          }          }
193    
194            #if DEVMODE
195            if (itKillEvent && Stage != stage_end) { // FIXME: should be removed before the final release (purpose: just a sanity check for debugging)
196                dmsg(1,("EGADSR: ERROR, voice killing not completed !!!\n"));
197                dmsg(1,("EGADSR: Stage=%d,iSample=%d,Samples=%d, TotalSamples=%d, MaxFadoutPos=%d\n",Stage,iSample,Samples,TotalSamples,pEngine->MaxFadeOutPos));
198            }
199            #endif // DEVMODE
200      }      }
201    
202      /**      /**
# Line 208  namespace LinuxSampler { namespace gig { Line 243  namespace LinuxSampler { namespace gig {
243              if (Decay2Time < EG_MIN_RELEASE_TIME) Decay2Time = EG_MIN_RELEASE_TIME;              if (Decay2Time < EG_MIN_RELEASE_TIME) Decay2Time = EG_MIN_RELEASE_TIME;
244              long Decay2Steps = (long) (Decay2Time * pEngine->pAudioOutputDevice->SampleRate());              long Decay2Steps = (long) (Decay2Time * pEngine->pAudioOutputDevice->SampleRate());
245              Decay2Coeff      = (Decay2Steps) ? exp((log(EG_ENVELOPE_LIMIT) - log(this->SustainLevel)) / Decay2Steps + log(this->SustainLevel)) - this->SustainLevel              Decay2Coeff      = (Decay2Steps) ? exp((log(EG_ENVELOPE_LIMIT) - log(this->SustainLevel)) / Decay2Steps + log(this->SustainLevel)) - this->SustainLevel
246                                              : 0.0;                                               : 0.0;
247          }          }
248    
249          // calcuate release stage parameters (exp. curve)          // calculate release stage parameters (exp. curve)
250          if (ReleaseTime < EG_MIN_RELEASE_TIME) ReleaseTime = EG_MIN_RELEASE_TIME;  // to avoid click sounds at the end of the sample playback          if (ReleaseTime < EG_MIN_RELEASE_TIME) ReleaseTime = EG_MIN_RELEASE_TIME;  // to avoid click sounds at the end of the sample playback
251          ReleaseStepsLeft = (long) (ReleaseTime * pEngine->pAudioOutputDevice->SampleRate());          ReleaseStepsLeft = (long) (ReleaseTime * pEngine->pAudioOutputDevice->SampleRate());
252          ReleaseCoeff     = exp((log(EG_ENVELOPE_LIMIT) - log(this->SustainLevel)) / ReleaseStepsLeft + log(this->SustainLevel)) - this->SustainLevel;          ReleaseCoeff     = exp((log(EG_ENVELOPE_LIMIT) - log(this->SustainLevel)) / ReleaseStepsLeft + log(this->SustainLevel)) - this->SustainLevel;

Legend:
Removed from v.237  
changed lines
  Added in v.286

  ViewVC Help
Powered by ViewVC