| 1 | /* PitchTier_to_PointProcess.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2005,2011,2012,2015-2017,2019,2023,2024 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 "PitchTier_to_PointProcess.h"cxx . . $ `<file>/fon/PitchTier_to_PointProcess.h`/ |
| 20 | #include "Pitch_to_PitchTier.h"cxx . . $ `<file>/fon/Pitch_to_PitchTier.h`/ |
| 21 | |
| 22 | autoPointProcesscxx . . $ autoPointProcess# PitchTier_to_PointProcesscxx . . $ PitchTier_to_PointProcess(f0370c77e10beb54). (PitchTiercxx . . $ PitchTier# me) { |
| 23 | try { |
| 24 | autoPointProcesscxx . . $ autoPointProcess# thee = PointProcess_createcxx . . $ PointProcess_create(7a858f446a8dd7cc). (mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax., 1000); |
| 25 | double area = 0.5; // imagine an event half a period before the beginning |
| 26 | integercxx . . $ integer# size = mycxx . . $ `melder/melder.h:44:9`! pointscxx . . $ structRealTier#points..sizecxx . . $ CollectionOf#size.; |
| 27 | if (size == 0) |
| 28 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 29 | for (integercxx . . $ integer# interval = 0; interval <= size; interval ++) { |
| 30 | const double t1 = ( interval == 0 ? mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin. : mycxx . . $ `melder/melder.h:44:9`! pointscxx . . $ structRealTier#points..atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).interval] -> numbercxx . . $ structSimpleDouble#number. ); |
| 31 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (isdefined (t1), |
| 32 | U"Time of point ", interval, U" not defined."); |
| 33 | const double t2 = ( interval == size ? mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax. : mycxx . . $ `melder/melder.h:44:9`! pointscxx . . $ structRealTier#points..atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).interval + 1] -> numbercxx . . $ structSimpleDouble#number. ); |
| 34 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (isdefined (t2), |
| 35 | U"Time of point ", interval + 1, U" not defined."); |
| 36 | const double f1 = mycxx . . $ `melder/melder.h:44:9`! pointscxx . . $ structRealTier#points..atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).interval == 0 ? 1 : interval] -> valuecxx . . $ structRealPoint#value.; |
| 37 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (isdefined (f1), |
| 38 | U"Pitch of point ", interval == 0 ? 1 : interval, U" not defined."); |
| 39 | const double f2 = mycxx . . $ `melder/melder.h:44:9`! pointscxx . . $ structRealTier#points..atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).interval == size ? size : interval + 1] -> valuecxx . . $ structRealPoint#value.; |
| 40 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (isdefined (f2), |
| 41 | U"Pitch of point ", interval == size ? size : interval + 1, U" not defined."); |
| 42 | area += (t2 - t1) * 0.5 * (f1 + f2); |
| 43 | while (area >= 1.0) { |
| 44 | const double slope = (f2 - f1) / (t2 - t1); |
| 45 | area -= 1.0; |
| 46 | const double discriminant = f2 * f2 - 2.0 * area * slope; |
| 47 | PointProcess_addPointcxx . . $ PointProcess_addPoint(29f3b64796387061). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), t2 - 2.0 * area / (f2 + NUMsqrt_0cxx . . $ NUMsqrt_0(447c67c6676f75c3). (discriminant))); |
| 48 | } |
| 49 | } |
| 50 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 51 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 52 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not converted to PointProcess."); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | autoPointProcesscxx . . $ autoPointProcess# PitchTier_Pitch_to_PointProcesscxx . . $ PitchTier_Pitch_to_PointProcess(27581ade3a75b545). (PitchTiercxx . . $ PitchTier# me, Pitchcxx . . $ Pitch# vuv) { |
| 57 | try { |
| 58 | autoPointProcesscxx . . $ autoPointProcess# fullPoint = PitchTier_to_PointProcesscxx . . $ PitchTier_to_PointProcess(f0370c77e10beb54). (me); |
| 59 | autoPointProcesscxx . . $ autoPointProcess# thee = PointProcess_createcxx . . $ PointProcess_create(7a858f446a8dd7cc). (mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax., fullPoint ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). ntcxx . . $ structPointProcess#nt.); |
| 60 | /* |
| 61 | Copy only voiced parts to result. |
| 62 | */ |
| 63 | for (integercxx . . $ integer# i = 1; i <= fullPoint ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). ntcxx . . $ structPointProcess#nt.; i ++) { |
| 64 | const double t = fullPoint ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). tcxx . . $ structPointProcess#t. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]; |
| 65 | if (Pitch_isVoiced_tcxx . . $ Pitch_isVoiced_t(9b4a997843717fd9). (vuv, t)) |
| 66 | PointProcess_addPointcxx . . $ PointProcess_addPoint(29f3b64796387061). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), t); |
| 67 | } |
| 68 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 69 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 70 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U" & ", vuv, U": not converted to PointProcess."); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | static bool PointProcess_isVoiced_tcxx . . $ PointProcess_isVoiced_t(e25b33f511a8ee9a). (PointProcesscxx . . $ PointProcess# me, const double t, const double maximumPeriod) { |
| 75 | const integercxx . . $ integer# imid = PointProcess_getNearestIndexcxx . . $ PointProcess_getNearestIndex(530dc1bca782083d). (me, t); |
| 76 | if (imid == 0) |
| 77 | return false; |
| 78 | const double tmid = mycxx . . $ `melder/melder.h:44:9`! tcxx . . $ structPointProcess#t. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).imid]; |
| 79 | const bool leftVoiced = ( imid > 1 && tmid - mycxx . . $ `melder/melder.h:44:9`! tcxx . . $ structPointProcess#t. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).imid - 1] <= maximumPeriod ); |
| 80 | const bool rightVoiced = ( imid < mycxx . . $ `melder/melder.h:44:9`! ntcxx . . $ structPointProcess#nt. && mycxx . . $ `melder/melder.h:44:9`! tcxx . . $ structPointProcess#t. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).imid + 1] - tmid <= maximumPeriod ); |
| 81 | if (leftVoiced && t <= tmid || rightVoiced && t >= tmid) |
| 82 | return true; |
| 83 | if (leftVoiced && t < 1.5 * tmid - 0.5 * mycxx . . $ `melder/melder.h:44:9`! tcxx . . $ structPointProcess#t. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).imid - 1]) |
| 84 | return true; |
| 85 | if (rightVoiced && t > 1.5 * tmid - 0.5 * mycxx . . $ `melder/melder.h:44:9`! tcxx . . $ structPointProcess#t. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).imid + 1]) |
| 86 | return true; |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | autoPointProcesscxx . . $ autoPointProcess# PitchTier_Point_to_PointProcesscxx . . $ PitchTier_Point_to_PointProcess(cf9e1213cef3e948). (PitchTiercxx . . $ PitchTier# me, PointProcesscxx . . $ PointProcess# vuv, const double maximumPeriod) { |
| 91 | try { |
| 92 | autoPointProcesscxx . . $ autoPointProcess# fullPoint = PitchTier_to_PointProcesscxx . . $ PitchTier_to_PointProcess(f0370c77e10beb54). (me); |
| 93 | autoPointProcesscxx . . $ autoPointProcess# thee = PointProcess_createcxx . . $ PointProcess_create(7a858f446a8dd7cc). (mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax., fullPoint ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). ntcxx . . $ structPointProcess#nt.); |
| 94 | /* |
| 95 | Copy only voiced parts to result. |
| 96 | */ |
| 97 | for (integercxx . . $ integer# i = 1; i <= fullPoint ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). ntcxx . . $ structPointProcess#nt.; i ++) { |
| 98 | const double t = fullPoint ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). tcxx . . $ structPointProcess#t. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]; |
| 99 | if (PointProcess_isVoiced_tcxx . . $ PointProcess_isVoiced_t(e25b33f511a8ee9a). (vuv, t, maximumPeriod)) |
| 100 | PointProcess_addPointcxx . . $ PointProcess_addPoint(29f3b64796387061). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), t); |
| 101 | } |
| 102 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 103 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 104 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U" & ", vuv, U": not converted to PointProcess."); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | autoPitchTiercxx . . $ autoPitchTier# PointProcess_to_PitchTiercxx . . $ PointProcess_to_PitchTier(abb1168919acef87). (PointProcesscxx . . $ PointProcess# me, const double maximumPeriod) { |
| 109 | try { |
| 110 | autoPitchTiercxx . . $ autoPitchTier# thee = PitchTier_createcxx . . $ PitchTier_create(6e304f2c8cc67c9c). (mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax.); |
| 111 | for (integercxx . . $ integer# i = 1; i < mycxx . . $ `melder/melder.h:44:9`! ntcxx . . $ structPointProcess#nt.; i ++) { |
| 112 | const double interval = mycxx . . $ `melder/melder.h:44:9`! tcxx . . $ structPointProcess#t. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i + 1] - mycxx . . $ `melder/melder.h:44:9`! tcxx . . $ structPointProcess#t. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]; |
| 113 | if (interval <= maximumPeriod) |
| 114 | RealTier_addPointcxx . . $ RealTier_addPoint(d84c0874d617a544). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), mycxx . . $ `melder/melder.h:44:9`! tcxx . . $ structPointProcess#t. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] + 0.5 * interval, 1.0 / interval); |
| 115 | } |
| 116 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 117 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 118 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not converted to PitchTier."); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | autoPitchTiercxx . . $ autoPitchTier# Pitch_PointProcess_to_PitchTiercxx . . $ Pitch_PointProcess_to_PitchTier(28040616f6cd7c5f). (Pitchcxx . . $ Pitch# me, PointProcesscxx . . $ PointProcess# pp) { |
| 123 | try { |
| 124 | autoPitchTiercxx . . $ autoPitchTier# temp = Pitch_to_PitchTiercxx . . $ Pitch_to_PitchTier(511873a878854c23). (me); |
| 125 | autoPitchTiercxx . . $ autoPitchTier# thee = PitchTier_PointProcess_to_PitchTiercxx . . $ PitchTier_PointProcess_to_PitchTier(86e9ab3420d037b8). (temp.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), pp); |
| 126 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 127 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 128 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U" & ", pp, U": not converted to PitchTier."); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | autoPitchTiercxx . . $ autoPitchTier# PitchTier_PointProcess_to_PitchTiercxx . . $ PitchTier_PointProcess_to_PitchTier(86e9ab3420d037b8). (PitchTiercxx . . $ PitchTier# me, PointProcesscxx . . $ PointProcess# pp) { |
| 133 | try { |
| 134 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! points.size > 0, |
| 135 | U"No pitch points."); |
| 136 | autoPitchTiercxx . . $ autoPitchTier# thee = PitchTier_createcxx . . $ PitchTier_create(6e304f2c8cc67c9c). (pp -> xmincxx . . $ structFunction#xmin., pp -> xmaxcxx . . $ structFunction#xmax.); |
| 137 | for (integercxx . . $ integer# i = 1; i <= pp -> ntcxx . . $ structPointProcess#nt.; i ++) { |
| 138 | const double time = pp -> tcxx . . $ structPointProcess#t. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]; |
| 139 | const double value = RealTier_getValueAtTimecxx . . $ RealTier_getValueAtTime(22f4103126fb5be4). (me, time); |
| 140 | RealTier_addPointcxx . . $ RealTier_addPoint(d84c0874d617a544). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), time, value); |
| 141 | } |
| 142 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 143 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 144 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U" & ", pp, U": not converted to PitchTier."); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | autoTableOfRealcxx . . $ autoTableOfReal# PitchTier_downto_TableOfRealcxx . . $ PitchTier_downto_TableOfReal(68059cf93943bc3b). (const constPitchTiercxx . . $ constPitchTier# me, const bool useSemitones) { |
| 149 | try { |
| 150 | autoTableOfRealcxx . . $ autoTableOfReal# thee = RealTier_downto_TableOfRealcxx . . $ RealTier_downto_TableOfReal(94a239d0d7c55341). (me, U"Time", U"F0"); |
| 151 | if (useSemitones) |
| 152 | for (integercxx . . $ integer# i = 1; i <= thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; i ++) |
| 153 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).2] = NUMhertzToSemitonescxx . . $ NUMhertzToSemitones(447c67c6676f75c3). (thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).2]); |
| 154 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 155 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 156 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not converted to TableOfReal."); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /* End of file PitchTier_to_PointProcess.cpp */ |