Refactor the oversampling helpers

This commit is contained in:
Paul Ferrand 2019-12-22 23:36:34 +01:00
parent d813cd8a04
commit 9187e0db41
6 changed files with 161 additions and 194 deletions

View file

@ -10,7 +10,7 @@ endif()
# SIMD checks
if (HAVE_X86INTRIN_H AND UNIX)
add_compile_options (-DHAVE_X86INTRIN_H)
set (SFIZZ_SIMD_SOURCES sfizz/SIMDSSE.cpp sfizz/OversamplerSSE.cpp)
set (SFIZZ_SIMD_SOURCES sfizz/SIMDSSE.cpp)
elseif (HAVE_INTRIN_H AND WIN32)
add_compile_options (/DHAVE_INTRIN_H)
set (SFIZZ_SIMD_SOURCES sfizz/SIMDSSE.cpp)
@ -20,9 +20,9 @@ elseif (HAVE_ARM_NEON_H AND UNIX)
add_compile_options (-march=native)
add_compile_options (-mtune=cortex-a53)
add_compile_options (-funsafe-math-optimizations)
set (SFIZZ_SIMD_SOURCES sfizz/SIMDDummy.cpp sfizz/OversamplerDummy.cpp)
set (SFIZZ_SIMD_SOURCES sfizz/SIMDDummy.cpp)
else()
set (SFIZZ_SIMD_SOURCES sfizz/SIMDDummy.cpp sfizz/OversamplerDummy.cpp)
set (SFIZZ_SIMD_SOURCES sfizz/SIMDDummy.cpp)
endif()
set (SFIZZ_SOURCES ${SFIZZ_SOURCES} ${SFIZZ_SIMD_SOURCES})

View file

@ -8,6 +8,7 @@ set (SFIZZ_SOURCES
sfizz/ScopedFTZ.cpp
sfizz/MidiState.cpp
sfizz/SfzHelpers.cpp
sfizz/Oversampler.cpp
sfizz/FloatEnvelopes.cpp
)
include (SfizzSIMDSourceFilesCheck)

145
src/sfizz/Oversampler.cpp Normal file
View file

@ -0,0 +1,145 @@
// Copyright (c) 2019, Paul Ferrand
// All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "Oversampler.h"
#include "Buffer.h"
#include "AudioSpan.h"
constexpr std::array<double, 12> coeffsStage2x {
0.036681502163648017,
0.13654762463195771,
0.27463175937945411,
0.42313861743656667,
0.56109869787919475,
0.67754004997416162,
0.76974183386322659,
0.83988962484963803,
0.89226081800387891,
0.9315419599631839,
0.96209454837808395,
0.98781637073289708
};
constexpr std::array<double, 4> coeffsStage4x {
0.042448989488488006,
0.17072114107630679,
0.39329183835224008,
0.74569514831986694
};
constexpr std::array<double, 3> coeffsStage8x {
0.055748680811302048,
0.24305119574153092,
0.6466991311926823
};
#if defined(__x86_64__) || defined(__i386__)
#include "hiir/Upsampler2xSse.h"
using Upsampler2x = hiir::Upsampler2xSse<coeffsStage2x.size()>;
using Upsampler4x = hiir::Upsampler2xSse<coeffsStage4x.size()>;
using Upsampler8x = hiir::Upsampler2xSse<coeffsStage8x.size()>;
#elif defined(__arm__) || defined(__aarch64__)
#include "hiir/Upsampler2xNeon.h"
using Upsampler2x = hiir::Upsampler2xNeon<coeffsStage2x.size()>;
using Upsampler4x = hiir::Upsampler2xNeon<coeffsStage4x.size()>;
using Upsampler8x = hiir::Upsampler2xNeon<coeffsStage8x.size()>;
#else
#include "hiir/Upsampler2xFpu.h"
using Upsampler2x = hiir::Upsampler2xFpu<coeffsStage2x.size()>;
using Upsampler4x = hiir::Upsampler2xFpu<coeffsStage4x.size()>;
using Upsampler8x = hiir::Upsampler2xFpu<coeffsStage8x.size()>;
#endif
sfz::Oversampler::Oversampler(sfz::Oversampling factor, size_t chunkSize)
: factor(factor), chunkSize(chunkSize)
{
}
void sfz::Oversampler::stream(const sfz::AudioBuffer<float>& input, sfz::AudioBuffer<float>& output, std::atomic<size_t>* framesReady)
{
ASSERT(output.getNumFrames() >= input.getNumFrames() * static_cast<int>(factor));
ASSERT(output.getNumChannels() == input.getNumChannels());
const auto numFrames = input.getNumFrames();
const auto numChannels = input.getNumChannels();
std::vector<Upsampler2x> upsampler2x (numChannels);
std::vector<Upsampler4x> upsampler4x (numChannels);
std::vector<Upsampler8x> upsampler8x (numChannels);
switch(factor)
{
case Oversampling::x8:
for (auto& upsampler: upsampler8x)
upsampler.set_coefs(coeffsStage8x.data());
[[fallthrough]];
case Oversampling::x4:
for (auto& upsampler: upsampler4x)
upsampler.set_coefs(coeffsStage4x.data());
[[fallthrough]];
case Oversampling::x2:
for (auto& upsampler: upsampler2x)
upsampler.set_coefs(coeffsStage2x.data());
[[fallthrough]];
case Oversampling::x1:
for (int i = 0; i < numChannels; ++i)
copy<float>(input.getConstSpan(i), output.getSpan(i));
return;
}
// Intermediate buffers
sfz::Buffer<float> buffer1 { chunkSize * 2 };
sfz::Buffer<float> buffer2 { chunkSize * 4 };
auto span1 = absl::MakeSpan(buffer1);
auto span2 = absl::MakeSpan(buffer2);
size_t inputFrameCounter { 0 };
size_t outputFrameCounter { 0 };
while(inputFrameCounter < numFrames)
{
// std::cout << "Input frames: " << inputFrameCounter << "/" << numFrames << '\n';
const auto thisChunkSize = std::min(chunkSize, numFrames - inputFrameCounter);
const auto outputChunkSize { thisChunkSize * static_cast<int>(factor) };
for (int chanIdx = 0; chanIdx < numChannels; chanIdx++) {
const auto inputChunk = input.getSpan(chanIdx).subspan(inputFrameCounter, chunkSize);
const auto outputChunk = output.getSpan(chanIdx).subspan(outputFrameCounter, outputChunkSize);
upsampler2x[chanIdx].process_block(span1.data(), inputChunk.data(), thisChunkSize);
if (factor == Oversampling::x4) {
upsampler4x[chanIdx].process_block(outputChunk.data(), span1.data(), thisChunkSize * 2);
}
else if (factor == Oversampling::x8) {
upsampler4x[chanIdx].process_block(span2.data(), span1.data(), thisChunkSize * 2);
upsampler8x[chanIdx].process_block(outputChunk.data(), span1.data(), thisChunkSize * 4);
}
}
inputFrameCounter += thisChunkSize;
outputFrameCounter += outputChunkSize;
if (framesReady != nullptr)
framesReady->fetch_add(outputChunkSize);
}
}

