audio_frame_view_unittest.cc (3492B)
1 /* 2 * Copyright 2018 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/include/audio_frame_view.h" 12 13 #include <array> 14 #include <cstddef> 15 16 #include "api/audio/audio_processing.h" 17 #include "api/audio/audio_view.h" 18 #include "common_audio/channel_buffer.h" 19 #include "modules/audio_processing/audio_buffer.h" 20 #include "test/gtest.h" 21 22 namespace webrtc { 23 TEST(AudioFrameTest, ConstructFromAudioBuffer) { 24 constexpr int kSampleRateHz = 48000; 25 constexpr int kNumChannels = 2; 26 constexpr float kFloatConstant = 1272.f; 27 constexpr float kIntConstant = 17252; 28 const StreamConfig stream_config(kSampleRateHz, kNumChannels); 29 AudioBuffer buffer( 30 stream_config.sample_rate_hz(), stream_config.num_channels(), 31 stream_config.sample_rate_hz(), stream_config.num_channels(), 32 stream_config.sample_rate_hz(), stream_config.num_channels()); 33 34 AudioFrameView<float> non_const_view(buffer.channels(), buffer.num_channels(), 35 buffer.num_frames()); 36 // Modification is allowed. 37 non_const_view.channel(0)[0] = kFloatConstant; 38 EXPECT_EQ(buffer.channels()[0][0], kFloatConstant); 39 40 AudioFrameView<const float> const_view( 41 buffer.channels(), buffer.num_channels(), buffer.num_frames()); 42 // Modification is not allowed. 43 // const_view.channel(0)[0] = kFloatConstant; 44 45 // Assignment is allowed. 46 AudioFrameView<const float> other_const_view = non_const_view; 47 static_cast<void>(other_const_view); 48 49 // But not the other way. The following will fail: 50 // non_const_view = other_const_view; 51 52 AudioFrameView<float> non_const_float_view( 53 buffer.channels(), buffer.num_channels(), buffer.num_frames()); 54 non_const_float_view.channel(0)[0] = kIntConstant; 55 EXPECT_EQ(buffer.channels()[0][0], kIntConstant); 56 } 57 58 TEST(AudioFrameTest, ConstructFromChannelBuffer) { 59 ChannelBuffer<float> buffer(480, 2); 60 AudioFrameView<float> view(buffer.channels(), buffer.num_channels(), 61 buffer.num_frames()); 62 EXPECT_EQ(view.num_channels(), 2); 63 EXPECT_EQ(view.samples_per_channel(), 480); 64 } 65 66 TEST(AudioFrameTest, ToDeinterleavedView) { 67 ChannelBuffer<float> buffer(480, 2); 68 AudioFrameView<float> view(buffer.channels(), buffer.num_channels(), 69 buffer.num_frames()); 70 71 DeinterleavedView<float> non_const_view = view.view(); 72 DeinterleavedView<const float> const_view = 73 static_cast<const AudioFrameView<float>&>(view).view(); 74 75 ASSERT_EQ(non_const_view.num_channels(), 2u); 76 ASSERT_EQ(const_view.num_channels(), 2u); 77 for (size_t i = 0; i < non_const_view.num_channels(); ++i) { 78 EXPECT_EQ(non_const_view[i].data(), const_view[i].data()); 79 EXPECT_EQ(non_const_view[i].data(), view.channel(i).data()); 80 } 81 } 82 83 TEST(AudioFrameTest, FromDeinterleavedView) { 84 std::array<float, 480 * 2> buffer; 85 DeinterleavedView<float> view(buffer.data(), buffer.size() / 2u, 2u); 86 AudioFrameView<float> frame_view(view); 87 EXPECT_EQ(static_cast<size_t>(frame_view.num_channels()), 88 view.num_channels()); 89 EXPECT_EQ(frame_view[0], view[0]); 90 EXPECT_EQ(frame_view[1], view[1]); 91 } 92 93 } // namespace webrtc