• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 #include "modules/audio_processing/audio_buffer.h"
12 
13 #include <cmath>
14 
15 #include "test/gtest.h"
16 #include "test/testsupport/rtc_expect_death.h"
17 
18 namespace webrtc {
19 
20 namespace {
21 
22 const size_t kSampleRateHz = 48000u;
23 const size_t kStereo = 2u;
24 const size_t kMono = 1u;
25 
ExpectNumChannels(const AudioBuffer & ab,size_t num_channels)26 void ExpectNumChannels(const AudioBuffer& ab, size_t num_channels) {
27   EXPECT_EQ(ab.num_channels(), num_channels);
28 }
29 
30 }  // namespace
31 
TEST(AudioBufferTest,SetNumChannelsSetsChannelBuffersNumChannels)32 TEST(AudioBufferTest, SetNumChannelsSetsChannelBuffersNumChannels) {
33   AudioBuffer ab(kSampleRateHz, kStereo, kSampleRateHz, kStereo, kSampleRateHz,
34                  kStereo);
35   ExpectNumChannels(ab, kStereo);
36   ab.set_num_channels(1);
37   ExpectNumChannels(ab, kMono);
38   ab.RestoreNumChannels();
39   ExpectNumChannels(ab, kStereo);
40 }
41 
42 #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
TEST(AudioBufferDeathTest,SetNumChannelsDeathTest)43 TEST(AudioBufferDeathTest, SetNumChannelsDeathTest) {
44   AudioBuffer ab(kSampleRateHz, kMono, kSampleRateHz, kMono, kSampleRateHz,
45                  kMono);
46   RTC_EXPECT_DEATH(ab.set_num_channels(kStereo), "num_channels");
47 }
48 #endif
49 
TEST(AudioBufferTest,CopyWithoutResampling)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 
TEST(AudioBufferTest,CopyWithResampling)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 }
93 }  // namespace webrtc
94