| 1 | /* Pitch_to_PitchTier.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2005,2007,2009-2012,2014-2020 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 | |
| 19 | #include "Pitch_to_PitchTier.h"cxx . . $ `<file>/fon/Pitch_to_PitchTier.h`/ |
| 20 | |
| 21 | autoPitchTiercxx . . $ autoPitchTier# Pitch_to_PitchTiercxx . . $ Pitch_to_PitchTier(511873a878854c23). (Pitchcxx . . $ Pitch# me) { |
| 22 | try { |
| 23 | autoPitchTiercxx . . $ autoPitchTier# you = PitchTier_createcxx . . $ PitchTier_create(6e304f2c8cc67c9c). (mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax.); |
| 24 | for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; i ++) { |
| 25 | const double frequency = mycxx . . $ `melder/melder.h:44:9`! framescxx . . $ structPitch#frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]. candidatescxx . . $ structPitch_Frame#candidates. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1]. frequencycxx . . $ structPitch_Candidate#frequency.; |
| 26 | /* |
| 27 | Count only voiced frames. |
| 28 | */ |
| 29 | if (Pitch_util_frequencyIsVoicedcxx . . $ Pitch_util_frequencyIsVoiced(828b76dce88fac). (frequency, mycxx . . $ `melder/melder.h:44:9`! ceilingcxx . . $ structPitch#ceiling.)) { |
| 30 | const double time = Sampled_indexToXcxx . . $ Sampled_indexToX(65bcd2c9acbb3dd5). (me, i); |
| 31 | RealTier_addPointcxx . . $ RealTier_addPoint(d84c0874d617a544). (you.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), time, frequency); |
| 32 | } |
| 33 | } |
| 34 | return youcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 35 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 36 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not converted to PitchTier."); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | static void Pitch_linecxx . . $ Pitch_line(4d7d428eae0d28a9). (Pitchcxx . . $ Pitch# me, Graphicscxx . . $ Graphics# graphics, double tmin, double fleft, double tmax, double fright, |
| 41 | int nonPeriodicLineType) |
| 42 | { |
| 43 | /* |
| 44 | f = fleft + (t - tmin) * (fright - fleft) / (tmax - tmin); |
| 45 | */ |
| 46 | const int lineType = Graphics_inqLineTypecxx . . $ Graphics_inqLineType(d4bbe1b9daaaf11b). (graphics); |
| 47 | const double lineWidth = Graphics_inqLineWidthcxx . . $ Graphics_inqLineWidth(f906975acce41a59). (graphics); |
| 48 | const double slope = (fright - fleft) / (tmax - tmin); |
| 49 | const integercxx . . $ integer# imin = Melder_clippedLeftcxx . . $ Melder_clippedLeft(f80f7d6260b3ee3a). (1_integercxx . . $ `operator""_integer`(85240eb457c3721a)., Sampled_xToNearestIndexcxx . . $ Sampled_xToNearestIndex(405c2491bc3636e9). (me, tmin)); |
| 50 | const integercxx . . $ integer# imax = Melder_clippedRightcxx . . $ Melder_clippedRight(f80f7d6260b3ee3a). (Sampled_xToNearestIndexcxx . . $ Sampled_xToNearestIndex(405c2491bc3636e9). (me, tmax), mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.); |
| 51 | for (integercxx . . $ integer# i = imin; i <= imax; i ++) { |
| 52 | if (! Pitch_isVoiced_icxx . . $ Pitch_isVoiced_i(fd5b671aa7b53262). (me, i)) { |
| 53 | if (nonPeriodicLineType == 2) |
| 54 | continue; |
| 55 | Graphics_setLineTypecxx . . $ Graphics_setLineType(9abe41586e87648d). (graphics, Graphics_DOTTEDcxx . . $ `sys/Graphics.h:288:9`!); |
| 56 | Graphics_setLineWidthcxx . . $ Graphics_setLineWidth(2514a1c07b6be5e2). (graphics, 0.67 * lineWidth); |
| 57 | } else if (nonPeriodicLineType != 2) { |
| 58 | Graphics_setLineWidthcxx . . $ Graphics_setLineWidth(2514a1c07b6be5e2). (graphics, 2 * lineWidth); |
| 59 | } |
| 60 | double tleft = Sampled_indexToXcxx . . $ Sampled_indexToX(65bcd2c9acbb3dd5). (me, i) - 0.5 * mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.; |
| 61 | double tright = tleft + mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.; |
| 62 | Melder_clipLeftcxx . . $ Melder_clipLeft(6245cd023a7d1552). (tmin, & tleft); // has to be ordered after the previous line! |
| 63 | Melder_clipRightcxx . . $ Melder_clipRight(5b63ce711df1ade7). (& tright, tmax); |
| 64 | Graphics_linecxx . . $ Graphics_line(72bdb7937dbb13da). (graphics, |
| 65 | tleft, fleft + (tleft - tmin) * slope, |
| 66 | tright, fleft + (tright - tmin) * slope |
| 67 | ); |
| 68 | Graphics_setLineTypecxx . . $ Graphics_setLineType(9abe41586e87648d). (graphics, lineType); |
| 69 | Graphics_setLineWidthcxx . . $ Graphics_setLineWidth(2514a1c07b6be5e2). (graphics, lineWidth); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | void PitchTier_Pitch_drawcxx . . $ PitchTier_Pitch_draw(6802f4c024091135). (PitchTiercxx . . $ PitchTier# me, Pitchcxx . . $ Pitch# uv, Graphicscxx . . $ Graphics# graphics, |
| 74 | double tmin, double tmax, double fmin, double fmax, int nonPeriodicLineType, bool garnish, conststring32cxx . . $ conststring32# method) |
| 75 | { |
| 76 | if (nonPeriodicLineType == 0) { |
| 77 | PitchTier_drawcxx . . $ PitchTier_draw(a0f332ff6e8fdb02). (me, graphics, tmin, tmax, fmin, fmax, garnish, method); |
| 78 | return; |
| 79 | } |
| 80 | Function_unidirectionalAutowindowcxx . . $ Function_unidirectionalAutowindow(88b7840c59a57eaf). (me, & tmin, & tmax); |
| 81 | Graphics_setWindowcxx . . $ Graphics_setWindow(72bdb7937dbb13da). (graphics, tmin, tmax, fmin, fmax); |
| 82 | Graphics_setInnercxx . . $ Graphics_setInner(2d838ed765120c5d). (graphics); |
| 83 | const integercxx . . $ integer# imin = AnyTier_timeToHighIndexcxx . . $ AnyTier_timeToHighIndex(9d58fc2471e8b6f6). (me->asAnyTiercxx . . $ structRealTier#asAnyTier(2fecc017ee6dfbab).(), tmin); |
| 84 | const integercxx . . $ integer# imax = AnyTier_timeToLowIndexcxx . . $ AnyTier_timeToLowIndex(9d58fc2471e8b6f6). (me->asAnyTiercxx . . $ structRealTier#asAnyTier(2fecc017ee6dfbab).(), tmax); |
| 85 | if (mycxx . . $ `melder/melder.h:44:9`! pointscxx . . $ structRealTier#points..sizecxx . . $ CollectionOf#size. == 0) { |
| 86 | } else if (imax < imin) { |
| 87 | const double fleft = RealTier_getValueAtTimecxx . . $ RealTier_getValueAtTime(22f4103126fb5be4). (me, tmin); |
| 88 | const double fright = RealTier_getValueAtTimecxx . . $ RealTier_getValueAtTime(22f4103126fb5be4). (me, tmax); |
| 89 | Pitch_linecxx . . $ Pitch_line(4d7d428eae0d28a9). (uv, graphics, tmin, fleft, tmax, fright, nonPeriodicLineType); |
| 90 | } else for (integercxx . . $ integer# i = imin; i <= imax; i ++) { |
| 91 | const RealPointcxx . . $ RealPoint# point = mycxx . . $ `melder/melder.h:44:9`! pointscxx . . $ structRealTier#points..atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).i]; |
| 92 | const double time = point -> numbercxx . . $ structSimpleDouble#number., frequency = point -> valuecxx . . $ structRealPoint#value.; |
| 93 | Graphics_specklecxx . . $ Graphics_speckle(c84ba9158791aef6). (graphics, time, frequency); |
| 94 | if (i == 1) |
| 95 | Pitch_linecxx . . $ Pitch_line(4d7d428eae0d28a9). (uv, graphics, tmin, frequency, time, frequency, nonPeriodicLineType); |
| 96 | else if (i == imin) |
| 97 | Pitch_linecxx . . $ Pitch_line(4d7d428eae0d28a9). (uv, graphics, time, frequency, tmin, RealTier_getValueAtTimecxx . . $ RealTier_getValueAtTime(22f4103126fb5be4). (me, tmin), nonPeriodicLineType); |
| 98 | if (i == mycxx . . $ `melder/melder.h:44:9`! pointscxx . . $ structRealTier#points..sizecxx . . $ CollectionOf#size.) |
| 99 | Pitch_linecxx . . $ Pitch_line(4d7d428eae0d28a9). (uv, graphics, time, frequency, tmax, frequency, nonPeriodicLineType); |
| 100 | else if (i == imax) |
| 101 | Pitch_linecxx . . $ Pitch_line(4d7d428eae0d28a9). (uv, graphics, time, frequency, tmax, RealTier_getValueAtTimecxx . . $ RealTier_getValueAtTime(22f4103126fb5be4). (me, tmax), nonPeriodicLineType); |
| 102 | else { |
| 103 | RealPointcxx . . $ RealPoint# pointRight = mycxx . . $ `melder/melder.h:44:9`! pointscxx . . $ structRealTier#points..atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).i + 1]; |
| 104 | Pitch_linecxx . . $ Pitch_line(4d7d428eae0d28a9). (uv, graphics, time, frequency, pointRight -> numbercxx . . $ structSimpleDouble#number., pointRight -> valuecxx . . $ structRealPoint#value., nonPeriodicLineType); |
| 105 | } |
| 106 | } |
| 107 | Graphics_unsetInnercxx . . $ Graphics_unsetInner(2d838ed765120c5d). (graphics); |
| 108 | if (garnish) { |
| 109 | Graphics_drawInnerBoxcxx . . $ Graphics_drawInnerBox(2d838ed765120c5d). (graphics); |
| 110 | Graphics_textBottomcxx . . $ Graphics_textBottom(48cf1f690ec61203). (graphics, true, U"Time (s)"); |
| 111 | Graphics_marksBottomcxx . . $ Graphics_marksBottom(c3a11a8a55b243ff). (graphics, 2, true, true, false); |
| 112 | Graphics_marksLeftcxx . . $ Graphics_marksLeft(c3a11a8a55b243ff). (graphics, 2, true, true, false); |
| 113 | Graphics_textLeftcxx . . $ Graphics_textLeft(48cf1f690ec61203). (graphics, true, U"Frequency (Hz)"); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | autoPitchcxx . . $ autoPitch# Pitch_PitchTier_to_Pitchcxx . . $ Pitch_PitchTier_to_Pitch(f1c8e8ac8d4876f3). (Pitchcxx . . $ Pitch# me, PitchTiercxx . . $ PitchTier# tier) { |
| 118 | try { |
| 119 | if (tier -> pointscxx . . $ structRealTier#points..sizecxx . . $ CollectionOf#size. == 0) |
| 120 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"No pitch points."); |
| 121 | autoPitchcxx . . $ autoPitch# you = Data_copycxx . . $ Data_copy(97a542a26794c80e). (me); |
| 122 | for (integercxx . . $ integer# iframe = 1; iframe <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; iframe ++) { |
| 123 | const Pitch_Framecxx . . $ Pitch_Frame# frame = & yourcxx . . $ `melder/melder.h:46:9`! framescxx . . $ structPitch#frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iframe]; |
| 124 | const Pitch_Candidatecxx . . $ Pitch_Candidate# candidate = & frame -> candidatescxx . . $ structPitch_Frame#candidates. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1]; |
| 125 | if (Pitch_util_frequencyIsVoicedcxx . . $ Pitch_util_frequencyIsVoiced(828b76dce88fac). (candidate -> frequencycxx . . $ structPitch_Candidate#frequency., mycxx . . $ `melder/melder.h:44:9`! ceilingcxx . . $ structPitch#ceiling.)) |
| 126 | candidate -> frequencycxx . . $ structPitch_Candidate#frequency. = RealTier_getValueAtTimecxx . . $ RealTier_getValueAtTime(22f4103126fb5be4). (tier, Sampled_indexToXcxx . . $ Sampled_indexToX(65bcd2c9acbb3dd5). (me, iframe)); |
| 127 | candidate -> strengthcxx . . $ structPitch_Candidate#strength. = 0.9; |
| 128 | frame -> candidatescxx . . $ structPitch_Frame#candidates.. resizecxx . . $ autovector#resize(d91e89fbd57cd1). (frame -> nCandidatescxx . . $ structPitch_Frame#nCandidates. = 1); |
| 129 | } |
| 130 | return youcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 131 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 132 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U" & ", tier, U": not converted to Pitch."); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /* End of file Pitch_to_PitchTier.cpp */ |