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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 319 - (show annotations) (download) (as text)
Mon Dec 13 00:46:42 2004 UTC (19 years, 3 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 11827 byte(s)
* introduced 'synthesis mode' to reduce the amount of code and conditionals
  for the current synthesis case in the main synthesis loop
* support for MMX and SSE(1) in the core synthesis algorithms (CPU feature
  detection at runtime, only x86 so far)

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 * *
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 #include "../../common/Pool.h"
35 #include "../../drivers/audio/AudioOutputDevice.h"
36 #include "../../lib/fileloader/libgig/gig.h"
37 #include "../common/BiquadFilter.h"
38 #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 #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 #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 // 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 // Attributes
89 type_t Type; ///< Voice Type
90 int MIDIKey; ///< MIDI key number of the key that triggered the voice
91 uint KeyGroup;
92 DiskThread* pDiskThread; ///< Pointer to the disk thread, to be able to order a disk stream and later to delete the stream again
93 RTList<Voice>::Iterator itChildVoice; ///< Points to the next layer voice (if any). This field is currently only used by the voice stealing algorithm.
94
95 // Methods
96 Voice();
97 ~Voice();
98 void Kill(Pool<Event>::Iterator& itKillEvent);
99 void Render(uint Samples);
100 void Reset();
101 void SetOutput(AudioOutputDevice* pAudioOutputDevice);
102 void SetEngine(Engine* pEngine);
103 int Trigger(Pool<Event>::Iterator& itNoteOnEvent, int PitchBend, ::gig::Instrument* pInstrument, int iLayer, bool ReleaseTriggerVoice, bool VoiceStealing);
104 inline bool IsActive() { return PlaybackState; }
105 //private:
106 // Types
107 enum playback_state_t {
108 playback_state_end = 0,
109 playback_state_ram = 1,
110 playback_state_disk = 2
111 };
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 float PanLeft;
117 float PanRight;
118 float CrossfadeVolume; ///< Current attenuation level caused by a crossfade (only if a crossfade is defined of course)
119 double Pos; ///< Current playback position in sample
120 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 ::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 ::gig::DimensionRegion* pDimRgn; ///< Pointer to the articulation information of current dimension region of this voice
125 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 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.
129 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
130 uint LoopCyclesLeft; ///< In case there is a RAMLoop and it's not an endless loop; reflects number of loop cycles left to be passed
131 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
132 EGADSR* pEG1; ///< Envelope Generator 1 (Amplification)
133 EGADSR* pEG2; ///< Envelope Generator 2 (Filter cutoff frequency)
134 EGDecay* pEG3; ///< Envelope Generator 3 (Pitch)
135 Filter FilterLeft;
136 Filter FilterRight;
137 midi_ctrl VCFCutoffCtrl;
138 midi_ctrl VCFResonanceCtrl;
139 int FilterUpdateCounter; ///< Used to update filter parameters all FILTER_UPDATE_PERIOD samples
140 static const float FILTER_CUTOFF_COEFF;
141 static const int FILTER_UPDATE_MASK;
142 VCAManipulator* pVCAManipulator;
143 VCFCManipulator* pVCFCManipulator;
144 VCOManipulator* pVCOManipulator;
145 LFO<gig::VCAManipulator>* pLFO1; ///< Low Frequency Oscillator 1 (Amplification)
146 LFO<gig::VCFCManipulator>* pLFO2; ///< Low Frequency Oscillator 2 (Filter cutoff frequency)
147 LFO<gig::VCOManipulator>* pLFO3; ///< Low Frequency Oscillator 3 (Pitch)
148 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).
149 //public: // FIXME: just made public for debugging (sanity check in Engine::RenderAudio()), should be changed to private before the final release
150 Pool<Event>::Iterator itKillEvent; ///< Event which caused this voice to be killed
151 //private:
152 int SynthesisMode;
153 void* SynthesizeFragmentFnPtr; ///< Points to the respective synthesis function for the current synthesis mode.
154
155 // Static Methods
156 static float CalculateFilterCutoffCoeff();
157 static int CalculateFilterUpdateMask();
158
159 // Methods
160 void KillImmediately();
161 void ProcessEvents(uint Samples);
162 void CalculateBiquadParameters(uint Samples);
163 void UpdateSynthesisMode();
164 void Synthesize(uint Samples, sample_t* pSrc, int Skip);
165
166 inline float CrossfadeAttenuation(uint8_t& CrossfadeControllerValue) {
167 return (CrossfadeControllerValue <= pDimRgn->Crossfade.in_start) ? 0.0f
168 : (CrossfadeControllerValue < pDimRgn->Crossfade.in_end) ? float(CrossfadeControllerValue - pDimRgn->Crossfade.in_start) / float(pDimRgn->Crossfade.in_end - pDimRgn->Crossfade.in_start)
169 : (CrossfadeControllerValue <= pDimRgn->Crossfade.out_start) ? 1.0f
170 : (CrossfadeControllerValue < pDimRgn->Crossfade.out_end) ? float(CrossfadeControllerValue - pDimRgn->Crossfade.out_start) / float(pDimRgn->Crossfade.out_end - pDimRgn->Crossfade.out_start)
171 : 0.0f;
172 }
173
174 inline float Constrain(float ValueToCheck, float Min, float Max) {
175 if (ValueToCheck > Max) ValueToCheck = Max;
176 else if (ValueToCheck < Min) ValueToCheck = Min;
177 return ValueToCheck;
178 }
179 };
180
181 }} // namespace LinuxSampler::gig
182
183 #endif // __LS_GIG_VOICE_H__

  ViewVC Help
Powered by ViewVC