/[svn]/linuxsampler/trunk/src/engines/gig/Voice.h
ViewVC logotype

Diff of /linuxsampler/trunk/src/engines/gig/Voice.h

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

revision 203 by schoenebeck, Tue Jul 13 22:44:13 2004 UTC revision 2382 by persson, Sun Dec 2 16:30:42 2012 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                       *
7     *   Copyright (C) 2009 - 2012 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 23  Line 25 
25  #ifndef __LS_GIG_VOICE_H__  #ifndef __LS_GIG_VOICE_H__
26  #define __LS_GIG_VOICE_H__  #define __LS_GIG_VOICE_H__
27    
28  #include "../../common/global.h"  #include "../../common/global_private.h"
29    
30  #if DEBUG_HEADERS  #include <gig.h>
 # warning Voice.h included  
 #endif // DEBUG_HEADERS  
31    
32  #include "../../common/RTMath.h"  #include "../../common/RTMath.h"
33  #include "../../common/RingBuffer.h"  #include "../../common/Pool.h"
 #include "../../common/RTELMemoryPool.h"  
34  #include "../../drivers/audio/AudioOutputDevice.h"  #include "../../drivers/audio/AudioOutputDevice.h"
 #include "../../lib/fileloader/libgig/gig.h"  
 #include "../common/BiquadFilter.h"  
 #include "Engine.h"  
35  #include "Stream.h"  #include "Stream.h"
36  #include "DiskThread.h"  #include "DiskThread.h"
37    #include "EGADSR.h"
38  #include "EGDecay.h"  #include "EGDecay.h"
39  #include "Filter.h"  #include "Filter.h"
40  #include "../common/LFO.h"  #include "../common/VoiceBase.h"
41    #include "SynthesisParam.h"
42  #define USE_LINEAR_INTERPOLATION        0  ///< set to 0 if you prefer cubic interpolation (slower, better quality)  #include "SmoothVolume.h"
 #define ENABLE_FILTER                   1  ///< if set to 0 then filter (VCF) code is ignored on compile time  
 #define FILTER_UPDATE_PERIOD            64 ///< amount of sample points after which filter parameters (cutoff, resonance) are going to be updated (higher value means less CPU load, but also worse parameter resolution, this value will be aligned to a power of two)  
 #define FORCE_FILTER_USAGE              0  ///< if set to 1 then filter is always used, if set to 0 filter is used only in case the instrument file defined one  
 #define FILTER_CUTOFF_MAX               10000.0f ///< maximum cutoff frequency (10kHz)  
 #define FILTER_CUTOFF_MIN               100.0f   ///< minimum cutoff frequency (100Hz)  
   
 // Uncomment following line to override external cutoff controller  
 //#define OVERRIDE_FILTER_CUTOFF_CTRL   1  ///< set to an arbitrary MIDI control change controller (e.g. 1 for 'modulation wheel')  
   
 // Uncomment following line to override external resonance controller  
 //#define OVERRIDE_FILTER_RES_CTRL      91  ///< set to an arbitrary MIDI control change controller (e.g. 91 for 'effect 1 depth')  
   
 // Uncomment following line to override filter type  
 //#define OVERRIDE_FILTER_TYPE          ::gig::vcf_type_lowpass  ///< either ::gig::vcf_type_lowpass, ::gig::vcf_type_bandpass or ::gig::vcf_type_highpass  
