/[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 2203 by persson, Sat Jul 9 16:44:27 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() {      Voice::Voice() {
# Line 67  namespace LinuxSampler { namespace sfz { Line 69  namespace LinuxSampler { namespace sfz {
69      Voice::RegionInfo Voice::GetRegionInfo() {      Voice::RegionInfo Voice::GetRegionInfo() {
70          RegionInfo ri;          RegionInfo ri;
71          ri.UnityNote = pRegion->pitch_keycenter;          ri.UnityNote = pRegion->pitch_keycenter;
72          ri.FineTune  = pRegion->tune;          ri.FineTune  = pRegion->tune + pRegion->transpose * 100;
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    
76          ri.EG1PreAttack        = pRegion->ampeg_start * 10;          ri.EG2PreAttack        = pRegion->fileg_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;  
   
         ri.EG2PreAttack        = pRegion->fileg_start;  
77          ri.EG2Attack           = pRegion->fileg_attack;          ri.EG2Attack           = pRegion->fileg_attack;
78          //ri.EG2Hold             = pRegion->fileg_hold; // TODO:          //ri.EG2Hold             = pRegion->fileg_hold; // TODO:
79          ri.EG2Decay1           = pRegion->fileg_decay;          ri.EG2Decay1           = pRegion->fileg_decay;
80          ri.EG2Decay2           = pRegion->fileg_decay;          ri.EG2Decay2           = pRegion->fileg_decay;
81          ri.EG2Sustain          = pRegion->fileg_sustain;          ri.EG2Sustain          = pRegion->fileg_sustain * 10;
82          ri.EG2InfiniteSustain  = true;          ri.EG2InfiniteSustain  = true;
83          ri.EG2Release          = pRegion->fileg_release;          ri.EG2Release          = pRegion->fileg_release;
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          ri.ReleaseTriggerDecay = 0;              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
131            // GetReleaseTriggerAttenuation: -ln(10) / 20 * rt_decay
132            ri.ReleaseTriggerDecay = -LN_10_DIV_20 * pRegion->rt_decay;
133    
134          return ri;          return ri;
135      }      }
# Line 109  namespace LinuxSampler { namespace sfz { Line 143  namespace LinuxSampler { namespace sfz {
143      }      }
144    
145      double Voice::GetSampleAttenuation() {      double Voice::GetSampleAttenuation() {
146          return 1.0; // TODO:          return exp(LN_10_DIV_20 * pRegion->volume);
147      }      }
148    
149      double Voice::GetVelocityAttenuation(uint8_t MIDIKeyVelocity) {      double Voice::GetVelocityAttenuation(uint8_t MIDIKeyVelocity) {
150          return double(MIDIKeyVelocity) / 127.0f; // TODO:          return pRegion->amp_velcurve[MIDIKeyVelocity];
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 130  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 202  namespace LinuxSampler { namespace sfz { Line 236  namespace LinuxSampler { namespace sfz {
236          return eg;          return eg;
237      }      }
238    
239        void Voice::TriggerEG1(const EGInfo& egInfo, double velrelease, double velocityAttenuation, uint sampleRate, uint8_t velocity) {
240    
241            // TODO: controller modulation
242    
243            // first check if there is a v2 EG for amplitude
244            for (int i = 0 ; i < pRegion->eg.size() ; i++) {
245                if (pRegion->eg[i].amplitude > 0) {
246                    // TODO: actually use the value of the amplitude parameter
247                    pEG1 = &EG1;
248                    EG1.trigger(pRegion->eg[i], sampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE, velocity);
249                    return;
250                }
251            }
252    
253            // otherwise use the v1 EGADSR
254            pEG1 = &EGADSR1;
255            EGADSR1.trigger(uint(pRegion->ampeg_start * 10),
256                            std::max(0.0, pRegion->ampeg_attack + pRegion->ampeg_vel2attack * velrelease),
257                            std::max(0.0, pRegion->ampeg_hold + pRegion->ampeg_vel2hold * velrelease),
258                            std::max(0.0, pRegion->ampeg_decay + pRegion->ampeg_vel2decay * velrelease),
259                            uint(std::min(std::max(0.0, 10 * (pRegion->ampeg_sustain + pRegion->ampeg_vel2sustain * velrelease)), 1000.0)),
260                            std::max(0.0, pRegion->ampeg_release + pRegion->ampeg_vel2release * velrelease),
261                            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) {
# Line 238  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 374  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;
         else {  
             cvalue = pRegion->VCFCutoff;  
466          }          }
467          float fco = cutoffBase * float(cvalue);          if (cutoff > 0.49 * pEngine->SampleRate) cutoff = 0.49 * pEngine->SampleRate;
468          if (fco > 127.0f) fco = 127.0f;          return cutoff;
   
         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 467  namespace LinuxSampler { namespace sfz { Line 501  namespace LinuxSampler { namespace sfz {
501          return 0;          return 0;
502      }      }
503    
504        float Voice::GetReleaseTriggerAttenuation(float noteLength) {
505            // pow(10, -rt_decay * noteLength / 20):
506            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.2045  
changed lines
  Added in v.2203

  ViewVC Help
Powered by ViewVC