OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 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/audio/utility/audio_frame_operations.h" | 11 #include "webrtc/audio/utility/audio_frame_operations.h" |
12 #include "webrtc/base/checks.h" | 12 #include "webrtc/base/checks.h" |
13 #include "webrtc/modules/include/module_common_types.h" | 13 #include "webrtc/modules/include/module_common_types.h" |
14 #include "webrtc/test/gtest.h" | 14 #include "webrtc/test/gtest.h" |
15 | 15 |
16 namespace webrtc { | 16 namespace webrtc { |
17 namespace { | 17 namespace { |
18 | 18 |
19 class AudioFrameOperationsTest : public ::testing::Test { | 19 class AudioFrameOperationsTest : public ::testing::Test { |
20 protected: | 20 protected: |
21 AudioFrameOperationsTest() { | 21 AudioFrameOperationsTest() { |
22 // Set typical values. | 22 // Set typical values. |
23 frame_.samples_per_channel_ = 320; | 23 frame_.samples_per_channel_ = 320; |
24 frame_.num_channels_ = 2; | 24 frame_.num_channels_ = 2; |
25 } | 25 } |
26 | 26 |
27 AudioFrame frame_; | 27 AudioFrame frame_; |
28 }; | 28 }; |
29 | 29 |
30 void SetFrameData(AudioFrame* frame, int16_t ch1, int16_t ch2, | |
hlundin-webrtc
2017/02/24 09:46:36
Please, run "git cl format" on your patch.
jens.nielsen
2017/02/24 14:55:49
Done.
| |
31 int16_t ch3, int16_t ch4) { | |
aleloi
2017/02/24 10:39:32
Please change parameter ordering: input params fir
jens.nielsen
2017/02/24 14:55:49
Done.
| |
32 for (size_t i = 0; i < frame->samples_per_channel_ * 4; i += 4) { | |
33 frame->data_[i] = ch1; | |
34 frame->data_[i + 1] = ch2; | |
35 frame->data_[i + 2] = ch3; | |
36 frame->data_[i + 3] = ch4; | |
37 } | |
38 } | |
39 | |
30 void SetFrameData(AudioFrame* frame, int16_t left, int16_t right) { | 40 void SetFrameData(AudioFrame* frame, int16_t left, int16_t right) { |
31 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) { | 41 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) { |
32 frame->data_[i] = left; | 42 frame->data_[i] = left; |
33 frame->data_[i + 1] = right; | 43 frame->data_[i + 1] = right; |
34 } | 44 } |
35 } | 45 } |
36 | 46 |
37 void SetFrameData(AudioFrame* frame, int16_t data) { | 47 void SetFrameData(AudioFrame* frame, int16_t data) { |
38 for (size_t i = 0; i < frame->samples_per_channel_; i++) { | 48 for (size_t i = 0; i < frame->samples_per_channel_; i++) { |
39 frame->data_[i] = data; | 49 frame->data_[i] = data; |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
138 SetFrameData(&frame_, -32768, -32768); | 148 SetFrameData(&frame_, -32768, -32768); |
139 EXPECT_EQ(0, AudioFrameOperations::StereoToMono(&frame_)); | 149 EXPECT_EQ(0, AudioFrameOperations::StereoToMono(&frame_)); |
140 | 150 |
141 AudioFrame mono_frame; | 151 AudioFrame mono_frame; |
142 mono_frame.samples_per_channel_ = 320; | 152 mono_frame.samples_per_channel_ = 320; |
143 mono_frame.num_channels_ = 1; | 153 mono_frame.num_channels_ = 1; |
144 SetFrameData(&mono_frame, -32768); | 154 SetFrameData(&mono_frame, -32768); |
145 VerifyFramesAreEqual(mono_frame, frame_); | 155 VerifyFramesAreEqual(mono_frame, frame_); |
146 } | 156 } |
147 | 157 |
158 TEST_F(AudioFrameOperationsTest, QuadToMonoFailsWithBadParameters) { | |
159 frame_.num_channels_ = 1; | |
160 EXPECT_EQ(-1, AudioFrameOperations::QuadToMono(&frame_)); | |
161 frame_.num_channels_ = 2; | |
162 EXPECT_EQ(-1, AudioFrameOperations::QuadToMono(&frame_)); | |
163 } | |
164 | |
165 TEST_F(AudioFrameOperationsTest, QuadToMonoSucceeds) { | |
166 frame_.num_channels_ = 4; | |
167 SetFrameData(&frame_, 4, 2, 6, 8); | |
168 AudioFrame temp_frame; | |
169 temp_frame.CopyFrom(frame_); | |
170 EXPECT_EQ(0, AudioFrameOperations::QuadToMono(&frame_)); | |
171 | |
172 AudioFrame mono_frame; | |
173 mono_frame.samples_per_channel_ = 320; | |
174 mono_frame.num_channels_ = 1; | |
175 SetFrameData(&mono_frame, 5); | |
176 VerifyFramesAreEqual(mono_frame, frame_); | |
177 | |
178 SetFrameData(&frame_, 0); | |
hlundin-webrtc
2017/02/24 09:46:36
Let this be a separate TEST_F. This will also avoi
jens.nielsen
2017/02/24 11:11:19
Will do. Should I also change the StereoToMono and
jens.nielsen
2017/02/24 14:55:49
Done.
hlundin-webrtc
2017/02/27 07:52:02
That is up to you. I think you are living by the B
jens.nielsen
2017/02/27 13:28:15
Alright I was reluctant to change too much in one
| |
179 AudioFrameOperations::QuadToMono(temp_frame.data_, | |
180 frame_.samples_per_channel_, | |
181 frame_.data_); | |
182 frame_.num_channels_ = 1; // Need to set manually. | |
183 VerifyFramesAreEqual(mono_frame, frame_); | |
184 } | |
185 | |
186 TEST_F(AudioFrameOperationsTest, QuadToMonoDoesNotWrapAround) { | |
187 frame_.num_channels_ = 4; | |
188 SetFrameData(&frame_, -32768, -32768, -32768, -32768); | |
189 EXPECT_EQ(0, AudioFrameOperations::QuadToMono(&frame_)); | |
190 | |
191 AudioFrame mono_frame; | |
192 mono_frame.samples_per_channel_ = 320; | |
193 mono_frame.num_channels_ = 1; | |
194 SetFrameData(&mono_frame, -32768); | |
195 VerifyFramesAreEqual(mono_frame, frame_); | |
196 } | |
197 | |
198 TEST_F(AudioFrameOperationsTest, QuadToStereoFailsWithBadParameters) { | |
199 frame_.num_channels_ = 1; | |
200 EXPECT_EQ(-1, AudioFrameOperations::QuadToStereo(&frame_)); | |
201 frame_.num_channels_ = 2; | |
202 EXPECT_EQ(-1, AudioFrameOperations::QuadToStereo(&frame_)); | |
203 } | |
204 | |
205 TEST_F(AudioFrameOperationsTest, QuadToStereoSucceeds) { | |
206 frame_.num_channels_ = 4; | |
207 SetFrameData(&frame_, 4, 2, 6, 8); | |
208 AudioFrame temp_frame; | |
209 temp_frame.CopyFrom(frame_); | |
210 EXPECT_EQ(0, AudioFrameOperations::QuadToStereo(&frame_)); | |
211 | |
212 AudioFrame stereo_frame; | |
213 stereo_frame.samples_per_channel_ = 320; | |
214 stereo_frame.num_channels_ = 2; | |
215 SetFrameData(&stereo_frame, 3, 7); | |
216 VerifyFramesAreEqual(stereo_frame, frame_); | |
217 | |
218 SetFrameData(&frame_, 0); | |
219 AudioFrameOperations::QuadToStereo(temp_frame.data_, | |
hlundin-webrtc
2017/02/24 09:46:36
Let this be a separate TEST_F.
jens.nielsen
2017/02/24 14:55:49
Done.
| |
220 frame_.samples_per_channel_, | |
221 frame_.data_); | |
222 frame_.num_channels_ = 2; // Need to set manually. | |
223 VerifyFramesAreEqual(stereo_frame, frame_); | |
224 } | |
225 | |
226 TEST_F(AudioFrameOperationsTest, QuadToStereoDoesNotWrapAround) { | |
227 frame_.num_channels_ = 4; | |
228 SetFrameData(&frame_, -32768, -32768, -32768, -32768); | |
229 EXPECT_EQ(0, AudioFrameOperations::QuadToStereo(&frame_)); | |
230 | |
231 AudioFrame stereo_frame; | |
232 stereo_frame.samples_per_channel_ = 320; | |
233 stereo_frame.num_channels_ = 2; | |
234 SetFrameData(&stereo_frame, -32768, -32768); | |
235 VerifyFramesAreEqual(stereo_frame, frame_); | |
236 } | |
237 | |
148 TEST_F(AudioFrameOperationsTest, SwapStereoChannelsSucceedsOnStereo) { | 238 TEST_F(AudioFrameOperationsTest, SwapStereoChannelsSucceedsOnStereo) { |
149 SetFrameData(&frame_, 0, 1); | 239 SetFrameData(&frame_, 0, 1); |
150 | 240 |
151 AudioFrame swapped_frame; | 241 AudioFrame swapped_frame; |
152 swapped_frame.samples_per_channel_ = 320; | 242 swapped_frame.samples_per_channel_ = 320; |
153 swapped_frame.num_channels_ = 2; | 243 swapped_frame.num_channels_ = 2; |
154 SetFrameData(&swapped_frame, 1, 0); | 244 SetFrameData(&swapped_frame, 1, 0); |
155 | 245 |
156 AudioFrameOperations::SwapStereoChannels(&frame_); | 246 AudioFrameOperations::SwapStereoChannels(&frame_); |
157 VerifyFramesAreEqual(swapped_frame, frame_); | 247 VerifyFramesAreEqual(swapped_frame, frame_); |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
380 frame_to_add_to.samples_per_channel_ = frame_.samples_per_channel_; | 470 frame_to_add_to.samples_per_channel_ = frame_.samples_per_channel_; |
381 frame_to_add_to.num_channels_ = frame_.num_channels_; | 471 frame_to_add_to.num_channels_ = frame_.num_channels_; |
382 SetFrameData(&frame_to_add_to, 1000); | 472 SetFrameData(&frame_to_add_to, 1000); |
383 | 473 |
384 AudioFrameOperations::Add(frame_, &frame_to_add_to); | 474 AudioFrameOperations::Add(frame_, &frame_to_add_to); |
385 SetFrameData(&frame_, frame_.data_[0] + 1000); | 475 SetFrameData(&frame_, frame_.data_[0] + 1000); |
386 VerifyFramesAreEqual(frame_, frame_to_add_to); | 476 VerifyFramesAreEqual(frame_, frame_to_add_to); |
387 } | 477 } |
388 } // namespace | 478 } // namespace |
389 } // namespace webrtc | 479 } // namespace webrtc |
OLD | NEW |