dsp_helper.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef MODULES_AUDIO_CODING_NETEQ_DSP_HELPER_H_
  11. #define MODULES_AUDIO_CODING_NETEQ_DSP_HELPER_H_
  12. #include <stdint.h>
  13. #include <string.h>
  14. #include "modules/audio_coding/neteq/audio_multi_vector.h"
  15. #include "modules/audio_coding/neteq/audio_vector.h"
  16. #include "rtc_base/constructor_magic.h"
  17. namespace webrtc {
  18. // This class contains various signal processing functions, all implemented as
  19. // static methods.
  20. class DspHelper {
  21. public:
  22. // Filter coefficients used when downsampling from the indicated sample rates
  23. // (8, 16, 32, 48 kHz) to 4 kHz. Coefficients are in Q12.
  24. static const int16_t kDownsample8kHzTbl[3];
  25. static const int16_t kDownsample16kHzTbl[5];
  26. static const int16_t kDownsample32kHzTbl[7];
  27. static const int16_t kDownsample48kHzTbl[7];
  28. // Constants used to mute and unmute over 5 samples. The coefficients are
  29. // in Q15.
  30. static const int kMuteFactorStart8kHz = 27307;
  31. static const int kMuteFactorIncrement8kHz = -5461;
  32. static const int kUnmuteFactorStart8kHz = 5461;
  33. static const int kUnmuteFactorIncrement8kHz = 5461;
  34. static const int kMuteFactorStart16kHz = 29789;
  35. static const int kMuteFactorIncrement16kHz = -2979;
  36. static const int kUnmuteFactorStart16kHz = 2979;
  37. static const int kUnmuteFactorIncrement16kHz = 2979;
  38. static const int kMuteFactorStart32kHz = 31208;
  39. static const int kMuteFactorIncrement32kHz = -1560;
  40. static const int kUnmuteFactorStart32kHz = 1560;
  41. static const int kUnmuteFactorIncrement32kHz = 1560;
  42. static const int kMuteFactorStart48kHz = 31711;
  43. static const int kMuteFactorIncrement48kHz = -1057;
  44. static const int kUnmuteFactorStart48kHz = 1057;
  45. static const int kUnmuteFactorIncrement48kHz = 1057;
  46. // Multiplies the signal with a gradually changing factor.
  47. // The first sample is multiplied with |factor| (in Q14). For each sample,
  48. // |factor| is increased (additive) by the |increment| (in Q20), which can
  49. // be negative. Returns the scale factor after the last increment.
  50. static int RampSignal(const int16_t* input,
  51. size_t length,
  52. int factor,
  53. int increment,
  54. int16_t* output);
  55. // Same as above, but with the samples of |signal| being modified in-place.
  56. static int RampSignal(int16_t* signal,
  57. size_t length,
  58. int factor,
  59. int increment);
  60. // Same as above, but processes |length| samples from |signal|, starting at
  61. // |start_index|.
  62. static int RampSignal(AudioVector* signal,
  63. size_t start_index,
  64. size_t length,
  65. int factor,
  66. int increment);
  67. // Same as above, but for an AudioMultiVector.
  68. static int RampSignal(AudioMultiVector* signal,
  69. size_t start_index,
  70. size_t length,
  71. int factor,
  72. int increment);
  73. // Peak detection with parabolic fit. Looks for |num_peaks| maxima in |data|,
  74. // having length |data_length| and sample rate multiplier |fs_mult|. The peak
  75. // locations and values are written to the arrays |peak_index| and
  76. // |peak_value|, respectively. Both arrays must hold at least |num_peaks|
  77. // elements.
  78. static void PeakDetection(int16_t* data,
  79. size_t data_length,
  80. size_t num_peaks,
  81. int fs_mult,
  82. size_t* peak_index,
  83. int16_t* peak_value);
  84. // Estimates the height and location of a maximum. The three values in the
  85. // array |signal_points| are used as basis for a parabolic fit, which is then
  86. // used to find the maximum in an interpolated signal. The |signal_points| are
  87. // assumed to be from a 4 kHz signal, while the maximum, written to
  88. // |peak_index| and |peak_value| is given in the full sample rate, as
  89. // indicated by the sample rate multiplier |fs_mult|.
  90. static void ParabolicFit(int16_t* signal_points,
  91. int fs_mult,
  92. size_t* peak_index,
  93. int16_t* peak_value);
  94. // Calculates the sum-abs-diff for |signal| when compared to a displaced
  95. // version of itself. Returns the displacement lag that results in the minimum
  96. // distortion. The resulting distortion is written to |distortion_value|.
  97. // The values of |min_lag| and |max_lag| are boundaries for the search.
  98. static size_t MinDistortion(const int16_t* signal,
  99. size_t min_lag,
  100. size_t max_lag,
  101. size_t length,
  102. int32_t* distortion_value);
  103. // Mixes |length| samples from |input1| and |input2| together and writes the
  104. // result to |output|. The gain for |input1| starts at |mix_factor| (Q14) and
  105. // is decreased by |factor_decrement| (Q14) for each sample. The gain for
  106. // |input2| is the complement 16384 - mix_factor.
  107. static void CrossFade(const int16_t* input1,
  108. const int16_t* input2,
  109. size_t length,
  110. int16_t* mix_factor,
  111. int16_t factor_decrement,
  112. int16_t* output);
  113. // Scales |input| with an increasing gain. Applies |factor| (Q14) to the first
  114. // sample and increases the gain by |increment| (Q20) for each sample. The
  115. // result is written to |output|. |length| samples are processed.
  116. static void UnmuteSignal(const int16_t* input,
  117. size_t length,
  118. int16_t* factor,
  119. int increment,
  120. int16_t* output);
  121. // Starts at unity gain and gradually fades out |signal|. For each sample,
  122. // the gain is reduced by |mute_slope| (Q14). |length| samples are processed.
  123. static void MuteSignal(int16_t* signal, int mute_slope, size_t length);
  124. // Downsamples |input| from |sample_rate_hz| to 4 kHz sample rate. The input
  125. // has |input_length| samples, and the method will write |output_length|
  126. // samples to |output|. Compensates for the phase delay of the downsampling
  127. // filters if |compensate_delay| is true. Returns -1 if the input is too short
  128. // to produce |output_length| samples, otherwise 0.
  129. static int DownsampleTo4kHz(const int16_t* input,
  130. size_t input_length,
  131. size_t output_length,
  132. int input_rate_hz,
  133. bool compensate_delay,
  134. int16_t* output);
  135. private:
  136. // Table of constants used in method DspHelper::ParabolicFit().
  137. static const int16_t kParabolaCoefficients[17][3];
  138. RTC_DISALLOW_COPY_AND_ASSIGN(DspHelper);
  139. };
  140. } // namespace webrtc
  141. #endif // MODULES_AUDIO_CODING_NETEQ_DSP_HELPER_H_