| 1 | #ifndef _Pitch_h_ |
| 2 | #define _Pitch_h_ |
| 3 | /* Pitch.h |
| 4 | * |
| 5 | * Copyright (C) 1992-2007,2009,2011,2012,2014-2020 Paul Boersma |
| 6 | * |
| 7 | * This code is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or (at |
| 10 | * your option) any later version. |
| 11 | * |
| 12 | * This code is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 15 | * See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include "Sampled.h" |
| 22 | #include "Graphics.h" |
| 23 | #include "Table.h" |
| 24 | Thing_declare (Interpreter); |
| 25 | |
| 26 | #include "Pitch_enums.h" |
| 27 | #include "Pitch_def.h" |
| 28 | |
| 29 | autoPitch Pitch_create (double tmin, double tmax, integer nt, double dt, double t1, |
| 30 | double ceiling, integer maxnCandidates); |
| 31 | /* |
| 32 | Function: |
| 33 | create an empty pitch contour (voiceless). |
| 34 | Preconditions: |
| 35 | tmax > tmin; |
| 36 | nt >= 1; |
| 37 | dt > 0.0; |
| 38 | maxnCandidates >= 2; |
| 39 | Postconditions: |
| 40 | my xmin == tmin; |
| 41 | my xmax == tmax; |
| 42 | my nx == nt; |
| 43 | my dx == dt; |
| 44 | my x1 == t1; |
| 45 | my ceiling == ceiling; |
| 46 | my maxnCandidates == maxnCandidates; |
| 47 | my frame [1..nt]. nCandidates == 1; |
| 48 | my frame [1..nt]. candidate [1]. frequency == 0.0; // unvoiced |
| 49 | my frame [1..nt]. candidate [1]. strength == 0.0; // aperiodic |
| 50 | my frame [1..nt]. intensity == 0.0; // silent |
| 51 | */ |
| 52 | |
| 53 | void Pitch_Frame_init (Pitch_Frame me, integer nCandidates); |
| 54 | /* |
| 55 | Function: |
| 56 | create space for a number of candidates; space already there is disposed of. |
| 57 | Preconditions: |
| 58 | nCandidates >= 1; |
| 59 | Postconditions: |
| 60 | my nCandidates == nCandidates; |
| 61 | my candidate [1..nCandidates]. frequency == 0.0; // unvoiced |
| 62 | my candidate [1..nCandidates]. strength == 0.0; // aperiodic |
| 63 | my intensity == 0.0; // silent |
| 64 | */ |
| 65 | |
| 66 | inline bool Pitch_util_frequencyIsVoiced (double f, double ceiling) { |
| 67 | return f > 0.0 && f < ceiling; // note: return false is f is NaN |
| 68 | } |
| 69 | |
| 70 | bool Pitch_isVoiced_i (Pitch me, integer index); |
| 71 | /* |
| 72 | Is the frame 'index' voiced? |
| 73 | A frame is considered voiced if the frequency of its first candidate |
| 74 | is greater than 0.0 but less than my ceiling. |
| 75 | Precondition: |
| 76 | index >= 1 && index <= my nx; |
| 77 | */ |
| 78 | |
| 79 | bool Pitch_isVoiced_t (Pitch me, double t); |
| 80 | /* |
| 81 | Are you voiced at time `t`? |
| 82 | The answer is `true` iff `t` lies within a voiced frame. |
| 83 | */ |
| 84 | |
| 85 | #define Pitch_LEVEL_FREQUENCY 1 |
| 86 | #define Pitch_LEVEL_STRENGTH 2 |
| 87 | |
| 88 | #define Pitch_STRENGTH_UNIT_min 0 |
| 89 | #define Pitch_STRENGTH_UNIT_AUTOCORRELATION 0 |
| 90 | #define Pitch_STRENGTH_UNIT_NOISE_HARMONICS_RATIO 1 |
| 91 | #define Pitch_STRENGTH_UNIT_HARMONICS_NOISE_DB 2 |
| 92 | #define Pitch_STRENGTH_UNIT_max 2 |
| 93 | |
| 94 | #define Pitch_NEAREST 0 |
| 95 | #define Pitch_LINEAR 1 |
| 96 | |
| 97 | double Pitch_getValueAtTime (Pitch me, double time, kPitch_unit unit, bool interpolate); |
| 98 | double Pitch_getStrengthAtTime (Pitch me, double time, kPitch_unit unit, bool interpolate); |
| 99 | |
| 100 | integer Pitch_countVoicedFrames (Pitch me); |
| 101 | |
| 102 | double Pitch_getMean (Pitch me, double tmin, double tmax, kPitch_unit unit); |
| 103 | double Pitch_getMeanStrength (Pitch me, double tmin, double tmax, int strengthUnit); |
| 104 | double Pitch_getQuantile (Pitch me, double tmin, double tmax, double quantile, kPitch_unit unit); |
| 105 | double Pitch_getStandardDeviation (Pitch me, double tmin, double tmax, kPitch_unit unit); |
| 106 | void Pitch_getMaximumAndTime (Pitch me, double tmin, double tmax, kPitch_unit unit, bool interpolate, |
| 107 | double *return_maximum, double *return_timeOfMaximum); |
| 108 | double Pitch_getMaximum (Pitch me, double tmin, double tmax, kPitch_unit unit, bool interpolate); |
| 109 | double Pitch_getTimeOfMaximum (Pitch me, double tmin, double tmax, kPitch_unit unit, bool interpolate); |
| 110 | void Pitch_getMinimumAndTime (Pitch me, double tmin, double tmax, kPitch_unit unit, bool interpolate, |
| 111 | double *return_minimum, double *return_timeOfMinimum); |
| 112 | double Pitch_getMinimum (Pitch me, double tmin, double tmax, kPitch_unit unit, bool interpolate); |
| 113 | double Pitch_getTimeOfMinimum (Pitch me, double tmin, double tmax, kPitch_unit unit, bool interpolate); |
| 114 | |
| 115 | integer Pitch_getMaxnCandidates (Pitch me); |
| 116 | /* |
| 117 | Returns the largest number of candidates actually attested in a frame. |
| 118 | */ |
| 119 | |
| 120 | void Pitch_setCeiling (Pitch me, double ceiling); |
| 121 | /* |
| 122 | Postcondition: |
| 123 | my ceiling = ceiling; |
| 124 | */ |
| 125 | |
| 126 | autoMAT Pitch_Frame_getAllCandidates (Pitch_Frame me); |
| 127 | autoMAT Pitch_getAllCandidatesInFrame (Pitch me, integer frameNumber); |
| 128 | |
| 129 | void Pitch_pathFinder (Pitch me, double silenceThreshold, double voicingThreshold, |
| 130 | double octaveCost, double octaveJumpCost, double voicedUnvoicedCost, |
| 131 | double ceiling, int pullFormants); |
| 132 | |
| 133 | /* Drawing methods. */ |
| 134 | #define Pitch_speckle_NO false |
| 135 | #define Pitch_speckle_YES true |
| 136 | void Pitch_drawInside (Pitch me, Graphics g, double tmin, double tmax, double fmin, double fmax, |
| 137 | bool speckle, kPitch_unit yscale); |
| 138 | void Pitch_draw (Pitch me, Graphics g, double tmin, double tmax, double fmin, double fmax, bool garnish, |
| 139 | bool speckle, kPitch_unit yscale); |
| 140 | /* |
| 141 | draw a pitch contour into a Graphics. |
| 142 | If tmax <= tmin, draw whole time domain. |
| 143 | */ |
| 144 | |
| 145 | void Pitch_difference (Pitch me, Pitch thee); |
| 146 | /* give information about frames that are different in me and thee. */ |
| 147 | |
| 148 | integer Pitch_getMeanAbsSlope_hertz (Pitch me, double *slope); |
| 149 | integer Pitch_getMeanAbsSlope_mel (Pitch me, double *slope); |
| 150 | integer Pitch_getMeanAbsSlope_semitones (Pitch me, double *slope); |
| 151 | integer Pitch_getMeanAbsSlope_erb (Pitch me, double *slope); |
| 152 | integer Pitch_getMeanAbsSlope_noOctave (Pitch me, double *slope); |
| 153 | /* |
| 154 | The value returned is the number of voiced frames (nVoiced); |
| 155 | this signals if the values are valid: |
| 156 | 'value', 'minimum', 'maximum', and 'mean' are valid if nVoiced >= 1; |
| 157 | 'variance' and 'slope' are valid if nVoiced >= 2. |
| 158 | Invalid variables are always set to 0.0. |
| 159 | 'minimum', 'maximum', 'mean', and 'variance' may be null. |
| 160 | */ |
| 161 | |
| 162 | MelderFraction Pitch_getFractionOfLocallyVoicedFrames (Pitch me, double tmin, double tmax, |
| 163 | double ceiling, double silenceThreshold, double voicingThreshold); |
| 164 | MelderFraction Pitch_getFractionOfLocallyUnvoicedFrames (Pitch me, double tmin, double tmax, |
| 165 | double ceiling, double silenceThreshold, double voicingThreshold); |
| 166 | |
| 167 | autoPitch Pitch_killOctaveJumps (Pitch me); |
| 168 | /* Add octave jumps so that every pitch step, |
| 169 | including those across unvoiced frames, |
| 170 | does not exceed 1/2 octave. |
| 171 | Postcondition: |
| 172 | result -> ceiling = my ceiling * 2; |
| 173 | */ |
| 174 | |
| 175 | autoPitch Pitch_interpolate (Pitch me); |
| 176 | /* Interpolate the pitch values of unvoiced frames. */ |
| 177 | /* No extrapolation beyond first and last voiced frames. */ |
| 178 | |
| 179 | autoPitch Pitch_subtractLinearFit (Pitch me, kPitch_unit unit); |
| 180 | |
| 181 | autoPitch Pitch_smooth (Pitch me, double bandWidth); |
| 182 | /* Smoothing by convolution with Gaussian curve. |
| 183 | Time domain: exp (- (pi t bandWidth) ^ 2) |
| 184 | down to 8.5 % for t = +- 0.5/bandWidth |
| 185 | Frequency domain: exp (- (f / bandWidth) ^ 2) |
| 186 | down to 1/e for f = +- bandWidth |
| 187 | Example: |
| 188 | if bandWidth = 10 Hz, |
| 189 | then the Gaussian curve has a 8.5% duration of 0.1 s, |
| 190 | and a 1/e low-pass point of 10 Hz. |
| 191 | Algorithm: |
| 192 | Interpolation of pitch at internal unvoiced parts. |
| 193 | Zeroth-degree extrapolation at edges (duration triples). |
| 194 | FFT; multiply by Gaussian; inverse FFT. |
| 195 | Cut back to normal duration. |
| 196 | Undo interpolation. |
| 197 | */ |
| 198 | |
| 199 | void Pitch_step (Pitch me, double step, double precision, double tmin, double tmax); |
| 200 | /* |
| 201 | Instead of the currently chosen candidate, |
| 202 | choose the candidate with another ("target") frequency, determined by 'step'. |
| 203 | E.g., for an upward octave jump, 'step' is 2. |
| 204 | Only consider frequencies between (1 - precision) * targetFrequency |
| 205 | and (1 - precision) * targetFrequency. |
| 206 | Take the candidate nearest to targetFrequency, |
| 207 | as long as that candidate is in between 0 and my ceiling. |
| 208 | */ |
| 209 | |
| 210 | void Pitch_formula (Pitch me, conststring32 formula, Interpreter interpreter); |
| 211 | |
| 212 | autoVEC Pitch_listValuesInAllFrames (Pitch me); |
| 213 | |
| 214 | autoTable Pitch_tabulateCandidatesInFrame (Pitch me, integer frameNumber); |
| 215 | autoTable Pitch_tabulateCandidates (Pitch me); |
| 216 | |
| 217 | /* End of file Pitch.h */ |
| 218 | #endif |