| 1 | /* Intensity_extensions.cpp |
| 2 | * |
| 3 | * Copyright (C) 2007-2019 David Weenink, 2015,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. See the GNU |
| 13 | * 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 | /* |
| 20 | djmw 20061204 Initial version |
| 21 | djmw 20070129 Extra argument for Vector_get<Maximum/Minimum>AndX |
| 22 | djmw 20071201 Melder_warning<n> |
| 23 | djmw 20090818 Intensity_to_TextGrid_detectSilences: mark last interval also if sounding |
| 24 | */ |
| 25 | |
| 26 | #include "Intensity_extensions.h" |
| 27 | #include "TextGrid_extensions.h" |
| 28 | |
| 29 | static void IntervalTier_addBoundaryUnsorted (IntervalTier me, integer iinterval, double time, conststring32 leftLabel) { |
| 30 | Melder_require (time > my xmin && time < my xmax, |
| 31 | U"Time is outside interval."); |
| 32 | /* |
| 33 | Find interval to split. |
| 34 | */ |
| 35 | if (iinterval <= 0) |
| 36 | iinterval = IntervalTier_timeToLowIndex (me, time); |
| 37 | |
| 38 | /* |
| 39 | Modify end time of left label. |
| 40 | */ |
| 41 | const TextInterval ti = my intervals.at [iinterval]; |
| 42 | ti -> xmax = time; |
| 43 | TextInterval_setText (ti, leftLabel); |
| 44 | |
| 45 | autoTextInterval ti_new = TextInterval_create (time, my xmax, U""); |
| 46 | my intervals. addItem_unsorted_move (ti_new.move()); |
| 47 | } |
| 48 | |
| 49 | autoTextGrid Intensity_to_TextGrid_detectSilences (Intensity me, |
| 50 | double silenceThreshold_dB, double minSilenceDuration, double minSoundingDuration, |
| 51 | conststring32 silenceLabel, conststring32 soundingLabel) |
| 52 | { |
| 53 | try { |
| 54 | const double duration = my xmax - my xmin; |
| 55 | |
| 56 | Melder_require (silenceThreshold_dB < 0.0, |
| 57 | U"The silence threshold w.r.t. the maximum intensity should be a negative number."); |
| 58 | |
| 59 | autoTextGrid thee = TextGrid_create (my xmin, my xmax, U"silences", U""); |
| 60 | const IntervalTier it = (IntervalTier) thy tiers->at [1]; |
| 61 | TextInterval_setText (it -> intervals.at [1], soundingLabel); |
| 62 | if (minSilenceDuration > duration) |
| 63 | return thee; |
| 64 | |
| 65 | double intensity_max_db, intensity_min_db, xOfMaximum, xOfMinimum; |
| 66 | Vector_getMaximumAndX (me, 0.0, 0.0, 1, kVector_peakInterpolation :: PARABOLIC, & intensity_max_db, & xOfMaximum); |
| 67 | Vector_getMinimumAndX (me, 0.0, 0.0, 1, kVector_peakInterpolation :: PARABOLIC, & intensity_min_db, & xOfMinimum); |
| 68 | const double intensity_dbRange = intensity_max_db - intensity_min_db; |
| 69 | |
| 70 | if (intensity_dbRange < 10.0) |
| 71 | Melder_warning (U"The loudest and softest part in your sound differ by only ", intensity_dbRange, U" dB."); |
| 72 | |
| 73 | const double intensityThreshold = intensity_max_db - fabs (silenceThreshold_dB); |
| 74 | |
| 75 | if (minSilenceDuration > duration || intensityThreshold < intensity_min_db) |
| 76 | return thee; |
| 77 | |
| 78 | bool inSilenceInterval = my z [1] [1] < intensityThreshold; |
| 79 | integer iinterval = 1; |
| 80 | conststring32 label; |
| 81 | for (integer i = 2; i <= my nx; i ++) { |
| 82 | bool addBoundary = false; |
| 83 | if (my z [1] [i] < intensityThreshold) { |
| 84 | if (! inSilenceInterval) { // start of silence |
| 85 | addBoundary = true; |
| 86 | inSilenceInterval = true; |
| 87 | label = soundingLabel; |
| 88 | } |
| 89 | } else { |
| 90 | if (inSilenceInterval) { // end of silence |
| 91 | addBoundary = true; |
| 92 | inSilenceInterval = false; |
| 93 | label = silenceLabel; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | if (addBoundary) { |
| 98 | const double time = my x1 + (i - 1) * my dx; |
| 99 | IntervalTier_addBoundaryUnsorted (it, iinterval, time, label); |
| 100 | iinterval ++; |
| 101 | } |
| 102 | } |
| 103 | /* |
| 104 | (re)label last interval. |
| 105 | */ |
| 106 | label = inSilenceInterval ? silenceLabel : soundingLabel; |
| 107 | TextInterval_setText (it -> intervals.at [iinterval], label); |
| 108 | it -> intervals. sort (); |
| 109 | |
| 110 | /* |
| 111 | First remove short non-silence intervals in-between silence intervals and |
| 112 | then remove the remaining short silence intervals. |
| 113 | This works much better than first removing short silence intervals and |
| 114 | then short non-silence intervals. |
| 115 | */ |
| 116 | if (minSoundingDuration > 0.0) { |
| 117 | IntervalTier_cutIntervals_minimumDuration (it, soundingLabel, minSoundingDuration); |
| 118 | IntervalTier_combineIntervalsOnLabelMatch (it, silenceLabel); |
| 119 | } |
| 120 | if (minSilenceDuration > 0.0) { |
| 121 | IntervalTier_cutIntervals_minimumDuration (it, silenceLabel, minSilenceDuration); |
| 122 | IntervalTier_combineIntervalsOnLabelMatch (it, soundingLabel); |
| 123 | } |
| 124 | |
| 125 | return thee; |
| 126 | } catch (MelderError) { |
| 127 | Melder_throw (me, U": TextGrid not created."); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | autoIntensity IntensityTier_to_Intensity (IntensityTier me, double dt) { |
| 132 | try { |
| 133 | const integer nt = Melder_ifloor ((my xmax - my xmin) / dt); |
| 134 | const double t1 = 0.5 * dt; |
| 135 | autoIntensity thee = Intensity_create (my xmin, my xmax, nt, dt, t1); |
| 136 | for (integer i = 1; i <= nt; i ++) { |
| 137 | const double time = t1 + (i - 1) * dt; |
| 138 | thy z [1] [i] = RealTier_getValueAtTime (me, time); |
| 139 | } |
| 140 | return thee; |
| 141 | } catch (MelderError) { |
| 142 | Melder_throw (me, U": Intensity not created."); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | autoTextGrid IntensityTier_to_TextGrid_detectSilences (IntensityTier me, double dt, double silenceThreshold_dB, double minSilenceDuration, |
| 147 | double minSoundingDuration, conststring32 silenceLabel, conststring32 soundingLabel) { |
| 148 | try { |
| 149 | autoIntensity intensity = IntensityTier_to_Intensity (me, dt); |
| 150 | autoTextGrid thee = Intensity_to_TextGrid_detectSilences (intensity.get(), silenceThreshold_dB, minSilenceDuration, minSoundingDuration, silenceLabel, soundingLabel); |
| 151 | return thee; |
| 152 | } catch (MelderError) { |
| 153 | Melder_throw (me, U": TextGrid not created."); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /* End of file Intensity_extensions.cpp */ |