blob: 1bb6f9f10a3678536fae38e998392ddf3b965448 [file] [log] [blame]
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001/*
2 * Copyright (c) 2013 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#include "modules/audio_processing/transient/transient_detector.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000012
pbos@webrtc.org788acd12014-12-15 09:41:24 +000013#include <float.h>
14#include <math.h>
15#include <string.h>
16
kwiberg85d8bb02016-02-16 20:39:36 -080017#include <algorithm>
18
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "modules/audio_processing/transient/common.h"
20#include "modules/audio_processing/transient/daubechies_8_wavelet_coeffs.h"
21#include "modules/audio_processing/transient/moving_moments.h"
22#include "modules/audio_processing/transient/wpd_tree.h"
23#include "rtc_base/checks.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000024
25namespace webrtc {
26
27static const int kTransientLengthMs = 30;
28static const int kChunksAtStartupLeftToDelete =
29 kTransientLengthMs / ts::kChunkSizeMs;
30static const float kDetectThreshold = 16.f;
31
32TransientDetector::TransientDetector(int sample_rate_hz)
33 : samples_per_chunk_(sample_rate_hz * ts::kChunkSizeMs / 1000),
34 last_first_moment_(),
35 last_second_moment_(),
36 chunks_at_startup_left_to_delete_(kChunksAtStartupLeftToDelete),
37 reference_energy_(1.f),
38 using_reference_(false) {
kwiberg9e2be5f2016-09-14 05:23:22 -070039 RTC_DCHECK(sample_rate_hz == ts::kSampleRate8kHz ||
40 sample_rate_hz == ts::kSampleRate16kHz ||
41 sample_rate_hz == ts::kSampleRate32kHz ||
42 sample_rate_hz == ts::kSampleRate48kHz);
pbos@webrtc.org788acd12014-12-15 09:41:24 +000043 int samples_per_transient = sample_rate_hz * kTransientLengthMs / 1000;
44 // Adjustment to avoid data loss while downsampling, making
45 // |samples_per_chunk_| and |samples_per_transient| always divisible by
46 // |kLeaves|.
47 samples_per_chunk_ -= samples_per_chunk_ % kLeaves;
48 samples_per_transient -= samples_per_transient % kLeaves;
49
50 tree_leaves_data_length_ = samples_per_chunk_ / kLeaves;
51 wpd_tree_.reset(new WPDTree(samples_per_chunk_,
52 kDaubechies8HighPassCoefficients,
53 kDaubechies8LowPassCoefficients,
54 kDaubechies8CoefficientsLength,
55 kLevels));
56 for (size_t i = 0; i < kLeaves; ++i) {
57 moving_moments_[i].reset(
58 new MovingMoments(samples_per_transient / kLeaves));
59 }
60
61 first_moments_.reset(new float[tree_leaves_data_length_]);
62 second_moments_.reset(new float[tree_leaves_data_length_]);
63
64 for (int i = 0; i < kChunksAtStartupLeftToDelete; ++i) {
65 previous_results_.push_back(0.f);
66 }
67}
68
69TransientDetector::~TransientDetector() {}
70
71float TransientDetector::Detect(const float* data,
72 size_t data_length,
73 const float* reference_data,
74 size_t reference_length) {
kwiberg9e2be5f2016-09-14 05:23:22 -070075 RTC_DCHECK(data);
76 RTC_DCHECK_EQ(samples_per_chunk_, data_length);
pbos@webrtc.org788acd12014-12-15 09:41:24 +000077
78 // TODO(aluebs): Check if these errors can logically happen and if not assert
79 // on them.
80 if (wpd_tree_->Update(data, samples_per_chunk_) != 0) {
81 return -1.f;
82 }
83
84 float result = 0.f;
85
86 for (size_t i = 0; i < kLeaves; ++i) {
87 WPDNode* leaf = wpd_tree_->NodeAt(kLevels, i);
88
89 moving_moments_[i]->CalculateMoments(leaf->data(),
90 tree_leaves_data_length_,
91 first_moments_.get(),
92 second_moments_.get());
93
94 // Add value delayed (Use the last moments from the last call to Detect).
95 float unbiased_data = leaf->data()[0] - last_first_moment_[i];
96 result +=
97 unbiased_data * unbiased_data / (last_second_moment_[i] + FLT_MIN);
98
99 // Add new values.
100 for (size_t j = 1; j < tree_leaves_data_length_; ++j) {
101 unbiased_data = leaf->data()[j] - first_moments_[j - 1];
102 result +=
103 unbiased_data * unbiased_data / (second_moments_[j - 1] + FLT_MIN);
104 }
105
106 last_first_moment_[i] = first_moments_[tree_leaves_data_length_ - 1];
107 last_second_moment_[i] = second_moments_[tree_leaves_data_length_ - 1];
108 }
109
110 result /= tree_leaves_data_length_;
111
112 result *= ReferenceDetectionValue(reference_data, reference_length);
113
114 if (chunks_at_startup_left_to_delete_ > 0) {
115 chunks_at_startup_left_to_delete_--;
116 result = 0.f;
117 }
118
119 if (result >= kDetectThreshold) {
120 result = 1.f;
121 } else {
122 // Get proportional value.
123 // Proportion achieved with a squared raised cosine function with domain
124 // [0, kDetectThreshold) and image [0, 1), it's always increasing.
125 const float horizontal_scaling = ts::kPi / kDetectThreshold;
126 const float kHorizontalShift = ts::kPi;
127 const float kVerticalScaling = 0.5f;
128 const float kVerticalShift = 1.f;
129
130 result = (cos(result * horizontal_scaling + kHorizontalShift)
131 + kVerticalShift) * kVerticalScaling;
132 result *= result;
133 }
134
135 previous_results_.pop_front();
136 previous_results_.push_back(result);
137
138 // In the current implementation we return the max of the current result and
139 // the previous results, so the high results have a width equals to
140 // |transient_length|.
141 return *std::max_element(previous_results_.begin(), previous_results_.end());
142}
143
144// Looks for the highest slope and compares it with the previous ones.
145// An exponential transformation takes this to the [0, 1] range. This value is
146// multiplied by the detection result to avoid false positives.
147float TransientDetector::ReferenceDetectionValue(const float* data,
148 size_t length) {
149 if (data == NULL) {
150 using_reference_ = false;
151 return 1.f;
152 }
153 static const float kEnergyRatioThreshold = 0.2f;
154 static const float kReferenceNonLinearity = 20.f;
155 static const float kMemory = 0.99f;
156 float reference_energy = 0.f;
157 for (size_t i = 1; i < length; ++i) {
158 reference_energy += data[i] * data[i];
159 }
160 if (reference_energy == 0.f) {
161 using_reference_ = false;
162 return 1.f;
163 }
kwiberg9e2be5f2016-09-14 05:23:22 -0700164 RTC_DCHECK_NE(0, reference_energy_);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000165 float result = 1.f / (1.f + exp(kReferenceNonLinearity *
166 (kEnergyRatioThreshold -
167 reference_energy / reference_energy_)));
168 reference_energy_ =
169 kMemory * reference_energy_ + (1.f - kMemory) * reference_energy;
170
171 using_reference_ = true;
172
173 return result;
174}
175
176} // namespace webrtc