1788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org/*
2788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org *
4788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org *  Use of this source code is governed by a BSD-style license
5788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org *  that can be found in the LICENSE file in the root of the source
6788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org *  tree. An additional intellectual property rights grant can be found
7788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org *  in the file PATENTS.  All contributing project authors may
8788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
9788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org */
10788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org
11788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org#include "webrtc/modules/audio_processing/transient/wpd_node.h"
12788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org
13788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org#include <assert.h>
14788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org#include <math.h>
15788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org#include <string.h>
16788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org
1700b8f6b3643332cce1ee711715f7fbb824d793cakwiberg@webrtc.org#include "webrtc/base/scoped_ptr.h"
18788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org#include "webrtc/common_audio/fir_filter.h"
19788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org#include "webrtc/modules/audio_processing/transient/dyadic_decimator.h"
20788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org
21788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.orgnamespace webrtc {
22788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org
23788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.orgWPDNode::WPDNode(size_t length,
24788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org                 const float* coefficients,
25788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org                 size_t coefficients_length)
26788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org    : // The data buffer has parent data length to be able to contain and filter
27788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org      // it.
28788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org      data_(new float[2 * length + 1]),
29788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org      length_(length),
30788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org      filter_(FIRFilter::Create(coefficients,
31788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org                                coefficients_length,
32788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org                                2 * length + 1)) {
33788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org  assert(length > 0 && coefficients && coefficients_length > 0);
34788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org  memset(data_.get(), 0.f, (2 * length + 1) * sizeof(data_[0]));
35788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org}
36788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org
37788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.orgWPDNode::~WPDNode() {}
38788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org
39788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.orgint WPDNode::Update(const float* parent_data, size_t parent_data_length) {
40788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org  if (!parent_data || (parent_data_length / 2) != length_) {
41788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org    return -1;
42788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org  }
43788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org
44788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org  // Filter data.
45788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org  filter_->Filter(parent_data, parent_data_length, data_.get());
46788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org
47788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org  // Decimate data.
48788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org  const bool kOddSequence = true;
49788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org  size_t output_samples = DyadicDecimate(
50788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org      data_.get(), parent_data_length, kOddSequence, data_.get(), length_);
51788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org  if (output_samples != length_) {
52788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org    return -1;
53788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org  }
54788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org
55788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org  // Get abs to all values.
56788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org  for (size_t i = 0; i < length_; ++i) {
57788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org    data_[i] = fabs(data_[i]);
58788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org  }
59788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org
60788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org  return 0;
61788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org}
62788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org
63788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.orgint WPDNode::set_data(const float* new_data, size_t length) {
64788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org  if (!new_data || length != length_) {
65788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org    return -1;
66788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org  }
67788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org  memcpy(data_.get(), new_data, length * sizeof(data_[0]));
68788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org  return 0;
69788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org}
70788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org
71788acd17adf6b3d605b5ea66cf394eb81fc086a9pbos@webrtc.org}  // namespace webrtc
72