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

Annotation of /linuxsampler/trunk/src/engines/sf2/Voice.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2012 - (hide annotations) (download) (as text)
Fri Oct 23 17:53:17 2009 UTC (14 years, 6 months ago) by iliev
File MIME type: text/x-c++hdr
File size: 12602 byte(s)
* Refactoring: moved the independent code from
  the Gigasampler format engine to base classes
* SFZ format engine: experimental code (not usable yet)
* SoundFont format engine: experimental code (not usable yet)
* Fixed crash which may occur when MIDI key + transpose is out of range

1 iliev 2012 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5     * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6     * Copyright (C) 2005 - 2007 Christian Schoenebeck *
7     * *
8     * This program is free software; you can redistribute it and/or modify *
9     * it under the terms of the GNU General Public License as published by *
10     * the Free Software Foundation; either version 2 of the License, or *
11     * (at your option) any later version. *
12     * *
13     * This program is distributed in the hope that it will be useful, *
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16     * GNU General Public License for more details. *
17     * *
18     * You should have received a copy of the GNU General Public License *
19     * along with this program; if not, write to the Free Software *
20     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
21     * MA 02111-1307 USA *
22     ***************************************************************************/
23    
24     #ifndef __LS_SF2_VOICE_H__
25     #define __LS_SF2_VOICE_H__
26    
27     #include "../../common/global_private.h"
28    
29     #include "../../common/RTMath.h"
30     #include "../../common/Pool.h"
31     #include "../../drivers/audio/AudioOutputDevice.h"
32     #include "Stream.h"
33     #include "DiskThread.h"
34     #include "../gig/EGADSR.h"
35     #include "../gig/EGDecay.h"
36     #include "../gig/Filter.h"
37     #include "../common/LFOBase.h"
38     #include "../common/VoiceBase.h"
39     #include "../gig/SynthesisParam.h"
40     #include "../gig/SmoothVolume.h"
41    
42     // include the appropriate (unsigned) triangle LFO implementation
43     #if CONFIG_UNSIGNED_TRIANG_ALGO == INT_MATH_SOLUTION
44     # include "../common/LFOTriangleIntMath.h"
45     #elif CONFIG_UNSIGNED_TRIANG_ALGO == INT_ABS_MATH_SOLUTION
46     # include "../common/LFOTriangleIntAbsMath.h"
47     #elif CONFIG_UNSIGNED_TRIANG_ALGO == DI_HARMONIC_SOLUTION
48     # include "../common/LFOTriangleDiHarmonic.h"
49     #else
50     # error "Unknown or no (unsigned) triangle LFO implementation selected!"
51     #endif
52    
53     // include the appropriate (signed) triangle LFO implementation
54     #if CONFIG_SIGNED_TRIANG_ALGO == INT_MATH_SOLUTION
55     # include "../common/LFOTriangleIntMath.h"
56     #elif CONFIG_SIGNED_TRIANG_ALGO == INT_ABS_MATH_SOLUTION
57     # include "../common/LFOTriangleIntAbsMath.h"
58     #elif CONFIG_SIGNED_TRIANG_ALGO == DI_HARMONIC_SOLUTION
59     # include "../common/LFOTriangleDiHarmonic.h"
60     #else
61     # error "Unknown or no (signed) triangle LFO implementation selected!"
62     #endif
63    
64     namespace LinuxSampler { namespace sf2 {
65     class Engine;
66     class EngineChannel;
67    
68     /// Reflects a MIDI controller
69     struct midi_ctrl {
70     uint8_t controller; ///< MIDI control change controller number
71     uint8_t value; ///< Current MIDI controller value
72     float fvalue; ///< Transformed / effective value (e.g. volume level or filter cutoff frequency)
73     };
74    
75     #if CONFIG_UNSIGNED_TRIANG_ALGO == INT_MATH_SOLUTION
76     typedef LFOTriangleIntMath<range_unsigned> LFOUnsigned;
77     #elif CONFIG_UNSIGNED_TRIANG_ALGO == INT_ABS_MATH_SOLUTION
78     typedef LFOTriangleIntAbsMath<range_unsigned> LFOUnsigned;
79     #elif CONFIG_UNSIGNED_TRIANG_ALGO == DI_HARMONIC_SOLUTION
80     typedef LFOTriangleDiHarmonic<range_unsigned> LFOUnsigned;
81     #endif
82    
83     #if CONFIG_SIGNED_TRIANG_ALGO == INT_MATH_SOLUTION
84     typedef LFOTriangleIntMath<range_signed> LFOSigned;
85     #elif CONFIG_SIGNED_TRIANG_ALGO == INT_ABS_MATH_SOLUTION
86     typedef LFOTriangleIntAbsMath<range_signed> LFOSigned;
87     #elif CONFIG_SIGNED_TRIANG_ALGO == DI_HARMONIC_SOLUTION
88     typedef LFOTriangleDiHarmonic<range_signed> LFOSigned;
89     #endif
90    
91     /** Sf2 Voice
92     *
93     * Renders a voice for the Gigasampler format.
94     */
95     class Voice : public LinuxSampler::VoiceBaseImpl< ::sf2::Region, ::sf2::Sample> {
96     public:
97     typedef LinuxSampler::gig::EGADSR EGADSR; // TODO: remove
98     typedef LinuxSampler::gig::EGDecay EGDecay; // TODO: remove
99     typedef LinuxSampler::gig::SmoothVolume SmoothVolume; // TODO: remove
100     typedef LinuxSampler::gig::SynthesisParam SynthesisParam; // TODO: remove
101     typedef LinuxSampler::gig::Loop Loop; // TODO: remove
102    
103     // Attributes
104     type_t Type; ///< Voice Type
105     int MIDIKey; ///< MIDI key number of the key that triggered the voice
106     uint KeyGroup;
107     DiskThread* pDiskThread; ///< Pointer to the disk thread, to be able to order a disk stream and later to delete the stream again
108    
109     // Methods
110     Voice();
111     virtual ~Voice();
112     void Kill(Pool<Event>::Iterator& itKillEvent);
113     void Render(uint Samples);
114     void Reset();
115     void SetOutput(AudioOutputDevice* pAudioOutputDevice);
116     void SetEngine(LinuxSampler::Engine* pEngine);
117     int Trigger(EngineChannel* pEngineChannel, Pool<Event>::Iterator& itNoteOnEvent, int PitchBend, ::sf2::Region* pRegion, type_t VoiceType, int iKeyGroup);
118     inline bool IsActive() { return PlaybackState; }
119     inline bool IsStealable() { return !itKillEvent && PlaybackState >= playback_state_ram; }
120     void UpdatePortamentoPos(Pool<Event>::Iterator& itNoteOffEvent);
121    
122     //private:
123     // Attributes
124     EngineChannel* pEngineChannel;
125     Engine* pEngine; ///< Pointer to the sampler engine, to be able to access the event lists.
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     SmoothVolume CrossfadeSmoother; ///< Crossfade volume, updated by crossfade CC events
129     SmoothVolume VolumeSmoother; ///< Volume, updated by CC 7 (volume) events
130     SmoothVolume PanLeftSmoother; ///< Left channel volume, updated by CC 10 (pan) events
131     SmoothVolume PanRightSmoother; ///< Right channel volume, updated by CC 10 (pan) events
132     double Pos; ///< Current playback position in sample
133     float PitchBase; ///< Basic pitch depth, stays the same for the whole life time of the voice
134     float PitchBend; ///< Current pitch value of the pitchbend wheel
135     float PitchBendRange; ///< The pitch range of the pitchbend wheel, value is in cents / 8192
136     float CutoffBase; ///< Cutoff frequency before control change, EG and LFO are applied
137     ::sf2::Sample* pSample; ///< Pointer to the sample to be played back
138     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.
139     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
140     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
141     Stream::reference_t DiskStreamRef; ///< Reference / link to the disk stream
142     int RealSampleWordsLeftToRead; ///< Number of samples left to read, not including the silence added for the interpolator
143     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.
144     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
145     //uint LoopCyclesLeft; ///< In case there is a RAMLoop and it's not an endless loop; reflects number of loop cycles left to be passed
146     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
147     EGADSR EG1; ///< Envelope Generator 1 (Amplification)
148     EGADSR EG2; ///< Envelope Generator 2 (Filter cutoff frequency)
149     EGDecay EG3; ///< Envelope Generator 3 (Pitch)
150     midi_ctrl VCFCutoffCtrl;
151     midi_ctrl VCFResonanceCtrl;
152     LFOUnsigned* pLFO1; ///< Low Frequency Oscillator 1 (Amplification)
153     LFOUnsigned* pLFO2; ///< Low Frequency Oscillator 2 (Filter cutoff frequency)
154     LFOSigned* pLFO3; ///< Low Frequency Oscillator 3 (Pitch)
155     bool bLFO1Enabled; ///< Should we use the Amplitude LFO for this voice?
156     bool bLFO2Enabled; ///< Should we use the Filter Cutoff LFO for this voice?
157     bool bLFO3Enabled; ///< Should we use the Pitch LFO for this voice?
158     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).
159     //public: // FIXME: just made public for debugging (sanity check in Engine::RenderAudio()), should be changed to private before the final release
160     Pool<Event>::Iterator itKillEvent; ///< Event which caused this voice to be killed
161     //private:
162     int SynthesisMode;
163     float fFinalCutoff;
164     float fFinalResonance;
165     SynthesisParam finalSynthesisParameters;
166     Loop loop;
167    
168     // Static Methods
169     static float CalculateFilterCutoffCoeff();
170    
171     // Methods
172     Stream::Handle KillImmediately(bool bRequestNotification = false);
173     void ProcessEvents(uint Samples);
174     void Synthesize(uint Samples, sample_t* pSrc, uint Skip);
175     void processTransitionEvents(RTList<Event>::Iterator& itEvent, uint End);
176     void processCCEvents(RTList<Event>::Iterator& itEvent, uint End);
177     void processPitchEvent(RTList<Event>::Iterator& itEvent);
178     void processCrossFadeEvent(RTList<Event>::Iterator& itEvent);
179     void processCutoffEvent(RTList<Event>::Iterator& itEvent);
180     void processResonanceEvent(RTList<Event>::Iterator& itEvent);
181    
182     inline float Constrain(float ValueToCheck, float Min, float Max) {
183     if (ValueToCheck > Max) ValueToCheck = Max;
184     else if (ValueToCheck < Min) ValueToCheck = Min;
185     return ValueToCheck;
186     }
187     };
188    
189     }} // namespace LinuxSampler::sf2
190    
191     #endif // __LS_SF2_VOICE_H__

  ViewVC Help
Powered by ViewVC