| 1 | /* PointProcess_and_Sound.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2012,2014-2018,2021 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 "PointProcess_and_Sound.h"cxx . . $ `<file>/fon/PointProcess_and_Sound.h`/ |
| 20 | |
| 21 | autoSoundcxx . . $ autoSound# PointProcess_to_Sound_pulseTraincxx . . $ PointProcess_to_Sound_pulseTrain(187ff6b615b4a390). |
| 22 | (PointProcesscxx . . $ PointProcess# me, double samplingFrequency, |
| 23 | double adaptFactor, double adaptTime, integercxx . . $ integer# interpolationDepth) |
| 24 | { |
| 25 | try { |
| 26 | const integercxx . . $ integer# sound_nt = 1 + Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! ((mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax. - mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin.) * samplingFrequency); // >= 1 |
| 27 | const double dt = 1.0 / samplingFrequency; |
| 28 | const double tmid = 0.5 * (mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin. + mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax.); |
| 29 | const double t1 = tmid - 0.5 * (sound_nt - 1) * dt; |
| 30 | autoSoundcxx . . $ autoSound# thee = Sound_createcxx . . $ Sound_create(39ab106d6b9a0135). (1, mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax., sound_nt, dt, t1); |
| 31 | const VECcxx . . $ VEC# sound = thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1); |
| 32 | for (integercxx . . $ integer# it = 1; it <= mycxx . . $ `melder/melder.h:44:9`! ntcxx . . $ structPointProcess#nt.; it ++) { |
| 33 | const double t = mycxx . . $ `melder/melder.h:44:9`! tcxx . . $ structPointProcess#t. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).it]; |
| 34 | double amplitude = 0.9; |
| 35 | integercxx . . $ integer# mid = Sampled_xToNearestIndexcxx . . $ Sampled_xToNearestIndex(405c2491bc3636e9). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), t); |
| 36 | if (it <= 2 || mycxx . . $ `melder/melder.h:44:9`! tcxx . . $ structPointProcess#t. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).it - 2] < mycxx . . $ `melder/melder.h:44:9`! tcxx . . $ structPointProcess#t. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).it] - adaptTime) { |
| 37 | amplitude *= adaptFactor; |
| 38 | if (it == 1 || mycxx . . $ `melder/melder.h:44:9`! tcxx . . $ structPointProcess#t. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).it - 1] < mycxx . . $ `melder/melder.h:44:9`! tcxx . . $ structPointProcess#t. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).it] - adaptTime) |
| 39 | amplitude *= adaptFactor; |
| 40 | } |
| 41 | integercxx . . $ integer# begin = mid - interpolationDepth, end = mid + interpolationDepth; |
| 42 | if (begin < 1) begin = 1; |
| 43 | if (end > thycxx . . $ `melder/melder.h:45:9`! nxcxx . . $ structSampled#nx.) end = thycxx . . $ `melder/melder.h:45:9`! nxcxx . . $ structSampled#nx.; |
| 44 | double angle = NUMpicxx . . $ NUMpi. * (Sampled_indexToXcxx . . $ Sampled_indexToX(65bcd2c9acbb3dd5). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), begin) - t) / thycxx . . $ `melder/melder.h:45:9`! dxcxx . . $ structSampled#dx.; |
| 45 | double halfampsinangle = 0.5 * amplitude * sincxx . . $ sin(fee4f1d22bc8e617). (angle); |
| 46 | for (integercxx . . $ integer# j = begin; j <= end; j ++) { |
| 47 | if (fabscxx . . $ fabs(fee4f1d22bc8e617). (angle) < 1e-6) |
| 48 | sound [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] += amplitude; |
| 49 | else if (angle < 0.0) |
| 50 | sound [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] += halfampsinangle * |
| 51 | (1.0 + coscxx . . $ cos(fee4f1d22bc8e617). (angle / (mid - begin + 1))) / angle; |
| 52 | else |
| 53 | sound [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] += halfampsinangle * |
| 54 | (1.0 + coscxx . . $ cos(fee4f1d22bc8e617). (angle / (end - mid + 1))) / angle; |
| 55 | angle += NUMpicxx . . $ NUMpi.; |
| 56 | halfampsinangle = - halfampsinangle; |
| 57 | } |
| 58 | } |
| 59 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 60 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 61 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": pulse train not synthesized."); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | autoSoundcxx . . $ autoSound# PointProcess_to_Sound_phonationcxx . . $ PointProcess_to_Sound_phonation(b0e41f72db7b1350). |
| 66 | (PointProcesscxx . . $ PointProcess# me, double samplingFrequency, double adaptFactor, double maximumPeriod, |
| 67 | double openPhase, double collisionPhase, double power1, double power2) |
| 68 | { |
| 69 | try { |
| 70 | const integercxx . . $ integer# sound_nt = 1 + Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! ((mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax. - mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin.) * samplingFrequency); // >= 1 |
| 71 | const double dt = 1.0 / samplingFrequency; |
| 72 | const double tmid = 0.5 * (mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin. + mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax.); |
| 73 | const double t1 = tmid - 0.5 * (sound_nt - 1) * dt; |
| 74 | const double a = (power1 + power2 + 1.0) / (power2 - power1); |
| 75 | autoSoundcxx . . $ autoSound# thee = Sound_createcxx . . $ Sound_create(39ab106d6b9a0135). (1, mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax., sound_nt, dt, t1); |
| 76 | /* |
| 77 | Compute "re" by iteration. |
| 78 | */ |
| 79 | double re = openPhase - collisionPhase; |
| 80 | if (collisionPhase <= 0.0) { |
| 81 | re = openPhase; |
| 82 | } else { |
| 83 | const double xmaxFlow = powcxx . . $ pow(b71928d39fde4295). (power1 / power2, 1.0 / (power2 - power1)); |
| 84 | /* mutable by iteration */ double xleft = xmaxFlow; |
| 85 | /* mutable by iteration */ double xright = 1.0; |
| 86 | for (int i = 1; i <= 50; i ++) { |
| 87 | const double xmid = 0.5 * (xleft + xright); |
| 88 | const double gmid = powcxx . . $ pow(b71928d39fde4295). (xmid, power1) - powcxx . . $ pow(b71928d39fde4295). (xmid, power2); |
| 89 | const double gderivmid = power1 * powcxx . . $ pow(b71928d39fde4295). (xmid, power1 - 1.0) - power2 * powcxx . . $ pow(b71928d39fde4295). (xmid, power2 - 1.0); |
| 90 | const double fmid = - gmid / gderivmid; |
| 91 | if (fmid > collisionPhase / openPhase) |
| 92 | xleft = xmid; |
| 93 | else |
| 94 | xright = xmid; |
| 95 | re = xmid * openPhase; |
| 96 | } |
| 97 | } |
| 98 | /* |
| 99 | Cycle through the points. Each will become a period. |
| 100 | */ |
| 101 | VECcxx . . $ VEC# sound = thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1); |
| 102 | for (integercxx . . $ integer# it = 1; it <= mycxx . . $ `melder/melder.h:44:9`! ntcxx . . $ structPointProcess#nt.; it ++) { |
| 103 | const double t = mycxx . . $ `melder/melder.h:44:9`! tcxx . . $ structPointProcess#t. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).it]; |
| 104 | const integercxx . . $ integer# midSample = Sampled_xToNearestIndexcxx . . $ Sampled_xToNearestIndex(405c2491bc3636e9). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), t); |
| 105 | /* |
| 106 | Determine the period: first look left (because that's where the open phase is), |
| 107 | then right. |
| 108 | */ |
| 109 | double period = undefinedcxx . . $ undefined.; |
| 110 | if (it >= 2) { |
| 111 | period = mycxx . . $ `melder/melder.h:44:9`! tcxx . . $ structPointProcess#t. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).it] - mycxx . . $ `melder/melder.h:44:9`! tcxx . . $ structPointProcess#t. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).it - 1]; |
| 112 | if (period > maximumPeriod) |
| 113 | period = undefinedcxx . . $ undefined.; |
| 114 | } |
| 115 | if (isundefcxx . . $ isundef(f8197d536af80c96). (period)) { |
| 116 | if (it < mycxx . . $ `melder/melder.h:44:9`! ntcxx . . $ structPointProcess#nt.) { |
| 117 | period = mycxx . . $ `melder/melder.h:44:9`! tcxx . . $ structPointProcess#t. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).it + 1] - mycxx . . $ `melder/melder.h:44:9`! tcxx . . $ structPointProcess#t. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).it]; |
| 118 | if (period > maximumPeriod) |
| 119 | period = undefinedcxx . . $ undefined.; |
| 120 | } |
| 121 | if (isundefcxx . . $ isundef(f8197d536af80c96). (period)) |
| 122 | period = 0.5 * maximumPeriod; // some default value |
| 123 | } |
| 124 | const double te = re * period; |
| 125 | /* |
| 126 | Determine the amplitude of this peak. |
| 127 | */ |
| 128 | const double unadaptedAmplitude = a / (period * openPhase); |
| 129 | const double amplitude = unadaptedAmplitude * ( |
| 130 | it == 1 || mycxx . . $ `melder/melder.h:44:9`! tcxx . . $ structPointProcess#t. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).it - 1] < mycxx . . $ `melder/melder.h:44:9`! tcxx . . $ structPointProcess#t. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).it] - maximumPeriod ? |
| 131 | adaptFactor * adaptFactor |
| 132 | : it == 2 || mycxx . . $ `melder/melder.h:44:9`! tcxx . . $ structPointProcess#t. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).it - 2] < mycxx . . $ `melder/melder.h:44:9`! tcxx . . $ structPointProcess#t. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).it - 1] - maximumPeriod ? |
| 133 | adaptFactor |
| 134 | : |
| 135 | 1.0 |
| 136 | ); |
| 137 | /* |
| 138 | Fill in the samples to the left of the current point. |
| 139 | */ |
| 140 | {// scope |
| 141 | const integercxx . . $ integer# beginSample = Melder_clippedLeftcxx . . $ Melder_clippedLeft(f80f7d6260b3ee3a). (1_integercxx . . $ `operator""_integer`(85240eb457c3721a)., midSample - Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (te / thycxx . . $ `melder/melder.h:45:9`! dxcxx . . $ structSampled#dx.)); |
| 142 | const integercxx . . $ integer# endSample = Melder_clippedRightcxx . . $ Melder_clippedRight(f80f7d6260b3ee3a). (midSample, thycxx . . $ `melder/melder.h:45:9`! nxcxx . . $ structSampled#nx.); |
| 143 | for (integercxx . . $ integer# isamp = beginSample; isamp <= endSample; isamp ++) { |
| 144 | const double tsamp = thycxx . . $ `melder/melder.h:45:9`! x1cxx . . $ structSampled#x1. + (isamp - 1) * thycxx . . $ `melder/melder.h:45:9`! dxcxx . . $ structSampled#dx.; |
| 145 | const double phase = (tsamp - (t - te)) / (period * openPhase); |
| 146 | if (phase > 0.0) |
| 147 | sound [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).isamp] += amplitude * (power1 * powcxx . . $ pow(b71928d39fde4295). (phase, power1 - 1.0) - power2 * powcxx . . $ pow(b71928d39fde4295). (phase, power2 - 1.0)); |
| 148 | } |
| 149 | } |
| 150 | /* |
| 151 | Determine the signal parameters at the current point. |
| 152 | */ |
| 153 | const double phase = te / (period * openPhase); |
| 154 | const double flow = amplitude * (period * openPhase) * (powcxx . . $ pow(b71928d39fde4295). (phase, power1) - powcxx . . $ pow(b71928d39fde4295). (phase, power2)); |
| 155 | /* |
| 156 | Fill in the samples to the right of the current point. |
| 157 | */ |
| 158 | if (flow > 0.0) { |
| 159 | const double flowDerivative = amplitude * (power1 * powcxx . . $ pow(b71928d39fde4295). (phase, power1 - 1.0) - power2 * powcxx . . $ pow(b71928d39fde4295). (phase, power2 - 1.0)); |
| 160 | const double ta = - flow / flowDerivative; |
| 161 | const double factorPerSample = expcxx . . $ exp(fee4f1d22bc8e617). (- thycxx . . $ `melder/melder.h:45:9`! dxcxx . . $ structSampled#dx. / ta); |
| 162 | const integercxx . . $ integer# beginSample = Melder_clippedLeftcxx . . $ Melder_clippedLeft(f80f7d6260b3ee3a). (1_integercxx . . $ `operator""_integer`(85240eb457c3721a)., midSample + 1); |
| 163 | const integercxx . . $ integer# endSample = Melder_clippedRightcxx . . $ Melder_clippedRight(f80f7d6260b3ee3a). (midSample + Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (20.0 * ta / thycxx . . $ `melder/melder.h:45:9`! dxcxx . . $ structSampled#dx.), thycxx . . $ `melder/melder.h:45:9`! nxcxx . . $ structSampled#nx.); |
| 164 | double value = flowDerivative * factorPerSample; |
| 165 | for (integercxx . . $ integer# isamp = beginSample; isamp <= endSample; isamp ++) { |
| 166 | sound [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).isamp] += value; |
| 167 | value *= factorPerSample; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | Vector_scalecxx . . $ Vector_scale(54f31cdb89d1acde). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 0.9); |
| 172 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 173 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 174 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not converted to Sound (phonation)."); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void PointProcess_playPartcxx . . $ PointProcess_playPart(76318b02fce8ae70). (PointProcesscxx . . $ PointProcess# me, double tmin, double tmax, Sound_PlayCallbackcxx . . $ Sound_PlayCallback# callback, Thingcxx . . $ Thing# boss) { |
| 179 | try { |
| 180 | autoSoundcxx . . $ autoSound# sound = PointProcess_to_Sound_pulseTraincxx . . $ PointProcess_to_Sound_pulseTrain(187ff6b615b4a390). (me, 44100.0, 0.7, 0.05, 30); |
| 181 | Sound_playPartcxx . . $ Sound_playPart(4a8b47ed00757136). (sound.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), tmin, tmax, callback, boss); |
| 182 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 183 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not played."); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | void PointProcess_playcxx . . $ PointProcess_play(9b74fc6f188c3c59). (PointProcesscxx . . $ PointProcess# me, Sound_PlayCallbackcxx . . $ Sound_PlayCallback# callback, Thingcxx . . $ Thing# boss) { |
| 188 | PointProcess_playPartcxx . . $ PointProcess_playPart(76318b02fce8ae70). (me, mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax., callback, boss); |
| 189 | } |
| 190 | |
| 191 | void PointProcess_humcxx . . $ PointProcess_hum(76318b02fce8ae70). (PointProcesscxx . . $ PointProcess# me, double tmin, double tmax, Sound_PlayCallbackcxx . . $ Sound_PlayCallback# callback, Thingcxx . . $ Thing# boss) { |
| 192 | static double formant [1 + 6] = { 0, 600.0, 1400.0, 2400.0, 3400.0, 4500.0, 5500.0 }; |
| 193 | static double bandwidth [1 + 6] = { 0, 50.0, 100.0, 200.0, 300.0, 400.0, 500.0 }; |
| 194 | autoSoundcxx . . $ autoSound# sound = PointProcess_to_Sound_pulseTraincxx . . $ PointProcess_to_Sound_pulseTrain(187ff6b615b4a390). (me, 44100, 0.7, 0.05, 30); |
| 195 | Sound_filterWithFormantscxx . . $ Sound_filterWithFormants(2aef0a9fc25232a8). (sound.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), tmin, tmax, 6, formant, bandwidth); |
| 196 | Sound_playPartcxx . . $ Sound_playPart(4a8b47ed00757136). (sound.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), tmin, tmax, callback, boss); |
| 197 | } |
| 198 | |
| 199 | autoSoundcxx . . $ autoSound# PointProcess_to_Sound_humcxx . . $ PointProcess_to_Sound_hum(e320a0d398950b85). (PointProcesscxx . . $ PointProcess# me) { |
| 200 | static double formant [1 + 6] = { 0, 600.0, 1400.0, 2400.0, 3400.0, 4500.0, 5500.0 }; |
| 201 | static double bandwidth [1 + 6] = { 0, 50.0, 100.0, 200.0, 300.0, 400.0, 500.0 }; |
| 202 | try { |
| 203 | autoSoundcxx . . $ autoSound# sound = PointProcess_to_Sound_pulseTraincxx . . $ PointProcess_to_Sound_pulseTrain(187ff6b615b4a390). (me, 44100.0, 0.7, 0.05, 30); |
| 204 | Sound_filterWithFormantscxx . . $ Sound_filterWithFormants(2aef0a9fc25232a8). (sound.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax., 6, formant, bandwidth); |
| 205 | return soundcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 206 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 207 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not converted to Sound (hum)."); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /* End of file PointProcess_and_Sound.cpp */ |