andrew@webrtc.org | b6fadb1 | 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) { |
andrew@webrtc.org | 5350e31 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 19 | EXPECT_EQ(ref[i], test[i]); |
andrew@webrtc.org | b6fadb1 | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 20 | } |
| 21 | } |
| 22 | |
andrew@webrtc.org | 5350e31 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 23 | void ExpectArraysEq(const float* ref, const float* test, int length) { |
| 24 | for (int i = 0; i < length; ++i) { |
| 25 | EXPECT_FLOAT_EQ(ref[i], test[i]); |
| 26 | } |
andrew@webrtc.org | 11a8868 | 2013-09-06 21:15:55 +0000 | [diff] [blame] | 27 | } |
| 28 | |
andrew@webrtc.org | 5350e31 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 29 | TEST(AudioUtilTest, RoundToInt16) { |
turaj@webrtc.org | 3528ad9 | 2014-02-20 20:55:21 +0000 | [diff] [blame] | 30 | const int kSize = 7; |
| 31 | const float kInput[kSize] = { |
| 32 | 0.f, 0.4f, 0.5f, -0.4f, -0.5f, 32768.f, -32769.f}; |
| 33 | const int16_t kReference[kSize] = {0, 0, 1, 0, -1, 32767, -32768}; |
| 34 | int16_t output[kSize]; |
| 35 | RoundToInt16(kInput, kSize, output); |
andrew@webrtc.org | 5350e31 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 36 | ExpectArraysEq(kReference, output, kSize); |
| 37 | } |
| 38 | |
| 39 | TEST(AudioUtilTest, ScaleAndRoundToInt16) { |
| 40 | const int kSize = 9; |
| 41 | const float kInput[kSize] = { |
| 42 | 0.f, 0.4f / 32767.f, 0.6f / 32767.f, -0.4f / 32768.f, -0.6f / 32768.f, |
| 43 | 1.f, -1.f, 1.1f, -1.1f}; |
| 44 | const int16_t kReference[kSize] = { |
| 45 | 0, 0, 1, 0, -1, 32767, -32768, 32767, -32768}; |
| 46 | int16_t output[kSize]; |
| 47 | ScaleAndRoundToInt16(kInput, kSize, output); |
| 48 | ExpectArraysEq(kReference, output, kSize); |
| 49 | } |
| 50 | |
| 51 | TEST(AudioUtilTest, ScaleToFloat) { |
| 52 | const int kSize = 7; |
| 53 | const int16_t kInput[kSize] = {0, 1, -1, 16384, -16384, 32767, -32768}; |
| 54 | const float kReference[kSize] = { |
| 55 | 0.f, 1.f / 32767.f, -1.f / 32768.f, 16384.f / 32767.f, -0.5f, 1.f, -1.f}; |
| 56 | float output[kSize]; |
| 57 | ScaleToFloat(kInput, kSize, output); |
| 58 | ExpectArraysEq(kReference, output, kSize); |
andrew@webrtc.org | 11a8868 | 2013-09-06 21:15:55 +0000 | [diff] [blame] | 59 | } |
| 60 | |
andrew@webrtc.org | b6fadb1 | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 61 | TEST(AudioUtilTest, InterleavingStereo) { |
| 62 | const int16_t kInterleaved[] = {2, 3, 4, 9, 8, 27, 16, 81}; |
| 63 | const int kSamplesPerChannel = 4; |
| 64 | const int kNumChannels = 2; |
| 65 | const int kLength = kSamplesPerChannel * kNumChannels; |
| 66 | int16_t left[kSamplesPerChannel], right[kSamplesPerChannel]; |
| 67 | int16_t* deinterleaved[] = {left, right}; |
| 68 | Deinterleave(kInterleaved, kSamplesPerChannel, kNumChannels, deinterleaved); |
| 69 | const int16_t kRefLeft[] = {2, 4, 8, 16}; |
| 70 | const int16_t kRefRight[] = {3, 9, 27, 81}; |
andrew@webrtc.org | 5350e31 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 71 | ExpectArraysEq(kRefLeft, left, kSamplesPerChannel); |
| 72 | ExpectArraysEq(kRefRight, right, kSamplesPerChannel); |
andrew@webrtc.org | b6fadb1 | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 73 | |
| 74 | int16_t interleaved[kLength]; |
| 75 | Interleave(deinterleaved, kSamplesPerChannel, kNumChannels, interleaved); |
andrew@webrtc.org | 5350e31 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 76 | ExpectArraysEq(kInterleaved, interleaved, kLength); |
andrew@webrtc.org | b6fadb1 | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | TEST(AudioUtilTest, InterleavingMonoIsIdentical) { |
| 80 | const int16_t kInterleaved[] = {1, 2, 3, 4, 5}; |
| 81 | const int kSamplesPerChannel = 5; |
| 82 | const int kNumChannels = 1; |
| 83 | int16_t mono[kSamplesPerChannel]; |
| 84 | int16_t* deinterleaved[] = {mono}; |
| 85 | Deinterleave(kInterleaved, kSamplesPerChannel, kNumChannels, deinterleaved); |
andrew@webrtc.org | 5350e31 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 86 | ExpectArraysEq(kInterleaved, mono, kSamplesPerChannel); |
andrew@webrtc.org | b6fadb1 | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 87 | |
| 88 | int16_t interleaved[kSamplesPerChannel]; |
| 89 | Interleave(deinterleaved, kSamplesPerChannel, kNumChannels, interleaved); |
andrew@webrtc.org | 5350e31 | 2014-03-04 20:58:13 +0000 | [diff] [blame] | 90 | ExpectArraysEq(mono, interleaved, kSamplesPerChannel); |
andrew@webrtc.org | b6fadb1 | 2013-04-29 17:27:29 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | } // namespace webrtc |