Debian Codegraph @

LPC/Sound_to_Formant_mt.cpp

raw
1 /* Sound_to_Formant_mt.cpp
2 *
3 * Copyright (C) 2024-2025 David Weenink
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 "Sound_to_Formant_mt.h"
20 #include "Sound_extensions.h"
21 #include "SoundFrames.h"
22 #include "Sound_and_LPC.h"
23 #include "LPC_and_Formant.h"
24
25 static void Sound_to_Formant_common (constSound inputSound, double& dt, double numberOfFormants_real, double maximumFrequency,
26 double effectiveAnalysisWidth, double preEmphasisFrequency, double safetyMargin,
27 autoFormant& outputFormant, autoLPC& outputLPC, autoSound& sound)
28 {
29 try {
30 if (dt <= 0.0)
31 dt = effectiveAnalysisWidth / 4.0;
32 autoSound resampled = Sound_resampleAndOrPreemphasize (inputSound, maximumFrequency, 50, preEmphasisFrequency);
33 const integer numberOfPoles = 2.0 * numberOfFormants_real;
34 const double physicalAnalysisWidth = getPhysicalAnalysisWidth2 (effectiveAnalysisWidth, kSound_windowShape::GAUSSIAN_2);
35 integer numberOfFrames;
36 double t1;
37 Sampled_shortTermAnalysis (sound.get(), physicalAnalysisWidth, dt, & numberOfFrames, & t1);
38 const integer numberOfFormants = ( safetyMargin == 0.0 ? numberOfPoles : (numberOfPoles + 1) / 2 );
39 autoFormant formant = Formant_create (sound -> xmin, sound -> xmax, numberOfFrames, dt, t1, numberOfFormants);
40 for (integer iframe = 1; iframe <= numberOfFrames; iframe ++) {
41 Formant_Frame formantFrame = &formant -> frames [iframe];
42 Formant_Frame_init (formantFrame, numberOfFormants);
43 }
44 outputFormant = formant.move();
45 autoLPC lpc = LPC_createCompletelyInitialized (sound -> xmin, sound -> xmax, formant -> nx, formant -> dx,
46 formant -> x1, numberOfPoles, sound -> dx);
47 outputLPC = lpc.move();
48 sound = resampled.move();
49 } catch (MelderError) {
50 Melder_throw (U"Cannot create Formant or LPC or Sound.");
51 }
52 }
53
54 static autoFormant createFormant_common (constSound me, double dt, integer numberOfPoles, double effectiveAnalysisWidth,
55 double safetyMargin)
56 {
57 integer numberOfFrames;
58 double t1;
59 const double physicalAnalysisWidth = getPhysicalAnalysisWidth2 (effectiveAnalysisWidth, kSound_windowShape::GAUSSIAN_2);
60 Sampled_shortTermAnalysis (me, physicalAnalysisWidth, dt, & numberOfFrames, & t1);
61 const integer numberOfFormants = numberOfFormantsFromNumberOfCoefficients2 (numberOfPoles, safetyMargin);
62 autoFormant formant = Formant_create (my xmin, my xmax, numberOfFrames, dt, t1, numberOfFormants);
63 return formant;
64 }
65
66 void Sound_into_Formant_burg (constSound me, mutableLPC intermediateLPC, mutableFormant outputFormant,
67 double effectiveAnalysisWidth, double safetyMargin)
68 {
69 Sampleds_requireEqualDomains (me, intermediateLPC);
70 Sampleds_assertEqualDomainsAndSampling (intermediateLPC, outputFormant);
71 const integer order = intermediateLPC -> maxnCoefficients;
72 const integer thresholdNumberOfFramesPerThread = 40;
73 const double samplingPeriod = intermediateLPC -> samplingPeriod;
74
75 MelderThread_PARALLEL (outputFormant -> nx, thresholdNumberOfFramesPerThread) {
76 autoSoundFrames soundFrames = SoundFrames_createForIntoSampled (me, intermediateLPC, effectiveAnalysisWidth,
77 kSound_windowShape::GAUSSIAN_2, true);
78 integer info;
79 autoVEC aa = raw_VEC (order);
80 autoVEC b1 = raw_VEC (soundFrames -> soundFrameSize);
81 autoVEC b2 = raw_VEC (soundFrames -> soundFrameSize);
82 autoVEC polynomialIntoRootsWorkspace = raw_VEC (order * order + order + order + 11 * order);
83 autoPolynomial p = Polynomial_create (-1.0, 1.0, order);
84 autoRoots roots = Roots_create (order);
85 MelderThread_FOR (iframe) {
86 const LPC_Frame lpcFrame = & intermediateLPC -> d_frames [iframe];
87 VEC soundFrame = soundFrames -> getMonoFrame (iframe);
88 soundFrameIntoLPCFrame_burg (soundFrame, lpcFrame, b1.get(), b2.get(), aa.get(), info);
89 Formant_Frame formantFrame = & outputFormant -> frames [iframe];
90 LPC_Frame_into_Formant_Frame (lpcFrame, formantFrame, samplingPeriod,
91 safetyMargin, p.get(), roots.get(), polynomialIntoRootsWorkspace.get());
92 }
93 } MelderThread_ENDPARALLEL
94 }
95
96 autoFormant Sound_to_Formant_burg_mt (constSound me, double dt, double numberOfFormants, double maximumFrequency,
97 double effectiveAnalysisWidth, double preEmphasisFrequency, double safetyMargin)
98 {
99 try {
100 autoSound sound;
101 autoFormant outputFormant;
102 autoLPC intermediateLPC;
103 Sound_to_Formant_common (me, dt, numberOfFormants, maximumFrequency, effectiveAnalysisWidth,
104 preEmphasisFrequency, safetyMargin, outputFormant, intermediateLPC, sound);
105 Sound_into_Formant_burg (me, intermediateLPC.get(), outputFormant.get(), effectiveAnalysisWidth, safetyMargin);
106 return outputFormant;
107 } catch (MelderError) {
108 Melder_throw (U"Could not create Formant (burg).");
109 }
110 }
111
112 #if 0
113 void Sound_and_LPC_into_Formant (constSound inputSound, constLPC inputLPC, mutableLPC intermediateLPC,
114 mutableFormant outputFormant, double effectiveAnalysisWidth, double safetyMargin, double k_stdev,
115 integer itermax, double tol, double location, bool wantlocation)
116 {
117 Sound_and_LPC_require_equalDomainsAndSamplingPeriods (inputSound, inputLPC);
118 SampledAndSampled_requireEqualDomainsAndSampling (inputLPC, intermediateLPC);
119 SampledAndSampled_requireEqualDomainsAndSampling (inputLPC, outputFormant);
120 const integer order = inputLPC -> maxnCoefficients;
121 const integer thresholdNumberOfFramesPerThread = 40;
122 const double samplingPeriod = inputLPC -> samplingPeriod;
123 MelderThread_PARALLEL (outputFormant -> nx, thresholdNumberOfFramesPerThread) {
124 autoSoundFrames soundFrames = SoundFrames_createForSampled (inputSound, intermediateLPC, effectiveAnalysisWidth,
125 kSound_windowShape::GAUSSIAN_2, true, false, 0_integer);
126 integer info;
127 autoRobustLPCWorkspace ws = RobustLPCWorkspace_create (inputLPC, inputSound, intermediateLPC,
128 effectiveAnalysisWidth, kSound_windowShape :: GAUSSIAN_2, k_stdev, itermax, tol, wantlocation);
129 autoVEC polynomialIntoRootsWorkspace = raw_VEC (order * order + order + order + 11 * order);
130 autoPolynomial p = Polynomial_create (-1.0, 1.0, order);
131 autoRoots roots = Roots_create (order);
132 MelderThread_FOR (iframe) {
133 const LPC_Frame inputLPCFrame = & inputLPC -> d_frames [iframe];
134 const LPC_Frame intermediateLPCFrame = & intermediateLPC -> d_frames [iframe];
135 const Formant_Frame formantFrame = & outputFormant -> frames [iframe];
136 VEC soundFrame = soundFrames -> getFrame (iframe);
137
138 soundFrameAndLPCFrameIntoLPCFrame (soundFrame, inputLPCFrame, intermediateLPCFrame, ws.get(), info);
139 LPC_Frame_into_Formant_Frame (intermediateLPCFrame, formantFrame, samplingPeriod,
140 safetyMargin, p.get(), roots.get(), polynomialIntoRootsWorkspace.get());
141 }
142 } MelderThread_ENDPARALLEL
143 }
144
145 autoFormant Sound_and_LPC_to_Formant (constSound inputSound, constLPC inputLPC, double effectiveAnalysisWidth, double preEmphasisFrequency,
146 double safetyMargin, double k_stdev, integer itermax, double tol, double location, bool wantlocation)
147 {
148 try {
149 const double maximumFrequency = 1.0 / inputLPC -> samplingPeriod;
150 autoSound sound = Sound_resampleAndOrPreemphasize (inputSound, maximumFrequency, 50, preEmphasisFrequency);
151 autoFormant outputFormant = createFormant_common (sound.get(), inputLPC -> dx,
152 inputLPC -> maxnCoefficients, effectiveAnalysisWidth, safetyMargin);
153 autoLPC intermediateLPC = Data_copy (inputLPC);
154 Sound_and_LPC_into_Formant (inputSound, inputLPC, intermediateLPC.get(), outputFormant.get(),
155 effectiveAnalysisWidth, safetyMargin, k_stdev, itermax, tol, location, wantlocation);
156 return outputFormant;
157 } catch (MelderError) {
158 Melder_throw (U"Could not create Formant from Sound and LPC.");
159 }
160 }
161 #endif
162
163 autoFormant Sound_to_Formant_robust_mt (constSound inputSound, double dt, double numberOfFormants, double maximumFrequency,
164 double effectiveAnalysisWidth, double preEmphasisFrequency, double safetyMargin, double k_stdev, integer itermax, double tol,
165 double location, bool wantlocation)
166 {
167 try {
168 autoSound sound;
169 autoFormant outputFormant;
170 autoLPC outputLPC;
171 Sound_to_Formant_common (inputSound, dt, numberOfFormants, maximumFrequency, effectiveAnalysisWidth, preEmphasisFrequency,
172 safetyMargin, outputFormant, outputLPC, sound);
173 const kSound_windowShape windowShape = kSound_windowShape::GAUSSIAN_2;
174
175 Sound_into_LPC_robust (sound.get(), outputLPC.get(), effectiveAnalysisWidth, k_stdev, itermax, tol, wantlocation);
176 LPC_into_Formant (outputLPC.get(), outputFormant.get(), safetyMargin);
177 return outputFormant;
178 } catch (MelderError) {
179 Melder_throw (inputSound, U": no robust Formant created.");
180 }
181 }
182
183 autoFormant Sound_to_Formant_robust (Sound me, double dt_in, double numberOfFormants, double maximumFrequency,
184 double effectiveAnalysisWidth, double preEmphasisFrequency, double safetyMargin,
185 double numberOfStandardDeviations, integer maximumNumberOfIterations, double tolerance,
186 double location, bool wantlocation)
187 {
188 const double dt = ( dt_in > 0.0 ? dt_in : effectiveAnalysisWidth / 4.0 );
189 const double nyquist = 0.5 / my dx;
190 const integer predictionOrder = Melder_ifloor (2 * numberOfFormants);
191 try {
192 autoSound sound;
193 if (maximumFrequency <= 0.0 || fabs (maximumFrequency / nyquist - 1.0) < 1.0e-12)
194 sound = Data_copy (me); // will be modified
195 else
196 sound = Sound_resample (me, maximumFrequency * 2.0, 50);
197
198 autoLPC lpc = Sound_to_LPC_auto (sound.get(), predictionOrder, effectiveAnalysisWidth, dt, preEmphasisFrequency);
199 autoLPC lpcRobust = LPC_and_Sound_to_LPC_robust (lpc.get(), sound.get(), effectiveAnalysisWidth, preEmphasisFrequency,
200 numberOfStandardDeviations, maximumNumberOfIterations, tolerance, wantlocation);
201 autoFormant thee = LPC_to_Formant (lpcRobust.get(), safetyMargin);
202 return thee;
203 } catch (MelderError) {
204 Melder_throw (me, U": no robust Formant created.");
205 }
206 }
207
208
209 /*
210 void Sound_into_Formant_robust_mt (constSound me, mutableFormant thee, double effectiveAnalysisWidth, integer numberOfPoles, double safetyMargin,
211 double k_stdev, integer itermax, double tol, double location, bool wantlocation)
212 {
213 autoSoundFrameIntoFormantFrameRobust ws = SoundFrameIntoFormantFrameRobust_create (me, thee,
214 effectiveAnalysisWidth, kSound_windowShape :: GAUSSIAN_2, k_stdev, itermax, tol, location, wantlocation, numberOfPoles, safetyMargin);
215
216
217 SampledToSampled_analyseThreaded (ws.get());
218 }
219
220 autoFormant Sound_to_Formant_robust_mt (constSound me, double dt_in, double numberOfFormants, double maximumFrequency,
221 double effectiveAnalysisWidth, double preEmphasisFrequency, double safetyMargin, double k_stdev, integer itermax, double tol,
222 double location, bool wantlocation)
223 {
224 const double dt = dt_in > 0.0 ? dt_in : effectiveAnalysisWidth / 4.0;
225 try {
226 autoSound sound = Sound_resampleAndOrPreemphasize (me, maximumFrequency, 50, preEmphasisFrequency);
227 integer numberOfFrames;
228 double t1;
229 const double physicalAnalysisWidth = getPhysicalAnalysisWidth (effectiveAnalysisWidth, kSound_windowShape::GAUSSIAN_2);
230 Sampled_shortTermAnalysis (sound.get(), physicalAnalysisWidth, dt, & numberOfFrames, & t1);
231 const integer numberOfPoles = numberOfPolesFromNumberOfFormants (numberOfFormants);
232 const integer numberOfFormants = numberOfFormantsFromNumberOfCoefficients (numberOfPoles, safetyMargin);
233 autoFormant formant = Formant_create (my xmin, my xmax, numberOfFrames, dt, t1, numberOfFormants);
234 Sound_into_Formant_robust_mt (sound.get(), formant.get(), effectiveAnalysisWidth, numberOfPoles, safetyMargin, k_stdev,
235 itermax, tol, location, wantlocation);
236 return formant;
237 } catch (MelderError) {
238 Melder_throw (me, U": no robust Formant created.");
239 }
240 }
241 */
242 /* End of file Sound_to_Formant_mt.cpp */