| 1 | /* Sound_and_TextGrid_extensions.cpp |
| 2 | * |
| 3 | * Copyright (C) 1993-2022 David Weenink, 2024,2025 Paul Boersma, 2026 Anastasia Shchupak |
| 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 3 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 | #include "Intensity_extensions.h" |
| 20 | #include "Sound_extensions.h" |
| 21 | #include "Sound_and_TextGrid_extensions.h" |
| 22 | #include "Sound_and_Spectrum.h" |
| 23 | #include "Sound_to_Intensity.h" |
| 24 | #include "SpeechRecognizer.h" |
| 25 | #include "TextGrid_Sound.h" |
| 26 | |
| 27 | autoIntervalTier Sound_to_IntervalTier_highMidLowIntervals (Sound me, double min, double max) { |
| 28 | try { |
| 29 | autoIntervalTier thee = IntervalTier_create (my xmin, my xmax); |
| 30 | double startTime = my xmin, endTime = my xmax; |
| 31 | bool firstInterval = true; |
| 32 | VEC z (my z[1]); |
| 33 | int level = ( z [1] > max ? 1 : z [1] < min ? -1 : 0 ); |
| 34 | for (integer isamp = 2; isamp <= z.size; isamp ++) { |
| 35 | const int leveli = ( z [isamp] > max ? 1 : z [isamp] < min ? -1 : 0 ); |
| 36 | if (leveli == level) |
| 37 | continue; |
| 38 | // new interval |
| 39 | endTime = my x1 + (isamp - 1 - 0.5) * my dx; // midpoint |
| 40 | conststring32 label = ( level == 1 ? U"High" : level == -1 ? U"Low" : U"Mid" ); |
| 41 | if (firstInterval) { |
| 42 | TextInterval interval = thy intervals.at [1]; |
| 43 | interval -> xmax = endTime; |
| 44 | autostring32 firstLabel = Melder_dup (label); |
| 45 | interval -> text = firstLabel.move(); |
| 46 | firstInterval = false; |
| 47 | } else { |
| 48 | autoTextInterval interval = TextInterval_create (startTime, endTime, label); |
| 49 | thy intervals. addItem_move (interval.move()); |
| 50 | } |
| 51 | startTime = endTime; |
| 52 | level = leveli; |
| 53 | } |
| 54 | conststring32 last_label = ( level == 1 ? U"High" : level == -1 ? U"Low" : U"Mid" ); |
| 55 | autoTextInterval interval = TextInterval_create (startTime, my xmax, last_label); |
| 56 | thy intervals. addItem_move (interval.move()); |
| 57 | return thee; |
| 58 | } catch (MelderError) { |
| 59 | Melder_throw (me, U": no IntervalTier with High/Mid/Low intervals created."); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | autoTextGrid Sound_to_TextGrid_highMidLowIntervals (Sound me, double min, double max) { |
| 64 | try { |
| 65 | autoTextGrid thee = TextGrid_createWithoutTiers (my xmin, my xmax); |
| 66 | autoIntervalTier intervals = Sound_to_IntervalTier_highMidLowIntervals (me, min, max); |
| 67 | thy tiers -> addItem_move (intervals.move()); |
| 68 | return thee; |
| 69 | } catch (MelderError) { |
| 70 | Melder_throw (me, U": no TextGrid with High/Mid/Low intervals created."); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | autoSound Sound_IntervalTier_cutPartsMatchingLabel (Sound me, IntervalTier thee, conststring32 match) { |
| 75 | try { |
| 76 | /* |
| 77 | Count samples of the trimmed sound |
| 78 | */ |
| 79 | integer ixmin, ixmax, numberOfSamples = 0, previous_ixmax = 0; |
| 80 | double xmin = my xmin; // start time of output sound is start time of input sound |
| 81 | for (integer iint = 1; iint <= thy intervals.size; iint ++) { |
| 82 | TextInterval interval = thy intervals.at [iint]; |
| 83 | if (! Melder_equ (interval -> text.get(), match)) { |
| 84 | numberOfSamples += Sampled_getWindowSamples (me, interval -> xmin, interval -> xmax, & ixmin, & ixmax); |
| 85 | /* |
| 86 | If two contiguous intervals have to be copied then the last sample of previous interval |
| 87 | and first sample of current interval might sometimes be equal |
| 88 | */ |
| 89 | if (ixmin == previous_ixmax) |
| 90 | -- numberOfSamples; |
| 91 | previous_ixmax = ixmax; |
| 92 | } else { // matches label |
| 93 | if (iint == 1) // start time of output sound is end time of first interval |
| 94 | xmin = interval -> xmax; |
| 95 | } |
| 96 | } |
| 97 | /* |
| 98 | Now copy the parts. The output sound starts at xmin |
| 99 | */ |
| 100 | autoSound him = Sound_create (my ny, xmin, xmin + numberOfSamples * my dx, numberOfSamples, my dx, xmin + 0.5 * my dx); |
| 101 | numberOfSamples = 0; |
| 102 | previous_ixmax = 0; |
| 103 | for (integer iint = 1; iint <= thy intervals.size; iint ++) { |
| 104 | const TextInterval interval = thy intervals.at [iint]; |
| 105 | if (! Melder_equ (interval -> text.get(), match)) { |
| 106 | Sampled_getWindowSamples (me, interval -> xmin, interval -> xmax, & ixmin, & ixmax); |
| 107 | if (ixmin == previous_ixmax) |
| 108 | ixmin ++; |
| 109 | previous_ixmax = ixmax; |
| 110 | integer numberOfSamplesToCopy = ixmax - ixmin + 1; |
| 111 | his z.part (1, my ny, numberOfSamples + 1, numberOfSamples + numberOfSamplesToCopy) <<= my z.part (1, my ny, ixmin, ixmax); |
| 112 | numberOfSamples += numberOfSamplesToCopy; |
| 113 | } |
| 114 | } |
| 115 | Melder_assert (numberOfSamples == his nx); |
| 116 | return him; |
| 117 | } catch (MelderError) { |
| 118 | Melder_throw (me, U": intervals not trimmed."); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | autoTextGrid Sound_to_TextGrid_speechActivity_silero (constSound me, const double speechProbabilityThreshold, |
| 123 | const double minNonSpeechDuration, const double minSpeechDuration, const double speechPad, |
| 124 | const conststring32 nonSpeechLabel, const conststring32 speechLabel |
| 125 | ) { |
| 126 | try { |
| 127 | Melder_require (speechProbabilityThreshold <= 1.0, |
| 128 | U"The speech probability threshold should be in the interval [0, 1]"); |
| 129 | |
| 130 | autovector <SpeechSegment> vadIntervals = doSileroVad (me, speechProbabilityThreshold, |
| 131 | minNonSpeechDuration, minSpeechDuration, speechPad, nonSpeechLabel, speechLabel); |
| 132 | |
| 133 | autoTextGrid thee = TextGrid_create (my xmin, my xmax, U"VAD", U""); |
| 134 | const IntervalTier vadTier = static_cast <IntervalTier> (thy tiers->at [1]); |
| 135 | splitIntervalIntoWhisperSegments (vadTier, 1, my xmin, my xmax, vadIntervals); |
| 136 | |
| 137 | return thee; |
| 138 | } catch (MelderError) { |
| 139 | Melder_throw (me, U": could not detect speech activity."); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | autoTextGrid Sound_to_TextGrid_detectSilences (Sound me, double minPitch, double timeStep, |
| 144 | double silenceThreshold, double minSilenceDuration, double minSoundingDuration, |
| 145 | conststring32 silentLabel, conststring32 soundingLabel) |
| 146 | { |
| 147 | try { |
| 148 | const bool subtractMeanPressure = true; |
| 149 | autoSound filtered = Sound_filter_passHannBand (me, 80.0, 8000.0, 80.0); |
| 150 | autoIntensity thee = Sound_to_Intensity (filtered.get(), minPitch, timeStep, subtractMeanPressure); |
| 151 | autoTextGrid him = Intensity_to_TextGrid_detectSilences (thee.get(), silenceThreshold, minSilenceDuration, minSoundingDuration, silentLabel, soundingLabel); |
| 152 | return him; |
| 153 | } catch (MelderError) { |
| 154 | Melder_throw (me, U": no TextGrid with silences created."); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /* End of file Sound_and_TextGrid_extensions.cpp */ |