Add width and position envelopes

This commit is contained in:
paulfd 2019-09-07 15:55:35 +02:00
parent 640ba8a64d
commit 3e0c5b2f75
12 changed files with 393 additions and 10 deletions

View file

@ -0,0 +1,96 @@
// 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 <benchmark/benchmark.h>
#include <random>
#include <numeric>
#include <vector>
#include <cmath>
#include <iostream>
#include "../sfizz/SIMDHelpers.h"
class MultiplyAdd : public benchmark::Fixture {
public:
void SetUp(const ::benchmark::State& state) {
std::random_device rd { };
std::mt19937 gen { rd() };
std::uniform_real_distribution<float> dist { 0, 1 };
input = std::vector<float>(state.range(0));
output = std::vector<float>(state.range(0));
gain = std::vector<float>(state.range(0));
std::fill(output.begin(), output.end(), 1.0f );
std::generate(gain.begin(), gain.end(), [&]() { return dist(gen); });
std::generate(input.begin(), input.end(), [&]() { return dist(gen); });
}
void TearDown(const ::benchmark::State& state [[maybe_unused]]) {
}
std::vector<float> gain;
std::vector<float> input;
std::vector<float> output;
};
BENCHMARK_DEFINE_F(MultiplyAdd, Straight)(benchmark::State& state) {
for (auto _ : state)
{
for (int i = 0; i < state.range(0); ++i)
output[i] += gain[i] * input[i];
}
}
BENCHMARK_DEFINE_F(MultiplyAdd, Scalar)(benchmark::State& state) {
for (auto _ : state)
{
multiplyAdd<float, false>(gain, input, absl::MakeSpan(output));
}
}
BENCHMARK_DEFINE_F(MultiplyAdd, SIMD)(benchmark::State& state) {
for (auto _ : state)
{
multiplyAdd<float, true>(gain, input, absl::MakeSpan(output));
}
}
BENCHMARK_DEFINE_F(MultiplyAdd, Scalar_Unaligned)(benchmark::State& state) {
for (auto _ : state)
{
multiplyAdd<float, false>(absl::MakeSpan(gain).subspan(1), absl::MakeSpan(input).subspan(1), absl::MakeSpan(output).subspan(1));
}
}
BENCHMARK_DEFINE_F(MultiplyAdd, SIMD_Unaligned)(benchmark::State& state) {
for (auto _ : state)
{
multiplyAdd<float, true>(absl::MakeSpan(gain).subspan(1), absl::MakeSpan(input).subspan(1), absl::MakeSpan(output).subspan(1));
}
}
BENCHMARK_REGISTER_F(MultiplyAdd, Straight)->RangeMultiplier(4)->Range(1 << 2, 1 << 12);
BENCHMARK_REGISTER_F(MultiplyAdd, Scalar)->RangeMultiplier(4)->Range(1 << 2, 1 << 12);
BENCHMARK_REGISTER_F(MultiplyAdd, SIMD)->RangeMultiplier(4)->Range(1 << 2, 1 << 12);
BENCHMARK_REGISTER_F(MultiplyAdd, Scalar_Unaligned)->RangeMultiplier(4)->Range(1 << 2, 1 << 12);
BENCHMARK_REGISTER_F(MultiplyAdd, SIMD_Unaligned)->RangeMultiplier(4)->Range(1 << 2, 1 << 12);
BENCHMARK_MAIN();

View file

