Refactor the Audio buffer and span

This commit is contained in:
Paul Ferrand 2019-12-22 23:35:27 +01:00
parent 8e95ce63ec
commit d813cd8a04
2 changed files with 127 additions and 96 deletions

View file

@ -30,19 +30,19 @@
#include <memory> #include <memory>
#include <array> #include <array>
namespace sfz namespace sfz
{ {
/** /**
* @brief A class to handle a collection of buffers, where each buffer has the same size. * @brief A class to handle a collection of buffers, where each buffer has the same size.
* *
* Unlike AudioSpan, this class *owns* its underlying buffers and they are freed when the buffer * Unlike AudioSpan, this class *owns* its underlying buffers and they are freed when the buffer
* is destroyed. * is destroyed.
* *
* @tparam Type the underlying type of the buffers * @tparam Type the underlying type of the buffers
* @tparam MaxChannels the maximum number of channels in the buffer * @tparam MaxChannels the maximum number of channels in the buffer
* @tparam Alignment the alignment for the buffers * @tparam Alignment the alignment for the buffers
*/ */
template <class Type, unsigned int MaxChannels = sfz::config::numChannels, unsigned int Alignment = SIMDConfig::defaultAlignment> template <class Type, int MaxChannels = sfz::config::numChannels, unsigned int Alignment = SIMDConfig::defaultAlignment>
class AudioBuffer { class AudioBuffer {
public: public:
using value_type = std::remove_cv_t<Type>; using value_type = std::remove_cv_t<Type>;
@ -54,7 +54,7 @@ public:
/** /**
* @brief Construct a new Audio Buffer object * @brief Construct a new Audio Buffer object
* *
*/ */
AudioBuffer() AudioBuffer()
{ {
@ -63,9 +63,9 @@ public:
/** /**
* @brief Construct a new Audio Buffer object with a specified number of * @brief Construct a new Audio Buffer object with a specified number of
* channels and frames. * channels and frames.
* *
* @param numChannels * @param numChannels
* @param numFrames * @param numFrames
*/ */
AudioBuffer(int numChannels, int numFrames) AudioBuffer(int numChannels, int numFrames)
: numChannels(numChannels) : numChannels(numChannels)
@ -77,24 +77,29 @@ public:
/** /**
* @brief Resizes all the underlying buffers to a new size. * @brief Resizes all the underlying buffers to a new size.
* *
* @param newSize * @param newSize
* @return true if the resize worked * @return true if the resize worked
* @return false otherwise * @return false otherwise
*/ */
bool resize(size_type newSize) bool resize(size_type newSize)
{ {
bool returnedOK = true; bool returnedOK = true;
for (auto i = 0; i < numChannels; ++i) for (auto i = 0; i < numChannels; ++i)
returnedOK &= buffers[i]->resize(newSize); returnedOK &= buffers[i]->resize(newSize);
if (returnedOK)
numFrames = newSize;
return returnedOK; return returnedOK;
} }
/** /**
* @brief Return an iterator to a specific channel with a non-const type. * @brief Return an iterator to a specific channel with a non-const type.
* *
* @param channelIndex * @param channelIndex
* @return iterator * @return iterator
*/ */
iterator channelWriter(int channelIndex) iterator channelWriter(int channelIndex)
{ {
@ -107,9 +112,9 @@ public:
/** /**
* @brief Returns a sentinel for the channelWriter(channelIndex) iterator * @brief Returns a sentinel for the channelWriter(channelIndex) iterator
* *
* @param channelIndex * @param channelIndex
* @return iterator * @return iterator
*/ */
iterator channelWriterEnd(int channelIndex) iterator channelWriterEnd(int channelIndex)
{ {
@ -122,9 +127,9 @@ public:
/** /**
* @brief Returns a const iterator for a specific channel * @brief Returns a const iterator for a specific channel
* *
* @param channelIndex * @param channelIndex
* @return const_iterator * @return const_iterator
*/ */
const_iterator channelReader(int channelIndex) const const_iterator channelReader(int channelIndex) const
{ {
@ -137,9 +142,9 @@ public:
/** /**
* @brief Returns a sentinel for the channelReader(channelIndex) iterator * @brief Returns a sentinel for the channelReader(channelIndex) iterator
* *
* @param channelIndex * @param channelIndex
* @return const_iterator * @return const_iterator
*/ */
const_iterator channelReaderEnd(int channelIndex) const const_iterator channelReaderEnd(int channelIndex) const
{ {
@ -152,9 +157,9 @@ public:
/** /**
* @brief Get a Span for a specific channel * @brief Get a Span for a specific channel
* *
* @param channelIndex * @param channelIndex
* @return absl::Span<value_type> * @return absl::Span<value_type>
*/ */
absl::Span<value_type> getSpan(int channelIndex) const absl::Span<value_type> getSpan(int channelIndex) const
{ {
@ -167,9 +172,9 @@ public:
/** /**
* @brief Get a const Span object for a specific channel * @brief Get a const Span object for a specific channel
* *
* @param channelIndex * @param channelIndex
* @return absl::Span<const value_type> * @return absl::Span<const value_type>
*/ */
absl::Span<const value_type> getConstSpan(int channelIndex) const absl::Span<const value_type> getConstSpan(int channelIndex) const
{ {
@ -178,7 +183,7 @@ public:
/** /**
* @brief Add a channel to the buffer with the current number of frames. * @brief Add a channel to the buffer with the current number of frames.
* *
*/ */
void addChannel() void addChannel()
{ {
@ -188,8 +193,8 @@ public:
/** /**
* @brief Get the number of elements in each buffer * @brief Get the number of elements in each buffer
* *
* @return size_type * @return size_type
*/ */
size_type getNumFrames() const size_type getNumFrames() const
{ {
@ -198,8 +203,8 @@ public:
/** /**
* @brief Get the number of channels * @brief Get the number of channels
* *
* @return int * @return int
*/ */
int getNumChannels() const int getNumChannels() const
{ {
@ -208,9 +213,9 @@ public:
/** /**
* @brief Check if the buffers contains no elements * @brief Check if the buffers contains no elements
* *
* @return true * @return true
* @return false * @return false
*/ */
bool empty() const bool empty() const
{ {
@ -219,12 +224,12 @@ public:
/** /**
* @brief Get a reference to a given element in a given buffer. * @brief Get a reference to a given element in a given buffer.
* *
* In release builds this is not checked and may touch bad memory. * In release builds this is not checked and may touch bad memory.
* *
* @param channelIndex * @param channelIndex
* @param frameIndex * @param frameIndex
* @return Type& * @return Type&
*/ */
Type& getSample(int channelIndex, size_type frameIndex) Type& getSample(int channelIndex, size_type frameIndex)
{ {
@ -237,21 +242,46 @@ public:
/** /**
* @brief Alias for getSample(...) * @brief Alias for getSample(...)
* *
* @param channelIndex * @param channelIndex
* @param frameIndex * @param frameIndex
* @return Type& * @return Type&
*/ */
Type& operator()(int channelIndex, size_type frameIndex) Type& operator()(int channelIndex, size_type frameIndex)
{ {
return getSample(channelIndex, frameIndex); return getSample(channelIndex, frameIndex);
} }
/**
* @brief Remove all channels from the buffer and reset it to empty
*
*/
void reset()
{
for (int i = 0; i < numChannels; ++i)
buffers[i].reset();
numFrames = 0;
numChannels = 0;
}
/**
* @brief Add a positive number of channels to the buffer
*
* @param numChannels
*/
void addChannels(int numChannels)
{
ASSERT(this->numChannels + numChannels <= MaxChannels);
for (int i = 0; i < numChannels; ++i)
addChannel();
}
private: private:
using buffer_type = Buffer<Type, Alignment>; using buffer_type = Buffer<Type, Alignment>;
using buffer_ptr = std::unique_ptr<buffer_type>; using buffer_ptr = std::unique_ptr<buffer_type>;
static_assert(MaxChannels > 0, "Need a positive number of channels");
std::array<buffer_ptr, MaxChannels> buffers; std::array<buffer_ptr, MaxChannels> buffers;
int numChannels { 0 }; int numChannels { 0 };
size_type numFrames { 0 }; size_type numFrames { 0 };
}; };
} }

View file

@ -36,15 +36,15 @@ namespace sfz
{ {
/** /**
* @brief Extension of the concept of spans to multiple channels. * @brief Extension of the concept of spans to multiple channels.
* *
* A span (and by extension an audiospan) is at its core a structure * A span (and by extension an audiospan) is at its core a structure
* containing a pointer and a size to a buffer that is owned by another * containing a pointer and a size to a buffer that is owned by another
* object. A span is thus a view into a buffer that is cheap to copy and * object. A span is thus a view into a buffer that is cheap to copy and
* pass around, and safe as long as the underlying buffer is allocated. * pass around, and safe as long as the underlying buffer is allocated.
* The goal of the class is to reduce interfaces and usage annoyance for * The goal of the class is to reduce interfaces and usage annoyance for
* codebases. Obviously, this requires that most functions use AudioSpans. * codebases. Obviously, this requires that most functions use AudioSpans.
* It also protects against overreading a buffer. Users can still indicate * It also protects against overreading a buffer. Users can still indicate
* that they * that they
* @code{.cpp} * @code{.cpp}
* constexpr int bufferSize { 1024 }; * constexpr int bufferSize { 1024 };
* void gain(AudioSpan<float, 2> arrayView, float gain) * void gain(AudioSpan<float, 2> arrayView, float gain)
@ -52,41 +52,41 @@ namespace sfz
* for (auto& f: arrayView) * for (auto& f: arrayView)
* f *= gain; * f *= gain;
* } * }
* *
* int main(char argc, char** argv) * int main(char argc, char** argv)
* { * {
* float leftChannel [bufferSize]; * float leftChannel [bufferSize];
* float rightChannel [bufferSize]; * float rightChannel [bufferSize];
* *
* for (int i = 0; i < bufferSize; ++i) * for (int i = 0; i < bufferSize; ++i)
* { * {
* leftChannel[i] = 1.0f; * leftChannel[i] = 1.0f;
* rightChannel[i] = 1.0f; * rightChannel[i] = 1.0f;
* } * }
* *
* // Type is inferred * // Type is inferred
* AudioSpan explicitView { { leftChannel, rightChannel }, bufferSize }; * AudioSpan explicitView { { leftChannel, rightChannel }, bufferSize };
* // Size will be taken as the minimum size of all the spans given * // Size will be taken as the minimum size of all the spans given
* // for all types that can be automatically cast to absl::Span or std::span * // for all types that can be automatically cast to absl::Span or std::span
* AudioSpan explicitView2 { { leftChannel, rightChannel } }; * AudioSpan explicitView2 { { leftChannel, rightChannel } };
* *
* gain(explicitView, 0.5f); // the array elements are now equal to 0.5f * gain(explicitView, 0.5f); // the array elements are now equal to 0.5f
* gain(explicitView2, 0.5f); // the array elements are now equal to 0.25f * gain(explicitView2, 0.5f); // the array elements are now equal to 0.25f
* *
* // You can also build spans implicitely * // You can also build spans implicitely
* gain({ { leftChannel, rightChannel }, bufferSize }, 0.5f); // elements equal to 0.125f * gain({ { leftChannel, rightChannel }, bufferSize }, 0.5f); // elements equal to 0.125f
* } * }
* @endcode * @endcode
* You can build AudioSpans from AudioBuffers directly, the AudioBufferT.cpp file in the test * You can build AudioSpans from AudioBuffers directly, the AudioBufferT.cpp file in the test
* folder show some example. * folder show some example.
* As with many things templated in C++ the `Type` can be`const` or `volatile`, and `const float` * As with many things templated in C++ the `Type` can be`const` or `volatile`, and `const float`
* is not the same type as `float`. You thus cannot build an `AudioSpan<float>` from * is not the same type as `float`. You thus cannot build an `AudioSpan<float>` from
* a `const float *` buffer for example. * a `const float *` buffer for example.
* *
* @tparam Type the underlying buffer type * @tparam Type the underlying buffer type
* @tparam MaxChannels the maximum number of channels. Defaults to sfz::config::numChannels * @tparam MaxChannels the maximum number of channels. Defaults to sfz::config::numChannels
*/ */
template <class Type, unsigned int MaxChannels = sfz::config::numChannels> template <class Type, int MaxChannels = sfz::config::numChannels>
class AudioSpan { class AudioSpan {
public: public:
using size_type = size_t; using size_type = size_t;
@ -96,7 +96,7 @@ public:
/** /**
* @brief Construct a new Audio Span object * @brief Construct a new Audio Span object
* *
* @param spans an array of MaxChannels pointers to buffers. * @param spans an array of MaxChannels pointers to buffers.
* @param numChannels the number of spans to take in from the array * @param numChannels the number of spans to take in from the array
* @param offset starting offset for the AudioSpan * @param offset starting offset for the AudioSpan
@ -106,14 +106,14 @@ public:
: numFrames(numFrames) : numFrames(numFrames)
, numChannels(numChannels) , numChannels(numChannels)
{ {
ASSERT(static_cast<unsigned int>(numChannels) <= MaxChannels); ASSERT(numChannels <= MaxChannels);
for (auto i = 0; i < numChannels; ++i) for (auto i = 0; i < numChannels; ++i)
this->spans[i] = spans[i] + offset; this->spans[i] = spans[i] + offset;
} }
/** /**
* @brief Construct a new Audio Span object from initializer lists * @brief Construct a new Audio Span object from initializer lists
* *
* @param spans the list of span * @param spans the list of span
* @param numFrames the size of the audio span * @param numFrames the size of the audio span
*/ */
@ -133,11 +133,11 @@ public:
/** /**
* @brief Construct a new Audio Span object from a list of absl::Span<Type> * @brief Construct a new Audio Span object from a list of absl::Span<Type>
* *
* This constructor can be implicitely called for any source that can be cast transparently to * This constructor can be implicitely called for any source that can be cast transparently to
* an absl::Span<Type>. The size of the AudioSpan is inferred from the size of the smallest * an absl::Span<Type>. The size of the AudioSpan is inferred from the size of the smallest
* absl::Span<Type>. * absl::Span<Type>.
* *
* @param spans a list of objects compatible with absl::Span<Type> * @param spans a list of objects compatible with absl::Span<Type>
*/ */
AudioSpan(std::initializer_list<absl::Span<Type>> spans) AudioSpan(std::initializer_list<absl::Span<Type>> spans)
@ -155,16 +155,16 @@ public:
/** /**
* @brief Construct a new Audio Span object from an AudioBuffer with a const Type. * @brief Construct a new Audio Span object from an AudioBuffer with a const Type.
* *
* This constructor can be implicitely called for any source that can be cast transparently to * This constructor can be implicitely called for any source that can be cast transparently to
* an AudioBuffer<const Type>. * an AudioBuffer<const Type>.
* *
* @tparam U the underlying type compatible with the template Type of the AudioSpan * @tparam U the underlying type compatible with the template Type of the AudioSpan
* @tparam N the number of channels in the AudioBuffer * @tparam N the number of channels in the AudioBuffer
* @tparam Alignment the alignment block size for the platform * @tparam Alignment the alignment block size for the platform
* @param audioBuffer the source AudioBuffer. * @param audioBuffer the source AudioBuffer.
*/ */
template <class U, unsigned int N, unsigned int Alignment, typename = std::enable_if<N <= MaxChannels>, typename = std::enable_if_t<std::is_const<U>::value, int>> template <class U, int N, unsigned int Alignment, typename = std::enable_if<N <= MaxChannels>, typename = std::enable_if_t<std::is_const<U>::value, int>>
AudioSpan(AudioBuffer<U, N, Alignment>& audioBuffer) AudioSpan(AudioBuffer<U, N, Alignment>& audioBuffer)
: numFrames(audioBuffer.getNumFrames()) : numFrames(audioBuffer.getNumFrames())
, numChannels(audioBuffer.getNumChannels()) , numChannels(audioBuffer.getNumChannels())
@ -176,16 +176,16 @@ public:
/** /**
* @brief Construct a new Audio Span object from an AudioBuffer with a non-const Type. * @brief Construct a new Audio Span object from an AudioBuffer with a non-const Type.
* *
* This constructor can be implicitely called for any source that can be cast transparently to * This constructor can be implicitely called for any source that can be cast transparently to
* an AudioBuffer<Type>. * an AudioBuffer<Type>.
* *
* @tparam U the underlying type compatible with the template Type of the AudioSpan * @tparam U the underlying type compatible with the template Type of the AudioSpan
* @tparam N the number of channels in the AudioBuffer * @tparam N the number of channels in the AudioBuffer
* @tparam Alignment the alignment block size for the platform * @tparam Alignment the alignment block size for the platform
* @param audioBuffer the source AudioBuffer. * @param audioBuffer the source AudioBuffer.
*/ */
template <class U, unsigned int N, unsigned int Alignment, typename = std::enable_if<N <= MaxChannels>> template <class U, int N, unsigned int Alignment, typename = std::enable_if<N <= MaxChannels>>
AudioSpan(AudioBuffer<U, N, Alignment>& audioBuffer) AudioSpan(AudioBuffer<U, N, Alignment>& audioBuffer)
: numFrames(audioBuffer.getNumFrames()) : numFrames(audioBuffer.getNumFrames())
, numChannels(audioBuffer.getNumChannels()) , numChannels(audioBuffer.getNumChannels())
@ -197,10 +197,10 @@ public:
/** /**
* @brief AudioSpan copy constructor * @brief AudioSpan copy constructor
* *
* @param other the other AudioSpan * @param other the other AudioSpan
*/ */
template <class U, unsigned int N, typename = std::enable_if<N <= MaxChannels>> template <class U, int N, typename = std::enable_if<N <= MaxChannels>>
AudioSpan(const AudioSpan<U, N>& other) AudioSpan(const AudioSpan<U, N>& other)
: numFrames(other.getNumFrames()) : numFrames(other.getNumFrames())
, numChannels(other.getNumChannels()) , numChannels(other.getNumChannels())
@ -212,7 +212,7 @@ public:
/** /**
* @brief Get a raw pointer to a specific channel from the AudioSpan * @brief Get a raw pointer to a specific channel from the AudioSpan
* *
* @param channelIndex the channel * @param channelIndex the channel
* @return Type* the raw pointer to the channel * @return Type* the raw pointer to the channel
*/ */
@ -227,9 +227,9 @@ public:
/** /**
* @brief Get a Span<Type> corresponding to a specific channel * @brief Get a Span<Type> corresponding to a specific channel
* *
* @param channelIndex the channel * @param channelIndex the channel
* @return absl::Span<Type> * @return absl::Span<Type>
*/ */
absl::Span<Type> getSpan(int channelIndex) absl::Span<Type> getSpan(int channelIndex)
{ {
@ -242,9 +242,9 @@ public:
/** /**
* @brief Get a Span<const Type> corresponding to a specific channel * @brief Get a Span<const Type> corresponding to a specific channel
* *
* @param channelIndex the channel * @param channelIndex the channel
* @return absl::Span<const Type> * @return absl::Span<const Type>
*/ */
absl::Span<const Type> getConstSpan(int channelIndex) absl::Span<const Type> getConstSpan(int channelIndex)
{ {
@ -256,8 +256,8 @@ public:
} }
/** /**
* @brief Get the mean of the squared values of the AudioSpan elements on all channels. * @brief Get the mean of the squared values of the AudioSpan elements on all channels.
* *
* @return Type * @return Type
*/ */
Type meanSquared() noexcept Type meanSquared() noexcept
@ -272,7 +272,7 @@ public:
/** /**
* @brief Fills all the elements of the AudioSpan with the same value * @brief Fills all the elements of the AudioSpan with the same value
* *
* @param value the filling value * @param value the filling value
*/ */
void fill(Type value) noexcept void fill(Type value) noexcept
@ -284,7 +284,7 @@ public:
/** /**
* @brief Apply a gain span elementwise to all channels in the AudioSpan. * @brief Apply a gain span elementwise to all channels in the AudioSpan.
* *
* @param gain the gain to apply * @param gain the gain to apply
*/ */
void applyGain(absl::Span<const Type> gain) noexcept void applyGain(absl::Span<const Type> gain) noexcept
@ -296,7 +296,7 @@ public:
/** /**
* @brief Apply a gain to all channels in the AudioSpan. * @brief Apply a gain to all channels in the AudioSpan.
* *
* @param gain the gain to apply * @param gain the gain to apply
*/ */
void applyGain(Type gain) noexcept void applyGain(Type gain) noexcept
@ -309,10 +309,10 @@ public:
/** /**
* @brief Add another AudioSpan with a compatible number of channels to the current * @brief Add another AudioSpan with a compatible number of channels to the current
* AudioSpan. * AudioSpan.
* *
* @param other the other AudioSpan * @param other the other AudioSpan
*/ */
template <class U, unsigned int N, typename = std::enable_if<N <= MaxChannels>> template <class U, int N, typename = std::enable_if<N <= MaxChannels>>
void add(AudioSpan<U, N>& other) void add(AudioSpan<U, N>& other)
{ {
static_assert(!std::is_const<Type>::value, "Can't allow mutating operations on const AudioSpans"); static_assert(!std::is_const<Type>::value, "Can't allow mutating operations on const AudioSpans");
@ -324,12 +324,12 @@ public:
} }
/** /**
* @brief Copy the elements of another AudioSpan with a compatible number of channels * @brief Copy the elements of another AudioSpan with a compatible number of channels
* to the current AudioSpan. * to the current AudioSpan.
* *
* @param other the other AudioSpan * @param other the other AudioSpan
*/ */
template <class U, unsigned int N, typename = std::enable_if<N <= MaxChannels>> template <class U, int N, typename = std::enable_if<N <= MaxChannels>>
void copy(AudioSpan<U, N>& other) void copy(AudioSpan<U, N>& other)
{ {
static_assert(!std::is_const<Type>::value, "Can't allow mutating operations on const AudioSpans"); static_assert(!std::is_const<Type>::value, "Can't allow mutating operations on const AudioSpans");
@ -342,7 +342,7 @@ public:
/** /**
* @brief Get the size of this AudioSpan. * @brief Get the size of this AudioSpan.
* *
* @returns size_type the number of frames in the AudioSpan * @returns size_type the number of frames in the AudioSpan
*/ */
size_type getNumFrames() size_type getNumFrames()
@ -352,17 +352,17 @@ public:
/** /**
* @brief Get the number of channels of this AudioSpan. * @brief Get the number of channels of this AudioSpan.
* *
* @returns size_type the number of channels in the AudioSpan * @returns size_type the number of channels in the AudioSpan
*/ */
int getNumChannels() int getNumChannels()
{ {
return numChannels; return numChannels;
} }
/** /**
* @brief Creates a new AudioSpan but with only the `length` first elements of each channel. * @brief Creates a new AudioSpan but with only the `length` first elements of each channel.
* *
* @param length the number of elements to take on each channel * @param length the number of elements to take on each channel
*/ */
AudioSpan<Type> first(size_type length) AudioSpan<Type> first(size_type length)
@ -373,7 +373,7 @@ public:
/** /**
* @brief Creates a new AudioSpan but with only the `length` last elements of each channel. * @brief Creates a new AudioSpan but with only the `length` last elements of each channel.
* *
* @param length the number of elements to take on each channel * @param length the number of elements to take on each channel
*/ */
AudioSpan<Type> last(size_type length) AudioSpan<Type> last(size_type length)
@ -387,7 +387,7 @@ public:
* taking `length` elements. The new AudioSpan will have `length` elements. This basically * taking `length` elements. The new AudioSpan will have `length` elements. This basically
* removes the first `offset` elements and the last `numFrames - length - offset` elements * removes the first `offset` elements and the last `numFrames - length - offset` elements
* from the AudioSpan. * from the AudioSpan.
* *
* @param length the number of elements to take on each channel * @param length the number of elements to take on each channel
*/ */
AudioSpan<Type> subspan(size_type offset, size_type length) AudioSpan<Type> subspan(size_type offset, size_type length)
@ -400,7 +400,7 @@ public:
* @brief Creates a new AudioSpan starting at an offset `offset` on each channel and * @brief Creates a new AudioSpan starting at an offset `offset` on each channel and
* taking all the remaining elements. This function basically removes the first `offset` * taking all the remaining elements. This function basically removes the first `offset`
* elements from the AudioSpan. The new Audiospan will have size `numFrames - offset`. * elements from the AudioSpan. The new Audiospan will have size `numFrames - offset`.
* *
* @param length the number of elements to take on each channel * @param length the number of elements to take on each channel
*/ */
AudioSpan<Type> subspan(size_type offset) AudioSpan<Type> subspan(size_type offset)
@ -410,8 +410,9 @@ public:
} }
private: private:
static_assert(MaxChannels > 0, "Need a positive number of channels");
std::array<Type*, MaxChannels> spans; std::array<Type*, MaxChannels> spans;
size_type numFrames { 0 }; size_type numFrames { 0 };
int numChannels { 0 }; int numChannels { 0 };
}; };
} }