blob: 9396d8ff519179ff748ed48e6239544a9b9aeabd [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
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000011#ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_TIME_STRETCH_H_
12#define WEBRTC_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
henrike@webrtc.org88fbb2d2014-05-21 21:18:46 +000017#include "webrtc/base/constructormagic.h"
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +000018#include "webrtc/modules/audio_coding/neteq/audio_multi_vector.h"
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000019#include "webrtc/typedefs.h"
20
21namespace webrtc {
22
23// Forward declarations.
24class 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.
29class 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, size_t num_channels,
39 const BackgroundNoise& background_noise)
40 : sample_rate_hz_(sample_rate_hz),
41 fs_mult_(sample_rate_hz / 8000),
turaj@webrtc.org362a55e2013-09-20 16:25:28 +000042 num_channels_(static_cast<int>(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) {
46 assert(sample_rate_hz_ == 8000 ||
47 sample_rate_hz_ == 16000 ||
48 sample_rate_hz_ == 32000 ||
49 sample_rate_hz_ == 48000);
50 assert(num_channels_ > 0);
51 assert(static_cast<int>(master_channel_) < num_channels_);
52 memset(auto_correlation_, 0, sizeof(auto_correlation_));
53 }
54
55 virtual ~TimeStretch() {}
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,
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000061 AudioMultiVector* output,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000062 int16_t* length_change_samples);
63
64 protected:
65 // Sets the parameters |best_correlation| and |peak_index| to suitable
66 // values when the signal contains no active speech. This method must be
67 // implemented by the sub-classes.
turaj@webrtc.org362a55e2013-09-20 16:25:28 +000068 virtual void SetParametersForPassiveSpeech(size_t input_length,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000069 int16_t* best_correlation,
70 int* peak_index) const = 0;
71
72 // Checks the criteria for performing the time-stretching operation and,
73 // if possible, performs the time-stretching. This method must be implemented
74 // by the sub-classes.
75 virtual ReturnCodes CheckCriteriaAndStretch(
turaj@webrtc.org362a55e2013-09-20 16:25:28 +000076 const int16_t* input, size_t input_length, size_t peak_index,
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000077 int16_t best_correlation, bool active_speech,
henrik.lundin@webrtc.orgfd11bbf2013-09-30 20:38:44 +000078 AudioMultiVector* output) const = 0;
henrik.lundin@webrtc.orgd94659d2013-01-29 12:09:21 +000079
80 static const int kCorrelationLen = 50;
81 static const int kLogCorrelationLen = 6; // >= log2(kCorrelationLen).
82 static const int kMinLag = 10;
83 static const int kMaxLag = 60;
84 static const int kDownsampledLen = kCorrelationLen + kMaxLag;
85 static const int kCorrelationThreshold = 14746; // 0.9 in Q14.
86
87 const int sample_rate_hz_;
88 const int fs_mult_; // Sample rate multiplier = sample_rate_hz_ / 8000.
89 const int num_channels_;
90 const size_t master_channel_;
91 const BackgroundNoise& background_noise_;
92 int16_t max_input_value_;
93 int16_t downsampled_input_[kDownsampledLen];
94 // Adding 1 to the size of |auto_correlation_| because of how it is used
95 // by the peak-detection algorithm.
96 int16_t auto_correlation_[kCorrelationLen + 1];
97
98 private:
99 // Calculates the auto-correlation of |downsampled_input_| and writes the
100 // result to |auto_correlation_|.
101 void AutoCorrelation();
102
103 // Performs a simple voice-activity detection based on the input parameters.
104 bool SpeechDetection(int32_t vec1_energy, int32_t vec2_energy,
105 int peak_index, int scaling) const;
106
107 DISALLOW_COPY_AND_ASSIGN(TimeStretch);
108};
109
110} // namespace webrtc
henrik.lundin@webrtc.org9c55f0f2014-06-09 08:10:28 +0000111#endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_TIME_STRETCH_H_