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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 330 - (hide annotations) (download) (as text)
Wed Dec 29 01:14:15 2004 UTC (19 years, 3 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 11799 byte(s)
* fixed endless loop at the end of sample playback
  (patch by Andreas Persson)

1 schoenebeck 53 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5 schoenebeck 56 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 schoenebeck 53 * *
7     * This program is free software; you can redistribute it and/or modify *
8     * it under the terms of the GNU General Public License as published by *
9     * the Free Software Foundation; either version 2 of the License, or *
10     * (at your option) any later version. *
11     * *
12     * This program is distributed in the hope that it will be useful, *
13     * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15     * GNU General Public License for more details. *
16     * *
17     * You should have received a copy of the GNU General Public License *
18     * along with this program; if not, write to the Free Software *
19     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
20     * MA 02111-1307 USA *
21     ***************************************************************************/
22    
23     #ifndef __LS_GIG_VOICE_H__
24     #define __LS_GIG_VOICE_H__
25    
26     #include "../../common/global.h"
27    
28     #if DEBUG_HEADERS
29     # warning Voice.h included
30     #endif // DEBUG_HEADERS
31    
32     #include "../../common/RTMath.h"
33     #include "../../common/RingBuffer.h"
34 schoenebeck 273 #include "../../common/Pool.h"
35 schoenebeck 203 #include "../../drivers/audio/AudioOutputDevice.h"
36 schoenebeck 53 #include "../../lib/fileloader/libgig/gig.h"
37 schoenebeck 80 #include "../common/BiquadFilter.h"
38 schoenebeck 53 #include "Engine.h"
39     #include "Stream.h"
40     #include "DiskThread.h"
41    
42     #include "EGDecay.h"
43     #include "Filter.h"
44     #include "../common/LFO.h"
45    
46 schoenebeck 80 #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)
47 schoenebeck 53 #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
48     #define FILTER_CUTOFF_MAX 10000.0f ///< maximum cutoff frequency (10kHz)
49     #define FILTER_CUTOFF_MIN 100.0f ///< minimum cutoff frequency (100Hz)
50    
51     // Uncomment following line to override external cutoff controller
52     //#define OVERRIDE_FILTER_CUTOFF_CTRL 1 ///< set to an arbitrary MIDI control change controller (e.g. 1 for 'modulation wheel')
53    
54     // Uncomment following line to override external resonance controller
55     //#define OVERRIDE_FILTER_RES_CTRL 91 ///< set to an arbitrary MIDI control change controller (e.g. 91 for 'effect 1 depth')
56    
57     // Uncomment following line to override filter type
58     //#define OVERRIDE_FILTER_TYPE ::gig::vcf_type_lowpass ///< either ::gig::vcf_type_lowpass, ::gig::vcf_type_bandpass or ::gig::vcf_type_highpass
59    
60     namespace LinuxSampler { namespace gig {
61    
62     class Engine;
63     class EGADSR;
64     class VCAManipulator;
65     class VCFCManipulator;
66     class VCOManipulator;
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     /** Gig Voice
76     *
77     * Renders a voice for the Gigasampler format.
78     */
79     class Voice {
80     public:
81 schoenebeck 242 // Types
82     enum type_t {
83     type_normal,
84     type_release_trigger_required, ///< If the key of this voice will be released, it causes a release triggered voice to be spawned
85     type_release_trigger ///< Release triggered voice which cannot be killed by releasing its key
86     };
87    
88 schoenebeck 53 // Attributes
89 schoenebeck 242 type_t Type; ///< Voice Type
90 schoenebeck 53 int MIDIKey; ///< MIDI key number of the key that triggered the voice
91 schoenebeck 239 uint KeyGroup;
92 schoenebeck 53 DiskThread* pDiskThread; ///< Pointer to the disk thread, to be able to order a disk stream and later to delete the stream again
93 schoenebeck 285 RTList<Voice>::Iterator itChildVoice; ///< Points to the next layer voice (if any). This field is currently only used by the voice stealing algorithm.
94 schoenebeck 53
95     // Methods
96     Voice();
97     ~Voice();
98 schoenebeck 271 void Kill(Pool<Event>::Iterator& itKillEvent);
99 schoenebeck 53 void Render(uint Samples);
100     void Reset();
101     void SetOutput(AudioOutputDevice* pAudioOutputDevice);
102     void SetEngine(Engine* pEngine);
103 schoenebeck 287 int Trigger(Pool<Event>::Iterator& itNoteOnEvent, int PitchBend, ::gig::Instrument* pInstrument, int iLayer, bool ReleaseTriggerVoice, bool VoiceStealing);
104 schoenebeck 285 inline bool IsActive() { return PlaybackState; }
105 schoenebeck 319 //private:
106 schoenebeck 53 // Types
107     enum playback_state_t {
108 schoenebeck 285 playback_state_end = 0,
109     playback_state_ram = 1,
110     playback_state_disk = 2
111 schoenebeck 53 };
112    
113     // Attributes
114     gig::Engine* pEngine; ///< Pointer to the sampler engine, to be able to access the event lists.
115     float Volume; ///< Volume level of the voice
116 schoenebeck 245 float PanLeft;
117     float PanRight;
118 schoenebeck 236 float CrossfadeVolume; ///< Current attenuation level caused by a crossfade (only if a crossfade is defined of course)
119 schoenebeck 53 double Pos; ///< Current playback position in sample
120 schoenebeck 319 float PitchBase; ///< Basic pitch depth, stays the same for the whole life time of the voice
121     float PitchBend; ///< Current pitch value of the pitchbend wheel
122 schoenebeck 53 ::gig::Sample* pSample; ///< Pointer to the sample to be played back
123     ::gig::Region* pRegion; ///< Pointer to the articulation information of the respective keyboard region of this voice
124 schoenebeck 236 ::gig::DimensionRegion* pDimRgn; ///< Pointer to the articulation information of current dimension region of this voice
125 schoenebeck 53 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
126     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
127     Stream::reference_t DiskStreamRef; ///< Reference / link to the disk stream
128 schoenebeck 330 int RealSampleWordsLeftToRead; ///< Number of samples left to read, not including the silence added for the interpolator
129 schoenebeck 53 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.
130     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
131 schoenebeck 319 uint LoopCyclesLeft; ///< In case there is a RAMLoop and it's not an endless loop; reflects number of loop cycles left to be passed
132 schoenebeck 53 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
133     EGADSR* pEG1; ///< Envelope Generator 1 (Amplification)
134     EGADSR* pEG2; ///< Envelope Generator 2 (Filter cutoff frequency)
135     EGDecay* pEG3; ///< Envelope Generator 3 (Pitch)
136     Filter FilterLeft;
137     Filter FilterRight;
138     midi_ctrl VCFCutoffCtrl;
139     midi_ctrl VCFResonanceCtrl;
140     int FilterUpdateCounter; ///< Used to update filter parameters all FILTER_UPDATE_PERIOD samples
141     static const float FILTER_CUTOFF_COEFF;
142 schoenebeck 80 static const int FILTER_UPDATE_MASK;
143 schoenebeck 53 VCAManipulator* pVCAManipulator;
144     VCFCManipulator* pVCFCManipulator;
145     VCOManipulator* pVCOManipulator;
146     LFO<gig::VCAManipulator>* pLFO1; ///< Low Frequency Oscillator 1 (Amplification)
147     LFO<gig::VCFCManipulator>* pLFO2; ///< Low Frequency Oscillator 2 (Filter cutoff frequency)
148     LFO<gig::VCOManipulator>* pLFO3; ///< Low Frequency Oscillator 3 (Pitch)
149 schoenebeck 271 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).
150 schoenebeck 319 //public: // FIXME: just made public for debugging (sanity check in Engine::RenderAudio()), should be changed to private before the final release
151 schoenebeck 271 Pool<Event>::Iterator itKillEvent; ///< Event which caused this voice to be killed
152 schoenebeck 319 //private:
153     int SynthesisMode;
154 schoenebeck 53
155     // Static Methods
156     static float CalculateFilterCutoffCoeff();
157 schoenebeck 80 static int CalculateFilterUpdateMask();
158 schoenebeck 53
159     // Methods
160 schoenebeck 319 void KillImmediately();
161     void ProcessEvents(uint Samples);
162     void CalculateBiquadParameters(uint Samples);
163 senkov 325 void Synthesize(uint Samples, sample_t* pSrc, uint Skip);
164 schoenebeck 245
165 schoenebeck 236 inline float CrossfadeAttenuation(uint8_t& CrossfadeControllerValue) {
166     return (CrossfadeControllerValue <= pDimRgn->Crossfade.in_start) ? 0.0f
167     : (CrossfadeControllerValue < pDimRgn->Crossfade.in_end) ? float(CrossfadeControllerValue - pDimRgn->Crossfade.in_start) / float(pDimRgn->Crossfade.in_end - pDimRgn->Crossfade.in_start)
168     : (CrossfadeControllerValue <= pDimRgn->Crossfade.out_start) ? 1.0f
169     : (CrossfadeControllerValue < pDimRgn->Crossfade.out_end) ? float(CrossfadeControllerValue - pDimRgn->Crossfade.out_start) / float(pDimRgn->Crossfade.out_end - pDimRgn->Crossfade.out_start)
170     : 0.0f;
171     }
172    
173 schoenebeck 53 inline float Constrain(float ValueToCheck, float Min, float Max) {
174     if (ValueToCheck > Max) ValueToCheck = Max;
175     else if (ValueToCheck < Min) ValueToCheck = Min;
176     return ValueToCheck;
177     }
178     };
179    
180     }} // namespace LinuxSampler::gig
181    
182     #endif // __LS_GIG_VOICE_H__

  ViewVC Help
Powered by ViewVC