/[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 361 - (show annotations) (download) (as text)
Wed Feb 9 01:22:18 2005 UTC (19 years, 2 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 20489 byte(s)
* bunch of fixes for OSX (patch by Stephane Letz)

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

  ViewVC Help
Powered by ViewVC