Debian Codegraph @

fon/Sound_enhance.cpp

raw symbols SCIP
1 /* Sound_enhance.cpp
2 *
3 * Copyright (C) 1992-2012,2015-2020,2023,2024,2026 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 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.
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 /*
20 * pb 2002/07/16 GPL
21 * pb 2004/11/22 simplified Sound_to_Spectrum ()
22 * pb 2007/01/28 made compatible with stereo sounds (by rejecting them)
23 * pb 2007/07/22 renamed the overlap-add method in such a way that it does not sound like a trademark for diphone concatenation
24 * pb 2008/01/19 double
25 * pb 2011/06/07 C++
26 */
27
28 #include "Manipulation.h"
29 #include "Sound_to_Pitch.h"
30 #include "Pitch_to_PitchTier.h"
31 #include "Pitch_to_PointProcess.h"
32 #include "Sound_and_Spectrum.h"
33
34 autoSound Sound_lengthen_overlapAdd (constSound me, double fmin, double fmax, double factor) {
35 try {
36 if (my ny > 1)
37 Melder_throw (U"Overlap-add works only on mono sounds.");
38 autoSound sound = Data_copy (me);
39 Vector_subtractMean (sound.get());
40 autoPitch pitch = Sound_to_Pitch (sound.get(), 0.8 / fmin, fmin, fmax);
41 autoPointProcess pulses = Sound_Pitch_to_PointProcess_cc (sound.get(), pitch.get());
42 autoPitchTier pitchTier = Pitch_to_PitchTier (pitch.get());
43 autoDurationTier duration = DurationTier_create (my xmin, my xmax);
44 RealTier_addPoint (duration.get(), 0.5 * (my xmin + my xmax), factor);
45 autoSound thee = Sound_Point_Pitch_Duration_to_Sound (sound.get(), pulses.get(), pitchTier.get(), duration.get(), 1.5 / fmin);
46 return thee;
47 } catch (MelderError) {
48 Melder_throw (me, U": not lengthened.");
49 }
50 }
51
52 autoSound Sound_deepenBandModulation (constSound me, double enhancement_dB,
53 double flow, double fhigh, double slowModulation, double fastModulation, double bandSmoothing)
54 {
55 try {
56 autoSound thee = Data_copy (me);
57 double maximumFactor = pow (10.0, enhancement_dB / 20.0), alpha = sqrt (log (2.0));
58 double alphaslow = alpha / slowModulation, alphafast = alpha / fastModulation;
59
60 for (integer channel = 1; channel <= my ny; channel ++) {
61 autoSound channelSound = Sound_extractChannel (me, channel);
62 autoSpectrum orgspec = Sound_to_Spectrum (channelSound.get(), true);
63
64 /*
65 Keep the part of the sound that is outside the filter bank.
66 */
67 autoSpectrum spec = Data_copy (orgspec.get());
68 Spectrum_stopHannBand (spec.get(), flow, fhigh, bandSmoothing);
69 autoSound filtered = Spectrum_to_Sound (spec.get());
70 integer n = thy nx;
71 VEC amp = thy z.row (channel);
72 for (integer i = 1; i <= n; i ++)
73 amp [i] = filtered -> z [1] [i];
74
75 autoMelderProgress progress (U"Sound: Deepen band modulation...");
76 double fmin = flow;
77 while (fmin < fhigh) {
78 /*
79 Take a one-bark frequency band.
80 */
81 const double fmid_bark = NUMhertzToBark (fmin) + 0.5;
82 const double fmax = Melder_clippedRight (NUMbarkToHertz (NUMhertzToBark (fmin) + 1.0), fhigh);
83 Melder_progress (fmin / fhigh, U"Band: ", Melder_fixed (fmin, 0), U" ... ", Melder_fixed (fmax, 0), U" Hz");
84 spec -> z.all() <<= orgspec -> z.all();
85 Spectrum_passHannBand (spec.get(), fmin, fmax, bandSmoothing);
86 autoSound band = Spectrum_to_Sound (spec.get());
87 /*
88 Compute a relative intensity contour.
89 */
90 autoSound intensity = Data_copy (band.get());
91 n = intensity -> nx;
92 amp = intensity -> z.row (1);
93 for (integer i = 1; i <= n; i ++)
94 amp [i] = 10.0 * log10 (amp [i] * amp [i] + 1e-6);
95 autoSpectrum intensityFilter = Sound_to_Spectrum (intensity.get(), true);
96 n = intensityFilter -> nx;
97 for (integer i = 1; i <= n; i ++) {
98 double frequency = intensityFilter -> x1 + (i - 1) * intensityFilter -> dx;
99 double slow = alphaslow * frequency, fast = alphafast * frequency;
100 double factor = exp (- fast * fast) - exp (- slow * slow);
101 intensityFilter -> z [1] [i] *= factor;
102 intensityFilter -> z [2] [i] *= factor;
103 }
104 intensity = Spectrum_to_Sound (intensityFilter.get());
105 n = intensity -> nx;
106 amp = intensity -> z.row (1);
107 for (integer i = 1; i <= n; i ++)
108 amp [i] = pow (10.0, amp [i] / 2.0);
109 /*
110 Clip to maximum enhancement.
111 */
112 const double ceiling = 1.0 + (maximumFactor - 1.0) * (0.5 - 0.5 * cos (NUMpi * fmid_bark / 13.0));
113 for (integer i = 1; i <= n; i ++)
114 amp [i] = 1.0 / (1.0 / amp [i] + 1.0 / ceiling);
115
116 n = thy nx;
117 amp = thy z.row (channel);
118 for (integer i = 1; i <= n; i ++)
119 amp [i] += band -> z [1] [i] * intensity -> z [1] [i];
120
121 fmin = fmax;
122 }
123 }
124 Vector_scale (thee.get(), 0.99);
125 return thee;
126 } catch (MelderError) {
127 Melder_throw (me, U": band modulation not deepened.");
128 }
129 }
130
131 /* End of file Sound_enhance.cpp */