Pitch.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #ifndef _Pitch_h_
  2. #define _Pitch_h_
  3. /* Pitch.h
  4. *
  5. * Copyright (C) 1992-2011,2014,2015,2016,2017 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. #include "Sampled.h"
  21. #include "Graphics.h"
  22. Thing_declare (Interpreter);
  23. #include "Pitch_enums.h"
  24. #include "Pitch_def.h"
  25. autoPitch Pitch_create (double tmin, double tmax, integer nt, double dt, double t1,
  26. double ceiling, int maxnCandidates);
  27. /*
  28. Function:
  29. create an empty pitch contour (voiceless).
  30. Preconditions:
  31. tmax > tmin;
  32. nt >= 1;
  33. dt > 0.0;
  34. maxnCandidates >= 2;
  35. Postconditions:
  36. my xmin == tmin;
  37. my xmax == tmax;
  38. my nx == nt;
  39. my dx == dt;
  40. my x1 == t1;
  41. my ceiling == ceiling;
  42. my maxnCandidates == maxnCandidates;
  43. my frame [1..nt]. nCandidates == 1;
  44. my frame [1..nt]. candidate [1]. frequency == 0.0; // unvoiced
  45. my frame [1..nt]. candidate [1]. strength == 0.0; // aperiodic
  46. my frame [1..nt]. intensity == 0.0; // silent
  47. */
  48. void Pitch_Frame_init (Pitch_Frame me, int nCandidates);
  49. /*
  50. Function:
  51. create space for a number of candidates; space already there is disposed of.
  52. Preconditions:
  53. nCandidates >= 1;
  54. Postconditions:
  55. my nCandidates == nCandidates;
  56. my candidate [1..nCandidates]. frequency == 0.0; // unvoiced
  57. my candidate [1..nCandidates]. strength == 0.0; // aperiodic
  58. my intensity == 0.0; // silent
  59. */
  60. inline static bool Pitch_util_frequencyIsVoiced (double f, double ceiling) {
  61. return f > 0.0 && f < ceiling; // note: return false is f is NaN
  62. }
  63. bool Pitch_isVoiced_i (Pitch me, integer index);
  64. /*
  65. Is the frame 'index' voiced?
  66. A frame is considered voiced if the frequency of its first candidate
  67. is greater than 0.0 but less than my ceiling.
  68. Precondition:
  69. index >= 1 && index <= my nx;
  70. */
  71. bool Pitch_isVoiced_t (Pitch me, double t);
  72. /*
  73. Are you voiced at time `t`?
  74. The answer is `true` iff `t` lies within a voiced frame.
  75. */
  76. #define Pitch_LEVEL_FREQUENCY 1
  77. #define Pitch_LEVEL_STRENGTH 2
  78. #define Pitch_STRENGTH_UNIT_min 0
  79. #define Pitch_STRENGTH_UNIT_AUTOCORRELATION 0
  80. #define Pitch_STRENGTH_UNIT_NOISE_HARMONICS_RATIO 1
  81. #define Pitch_STRENGTH_UNIT_HARMONICS_NOISE_DB 2
  82. #define Pitch_STRENGTH_UNIT_max 2
  83. #define Pitch_NEAREST 0
  84. #define Pitch_LINEAR 1
  85. double Pitch_getValueAtTime (Pitch me, double time, kPitch_unit unit, bool interpolate);
  86. double Pitch_getStrengthAtTime (Pitch me, double time, kPitch_unit unit, bool interpolate);
  87. integer Pitch_countVoicedFrames (Pitch me);
  88. double Pitch_getMean (Pitch me, double tmin, double tmax, kPitch_unit unit);
  89. double Pitch_getMeanStrength (Pitch me, double tmin, double tmax, int strengthUnit);
  90. double Pitch_getQuantile (Pitch me, double tmin, double tmax, double quantile, kPitch_unit unit);
  91. double Pitch_getStandardDeviation (Pitch me, double tmin, double tmax, kPitch_unit unit);
  92. void Pitch_getMaximumAndTime (Pitch me, double tmin, double tmax, kPitch_unit unit, bool interpolate,
  93. double *return_maximum, double *return_timeOfMaximum);
  94. double Pitch_getMaximum (Pitch me, double tmin, double tmax, kPitch_unit unit, bool interpolate);
  95. double Pitch_getTimeOfMaximum (Pitch me, double tmin, double tmax, kPitch_unit unit, bool interpolate);
  96. void Pitch_getMinimumAndTime (Pitch me, double tmin, double tmax, kPitch_unit unit, bool interpolate,
  97. double *return_minimum, double *return_timeOfMinimum);
  98. double Pitch_getMinimum (Pitch me, double tmin, double tmax, kPitch_unit unit, bool interpolate);
  99. double Pitch_getTimeOfMinimum (Pitch me, double tmin, double tmax, kPitch_unit unit, bool interpolate);
  100. int Pitch_getMaxnCandidates (Pitch me);
  101. /*
  102. Returns the largest number of candidates actually attested in a frame.
  103. */
  104. void Pitch_setCeiling (Pitch me, double ceiling);
  105. /*
  106. Postcondition:
  107. my ceiling = ceiling;
  108. */
  109. void Pitch_pathFinder (Pitch me, double silenceThreshold, double voicingThreshold,
  110. double octaveCost, double octaveJumpCost, double voicedUnvoicedCost,
  111. double ceiling, int pullFormants);
  112. /* Drawing methods. */
  113. #define Pitch_speckle_NO false
  114. #define Pitch_speckle_YES true
  115. void Pitch_drawInside (Pitch me, Graphics g, double tmin, double tmax, double fmin, double fmax,
  116. bool speckle, kPitch_unit yscale);
  117. void Pitch_draw (Pitch me, Graphics g, double tmin, double tmax, double fmin, double fmax, bool garnish,
  118. bool speckle, kPitch_unit yscale);
  119. /*
  120. draw a pitch contour into a Graphics.
  121. If tmax <= tmin, draw whole time domain.
  122. */
  123. void Pitch_difference (Pitch me, Pitch thee);
  124. /* give information about frames that are different in me and thee. */
  125. integer Pitch_getMeanAbsSlope_hertz (Pitch me, double *slope);
  126. integer Pitch_getMeanAbsSlope_mel (Pitch me, double *slope);
  127. integer Pitch_getMeanAbsSlope_semitones (Pitch me, double *slope);
  128. integer Pitch_getMeanAbsSlope_erb (Pitch me, double *slope);
  129. integer Pitch_getMeanAbsSlope_noOctave (Pitch me, double *slope);
  130. /*
  131. The value returned is the number of voiced frames (nVoiced);
  132. this signals if the values are valid:
  133. 'value', 'minimum', 'maximum', and 'mean' are valid if nVoiced >= 1;
  134. 'variance' and 'slope' are valid if nVoiced >= 2.
  135. Invalid variables are always set to 0.0.
  136. 'minimum', 'maximum', 'mean', and 'variance' may be null.
  137. */
  138. autoPitch Pitch_killOctaveJumps (Pitch me);
  139. /* Add octave jumps so that every pitch step,
  140. including those across unvoiced frames,
  141. does not exceed 1/2 octave.
  142. Postcondition:
  143. result -> ceiling = my ceiling * 2;
  144. */
  145. autoPitch Pitch_interpolate (Pitch me);
  146. /* Interpolate the pitch values of unvoiced frames. */
  147. /* No extrapolation beyond first and last voiced frames. */
  148. autoPitch Pitch_subtractLinearFit (Pitch me, kPitch_unit unit);
  149. autoPitch Pitch_smooth (Pitch me, double bandWidth);
  150. /* Smoothing by convolution with Gaussian curve.
  151. Time domain: exp (- (pi t bandWidth) ^ 2)
  152. down to 8.5 % for t = +- 0.5/bandWidth
  153. Frequency domain: exp (- (f / bandWidth) ^ 2)
  154. down to 1/e for f = +- bandWidth
  155. Example:
  156. if bandWidth = 10 Hz,
  157. then the Gaussian curve has a 8.5% duration of 0.1 s,
  158. and a 1/e low-pass point of 10 Hz.
  159. Algorithm:
  160. Interpolation of pitch at internal unvoiced parts.
  161. Zeroth-degree extrapolation at edges (duration triples).
  162. FFT; multiply by Gaussian; inverse FFT.
  163. Cut back to normal duration.
  164. Undo interpolation.
  165. */
  166. void Pitch_step (Pitch me, double step, double precision, double tmin, double tmax);
  167. /*
  168. Instead of the currently chosen candidate,
  169. choose the candidate with another ("target") frequency, determined by 'step'.
  170. E.g., for an upward octave jump, 'step' is 2.
  171. Only consider frequencies between (1 - precision) * targetFrequency
  172. and (1 - precision) * targetFrequency.
  173. Take the candidate nearest to targetFrequency,
  174. as long as that candidate is in between 0 and my ceiling.
  175. */
  176. void Pitch_formula (Pitch me, conststring32 formula, Interpreter interpreter);
  177. /* End of file Pitch.h */
  178. #endif