30template <
class ElementType,
size_t Extent>
40 AudioBlockView (std::span<ElementType, Extent> view,
const uint_fast32_t numChannels) noexcept
42 numChannels (numChannels),
43 numSamplesPerChannel (view.size() / numChannels)
57 return numSamplesPerChannel;
65 void setSample (
const uint_fast32_t destChannel,
const uint_fast32_t destSample,
const ElementType newValue)
67 assert (destChannel < numChannels);
68 assert (destSample < numSamplesPerChannel);
69 view[destSample * numChannels + destChannel] = newValue;
77 void addSample (
const uint_fast32_t destChannel,
const uint_fast32_t destSample,
const ElementType valueToAdd)
79 assert (destChannel < numChannels);
80 assert (destSample < numSamplesPerChannel);
81 view[destSample * numChannels + destChannel] += valueToAdd;
87 std::fill (view.begin(), view.end(), ElementType (0.0));
93 for_each (view.begin(), view.end(), [&gain] (
auto& x)
101 float getPeak (
const uint_fast32_t channel)
const
105 for (
auto i = channel; i < view.size(); i += numChannels)
107 const auto v = std::abs (view[i]);
122 assert (channel < numChannels);
124 for (
auto i = channel; i < view.size(); i += numChannels)
126 const auto sample = view[i];
127 sum += sample * sample;
130 return std::sqrt (sum / numSamplesPerChannel);
141 auto subView (uint_fast32_t offset, uint_fast32_t size)
143 return AudioBlockView { view.subspan (offset, size), numChannels };
146 std::span<ElementType, Extent> view;
149 uint_fast32_t numChannels;
150 uint_fast32_t numSamplesPerChannel;
159template <
typename FloatType,
size_t Size>
163 explicit AudioBuffer (
const uint_fast32_t numChannels) : numChannels (numChannels), numSamplesPerChannel (Size / numChannels)
174 numChannels = channels;
175 numSamplesPerChannel = Size / numChannels;
187 return numSamplesPerChannel;
199 std::for_each (buffer.begin(), buffer.end(), [&gain] (
auto& e)
213 std::span sp { buffer.data() + offset, size };
218 template <
size_t Extent>
219 void addFrom (
const uint_fast32_t destChannel,
const uint_fast32_t destStartSample,
const AudioBlockView<FloatType, Extent>& source,
const uint_fast32_t sourceChannel,
const uint_fast32_t sourceStartSample,
const uint_fast32_t numSamplesToAdd)
221 const auto& destOffset = destStartSample * numChannels;
222 const auto& sourceOffset = sourceStartSample * source.
getNumChannels();
223 const auto& destEnd = destOffset + numSamplesToAdd * numChannels;
224 const auto& sourceEnd = sourceOffset + numSamplesToAdd * source.
getNumChannels();
226 assert (destEnd <= Size);
227 assert (sourceEnd <= source.getSize());
229 for (uint_fast32_t i = 0; i < numSamplesToAdd; ++i)
231 buffer[destOffset + i * destChannel] += source.buffer[sourceOffset + i * source.
getNumChannels];
239 FloatType
getPeak (
const uint_fast32_t channel)
const
241 assert (channel < numChannels);
242 FloatType peak = 0.0;
243 for (
auto i = channel; i < numSamplesPerChannel * numChannels; i += numChannels)
245 const auto v = std::abs (buffer[i]);
260 assert (channel < numChannels);
262 for (
auto i = channel; i < numSamplesPerChannel * numChannels; i += numChannels)
264 const auto sample = buffer[i];
265 sum += sample * sample;
268 return static_cast<FloatType
> (std::sqrt (sum / numSamplesPerChannel));
271 std::array<FloatType, Size> buffer = {};
274 uint_fast32_t numChannels;
275 uint_fast32_t numSamplesPerChannel;
A lightweight data structure that stores a pointer to an audio buffer.
Definition: ame_AudioBuffer.hpp:32
uint_fast32_t getNumChannels() const noexcept
Returns the number of channels.
Definition: ame_AudioBuffer.hpp:49
void setSample(const uint_fast32_t destChannel, const uint_fast32_t destSample, const ElementType newValue)
Set a sample in the buffer.
Definition: ame_AudioBuffer.hpp:65
AudioBlockView(std::span< ElementType, Extent > view, const uint_fast32_t numChannels) noexcept
Constructor.
Definition: ame_AudioBuffer.hpp:40
float getRMSLevel(const uint_fast32_t channel) const
Returns the root mean squared level for a region of a channel.
Definition: ame_AudioBuffer.hpp:120
float getPeak(const uint_fast32_t channel) const
Finds the highest absolute sample value within a region of a channel.
Definition: ame_AudioBuffer.hpp:101
void clear()
Set all samples to 0.
Definition: ame_AudioBuffer.hpp:85
void addSample(const uint_fast32_t destChannel, const uint_fast32_t destSample, const ElementType valueToAdd)
Add a value to a sample in the buffer.
Definition: ame_AudioBuffer.hpp:77
void applyGain(const float gain)
Applies a gain multiple to all the audio data.
Definition: ame_AudioBuffer.hpp:91
auto subView(uint_fast32_t offset, uint_fast32_t size)
Returns an AudioBlockView that references N elements from an arbitrary position.
Definition: ame_AudioBuffer.hpp:141
uint_fast32_t getNumSamplesPerChannel() const noexcept
Returns the number of samples per channel.
Definition: ame_AudioBuffer.hpp:55
An audio buffer that supports multiple channels whose size is determined at compile time.
Definition: ame_AudioBuffer.hpp:161
void setNumChannels(const uint_fast32_t channels)
Set the number of channels.
Definition: ame_AudioBuffer.hpp:172
FloatType getPeak(const uint_fast32_t channel) const
Finds the highest absolute sample value within a region of a channel.
Definition: ame_AudioBuffer.hpp:239
FloatType getRMSLevel(const uint_fast32_t channel) const
Returns the root mean squared level for a region of a channel.
Definition: ame_AudioBuffer.hpp:258
void applyGain(const float gain)
Applies a gain multiple to all the audio data.
Definition: ame_AudioBuffer.hpp:197
void clear()
Set all samples to 0.
Definition: ame_AudioBuffer.hpp:191
auto makeAudioBlockView(uint_fast32_t offset=0, uint_fast32_t size=Size)
Returns an AudioBlockView that references N elements from an arbitrary position.
Definition: ame_AudioBuffer.hpp:211
uint_fast32_t getNumSamplesPerChannel() const noexcept
Returns the number of samples per channel.
Definition: ame_AudioBuffer.hpp:185
void addFrom(const uint_fast32_t destChannel, const uint_fast32_t destStartSample, const AudioBlockView< FloatType, Extent > &source, const uint_fast32_t sourceChannel, const uint_fast32_t sourceStartSample, const uint_fast32_t numSamplesToAdd)
addBuffer
Definition: ame_AudioBuffer.hpp:219
uint_fast32_t getNumChannels() const noexcept
Returns the number of channels.
Definition: ame_AudioBuffer.hpp:179