andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 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 "testing/gtest/include/gtest/gtest.h" |
| 12 | #include "webrtc/common_audio/include/audio_util.h" |
| 13 | #include "webrtc/typedefs.h" |
| 14 | |
| 15 | namespace webrtc { |
| 16 | |
| 17 | void ExpectArraysEq(const int16_t* ref, const int16_t* test, int length) { |
| 18 | for (int i = 0; i < length; ++i) { |
| 19 | EXPECT_EQ(test[i], ref[i]); |
| 20 | } |
| 21 | } |
| 22 | |
andrew@webrtc.org | b159c2e | 2013-09-06 21:15:55 +0000 | [diff] [blame^] | 23 | TEST(AudioUtilTest, Clamp) { |
| 24 | EXPECT_EQ(1000.f, ClampInt16(1000.f)); |
| 25 | EXPECT_EQ(32767.f, ClampInt16(32767.5f)); |
| 26 | EXPECT_EQ(-32768.f, ClampInt16(-32768.5f)); |
| 27 | } |
| 28 | |
| 29 | TEST(AudioUtilTest, Round) { |
| 30 | EXPECT_EQ(0, RoundToInt16(0.f)); |
| 31 | EXPECT_EQ(0, RoundToInt16(0.4f)); |
| 32 | EXPECT_EQ(1, RoundToInt16(0.5f)); |
| 33 | EXPECT_EQ(0, RoundToInt16(-0.4f)); |
| 34 | EXPECT_EQ(-1, RoundToInt16(-0.5f)); |
| 35 | } |
| 36 | |
andrew@webrtc.org | 50b2efe | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 37 | TEST(AudioUtilTest, InterleavingStereo) { |
| 38 | const int16_t kInterleaved[] = {2, 3, 4, 9, 8, 27, 16, 81}; |
| 39 | const int kSamplesPerChannel = 4; |
| 40 | const int kNumChannels = 2; |
| 41 | const int kLength = kSamplesPerChannel * kNumChannels; |
| 42 | int16_t left[kSamplesPerChannel], right[kSamplesPerChannel]; |
| 43 | int16_t* deinterleaved[] = {left, right}; |
| 44 | Deinterleave(kInterleaved, kSamplesPerChannel, kNumChannels, deinterleaved); |
| 45 | const int16_t kRefLeft[] = {2, 4, 8, 16}; |
| 46 | const int16_t kRefRight[] = {3, 9, 27, 81}; |
| 47 | ExpectArraysEq(left, kRefLeft, kSamplesPerChannel); |
| 48 | ExpectArraysEq(right, kRefRight, kSamplesPerChannel); |
| 49 | |
| 50 | int16_t interleaved[kLength]; |
| 51 | Interleave(deinterleaved, kSamplesPerChannel, kNumChannels, interleaved); |
| 52 | ExpectArraysEq(interleaved, kInterleaved, kLength); |
| 53 | } |
| 54 | |
| 55 | TEST(AudioUtilTest, InterleavingMonoIsIdentical) { |
| 56 | const int16_t kInterleaved[] = {1, 2, 3, 4, 5}; |
| 57 | const int kSamplesPerChannel = 5; |
| 58 | const int kNumChannels = 1; |
| 59 | int16_t mono[kSamplesPerChannel]; |
| 60 | int16_t* deinterleaved[] = {mono}; |
| 61 | Deinterleave(kInterleaved, kSamplesPerChannel, kNumChannels, deinterleaved); |
| 62 | ExpectArraysEq(mono, kInterleaved, kSamplesPerChannel); |
| 63 | |
| 64 | int16_t interleaved[kSamplesPerChannel]; |
| 65 | Interleave(deinterleaved, kSamplesPerChannel, kNumChannels, interleaved); |
| 66 | ExpectArraysEq(interleaved, mono, kSamplesPerChannel); |
| 67 | } |
| 68 | |
| 69 | } // namespace webrtc |