43    
44  namespace LinuxSampler { namespace gig {  namespace LinuxSampler { namespace gig {
   
45      class Engine;      class Engine;
46      class EGADSR;      class EngineChannel;
     class VCAManipulator;  
     class VCFCManipulator;  
     class VCOManipulator;  
   
     /// Reflects a MIDI controller  
     struct midi_ctrl {  
         uint8_t controller; ///< MIDI control change controller number  
         uint8_t value;      ///< Current MIDI controller value  
         float   fvalue;     ///< Transformed / effective value (e.g. volume level or filter cutoff frequency)  
     };  
47    
48      /** Gig Voice      /** Gig Voice
49       *       *
50       * Renders a voice for the Gigasampler format.       * Renders a voice for the Gigasampler format.
51       */       */
52      class Voice {      class Voice : public LinuxSampler::VoiceBase<EngineChannel, ::gig::DimensionRegion, ::gig::Sample, DiskThread> {
53          public:          public:
             // Attributes  
             int          MIDIKey;      ///< MIDI key number of the key that triggered the voice  
             DiskThread*  pDiskThread;  ///< Pointer to the disk thread, to be able to order a disk stream and later to delete the stream again  
   
             // Methods  
54              Voice();              Voice();
55             ~Voice();              virtual ~Voice();
             void Kill();  
             void Render(uint Samples);  
             void Reset();  
56              void SetOutput(AudioOutputDevice* pAudioOutputDevice);              void SetOutput(AudioOutputDevice* pAudioOutputDevice);
57              void SetEngine(Engine* pEngine);              void SetEngine(LinuxSampler::Engine* pEngine);
58              int  Trigger(Event* pNoteOnEvent, int PitchBend, ::gig::Instrument* pInstrument);              void CalculateFadeOutCoeff(float FadeOutTime, float SampleRate);
59              inline bool IsActive() { return Active; }  
60            protected:
61                virtual SampleInfo       GetSampleInfo();
62                virtual RegionInfo       GetRegionInfo();
63                virtual InstrumentInfo   GetInstrumentInfo();
64                virtual double           CalculateCrossfadeVolume(uint8_t MIDIKeyVelocity);
65                virtual AbstractEngine*  GetEngine() { return (AbstractEngine*)pEngine; }
66                virtual double           GetEG1ControllerValue(uint8_t MIDIKeyVelocity);
67                virtual EGInfo           CalculateEG1ControllerInfluence(double eg1ControllerValue);
68                virtual void             TriggerEG1(const EGInfo& egInfo, double velrelease, double velocityAttenuation, uint sampleRate, uint8_t velocity);
69                virtual double           GetEG2ControllerValue(uint8_t MIDIKeyVelocity);
70                virtual EGInfo           CalculateEG2ControllerInfluence(double eg2ControllerValue);
71                virtual void             TriggerEG2(const EGInfo& egInfo, double velrelease, double velocityAttenuation, uint sampleRate, uint8_t velocity);
72                virtual void             InitLFO1();
73                virtual void             InitLFO2();
74                virtual void             InitLFO3();
75                virtual float            CalculateCutoffBase(uint8_t MIDIKeyVelocity);
76                virtual float            CalculateFinalCutoff(float cutoffBase);
77                virtual uint8_t          GetVCFCutoffCtrl();
78                virtual uint8_t          GetVCFResonanceCtrl();
79                virtual void             ProcessCCEvent(RTList<Event>::Iterator& itEvent);
80                virtual void             ProcessCutoffEvent(RTList<Event>::Iterator& itEvent);
81                virtual double           GetVelocityAttenuation(uint8_t MIDIKeyVelocity);
82                virtual double           GetVelocityRelease(uint8_t MIDIKeyVelocity);
83                virtual double           GetSampleAttenuation();
84                virtual void             ProcessGroupEvent(RTList<Event>::Iterator& itEvent);
85                virtual int              CalculatePan(uint8_t pan);
86    
87          private:          private:
88              // Types              EGADSR EG1;
89              enum playback_state_t {              EGADSR EG2;
                 playback_state_ram,  
                 playback_state_disk,  
                 playback_state_end  
             };  
90    
91            public: // FIXME: just made public for debugging (sanity check in Engine::RenderAudio()), should be changed to private before the final release
92              // Attributes              // Attributes
93              gig::Engine*                pEngine;            ///< Pointer to the sampler engine, to be able to access the event lists.              Engine*                     pEngine;            ///< Pointer to the sampler engine, to be able to access the event lists.
94              float                       Volume;             ///< Volume level of the voice              //uint                        LoopCyclesLeft;     ///< In case there is a RAMLoop and it's not an endless loop; reflects number of loop cycles left to be passed
             float*                      pOutputLeft;        ///< Audio output channel buffer (left)  
             float*                      pOutputRight;       ///< Audio output channel buffer (right)  
             uint                        SampleRate;         ///< Sample rate of the engines output audio signal (in Hz)  
             uint                        MaxSamplesPerCycle; ///< Size of each audio output buffer  
             double                      Pos;                ///< Current playback position in sample  
             double                      PitchBase;          ///< Basic pitch depth, stays the same for the whole life time of the voice  
             double                      PitchBend;          ///< Current pitch value of the pitchbend wheel  
             ::gig::Sample*              pSample;            ///< Pointer to the sample to be played back  
             ::gig::Region*              pRegion;            ///< Pointer to the articulation information of the respective keyboard region of this voice  
             bool                        Active;             ///< If this voice object is currently in usage  
             playback_state_t            PlaybackState;      ///< When a sample will be triggered, it will be first played from RAM cache and after a couple of sample points it will switch to disk streaming and at the end of a disk stream we have to add null samples, so the interpolator can do it's work correctly  
             bool                        DiskVoice;          ///< If the sample is very short it completely fits into the RAM cache and doesn't need to be streamed from disk, in that case this flag is set to false  
             Stream::reference_t         DiskStreamRef;      ///< Reference / link to the disk stream  
             unsigned long               MaxRAMPos;          ///< The upper allowed limit (not actually the end) in the RAM sample cache, after that point it's not safe to chase the interpolator another time over over the current cache position, instead we switch to disk then.  
             bool                        RAMLoop;            ///< If this voice has a loop defined which completely fits into the cached RAM part of the sample, in this case we handle the looping within the voice class, else if the loop is located in the disk stream part, we let the disk stream handle the looping  
             int                         LoopCyclesLeft;     ///< In case there is a RAMLoop and it's not an endless loop; reflects number of loop cycles left to be passed  
             uint                        Delay;              ///< Number of sample points the rendering process of this voice should be delayed (jitter correction), will be set to 0 after the first audio fragment cycle  
             EGADSR*                     pEG1;               ///< Envelope Generator 1 (Amplification)  
             EGADSR*                     pEG2;               ///< Envelope Generator 2 (Filter cutoff frequency)  
             EGDecay*                    pEG3;               ///< Envelope Generator 3 (Pitch)  
             Filter                      FilterLeft;  
             Filter                      FilterRight;  
             midi_ctrl                   VCFCutoffCtrl;  
             midi_ctrl                   VCFResonanceCtrl;  
             int                         FilterUpdateCounter; ///< Used to update filter parameters all FILTER_UPDATE_PERIOD samples  
             static const float          FILTER_CUTOFF_COEFF;  
             static const int            FILTER_UPDATE_MASK;  
             VCAManipulator*             pVCAManipulator;  
             VCFCManipulator*            pVCFCManipulator;  
             VCOManipulator*             pVCOManipulator;  
             LFO<gig::VCAManipulator>*   pLFO1;              ///< Low Frequency Oscillator 1 (Amplification)  
             LFO<gig::VCFCManipulator>*  pLFO2;             ///< Low Frequency Oscillator 2 (Filter cutoff frequency)  
             LFO<gig::VCOManipulator>*   pLFO3;              ///< Low Frequency Oscillator 3 (Pitch)  
             Event*                      pTriggerEvent;      ///< First event on the key's list the voice should process (only needed for the first audio fragment in which voice was triggered, after that it will be set to NULL).  
95    
96              // Static Methods              // Static Methods
97              static float CalculateFilterCutoffCoeff();              static float CalculateFilterCutoffCoeff();
             static int   CalculateFilterUpdateMask();  
98    
99              // Methods              // Methods
100              void        ProcessEvents(uint Samples);              void ProcessEvents(uint Samples);
101              #if ENABLE_FILTER              void processCrossFadeEvent(RTList<Event>::Iterator& itEvent);
             void        CalculateBiquadParameters(uint Samples);  
             #endif // ENABLE_FILTER  
             void        Interpolate(uint Samples, sample_t* pSrc, uint Skip);  
             void        InterpolateAndLoop(uint Samples, sample_t* pSrc, uint Skip);  
             inline void InterpolateOneStep_Stereo(sample_t* pSrc, int& i, float& effective_volume, float& pitch, biquad_param_t& bq_base, biquad_param_t& bq_main) {  
                 int   pos_int   = RTMath::DoubleToInt(this->Pos);  // integer position  
                 float pos_fract = this->Pos - pos_int;             // fractional part of position  
                 pos_int <<= 1;  
   
                 #if USE_LINEAR_INTERPOLATION  
                     #if ENABLE_FILTER  
                         // left channel  
                         pOutputLeft[i]    += this->FilterLeft.Apply(&bq_base, &bq_main, effective_volume * (pSrc[pos_int]   + pos_fract * (pSrc[pos_int+2] - pSrc[pos_int])));  
                         // right channel  
                         pOutputRight[i++] += this->FilterRight.Apply(&bq_base, &bq_main, effective_volume * (pSrc[pos_int+1] + pos_fract * (pSrc[pos_int+3] - pSrc[pos_int+1])));  
                     #else // no filter  
                         // left channel  
                         pOutputLeft[i]    += effective_volume * (pSrc[pos_int]   + pos_fract * (pSrc[pos_int+2] - pSrc[pos_int]));  
                         // right channel  
                         pOutputRight[i++] += effective_volume * (pSrc[pos_int+1] + pos_fract * (pSrc[pos_int+3] - pSrc[pos_int+1]));  
                     #endif // ENABLE_FILTER  
                 #else // polynomial interpolation  
                     // calculate left channel  
                     float xm1 = pSrc[pos_int];  
                     float x0  = pSrc[pos_int+2];  
                     float x1  = pSrc[pos_int+4];  
                     float x2  = pSrc[pos_int+6];  
                     float a   = (3.0f * (x0 - x1) - xm1 + x2) * 0.5f;  
                     float b   = 2.0f * x1 + xm1 - (5.0f * x0 + x2) * 0.5f;  
                     float c   = (x1 - xm1) * 0.5f;  
                     #if ENABLE_FILTER  
                         pOutputLeft[i] += this->FilterLeft.Apply(&bq_base, &bq_main, effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0));  
                     #else // no filter  
                         pOutputLeft[i] += effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0);  
                     #endif // ENABLE_FILTER  
   
                     //calculate right channel  
                     xm1 = pSrc[pos_int+1];  
                     x0  = pSrc[pos_int+3];  
                     x1  = pSrc[pos_int+5];  
                     x2  = pSrc[pos_int+7];  
                     a   = (3.0f * (x0 - x1) - xm1 + x2) * 0.5f;  
                     b   = 2.0f * x1 + xm1 - (5.0f * x0 + x2) * 0.5f;  
                     c   = (x1 - xm1) * 0.5f;  
                     #if ENABLE_FILTER  
                         pOutputRight[i++] += this->FilterRight.Apply(&bq_base, &bq_main, effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0));  
                     #else // no filter  
                         pOutputRight[i++] += effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0);  
                     #endif // ENABLE_FILTER  
                 #endif // USE_LINEAR_INTERPOLATION  
   
                 this->Pos += pitch;  
             }  
   
             inline void InterpolateOneStep_Mono(sample_t* pSrc, int& i, float& effective_volume, float& pitch,  biquad_param_t& bq_base, biquad_param_t& bq_main) {  
                 int   pos_int   = RTMath::DoubleToInt(this->Pos);  // integer position  
                 float pos_fract = this->Pos - pos_int;             // fractional part of position  
   
                 #if USE_LINEAR_INTERPOLATION  
                     float sample_point  = effective_volume * (pSrc[pos_int] + pos_fract * (pSrc[pos_int+1] - pSrc[pos_int]));  
                 #else // polynomial interpolation  
                     float xm1 = pSrc[pos_int];  
                     float x0  = pSrc[pos_int+1];  
                     float x1  = pSrc[pos_int+2];  
                     float x2  = pSrc[pos_int+3];  
                     float a   = (3.0f * (x0 - x1) - xm1 + x2) * 0.5f;  
                     float b   = 2.0f * x1 + xm1 - (5.0f * x0 + x2) * 0.5f;  
                     float c   = (x1 - xm1) * 0.5f;  
                     float sample_point = effective_volume * ((((a * pos_fract) + b) * pos_fract + c) * pos_fract + x0);  
                 #endif // USE_LINEAR_INTERPOLATION  
   
                 #if ENABLE_FILTER  
                     sample_point = this->FilterLeft.Apply(&bq_base, &bq_main, sample_point);  
                 #endif // ENABLE_FILTER  
102    
103                  pOutputLeft[i]    += sample_point;              EngineChannel* GetGigEngineChannel();
                 pOutputRight[i++] += sample_point;  
104    
105                  this->Pos += pitch;          protected:
106                virtual uint8_t CrossfadeAttenuation(uint8_t& CrossfadeControllerValue) {
107                    uint8_t c = std::max(CrossfadeControllerValue, pRegion->AttenuationControllerThreshold);
108                    c = (!pRegion->Crossfade.out_end) ? c /* 0,0,0,0 means no crossfade defined */
109                              : (c < pRegion->Crossfade.in_end) ?
110                                    ((c <= pRegion->Crossfade.in_start) ? 0
111                                    : 127 * (c - pRegion->Crossfade.in_start) / (pRegion->Crossfade.in_end - pRegion->Crossfade.in_start))
112                              : (c <= pRegion->Crossfade.out_start) ? 127
113                              : (c < pRegion->Crossfade.out_end) ? 127 * (pRegion->Crossfade.out_end - c) / (pRegion->Crossfade.out_end - pRegion->Crossfade.out_start)
114                              : 0;
115                    return pRegion->InvertAttenuationController ? 127 - c : c;
116              }              }
117    
118              inline float Constrain(float ValueToCheck, float Min, float Max) {              inline float Constrain(float ValueToCheck, float Min, float Max) {

Legend:
Removed from v.203  
changed lines
  Added in v.2382

  ViewVC Help
Powered by ViewVC