/[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 325 - (show annotations) (download) (as text)
Tue Dec 21 04:54:37 2004 UTC (19 years, 3 months ago) by senkov
File MIME type: text/x-c++hdr
File size: 20243 byte(s)
* Added some profiling capabilities, bugs.

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

  ViewVC Help
Powered by ViewVC