55 if (lhs.num_channels_ != rhs.num_channels_ || lhs.num_frames_ != rhs.num_frames_) {
58 for (
size_t ch = 0; ch < lhs.num_channels_; ++ch) {
59 if (!std::equal(lhs.channels_[ch], lhs.channels_[ch] + lhs.num_frames_, rhs.channels_[ch])) {
99 return channels_[channel_index];
109 RAV_ASSERT_DEBUG(channel_index < num_channels_,
"Channel index out of bounds");
110 return channels_[channel_index];
135 void set_sample(
size_t channel_index,
size_t frame_index, T value) {
136 RAV_ASSERT_DEBUG(channel_index < num_channels_,
"Channel index out of bounds");
138 channels_[channel_index][frame_index] = value;
145 for (
size_t i = 0; i < num_channels_; ++i) {
146 clear_audio_data(channels_[i], channels_[i] + num_frames_);
155 if (channels_ ==
nullptr) {
158 for (
size_t i = 0; i < num_channels_; ++i) {
159 std::fill(channels_[i], channels_[i] + num_frames_, value);
169 void clear(
const size_t channel_index,
const size_t start_sample,
const size_t num_samples_to_clear) {
172 clear_audio_data(channels_[channel_index] + start_sample, channels_[channel_index] + start_sample + num_samples_to_clear);
185 const size_t dst_start_frame,
const size_t num_frames_to_copy,
const T*
const* src,
const size_t src_num_channels,
186 const size_t src_start_frame = 0
189 for (
size_t i = 0; i < std::min(src_num_channels,
num_channels()); ++i) {
190 copy_from(i, dst_start_frame, num_frames_to_copy, src[i] + src_start_frame);
201 void copy_from(
const size_t dst_channel_index,
const size_t dst_start_sample,
const size_t num_samples_to_copy,
const T* src) {
205 if (num_samples_to_copy == 0) {
209 std::memcpy(channels_[dst_channel_index] + dst_start_sample, src, num_samples_to_copy *
sizeof(T));
221 const size_t src_start_frame,
const size_t num_frames, T*
const* dst,
const size_t dst_num_channels,
222 const size_t dst_start_frame = 0
225 for (
size_t i = 0; i < std::min(
num_channels(), dst_num_channels); ++i) {
237 void copy_to(
const size_t src_channel_index,
const size_t src_start_sample,
const size_t num_samples_to_copy, T* dst) {
241 if (num_samples_to_copy == 0) {
245 std::memcpy(dst, channels_[src_channel_index] + src_start_sample, num_samples_to_copy *
sizeof(T));
254 static_assert(std::is_floating_point_v<T>,
"Not supported for integer types");
256 if (num_channels_ != other.num_channels_ || num_frames_ != other.num_frames_) {
260 for (
size_t ch = 0; ch < num_channels_; ++ch) {
261 for (
size_t frame = 0; frame < num_frames_; ++frame) {
262 channels_[ch][frame] += other.channels_[ch][frame];
274 return channels_ !=
nullptr && num_channels_ > 0 && num_frames_ > 0;
281 std::remove_const_t<T> max_value = channels_[0][0];
282 for (
size_t ch = 0; ch < num_channels_; ++ch) {
283 for (
size_t frame = 0; frame < num_frames_; ++frame) {
284 max_value = std::max(max_value, std::fabs(channels_[ch][frame]));
295 if (channel_index >= num_channels_) {
298 std::remove_const_t<T> max_value = channels_[channel_index][0];
299 for (
size_t frame = 0; frame < num_frames_; ++frame) {
300 max_value = std::max(max_value, std::fabs(channels_[channel_index][frame]));
344 T*
const* channels_ {};
345 size_t num_channels_ {};
346 size_t num_frames_ {};
348 template<
class First,
class Last>
349 void clear_audio_data(First first, Last last)
const {
350 if constexpr (std::is_unsigned_v<T>) {
351 std::fill(first, last, std::numeric_limits<T>::max() / 2 + 1);
353 std::fill(first, last, T {});
#define RAV_ASSERT_DEBUG(condition, message)
If RAV_DEBUG is enabled, asserts condition to be true, otherwise:
friend bool operator!=(const AudioBufferView &lhs, const AudioBufferView &rhs)
Compares two audio buffers for inequality.
void clear() const
Clears the buffer by setting all samples to zero.
void clear(const size_t channel_index, const size_t start_sample, const size_t num_samples_to_clear)
Clear a range of samples in a channel, without bounds checking.
AudioBufferView(T *const *channels, const size_t num_channels, const size_t num_frames)
Constructs an audio buffer view with the given channels, number of channels, and number of frames.
void copy_to(const size_t src_start_frame, const size_t num_frames, T *const *dst, const size_t dst_num_channels, const size_t dst_start_frame=0)
Copies data from all channels of this buffer into dst.
T * operator[](size_t channel_index)
Accesses the channel at the given index Does not perform bounds checking.
void update(T *const *channels, const size_t num_channels, const size_t num_frames)
Updates the channel pointers, number of channels and number of frames.
size_t num_channels() const
AudioBufferView with_num_frames(const size_t num_frames)
Returns a copy of this view with given number of frames.
AudioBufferView< std::add_const_t< T > > const_view() const
const T * operator[](size_t channel_index) const
Accesses the channel at the given index.
friend bool operator==(const AudioBufferView &lhs, const AudioBufferView &rhs)
Compares two audio buffers for equality.
bool add(const AudioBufferView &other)
Adds the samples of another audio buffer view to this audio buffer view.
void copy_from(const size_t dst_start_frame, const size_t num_frames_to_copy, const T *const *src, const size_t src_num_channels, const size_t src_start_frame=0)
Copies data from all channels of src into all channels of this buffer.
void set_sample(size_t channel_index, size_t frame_index, T value)
Sets the value of an individual sample.
void copy_from(const size_t dst_channel_index, const size_t dst_start_sample, const size_t num_samples_to_copy, const T *src)
Copies data from src into this buffer, without bounds checking.
size_t num_frames() const
void clear(T value) const
Clears the buffer by setting all samples to the given value.
void copy_to(const size_t src_channel_index, const size_t src_start_sample, const size_t num_samples_to_copy, T *dst)
Copies data from this buffer into dst.
AudioBufferView with_num_channels(const size_t num_channels)
Returns a copy of this view with given number of channels.
T find_max_abs(const size_t channel_index) const
@channel_index The index of the channel to find the maximum value for.
const T *const * data() const