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

@ -42,7 +42,7 @@ namespace sfz
* @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>;
@ -85,8 +85,13 @@ public:
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;
} }
@ -247,9 +252,34 @@ public:
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

@ -86,7 +86,7 @@ namespace sfz
* @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;
@ -106,7 +106,7 @@ 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;
} }
@ -164,7 +164,7 @@ public:
* @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())
@ -185,7 +185,7 @@ public:
* @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())
@ -200,7 +200,7 @@ public:
* *
* @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())
@ -312,7 +312,7 @@ public:
* *
* @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");
@ -329,7 +329,7 @@ public:
* *
* @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");
@ -410,6 +410,7 @@ 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 };