/[svn]/linuxsampler/trunk/src/engines/common/AbstractVoice.h
ViewVC logotype

Annotation of /linuxsampler/trunk/src/engines/common/AbstractVoice.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2015 - (hide annotations) (download) (as text)
Sun Oct 25 22:22:52 2009 UTC (14 years, 6 months ago) by iliev
File MIME type: text/x-c++hdr
File size: 13111 byte(s)
* Refactoring: moved the independent code from gig::Voice to base classes
* SoundFont format engine: implemented EG1 & EG2

1 iliev 2015 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5     * Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck *
6     * Copyright (C) 2005-2009 Christian Schoenebeck *
7     * Copyright (C) 2009 Grigor Iliev *
8     * *
9     * 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 *
11     * the Free Software Foundation; either version 2 of the License, or *
12     * (at your option) any later version. *
13     * *
14     * This program is distributed in the hope that it will be useful, *
15     * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17     * GNU General Public License for more details. *
18     * *
19     * You should have received a copy of the GNU General Public License *
20     * along with this program; if not, write to the Free Software *
21     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
22     * MA 02111-1307 USA *
23     ***************************************************************************/
24    
25     #ifndef __LS_ABSTRACTVOICE_H__
26     #define __LS_ABSTRACTVOICE_H__
27    
28     #include "Voice.h"
29    
30     #include "../../common/global_private.h"
31     #include "../AbstractEngineChannel.h"
32     #include "../common/LFOBase.h"
33     #include "../EngineBase.h"
34     #include "../gig/EGADSR.h"
35     #include "../gig/EGDecay.h"
36     #include "../gig/SmoothVolume.h"
37     #include "../gig/Synthesizer.h"
38     #include "../gig/Profiler.h"
39    
40     // include the appropriate (unsigned) triangle LFO implementation
41     #if CONFIG_UNSIGNED_TRIANG_ALGO == INT_MATH_SOLUTION
42     # include "../common/LFOTriangleIntMath.h"
43     #elif CONFIG_UNSIGNED_TRIANG_ALGO == INT_ABS_MATH_SOLUTION
44     # include "../common/LFOTriangleIntAbsMath.h"
45     #elif CONFIG_UNSIGNED_TRIANG_ALGO == DI_HARMONIC_SOLUTION
46     # include "../common/LFOTriangleDiHarmonic.h"
47     #else
48     # error "Unknown or no (unsigned) triangle LFO implementation selected!"
49     #endif
50    
51     // include the appropriate (signed) triangle LFO implementation
52     #if CONFIG_SIGNED_TRIANG_ALGO == INT_MATH_SOLUTION
53     # include "../common/LFOTriangleIntMath.h"
54     #elif CONFIG_SIGNED_TRIANG_ALGO == INT_ABS_MATH_SOLUTION
55     # include "../common/LFOTriangleIntAbsMath.h"
56     #elif CONFIG_SIGNED_TRIANG_ALGO == DI_HARMONIC_SOLUTION
57     # include "../common/LFOTriangleDiHarmonic.h"
58     #else
59     # error "Unknown or no (signed) triangle LFO implementation selected!"
60     #endif
61    
62     namespace LinuxSampler {
63    
64     #if CONFIG_UNSIGNED_TRIANG_ALGO == INT_MATH_SOLUTION
65     typedef LFOTriangleIntMath<range_unsigned> LFOUnsigned;
66     #elif CONFIG_UNSIGNED_TRIANG_ALGO == INT_ABS_MATH_SOLUTION
67     typedef LFOTriangleIntAbsMath<range_unsigned> LFOUnsigned;
68     #elif CONFIG_UNSIGNED_TRIANG_ALGO == DI_HARMONIC_SOLUTION
69     typedef LFOTriangleDiHarmonic<range_unsigned> LFOUnsigned;
70     #endif
71    
72     #if CONFIG_SIGNED_TRIANG_ALGO == INT_MATH_SOLUTION
73     typedef LFOTriangleIntMath<range_signed> LFOSigned;
74     #elif CONFIG_SIGNED_TRIANG_ALGO == INT_ABS_MATH_SOLUTION
75     typedef LFOTriangleIntAbsMath<range_signed> LFOSigned;
76     #elif CONFIG_SIGNED_TRIANG_ALGO == DI_HARMONIC_SOLUTION
77     typedef LFOTriangleDiHarmonic<range_signed> LFOSigned;
78     #endif
79    
80     class AbstractVoice : public Voice {
81     public:
82     type_t Type; ///< Voice Type
83     int MIDIKey; ///< MIDI key number of the key that triggered the voice
84     uint KeyGroup;
85    
86     AbstractVoice();
87     virtual ~AbstractVoice();
88    
89     inline bool IsActive() { return PlaybackState; }
90     inline bool IsStealable() { return !itKillEvent && PlaybackState >= playback_state_ram; }
91    
92     virtual void Reset();
93    
94     virtual int Trigger (
95     AbstractEngineChannel* pEngineChannel,
96     Pool<Event>::Iterator& itNoteOnEvent,
97     int PitchBend,
98     type_t VoiceType,
99     int iKeyGroup
100     );
101    
102     virtual void Synthesize(uint Samples, sample_t* pSrc, uint Skip);
103    
104     void processCCEvents(RTList<Event>::Iterator& itEvent, uint End);
105     void processPitchEvent(RTList<Event>::Iterator& itEvent);
106     void processResonanceEvent(RTList<Event>::Iterator& itEvent);
107     void processTransitionEvents(RTList<Event>::Iterator& itEvent, uint End);
108     void UpdatePortamentoPos(Pool<Event>::Iterator& itNoteOffEvent);
109     void Kill(Pool<Event>::Iterator& itKillEvent);
110    
111     bool Orphan; ///< true if this voice is playing a sample from an instrument that is unloaded. When the voice dies, the sample (and dimension region) will be handed back to the instrument resource manager.
112     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
113     Stream::reference_t DiskStreamRef; ///< Reference / link to the disk stream
114    
115     template<class TV, class TRR, class TR, class TD, class TIM, class TI> friend class EngineBase;
116    
117     protected:
118     SampleInfo SmplInfo;
119     RegionInfo RgnInfo;
120     InstrumentInfo InstrInfo;
121     AbstractEngineChannel* pEngineChannel;
122    
123     double Pos; ///< Current playback position in sample
124     PitchInfo Pitch;
125     float CutoffBase; ///< Cutoff frequency before control change, EG and LFO are applied
126     float VolumeLeft; ///< Left channel volume. This factor is calculated when the voice is triggered and doesn't change after that.
127     float VolumeRight; ///< Right channel volume. This factor is calculated when the voice is triggered and doesn't change after that.
128     gig::SmoothVolume CrossfadeSmoother; ///< Crossfade volume, updated by crossfade CC events
129     gig::SmoothVolume VolumeSmoother; ///< Volume, updated by CC 7 (volume) events
130     gig::SmoothVolume PanLeftSmoother; ///< Left channel volume, updated by CC 10 (pan) events
131     gig::SmoothVolume PanRightSmoother; ///< Right channel volume, updated by CC 10 (pan) events
132     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
133     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
134     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.
135     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
136     gig::EGADSR EG1; ///< Envelope Generator 1 (Amplification)
137     gig::EGADSR EG2; ///< Envelope Generator 2 (Filter cutoff frequency)
138     gig::EGDecay EG3; ///< Envelope Generator 3 (Pitch)
139     midi_ctrl VCFCutoffCtrl;
140     midi_ctrl VCFResonanceCtrl;
141     LFOUnsigned* pLFO1; ///< Low Frequency Oscillator 1 (Amplification)
142     LFOUnsigned* pLFO2; ///< Low Frequency Oscillator 2 (Filter cutoff frequency)
143     LFOSigned* pLFO3; ///< Low Frequency Oscillator 3 (Pitch)
144     bool bLFO1Enabled; ///< Should we use the Amplitude LFO for this voice?
145     bool bLFO2Enabled; ///< Should we use the Filter Cutoff LFO for this voice?
146     bool bLFO3Enabled; ///< Should we use the Pitch LFO for this voice?
147     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).
148     Pool<Event>::Iterator itKillEvent; ///< Event which caused this voice to be killed
149     int SynthesisMode;
150     float fFinalCutoff;
151     float fFinalResonance;
152     gig::SynthesisParam finalSynthesisParameters;
153     gig::Loop loop;
154    
155    
156     virtual AbstractEngine* GetEngine() = 0;
157     virtual SampleInfo GetSampleInfo() = 0;
158     virtual RegionInfo GetRegionInfo() = 0;
159     virtual InstrumentInfo GetInstrumentInfo() = 0;
160    
161     /**
162     * Gets the sample cache size in bytes.
163     */
164     virtual unsigned long GetSampleCacheSize() = 0;
165    
166     /**
167     * Returns the correct amplitude factor for the given \a MIDIKeyVelocity.
168     * All involved parameters (VelocityResponseCurve, VelocityResponseDepth
169     * and VelocityResponseCurveScaling) involved are taken into account to
170     * calculate the amplitude factor. Use this method when a key was
171     * triggered to get the volume with which the sample should be played
172     * back.
173     *
174     * @param MIDIKeyVelocity MIDI velocity value of the triggered key (between 0 and 127)
175     * @returns amplitude factor (between 0.0 and 1.0)
176     */
177     virtual double GetVelocityAttenuation(uint8_t MIDIKeyVelocity) = 0;
178    
179     virtual double GetSampleAttenuation() = 0;
180    
181     virtual double CalculateVolume(double velocityAttenuation);
182    
183     /**
184     * Get starting crossfade volume level
185     */
186     virtual double CalculateCrossfadeVolume(uint8_t MIDIKeyVelocity) = 0;
187    
188     virtual int OrderNewStream() = 0;
189    
190     virtual PitchInfo CalculatePitchInfo(int PitchBend);
191    
192     /**
193     * Get current value of EG1 controller.
194     */
195     virtual double GetEG1ControllerValue(uint8_t MIDIKeyVelocity) = 0;
196    
197     /**
198     * Calculate influence of EG1 controller on EG1's parameters.
199     */
200     virtual EGInfo CalculateEG1ControllerInfluence(double eg1ControllerValue) = 0;
201    
202     /**
203     * Get current value of EG2 controller.
204     */
205     virtual double GetEG2ControllerValue(uint8_t MIDIKeyVelocity) = 0;
206    
207     /**
208     * Calculate influence of EG2 controller on EG2's parameters.
209     */
210     virtual EGInfo CalculateEG2ControllerInfluence(double eg2ControllerValue) = 0;
211    
212     virtual float CalculateCutoffBase(uint8_t MIDIKeyVelocity) = 0;
213     virtual float CalculateFinalCutoff(float cutoffBase) = 0;
214    
215     virtual void InitLFO1() = 0;
216     virtual void InitLFO2() = 0;
217     virtual void InitLFO3() = 0;
218    
219     virtual uint8_t GetVCFCutoffCtrl() = 0;
220     virtual uint8_t GetVCFResonanceCtrl() = 0;
221     virtual uint8_t CrossfadeAttenuation(uint8_t& CrossfadeControllerValue) = 0;
222    
223     virtual void GetFirstEventOnKey(uint8_t MIDIKey, RTList<Event>::Iterator& itEvent) = 0;
224     virtual void ProcessCCEvent(RTList<Event>::Iterator& itEvent) = 0;
225     virtual void ProcessCutoffEvent(RTList<Event>::Iterator& itEvent) = 0;
226     virtual double GetVelocityRelease(uint8_t MIDIKeyVelocity) = 0;
227    
228     virtual unsigned long GetNoteOnTime(int MIDIKey) = 0;
229     };
230     } // namespace LinuxSampler
231    
232     #endif /* __LS_ABSTRACTVOICE_H__ */

  ViewVC Help
Powered by ViewVC