1 |
schoenebeck |
320 |
/*************************************************************************** |
2 |
|
|
* * |
3 |
|
|
* LinuxSampler - modular, streaming capable sampler * |
4 |
|
|
* * |
5 |
|
|
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
6 |
schoenebeck |
411 |
* Copyright (C) 2005 Christian Schoenebeck * |
7 |
schoenebeck |
320 |
* * |
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 |
schoenebeck |
328 |
#include "../../common/global.h" |
28 |
schoenebeck |
320 |
#include "../../common/RTMath.h" |
29 |
|
|
#include "../common/Resampler.h" |
30 |
|
|
#include "../common/BiquadFilter.h" |
31 |
|
|
#include "Filter.h" |
32 |
schoenebeck |
770 |
#include "SynthesisParam.h" |
33 |
schoenebeck |
320 |
|
34 |
schoenebeck |
738 |
#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 |
schoenebeck |
770 |
#define SYNTHESIS_MODE_SET_PROFILING(iMode,bVal) if (bVal) iMode |= 0x20; else iMode &= ~0x20 /* (un)set mode bit 5 */ |
40 |
schoenebeck |
320 |
|
41 |
schoenebeck |
738 |
#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 |
schoenebeck |
320 |
namespace LinuxSampler { namespace gig { |
48 |
|
|
|
49 |
schoenebeck |
770 |
typedef void SynthesizeFragment_Fn(SynthesisParam* pFinalParam, Loop* pLoop); |
50 |
schoenebeck |
320 |
|
51 |
|
|
void* GetSynthesisFunction(const int SynthesisMode); |
52 |
schoenebeck |
770 |
void RunSynthesisFunction(const int SynthesisMode, SynthesisParam* pFinalParam, Loop* pLoop); |
53 |
schoenebeck |
320 |
|
54 |
|
|
enum channels_t { |
55 |
|
|
MONO, |
56 |
|
|
STEREO |
57 |
|
|
}; |
58 |
|
|
|
59 |
schoenebeck |
563 |
/** @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 |
schoenebeck |
770 |
template<channels_t CHANNELS, bool DOLOOP, bool USEFILTER, bool INTERPOLATE> |
66 |
|
|
class Synthesizer : public __RTMath<CPP>, public LinuxSampler::Resampler<INTERPOLATE> { |
67 |
persson |
497 |
|
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 |
schoenebeck |
770 |
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 |
persson |
497 |
|
79 |
schoenebeck |
320 |
public: |
80 |
|
|
//protected: |
81 |
|
|
|
82 |
schoenebeck |
770 |
static void SynthesizeSubFragment(SynthesisParam* pFinalParam, Loop* pLoop) { |
83 |
schoenebeck |
320 |
if (DOLOOP) { |
84 |
schoenebeck |
770 |
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 |
schoenebeck |
320 |
// render loop (loop count limited) |
89 |
schoenebeck |
770 |
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 |
schoenebeck |
320 |
} |
93 |
|
|
// render on without loop |
94 |
schoenebeck |
770 |
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 |
schoenebeck |
320 |
} |
100 |
|
|
} |
101 |
schoenebeck |
770 |
} else { // no looping |
102 |
|
|
SynthesizeSubSubFragment(pFinalParam, pFinalParam->uiToGo); |
103 |
schoenebeck |
320 |
} |
104 |
|
|
} |
105 |
|
|
|
106 |
schoenebeck |
563 |
/** |
107 |
|
|
* Returns the difference to the sample's loop end. |
108 |
|
|
*/ |
109 |
schoenebeck |
320 |
inline static int DiffToLoopEnd(const float& LoopEnd, const void* Pos, const float& Pitch) { |
110 |
schoenebeck |
770 |
return uint((LoopEnd - *((double *)Pos)) / Pitch); |
111 |
schoenebeck |
320 |
} |
112 |
|
|
|
113 |
schoenebeck |
770 |
#if 0 |
114 |
schoenebeck |
738 |
//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 |
schoenebeck |
770 |
//TODO: we can easily eliminate the branch here |
117 |
|
|
if (Pos < LoopEnd) return 0; |
118 |
|
|
Pos = (Pos - LoopEnd) % LoopSize + LoopStart; |
119 |
|
|
return 1; |
120 |
schoenebeck |
738 |
} |
121 |
schoenebeck |
770 |
#endif |
122 |
schoenebeck |
738 |
|
123 |
schoenebeck |
563 |
/** |
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 |
schoenebeck |
320 |
inline static int WrapLoop(const float& LoopStart, const float& LoopSize, const float& LoopEnd, void* vPos) { |
131 |
schoenebeck |
770 |
double * Pos = (double *)vPos; |
132 |
|
|
if (*Pos < LoopEnd) return 0; |
133 |
|
|
*Pos = fmod(*Pos - LoopEnd, LoopSize) + LoopStart; |
134 |
|
|
return 1; |
135 |
schoenebeck |
320 |
} |
136 |
|
|
|
137 |
schoenebeck |
770 |
static void SynthesizeSubSubFragment(SynthesisParam* pFinalParam, uint uiToGo) { |
138 |
persson |
830 |
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 |
schoenebeck |
770 |
switch (CHANNELS) { |
148 |
|
|
case MONO: { |
149 |
persson |
830 |
float samplePoint; |
150 |
schoenebeck |
770 |
if (INTERPOLATE) { |
151 |
persson |
830 |
double dPos = pFinalParam->dPos; |
152 |
|
|
float fPitch = pFinalParam->fFinalPitch; |
153 |
schoenebeck |
770 |
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 |
persson |
830 |
#ifdef CONFIG_INTERPOLATE_VOLUME |
159 |
|
|
fVolumeL += fDeltaL; |
160 |
|
|
fVolumeR += fDeltaR; |
161 |
|
|
#endif |
162 |
schoenebeck |
770 |
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 |
persson |
830 |
#ifdef CONFIG_INTERPOLATE_VOLUME |
169 |
|
|
fVolumeL += fDeltaL; |
170 |
|
|
fVolumeR += fDeltaR; |
171 |
|
|
#endif |
172 |
schoenebeck |
770 |
pOutL[i] += samplePoint * fVolumeL; |
173 |
|
|
pOutR[i] += samplePoint * fVolumeR; |
174 |
|
|
} |
175 |
schoenebeck |
320 |
} |
176 |
persson |
830 |
pFinalParam->dPos = dPos; |
177 |
schoenebeck |
770 |
} else { // no interpolation |
178 |
persson |
830 |
int pos_offset = (int) pFinalParam->dPos; |
179 |
schoenebeck |
770 |
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 |
persson |
830 |
#ifdef CONFIG_INTERPOLATE_VOLUME |
185 |
|
|
fVolumeL += fDeltaL; |
186 |
|
|
fVolumeR += fDeltaR; |
187 |
|
|
#endif |
188 |
schoenebeck |
770 |
pOutL[i] += samplePoint * fVolumeL; |
189 |
|
|
pOutR[i] += samplePoint * fVolumeR; |
190 |
schoenebeck |
320 |
} |
191 |
schoenebeck |
770 |
} else { // no filter needed |
192 |
|
|
for (int i = 0; i < uiToGo; ++i) { |
193 |
|
|
samplePoint = pSrc[i + pos_offset]; |
194 |
persson |
830 |
#ifdef CONFIG_INTERPOLATE_VOLUME |
195 |
|
|
fVolumeL += fDeltaL; |
196 |
|
|
fVolumeR += fDeltaR; |
197 |
|
|
#endif |
198 |
schoenebeck |
770 |
pOutL[i] += samplePoint * fVolumeL; |
199 |
|
|
pOutR[i] += samplePoint * fVolumeR; |
200 |
|
|
} |
201 |
schoenebeck |
320 |
} |
202 |
persson |
830 |
pFinalParam->dPos += uiToGo; |
203 |
schoenebeck |
320 |
} |
204 |
|
|
break; |
205 |
|
|
} |
206 |
schoenebeck |
770 |
case STEREO: { |
207 |
persson |
830 |
stereo_sample_t samplePoint; |
208 |
schoenebeck |
770 |
if (INTERPOLATE) { |
209 |
persson |
830 |
double dPos = pFinalParam->dPos; |
210 |
|
|
float fPitch = pFinalParam->fFinalPitch; |
211 |
schoenebeck |
770 |
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 |
persson |
830 |
#ifdef CONFIG_INTERPOLATE_VOLUME |
219 |
|
|
fVolumeL += fDeltaL; |
220 |
|
|
fVolumeR += fDeltaR; |
221 |
|
|
#endif |
222 |
schoenebeck |
770 |
pOutL[i] += samplePoint.left * fVolumeL; |
223 |
|
|
pOutR[i] += samplePoint.right * fVolumeR; |
224 |
schoenebeck |
320 |
} |
225 |
schoenebeck |
770 |
} else { // no filter needed |
226 |
|
|
for (int i = 0; i < uiToGo; ++i) { |
227 |
|
|
samplePoint = Interpolate1StepStereoCPP(pSrc, &dPos, fPitch); |
228 |
persson |
830 |
#ifdef CONFIG_INTERPOLATE_VOLUME |
229 |
|
|
fVolumeL += fDeltaL; |
230 |
|
|
fVolumeR += fDeltaR; |
231 |
|
|
#endif |
232 |
schoenebeck |
770 |
pOutL[i] += samplePoint.left * fVolumeL; |
233 |
|
|
pOutR[i] += samplePoint.right * fVolumeR; |
234 |
|
|
} |
235 |
schoenebeck |
320 |
} |
236 |
persson |
830 |
pFinalParam->dPos = dPos; |
237 |
schoenebeck |
770 |
} else { // no interpolation |
238 |
persson |
830 |
int pos_offset = ((int) pFinalParam->dPos) << 1; |
239 |
schoenebeck |
770 |
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 |
persson |
830 |
#ifdef CONFIG_INTERPOLATE_VOLUME |
248 |
|
|
fVolumeL += fDeltaL; |
249 |
|
|
fVolumeR += fDeltaR; |
250 |
|
|
#endif |
251 |
schoenebeck |
770 |
pOutL[i] += samplePoint.left * fVolumeL; |
252 |
|
|
pOutR[i] += samplePoint.right * fVolumeR; |
253 |
schoenebeck |
320 |
} |
254 |
schoenebeck |
770 |
} 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 |
persson |
830 |
#ifdef CONFIG_INTERPOLATE_VOLUME |
259 |
|
|
fVolumeL += fDeltaL; |
260 |
|
|
fVolumeR += fDeltaR; |
261 |
|
|
#endif |
262 |
schoenebeck |
770 |
pOutL[i] += samplePoint.left * fVolumeL; |
263 |
|
|
pOutR[i] += samplePoint.right * fVolumeR; |
264 |
|
|
} |
265 |
schoenebeck |
320 |
} |
266 |
persson |
830 |
pFinalParam->dPos += uiToGo; |
267 |
schoenebeck |
320 |
} |
268 |
schoenebeck |
770 |
break; |
269 |
schoenebeck |
320 |
} |
270 |
|
|
} |
271 |
persson |
830 |
pFinalParam->fFinalVolumeLeft = fVolumeL; |
272 |
|
|
pFinalParam->fFinalVolumeRight = fVolumeR; |
273 |
schoenebeck |
770 |
pFinalParam->pOutRight += uiToGo; |
274 |
|
|
pFinalParam->pOutLeft += uiToGo; |
275 |
|
|
pFinalParam->uiToGo -= uiToGo; |
276 |
schoenebeck |
320 |
} |
277 |
|
|
}; |
278 |
|
|
|
279 |
|
|
}} // namespace LinuxSampler::gig |
280 |
|
|
|
281 |
|
|
#endif // __LS_GIG_SYNTHESIZER_H__ |