2013-02-13 18:08:47 +01:00
|
|
|
/* (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 "cc6__kiss_fft_guts.h"
|
2013-02-13 19:59:11 +01:00
|
|
|
#include "cc6_kiss_fftr.h"*/
|
2013-02-13 18:30:28 +01:00
|
|
|
#include "cc6_kfft_single.h"
|
2013-02-13 18:08:47 +01:00
|
|
|
|
2013-02-13 19:51:13 +01:00
|
|
|
#include "cc6_pitch.h"
|
|
|
|
#include "cc6_psy.h"
|
|
|
|
#include "cc6_os_support.h"
|
|
|
|
#include "cc6_mathops.h"
|
2013-02-13 19:48:36 +01:00
|
|
|
#include "cc6_modes.h"
|
|
|
|
#include "cc6_stack_alloc.h"
|
2013-02-13 18:08:47 +01:00
|
|
|
|
2013-02-15 21:04:02 +01:00
|
|
|
cc6_kiss_fftr_cfg cc6_pitch_state_alloc(int max_lag)
|
2013-02-13 18:08:47 +01:00
|
|
|
{
|
2013-02-15 21:04:02 +01:00
|
|
|
return cc6_real16_fft_alloc(max_lag);
|
2013-02-13 18:08:47 +01:00
|
|
|
}
|
|
|
|
|
2013-02-15 21:04:02 +01:00
|
|
|
void cc6_pitch_state_free(cc6_kiss_fftr_cfg st)
|
2013-02-13 18:08:47 +01:00
|
|
|
{
|
2013-02-15 21:04:02 +01:00
|
|
|
cc6_real16_fft_free(st);
|
2013-02-13 18:08:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef FIXED_POINT
|
2013-02-15 21:04:02 +01:00
|
|
|
static void cc6_normalise16(cc6_celt_word16_t *x, int len, cc6_celt_word16_t val)
|
2013-02-13 18:08:47 +01:00
|
|
|
{
|
|
|
|
int i;
|
2013-02-15 21:04:02 +01:00
|
|
|
cc6_celt_word16_t maxabs;
|
|
|
|
maxabs = cc6_celt_maxabs16(x,len);
|
2013-02-13 18:08:47 +01:00
|
|
|
if (maxabs > val)
|
|
|
|
{
|
|
|
|
int shift = 0;
|
|
|
|
while (maxabs > val)
|
|
|
|
{
|
|
|
|
maxabs >>= 1;
|
|
|
|
shift++;
|
|
|
|
}
|
|
|
|
if (shift==0)
|
|
|
|
return;
|
|
|
|
i=0;
|
|
|
|
do{
|
2013-02-15 21:04:02 +01:00
|
|
|
x[i] = cc6_SHR16(x[i], shift);
|
2013-02-13 18:08:47 +01:00
|
|
|
} while (++i<len);
|
|
|
|
} else {
|
|
|
|
int shift=0;
|
|
|
|
if (maxabs == 0)
|
|
|
|
return;
|
|
|
|
val >>= 1;
|
|
|
|
while (maxabs < val)
|
|
|
|
{
|
|
|
|
val >>= 1;
|
|
|
|
shift++;
|
|
|
|
}
|
|
|
|
if (shift==0)
|
|
|
|
return;
|
|
|
|
i=0;
|
|
|
|
do{
|
2013-02-15 21:04:02 +01:00
|
|
|
x[i] = cc6_SHL16(x[i], shift);
|
2013-02-13 18:08:47 +01:00
|
|
|
} while (++i<len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2013-02-15 21:04:02 +01:00
|
|
|
#define cc6_normalise16(x,len,val)
|
2013-02-13 18:08:47 +01:00
|
|
|
#endif
|
|
|
|
|
2013-02-15 21:04:02 +01:00
|
|
|
#define cc6_INPUT_SHIFT 15
|
2013-02-13 18:08:47 +01:00
|
|
|
|
2013-02-15 21:04:02 +01:00
|
|
|
void cc6_find_spectral_pitch(const cc6_CELTMode *m, cc6_kiss_fftr_cfg fft, const struct cc6_PsyDecay *decay, const cc6_celt_sig_t * __restrict x, const cc6_celt_sig_t * __restrict y, const cc6_celt_word16_t * __restrict window, cc6_celt_word16_t * __restrict spectrum, int len, int max_pitch, int *pitch)
|
2013-02-13 18:08:47 +01:00
|
|
|
{
|
|
|
|
int c, i;
|
2013-02-15 21:04:02 +01:00
|
|
|
cc6_VARDECL(cc6_celt_word16_t, _X);
|
|
|
|
cc6_VARDECL(cc6_celt_word16_t, _Y);
|
|
|
|
const cc6_celt_word16_t * __restrict wptr;
|
2013-02-13 18:08:47 +01:00
|
|
|
#ifndef SHORTCUTS
|
2013-02-15 21:04:02 +01:00
|
|
|
cc6_VARDECL(cc6_celt_mask_t, curve);
|
2013-02-13 18:08:47 +01:00
|
|
|
#endif
|
2013-02-15 21:04:02 +01:00
|
|
|
cc6_celt_word16_t * __restrict X, * __restrict Y;
|
|
|
|
cc6_celt_word16_t * __restrict Xptr, * __restrict Yptr;
|
|
|
|
const cc6_celt_sig_t * __restrict yptr;
|
2013-02-13 18:08:47 +01:00
|
|
|
int n2;
|
|
|
|
int L2;
|
2013-02-15 21:04:02 +01:00
|
|
|
const int C = cc6_CHANNELS(m);
|
|
|
|
const int overlap = cc6_OVERLAP(m);
|
|
|
|
const int lag = cc6_MAX_PERIOD;
|
|
|
|
cc6_SAVE_STACK;
|
2013-02-13 18:08:47 +01:00
|
|
|
n2 = lag>>1;
|
|
|
|
L2 = len>>1;
|
2013-02-15 21:04:02 +01:00
|
|
|
cc6_ALLOC(_X, lag, cc6_celt_word16_t);
|
2013-02-13 18:08:47 +01:00
|
|
|
X = _X;
|
|
|
|
#ifndef SHORTCUTS
|
2013-02-15 21:04:02 +01:00
|
|
|
cc6_ALLOC(curve, n2, cc6_celt_mask_t);
|
2013-02-13 18:08:47 +01:00
|
|
|
#endif
|
2013-02-15 21:04:02 +01:00
|
|
|
cc6_CELT_MEMSET(X,0,lag);
|
2013-02-13 18:08:47 +01:00
|
|
|
/* Sum all channels of the current frame and copy into X in bit-reverse order */
|
|
|
|
for (c=0;c<C;c++)
|
|
|
|
{
|
2013-02-15 21:04:02 +01:00
|
|
|
const cc6_celt_sig_t * __restrict xptr = &x[c];
|
2013-02-13 18:08:47 +01:00
|
|
|
for (i=0;i<L2;i++)
|
|
|
|
{
|
2013-02-15 21:04:02 +01:00
|
|
|
X[2*cc6_BITREV(fft,i)] += cc6_SHR32(*xptr,cc6_INPUT_SHIFT);
|
2013-02-13 18:08:47 +01:00
|
|
|
xptr += C;
|
2013-02-15 21:04:02 +01:00
|
|
|
X[2*cc6_BITREV(fft,i)+1] += cc6_SHR32(*xptr,cc6_INPUT_SHIFT);
|
2013-02-13 18:08:47 +01:00
|
|
|
xptr += C;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Applying the window in the bit-reverse domain. It's a bit weird, but it
|
|
|
|
can help save memory */
|
|
|
|
wptr = window;
|
|
|
|
for (i=0;i<overlap>>1;i++)
|
|
|
|
{
|
2013-02-15 21:04:02 +01:00
|
|
|
X[2*cc6_BITREV(fft,i)] = cc6_MULT16_16_Q15(wptr[0], X[2*cc6_BITREV(fft,i)]);
|
|
|
|
X[2*cc6_BITREV(fft,i)+1] = cc6_MULT16_16_Q15(wptr[1], X[2*cc6_BITREV(fft,i)+1]);
|
|
|
|
X[2*cc6_BITREV(fft,L2-i-1)] = cc6_MULT16_16_Q15(wptr[1], X[2*cc6_BITREV(fft,L2-i-1)]);
|
|
|
|
X[2*cc6_BITREV(fft,L2-i-1)+1] = cc6_MULT16_16_Q15(wptr[0], X[2*cc6_BITREV(fft,L2-i-1)+1]);
|
2013-02-13 18:08:47 +01:00
|
|
|
wptr += 2;
|
|
|
|
}
|
2013-02-15 21:04:02 +01:00
|
|
|
cc6_normalise16(X, lag, 8192);
|
2013-02-13 18:08:47 +01:00
|
|
|
/*for (i=0;i<lag;i++) printf ("%d ", X[i]);printf ("\n");*/
|
|
|
|
/* Forward real FFT (in-place) */
|
2013-02-15 21:04:02 +01:00
|
|
|
cc6_real16_fft_inplace(fft, X, lag);
|
2013-02-13 18:08:47 +01:00
|
|
|
|
|
|
|
if (spectrum)
|
|
|
|
{
|
|
|
|
for (i=0;i<lag/4;i++)
|
|
|
|
{
|
|
|
|
spectrum[2*i] = X[4*i];
|
|
|
|
spectrum[2*i+1] = X[4*i+1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifndef SHORTCUTS
|
2013-02-15 21:04:02 +01:00
|
|
|
cc6_compute_masking(decay, X, curve, lag);
|
2013-02-13 18:08:47 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Deferred allocation to reduce peak stack usage */
|
2013-02-15 21:04:02 +01:00
|
|
|
cc6_ALLOC(_Y, lag, cc6_celt_word16_t);
|
2013-02-13 18:08:47 +01:00
|
|
|
Y = _Y;
|
|
|
|
yptr = &y[0];
|
|
|
|
/* Copy first channel of the past audio into Y in bit-reverse order */
|
|
|
|
for (i=0;i<n2;i++)
|
|
|
|
{
|
2013-02-15 21:04:02 +01:00
|
|
|
Y[2*cc6_BITREV(fft,i)] = cc6_SHR32(*yptr,cc6_INPUT_SHIFT);
|
2013-02-13 18:08:47 +01:00
|
|
|
yptr += C;
|
2013-02-15 21:04:02 +01:00
|
|
|
Y[2*cc6_BITREV(fft,i)+1] = cc6_SHR32(*yptr,cc6_INPUT_SHIFT);
|
2013-02-13 18:08:47 +01:00
|
|
|
yptr += C;
|
|
|
|
}
|
|
|
|
/* Add remaining channels into Y in bit-reverse order */
|
|
|
|
for (c=1;c<C;c++)
|
|
|
|
{
|
|
|
|
yptr = &y[c];
|
|
|
|
for (i=0;i<n2;i++)
|
|
|
|
{
|
2013-02-15 21:04:02 +01:00
|
|
|
Y[2*cc6_BITREV(fft,i)] += cc6_SHR32(*yptr,cc6_INPUT_SHIFT);
|
2013-02-13 18:08:47 +01:00
|
|
|
yptr += C;
|
2013-02-15 21:04:02 +01:00
|
|
|
Y[2*cc6_BITREV(fft,i)+1] += cc6_SHR32(*yptr,cc6_INPUT_SHIFT);
|
2013-02-13 18:08:47 +01:00
|
|
|
yptr += C;
|
|
|
|
}
|
|
|
|
}
|
2013-02-15 21:04:02 +01:00
|
|
|
cc6_normalise16(Y, lag, 8192);
|
2013-02-13 18:08:47 +01:00
|
|
|
/* Forward real FFT (in-place) */
|
2013-02-15 21:04:02 +01:00
|
|
|
cc6_real16_fft_inplace(fft, Y, lag);
|
2013-02-13 18:08:47 +01:00
|
|
|
|
|
|
|
/* Compute cross-spectrum using the inverse masking curve as weighting */
|
|
|
|
Xptr = &X[2];
|
|
|
|
Yptr = &Y[2];
|
|
|
|
for (i=1;i<n2;i++)
|
|
|
|
{
|
2013-02-15 21:04:02 +01:00
|
|
|
cc6_celt_word16_t Xr, Xi, n;
|
2013-02-13 18:08:47 +01:00
|
|
|
/* weight = 1/sqrt(curve) */
|
|
|
|
Xr = Xptr[0];
|
|
|
|
Xi = Xptr[1];
|
|
|
|
#ifdef SHORTCUTS
|
2013-02-15 21:04:02 +01:00
|
|
|
/*n = cc6_SHR32(32767,(cc6_celt_ilog2(cc6_EPSILON+curve[i])>>1));*/
|
|
|
|
n = 1+(8192>>(cc6_celt_ilog2(1+cc6_MULT16_16(Xr,Xr)+cc6_MULT16_16(Xi,Xi))>>1));
|
2013-02-13 18:08:47 +01:00
|
|
|
/* Pre-multiply X by n, so we can keep everything in 16 bits */
|
2013-02-15 21:04:02 +01:00
|
|
|
Xr = cc6_MULT16_16_16(n, Xr);
|
|
|
|
Xi = cc6_MULT16_16_16(n, Xi);
|
2013-02-13 18:08:47 +01:00
|
|
|
#else
|
2013-02-15 21:04:02 +01:00
|
|
|
n = cc6_celt_rsqrt(cc6_EPSILON+curve[i]);
|
2013-02-13 18:08:47 +01:00
|
|
|
/* Pre-multiply X by n, so we can keep everything in 16 bits */
|
2013-02-15 21:04:02 +01:00
|
|
|
Xr = cc6_EXTRACT16(cc6_SHR32(cc6_MULT16_16(n, Xr),3));
|
|
|
|
Xi = cc6_EXTRACT16(cc6_SHR32(cc6_MULT16_16(n, Xi),3));
|
2013-02-13 18:08:47 +01:00
|
|
|
#endif
|
|
|
|
/* Cross-spectrum between X and conj(Y) */
|
2013-02-15 21:04:02 +01:00
|
|
|
*Xptr++ = cc6_ADD16(cc6_MULT16_16_Q15(Xr, Yptr[0]), cc6_MULT16_16_Q15(Xi,Yptr[1]));
|
|
|
|
*Xptr++ = cc6_SUB16(cc6_MULT16_16_Q15(Xr, Yptr[1]), cc6_MULT16_16_Q15(Xi,Yptr[0]));
|
2013-02-13 18:08:47 +01:00
|
|
|
Yptr += 2;
|
|
|
|
}
|
|
|
|
/*printf ("\n");*/
|
|
|
|
X[0] = X[1] = 0;
|
|
|
|
/*for (i=0;i<lag;i++) printf ("%d ", X[i]);printf ("\n");*/
|
2013-02-15 21:04:02 +01:00
|
|
|
cc6_normalise16(X, lag, 50);
|
2013-02-13 18:08:47 +01:00
|
|
|
/* Inverse half-complex to real FFT gives us the correlation */
|
2013-02-15 21:04:02 +01:00
|
|
|
cc6_real16_ifft(fft, X, Y, lag);
|
2013-02-13 18:08:47 +01:00
|
|
|
|
|
|
|
/* The peak in the correlation gives us the pitch */
|
2013-02-15 21:04:02 +01:00
|
|
|
*pitch = cc6_find_max16(Y, max_pitch);
|
|
|
|
cc6_RESTORE_STACK;
|
2013-02-13 18:08:47 +01:00
|
|
|
}
|