File: Sound_and_TextGrid_extensions.cpp

package info (click to toggle)
praat 6.4.27%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 206,060 kB
  • sloc: cpp: 1,409,811; ansic: 286,305; makefile: 946; python: 340; sh: 35
file content (136 lines) | stat: -rw-r--r-- 5,487 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* Sound_and_TextGrid_extensions.cpp
 *
 * Copyright (C) 1993-2022 David Weenink, 2024 Paul Boersma
 *
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This code is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this work. If not, see <http://www.gnu.org/licenses/>.
 */

#include "Intensity_extensions.h"
#include "Sound_extensions.h"
#include "Sound_and_TextGrid_extensions.h"
#include "Sound_and_Spectrum.h"
#include "Sound_to_Intensity.h"

autoIntervalTier Sound_to_IntervalTier_highMidLowIntervals (Sound me, double min, double max) {
	try {
		autoIntervalTier thee = IntervalTier_create (my xmin, my xmax);
		double startTime = my xmin, endTime = my xmax;
		bool firstInterval = true;
		VEC z (my z[1]);
		int level = ( z [1] > max ? 1 : z [1] < min ? -1 : 0 );
		for (integer isamp = 2; isamp <= z.size; isamp ++) {
			const int leveli = ( z [isamp] > max ? 1 : z [isamp] < min ? -1 : 0 );
			if (leveli == level)
				continue;
			// new interval
			endTime = my x1 + (isamp - 1 - 0.5) * my dx; // midpoint
			conststring32 label = ( level == 1 ? U"High" : level == -1 ? U"Low" : U"Mid" );
			if (firstInterval) {
				TextInterval interval = thy intervals .at[1];
				interval -> xmax = endTime;
				autostring32 firstLabel = Melder_dup (label);
				interval ->  text = firstLabel. move();
				firstInterval = false;
			} else {
				autoTextInterval interval = TextInterval_create (startTime, endTime, label);
				thy intervals. addItem_move (interval.move());
			}
			startTime = endTime;
			level = leveli;
		}
		conststring32 last_label = ( level == 1 ? U"High" : level == -1 ? U"Low" : U"Mid" );
		autoTextInterval interval = TextInterval_create (startTime, my xmax, last_label);
		thy intervals. addItem_move (interval.move());
		return thee;
	} catch (MelderError) {
		Melder_throw (me, U": no IntervalTier with High/Mid/Low intervals created.");
	}
}

autoTextGrid Sound_to_TextGrid_highMidLowIntervals (Sound me, double min, double max) {
	try {
		autoTextGrid thee = TextGrid_createWithoutTiers (my xmin, my xmax);
		autoIntervalTier intervals =  Sound_to_IntervalTier_highMidLowIntervals (me, min, max);
		thy tiers -> addItem_move (intervals.move());
		return thee;
	} catch (MelderError) {
		Melder_throw (me, U": no TextGrid with High/Mid/Low intervals created.");
	}
}

autoSound Sound_IntervalTier_cutPartsMatchingLabel (Sound me, IntervalTier thee, conststring32 match) {
	try {
		/*
			Count samples of the trimmed sound
		*/
		integer ixmin, ixmax, numberOfSamples = 0, previous_ixmax = 0;
		double xmin = my xmin; // start time of output sound is start time of input sound
		for (integer iint = 1; iint <= thy intervals.size; iint ++) {
			TextInterval interval = thy intervals.at [iint];
			if (! Melder_equ (interval -> text.get(), match)) {
				numberOfSamples += Sampled_getWindowSamples (me, interval -> xmin, interval -> xmax, & ixmin, & ixmax);
				/*
					If two contiguous intervals have to be copied then the last sample of previous interval
					and first sample of current interval might sometimes be equal
				*/
				if (ixmin == previous_ixmax)
					-- numberOfSamples;
				previous_ixmax = ixmax;
			} else { // matches label
				if (iint == 1) // Start time of output sound is end time of first interval
					xmin = interval -> xmax;
			}
		}
		/*
			Now copy the parts. The output sound starts at xmin
		*/
		autoSound him = Sound_create (my ny, xmin, xmin + numberOfSamples * my dx, numberOfSamples, my dx, xmin + 0.5 * my dx);
		numberOfSamples = 0;
		previous_ixmax = 0;
		for (integer iint = 1; iint <= thy intervals.size; iint ++) {
			const TextInterval interval = thy intervals.at [iint];
			if (! Melder_equ (interval -> text.get(), match)) {
				Sampled_getWindowSamples (me, interval -> xmin, interval -> xmax, & ixmin, & ixmax);
				if (ixmin == previous_ixmax)
					ixmin ++;
				previous_ixmax = ixmax;
				integer numberOfSamplesToCopy = ixmax - ixmin + 1;
				his z.part (1, my ny, numberOfSamples + 1, numberOfSamples + numberOfSamplesToCopy)  <<=  my z.part (1, my ny, ixmin, ixmax);
				numberOfSamples += numberOfSamplesToCopy;
			}
		}
		Melder_assert (numberOfSamples == his nx);
		return him;
	} catch (MelderError) {
		Melder_throw (me, U": intervals not trimmed.");
	}
}

autoTextGrid Sound_to_TextGrid_detectSilences (Sound me, double minPitch, double timeStep,
	double silenceThreshold, double minSilenceDuration, double minSoundingDuration,
	conststring32 silentLabel, conststring32 soundingLabel)
{
	try {
		const bool subtractMeanPressure = true;
		autoSound filtered = Sound_filter_passHannBand (me, 80.0, 8000.0, 80.0);
		autoIntensity thee = Sound_to_Intensity (filtered.get(), minPitch, timeStep, subtractMeanPressure);
		autoTextGrid him = Intensity_to_TextGrid_detectSilences (thee.get(), silenceThreshold, minSilenceDuration, minSoundingDuration, silentLabel, soundingLabel);
		return him;
	} catch (MelderError) {
		Melder_throw (me, U": no TextGrid with silences created.");
	}
}


/* End of file Sound_and_TextGrid_extensions.cpp */