xref: /aosp_15_r20/external/webrtc/modules/audio_processing/include/audio_frame_view.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 
16 namespace webrtc {
17 
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.
AudioFrameView(T * const * audio_samples,int num_channels,int channel_size)25   AudioFrameView(T* const* audio_samples, int num_channels, int channel_size)
26       : audio_samples_(audio_samples),
27         num_channels_(num_channels),
28         channel_size_(channel_size) {
29     RTC_DCHECK_GE(num_channels_, 0);
30     RTC_DCHECK_GE(channel_size_, 0);
31   }
32 
33   // Implicit cast to allow converting Frame<float> to
34   // Frame<const float>.
35   template <class U>
AudioFrameView(AudioFrameView<U> other)36   AudioFrameView(AudioFrameView<U> other)
37       : audio_samples_(other.data()),
38         num_channels_(other.num_channels()),
39         channel_size_(other.samples_per_channel()) {}
40 
41   AudioFrameView() = delete;
42 
num_channels()43   int num_channels() const { return num_channels_; }
44 
samples_per_channel()45   int samples_per_channel() const { return channel_size_; }
46 
channel(int idx)47   rtc::ArrayView<T> channel(int idx) {
48     RTC_DCHECK_LE(0, idx);
49     RTC_DCHECK_LE(idx, num_channels_);
50     return rtc::ArrayView<T>(audio_samples_[idx], channel_size_);
51   }
52 
channel(int idx)53   rtc::ArrayView<const T> channel(int idx) const {
54     RTC_DCHECK_LE(0, idx);
55     RTC_DCHECK_LE(idx, num_channels_);
56     return rtc::ArrayView<const T>(audio_samples_[idx], channel_size_);
57   }
58 
data()59   T* const* data() { return audio_samples_; }
60 
61  private:
62   T* const* audio_samples_;
63   int num_channels_;
64   int channel_size_;
65 };
66 }  // namespace webrtc
67 
68 #endif  // MODULES_AUDIO_PROCESSING_INCLUDE_AUDIO_FRAME_VIEW_H_
69