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