/[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 903 - (show annotations) (download) (as text)
Sat Jul 22 14:22:53 2006 UTC (17 years, 9 months ago) by persson
File MIME type: text/x-c++hdr
File size: 16000 byte(s)
* real support for 24 bit samples - samples are not truncated to 16
  bits anymore
* support for aftertouch (channel pressure, not polyphonic aftertouch)

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 "Filter.h"
31 #include "SynthesisParam.h"
32
33 #define SYNTHESIS_MODE_SET_INTERPOLATE(iMode,bVal) if (bVal) iMode |= 0x01; else iMode &= ~0x01 /* (un)set mode bit 0 */
34 #define SYNTHESIS_MODE_SET_FILTER(iMode,bVal) if (bVal) iMode |= 0x02; else iMode &= ~0x02 /* (un)set mode bit 1 */
35 #define SYNTHESIS_MODE_SET_LOOP(iMode,bVal) if (bVal) iMode |= 0x04; else iMode &= ~0x04 /* (un)set mode bit 2 */
36 #define SYNTHESIS_MODE_SET_CHANNELS(iMode,bVal) if (bVal) iMode |= 0x08; else iMode &= ~0x08 /* (un)set mode bit 3 */
37 #define SYNTHESIS_MODE_SET_BITDEPTH24(iMode,bVal) if (bVal) iMode |= 0x10; else iMode &= ~0x10 /* (un)set mode bit 4 */
38 #define SYNTHESIS_MODE_SET_IMPLEMENTATION(iMode,bVal) if (bVal) iMode |= 0x20; else iMode &= ~0x20 /* (un)set mode bit 5 */
39 #define SYNTHESIS_MODE_SET_PROFILING(iMode,bVal) if (bVal) iMode |= 0x40; else iMode &= ~0x40 /* (un)set mode bit 6 */
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_BITDEPTH24(iMode) iMode & 0x10
46 #define SYNTHESIS_MODE_GET_IMPLEMENTATION(iMode) iMode & 0x20
47
48
49 namespace LinuxSampler { namespace gig {
50
51 typedef void SynthesizeFragment_Fn(SynthesisParam* pFinalParam, Loop* pLoop);
52
53 void* GetSynthesisFunction(const int SynthesisMode);
54 void RunSynthesisFunction(const int SynthesisMode, SynthesisParam* pFinalParam, Loop* pLoop);
55
56 enum channels_t {
57 MONO,
58 STEREO
59 };
60
61 /** @brief Main Synthesis algorithms for the gig::Engine
62 *
63 * Implementation of the main synthesis algorithms of the Gigasampler
64 * format capable sampler engine. This means resampling / interpolation
65 * for pitching the audio signal, looping, filter and amplification.
66 */
67 template<channels_t CHANNELS, bool DOLOOP, bool USEFILTER, bool INTERPOLATE, bool BITDEPTH24>
68 class Synthesizer : public __RTMath<CPP>, public LinuxSampler::Resampler<INTERPOLATE,BITDEPTH24> {
69
70 // declarations of derived functions (see "Name lookup,
71 // templates, and accessing members of base classes" in
72 // the gcc manual for an explanation of why this is
73 // needed).
74 //using LinuxSampler::Resampler<INTERPOLATE>::GetNextSampleMonoCPP;
75 //using LinuxSampler::Resampler<INTERPOLATE>::GetNextSampleStereoCPP;
76 using LinuxSampler::Resampler<INTERPOLATE,BITDEPTH24>::Interpolate1StepMonoCPP;
77 using LinuxSampler::Resampler<INTERPOLATE,BITDEPTH24>::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 int getSample(sample_t* src, int pos) {
138 if (BITDEPTH24) {
139 pos *= 3;
140 unsigned char* p = (unsigned char*)src;
141 return p[pos] << 8 | p[pos + 1] << 16 | p[pos + 2] << 24;
142 } else {
143 return src[pos];
144 }
145 }
146
147 static void SynthesizeSubSubFragment(SynthesisParam* pFinalParam, uint uiToGo) {
148 float fVolumeL = pFinalParam->fFinalVolumeLeft;
149 float fVolumeR = pFinalParam->fFinalVolumeRight;
150 sample_t* pSrc = pFinalParam->pSrc;
151 float* pOutL = pFinalParam->pOutLeft;
152 float* pOutR = pFinalParam->pOutRight;
153 #ifdef CONFIG_INTERPOLATE_VOLUME
154 float fDeltaL = pFinalParam->fFinalVolumeDeltaLeft;
155 float fDeltaR = pFinalParam->fFinalVolumeDeltaRight;
156 #endif
157 switch (CHANNELS) {
158 case MONO: {
159 float samplePoint;
160 if (INTERPOLATE) {
161 double dPos = pFinalParam->dPos;
162 float fPitch = pFinalParam->fFinalPitch;
163 if (USEFILTER) {
164 Filter filterL = pFinalParam->filterLeft;
165 for (int i = 0; i < uiToGo; ++i) {
166 samplePoint = Interpolate1StepMonoCPP(pSrc, &dPos, fPitch);
167 samplePoint = filterL.Apply(samplePoint);
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 } else { // no filter needed
176 for (int i = 0; i < uiToGo; ++i) {
177 samplePoint = Interpolate1StepMonoCPP(pSrc, &dPos, fPitch);
178 #ifdef CONFIG_INTERPOLATE_VOLUME
179 fVolumeL += fDeltaL;
180 fVolumeR += fDeltaR;
181 #endif
182 pOutL[i] += samplePoint * fVolumeL;
183 pOutR[i] += samplePoint * fVolumeR;
184 }
185 }
186 pFinalParam->dPos = dPos;
187 } else { // no interpolation
188 int pos_offset = (int) pFinalParam->dPos;
189 if (USEFILTER) {
190 Filter filterL = pFinalParam->filterLeft;
191 for (int i = 0; i < uiToGo; ++i) {
192 samplePoint = getSample(pSrc, i + pos_offset);
193 samplePoint = filterL.Apply(samplePoint);
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 } else { // no filter needed
202 for (int i = 0; i < uiToGo; ++i) {
203 samplePoint = getSample(pSrc, i + pos_offset);
204 #ifdef CONFIG_INTERPOLATE_VOLUME
205 fVolumeL += fDeltaL;
206 fVolumeR += fDeltaR;
207 #endif
208 pOutL[i] += samplePoint * fVolumeL;
209 pOutR[i] += samplePoint * fVolumeR;
210 }
211 }
212 pFinalParam->dPos += uiToGo;
213 }
214 break;
215 }
216 case STEREO: {
217 stereo_sample_t samplePoint;
218 if (INTERPOLATE) {
219 double dPos = pFinalParam->dPos;
220 float fPitch = pFinalParam->fFinalPitch;
221 if (USEFILTER) {
222 Filter filterL = pFinalParam->filterLeft;
223 Filter filterR = pFinalParam->filterRight;
224 for (int i = 0; i < uiToGo; ++i) {
225 samplePoint = Interpolate1StepStereoCPP(pSrc, &dPos, fPitch);
226 samplePoint.left = filterL.Apply(samplePoint.left);
227 samplePoint.right = filterR.Apply(samplePoint.right);
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 } else { // no filter needed
236 for (int i = 0; i < uiToGo; ++i) {
237 samplePoint = Interpolate1StepStereoCPP(pSrc, &dPos, fPitch);
238 #ifdef CONFIG_INTERPOLATE_VOLUME
239 fVolumeL += fDeltaL;
240 fVolumeR += fDeltaR;
241 #endif
242 pOutL[i] += samplePoint.left * fVolumeL;
243 pOutR[i] += samplePoint.right * fVolumeR;
244 }
245 }
246 pFinalParam->dPos = dPos;
247 } else { // no interpolation
248 int pos_offset = ((int) pFinalParam->dPos) << 1;
249 if (USEFILTER) {
250 Filter filterL = pFinalParam->filterLeft;
251 Filter filterR = pFinalParam->filterRight;
252 for (int i = 0, ii = 0; i < uiToGo; ++i, ii+=2) {
253 samplePoint.left = getSample(pSrc, ii + pos_offset);
254 samplePoint.right = getSample(pSrc, ii + pos_offset + 1);
255 samplePoint.left = filterL.Apply(samplePoint.left);
256 samplePoint.right = filterR.Apply(samplePoint.right);
257 #ifdef CONFIG_INTERPOLATE_VOLUME
258 fVolumeL += fDeltaL;
259 fVolumeR += fDeltaR;
260 #endif
261 pOutL[i] += samplePoint.left * fVolumeL;
262 pOutR[i] += samplePoint.right * fVolumeR;
263 }
264 } else { // no filter needed
265 for (int i = 0, ii = 0; i < uiToGo; ++i, ii+=2) {
266 samplePoint.left = getSample(pSrc, ii + pos_offset);
267 samplePoint.right = getSample(pSrc, ii + pos_offset + 1);
268 #ifdef CONFIG_INTERPOLATE_VOLUME
269 fVolumeL += fDeltaL;
270 fVolumeR += fDeltaR;
271 #endif
272 pOutL[i] += samplePoint.left * fVolumeL;
273 pOutR[i] += samplePoint.right * fVolumeR;
274 }
275 }
276 pFinalParam->dPos += uiToGo;
277 }
278 break;
279 }
280 }
281 pFinalParam->fFinalVolumeLeft = fVolumeL;
282 pFinalParam->fFinalVolumeRight = fVolumeR;
283 pFinalParam->pOutRight += uiToGo;
284 pFinalParam->pOutLeft += uiToGo;
285 pFinalParam->uiToGo -= uiToGo;
286 }
287 };
288
289 }} // namespace LinuxSampler::gig
290
291 #endif // __LS_GIG_SYNTHESIZER_H__

  ViewVC Help
Powered by ViewVC