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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 830 - (show annotations) (download) (as text)
Sun Jan 15 18:23:11 2006 UTC (14 years, 1 month ago) by persson
File MIME type: text/x-c++hdr
File size: 15446 byte(s)
* added linear interpolation of volume modulation inside a
  subfragment; this prevents clicks during voice stealing. Can be
  switched off with the --disable-interpolate-volume configure option.

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 * Copyright (C) 2005 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_GIG_SYNTHESIZER_H__
25 #define __LS_GIG_SYNTHESIZER_H__
26
27 #include "../../common/global.h"
28 #include "../../common/RTMath.h"
29 #include "../common/Resampler.h"
30 #include "../common/BiquadFilter.h"
31 #include "Filter.h"
32 #include "SynthesisParam.h"
33
34 #define SYNTHESIS_MODE_SET_INTERPOLATE(iMode,bVal) if (bVal) iMode |= 0x01; else iMode &= ~0x01 /* (un)set mode bit 0 */
35 #define SYNTHESIS_MODE_SET_FILTER(iMode,bVal) if (bVal) iMode |= 0x02; else iMode &= ~0x02 /* (un)set mode bit 1 */
36 #define SYNTHESIS_MODE_SET_LOOP(iMode,bVal) if (bVal) iMode |= 0x04; else iMode &= ~0x04 /* (un)set mode bit 2 */
37 #define SYNTHESIS_MODE_SET_CHANNELS(iMode,bVal) if (bVal) iMode |= 0x08; else iMode &= ~0x08 /* (un)set mode bit 3 */
38 #define SYNTHESIS_MODE_SET_IMPLEMENTATION(iMode,bVal) if (bVal) iMode |= 0x10; else iMode &= ~0x10 /* (un)set mode bit 4 */
39 #define SYNTHESIS_MODE_SET_PROFILING(iMode,bVal) if (bVal) iMode |= 0x20; else iMode &= ~0x20 /* (un)set mode bit 5 */
40
41 #define SYNTHESIS_MODE_GET_INTERPOLATE(iMode) iMode & 0x01
42 #define SYNTHESIS_MODE_GET_FILTER(iMode) iMode & 0x02
43 #define SYNTHESIS_MODE_GET_LOOP(iMode) iMode & 0x04
44 #define SYNTHESIS_MODE_GET_CHANNELS(iMode) iMode & 0x08
45 #define SYNTHESIS_MODE_GET_IMPLEMENTATION(iMode) iMode & 0x10
46
47 namespace LinuxSampler { namespace gig {
48
49 typedef void SynthesizeFragment_Fn(SynthesisParam* pFinalParam, Loop* pLoop);
50
51 void* GetSynthesisFunction(const int SynthesisMode);
52 void RunSynthesisFunction(const int SynthesisMode, SynthesisParam* pFinalParam, Loop* pLoop);
53
54 enum channels_t {
55 MONO,
56 STEREO
57 };
58
59 /** @brief Main Synthesis algorithms for the gig::Engine
60 *
61 * Implementation of the main synthesis algorithms of the Gigasampler
62 * format capable sampler engine. This means resampling / interpolation
63 * for pitching the audio signal, looping, filter and amplification.
64 */
65 template<channels_t CHANNELS, bool DOLOOP, bool USEFILTER, bool INTERPOLATE>
66 class Synthesizer : public __RTMath<CPP>, public LinuxSampler::Resampler<INTERPOLATE> {
67
68 // declarations of derived functions (see "Name lookup,
69 // templates, and accessing members of base classes" in
70 // the gcc manual for an explanation of why this is
71 // needed).
72 using __RTMath<CPP>::Mul;
73 using __RTMath<CPP>::Float;
74 //using LinuxSampler::Resampler<INTERPOLATE>::GetNextSampleMonoCPP;
75 //using LinuxSampler::Resampler<INTERPOLATE>::GetNextSampleStereoCPP;
76 using LinuxSampler::Resampler<INTERPOLATE>::Interpolate1StepMonoCPP;
77 using LinuxSampler::Resampler<INTERPOLATE>::Interpolate1StepStereoCPP;
78
79 public:
80 //protected:
81
82 static void SynthesizeSubFragment(SynthesisParam* pFinalParam, Loop* pLoop) {
83 if (DOLOOP) {
84 const float fLoopEnd = Float(pLoop->uiEnd);
85 const float fLoopStart = Float(pLoop->uiStart);
86 const float fLoopSize = Float(pLoop->uiSize);
87 if (pLoop->uiTotalCycles) {
88 // render loop (loop count limited)
89 for (; pFinalParam->uiToGo > 0 && pLoop->uiCyclesLeft; pLoop->uiCyclesLeft -= WrapLoop(fLoopStart, fLoopSize, fLoopEnd, &pFinalParam->dPos)) {
90 const uint uiToGo = Min(pFinalParam->uiToGo, DiffToLoopEnd(fLoopEnd, &pFinalParam->dPos, pFinalParam->fFinalPitch) + 1); //TODO: instead of +1 we could also round up
91 SynthesizeSubSubFragment(pFinalParam, uiToGo);
92 }
93 // render on without loop
94 SynthesizeSubSubFragment(pFinalParam, pFinalParam->uiToGo);
95 } else { // render loop (endless loop)
96 for (; pFinalParam->uiToGo > 0; WrapLoop(fLoopStart, fLoopSize, fLoopEnd, &pFinalParam->dPos)) {
97 const uint uiToGo = Min(pFinalParam->uiToGo, DiffToLoopEnd(fLoopEnd, &pFinalParam->dPos, pFinalParam->fFinalPitch) + 1); //TODO: instead of +1 we could also round up
98 SynthesizeSubSubFragment(pFinalParam, uiToGo);
99 }
100 }
101 } else { // no looping
102 SynthesizeSubSubFragment(pFinalParam, pFinalParam->uiToGo);
103 }
104 }
105
106 /**
107 * Returns the difference to the sample's loop end.
108 */
109 inline static int DiffToLoopEnd(const float& LoopEnd, const void* Pos, const float& Pitch) {
110 return uint((LoopEnd - *((double *)Pos)) / Pitch);
111 }
112
113 #if 0
114 //TODO: this method is not in use yet, it's intended to be used for pitch=x.0f where we could use integer instead of float as playback position variable
115 inline static int WrapLoop(const int& LoopStart, const int& LoopSize, const int& LoopEnd, int& Pos) {
116 //TODO: we can easily eliminate the branch here
117 if (Pos < LoopEnd) return 0;
118 Pos = (Pos - LoopEnd) % LoopSize + LoopStart;
119 return 1;
120 }
121 #endif
122
123 /**
124 * This method handles looping of the RAM playback part of the
125 * sample, thus repositioning the playback position once the
126 * loop limit was reached. Note: looping of the disk streaming
127 * part is handled by libgig (ReadAndLoop() method which will
128 * be called by the DiskThread).
129 */
130 inline static int WrapLoop(const float& LoopStart, const float& LoopSize, const float& LoopEnd, void* vPos) {
131 double * Pos = (double *)vPos;
132 if (*Pos < LoopEnd) return 0;
133 *Pos = fmod(*Pos - LoopEnd, LoopSize) + LoopStart;
134 return 1;
135 }
136
137 static void SynthesizeSubSubFragment(SynthesisParam* pFinalParam, uint uiToGo) {
138 float fVolumeL = pFinalParam->fFinalVolumeLeft;
139 float fVolumeR = pFinalParam->fFinalVolumeRight;
140 sample_t* pSrc = pFinalParam->pSrc;
141 float* pOutL = pFinalParam->pOutLeft;
142 float* pOutR = pFinalParam->pOutRight;
143 #ifdef CONFIG_INTERPOLATE_VOLUME
144 float fDeltaL = pFinalParam->fFinalVolumeDeltaLeft;
145 float fDeltaR = pFinalParam->fFinalVolumeDeltaRight;
146 #endif
147 switch (CHANNELS) {
148 case MONO: {
149 float samplePoint;
150 if (INTERPOLATE) {
151 double dPos = pFinalParam->dPos;
152 float fPitch = pFinalParam->fFinalPitch;
153 if (USEFILTER) {
154 Filter filterL = pFinalParam->filterLeft;
155 for (int i = 0; i < uiToGo; ++i) {
156 samplePoint = Interpolate1StepMonoCPP(pSrc, &dPos, fPitch);
157 samplePoint = filterL.Apply(samplePoint);
158 #ifdef CONFIG_INTERPOLATE_VOLUME
159 fVolumeL += fDeltaL;
160 fVolumeR += fDeltaR;
161 #endif
162 pOutL[i] += samplePoint * fVolumeL;
163 pOutR[i] += samplePoint * fVolumeR;
164 }
165 } else { // no filter needed
166 for (int i = 0; i < uiToGo; ++i) {
167 samplePoint = Interpolate1StepMonoCPP(pSrc, &dPos, fPitch);
168 #ifdef CONFIG_INTERPOLATE_VOLUME
169 fVolumeL += fDeltaL;
170 fVolumeR += fDeltaR;
171 #endif
172 pOutL[i] += samplePoint * fVolumeL;
173 pOutR[i] += samplePoint * fVolumeR;
174 }
175 }
176 pFinalParam->dPos = dPos;
177 } else { // no interpolation
178 int pos_offset = (int) pFinalParam->dPos;
179 if (USEFILTER) {
180 Filter filterL = pFinalParam->filterLeft;
181 for (int i = 0; i < uiToGo; ++i) {
182 samplePoint = pSrc[i + pos_offset];
183 samplePoint = filterL.Apply(samplePoint);
184 #ifdef CONFIG_INTERPOLATE_VOLUME
185 fVolumeL += fDeltaL;
186 fVolumeR += fDeltaR;
187 #endif
188 pOutL[i] += samplePoint * fVolumeL;
189 pOutR[i] += samplePoint * fVolumeR;
190 }
191 } else { // no filter needed
192 for (int i = 0; i < uiToGo; ++i) {
193 samplePoint = pSrc[i + pos_offset];
194 #ifdef CONFIG_INTERPOLATE_VOLUME
195 fVolumeL += fDeltaL;
196 fVolumeR += fDeltaR;
197 #endif
198 pOutL[i] += samplePoint * fVolumeL;
199 pOutR[i] += samplePoint * fVolumeR;
200 }
201 }
202 pFinalParam->dPos += uiToGo;
203 }
204 break;
205 }
206 case STEREO: {
207 stereo_sample_t samplePoint;
208 if (INTERPOLATE) {
209 double dPos = pFinalParam->dPos;
210 float fPitch = pFinalParam->fFinalPitch;
211 if (USEFILTER) {
212 Filter filterL = pFinalParam->filterLeft;
213 Filter filterR = pFinalParam->filterRight;
214 for (int i = 0; i < uiToGo; ++i) {
215 samplePoint = Interpolate1StepStereoCPP(pSrc, &dPos, fPitch);
216 samplePoint.left = filterL.Apply(samplePoint.left);
217 samplePoint.right = filterR.Apply(samplePoint.right);
218 #ifdef CONFIG_INTERPOLATE_VOLUME
219 fVolumeL += fDeltaL;
220 fVolumeR += fDeltaR;
221 #endif
222 pOutL[i] += samplePoint.left * fVolumeL;
223 pOutR[i] += samplePoint.right * fVolumeR;
224 }
225 } else { // no filter needed
226 for (int i = 0; i < uiToGo; ++i) {
227 samplePoint = Interpolate1StepStereoCPP(pSrc, &dPos, fPitch);
228 #ifdef CONFIG_INTERPOLATE_VOLUME
229 fVolumeL += fDeltaL;
230 fVolumeR += fDeltaR;
231 #endif
232 pOutL[i] += samplePoint.left * fVolumeL;
233 pOutR[i] += samplePoint.right * fVolumeR;
234 }
235 }
236 pFinalParam->dPos = dPos;
237 } else { // no interpolation
238 int pos_offset = ((int) pFinalParam->dPos) << 1;
239 if (USEFILTER) {
240 Filter filterL = pFinalParam->filterLeft;
241 Filter filterR = pFinalParam->filterRight;
242 for (int i = 0, ii = 0; i < uiToGo; ++i, ii+=2) {
243 samplePoint.left = pSrc[ii + pos_offset];
244 samplePoint.right = pSrc[ii + pos_offset + 1];
245 samplePoint.left = filterL.Apply(samplePoint.left);
246 samplePoint.right = filterR.Apply(samplePoint.right);
247 #ifdef CONFIG_INTERPOLATE_VOLUME
248 fVolumeL += fDeltaL;
249 fVolumeR += fDeltaR;
250 #endif
251 pOutL[i] += samplePoint.left * fVolumeL;
252 pOutR[i] += samplePoint.right * fVolumeR;
253 }
254 } else { // no filter needed
255 for (int i = 0, ii = 0; i < uiToGo; ++i, ii+=2) {
256 samplePoint.left = pSrc[ii + pos_offset];
257 samplePoint.right = pSrc[ii + pos_offset + 1];
258 #ifdef CONFIG_INTERPOLATE_VOLUME
259 fVolumeL += fDeltaL;
260 fVolumeR += fDeltaR;
261 #endif
262 pOutL[i] += samplePoint.left * fVolumeL;
263 pOutR[i] += samplePoint.right * fVolumeR;
264 }
265 }
266 pFinalParam->dPos += uiToGo;
267 }
268 break;
269 }
270 }
271 pFinalParam->fFinalVolumeLeft = fVolumeL;
272 pFinalParam->fFinalVolumeRight = fVolumeR;
273 pFinalParam->pOutRight += uiToGo;
274 pFinalParam->pOutLeft += uiToGo;
275 pFinalParam->uiToGo -= uiToGo;
276 }
277 };
278
279 }} // namespace LinuxSampler::gig
280
281 #endif // __LS_GIG_SYNTHESIZER_H__

  ViewVC Help
Powered by ViewVC