OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/audio_processing/transient/transient_detector.h" | 11 #include "webrtc/modules/audio_processing/transient/transient_detector.h" |
12 | 12 |
13 #include <assert.h> | |
14 #include <float.h> | 13 #include <float.h> |
15 #include <math.h> | 14 #include <math.h> |
16 #include <string.h> | 15 #include <string.h> |
17 | 16 |
18 #include <algorithm> | 17 #include <algorithm> |
19 | 18 |
| 19 #include "webrtc/base/checks.h" |
20 #include "webrtc/modules/audio_processing/transient/common.h" | 20 #include "webrtc/modules/audio_processing/transient/common.h" |
21 #include "webrtc/modules/audio_processing/transient/daubechies_8_wavelet_coeffs.
h" | 21 #include "webrtc/modules/audio_processing/transient/daubechies_8_wavelet_coeffs.
h" |
22 #include "webrtc/modules/audio_processing/transient/moving_moments.h" | 22 #include "webrtc/modules/audio_processing/transient/moving_moments.h" |
23 #include "webrtc/modules/audio_processing/transient/wpd_tree.h" | 23 #include "webrtc/modules/audio_processing/transient/wpd_tree.h" |
24 | 24 |
25 namespace webrtc { | 25 namespace webrtc { |
26 | 26 |
27 static const int kTransientLengthMs = 30; | 27 static const int kTransientLengthMs = 30; |
28 static const int kChunksAtStartupLeftToDelete = | 28 static const int kChunksAtStartupLeftToDelete = |
29 kTransientLengthMs / ts::kChunkSizeMs; | 29 kTransientLengthMs / ts::kChunkSizeMs; |
30 static const float kDetectThreshold = 16.f; | 30 static const float kDetectThreshold = 16.f; |
31 | 31 |
32 TransientDetector::TransientDetector(int sample_rate_hz) | 32 TransientDetector::TransientDetector(int sample_rate_hz) |
33 : samples_per_chunk_(sample_rate_hz * ts::kChunkSizeMs / 1000), | 33 : samples_per_chunk_(sample_rate_hz * ts::kChunkSizeMs / 1000), |
34 last_first_moment_(), | 34 last_first_moment_(), |
35 last_second_moment_(), | 35 last_second_moment_(), |
36 chunks_at_startup_left_to_delete_(kChunksAtStartupLeftToDelete), | 36 chunks_at_startup_left_to_delete_(kChunksAtStartupLeftToDelete), |
37 reference_energy_(1.f), | 37 reference_energy_(1.f), |
38 using_reference_(false) { | 38 using_reference_(false) { |
39 assert(sample_rate_hz == ts::kSampleRate8kHz || | 39 RTC_DCHECK(sample_rate_hz == ts::kSampleRate8kHz || |
40 sample_rate_hz == ts::kSampleRate16kHz || | 40 sample_rate_hz == ts::kSampleRate16kHz || |
41 sample_rate_hz == ts::kSampleRate32kHz || | 41 sample_rate_hz == ts::kSampleRate32kHz || |
42 sample_rate_hz == ts::kSampleRate48kHz); | 42 sample_rate_hz == ts::kSampleRate48kHz); |
43 int samples_per_transient = sample_rate_hz * kTransientLengthMs / 1000; | 43 int samples_per_transient = sample_rate_hz * kTransientLengthMs / 1000; |
44 // Adjustment to avoid data loss while downsampling, making | 44 // Adjustment to avoid data loss while downsampling, making |
45 // |samples_per_chunk_| and |samples_per_transient| always divisible by | 45 // |samples_per_chunk_| and |samples_per_transient| always divisible by |
46 // |kLeaves|. | 46 // |kLeaves|. |
47 samples_per_chunk_ -= samples_per_chunk_ % kLeaves; | 47 samples_per_chunk_ -= samples_per_chunk_ % kLeaves; |
48 samples_per_transient -= samples_per_transient % kLeaves; | 48 samples_per_transient -= samples_per_transient % kLeaves; |
49 | 49 |
50 tree_leaves_data_length_ = samples_per_chunk_ / kLeaves; | 50 tree_leaves_data_length_ = samples_per_chunk_ / kLeaves; |
51 wpd_tree_.reset(new WPDTree(samples_per_chunk_, | 51 wpd_tree_.reset(new WPDTree(samples_per_chunk_, |
52 kDaubechies8HighPassCoefficients, | 52 kDaubechies8HighPassCoefficients, |
(...skipping 12 matching lines...) Expand all Loading... |
65 previous_results_.push_back(0.f); | 65 previous_results_.push_back(0.f); |
66 } | 66 } |
67 } | 67 } |
68 | 68 |
69 TransientDetector::~TransientDetector() {} | 69 TransientDetector::~TransientDetector() {} |
70 | 70 |
71 float TransientDetector::Detect(const float* data, | 71 float TransientDetector::Detect(const float* data, |
72 size_t data_length, | 72 size_t data_length, |
73 const float* reference_data, | 73 const float* reference_data, |
74 size_t reference_length) { | 74 size_t reference_length) { |
75 assert(data && data_length == samples_per_chunk_); | 75 RTC_DCHECK(data); |
| 76 RTC_DCHECK_EQ(samples_per_chunk_, data_length); |
76 | 77 |
77 // TODO(aluebs): Check if these errors can logically happen and if not assert | 78 // TODO(aluebs): Check if these errors can logically happen and if not assert |
78 // on them. | 79 // on them. |
79 if (wpd_tree_->Update(data, samples_per_chunk_) != 0) { | 80 if (wpd_tree_->Update(data, samples_per_chunk_) != 0) { |
80 return -1.f; | 81 return -1.f; |
81 } | 82 } |
82 | 83 |
83 float result = 0.f; | 84 float result = 0.f; |
84 | 85 |
85 for (size_t i = 0; i < kLeaves; ++i) { | 86 for (size_t i = 0; i < kLeaves; ++i) { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 static const float kReferenceNonLinearity = 20.f; | 154 static const float kReferenceNonLinearity = 20.f; |
154 static const float kMemory = 0.99f; | 155 static const float kMemory = 0.99f; |
155 float reference_energy = 0.f; | 156 float reference_energy = 0.f; |
156 for (size_t i = 1; i < length; ++i) { | 157 for (size_t i = 1; i < length; ++i) { |
157 reference_energy += data[i] * data[i]; | 158 reference_energy += data[i] * data[i]; |
158 } | 159 } |
159 if (reference_energy == 0.f) { | 160 if (reference_energy == 0.f) { |
160 using_reference_ = false; | 161 using_reference_ = false; |
161 return 1.f; | 162 return 1.f; |
162 } | 163 } |
163 assert(reference_energy_ != 0); | 164 RTC_DCHECK_NE(0, reference_energy_); |
164 float result = 1.f / (1.f + exp(kReferenceNonLinearity * | 165 float result = 1.f / (1.f + exp(kReferenceNonLinearity * |
165 (kEnergyRatioThreshold - | 166 (kEnergyRatioThreshold - |
166 reference_energy / reference_energy_))); | 167 reference_energy / reference_energy_))); |
167 reference_energy_ = | 168 reference_energy_ = |
168 kMemory * reference_energy_ + (1.f - kMemory) * reference_energy; | 169 kMemory * reference_energy_ + (1.f - kMemory) * reference_energy; |
169 | 170 |
170 using_reference_ = true; | 171 using_reference_ = true; |
171 | 172 |
172 return result; | 173 return result; |
173 } | 174 } |
174 | 175 |
175 } // namespace webrtc | 176 } // namespace webrtc |
OLD | NEW |