blob: 164784a7ccab53ca17a2e6638b3403bdab4bb179 [file] [log] [blame]
Alex Loikoe36e8bb2018-02-16 11:54:07 +01001/*
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 Loikof55babc2018-05-14 12:39:32 +020016namespace webrtc {
17
Alex Loikoe36e8bb2018-02-16 11:54:07 +010018// Class to pass audio data in T** format, where T is a numeric type.
19template <class T>
20class AudioFrameView {
21 public:
Artem Titov0b489302021-07-28 20:50:03 +020022 // `num_channels` and `channel_size` describe the T**
23 // `audio_samples`. `audio_samples` is assumed to point to a
Alex Loikoe36e8bb2018-02-16 11:54:07 +010024 // two-dimensional |num_channels * channel_size| array of floats.
Alessio Bazzica5c3ae492021-10-07 14:08:59 +020025 AudioFrameView(T* const* audio_samples, int num_channels, int channel_size)
Alex Loikoe36e8bb2018-02-16 11:54:07 +010026 : audio_samples_(audio_samples),
27 num_channels_(num_channels),
Alessio Bazzica5c3ae492021-10-07 14:08:59 +020028 channel_size_(channel_size) {
29 RTC_DCHECK_GE(num_channels_, 0);
30 RTC_DCHECK_GE(channel_size_, 0);
31 }
Alex Loikoe36e8bb2018-02-16 11:54:07 +010032
Alex Loiko153f11e2018-02-16 12:39:00 +010033 // Implicit cast to allow converting Frame<float> to
34 // Frame<const float>.
35 template <class U>
36 AudioFrameView(AudioFrameView<U> other)
37 : audio_samples_(other.data()),
38 num_channels_(other.num_channels()),
39 channel_size_(other.samples_per_channel()) {}
40
Alex Loikoe36e8bb2018-02-16 11:54:07 +010041 AudioFrameView() = delete;
42
Alessio Bazzica5c3ae492021-10-07 14:08:59 +020043 int num_channels() const { return num_channels_; }
Alex Loikoe36e8bb2018-02-16 11:54:07 +010044
Alessio Bazzica5c3ae492021-10-07 14:08:59 +020045 int samples_per_channel() const { return channel_size_; }
Alex Loikoe36e8bb2018-02-16 11:54:07 +010046
Alessio Bazzica5c3ae492021-10-07 14:08:59 +020047 rtc::ArrayView<T> channel(int idx) {
Alex Loikoe36e8bb2018-02-16 11:54:07 +010048 RTC_DCHECK_LE(0, idx);
49 RTC_DCHECK_LE(idx, num_channels_);
50 return rtc::ArrayView<T>(audio_samples_[idx], channel_size_);
51 }
52
Alessio Bazzica5c3ae492021-10-07 14:08:59 +020053 rtc::ArrayView<const T> channel(int idx) const {
Alex Loikoe36e8bb2018-02-16 11:54:07 +010054 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
Alex Loiko153f11e2018-02-16 12:39:00 +010059 T* const* data() { return audio_samples_; }
60
Alex Loikoe36e8bb2018-02-16 11:54:07 +010061 private:
62 T* const* audio_samples_;
Alessio Bazzica5c3ae492021-10-07 14:08:59 +020063 int num_channels_;
64 int channel_size_;
Alex Loikoe36e8bb2018-02-16 11:54:07 +010065};
Alex Loikof55babc2018-05-14 12:39:32 +020066} // namespace webrtc
Alex Loikoe36e8bb2018-02-16 11:54:07 +010067
68#endif // MODULES_AUDIO_PROCESSING_INCLUDE_AUDIO_FRAME_VIEW_H_