/[svn]/linuxsampler/trunk/src/engines/sfz/Voice.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/engines/sfz/Voice.cpp

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

revision 2045 by persson, Sun Jan 10 13:22:19 2010 UTC revision 2218 by iliev, Thu Jul 28 08:05:57 2011 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-2008 Christian Schoenebeck                         *   *   Copyright (C) 2005 - 2008 Christian Schoenebeck                       *
7   *   Copyright (C) 2009-2010 Christian Schoenebeck and Grigor Iliev        *   *   Copyright (C) 2009 - 2011 Christian Schoenebeck and Grigor Iliev      *
8   *                                                                         *   *                                                                         *
9   *   This program is free software; you can redistribute it and/or modify  *   *   This program is free software; you can redistribute it and/or modify  *
10   *   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 27  Line 27 
27  #include "Engine.h"  #include "Engine.h"
28  #include "EngineChannel.h"  #include "EngineChannel.h"
29    
30    #define LN_10_DIV_20 0.115129254649702
31    
32  namespace LinuxSampler { namespace sfz {  namespace LinuxSampler { namespace sfz {
33    
34      Voice::Voice() {      typedef LinuxSampler::VoiceBase<EngineChannel, ::sfz::Region, Sample, DiskThread> SfzVoice;
35        Voice::Voice(): SignalRack(this), SfzVoice(&SignalRack) {
36          pEngine     = NULL;          pEngine     = NULL;
37      }      }
38    
# Line 67  namespace LinuxSampler { namespace sfz { Line 70  namespace LinuxSampler { namespace sfz {
70      Voice::RegionInfo Voice::GetRegionInfo() {      Voice::RegionInfo Voice::GetRegionInfo() {
71          RegionInfo ri;          RegionInfo ri;
72          ri.UnityNote = pRegion->pitch_keycenter;          ri.UnityNote = pRegion->pitch_keycenter;
73          ri.FineTune  = pRegion->tune;          ri.FineTune  = pRegion->tune + pRegion->transpose * 100;
74          ri.Pan       = int(pRegion->pan * 0.63); // convert from -100..100 to -64..63          ri.Pan       = int(pRegion->pan * 0.63); // convert from -100..100 to -64..63
75          ri.SampleStartOffset = 0; // TODO:          ri.SampleStartOffset = pRegion->offset ? *(pRegion->offset) : 0;
   
         ri.EG1PreAttack        = pRegion->ampeg_start * 10;  
         ri.EG1Attack           = pRegion->ampeg_attack;  
         ri.EG1Hold             = pRegion->ampeg_hold;  
         ri.EG1Decay1           = pRegion->ampeg_decay;  
         ri.EG1Decay2           = pRegion->ampeg_decay;  
         ri.EG1Sustain          = pRegion->ampeg_sustain;  
         ri.EG1InfiniteSustain  = true;  
         ri.EG1Release          = pRegion->ampeg_release;  
76    
77          ri.EG2PreAttack        = pRegion->fileg_start;          ri.EG2PreAttack        = pRegion->fileg_start * 10;
78          ri.EG2Attack           = pRegion->fileg_attack;          ri.EG2Attack           = pRegion->fileg_attack;
79          //ri.EG2Hold             = pRegion->fileg_hold; // TODO:          //ri.EG2Hold             = pRegion->fileg_hold; // TODO:
80          ri.EG2Decay1           = pRegion->fileg_decay;          ri.EG2Decay1           = pRegion->fileg_decay;
81          ri.EG2Decay2           = pRegion->fileg_decay;          ri.EG2Decay2           = pRegion->fileg_decay;
82          ri.EG2Sustain          = pRegion->fileg_sustain;          ri.EG2Sustain          = pRegion->fileg_sustain * 10;
83          ri.EG2InfiniteSustain  = true;          ri.EG2InfiniteSustain  = true;
84          ri.EG2Release          = pRegion->fileg_release;          ri.EG2Release          = pRegion->fileg_release;
85    
86          ri.EG3Attack     = pRegion->pitcheg_attack;          ri.EG3Attack     = pRegion->pitcheg_attack;
87          ri.EG3Depth      = 0; // TODO:          ri.EG3Depth      = 0; // TODO:
88          ri.VCFEnabled    = false; // TODO:          ri.VCFEnabled    = pRegion->cutoff;
89          ri.VCFType       = ::gig::vcf_type_lowpass; // TODO:          switch (pRegion->fil_type) {
90          ri.VCFResonance  = 0; // TODO:          case ::sfz::LPF_1P:
91                ri.VCFType = Filter::vcf_type_1p_lowpass;
92          ri.ReleaseTriggerDecay = 0;              break;
93            case ::sfz::LPF_2P:
94                ri.VCFType = Filter::vcf_type_2p_lowpass;
95                break;
96            case ::sfz::LPF_4P:
97                ri.VCFType = Filter::vcf_type_4p_lowpass;
98                break;
99            case ::sfz::LPF_6P:
100                ri.VCFType = Filter::vcf_type_6p_lowpass;
101                break;
102            case ::sfz::HPF_1P:
103                ri.VCFType = Filter::vcf_type_1p_highpass;
104                break;
105            case ::sfz::HPF_2P:
106                ri.VCFType = Filter::vcf_type_2p_highpass;
107                break;
108            case ::sfz::HPF_4P:
109                ri.VCFType = Filter::vcf_type_4p_highpass;
110                break;
111            case ::sfz::HPF_6P:
112                ri.VCFType = Filter::vcf_type_6p_highpass;
113                break;
114            case ::sfz::BPF_1P:
115            case ::sfz::BPF_2P:
116                ri.VCFType = Filter::vcf_type_2p_bandpass;
117                break;
118            case ::sfz::BRF_1P:
119            case ::sfz::BRF_2P:
120                ri.VCFType = Filter::vcf_type_2p_bandreject;
121                break;
122            case ::sfz::APF_1P:
123            case ::sfz::PKF_2P:
124            default:
125                ri.VCFEnabled = false;
126                break;
127            }
128    
129            ri.VCFResonance  = pRegion->resonance;
130    
131            // rt_decay is in dB. Precalculate a suitable value for exp in
132            // GetReleaseTriggerAttenuation: -ln(10) / 20 * rt_decay
133            ri.ReleaseTriggerDecay = -LN_10_DIV_20 * pRegion->rt_decay;
134    
135          return ri;          return ri;
136      }      }
# Line 109  namespace LinuxSampler { namespace sfz { Line 144  namespace LinuxSampler { namespace sfz {
144      }      }
145    
146      double Voice::GetSampleAttenuation() {      double Voice::GetSampleAttenuation() {
147          return 1.0; // TODO:          return exp(LN_10_DIV_20 * pRegion->volume);
148      }      }
149    
150      double Voice::GetVelocityAttenuation(uint8_t MIDIKeyVelocity) {      double Voice::GetVelocityAttenuation(uint8_t MIDIKeyVelocity) {
151          return double(MIDIKeyVelocity) / 127.0f; // TODO:          return pRegion->amp_velcurve[MIDIKeyVelocity];
152      }      }
153    
154      double Voice::GetVelocityRelease(uint8_t MIDIKeyVelocity) {      double Voice::GetVelocityRelease(uint8_t MIDIKeyVelocity) {
155          return 0.9; // TODO:          return 127.0 / MIDIKeyVelocity;
156      }      }
157    
158      void Voice::ProcessCCEvent(RTList<Event>::Iterator& itEvent) {      void Voice::ProcessCCEvent(RTList<Event>::Iterator& itEvent) {
# Line 130  namespace LinuxSampler { namespace sfz { Line 165  namespace LinuxSampler { namespace sfz {
165      }      }
166    
167      void Voice::ProcessCutoffEvent(RTList<Event>::Iterator& itEvent) {      void Voice::ProcessCutoffEvent(RTList<Event>::Iterator& itEvent) {
168          /*int ccvalue = itEvent->Param.CC.Value;          int ccvalue = itEvent->Param.CC.Value;
169          if (VCFCutoffCtrl.value == ccvalue) return;          if (VCFCutoffCtrl.value == ccvalue) return;
170          VCFCutoffCtrl.value == ccvalue;          VCFCutoffCtrl.value = ccvalue;
171          if (pRegion->VCFCutoffControllerInvert)  ccvalue = 127 - ccvalue;  
172          if (ccvalue < pRegion->VCFVelocityScale) ccvalue = pRegion->VCFVelocityScale;          float cutoff = CutoffBase * RTMath::CentsToFreqRatioUnlimited(
173          float cutoff = CutoffBase * float(ccvalue);              ccvalue / 127.0f * pRegion->cutoff_oncc[VCFCutoffCtrl.controller]);
174          if (cutoff > 127.0f) cutoff = 127.0f;          if (cutoff > 0.49 * pEngine->SampleRate) cutoff = 0.49 * pEngine->SampleRate;
175    
176          VCFCutoffCtrl.fvalue = cutoff; // needed for initialization of fFinalCutoff next time          VCFCutoffCtrl.fvalue = cutoff; // needed for initialization of fFinalCutoff next time
177          fFinalCutoff = cutoff;*/ // TODO: ^^^          fFinalCutoff = cutoff;
178      }      }
179    
180      double Voice::CalculateCrossfadeVolume(uint8_t MIDIKeyVelocity) {      double Voice::CalculateCrossfadeVolume(uint8_t MIDIKeyVelocity) {
# Line 202  namespace LinuxSampler { namespace sfz { Line 237  namespace LinuxSampler { namespace sfz {
237          return eg;          return eg;
238      }      }
239    
240        void Voice::TriggerEG1(const EGInfo& egInfo, double velrelease, double velocityAttenuation, uint sampleRate, uint8_t velocity) {
241    
242            // TODO: controller modulation
243    
244            // first check if there is a v2 EG for amplitude
245            for (int i = 0 ; i < pRegion->eg.size() ; i++) {
246                if (pRegion->eg[i].amplitude > 0) {
247                    // TODO: actually use the value of the amplitude parameter
248                    pEG1 = &EG1;
249                    EG1.trigger(pRegion->eg[i], sampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE, velocity);
250                    return;
251                }
252            }
253    
254            // otherwise use the v1 EGADSR
255            pEG1 = &EGADSR1;
256            EGADSR1.trigger(uint(pRegion->ampeg_start * 10),
257                            std::max(0.0, pRegion->ampeg_attack + pRegion->ampeg_vel2attack * velrelease),
258                            std::max(0.0, pRegion->ampeg_hold + pRegion->ampeg_vel2hold * velrelease),
259                            std::max(0.0, pRegion->ampeg_decay + pRegion->ampeg_vel2decay * velrelease),
260                            uint(std::min(std::max(0.0, 10 * (pRegion->ampeg_sustain + pRegion->ampeg_vel2sustain * velrelease)), 1000.0)),
261                            std::max(0.0, pRegion->ampeg_release + pRegion->ampeg_vel2release * velrelease),
262                            sampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
263        }
264    
265      double Voice::GetEG2ControllerValue(uint8_t MIDIKeyVelocity) {      double Voice::GetEG2ControllerValue(uint8_t MIDIKeyVelocity) {
266          /*double eg2controllervalue = 0;          /*double eg2controllervalue = 0;
267          switch (pRegion->EG2Controller.type) {          switch (pRegion->EG2Controller.type) {
# Line 238  namespace LinuxSampler { namespace sfz { Line 298  namespace LinuxSampler { namespace sfz {
298          return eg;          return eg;
299      }      }
300    
301        void Voice::TriggerEG2(const EGInfo& egInfo, double velrelease, double velocityAttenuation, uint sampleRate, uint8_t velocity) {
302    
303            // TODO: the sfz filter EG should modulate cents, not hertz,
304            // so we can't use the EG or EGADSR as it is. Disable for now.
305    
306            pEG2 = &EGADSR2;
307            EGADSR2.trigger(0,
308                            0,
309                            false,
310                            0,
311                            1000,
312                            0,
313                            sampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
314        }
315    
316      void Voice::InitLFO1() {      void Voice::InitLFO1() {
317          /*uint16_t lfo1_internal_depth;          /*uint16_t lfo1_internal_depth;
318          switch (pRegion->LFO1Controller) {          switch (pRegion->LFO1Controller) {
# Line 374  namespace LinuxSampler { namespace sfz { Line 449  namespace LinuxSampler { namespace sfz {
449      }      }
450    
451      float Voice::CalculateCutoffBase(uint8_t MIDIKeyVelocity) {      float Voice::CalculateCutoffBase(uint8_t MIDIKeyVelocity) {
452          /*float cutoff = pRegion->GetVelocityCutoff(MIDIKeyVelocity);          float cutoff = *pRegion->cutoff;
453          if (pRegion->VCFKeyboardTracking) {          cutoff *= RTMath::CentsToFreqRatioUnlimited(
454              cutoff *= exp((MIDIKeyVelocity - pRegion->VCFKeyboardTrackingBreakpoint) * 0.057762265f); // (ln(2) / 12)              MIDIKeyVelocity / 127.0f * pRegion->fil_veltrack +
455          }              (MIDIKey - pRegion->fil_keycenter) * pRegion->fil_keytrack);
456          return cutoff;*/ // TODO: ^^^          return cutoff;
         return 1.0f;  
457      }      }
458    
459      float Voice::CalculateFinalCutoff(float cutoffBase) {      float Voice::CalculateFinalCutoff(float cutoffBase) {
460          /*int cvalue;          float cutoff;
461          if (VCFCutoffCtrl.controller) {          if (VCFCutoffCtrl.controller) {
462              cvalue = GetSfzEngineChannel()->ControllerTable[VCFCutoffCtrl.controller];              int ccvalue = GetSfzEngineChannel()->ControllerTable[VCFCutoffCtrl.controller];
463              if (pRegion->VCFCutoffControllerInvert) cvalue = 127 - cvalue;              cutoff = CutoffBase * RTMath::CentsToFreqRatioUnlimited(
464              // VCFVelocityScale in this case means Minimum cutoff                  ccvalue / 127.0f * pRegion->cutoff_oncc[VCFCutoffCtrl.controller]);
465              if (cvalue < pRegion->VCFVelocityScale) cvalue = pRegion->VCFVelocityScale;          } else {
466          }              cutoff = cutoffBase;
         else {  
             cvalue = pRegion->VCFCutoff;  
467          }          }
468          float fco = cutoffBase * float(cvalue);          if (cutoff > 0.49 * pEngine->SampleRate) cutoff = 0.49 * pEngine->SampleRate;
469          if (fco > 127.0f) fco = 127.0f;          return cutoff;
   
         return fco;*/ // TODO: ^^^  
         return 127.0f;  
470      }      }
471    
472      uint8_t Voice::GetVCFCutoffCtrl() {      uint8_t Voice::GetVCFCutoffCtrl() {
473          /*uint8_t ctrl;          // TODO: the sfz format allows several CC for the same
474          switch (pRegion->VCFCutoffController) {          // modulation destination. The Voice interface needs to be
475              case ::gig::vcf_cutoff_ctrl_modwheel:          // changed to support that.
476                  ctrl = 1;          if (pRegion->cutoff_cc) return pRegion->cutoff_cc;
477                  break;          else if (pRegion->cutoff_chanaft) return 128;
             case ::gig::vcf_cutoff_ctrl_effect1:  
                 ctrl = 12;  
                 break;  
             case ::gig::vcf_cutoff_ctrl_effect2:  
                 ctrl = 13;  
                 break;  
             case ::gig::vcf_cutoff_ctrl_breath:  
                 ctrl = 2;  
                 break;  
             case ::gig::vcf_cutoff_ctrl_foot:  
                 ctrl = 4;  
                 break;  
             case ::gig::vcf_cutoff_ctrl_sustainpedal:  
                 ctrl = 64;  
                 break;  
             case ::gig::vcf_cutoff_ctrl_softpedal:  
                 ctrl = 67;  
                 break;  
             case ::gig::vcf_cutoff_ctrl_genpurpose7:  
                 ctrl = 82;  
                 break;  
             case ::gig::vcf_cutoff_ctrl_genpurpose8:  
                 ctrl = 83;  
                 break;  
             case ::gig::vcf_cutoff_ctrl_aftertouch:  
                 ctrl = 128;  
                 break;  
             case ::gig::vcf_cutoff_ctrl_none:  
             default:  
                 ctrl = 0;  
                 break;  
         }  
   
         return ctrl;*/ // TODO: ^^^  
478          return 0;          return 0;
479      }      }
480    
# Line 467  namespace LinuxSampler { namespace sfz { Line 502  namespace LinuxSampler { namespace sfz {
502          return 0;          return 0;
503      }      }
504    
505        float Voice::GetReleaseTriggerAttenuation(float noteLength) {
506            // pow(10, -rt_decay * noteLength / 20):
507            return expf(RgnInfo.ReleaseTriggerDecay * noteLength);
508        }
509    
510        void Voice::ProcessGroupEvent(RTList<Event>::Iterator& itEvent) {
511            dmsg(4,("Voice %x processGroupEvents event type=%d", this, itEvent->Type));
512            if (itEvent->Type == Event::type_control_change ||
513                (Type & Voice::type_controller_triggered) ||
514                itEvent->Param.Note.Key != MIDIKey) {
515                dmsg(4,("Voice %x - kill", this));
516                if (pRegion->off_mode == ::sfz::OFF_NORMAL) {
517                    // turn off the voice by entering release envelope stage
518                    EnterReleaseStage();
519                } else {
520                    // kill the voice fast
521                    SignalRack.EnterFadeOutStage();
522                }
523            }
524        }
525        
526        void Voice::SetSampleStartOffset() {
527            if (DiskVoice && RgnInfo.SampleStartOffset > pSample->MaxOffset) {
528                // The offset is applied to the RAM buffer
529                finalSynthesisParameters.dPos = 0;
530                Pos = 0;
531            } else {
532                finalSynthesisParameters.dPos = RgnInfo.SampleStartOffset; // offset where we should start playback of sample
533                Pos = RgnInfo.SampleStartOffset;
534            }
535        }
536    
537  }} // namespace LinuxSampler::sfz  }} // namespace LinuxSampler::sfz

Legend:
Removed from v.2045  
changed lines
  Added in v.2218

  ViewVC Help
Powered by ViewVC