Pitch_AnyTier_to_PitchTier.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* Pitch_AnyTier_to_PitchTier.cpp
  2. *
  3. * Copyright (C) 1992-2011,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 "Pitch_AnyTier_to_PitchTier.h"
  19. #include "Pitch_to_PitchTier.h"
  20. autoPitchTier PitchTier_AnyTier_to_PitchTier (PitchTier pitch, AnyTier tier) {
  21. try {
  22. if (pitch -> points.size == 0) Melder_throw (U"No pitch points.");
  23. /*
  24. * Result's domain is a union of both domains.
  25. */
  26. autoPitchTier thee = PitchTier_create (
  27. pitch -> xmin < tier -> xmin ? pitch -> xmin : tier -> xmin,
  28. pitch -> xmax > tier -> xmax ? pitch -> xmax : tier -> xmax);
  29. /*
  30. * Copy pitch's frequencies at tier's points to the resulting PitchTier.
  31. */
  32. for (integer ipoint = 1; ipoint <= tier -> points.size; ipoint ++) {
  33. AnyPoint point = tier -> points.at [ipoint];
  34. double time = point -> number;
  35. double frequency = RealTier_getValueAtTime (pitch, time);
  36. RealTier_addPoint (thee.get(), time, frequency);
  37. }
  38. return thee;
  39. } catch (MelderError) {
  40. Melder_throw (pitch, U" & ", tier, U": not converted to PitchTier.");
  41. }
  42. }
  43. autoPitchTier Pitch_AnyTier_to_PitchTier (Pitch pitch, AnyTier tier, int checkMethod) {
  44. try {
  45. if (checkMethod == 2) {
  46. autoPitchTier temp = Pitch_to_PitchTier (pitch);
  47. return PitchTier_AnyTier_to_PitchTier (temp.get(), tier);
  48. }
  49. /*
  50. * Result's domain is a union of both domains.
  51. */
  52. autoPitchTier thee = PitchTier_create (
  53. pitch -> xmin < tier -> xmin ? pitch -> xmin : tier -> xmin,
  54. pitch -> xmax > tier -> xmax ? pitch -> xmax : tier -> xmax);
  55. /*
  56. * Copy pitch's frequencies at tier's points to the resulting PitchTier.
  57. */
  58. for (integer ipoint = 1; ipoint <= tier -> points.size; ipoint ++) {
  59. AnyPoint point = tier -> points.at [ipoint];
  60. double time = point -> number;
  61. double frequency = Pitch_getValueAtTime (pitch, time, kPitch_unit::HERTZ, Pitch_LINEAR);
  62. if (isundef (frequency) && checkMethod != 0)
  63. Melder_throw (U"No periodicity at time ", time, U" seconds.");
  64. RealTier_addPoint (thee.get(), time, frequency);
  65. }
  66. return thee;
  67. } catch (MelderError) {
  68. Melder_throw (pitch, U" & ", tier, U": not converted to PitchTier.");
  69. }
  70. }
  71. /* End of file Pitch_AnyTier_to_PitchTier.cpp */