@ -0,0 +1,85 @@
// 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 <benchmark/benchmark.h>
#include <random>
#include <numeric>
#include <vector>
#include <cmath>
#include <iostream>
#include "../sfizz/SIMDHelpers.h"
class SubArray : public benchmark::Fixture {
public:
void SetUp(const ::benchmark::State& state) {
std::random_device rd { };
std::mt19937 gen { rd() };
std::uniform_real_distribution<float> dist { 0, 1 };
input = std::vector<float>(state.range(0));
output = std::vector<float>(state.range(0));
std::generate(output.begin(), output.end(), [&]() { return dist(gen); });
std::generate(input.begin(), input.end(), [&]() { return dist(gen); });
}
void TearDown(const ::benchmark::State& state [[maybe_unused]]) {
}
std::vector<float> input;
std::vector<float> output;
};
BENCHMARK_DEFINE_F(SubArray, Scalar)(benchmark::State& state) {
for (auto _ : state)
{
subtract<float, false>(input, absl::MakeSpan(output));
}
}
BENCHMARK_DEFINE_F(SubArray, SIMD)(benchmark::State& state) {
for (auto _ : state)
{
subtract<float, true>(input, absl::MakeSpan(output));
}
}
BENCHMARK_DEFINE_F(SubArray, Scalar_Unaligned)(benchmark::State& state) {
for (auto _ : state)
{
subtract<float, false>(absl::MakeSpan(input).subspan(1), absl::MakeSpan(output).subspan(1));
}
}
BENCHMARK_DEFINE_F(SubArray, SIMD_Unaligned)(benchmark::State& state) {
for (auto _ : state)
{
subtract<float, true>(absl::MakeSpan(input).subspan(1), absl::MakeSpan(output).subspan(1));
}
}
BENCHMARK_REGISTER_F(SubArray, Scalar)->RangeMultiplier(4)->Range(1 << 2, 1 << 12);
BENCHMARK_REGISTER_F(SubArray, SIMD)->RangeMultiplier(4)->Range(1 << 2, 1 << 12);
BENCHMARK_REGISTER_F(SubArray, Scalar_Unaligned)->RangeMultiplier(4)->Range(1 << 2, 1 << 12);
BENCHMARK_REGISTER_F(SubArray, SIMD_Unaligned)->RangeMultiplier(4)->Range(1 << 2, 1 << 12);
BENCHMARK_MAIN();

View file

@ -57,6 +57,12 @@ target_link_libraries(bm_ADSR benchmark absl::span absl::algorithm)
add_executable(bm_add BM_add.cpp ${SFIZZ_SIMD_SOURCES})
target_link_libraries(bm_add benchmark absl::span absl::algorithm)
add_executable(bm_multiplyAdd BM_multiplyAdd.cpp ${SFIZZ_SIMD_SOURCES})
target_link_libraries(bm_multiplyAdd benchmark absl::span absl::algorithm)
add_executable(bm_subtract BM_subtract.cpp ${SFIZZ_SIMD_SOURCES})
target_link_libraries(bm_subtract benchmark absl::span absl::algorithm)
add_executable(bm_copy BM_copy.cpp ${SFIZZ_SIMD_SOURCES})
target_link_libraries(bm_copy benchmark absl::span absl::algorithm)
@ -73,4 +79,6 @@ add_dependencies(sfizz_benchmarks
bm_ramp
bm_ADSR
bm_add
bm_subtract
bm_multiplyAdd
)

2
external/Catch2 vendored

@ -1 +1 @@
Subproject commit b77ab74b723cbd55ee12883562e22c7e467dbcb1
Subproject commit f2c2711bdcc583938e444f2039b9ceba840defcf

View file

@ -54,5 +54,7 @@ namespace SIMDConfig {
constexpr bool linearRamp { false };
constexpr bool multiplicativeRamp { true };
constexpr bool add { false };
constexpr bool subtract { false };
constexpr bool multiplyAdd { false };
constexpr bool copy { false };
}

View file

@ -75,4 +75,8 @@ constexpr Type twoPi { 2 * pi<Type> };
template <class Type>
constexpr Type piTwo { pi<Type> / 2 };
template <class Type>
constexpr Type piFour { pi<Type> / 4 };
constexpr Type piFour { pi<Type> / 4 };
template <class Type>
constexpr Type sqrtTwo { 1.414213562373095048801688724209698078569671875376948073176 };
template <class Type>
constexpr Type sqrtTwoInv { 0.707106781186547524400844362104849039284835937688474036588 };

View file

