blob: a4ad4ccd8c0d56f6f09e408844b0c4be0fb4ec61 [file] [log] [blame]
Alex Loiko153f11e2018-02-16 12:39:00 +01001/*
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
Alex Loikof55babc2018-05-14 12:39:32 +020016namespace webrtc {
Alex Loiko153f11e2018-02-16 12:39:00 +010017TEST(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, false);
23 webrtc::AudioBuffer buffer(
Per Åhgrend47941e2019-08-22 11:51:13 +020024 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());
Alex Loiko153f11e2018-02-16 12:39:00 +010027
Per Åhgrend47941e2019-08-22 11:51:13 +020028 AudioFrameView<float> non_const_view(buffer.channels(), buffer.num_channels(),
29 buffer.num_frames());
Alex Loiko153f11e2018-02-16 12:39:00 +010030 // Modification is allowed.
31 non_const_view.channel(0)[0] = kFloatConstant;
Per Åhgrend47941e2019-08-22 11:51:13 +020032 EXPECT_EQ(buffer.channels()[0][0], kFloatConstant);
Alex Loiko153f11e2018-02-16 12:39:00 +010033
34 AudioFrameView<const float> const_view(
Per Åhgrend47941e2019-08-22 11:51:13 +020035 buffer.channels(), buffer.num_channels(), buffer.num_frames());
Alex Loiko153f11e2018-02-16 12:39:00 +010036 // 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
Alex Loikoa0262da2018-02-16 13:58:45 +010043 // But not the other way. The following will fail:
Alex Loiko153f11e2018-02-16 12:39:00 +010044 // non_const_view = other_const_view;
45
Per Åhgren928146f2019-08-20 09:19:21 +020046 AudioFrameView<float> non_const_float_view(
Per Åhgrend47941e2019-08-22 11:51:13 +020047 buffer.channels(), buffer.num_channels(), buffer.num_frames());
Per Åhgren928146f2019-08-20 09:19:21 +020048 non_const_float_view.channel(0)[0] = kIntConstant;
Per Åhgrend47941e2019-08-22 11:51:13 +020049 EXPECT_EQ(buffer.channels()[0][0], kIntConstant);
Alex Loiko153f11e2018-02-16 12:39:00 +010050}
Alex Loikof55babc2018-05-14 12:39:32 +020051} // namespace webrtc