blob: 13ad2c8554d15a0be3957cc7b5bb7ac24fbecfe0 [file] [log] [blame]
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_CODING_NETEQ_TIME_STRETCH_H_
12#define MODULES_AUDIO_CODING_NETEQ_TIME_STRETCH_H_
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000013
14#include <assert.h>
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000015#include <string.h> // memset, size_t
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/audio_coding/neteq/audio_multi_vector.h"
18#include "rtc_base/constructormagic.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000019
20namespace webrtc {
21
22// Forward declarations.
23class BackgroundNoise;
24
25// This is the base class for Accelerate and PreemptiveExpand. This class
26// cannot be instantiated, but must be used through either of the derived
27// classes.
28class TimeStretch {
29 public:
30 enum ReturnCodes {
31 kSuccess = 0,
32 kSuccessLowEnergy = 1,
33 kNoStretch = 2,
34 kError = -1
35 };
36
Yves Gerey665174f2018-06-19 15:03:05 +020037 TimeStretch(int sample_rate_hz,
38 size_t num_channels,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000039 const BackgroundNoise& background_noise)
40 : sample_rate_hz_(sample_rate_hz),
41 fs_mult_(sample_rate_hz / 8000),
Peter Kastingdce40cf2015-08-24 14:52:23 -070042 num_channels_(num_channels),
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000043 master_channel_(0), // First channel is master.
44 background_noise_(background_noise),
45 max_input_value_(0) {
Yves Gerey665174f2018-06-19 15:03:05 +020046 assert(sample_rate_hz_ == 8000 || sample_rate_hz_ == 16000 ||
47 sample_rate_hz_ == 32000 || sample_rate_hz_ == 48000);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000048 assert(num_channels_ > 0);
Peter Kastingdce40cf2015-08-24 14:52:23 -070049 assert(master_channel_ < num_channels_);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000050 memset(auto_correlation_, 0, sizeof(auto_correlation_));
51 }
52
53 virtual ~TimeStretch() {}
54
55 // This method performs the processing common to both Accelerate and
56 // PreemptiveExpand.
57 ReturnCodes Process(const int16_t* input,
58 size_t input_len,
Henrik Lundincf808d22015-05-27 14:33:29 +020059 bool fast_mode,
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000060 AudioMultiVector* output,
Peter Kastingdce40cf2015-08-24 14:52:23 -070061 size_t* length_change_samples);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000062
63 protected:
64 // Sets the parameters |best_correlation| and |peak_index| to suitable
65 // values when the signal contains no active speech. This method must be
66 // implemented by the sub-classes.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +000067 virtual void SetParametersForPassiveSpeech(size_t input_length,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000068 int16_t* best_correlation,
Peter Kastingdce40cf2015-08-24 14:52:23 -070069 size_t* peak_index) const = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000070
71 // Checks the criteria for performing the time-stretching operation and,
72 // if possible, performs the time-stretching. This method must be implemented
73 // by the sub-classes.
74 virtual ReturnCodes CheckCriteriaAndStretch(
Henrik Lundincf808d22015-05-27 14:33:29 +020075 const int16_t* input,
76 size_t input_length,
77 size_t peak_index,
78 int16_t best_correlation,
79 bool active_speech,
80 bool fast_mode,
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000081 AudioMultiVector* output) const = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000082
Peter Kastingdce40cf2015-08-24 14:52:23 -070083 static const size_t kCorrelationLen = 50;
84 static const size_t kLogCorrelationLen = 6; // >= log2(kCorrelationLen).
85 static const size_t kMinLag = 10;
86 static const size_t kMaxLag = 60;
87 static const size_t kDownsampledLen = kCorrelationLen + kMaxLag;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000088 static const int kCorrelationThreshold = 14746; // 0.9 in Q14.
89
90 const int sample_rate_hz_;
91 const int fs_mult_; // Sample rate multiplier = sample_rate_hz_ / 8000.
Peter Kastingdce40cf2015-08-24 14:52:23 -070092 const size_t num_channels_;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000093 const size_t master_channel_;
94 const BackgroundNoise& background_noise_;
95 int16_t max_input_value_;
96 int16_t downsampled_input_[kDownsampledLen];
97 // Adding 1 to the size of |auto_correlation_| because of how it is used
98 // by the peak-detection algorithm.
99 int16_t auto_correlation_[kCorrelationLen + 1];
100
101 private:
102 // Calculates the auto-correlation of |downsampled_input_| and writes the
103 // result to |auto_correlation_|.
104 void AutoCorrelation();
105
106 // Performs a simple voice-activity detection based on the input parameters.
Yves Gerey665174f2018-06-19 15:03:05 +0200107 bool SpeechDetection(int32_t vec1_energy,
108 int32_t vec2_energy,
109 size_t peak_index,
110 int scaling) const;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000111
henrikg3c089d72015-09-16 05:37:44 -0700112 RTC_DISALLOW_COPY_AND_ASSIGN(TimeStretch);
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +0000113};
114
115} // namespace webrtc
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200116#endif // MODULES_AUDIO_CODING_NETEQ_TIME_STRETCH_H_