blob: bf9ad81252332860713fba6280b3f5c577b2972f [file] [log] [blame]
andrew@webrtc.orgb6fadb12013-04-29 17:27:29 +00001/*
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) {
andrew@webrtc.org5350e312014-03-04 20:58:13 +000019 EXPECT_EQ(ref[i], test[i]);
andrew@webrtc.orgb6fadb12013-04-29 17:27:29 +000020 }
21}
22
andrew@webrtc.org5350e312014-03-04 20:58:13 +000023void 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.org11a88682013-09-06 21:15:55 +000027}
28
andrew@webrtc.org5350e312014-03-04 20:58:13 +000029TEST(AudioUtilTest, RoundToInt16) {
turaj@webrtc.org3528ad92014-02-20 20:55:21 +000030 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.org5350e312014-03-04 20:58:13 +000036 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);
andrew@webrtc.org11a88682013-09-06 21:15:55 +000059}
60
andrew@webrtc.orgb6fadb12013-04-29 17:27:29 +000061TEST(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.org5350e312014-03-04 20:58:13 +000071 ExpectArraysEq(kRefLeft, left, kSamplesPerChannel);
72 ExpectArraysEq(kRefRight, right, kSamplesPerChannel);
andrew@webrtc.orgb6fadb12013-04-29 17:27:29 +000073
74 int16_t interleaved[kLength];
75 Interleave(deinterleaved, kSamplesPerChannel, kNumChannels, interleaved);
andrew@webrtc.org5350e312014-03-04 20:58:13 +000076 ExpectArraysEq(kInterleaved, interleaved, kLength);
andrew@webrtc.orgb6fadb12013-04-29 17:27:29 +000077}
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);
andrew@webrtc.org5350e312014-03-04 20:58:13 +000086 ExpectArraysEq(kInterleaved, mono, kSamplesPerChannel);
andrew@webrtc.orgb6fadb12013-04-29 17:27:29 +000087
88 int16_t interleaved[kSamplesPerChannel];
89 Interleave(deinterleaved, kSamplesPerChannel, kNumChannels, interleaved);
andrew@webrtc.org5350e312014-03-04 20:58:13 +000090 ExpectArraysEq(mono, interleaved, kSamplesPerChannel);
andrew@webrtc.orgb6fadb12013-04-29 17:27:29 +000091}
92
93} // namespace webrtc