@ -77,6 +77,12 @@ void applyGain<float, true>(absl::Span<const float> gain, absl::Span<const float
applyGain<float, false>(gain, input, output);
}
template <>
void multiplyAdd<float, true>(absl::Span<const float> gain, absl::Span<const float> input, absl::Span<float> output) noexcept
{
multiplyAdd<float, false>(gain, input, output);
}
template <>
float loopingSFZIndex<float, true>(absl::Span<const float> jumps, absl::Span<float> leftCoeff, absl::Span<float> rightCoeff, absl::Span<int> indices, float floatIndex, float loopEnd, float loopStart) noexcept
{
@ -108,6 +114,12 @@ void add<float, true>(absl::Span<const float> input, absl::Span<float> output) n
add<float, false>(input, output);
}
template <>
void subtract<float, true>(absl::Span<const float> input, absl::Span<float> output) noexcept
{
subtract<float, false>(input, output);
}
template <>
void copy<float, true>(absl::Span<const float> input, absl::Span<float> output) noexcept
{

View file

@ -269,6 +269,28 @@ void applyGain<float, true>(float gain, absl::Span<const float> input, absl::Spa
template <>
void applyGain<float, true>(absl::Span<const float> gain, absl::Span<const float> input, absl::Span<float> output) noexcept;
template <class T>
inline void snippetMultiplyAdd(const T*& gain, const T*& input, T*& output)
{
*output++ += (*gain++) * (*input++);
}
template <class T, bool SIMD = SIMDConfig::multiplyAdd>
void multiplyAdd(absl::Span<const T> gain, absl::Span<const T> input, absl::Span<T> output) noexcept
{
ASSERT(gain.size() == input.size());
ASSERT(input.size() <= output.size());
auto* in = input.begin();
auto* g = gain.begin();
auto* out = output.begin();
auto* sentinel = out + std::min(gain.size(), std::min(output.size(), input.size()));
while (out < sentinel)
snippetMultiplyAdd<T>(g, in, out);
}
template <>
void multiplyAdd<float, true>(absl::Span<const float> gain, absl::Span<const float> input, absl::Span<float> output) noexcept;
template <class T>
inline void snippetRampLinear(T*& output, T& value, T step)
{
@ -327,6 +349,26 @@ void add(absl::Span<const T> input, absl::Span<T> output) noexcept
template <>
void add<float, true>(absl::Span<const float> input, absl::Span<float> output) noexcept;
template <class T>
inline void snippetSubtract(const T*& input, T*& output)
{
*output++ -= *input++;
}
template <class T, bool SIMD = SIMDConfig::subtract>
void subtract(absl::Span<const T> input, absl::Span<T> output) noexcept
{
ASSERT(output.size() >= input.size());
auto* in = input.begin();
auto* out = output.begin();
auto* sentinel = out + min(input.size(), output.size());
while (out < sentinel)
snippetSubtract(in, out);
}
template <>
void subtract<float, true>(absl::Span<const float> input, absl::Span<float> output) noexcept;
template <class T>
void snippetCopy(const T*& input, T*& output)

View file

@ -22,6 +22,7 @@
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "SIMDHelpers.h"
#include <xmmintrin.h>
#if HAVE_X86INTRIN_H
#include <x86intrin.h>
@ -304,6 +305,31 @@ void applyGain<float, true>(absl::Span<const float> gain, absl::Span<const float
snippetGainSpan<float>(g, in, out);
}
template <>
void multiplyAdd<float, true>(absl::Span<const float> gain, absl::Span<const float> input, absl::Span<float> output) noexcept
{
auto* in = input.begin();
auto* out = output.begin();
auto* g = gain.begin();
const auto size = std::min(output.size(), std::min(input.size(), gain.size()));
const auto* lastAligned = prevAligned(output.begin() + size);
while (unaligned(out, in, g) && out < lastAligned)
snippetMultiplyAdd<float>(g, in, out);
while (out < lastAligned) {
auto mmOut = _mm_load_ps(out);
mmOut = _mm_add_ps(_mm_mul_ps(_mm_load_ps(g), _mm_load_ps(in)), mmOut);
_mm_store_ps(out, mmOut);
g += TypeAlignment;
in += TypeAlignment;
out += TypeAlignment;
}
while (out < output.end())
snippetMultiplyAdd<float>(g, in, out);
}
template <>
float loopingSFZIndex<float, true>(absl::Span<const float> jumps,
absl::Span<float> leftCoeffs,
@ -496,6 +522,28 @@ void add<float, true>(absl::Span<const float> input, absl::Span<float> output) n
snippetAdd<float>(in, out);
}
template <>
void subtract<float, true>(absl::Span<const float> input, absl::Span<float> output) noexcept
{
ASSERT(output.size() >= input.size());
auto* in = input.begin();
auto* out = output.begin();
auto* sentinel = out + min(input.size(), output.size());
const auto* lastAligned = prevAligned(sentinel);
while (unaligned(in, out) && out < lastAligned)
snippetSubtract<float>(in, out);
while (out < lastAligned) {
_mm_store_ps(out, _mm_sub_ps(_mm_load_ps(in), _mm_load_ps(out)));
out += TypeAlignment;
in += TypeAlignment;
}
while (out < sentinel)
snippetSubtract<float>(in, out);
}
template <>
void copy<float, true>(absl::Span<const float> input, absl::Span<float> output) noexcept
{

View file

@ -63,10 +63,20 @@ void sfz::Voice::startVoice(Region* region, int delay, int channel, int number,
basePan = normalizeNegativePercents(region->pan);
auto pan = basePan;
if (region->panCC)
pan += normalizeCC(ccState[region->amplitudeCC->first]) * normalizeNegativePercents(region->amplitudeCC->second);
pan += normalizeCC(ccState[region->panCC->first]) * normalizeNegativePercents(region->panCC->second);
panEnvelope.reset(pan);
DBG("Base Panning: " << pan);
basePosition = normalizeNegativePercents(region->position);
auto position = basePosition;
if (region->positionCC)
position += normalizeCC(ccState[region->positionCC->first]) * normalizeNegativePercents(region->positionCC->second);
positionEnvelope.reset(position);
baseWidth = normalizeNegativePercents(region->width);
auto width = baseWidth;
if (region->widthCC)
width += normalizeCC(ccState[region->widthCC->first]) * normalizeNegativePercents(region->widthCC->second);
widthEnvelope.reset(width);
sourcePosition = region->getOffset();
floatPosition = static_cast<float>(sourcePosition);
@ -172,9 +182,11 @@ void sfz::Voice::setSamplesPerBlock(int samplesPerBlock) noexcept
this->samplesPerBlock = samplesPerBlock;
tempBuffer1.resize(samplesPerBlock);
tempBuffer2.resize(samplesPerBlock);
tempBuffer3.resize(samplesPerBlock);
indexBuffer.resize(samplesPerBlock);
tempSpan1 = absl::MakeSpan(tempBuffer1);
tempSpan2 = absl::MakeSpan(tempBuffer2);
tempSpan3 = absl::MakeSpan(tempBuffer3);
indexSpan = absl::MakeSpan(indexBuffer);
}
@ -234,13 +246,49 @@ void sfz::Voice::processMono(AudioSpan<float> buffer) noexcept
void sfz::Voice::processStereo(AudioSpan<float> buffer) noexcept
{
const auto numSamples = buffer.getNumFrames();
auto envelopeSpan = tempSpan1.first(numSamples);
amplitudeEnvelope.getBlock(envelopeSpan);
buffer.applyGain(envelopeSpan);
auto span1 = tempSpan1.first(numSamples);
auto span2 = tempSpan2.first(numSamples);
auto span3 = tempSpan3.first(numSamples);
auto leftBuffer = buffer.getSpan(0);
auto rightBuffer = buffer.getSpan(1);
amplitudeEnvelope.getBlock(span1);
buffer.applyGain(span1);
egEnvelope.getBlock(envelopeSpan);
buffer.applyGain(envelopeSpan);
egEnvelope.getBlock(span1);
buffer.applyGain(span1);
// Create mid/side from left/right in the output buffer
::copy<float>(rightBuffer, span1);
::add<float>(leftBuffer, rightBuffer);
::subtract<float>(span1, leftBuffer);
::applyGain<float>(sqrtTwoInv<float>, leftBuffer);
::applyGain<float>(sqrtTwoInv<float>, rightBuffer);
// Apply the width process
widthEnvelope.getBlock(span1);
::fill<float>(span2, 1.0f);
::add<float>(span1, span2);
::applyGain<float>(piFour<float>, span2);
::cos<float>(span2, span1);
::sin<float>(span2, span2);
::applyGain<float>(span1, leftBuffer);
::applyGain<float>(span2, rightBuffer);
// Apply a position to the "left" channel which is supposed to be our mid channel
// TODO: add panning here too?
positionEnvelope.getBlock(span1);
::fill<float>(span2, 1.0f);
::add<float>(span1, span2);
::applyGain<float>(piFour<float>, span2);
::cos<float>(span2, span1);
::sin<float>(span2, span2);
::copy<float>(leftBuffer, span3);
::copy<float>(rightBuffer, leftBuffer);
::multiplyAdd<float>(span1, span3, leftBuffer);
::multiplyAdd<float>(span2, span3, rightBuffer);
::applyGain<float>(sqrtTwoInv<float>, leftBuffer);
::applyGain<float>(sqrtTwoInv<float>, rightBuffer);
}
void sfz::Voice::fillWithData(AudioSpan<float> buffer) noexcept

View file

@ -91,6 +91,8 @@ private:
float pitchRatio { 1.0 };
float baseGain { 1.0 };
float basePan { 0.0 };
float basePosition { 0.0 };
float baseWidth { 0.0 };
float baseFrequency { 440.0 };
float phase { 0.0f };
@ -103,9 +105,11 @@ private:
Buffer<float> tempBuffer1;
Buffer<float> tempBuffer2;
Buffer<float> tempBuffer3;
Buffer<int> indexBuffer;
absl::Span<float> tempSpan1 { absl::MakeSpan(tempBuffer1) };
absl::Span<float> tempSpan2 { absl::MakeSpan(tempBuffer2) };
absl::Span<float> tempSpan3 { absl::MakeSpan(tempBuffer3) };
absl::Span<int> indexSpan { absl::MakeSpan(indexBuffer) };
int samplesPerBlock { config::defaultSamplesPerBlock };
@ -115,6 +119,8 @@ private:
ADSREnvelope<float> egEnvelope;
LinearEnvelope<float> amplitudeEnvelope;
LinearEnvelope<float> panEnvelope;
LinearEnvelope<float> positionEnvelope;
LinearEnvelope<float> widthEnvelope;
LEAK_DETECTOR(Voice);
};

View file

@ -623,6 +623,38 @@ TEST_CASE("[Helpers] Add (SIMD vs scalar)")
REQUIRE(approxEqual<float>(outputScalar, outputSIMD));
}
TEST_CASE("[Helpers] Subtract")
{
std::array<float, 5> input { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
std::array<float, 5> output { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f };
std::array<float, 5> expected { 0.0, -1.0, -2.0, -3.0, -4.0 };
subtract<float, false>(input, absl::MakeSpan(output));
REQUIRE(output == expected);
}
TEST_CASE("[Helpers] Subtract (SIMD)")
{
std::array<float, 5> input { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };
std::array<float, 5> output { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f };
std::array<float, 5> expected { 0.0, -1.0, -2.0, -3.0, -4.0 };
subtract<float, true>(input, absl::MakeSpan(output));
REQUIRE(output == expected);
}
TEST_CASE("[Helpers] Subtract (SIMD vs scalar)")
{
std::vector<float> input(bigBufferSize);
std::vector<float> outputScalar(bigBufferSize);
std::vector<float> outputSIMD(bigBufferSize);
absl::c_iota(input, 0.0);
absl::c_fill(outputScalar, 0.0);
absl::c_fill(outputSIMD, 0.0);
subtract<float, false>(input, absl::MakeSpan(outputScalar));
subtract<float, true>(input, absl::MakeSpan(outputSIMD));
REQUIRE(approxEqual<float>(outputScalar, outputSIMD));
}
TEST_CASE("[Helpers] copy")
{
std::array<float, 5> input { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f };