Debian Codegraph @

fon/FormantGrid.cpp

raw symbols SCIP
1 /* FormantGrid.cpp
2 *
3 * Copyright (C) 2008,2009,2011,2012,2014-2020,2023,2025 Paul Boersma & 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 "FormantGrid.h"
20 #include "PitchTier_to_Sound.h"
21 #include "Formula.h"
22
23 #include "oo_DESTROY.h"
24 #include "FormantGrid_def.h"
25 #include "oo_COPY.h"
26 #include "FormantGrid_def.h"
27 #include "oo_EQUAL.h"
28 #include "FormantGrid_def.h"
29 #include "oo_CAN_WRITE_AS_ENCODING.h"
30 #include "FormantGrid_def.h"
31 #include "oo_WRITE_TEXT.h"
32 #include "FormantGrid_def.h"
33 #include "oo_READ_TEXT.h"
34 #include "FormantGrid_def.h"
35 #include "oo_WRITE_BINARY.h"
36 #include "FormantGrid_def.h"
37 #include "oo_READ_BINARY.h"
38 #include "FormantGrid_def.h"
39 #include "oo_DESCRIPTION.h"
40 #include "FormantGrid_def.h"
41
42 Thing_implement (FormantGrid, Function, 0);
43
44 double structFormantGrid :: v_getVector (
45 const integer irow,
46 const integer icol
47 ) const {
48 const constRealTier tier = our formants.at [irow];
49 return RealTier_getValueAtIndex (tier, icol);
50 }
51
52 double structFormantGrid :: v_getFunction1 (
53 const integer irow,
54 const double x
55 ) const {
56 const constRealTier tier = our formants.at [irow];
57 return RealTier_getValueAtTime (tier, x);
58 }
59
60 void structFormantGrid :: v_shiftX (
61 const double xfrom,
62 const double xto
63 ) /* mutable */ {
64 FormantGrid_Parent :: v_shiftX (xfrom, xto);
65 for (integer i = 1; i <= our formants.size; i ++) {
66 const mutableRealTier tier = our formants.at [i];
67 tier -> v_shiftX (xfrom, xto);
68 }
69 for (integer i = 1; i <= our bandwidths.size; i ++) {
70 const mutableRealTier tier = our bandwidths.at [i];
71 tier -> v_shiftX (xfrom, xto);
72 }
73 }
74
75 void structFormantGrid :: v_scaleX (
76 const double xminfrom,
77 const double xmaxfrom,
78 const double xminto,
79 const double xmaxto
80 ) /* mutable */ {
81 FormantGrid_Parent :: v_scaleX (xminfrom, xmaxfrom, xminto, xmaxto);
82 for (integer i = 1; i <= our formants.size; i ++) {
83 const mutableRealTier tier = our formants.at [i];
84 tier -> v_scaleX (xminfrom, xmaxfrom, xminto, xmaxto);
85 }
86 for (integer i = 1; i <= our bandwidths.size; i ++) {
87 const mutableRealTier tier = our bandwidths.at [i];
88 tier -> v_scaleX (xminfrom, xmaxfrom, xminto, xmaxto);
89 }
90 }
91
92 void FormantGrid_init (
93 const mutableFormantGrid me,
94 const double tmin,
95 const double tmax,
96 const integer numberOfFormants
97 ) {
98 for (integer iformant = 1; iformant <= numberOfFormants; iformant ++) {
99 autoRealTier formant = RealTier_create (tmin, tmax);
100 my formants. addItem_move (formant.move());
101 autoRealTier bandwidth = RealTier_create (tmin, tmax);
102 my bandwidths. addItem_move (bandwidth.move());
103 }
104 my xmin = tmin;
105 my xmax = tmax;
106 }
107
108 autoFormantGrid FormantGrid_createEmpty (
109 const double tmin,
110 const double tmax,
111 const integer numberOfFormants
112 ) {
113 try {
114 autoFormantGrid me = Thing_new (FormantGrid);
115 FormantGrid_init (me.get(), tmin, tmax, numberOfFormants);
116 return me;
117 } catch (MelderError) {
118 Melder_throw (U"Empty FormantGrid not created.");
119 }
120 }
121
122 autoFormantGrid FormantGrid_create (
123 const double tmin,
124 const double tmax,
125 const integer numberOfFormants,
126 const double initialFirstFormant,
127 const double initialFormantSpacing,
128 const double initialFirstBandwidth,
129 const double initialBandwidthSpacing
130 ) {
131 try {
132 autoFormantGrid me = FormantGrid_createEmpty (tmin, tmax, numberOfFormants);
133 for (integer iformant = 1; iformant <= numberOfFormants; iformant ++) {
134 FormantGrid_addFormantPoint (me.get(), iformant, 0.5 * (tmin + tmax),
135 initialFirstFormant + (iformant - 1) * initialFormantSpacing);
136 FormantGrid_addBandwidthPoint (me.get(), iformant, 0.5 * (tmin + tmax),
137 initialFirstBandwidth + (iformant - 1) * initialBandwidthSpacing);
138 }
139 return me;
140 } catch (MelderError) {
141 Melder_throw (U"FormantGrid not created.");
142 }
143 }
144
145 void FormantGrid_addFormantPoint (
146 const mutableFormantGrid me,
147 const integer iformant,
148 const double time,
149 const double value
150 ) {
151 try {
152 if (iformant < 1 || iformant > my formants.size)
153 Melder_throw (U"No such formant number.");
154 RealTier formantTier = my formants.at [iformant];
155 RealTier_addPoint (formantTier, time, value);
156 } catch (MelderError) {
157 Melder_throw (me, U": formant point not added.");
158 }
159 }
160
161 void FormantGrid_addBandwidthPoint (
162 const mutableFormantGrid me,
163 const integer iformant,
164 const double time,
165 const double value
166 ) {
167 try {
168 if (iformant < 1 || iformant > my formants.size)
169 Melder_throw (U"No such formant number.");
170 RealTier bandwidthTier = my bandwidths.at [iformant];
171 RealTier_addPoint (bandwidthTier, time, value);
172 } catch (MelderError) {
173 Melder_throw (me, U": bandwidth point not added.");
174 }
175 }
176
177 double FormantGrid_getFormantAtTime (
178 const constFormantGrid me,
179 const integer iformant,
180 const double time
181 ) {
182 if (iformant < 1 || iformant > my formants.size)
183 return undefined;
184 return RealTier_getValueAtTime (my formants.at [iformant], time);
185 }
186
187 double FormantGrid_getBandwidthAtTime (
188 const constFormantGrid me,
189 const integer iformant,
190 const double time
191 ) {
192 if (iformant < 1 || iformant > my bandwidths.size)
193 return undefined;
194 return RealTier_getValueAtTime (my bandwidths.at [iformant], time);
195 }
196
197 void FormantGrid_removeFormantPointsBetween (
198 const mutableFormantGrid me,
199 const integer iformant,
200 const double tmin,
201 const double tmax
202 ) {
203 if (iformant < 1 || iformant > my formants.size)
204 return;
205 AnyTier_removePointsBetween (my formants.at [iformant]->asAnyTier(), tmin, tmax);
206 }
207
208 void FormantGrid_removeBandwidthPointsBetween (
209 const mutableFormantGrid me,
210 const integer iformant,
211 const double tmin,
212 const double tmax
213 ) {
214 if (iformant < 1 || iformant > my bandwidths.size)
215 return;
216 AnyTier_removePointsBetween (my bandwidths.at [iformant]->asAnyTier(), tmin, tmax);
217 }
218
219 void Sound_FormantGrid_filter_inplace (
220 const mutableSound me,
221 const constFormantGrid formantGrid
222 ) {
223 const double dt = my dx;
224 if (formantGrid -> formants.size > 0 && formantGrid -> bandwidths.size > 0) {
225 for (integer iformant = 1; iformant <= formantGrid -> formants.size; iformant ++) {
226 const constRealTier formantTier = formantGrid -> formants.at [iformant];
227 const constRealTier bandwidthTier = formantGrid -> bandwidths.at [iformant];
228 for (integer isamp = 1; isamp <= my nx; isamp ++) {
229 const double t = my x1 + (isamp - 1) * my dx;
230 /*
231 Compute LP coefficients.
232 */
233 const double formant = RealTier_getValueAtTime (formantTier, t);
234 const double bandwidth = RealTier_getValueAtTime (bandwidthTier, t);
235 if (isdefined (formant) && isdefined (bandwidth)) {
236 const double cosomdt = cos (2 * NUMpi * formant * dt);
237 const double r = exp (- NUMpi * bandwidth * dt);
238 /* Formants at 0 Hz or the Nyquist are single poles, others are double poles. */
239 if (fabs (cosomdt) > 0.999999) { /* Allow for round-off errors. */
240 /* single pole: D(z) = 1 - r z^-1 */
241 for (integer channel = 1; channel <= my ny; channel ++)
242 if (isamp > 1)
243 my z [channel] [isamp] += r * my z [channel] [isamp - 1];
244 } else {
245 /* double pole: D(z) = 1 + p z^-1 + q z^-2 */
246 const double p = - 2 * r * cosomdt;
247 const double q = r * r;
248 for (integer channel = 1; channel <= my ny; channel ++) {
249 if (isamp > 1)
250 my z [channel] [isamp] -= p * my z [channel] [isamp - 1];
251 if (isamp > 2)
252 my z [channel] [isamp] -= q * my z [channel] [isamp - 2];
253 }
254 }
255 }
256 }
257 }
258 }
259 }
260
261 autoSound Sound_FormantGrid_filter (
262 const constSound me,
263 const constFormantGrid formantGrid
264 ) {
265 try {
266 autoSound thee = Data_copy (me);
267 Sound_FormantGrid_filter_inplace (thee.get(), formantGrid);
268 Vector_scale (thee.get(), 0.99);
269 return thee;
270 } catch (MelderError) {
271 Melder_throw (me, U": not filtered with ", formantGrid, U".");
272 }
273 }
274
275 autoSound Sound_FormantGrid_filter_noscale (
276 const constSound me,
277 const constFormantGrid formantGrid
278 ) {
279 try {
280 autoSound thee = Data_copy (me);
281 Sound_FormantGrid_filter_inplace (thee.get(), formantGrid);
282 return thee;
283 } catch (MelderError) {
284 Melder_throw (me, U": not filtered with ", formantGrid, U".");
285 }
286 }
287
288 autoSound FormantGrid_to_Sound (
289 const constFormantGrid me,
290 const double samplingFrequency,
291 const double tStart, const double f0Start,
292 const double tMid, const double f0Mid,
293 const double tEnd, const double f0End,
294 const double adaptFactor, const double maximumPeriod,
295 const double openPhase, const double collisionPhase, const double power1, const double power2
296 ) {
297 try {
298 autoPitchTier pitch = PitchTier_create (my xmin, my xmax);
299 RealTier_addPoint (pitch.get(), my xmin + tStart * (my xmax - my xmin), f0Start);
300 RealTier_addPoint (pitch.get(), my xmin + tMid * (my xmax - my xmin), f0Mid);
301 RealTier_addPoint (pitch.get(), my xmax - (1.0 - tEnd) * (my xmax - my xmin), f0End);
302 autoSound thee = PitchTier_to_Sound_phonation (pitch.get(), samplingFrequency,
303 adaptFactor, maximumPeriod, openPhase, collisionPhase, power1, power2, false);
304 Sound_FormantGrid_filter_inplace (thee.get(), me);
305 return thee;
306 } catch (MelderError) {
307 Melder_throw (me, U": not converted to Sound.");
308 }
309 }
310
311 void FormantGrid_playPart (
312 const constFormantGrid me,
313 const double tmin, const double tmax,
314 const double samplingFrequency,
315 const double tStart, const double f0Start,
316 const double tMid, const double f0Mid,
317 const double tEnd, const double f0End,
318 const double adaptFactor, const double maximumPeriod,
319 const double openPhase, const double collisionPhase, const double power1, const double power2,
320 const Sound_PlayCallback playCallback, const mutableThing playBoss
321 ) {
322 try {
323 autoSound sound = FormantGrid_to_Sound (me, samplingFrequency,
324 tStart, f0Start, tMid, f0Mid, tEnd, f0End,
325 adaptFactor, maximumPeriod, openPhase, collisionPhase, power1, power2
326 );
327 Vector_scale (sound.get(), 0.99);
328 Sound_playPart (sound.get(), tmin, tmax, playCallback, playBoss);
329 } catch (MelderError) {
330 Melder_throw (me, U": not played.");
331 }
332 }
333
334 void FormantGrid_formula_frequencies (
335 const mutableFormantGrid me,
336 const conststring32 expression,
337 const Interpreter interpreter,
338 /* mutable assign if null */ mutableFormantGrid thee
339 ) {
340 try {
341 Formula_compile (interpreter, me, expression, kFormula_EXPRESSION_TYPE_NUMERIC, true);
342 Formula_Result result;
343 if (! thee)
344 thee = me;
345 for (integer irow = 1; irow <= my formants.size; irow ++) {
346 const RealTier formant = thy formants.at [irow];
347 for (integer icol = 1; icol <= formant -> points.size; icol ++) {
348 Formula_run (irow, icol, & result);
349 if (isundef (result. numericResult))
350 Melder_throw (U"Cannot put an undefined value into the tier.\nFormula not finished.");
351 formant -> points.at [icol] -> value = result. numericResult;
352 }
353 }
354 } catch (MelderError) {
355 Melder_throw (me, U": frequency formula not completed.");
356 }
357 }
358
359 void FormantGrid_formula_bandwidths (
360 const mutableFormantGrid me,
361 const conststring32 expression,
362 const Interpreter interpreter,
363 /* mutable assign if null */ mutableFormantGrid thee
364 ) {
365 try {
366 Formula_compile (interpreter, me, expression, kFormula_EXPRESSION_TYPE_NUMERIC, true);
367 Formula_Result result;
368 if (! thee)
369 thee = me;
370 for (integer irow = 1; irow <= my formants.size; irow ++) {
371 const RealTier bandwidth = thy bandwidths.at [irow];
372 for (integer icol = 1; icol <= bandwidth -> points.size; icol ++) {
373 Formula_run (irow, icol, & result);
374 if (isundef (result. numericResult))
375 Melder_throw (U"Cannot put an undefined value into the tier.\nFormula not finished.");
376 bandwidth -> points.at [icol] -> value = result. numericResult;
377 }
378 }
379 } catch (MelderError) {
380 Melder_throw (me, U": bandwidth formula not completed.");
381 }
382 }
383
384 autoFormantGrid Formant_downto_FormantGrid (const constFormant me) {
385 try {
386 autoFormantGrid thee = FormantGrid_createEmpty (my xmin, my xmax, my maxnFormants);
387 for (integer iframe = 1; iframe <= my nx; iframe ++) {
388 const Formant_Frame frame = & my frames [iframe];
389 const double t = Sampled_indexToX (me, iframe);
390 for (integer iformant = 1; iformant <= frame -> numberOfFormants; iformant ++) {
391 Formant_Formant pair = & frame -> formant [iformant];
392 FormantGrid_addFormantPoint (thee.get(), iformant, t, pair -> frequency);
393 FormantGrid_addBandwidthPoint (thee.get(), iformant, t, pair -> bandwidth);
394 }
395 }
396 return thee;
397 } catch (MelderError) {
398 Melder_throw (me, U": not converted to FormantGrid.");
399 }
400 }
401
402 autoFormant FormantGrid_to_Formant (const constFormantGrid me, const double dt, const double intensity) {
403 try {
404 Melder_assert (dt > 0.0);
405 Melder_assert (intensity >= 0.0);
406 const integer nt = Melder_ifloor ((my xmax - my xmin) / dt) + 1;
407 const double t1 = 0.5 * (my xmin + my xmax - (nt - 1) * dt);
408 autoFormant thee = Formant_create (my xmin, my xmax, nt, dt, t1, my formants.size);
409 for (integer iframe = 1; iframe <= nt; iframe ++) {
410 const Formant_Frame frame = & thy frames [iframe];
411 frame -> intensity = intensity;
412 frame -> numberOfFormants = my formants.size;
413 frame -> formant = newvectorzero <structFormant_Formant> (my formants.size);
414 const double time = t1 + (iframe - 1) * dt;
415 for (integer iformant = 1; iformant <= my formants.size; iformant ++) {
416 const Formant_Formant formant = & frame -> formant [iformant];
417 formant -> frequency = RealTier_getValueAtTime (my formants.at [iformant], time);
418 formant -> bandwidth = RealTier_getValueAtTime (my bandwidths.at [iformant], time);
419 }
420 }
421 return thee;
422 } catch (MelderError) {
423 Melder_throw (me, U": not converted to Formant.");
424 }
425 }
426
427 autoSound Sound_Formant_filter (const constSound me, const constFormant formant) {
428 try {
429 autoFormantGrid grid = Formant_downto_FormantGrid (formant);
430 autoSound thee = Sound_FormantGrid_filter (me, grid.get());
431 return thee;
432 } catch (MelderError) {
433 Melder_throw (me, U": not filtered with ", formant, U".");
434 }
435 }
436
437 autoSound Sound_Formant_filter_noscale (const constSound me, const constFormant formant) {
438 try {
439 autoFormantGrid grid = Formant_downto_FormantGrid (formant);
440 autoSound thee = Sound_FormantGrid_filter_noscale (me, grid.get());
441 return thee;
442 } catch (MelderError) {
443 Melder_throw (me, U": not filtered with ", formant, U".");
444 }
445 }
446
447 /* End of file FormantGrid.cpp */