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 "modules/audio_processing/audio_buffer.h"
14 #include "test/gtest.h"
15
16 namespace webrtc {
TEST(AudioFrameTest,ConstructFromAudioBuffer)17 TEST(AudioFrameTest, ConstructFromAudioBuffer) {
18 constexpr int kSampleRateHz = 48000;
19 constexpr int kNumChannels = 2;
20 constexpr float kFloatConstant = 1272.f;
21 constexpr float kIntConstant = 17252;
22 const webrtc::StreamConfig stream_config(kSampleRateHz, kNumChannels);
23 webrtc::AudioBuffer buffer(
24 stream_config.sample_rate_hz(), stream_config.num_channels(),
25 stream_config.sample_rate_hz(), stream_config.num_channels(),
26 stream_config.sample_rate_hz(), stream_config.num_channels());
27
28 AudioFrameView<float> non_const_view(buffer.channels(), buffer.num_channels(),
29 buffer.num_frames());
30 // Modification is allowed.
31 non_const_view.channel(0)[0] = kFloatConstant;
32 EXPECT_EQ(buffer.channels()[0][0], kFloatConstant);
33
34 AudioFrameView<const float> const_view(
35 buffer.channels(), buffer.num_channels(), buffer.num_frames());
36 // Modification is not allowed.
37 // const_view.channel(0)[0] = kFloatConstant;
38
39 // Assignment is allowed.
40 AudioFrameView<const float> other_const_view = non_const_view;
41 static_cast<void>(other_const_view);
42
43 // But not the other way. The following will fail:
44 // non_const_view = other_const_view;
45
46 AudioFrameView<float> non_const_float_view(
47 buffer.channels(), buffer.num_channels(), buffer.num_frames());
48 non_const_float_view.channel(0)[0] = kIntConstant;
49 EXPECT_EQ(buffer.channels()[0][0], kIntConstant);
50 }
51 } // namespace webrtc
52