/[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 325 by senkov, Tue Dec 21 04:54:37 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"
 #include "../../common/RingBuffer.h"  
33  #include "../../common/Pool.h"  #include "../../common/Pool.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 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)  #include "SmoothVolume.h"
 #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:
             // Types  
             enum type_t {  
                 type_normal,  
                 type_release_trigger_required,  ///< If the key of this voice will be released, it causes a release triggered voice to be spawned  
                 type_release_trigger            ///< Release triggered voice which cannot be killed by releasing its key  
             };  
   
             // Attributes  
             type_t       Type;         ///< Voice Type  
             int          MIDIKey;      ///< MIDI key number of the key that triggered the voice  
             uint         KeyGroup;  
             DiskThread*  pDiskThread;  ///< Pointer to the disk thread, to be able to order a disk stream and later to delete the stream again  
             RTList<Voice>::Iterator itChildVoice; ///< Points to the next layer voice (if any). This field is currently only used by the voice stealing algorithm.  
   
             // Methods  
54              Voice();              Voice();
55             ~Voice();              virtual ~Voice();
             void Kill(Pool<Event>::Iterator& itKillEvent);  
             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(Pool<Event>::Iterator& itNoteOnEvent, int PitchBend, ::gig::Instrument* pInstrument, int iLayer, bool ReleaseTriggerVoice, bool VoiceStealing);              void CalculateFadeOutCoeff(float FadeOutTime, float SampleRate);
59              inline bool IsActive() { return PlaybackState; }  
60          //private:          protected:
61              // Types              virtual SampleInfo       GetSampleInfo();
62              enum playback_state_t {              virtual RegionInfo       GetRegionInfo();
63                  playback_state_end  = 0,              virtual InstrumentInfo   GetInstrumentInfo();
64                  playback_state_ram  = 1,              virtual double           CalculateCrossfadeVolume(uint8_t MIDIKeyVelocity);
65                  playback_state_disk = 2              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:
88                EGADSR EG1;
89                EGADSR EG2;
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                       PanLeft;  
             float                       PanRight;  
             float                       CrossfadeVolume;    ///< Current attenuation level caused by a crossfade (only if a crossfade is defined of course)  
             double                      Pos;                ///< Current playback position in sample  
             float                       PitchBase;          ///< Basic pitch depth, stays the same for the whole life time of the voice  
             float                       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  
             ::gig::DimensionRegion*     pDimRgn;            ///< Pointer to the articulation information of current dimension region of this voice  
             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  
             uint                        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)  
             Pool<Event>::Iterator       itTriggerEvent;      ///< 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).  
         //public: // FIXME: just made public for debugging (sanity check in Engine::RenderAudio()), should be changed to private before the final release  
             Pool<Event>::Iterator       itKillEvent;         ///< Event which caused this voice to be killed  
         //private:  
             int                         SynthesisMode;  
95    
96              // Static Methods              // Static Methods
97              static float CalculateFilterCutoffCoeff();              static float CalculateFilterCutoffCoeff();
             static int   CalculateFilterUpdateMask();  
98    
99              // Methods              // Methods
             void KillImmediately();  
100              void ProcessEvents(uint Samples);              void ProcessEvents(uint Samples);
101              void CalculateBiquadParameters(uint Samples);              void processCrossFadeEvent(RTList<Event>::Iterator& itEvent);
102              void Synthesize(uint Samples, sample_t* pSrc, uint Skip);  
103                EngineChannel* GetGigEngineChannel();
104    
105              inline float CrossfadeAttenuation(uint8_t& CrossfadeControllerValue) {          protected:
106                  return (CrossfadeControllerValue <= pDimRgn->Crossfade.in_start)  ? 0.0f              virtual uint8_t CrossfadeAttenuation(uint8_t& CrossfadeControllerValue) {
107                       : (CrossfadeControllerValue < pDimRgn->Crossfade.in_end)     ? float(CrossfadeControllerValue - pDimRgn->Crossfade.in_start) / float(pDimRgn->Crossfade.in_end - pDimRgn->Crossfade.in_start)                  uint8_t c = std::max(CrossfadeControllerValue, pRegion->AttenuationControllerThreshold);
108                       : (CrossfadeControllerValue <= pDimRgn->Crossfade.out_start) ? 1.0f                  c = (!pRegion->Crossfade.out_end) ? c /* 0,0,0,0 means no crossfade defined */
109                       : (CrossfadeControllerValue < pDimRgn->Crossfade.out_end)    ? float(CrossfadeControllerValue - pDimRgn->Crossfade.out_start) / float(pDimRgn->Crossfade.out_end - pDimRgn->Crossfade.out_start)                            : (c < pRegion->Crossfade.in_end) ?
110                       : 0.0f;                                  ((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.325  
changed lines
  Added in v.2382

  ViewVC Help
Powered by ViewVC