blob: ed4610fc1813539c05027afdd90306717f9c4f5c [file] [log] [blame]
andrew@webrtc.org325cff02014-10-01 17:42:18 +00001/*
2 * Copyright (c) 2014 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
11#include "webrtc/common_audio/lapped_transform.h"
12
13#include <cstdlib>
14#include <cstring>
15
16#include "webrtc/base/checks.h"
17#include "webrtc/common_audio/real_fourier.h"
18
19namespace webrtc {
20
21void LappedTransform::BlockThunk::ProcessBlock(const float* const* input,
22 int num_frames,
23 int num_input_channels,
24 int num_output_channels,
25 float* const* output) {
26 CHECK_EQ(num_input_channels, parent_->in_channels_);
27 CHECK_EQ(num_output_channels, parent_->out_channels_);
28 CHECK_EQ(parent_->block_length_, num_frames);
29
30 for (int i = 0; i < num_input_channels; ++i) {
31 memcpy(parent_->real_buf_.Row(i), input[i],
32 num_frames * sizeof(*input[0]));
33 parent_->fft_.Forward(parent_->real_buf_.Row(i), parent_->cplx_pre_.Row(i));
34 }
35
36 int block_length = RealFourier::ComplexLength(
37 RealFourier::FftOrder(num_frames));
38 CHECK_EQ(parent_->cplx_length_, block_length);
39 parent_->block_processor_->ProcessAudioBlock(parent_->cplx_pre_.Array(),
40 num_input_channels,
41 parent_->cplx_length_,
42 num_output_channels,
43 parent_->cplx_post_.Array());
44
45 for (int i = 0; i < num_output_channels; ++i) {
46 parent_->fft_.Inverse(parent_->cplx_post_.Row(i),
47 parent_->real_buf_.Row(i));
48 memcpy(output[i], parent_->real_buf_.Row(i),
49 num_frames * sizeof(*input[0]));
50 }
51}
52
53LappedTransform::LappedTransform(int in_channels, int out_channels,
54 int chunk_length, const float* window,
55 int block_length, int shift_amount,
56 Callback* callback)
57 : blocker_callback_(this),
58 in_channels_(in_channels),
59 out_channels_(out_channels),
60 window_(window),
61 own_window_(false),
62 window_shift_amount_(shift_amount),
63 block_length_(block_length),
64 chunk_length_(chunk_length),
65 block_processor_(callback),
66 blocker_(nullptr),
67 fft_(RealFourier::FftOrder(block_length_)),
68 cplx_length_(RealFourier::ComplexLength(fft_.order())),
69 real_buf_(in_channels, block_length, RealFourier::kFftBufferAlignment),
70 cplx_pre_(in_channels, cplx_length_, RealFourier::kFftBufferAlignment),
71 cplx_post_(out_channels, cplx_length_, RealFourier::kFftBufferAlignment) {
72 CHECK(in_channels_ > 0 && out_channels_ > 0);
73 CHECK_GT(block_length_, 0);
74 CHECK_GT(chunk_length_, 0);
75 CHECK(block_processor_);
76 CHECK_EQ(0, block_length & (block_length - 1)); // block_length_ power of 2?
77
78 if (!window_) {
79 own_window_ = true;
80 window_ = new float[block_length_];
81 CHECK(window_ != nullptr);
82 window_shift_amount_ = block_length_;
83 float* temp = const_cast<float*>(window_);
84 for (int i = 0; i < block_length_; ++i) {
85 temp[i] = 1.0f;
86 }
87 }
88
89 blocker_.reset(new Blocker(chunk_length_, block_length_, in_channels_,
90 out_channels_, window_, window_shift_amount_,
91 &blocker_callback_));
92}
93
94LappedTransform::~LappedTransform() {
95 if (own_window_) {
96 delete [] window_;
97 }
98}
99
100void LappedTransform::ProcessChunk(const float* const* in_chunk,
101 float* const* out_chunk) {
102 blocker_->ProcessChunk(in_chunk, chunk_length_, in_channels_, out_channels_,
103 out_chunk);
104}
105
106} // namespace webrtc
107