51template <
typename T,
int MaxViewChannels = 16>
66 requires std::is_convertible_v<U*, T*>
69 assert(numChannels >= 0 && numChannels <= MaxViewChannels);
74 numChannels_ = std::clamp(numChannels, 0, MaxViewChannels);
75 numSamples_ = numSamples >= 0 ? numSamples : 0;
77 for (
int ch = 0; ch < numChannels_; ++ch)
78 channels_[ch] = channelPtrs[ch];
102 template <
typename U,
int OtherChannels>
103 requires (std::is_convertible_v<U*, T*> && OtherChannels <= MaxViewChannels)
107 for (
int ch = 0; ch < numChannels_; ++ch)
116 assert(ch >= 0 && ch < numChannels_);
117 return channels_[ch];
136 assert(startSample >= 0);
138 assert(startSample + length <= numSamples_);
141 sub.numChannels_ = numChannels_;
142 sub.numSamples_ = length;
143 for (
int ch = 0; ch < numChannels_; ++ch)
144 sub.channels_[ch] = channels_[ch] + startSample;
156 static_assert(!std::is_const_v<T>,
"Cannot clear a const view");
157 const auto bytes =
static_cast<std::size_t
>(numSamples_) *
sizeof(T);
161 if (bytes == 0)
return;
162 for (
int ch = 0; ch < numChannels_; ++ch)
163 std::memset(channels_[ch], 0, bytes);
177 template <
typename U,
int M>
180 static_assert(!std::is_const_v<T>,
"Cannot copy into a const view");
182 const int chCount = std::min(numChannels_, src.getNumChannels());
183 const int nSamples = std::min(numSamples_, src.getNumSamples());
185 for (
int ch = 0; ch < chCount; ++ch)
187 T* dst = channels_[ch];
188 const U* s = src.getChannel(ch);
190 if constexpr (std::is_same_v<std::remove_const_t<U>, T>)
192 std::memmove(dst, s,
static_cast<std::size_t
>(nSamples) *
sizeof(T));
196 for (
int i = 0; i < nSamples; ++i)
197 dst[i] =
static_cast<T
>(s[i]);
214 template <
typename U,
int M>
217 static_assert(!std::is_const_v<T>,
"Cannot add into a const view");
220 const int nSamples = std::min(numSamples_, src.
getNumSamples());
222 for (
int ch = 0; ch < chCount; ++ch)
224 T* dst = channels_[ch];
227 if constexpr (std::is_same_v<std::remove_const_t<U>, T>)
233 for (
int i = 0; i < nSamples; ++i)
234 dst[i] +=
static_cast<T
>(s[i]) * gain;
245 static_assert(!std::is_const_v<T>,
"Cannot apply gain to a const view");
246 for (
int ch = 0; ch < numChannels_; ++ch)
259 using Value = std::remove_const_t<T>;
260 Value peak = Value(0);
261 for (
int ch = 0; ch < numChannels_; ++ch)
264 if (chPeak > peak) peak = chPeak;
270 std::array<T*, MaxViewChannels> channels_ {};
271 int numChannels_ = 0;
277static_assert(std::is_trivially_copyable_v<AudioBufferView<float>>,
278 "AudioBufferView must remain trivially copyable");
279static_assert(std::is_trivially_copyable_v<AudioBufferView<const float>>,
280 "AudioBufferView must remain trivially copyable");
293template <
typename T,
int MaxChannels = 16>
296 static constexpr std::size_t kAlignment = 32;
297 static_assert(std::has_single_bit(kAlignment),
"Alignment must be a power of two");
298 static_assert(std::is_same_v<T, std::remove_cv_t<T>> && std::is_trivially_copyable_v<T>,
299 "AudioBuffer requires a non-const, trivially copyable sample type");
311 : rawData_(std::exchange(other.rawData_,
nullptr))
312 , channelPtrs_(std::exchange(other.channelPtrs_, {}))
313 , numChannels_(std::exchange(other.numChannels_, 0))
314 , numSamples_(std::exchange(other.numSamples_, 0))
315 , allocatedBytes_(std::exchange(other.allocatedBytes_, 0))
316 , strideBytes_(std::exchange(other.strideBytes_, 0))
325 rawData_ = std::exchange(other.rawData_,
nullptr);
326 channelPtrs_ = std::exchange(other.channelPtrs_, {});
327 numChannels_ = std::exchange(other.numChannels_, 0);
328 numSamples_ = std::exchange(other.numSamples_, 0);
329 allocatedBytes_ = std::exchange(other.allocatedBytes_, 0);
330 strideBytes_ = std::exchange(other.strideBytes_, 0);
348 void resize(
int numChannels,
int numSamples)
350 assert(numChannels >= 0 && numChannels <= MaxChannels);
351 assert(numSamples >= 0);
355 numChannels = std::clamp(numChannels, 0, MaxChannels);
356 if (numSamples < 0) numSamples = 0;
361 const auto samplesBytes =
static_cast<std::uint64_t
>(numSamples) *
sizeof(T);
362 const auto stride64 = (samplesBytes + (kAlignment - 1)) & ~static_cast<std::uint64_t>(kAlignment - 1);
363 const auto totalBytes64 = stride64 *
static_cast<std::uint64_t
>(numChannels);
364 if (totalBytes64 > std::numeric_limits<std::size_t>::max())
366 const auto stride =
static_cast<std::size_t
>(stride64);
367 const auto totalBytes =
static_cast<std::size_t
>(totalBytes64);
370 if (totalBytes > allocatedBytes_)
373 rawData_ =
static_cast<T*
>(::operator
new(totalBytes, std::align_val_t(kAlignment)));
374 allocatedBytes_ = totalBytes;
377 numChannels_ = numChannels;
378 numSamples_ = numSamples;
379 strideBytes_ = stride;
381 auto* base =
reinterpret_cast<char*
>(rawData_);
384 for (
int ch = 0; ch < numChannels; ++ch)
385 channelPtrs_[ch] =
reinterpret_cast<T*
>(base + stride *
static_cast<std::size_t
>(ch));
388 for (
int ch = numChannels; ch < MaxChannels; ++ch)
389 channelPtrs_[ch] =
nullptr;
402 return { channelPtrs_.data(), numChannels_, numSamples_ };
408 return { channelPtrs_.data(), numChannels_, numSamples_ };
416 assert(ch >= 0 && ch < numChannels_);
417 return channelPtrs_[ch];
423 assert(ch >= 0 && ch < numChannels_);
424 return channelPtrs_[ch];
443 if (rawData_ ==
nullptr)
return;
444 for (
int ch = 0; ch < numChannels_; ++ch)
445 std::memset(channelPtrs_[ch], 0, strideBytes_);
450 [[noreturn]]
static void failAllocation()
452#if defined(__cpp_exceptions) || defined(_CPPUNWIND)
453 throw std::bad_alloc();
459 void deallocate() noexcept
463 ::operator
delete(rawData_, std::align_val_t(kAlignment));
477 channelPtrs_.fill(
nullptr);
483 T* rawData_ =
nullptr;
484 std::array<T*, MaxChannels> channelPtrs_ {};
485 int numChannels_ = 0;
487 std::size_t allocatedBytes_ = 0;
488 std::size_t strideBytes_ = 0;
Non-owning view over audio channel data.
void clear() const noexcept
Fills the valid sample range of all channels with zeros.
int getNumSamples() const noexcept
Returns the number of samples per channel.
int getNumChannels() const noexcept
Returns the number of channels in this view.
AudioBufferView() noexcept=default
Creates an empty view (0 channels, 0 samples).
void addFrom(const AudioBufferView< U, M > &src, T gain=T(1)) const noexcept
Adds samples from a source view into this view with optional gain.
T * getChannel(int ch) const noexcept
Returns a pointer to the sample data for the given channel.
AudioBufferView(const AudioBufferView< U, OtherChannels > &other) noexcept
Converts compatible sample types and widens pointer-array capacity.
void copyFrom(const AudioBufferView< U, M > &src) const noexcept
Safely copies samples from a source view into this view.
AudioBufferView getSubView(int startSample, int length) const noexcept
Returns a zero-copy sub-view starting at an offset.
std::remove_const_t< T > getPeakLevel() const noexcept
Returns the peak absolute sample value across all channels.
void applyGain(T gain) const noexcept
Multiplies all samples in all valid channels by a gain factor.
Owning audio buffer with contiguous, 32-byte aligned storage.
AudioBuffer & operator=(const AudioBuffer &)=delete
AudioBufferView< T, MaxChannels > toView() noexcept
Returns a non-owning mutable view of this buffer. The view's channel capacity is propagated from MaxC...
int getNumChannels() const noexcept
Returns the number of active channels.
AudioBuffer(const AudioBuffer &)=delete
T * getChannel(int ch) noexcept
Returns a pointer to the sample data.
int getNumSamples() const noexcept
Returns the number of samples per channel.
AudioBufferView< const T, MaxChannels > toView() const noexcept
Returns a non-owning const view of this buffer.
AudioBuffer(AudioBuffer &&other) noexcept
C++20 idiomatic move constructor.
void resize(int numChannels, int numSamples)
Allocates the buffer for the given dimensions.
const T * getChannel(int ch) const noexcept
Const overload.
void clear() noexcept
Clears all channels, including SIMD alignment padding. Ensures any SIMD over-read will consume pure z...
AudioBuffer & operator=(AudioBuffer &&other) noexcept
C++20 idiomatic move assignment.
void addWithGain(float *DSPARK_RESTRICT dst, const float *DSPARK_RESTRICT src, float gain, int count) noexcept
Adds source samples scaled by a gain factor into a destination buffer.
float peakLevel(const float *DSPARK_RESTRICT data, int count) noexcept
Returns the peak absolute sample value in a buffer.
void applyGain(float *DSPARK_RESTRICT data, float gain, int count) noexcept
Multiplies all samples in a buffer by a gain factor.
Main namespace for the DSPark framework.