Alex Loiko | e36e8bb | 2018-02-16 11:54:07 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 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 | #ifndef MODULES_AUDIO_PROCESSING_INCLUDE_AUDIO_FRAME_VIEW_H_ |
| 12 | #define MODULES_AUDIO_PROCESSING_INCLUDE_AUDIO_FRAME_VIEW_H_ |
| 13 | |
| 14 | #include "api/array_view.h" |
| 15 | |
Alex Loiko | f55babc | 2018-05-14 12:39:32 +0200 | [diff] [blame] | 16 | namespace webrtc { |
| 17 | |
Alex Loiko | e36e8bb | 2018-02-16 11:54:07 +0100 | [diff] [blame] | 18 | // Class to pass audio data in T** format, where T is a numeric type. |
| 19 | template <class T> |
| 20 | class AudioFrameView { |
| 21 | public: |
| 22 | // |num_channels| and |channel_size| describe the T** |
| 23 | // |audio_samples|. |audio_samples| is assumed to point to a |
| 24 | // two-dimensional |num_channels * channel_size| array of floats. |
| 25 | AudioFrameView(T* const* audio_samples, |
| 26 | size_t num_channels, |
| 27 | size_t channel_size) |
| 28 | : audio_samples_(audio_samples), |
| 29 | num_channels_(num_channels), |
| 30 | channel_size_(channel_size) {} |
| 31 | |
Alex Loiko | 153f11e | 2018-02-16 12:39:00 +0100 | [diff] [blame] | 32 | // Implicit cast to allow converting Frame<float> to |
| 33 | // Frame<const float>. |
| 34 | template <class U> |
| 35 | AudioFrameView(AudioFrameView<U> other) |
| 36 | : audio_samples_(other.data()), |
| 37 | num_channels_(other.num_channels()), |
| 38 | channel_size_(other.samples_per_channel()) {} |
| 39 | |
Alex Loiko | e36e8bb | 2018-02-16 11:54:07 +0100 | [diff] [blame] | 40 | AudioFrameView() = delete; |
| 41 | |
| 42 | size_t num_channels() const { return num_channels_; } |
| 43 | |
| 44 | size_t samples_per_channel() const { return channel_size_; } |
| 45 | |
| 46 | rtc::ArrayView<T> channel(size_t idx) { |
| 47 | RTC_DCHECK_LE(0, idx); |
| 48 | RTC_DCHECK_LE(idx, num_channels_); |
| 49 | return rtc::ArrayView<T>(audio_samples_[idx], channel_size_); |
| 50 | } |
| 51 | |
| 52 | rtc::ArrayView<const T> channel(size_t idx) const { |
| 53 | RTC_DCHECK_LE(0, idx); |
| 54 | RTC_DCHECK_LE(idx, num_channels_); |
| 55 | return rtc::ArrayView<const T>(audio_samples_[idx], channel_size_); |
| 56 | } |
| 57 | |
Alex Loiko | 153f11e | 2018-02-16 12:39:00 +0100 | [diff] [blame] | 58 | T* const* data() { return audio_samples_; } |
| 59 | |
Alex Loiko | e36e8bb | 2018-02-16 11:54:07 +0100 | [diff] [blame] | 60 | private: |
| 61 | T* const* audio_samples_; |
| 62 | size_t num_channels_; |
| 63 | size_t channel_size_; |
| 64 | }; |
Alex Loiko | f55babc | 2018-05-14 12:39:32 +0200 | [diff] [blame] | 65 | } // namespace webrtc |
Alex Loiko | e36e8bb | 2018-02-16 11:54:07 +0100 | [diff] [blame] | 66 | |
| 67 | #endif // MODULES_AUDIO_PROCESSING_INCLUDE_AUDIO_FRAME_VIEW_H_ |