Alex Loiko | 153f11e | 2018-02-16 12:39:00 +0100 | [diff] [blame] | 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 | |
Alex Loiko | f55babc | 2018-05-14 12:39:32 +0200 | [diff] [blame] | 16 | namespace webrtc { |
Alex Loiko | 153f11e | 2018-02-16 12:39:00 +0100 | [diff] [blame] | 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, false); |
| 23 | webrtc::AudioBuffer buffer( |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 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()); |
Alex Loiko | 153f11e | 2018-02-16 12:39:00 +0100 | [diff] [blame] | 27 | |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 28 | AudioFrameView<float> non_const_view(buffer.channels(), buffer.num_channels(), |
| 29 | buffer.num_frames()); |
Alex Loiko | 153f11e | 2018-02-16 12:39:00 +0100 | [diff] [blame] | 30 | // Modification is allowed. |
| 31 | non_const_view.channel(0)[0] = kFloatConstant; |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 32 | EXPECT_EQ(buffer.channels()[0][0], kFloatConstant); |
Alex Loiko | 153f11e | 2018-02-16 12:39:00 +0100 | [diff] [blame] | 33 | |
| 34 | AudioFrameView<const float> const_view( |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 35 | buffer.channels(), buffer.num_channels(), buffer.num_frames()); |
Alex Loiko | 153f11e | 2018-02-16 12:39:00 +0100 | [diff] [blame] | 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 | |
Alex Loiko | a0262da | 2018-02-16 13:58:45 +0100 | [diff] [blame] | 43 | // But not the other way. The following will fail: |
Alex Loiko | 153f11e | 2018-02-16 12:39:00 +0100 | [diff] [blame] | 44 | // non_const_view = other_const_view; |
| 45 | |
Per Åhgren | 928146f | 2019-08-20 09:19:21 +0200 | [diff] [blame] | 46 | AudioFrameView<float> non_const_float_view( |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 47 | buffer.channels(), buffer.num_channels(), buffer.num_frames()); |
Per Åhgren | 928146f | 2019-08-20 09:19:21 +0200 | [diff] [blame] | 48 | non_const_float_view.channel(0)[0] = kIntConstant; |
Per Åhgren | d47941e | 2019-08-22 11:51:13 +0200 | [diff] [blame] | 49 | EXPECT_EQ(buffer.channels()[0][0], kIntConstant); |
Alex Loiko | 153f11e | 2018-02-16 12:39:00 +0100 | [diff] [blame] | 50 | } |
Alex Loiko | f55babc | 2018-05-14 12:39:32 +0200 | [diff] [blame] | 51 | } // namespace webrtc |