diff --git a/src/sfizz/AudioBuffer.h b/src/sfizz/AudioBuffer.h index 98105a5b..d65e2a2d 100644 --- a/src/sfizz/AudioBuffer.h +++ b/src/sfizz/AudioBuffer.h @@ -30,19 +30,19 @@ #include #include -namespace sfz +namespace sfz { /** * @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 - * is destroyed. - * + * is destroyed. + * * @tparam Type the underlying type of the buffers * @tparam MaxChannels the maximum number of channels in the buffer * @tparam Alignment the alignment for the buffers */ -template +template class AudioBuffer { public: using value_type = std::remove_cv_t; @@ -54,7 +54,7 @@ public: /** * @brief Construct a new Audio Buffer object - * + * */ AudioBuffer() { @@ -63,9 +63,9 @@ public: /** * @brief Construct a new Audio Buffer object with a specified number of * channels and frames. - * - * @param numChannels - * @param numFrames + * + * @param numChannels + * @param numFrames */ AudioBuffer(int numChannels, int numFrames) : numChannels(numChannels) @@ -77,24 +77,29 @@ public: /** * @brief Resizes all the underlying buffers to a new size. - * - * @param newSize + * + * @param newSize * @return true if the resize worked * @return false otherwise */ bool resize(size_type newSize) { bool returnedOK = true; + for (auto i = 0; i < numChannels; ++i) returnedOK &= buffers[i]->resize(newSize); + + if (returnedOK) + numFrames = newSize; + return returnedOK; } /** * @brief Return an iterator to a specific channel with a non-const type. - * - * @param channelIndex - * @return iterator + * + * @param channelIndex + * @return iterator */ iterator channelWriter(int channelIndex) { @@ -107,9 +112,9 @@ public: /** * @brief Returns a sentinel for the channelWriter(channelIndex) iterator - * - * @param channelIndex - * @return iterator + * + * @param channelIndex + * @return iterator */ iterator channelWriterEnd(int channelIndex) { @@ -122,9 +127,9 @@ public: /** * @brief Returns a const iterator for a specific channel - * - * @param channelIndex - * @return const_iterator + * + * @param channelIndex + * @return const_iterator */ const_iterator channelReader(int channelIndex) const { @@ -137,9 +142,9 @@ public: /** * @brief Returns a sentinel for the channelReader(channelIndex) iterator - * - * @param channelIndex - * @return const_iterator + * + * @param channelIndex + * @return const_iterator */ const_iterator channelReaderEnd(int channelIndex) const { @@ -152,9 +157,9 @@ public: /** * @brief Get a Span for a specific channel - * - * @param channelIndex - * @return absl::Span + * + * @param channelIndex + * @return absl::Span */ absl::Span getSpan(int channelIndex) const { @@ -167,9 +172,9 @@ public: /** * @brief Get a const Span object for a specific channel - * - * @param channelIndex - * @return absl::Span + * + * @param channelIndex + * @return absl::Span */ absl::Span getConstSpan(int channelIndex) const { @@ -178,7 +183,7 @@ public: /** * @brief Add a channel to the buffer with the current number of frames. - * + * */ void addChannel() { @@ -188,8 +193,8 @@ public: /** * @brief Get the number of elements in each buffer - * - * @return size_type + * + * @return size_type */ size_type getNumFrames() const { @@ -198,8 +203,8 @@ public: /** * @brief Get the number of channels - * - * @return int + * + * @return int */ int getNumChannels() const { @@ -208,9 +213,9 @@ public: /** * @brief Check if the buffers contains no elements - * + * * @return true - * @return false + * @return false */ bool empty() const { @@ -219,12 +224,12 @@ public: /** * @brief Get a reference to a given element in a given buffer. - * + * * In release builds this is not checked and may touch bad memory. - * - * @param channelIndex - * @param frameIndex - * @return Type& + * + * @param channelIndex + * @param frameIndex + * @return Type& */ Type& getSample(int channelIndex, size_type frameIndex) { @@ -237,21 +242,46 @@ public: /** * @brief Alias for getSample(...) - * - * @param channelIndex - * @param frameIndex - * @return Type& + * + * @param channelIndex + * @param frameIndex + * @return Type& */ Type& operator()(int channelIndex, size_type 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: using buffer_type = Buffer; using buffer_ptr = std::unique_ptr; + static_assert(MaxChannels > 0, "Need a positive number of channels"); std::array buffers; int numChannels { 0 }; size_type numFrames { 0 }; }; -} \ No newline at end of file +} diff --git a/src/sfizz/AudioSpan.h b/src/sfizz/AudioSpan.h index 4c02106a..0ccfd5e9 100644 --- a/src/sfizz/AudioSpan.h +++ b/src/sfizz/AudioSpan.h @@ -36,15 +36,15 @@ namespace sfz { /** * @brief Extension of the concept of spans to multiple channels. - * + * * 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 * 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. - * 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. * It also protects against overreading a buffer. Users can still indicate - * that they + * that they * @code{.cpp} * constexpr int bufferSize { 1024 }; * void gain(AudioSpan arrayView, float gain) @@ -52,41 +52,41 @@ namespace sfz * for (auto& f: arrayView) * f *= gain; * } - * + * * int main(char argc, char** argv) * { * float leftChannel [bufferSize]; * float rightChannel [bufferSize]; - * + * * for (int i = 0; i < bufferSize; ++i) * { * leftChannel[i] = 1.0f; * rightChannel[i] = 1.0f; * } - * + * * // Type is inferred * AudioSpan explicitView { { leftChannel, rightChannel }, bufferSize }; * // 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 * AudioSpan explicitView2 { { leftChannel, rightChannel } }; - * + * * 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 - * + * * // You can also build spans implicitely * gain({ { leftChannel, rightChannel }, bufferSize }, 0.5f); // elements equal to 0.125f * } * @endcode * You can build AudioSpans from AudioBuffers directly, the AudioBufferT.cpp file in the test * folder show some example. - * 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` from + * 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` from * a `const float *` buffer for example. - * + * * @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 +template class AudioSpan { public: using size_type = size_t; @@ -96,7 +96,7 @@ public: /** * @brief Construct a new Audio Span object - * + * * @param spans an array of MaxChannels pointers to buffers. * @param numChannels the number of spans to take in from the array * @param offset starting offset for the AudioSpan @@ -106,14 +106,14 @@ public: : numFrames(numFrames) , numChannels(numChannels) { - ASSERT(static_cast(numChannels) <= MaxChannels); + ASSERT(numChannels <= MaxChannels); for (auto i = 0; i < numChannels; ++i) this->spans[i] = spans[i] + offset; } /** * @brief Construct a new Audio Span object from initializer lists - * + * * @param spans the list of 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 - * + * * This constructor can be implicitely called for any source that can be cast transparently to - * an absl::Span. The size of the AudioSpan is inferred from the size of the smallest + * an absl::Span. The size of the AudioSpan is inferred from the size of the smallest * absl::Span. - * + * * @param spans a list of objects compatible with absl::Span */ AudioSpan(std::initializer_list> spans) @@ -155,16 +155,16 @@ public: /** * @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 * an AudioBuffer. - * + * * @tparam U the underlying type compatible with the template Type of the AudioSpan * @tparam N the number of channels in the AudioBuffer * @tparam Alignment the alignment block size for the platform * @param audioBuffer the source AudioBuffer. */ - template , typename = std::enable_if_t::value, int>> + template , typename = std::enable_if_t::value, int>> AudioSpan(AudioBuffer& audioBuffer) : numFrames(audioBuffer.getNumFrames()) , numChannels(audioBuffer.getNumChannels()) @@ -176,16 +176,16 @@ public: /** * @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 * an AudioBuffer. - * + * * @tparam U the underlying type compatible with the template Type of the AudioSpan * @tparam N the number of channels in the AudioBuffer * @tparam Alignment the alignment block size for the platform * @param audioBuffer the source AudioBuffer. */ - template > + template > AudioSpan(AudioBuffer& audioBuffer) : numFrames(audioBuffer.getNumFrames()) , numChannels(audioBuffer.getNumChannels()) @@ -197,10 +197,10 @@ public: /** * @brief AudioSpan copy constructor - * + * * @param other the other AudioSpan */ - template > + template > AudioSpan(const AudioSpan& other) : numFrames(other.getNumFrames()) , numChannels(other.getNumChannels()) @@ -212,7 +212,7 @@ public: /** * @brief Get a raw pointer to a specific channel from the AudioSpan - * + * * @param channelIndex the channel * @return Type* the raw pointer to the channel */ @@ -227,9 +227,9 @@ public: /** * @brief Get a Span corresponding to a specific channel - * + * * @param channelIndex the channel - * @return absl::Span + * @return absl::Span */ absl::Span getSpan(int channelIndex) { @@ -242,9 +242,9 @@ public: /** * @brief Get a Span corresponding to a specific channel - * + * * @param channelIndex the channel - * @return absl::Span + * @return absl::Span */ absl::Span 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 */ Type meanSquared() noexcept @@ -272,7 +272,7 @@ public: /** * @brief Fills all the elements of the AudioSpan with the same value - * + * * @param value the filling value */ void fill(Type value) noexcept @@ -284,7 +284,7 @@ public: /** * @brief Apply a gain span elementwise to all channels in the AudioSpan. - * + * * @param gain the gain to apply */ void applyGain(absl::Span gain) noexcept @@ -296,7 +296,7 @@ public: /** * @brief Apply a gain to all channels in the AudioSpan. - * + * * @param gain the gain to apply */ void applyGain(Type gain) noexcept @@ -309,10 +309,10 @@ public: /** * @brief Add another AudioSpan with a compatible number of channels to the current * AudioSpan. - * + * * @param other the other AudioSpan */ - template > + template > void add(AudioSpan& other) { static_assert(!std::is_const::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. - * + * * @param other the other AudioSpan */ - template > + template > void copy(AudioSpan& other) { static_assert(!std::is_const::value, "Can't allow mutating operations on const AudioSpans"); @@ -342,7 +342,7 @@ public: /** * @brief Get the size of this AudioSpan. - * + * * @returns size_type the number of frames in the AudioSpan */ size_type getNumFrames() @@ -352,17 +352,17 @@ public: /** * @brief Get the number of channels of this AudioSpan. - * + * * @returns size_type the number of channels in the AudioSpan */ int getNumChannels() { return numChannels; } - + /** * @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 */ AudioSpan first(size_type length) @@ -373,7 +373,7 @@ public: /** * @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 */ AudioSpan last(size_type length) @@ -387,7 +387,7 @@ public: * taking `length` elements. The new AudioSpan will have `length` elements. This basically * removes the first `offset` elements and the last `numFrames - length - offset` elements * from the AudioSpan. - * + * * @param length the number of elements to take on each channel */ AudioSpan 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 * taking all the remaining elements. This function basically removes the first `offset` * elements from the AudioSpan. The new Audiospan will have size `numFrames - offset`. - * + * * @param length the number of elements to take on each channel */ AudioSpan subspan(size_type offset) @@ -410,8 +410,9 @@ public: } private: + static_assert(MaxChannels > 0, "Need a positive number of channels"); std::array spans; size_type numFrames { 0 }; int numChannels { 0 }; }; -} \ No newline at end of file +}