/[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 424 - (hide annotations) (download) (as text)
Fri Mar 4 22:54:11 2005 UTC (19 years, 1 month ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 21097 byte(s)
* implemented MIDI Control Change 7 (Volume)
* implemented MIDI Control Change 10 (Panpot)

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

  ViewVC Help
Powered by ViewVC