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 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 {
public:
using value_type = std::remove_cv_t<Type>;
@ -85,8 +85,13 @@ public:
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;
}
@ -247,9 +252,34 @@ public:
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<Type, Alignment>;
using buffer_ptr = std::unique_ptr<buffer_type>;
static_assert(MaxChannels > 0, "Need a positive number of channels");
std::array<buffer_ptr, MaxChannels> buffers;
int numChannels { 0 };
size_type numFrames { 0 };

View file

@ -86,7 +86,7 @@ namespace sfz
* @tparam Type the underlying buffer type
* @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 {
public:
using size_type = size_t;
@ -106,7 +106,7 @@ public:
: numFrames(numFrames)
, numChannels(numChannels)
{
ASSERT(static_cast<unsigned int>(numChannels) <= MaxChannels);
ASSERT(numChannels <= MaxChannels);
for (auto i = 0; i < numChannels; ++i)
this->spans[i] = spans[i] + offset;
}
@ -164,7 +164,7 @@ public:
* @tparam Alignment the alignment block size for the platform
* @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)
: numFrames(audioBuffer.getNumFrames())
, numChannels(audioBuffer.getNumChannels())
@ -185,7 +185,7 @@ public:
* @tparam Alignment the alignment block size for the platform
* @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)
: numFrames(audioBuffer.getNumFrames())
, numChannels(audioBuffer.getNumChannels())
@ -200,7 +200,7 @@ public:
*
* @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)
: numFrames(other.getNumFrames())
, numChannels(other.getNumChannels())
@ -312,7 +312,7 @@ public:
*
* @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)
{
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
*/
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)
{
static_assert(!std::is_const<Type>::value, "Can't allow mutating operations on const AudioSpans");
@ -410,6 +410,7 @@ public:
}
private:
static_assert(MaxChannels > 0, "Need a positive number of channels");
std::array<Type*, MaxChannels> spans;
size_type numFrames { 0 };
int numChannels { 0 };