/[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 285 by schoenebeck, Thu Oct 14 21:31:26 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) {          while (iSample < TotalSamples) {
65    
66                // if the voice was killed in this fragment and we already processed the time before this kill event
67                if (itKillEvent && iSample >= Samples) Stage = stage_fadeout;
68    
69              switch (Stage) {              switch (Stage) {
70                  case stage_attack: {                  case stage_attack: {
71                      TriggerDelay = 0;                      TriggerDelay = 0;
# Line 66  namespace LinuxSampler { namespace gig { Line 76  namespace LinuxSampler { namespace gig {
76                          Level += AttackCoeff;                          Level += AttackCoeff;
77                          pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;                          pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
78                      }                      }
79                      if (iSample == Samples) { // postpone last transition event for the next audio fragment                      if (iSample == TotalSamples) { // postpone last transition event for the next audio fragment
80                          Event* pLastEvent = pEvents->last();                          RTList<Event>::Iterator itLastEvent = pEvents->last();
81                          if (pLastEvent) ReleasePostponed = (pLastEvent->Type == Event::type_release);                          if (itLastEvent) ReleasePostponed = (itLastEvent->Type == Event::type_release);
82                      }                      }
83                      if (!AttackStepsLeft) Stage = (ReleasePostponed) ? stage_release : (HoldAttack) ? stage_attack_hold : stage_decay1;                      if (!AttackStepsLeft) Stage = (ReleasePostponed) ? stage_release : (HoldAttack) ? stage_attack_hold : stage_decay1;
84                      break;                      break;
# Line 81  namespace LinuxSampler { namespace gig { Line 91  namespace LinuxSampler { namespace gig {
91                      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
92                      int to_process    = RTMath::Min(holdstepsleft, Samples - iSample);                      int to_process    = RTMath::Min(holdstepsleft, Samples - iSample);
93                      int process_end   = iSample + to_process;                      int process_end   = iSample + to_process;
94                      if (pTransitionEvent && pTransitionEvent->FragmentPos() <= process_end) {                      if (itTransitionEvent && itTransitionEvent->FragmentPos() <= process_end) {
95                          process_end      = pTransitionEvent->FragmentPos();                          process_end       = itTransitionEvent->FragmentPos();
96                          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;
97                          pTransitionEvent = pEvents->next();                          ++itTransitionEvent;
98                      }                      }
99                      else if (to_process == holdstepsleft) Stage = stage_decay1;                      else if (to_process == holdstepsleft) Stage = stage_decay1;
100                      while (iSample < process_end) {                      while (iSample < process_end) {
# Line 95  namespace LinuxSampler { namespace gig { Line 105  namespace LinuxSampler { namespace gig {
105                  case stage_decay1: {                  case stage_decay1: {
106                      int to_process   = RTMath::Min(Samples - iSample, Decay1StepsLeft);                      int to_process   = RTMath::Min(Samples - iSample, Decay1StepsLeft);
107                      int process_end  = iSample + to_process;                      int process_end  = iSample + to_process;
108                      if (pTransitionEvent && pTransitionEvent->FragmentPos() <= process_end) {                      if (itTransitionEvent && itTransitionEvent->FragmentPos() <= process_end) {
109                          process_end      = pTransitionEvent->FragmentPos();                          process_end       = itTransitionEvent->FragmentPos();
110                          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;
111                          pTransitionEvent = pEvents->next();                          ++itTransitionEvent;
112                      }                      }
113                      else {                      else {
114                          Decay1StepsLeft -= to_process;                          Decay1StepsLeft -= to_process;
# Line 112  namespace LinuxSampler { namespace gig { Line 122  namespace LinuxSampler { namespace gig {
122                  }                  }
123                  case stage_decay2: {                  case stage_decay2: {
124                      int process_end;                      int process_end;
125                      if (pTransitionEvent && pTransitionEvent->Type == Event::type_release && pTransitionEvent->FragmentPos() <= Samples) {                      if (itTransitionEvent && itTransitionEvent->Type == Event::type_release && itTransitionEvent->FragmentPos() <= Samples) {
126                          process_end      = pTransitionEvent->FragmentPos();                          process_end       = itTransitionEvent->FragmentPos();
127                          pTransitionEvent = pEvents->next();                          ++itTransitionEvent;
128                          Stage            = stage_release; // switch to release stage soon                          Stage             = stage_release; // switch to release stage soon
129                      }                      }
130                      else process_end = Samples;                      else process_end = Samples;
131                      while (iSample < process_end) {                      while (iSample < process_end) {
132                          Level += Level * Decay2Coeff;                          Level += Level * Decay2Coeff;
133                          pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;                          pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
134                      }                      }
135                      if (Level <= EG_ENVELOPE_LIMIT) Stage = stage_end;                      if (Level <= EG_ENVELOPE_LIMIT) Stage = stage_fadeout;
136                      break;                      break;
137                  }                  }
138                  case stage_sustain: {                  case stage_sustain: {
139                      int process_end;                      int process_end;
140                      if (pTransitionEvent && pTransitionEvent->Type == Event::type_release && pTransitionEvent->FragmentPos() <= Samples) {                      if (itTransitionEvent && itTransitionEvent->Type == Event::type_release && itTransitionEvent->FragmentPos() <= Samples) {
141                          process_end      = pTransitionEvent->FragmentPos();                          process_end       = itTransitionEvent->FragmentPos();
142                          pTransitionEvent = pEvents->next();                          ++itTransitionEvent;
143                          Stage            = stage_release; // switch to release stage soon                          Stage             = stage_release; // switch to release stage soon
144                      }                      }
145                      else process_end = Samples;                      else process_end = Samples;
146                      while (iSample < process_end) {                      while (iSample < process_end) {
# Line 140  namespace LinuxSampler { namespace gig { Line 150  namespace LinuxSampler { namespace gig {
150                  }                  }
151                  case stage_release: {                  case stage_release: {
152                      int process_end;                      int process_end;
153                      if (pTransitionEvent && pTransitionEvent->Type == Event::type_cancel_release && pTransitionEvent->FragmentPos() <= Samples) {                      if (itTransitionEvent && itTransitionEvent->Type == Event::type_cancel_release && itTransitionEvent->FragmentPos() <= Samples) {
154                          process_end      = pTransitionEvent->FragmentPos();                          process_end       = itTransitionEvent->FragmentPos();
155                          pTransitionEvent = pEvents->next();                          ++itTransitionEvent;
156                          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
157                      }                      }
158                      else process_end = Samples;                      else process_end = Samples;
159                      while (iSample < process_end) {                      while (iSample < process_end) {
160                          Level += Level * ReleaseCoeff;                          Level += Level * ReleaseCoeff;
161                          pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;                          pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
162                      }                      }
163                      if (Level <= EG_ENVELOPE_LIMIT) Stage = stage_end;                      if (Level <= EG_ENVELOPE_LIMIT) Stage = stage_fadeout;
164                      break;                      break;
165                  }                  }
166                  case stage_end: {                  case stage_fadeout: {
167                      while (iSample < Samples) {                      int to_process   = RTMath::Min(int(Level / (-FadeOutCoeff)), TotalSamples - iSample);
168                          Level += Level * ReleaseCoeff;                      int process_end  = iSample + to_process;
169                        while (iSample < process_end) {
170                            Level += FadeOutCoeff;
171                          pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;                          pEngine->pSynthesisParameters[ModulationDestination][iSample++] *= Level;
172                      }                      }
173                        Stage = stage_end;
174                        if (Level > -FadeOutCoeff) dmsg(1,("EGADSR: Warning, final fade out level too high, may result in click sound!\n"));
175                    } //Fall through here instead of breaking otherwise we can get back into stage_fadeout and loop forever!
176                    case stage_end: {
177                        while (iSample < TotalSamples) {
178                            pEngine->pSynthesisParameters[ModulationDestination][iSample++] = 0.0f;
179                        }
180                      break;                      break;
181                  }                  }
182              }              }
183          }          }
184    
185            if (itKillEvent && Stage != stage_end) {
186                dmsg(1,("EGADSR: VOICE KILLING NOT COMPLETED !!!\n"));
187                dmsg(1,("EGADSR: Stage=%d,iSample=%d,Samples=%d, TotalSamples=%d, MaxFadoutPos=%d\n",Stage,iSample,Samples,TotalSamples,pEngine->MaxFadeOutPos));
188            }
189      }      }
190    
191      /**      /**
# Line 208  namespace LinuxSampler { namespace gig { Line 232  namespace LinuxSampler { namespace gig {
232              if (Decay2Time < EG_MIN_RELEASE_TIME) Decay2Time = EG_MIN_RELEASE_TIME;              if (Decay2Time < EG_MIN_RELEASE_TIME) Decay2Time = EG_MIN_RELEASE_TIME;
233              long Decay2Steps = (long) (Decay2Time * pEngine->pAudioOutputDevice->SampleRate());              long Decay2Steps = (long) (Decay2Time * pEngine->pAudioOutputDevice->SampleRate());
234              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
235                                              : 0.0;                                               : 0.0;
236          }          }
237    
238          // calcuate release stage parameters (exp. curve)          // calculate release stage parameters (exp. curve)
239          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
240          ReleaseStepsLeft = (long) (ReleaseTime * pEngine->pAudioOutputDevice->SampleRate());          ReleaseStepsLeft = (long) (ReleaseTime * pEngine->pAudioOutputDevice->SampleRate());
241          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.285

  ViewVC Help
Powered by ViewVC