/[svn]/linuxsampler/trunk/src/engines/gig/Synthesizer.h
ViewVC logotype

Annotation of /linuxsampler/trunk/src/engines/gig/Synthesizer.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 332 - (hide annotations) (download) (as text)
Sat Jan 1 03:06:06 2005 UTC (19 years, 3 months ago) by senkov
File MIME type: text/x-c++hdr
File size: 20498 byte(s)
* Initialize the synthesis mode bitmask properly

1 schoenebeck 320 /***************************************************************************
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 schoenebeck 328 #include "../../common/global.h"
27 schoenebeck 320 #include "../../common/RTMath.h"
28     #include "../common/Resampler.h"
29     #include "../common/BiquadFilter.h"
30     #include "Filter.h"
31     #include "Voice.h"
32    
33 senkov 332 #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 senkov 325 #define SYNTHESIS_MODE_SET_PROFILING(iMode,bVal) if (bVal) iMode |= 0x40; else iMode &= ~0x40 /* (un)set mode bit 6 */
40 schoenebeck 320
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 senkov 325 typedef void SynthesizeFragment_Fn(VOICE&, uint, sample_t*, uint);
58 schoenebeck 320
59     void* GetSynthesisFunction(const int SynthesisMode);
60 senkov 325 void RunSynthesisFunction(const int SynthesisMode, VOICE& voice, uint Samples, sample_t* pSrc, uint Skip);
61 schoenebeck 320
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 senkov 325 inline static void SynthesizeFragment(VOICE_T& Voice, uint Samples, sample_t* pSrc, uint i) {
72 schoenebeck 320 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 senkov 325 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 schoenebeck 320 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 senkov 325 const uint processEnd = Min(Samples, i + DiffToLoopEnd(loopEnd,Pos, PBbyPB) + 1); //TODO: instead of +1 we could also round up
110 schoenebeck 320 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 senkov 325 const uint processEnd = Min(Samples, i + DiffToLoopEnd(loopEnd, Pos, PBbyPB) + 1); //TODO: instead of +1 we could also round up
122 schoenebeck 320 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 senkov 325 inline static void Synthesize(VOICE_T& Voice, void* Pos, sample_t* pSrc, uint& i) {
136 schoenebeck 320 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 senkov 325 return uint((LoopEnd - *((double *)Pos)) / Pitch);
155 schoenebeck 320 }
156 schoenebeck 328 #if ARCH_X86
157 schoenebeck 320 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 schoenebeck 328 #endif // ARCH_X86
172 schoenebeck 320 }
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 schoenebeck 328 #if ARCH_X86
185 schoenebeck 320 case ASM_X86_MMX_SSE: {
186     int result;
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     "movl $0,%%eax # result = 0\n\t"
192     "jb 1f # jump if no work needs to be done\n\t"
193     "movss (%3), %%xmm2 # load LoopSize\n\t"
194     "subss %%xmm0, %%xmm1 # Pos - LoopEnd\n\t"
195     //now the fmodf
196     "movss %%xmm1, %%xmm3 # xmm3 = (Pos - LoopEnd)\n\t"
197     "divss %%xmm2, %%xmm1 # (Pos - LoopEnd) / LoopSize\n\t"
198     "cvttss2si %%xmm1, %%eax # convert to int\n\t"
199     "cvtsi2ss %%eax, %%xmm1 # convert back to float\n\t"
200     "movss (%4), %%xmm0 # load LoopStart\n\t"
201     "mulss %%xmm2, %%xmm1 # LoopSize * int((Pos-LoopEnd)/LoopSize)\n\t"
202     "subss %%xmm1, %%xmm3 # xmm2 = fmodf(Pos - LoopEnd, LoopSize)\n\t"
203     //done with fmodf
204     "addss %%xmm0, %%xmm3 # add LoopStart\n\t"
205     "movss %%xmm3, (%1) # update Pos\n\t"
206     "movl $1,%%eax # result = 1\n\t"
207     ".balign 16 \n\t"
208     "1:\n\t"
209     : "=a" (result) /* %0 */
210     : "r" (vPos), /* %1 */
211     "r" (&LoopEnd), /* %2 */
212     "r" (&LoopSize), /* %3 */
213     "r" (&LoopStart) /* %4 */
214     );
215     return result;
216     }
217 schoenebeck 328 #endif // ARCH_X86
218 schoenebeck 320 }
219     }
220    
221 senkov 325 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) {
222 schoenebeck 320 switch (IMPLEMENTATION) {
223     // pure C++ implementation (thus platform independent)
224     case CPP: {
225     switch (CHANNELS) {
226     case MONO: {
227     float samplePoint = GetNextSampleMonoCPP(pSrc, (double *)Pos, Pitch);
228     if (USEFILTER) samplePoint = FilterL.Apply(&bqBase, &bqMain, samplePoint);
229     pOutL[i] += samplePoint * Volume[i] * *PanL;
230     pOutR[i] += samplePoint * Volume[i] * *PanR;
231     i++;
232     break;
233     }
234     case STEREO: {
235     stereo_sample_t samplePoint = GetNextSampleStereoCPP(pSrc, (double *)Pos, Pitch);
236     if (USEFILTER) {
237     samplePoint.left = FilterL.Apply(&bqBase, &bqMain, samplePoint.left);
238     samplePoint.right = FilterR.Apply(&bqBase, &bqMain, samplePoint.right);
239     }
240     pOutL[i] += samplePoint.left * Volume[i] * *PanL;
241     pOutR[i] += samplePoint.right * Volume[i] * *PanR;
242     i++;
243     break;
244     }
245     }
246     break;
247     }
248 schoenebeck 328 #if ARCH_X86
249 schoenebeck 320 // Assembly optimization using the MMX & SSE(1) instruction set (thus only for x86)
250     case ASM_X86_MMX_SSE: {
251     const int ii = i & 0xfffffffc;
252     i += 4;
253     switch (CHANNELS) {
254     case MONO: {
255     GetNext4SamplesMonoMMXSSE(pSrc, (float *)Pos, Pitch); // outputs samples in xmm2
256     if (USEFILTER) {
257     /* prepare filter input */
258     __asm__ __volatile__ (
259     "movaps %xmm2,%xmm0"
260     );
261     FilterL.Apply4StepsSSE(&bqBase, &bqMain); // xmm0 input, xmm7 output
262     __asm__ __volatile__ (
263     "movaps %xmm7,%xmm2 # mono filter result -> xmm2"
264     );
265     }
266     /* apply panorama and volume factors */
267     __asm__ __volatile__ (
268     "movss (%1),%%xmm0 # load pan left\n\t"
269     "movss (%2),%%xmm1 # load pan right\n\t"
270     "movaps (%0),%%xmm4 # load vca\n\t"
271     "shufps $0x00,%%xmm0,%%xmm0 # copy pan left to the other 3 cells\n\t"
272     "shufps $0x00,%%xmm1,%%xmm1 # copy pan right to the other 3 cells\n\t"
273     "mulps %%xmm2,%%xmm0 # left = sample * pan_left\n\t"
274     "mulps %%xmm2,%%xmm1 # right = sample * pan_right\n\t"
275     "mulps %%xmm4,%%xmm0 # left = vca * (sample * pan_left)\n\t"
276     "mulps %%xmm4,%%xmm1 # right = vca * (sample * pan_right)\n\t"
277     : /* no output */
278     : "r" (&Volume[ii]), /* %0 */
279     "r" (PanL), /* %1 */
280     "r" (PanR) /* %2 */
281     : "xmm0", /* holds final left sample (for the 4 samples) at the end */
282     "xmm1" /* holds final right sample (for the 4 samples) at the end */
283     );
284     break;
285     }
286     case STEREO: {
287     GetNext4SamplesStereoMMXSSE(pSrc, (float *)Pos, Pitch); // outputs samples in xmm2 (left channel) and xmm3 (right channel)
288     if (USEFILTER) {
289     __asm__ __volatile__ (
290     "movaps %xmm2,%xmm0 # prepare left channel for filter\n\t"
291     "movaps %xmm3,%xmm1 # save right channel not to get overwritten by filter algorithms\n\t"
292     );
293     FilterL.Apply4StepsSSE(&bqBase, &bqMain); // xmm0 input, xmm7 output
294     __asm__ __volatile__ (
295     "movaps %xmm1,%xmm0 # prepare right channel for filter\n\t"
296     "movaps %xmm7,%xmm1 # save filter output for left channel\n\t"
297     );
298     FilterR.Apply4StepsSSE(&bqBase, &bqMain); // xmm0 input, xmm7 output
299     __asm__ __volatile__ (
300     "movaps %xmm1,%xmm2 # result left channel -> xmm2\n\t"
301     "movaps %xmm7,%xmm3 # result right channel -> xmm3\n\t"
302     );
303     }
304     /* apply panorama and volume factors */
305     __asm__ __volatile__ (
306     "movss (%1),%%xmm0 # load pan left\n\t"
307     "movss (%2),%%xmm1 # load pan right\n\t"
308     "movaps (%0),%%xmm4 # load vca\n\t"
309     "shufps $0x00,%%xmm0,%%xmm0 # copy pan left to the other 3 cells\n\t"
310     "shufps $0x00,%%xmm1,%%xmm1 # copy pan right to the other 3 cells\n\t"
311     "mulps %%xmm2,%%xmm0 # left = sample_left * pan_left\n\t"
312     "mulps %%xmm3,%%xmm1 # right = sample_right * pan_right\n\t"
313     "mulps %%xmm4,%%xmm0 # left = vca * (sample_left * pan_left)\n\t"
314     "mulps %%xmm4,%%xmm1 # right = vca * (sample_right * pan_right)\n\t"
315     : /* no output */
316     : "r" (&Volume[ii]), /* %0 */
317     "r" (PanL), /* %1 */
318     "r" (PanR) /* %2 */
319     );
320     break;
321     }
322     }
323     /* mix the 4 samples to the output channels */
324     __asm__ __volatile__ (
325     "addps (%0),%%xmm0 # mix calculated sample(s) to output left\n\t"
326     "movaps %%xmm0,(%0) # output to left channel\n\t"
327     "addps (%1),%%xmm1 # mix calculated sample(s) to output right\n\t"
328     "movaps %%xmm1,(%1) # output to right channel\n\t"
329     : /* no output */
330     : "r" (&pOutL[ii]), /* %0 - must be 16 byte aligned ! */
331     "r" (&pOutR[ii]) /* %1 - must be 16 byte aligned ! */
332     );
333     }
334 schoenebeck 328 #endif // ARCH_X86
335 schoenebeck 320 }
336     }
337     };
338    
339     }} // namespace LinuxSampler::gig
340    
341     #endif // __LS_GIG_SYNTHESIZER_H__

  ViewVC Help
Powered by ViewVC