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 |
11 #include "webrtc/common_audio/lapped_transform.h" | 11 #include "webrtc/common_audio/lapped_transform.h" |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 #include <cmath> | 14 #include <cmath> |
15 #include <cstring> | 15 #include <cstring> |
16 | 16 |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 | 18 |
19 using std::complex; | 19 using std::complex; |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 class NoopCallback : public webrtc::LappedTransform::Callback { | 23 class NoopCallback : public webrtc::LappedTransform::Callback { |
24 public: | 24 public: |
25 NoopCallback() : block_num_(0) {} | 25 NoopCallback() : block_num_(0) {} |
26 | 26 |
27 virtual void ProcessAudioBlock(const complex<float>* const* in_block, | 27 virtual void ProcessAudioBlock(const complex<float>* const* in_block, |
28 int in_channels, | 28 size_t in_channels, |
29 size_t frames, | 29 size_t frames, |
30 int out_channels, | 30 size_t 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 (size_t 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 size_t block_num() { | 39 size_t block_num() { |
40 return block_num_; | 40 return block_num_; |
41 } | 41 } |
42 | 42 |
43 private: | 43 private: |
44 size_t 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 size_t in_channels, |
53 size_t frames, | 53 size_t frames, |
54 int out_channels, | 54 size_t 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); |
(...skipping 18 matching lines...) Expand all Loading... |
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 size_t kChannels = 3; |
94 const size_t kChunkLength = 512; | 94 const size_t kChunkLength = 512; |
95 const size_t kBlockLength = 64; | 95 const size_t kBlockLength = 64; |
96 const size_t 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 (size_t i = 0; i < kChannels; ++i) { |
122 for (size_t 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 size_t kChunkLength = 512; | 131 const size_t kChunkLength = 512; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 |