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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 320 by schoenebeck, Mon Dec 13 00:53:16 2004 UTC revision 424 by schoenebeck, Fri Mar 4 22:54:11 2005 UTC
# Line 3  Line 3 
3   *   LinuxSampler - modular, streaming capable sampler                     *   *   LinuxSampler - modular, streaming capable sampler                     *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *
6     *   Copyright (C) 2005 Christian Schoenebeck                              *
7   *                                                                         *   *                                                                         *
8   *   This program is free software; you can redistribute it and/or modify  *   *   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  *   *   it under the terms of the GNU General Public License as published by  *
# Line 23  Line 24 
24  #ifndef __LS_GIG_SYNTHESIZER_H__  #ifndef __LS_GIG_SYNTHESIZER_H__
25  #define __LS_GIG_SYNTHESIZER_H__  #define __LS_GIG_SYNTHESIZER_H__
26    
27    #include "../../common/global.h"
28  #include "../../common/RTMath.h"  #include "../../common/RTMath.h"
29  #include "../common/Resampler.h"  #include "../common/Resampler.h"
30  #include "../common/BiquadFilter.h"  #include "../common/BiquadFilter.h"
31  #include "Filter.h"  #include "Filter.h"
32  #include "Voice.h"  #include "Voice.h"
33    
34  #define SYNTHESIS_MODE_SET_CONSTPITCH(iMode,bVal)       if (bVal) iMode |= 0x01; else iMode &= 0xfe   /* (un)set mode bit 0 */  #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 &= 0xfd   /* (un)set mode bit 1 */  #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 &= 0xfb   /* (un)set mode bit 2 */  #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 &= 0xf7   /* (un)set mode bit 3 */  #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 &= 0xef   /* (un)set mode bit 4 */  #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 &= 0xdf   /* (un)set mode bit 5 */  #define SYNTHESIS_MODE_SET_IMPLEMENTATION(iMode,bVal)   if (bVal) iMode |= 0x20; else iMode &= ~0x20   /* (un)set mode bit 5 */
40    #define SYNTHESIS_MODE_SET_PROFILING(iMode,bVal)        if (bVal) iMode |= 0x40; else iMode &= ~0x40   /* (un)set mode bit 6 */
41    
42  #define SYNTHESIS_MODE_GET_CONSTPITCH(iMode)            iMode & 0x01  #define SYNTHESIS_MODE_GET_CONSTPITCH(iMode)            iMode & 0x01
43  #define SYNTHESIS_MODE_GET_LOOP(iMode)                  iMode & 0x02  #define SYNTHESIS_MODE_GET_LOOP(iMode)                  iMode & 0x02
# Line 52  Line 55 
55    
56  namespace LinuxSampler { namespace gig {  namespace LinuxSampler { namespace gig {
57    
58      typedef void SynthesizeFragment_Fn(VOICE&, uint, sample_t*, int);      typedef void SynthesizeFragment_Fn(VOICE&, uint, sample_t*, uint);
59    
60      void* GetSynthesisFunction(const int SynthesisMode);      void* GetSynthesisFunction(const int SynthesisMode);
61        void RunSynthesisFunction(const int SynthesisMode, VOICE& voice, uint Samples, sample_t* pSrc, uint Skip);
62    
63      enum channels_t {      enum channels_t {
64          MONO,          MONO,
# Line 65  namespace LinuxSampler { namespace gig { Line 69  namespace LinuxSampler { namespace gig {
69      class Synthesizer : public __RTMath<IMPLEMENTATION>, public LinuxSampler::Resampler<INTERPOLATE> {      class Synthesizer : public __RTMath<IMPLEMENTATION>, public LinuxSampler::Resampler<INTERPOLATE> {
70          public:          public:
71              template<typename VOICE_T>              template<typename VOICE_T>
72              inline static void SynthesizeFragment(VOICE_T& Voice, uint Samples, sample_t* pSrc, int& i) {              inline static void SynthesizeFragment(VOICE_T& Voice, uint Samples, sample_t* pSrc, uint i) {
73                    const float panLeft  = Mul(Voice.PanLeft,  Voice.pEngineChannel->GlobalPanLeft);
74                    const float panRight = Mul(Voice.PanRight, Voice.pEngineChannel->GlobalPanRight);
75                  if (IMPLEMENTATION == ASM_X86_MMX_SSE) {                  if (IMPLEMENTATION == ASM_X86_MMX_SSE) {
76                      float fPos = (float) Voice.Pos;                      float fPos = (float) Voice.Pos;
77                      SynthesizeFragment(Voice, Samples, pSrc, i, Voice.pSample->LoopPlayCount,                      SynthesizeFragment(Voice, Samples, pSrc, i, Voice.pSample->LoopPlayCount,
# Line 75  namespace LinuxSampler { namespace gig { Line 81  namespace LinuxSampler { namespace gig {
81                                         Voice.LoopCyclesLeft,                                         Voice.LoopCyclesLeft,
82                                         (void *)&fPos,                                         (void *)&fPos,
83                                         Voice.PitchBase,                                         Voice.PitchBase,
84                                         Voice.PitchBend);                                         Voice.PitchBend,
85                                           &panLeft, &panRight);
86                        #if  ARCH_X86
87                      if (INTERPOLATE) EMMS;                      if (INTERPOLATE) EMMS;
88                        #endif
89                      Voice.Pos = (double) fPos;                      Voice.Pos = (double) fPos;
90                  } else {                  } else {
91                      SynthesizeFragment(Voice, Samples, pSrc, i, Voice.pSample->LoopPlayCount,                      SynthesizeFragment(Voice, Samples, pSrc, i, Voice.pSample->LoopPlayCount,
# Line 86  namespace LinuxSampler { namespace gig { Line 95  namespace LinuxSampler { namespace gig {
95                                         Voice.LoopCyclesLeft,                                         Voice.LoopCyclesLeft,
96                                         (void *)&Voice.Pos,                                         (void *)&Voice.Pos,
97                                         Voice.PitchBase,                                         Voice.PitchBase,
98                                         Voice.PitchBend);                                         Voice.PitchBend,
99                                           &panLeft, &panRight);
100                  }                  }
101              }              }
102    
103          //protected:          //protected:
104    
105              template<typename VOICE_T>              template<typename VOICE_T>
106              inline static void SynthesizeFragment(VOICE_T& Voice, uint Samples, sample_t* pSrc, int& i, uint& LoopPlayCount, uint LoopStart, uint LoopEnd, uint LoopSize, uint& LoopCyclesLeft, void* Pos, float& PitchBase, float& PitchBend) {              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                  const float loopEnd = Float(LoopEnd);                  const float loopEnd = Float(LoopEnd);
108                  const float PBbyPB = Mul(PitchBase, PitchBend);                  const float PBbyPB = Mul(PitchBase, PitchBend);
109                  const float f_LoopStart = Float(LoopStart);                  const float f_LoopStart = Float(LoopStart);
# Line 103  namespace LinuxSampler { namespace gig { Line 113  namespace LinuxSampler { namespace gig {
113                          // render loop (loop count limited)                          // render loop (loop count limited)
114                          while (i < Samples && LoopCyclesLeft) {                          while (i < Samples && LoopCyclesLeft) {
115                              if (CONSTPITCH) {                              if (CONSTPITCH) {
116                                  const int processEnd = Min(Samples, i + DiffToLoopEnd(loopEnd,Pos, PBbyPB) + 1); //TODO: instead of +1 we could also round up                                  const uint processEnd = Min(Samples, i + DiffToLoopEnd(loopEnd,Pos, PBbyPB) + 1); //TODO: instead of +1 we could also round up
117                                  while (i < processEnd) Synthesize(Voice, Pos, pSrc, i);                                  while (i < processEnd) Synthesize(Voice, Pos, pSrc, i, PanLeft, PanRight);
118                              }                              }
119                              else Synthesize(Voice, Pos, pSrc, i);                              else Synthesize(Voice, Pos, pSrc, i, PanLeft, PanRight);
120                              if (WrapLoop(f_LoopStart, f_LoopSize, loopEnd, Pos)) LoopCyclesLeft--;                              if (WrapLoop(f_LoopStart, f_LoopSize, loopEnd, Pos)) LoopCyclesLeft--;
121                          }                          }
122                          // render on without loop                          // render on without loop
123                          while (i < Samples) Synthesize(Voice, Pos, pSrc, i);                          while (i < Samples) Synthesize(Voice, Pos, pSrc, i, PanLeft, PanRight);
124                      }                      }
125                      else { // render loop (endless loop)                      else { // render loop (endless loop)
126                          while (i < Samples) {                          while (i < Samples) {
127                              if (CONSTPITCH) {                              if (CONSTPITCH) {
128                                  const int processEnd = Min(Samples, i + DiffToLoopEnd(loopEnd, Pos, PBbyPB) + 1); //TODO: instead of +1 we could also round up                                  const uint processEnd = Min(Samples, i + DiffToLoopEnd(loopEnd, Pos, PBbyPB) + 1); //TODO: instead of +1 we could also round up
129                                  while (i < processEnd) Synthesize(Voice, Pos, pSrc, i);                                  while (i < processEnd) Synthesize(Voice, Pos, pSrc, i, PanLeft, PanRight);
130                              }                              }
131                              else Synthesize(Voice, Pos, pSrc, i);                              else Synthesize(Voice, Pos, pSrc, i, PanLeft, PanRight);
132                              WrapLoop(f_LoopStart, f_LoopSize, loopEnd, Pos);                              WrapLoop(f_LoopStart, f_LoopSize, loopEnd, Pos);
133                          }                          }
134                      }                      }
135                  }                  }
136                  else { // no looping                  else { // no looping
137                      while (i < Samples) { Synthesize(Voice, Pos, pSrc, i);}                      while (i < Samples) { Synthesize(Voice, Pos, pSrc, i, PanLeft, PanRight);}
138                  }                  }
139              }              }
140    
141              template<typename VOICE_T>              template<typename VOICE_T>
142              inline static void Synthesize(VOICE_T& Voice, void* Pos, sample_t* pSrc, int& i) {              inline static void Synthesize(VOICE_T& Voice, void* Pos, sample_t* pSrc, uint& i, const float* PanLeft, const float* PanRight) {
143                  Synthesize(pSrc, Pos,                  Synthesize(pSrc, Pos,
144                             Voice.pEngine->pSynthesisParameters[Event::destination_vco][i],                             Voice.pEngine->pSynthesisParameters[Event::destination_vco][i],
145                             Voice.pEngine->pOutputLeft,                             Voice.pEngineChannel->pOutputLeft,
146                             Voice.pEngine->pOutputRight,                             Voice.pEngineChannel->pOutputRight,
147                             i,                             i,
148                             Voice.pEngine->pSynthesisParameters[Event::destination_vca],                             Voice.pEngine->pSynthesisParameters[Event::destination_vca],
149                             &Voice.PanLeft,                             PanLeft,
150                             &Voice.PanRight,                             PanRight,
151                             Voice.FilterLeft,                             Voice.FilterLeft,
152                             Voice.FilterRight,                             Voice.FilterRight,
153                             Voice.pEngine->pBasicFilterParameters[i],                             Voice.pEngine->pBasicFilterParameters[i],
# Line 148  namespace LinuxSampler { namespace gig { Line 158  namespace LinuxSampler { namespace gig {
158                  switch (IMPLEMENTATION) {                  switch (IMPLEMENTATION) {
159                      // pure C++ implementation (thus platform independent)                      // pure C++ implementation (thus platform independent)
160                      case CPP: {                      case CPP: {
161                          return int((LoopEnd - *((double *)Pos)) / Pitch);                          return uint((LoopEnd - *((double *)Pos)) / Pitch);
162                      }                      }
163                        #if ARCH_X86
164                      case ASM_X86_MMX_SSE: {                      case ASM_X86_MMX_SSE: {
165                          int result;                          int result;
166                          __asm__ __volatile__ (                          __asm__ __volatile__ (
# Line 164  namespace LinuxSampler { namespace gig { Line 175  namespace LinuxSampler { namespace gig {
175                          );                          );
176                          return result;                          return result;
177                      }                      }
178                        #endif // ARCH_X86
179                  }                  }
180              }              }
181    
# Line 176  namespace LinuxSampler { namespace gig { Line 188  namespace LinuxSampler { namespace gig {
188                          *Pos = fmod(*Pos - LoopEnd, LoopSize) + LoopStart;                          *Pos = fmod(*Pos - LoopEnd, LoopSize) + LoopStart;
189                          return 1;                          return 1;
190                      }                      }
191                        #if ARCH_X86
192                      case ASM_X86_MMX_SSE: {                      case ASM_X86_MMX_SSE: {
193                          int result;                          int result = 0;
194                          __asm__ __volatile__ (                          __asm__ __volatile__ (
195                              "movss  (%2), %%xmm0          # load LoopEnd\n\t"                              "movss  (%2), %%xmm0          # load LoopEnd\n\t"
196                              "movss  (%1), %%xmm1          # load Pos\n\t"                              "movss  (%1), %%xmm1          # load Pos\n\t"
197                              "comiss %%xmm0, %%xmm1      # LoopEnd <> Pos\n\t"                              "comiss %%xmm0, %%xmm1      # LoopEnd <> Pos\n\t"
                             "movl    $0,%%eax            # result = 0\n\t"  
198                              "jb     1f                  # jump if no work needs to be done\n\t"                              "jb     1f                  # jump if no work needs to be done\n\t"
199                              "movss    (%3), %%xmm2        # load LoopSize\n\t"                              "movss    (%3), %%xmm2        # load LoopSize\n\t"
200                              "subss    %%xmm0, %%xmm1    # Pos - LoopEnd\n\t"                              "subss    %%xmm0, %%xmm1    # Pos - LoopEnd\n\t"
# Line 197  namespace LinuxSampler { namespace gig { Line 209  namespace LinuxSampler { namespace gig {
209                              //done with fmodf                              //done with fmodf
210                              "addss    %%xmm0, %%xmm3      # add LoopStart\n\t"                              "addss    %%xmm0, %%xmm3      # add LoopStart\n\t"
211                              "movss    %%xmm3, (%1)        # update Pos\n\t"                              "movss    %%xmm3, (%1)        # update Pos\n\t"
212                              "movl    $1,%%eax            # result = 1\n\t"                              "movl    $1, (%0)             # result = 1\n\t"
213                              ".balign 16 \n\t"                              ".balign 16 \n\t"
214                              "1:\n\t"                              "1:\n\t"
215                              : "=a" (result)     /* %0 */                              :: "r" (&result),   /* %0 */
216                              : "r"  (vPos),      /* %1 */                                "r"  (vPos),      /* %1 */
217                                "r"  (&LoopEnd),  /* %2 */                                "r"  (&LoopEnd),  /* %2 */
218                                "r"  (&LoopSize), /* %3 */                                "r"  (&LoopSize), /* %3 */
219                                "r"  (&LoopStart) /* %4 */                                "r"  (&LoopStart) /* %4 */
220                          );                          );
221                          return result;                          return result;
222                      }                      }
223                        #endif // ARCH_X86
224                  }                  }
225              }              }
226    
227              inline static void Synthesize(sample_t* pSrc, void* Pos, float& Pitch, float* pOutL, float* pOutR, int& i, float* Volume, float* PanL, float* PanR, Filter& FilterL, Filter& FilterR, biquad_param_t& bqBase, biquad_param_t& bqMain) {              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                  switch (IMPLEMENTATION) {                  switch (IMPLEMENTATION) {
229                      // pure C++ implementation (thus platform independent)                      // pure C++ implementation (thus platform independent)
230                      case CPP: {                      case CPP: {
# Line 238  namespace LinuxSampler { namespace gig { Line 251  namespace LinuxSampler { namespace gig {
251                          }                          }
252                          break;                          break;
253                      }                      }
254                        #if ARCH_X86
255                      // Assembly optimization using the MMX & SSE(1) instruction set (thus only for x86)                      // Assembly optimization using the MMX & SSE(1) instruction set (thus only for x86)
256                      case ASM_X86_MMX_SSE: {                      case ASM_X86_MMX_SSE: {
257                          const int ii = i & 0xfffffffc;                          const int ii = i & 0xfffffffc;
# Line 323  namespace LinuxSampler { namespace gig { Line 337  namespace LinuxSampler { namespace gig {
337                                "r" (&pOutR[ii])  /* %1 - must be 16 byte aligned ! */                                "r" (&pOutR[ii])  /* %1 - must be 16 byte aligned ! */
338                          );                          );
339                      }                      }
340                        #endif // ARCH_X86
341                  }                  }
342              }              }
343      };      };

Legend:
Removed from v.320  
changed lines
  Added in v.424

  ViewVC Help
Powered by ViewVC