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