Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pitch.c « libcelt - github.com/mumble-voip/celt-0.7.0.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 413dc113ce9b275e6538eb42fbae7599d464cb97 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* Copyright (c) 2007-2008 CSIRO
   Copyright (c) 2007-2009 Xiph.Org Foundation
   Written by Jean-Marc Valin */
/**
   @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 *x, int len, celt_word16 val)
{
   int i;
   celt_word16 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<len);
   } else {
      int shift=0;
      if (maxabs == 0)
         return;
      val >>= 1;
      while (maxabs < val)
      {
         val >>= 1;
         shift++;
      }
      if (shift==0)
         return;
      i=0;
      do{
         x[i] = SHL16(x[i], shift);
      } while (++i<len);
   }
}
#else
#define normalise16(x,len,val)
#endif

#define INPUT_SHIFT 15

void find_spectral_pitch(const CELTMode *m, kiss_fftr_cfg fft, const struct PsyDecay *decay, const celt_sig * restrict x, const celt_sig * restrict y, const celt_word16 * restrict window, celt_word16 * restrict spectrum, int len, int max_pitch, int *pitch, int _C)
{
   int c, i;
   VARDECL(celt_word16, _X);
   VARDECL(celt_word16, _Y);
   const celt_word16 * restrict wptr;
#ifndef SHORTCUTS
   VARDECL(celt_mask, curve);
#endif
   celt_word16 * restrict X, * restrict Y;
   celt_word16 * restrict Xptr, * restrict Yptr;
   const celt_sig * restrict yptr;
   int n2;
   int L2;
   const int C = CHANNELS(_C);
   const int overlap = OVERLAP(m);
   const int lag = MAX_PERIOD;
   SAVE_STACK;
   n2 = lag>>1;
   L2 = len>>1;
   ALLOC(_X, lag, celt_word16);
   X = _X;
#ifndef SHORTCUTS
   ALLOC(curve, n2, celt_mask);
#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<C;c++)
   {
      const celt_sig * restrict xptr = &x[c];
      for (i=0;i<L2;i++)
      {
         X[2*BITREV(fft,i)] += SHR32(*xptr,INPUT_SHIFT);
         xptr += C;
         X[2*BITREV(fft,i)+1] += SHR32(*xptr,INPUT_SHIFT);
         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++)
   {
      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<lag;i++) printf ("%d ", X[i]);printf ("\n");*/
   /* Forward real FFT (in-place) */
   real16_fft_inplace(fft, X, lag);

   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
   compute_masking(decay, X, curve, lag);
#endif
   
   /* Deferred allocation to reduce peak stack usage */
   ALLOC(_Y, lag, celt_word16);
   Y = _Y;
   yptr = &y[0];
   /* Copy first channel of the past audio into Y in bit-reverse order */
   for (i=0;i<n2;i++)
   {
      Y[2*BITREV(fft,i)] = SHR32(*yptr,INPUT_SHIFT);
      yptr += C;
      Y[2*BITREV(fft,i)+1] = SHR32(*yptr,INPUT_SHIFT);
      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++)
      {
         Y[2*BITREV(fft,i)] += SHR32(*yptr,INPUT_SHIFT);
         yptr += C;
         Y[2*BITREV(fft,i)+1] += SHR32(*yptr,INPUT_SHIFT);
         yptr += C;
      }
   }
   normalise16(Y, lag, 8192);
   /* Forward real FFT (in-place) */
   real16_fft_inplace(fft, Y, lag);

   /* Compute cross-spectrum using the inverse masking curve as weighting */
   Xptr = &X[2];
   Yptr = &Y[2];
   for (i=1;i<n2;i++)
   {
      celt_word16 Xr, Xi, n;
      /* weight = 1/sqrt(curve) */
      Xr = Xptr[0];
      Xi = Xptr[1];
#ifdef SHORTCUTS
      /*n = SHR32(32767,(celt_ilog2(EPSILON+curve[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<lag;i++) printf ("%d ", X[i]);printf ("\n");*/
   normalise16(X, lag, 50);
   /* Inverse half-complex to real FFT gives us the correlation */
   real16_ifft(fft, X, Y, lag);
   
   /* The peak in the correlation gives us the pitch */
   *pitch = find_max16(Y, max_pitch);
   RESTORE_STACK;
}