/[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 617 by schoenebeck, Wed Jun 8 21:00:06 2005 UTC revision 2175 by persson, Mon Apr 25 08:12:36 2011 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                              *   *   Copyright (C) 2005 - 2011 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 24  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"  #include "../../common/global_private.h"
28  #include "../../common/RTMath.h"  #include "../../common/RTMath.h"
29  #include "../common/Resampler.h"  #include "../common/Resampler.h"
 #include "../common/BiquadFilter.h"  
30  #include "Filter.h"  #include "Filter.h"
31  #include "Voice.h"  #include "SynthesisParam.h"
32    
33  #define SYNTHESIS_MODE_SET_CONSTPITCH(iMode,bVal)       if (bVal) iMode |= 0x01; else iMode &= ~0x01   /* (un)set mode bit 0 */  #define SYNTHESIS_MODE_SET_INTERPOLATE(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 */  #define SYNTHESIS_MODE_SET_FILTER(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 */  #define SYNTHESIS_MODE_SET_LOOP(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 */  #define SYNTHESIS_MODE_SET_CHANNELS(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 */  #define SYNTHESIS_MODE_SET_BITDEPTH24(iMode,bVal)       if (bVal) iMode |= 0x10; else iMode &= ~0x10   /* (un)set mode bit 4 */
38    //TODO: the Asm implementation mode is currently not implemented anymore, since Asm synthesis code is currently broken!
39  #define SYNTHESIS_MODE_SET_IMPLEMENTATION(iMode,bVal)   if (bVal) iMode |= 0x20; else iMode &= ~0x20   /* (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 */  //TODO: the profiling mode is currently not implemented anymore!
41    #define SYNTHESIS_MODE_SET_PROFILING(iMode,bVal)        if (bVal) iMode |= 0x40; else iMode &= ~0x40   /* (un)set mode bit 6 */
42    
43  #define SYNTHESIS_MODE_GET_CONSTPITCH(iMode)            iMode & 0x01  #define SYNTHESIS_MODE_GET_INTERPOLATE(iMode)           iMode & 0x01
44  #define SYNTHESIS_MODE_GET_LOOP(iMode)                  iMode & 0x02  #define SYNTHESIS_MODE_GET_FILTER(iMode)                iMode & 0x02
45  #define SYNTHESIS_MODE_GET_INTERPOLATE(iMode)           iMode & 0x04  #define SYNTHESIS_MODE_GET_LOOP(iMode)                  iMode & 0x04
46  #define SYNTHESIS_MODE_GET_FILTER(iMode)                iMode & 0x08  #define SYNTHESIS_MODE_GET_CHANNELS(iMode)              iMode & 0x08
47  #define SYNTHESIS_MODE_GET_CHANNELS(iMode)              iMode & 0x10  #define SYNTHESIS_MODE_GET_BITDEPTH24(iMode)            iMode & 0x10
48  #define SYNTHESIS_MODE_GET_IMPLEMENTATION(iMode)        iMode & 0x20  #define SYNTHESIS_MODE_GET_IMPLEMENTATION(iMode)        iMode & 0x20
49    
 // that's usually gig::Voice of course, but we make it a macro so we can  
 // include this code for our synthesis benchmark which uses fake data  
 // structures  
 #ifndef VOICE  
 # define VOICE Voice  
 #endif // VOICE  
50    
51  namespace LinuxSampler { namespace gig {  namespace LinuxSampler { namespace gig {
52    
53      typedef void SynthesizeFragment_Fn(VOICE&, uint, sample_t*, uint);      typedef void SynthesizeFragment_Fn(SynthesisParam* pFinalParam, Loop* pLoop);
54    
55      void* GetSynthesisFunction(const int SynthesisMode);      void* GetSynthesisFunction(const int SynthesisMode);
56      void RunSynthesisFunction(const int SynthesisMode, VOICE& voice, uint Samples, sample_t* pSrc, uint Skip);      void RunSynthesisFunction(const int SynthesisMode, SynthesisParam* pFinalParam, Loop* pLoop);
57    
58      enum channels_t {      enum channels_t {
59          MONO,          MONO,
# Line 71  namespace LinuxSampler { namespace gig { Line 66  namespace LinuxSampler { namespace gig {
66       * format capable sampler engine. This means resampling / interpolation       * format capable sampler engine. This means resampling / interpolation
67       * for pitching the audio signal, looping, filter and amplification.       * for pitching the audio signal, looping, filter and amplification.
68       */       */
69      template<implementation_t IMPLEMENTATION, channels_t CHANNELS, bool USEFILTER, bool INTERPOLATE, bool DOLOOP, bool CONSTPITCH>      template<channels_t CHANNELS, bool DOLOOP, bool USEFILTER, bool INTERPOLATE, bool BITDEPTH24>
70      class Synthesizer : public __RTMath<IMPLEMENTATION>, public LinuxSampler::Resampler<INTERPOLATE> {      class Synthesizer : public __RTMath<CPP>, public LinuxSampler::Resampler<INTERPOLATE,BITDEPTH24> {
71    
72              // declarations of derived functions (see "Name lookup,              // declarations of derived functions (see "Name lookup,
73              // templates, and accessing members of base classes" in              // templates, and accessing members of base classes" in
74              // the gcc manual for an explanation of why this is              // the gcc manual for an explanation of why this is
75              // needed).              // needed).
76              using __RTMath<IMPLEMENTATION>::Mul;              //using LinuxSampler::Resampler<INTERPOLATE>::GetNextSampleMonoCPP;
77              using __RTMath<IMPLEMENTATION>::Float;              //using LinuxSampler::Resampler<INTERPOLATE>::GetNextSampleStereoCPP;
78              using LinuxSampler::Resampler<INTERPOLATE>::GetNextSampleMonoCPP;              using LinuxSampler::Resampler<INTERPOLATE,BITDEPTH24>::Interpolate1StepMonoCPP;
79              using LinuxSampler::Resampler<INTERPOLATE>::GetNextSampleStereoCPP;              using LinuxSampler::Resampler<INTERPOLATE,BITDEPTH24>::Interpolate1StepStereoCPP;
 #if CONFIG_ASM && ARCH_X86  
             using LinuxSampler::Resampler<INTERPOLATE>::GetNext4SamplesMonoMMXSSE;  
             using LinuxSampler::Resampler<INTERPOLATE>::GetNext4SamplesStereoMMXSSE;  
 #endif  
80    
81          public:          public:
             /**  
              * Render audio for the current fragment for the given voice.  
              * This is the toplevel method of this class.  
              */              
             template<typename VOICE_T>  
             inline static void SynthesizeFragment(VOICE_T& Voice, uint Samples, sample_t* pSrc, uint i) {  
                 const float panLeft  = Mul(Voice.PanLeft,  Voice.pEngineChannel->GlobalPanLeft);  
                 const float panRight = Mul(Voice.PanRight, Voice.pEngineChannel->GlobalPanRight);  
                 if (IMPLEMENTATION == ASM_X86_MMX_SSE) {  
                     float fPos = (float) Voice.Pos;  
                     SynthesizeFragment(Voice, Samples, pSrc, i, Voice.pSample->LoopPlayCount,  
                                        Voice.pSample->LoopStart,  
                                        Voice.pSample->LoopEnd,  
                                        Voice.pSample->LoopSize,  
                                        Voice.LoopCyclesLeft,  
                                        (void *)&fPos,  
                                        Voice.PitchBase,  
                                        Voice.PitchBend,  
                                        &panLeft, &panRight);  
                     #if CONFIG_ASM && ARCH_X86  
                     if (INTERPOLATE) EMMS;  
                     #endif  
                     Voice.Pos = (double) fPos;  
                 } else {  
                     SynthesizeFragment(Voice, Samples, pSrc, i, Voice.pSample->LoopPlayCount,  
                                        Voice.pSample->LoopStart,  
                                        Voice.pSample->LoopEnd,  
                                        Voice.pSample->LoopSize,  
                                        Voice.LoopCyclesLeft,  
                                        (void *)&Voice.Pos,  
                                        Voice.PitchBase,  
                                        Voice.PitchBend,  
                                        &panLeft, &panRight);  
                 }  
             }  
   
82          //protected:          //protected:
83    
84              /**              static void SynthesizeSubFragment(SynthesisParam* pFinalParam, Loop* pLoop) {
              * Render audio for the current fragment for the given voice.  
              * Will be called by the toplevel SynthesizeFragment() method.  
              */    
             template<typename VOICE_T>  
             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) {  
                 const float loopEnd = Float(LoopEnd);  
                 const float PBbyPB = Mul(PitchBase, PitchBend);  
                 const float f_LoopStart = Float(LoopStart);  
                 const float f_LoopSize = Float(LoopSize);  
85                  if (DOLOOP) {                  if (DOLOOP) {
86                      if (LoopPlayCount) {                      const float fLoopEnd   = Float(pLoop->uiEnd);
87                        const float fLoopStart = Float(pLoop->uiStart);
88                        const float fLoopSize  = Float(pLoop->uiSize);
89                        if (pLoop->uiTotalCycles) {
90                          // render loop (loop count limited)                          // render loop (loop count limited)
91                          while (i < Samples && LoopCyclesLeft) {                          for (; pFinalParam->uiToGo > 0 && pLoop->uiCyclesLeft; pLoop->uiCyclesLeft -= WrapLoop(fLoopStart, fLoopSize, fLoopEnd, &pFinalParam->dPos)) {
92                              if (CONSTPITCH) {                              const uint uiToGo = Min(pFinalParam->uiToGo, DiffToLoopEnd(fLoopEnd, &pFinalParam->dPos, pFinalParam->fFinalPitch) + 1); //TODO: instead of +1 we could also round up
93                                  const uint processEnd = Min(Samples, i + DiffToLoopEnd(loopEnd,Pos, PBbyPB) + 1); //TODO: instead of +1 we could also round up                              SynthesizeSubSubFragment(pFinalParam, uiToGo);
                                 while (i < processEnd) Synthesize(Voice, Pos, pSrc, i, PanLeft, PanRight);  
                             }  
                             else Synthesize(Voice, Pos, pSrc, i, PanLeft, PanRight);  
                             if (WrapLoop(f_LoopStart, f_LoopSize, loopEnd, Pos)) LoopCyclesLeft--;  
94                          }                          }
95                          // render on without loop                          // render on without loop
96                          while (i < Samples) Synthesize(Voice, Pos, pSrc, i, PanLeft, PanRight);                          SynthesizeSubSubFragment(pFinalParam, pFinalParam->uiToGo);
97                      }                      } else { // render loop (endless loop)
98                      else { // render loop (endless loop)                          for (; pFinalParam->uiToGo > 0; WrapLoop(fLoopStart, fLoopSize, fLoopEnd, &pFinalParam->dPos)) {
99                          while (i < Samples) {                              const uint uiToGo = Min(pFinalParam->uiToGo, DiffToLoopEnd(fLoopEnd, &pFinalParam->dPos, pFinalParam->fFinalPitch) + 1); //TODO: instead of +1 we could also round up
100                              if (CONSTPITCH) {                              SynthesizeSubSubFragment(pFinalParam, uiToGo);
                                 const uint processEnd = Min(Samples, i + DiffToLoopEnd(loopEnd, Pos, PBbyPB) + 1); //TODO: instead of +1 we could also round up  
                                 while (i < processEnd) Synthesize(Voice, Pos, pSrc, i, PanLeft, PanRight);  
                             }  
                             else Synthesize(Voice, Pos, pSrc, i, PanLeft, PanRight);  
                             WrapLoop(f_LoopStart, f_LoopSize, loopEnd, Pos);  
101                          }                          }
102                      }                      }
103                  }                  } else { // no looping
104                  else { // no looping                      SynthesizeSubSubFragment(pFinalParam, pFinalParam->uiToGo);
                     while (i < Samples) { Synthesize(Voice, Pos, pSrc, i, PanLeft, PanRight);}  
105                  }                  }
106              }              }
107    
108              /**              /**
              * Atomicly render a piece for the voice. For the C++  
              * implementation this means rendering exactly one sample  
              * point, whereas for the MMX/SSE implementation this means  
              * rendering 4 sample points.  
              */  
             template<typename VOICE_T>  
             inline static void Synthesize(VOICE_T& Voice, void* Pos, sample_t* pSrc, uint& i, const float* PanLeft, const float* PanRight) {  
                 Synthesize(pSrc, Pos,  
                            Voice.pEngine->pSynthesisParameters[Event::destination_vco][i],  
                            Voice.pEngineChannel->pOutputLeft,  
                            Voice.pEngineChannel->pOutputRight,  
                            i,  
                            Voice.pEngine->pSynthesisParameters[Event::destination_vca],  
                            PanLeft,  
                            PanRight,  
                            Voice.FilterLeft,  
                            Voice.FilterRight,  
                            Voice.pEngine->pBasicFilterParameters[i],  
                            Voice.pEngine->pMainFilterParameters[i]);  
             }  
   
             /**  
109               * Returns the difference to the sample's loop end.               * Returns the difference to the sample's loop end.
110               */               */
111              inline static int DiffToLoopEnd(const float& LoopEnd, const void* Pos, const float& Pitch) {              inline static int DiffToLoopEnd(const float& LoopEnd, const void* Pos, const float& Pitch) {
112                  switch (IMPLEMENTATION) {                  return uint((LoopEnd - *((double *)Pos)) / Pitch);
                     // pure C++ implementation (thus platform independent)  
                     case CPP: {  
                         return uint((LoopEnd - *((double *)Pos)) / Pitch);  
                     }  
                     #if CONFIG_ASM && ARCH_X86  
                     case ASM_X86_MMX_SSE: {  
                         int result;  
                         __asm__ __volatile__ (  
                             "movss    (%1), %%xmm0  #read loopend\n\t"  
                             "subss    (%2), %%xmm0  #sub  pos\n\t"  
                             "divss    (%3), %%xmm0  #div  by pitch\n\t"  
                             "cvtss2si %%xmm0, %0    #convert to int\n\t"  
                             : "=r" (result)   /* %0 */  
                             : "r" (&LoopEnd), /* %1 */  
                               "r" (Pos),      /* %2 */  
                               "r" (&Pitch)    /* %3 */  
                         );  
                         return result;  
                     }  
                     #endif // CONFIG_ASM && ARCH_X86  
                 }  
113              }              }
114    
115    #if 0
116                //TODO: this method is not in use yet, it's intended to be used for pitch=x.0f where we could use integer instead of float as playback position variable
117                inline static int WrapLoop(const int& LoopStart, const int& LoopSize, const int& LoopEnd, int& Pos) {
118                    //TODO: we can easily eliminate the branch here
119                    if (Pos < LoopEnd) return 0;
120                    Pos = (Pos - LoopEnd) % LoopSize + LoopStart;
121                    return 1;
122                }
123    #endif
124    
125              /**              /**
126               * This method handles looping of the RAM playback part of the               * This method handles looping of the RAM playback part of the
127               * sample, thus repositioning the playback position once the               * sample, thus repositioning the playback position once the
# Line 224  namespace LinuxSampler { namespace gig { Line 130  namespace LinuxSampler { namespace gig {
130               * be called by the DiskThread).               * be called by the DiskThread).
131               */               */
132              inline static int WrapLoop(const float& LoopStart, const float& LoopSize, const float& LoopEnd, void* vPos) {              inline static int WrapLoop(const float& LoopStart, const float& LoopSize, const float& LoopEnd, void* vPos) {
133                  switch (IMPLEMENTATION) {                  double * Pos = (double *)vPos;
134                      // pure C++ implementation (thus platform independent)                  if (*Pos < LoopEnd) return 0;
135                      case CPP: {                  *Pos = fmod(*Pos - LoopEnd, LoopSize) + LoopStart;
136                          double * Pos = (double *)vPos;                  return 1;
137                          if (*Pos < LoopEnd) return 0;              }
138                          *Pos = fmod(*Pos - LoopEnd, LoopSize) + LoopStart;  
139                          return 1;              inline static int32_t getSample(sample_t* src, int pos) {
140                      }                  if (BITDEPTH24) {
141                      #if CONFIG_ASM && ARCH_X86                      pos *= 3;
142                      case ASM_X86_MMX_SSE: {                      #if WORDS_BIGENDIAN
143                          int result = 0;                      unsigned char* p = (unsigned char*)src;
144                          __asm__ __volatile__ (                      return p[pos] << 8 | p[pos + 1] << 16 | p[pos + 2] << 24;
145                              "movss  (%2), %%xmm0          # load LoopEnd\n\t"                      #else
146                              "movss  (%1), %%xmm1          # load Pos\n\t"                      // 24bit read optimization:
147                              "comiss %%xmm0, %%xmm1      # LoopEnd <> Pos\n\t"                      // a misaligned 32bit read and subquent 8 bit shift is faster (on x86)  than reading 3 single bytes and shifting them
148                              "jb     1f                  # jump if no work needs to be done\n\t"                      return (*((int32_t *)(&((char *)(src))[pos])))<<8;
149                              "movss    (%3), %%xmm2        # load LoopSize\n\t"                      #endif
150                              "subss    %%xmm0, %%xmm1    # Pos - LoopEnd\n\t"                  } else {
151                              //now the fmodf                      return src[pos];
                             "movss    %%xmm1, %%xmm3    # xmm3 = (Pos - LoopEnd)\n\t"  
                             "divss    %%xmm2, %%xmm1    # (Pos - LoopEnd) / LoopSize\n\t"  
                             "cvttss2si %%xmm1, %2    # convert to int\n\t"  
                             "cvtsi2ss  %2, %%xmm1    # convert back to float\n\t"  
                             "movss    (%4), %%xmm0      # load LoopStart\n\t"  
                             "mulss    %%xmm2, %%xmm1    # LoopSize * int((Pos-LoopEnd)/LoopSize)\n\t"  
                             "subss    %%xmm1, %%xmm3    # xmm2 = fmodf(Pos - LoopEnd, LoopSize)\n\t"  
                             //done with fmodf  
                             "addss    %%xmm0, %%xmm3      # add LoopStart\n\t"  
                             "movss    %%xmm3, (%1)        # update Pos\n\t"  
                             "movl    $1, (%0)             # result = 1\n\t"  
                             ".balign 16 \n\t"  
                             "1:\n\t"  
                             :: "r" (&result),   /* %0 */  
                               "r"  (vPos),      /* %1 */  
                               "r"  (&LoopEnd),  /* %2 */  
                               "r"  (&LoopSize), /* %3 */  
                               "r"  (&LoopStart) /* %4 */  
                         );  
                         return result;  
                     }  
                     #endif // CONFIG_ASM && ARCH_X86  
152                  }                  }
153              }              }
154    
155              /**              static void SynthesizeSubSubFragment(SynthesisParam* pFinalParam, uint uiToGo) {
156               * Atomicly render a piece for the voice. For the C++                  float fVolumeL = pFinalParam->fFinalVolumeLeft;
157               * implementation this means rendering exactly one sample                  float fVolumeR = pFinalParam->fFinalVolumeRight;
158               * point, whereas for the MMX/SSE implementation this means                  sample_t* pSrc = pFinalParam->pSrc;
159               * rendering 4 sample points.                  float* pOutL   = pFinalParam->pOutLeft;
160               */                  float* pOutR   = pFinalParam->pOutRight;
161              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) {  #ifdef CONFIG_INTERPOLATE_VOLUME
162                  switch (IMPLEMENTATION) {                  float fDeltaL  = pFinalParam->fFinalVolumeDeltaLeft;
163                      // pure C++ implementation (thus platform independent)                  float fDeltaR  = pFinalParam->fFinalVolumeDeltaRight;
164                      case CPP: {  #endif
165                          switch (CHANNELS) {                  switch (CHANNELS) {
166                              case MONO: {                      case MONO: {
167                                  float samplePoint = GetNextSampleMonoCPP(pSrc, (double *)Pos, Pitch);                          float samplePoint;
168                                  if (USEFILTER) samplePoint = FilterL.Apply(&bqBase, &bqMain, samplePoint);                          if (INTERPOLATE) {
169                                  pOutL[i] += samplePoint * Volume[i] * *PanL;                              double dPos    = pFinalParam->dPos;
170                                  pOutR[i] += samplePoint * Volume[i] * *PanR;                              float fPitch   = pFinalParam->fFinalPitch;
171                                  i++;                              if (USEFILTER) {
172                                  break;                                  Filter& filterL = pFinalParam->filterLeft;
173                                    for (int i = 0; i < uiToGo; ++i) {
174                                        samplePoint = Interpolate1StepMonoCPP(pSrc, &dPos, fPitch);
175                                        samplePoint = filterL.Apply(samplePoint);
176    #ifdef CONFIG_INTERPOLATE_VOLUME
177                                        fVolumeL += fDeltaL;
178                                        fVolumeR += fDeltaR;
179    #endif
180                                        pOutL[i] += samplePoint * fVolumeL;
181                                        pOutR[i] += samplePoint * fVolumeR;
182                                    }
183                                } else { // no filter needed
184                                    for (int i = 0; i < uiToGo; ++i) {
185                                        samplePoint = Interpolate1StepMonoCPP(pSrc, &dPos, fPitch);
186    #ifdef CONFIG_INTERPOLATE_VOLUME
187                                        fVolumeL += fDeltaL;
188                                        fVolumeR += fDeltaR;
189    #endif
190                                        pOutL[i] += samplePoint * fVolumeL;
191                                        pOutR[i] += samplePoint * fVolumeR;
192                                    }
193                              }                              }
194                              case STEREO: {                              pFinalParam->dPos = dPos;
195                                  stereo_sample_t samplePoint = GetNextSampleStereoCPP(pSrc, (double *)Pos, Pitch);                          } else { // no interpolation
196                                  if (USEFILTER) {                              int pos_offset = (int) pFinalParam->dPos;
197                                      samplePoint.left  = FilterL.Apply(&bqBase, &bqMain, samplePoint.left);                              if (USEFILTER) {
198                                      samplePoint.right = FilterR.Apply(&bqBase, &bqMain, samplePoint.right);                                  Filter& filterL = pFinalParam->filterLeft;
199                                    for (int i = 0; i < uiToGo; ++i) {
200                                        samplePoint = getSample(pSrc, i + pos_offset);
201                                        samplePoint = filterL.Apply(samplePoint);
202    #ifdef CONFIG_INTERPOLATE_VOLUME
203                                        fVolumeL += fDeltaL;
204                                        fVolumeR += fDeltaR;
205    #endif
206                                        pOutL[i] += samplePoint * fVolumeL;
207                                        pOutR[i] += samplePoint * fVolumeR;
208                                    }
209                                } else { // no filter needed
210                                    for (int i = 0; i < uiToGo; ++i) {
211                                        samplePoint = getSample(pSrc, i + pos_offset);
212    #ifdef CONFIG_INTERPOLATE_VOLUME
213                                        fVolumeL += fDeltaL;
214                                        fVolumeR += fDeltaR;
215    #endif
216                                        pOutL[i] += samplePoint * fVolumeL;
217                                        pOutR[i] += samplePoint * fVolumeR;
218                                  }                                  }
                                 pOutL[i] += samplePoint.left  * Volume[i] * *PanL;  
                                 pOutR[i] += samplePoint.right * Volume[i] * *PanR;  
                                 i++;  
                                 break;  
219                              }                              }
220                                pFinalParam->dPos += uiToGo;
221                          }                          }
222                          break;                          break;
223                      }                      }
224                      #if CONFIG_ASM && ARCH_X86                      case STEREO: {
225                      // Assembly optimization using the MMX & SSE(1) instruction set (thus only for x86)                          stereo_sample_t samplePoint;
226                      case ASM_X86_MMX_SSE: {                          if (INTERPOLATE) {
227                          const int ii = i & 0xfffffffc;                              double dPos    = pFinalParam->dPos;
228                          i += 4;                              float fPitch   = pFinalParam->fFinalPitch;
229                          switch (CHANNELS) {                              if (USEFILTER) {
230                              case MONO: {                                  Filter& filterL = pFinalParam->filterLeft;
231                                  GetNext4SamplesMonoMMXSSE(pSrc, (float *)Pos, Pitch); // outputs samples in xmm2                                  Filter& filterR = pFinalParam->filterRight;
232                                  if (USEFILTER) {                                  for (int i = 0; i < uiToGo; ++i) {
233                                      /* prepare filter input */                                      samplePoint = Interpolate1StepStereoCPP(pSrc, &dPos, fPitch);
234                                      __asm__ __volatile__ (                                      samplePoint.left  = filterL.Apply(samplePoint.left);
235                                          "movaps %xmm2,%xmm0"                                      samplePoint.right = filterR.Apply(samplePoint.right);
236                                      );  #ifdef CONFIG_INTERPOLATE_VOLUME
237                                      FilterL.Apply4StepsSSE(&bqBase, &bqMain); // xmm0 input, xmm7 output                                      fVolumeL += fDeltaL;
238                                      __asm__ __volatile__ (                                      fVolumeR += fDeltaR;
239                                          "movaps %xmm7,%xmm2       # mono filter result -> xmm2"  #endif
240                                      );                                      pOutL[i] += samplePoint.left  * fVolumeL;
241                                        pOutR[i] += samplePoint.right * fVolumeR;
242                                    }
243                                } else { // no filter needed
244                                    for (int i = 0; i < uiToGo; ++i) {
245                                        samplePoint = Interpolate1StepStereoCPP(pSrc, &dPos, fPitch);
246    #ifdef CONFIG_INTERPOLATE_VOLUME
247                                        fVolumeL += fDeltaL;
248                                        fVolumeR += fDeltaR;
249    #endif
250                                        pOutL[i] += samplePoint.left  * fVolumeL;
251                                        pOutR[i] += samplePoint.right * fVolumeR;
252                                  }                                  }
                                 /* apply panorama and volume factors */  
                                 __asm__ __volatile__ (  
                                     "movss    (%1),%%xmm0             # load pan left\n\t"  
                                     "movss    (%2),%%xmm1             # load pan right\n\t"  
                                     "movaps   (%0),%%xmm4             # load vca\n\t"  
                                     "shufps   $0x00,%%xmm0,%%xmm0     # copy pan left to the other 3 cells\n\t"  
                                     "shufps   $0x00,%%xmm1,%%xmm1     # copy pan right to the other 3 cells\n\t"  
                                     "mulps    %%xmm2,%%xmm0           # left  = sample * pan_left\n\t"  
                                     "mulps    %%xmm2,%%xmm1           # right = sample * pan_right\n\t"  
                                     "mulps    %%xmm4,%%xmm0           # left  = vca * (sample * pan_left)\n\t"  
                                     "mulps    %%xmm4,%%xmm1           # right = vca * (sample * pan_right)\n\t"  
                                     : /* no output */  
                                     : "r" (&Volume[ii]), /* %0 */  
                                       "r" (PanL),   /* %1 */  
                                       "r" (PanR)    /* %2 */  
                                     : "xmm0", /* holds final left  sample (for the 4 samples) at the end */  
                                       "xmm1"  /* holds final right sample (for the 4 samples) at the end */  
                                 );  
                                 break;  
253                              }                              }
254                              case STEREO: {                              pFinalParam->dPos = dPos;
255                                  GetNext4SamplesStereoMMXSSE(pSrc, (float *)Pos, Pitch); // outputs samples in xmm2 (left channel) and xmm3 (right channel)                          } else { // no interpolation
256                                  if (USEFILTER) {                              int pos_offset = ((int) pFinalParam->dPos) << 1;
257                                      __asm__ __volatile__ (                              if (USEFILTER) {
258                                          "movaps %xmm2,%xmm0     # prepare left channel for filter\n\t"                                  Filter& filterL = pFinalParam->filterLeft;
259                                          "movaps %xmm3,%xmm1     # save right channel not to get overwritten by filter algorithms\n\t"                                  Filter& filterR = pFinalParam->filterRight;
260                                      );                                  for (int i = 0, ii = 0; i < uiToGo; ++i, ii+=2) {
261                                      FilterL.Apply4StepsSSE(&bqBase, &bqMain); // xmm0 input, xmm7 output                                      samplePoint.left = getSample(pSrc, ii + pos_offset);
262                                      __asm__ __volatile__ (                                      samplePoint.right = getSample(pSrc, ii + pos_offset + 1);
263                                          "movaps %xmm1,%xmm0     # prepare right channel for filter\n\t"                                      samplePoint.left  = filterL.Apply(samplePoint.left);
264                                          "movaps %xmm7,%xmm1     # save filter output for left channel\n\t"                                      samplePoint.right = filterR.Apply(samplePoint.right);
265                                      );  #ifdef CONFIG_INTERPOLATE_VOLUME
266                                      FilterR.Apply4StepsSSE(&bqBase, &bqMain); // xmm0 input, xmm7 output                                      fVolumeL += fDeltaL;
267                                      __asm__ __volatile__ (                                      fVolumeR += fDeltaR;
268                                          "movaps %xmm1,%xmm2     # result left channel -> xmm2\n\t"  #endif
269                                          "movaps %xmm7,%xmm3     # result right channel -> xmm3\n\t"                                      pOutL[i] += samplePoint.left  * fVolumeL;
270                                      );                                      pOutR[i] += samplePoint.right * fVolumeR;
271                                    }
272                                } else { // no filter needed
273                                    for (int i = 0, ii = 0; i < uiToGo; ++i, ii+=2) {
274                                        samplePoint.left = getSample(pSrc, ii + pos_offset);
275                                        samplePoint.right = getSample(pSrc, ii + pos_offset + 1);
276    #ifdef CONFIG_INTERPOLATE_VOLUME
277                                        fVolumeL += fDeltaL;
278                                        fVolumeR += fDeltaR;
279    #endif
280                                        pOutL[i] += samplePoint.left  * fVolumeL;
281                                        pOutR[i] += samplePoint.right * fVolumeR;
282                                  }                                  }
                                 /* apply panorama and volume factors */  
                                 __asm__ __volatile__ (  
                                     "movss    (%1),%%xmm0             # load pan left\n\t"  
                                     "movss    (%2),%%xmm1             # load pan right\n\t"  
                                     "movaps   (%0),%%xmm4             # load vca\n\t"  
                                     "shufps   $0x00,%%xmm0,%%xmm0     # copy pan left to the other 3 cells\n\t"  
                                     "shufps   $0x00,%%xmm1,%%xmm1     # copy pan right to the other 3 cells\n\t"  
                                     "mulps    %%xmm2,%%xmm0           # left  = sample_left  * pan_left\n\t"  
                                     "mulps    %%xmm3,%%xmm1           # right = sample_right * pan_right\n\t"  
                                     "mulps    %%xmm4,%%xmm0           # left  = vca * (sample_left  * pan_left)\n\t"  
                                     "mulps    %%xmm4,%%xmm1           # right = vca * (sample_right * pan_right)\n\t"  
                                     : /* no output */  
                                     : "r" (&Volume[ii]), /* %0 */  
                                       "r" (PanL),   /* %1 */  
                                       "r" (PanR)    /* %2 */  
                                 );  
                                 break;  
283                              }                              }
284                                pFinalParam->dPos += uiToGo;
285                          }                          }
286                          /* mix the 4 samples to the output channels */                          break;
                         __asm__ __volatile__ (  
                             "addps  (%0),%%xmm0       # mix calculated sample(s) to output left\n\t"  
                             "movaps %%xmm0,(%0)       # output to left channel\n\t"  
                             "addps  (%1),%%xmm1       # mix calculated sample(s) to output right\n\t"  
                             "movaps %%xmm1,(%1)       # output to right channel\n\t"  
                             : /* no output */  
                             : "r" (&pOutL[ii]), /* %0 - must be 16 byte aligned ! */  
                               "r" (&pOutR[ii])  /* %1 - must be 16 byte aligned ! */  
                         );  
287                      }                      }
                     #endif // CONFIG_ASM && ARCH_X86  
288                  }                  }
289                    pFinalParam->fFinalVolumeLeft = fVolumeL;
290                    pFinalParam->fFinalVolumeRight = fVolumeR;
291                    pFinalParam->pOutRight += uiToGo;
292                    pFinalParam->pOutLeft  += uiToGo;
293                    pFinalParam->uiToGo    -= uiToGo;
294              }              }
295      };      };
296    

Legend:
Removed from v.617  
changed lines
  Added in v.2175

  ViewVC Help
Powered by ViewVC