blob: 7e8dee389dabae47acb839659e6779bb8046636c [file] [log] [blame]
andrew@webrtc.org50b2efe2013-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) {
19 EXPECT_EQ(test[i], ref[i]);
20 }
21}
22
andrew@webrtc.orgb159c2e2013-09-06 21:15:55 +000023TEST(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
29TEST(AudioUtilTest, Round) {
turaj@webrtc.orgd4d5be82014-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);
36 for (int n = 0; n < kSize; ++n)
37 EXPECT_EQ(kReference[n], output[n]);
andrew@webrtc.orgb159c2e2013-09-06 21:15:55 +000038}
39
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +000040TEST(AudioUtilTest, InterleavingStereo) {
41 const int16_t kInterleaved[] = {2, 3, 4, 9, 8, 27, 16, 81};
42 const int kSamplesPerChannel = 4;
43 const int kNumChannels = 2;
44 const int kLength = kSamplesPerChannel * kNumChannels;
45 int16_t left[kSamplesPerChannel], right[kSamplesPerChannel];
46 int16_t* deinterleaved[] = {left, right};
47 Deinterleave(kInterleaved, kSamplesPerChannel, kNumChannels, deinterleaved);
48 const int16_t kRefLeft[] = {2, 4, 8, 16};
49 const int16_t kRefRight[] = {3, 9, 27, 81};
50 ExpectArraysEq(left, kRefLeft, kSamplesPerChannel);
51 ExpectArraysEq(right, kRefRight, kSamplesPerChannel);
52
53 int16_t interleaved[kLength];
54 Interleave(deinterleaved, kSamplesPerChannel, kNumChannels, interleaved);
55 ExpectArraysEq(interleaved, kInterleaved, kLength);
56}
57
58TEST(AudioUtilTest, InterleavingMonoIsIdentical) {
59 const int16_t kInterleaved[] = {1, 2, 3, 4, 5};
60 const int kSamplesPerChannel = 5;
61 const int kNumChannels = 1;
62 int16_t mono[kSamplesPerChannel];
63 int16_t* deinterleaved[] = {mono};
64 Deinterleave(kInterleaved, kSamplesPerChannel, kNumChannels, deinterleaved);
65 ExpectArraysEq(mono, kInterleaved, kSamplesPerChannel);
66
67 int16_t interleaved[kSamplesPerChannel];
68 Interleave(deinterleaved, kSamplesPerChannel, kNumChannels, interleaved);
69 ExpectArraysEq(interleaved, mono, kSamplesPerChannel);
70}
71
72} // namespace webrtc