OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 18 matching lines...) Expand all Loading... |
29 size_t frames, | 29 size_t frames, |
30 int out_channels, | 30 int out_channels, |
31 complex<float>* const* out_block) { | 31 complex<float>* const* out_block) { |
32 RTC_CHECK_EQ(in_channels, out_channels); | 32 RTC_CHECK_EQ(in_channels, out_channels); |
33 for (int i = 0; i < out_channels; ++i) { | 33 for (int i = 0; i < out_channels; ++i) { |
34 memcpy(out_block[i], in_block[i], sizeof(**in_block) * frames); | 34 memcpy(out_block[i], in_block[i], sizeof(**in_block) * frames); |
35 } | 35 } |
36 ++block_num_; | 36 ++block_num_; |
37 } | 37 } |
38 | 38 |
39 int block_num() { | 39 size_t block_num() { |
40 return block_num_; | 40 return block_num_; |
41 } | 41 } |
42 | 42 |
43 private: | 43 private: |
44 int block_num_; | 44 size_t block_num_; |
45 }; | 45 }; |
46 | 46 |
47 class FftCheckerCallback : public webrtc::LappedTransform::Callback { | 47 class FftCheckerCallback : public webrtc::LappedTransform::Callback { |
48 public: | 48 public: |
49 FftCheckerCallback() : block_num_(0) {} | 49 FftCheckerCallback() : block_num_(0) {} |
50 | 50 |
51 virtual void ProcessAudioBlock(const complex<float>* const* in_block, | 51 virtual void ProcessAudioBlock(const complex<float>* const* in_block, |
52 int in_channels, | 52 int in_channels, |
53 size_t frames, | 53 size_t frames, |
54 int out_channels, | 54 int out_channels, |
55 complex<float>* const* out_block) { | 55 complex<float>* const* out_block) { |
56 RTC_CHECK_EQ(in_channels, out_channels); | 56 RTC_CHECK_EQ(in_channels, out_channels); |
57 | 57 |
58 size_t full_length = (frames - 1) * 2; | 58 size_t full_length = (frames - 1) * 2; |
59 ++block_num_; | 59 ++block_num_; |
60 | 60 |
61 if (block_num_ > 0) { | 61 if (block_num_ > 0) { |
62 ASSERT_NEAR(in_block[0][0].real(), static_cast<float>(full_length), | 62 ASSERT_NEAR(in_block[0][0].real(), static_cast<float>(full_length), |
63 1e-5f); | 63 1e-5f); |
64 ASSERT_NEAR(in_block[0][0].imag(), 0.0f, 1e-5f); | 64 ASSERT_NEAR(in_block[0][0].imag(), 0.0f, 1e-5f); |
65 for (size_t i = 1; i < frames; ++i) { | 65 for (size_t i = 1; i < frames; ++i) { |
66 ASSERT_NEAR(in_block[0][i].real(), 0.0f, 1e-5f); | 66 ASSERT_NEAR(in_block[0][i].real(), 0.0f, 1e-5f); |
67 ASSERT_NEAR(in_block[0][i].imag(), 0.0f, 1e-5f); | 67 ASSERT_NEAR(in_block[0][i].imag(), 0.0f, 1e-5f); |
68 } | 68 } |
69 } | 69 } |
70 } | 70 } |
71 | 71 |
72 int block_num() { | 72 size_t block_num() { |
73 return block_num_; | 73 return block_num_; |
74 } | 74 } |
75 | 75 |
76 private: | 76 private: |
77 int block_num_; | 77 size_t block_num_; |
78 }; | 78 }; |
79 | 79 |
80 void SetFloatArray(float value, int rows, int cols, float* const* array) { | 80 void SetFloatArray(float value, int rows, int cols, float* const* array) { |
81 for (int i = 0; i < rows; ++i) { | 81 for (int i = 0; i < rows; ++i) { |
82 for (int j = 0; j < cols; ++j) { | 82 for (int j = 0; j < cols; ++j) { |
83 array[i][j] = value; | 83 array[i][j] = value; |
84 } | 84 } |
85 } | 85 } |
86 } | 86 } |
87 | 87 |
88 } // namespace | 88 } // namespace |
89 | 89 |
90 namespace webrtc { | 90 namespace webrtc { |
91 | 91 |
92 TEST(LappedTransformTest, Windowless) { | 92 TEST(LappedTransformTest, Windowless) { |
93 const int kChannels = 3; | 93 const int kChannels = 3; |
94 const int kChunkLength = 512; | 94 const size_t kChunkLength = 512; |
95 const int kBlockLength = 64; | 95 const size_t kBlockLength = 64; |
96 const int kShiftAmount = 64; | 96 const size_t kShiftAmount = 64; |
97 NoopCallback noop; | 97 NoopCallback noop; |
98 | 98 |
99 // Rectangular window. | 99 // Rectangular window. |
100 float window[kBlockLength]; | 100 float window[kBlockLength]; |
101 std::fill(window, &window[kBlockLength], 1.0f); | 101 std::fill(window, &window[kBlockLength], 1.0f); |
102 | 102 |
103 LappedTransform trans(kChannels, kChannels, kChunkLength, window, | 103 LappedTransform trans(kChannels, kChannels, kChunkLength, window, |
104 kBlockLength, kShiftAmount, &noop); | 104 kBlockLength, kShiftAmount, &noop); |
105 float in_buffer[kChannels][kChunkLength]; | 105 float in_buffer[kChannels][kChunkLength]; |
106 float* in_chunk[kChannels]; | 106 float* in_chunk[kChannels]; |
107 float out_buffer[kChannels][kChunkLength]; | 107 float out_buffer[kChannels][kChunkLength]; |
108 float* out_chunk[kChannels]; | 108 float* out_chunk[kChannels]; |
109 | 109 |
110 in_chunk[0] = in_buffer[0]; | 110 in_chunk[0] = in_buffer[0]; |
111 in_chunk[1] = in_buffer[1]; | 111 in_chunk[1] = in_buffer[1]; |
112 in_chunk[2] = in_buffer[2]; | 112 in_chunk[2] = in_buffer[2]; |
113 out_chunk[0] = out_buffer[0]; | 113 out_chunk[0] = out_buffer[0]; |
114 out_chunk[1] = out_buffer[1]; | 114 out_chunk[1] = out_buffer[1]; |
115 out_chunk[2] = out_buffer[2]; | 115 out_chunk[2] = out_buffer[2]; |
116 SetFloatArray(2.0f, kChannels, kChunkLength, in_chunk); | 116 SetFloatArray(2.0f, kChannels, kChunkLength, in_chunk); |
117 SetFloatArray(-1.0f, kChannels, kChunkLength, out_chunk); | 117 SetFloatArray(-1.0f, kChannels, kChunkLength, out_chunk); |
118 | 118 |
119 trans.ProcessChunk(in_chunk, out_chunk); | 119 trans.ProcessChunk(in_chunk, out_chunk); |
120 | 120 |
121 for (int i = 0; i < kChannels; ++i) { | 121 for (int i = 0; i < kChannels; ++i) { |
122 for (int j = 0; j < kChunkLength; ++j) { | 122 for (size_t j = 0; j < kChunkLength; ++j) { |
123 ASSERT_NEAR(out_chunk[i][j], 2.0f, 1e-5f); | 123 ASSERT_NEAR(out_chunk[i][j], 2.0f, 1e-5f); |
124 } | 124 } |
125 } | 125 } |
126 | 126 |
127 ASSERT_EQ(kChunkLength / kBlockLength, noop.block_num()); | 127 ASSERT_EQ(kChunkLength / kBlockLength, noop.block_num()); |
128 } | 128 } |
129 | 129 |
130 TEST(LappedTransformTest, IdentityProcessor) { | 130 TEST(LappedTransformTest, IdentityProcessor) { |
131 const int kChunkLength = 512; | 131 const size_t kChunkLength = 512; |
132 const int kBlockLength = 64; | 132 const size_t kBlockLength = 64; |
133 const int kShiftAmount = 32; | 133 const size_t kShiftAmount = 32; |
134 NoopCallback noop; | 134 NoopCallback noop; |
135 | 135 |
136 // Identity window for |overlap = block_size / 2|. | 136 // Identity window for |overlap = block_size / 2|. |
137 float window[kBlockLength]; | 137 float window[kBlockLength]; |
138 std::fill(window, &window[kBlockLength], std::sqrt(0.5f)); | 138 std::fill(window, &window[kBlockLength], std::sqrt(0.5f)); |
139 | 139 |
140 LappedTransform trans(1, 1, kChunkLength, window, kBlockLength, kShiftAmount, | 140 LappedTransform trans(1, 1, kChunkLength, window, kBlockLength, kShiftAmount, |
141 &noop); | 141 &noop); |
142 float in_buffer[kChunkLength]; | 142 float in_buffer[kChunkLength]; |
143 float* in_chunk = in_buffer; | 143 float* in_chunk = in_buffer; |
144 float out_buffer[kChunkLength]; | 144 float out_buffer[kChunkLength]; |
145 float* out_chunk = out_buffer; | 145 float* out_chunk = out_buffer; |
146 | 146 |
147 SetFloatArray(2.0f, 1, kChunkLength, &in_chunk); | 147 SetFloatArray(2.0f, 1, kChunkLength, &in_chunk); |
148 SetFloatArray(-1.0f, 1, kChunkLength, &out_chunk); | 148 SetFloatArray(-1.0f, 1, kChunkLength, &out_chunk); |
149 | 149 |
150 trans.ProcessChunk(&in_chunk, &out_chunk); | 150 trans.ProcessChunk(&in_chunk, &out_chunk); |
151 | 151 |
152 for (int i = 0; i < kChunkLength; ++i) { | 152 for (size_t i = 0; i < kChunkLength; ++i) { |
153 ASSERT_NEAR(out_chunk[i], | 153 ASSERT_NEAR(out_chunk[i], |
154 (i < kBlockLength - kShiftAmount) ? 0.0f : 2.0f, | 154 (i < kBlockLength - kShiftAmount) ? 0.0f : 2.0f, |
155 1e-5f); | 155 1e-5f); |
156 } | 156 } |
157 | 157 |
158 ASSERT_EQ(kChunkLength / kShiftAmount, noop.block_num()); | 158 ASSERT_EQ(kChunkLength / kShiftAmount, noop.block_num()); |
159 } | 159 } |
160 | 160 |
161 TEST(LappedTransformTest, Callbacks) { | 161 TEST(LappedTransformTest, Callbacks) { |
162 const int kChunkLength = 512; | 162 const size_t kChunkLength = 512; |
163 const int kBlockLength = 64; | 163 const size_t kBlockLength = 64; |
164 FftCheckerCallback call; | 164 FftCheckerCallback call; |
165 | 165 |
166 // Rectangular window. | 166 // Rectangular window. |
167 float window[kBlockLength]; | 167 float window[kBlockLength]; |
168 std::fill(window, &window[kBlockLength], 1.0f); | 168 std::fill(window, &window[kBlockLength], 1.0f); |
169 | 169 |
170 LappedTransform trans(1, 1, kChunkLength, window, kBlockLength, | 170 LappedTransform trans(1, 1, kChunkLength, window, kBlockLength, |
171 kBlockLength, &call); | 171 kBlockLength, &call); |
172 float in_buffer[kChunkLength]; | 172 float in_buffer[kChunkLength]; |
173 float* in_chunk = in_buffer; | 173 float* in_chunk = in_buffer; |
174 float out_buffer[kChunkLength]; | 174 float out_buffer[kChunkLength]; |
175 float* out_chunk = out_buffer; | 175 float* out_chunk = out_buffer; |
176 | 176 |
177 SetFloatArray(1.0f, 1, kChunkLength, &in_chunk); | 177 SetFloatArray(1.0f, 1, kChunkLength, &in_chunk); |
178 SetFloatArray(-1.0f, 1, kChunkLength, &out_chunk); | 178 SetFloatArray(-1.0f, 1, kChunkLength, &out_chunk); |
179 | 179 |
180 trans.ProcessChunk(&in_chunk, &out_chunk); | 180 trans.ProcessChunk(&in_chunk, &out_chunk); |
181 | 181 |
182 ASSERT_EQ(kChunkLength / kBlockLength, call.block_num()); | 182 ASSERT_EQ(kChunkLength / kBlockLength, call.block_num()); |
183 } | 183 } |
184 | 184 |
185 TEST(LappedTransformTest, chunk_length) { | 185 TEST(LappedTransformTest, chunk_length) { |
186 const int kBlockLength = 64; | 186 const size_t kBlockLength = 64; |
187 FftCheckerCallback call; | 187 FftCheckerCallback call; |
188 const float window[kBlockLength] = {}; | 188 const float window[kBlockLength] = {}; |
189 | 189 |
190 // Make sure that chunk_length returns the same value passed to the | 190 // Make sure that chunk_length returns the same value passed to the |
191 // LappedTransform constructor. | 191 // LappedTransform constructor. |
192 { | 192 { |
193 const size_t kExpectedChunkLength = 512; | 193 const size_t kExpectedChunkLength = 512; |
194 const LappedTransform trans(1, 1, kExpectedChunkLength, window, | 194 const LappedTransform trans(1, 1, kExpectedChunkLength, window, |
195 kBlockLength, kBlockLength, &call); | 195 kBlockLength, kBlockLength, &call); |
196 | 196 |
197 EXPECT_EQ(kExpectedChunkLength, trans.chunk_length()); | 197 EXPECT_EQ(kExpectedChunkLength, trans.chunk_length()); |
198 } | 198 } |
199 { | 199 { |
200 const size_t kExpectedChunkLength = 160; | 200 const size_t kExpectedChunkLength = 160; |
201 const LappedTransform trans(1, 1, kExpectedChunkLength, window, | 201 const LappedTransform trans(1, 1, kExpectedChunkLength, window, |
202 kBlockLength, kBlockLength, &call); | 202 kBlockLength, kBlockLength, &call); |
203 | 203 |
204 EXPECT_EQ(kExpectedChunkLength, trans.chunk_length()); | 204 EXPECT_EQ(kExpectedChunkLength, trans.chunk_length()); |
205 } | 205 } |
206 } | 206 } |
207 | 207 |
208 } // namespace webrtc | 208 } // namespace webrtc |
OLD | NEW |