Alejandro Luebs | a181c9a | 2016-06-30 15:33:37 -0700 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_processing/audio_buffer.h" |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 12 | |
Gustaf Ullberg | 422b9e0 | 2019-10-09 13:02:14 +0200 | [diff] [blame] | 13 | #include <cmath> |
Artem Titov | 9dc209a | 2019-11-28 17:09:30 +0100 | [diff] [blame] | 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "test/gtest.h" |
Artem Titov | 9dc209a | 2019-11-28 17:09:30 +0100 | [diff] [blame] | 16 | #include "test/testsupport/rtc_expect_death.h" |
Alejandro Luebs | a181c9a | 2016-06-30 15:33:37 -0700 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | |
| 20 | namespace { |
| 21 | |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 22 | const size_t kSampleRateHz = 48000u; |
Alejandro Luebs | a181c9a | 2016-06-30 15:33:37 -0700 | [diff] [blame] | 23 | const size_t kStereo = 2u; |
| 24 | const size_t kMono = 1u; |
| 25 | |
| 26 | void ExpectNumChannels(const AudioBuffer& ab, size_t num_channels) { |
Alejandro Luebs | a181c9a | 2016-06-30 15:33:37 -0700 | [diff] [blame] | 27 | EXPECT_EQ(ab.num_channels(), num_channels); |
| 28 | } |
| 29 | |
| 30 | } // namespace |
| 31 | |
| 32 | TEST(AudioBufferTest, SetNumChannelsSetsChannelBuffersNumChannels) { |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 33 | AudioBuffer ab(kSampleRateHz, kStereo, kSampleRateHz, kStereo, kSampleRateHz, |
| 34 | kStereo); |
Alejandro Luebs | a181c9a | 2016-06-30 15:33:37 -0700 | [diff] [blame] | 35 | ExpectNumChannels(ab, kStereo); |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 36 | ab.set_num_channels(1); |
Alejandro Luebs | a181c9a | 2016-06-30 15:33:37 -0700 | [diff] [blame] | 37 | ExpectNumChannels(ab, kMono); |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 38 | ab.RestoreNumChannels(); |
Alejandro Luebs | a181c9a | 2016-06-30 15:33:37 -0700 | [diff] [blame] | 39 | ExpectNumChannels(ab, kStereo); |
| 40 | } |
| 41 | |
| 42 | #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
Tommi | a5e07cc | 2020-05-26 21:40:37 +0200 | [diff] [blame] | 43 | TEST(AudioBufferDeathTest, SetNumChannelsDeathTest) { |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 44 | AudioBuffer ab(kSampleRateHz, kMono, kSampleRateHz, kMono, kSampleRateHz, |
| 45 | kMono); |
Artem Titov | 9dc209a | 2019-11-28 17:09:30 +0100 | [diff] [blame] | 46 | RTC_EXPECT_DEATH(ab.set_num_channels(kStereo), "num_channels"); |
Alejandro Luebs | a181c9a | 2016-06-30 15:33:37 -0700 | [diff] [blame] | 47 | } |
| 48 | #endif |
| 49 | |
Gustaf Ullberg | 422b9e0 | 2019-10-09 13:02:14 +0200 | [diff] [blame] | 50 | TEST(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 | |
| 69 | TEST(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 Luebs | a181c9a | 2016-06-30 15:33:37 -0700 | [diff] [blame] | 93 | } // namespace webrtc |