Sound_enhance.cpp 5.1 KB

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