/[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 770 - (show annotations) (download) (as text)
Sun Sep 11 15:56:29 2005 UTC (18 years, 7 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 17691 byte(s)
* synthesis core optimizations

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 switch (CHANNELS) {
139 case MONO: {
140 if (INTERPOLATE) {
141 if (USEFILTER) {
142 Filter filterL = pFinalParam->filterLeft;
143 sample_t* pSrc = pFinalParam->pSrc;
144 double dPos = pFinalParam->dPos;
145 float fPitch = pFinalParam->fFinalPitch;
146 float* pOutL = pFinalParam->pOutLeft;
147 float* pOutR = pFinalParam->pOutRight;
148 float fVolumeL = pFinalParam->fFinalVolumeLeft;
149 float fVolumeR = pFinalParam->fFinalVolumeRight;
150 float samplePoint;
151 for (int i = 0; i < uiToGo; ++i) {
152 samplePoint = Interpolate1StepMonoCPP(pSrc, &dPos, fPitch);
153 samplePoint = filterL.Apply(samplePoint);
154 pOutL[i] += samplePoint * fVolumeL;
155 pOutR[i] += samplePoint * fVolumeR;
156 }
157 pFinalParam->dPos = dPos;
158 } else { // no filter needed
159 sample_t* pSrc = pFinalParam->pSrc;
160 double dPos = pFinalParam->dPos;
161 float fPitch = pFinalParam->fFinalPitch;
162 float* pOutL = pFinalParam->pOutLeft;
163 float* pOutR = pFinalParam->pOutRight;
164 float fVolumeL = pFinalParam->fFinalVolumeLeft;
165 float fVolumeR = pFinalParam->fFinalVolumeRight;
166 float samplePoint;
167 for (int i = 0; i < uiToGo; ++i) {
168 samplePoint = Interpolate1StepMonoCPP(pSrc, &dPos, fPitch);
169 pOutL[i] += samplePoint * fVolumeL;
170 pOutR[i] += samplePoint * fVolumeR;
171 }
172 pFinalParam->dPos = dPos;
173 }
174 } else { // no interpolation
175 if (USEFILTER) {
176 Filter filterL = pFinalParam->filterLeft;
177 sample_t* pSrc = pFinalParam->pSrc;
178 float* pOutL = pFinalParam->pOutLeft;
179 float* pOutR = pFinalParam->pOutRight;
180 float fVolumeL = pFinalParam->fFinalVolumeLeft;
181 float fVolumeR = pFinalParam->fFinalVolumeRight;
182 int pos_offset = (int) pFinalParam->dPos;
183 float samplePoint;
184 for (int i = 0; i < uiToGo; ++i) {
185 samplePoint = pSrc[i + pos_offset];
186 samplePoint = filterL.Apply(samplePoint);
187 pOutL[i] += samplePoint * fVolumeL;
188 pOutR[i] += samplePoint * fVolumeR;
189 }
190 pFinalParam->dPos += uiToGo;
191 } else { // no filter needed
192 sample_t* pSrc = pFinalParam->pSrc;
193 float* pOutL = pFinalParam->pOutLeft;
194 float* pOutR = pFinalParam->pOutRight;
195 float fVolumeL = pFinalParam->fFinalVolumeLeft;
196 float fVolumeR = pFinalParam->fFinalVolumeRight;
197 int pos_offset = (int) pFinalParam->dPos;
198 float samplePoint;
199 for (int i = 0; i < uiToGo; ++i) {
200 samplePoint = pSrc[i + pos_offset];
201 pOutL[i] += samplePoint * fVolumeL;
202 pOutR[i] += samplePoint * fVolumeR;
203 }
204 pFinalParam->dPos += uiToGo;
205 }
206 }
207 break;
208 }
209 case STEREO: {
210 if (INTERPOLATE) {
211 if (USEFILTER) {
212 Filter filterL = pFinalParam->filterLeft;
213 Filter filterR = pFinalParam->filterRight;
214 sample_t* pSrc = pFinalParam->pSrc;
215 double dPos = pFinalParam->dPos;
216 float fPitch = pFinalParam->fFinalPitch;
217 float* pOutL = pFinalParam->pOutLeft;
218 float* pOutR = pFinalParam->pOutRight;
219 float fVolumeL = pFinalParam->fFinalVolumeLeft;
220 float fVolumeR = pFinalParam->fFinalVolumeRight;
221 stereo_sample_t samplePoint;
222 for (int i = 0; i < uiToGo; ++i) {
223 samplePoint = Interpolate1StepStereoCPP(pSrc, &dPos, fPitch);
224 samplePoint.left = filterL.Apply(samplePoint.left);
225 samplePoint.right = filterR.Apply(samplePoint.right);
226 pOutL[i] += samplePoint.left * fVolumeL;
227 pOutR[i] += samplePoint.right * fVolumeR;
228 }
229 pFinalParam->dPos = dPos;
230 } else { // no filter needed
231 sample_t* pSrc = pFinalParam->pSrc;
232 double dPos = pFinalParam->dPos;
233 float fPitch = pFinalParam->fFinalPitch;
234 float* pOutL = pFinalParam->pOutLeft;
235 float* pOutR = pFinalParam->pOutRight;
236 float fVolumeL = pFinalParam->fFinalVolumeLeft;
237 float fVolumeR = pFinalParam->fFinalVolumeRight;
238 stereo_sample_t samplePoint;
239 for (int i = 0; i < uiToGo; ++i) {
240 samplePoint = Interpolate1StepStereoCPP(pSrc, &dPos, fPitch);
241 pOutL[i] += samplePoint.left * fVolumeL;
242 pOutR[i] += samplePoint.right * fVolumeR;
243 }
244 pFinalParam->dPos = dPos;
245 }
246 } else { // no interpolation
247 if (USEFILTER) {
248 Filter filterL = pFinalParam->filterLeft;
249 Filter filterR = pFinalParam->filterRight;
250 sample_t* pSrc = pFinalParam->pSrc;
251 float* pOutL = pFinalParam->pOutLeft;
252 float* pOutR = pFinalParam->pOutRight;
253 float fVolumeL = pFinalParam->fFinalVolumeLeft;
254 float fVolumeR = pFinalParam->fFinalVolumeRight;
255 int pos_offset = ((int) pFinalParam->dPos) << 1;
256 stereo_sample_t samplePoint;
257 for (int i = 0, ii = 0; i < uiToGo; ++i, ii+=2) {
258 samplePoint.left = pSrc[ii + pos_offset];
259 samplePoint.right = pSrc[ii + pos_offset + 1];
260 samplePoint.left = filterL.Apply(samplePoint.left);
261 samplePoint.right = filterR.Apply(samplePoint.right);
262 pOutL[i] += samplePoint.left * fVolumeL;
263 pOutR[i] += samplePoint.right * fVolumeR;
264 }
265 pFinalParam->dPos += uiToGo;
266 } else { // no filter needed
267 sample_t* pSrc = pFinalParam->pSrc;
268 float* pOutL = pFinalParam->pOutLeft;
269 float* pOutR = pFinalParam->pOutRight;
270 float fVolumeL = pFinalParam->fFinalVolumeLeft;
271 float fVolumeR = pFinalParam->fFinalVolumeRight;
272 int pos_offset = ((int) pFinalParam->dPos) << 1;
273 stereo_sample_t samplePoint;
274 for (int i = 0, ii = 0; i < uiToGo; ++i, ii+=2) {
275 samplePoint.left = pSrc[ii + pos_offset];
276 samplePoint.right = pSrc[ii + pos_offset + 1];
277 pOutL[i] += samplePoint.left * fVolumeL;
278 pOutR[i] += samplePoint.right * fVolumeR;
279 }
280 pFinalParam->dPos += uiToGo;
281 }
282 }
283 break;
284 }
285 }
286 pFinalParam->pOutRight += uiToGo;
287 pFinalParam->pOutLeft += uiToGo;
288 pFinalParam->uiToGo -= uiToGo;
289 }
290 };
291
292 }} // namespace LinuxSampler::gig
293
294 #endif // __LS_GIG_SYNTHESIZER_H__

  ViewVC Help
Powered by ViewVC