1 /* (C) 2007-2008 Jean-Marc Valin, CSIRO
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions
13 - Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
16 - Redistributions in binary form must reproduce the above copyright
17 notice, this list of conditions and the following disclaimer in the
18 documentation and/or other materials provided with the distribution.
20 - Neither the name of the Xiph.org Foundation nor the names of its
21 contributors may be used to endorse or promote products derived from
22 this software without specific prior written permission.
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
28 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 /*#include "_kiss_fft_guts.h"
43 #include "kiss_fftr.h"*/
44 #include "kfft_single.h"
48 #include "os_support.h"
51 #include "stack_alloc.h"
53 kiss_fftr_cfg pitch_state_alloc(int max_lag)
55 return real16_fft_alloc(max_lag);
58 void pitch_state_free(kiss_fftr_cfg st)
64 static void normalise16(celt_word16_t *x, int len, celt_word16_t val)
68 maxabs = celt_maxabs16(x,len);
81 x[i] = SHR16(x[i], shift);
97 x[i] = SHL16(x[i], shift);
102 #define normalise16(x,len,val)
105 #define INPUT_SHIFT 15
107 void find_spectral_pitch(const CELTMode *m, kiss_fftr_cfg fft, const struct PsyDecay *decay, const celt_sig_t * restrict x, const celt_sig_t * restrict y, const celt_word16_t * restrict window, celt_word16_t * restrict spectrum, int len, int max_pitch, int *pitch)
110 VARDECL(celt_word16_t, _X);
111 VARDECL(celt_word16_t, _Y);
112 const celt_word16_t * restrict wptr;
114 VARDECL(celt_mask_t, curve);
116 celt_word16_t * restrict X, * restrict Y;
117 celt_word16_t * restrict Xptr, * restrict Yptr;
118 const celt_sig_t * restrict yptr;
121 const int C = CHANNELS(m);
122 const int overlap = OVERLAP(m);
123 const int lag = MAX_PERIOD;
127 ALLOC(_X, lag, celt_word16_t);
130 ALLOC(curve, n2, celt_mask_t);
132 CELT_MEMSET(X,0,lag);
133 /* Sum all channels of the current frame and copy into X in bit-reverse order */
136 const celt_sig_t * restrict xptr = &x[c];
139 X[2*BITREV(fft,i)] += SHR32(*xptr,INPUT_SHIFT);
141 X[2*BITREV(fft,i)+1] += SHR32(*xptr,INPUT_SHIFT);
145 /* Applying the window in the bit-reverse domain. It's a bit weird, but it
146 can help save memory */
148 for (i=0;i<overlap>>1;i++)
150 X[2*BITREV(fft,i)] = MULT16_16_Q15(wptr[0], X[2*BITREV(fft,i)]);
151 X[2*BITREV(fft,i)+1] = MULT16_16_Q15(wptr[1], X[2*BITREV(fft,i)+1]);
152 X[2*BITREV(fft,L2-i-1)] = MULT16_16_Q15(wptr[1], X[2*BITREV(fft,L2-i-1)]);
153 X[2*BITREV(fft,L2-i-1)+1] = MULT16_16_Q15(wptr[0], X[2*BITREV(fft,L2-i-1)+1]);
156 normalise16(X, lag, 8192);
157 /*for (i=0;i<lag;i++) printf ("%d ", X[i]);printf ("\n");*/
158 /* Forward real FFT (in-place) */
159 real16_fft_inplace(fft, X, lag);
163 for (i=0;i<lag/4;i++)
165 spectrum[2*i] = X[4*i];
166 spectrum[2*i+1] = X[4*i+1];
170 compute_masking(decay, X, curve, lag);
173 /* Deferred allocation to reduce peak stack usage */
174 ALLOC(_Y, lag, celt_word16_t);
177 /* Copy first channel of the past audio into Y in bit-reverse order */
180 Y[2*BITREV(fft,i)] = SHR32(*yptr,INPUT_SHIFT);
182 Y[2*BITREV(fft,i)+1] = SHR32(*yptr,INPUT_SHIFT);
185 /* Add remaining channels into Y in bit-reverse order */
191 Y[2*BITREV(fft,i)] += SHR32(*yptr,INPUT_SHIFT);
193 Y[2*BITREV(fft,i)+1] += SHR32(*yptr,INPUT_SHIFT);
197 normalise16(Y, lag, 8192);
198 /* Forward real FFT (in-place) */
199 real16_fft_inplace(fft, Y, lag);
201 /* Compute cross-spectrum using the inverse masking curve as weighting */
206 celt_word16_t Xr, Xi, n;
207 /* weight = 1/sqrt(curve) */
211 /*n = SHR32(32767,(celt_ilog2(EPSILON+curve[i])>>1));*/
212 n = 1+(8192>>(celt_ilog2(1+MULT16_16(Xr,Xr)+MULT16_16(Xi,Xi))>>1));
213 /* Pre-multiply X by n, so we can keep everything in 16 bits */
214 Xr = MULT16_16_16(n, Xr);
215 Xi = MULT16_16_16(n, Xi);
217 n = celt_rsqrt(EPSILON+curve[i]);
218 /* Pre-multiply X by n, so we can keep everything in 16 bits */
219 Xr = EXTRACT16(SHR32(MULT16_16(n, Xr),3));
220 Xi = EXTRACT16(SHR32(MULT16_16(n, Xi),3));
222 /* Cross-spectrum between X and conj(Y) */
223 *Xptr++ = ADD16(MULT16_16_Q15(Xr, Yptr[0]), MULT16_16_Q15(Xi,Yptr[1]));
224 *Xptr++ = SUB16(MULT16_16_Q15(Xr, Yptr[1]), MULT16_16_Q15(Xi,Yptr[0]));
229 /*for (i=0;i<lag;i++) printf ("%d ", X[i]);printf ("\n");*/
230 normalise16(X, lag, 50);
231 /* Inverse half-complex to real FFT gives us the correlation */
232 real16_ifft(fft, X, Y, lag);
234 /* The peak in the correlation gives us the pitch */
235 *pitch = find_max16(Y, max_pitch);