Matrix_and_Pitch.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* Matrix_and_Pitch.cpp
  2. *
  3. * Copyright (C) 1992-2005,2011,2012,2015-2018 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 "Matrix_and_Pitch.h"
  19. autoMatrix Pitch_to_Matrix (Pitch me) {
  20. try {
  21. autoMatrix you = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, 1.0, 1.0, 1, 1.0, 1.0);
  22. for (integer i = 1; i <= my nx; i ++) {
  23. double value = my frame [i]. candidate [1]. frequency;
  24. your z [1] [i] =
  25. Pitch_util_frequencyIsVoiced (value, my ceiling) ? my frame [i]. candidate [1]. frequency : 0.0;
  26. }
  27. return you;
  28. } catch (MelderError) {
  29. Melder_throw (me, U": not converted to Matrix.");
  30. }
  31. }
  32. autoPitch Matrix_to_Pitch (Matrix me) {
  33. try {
  34. autoPitch you = Pitch_create (my xmin, my xmax, my nx, my dx, my x1, 5000.0, 2);
  35. for (integer i = 1; i <= my nx; i ++) {
  36. Pitch_Frame frame = & your frame [i];
  37. if (my z [1] [i] == 0.0) {
  38. Pitch_Frame_init (frame, 1);
  39. frame -> candidate [1]. frequency = 0.0; // voiceless candidate always present
  40. frame -> candidate [1]. strength = 0.4;
  41. } else {
  42. Pitch_Frame_init (frame, 2);
  43. frame -> intensity = 1;
  44. frame -> candidate [1]. frequency = my z [1] [i];
  45. frame -> candidate [1]. strength = 0.9;
  46. frame -> candidate [2]. frequency = 0.0; // voiceless candidate always present
  47. frame -> candidate [2]. strength = 0.4;
  48. }
  49. }
  50. return you;
  51. } catch (MelderError) {
  52. Melder_throw (me, U": not converted to Pitch.");
  53. }
  54. }
  55. void Pitch_formula (Pitch me, conststring32 formula, Interpreter interpreter) {
  56. try {
  57. autoMatrix m = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, 1.0, my maxnCandidates, my maxnCandidates, 1.0, 1.0);
  58. for (integer iframe = 1; iframe <= my nx; iframe ++) {
  59. Pitch_Frame frame = & my frame [iframe];
  60. for (integer icand = 1; icand <= frame -> nCandidates; icand ++)
  61. m -> z [icand] [iframe] = frame -> candidate [icand]. frequency;
  62. }
  63. Matrix_formula (m.get(), formula, interpreter, nullptr);
  64. for (integer iframe = 1; iframe <= my nx; iframe ++) {
  65. Pitch_Frame frame = & my frame [iframe];
  66. for (integer icand = 1; icand <= frame -> nCandidates; icand ++)
  67. frame -> candidate [icand]. frequency = m -> z [icand] [iframe];
  68. }
  69. } catch (MelderError) {
  70. Melder_throw (me, U": formula not completed.");
  71. }
  72. }
  73. /* End of file Matrix_and_Pitch.cpp */