blob: 72f4d76891f386de3dc12707b79d16141ed07d85 [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_tree.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 "modules/audio_processing/transient/dyadic_decimator.h"
17#include "modules/audio_processing/transient/wpd_node.h"
18#include "rtc_base/checks.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000019
20namespace webrtc {
21
Yves Gerey665174f2018-06-19 15:03:05 +020022WPDTree::WPDTree(size_t data_length,
23 const float* high_pass_coefficients,
24 const float* low_pass_coefficients,
25 size_t coefficients_length,
pbos@webrtc.org788acd12014-12-15 09:41:24 +000026 int levels)
27 : data_length_(data_length),
28 levels_(levels),
29 num_nodes_((1 << (levels + 1)) - 1) {
kwiberg9e2be5f2016-09-14 05:23:22 -070030 RTC_DCHECK_GT(data_length, (static_cast<size_t>(1) << levels));
31 RTC_DCHECK(high_pass_coefficients);
32 RTC_DCHECK(low_pass_coefficients);
33 RTC_DCHECK_GT(levels, 0);
pbos@webrtc.org788acd12014-12-15 09:41:24 +000034 // Size is 1 more, so we can use the array as 1-based. nodes_[0] is never
35 // allocated.
kwiberg85d8bb02016-02-16 20:39:36 -080036 nodes_.reset(new std::unique_ptr<WPDNode>[num_nodes_ + 1]);
pbos@webrtc.org788acd12014-12-15 09:41:24 +000037
38 // Create the first node
39 const float kRootCoefficient = 1.f; // Identity Coefficient.
40 nodes_[1].reset(new WPDNode(data_length, &kRootCoefficient, 1));
41 // Variables used to create the rest of the nodes.
42 size_t index = 1;
43 size_t index_left_child = 0;
44 size_t index_right_child = 0;
45
46 int num_nodes_at_curr_level = 0;
47
48 // Branching each node in each level to create its children. The last level is
49 // not branched (all the nodes of that level are leaves).
50 for (int current_level = 0; current_level < levels; ++current_level) {
51 num_nodes_at_curr_level = 1 << current_level;
52 for (int i = 0; i < num_nodes_at_curr_level; ++i) {
53 index = (1 << current_level) + i;
54 // Obtain the index of the current node children.
55 index_left_child = index * 2;
56 index_right_child = index_left_child + 1;
57 nodes_[index_left_child].reset(new WPDNode(nodes_[index]->length() / 2,
58 low_pass_coefficients,
59 coefficients_length));
60 nodes_[index_right_child].reset(new WPDNode(nodes_[index]->length() / 2,
61 high_pass_coefficients,
62 coefficients_length));
63 }
64 }
65}
66
67WPDTree::~WPDTree() {}
68
69WPDNode* WPDTree::NodeAt(int level, int index) {
aluebs73e23732016-02-11 11:02:49 -080070 if (level < 0 || level > levels_ || index < 0 || index >= 1 << level) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000071 return NULL;
72 }
aluebs73e23732016-02-11 11:02:49 -080073
pbos@webrtc.org788acd12014-12-15 09:41:24 +000074 return nodes_[(1 << level) + index].get();
75}
76
77int WPDTree::Update(const float* data, size_t data_length) {
78 if (!data || data_length != data_length_) {
79 return -1;
80 }
81
82 // Update the root node.
83 int update_result = nodes_[1]->set_data(data, data_length);
84 if (update_result != 0) {
85 return -1;
86 }
87
88 // Variables used to update the rest of the nodes.
89 size_t index = 1;
90 size_t index_left_child = 0;
91 size_t index_right_child = 0;
92
93 int num_nodes_at_curr_level = 0;
94
95 for (int current_level = 0; current_level < levels_; ++current_level) {
96 num_nodes_at_curr_level = 1 << current_level;
97 for (int i = 0; i < num_nodes_at_curr_level; ++i) {
98 index = (1 << current_level) + i;
99 // Obtain the index of the current node children.
100 index_left_child = index * 2;
101 index_right_child = index_left_child + 1;
102
Yves Gerey665174f2018-06-19 15:03:05 +0200103 update_result = nodes_[index_left_child]->Update(nodes_[index]->data(),
104 nodes_[index]->length());
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000105 if (update_result != 0) {
106 return -1;
107 }
108
109 update_result = nodes_[index_right_child]->Update(
110 nodes_[index]->data(), nodes_[index]->length());
111 if (update_result != 0) {
112 return -1;
113 }
114 }
115 }
116
117 return 0;
118}
119
120} // namespace webrtc