blob: 402e5c4065a6c38c2172b41c2e1d30b2cec68d93 [file] [log] [blame]
Alejandro Luebsa181c9a2016-06-30 15:33:37 -07001/*
2 * Copyright (c) 2016 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/audio_buffer.h"
Jonas Olssona4d87372019-07-05 19:08:33 +020012
Gustaf Ullberg422b9e02019-10-09 13:02:14 +020013#include <cmath>
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "test/gtest.h"
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070015
16namespace webrtc {
17
18namespace {
19
Per Åhgrend47941e2019-08-22 11:51:13 +020020const size_t kSampleRateHz = 48000u;
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070021const size_t kStereo = 2u;
22const size_t kMono = 1u;
23
24void ExpectNumChannels(const AudioBuffer& ab, size_t num_channels) {
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070025 EXPECT_EQ(ab.num_channels(), num_channels);
26}
27
28} // namespace
29
30TEST(AudioBufferTest, SetNumChannelsSetsChannelBuffersNumChannels) {
Per Åhgrend47941e2019-08-22 11:51:13 +020031 AudioBuffer ab(kSampleRateHz, kStereo, kSampleRateHz, kStereo, kSampleRateHz,
32 kStereo);
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070033 ExpectNumChannels(ab, kStereo);
Per Åhgrend47941e2019-08-22 11:51:13 +020034 ab.set_num_channels(1);
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070035 ExpectNumChannels(ab, kMono);
Per Åhgrend47941e2019-08-22 11:51:13 +020036 ab.RestoreNumChannels();
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070037 ExpectNumChannels(ab, kStereo);
38}
39
40#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
41TEST(AudioBufferTest, SetNumChannelsDeathTest) {
Per Åhgrend47941e2019-08-22 11:51:13 +020042 AudioBuffer ab(kSampleRateHz, kMono, kSampleRateHz, kMono, kSampleRateHz,
43 kMono);
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070044 EXPECT_DEATH(ab.set_num_channels(kStereo), "num_channels");
45}
46#endif
47
Gustaf Ullberg422b9e02019-10-09 13:02:14 +020048TEST(AudioBufferTest, CopyWithoutResampling) {
49 AudioBuffer ab1(32000, 2, 32000, 2, 32000, 2);
50 AudioBuffer ab2(32000, 2, 32000, 2, 32000, 2);
51 // Fill first buffer.
52 for (size_t ch = 0; ch < ab1.num_channels(); ++ch) {
53 for (size_t i = 0; i < ab1.num_frames(); ++i) {
54 ab1.channels()[ch][i] = i + ch;
55 }
56 }
57 // Copy to second buffer.
58 ab1.CopyTo(&ab2);
59 // Verify content of second buffer.
60 for (size_t ch = 0; ch < ab2.num_channels(); ++ch) {
61 for (size_t i = 0; i < ab2.num_frames(); ++i) {
62 EXPECT_EQ(ab2.channels()[ch][i], i + ch);
63 }
64 }
65}
66
67TEST(AudioBufferTest, CopyWithResampling) {
68 AudioBuffer ab1(32000, 2, 32000, 2, 48000, 2);
69 AudioBuffer ab2(48000, 2, 48000, 2, 48000, 2);
70 float energy_ab1 = 0.f;
71 float energy_ab2 = 0.f;
72 const float pi = std::acos(-1.f);
73 // Put a sine and compute energy of first buffer.
74 for (size_t ch = 0; ch < ab1.num_channels(); ++ch) {
75 for (size_t i = 0; i < ab1.num_frames(); ++i) {
76 ab1.channels()[ch][i] = std::sin(2 * pi * 100.f / 32000.f * i);
77 energy_ab1 += ab1.channels()[ch][i] * ab1.channels()[ch][i];
78 }
79 }
80 // Copy to second buffer.
81 ab1.CopyTo(&ab2);
82 // Compute energy of second buffer.
83 for (size_t ch = 0; ch < ab2.num_channels(); ++ch) {
84 for (size_t i = 0; i < ab2.num_frames(); ++i) {
85 energy_ab2 += ab2.channels()[ch][i] * ab2.channels()[ch][i];
86 }
87 }
88 // Verify that energies match.
89 EXPECT_NEAR(energy_ab1, energy_ab2 * 32000.f / 48000.f, .01f * energy_ab1);
90}
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070091} // namespace webrtc