1/*
2Copyright 2018 Google Inc. All Rights Reserved.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS-IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef RESONANCE_AUDIO_BASE_CHANNEL_VIEW_H_
18#define RESONANCE_AUDIO_BASE_CHANNEL_VIEW_H_
19
20#include <algorithm>
21#include <cstring>
22#include <vector>
23
24#include "base/logging.h"
25
26namespace vraudio {
27
28// Provides an interface to a single audio channel in |AudioBuffer|. Note that a
29// |ChannelView| instance does not own the data it is initialized with.
30class ChannelView {
31 public:
32 // Array subscript operator returning a reference.
33 float& operator[](size_t index) {
34 DCHECK(enabled_);
35 DCHECK_LT(index, size_);
36 return *(begin() + index);
37 }
38
39 // Const array subscript operator returning a const reference.
40 const float& operator[](size_t index) const {
41 DCHECK(enabled_);
42 DCHECK_LT(index, size_);
43 return *(begin() + index);
44 }
45
46 // Returns the size of the channel in samples.
47 size_t size() const { return size_; }
48
49 // Returns a float pointer to the begin of the channel data.
50 float* begin() {
51 DCHECK(enabled_);
52 return begin_itr_;
53 }
54
55 // Returns a float pointer to the end of the channel data.
56 float* end() {
57 DCHECK(enabled_);
58 return begin_itr_ + size_;
59 }
60
61 // Returns a const float pointer to the begin of the channel data.
62 const float* begin() const {
63 DCHECK(enabled_);
64 return begin_itr_;
65 }
66
67 // Returns a const float pointer to the end of the channel data.
68 const float* end() const {
69 DCHECK(enabled_);
70 return begin_itr_ + size_;
71 }
72
73 // Copy assignment from float vector.
74 ChannelView& operator=(const std::vector<float>& other) {
75 DCHECK(enabled_);
76 DCHECK_EQ(other.size(), size_);
77 memcpy(dest: begin(), src: other.data(), n: sizeof(float) * size_);
78 return *this;
79 }
80
81 // Copy assignment from ChannelView.
82 ChannelView& operator=(const ChannelView& other) {
83 if (this != &other) {
84 DCHECK(enabled_);
85 DCHECK_EQ(other.size(), size_);
86 memcpy(dest: begin(), src: other.begin(), n: sizeof(float) * size_);
87 }
88 return *this;
89 }
90
91 // Adds a |ChannelView| to this |ChannelView|.
92 ChannelView& operator+=(const ChannelView& other);
93
94 // Subtracts a |ChannelView| from this |ChannelView|.
95 ChannelView& operator-=(const ChannelView& other);
96
97 // Pointwise multiplies a |ChannelView| with this |Channelview|.
98 ChannelView& operator*=(const ChannelView& other);
99
100 // Fills channel buffer with zeros.
101 void Clear() {
102 DCHECK(enabled_);
103 memset(s: begin(), c: 0, n: sizeof(float) * size_);
104 }
105
106 // Allows for disabling the channel to prevent access to the channel data and
107 // channel iterators. It is used in the |Mixer| class to prevent the copies of
108 // silence |ChannelView|s. Note that |ChannelView| are enabled by default.
109 //
110 // @param enabled True to enable the channel.
111 void SetEnabled(bool enabled) { enabled_ = enabled; }
112
113 // Returns true if |ChannelView| is enabled.
114 //
115 // @return State of |enabled_| flag.
116 bool IsEnabled() const { return enabled_; }
117
118 private:
119 friend class AudioBuffer;
120
121 // Constructor is initialized with a float pointer to the first sample and the
122 // size of chunk of planar channel data.
123 ChannelView(float* begin_itr, size_t size)
124 : begin_itr_(begin_itr), size_(size), enabled_(true) {}
125
126 // Iterator of first and last element in channel.
127 float* const begin_itr_;
128
129 // Channel size.
130 const size_t size_;
131
132 // Flag indicating if the channel is enabled.
133 bool enabled_;
134};
135
136} // namespace vraudio
137
138#endif // RESONANCE_AUDIO_BASE_CHANNEL_VIEW_H_
139

source code of qtmultimedia/src/3rdparty/resonance-audio/resonance_audio/base/channel_view.h