| 1 | /* Sound_to_Pitch.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2005,2007-2012,2014-2020,2023-2025 Paul Boersma |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | * See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | * pb 2002/07/16 GPL |
| 21 | * pb 2002/10/11 removed some assertions |
| 22 | * pb 2003/05/20 default time step is four times oversampling |
| 23 | * pb 2003/07/02 checks on NUMrealft |
| 24 | * pb 2004/05/10 better error messages |
| 25 | * pb 2004/10/18 auto maxnCandidates |
| 26 | * pb 2004/10/18 use of constant FFT tables speeds up AC method by a factor of 1.9 |
| 27 | * pb 2006/12/31 compatible with stereo sounds |
| 28 | * pb 2007/01/30 loop split for stereo speeds up CC method by a factor of 6 |
| 29 | * pb 2008/01/19 double |
| 30 | * pb 2010/12/07 compatible with sounds with any number of channels |
| 31 | * pb 2011/03/08 C++ |
| 32 | * pb 2014/05/23 threads |
| 33 | */ |
| 34 | |
| 35 | #include "Sound_to_Pitch.h" |
| 36 | #include "NUM2.h" |
| 37 | #include "Sound_and_Spectrum.h" |
| 38 | |
| 39 | #define AC_HANNING 0 |
| 40 | #define AC_GAUSS 1 |
| 41 | #define FCC_NORMAL 2 |
| 42 | #define FCC_ACCURATE 3 |
| 43 | |
| 44 | static void Sound_into_PitchFrame (Sound me, Pitch_Frame pitchFrame, double t, |
| 45 | double pitchFloor, int maxnCandidates, int method, double voicingThreshold, double octaveCost, |
| 46 | NUMFourierTable fftTable, double dt_window, integer nsamp_window, integer halfnsamp_window, |
| 47 | integer maximumLag, integer nsampFFT, integer nsamp_period, integer halfnsamp_period, |
| 48 | integer brent_ixmax, integer brent_depth, double globalPeak, |
| 49 | MAT const& frame, VEC const& ac, VEC const& window, VEC const& windowR, |
| 50 | double *r, INTVEC const& imax, VEC const& localMean) |
| 51 | { |
| 52 | integer leftSample = Sampled_xToLowIndex (me, t), rightSample = leftSample + 1; |
| 53 | integer startSample, endSample; |
| 54 | |
| 55 | for (integer channel = 1; channel <= my ny; channel ++) { |
| 56 | /* |
| 57 | Compute the local mean; look one longest period to both sides. |
| 58 | */ |
| 59 | startSample = rightSample - nsamp_period; |
| 60 | endSample = leftSample + nsamp_period; |
| 61 | Melder_assert (startSample >= 1); |
| 62 | Melder_assert (endSample <= my nx); |
| 63 | localMean [channel] = 0.0; |
| 64 | for (integer i = startSample; i <= endSample; i ++) |
| 65 | localMean [channel] += my z [channel] [i]; |
| 66 | localMean [channel] /= 2 * nsamp_period; |
| 67 | |
| 68 | /* |
| 69 | Copy a window to a frame and subtract the local mean. |
| 70 | We are going to kill the DC component before windowing. |
| 71 | */ |
| 72 | startSample = rightSample - halfnsamp_window; |
| 73 | endSample = leftSample + halfnsamp_window; |
| 74 | Melder_assert (startSample >= 1); |
| 75 | Melder_assert (endSample <= my nx); |
| 76 | if (method < FCC_NORMAL) { |
| 77 | for (integer j = 1, i = startSample; j <= nsamp_window; j ++) |
| 78 | frame [channel] [j] = (my z [channel] [i ++] - localMean [channel]) * window [j]; |
| 79 | for (integer j = nsamp_window + 1; j <= nsampFFT; j ++) |
| 80 | frame [channel] [j] = 0.0; |
| 81 | } else { |
| 82 | for (integer j = 1, i = startSample; j <= nsamp_window; j ++) |
| 83 | frame [channel] [j] = my z [channel] [i ++] - localMean [channel]; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | Compute the local peak; look half a longest period to both sides. |
| 89 | */ |
| 90 | double localPeak = 0.0; |
| 91 | if ((startSample = halfnsamp_window + 1 - halfnsamp_period) < 1) |
| 92 | startSample = 1; |
| 93 | if ((endSample = halfnsamp_window + halfnsamp_period) > nsamp_window) |
| 94 | endSample = nsamp_window; |
| 95 | for (integer channel = 1; channel <= my ny; channel ++) { |
| 96 | for (integer j = startSample; j <= endSample; j ++) { |
| 97 | double value = fabs (frame [channel] [j]); |
| 98 | if (value > localPeak) |
| 99 | localPeak = value; |
| 100 | } |
| 101 | } |
| 102 | pitchFrame -> intensity = ( localPeak > globalPeak ? 1.0 : localPeak / globalPeak ); |
| 103 | |
| 104 | /* |
| 105 | Compute the correlation into the array 'r'. |
| 106 | */ |
| 107 | if (method >= FCC_NORMAL) { |
| 108 | const double startTime = t - 0.5 * (1.0 / pitchFloor + dt_window); |
| 109 | integer localSpan = maximumLag + nsamp_window; |
| 110 | if ((startSample = Sampled_xToLowIndex (me, startTime)) < 1) |
| 111 | startSample = 1; |
| 112 | if (localSpan > my nx + 1 - startSample) |
| 113 | localSpan = my nx + 1 - startSample; |
| 114 | const integer localMaximumLag = localSpan - nsamp_window; |
| 115 | const integer offset = startSample - 1; |
| 116 | longdouble sumx2 = 0.0; // sum of squares |
| 117 | for (integer channel = 1; channel <= my ny; channel ++) { |
| 118 | const double *const amp = & my z [channel] [0] + offset; |
| 119 | for (integer i = 1; i <= nsamp_window; i ++) { |
| 120 | const double x = amp [i] - localMean [channel]; |
| 121 | sumx2 += x * x; |
| 122 | } |
| 123 | } |
| 124 | longdouble sumy2 = sumx2; // at zero lag, these are still equal |
| 125 | r [0] = 1.0; |
| 126 | for (integer i = 1; i <= localMaximumLag; i ++) { |
| 127 | longdouble product = 0.0; |
| 128 | for (integer channel = 1; channel <= my ny; channel ++) { |
| 129 | const double *const amp = & my z [channel] [0] + offset; |
| 130 | const double y0 = amp [i] - localMean [channel]; |
| 131 | const double yZ = amp [i + nsamp_window] - localMean [channel]; |
| 132 | sumy2 += yZ * yZ - y0 * y0; |
| 133 | for (integer j = 1; j <= nsamp_window; j ++) { |
| 134 | const double x = amp [j] - localMean [channel]; |
| 135 | const double y = amp [i + j] - localMean [channel]; |
| 136 | product += x * y; |
| 137 | } |
| 138 | } |
| 139 | r [- i] = r [i] = (double) product / sqrt ((double) sumx2 * (double) sumy2); |
| 140 | } |
| 141 | } else { |
| 142 | |
| 143 | /* |
| 144 | The FFT of the autocorrelation is the power spectrum. |
| 145 | */ |
| 146 | for (integer i = 1; i <= nsampFFT; i ++) |
| 147 | ac [i] = 0.0; |
| 148 | for (integer channel = 1; channel <= my ny; channel ++) { |
| 149 | NUMfft_forward (fftTable, VEC (& frame [channel] [1], fftTable->n)); // complex spectrum |
| 150 | ac [1] += frame [channel] [1] * frame [channel] [1]; // DC component |
| 151 | for (integer i = 2; i < nsampFFT; i += 2) |
| 152 | ac [i] += frame [channel] [i] * frame [channel] [i] + frame [channel] [i+1] * frame [channel] [i+1]; // power spectrum |
| 153 | ac [nsampFFT] += frame [channel] [nsampFFT] * frame [channel] [nsampFFT]; // Nyquist frequency |
| 154 | } |
| 155 | NUMfft_backward (fftTable, ac); // autocorrelation |
| 156 | |
| 157 | /* |
| 158 | Normalize the autocorrelation to the value with zero lag, |
| 159 | and divide it by the normalized autocorrelation of the window. |
| 160 | */ |
| 161 | r [0] = 1.0; |
| 162 | for (integer i = 1; i <= brent_ixmax; i ++) |
| 163 | r [- i] = r [i] = ac [i + 1] / (ac [1] * windowR [i + 1]); |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | Register the first candidate, which is always present: voicelessness. |
| 168 | */ |
| 169 | pitchFrame -> candidates. resize (pitchFrame -> nCandidates = 1); // maintain invariant; no memory allocations |
| 170 | pitchFrame -> candidates [1]. frequency = 0.0; // voiceless: always present |
| 171 | pitchFrame -> candidates [1]. strength = 0.0; |
| 172 | |
| 173 | /* |
| 174 | Shortcut: absolute silence is always voiceless. |
| 175 | We are done for this frame. |
| 176 | */ |
| 177 | if (localPeak == 0.0) |
| 178 | return; |
| 179 | |
| 180 | /* |
| 181 | Find the strongest maxima of the correlation of this frame, |
| 182 | and register them as candidates. |
| 183 | */ |
| 184 | imax [1] = 0; |
| 185 | for (integer i = 2; i < maximumLag && i < brent_ixmax; i ++) |
| 186 | if (r [i] > 0.5 * voicingThreshold && // not too unvoiced? |
| 187 | r [i] > r [i-1] && r [i] >= r [i+1]) // maximum? |
| 188 | { |
| 189 | integer place = 0; |
| 190 | |
| 191 | /* |
| 192 | Use parabolic interpolation for first estimate of frequency, |
| 193 | and sin(x)/x interpolation to compute the strength of this frequency. |
| 194 | */ |
| 195 | const double dr = 0.5 * (r [i+1] - r [i-1]); |
| 196 | const double d2r = (r [i] - r [i-1]) + (r [i] - r [i+1]); |
| 197 | // not 2.0 * r [i] - r [i-1] - r [i+1] because of rounhding errors! (bug removed 2025-04-18) |
| 198 | Melder_assert (d2r > 0); |
| 199 | const double frequencyOfMaximum = 1.0 / my dx / (i + dr / d2r); |
| 200 | const integer offset = - brent_ixmax - 1; |
| 201 | double strengthOfMaximum = /* method & 1 ? */ |
| 202 | NUM_interpolate_sinc (constVEC (& r [offset + 1], brent_ixmax - offset), 1.0 / my dx / frequencyOfMaximum - offset, 30) |
| 203 | /* : r [i] + 0.5 * dr * dr / d2r */; |
| 204 | /* |
| 205 | High values due to short windows are to be reflected around 1. |
| 206 | */ |
| 207 | if (strengthOfMaximum > 1.0) |
| 208 | strengthOfMaximum = 1.0 / strengthOfMaximum; |
| 209 | |
| 210 | /* |
| 211 | Find a place for this maximum. |
| 212 | */ |
| 213 | if (pitchFrame->nCandidates < maxnCandidates) { // is there still a free place? |
| 214 | pitchFrame -> candidates. resize (++ pitchFrame -> nCandidates); |
| 215 | place = pitchFrame -> nCandidates; |
| 216 | } else { |
| 217 | /* |
| 218 | Try the place of the weakest candidate so far. |
| 219 | */ |
| 220 | double weakest = 2.0; |
| 221 | for (int iweak = 2; iweak <= maxnCandidates; iweak ++) { |
| 222 | /* |
| 223 | High frequencies are to be favoured |
| 224 | if we want to analyze a perfectly periodic signal correctly. |
| 225 | */ |
| 226 | const double localStrength = pitchFrame -> candidates [iweak]. strength - octaveCost * |
| 227 | NUMlog2 (pitchFloor / pitchFrame -> candidates [iweak]. frequency); |
| 228 | if (localStrength < weakest) { |
| 229 | weakest = localStrength; |
| 230 | place = iweak; |
| 231 | } |
| 232 | } |
| 233 | /* |
| 234 | If this maximum is weaker than the weakest candidate so far, give it no place. |
| 235 | */ |
| 236 | if (strengthOfMaximum - octaveCost * NUMlog2 (pitchFloor / frequencyOfMaximum) <= weakest) |
| 237 | place = 0; |
| 238 | } |
| 239 | if (place) { // have we found a place for this candidate? |
| 240 | pitchFrame -> candidates [place]. frequency = frequencyOfMaximum; |
| 241 | pitchFrame -> candidates [place]. strength = strengthOfMaximum; |
| 242 | imax [place] = i; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | Second pass: for extra precision, maximize sin(x)/x interpolation ('sinc'). |
| 248 | */ |
| 249 | for (integer i = 2; i <= pitchFrame -> nCandidates; i ++) { |
| 250 | if (method != AC_HANNING || pitchFrame -> candidates [i]. frequency > 0.0 / my dx) { |
| 251 | double xmid, ymid; |
| 252 | const integer offset = - brent_ixmax - 1; |
| 253 | ymid = NUMimproveMaximum (constVEC (& r [offset + 1], brent_ixmax - offset), imax [i] - offset, |
| 254 | pitchFrame -> candidates [i]. frequency > 0.3 / my dx ? NUM_PEAK_INTERPOLATE_SINC700 : brent_depth, & xmid); |
| 255 | xmid += offset; |
| 256 | pitchFrame -> candidates [i]. frequency = 1.0 / my dx / xmid; |
| 257 | if (ymid > 1.0) |
| 258 | ymid = 1.0 / ymid; |
| 259 | pitchFrame -> candidates [i]. strength = ymid; |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | autoPitch Sound_to_Pitch_any (Sound me, |
| 265 | int method, double periodsPerWindow, |
| 266 | double dt, double pitchFloor, double pitchCeiling, |
| 267 | integer maxnCandidates, |
| 268 | double silenceThreshold, double voicingThreshold, |
| 269 | double octaveCost, double octaveJumpCost, double voicedUnvoicedCost) |
| 270 | { |
| 271 | try { |
| 272 | double t1; |
| 273 | integer numberOfFrames; |
| 274 | integer nsampFFT; |
| 275 | double interpolation_depth; |
| 276 | integer brent_ixmax, brent_depth; |
| 277 | double globalPeak; |
| 278 | |
| 279 | Melder_assert (maxnCandidates >= 2); |
| 280 | Melder_assert (method >= AC_HANNING && method <= FCC_ACCURATE); |
| 281 | |
| 282 | if (maxnCandidates < pitchCeiling / pitchFloor) |
| 283 | maxnCandidates = Melder_ifloor (pitchCeiling / pitchFloor); |
| 284 | |
| 285 | if (dt <= 0.0) |
| 286 | dt = periodsPerWindow / pitchFloor / 4.0; // e.g. 3 periods, 75 Hz: 10 milliseconds |
| 287 | |
| 288 | switch (method) { |
| 289 | case AC_HANNING: |
| 290 | brent_depth = NUM_PEAK_INTERPOLATE_SINC70; |
| 291 | interpolation_depth = 0.5; |
| 292 | break; |
| 293 | case AC_GAUSS: |
| 294 | periodsPerWindow *= 2; // because Gaussian window is twice as long |
| 295 | brent_depth = NUM_PEAK_INTERPOLATE_SINC700; |
| 296 | interpolation_depth = 0.25; // because Gaussian window is twice as long |
| 297 | break; |
| 298 | case FCC_NORMAL: |
| 299 | brent_depth = NUM_PEAK_INTERPOLATE_SINC70; |
| 300 | interpolation_depth = 1.0; |
| 301 | break; |
| 302 | case FCC_ACCURATE: |
| 303 | brent_depth = NUM_PEAK_INTERPOLATE_SINC700; |
| 304 | interpolation_depth = 1.0; |
| 305 | break; |
| 306 | } |
| 307 | volatile const double duration = my dx * my nx; // volatile, because we need to truncate to 64 bits |
| 308 | if (pitchFloor < periodsPerWindow / duration) |
| 309 | Melder_throw (U"To analyse this Sound, “pitch floor” must not be less than ", periodsPerWindow / duration, U" Hz."); |
| 310 | |
| 311 | /* |
| 312 | Determine the number of samples in the longest period. |
| 313 | We need this to compute the local mean of the sound (looking one period in both directions), |
| 314 | and to compute the local peak of the sound (looking half a period in both directions). |
| 315 | */ |
| 316 | const integer nsamp_period = Melder_ifloor (1.0 / my dx / pitchFloor); |
| 317 | const integer halfnsamp_period = nsamp_period / 2 + 1; |
| 318 | |
| 319 | Melder_clipRight (& pitchCeiling, 0.5 / my dx); |
| 320 | |
| 321 | /* |
| 322 | Determine window duration in seconds and in samples. |
| 323 | */ |
| 324 | const double dt_window = periodsPerWindow / pitchFloor; |
| 325 | integer nsamp_window = Melder_ifloor (dt_window / my dx); |
| 326 | const integer halfnsamp_window = nsamp_window / 2 - 1; |
| 327 | if (halfnsamp_window < 2) |
| 328 | Melder_throw (U"Analysis window too short."); |
| 329 | nsamp_window = halfnsamp_window * 2; |
| 330 | |
| 331 | /* |
| 332 | Determine the minimum and maximum lags. |
| 333 | */ |
| 334 | const integer minimumLag = std::max (2_integer, Melder_ifloor (1.0 / my dx / pitchCeiling)); |
| 335 | const integer maximumLag = std::min (Melder_ifloor (nsamp_window / periodsPerWindow) + 2, nsamp_window); |
| 336 | |
| 337 | /* |
| 338 | Determine the number of frames. |
| 339 | Fit as many frames as possible symmetrically in the total duration. |
| 340 | We do this even for the forward cross-correlation method, |
| 341 | because that allows us to compare the two methods. |
| 342 | */ |
| 343 | try { |
| 344 | Sampled_shortTermAnalysis (me, method >= FCC_NORMAL ? 1.0 / pitchFloor + dt_window : dt_window, dt, & numberOfFrames, & t1); |
| 345 | } catch (MelderError) { |
| 346 | Melder_throw (U"The pitch analysis would give zero pitch frames."); |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | Create the resulting pitch contour. |
| 351 | */ |
| 352 | autoPitch thee = Pitch_create (my xmin, my xmax, numberOfFrames, dt, t1, pitchCeiling, maxnCandidates); |
| 353 | |
| 354 | /* |
| 355 | Create (too much) space for candidates. |
| 356 | */ |
| 357 | for (integer iframe = 1; iframe <= numberOfFrames; iframe ++) { |
| 358 | const Pitch_Frame pitchFrame = & thy frames [iframe]; |
| 359 | Pitch_Frame_init (pitchFrame, maxnCandidates); |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | Compute the global absolute peak for determination of silence threshold. |
| 364 | */ |
| 365 | globalPeak = 0.0; |
| 366 | for (integer ichan = 1; ichan <= my ny; ichan ++) { |
| 367 | const double mean = NUMmean (my z.row (ichan)); |
| 368 | for (integer i = 1; i <= my nx; i ++) { |
| 369 | double value = fabs (my z [ichan] [i] - mean); |
| 370 | if (value > globalPeak) |
| 371 | globalPeak = value; |
| 372 | } |
| 373 | } |
| 374 | if (globalPeak == 0.0) |
| 375 | return thee; |
| 376 | |
| 377 | autoVEC window, windowR; |
| 378 | if (method >= FCC_NORMAL) { // for cross-correlation analysis |
| 379 | |
| 380 | nsampFFT = 0; |
| 381 | brent_ixmax = Melder_ifloor (nsamp_window * interpolation_depth); |
| 382 | |
| 383 | } else { // for autocorrelation analysis |
| 384 | |
| 385 | /* |
| 386 | Compute the number of samples needed for doing FFT. |
| 387 | To avoid edge effects, we have to append zeroes to the window. |
| 388 | The maximum lag considered for maxima is maximumLag. |
| 389 | The maximum lag used in interpolation is nsamp_window * interpolation_depth. |
| 390 | */ |
| 391 | nsampFFT = 1; |
| 392 | while (nsampFFT < nsamp_window * (1 + interpolation_depth)) |
| 393 | nsampFFT *= 2; |
| 394 | |
| 395 | /* |
| 396 | Create buffers for autocorrelation analysis. |
| 397 | */ |
| 398 | windowR. resize (nsampFFT); |
| 399 | window. resize (nsamp_window); |
| 400 | autoNUMFourierTable fftTable = NUMFourierTable_create (nsampFFT); |
| 401 | |
| 402 | /* |
| 403 | A Gaussian or Hanning window is applied against phase effects. |
| 404 | The Hanning window is 2 to 5 dB better for 3 periods/window. |
| 405 | The Gaussian window is 25 to 29 dB better for 6 periods/window. |
| 406 | */ |
| 407 | if (method == AC_GAUSS) { // Gaussian window |
| 408 | double imid = 0.5 * (nsamp_window + 1), edge = exp (-12.0); |
| 409 | for (integer i = 1; i <= nsamp_window; i ++) |
| 410 | window [i] = (exp (-48.0 * (i - imid) * (i - imid) / |
| 411 | (nsamp_window + 1) / (nsamp_window + 1)) - edge) / (1.0 - edge); |
| 412 | } else { // Hanning window |
| 413 | for (integer i = 1; i <= nsamp_window; i ++) |
| 414 | window [i] = 0.5 - 0.5 * cos (NUM2pi * i / (nsamp_window + 1)); |
| 415 | } |
| 416 | |
| 417 | /* |
| 418 | Compute the normalized autocorrelation of the window. |
| 419 | */ |
| 420 | for (integer i = 1; i <= nsamp_window; i ++) |
| 421 | windowR [i] = window [i]; |
| 422 | NUMfft_forward (fftTable.get(), windowR.get()); |
| 423 | windowR [1] *= windowR [1]; // DC component |
| 424 | for (integer i = 2; i < nsampFFT; i += 2) { |
| 425 | windowR [i] = windowR [i] * windowR [i] + windowR [i + 1] * windowR [i + 1]; |
| 426 | windowR [i + 1] = 0.0; // power spectrum: square and zero |
| 427 | } |
| 428 | windowR [nsampFFT] *= windowR [nsampFFT]; // Nyquist frequency |
| 429 | NUMfft_backward (fftTable.get(), windowR.get()); // autocorrelation |
| 430 | for (integer i = 2; i <= nsamp_window; i ++) |
| 431 | windowR [i] /= windowR [1]; // normalize |
| 432 | windowR [1] = 1.0; // normalize |
| 433 | |
| 434 | brent_ixmax = Melder_ifloor (nsamp_window * interpolation_depth); |
| 435 | } |
| 436 | |
| 437 | autoMelderProgress progress (U"Sound to Pitch..."); |
| 438 | if (MelderThread_TRACING) |
| 439 | Melder_casual (U"channel frame time pitch"); |
| 440 | |
| 441 | MelderThread_PARALLEL (numberOfFrames, 5) { |
| 442 | autoMAT frame; |
| 443 | autoNUMFourierTable fftTable; |
| 444 | autoVEC ac; |
| 445 | if (method >= FCC_NORMAL) { // cross-correlation |
| 446 | frame = zero_MAT (my ny, nsamp_window); |
| 447 | } else { // autocorrelation |
| 448 | fftTable = NUMFourierTable_create (nsampFFT); |
| 449 | frame = zero_MAT (my ny, nsampFFT); |
| 450 | ac = zero_VEC (nsampFFT); |
| 451 | } |
| 452 | autoVEC rbuffer = zero_VEC (2 * nsamp_window + 1); |
| 453 | double *r = & rbuffer [1 + nsamp_window]; |
| 454 | autoINTVEC imax = zero_INTVEC (maxnCandidates); |
| 455 | autoVEC localMean = zero_VEC (my ny); |
| 456 | MelderThread_FOR (iframe) { |
| 457 | Pitch_Frame pitchFrame = & thy frames [iframe]; |
| 458 | const double time = Sampled_indexToX (thee.get(), iframe); |
| 459 | if (MelderThread_IS_MASTER) { // then we can interact with the GUI |
| 460 | const double estimatedProgress = MelderThread_ESTIMATED_PROGRESS; |
| 461 | Melder_progress (0.1 + 0.8 * estimatedProgress, |
| 462 | U"Sound to Pitch: analysed approximately ", Melder_iround (numberOfFrames * estimatedProgress), |
| 463 | U" out of ", numberOfFrames, U" frames" |
| 464 | ); |
| 465 | } |
| 466 | Sound_into_PitchFrame (me, pitchFrame, time, |
| 467 | pitchFloor, maxnCandidates, method, voicingThreshold, octaveCost, |
| 468 | fftTable.get(), dt_window, nsamp_window, halfnsamp_window, |
| 469 | maximumLag, nsampFFT, nsamp_period, halfnsamp_period, |
| 470 | brent_ixmax, brent_depth, globalPeak, |
| 471 | frame.get(), ac.get(), window.get(), windowR.get(), |
| 472 | r, imax.get(), localMean.get() |
| 473 | ); |
| 474 | if (MelderThread_TRACING) |
| 475 | Melder_casual (MelderThread_CHANNEL, U" ", iframe, U" ", time, U" ", pitchFrame -> candidates [1]. frequency); |
| 476 | } |
| 477 | } MelderThread_ENDPARALLEL |
| 478 | |
| 479 | Melder_progress (0.95, U"Sound to Pitch: path finder"); |
| 480 | Pitch_pathFinder (thee.get(), silenceThreshold, voicingThreshold, |
| 481 | octaveCost, octaveJumpCost, voicedUnvoicedCost, pitchCeiling, Melder_debug == 31 ? true : false); |
| 482 | |
| 483 | return thee; |
| 484 | } catch (MelderError) { |
| 485 | Melder_throw (me, U": pitch analysis not performed."); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | autoPitch Sound_to_Pitch (Sound me, double timeStep, double pitchFloor, double pitchCeiling) { |
| 490 | return Sound_to_Pitch_rawAc (me, timeStep, pitchFloor, pitchCeiling, |
| 491 | 15, false, 0.03, 0.45, 0.01, 0.35, 0.14); |
| 492 | } |
| 493 | |
| 494 | autoPitch Sound_to_Pitch_rawAc (Sound me, |
| 495 | double timeStep, double pitchFloor, double pitchCeiling, |
| 496 | integer maxnCandidates, bool veryAccurate, |
| 497 | double silenceThreshold, double voicingThreshold, |
| 498 | double octaveCost, double octaveJumpCost, double voicedUnvoicedCost) |
| 499 | { |
| 500 | return Sound_to_Pitch_any (me, (int) veryAccurate, 3.0, |
| 501 | timeStep, pitchFloor, pitchCeiling, |
| 502 | maxnCandidates, |
| 503 | silenceThreshold, voicingThreshold, octaveCost, octaveJumpCost, voicedUnvoicedCost |
| 504 | ); |
| 505 | } |
| 506 | |
| 507 | autoPitch Sound_to_Pitch_rawCc (Sound me, |
| 508 | double timeStep, double pitchFloor, double pitchCeiling, |
| 509 | integer maxnCandidates, bool veryAccurate, |
| 510 | double silenceThreshold, double voicingThreshold, |
| 511 | double octaveCost, double octaveJumpCost, double voicedUnvoicedCost) |
| 512 | { |
| 513 | return Sound_to_Pitch_any (me, 2 + (int) veryAccurate, 1.0, |
| 514 | timeStep, pitchFloor, pitchCeiling, |
| 515 | maxnCandidates, |
| 516 | silenceThreshold, voicingThreshold, octaveCost, octaveJumpCost, voicedUnvoicedCost |
| 517 | ); |
| 518 | } |
| 519 | |
| 520 | autoPitch Sound_to_Pitch_filteredAc (Sound me, |
| 521 | double timeStep, double pitchFloor, double pitchTop, |
| 522 | integer maxnCandidates, bool veryAccurate, |
| 523 | double attenuationAtTop, |
| 524 | double silenceThreshold, double voicingThreshold, |
| 525 | double octaveCost, double octaveJumpCost, double voicedUnvoicedCost) |
| 526 | { |
| 527 | try { |
| 528 | const double lowPassCutoffFrequency = pitchTop / NUMsqrt_e (-2.0 * log (attenuationAtTop)); |
| 529 | autoSound thee = Data_copy (me); |
| 530 | if (my ny == 1) { |
| 531 | autoSpectrum spec = Sound_to_Spectrum (me, true); |
| 532 | for (integer ibin = 1; ibin <= spec -> nx; ibin ++) { |
| 533 | const double frequency = Sampled_indexToX (spec.get(), ibin); |
| 534 | const double factor = exp (-0.5 * sqr (frequency / lowPassCutoffFrequency)); |
| 535 | spec -> z [1] [ibin] *= factor; |
| 536 | spec -> z [2] [ibin] *= factor; |
| 537 | } |
| 538 | autoSound him = Spectrum_to_Sound (spec.get()); |
| 539 | thy z.row (1) <<= his z.row (1).part (1, thy nx); |
| 540 | } else { |
| 541 | for (integer ichan = 1; ichan <= my ny; ichan ++) { |
| 542 | autoSound channel = Sound_extractChannel (me, ichan); |
| 543 | autoSpectrum spec = Sound_to_Spectrum (channel.get(), true); |
| 544 | for (integer ibin = 1; ibin <= spec -> nx; ibin ++) { |
| 545 | const double frequency = Sampled_indexToX (spec.get(), ibin); |
| 546 | const double factor = exp (-0.5 * sqr (frequency / lowPassCutoffFrequency)); |
| 547 | spec -> z [1] [ibin] *= factor; |
| 548 | spec -> z [2] [ibin] *= factor; |
| 549 | } |
| 550 | autoSound him = Spectrum_to_Sound (spec.get()); |
| 551 | thy z.row (ichan) <<= his z.row (1).part (1, thy nx); |
| 552 | } |
| 553 | } |
| 554 | return Sound_to_Pitch_any (thee.get(), (int) veryAccurate, 3.0, |
| 555 | timeStep, pitchFloor, pitchTop, |
| 556 | maxnCandidates, |
| 557 | silenceThreshold, voicingThreshold, octaveCost, octaveJumpCost, voicedUnvoicedCost |
| 558 | ); |
| 559 | } catch (MelderError) { |
| 560 | Melder_throw (me, U": pitch analysis (filtered AC) not performed."); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | autoPitch Sound_to_Pitch_filteredCc (Sound me, |
| 565 | double timeStep, double pitchFloor, double pitchTop, |
| 566 | integer maxnCandidates, bool veryAccurate, |
| 567 | double attenuationAtTop, |
| 568 | double silenceThreshold, double voicingThreshold, |
| 569 | double octaveCost, double octaveJumpCost, double voicedUnvoicedCost) |
| 570 | { |
| 571 | try { |
| 572 | const double lowPassCutoffFrequency = pitchTop / NUMsqrt_e (-2.0 * log (attenuationAtTop)); |
| 573 | autoSound thee = Data_copy (me); |
| 574 | if (my ny == 1) { |
| 575 | autoSpectrum spec = Sound_to_Spectrum (me, true); |
| 576 | for (integer ibin = 1; ibin <= spec -> nx; ibin ++) { |
| 577 | const double frequency = Sampled_indexToX (spec.get(), ibin); |
| 578 | const double factor = exp (-0.5 * sqr (frequency / lowPassCutoffFrequency)); |
| 579 | spec -> z [1] [ibin] *= factor; |
| 580 | spec -> z [2] [ibin] *= factor; |
| 581 | } |
| 582 | autoSound him = Spectrum_to_Sound (spec.get()); |
| 583 | thy z.row (1) <<= his z.row (1).part (1, thy nx); |
| 584 | } else { |
| 585 | for (integer ichan = 1; ichan <= my ny; ichan ++) { |
| 586 | autoSound channel = Sound_extractChannel (me, ichan); |
| 587 | autoSpectrum spec = Sound_to_Spectrum (channel.get(), true); |
| 588 | for (integer ibin = 1; ibin <= spec -> nx; ibin ++) { |
| 589 | const double frequency = Sampled_indexToX (spec.get(), ibin); |
| 590 | const double factor = exp (-0.5 * sqr (frequency / lowPassCutoffFrequency)); |
| 591 | spec -> z [1] [ibin] *= factor; |
| 592 | spec -> z [2] [ibin] *= factor; |
| 593 | } |
| 594 | autoSound him = Spectrum_to_Sound (spec.get()); |
| 595 | thy z.row (ichan) <<= his z.row (1).part (1, thy nx); |
| 596 | } |
| 597 | } |
| 598 | return Sound_to_Pitch_any (thee.get(), 2 + (int) veryAccurate, 1.0, |
| 599 | timeStep, pitchFloor, pitchTop, |
| 600 | maxnCandidates, |
| 601 | silenceThreshold, voicingThreshold, octaveCost, octaveJumpCost, voicedUnvoicedCost |
| 602 | ); |
| 603 | } catch (MelderError) { |
| 604 | Melder_throw (me, U": pitch analysis (filtered CC) not performed."); |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | /* End of file Sound_to_Pitch.cpp */ |