Chromium Code Reviews| 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 "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 12 |
| 13 #include "webrtc/modules/include/module_common_types.h" | 13 #include "webrtc/modules/include/module_common_types.h" |
| 14 #include "webrtc/modules/utility/include/audio_frame_operations.h" | 14 #include "webrtc/modules/utility/include/audio_frame_operations.h" |
| 15 #include "webrtc/base/checks.h" | |
| 15 | 16 |
| 16 namespace webrtc { | 17 namespace webrtc { |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 class AudioFrameOperationsTest : public ::testing::Test { | 20 class AudioFrameOperationsTest : public ::testing::Test { |
| 20 protected: | 21 protected: |
| 21 AudioFrameOperationsTest() { | 22 AudioFrameOperationsTest() { |
| 22 // Set typical values. | 23 // Set typical values. |
| 23 frame_.samples_per_channel_ = 320; | 24 frame_.samples_per_channel_ = 320; |
| 24 frame_.num_channels_ = 2; | 25 frame_.num_channels_ = 2; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 44 EXPECT_EQ(frame1.num_channels_, frame2.num_channels_); | 45 EXPECT_EQ(frame1.num_channels_, frame2.num_channels_); |
| 45 EXPECT_EQ(frame1.samples_per_channel_, | 46 EXPECT_EQ(frame1.samples_per_channel_, |
| 46 frame2.samples_per_channel_); | 47 frame2.samples_per_channel_); |
| 47 | 48 |
| 48 for (size_t i = 0; i < frame1.samples_per_channel_ * frame1.num_channels_; | 49 for (size_t i = 0; i < frame1.samples_per_channel_ * frame1.num_channels_; |
| 49 i++) { | 50 i++) { |
| 50 EXPECT_EQ(frame1.data_[i], frame2.data_[i]); | 51 EXPECT_EQ(frame1.data_[i], frame2.data_[i]); |
| 51 } | 52 } |
| 52 } | 53 } |
| 53 | 54 |
| 55 void InitFrame(AudioFrame* frame, size_t channels, size_t samples_per_channel, | |
| 56 int16_t left_data, int16_t right_data) { | |
| 57 RTC_DCHECK(frame); | |
| 58 RTC_DCHECK_GE(2u, channels); | |
| 59 // RTC_DCHECK_GE(AudioFrame::kMaxDataSizeSamples, | |
| 60 // samples_per_channel * channels); | |
| 61 frame->samples_per_channel_ = samples_per_channel; | |
| 62 frame->num_channels_ = channels; | |
| 63 if (channels == 2) { | |
| 64 SetFrameData(frame, left_data, right_data); | |
| 65 } else if (channels == 1) { | |
| 66 SetFrameData(frame, left_data); | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 int16_t GetChannelData(const AudioFrame& frame, size_t channel, size_t index) { | |
| 71 RTC_DCHECK_LT(channel, frame.num_channels_); | |
| 72 RTC_DCHECK_LT(index, frame.samples_per_channel_); | |
| 73 return frame.data_[index * frame.num_channels_ + channel]; | |
| 74 } | |
| 75 | |
| 76 void VerifyFrameDataBounds(const AudioFrame& frame, size_t channel, int16_t max, | |
| 77 int16_t min) { | |
| 78 for (size_t i = 0; i < frame.samples_per_channel_; ++i) { | |
| 79 int16_t s = GetChannelData(frame, channel, i); | |
| 80 EXPECT_LE(min, s); | |
| 81 EXPECT_GE(max, s); | |
| 82 } | |
| 83 } | |
| 84 | |
| 54 TEST_F(AudioFrameOperationsTest, MonoToStereoFailsWithBadParameters) { | 85 TEST_F(AudioFrameOperationsTest, MonoToStereoFailsWithBadParameters) { |
| 55 EXPECT_EQ(-1, AudioFrameOperations::MonoToStereo(&frame_)); | 86 EXPECT_EQ(-1, AudioFrameOperations::MonoToStereo(&frame_)); |
| 56 | 87 |
| 57 frame_.samples_per_channel_ = AudioFrame::kMaxDataSizeSamples; | 88 frame_.samples_per_channel_ = AudioFrame::kMaxDataSizeSamples; |
| 58 frame_.num_channels_ = 1; | 89 frame_.num_channels_ = 1; |
| 59 EXPECT_EQ(-1, AudioFrameOperations::MonoToStereo(&frame_)); | 90 EXPECT_EQ(-1, AudioFrameOperations::MonoToStereo(&frame_)); |
| 60 } | 91 } |
| 61 | 92 |
| 62 TEST_F(AudioFrameOperationsTest, MonoToStereoSucceeds) { | 93 TEST_F(AudioFrameOperationsTest, MonoToStereoSucceeds) { |
| 63 frame_.num_channels_ = 1; | 94 frame_.num_channels_ = 1; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 133 // Set data to "stereo", despite it being a mono frame. | 164 // Set data to "stereo", despite it being a mono frame. |
| 134 SetFrameData(&frame_, 0, 1); | 165 SetFrameData(&frame_, 0, 1); |
| 135 | 166 |
| 136 AudioFrame orig_frame; | 167 AudioFrame orig_frame; |
| 137 orig_frame.CopyFrom(frame_); | 168 orig_frame.CopyFrom(frame_); |
| 138 AudioFrameOperations::SwapStereoChannels(&frame_); | 169 AudioFrameOperations::SwapStereoChannels(&frame_); |
| 139 // Verify that no swap occurred. | 170 // Verify that no swap occurred. |
| 140 VerifyFramesAreEqual(orig_frame, frame_); | 171 VerifyFramesAreEqual(orig_frame, frame_); |
| 141 } | 172 } |
| 142 | 173 |
| 143 TEST_F(AudioFrameOperationsTest, MuteSucceeds) { | 174 TEST_F(AudioFrameOperationsTest, MuteDisabled) { |
| 144 SetFrameData(&frame_, 1000, 1000); | 175 SetFrameData(&frame_, 1000, -1000); |
| 145 AudioFrameOperations::Mute(frame_); | 176 AudioFrameOperations::Mute(&frame_, false, false); |
| 146 | 177 |
| 147 AudioFrame muted_frame; | 178 AudioFrame muted_frame; |
| 148 muted_frame.samples_per_channel_ = 320; | 179 muted_frame.samples_per_channel_ = 320; |
| 180 muted_frame.num_channels_ = 2; | |
| 181 SetFrameData(&muted_frame, 1000, -1000); | |
| 182 VerifyFramesAreEqual(muted_frame, frame_); | |
| 183 } | |
| 184 | |
| 185 TEST_F(AudioFrameOperationsTest, MuteEnabled) { | |
| 186 SetFrameData(&frame_, 1000, -1000); | |
| 187 AudioFrameOperations::Mute(&frame_, true, true); | |
| 188 | |
| 189 AudioFrame muted_frame; | |
| 190 muted_frame.samples_per_channel_ = 320; | |
| 149 muted_frame.num_channels_ = 2; | 191 muted_frame.num_channels_ = 2; |
| 150 SetFrameData(&muted_frame, 0, 0); | 192 SetFrameData(&muted_frame, 0, 0); |
| 151 VerifyFramesAreEqual(muted_frame, frame_); | 193 VerifyFramesAreEqual(muted_frame, frame_); |
| 152 } | 194 } |
| 153 | 195 |
| 196 // Verify that *beginning* to mute works for short, long (>128) frames, mono | |
| 197 // and stereo. Beginning mute should yields a ramp down to zero. | |
| 198 TEST_F(AudioFrameOperationsTest, MuteBeginMonoLong) { | |
| 199 InitFrame(&frame_, 1, 228, 1000, -1000); | |
| 200 AudioFrameOperations::Mute(&frame_, false, true); | |
| 201 VerifyFrameDataBounds(frame_, 0, 1000, 0); | |
| 202 EXPECT_EQ(1000, GetChannelData(frame_, 0, 99)); | |
| 203 EXPECT_EQ(992, GetChannelData(frame_, 0, 100)); | |
| 204 EXPECT_EQ(7, GetChannelData(frame_, 0, 226)); | |
| 205 EXPECT_EQ(0, GetChannelData(frame_, 0, 227)); | |
| 206 } | |
| 207 | |
| 208 TEST_F(AudioFrameOperationsTest, MuteBeginMonoShort) { | |
| 209 InitFrame(&frame_, 1, 93, 1000, -1000); | |
| 210 AudioFrameOperations::Mute(&frame_, false, true); | |
| 211 VerifyFrameDataBounds(frame_, 0, 1000, 0); | |
| 212 EXPECT_EQ(989, GetChannelData(frame_, 0, 0)); | |
| 213 EXPECT_EQ(978, GetChannelData(frame_, 0, 1)); | |
| 214 EXPECT_EQ(10, GetChannelData(frame_, 0, 91)); | |
| 215 EXPECT_EQ(0, GetChannelData(frame_, 0, 92)); | |
| 216 } | |
| 217 | |
| 218 TEST_F(AudioFrameOperationsTest, MuteBeginStereoLong) { | |
| 219 InitFrame(&frame_, 2, 228, 1000, -1000); | |
| 220 AudioFrameOperations::Mute(&frame_, false, true); | |
| 221 VerifyFrameDataBounds(frame_, 0, 1000, 0); | |
| 222 VerifyFrameDataBounds(frame_, 1, 0, -1000); | |
| 223 EXPECT_EQ(1000, GetChannelData(frame_, 0, 99)); | |
| 224 EXPECT_EQ(-1000, GetChannelData(frame_, 1, 99)); | |
| 225 EXPECT_EQ(992, GetChannelData(frame_, 0, 100)); | |
| 226 EXPECT_EQ(-992, GetChannelData(frame_, 1, 100)); | |
| 227 EXPECT_EQ(7, GetChannelData(frame_, 0, 226)); | |
| 228 EXPECT_EQ(-7, GetChannelData(frame_, 1, 226)); | |
| 229 EXPECT_EQ(0, GetChannelData(frame_, 0, 227)); | |
| 230 EXPECT_EQ(0, GetChannelData(frame_, 1, 227)); | |
| 231 } | |
| 232 | |
| 233 TEST_F(AudioFrameOperationsTest, MuteBeginStereoShort) { | |
| 234 InitFrame(&frame_, 2, 93, 1000, -1000); | |
| 235 AudioFrameOperations::Mute(&frame_, false, true); | |
| 236 VerifyFrameDataBounds(frame_, 0, 1000, 0); | |
| 237 VerifyFrameDataBounds(frame_, 1, 0, -1000); | |
| 238 EXPECT_EQ(989, GetChannelData(frame_, 0, 0)); | |
| 239 EXPECT_EQ(-989, GetChannelData(frame_, 1, 0)); | |
| 240 EXPECT_EQ(978, GetChannelData(frame_, 0, 1)); | |
| 241 EXPECT_EQ(-978, GetChannelData(frame_, 1, 1)); | |
| 242 EXPECT_EQ(10, GetChannelData(frame_, 0, 91)); | |
| 243 EXPECT_EQ(-10, GetChannelData(frame_, 1, 91)); | |
| 244 EXPECT_EQ(0, GetChannelData(frame_, 0, 92)); | |
| 245 EXPECT_EQ(0, GetChannelData(frame_, 1, 92)); | |
| 246 } | |
| 247 | |
| 248 // Verify that *ending* to mute works for short, long (>128) frames, mono | |
| 249 // and stereo. Ending mute should yields a ramp down to zero. | |
|
tlegrand-webrtc
2016/03/23 13:42:41
ramp down to zero -> ramp up from zero
the sun
2016/03/23 14:25:59
Done.
| |
| 250 TEST_F(AudioFrameOperationsTest, MuteEndMonoLong) { | |
| 251 InitFrame(&frame_, 1, 228, 1000, -1000); | |
| 252 AudioFrameOperations::Mute(&frame_, true, false); | |
| 253 VerifyFrameDataBounds(frame_, 0, 1000, 0); | |
| 254 EXPECT_EQ(7, GetChannelData(frame_, 0, 0)); | |
| 255 EXPECT_EQ(15, GetChannelData(frame_, 0, 1)); | |
| 256 EXPECT_EQ(1000, GetChannelData(frame_, 0, 127)); | |
| 257 EXPECT_EQ(1000, GetChannelData(frame_, 0, 128)); | |
| 258 } | |
| 259 | |
| 260 TEST_F(AudioFrameOperationsTest, MuteEndMonoShort) { | |
| 261 InitFrame(&frame_, 1, 93, 1000, -1000); | |
| 262 AudioFrameOperations::Mute(&frame_, true, false); | |
| 263 VerifyFrameDataBounds(frame_, 0, 1000, 0); | |
| 264 EXPECT_EQ(10, GetChannelData(frame_, 0, 0)); | |
| 265 EXPECT_EQ(21, GetChannelData(frame_, 0, 1)); | |
| 266 EXPECT_EQ(989, GetChannelData(frame_, 0, 91)); | |
| 267 EXPECT_EQ(999, GetChannelData(frame_, 0, 92)); | |
| 268 } | |
| 269 | |
| 270 TEST_F(AudioFrameOperationsTest, MuteEndStereoLong) { | |
| 271 InitFrame(&frame_, 2, 228, 1000, -1000); | |
| 272 AudioFrameOperations::Mute(&frame_, true, false); | |
| 273 VerifyFrameDataBounds(frame_, 0, 1000, 0); | |
| 274 VerifyFrameDataBounds(frame_, 1, 0, -1000); | |
| 275 EXPECT_EQ(7, GetChannelData(frame_, 0, 0)); | |
| 276 EXPECT_EQ(-7, GetChannelData(frame_, 1, 0)); | |
| 277 EXPECT_EQ(15, GetChannelData(frame_, 0, 1)); | |
| 278 EXPECT_EQ(-15, GetChannelData(frame_, 1, 1)); | |
| 279 EXPECT_EQ(1000, GetChannelData(frame_, 0, 127)); | |
| 280 EXPECT_EQ(-1000, GetChannelData(frame_, 1, 127)); | |
| 281 EXPECT_EQ(1000, GetChannelData(frame_, 0, 128)); | |
| 282 EXPECT_EQ(-1000, GetChannelData(frame_, 1, 128)); | |
| 283 } | |
| 284 | |
| 285 TEST_F(AudioFrameOperationsTest, MuteEndStereoShort) { | |
| 286 InitFrame(&frame_, 2, 93, 1000, -1000); | |
| 287 AudioFrameOperations::Mute(&frame_, true, false); | |
| 288 VerifyFrameDataBounds(frame_, 0, 1000, 0); | |
| 289 VerifyFrameDataBounds(frame_, 1, 0, -1000); | |
| 290 EXPECT_EQ(10, GetChannelData(frame_, 0, 0)); | |
| 291 EXPECT_EQ(-10, GetChannelData(frame_, 1, 0)); | |
| 292 EXPECT_EQ(21, GetChannelData(frame_, 0, 1)); | |
| 293 EXPECT_EQ(-21, GetChannelData(frame_, 1, 1)); | |
| 294 EXPECT_EQ(989, GetChannelData(frame_, 0, 91)); | |
| 295 EXPECT_EQ(-989, GetChannelData(frame_, 1, 91)); | |
| 296 EXPECT_EQ(999, GetChannelData(frame_, 0, 92)); | |
| 297 EXPECT_EQ(-999, GetChannelData(frame_, 1, 92)); | |
| 298 } | |
| 299 | |
| 154 // TODO(andrew): should not allow negative scales. | 300 // TODO(andrew): should not allow negative scales. |
| 155 TEST_F(AudioFrameOperationsTest, DISABLED_ScaleFailsWithBadParameters) { | 301 TEST_F(AudioFrameOperationsTest, DISABLED_ScaleFailsWithBadParameters) { |
| 156 frame_.num_channels_ = 1; | 302 frame_.num_channels_ = 1; |
| 157 EXPECT_EQ(-1, AudioFrameOperations::Scale(1.0, 1.0, frame_)); | 303 EXPECT_EQ(-1, AudioFrameOperations::Scale(1.0, 1.0, frame_)); |
| 158 | 304 |
| 159 frame_.num_channels_ = 3; | 305 frame_.num_channels_ = 3; |
| 160 EXPECT_EQ(-1, AudioFrameOperations::Scale(1.0, 1.0, frame_)); | 306 EXPECT_EQ(-1, AudioFrameOperations::Scale(1.0, 1.0, frame_)); |
| 161 | 307 |
| 162 frame_.num_channels_ = 2; | 308 frame_.num_channels_ = 2; |
| 163 EXPECT_EQ(-1, AudioFrameOperations::Scale(-1.0, 1.0, frame_)); | 309 EXPECT_EQ(-1, AudioFrameOperations::Scale(-1.0, 1.0, frame_)); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 216 | 362 |
| 217 AudioFrame scaled_frame; | 363 AudioFrame scaled_frame; |
| 218 scaled_frame.samples_per_channel_ = 320; | 364 scaled_frame.samples_per_channel_ = 320; |
| 219 scaled_frame.num_channels_ = 1; | 365 scaled_frame.num_channels_ = 1; |
| 220 SetFrameData(&scaled_frame, 2); | 366 SetFrameData(&scaled_frame, 2); |
| 221 VerifyFramesAreEqual(scaled_frame, frame_); | 367 VerifyFramesAreEqual(scaled_frame, frame_); |
| 222 } | 368 } |
| 223 | 369 |
| 224 } // namespace | 370 } // namespace |
| 225 } // namespace webrtc | 371 } // namespace webrtc |
| OLD | NEW |