2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
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.
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "webrtc/common_audio/include/audio_util.h"
13 #include "webrtc/typedefs.h"
17 void 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]);
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]);
29 TEST(AudioUtilTest, FloatToS16) {
31 const float kInput[kSize] = {
32 0.f, 0.4f / 32767.f, 0.6f / 32767.f, -0.4f / 32768.f, -0.6f / 32768.f,
33 1.f, -1.f, 1.1f, -1.1f};
34 const int16_t kReference[kSize] = {
35 0, 0, 1, 0, -1, 32767, -32768, 32767, -32768};
36 int16_t output[kSize];
37 FloatToS16(kInput, kSize, output);
38 ExpectArraysEq(kReference, output, kSize);
41 TEST(AudioUtilTest, S16ToFloat) {
43 const int16_t kInput[kSize] = {0, 1, -1, 16384, -16384, 32767, -32768};
44 const float kReference[kSize] = {
45 0.f, 1.f / 32767.f, -1.f / 32768.f, 16384.f / 32767.f, -0.5f, 1.f, -1.f};
47 S16ToFloat(kInput, kSize, output);
48 ExpectArraysEq(kReference, output, kSize);
51 TEST(AudioUtilTest, FloatS16ToS16) {
53 const float kInput[kSize] = {
54 0.f, 0.4f, 0.5f, -0.4f, -0.5f, 32768.f, -32769.f};
55 const int16_t kReference[kSize] = {0, 0, 1, 0, -1, 32767, -32768};
56 int16_t output[kSize];
57 FloatS16ToS16(kInput, kSize, output);
58 ExpectArraysEq(kReference, output, kSize);
61 TEST(AudioUtilTest, FloatToFloatS16) {
63 const float kInput[kSize] = {
64 0.f, 0.4f / 32767.f, 0.6f / 32767.f, -0.4f / 32768.f, -0.6f / 32768.f,
65 1.f, -1.f, 1.1f, -1.1f};
66 const float kReference[kSize] = {
67 0.f, 0.4f, 0.6f, -0.4f, -0.6f, 32767.f, -32768.f, 36043.7f, -36044.8f};
69 FloatToFloatS16(kInput, kSize, output);
70 ExpectArraysEq(kReference, output, kSize);
73 TEST(AudioUtilTest, FloatS16ToFloat) {
75 const float kInput[kSize] = {
76 0.f, 0.4f, 0.6f, -0.4f, -0.6f, 32767.f, -32768.f, 36043.7f, -36044.8f};
77 const float kReference[kSize] = {
78 0.f, 0.4f / 32767.f, 0.6f / 32767.f, -0.4f / 32768.f, -0.6f / 32768.f,
79 1.f, -1.f, 1.1f, -1.1f};
81 FloatS16ToFloat(kInput, kSize, output);
82 ExpectArraysEq(kReference, output, kSize);
85 TEST(AudioUtilTest, InterleavingStereo) {
86 const int16_t kInterleaved[] = {2, 3, 4, 9, 8, 27, 16, 81};
87 const int kSamplesPerChannel = 4;
88 const int kNumChannels = 2;
89 const int kLength = kSamplesPerChannel * kNumChannels;
90 int16_t left[kSamplesPerChannel], right[kSamplesPerChannel];
91 int16_t* deinterleaved[] = {left, right};
92 Deinterleave(kInterleaved, kSamplesPerChannel, kNumChannels, deinterleaved);
93 const int16_t kRefLeft[] = {2, 4, 8, 16};
94 const int16_t kRefRight[] = {3, 9, 27, 81};
95 ExpectArraysEq(kRefLeft, left, kSamplesPerChannel);
96 ExpectArraysEq(kRefRight, right, kSamplesPerChannel);
98 int16_t interleaved[kLength];
99 Interleave(deinterleaved, kSamplesPerChannel, kNumChannels, interleaved);
100 ExpectArraysEq(kInterleaved, interleaved, kLength);
103 TEST(AudioUtilTest, InterleavingMonoIsIdentical) {
104 const int16_t kInterleaved[] = {1, 2, 3, 4, 5};
105 const int kSamplesPerChannel = 5;
106 const int kNumChannels = 1;
107 int16_t mono[kSamplesPerChannel];
108 int16_t* deinterleaved[] = {mono};
109 Deinterleave(kInterleaved, kSamplesPerChannel, kNumChannels, deinterleaved);
110 ExpectArraysEq(kInterleaved, mono, kSamplesPerChannel);
112 int16_t interleaved[kSamplesPerChannel];
113 Interleave(deinterleaved, kSamplesPerChannel, kNumChannels, interleaved);
114 ExpectArraysEq(mono, interleaved, kSamplesPerChannel);
117 } // namespace webrtc