From dffd9449b5cdf2833af293c8d18aedebb6eb8728 Mon Sep 17 00:00:00 2001 From: Jean-Marc Valin Date: Wed, 15 Oct 2008 07:29:58 -0400 Subject: [PATCH] Tonality estimation code --- libcelt/celt.c | 18 +++++++++++++----- libcelt/pitch.c | 10 +++++++++- libcelt/pitch.h | 2 +- libcelt/psy.c | 37 +++++++++++++++++++++++++++++++++++++ libcelt/psy.h | 3 +++ 5 files changed, 63 insertions(+), 7 deletions(-) diff --git a/libcelt/celt.c b/libcelt/celt.c index c8b288be..92947557 100644 --- a/libcelt/celt.c +++ b/libcelt/celt.c @@ -471,8 +471,16 @@ int celt_encode_float(CELTEncoder * restrict st, const celt_sig_t * pcm, celt_si } /* Pitch analysis: we do it early to save on the peak stack space */ if (st->pitch_enabled && !shortBlocks) - find_spectral_pitch(st->mode, st->mode->fft, &st->mode->psy, in, st->out_mem, st->mode->window, 2*N-2*N4, MAX_PERIOD-(2*N-2*N4), &pitch_index); - + { +#ifdef EXP_PSY + VARDECL(celt_word16_t, X); + ALLOC(X, MAX_PERIOD/2, celt_word16_t); + find_spectral_pitch(st->mode, st->mode->fft, &st->mode->psy, in, st->out_mem, st->mode->window, X, 2*N-2*N4, MAX_PERIOD-(2*N-2*N4), &pitch_index); + compute_tonality(st->mode, X, st->psy_mem, MAX_PERIOD); +#else + find_spectral_pitch(st->mode, st->mode->fft, &st->mode->psy, in, st->out_mem, st->mode->window, NULL, 2*N-2*N4, MAX_PERIOD-(2*N-2*N4), &pitch_index); +#endif + } ALLOC(freq, C*N, celt_sig_t); /**< Interleaved signal MDCTs */ /*for (i=0;i<(B+1)*C*N;i++) printf ("%f(%d) ", in[i], i); printf ("\n");*/ @@ -480,13 +488,13 @@ int celt_encode_float(CELTEncoder * restrict st, const celt_sig_t * pcm, celt_si compute_mdcts(st->mode, shortBlocks, in, freq); #ifdef EXP_PSY - CELT_MOVE(st->psy_mem, st->out_mem+N, MAX_PERIOD+st->overlap-N); + /*CELT_MOVE(st->psy_mem, st->out_mem+N, MAX_PERIOD+st->overlap-N); for (i=0;ipsy_mem[MAX_PERIOD+st->overlap-N+i] = in[C*(st->overlap+i)]; for (c=1;cpsy_mem[MAX_PERIOD+st->overlap-N+i] += in[C*(st->overlap+i)+c]; - + */ ALLOC(mask, N, celt_sig_t); compute_mdct_masking(&st->psy, freq, st->psy_mem, mask, C*N); @@ -846,7 +854,7 @@ static void celt_decode_lost(CELTDecoder * restrict st, celt_word16_t * restrict compute_mdcts(st->mode, st->mode->window, st->out_mem+pitch_index*C, freq); #else - find_spectral_pitch(st->mode, st->mode->fft, &st->mode->psy, st->out_mem+MAX_PERIOD-len, st->out_mem, st->mode->window, len, MAX_PERIOD-len-100, &pitch_index); + find_spectral_pitch(st->mode, st->mode->fft, &st->mode->psy, st->out_mem+MAX_PERIOD-len, st->out_mem, st->mode->window, NULL, len, MAX_PERIOD-len-100, &pitch_index); pitch_index = MAX_PERIOD-len-pitch_index; offset = MAX_PERIOD-pitch_index; while (offset+len >= MAX_PERIOD) diff --git a/libcelt/pitch.c b/libcelt/pitch.c index 25ee4755..dd740779 100644 --- a/libcelt/pitch.c +++ b/libcelt/pitch.c @@ -104,7 +104,7 @@ static void normalise16(celt_word16_t *x, int len, celt_word16_t val) #define INPUT_SHIFT 15 -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, int len, int max_pitch, int *pitch) +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) { int c, i; VARDECL(celt_word16_t, _X); @@ -158,6 +158,14 @@ void find_spectral_pitch(const CELTMode *m, kiss_fftr_cfg fft, const struct PsyD /* Forward real FFT (in-place) */ real16_fft_inplace(fft, X, lag); + if (spectrum) + { + for (i=0;iBark mapping */ #define toBARK(n) (13.1f*atan(.00074f*(n))+2.24f*atan((n)*(n)*1.85e-8f)+1e-4f*(n)) @@ -162,4 +163,40 @@ void compute_mdct_masking(const struct PsyDecay *decay, celt_word32_t *X, celt_w spreading_func(decay, mask, len); RESTORE_STACK; } + +void compute_tonality(const CELTMode *m, celt_word16_t * restrict X, celt_word16_t * mem, int len) +{ + int i; + celt_word16_t norm_1; + celt_word16_t *mem2; + int N = len>>2; + + mem2 = mem+2*N; + X[0] = 0; + X[1] = 0; + for (i=1;i