blob: f3b2ddc689506e627c4e45f1273044f054c2669f [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>
Artem Titov9dc209a2019-11-28 17:09:30 +010014
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "test/gtest.h"
Artem Titov9dc209a2019-11-28 17:09:30 +010016#include "test/testsupport/rtc_expect_death.h"
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070017
18namespace webrtc {
19
20namespace {
21
Per Åhgrend47941e2019-08-22 11:51:13 +020022const size_t kSampleRateHz = 48000u;
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070023const size_t kStereo = 2u;
24const size_t kMono = 1u;
25
26void ExpectNumChannels(const AudioBuffer& ab, size_t num_channels) {
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070027 EXPECT_EQ(ab.num_channels(), num_channels);
28}
29
30} // namespace
31
32TEST(AudioBufferTest, SetNumChannelsSetsChannelBuffersNumChannels) {
Per Åhgrend47941e2019-08-22 11:51:13 +020033 AudioBuffer ab(kSampleRateHz, kStereo, kSampleRateHz, kStereo, kSampleRateHz,
34 kStereo);
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070035 ExpectNumChannels(ab, kStereo);
Per Åhgrend47941e2019-08-22 11:51:13 +020036 ab.set_num_channels(1);
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070037 ExpectNumChannels(ab, kMono);
Per Åhgrend47941e2019-08-22 11:51:13 +020038 ab.RestoreNumChannels();
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070039 ExpectNumChannels(ab, kStereo);
40}
41
42#if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
Tommia5e07cc2020-05-26 21:40:37 +020043TEST(AudioBufferDeathTest, SetNumChannelsDeathTest) {
Per Åhgrend47941e2019-08-22 11:51:13 +020044 AudioBuffer ab(kSampleRateHz, kMono, kSampleRateHz, kMono, kSampleRateHz,
45 kMono);
Artem Titov9dc209a2019-11-28 17:09:30 +010046 RTC_EXPECT_DEATH(ab.set_num_channels(kStereo), "num_channels");
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070047}
48#endif
49
Gustaf Ullberg422b9e02019-10-09 13:02:14 +020050TEST(AudioBufferTest, CopyWithoutResampling) {
51 AudioBuffer ab1(32000, 2, 32000, 2, 32000, 2);
52 AudioBuffer ab2(32000, 2, 32000, 2, 32000, 2);
53 // Fill first buffer.
54 for (size_t ch = 0; ch < ab1.num_channels(); ++ch) {
55 for (size_t i = 0; i < ab1.num_frames(); ++i) {
56 ab1.channels()[ch][i] = i + ch;
57 }
58 }
59 // Copy to second buffer.
60 ab1.CopyTo(&ab2);
61 // Verify content of second buffer.
62 for (size_t ch = 0; ch < ab2.num_channels(); ++ch) {
63 for (size_t i = 0; i < ab2.num_frames(); ++i) {
64 EXPECT_EQ(ab2.channels()[ch][i], i + ch);
65 }
66 }
67}
68
69TEST(AudioBufferTest, CopyWithResampling) {
70 AudioBuffer ab1(32000, 2, 32000, 2, 48000, 2);
71 AudioBuffer ab2(48000, 2, 48000, 2, 48000, 2);
72 float energy_ab1 = 0.f;
73 float energy_ab2 = 0.f;
74 const float pi = std::acos(-1.f);
75 // Put a sine and compute energy of first buffer.
76 for (size_t ch = 0; ch < ab1.num_channels(); ++ch) {
77 for (size_t i = 0; i < ab1.num_frames(); ++i) {
78 ab1.channels()[ch][i] = std::sin(2 * pi * 100.f / 32000.f * i);
79 energy_ab1 += ab1.channels()[ch][i] * ab1.channels()[ch][i];
80 }
81 }
82 // Copy to second buffer.
83 ab1.CopyTo(&ab2);
84 // Compute energy of second buffer.
85 for (size_t ch = 0; ch < ab2.num_channels(); ++ch) {
86 for (size_t i = 0; i < ab2.num_frames(); ++i) {
87 energy_ab2 += ab2.channels()[ch][i] * ab2.channels()[ch][i];
88 }
89 }
90 // Verify that energies match.
91 EXPECT_NEAR(energy_ab1, energy_ab2 * 32000.f / 48000.f, .01f * energy_ab1);
92}
Alejandro Luebsa181c9a2016-06-30 15:33:37 -070093} // namespace webrtc