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 "modules/audio_coding/codecs/opus/test/lapped_transform.h"
12
13 #include <algorithm>
14 #include <cmath>
15 #include <cstring>
16
17 #include "test/gtest.h"
18
19 using std::complex;
20
21 namespace {
22
23 class NoopCallback : public webrtc::LappedTransform::Callback {
24 public:
NoopCallback()25 NoopCallback() : block_num_(0) {}
26
ProcessAudioBlock(const complex<float> * const * in_block,size_t in_channels,size_t frames,size_t out_channels,complex<float> * const * out_block)27 void ProcessAudioBlock(const complex<float>* const* in_block,
28 size_t in_channels,
29 size_t frames,
30 size_t out_channels,
31 complex<float>* const* out_block) override {
32 RTC_CHECK_EQ(in_channels, out_channels);
33 for (size_t i = 0; i < out_channels; ++i) {
34 memcpy(out_block[i], in_block[i], sizeof(**in_block) * frames);
35 }
36 ++block_num_;
37 }
38
block_num()39 size_t block_num() { return block_num_; }
40
41 private:
42 size_t block_num_;
43 };
44
45 class FftCheckerCallback : public webrtc::LappedTransform::Callback {
46 public:
FftCheckerCallback()47 FftCheckerCallback() : block_num_(0) {}
48
ProcessAudioBlock(const complex<float> * const * in_block,size_t in_channels,size_t frames,size_t out_channels,complex<float> * const * out_block)49 void ProcessAudioBlock(const complex<float>* const* in_block,
50 size_t in_channels,
51 size_t frames,
52 size_t out_channels,
53 complex<float>* const* out_block) override {
54 RTC_CHECK_EQ(in_channels, out_channels);
55
56 size_t full_length = (frames - 1) * 2;
57 ++block_num_;
58
59 if (block_num_ > 0) {
60 ASSERT_NEAR(in_block[0][0].real(), static_cast<float>(full_length),
61 1e-5f);
62 ASSERT_NEAR(in_block[0][0].imag(), 0.0f, 1e-5f);
63 for (size_t i = 1; i < frames; ++i) {
64 ASSERT_NEAR(in_block[0][i].real(), 0.0f, 1e-5f);
65 ASSERT_NEAR(in_block[0][i].imag(), 0.0f, 1e-5f);
66 }
67 }
68 }
69
block_num()70 size_t block_num() { return block_num_; }
71
72 private:
73 size_t block_num_;
74 };
75
SetFloatArray(float value,int rows,int cols,float * const * array)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
TEST(LappedTransformTest,Windowless)88 TEST(LappedTransformTest, Windowless) {
89 const size_t kChannels = 3;
90 const size_t kChunkLength = 512;
91 const size_t kBlockLength = 64;
92 const size_t kShiftAmount = 64;
93 NoopCallback noop;
94
95 // Rectangular window.
96 float window[kBlockLength];
97 std::fill(window, &window[kBlockLength], 1.0f);
98
99 LappedTransform trans(kChannels, kChannels, kChunkLength, window,
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 (size_t i = 0; i < kChannels; ++i) {
118 for (size_t j = 0; j < kChunkLength; ++j) {
119 ASSERT_NEAR(out_chunk[i][j], 2.0f, 1e-5f);
120 }
121 }
122
123 ASSERT_EQ(kChunkLength / kBlockLength, noop.block_num());
124 }
125
TEST(LappedTransformTest,IdentityProcessor)126 TEST(LappedTransformTest, IdentityProcessor) {
127 const size_t kChunkLength = 512;
128 const size_t kBlockLength = 64;
129 const size_t kShiftAmount = 32;
130 NoopCallback noop;
131
132 // Identity window for |overlap = block_size / 2|.
133 float window[kBlockLength];
134 std::fill(window, &window[kBlockLength], std::sqrt(0.5f));
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 (size_t i = 0; i < kChunkLength; ++i) {
149 ASSERT_NEAR(out_chunk[i], (i < kBlockLength - kShiftAmount) ? 0.0f : 2.0f,
150 1e-5f);
151 }
152
153 ASSERT_EQ(kChunkLength / kShiftAmount, noop.block_num());
154 }
155
TEST(LappedTransformTest,Callbacks)156 TEST(LappedTransformTest, Callbacks) {
157 const size_t kChunkLength = 512;
158 const size_t kBlockLength = 64;
159 FftCheckerCallback call;
160
161 // Rectangular window.
162 float window[kBlockLength];
163 std::fill(window, &window[kBlockLength], 1.0f);
164
165 LappedTransform trans(1, 1, kChunkLength, window, kBlockLength, kBlockLength,
166 &call);
167 float in_buffer[kChunkLength];
168 float* in_chunk = in_buffer;
169 float out_buffer[kChunkLength];
170 float* out_chunk = out_buffer;
171
172 SetFloatArray(1.0f, 1, kChunkLength, &in_chunk);
173 SetFloatArray(-1.0f, 1, kChunkLength, &out_chunk);
174
175 trans.ProcessChunk(&in_chunk, &out_chunk);
176
177 ASSERT_EQ(kChunkLength / kBlockLength, call.block_num());
178 }
179
TEST(LappedTransformTest,chunk_length)180 TEST(LappedTransformTest, chunk_length) {
181 const size_t kBlockLength = 64;
182 FftCheckerCallback call;
183 const float window[kBlockLength] = {};
184
185 // Make sure that chunk_length returns the same value passed to the
186 // LappedTransform constructor.
187 {
188 const size_t kExpectedChunkLength = 512;
189 const LappedTransform trans(1, 1, kExpectedChunkLength, window,
190 kBlockLength, kBlockLength, &call);
191
192 EXPECT_EQ(kExpectedChunkLength, trans.chunk_length());
193 }
194 {
195 const size_t kExpectedChunkLength = 160;
196 const LappedTransform trans(1, 1, kExpectedChunkLength, window,
197 kBlockLength, kBlockLength, &call);
198
199 EXPECT_EQ(kExpectedChunkLength, trans.chunk_length());
200 }
201 }
202
203 } // namespace webrtc
204