Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(192)

Side by Side Diff: webrtc/modules/utility/source/audio_frame_operations_unittest.cc

Issue 2424173003: Move functionality out from AudioFrame and into AudioFrameOperations. (Closed)
Patch Set: Include order & DCHECKs. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/modules/utility/source/audio_frame_operations.cc ('k') | webrtc/voice_engine/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2012 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 "webrtc/base/checks.h"
12 #include "webrtc/modules/include/module_common_types.h"
13 #include "webrtc/modules/utility/include/audio_frame_operations.h"
14 #include "webrtc/test/gtest.h"
15
16 namespace webrtc {
17 namespace {
18
19 class AudioFrameOperationsTest : public ::testing::Test {
20 protected:
21 AudioFrameOperationsTest() {
22 // Set typical values.
23 frame_.samples_per_channel_ = 320;
24 frame_.num_channels_ = 2;
25 }
26
27 AudioFrame frame_;
28 };
29
30 void SetFrameData(AudioFrame* frame, int16_t left, int16_t right) {
31 for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) {
32 frame->data_[i] = left;
33 frame->data_[i + 1] = right;
34 }
35 }
36
37 void SetFrameData(AudioFrame* frame, int16_t data) {
38 for (size_t i = 0; i < frame->samples_per_channel_; i++) {
39 frame->data_[i] = data;
40 }
41 }
42
43 void VerifyFramesAreEqual(const AudioFrame& frame1, const AudioFrame& frame2) {
44 EXPECT_EQ(frame1.num_channels_, frame2.num_channels_);
45 EXPECT_EQ(frame1.samples_per_channel_,
46 frame2.samples_per_channel_);
47 for (size_t i = 0; i < frame1.samples_per_channel_ * frame1.num_channels_;
48 i++) {
49 EXPECT_EQ(frame1.data_[i], frame2.data_[i]);
50 }
51 }
52
53 void InitFrame(AudioFrame* frame, size_t channels, size_t samples_per_channel,
54 int16_t left_data, int16_t right_data) {
55 RTC_DCHECK(frame);
56 RTC_DCHECK_GE(2, channels);
57 RTC_DCHECK_GE(AudioFrame::kMaxDataSizeSamples,
58 samples_per_channel * channels);
59 frame->samples_per_channel_ = samples_per_channel;
60 frame->num_channels_ = channels;
61 if (channels == 2) {
62 SetFrameData(frame, left_data, right_data);
63 } else if (channels == 1) {
64 SetFrameData(frame, left_data);
65 }
66 }
67
68 int16_t GetChannelData(const AudioFrame& frame, size_t channel, size_t index) {
69 RTC_DCHECK_LT(channel, frame.num_channels_);
70 RTC_DCHECK_LT(index, frame.samples_per_channel_);
71 return frame.data_[index * frame.num_channels_ + channel];
72 }
73
74 void VerifyFrameDataBounds(const AudioFrame& frame, size_t channel, int16_t max,
75 int16_t min) {
76 for (size_t i = 0; i < frame.samples_per_channel_; ++i) {
77 int16_t s = GetChannelData(frame, channel, i);
78 EXPECT_LE(min, s);
79 EXPECT_GE(max, s);
80 }
81 }
82
83 TEST_F(AudioFrameOperationsTest, MonoToStereoFailsWithBadParameters) {
84 EXPECT_EQ(-1, AudioFrameOperations::MonoToStereo(&frame_));
85
86 frame_.samples_per_channel_ = AudioFrame::kMaxDataSizeSamples;
87 frame_.num_channels_ = 1;
88 EXPECT_EQ(-1, AudioFrameOperations::MonoToStereo(&frame_));
89 }
90
91 TEST_F(AudioFrameOperationsTest, MonoToStereoSucceeds) {
92 frame_.num_channels_ = 1;
93 SetFrameData(&frame_, 1);
94 AudioFrame temp_frame;
95 temp_frame.CopyFrom(frame_);
96 EXPECT_EQ(0, AudioFrameOperations::MonoToStereo(&frame_));
97
98 AudioFrame stereo_frame;
99 stereo_frame.samples_per_channel_ = 320;
100 stereo_frame.num_channels_ = 2;
101 SetFrameData(&stereo_frame, 1, 1);
102 VerifyFramesAreEqual(stereo_frame, frame_);
103
104 SetFrameData(&frame_, 0);
105 AudioFrameOperations::MonoToStereo(temp_frame.data_,
106 frame_.samples_per_channel_,
107 frame_.data_);
108 frame_.num_channels_ = 2; // Need to set manually.
109 VerifyFramesAreEqual(stereo_frame, frame_);
110 }
111
112 TEST_F(AudioFrameOperationsTest, StereoToMonoFailsWithBadParameters) {
113 frame_.num_channels_ = 1;
114 EXPECT_EQ(-1, AudioFrameOperations::StereoToMono(&frame_));
115 }
116
117 TEST_F(AudioFrameOperationsTest, StereoToMonoSucceeds) {
118 SetFrameData(&frame_, 4, 2);
119 AudioFrame temp_frame;
120 temp_frame.CopyFrom(frame_);
121 EXPECT_EQ(0, AudioFrameOperations::StereoToMono(&frame_));
122
123 AudioFrame mono_frame;
124 mono_frame.samples_per_channel_ = 320;
125 mono_frame.num_channels_ = 1;
126 SetFrameData(&mono_frame, 3);
127 VerifyFramesAreEqual(mono_frame, frame_);
128
129 SetFrameData(&frame_, 0);
130 AudioFrameOperations::StereoToMono(temp_frame.data_,
131 frame_.samples_per_channel_,
132 frame_.data_);
133 frame_.num_channels_ = 1; // Need to set manually.
134 VerifyFramesAreEqual(mono_frame, frame_);
135 }
136
137 TEST_F(AudioFrameOperationsTest, StereoToMonoDoesNotWrapAround) {
138 SetFrameData(&frame_, -32768, -32768);
139 EXPECT_EQ(0, AudioFrameOperations::StereoToMono(&frame_));
140
141 AudioFrame mono_frame;
142 mono_frame.samples_per_channel_ = 320;
143 mono_frame.num_channels_ = 1;
144 SetFrameData(&mono_frame, -32768);
145 VerifyFramesAreEqual(mono_frame, frame_);
146 }
147
148 TEST_F(AudioFrameOperationsTest, SwapStereoChannelsSucceedsOnStereo) {
149 SetFrameData(&frame_, 0, 1);
150
151 AudioFrame swapped_frame;
152 swapped_frame.samples_per_channel_ = 320;
153 swapped_frame.num_channels_ = 2;
154 SetFrameData(&swapped_frame, 1, 0);
155
156 AudioFrameOperations::SwapStereoChannels(&frame_);
157 VerifyFramesAreEqual(swapped_frame, frame_);
158 }
159
160 TEST_F(AudioFrameOperationsTest, SwapStereoChannelsFailsOnMono) {
161 frame_.num_channels_ = 1;
162 // Set data to "stereo", despite it being a mono frame.
163 SetFrameData(&frame_, 0, 1);
164
165 AudioFrame orig_frame;
166 orig_frame.CopyFrom(frame_);
167 AudioFrameOperations::SwapStereoChannels(&frame_);
168 // Verify that no swap occurred.
169 VerifyFramesAreEqual(orig_frame, frame_);
170 }
171
172 TEST_F(AudioFrameOperationsTest, MuteDisabled) {
173 SetFrameData(&frame_, 1000, -1000);
174 AudioFrameOperations::Mute(&frame_, false, false);
175
176 AudioFrame muted_frame;
177 muted_frame.samples_per_channel_ = 320;
178 muted_frame.num_channels_ = 2;
179 SetFrameData(&muted_frame, 1000, -1000);
180 VerifyFramesAreEqual(muted_frame, frame_);
181 }
182
183 TEST_F(AudioFrameOperationsTest, MuteEnabled) {
184 SetFrameData(&frame_, 1000, -1000);
185 AudioFrameOperations::Mute(&frame_, true, true);
186
187 AudioFrame muted_frame;
188 muted_frame.samples_per_channel_ = 320;
189 muted_frame.num_channels_ = 2;
190 SetFrameData(&muted_frame, 0, 0);
191 VerifyFramesAreEqual(muted_frame, frame_);
192 }
193
194 // Verify that *beginning* to mute works for short and long (>128) frames, mono
195 // and stereo. Beginning mute should yield a ramp down to zero.
196 TEST_F(AudioFrameOperationsTest, MuteBeginMonoLong) {
197 InitFrame(&frame_, 1, 228, 1000, -1000);
198 AudioFrameOperations::Mute(&frame_, false, true);
199 VerifyFrameDataBounds(frame_, 0, 1000, 0);
200 EXPECT_EQ(1000, GetChannelData(frame_, 0, 99));
201 EXPECT_EQ(992, GetChannelData(frame_, 0, 100));
202 EXPECT_EQ(7, GetChannelData(frame_, 0, 226));
203 EXPECT_EQ(0, GetChannelData(frame_, 0, 227));
204 }
205
206 TEST_F(AudioFrameOperationsTest, MuteBeginMonoShort) {
207 InitFrame(&frame_, 1, 93, 1000, -1000);
208 AudioFrameOperations::Mute(&frame_, false, true);
209 VerifyFrameDataBounds(frame_, 0, 1000, 0);
210 EXPECT_EQ(989, GetChannelData(frame_, 0, 0));
211 EXPECT_EQ(978, GetChannelData(frame_, 0, 1));
212 EXPECT_EQ(10, GetChannelData(frame_, 0, 91));
213 EXPECT_EQ(0, GetChannelData(frame_, 0, 92));
214 }
215
216 TEST_F(AudioFrameOperationsTest, MuteBeginStereoLong) {
217 InitFrame(&frame_, 2, 228, 1000, -1000);
218 AudioFrameOperations::Mute(&frame_, false, true);
219 VerifyFrameDataBounds(frame_, 0, 1000, 0);
220 VerifyFrameDataBounds(frame_, 1, 0, -1000);
221 EXPECT_EQ(1000, GetChannelData(frame_, 0, 99));
222 EXPECT_EQ(-1000, GetChannelData(frame_, 1, 99));
223 EXPECT_EQ(992, GetChannelData(frame_, 0, 100));
224 EXPECT_EQ(-992, GetChannelData(frame_, 1, 100));
225 EXPECT_EQ(7, GetChannelData(frame_, 0, 226));
226 EXPECT_EQ(-7, GetChannelData(frame_, 1, 226));
227 EXPECT_EQ(0, GetChannelData(frame_, 0, 227));
228 EXPECT_EQ(0, GetChannelData(frame_, 1, 227));
229 }
230
231 TEST_F(AudioFrameOperationsTest, MuteBeginStereoShort) {
232 InitFrame(&frame_, 2, 93, 1000, -1000);
233 AudioFrameOperations::Mute(&frame_, false, true);
234 VerifyFrameDataBounds(frame_, 0, 1000, 0);
235 VerifyFrameDataBounds(frame_, 1, 0, -1000);
236 EXPECT_EQ(989, GetChannelData(frame_, 0, 0));
237 EXPECT_EQ(-989, GetChannelData(frame_, 1, 0));
238 EXPECT_EQ(978, GetChannelData(frame_, 0, 1));
239 EXPECT_EQ(-978, GetChannelData(frame_, 1, 1));
240 EXPECT_EQ(10, GetChannelData(frame_, 0, 91));
241 EXPECT_EQ(-10, GetChannelData(frame_, 1, 91));
242 EXPECT_EQ(0, GetChannelData(frame_, 0, 92));
243 EXPECT_EQ(0, GetChannelData(frame_, 1, 92));
244 }
245
246 // Verify that *ending* to mute works for short and long (>128) frames, mono
247 // and stereo. Ending mute should yield a ramp up from zero.
248 TEST_F(AudioFrameOperationsTest, MuteEndMonoLong) {
249 InitFrame(&frame_, 1, 228, 1000, -1000);
250 AudioFrameOperations::Mute(&frame_, true, false);
251 VerifyFrameDataBounds(frame_, 0, 1000, 0);
252 EXPECT_EQ(7, GetChannelData(frame_, 0, 0));
253 EXPECT_EQ(15, GetChannelData(frame_, 0, 1));
254 EXPECT_EQ(1000, GetChannelData(frame_, 0, 127));
255 EXPECT_EQ(1000, GetChannelData(frame_, 0, 128));
256 }
257
258 TEST_F(AudioFrameOperationsTest, MuteEndMonoShort) {
259 InitFrame(&frame_, 1, 93, 1000, -1000);
260 AudioFrameOperations::Mute(&frame_, true, false);
261 VerifyFrameDataBounds(frame_, 0, 1000, 0);
262 EXPECT_EQ(10, GetChannelData(frame_, 0, 0));
263 EXPECT_EQ(21, GetChannelData(frame_, 0, 1));
264 EXPECT_EQ(989, GetChannelData(frame_, 0, 91));
265 EXPECT_EQ(999, GetChannelData(frame_, 0, 92));
266 }
267
268 TEST_F(AudioFrameOperationsTest, MuteEndStereoLong) {
269 InitFrame(&frame_, 2, 228, 1000, -1000);
270 AudioFrameOperations::Mute(&frame_, true, false);
271 VerifyFrameDataBounds(frame_, 0, 1000, 0);
272 VerifyFrameDataBounds(frame_, 1, 0, -1000);
273 EXPECT_EQ(7, GetChannelData(frame_, 0, 0));
274 EXPECT_EQ(-7, GetChannelData(frame_, 1, 0));
275 EXPECT_EQ(15, GetChannelData(frame_, 0, 1));
276 EXPECT_EQ(-15, GetChannelData(frame_, 1, 1));
277 EXPECT_EQ(1000, GetChannelData(frame_, 0, 127));
278 EXPECT_EQ(-1000, GetChannelData(frame_, 1, 127));
279 EXPECT_EQ(1000, GetChannelData(frame_, 0, 128));
280 EXPECT_EQ(-1000, GetChannelData(frame_, 1, 128));
281 }
282
283 TEST_F(AudioFrameOperationsTest, MuteEndStereoShort) {
284 InitFrame(&frame_, 2, 93, 1000, -1000);
285 AudioFrameOperations::Mute(&frame_, true, false);
286 VerifyFrameDataBounds(frame_, 0, 1000, 0);
287 VerifyFrameDataBounds(frame_, 1, 0, -1000);
288 EXPECT_EQ(10, GetChannelData(frame_, 0, 0));
289 EXPECT_EQ(-10, GetChannelData(frame_, 1, 0));
290 EXPECT_EQ(21, GetChannelData(frame_, 0, 1));
291 EXPECT_EQ(-21, GetChannelData(frame_, 1, 1));
292 EXPECT_EQ(989, GetChannelData(frame_, 0, 91));
293 EXPECT_EQ(-989, GetChannelData(frame_, 1, 91));
294 EXPECT_EQ(999, GetChannelData(frame_, 0, 92));
295 EXPECT_EQ(-999, GetChannelData(frame_, 1, 92));
296 }
297
298 // TODO(andrew): should not allow negative scales.
299 TEST_F(AudioFrameOperationsTest, DISABLED_ScaleFailsWithBadParameters) {
300 frame_.num_channels_ = 1;
301 EXPECT_EQ(-1, AudioFrameOperations::Scale(1.0, 1.0, frame_));
302
303 frame_.num_channels_ = 3;
304 EXPECT_EQ(-1, AudioFrameOperations::Scale(1.0, 1.0, frame_));
305
306 frame_.num_channels_ = 2;
307 EXPECT_EQ(-1, AudioFrameOperations::Scale(-1.0, 1.0, frame_));
308 EXPECT_EQ(-1, AudioFrameOperations::Scale(1.0, -1.0, frame_));
309 }
310
311 // TODO(andrew): fix the wraparound bug. We should always saturate.
312 TEST_F(AudioFrameOperationsTest, DISABLED_ScaleDoesNotWrapAround) {
313 SetFrameData(&frame_, 4000, -4000);
314 EXPECT_EQ(0, AudioFrameOperations::Scale(10.0, 10.0, frame_));
315
316 AudioFrame clipped_frame;
317 clipped_frame.samples_per_channel_ = 320;
318 clipped_frame.num_channels_ = 2;
319 SetFrameData(&clipped_frame, 32767, -32768);
320 VerifyFramesAreEqual(clipped_frame, frame_);
321 }
322
323 TEST_F(AudioFrameOperationsTest, ScaleSucceeds) {
324 SetFrameData(&frame_, 1, -1);
325 EXPECT_EQ(0, AudioFrameOperations::Scale(2.0, 3.0, frame_));
326
327 AudioFrame scaled_frame;
328 scaled_frame.samples_per_channel_ = 320;
329 scaled_frame.num_channels_ = 2;
330 SetFrameData(&scaled_frame, 2, -3);
331 VerifyFramesAreEqual(scaled_frame, frame_);
332 }
333
334 // TODO(andrew): should fail with a negative scale.
335 TEST_F(AudioFrameOperationsTest, DISABLED_ScaleWithSatFailsWithBadParameters) {
336 EXPECT_EQ(-1, AudioFrameOperations::ScaleWithSat(-1.0, frame_));
337 }
338
339 TEST_F(AudioFrameOperationsTest, ScaleWithSatDoesNotWrapAround) {
340 frame_.num_channels_ = 1;
341 SetFrameData(&frame_, 4000);
342 EXPECT_EQ(0, AudioFrameOperations::ScaleWithSat(10.0, frame_));
343
344 AudioFrame clipped_frame;
345 clipped_frame.samples_per_channel_ = 320;
346 clipped_frame.num_channels_ = 1;
347 SetFrameData(&clipped_frame, 32767);
348 VerifyFramesAreEqual(clipped_frame, frame_);
349
350 SetFrameData(&frame_, -4000);
351 EXPECT_EQ(0, AudioFrameOperations::ScaleWithSat(10.0, frame_));
352 SetFrameData(&clipped_frame, -32768);
353 VerifyFramesAreEqual(clipped_frame, frame_);
354 }
355
356 TEST_F(AudioFrameOperationsTest, ScaleWithSatSucceeds) {
357 frame_.num_channels_ = 1;
358 SetFrameData(&frame_, 1);
359 EXPECT_EQ(0, AudioFrameOperations::ScaleWithSat(2.0, frame_));
360
361 AudioFrame scaled_frame;
362 scaled_frame.samples_per_channel_ = 320;
363 scaled_frame.num_channels_ = 1;
364 SetFrameData(&scaled_frame, 2);
365 VerifyFramesAreEqual(scaled_frame, frame_);
366 }
367
368 } // namespace
369 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/modules/utility/source/audio_frame_operations.cc ('k') | webrtc/voice_engine/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698