time_stretch.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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_TIME_STRETCH_H_
  11. #define MODULES_AUDIO_CODING_NETEQ_TIME_STRETCH_H_
  12. #include <assert.h>
  13. #include <string.h> // memset, size_t
  14. #include "modules/audio_coding/neteq/audio_multi_vector.h"
  15. #include "rtc_base/constructor_magic.h"
  16. namespace webrtc {
  17. // Forward declarations.
  18. class BackgroundNoise;
  19. // This is the base class for Accelerate and PreemptiveExpand. This class
  20. // cannot be instantiated, but must be used through either of the derived
  21. // classes.
  22. class TimeStretch {
  23. public:
  24. enum ReturnCodes {
  25. kSuccess = 0,
  26. kSuccessLowEnergy = 1,
  27. kNoStretch = 2,
  28. kError = -1
  29. };
  30. TimeStretch(int sample_rate_hz,
  31. size_t num_channels,
  32. const BackgroundNoise& background_noise)
  33. : sample_rate_hz_(sample_rate_hz),
  34. fs_mult_(sample_rate_hz / 8000),
  35. num_channels_(num_channels),
  36. background_noise_(background_noise),
  37. max_input_value_(0) {
  38. assert(sample_rate_hz_ == 8000 || sample_rate_hz_ == 16000 ||
  39. sample_rate_hz_ == 32000 || sample_rate_hz_ == 48000);
  40. assert(num_channels_ > 0);
  41. memset(auto_correlation_, 0, sizeof(auto_correlation_));
  42. }
  43. virtual ~TimeStretch() {}
  44. // This method performs the processing common to both Accelerate and
  45. // PreemptiveExpand.
  46. ReturnCodes Process(const int16_t* input,
  47. size_t input_len,
  48. bool fast_mode,
  49. AudioMultiVector* output,
  50. size_t* length_change_samples);
  51. protected:
  52. // Sets the parameters |best_correlation| and |peak_index| to suitable
  53. // values when the signal contains no active speech. This method must be
  54. // implemented by the sub-classes.
  55. virtual void SetParametersForPassiveSpeech(size_t input_length,
  56. int16_t* best_correlation,
  57. size_t* peak_index) const = 0;
  58. // Checks the criteria for performing the time-stretching operation and,
  59. // if possible, performs the time-stretching. This method must be implemented
  60. // by the sub-classes.
  61. virtual ReturnCodes CheckCriteriaAndStretch(
  62. const int16_t* input,
  63. size_t input_length,
  64. size_t peak_index,
  65. int16_t best_correlation,
  66. bool active_speech,
  67. bool fast_mode,
  68. AudioMultiVector* output) const = 0;
  69. static const size_t kCorrelationLen = 50;
  70. static const size_t kLogCorrelationLen = 6; // >= log2(kCorrelationLen).
  71. static const size_t kMinLag = 10;
  72. static const size_t kMaxLag = 60;
  73. static const size_t kDownsampledLen = kCorrelationLen + kMaxLag;
  74. static const int kCorrelationThreshold = 14746; // 0.9 in Q14.
  75. static constexpr size_t kRefChannel = 0; // First channel is reference.
  76. const int sample_rate_hz_;
  77. const int fs_mult_; // Sample rate multiplier = sample_rate_hz_ / 8000.
  78. const size_t num_channels_;
  79. const BackgroundNoise& background_noise_;
  80. int16_t max_input_value_;
  81. int16_t downsampled_input_[kDownsampledLen];
  82. // Adding 1 to the size of |auto_correlation_| because of how it is used
  83. // by the peak-detection algorithm.
  84. int16_t auto_correlation_[kCorrelationLen + 1];
  85. private:
  86. // Calculates the auto-correlation of |downsampled_input_| and writes the
  87. // result to |auto_correlation_|.
  88. void AutoCorrelation();
  89. // Performs a simple voice-activity detection based on the input parameters.
  90. bool SpeechDetection(int32_t vec1_energy,
  91. int32_t vec2_energy,
  92. size_t peak_index,
  93. int scaling) const;
  94. RTC_DISALLOW_COPY_AND_ASSIGN(TimeStretch);
  95. };
  96. } // namespace webrtc
  97. #endif // MODULES_AUDIO_CODING_NETEQ_TIME_STRETCH_H_