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