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