View file

@ -26,110 +26,23 @@
#include <memory>
#include "absl/types/span.h"
#include "Debug.h"
#include "Buffer.h"
#include "AudioBuffer.h"
#include "Config.h"
#include "hiir/Upsampler2xFpu.h"
namespace sfz {
constexpr std::array<double, 12> coeffsStage2x {
0.036681502163648017,
0.13654762463195771,
0.27463175937945411,
0.42313861743656667,
0.56109869787919475,
0.67754004997416162,
0.76974183386322659,
0.83988962484963803,
0.89226081800387891,
0.9315419599631839,
0.96209454837808395,
0.98781637073289708
class Oversampler
{
public:
Oversampler() = delete;
Oversampler(Oversampling factor = Oversampling::x1, size_t chunkSize = config::chunkSize);
Oversampler(const Oversampler&) = delete;
Oversampler(Oversampler&&) = delete;
void stream(const AudioBuffer<float>& input, AudioBuffer<float>& output, std::atomic<size_t>* framesReady = nullptr);
private:
Oversampling factor;
size_t chunkSize;
};
constexpr std::array<double, 4> coeffsStage4x {
0.042448989488488006,
0.17072114107630679,
0.39329183835224008,
0.74569514831986694
};
constexpr std::array<double, 3> coeffsStage8x {
0.055748680811302048,
0.24305119574153092,
0.6466991311926823
};
template<bool SIMD=SIMDConfig::upsampling>
void upsample2xStage(absl::Span<const float> input, absl::Span<float> output)
{
ASSERT(output.size() >= 2 * input.size());
hiir::Upsampler2xFpu<coeffsStage2x.size()> upsampler;
upsampler.set_coefs(coeffsStage2x.data());
upsampler.process_block(output.data(), input.data(), input.size());
}
template<bool SIMD=SIMDConfig::upsampling>
void upsample4xStage(absl::Span<const float> input, absl::Span<float> output)
{
ASSERT(output.size() >= 2 * input.size());
hiir::Upsampler2xFpu<coeffsStage4x.size()> upsampler;
upsampler.set_coefs(coeffsStage4x.data());
upsampler.process_block(output.data(), input.data(), input.size());
}
template<bool SIMD=SIMDConfig::upsampling>
void upsample8xStage(absl::Span<const float> input, absl::Span<float> output)
{
ASSERT(output.size() >= 2 * input.size());
hiir::Upsampler2xFpu<coeffsStage8x.size()> upsampler;
upsampler.set_coefs(coeffsStage8x.data());
upsampler.process_block(output.data(), input.data(), input.size());
}
template<>
void upsample2xStage<true>(absl::Span<const float> input, absl::Span<float> output);
template<>
void upsample4xStage<true>(absl::Span<const float> input, absl::Span<float> output);
template<>
void upsample8xStage<true>(absl::Span<const float> input, absl::Span<float> output);
template <class T, bool SIMD=SIMDConfig::upsampling>
std::unique_ptr<sfz::AudioBuffer<T>> upsample2x(const sfz::AudioBuffer<T>& buffer)
{
// auto tempBuffer = std::make_unique<sfz::Buffer<T>>(buffer.getNumFrames() * 2);
auto outputBuffer = std::make_unique<sfz::AudioBuffer<T>>(buffer.getNumChannels(), buffer.getNumFrames() * 2);
for (int channelIdx = 0; channelIdx < buffer.getNumChannels(); channelIdx++) {
sfz::upsample2xStage<SIMD>(buffer.getConstSpan(channelIdx), outputBuffer->getSpan(channelIdx));
}
return outputBuffer;
}
template <class T, bool SIMD=SIMDConfig::upsampling>
std::unique_ptr<sfz::AudioBuffer<T>> upsample4x(const sfz::AudioBuffer<T>& buffer)
{
auto tempBuffer = std::make_unique<sfz::Buffer<T>>(buffer.getNumFrames() * 2);
auto outputBuffer = std::make_unique<sfz::AudioBuffer<T>>(buffer.getNumChannels(), buffer.getNumFrames() * 4);
for (int channelIdx = 0; channelIdx < buffer.getNumChannels(); channelIdx++) {
sfz::upsample2xStage<SIMD>(buffer.getConstSpan(channelIdx), absl::MakeSpan(*tempBuffer));
sfz::upsample4xStage<SIMD>(absl::MakeConstSpan(*tempBuffer), outputBuffer->getSpan(channelIdx));
}
return outputBuffer;
}
template <class T, bool SIMD=SIMDConfig::upsampling>
std::unique_ptr<sfz::AudioBuffer<T>> upsample8x(const sfz::AudioBuffer<T>& buffer)
{
auto tempBuffer2x = std::make_unique<sfz::Buffer<T>>(buffer.getNumFrames() * 2);
auto tempBuffer4x = std::make_unique<sfz::Buffer<T>>(buffer.getNumFrames() * 4);
auto outputBuffer = std::make_unique<sfz::AudioBuffer<T>>(buffer.getNumChannels(), buffer.getNumFrames() * 8);
for (int channelIdx = 0; channelIdx < buffer.getNumChannels(); channelIdx++) {
sfz::upsample2xStage<SIMD>(buffer.getConstSpan(channelIdx), absl::MakeSpan(*tempBuffer2x));
sfz::upsample4xStage<SIMD>(absl::MakeConstSpan(*tempBuffer2x), absl::MakeSpan(*tempBuffer4x));
sfz::upsample8xStage<SIMD>(absl::MakeConstSpan(*tempBuffer4x), outputBuffer->getSpan(channelIdx));
}
return outputBuffer;
}
}

View file

@ -1,40 +0,0 @@
// Copyright (c) 2019, Paul Ferrand
// All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "Oversampler.h"
template<>
void sfz::upsample2xStage<true>(absl::Span<const float> input, absl::Span<float> output)
{
sfz::upsample2xStage<false>(input, output);
}
template<>
void sfz::upsample4xStage<true>(absl::Span<const float> input, absl::Span<float> output)
{
sfz::upsample4xStage<false>(input, output);
}
template<>
void sfz::upsample8xStage<true>(absl::Span<const float> input, absl::Span<float> output)
{
sfz::upsample8xStage<false>(input, output);
}

View file

@ -1,52 +0,0 @@
// Copyright (c) 2019, Paul Ferrand
// All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "Oversampler.h"
#include "hiir/Upsampler2xSse.h"
template<>
void sfz::upsample2xStage<true>(absl::Span<const float> input, absl::Span<float> output)
{
ASSERT(output.size() >= 2 * input.size());
hiir::Upsampler2xSse<sfz::coeffsStage2x.size()> upsampler;
upsampler.set_coefs(sfz::coeffsStage2x.data());
upsampler.process_block(output.data(), input.data(), input.size());
}
template<>
void sfz::upsample4xStage<true>(absl::Span<const float> input, absl::Span<float> output)
{
ASSERT(output.size() >= 2 * input.size());
hiir::Upsampler2xSse<sfz::coeffsStage4x.size()> upsampler;
upsampler.set_coefs(sfz::coeffsStage4x.data());
upsampler.process_block(output.data(), input.data(), input.size());
}
template<>
void sfz::upsample8xStage<true>(absl::Span<const float> input, absl::Span<float> output)
{
ASSERT(output.size() >= 2 * input.size());
hiir::Upsampler2xSse<sfz::coeffsStage8x.size()> upsampler;
upsampler.set_coefs(sfz::coeffsStage8x.data());
upsampler.process_block(output.data(), input.data(), input.size());
}