summaryrefslogtreecommitdiffstats
path: root/media/audio/audio_util_unittest.cc
blob: 11e10a93051766893907b939ea8ee533e3a34fbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/basictypes.h"
#include "media/audio/audio_util.h"
#include "testing/gtest/include/gtest/gtest.h"

// Number of samples in each audio array.
static const size_t kNumberOfSamples = 4;

namespace media {

TEST(AudioUtilTest, AdjustVolume_u8) {
  // Test AdjustVolume() on 8 bit samples.
  uint8 samples_u8[kNumberOfSamples] = { 4, 0x40, 0x80, 0xff };
  uint8 expected_u8[kNumberOfSamples] = { (4 - 128) / 2 + 128,
                                          (0x40 - 128) / 2 + 128,
                                          (0x80 - 128) / 2 + 128,
                                          (0xff - 128) / 2 + 128 };
  bool result_u8 = media::AdjustVolume(samples_u8, sizeof(samples_u8),
                                       1,  // channels.
                                       sizeof(samples_u8[0]),
                                       0.5f);
  EXPECT_TRUE(result_u8);
  int expected_test = memcmp(samples_u8, expected_u8, sizeof(expected_u8));
  EXPECT_EQ(0, expected_test);
}

TEST(AudioUtilTest, AdjustVolume_s16) {
  // Test AdjustVolume() on 16 bit samples.
  int16 samples_s16[kNumberOfSamples] = { -4, 0x40, -32768, 123 };
  int16 expected_s16[kNumberOfSamples] = { -1, 0x10, -8192, 30 };
  bool result_s16 = media::AdjustVolume(samples_s16, sizeof(samples_s16),
                                        2,  // channels.
                                        sizeof(samples_s16[0]),
                                        0.25f);
  EXPECT_TRUE(result_s16);
  int expected_test = memcmp(samples_s16, expected_s16, sizeof(expected_s16));
  EXPECT_EQ(0, expected_test);
}

TEST(AudioUtilTest, AdjustVolume_s16_zero) {
  // Test AdjustVolume() on 16 bit samples.
  int16 samples_s16[kNumberOfSamples] = { -4, 0x40, -32768, 123 };
  int16 expected_s16[kNumberOfSamples] = { 0, 0, 0, 0 };
  bool result_s16 = media::AdjustVolume(samples_s16, sizeof(samples_s16),
                                        2,  // channels.
                                        sizeof(samples_s16[0]),
                                        0.0f);
  EXPECT_TRUE(result_s16);
  int expected_test = memcmp(samples_s16, expected_s16, sizeof(expected_s16));
  EXPECT_EQ(0, expected_test);
}

TEST(AudioUtilTest, AdjustVolume_s16_one) {
  // Test AdjustVolume() on 16 bit samples.
  int16 samples_s16[kNumberOfSamples] = { -4, 0x40, -32768, 123 };
  int16 expected_s16[kNumberOfSamples] = { -4, 0x40, -32768, 123 };
  bool result_s16 = media::AdjustVolume(samples_s16, sizeof(samples_s16),
                                        2,  // channels.
                                        sizeof(samples_s16[0]),
                                        1.0f);
  EXPECT_TRUE(result_s16);
  int expected_test = memcmp(samples_s16, expected_s16, sizeof(expected_s16));
  EXPECT_EQ(0, expected_test);
}

TEST(AudioUtilTest, AdjustVolume_s32) {
  // Test AdjustVolume() on 32 bit samples.
  int32 samples_s32[kNumberOfSamples] = { -4, 0x40, -32768, 123 };
  int32 expected_s32[kNumberOfSamples] = { -1, 0x10, -8192, 30 };
  bool result_s32 = media::AdjustVolume(samples_s32, sizeof(samples_s32),
                                        4,  // channels.
                                        sizeof(samples_s32[0]),
                                        0.25f);
  EXPECT_TRUE(result_s32);
  int expected_test = memcmp(samples_s32, expected_s32, sizeof(expected_s32));
  EXPECT_EQ(0, expected_test);
}

TEST(AudioUtilTest, FoldChannels_u8) {
  // Test FoldChannels() on 8 bit samples.
  uint8 samples_u8[6] = { 100, 150, 130, 70, 130, 170 };
  uint8 expected_u8[2] = { static_cast<uint8>((130 - 128) * 0.707 +
                                              (100 - 128) + 128),
                           static_cast<uint8>((130 - 128) * 0.707 +
                                              (150 - 128) + 128) };
  bool result_u8 = media::FoldChannels(samples_u8, sizeof(samples_u8),
                                        6,  // channels.
                                        sizeof(samples_u8[0]),
                                        1.0f);
  EXPECT_TRUE(result_u8);
  int expected_test = memcmp(samples_u8, expected_u8, sizeof(expected_u8));
  EXPECT_EQ(0, expected_test);
}

TEST(AudioUtilTest, FoldChannels_s16) {
  // Test FoldChannels() on 16 bit samples.
  int16 samples_s16[6] = { 1, 3, 12, 7, 13, 17 };
  int16 expected_s16[2] = { static_cast<int16>(12 * .707 + 1),
                            static_cast<int16>(12 * .707 + 3) };
  bool result_s16 = media::FoldChannels(samples_s16, sizeof(samples_s16),
                                        6,  // channels.
                                        sizeof(samples_s16[0]),
                                        1.00f);
  EXPECT_TRUE(result_s16);
  int expected_test = memcmp(samples_s16, expected_s16, sizeof(expected_s16));
  EXPECT_EQ(0, expected_test);
}

TEST(AudioUtilTest, FoldChannels_s32) {
  // Test FoldChannels() on 32 bit samples.
  int32 samples_s32[6] = { 1, 3, 12, 7, 13, 17 };
  int32 expected_s32[2] = { static_cast<int16>(12 * .707 + 1),
                            static_cast<int16>(12 * .707 + 3) };
  bool result_s32 = media::FoldChannels(samples_s32, sizeof(samples_s32),
                                        6,  // channels.
                                        sizeof(samples_s32[0]),
                                        1.00f);
  EXPECT_TRUE(result_s32);
  int expected_test = memcmp(samples_s32, expected_s32, sizeof(expected_s32));
  EXPECT_EQ(0, expected_test);
}

// Fold 7.1
TEST(AudioUtilTest, FoldChannels71_s16) {
  // Test FoldChannels() on 16 bit samples.
  int16 samples_s16[8] = { 1, 3, 12, 7, 13, 17, 30, 40 };
  int16 expected_s16[2] = { static_cast<int16>(12 * .707 + 1),
                            static_cast<int16>(12 * .707 + 3) };
  bool result_s16 = media::FoldChannels(samples_s16, sizeof(samples_s16),
                                        8,  // channels.
                                        sizeof(samples_s16[0]),
                                        1.00f);
  EXPECT_TRUE(result_s16);
  int expected_test = memcmp(samples_s16, expected_s16, sizeof(expected_s16));
  EXPECT_EQ(0, expected_test);
}

}  // namespace media