Index: webrtc/audio/utility/audio_frame_operations_unittest.cc |
diff --git a/webrtc/audio/utility/audio_frame_operations_unittest.cc b/webrtc/audio/utility/audio_frame_operations_unittest.cc |
index 36377bd02d8e0db830e7a8353521703908cfc5d1..af8ae246262808e93437b7d6752122bcacff4e2b 100644 |
--- a/webrtc/audio/utility/audio_frame_operations_unittest.cc |
+++ b/webrtc/audio/utility/audio_frame_operations_unittest.cc |
@@ -27,14 +27,27 @@ class AudioFrameOperationsTest : public ::testing::Test { |
AudioFrame frame_; |
}; |
-void SetFrameData(AudioFrame* frame, int16_t left, int16_t right) { |
+void SetFrameData(int16_t ch1, |
+ int16_t ch2, |
+ int16_t ch3, |
+ int16_t ch4, |
+ AudioFrame* frame) { |
+ for (size_t i = 0; i < frame->samples_per_channel_ * 4; i += 4) { |
+ frame->data_[i] = ch1; |
+ frame->data_[i + 1] = ch2; |
+ frame->data_[i + 2] = ch3; |
+ frame->data_[i + 3] = ch4; |
+ } |
+} |
+ |
+void SetFrameData(int16_t left, int16_t right, AudioFrame* frame) { |
for (size_t i = 0; i < frame->samples_per_channel_ * 2; i += 2) { |
frame->data_[i] = left; |
frame->data_[i + 1] = right; |
} |
} |
-void SetFrameData(AudioFrame* frame, int16_t data) { |
+void SetFrameData(int16_t data, AudioFrame* frame) { |
for (size_t i = 0; i < frame->samples_per_channel_; i++) { |
frame->data_[i] = data; |
} |
@@ -59,9 +72,9 @@ void InitFrame(AudioFrame* frame, size_t channels, size_t samples_per_channel, |
frame->samples_per_channel_ = samples_per_channel; |
frame->num_channels_ = channels; |
if (channels == 2) { |
- SetFrameData(frame, left_data, right_data); |
+ SetFrameData(left_data, right_data, frame); |
} else if (channels == 1) { |
- SetFrameData(frame, left_data); |
+ SetFrameData(left_data, frame); |
} |
} |
@@ -90,23 +103,33 @@ TEST_F(AudioFrameOperationsTest, MonoToStereoFailsWithBadParameters) { |
TEST_F(AudioFrameOperationsTest, MonoToStereoSucceeds) { |
frame_.num_channels_ = 1; |
- SetFrameData(&frame_, 1); |
- AudioFrame temp_frame; |
- temp_frame.CopyFrom(frame_); |
+ SetFrameData(1, &frame_); |
+ |
EXPECT_EQ(0, AudioFrameOperations::MonoToStereo(&frame_)); |
AudioFrame stereo_frame; |
stereo_frame.samples_per_channel_ = 320; |
stereo_frame.num_channels_ = 2; |
- SetFrameData(&stereo_frame, 1, 1); |
+ SetFrameData(1, 1, &stereo_frame); |
VerifyFramesAreEqual(stereo_frame, frame_); |
+} |
- SetFrameData(&frame_, 0); |
- AudioFrameOperations::MonoToStereo(temp_frame.data_, |
- frame_.samples_per_channel_, |
- frame_.data_); |
- frame_.num_channels_ = 2; // Need to set manually. |
- VerifyFramesAreEqual(stereo_frame, frame_); |
+TEST_F(AudioFrameOperationsTest, MonoToStereoBufferSucceeds) { |
+ AudioFrame target_frame; |
+ frame_.num_channels_ = 1; |
+ SetFrameData(4, &frame_); |
+ |
+ target_frame.num_channels_ = 2; |
+ target_frame.samples_per_channel_ = frame_.samples_per_channel_; |
+ |
+ AudioFrameOperations::MonoToStereo(frame_.data_, frame_.samples_per_channel_, |
+ target_frame.data_); |
+ |
+ AudioFrame stereo_frame; |
+ stereo_frame.samples_per_channel_ = 320; |
+ stereo_frame.num_channels_ = 2; |
+ SetFrameData(4, 4, &stereo_frame); |
+ VerifyFramesAreEqual(stereo_frame, target_frame); |
} |
TEST_F(AudioFrameOperationsTest, StereoToMonoFailsWithBadParameters) { |
@@ -115,43 +138,148 @@ TEST_F(AudioFrameOperationsTest, StereoToMonoFailsWithBadParameters) { |
} |
TEST_F(AudioFrameOperationsTest, StereoToMonoSucceeds) { |
- SetFrameData(&frame_, 4, 2); |
- AudioFrame temp_frame; |
- temp_frame.CopyFrom(frame_); |
+ SetFrameData(4, 2, &frame_); |
EXPECT_EQ(0, AudioFrameOperations::StereoToMono(&frame_)); |
AudioFrame mono_frame; |
mono_frame.samples_per_channel_ = 320; |
mono_frame.num_channels_ = 1; |
- SetFrameData(&mono_frame, 3); |
+ SetFrameData(3, &mono_frame); |
VerifyFramesAreEqual(mono_frame, frame_); |
+} |
- SetFrameData(&frame_, 0); |
- AudioFrameOperations::StereoToMono(temp_frame.data_, |
- frame_.samples_per_channel_, |
- frame_.data_); |
- frame_.num_channels_ = 1; // Need to set manually. |
- VerifyFramesAreEqual(mono_frame, frame_); |
+TEST_F(AudioFrameOperationsTest, StereoToMonoBufferSucceeds) { |
+ AudioFrame target_frame; |
+ SetFrameData(4, 2, &frame_); |
+ |
+ target_frame.num_channels_ = 1; |
+ target_frame.samples_per_channel_ = frame_.samples_per_channel_; |
+ |
+ AudioFrameOperations::StereoToMono(frame_.data_, frame_.samples_per_channel_, |
+ target_frame.data_); |
+ |
+ AudioFrame mono_frame; |
+ mono_frame.samples_per_channel_ = 320; |
+ mono_frame.num_channels_ = 1; |
+ SetFrameData(3, &mono_frame); |
+ VerifyFramesAreEqual(mono_frame, target_frame); |
} |
TEST_F(AudioFrameOperationsTest, StereoToMonoDoesNotWrapAround) { |
- SetFrameData(&frame_, -32768, -32768); |
+ SetFrameData(-32768, -32768, &frame_); |
EXPECT_EQ(0, AudioFrameOperations::StereoToMono(&frame_)); |
AudioFrame mono_frame; |
mono_frame.samples_per_channel_ = 320; |
mono_frame.num_channels_ = 1; |
- SetFrameData(&mono_frame, -32768); |
+ SetFrameData(-32768, &mono_frame); |
+ VerifyFramesAreEqual(mono_frame, frame_); |
+} |
+ |
+TEST_F(AudioFrameOperationsTest, QuadToMonoFailsWithBadParameters) { |
+ frame_.num_channels_ = 1; |
+ EXPECT_EQ(-1, AudioFrameOperations::QuadToMono(&frame_)); |
+ frame_.num_channels_ = 2; |
+ EXPECT_EQ(-1, AudioFrameOperations::QuadToMono(&frame_)); |
+} |
+ |
+TEST_F(AudioFrameOperationsTest, QuadToMonoSucceeds) { |
+ frame_.num_channels_ = 4; |
+ SetFrameData(4, 2, 6, 8, &frame_); |
+ |
+ EXPECT_EQ(0, AudioFrameOperations::QuadToMono(&frame_)); |
+ |
+ AudioFrame mono_frame; |
+ mono_frame.samples_per_channel_ = 320; |
+ mono_frame.num_channels_ = 1; |
+ SetFrameData(5, &mono_frame); |
+ VerifyFramesAreEqual(mono_frame, frame_); |
+} |
+ |
+TEST_F(AudioFrameOperationsTest, QuadToMonoBufferSucceeds) { |
+ AudioFrame target_frame; |
+ frame_.num_channels_ = 4; |
+ SetFrameData(4, 2, 6, 8, &frame_); |
+ |
+ target_frame.num_channels_ = 1; |
+ target_frame.samples_per_channel_ = frame_.samples_per_channel_; |
+ |
+ AudioFrameOperations::QuadToMono(frame_.data_, frame_.samples_per_channel_, |
+ target_frame.data_); |
+ AudioFrame mono_frame; |
+ mono_frame.samples_per_channel_ = 320; |
+ mono_frame.num_channels_ = 1; |
+ SetFrameData(5, &mono_frame); |
+ VerifyFramesAreEqual(mono_frame, target_frame); |
+} |
+ |
+TEST_F(AudioFrameOperationsTest, QuadToMonoDoesNotWrapAround) { |
+ frame_.num_channels_ = 4; |
+ SetFrameData(-32768, -32768, -32768, -32768, &frame_); |
+ EXPECT_EQ(0, AudioFrameOperations::QuadToMono(&frame_)); |
+ |
+ AudioFrame mono_frame; |
+ mono_frame.samples_per_channel_ = 320; |
+ mono_frame.num_channels_ = 1; |
+ SetFrameData(-32768, &mono_frame); |
VerifyFramesAreEqual(mono_frame, frame_); |
} |
+TEST_F(AudioFrameOperationsTest, QuadToStereoFailsWithBadParameters) { |
+ frame_.num_channels_ = 1; |
+ EXPECT_EQ(-1, AudioFrameOperations::QuadToStereo(&frame_)); |
+ frame_.num_channels_ = 2; |
+ EXPECT_EQ(-1, AudioFrameOperations::QuadToStereo(&frame_)); |
+} |
+ |
+TEST_F(AudioFrameOperationsTest, QuadToStereoSucceeds) { |
+ frame_.num_channels_ = 4; |
+ SetFrameData(4, 2, 6, 8, &frame_); |
+ EXPECT_EQ(0, AudioFrameOperations::QuadToStereo(&frame_)); |
+ |
+ AudioFrame stereo_frame; |
+ stereo_frame.samples_per_channel_ = 320; |
+ stereo_frame.num_channels_ = 2; |
+ SetFrameData(3, 7, &stereo_frame); |
+ VerifyFramesAreEqual(stereo_frame, frame_); |
+} |
+ |
+TEST_F(AudioFrameOperationsTest, QuadToStereoBufferSucceeds) { |
+ AudioFrame target_frame; |
+ frame_.num_channels_ = 4; |
+ SetFrameData(4, 2, 6, 8, &frame_); |
+ |
+ target_frame.num_channels_ = 2; |
+ target_frame.samples_per_channel_ = frame_.samples_per_channel_; |
+ |
+ AudioFrameOperations::QuadToStereo(frame_.data_, frame_.samples_per_channel_, |
+ target_frame.data_); |
+ AudioFrame stereo_frame; |
+ stereo_frame.samples_per_channel_ = 320; |
+ stereo_frame.num_channels_ = 2; |
+ SetFrameData(3, 7, &stereo_frame); |
+ VerifyFramesAreEqual(stereo_frame, target_frame); |
+} |
+ |
+TEST_F(AudioFrameOperationsTest, QuadToStereoDoesNotWrapAround) { |
+ frame_.num_channels_ = 4; |
+ SetFrameData(-32768, -32768, -32768, -32768, &frame_); |
+ EXPECT_EQ(0, AudioFrameOperations::QuadToStereo(&frame_)); |
+ |
+ AudioFrame stereo_frame; |
+ stereo_frame.samples_per_channel_ = 320; |
+ stereo_frame.num_channels_ = 2; |
+ SetFrameData(-32768, -32768, &stereo_frame); |
+ VerifyFramesAreEqual(stereo_frame, frame_); |
+} |
+ |
TEST_F(AudioFrameOperationsTest, SwapStereoChannelsSucceedsOnStereo) { |
- SetFrameData(&frame_, 0, 1); |
+ SetFrameData(0, 1, &frame_); |
AudioFrame swapped_frame; |
swapped_frame.samples_per_channel_ = 320; |
swapped_frame.num_channels_ = 2; |
- SetFrameData(&swapped_frame, 1, 0); |
+ SetFrameData(1, 0, &swapped_frame); |
AudioFrameOperations::SwapStereoChannels(&frame_); |
VerifyFramesAreEqual(swapped_frame, frame_); |
@@ -160,7 +288,7 @@ TEST_F(AudioFrameOperationsTest, SwapStereoChannelsSucceedsOnStereo) { |
TEST_F(AudioFrameOperationsTest, SwapStereoChannelsFailsOnMono) { |
frame_.num_channels_ = 1; |
// Set data to "stereo", despite it being a mono frame. |
- SetFrameData(&frame_, 0, 1); |
+ SetFrameData(0, 1, &frame_); |
AudioFrame orig_frame; |
orig_frame.CopyFrom(frame_); |
@@ -170,24 +298,24 @@ TEST_F(AudioFrameOperationsTest, SwapStereoChannelsFailsOnMono) { |
} |
TEST_F(AudioFrameOperationsTest, MuteDisabled) { |
- SetFrameData(&frame_, 1000, -1000); |
+ SetFrameData(1000, -1000, &frame_); |
AudioFrameOperations::Mute(&frame_, false, false); |
AudioFrame muted_frame; |
muted_frame.samples_per_channel_ = 320; |
muted_frame.num_channels_ = 2; |
- SetFrameData(&muted_frame, 1000, -1000); |
+ SetFrameData(1000, -1000, &muted_frame); |
VerifyFramesAreEqual(muted_frame, frame_); |
} |
TEST_F(AudioFrameOperationsTest, MuteEnabled) { |
- SetFrameData(&frame_, 1000, -1000); |
+ SetFrameData(1000, -1000, &frame_); |
AudioFrameOperations::Mute(&frame_, true, true); |
AudioFrame muted_frame; |
muted_frame.samples_per_channel_ = 320; |
muted_frame.num_channels_ = 2; |
- SetFrameData(&muted_frame, 0, 0); |
+ SetFrameData(0, 0, &muted_frame); |
VerifyFramesAreEqual(muted_frame, frame_); |
} |
@@ -310,24 +438,24 @@ TEST_F(AudioFrameOperationsTest, DISABLED_ScaleFailsWithBadParameters) { |
// TODO(andrew): fix the wraparound bug. We should always saturate. |
TEST_F(AudioFrameOperationsTest, DISABLED_ScaleDoesNotWrapAround) { |
- SetFrameData(&frame_, 4000, -4000); |
+ SetFrameData(4000, -4000, &frame_); |
EXPECT_EQ(0, AudioFrameOperations::Scale(10.0, 10.0, frame_)); |
AudioFrame clipped_frame; |
clipped_frame.samples_per_channel_ = 320; |
clipped_frame.num_channels_ = 2; |
- SetFrameData(&clipped_frame, 32767, -32768); |
+ SetFrameData(32767, -32768, &clipped_frame); |
VerifyFramesAreEqual(clipped_frame, frame_); |
} |
TEST_F(AudioFrameOperationsTest, ScaleSucceeds) { |
- SetFrameData(&frame_, 1, -1); |
+ SetFrameData(1, -1, &frame_); |
EXPECT_EQ(0, AudioFrameOperations::Scale(2.0, 3.0, frame_)); |
AudioFrame scaled_frame; |
scaled_frame.samples_per_channel_ = 320; |
scaled_frame.num_channels_ = 2; |
- SetFrameData(&scaled_frame, 2, -3); |
+ SetFrameData(2, -3, &scaled_frame); |
VerifyFramesAreEqual(scaled_frame, frame_); |
} |
@@ -338,30 +466,30 @@ TEST_F(AudioFrameOperationsTest, DISABLED_ScaleWithSatFailsWithBadParameters) { |
TEST_F(AudioFrameOperationsTest, ScaleWithSatDoesNotWrapAround) { |
frame_.num_channels_ = 1; |
- SetFrameData(&frame_, 4000); |
+ SetFrameData(4000, &frame_); |
EXPECT_EQ(0, AudioFrameOperations::ScaleWithSat(10.0, frame_)); |
AudioFrame clipped_frame; |
clipped_frame.samples_per_channel_ = 320; |
clipped_frame.num_channels_ = 1; |
- SetFrameData(&clipped_frame, 32767); |
+ SetFrameData(32767, &clipped_frame); |
VerifyFramesAreEqual(clipped_frame, frame_); |
- SetFrameData(&frame_, -4000); |
+ SetFrameData(-4000, &frame_); |
EXPECT_EQ(0, AudioFrameOperations::ScaleWithSat(10.0, frame_)); |
- SetFrameData(&clipped_frame, -32768); |
+ SetFrameData(-32768, &clipped_frame); |
VerifyFramesAreEqual(clipped_frame, frame_); |
} |
TEST_F(AudioFrameOperationsTest, ScaleWithSatSucceeds) { |
frame_.num_channels_ = 1; |
- SetFrameData(&frame_, 1); |
+ SetFrameData(1, &frame_); |
EXPECT_EQ(0, AudioFrameOperations::ScaleWithSat(2.0, frame_)); |
AudioFrame scaled_frame; |
scaled_frame.samples_per_channel_ = 320; |
scaled_frame.num_channels_ = 1; |
- SetFrameData(&scaled_frame, 2); |
+ SetFrameData(2, &scaled_frame); |
VerifyFramesAreEqual(scaled_frame, frame_); |
} |
@@ -379,10 +507,10 @@ TEST_F(AudioFrameOperationsTest, AddingTwoFramesProducesTheirSum) { |
AudioFrame frame_to_add_to; |
frame_to_add_to.samples_per_channel_ = frame_.samples_per_channel_; |
frame_to_add_to.num_channels_ = frame_.num_channels_; |
- SetFrameData(&frame_to_add_to, 1000); |
+ SetFrameData(1000, &frame_to_add_to); |
AudioFrameOperations::Add(frame_, &frame_to_add_to); |
- SetFrameData(&frame_, frame_.data_[0] + 1000); |
+ SetFrameData(frame_.data_[0] + 1000, &frame_); |
VerifyFramesAreEqual(frame_, frame_to_add_to); |
} |
} // namespace |