/[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 2216 by iliev, Mon Jul 25 17:21:16 2011 UTC revision 3054 by schoenebeck, Thu Dec 15 12:47:45 2016 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 - 2011 Christian Schoenebeck and Grigor Iliev      *   *   Copyright (C) 2009 - 2015 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 31  Line 31 
31    
32  namespace LinuxSampler { namespace sfz {  namespace LinuxSampler { namespace sfz {
33    
34      Voice::Voice() {      typedef LinuxSampler::VoiceBase<EngineChannel, ::sfz::Region, Sample, DiskThread> SfzVoiceBase;
35    
36        Voice::Voice(): SfzVoiceBase(&SignalRack), SignalRack(this) {
37          pEngine     = NULL;          pEngine     = NULL;
38            bEqSupport = true;
39      }      }
40    
41      Voice::~Voice() {      Voice::~Voice() {
# Line 56  namespace LinuxSampler { namespace sfz { Line 59  namespace LinuxSampler { namespace sfz {
59          si.ChannelCount     = pSample->GetChannelCount();          si.ChannelCount     = pSample->GetChannelCount();
60          si.FrameSize        = pSample->GetFrameSize();          si.FrameSize        = pSample->GetFrameSize();
61          si.BitDepth         = (pSample->GetFrameSize() / pSample->GetChannelCount()) * 8;          si.BitDepth         = (pSample->GetFrameSize() / pSample->GetChannelCount()) * 8;
62          si.TotalFrameCount  = pSample->GetTotalFrameCount();          si.TotalFrameCount  = (uint)pSample->GetTotalFrameCount();
63    
64          si.HasLoops       = pRegion->HasLoop();          si.HasLoops       = pRegion->HasLoop();
65          si.LoopStart      = pRegion->GetLoopStart();          si.LoopStart      = pRegion->GetLoopStart();
# Line 73  namespace LinuxSampler { namespace sfz { Line 76  namespace LinuxSampler { namespace sfz {
76          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
77          ri.SampleStartOffset = pRegion->offset ? *(pRegion->offset) : 0;          ri.SampleStartOffset = pRegion->offset ? *(pRegion->offset) : 0;
78    
         ri.EG2PreAttack        = pRegion->fileg_start * 10;  
         ri.EG2Attack           = pRegion->fileg_attack;  
         //ri.EG2Hold             = pRegion->fileg_hold; // TODO:  
         ri.EG2Decay1           = pRegion->fileg_decay;  
         ri.EG2Decay2           = pRegion->fileg_decay;  
         ri.EG2Sustain          = pRegion->fileg_sustain * 10;  
         ri.EG2InfiniteSustain  = true;  
         ri.EG2Release          = pRegion->fileg_release;  
   
         ri.EG3Attack     = pRegion->pitcheg_attack;  
         ri.EG3Depth      = 0; // TODO:  
79          ri.VCFEnabled    = pRegion->cutoff;          ri.VCFEnabled    = pRegion->cutoff;
80          switch (pRegion->fil_type) {          switch (pRegion->fil_type) {
81          case ::sfz::LPF_1P:          case ::sfz::LPF_1P:
# Line 143  namespace LinuxSampler { namespace sfz { Line 135  namespace LinuxSampler { namespace sfz {
135      }      }
136    
137      double Voice::GetSampleAttenuation() {      double Voice::GetSampleAttenuation() {
138          return exp(LN_10_DIV_20 * pRegion->volume);          return exp(LN_10_DIV_20 * pRegion->volume) * pRegion->amplitude / 100;
139      }      }
140    
141      double Voice::GetVelocityAttenuation(uint8_t MIDIKeyVelocity) {      double Voice::GetVelocityAttenuation(uint8_t MIDIKeyVelocity) {
142          return pRegion->amp_velcurve[MIDIKeyVelocity];          float offset = -pRegion->amp_veltrack;
143            if (offset <= 0) offset += 100;
144            return (offset + pRegion->amp_veltrack * pRegion->amp_velcurve[MIDIKeyVelocity]) / 100;
145      }      }
146    
147      double Voice::GetVelocityRelease(uint8_t MIDIKeyVelocity) {      double Voice::GetVelocityRelease(uint8_t MIDIKeyVelocity) {
# Line 163  namespace LinuxSampler { namespace sfz { Line 157  namespace LinuxSampler { namespace sfz {
157          }*/ // TODO: ^^^          }*/ // TODO: ^^^
158      }      }
159    
160      void Voice::ProcessCutoffEvent(RTList<Event>::Iterator& itEvent) {      void Voice::ProcessChannelPressureEvent(RTList<Event>::Iterator& itEvent) {
161          int ccvalue = itEvent->Param.CC.Value;          //TODO: ...
162          if (VCFCutoffCtrl.value == ccvalue) return;      }
         VCFCutoffCtrl.value = ccvalue;  
   
         float cutoff = CutoffBase * RTMath::CentsToFreqRatioUnlimited(  
             ccvalue / 127.0f * pRegion->cutoff_oncc[VCFCutoffCtrl.controller]);  
         if (cutoff > 0.49 * pEngine->SampleRate) cutoff = 0.49 * pEngine->SampleRate;  
163    
164          VCFCutoffCtrl.fvalue = cutoff; // needed for initialization of fFinalCutoff next time      void Voice::ProcessPolyphonicKeyPressureEvent(RTList<Event>::Iterator& itEvent) {
165          fFinalCutoff = cutoff;          //TODO: ...
166      }      }
167    
168      double Voice::CalculateCrossfadeVolume(uint8_t MIDIKeyVelocity) {      double Voice::CalculateCrossfadeVolume(uint8_t MIDIKeyVelocity) {
# Line 236  namespace LinuxSampler { namespace sfz { Line 225  namespace LinuxSampler { namespace sfz {
225          return eg;          return eg;
226      }      }
227    
     void Voice::TriggerEG1(const EGInfo& egInfo, double velrelease, double velocityAttenuation, uint sampleRate, uint8_t velocity) {  
   
         // TODO: controller modulation  
   
         // first check if there is a v2 EG for amplitude  
         for (int i = 0 ; i < pRegion->eg.size() ; i++) {  
             if (pRegion->eg[i].amplitude > 0) {  
                 // TODO: actually use the value of the amplitude parameter  
                 pEG1 = &EG1;  
                 EG1.trigger(pRegion->eg[i], sampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE, velocity);  
                 return;  
             }  
         }  
   
         // otherwise use the v1 EGADSR  
         pEG1 = &EGADSR1;  
         EGADSR1.trigger(uint(pRegion->ampeg_start * 10),  
                         std::max(0.0, pRegion->ampeg_attack + pRegion->ampeg_vel2attack * velrelease),  
                         std::max(0.0, pRegion->ampeg_hold + pRegion->ampeg_vel2hold * velrelease),  
                         std::max(0.0, pRegion->ampeg_decay + pRegion->ampeg_vel2decay * velrelease),  
                         uint(std::min(std::max(0.0, 10 * (pRegion->ampeg_sustain + pRegion->ampeg_vel2sustain * velrelease)), 1000.0)),  
                         std::max(0.0, pRegion->ampeg_release + pRegion->ampeg_vel2release * velrelease),  
                         sampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);  
     }  
   
228      double Voice::GetEG2ControllerValue(uint8_t MIDIKeyVelocity) {      double Voice::GetEG2ControllerValue(uint8_t MIDIKeyVelocity) {
229          /*double eg2controllervalue = 0;          /*double eg2controllervalue = 0;
230          switch (pRegion->EG2Controller.type) {          switch (pRegion->EG2Controller.type) {
# Line 297  namespace LinuxSampler { namespace sfz { Line 261  namespace LinuxSampler { namespace sfz {
261          return eg;          return eg;
262      }      }
263    
     void Voice::TriggerEG2(const EGInfo& egInfo, double velrelease, double velocityAttenuation, uint sampleRate, uint8_t velocity) {  
   
         // TODO: the sfz filter EG should modulate cents, not hertz,  
         // so we can't use the EG or EGADSR as it is. Disable for now.  
   
         pEG2 = &EGADSR2;  
         EGADSR2.trigger(0,  
                         0,  
                         false,  
                         0,  
                         1000,  
                         0,  
                         sampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);  
     }  
   
     void Voice::InitLFO1() {  
         /*uint16_t lfo1_internal_depth;  
         switch (pRegion->LFO1Controller) {  
             case ::gig::lfo1_ctrl_internal:  
                 lfo1_internal_depth  = pRegion->LFO1InternalDepth;  
                 pLFO1->ExtController = 0; // no external controller  
                 bLFO1Enabled         = (lfo1_internal_depth > 0);  
                 break;  
             case ::gig::lfo1_ctrl_modwheel:  
                 lfo1_internal_depth  = 0;  
                 pLFO1->ExtController = 1; // MIDI controller 1  
                 bLFO1Enabled         = (pRegion->LFO1ControlDepth > 0);  
                 break;  
             case ::gig::lfo1_ctrl_breath:  
                 lfo1_internal_depth  = 0;  
                 pLFO1->ExtController = 2; // MIDI controller 2  
                 bLFO1Enabled         = (pRegion->LFO1ControlDepth > 0);  
                 break;  
             case ::gig::lfo1_ctrl_internal_modwheel:  
                 lfo1_internal_depth  = pRegion->LFO1InternalDepth;  
                 pLFO1->ExtController = 1; // MIDI controller 1  
                 bLFO1Enabled         = (lfo1_internal_depth > 0 || pRegion->LFO1ControlDepth > 0);  
                 break;  
             case ::gig::lfo1_ctrl_internal_breath:  
                 lfo1_internal_depth  = pRegion->LFO1InternalDepth;  
                 pLFO1->ExtController = 2; // MIDI controller 2  
                 bLFO1Enabled         = (lfo1_internal_depth > 0 || pRegion->LFO1ControlDepth > 0);  
                 break;  
             default:  
                 lfo1_internal_depth  = 0;  
                 pLFO1->ExtController = 0; // no external controller  
                 bLFO1Enabled         = false;  
         }  
         if (bLFO1Enabled) {  
             pLFO1->trigger(pRegion->LFO1Frequency,  
                            start_level_min,  
                            lfo1_internal_depth,  
                            pRegion->LFO1ControlDepth,  
                            pRegion->LFO1FlipPhase,  
                            pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);  
             pLFO1->update(pLFO1->ExtController ? GetSfzEngineChannel()->ControllerTable[pLFO1->ExtController] : 0);  
         }*/ // TODO: ^^^  
         bLFO1Enabled = false;  
     }  
   
     void Voice::InitLFO2() {  
         /*uint16_t lfo2_internal_depth;  
         switch (pRegion->LFO2Controller) {  
             case ::gig::lfo2_ctrl_internal:  
                 lfo2_internal_depth  = pRegion->LFO2InternalDepth;  
                 pLFO2->ExtController = 0; // no external controller  
                 bLFO2Enabled         = (lfo2_internal_depth > 0);  
                 break;  
             case ::gig::lfo2_ctrl_modwheel:  
                 lfo2_internal_depth  = 0;  
                 pLFO2->ExtController = 1; // MIDI controller 1  
                 bLFO2Enabled         = (pRegion->LFO2ControlDepth > 0);  
                 break;  
             case ::gig::lfo2_ctrl_foot:  
                 lfo2_internal_depth  = 0;  
                 pLFO2->ExtController = 4; // MIDI controller 4  
                 bLFO2Enabled         = (pRegion->LFO2ControlDepth > 0);  
                 break;  
             case ::gig::lfo2_ctrl_internal_modwheel:  
                 lfo2_internal_depth  = pRegion->LFO2InternalDepth;  
                 pLFO2->ExtController = 1; // MIDI controller 1  
                 bLFO2Enabled         = (lfo2_internal_depth > 0 || pRegion->LFO2ControlDepth > 0);  
                 break;  
             case ::gig::lfo2_ctrl_internal_foot:  
                 lfo2_internal_depth  = pRegion->LFO2InternalDepth;  
                 pLFO2->ExtController = 4; // MIDI controller 4  
                 bLFO2Enabled         = (lfo2_internal_depth > 0 || pRegion->LFO2ControlDepth > 0);  
                 break;  
             default:  
                 lfo2_internal_depth  = 0;  
                 pLFO2->ExtController = 0; // no external controller  
                 bLFO2Enabled         = false;  
         }  
         if (bLFO2Enabled) {  
             pLFO2->trigger(pRegion->LFO2Frequency,  
                            start_level_max,  
                            lfo2_internal_depth,  
                            pRegion->LFO2ControlDepth,  
                            pRegion->LFO2FlipPhase,  
                            pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);  
             pLFO2->update(pLFO2->ExtController ? GetSfzEngineChannel()->ControllerTable[pLFO2->ExtController] : 0);  
         }*/ // TODO: ^^^  
         bLFO2Enabled = false;  
     }  
   
     void Voice::InitLFO3() {  
         /*uint16_t lfo3_internal_depth;  
         switch (pRegion->LFO3Controller) {  
             case ::gig::lfo3_ctrl_internal:  
                 lfo3_internal_depth  = pRegion->LFO3InternalDepth;  
                 pLFO3->ExtController = 0; // no external controller  
                 bLFO3Enabled         = (lfo3_internal_depth > 0);  
                 break;  
             case ::gig::lfo3_ctrl_modwheel:  
                 lfo3_internal_depth  = 0;  
                 pLFO3->ExtController = 1; // MIDI controller 1  
                 bLFO3Enabled         = (pRegion->LFO3ControlDepth > 0);  
                 break;  
             case ::gig::lfo3_ctrl_aftertouch:  
                 lfo3_internal_depth  = 0;  
                 pLFO3->ExtController = 128;  
                 bLFO3Enabled         = true;  
                 break;  
             case ::gig::lfo3_ctrl_internal_modwheel:  
                 lfo3_internal_depth  = pRegion->LFO3InternalDepth;  
                 pLFO3->ExtController = 1; // MIDI controller 1  
                 bLFO3Enabled         = (lfo3_internal_depth > 0 || pRegion->LFO3ControlDepth > 0);  
                 break;  
             case ::gig::lfo3_ctrl_internal_aftertouch:  
                 lfo3_internal_depth  = pRegion->LFO3InternalDepth;  
                 pLFO1->ExtController = 128;  
                 bLFO3Enabled         = (lfo3_internal_depth > 0 || pRegion->LFO3ControlDepth > 0);  
                 break;  
             default:  
                 lfo3_internal_depth  = 0;  
                 pLFO3->ExtController = 0; // no external controller  
                 bLFO3Enabled         = false;  
         }  
         if (bLFO3Enabled) {  
             pLFO3->trigger(pRegion->LFO3Frequency,  
                            start_level_mid,  
                            lfo3_internal_depth,  
                            pRegion->LFO3ControlDepth,  
                            false,  
                            pEngine->SampleRate / CONFIG_DEFAULT_SUBFRAGMENT_SIZE);  
             pLFO3->update(pLFO3->ExtController ? GetSfzEngineChannel()->ControllerTable[pLFO3->ExtController] : 0);  
         }*/ // TODO: ^^^  
         bLFO3Enabled = false;  
     }  
   
264      float Voice::CalculateCutoffBase(uint8_t MIDIKeyVelocity) {      float Voice::CalculateCutoffBase(uint8_t MIDIKeyVelocity) {
265          float cutoff = *pRegion->cutoff;          float cutoff = *pRegion->cutoff;
266          cutoff *= RTMath::CentsToFreqRatioUnlimited(          cutoff *= RTMath::CentsToFreqRatioUnlimited(
267              MIDIKeyVelocity / 127.0f * pRegion->fil_veltrack +              MIDIKeyVelocity / 127.0f * pRegion->fil_veltrack +
268              (MIDIKey - pRegion->fil_keycenter) * pRegion->fil_keytrack);              (MIDIKey() - pRegion->fil_keycenter) * pRegion->fil_keytrack);
269          return cutoff;          return cutoff;
270      }      }
271    
272      float Voice::CalculateFinalCutoff(float cutoffBase) {      float Voice::CalculateFinalCutoff(float cutoffBase) {
273          float cutoff;          float cutoff = cutoffBase;
         if (VCFCutoffCtrl.controller) {  
             int ccvalue = GetSfzEngineChannel()->ControllerTable[VCFCutoffCtrl.controller];  
             cutoff = CutoffBase * RTMath::CentsToFreqRatioUnlimited(  
                 ccvalue / 127.0f * pRegion->cutoff_oncc[VCFCutoffCtrl.controller]);  
         } else {  
             cutoff = cutoffBase;  
         }  
274          if (cutoff > 0.49 * pEngine->SampleRate) cutoff = 0.49 * pEngine->SampleRate;          if (cutoff > 0.49 * pEngine->SampleRate) cutoff = 0.49 * pEngine->SampleRate;
275          return cutoff;          return cutoff;
276      }      }
277    
     uint8_t Voice::GetVCFCutoffCtrl() {  
         // TODO: the sfz format allows several CC for the same  
         // modulation destination. The Voice interface needs to be  
         // changed to support that.  
         if (pRegion->cutoff_cc) return pRegion->cutoff_cc;  
         else if (pRegion->cutoff_chanaft) return 128;  
         return 0;  
     }  
   
     uint8_t Voice::GetVCFResonanceCtrl() {  
         /*uint8_t ctrl;  
         switch (pRegion->VCFResonanceController) {  
             case ::gig::vcf_res_ctrl_genpurpose3:  
                 ctrl = 18;  
                 break;  
             case ::gig::vcf_res_ctrl_genpurpose4:  
                 ctrl = 19;  
                 break;  
             case ::gig::vcf_res_ctrl_genpurpose5:  
                 ctrl = 80;  
                 break;  
             case ::gig::vcf_res_ctrl_genpurpose6:  
                 ctrl = 81;  
                 break;  
             case ::gig::vcf_res_ctrl_none:  
             default:  
                 ctrl = 0;  
         }  
   
         return ctrl;*/ // TODO: ^^^  
         return 0;  
     }  
   
278      float Voice::GetReleaseTriggerAttenuation(float noteLength) {      float Voice::GetReleaseTriggerAttenuation(float noteLength) {
279          // pow(10, -rt_decay * noteLength / 20):          // pow(10, -rt_decay * noteLength / 20):
280          return expf(RgnInfo.ReleaseTriggerDecay * noteLength);          return expf(RgnInfo.ReleaseTriggerDecay * noteLength);
281      }      }
282    
283      void Voice::ProcessGroupEvent(RTList<Event>::Iterator& itEvent) {      void Voice::ProcessGroupEvent(RTList<Event>::Iterator& itEvent) {
284          dmsg(4,("Voice %x processGroupEvents event type=%d", this, itEvent->Type));          dmsg(4,("Voice %p processGroupEvents event type=%d", (void*)this, itEvent->Type));
285          if (itEvent->Type == Event::type_control_change ||          if (itEvent->Type == Event::type_control_change ||
286              (Type & Voice::type_controller_triggered) ||              (Type & Voice::type_controller_triggered) ||
287              itEvent->Param.Note.Key != MIDIKey) {              itEvent->Param.Note.Key != HostKey()) {
288              dmsg(4,("Voice %x - kill", this));              dmsg(4,("Voice %p - kill", (void*)this));
289              if (pRegion->off_mode == ::sfz::OFF_NORMAL) {              if (pRegion->off_mode == ::sfz::OFF_NORMAL) {
290                  // turn off the voice by entering release envelope stage                  // turn off the voice by entering release envelope stage
291                  EnterReleaseStage();                  EnterReleaseStage();
292              } else {              } else {
293                  // kill the voice fast                  // kill the voice fast
294                  pEG1->enterFadeOutStage();                  SignalRack.EnterFadeOutStage();
295              }              }
296          }          }
297      }      }
# Line 533  namespace LinuxSampler { namespace sfz { Line 307  namespace LinuxSampler { namespace sfz {
307          }          }
308      }      }
309    
310        void Voice::CalculateFadeOutCoeff(float FadeOutTime, float SampleRate) {
311            SignalRack.CalculateFadeOutCoeff(FadeOutTime, SampleRate);
312        }
313    
314        int Voice::CalculatePan(uint8_t pan) {
315            // the value isn't limited to [0, 127] here, as this is done
316            // later in SignalUnit.CalculatePan
317            return pan + RgnInfo.Pan;
318        }
319    
320  }} // namespace LinuxSampler::sfz  }} // namespace LinuxSampler::sfz

Legend:
Removed from v.2216  
changed lines
  Added in v.3054

  ViewVC Help
Powered by ViewVC