| 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"cxx . . $ `<file>/fon/Manipulation.h`/ |
| 29 | #include "Sound_to_Pitch.h"cxx . . $ `<file>/fon/Sound_to_Pitch.h`/ |
| 30 | #include "Pitch_to_PitchTier.h"cxx . . $ `<file>/fon/Pitch_to_PitchTier.h`/ |
| 31 | #include "Pitch_to_PointProcess.h"cxx . . $ `<file>/fon/Pitch_to_PointProcess.h`/ |
| 32 | #include "Sound_and_Spectrum.h"cxx . . $ `<file>/fon/Sound_and_Spectrum.h`/ |
| 33 | |
| 34 | autoSoundcxx . . $ autoSound# Sound_lengthen_overlapAddcxx . . $ Sound_lengthen_overlapAdd(a9298ecf594d39cf). (constSoundcxx . . $ constSound# me, double fmin, double fmax, double factor) { |
| 35 | try { |
| 36 | if (mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny. > 1) |
| 37 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Overlap-add works only on mono sounds."); |
| 38 | autoSoundcxx . . $ autoSound# sound = Data_copycxx . . $ Data_copy(97a542a26794c80e). (me); |
| 39 | Vector_subtractMeancxx . . $ Vector_subtractMean(8ab8c8e01f03cabf). (sound.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 40 | autoPitchcxx . . $ autoPitch# pitch = Sound_to_Pitchcxx . . $ Sound_to_Pitch(c773ddec40eb7e2b). (sound.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 0.8 / fmin, fmin, fmax); |
| 41 | autoPointProcesscxx . . $ autoPointProcess# pulses = Sound_Pitch_to_PointProcess_cccxx . . $ Sound_Pitch_to_PointProcess_cc(f94f2d65883a605d). (sound.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), pitch.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 42 | autoPitchTiercxx . . $ autoPitchTier# pitchTier = Pitch_to_PitchTiercxx . . $ Pitch_to_PitchTier(511873a878854c23). (pitch.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 43 | autoDurationTiercxx . . $ autoDurationTier# duration = DurationTier_createcxx . . $ DurationTier_create(a86647bd247c7eaf). (mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax.); |
| 44 | RealTier_addPointcxx . . $ RealTier_addPoint(d84c0874d617a544). (duration.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 0.5 * (mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin. + mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax.), factor); |
| 45 | autoSoundcxx . . $ autoSound# thee = Sound_Point_Pitch_Duration_to_Soundcxx . . $ Sound_Point_Pitch_Duration_to_Sound(f5fdd517ed726be5). (sound.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), pulses.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), pitchTier.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), duration.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 1.5 / fmin); |
| 46 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 47 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 48 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not lengthened."); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | autoSoundcxx . . $ autoSound# Sound_deepenBandModulationcxx . . $ Sound_deepenBandModulation(654ce24bee9d807b). (constSoundcxx . . $ constSound# me, double enhancement_dB, |
| 53 | double flow, double fhigh, double slowModulation, double fastModulation, double bandSmoothing) |
| 54 | { |
| 55 | try { |
| 56 | autoSoundcxx . . $ autoSound# thee = Data_copycxx . . $ Data_copy(97a542a26794c80e). (me); |
| 57 | double maximumFactor = powcxx . . $ pow(b71928d39fde4295). (10.0, enhancement_dB / 20.0), alpha = sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (logcxx . . $ log(fee4f1d22bc8e617). (2.0)); |
| 58 | double alphaslow = alpha / slowModulation, alphafast = alpha / fastModulation; |
| 59 | |
| 60 | for (integercxx . . $ integer# channel = 1; channel <= mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.; channel ++) { |
| 61 | autoSoundcxx . . $ autoSound# channelSound = Sound_extractChannelcxx . . $ Sound_extractChannel(fdb1ba3dfee1bd33). (me, channel); |
| 62 | autoSpectrumcxx . . $ autoSpectrum# orgspec = Sound_to_Spectrumcxx . . $ Sound_to_Spectrum(e96f4d575d8eeb8c). (channelSound.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), true); |
| 63 | |
| 64 | /* |
| 65 | Keep the part of the sound that is outside the filter bank. |
| 66 | */ |
| 67 | autoSpectrumcxx . . $ autoSpectrum# spec = Data_copycxx . . $ Data_copy(97a542a26794c80e). (orgspec.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 68 | Spectrum_stopHannBandcxx . . $ Spectrum_stopHannBand(4e1fbc861a283982). (spec.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), flow, fhigh, bandSmoothing); |
| 69 | autoSoundcxx . . $ autoSound# filtered = Spectrum_to_Soundcxx . . $ Spectrum_to_Sound(b9577246d0f6b8d7). (spec.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 70 | integercxx . . $ integer# n = thycxx . . $ `melder/melder.h:45:9`! nxcxx . . $ structSampled#nx.; |
| 71 | VECcxx . . $ VEC# amp = thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (channel); |
| 72 | for (integercxx . . $ integer# i = 1; i <= n; i ++) |
| 73 | amp [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = filtered ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i]; |
| 74 | |
| 75 | autoMelderProgresscxx . . $ autoMelderProgress# progresscxx . . $ autoMelderProgress#autoMelderProgress(ff00d524ab0355ec). (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 = NUMhertzToBarkcxx . . $ NUMhertzToBark(447c67c6676f75c3). (fmin) + 0.5; |
| 82 | const double fmax = Melder_clippedRightcxx . . $ Melder_clippedRight(f80f7d6260b3ee3a). (NUMbarkToHertzcxx . . $ NUMbarkToHertz(447c67c6676f75c3). (NUMhertzToBarkcxx . . $ NUMhertzToBark(447c67c6676f75c3). (fmin) + 1.0), fhigh); |
| 83 | Melder_progresscxx . . $ Melder_progress(3af686d0d3721642). (fmin / fhigh, U"Band: ", Melder_fixedcxx . . $ Melder_fixed(5b9062840a6ade99). (fmin, 0), U" ... ", Melder_fixedcxx . . $ Melder_fixed(5b9062840a6ade99). (fmax, 0), U" Hz"); |
| 84 | spec ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z..allcxx . . $ automatrix#all(2114117ba8ed4224).() <<=cxx . . $ `operator<<=`(a7d93ba40fe22fe0). orgspeccxx . . $ constmatrixview#constmatrixview(c98a2930b9469e58). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z..allcxx . . $ automatrix#all(2114117ba8ed4224).(); |
| 85 | Spectrum_passHannBandcxx . . $ Spectrum_passHannBand(4e1fbc861a283982). (spec.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), fmin, fmax, bandSmoothing); |
| 86 | autoSoundcxx . . $ autoSound# band = Spectrum_to_Soundcxx . . $ Spectrum_to_Sound(b9577246d0f6b8d7). (spec.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 87 | /* |
| 88 | Compute a relative intensity contour. |
| 89 | */ |
| 90 | autoSoundcxx . . $ autoSound# intensity = Data_copycxx . . $ Data_copy(97a542a26794c80e). (band.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 91 | n = intensity ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). nxcxx . . $ structSampled#nx.; |
| 92 | amp =cxx . . $ vector#`operator=`(b5ebf627ac2fae1e). intensity ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1); |
| 93 | for (integercxx . . $ integer# i = 1; i <= n; i ++) |
| 94 | amp [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = 10.0 * log10cxx . . $ log10(fee4f1d22bc8e617). (amp [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] * amp [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] + 1e-6); |
| 95 | autoSpectrumcxx . . $ autoSpectrum# intensityFilter = Sound_to_Spectrumcxx . . $ Sound_to_Spectrum(e96f4d575d8eeb8c). (intensity.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), true); |
| 96 | n = intensityFilter ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). nxcxx . . $ structSampled#nx.; |
| 97 | for (integercxx . . $ integer# i = 1; i <= n; i ++) { |
| 98 | double frequency = intensityFilter ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). x1cxx . . $ structSampled#x1. + (i - 1) * intensityFilter ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). dxcxx . . $ structSampled#dx.; |
| 99 | double slow = alphaslow * frequency, fast = alphafast * frequency; |
| 100 | double factor = expcxx . . $ exp(fee4f1d22bc8e617). (- fast * fast) - expcxx . . $ exp(fee4f1d22bc8e617). (- slow * slow); |
| 101 | intensityFilter ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] *= factor; |
| 102 | intensityFilter ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] *= factor; |
| 103 | } |
| 104 | intensity =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Spectrum_to_Soundcxx . . $ Spectrum_to_Sound(b9577246d0f6b8d7). (intensityFilter.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 105 | n = intensity ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). nxcxx . . $ structSampled#nx.; |
| 106 | amp =cxx . . $ vector#`operator=`(b5ebf627ac2fae1e). intensity ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1); |
| 107 | for (integercxx . . $ integer# i = 1; i <= n; i ++) |
| 108 | amp [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = powcxx . . $ pow(b71928d39fde4295). (10.0, amp [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] / 2.0); |
| 109 | /* |
| 110 | Clip to maximum enhancement. |
| 111 | */ |
| 112 | const double ceiling = 1.0 + (maximumFactor - 1.0) * (0.5 - 0.5 * coscxx . . $ cos(fee4f1d22bc8e617). (NUMpicxx . . $ NUMpi. * fmid_bark / 13.0)); |
| 113 | for (integercxx . . $ integer# i = 1; i <= n; i ++) |
| 114 | amp [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = 1.0 / (1.0 / amp [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] + 1.0 / ceiling); |
| 115 | |
| 116 | n = thycxx . . $ `melder/melder.h:45:9`! nxcxx . . $ structSampled#nx.; |
| 117 | amp =cxx . . $ vector#`operator=`(b5ebf627ac2fae1e). thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (channel); |
| 118 | for (integercxx . . $ integer# i = 1; i <= n; i ++) |
| 119 | amp [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] += band ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] * intensity ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i]; |
| 120 | |
| 121 | fmin = fmax; |
| 122 | } |
| 123 | } |
| 124 | Vector_scalecxx . . $ Vector_scale(54f31cdb89d1acde). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 0.99); |
| 125 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 126 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 127 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": band modulation not deepened."); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /* End of file Sound_enhance.cpp */ |