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_SYNTHESIZER_H__ |
24 |
#define __LS_GIG_SYNTHESIZER_H__ |
25 |
|
26 |
#include "../../common/global.h" |
27 |
#include "../../common/RTMath.h" |
28 |
#include "../common/Resampler.h" |
29 |
#include "../common/BiquadFilter.h" |
30 |
#include "Filter.h" |
31 |
#include "Voice.h" |
32 |
|
33 |
#define SYNTHESIS_MODE_SET_CONSTPITCH(iMode,bVal) if (bVal) iMode |= 0x01; else iMode &= ~0x01 /* (un)set mode bit 0 */ |
34 |
#define SYNTHESIS_MODE_SET_LOOP(iMode,bVal) if (bVal) iMode |= 0x02; else iMode &= ~0x02 /* (un)set mode bit 1 */ |
35 |
#define SYNTHESIS_MODE_SET_INTERPOLATE(iMode,bVal) if (bVal) iMode |= 0x04; else iMode &= ~0x04 /* (un)set mode bit 2 */ |
36 |
#define SYNTHESIS_MODE_SET_FILTER(iMode,bVal) if (bVal) iMode |= 0x08; else iMode &= ~0x08 /* (un)set mode bit 3 */ |
37 |
#define SYNTHESIS_MODE_SET_CHANNELS(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_CONSTPITCH(iMode) iMode & 0x01 |
42 |
#define SYNTHESIS_MODE_GET_LOOP(iMode) iMode & 0x02 |
43 |
#define SYNTHESIS_MODE_GET_INTERPOLATE(iMode) iMode & 0x04 |
44 |
#define SYNTHESIS_MODE_GET_FILTER(iMode) iMode & 0x08 |
45 |
#define SYNTHESIS_MODE_GET_CHANNELS(iMode) iMode & 0x10 |
46 |
#define SYNTHESIS_MODE_GET_IMPLEMENTATION(iMode) iMode & 0x20 |
47 |
|
48 |
// that's usually gig::Voice of course, but we make it a macro so we can |
49 |
// include this code for our synthesis benchmark which uses fake data |
50 |
// structures |
51 |
#ifndef VOICE |
52 |
# define VOICE Voice |
53 |
#endif // VOICE |
54 |
|
55 |
namespace LinuxSampler { namespace gig { |
56 |
|
57 |
typedef void SynthesizeFragment_Fn(VOICE&, uint, sample_t*, uint); |
58 |
|
59 |
void* GetSynthesisFunction(const int SynthesisMode); |
60 |
void RunSynthesisFunction(const int SynthesisMode, VOICE& voice, uint Samples, sample_t* pSrc, uint Skip); |
61 |
|
62 |
enum channels_t { |
63 |
MONO, |
64 |
STEREO |
65 |
}; |
66 |
|
67 |
template<implementation_t IMPLEMENTATION, channels_t CHANNELS, bool USEFILTER, bool INTERPOLATE, bool DOLOOP, bool CONSTPITCH> |
68 |
class Synthesizer : public __RTMath<IMPLEMENTATION>, public LinuxSampler::Resampler<INTERPOLATE> { |
69 |
public: |
70 |
template<typename VOICE_T> |
71 |
inline static void SynthesizeFragment(VOICE_T& Voice, uint Samples, sample_t* pSrc, uint i) { |
72 |
if (IMPLEMENTATION == ASM_X86_MMX_SSE) { |
73 |
float fPos = (float) Voice.Pos; |
74 |
SynthesizeFragment(Voice, Samples, pSrc, i, Voice.pSample->LoopPlayCount, |
75 |
Voice.pSample->LoopStart, |
76 |
Voice.pSample->LoopEnd, |
77 |
Voice.pSample->LoopSize, |
78 |
Voice.LoopCyclesLeft, |
79 |
(void *)&fPos, |
80 |
Voice.PitchBase, |
81 |
Voice.PitchBend); |
82 |
if (INTERPOLATE) EMMS; |
83 |
Voice.Pos = (double) fPos; |
84 |
} else { |
85 |
SynthesizeFragment(Voice, Samples, pSrc, i, Voice.pSample->LoopPlayCount, |
86 |
Voice.pSample->LoopStart, |
87 |
Voice.pSample->LoopEnd, |
88 |
Voice.pSample->LoopSize, |
89 |
Voice.LoopCyclesLeft, |
90 |
(void *)&Voice.Pos, |
91 |
Voice.PitchBase, |
92 |
Voice.PitchBend); |
93 |
} |
94 |
} |
95 |
|
96 |
//protected: |
97 |
|
98 |
template<typename VOICE_T> |
99 |
inline static void SynthesizeFragment(VOICE_T& Voice, uint Samples, sample_t* pSrc, uint& i, uint& LoopPlayCount, uint LoopStart, uint LoopEnd, uint LoopSize, uint& LoopCyclesLeft, void* Pos, float& PitchBase, float& PitchBend) { |
100 |
const float loopEnd = Float(LoopEnd); |
101 |
const float PBbyPB = Mul(PitchBase, PitchBend); |
102 |
const float f_LoopStart = Float(LoopStart); |
103 |
const float f_LoopSize = Float(LoopSize); |
104 |
if (DOLOOP) { |
105 |
if (LoopPlayCount) { |
106 |
// render loop (loop count limited) |
107 |
while (i < Samples && LoopCyclesLeft) { |
108 |
if (CONSTPITCH) { |
109 |
const uint processEnd = Min(Samples, i + DiffToLoopEnd(loopEnd,Pos, PBbyPB) + 1); //TODO: instead of +1 we could also round up |
110 |
while (i < processEnd) Synthesize(Voice, Pos, pSrc, i); |
111 |
} |
112 |
else Synthesize(Voice, Pos, pSrc, i); |
113 |
if (WrapLoop(f_LoopStart, f_LoopSize, loopEnd, Pos)) LoopCyclesLeft--; |
114 |
} |
115 |
// render on without loop |
116 |
while (i < Samples) Synthesize(Voice, Pos, pSrc, i); |
117 |
} |
118 |
else { // render loop (endless loop) |
119 |
while (i < Samples) { |
120 |
if (CONSTPITCH) { |
121 |
const uint processEnd = Min(Samples, i + DiffToLoopEnd(loopEnd, Pos, PBbyPB) + 1); //TODO: instead of +1 we could also round up |
122 |
while (i < processEnd) Synthesize(Voice, Pos, pSrc, i); |
123 |
} |
124 |
else Synthesize(Voice, Pos, pSrc, i); |
125 |
WrapLoop(f_LoopStart, f_LoopSize, loopEnd, Pos); |
126 |
} |
127 |
} |
128 |
} |
129 |
else { // no looping |
130 |
while (i < Samples) { Synthesize(Voice, Pos, pSrc, i);} |
131 |
} |
132 |
} |
133 |
|
134 |
template<typename VOICE_T> |
135 |
inline static void Synthesize(VOICE_T& Voice, void* Pos, sample_t* pSrc, uint& i) { |
136 |
Synthesize(pSrc, Pos, |
137 |
Voice.pEngine->pSynthesisParameters[Event::destination_vco][i], |
138 |
Voice.pEngine->pOutputLeft, |
139 |
Voice.pEngine->pOutputRight, |
140 |
i, |
141 |
Voice.pEngine->pSynthesisParameters[Event::destination_vca], |
142 |
&Voice.PanLeft, |
143 |
&Voice.PanRight, |
144 |
Voice.FilterLeft, |
145 |
Voice.FilterRight, |
146 |
Voice.pEngine->pBasicFilterParameters[i], |
147 |
Voice.pEngine->pMainFilterParameters[i]); |
148 |
} |
149 |
|
150 |
inline static int DiffToLoopEnd(const float& LoopEnd, const void* Pos, const float& Pitch) { |
151 |
switch (IMPLEMENTATION) { |
152 |
// pure C++ implementation (thus platform independent) |
153 |
case CPP: { |
154 |
return uint((LoopEnd - *((double *)Pos)) / Pitch); |
155 |
} |
156 |
#if ARCH_X86 |
157 |
case ASM_X86_MMX_SSE: { |
158 |
int result; |
159 |
__asm__ __volatile__ ( |
160 |
"movss (%1), %%xmm0 #read loopend\n\t" |
161 |
"subss (%2), %%xmm0 #sub pos\n\t" |
162 |
"divss (%3), %%xmm0 #div by pitch\n\t" |
163 |
"cvtss2si %%xmm0, %0 #convert to int\n\t" |
164 |
: "=r" (result) /* %0 */ |
165 |
: "r" (&LoopEnd), /* %1 */ |
166 |
"r" (Pos), /* %2 */ |
167 |
"r" (&Pitch) /* %3 */ |
168 |
); |
169 |
return result; |
170 |
} |
171 |
#endif // ARCH_X86 |
172 |
} |
173 |
} |
174 |
|
175 |
inline static int WrapLoop(const float& LoopStart, const float& LoopSize, const float& LoopEnd, void* vPos) { |
176 |
switch (IMPLEMENTATION) { |
177 |
// pure C++ implementation (thus platform independent) |
178 |
case CPP: { |
179 |
double * Pos = (double *)vPos; |
180 |
if (*Pos < LoopEnd) return 0; |
181 |
*Pos = fmod(*Pos - LoopEnd, LoopSize) + LoopStart; |
182 |
return 1; |
183 |
} |
184 |
#if ARCH_X86 |
185 |
case ASM_X86_MMX_SSE: { |
186 |
int result = 0; |
187 |
__asm__ __volatile__ ( |
188 |
"movss (%2), %%xmm0 # load LoopEnd\n\t" |
189 |
"movss (%1), %%xmm1 # load Pos\n\t" |
190 |
"comiss %%xmm0, %%xmm1 # LoopEnd <> Pos\n\t" |
191 |
"jb 1f # jump if no work needs to be done\n\t" |
192 |
"movss (%3), %%xmm2 # load LoopSize\n\t" |
193 |
"subss %%xmm0, %%xmm1 # Pos - LoopEnd\n\t" |
194 |
//now the fmodf |
195 |
"movss %%xmm1, %%xmm3 # xmm3 = (Pos - LoopEnd)\n\t" |
196 |
"divss %%xmm2, %%xmm1 # (Pos - LoopEnd) / LoopSize\n\t" |
197 |
"cvttss2si %%xmm1, %%eax # convert to int\n\t" |
198 |
"cvtsi2ss %%eax, %%xmm1 # convert back to float\n\t" |
199 |
"movss (%4), %%xmm0 # load LoopStart\n\t" |
200 |
"mulss %%xmm2, %%xmm1 # LoopSize * int((Pos-LoopEnd)/LoopSize)\n\t" |
201 |
"subss %%xmm1, %%xmm3 # xmm2 = fmodf(Pos - LoopEnd, LoopSize)\n\t" |
202 |
//done with fmodf |
203 |
"addss %%xmm0, %%xmm3 # add LoopStart\n\t" |
204 |
"movss %%xmm3, (%1) # update Pos\n\t" |
205 |
"movl $1, (%0) # result = 1\n\t" |
206 |
".balign 16 \n\t" |
207 |
"1:\n\t" |
208 |
:: "r" (&result), /* %0 */ |
209 |
"r" (vPos), /* %1 */ |
210 |
"r" (&LoopEnd), /* %2 */ |
211 |
"r" (&LoopSize), /* %3 */ |
212 |
"r" (&LoopStart) /* %4 */ |
213 |
); |
214 |
return result; |
215 |
} |
216 |
#endif // ARCH_X86 |
217 |
} |
218 |
} |
219 |
|
220 |
inline static void Synthesize(sample_t* pSrc, void* Pos, float& Pitch, float* pOutL, float* pOutR, uint& i, float* Volume, float* PanL, float* PanR, Filter& FilterL, Filter& FilterR, biquad_param_t& bqBase, biquad_param_t& bqMain) { |
221 |
switch (IMPLEMENTATION) { |
222 |
// pure C++ implementation (thus platform independent) |
223 |
case CPP: { |
224 |
switch (CHANNELS) { |
225 |
case MONO: { |
226 |
float samplePoint = GetNextSampleMonoCPP(pSrc, (double *)Pos, Pitch); |
227 |
if (USEFILTER) samplePoint = FilterL.Apply(&bqBase, &bqMain, samplePoint); |
228 |
pOutL[i] += samplePoint * Volume[i] * *PanL; |
229 |
pOutR[i] += samplePoint * Volume[i] * *PanR; |
230 |
i++; |
231 |
break; |
232 |
} |
233 |
case STEREO: { |
234 |
stereo_sample_t samplePoint = GetNextSampleStereoCPP(pSrc, (double *)Pos, Pitch); |
235 |
if (USEFILTER) { |
236 |
samplePoint.left = FilterL.Apply(&bqBase, &bqMain, samplePoint.left); |
237 |
samplePoint.right = FilterR.Apply(&bqBase, &bqMain, samplePoint.right); |
238 |
} |
239 |
pOutL[i] += samplePoint.left * Volume[i] * *PanL; |
240 |
pOutR[i] += samplePoint.right * Volume[i] * *PanR; |
241 |
i++; |
242 |
break; |
243 |
} |
244 |
} |
245 |
break; |
246 |
} |
247 |
#if ARCH_X86 |
248 |
// Assembly optimization using the MMX & SSE(1) instruction set (thus only for x86) |
249 |
case ASM_X86_MMX_SSE: { |
250 |
const int ii = i & 0xfffffffc; |
251 |
i += 4; |
252 |
switch (CHANNELS) { |
253 |
case MONO: { |
254 |
GetNext4SamplesMonoMMXSSE(pSrc, (float *)Pos, Pitch); // outputs samples in xmm2 |
255 |
if (USEFILTER) { |
256 |
/* prepare filter input */ |
257 |
__asm__ __volatile__ ( |
258 |
"movaps %xmm2,%xmm0" |
259 |
); |
260 |
FilterL.Apply4StepsSSE(&bqBase, &bqMain); // xmm0 input, xmm7 output |
261 |
__asm__ __volatile__ ( |
262 |
"movaps %xmm7,%xmm2 # mono filter result -> xmm2" |
263 |
); |
264 |
} |
265 |
/* apply panorama and volume factors */ |
266 |
__asm__ __volatile__ ( |
267 |
"movss (%1),%%xmm0 # load pan left\n\t" |
268 |
"movss (%2),%%xmm1 # load pan right\n\t" |
269 |
"movaps (%0),%%xmm4 # load vca\n\t" |
270 |
"shufps $0x00,%%xmm0,%%xmm0 # copy pan left to the other 3 cells\n\t" |
271 |
"shufps $0x00,%%xmm1,%%xmm1 # copy pan right to the other 3 cells\n\t" |
272 |
"mulps %%xmm2,%%xmm0 # left = sample * pan_left\n\t" |
273 |
"mulps %%xmm2,%%xmm1 # right = sample * pan_right\n\t" |
274 |
"mulps %%xmm4,%%xmm0 # left = vca * (sample * pan_left)\n\t" |
275 |
"mulps %%xmm4,%%xmm1 # right = vca * (sample * pan_right)\n\t" |
276 |
: /* no output */ |
277 |
: "r" (&Volume[ii]), /* %0 */ |
278 |
"r" (PanL), /* %1 */ |
279 |
"r" (PanR) /* %2 */ |
280 |
: "xmm0", /* holds final left sample (for the 4 samples) at the end */ |
281 |
"xmm1" /* holds final right sample (for the 4 samples) at the end */ |
282 |
); |
283 |
break; |
284 |
} |
285 |
case STEREO: { |
286 |
GetNext4SamplesStereoMMXSSE(pSrc, (float *)Pos, Pitch); // outputs samples in xmm2 (left channel) and xmm3 (right channel) |
287 |
if (USEFILTER) { |
288 |
__asm__ __volatile__ ( |
289 |
"movaps %xmm2,%xmm0 # prepare left channel for filter\n\t" |
290 |
"movaps %xmm3,%xmm1 # save right channel not to get overwritten by filter algorithms\n\t" |
291 |
); |
292 |
FilterL.Apply4StepsSSE(&bqBase, &bqMain); // xmm0 input, xmm7 output |
293 |
__asm__ __volatile__ ( |
294 |
"movaps %xmm1,%xmm0 # prepare right channel for filter\n\t" |
295 |
"movaps %xmm7,%xmm1 # save filter output for left channel\n\t" |
296 |
); |
297 |
FilterR.Apply4StepsSSE(&bqBase, &bqMain); // xmm0 input, xmm7 output |
298 |
__asm__ __volatile__ ( |
299 |
"movaps %xmm1,%xmm2 # result left channel -> xmm2\n\t" |
300 |
"movaps %xmm7,%xmm3 # result right channel -> xmm3\n\t" |
301 |
); |
302 |
} |
303 |
/* apply panorama and volume factors */ |
304 |
__asm__ __volatile__ ( |
305 |
"movss (%1),%%xmm0 # load pan left\n\t" |
306 |
"movss (%2),%%xmm1 # load pan right\n\t" |
307 |
"movaps (%0),%%xmm4 # load vca\n\t" |
308 |
"shufps $0x00,%%xmm0,%%xmm0 # copy pan left to the other 3 cells\n\t" |
309 |
"shufps $0x00,%%xmm1,%%xmm1 # copy pan right to the other 3 cells\n\t" |
310 |
"mulps %%xmm2,%%xmm0 # left = sample_left * pan_left\n\t" |
311 |
"mulps %%xmm3,%%xmm1 # right = sample_right * pan_right\n\t" |
312 |
"mulps %%xmm4,%%xmm0 # left = vca * (sample_left * pan_left)\n\t" |
313 |
"mulps %%xmm4,%%xmm1 # right = vca * (sample_right * pan_right)\n\t" |
314 |
: /* no output */ |
315 |
: "r" (&Volume[ii]), /* %0 */ |
316 |
"r" (PanL), /* %1 */ |
317 |
"r" (PanR) /* %2 */ |
318 |
); |
319 |
break; |
320 |
} |
321 |
} |
322 |
/* mix the 4 samples to the output channels */ |
323 |
__asm__ __volatile__ ( |
324 |
"addps (%0),%%xmm0 # mix calculated sample(s) to output left\n\t" |
325 |
"movaps %%xmm0,(%0) # output to left channel\n\t" |
326 |
"addps (%1),%%xmm1 # mix calculated sample(s) to output right\n\t" |
327 |
"movaps %%xmm1,(%1) # output to right channel\n\t" |
328 |
: /* no output */ |
329 |
: "r" (&pOutL[ii]), /* %0 - must be 16 byte aligned ! */ |
330 |
"r" (&pOutR[ii]) /* %1 - must be 16 byte aligned ! */ |
331 |
); |
332 |
} |
333 |
#endif // ARCH_X86 |
334 |
} |
335 |
} |
336 |
}; |
337 |
|
338 |
}} // namespace LinuxSampler::gig |
339 |
|
340 |
#endif // __LS_GIG_SYNTHESIZER_H__ |