OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 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 "testing/gmock/include/gmock/gmock.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
12 #include "webrtc/common_audio/include/audio_util.h" | 13 #include "webrtc/common_audio/include/audio_util.h" |
13 #include "webrtc/typedefs.h" | 14 #include "webrtc/typedefs.h" |
14 | 15 |
15 namespace webrtc { | 16 namespace webrtc { |
| 17 namespace { |
| 18 |
| 19 using ::testing::ElementsAreArray; |
16 | 20 |
17 void ExpectArraysEq(const int16_t* ref, const int16_t* test, int length) { | 21 void ExpectArraysEq(const int16_t* ref, const int16_t* test, int length) { |
18 for (int i = 0; i < length; ++i) { | 22 for (int i = 0; i < length; ++i) { |
19 EXPECT_EQ(ref[i], test[i]); | 23 EXPECT_EQ(ref[i], test[i]); |
20 } | 24 } |
21 } | 25 } |
22 | 26 |
23 void ExpectArraysEq(const float* ref, const float* test, int length) { | 27 void ExpectArraysEq(const float* ref, const float* test, int length) { |
24 for (int i = 0; i < length; ++i) { | 28 for (int i = 0; i < length; ++i) { |
25 EXPECT_FLOAT_EQ(ref[i], test[i]); | 29 EXPECT_FLOAT_EQ(ref[i], test[i]); |
26 } | 30 } |
27 } | 31 } |
28 | 32 |
29 TEST(AudioUtilTest, FloatToS16) { | 33 TEST(AudioUtilTest, FloatToS16) { |
30 const int kSize = 9; | 34 const int kSize = 9; |
31 const float kInput[kSize] = { | 35 const float kInput[kSize] = {0.f, |
32 0.f, 0.4f / 32767.f, 0.6f / 32767.f, -0.4f / 32768.f, -0.6f / 32768.f, | 36 0.4f / 32767.f, |
33 1.f, -1.f, 1.1f, -1.1f}; | 37 0.6f / 32767.f, |
34 const int16_t kReference[kSize] = { | 38 -0.4f / 32768.f, |
35 0, 0, 1, 0, -1, 32767, -32768, 32767, -32768}; | 39 -0.6f / 32768.f, |
| 40 1.f, |
| 41 -1.f, |
| 42 1.1f, |
| 43 -1.1f}; |
| 44 const int16_t kReference[kSize] = {0, 0, 1, 0, -1, |
| 45 32767, -32768, 32767, -32768}; |
36 int16_t output[kSize]; | 46 int16_t output[kSize]; |
37 FloatToS16(kInput, kSize, output); | 47 FloatToS16(kInput, kSize, output); |
38 ExpectArraysEq(kReference, output, kSize); | 48 ExpectArraysEq(kReference, output, kSize); |
39 } | 49 } |
40 | 50 |
41 TEST(AudioUtilTest, S16ToFloat) { | 51 TEST(AudioUtilTest, S16ToFloat) { |
42 const int kSize = 7; | 52 const int kSize = 7; |
43 const int16_t kInput[kSize] = {0, 1, -1, 16384, -16384, 32767, -32768}; | 53 const int16_t kInput[kSize] = {0, 1, -1, 16384, -16384, 32767, -32768}; |
44 const float kReference[kSize] = { | 54 const float kReference[kSize] = { |
45 0.f, 1.f / 32767.f, -1.f / 32768.f, 16384.f / 32767.f, -0.5f, 1.f, -1.f}; | 55 0.f, 1.f / 32767.f, -1.f / 32768.f, 16384.f / 32767.f, -0.5f, 1.f, -1.f}; |
46 float output[kSize]; | 56 float output[kSize]; |
47 S16ToFloat(kInput, kSize, output); | 57 S16ToFloat(kInput, kSize, output); |
48 ExpectArraysEq(kReference, output, kSize); | 58 ExpectArraysEq(kReference, output, kSize); |
49 } | 59 } |
50 | 60 |
51 TEST(AudioUtilTest, FloatS16ToS16) { | 61 TEST(AudioUtilTest, FloatS16ToS16) { |
52 const int kSize = 7; | 62 const int kSize = 7; |
53 const float kInput[kSize] = { | 63 const float kInput[kSize] = {0.f, 0.4f, 0.5f, -0.4f, |
54 0.f, 0.4f, 0.5f, -0.4f, -0.5f, 32768.f, -32769.f}; | 64 -0.5f, 32768.f, -32769.f}; |
55 const int16_t kReference[kSize] = {0, 0, 1, 0, -1, 32767, -32768}; | 65 const int16_t kReference[kSize] = {0, 0, 1, 0, -1, 32767, -32768}; |
56 int16_t output[kSize]; | 66 int16_t output[kSize]; |
57 FloatS16ToS16(kInput, kSize, output); | 67 FloatS16ToS16(kInput, kSize, output); |
58 ExpectArraysEq(kReference, output, kSize); | 68 ExpectArraysEq(kReference, output, kSize); |
59 } | 69 } |
60 | 70 |
61 TEST(AudioUtilTest, FloatToFloatS16) { | 71 TEST(AudioUtilTest, FloatToFloatS16) { |
62 const int kSize = 9; | 72 const int kSize = 9; |
63 const float kInput[kSize] = { | 73 const float kInput[kSize] = {0.f, |
64 0.f, 0.4f / 32767.f, 0.6f / 32767.f, -0.4f / 32768.f, -0.6f / 32768.f, | 74 0.4f / 32767.f, |
65 1.f, -1.f, 1.1f, -1.1f}; | 75 0.6f / 32767.f, |
66 const float kReference[kSize] = { | 76 -0.4f / 32768.f, |
67 0.f, 0.4f, 0.6f, -0.4f, -0.6f, 32767.f, -32768.f, 36043.7f, -36044.8f}; | 77 -0.6f / 32768.f, |
| 78 1.f, |
| 79 -1.f, |
| 80 1.1f, |
| 81 -1.1f}; |
| 82 const float kReference[kSize] = {0.f, 0.4f, 0.6f, -0.4f, -0.6f, |
| 83 32767.f, -32768.f, 36043.7f, -36044.8f}; |
68 float output[kSize]; | 84 float output[kSize]; |
69 FloatToFloatS16(kInput, kSize, output); | 85 FloatToFloatS16(kInput, kSize, output); |
70 ExpectArraysEq(kReference, output, kSize); | 86 ExpectArraysEq(kReference, output, kSize); |
71 } | 87 } |
72 | 88 |
73 TEST(AudioUtilTest, FloatS16ToFloat) { | 89 TEST(AudioUtilTest, FloatS16ToFloat) { |
74 const int kSize = 9; | 90 const int kSize = 9; |
75 const float kInput[kSize] = { | 91 const float kInput[kSize] = {0.f, 0.4f, 0.6f, -0.4f, -0.6f, |
76 0.f, 0.4f, 0.6f, -0.4f, -0.6f, 32767.f, -32768.f, 36043.7f, -36044.8f}; | 92 32767.f, -32768.f, 36043.7f, -36044.8f}; |
77 const float kReference[kSize] = { | 93 const float kReference[kSize] = {0.f, |
78 0.f, 0.4f / 32767.f, 0.6f / 32767.f, -0.4f / 32768.f, -0.6f / 32768.f, | 94 0.4f / 32767.f, |
79 1.f, -1.f, 1.1f, -1.1f}; | 95 0.6f / 32767.f, |
| 96 -0.4f / 32768.f, |
| 97 -0.6f / 32768.f, |
| 98 1.f, |
| 99 -1.f, |
| 100 1.1f, |
| 101 -1.1f}; |
80 float output[kSize]; | 102 float output[kSize]; |
81 FloatS16ToFloat(kInput, kSize, output); | 103 FloatS16ToFloat(kInput, kSize, output); |
82 ExpectArraysEq(kReference, output, kSize); | 104 ExpectArraysEq(kReference, output, kSize); |
83 } | 105 } |
84 | 106 |
85 TEST(AudioUtilTest, InterleavingStereo) { | 107 TEST(AudioUtilTest, InterleavingStereo) { |
86 const int16_t kInterleaved[] = {2, 3, 4, 9, 8, 27, 16, 81}; | 108 const int16_t kInterleaved[] = {2, 3, 4, 9, 8, 27, 16, 81}; |
87 const int kSamplesPerChannel = 4; | 109 const int kSamplesPerChannel = 4; |
88 const int kNumChannels = 2; | 110 const int kNumChannels = 2; |
89 const int kLength = kSamplesPerChannel * kNumChannels; | 111 const int kLength = kSamplesPerChannel * kNumChannels; |
(...skipping 17 matching lines...) Expand all Loading... |
107 int16_t mono[kSamplesPerChannel]; | 129 int16_t mono[kSamplesPerChannel]; |
108 int16_t* deinterleaved[] = {mono}; | 130 int16_t* deinterleaved[] = {mono}; |
109 Deinterleave(kInterleaved, kSamplesPerChannel, kNumChannels, deinterleaved); | 131 Deinterleave(kInterleaved, kSamplesPerChannel, kNumChannels, deinterleaved); |
110 ExpectArraysEq(kInterleaved, mono, kSamplesPerChannel); | 132 ExpectArraysEq(kInterleaved, mono, kSamplesPerChannel); |
111 | 133 |
112 int16_t interleaved[kSamplesPerChannel]; | 134 int16_t interleaved[kSamplesPerChannel]; |
113 Interleave(deinterleaved, kSamplesPerChannel, kNumChannels, interleaved); | 135 Interleave(deinterleaved, kSamplesPerChannel, kNumChannels, interleaved); |
114 ExpectArraysEq(mono, interleaved, kSamplesPerChannel); | 136 ExpectArraysEq(mono, interleaved, kSamplesPerChannel); |
115 } | 137 } |
116 | 138 |
| 139 TEST(AudioUtilTest, DownmixInterleavedToMono) { |
| 140 { |
| 141 const int kNumFrames = 4; |
| 142 const int kNumChannels = 1; |
| 143 const int16_t interleaved[kNumChannels * kNumFrames] = {1, 2, -1, -3}; |
| 144 int16_t deinterleaved[kNumFrames]; |
| 145 |
| 146 DownmixInterleavedToMono(interleaved, kNumFrames, kNumChannels, |
| 147 deinterleaved); |
| 148 |
| 149 EXPECT_THAT(deinterleaved, ElementsAreArray(interleaved)); |
| 150 } |
| 151 { |
| 152 const int kNumFrames = 2; |
| 153 const int kNumChannels = 2; |
| 154 const int16_t interleaved[kNumChannels * kNumFrames] = {10, 20, -10, -30}; |
| 155 int16_t deinterleaved[kNumFrames]; |
| 156 |
| 157 DownmixInterleavedToMono(interleaved, kNumFrames, kNumChannels, |
| 158 deinterleaved); |
| 159 const int16_t expected[kNumFrames] = {15, -20}; |
| 160 |
| 161 EXPECT_THAT(deinterleaved, ElementsAreArray(expected)); |
| 162 } |
| 163 { |
| 164 const int kNumFrames = 3; |
| 165 const int kNumChannels = 3; |
| 166 const int16_t interleaved[kNumChannels * kNumFrames] = { |
| 167 30000, 30000, 24001, -5, -10, -20, -30000, -30999, -30000}; |
| 168 int16_t deinterleaved[kNumFrames]; |
| 169 |
| 170 DownmixInterleavedToMono(interleaved, kNumFrames, kNumChannels, |
| 171 deinterleaved); |
| 172 const int16_t expected[kNumFrames] = {28000, -11, -30333}; |
| 173 |
| 174 EXPECT_THAT(deinterleaved, ElementsAreArray(expected)); |
| 175 } |
| 176 } |
| 177 |
| 178 TEST(AudioUtilTest, DownmixToMonoTest) { |
| 179 { |
| 180 const int kNumFrames = 4; |
| 181 const int kNumChannels = 1; |
| 182 const float input_data[kNumChannels][kNumFrames] = {{1.f, 2.f, -1.f, -3.f}}; |
| 183 const float* input[kNumChannels]; |
| 184 for (int i = 0; i < kNumChannels; ++i) { |
| 185 input[i] = input_data[i]; |
| 186 } |
| 187 |
| 188 float downmixed[kNumFrames]; |
| 189 |
| 190 DownmixToMono<float, float>(input, kNumFrames, kNumChannels, downmixed); |
| 191 |
| 192 EXPECT_THAT(downmixed, ElementsAreArray(input_data[0])); |
| 193 } |
| 194 { |
| 195 const int kNumFrames = 3; |
| 196 const int kNumChannels = 2; |
| 197 const float input_data[kNumChannels][kNumFrames] = {{1.f, 2.f, -1.f}, |
| 198 {3.f, 0.f, 1.f}}; |
| 199 const float* input[kNumChannels]; |
| 200 for (int i = 0; i < kNumChannels; ++i) { |
| 201 input[i] = input_data[i]; |
| 202 } |
| 203 |
| 204 float downmixed[kNumFrames]; |
| 205 const float expected[kNumFrames] = {2.f, 1.f, 0.f}; |
| 206 |
| 207 DownmixToMono<float, float>(input, kNumFrames, kNumChannels, downmixed); |
| 208 |
| 209 EXPECT_THAT(downmixed, ElementsAreArray(expected)); |
| 210 } |
| 211 { |
| 212 const int kNumFrames = 3; |
| 213 const int kNumChannels = 3; |
| 214 const int16_t input_data[kNumChannels][kNumFrames] = { |
| 215 {30000, -5, -30000}, {30000, -10, -30999}, {24001, -20, -30000}}; |
| 216 const int16_t* input[kNumChannels]; |
| 217 for (int i = 0; i < kNumChannels; ++i) { |
| 218 input[i] = input_data[i]; |
| 219 } |
| 220 |
| 221 int16_t downmixed[kNumFrames]; |
| 222 const int16_t expected[kNumFrames] = {28000, -11, -30333}; |
| 223 |
| 224 DownmixToMono<int16_t, int32_t>(input, kNumFrames, kNumChannels, downmixed); |
| 225 |
| 226 EXPECT_THAT(downmixed, ElementsAreArray(expected)); |
| 227 } |
| 228 } |
| 229 |
| 230 } // namespace |
117 } // namespace webrtc | 231 } // namespace webrtc |
OLD | NEW |