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
15namespace webrtc {
16
17void ExpectArraysEq(const int16_t* ref, const int16_t* test, int length) {
18  for (int i = 0; i < length; ++i) {
19    EXPECT_EQ(ref[i], test[i]);
20  }
21}
22
23void 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  }
27}
28
29TEST(AudioUtilTest, RoundToInt16) {
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);
36  ExpectArraysEq(kReference, output, kSize);
37}
38
39TEST(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
51TEST(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);
59}
60
61TEST(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};
71  ExpectArraysEq(kRefLeft, left, kSamplesPerChannel);
72  ExpectArraysEq(kRefRight, right, kSamplesPerChannel);
73
74  int16_t interleaved[kLength];
75  Interleave(deinterleaved, kSamplesPerChannel, kNumChannels, interleaved);
76  ExpectArraysEq(kInterleaved, interleaved, kLength);
77}
78
79TEST(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);
86  ExpectArraysEq(kInterleaved, mono, kSamplesPerChannel);
87
88  int16_t interleaved[kSamplesPerChannel];
89  Interleave(deinterleaved, kSamplesPerChannel, kNumChannels, interleaved);
90  ExpectArraysEq(mono, interleaved, kSamplesPerChannel);
91}
92
93}  // namespace webrtc
94