/[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 2086 by persson, Sun Apr 25 12:51:30 2010 UTC revision 2203 by persson, Sat Jul 9 16:44:27 2011 UTC
# Line 4  Line 4 
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 73  namespace LinuxSampler { namespace sfz { Line 73  namespace LinuxSampler { namespace sfz {
73          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
74          ri.SampleStartOffset = 0; // TODO:          ri.SampleStartOffset = 0; // TODO:
75    
         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 * 10;  
         ri.EG1InfiniteSustain  = true;  
         ri.EG1Release          = pRegion->ampeg_release;  
   
76          ri.EG2PreAttack        = pRegion->fileg_start * 10;          ri.EG2PreAttack        = pRegion->fileg_start * 10;
77          ri.EG2Attack           = pRegion->fileg_attack;          ri.EG2Attack           = pRegion->fileg_attack;
78          //ri.EG2Hold             = pRegion->fileg_hold; // TODO:          //ri.EG2Hold             = pRegion->fileg_hold; // TODO:
# Line 93  namespace LinuxSampler { namespace sfz { Line 84  namespace LinuxSampler { namespace sfz {
84    
85          ri.EG3Attack     = pRegion->pitcheg_attack;          ri.EG3Attack     = pRegion->pitcheg_attack;
86          ri.EG3Depth      = 0; // TODO:          ri.EG3Depth      = 0; // TODO:
87          ri.VCFEnabled    = false; // TODO:          ri.VCFEnabled    = pRegion->cutoff;
88          ri.VCFType       = ::gig::vcf_type_lowpass; // TODO:          switch (pRegion->fil_type) {
89          ri.VCFResonance  = 0; // TODO:          case ::sfz::LPF_1P:
90                ri.VCFType = Filter::vcf_type_1p_lowpass;
91                break;
92            case ::sfz::LPF_2P:
93                ri.VCFType = Filter::vcf_type_2p_lowpass;
94                break;
95            case ::sfz::LPF_4P:
96                ri.VCFType = Filter::vcf_type_4p_lowpass;
97                break;
98            case ::sfz::LPF_6P:
99                ri.VCFType = Filter::vcf_type_6p_lowpass;
100                break;
101            case ::sfz::HPF_1P:
102                ri.VCFType = Filter::vcf_type_1p_highpass;
103                break;
104            case ::sfz::HPF_2P:
105                ri.VCFType = Filter::vcf_type_2p_highpass;
106                break;
107            case ::sfz::HPF_4P:
108                ri.VCFType = Filter::vcf_type_4p_highpass;
109                break;
110            case ::sfz::HPF_6P:
111                ri.VCFType = Filter::vcf_type_6p_highpass;
112                break;
113            case ::sfz::BPF_1P:
114            case ::sfz::BPF_2P:
115                ri.VCFType = Filter::vcf_type_2p_bandpass;
116                break;
117            case ::sfz::BRF_1P:
118            case ::sfz::BRF_2P:
119                ri.VCFType = Filter::vcf_type_2p_bandreject;
120                break;
121            case ::sfz::APF_1P:
122            case ::sfz::PKF_2P:
123            default:
124                ri.VCFEnabled = false;
125                break;
126            }
127    
128            ri.VCFResonance  = pRegion->resonance;
129    
130          // rt_decay is in dB. Precalculate a suitable value for exp in          // rt_decay is in dB. Precalculate a suitable value for exp in
131          // GetReleaseTriggerAttenuation: -ln(10) / 20 * rt_decay          // GetReleaseTriggerAttenuation: -ln(10) / 20 * rt_decay
# Line 121  namespace LinuxSampler { namespace sfz { Line 151  namespace LinuxSampler { namespace sfz {
151      }      }
152    
153      double Voice::GetVelocityRelease(uint8_t MIDIKeyVelocity) {      double Voice::GetVelocityRelease(uint8_t MIDIKeyVelocity) {
154          return 0.9; // TODO:          return 127.0 / MIDIKeyVelocity;
155      }      }
156    
157      void Voice::ProcessCCEvent(RTList<Event>::Iterator& itEvent) {      void Voice::ProcessCCEvent(RTList<Event>::Iterator& itEvent) {
# Line 134  namespace LinuxSampler { namespace sfz { Line 164  namespace LinuxSampler { namespace sfz {
164      }      }
165    
166      void Voice::ProcessCutoffEvent(RTList<Event>::Iterator& itEvent) {      void Voice::ProcessCutoffEvent(RTList<Event>::Iterator& itEvent) {
167          /*int ccvalue = itEvent->Param.CC.Value;          int ccvalue = itEvent->Param.CC.Value;
168          if (VCFCutoffCtrl.value == ccvalue) return;          if (VCFCutoffCtrl.value == ccvalue) return;
169          VCFCutoffCtrl.value == ccvalue;          VCFCutoffCtrl.value = ccvalue;
170          if (pRegion->VCFCutoffControllerInvert)  ccvalue = 127 - ccvalue;  
171          if (ccvalue < pRegion->VCFVelocityScale) ccvalue = pRegion->VCFVelocityScale;          float cutoff = CutoffBase * RTMath::CentsToFreqRatioUnlimited(
172          float cutoff = CutoffBase * float(ccvalue);              ccvalue / 127.0f * pRegion->cutoff_oncc[VCFCutoffCtrl.controller]);
173          if (cutoff > 127.0f) cutoff = 127.0f;          if (cutoff > 0.49 * pEngine->SampleRate) cutoff = 0.49 * pEngine->SampleRate;
174    
175          VCFCutoffCtrl.fvalue = cutoff; // needed for initialization of fFinalCutoff next time          VCFCutoffCtrl.fvalue = cutoff; // needed for initialization of fFinalCutoff next time
176          fFinalCutoff = cutoff;*/ // TODO: ^^^          fFinalCutoff = cutoff;
177      }      }
178    
179      double Voice::CalculateCrossfadeVolume(uint8_t MIDIKeyVelocity) {      double Voice::CalculateCrossfadeVolume(uint8_t MIDIKeyVelocity) {
# Line 222  namespace LinuxSampler { namespace sfz { Line 252  namespace LinuxSampler { namespace sfz {
252    
253          // otherwise use the v1 EGADSR          // otherwise use the v1 EGADSR
254          pEG1 = &EGADSR1;          pEG1 = &EGADSR1;
255          EGADSR1.trigger(uint(RgnInfo.EG1PreAttack),          EGADSR1.trigger(uint(pRegion->ampeg_start * 10),
256                          RgnInfo.EG1Attack,                          std::max(0.0, pRegion->ampeg_attack + pRegion->ampeg_vel2attack * velrelease),
257                          RgnInfo.EG1Hold,                          std::max(0.0, pRegion->ampeg_hold + pRegion->ampeg_vel2hold * velrelease),
258                          RgnInfo.EG1Decay1,                          std::max(0.0, pRegion->ampeg_decay + pRegion->ampeg_vel2decay * velrelease),
259                          uint(RgnInfo.EG1Sustain),                          uint(std::min(std::max(0.0, 10 * (pRegion->ampeg_sustain + pRegion->ampeg_vel2sustain * velrelease)), 1000.0)),
260                          RgnInfo.EG1Release,                          std::max(0.0, pRegion->ampeg_release + pRegion->ampeg_vel2release * velrelease),
261                          sampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);                          sampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
262       }      }
263    
264       double Voice::GetEG2ControllerValue(uint8_t MIDIKeyVelocity) {      double Voice::GetEG2ControllerValue(uint8_t MIDIKeyVelocity) {
265          /*double eg2controllervalue = 0;          /*double eg2controllervalue = 0;
266          switch (pRegion->EG2Controller.type) {          switch (pRegion->EG2Controller.type) {
267              case ::gig::eg2_ctrl_t::type_none: // no controller defined              case ::gig::eg2_ctrl_t::type_none: // no controller defined
# Line 267  namespace LinuxSampler { namespace sfz { Line 297  namespace LinuxSampler { namespace sfz {
297          return eg;          return eg;
298      }      }
299    
300        void Voice::TriggerEG2(const EGInfo& egInfo, double velrelease, double velocityAttenuation, uint sampleRate, uint8_t velocity) {
301    
302            // TODO: the sfz filter EG should modulate cents, not hertz,
303            // so we can't use the EG or EGADSR as it is. Disable for now.
304    
305            pEG2 = &EGADSR2;
306            EGADSR2.trigger(0,
307                            0,
308                            false,
309                            0,
310                            1000,
311                            0,
312                            sampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);
313        }
314    
315      void Voice::InitLFO1() {      void Voice::InitLFO1() {
316          /*uint16_t lfo1_internal_depth;          /*uint16_t lfo1_internal_depth;
317          switch (pRegion->LFO1Controller) {          switch (pRegion->LFO1Controller) {
# Line 403  namespace LinuxSampler { namespace sfz { Line 448  namespace LinuxSampler { namespace sfz {
448      }      }
449    
450      float Voice::CalculateCutoffBase(uint8_t MIDIKeyVelocity) {      float Voice::CalculateCutoffBase(uint8_t MIDIKeyVelocity) {
451          /*float cutoff = pRegion->GetVelocityCutoff(MIDIKeyVelocity);          float cutoff = *pRegion->cutoff;
452          if (pRegion->VCFKeyboardTracking) {          cutoff *= RTMath::CentsToFreqRatioUnlimited(
453              cutoff *= exp((MIDIKeyVelocity - pRegion->VCFKeyboardTrackingBreakpoint) * 0.057762265f); // (ln(2) / 12)              MIDIKeyVelocity / 127.0f * pRegion->fil_veltrack +
454          }              (MIDIKey - pRegion->fil_keycenter) * pRegion->fil_keytrack);
455          return cutoff;*/ // TODO: ^^^          return cutoff;
         return 1.0f;  
456      }      }
457    
458      float Voice::CalculateFinalCutoff(float cutoffBase) {      float Voice::CalculateFinalCutoff(float cutoffBase) {
459          /*int cvalue;          float cutoff;
460          if (VCFCutoffCtrl.controller) {          if (VCFCutoffCtrl.controller) {
461              cvalue = GetSfzEngineChannel()->ControllerTable[VCFCutoffCtrl.controller];              int ccvalue = GetSfzEngineChannel()->ControllerTable[VCFCutoffCtrl.controller];
462              if (pRegion->VCFCutoffControllerInvert) cvalue = 127 - cvalue;              cutoff = CutoffBase * RTMath::CentsToFreqRatioUnlimited(
463              // VCFVelocityScale in this case means Minimum cutoff                  ccvalue / 127.0f * pRegion->cutoff_oncc[VCFCutoffCtrl.controller]);
464              if (cvalue < pRegion->VCFVelocityScale) cvalue = pRegion->VCFVelocityScale;          } else {
465                cutoff = cutoffBase;
466          }          }
467          else {          if (cutoff > 0.49 * pEngine->SampleRate) cutoff = 0.49 * pEngine->SampleRate;
468              cvalue = pRegion->VCFCutoff;          return cutoff;
         }  
         float fco = cutoffBase * float(cvalue);  
         if (fco > 127.0f) fco = 127.0f;  
   
         return fco;*/ // TODO: ^^^  
         return 127.0f;  
469      }      }
470    
471      uint8_t Voice::GetVCFCutoffCtrl() {      uint8_t Voice::GetVCFCutoffCtrl() {
472          /*uint8_t ctrl;          // TODO: the sfz format allows several CC for the same
473          switch (pRegion->VCFCutoffController) {          // modulation destination. The Voice interface needs to be
474              case ::gig::vcf_cutoff_ctrl_modwheel:          // changed to support that.
475                  ctrl = 1;          if (pRegion->cutoff_cc) return pRegion->cutoff_cc;
476                  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: ^^^  
477          return 0;          return 0;
478      }      }
479    
# Line 501  namespace LinuxSampler { namespace sfz { Line 506  namespace LinuxSampler { namespace sfz {
506          return expf(RgnInfo.ReleaseTriggerDecay * noteLength);          return expf(RgnInfo.ReleaseTriggerDecay * noteLength);
507      }      }
508    
509        void Voice::ProcessGroupEvent(RTList<Event>::Iterator& itEvent) {
510            dmsg(4,("Voice %x processGroupEvents event type=%d", this, itEvent->Type));
511            if (itEvent->Type == Event::type_control_change ||
512                (Type & Voice::type_controller_triggered) ||
513                itEvent->Param.Note.Key != MIDIKey) {
514                dmsg(4,("Voice %x - kill", this));
515                if (pRegion->off_mode == ::sfz::OFF_NORMAL) {
516                    // turn off the voice by entering release envelope stage
517                    EnterReleaseStage();
518                } else {
519                    // kill the voice fast
520                    pEG1->enterFadeOutStage();
521                }
522            }
523        }
524    
525  }} // namespace LinuxSampler::sfz  }} // namespace LinuxSampler::sfz

Legend:
Removed from v.2086  
changed lines
  Added in v.2203

  ViewVC Help
Powered by ViewVC