audio_frame_view.h (2674B)
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/audio/audio_view.h" 15 #include "rtc_base/checks.h" 16 17 namespace webrtc { 18 19 // Class to pass audio data in T** format, where T is a numeric type. 20 template <class T> 21 class AudioFrameView { 22 public: 23 // `num_channels` and `channel_size` describe the T** 24 // `audio_samples`. `audio_samples` is assumed to point to a 25 // two-dimensional |num_channels * channel_size| array of floats. 26 // 27 // Note: The implementation now only requires the first channel pointer. 28 // The previous implementation retained a pointer to externally owned array 29 // of channel pointers, but since the channel size and count are provided 30 // and the array is assumed to be a single two-dimensional array, the other 31 // channel pointers can be calculated based on that (which is what the class 32 // now uses `DeinterleavedView<>` internally for). 33 AudioFrameView(T* const* audio_samples, int num_channels, int channel_size) 34 : view_(num_channels && channel_size ? audio_samples : nullptr, 35 channel_size, 36 num_channels) { 37 RTC_DCHECK_GE(view_.num_channels(), 0); 38 RTC_DCHECK_GE(view_.samples_per_channel(), 0); 39 } 40 41 // Implicit cast to allow converting AudioFrameView<float> to 42 // AudioFrameView<const float>. 43 template <class U> 44 AudioFrameView(AudioFrameView<U> other) : view_(other.view()) {} 45 46 // Allow constructing AudioFrameView from a DeinterleavedView. 47 template <class U> 48 explicit AudioFrameView(DeinterleavedView<U> view) : view_(view) {} 49 50 AudioFrameView() = delete; 51 52 int num_channels() const { return view_.num_channels(); } 53 int samples_per_channel() const { return view_.samples_per_channel(); } 54 MonoView<T> channel(int idx) { return view_[idx]; } 55 MonoView<const T> channel(int idx) const { return view_[idx]; } 56 MonoView<T> operator[](int idx) { return view_[idx]; } 57 MonoView<const T> operator[](int idx) const { return view_[idx]; } 58 59 DeinterleavedView<T> view() { return view_; } 60 DeinterleavedView<const T> view() const { return view_; } 61 62 private: 63 DeinterleavedView<T> view_; 64 }; 65 } // namespace webrtc 66 67 #endif // MODULES_AUDIO_PROCESSING_INCLUDE_AUDIO_FRAME_VIEW_H_