/* (C) 2007-2008 Jean-Marc Valin, CSIRO */ /** @file pitch.c @brief Pitch analysis */ /* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - Neither the name of the Xiph.org Foundation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifdef HAVE_CONFIG_H #include "config.h" #endif /*#include "_kiss_fft_guts.h" #include "kiss_fftr.h"*/ #include "kfft_single.h" #include "pitch.h" #include "psy.h" #include "os_support.h" #include "mathops.h" #include "modes.h" #include "stack_alloc.h" kiss_fftr_cfg pitch_state_alloc(int max_lag) { return real16_fft_alloc(max_lag); } void pitch_state_free(kiss_fftr_cfg st) { real16_fft_free(st); } #ifdef FIXED_POINT static void normalise16(celt_word16_t *x, int len, celt_word16_t val) { int i; celt_word16_t maxabs; maxabs = celt_maxabs16(x,len); if (maxabs > val) { int shift = 0; while (maxabs > val) { maxabs >>= 1; shift++; } if (shift==0) return; i=0; do{ x[i] = SHR16(x[i], shift); } while (++i>= 1; while (maxabs < val) { val >>= 1; shift++; } if (shift==0) return; i=0; do{ x[i] = SHL16(x[i], shift); } while (++i>1; L2 = len>>1; ALLOC(_X, lag, celt_word16_t); X = _X; #ifndef SHORTCUTS ALLOC(curve, n2, celt_mask_t); #endif CELT_MEMSET(X,0,lag); /* Sum all channels of the current frame and copy into X in bit-reverse order */ for (c=0;c>1;i++) { X[2*BITREV(fft,i)] = MULT16_16_Q15(wptr[0], X[2*BITREV(fft,i)]); X[2*BITREV(fft,i)+1] = MULT16_16_Q15(wptr[1], X[2*BITREV(fft,i)+1]); X[2*BITREV(fft,L2-i-1)] = MULT16_16_Q15(wptr[1], X[2*BITREV(fft,L2-i-1)]); X[2*BITREV(fft,L2-i-1)+1] = MULT16_16_Q15(wptr[0], X[2*BITREV(fft,L2-i-1)+1]); wptr += 2; } normalise16(X, lag, 8192); /*for (i=0;i>1));*/ n = 1+(8192>>(celt_ilog2(1+MULT16_16(Xr,Xr)+MULT16_16(Xi,Xi))>>1)); /* Pre-multiply X by n, so we can keep everything in 16 bits */ Xr = MULT16_16_16(n, Xr); Xi = MULT16_16_16(n, Xi); #else n = celt_rsqrt(EPSILON+curve[i]); /* Pre-multiply X by n, so we can keep everything in 16 bits */ Xr = EXTRACT16(SHR32(MULT16_16(n, Xr),3)); Xi = EXTRACT16(SHR32(MULT16_16(n, Xi),3)); #endif /* Cross-spectrum between X and conj(Y) */ *Xptr++ = ADD16(MULT16_16_Q15(Xr, Yptr[0]), MULT16_16_Q15(Xi,Yptr[1])); *Xptr++ = SUB16(MULT16_16_Q15(Xr, Yptr[1]), MULT16_16_Q15(Xi,Yptr[0])); Yptr += 2; } /*printf ("\n");*/ X[0] = X[1] = 0; /*for (i=0;i