blob: 20d6a9013e41af1ddf06d402b577c716523b0ffb [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/wpd_node.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000012
pbos@webrtc.org788acd12014-12-15 09:41:24 +000013#include <math.h>
14#include <string.h>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "common_audio/fir_filter.h"
Patrik Höglundf715c532017-11-17 11:04:15 +010017#include "common_audio/fir_filter_factory.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/audio_processing/transient/dyadic_decimator.h"
19#include "rtc_base/checks.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000020
21namespace webrtc {
22
23WPDNode::WPDNode(size_t length,
24 const float* coefficients,
25 size_t coefficients_length)
26 : // The data buffer has parent data length to be able to contain and filter
27 // it.
28 data_(new float[2 * length + 1]),
29 length_(length),
Patrik Höglundf715c532017-11-17 11:04:15 +010030 filter_(CreateFirFilter(coefficients,
31 coefficients_length,
32 2 * length + 1)) {
kwibergaf476c72016-11-28 15:21:39 -080033 RTC_DCHECK_GT(length, 0);
kwiberg9e2be5f2016-09-14 05:23:22 -070034 RTC_DCHECK(coefficients);
kwibergaf476c72016-11-28 15:21:39 -080035 RTC_DCHECK_GT(coefficients_length, 0);
pbos@webrtc.org788acd12014-12-15 09:41:24 +000036 memset(data_.get(), 0.f, (2 * length + 1) * sizeof(data_[0]));
37}
38
39WPDNode::~WPDNode() {}
40
41int WPDNode::Update(const float* parent_data, size_t parent_data_length) {
42 if (!parent_data || (parent_data_length / 2) != length_) {
43 return -1;
44 }
45
46 // Filter data.
47 filter_->Filter(parent_data, parent_data_length, data_.get());
48
49 // Decimate data.
50 const bool kOddSequence = true;
51 size_t output_samples = DyadicDecimate(
52 data_.get(), parent_data_length, kOddSequence, data_.get(), length_);
53 if (output_samples != length_) {
54 return -1;
55 }
56
57 // Get abs to all values.
58 for (size_t i = 0; i < length_; ++i) {
59 data_[i] = fabs(data_[i]);
60 }
61
62 return 0;
63}
64
65int WPDNode::set_data(const float* new_data, size_t length) {
66 if (!new_data || length != length_) {
67 return -1;
68 }
69 memcpy(data_.get(), new_data, length * sizeof(data_[0]));
70 return 0;
71}
72
73} // namespace webrtc