PitchTier.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* PitchTier.cpp
  2. *
  3. * Copyright (C) 1992-2012,2015,2016,2017 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 2 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. #include "PitchTier.h"
  19. Thing_implement (PitchTier, RealTier, 0);
  20. void structPitchTier :: v_info () {
  21. structDaata :: v_info ();
  22. MelderInfo_writeLine (U"Time domain:");
  23. MelderInfo_writeLine (U" Start time: ", xmin, U" seconds");
  24. MelderInfo_writeLine (U" End time: ", xmax, U" seconds");
  25. MelderInfo_writeLine (U" Total duration: ", xmax - xmin, U" seconds");
  26. MelderInfo_writeLine (U"Number of points: ", points.size);
  27. MelderInfo_writeLine (U"Minimum pitch value: ", RealTier_getMinimumValue (this), U" Hz");
  28. MelderInfo_writeLine (U"Maximum pitch value: ", RealTier_getMaximumValue (this), U" Hz");
  29. }
  30. autoPitchTier PitchTier_create (double tmin, double tmax) {
  31. try {
  32. autoPitchTier me = Thing_new (PitchTier);
  33. RealTier_init (me.get(), tmin, tmax);
  34. return me;
  35. } catch (MelderError) {
  36. Melder_throw (U"PitchTier not created.");
  37. }
  38. }
  39. void PitchTier_draw (PitchTier me, Graphics g, double tmin, double tmax,
  40. double fmin, double fmax, bool garnish, conststring32 method)
  41. {
  42. RealTier_draw (me, g, tmin, tmax, fmin, fmax, garnish, method, U"Frequency (Hz)");
  43. }
  44. autoPitchTier PointProcess_upto_PitchTier (PointProcess me, double frequency) {
  45. try {
  46. autoPitchTier thee = PointProcess_upto_RealTier (me, frequency, classPitchTier).static_cast_move<structPitchTier>();
  47. return thee;
  48. } catch (MelderError) {
  49. Melder_throw (me, U": not converted to PitchTier.");
  50. }
  51. }
  52. void PitchTier_stylize (PitchTier me, double frequencyResolution, bool useSemitones) {
  53. for (;;) {
  54. integer imin = 0;
  55. double dfmin = 1e308;
  56. for (integer i = 2; i <= my points.size - 1; i ++) {
  57. RealPoint pm = my points.at [i];
  58. RealPoint pl = my points.at [i - 1];
  59. RealPoint pr = my points.at [i + 1];
  60. double expectedFrequency = pl -> value + (pr -> value - pl -> value) /
  61. (pr -> number - pl -> number) * (pm -> number - pl -> number);
  62. double df = useSemitones ?
  63. 12 * fabs (log (pm -> value / expectedFrequency)) / NUMln2:
  64. fabs (pm -> value - expectedFrequency);
  65. if (df < dfmin) {
  66. imin = i;
  67. dfmin = df;
  68. }
  69. }
  70. if (imin == 0 || dfmin > frequencyResolution) break;
  71. my points. removeItem (imin);
  72. }
  73. }
  74. static void PitchTier_writeToSpreadsheetFile (PitchTier me, MelderFile file, bool hasHeader) {
  75. autofile f = Melder_fopen (file, "w");
  76. if (hasHeader)
  77. fprintf (f, "\"ooTextFile\"\n\"PitchTier\"\n%s %s %s\n",
  78. Melder8_double (my xmin), Melder8_double (my xmax), Melder8_integer (my points.size));
  79. for (integer i = 1; i <= my points.size; i ++) {
  80. RealPoint point = my points.at [i];
  81. fprintf (f, "%.17g\t%.17g\n", point -> number, point -> value);
  82. }
  83. f.close (file);
  84. }
  85. void PitchTier_writeToPitchTierSpreadsheetFile (PitchTier me, MelderFile file) {
  86. try {
  87. PitchTier_writeToSpreadsheetFile (me, file, true);
  88. } catch (MelderError) {
  89. Melder_throw (me, U" not written to tab-separated PitchTier file.");
  90. }
  91. }
  92. void PitchTier_writeToHeaderlessSpreadsheetFile (PitchTier me, MelderFile file) {
  93. try {
  94. PitchTier_writeToSpreadsheetFile (me, file, false);
  95. } catch (MelderError) {
  96. Melder_throw (me, U" not written to tab-separated table file.");
  97. }
  98. }
  99. void PitchTier_shiftFrequencies (PitchTier me, double tmin, double tmax, double shift, kPitch_unit unit) {
  100. try {
  101. for (integer i = 1; i <= my points.size; i ++) {
  102. RealPoint point = my points.at [i];
  103. double frequency = point -> value;
  104. if (point -> number < tmin || point -> number > tmax) continue;
  105. switch (unit) {
  106. case kPitch_unit::HERTZ: {
  107. frequency += shift;
  108. if (frequency <= 0.0)
  109. Melder_throw (U"The resulting frequency has to be greater than 0 Hz.");
  110. } break; case kPitch_unit::MEL: {
  111. frequency = NUMhertzToMel (frequency) + shift;
  112. if (frequency <= 0.0)
  113. Melder_throw (U"The resulting frequency has to be greater than 0 mel.");
  114. frequency = NUMmelToHertz (frequency);
  115. } break; case kPitch_unit::LOG_HERTZ: {
  116. frequency = pow (10.0, log10 (frequency) + shift);
  117. } break; case kPitch_unit::SEMITONES_1: {
  118. frequency = NUMsemitonesToHertz (NUMhertzToSemitones (frequency) + shift);
  119. } break; case kPitch_unit::ERB: {
  120. frequency = NUMhertzToErb (frequency) + shift;
  121. if (frequency <= 0.0)
  122. Melder_throw (U"The resulting frequency has to be greater than 0 ERB.");
  123. frequency = NUMerbToHertz (frequency);
  124. }
  125. }
  126. point -> value = frequency;
  127. }
  128. } catch (MelderError) {
  129. Melder_throw (me, U": not all frequencies were shifted.");
  130. }
  131. }
  132. void PitchTier_multiplyFrequencies (PitchTier me, double tmin, double tmax, double factor) {
  133. Melder_assert (factor > 0.0);
  134. for (integer i = 1; i <= my points.size; i ++) {
  135. RealPoint point = my points.at [i];
  136. if (point -> number < tmin || point -> number > tmax) continue;
  137. point -> value *= factor;
  138. }
  139. }
  140. /* End of file PitchTier.cpp */