andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
mgraczyk@chromium.org | e534086 | 2015-03-12 23:23:38 +0000 | [diff] [blame] | 13 | #include <algorithm> |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 14 | #include <cmath> |
| 15 | #include <cstring> |
| 16 | |
| 17 | #include "testing/gtest/include/gtest/gtest.h" |
| 18 | |
| 19 | using std::complex; |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | class NoopCallback : public webrtc::LappedTransform::Callback { |
| 24 | public: |
| 25 | NoopCallback() : block_num_(0) {} |
| 26 | |
| 27 | virtual void ProcessAudioBlock(const complex<float>* const* in_block, |
| 28 | int in_channels, int frames, int out_channels, |
| 29 | complex<float>* const* out_block) { |
| 30 | CHECK_EQ(in_channels, out_channels); |
| 31 | for (int i = 0; i < out_channels; ++i) { |
| 32 | memcpy(out_block[i], in_block[i], sizeof(**in_block) * frames); |
| 33 | } |
| 34 | ++block_num_; |
| 35 | } |
| 36 | |
| 37 | int block_num() { |
| 38 | return block_num_; |
| 39 | } |
| 40 | |
| 41 | private: |
| 42 | int block_num_; |
| 43 | }; |
| 44 | |
| 45 | class FftCheckerCallback : public webrtc::LappedTransform::Callback { |
| 46 | public: |
| 47 | FftCheckerCallback() : block_num_(0) {} |
| 48 | |
| 49 | virtual void ProcessAudioBlock(const complex<float>* const* in_block, |
| 50 | int in_channels, int frames, int out_channels, |
| 51 | complex<float>* const* out_block) { |
| 52 | CHECK_EQ(in_channels, out_channels); |
| 53 | |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame^] | 54 | int full_length = (frames - 1) * 2; |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 55 | ++block_num_; |
| 56 | |
aluebs@webrtc.org | c0da63c | 2015-01-13 22:28:35 +0000 | [diff] [blame] | 57 | if (block_num_ > 0) { |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame^] | 58 | ASSERT_NEAR(in_block[0][0].real(), static_cast<float>(full_length), |
| 59 | 1e-5f); |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 60 | ASSERT_NEAR(in_block[0][0].imag(), 0.0f, 1e-5f); |
| 61 | for (int i = 1; i < frames; ++i) { |
| 62 | ASSERT_NEAR(in_block[0][i].real(), 0.0f, 1e-5f); |
| 63 | ASSERT_NEAR(in_block[0][i].imag(), 0.0f, 1e-5f); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | int block_num() { |
| 69 | return block_num_; |
| 70 | } |
| 71 | |
| 72 | private: |
| 73 | int block_num_; |
| 74 | }; |
| 75 | |
| 76 | void SetFloatArray(float value, int rows, int cols, float* const* array) { |
| 77 | for (int i = 0; i < rows; ++i) { |
| 78 | for (int j = 0; j < cols; ++j) { |
| 79 | array[i][j] = value; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | } // namespace |
| 85 | |
| 86 | namespace webrtc { |
| 87 | |
| 88 | TEST(LappedTransformTest, Windowless) { |
| 89 | const int kChannels = 3; |
| 90 | const int kChunkLength = 512; |
| 91 | const int kBlockLength = 64; |
mgraczyk@chromium.org | e534086 | 2015-03-12 23:23:38 +0000 | [diff] [blame] | 92 | const int kShiftAmount = 64; |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 93 | NoopCallback noop; |
mgraczyk@chromium.org | e534086 | 2015-03-12 23:23:38 +0000 | [diff] [blame] | 94 | |
| 95 | // Rectangular window. |
| 96 | float window[kBlockLength]; |
| 97 | std::fill(window, &window[kBlockLength], 1.0f); |
| 98 | |
| 99 | LappedTransform trans(kChannels, kChannels, kChunkLength, window, |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 100 | kBlockLength, kShiftAmount, &noop); |
| 101 | float in_buffer[kChannels][kChunkLength]; |
| 102 | float* in_chunk[kChannels]; |
| 103 | float out_buffer[kChannels][kChunkLength]; |
| 104 | float* out_chunk[kChannels]; |
| 105 | |
| 106 | in_chunk[0] = in_buffer[0]; |
| 107 | in_chunk[1] = in_buffer[1]; |
| 108 | in_chunk[2] = in_buffer[2]; |
| 109 | out_chunk[0] = out_buffer[0]; |
| 110 | out_chunk[1] = out_buffer[1]; |
| 111 | out_chunk[2] = out_buffer[2]; |
| 112 | SetFloatArray(2.0f, kChannels, kChunkLength, in_chunk); |
| 113 | SetFloatArray(-1.0f, kChannels, kChunkLength, out_chunk); |
| 114 | |
| 115 | trans.ProcessChunk(in_chunk, out_chunk); |
| 116 | |
| 117 | for (int i = 0; i < kChannels; ++i) { |
| 118 | for (int j = 0; j < kChunkLength; ++j) { |
aluebs@webrtc.org | c0da63c | 2015-01-13 22:28:35 +0000 | [diff] [blame] | 119 | ASSERT_NEAR(out_chunk[i][j], 2.0f, 1e-5f); |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | ASSERT_EQ(kChunkLength / kBlockLength, noop.block_num()); |
| 124 | } |
| 125 | |
| 126 | TEST(LappedTransformTest, IdentityProcessor) { |
| 127 | const int kChunkLength = 512; |
| 128 | const int kBlockLength = 64; |
| 129 | const int kShiftAmount = 32; |
| 130 | NoopCallback noop; |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 131 | |
| 132 | // Identity window for |overlap = block_size / 2|. |
mgraczyk@chromium.org | e534086 | 2015-03-12 23:23:38 +0000 | [diff] [blame] | 133 | float window[kBlockLength]; |
| 134 | std::fill(window, &window[kBlockLength], std::sqrt(0.5f)); |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 135 | |
| 136 | LappedTransform trans(1, 1, kChunkLength, window, kBlockLength, kShiftAmount, |
| 137 | &noop); |
| 138 | float in_buffer[kChunkLength]; |
| 139 | float* in_chunk = in_buffer; |
| 140 | float out_buffer[kChunkLength]; |
| 141 | float* out_chunk = out_buffer; |
| 142 | |
| 143 | SetFloatArray(2.0f, 1, kChunkLength, &in_chunk); |
| 144 | SetFloatArray(-1.0f, 1, kChunkLength, &out_chunk); |
| 145 | |
| 146 | trans.ProcessChunk(&in_chunk, &out_chunk); |
| 147 | |
| 148 | for (int i = 0; i < kChunkLength; ++i) { |
aluebs@webrtc.org | c0da63c | 2015-01-13 22:28:35 +0000 | [diff] [blame] | 149 | ASSERT_NEAR(out_chunk[i], |
| 150 | (i < kBlockLength - kShiftAmount) ? 0.0f : 2.0f, |
| 151 | 1e-5f); |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | ASSERT_EQ(kChunkLength / kShiftAmount, noop.block_num()); |
| 155 | } |
| 156 | |
| 157 | TEST(LappedTransformTest, Callbacks) { |
| 158 | const int kChunkLength = 512; |
| 159 | const int kBlockLength = 64; |
| 160 | FftCheckerCallback call; |
mgraczyk@chromium.org | e534086 | 2015-03-12 23:23:38 +0000 | [diff] [blame] | 161 | |
| 162 | // Rectangular window. |
| 163 | float window[kBlockLength]; |
| 164 | std::fill(window, &window[kBlockLength], 1.0f); |
| 165 | |
| 166 | LappedTransform trans(1, 1, kChunkLength, window, kBlockLength, |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 167 | kBlockLength, &call); |
| 168 | float in_buffer[kChunkLength]; |
| 169 | float* in_chunk = in_buffer; |
| 170 | float out_buffer[kChunkLength]; |
| 171 | float* out_chunk = out_buffer; |
| 172 | |
| 173 | SetFloatArray(1.0f, 1, kChunkLength, &in_chunk); |
| 174 | SetFloatArray(-1.0f, 1, kChunkLength, &out_chunk); |
| 175 | |
| 176 | trans.ProcessChunk(&in_chunk, &out_chunk); |
| 177 | |
| 178 | ASSERT_EQ(kChunkLength / kBlockLength, call.block_num()); |
| 179 | } |
| 180 | |
Michael Graczyk | cc84649 | 2015-05-28 18:01:33 -0700 | [diff] [blame] | 181 | TEST(LappedTransformTest, chunk_length) { |
Michael Graczyk | 9b720f7 | 2015-05-27 17:09:47 -0700 | [diff] [blame] | 182 | const int kBlockLength = 64; |
| 183 | FftCheckerCallback call; |
| 184 | const float window[kBlockLength] = {}; |
andrew@webrtc.org | 325cff0 | 2014-10-01 17:42:18 +0000 | [diff] [blame] | 185 | |
Michael Graczyk | cc84649 | 2015-05-28 18:01:33 -0700 | [diff] [blame] | 186 | // Make sure that chunk_length returns the same value passed to the |
Michael Graczyk | 9b720f7 | 2015-05-27 17:09:47 -0700 | [diff] [blame] | 187 | // LappedTransform constructor. |
| 188 | { |
| 189 | const int kExpectedChunkLength = 512; |
| 190 | const LappedTransform trans(1, 1, kExpectedChunkLength, window, |
| 191 | kBlockLength, kBlockLength, &call); |
| 192 | |
Michael Graczyk | cc84649 | 2015-05-28 18:01:33 -0700 | [diff] [blame] | 193 | EXPECT_EQ(kExpectedChunkLength, trans.chunk_length()); |
Michael Graczyk | 9b720f7 | 2015-05-27 17:09:47 -0700 | [diff] [blame] | 194 | } |
| 195 | { |
| 196 | const int kExpectedChunkLength = 160; |
| 197 | const LappedTransform trans(1, 1, kExpectedChunkLength, window, |
| 198 | kBlockLength, kBlockLength, &call); |
| 199 | |
Michael Graczyk | cc84649 | 2015-05-28 18:01:33 -0700 | [diff] [blame] | 200 | EXPECT_EQ(kExpectedChunkLength, trans.chunk_length()); |
Michael Graczyk | 9b720f7 | 2015-05-27 17:09:47 -0700 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
| 204 | } // namespace webrtc |