Added an historical tracking of the mean squared voice power

This commit is contained in:
paulfd 2019-09-13 23:20:57 +02:00
parent c4f681c371
commit a0433a0816
13 changed files with 287 additions and 19 deletions

View file

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

View file

@ -72,12 +72,16 @@ target_link_libraries(bm_pan benchmark absl::span absl::algorithm)
add_executable(bm_mean BM_mean.cpp ${SFIZZ_SIMD_SOURCES})
target_link_libraries(bm_mean benchmark absl::span absl::algorithm)
add_executable(bm_meanSquared BM_meanSquared.cpp ${SFIZZ_SIMD_SOURCES})
target_link_libraries(bm_meanSquared benchmark absl::span absl::algorithm)
add_custom_target(sfizz_benchmarks)
add_dependencies(sfizz_benchmarks
bm_opf_high_vs_low
bm_write
bm_read
bm_mean
bm_meanSquared
bm_fill
bm_mathfuns
bm_gain

View file

@ -125,6 +125,16 @@ public:
return {};
}
Type meanSquared() noexcept
{
if (numChannels == 0)
return 0.0;
Type result = 0.0;
for (int i = 0; i < numChannels; ++i)
result += ::meanSquared<Type>(getConstSpan(i));
return result / numChannels;
}
void fill(Type value) noexcept
{
for (int i = 0; i < numChannels; ++i)

View file

@ -39,6 +39,8 @@ namespace config {
constexpr char defineCharacter { '$' };
constexpr int oversamplingFactor { 2 };
constexpr float A440 { 440.0 };
constexpr unsigned powerHistoryLength { 16 };
constexpr float voiceStealingThreshold { 0.00001 };
} // namespace config
} // namespace sfz
@ -59,5 +61,6 @@ namespace SIMDConfig {
constexpr bool multiplyAdd { false };
constexpr bool copy { false };
constexpr bool pan { true };
constexpr bool mean { true };
constexpr bool mean { false };
constexpr bool meanSquared { false };
}

39
sfizz/HistoricalBuffer.h Normal file
View file

@ -0,0 +1,39 @@
#include <vector>
#include "SIMDHelpers.h"
#include "absl/types/span.h"
template<class ValueType>
class HistoricalBuffer {
public:
HistoricalBuffer() = delete;
HistoricalBuffer(size_t size)
: size(size)
{
resize(size);
}
void resize(int size)
{
buffer.resize(size);
::fill<ValueType>(absl::MakeSpan(buffer), 0.0);
index = 0;
}
void push(ValueType value)
{
if (size > 0) {
buffer[index] = value;
if (++index == size)
index = 0;
}
}
ValueType getAverage() const
{
return ::mean<ValueType>(buffer);
}
private:
std::vector<ValueType> buffer;
size_t size { 0 };
size_t index { 0 };
};

View file

@ -136,4 +136,10 @@ template <>
float mean<float, true>(absl::Span<const float> vector) noexcept
{
return mean<float, false>(vector);
}
template <>
float meanSquared<float, true>(absl::Span<const float> vector) noexcept
{
return meanSquared<float, false>(vector);
}

View file

@ -429,4 +429,23 @@ T mean(absl::Span<const T> vector) noexcept
}
template <>
float mean<float, true>(absl::Span<const float> vector) noexcept;
float mean<float, true>(absl::Span<const float> vector) noexcept;
template <class T, bool SIMD = SIMDConfig::meanSquared>
T meanSquared(absl::Span<const T> vector) noexcept
{
T result { 0.0 };
if (vector.size() == 0)
return result;
auto* value = vector.begin();
while (value < vector.end()) {
result += (*value) * (*value);
value++;
}
return result / static_cast<T>(vector.size());
}
template <>
float meanSquared<float, true>(absl::Span<const float> vector) noexcept;

View file

@ -616,14 +616,14 @@ float mean<float, true>(absl::Span<const float> vector) noexcept
while (unaligned(value))
result += *value++;
auto mmValues = _mm_setzero_ps();
auto mmSums = _mm_setzero_ps();
while(value < lastAligned) {
mmValues = _mm_add_ps(mmValues, _mm_load_ps(value));
mmSums = _mm_add_ps(mmSums, _mm_load_ps(value));
value += TypeAlignment;
}
std::array<float, 4> sseResult;
_mm_store_ps(sseResult.data(), mmValues);
_mm_store_ps(sseResult.data(), mmSums);
for (auto sseValue: sseResult)
result += sseValue;
@ -631,5 +631,42 @@ float mean<float, true>(absl::Span<const float> vector) noexcept
while (value < sentinel)
result += *value++;
return result / static_cast<float>(vector.size());
}
template <>
float meanSquared<float, true>(absl::Span<const float> vector) noexcept
{
float result { 0.0 };
if (vector.size() == 0)
return result;
auto* value = vector.begin();
auto* sentinel = vector.end();
const auto* lastAligned = prevAligned(sentinel);
while (unaligned(value)) {
result += (*value) * (*value);
value++;
}
auto mmSums = _mm_setzero_ps();
while(value < lastAligned) {
const auto mmValues = _mm_load_ps(value);
mmSums = _mm_add_ps(mmSums, _mm_mul_ps(mmValues, mmValues));
value += TypeAlignment;
}
std::array<float, 4> sseResult;
_mm_store_ps(sseResult.data(), mmSums);
for (auto sseValue: sseResult)
result += sseValue;
while (value < sentinel) {
result += (*value) * (*value);
value++;
}
return result / static_cast<float>(vector.size());
}

View file

@ -22,6 +22,7 @@
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "Synth.h"
#include "Config.h"
#include "Debug.h"
#include "ScopedFTZ.h"
#include "StringViewHelpers.h"
@ -34,6 +35,7 @@ sfz::Synth::Synth()
{
for (int i = 0; i < config::numVoices; ++i)
voices.push_back(std::make_unique<Voice>(ccState));
voiceViewArray.reserve(config::numVoices);
}
void sfz::Synth::callback(std::string_view header, const std::vector<Opcode>& members)
@ -235,11 +237,28 @@ bool sfz::Synth::loadSfzFile(const std::filesystem::path& filename)
sfz::Voice* sfz::Synth::findFreeVoice() noexcept
{
auto freeVoice = absl::c_find_if(voices, [](const auto& voice) { return voice->isFree(); });
if (freeVoice == voices.end()) {
DBG("Voices are overloaded, can't start a new note");
return {};
if (freeVoice != voices.end())
return freeVoice->get();
// Find voices that can be stolen
DBG("No free voice, trying to steal");
voiceViewArray.clear();
for (auto& voice: voices)
if (voice->canBeStolen())
voiceViewArray.push_back(voice.get());
absl::c_sort(voices, [](const auto& lhs, const auto& rhs) { return lhs->getSourcePosition() > rhs->getSourcePosition(); });
for (auto* voice: voiceViewArray) {
DBG("Average voice power: " << voice->getMeanSquaredAverage());
if (voice->getMeanSquaredAverage() < config::voiceStealingThreshold) {
DBG("Stealing voice...");
voice->reset();
return voice;
}
}
return freeVoice->get();
DBG("Voices are overloaded, can't start a new note");
return {};
}
void sfz::Synth::getNumActiveVoices() const noexcept

View file

@ -86,8 +86,10 @@ private:
std::optional<uint8_t> defaultSwitch;
std::set<std::string_view> unknownOpcodes;
using RegionPtrVector = std::vector<Region*>;
using VoicePtrVector = std::vector<Voice*>;
std::vector<std::unique_ptr<Region>> regions;
std::vector<std::unique_ptr<Voice>> voices;
VoicePtrVector voiceViewArray;
std::array<RegionPtrVector, 128> noteActivationLists;
std::array<RegionPtrVector, 128> ccActivationLists;

View file

@ -48,7 +48,7 @@ void sfz::Voice::startVoice(Region* region, int delay, int channel, int number,
if (delay < 0)
delay = 0;
DBG("Starting voice with " << region->sample);
// DBG("Starting voice with " << region->sample);
state = State::playing;
speedRatio = static_cast<float>(region->sampleRate / this->sampleRate);
@ -59,7 +59,7 @@ void sfz::Voice::startVoice(Region* region, int delay, int channel, int number,
if (region->volumeCC)
volumedB += normalizeCC(ccState[region->volumeCC->first]) * region->volumeCC->second;
volumeEnvelope.reset(db2mag(volumedB));
DBG("Base volume: " << baseVolumedB << " dB - with modifier: " << volumedB << " dB");
// DBG("Base volume: " << baseVolumedB << " dB - with modifier: " << volumedB << " dB");
baseGain = region->getBaseGain();
baseGain *= region->getCrossfadeGain(ccState);
@ -70,32 +70,32 @@ void sfz::Voice::startVoice(Region* region, int delay, int channel, int number,
if (region->amplitudeCC)
gain *= normalizeCC(ccState[region->amplitudeCC->first]) * normalizePercents(region->amplitudeCC->second);
amplitudeEnvelope.reset(gain);
DBG("Base gain: " << baseGain << " - with modifier: " << gain);
// DBG("Base gain: " << baseGain << " - with modifier: " << gain);
basePan = normalizeNegativePercents(region->pan);
auto pan = basePan;
if (region->panCC)
pan += normalizeCC(ccState[region->panCC->first]) * normalizeNegativePercents(region->panCC->second);
panEnvelope.reset(pan);
DBG("Base pan: " << basePan << " - with modifier: " << pan);
// DBG("Base pan: " << basePan << " - with modifier: " << pan);
basePosition = normalizeNegativePercents(region->position);
auto position = basePosition;
if (region->positionCC)
position += normalizeCC(ccState[region->positionCC->first]) * normalizeNegativePercents(region->positionCC->second);
positionEnvelope.reset(position);
DBG("Base position: " << basePosition << " - with modifier: " << position);
// DBG("Base position: " << basePosition << " - with modifier: " << position);
baseWidth = normalizeNegativePercents(region->width);
auto width = baseWidth;
if (region->widthCC)
width += normalizeCC(ccState[region->widthCC->first]) * normalizeNegativePercents(region->widthCC->second);
widthEnvelope.reset(width);
DBG("Base width: " << baseWidth << " - with modifier: " << width);
// DBG("Base width: " << baseWidth << " - with modifier: " << width);
sourcePosition = region->getOffset();
floatPosition = static_cast<float>(sourcePosition);
DBG("Offset: " << floatPosition);
// DBG("Offset: " << floatPosition);
initialDelay = delay + region->getDelay();
baseFrequency = midiNoteFrequency(number) * pitchRatio;
prepareEGEnvelope(delay, value);
@ -230,8 +230,10 @@ void sfz::Voice::renderBlock(AudioSpan<float> buffer) noexcept
ASSERT(static_cast<int>(buffer.getNumFrames()) <= samplesPerBlock);
buffer.fill(0.0f);
if (state == State::idle || region == nullptr)
if (state == State::idle || region == nullptr) {
powerHistory.push(0.0);
return;
}
if (region->isGenerator())
fillWithGenerator(buffer);
@ -245,6 +247,8 @@ void sfz::Voice::renderBlock(AudioSpan<float> buffer) noexcept
if (!egEnvelope.isSmoothing())
reset();
powerHistory.push(buffer.meanSquared());
}
void sfz::Voice::processMono(AudioSpan<float> buffer) noexcept
@ -471,4 +475,19 @@ void sfz::Voice::garbageCollect() noexcept
void sfz::Voice::expectFileData(unsigned ticket)
{
this->ticket = ticket;
}
}
float sfz::Voice::getMeanSquaredAverage() const noexcept
{
return powerHistory.getAverage();
}
bool sfz::Voice::canBeStolen() const noexcept
{
return state == State::release;
}
float sfz::Voice::getSourcePosition() const noexcept
{
return floatPosition;
}

View file

@ -25,6 +25,7 @@
#include "ADSREnvelope.h"
#include "Config.h"
#include "LinearEnvelope.h"
#include "HistoricalBuffer.h"
#include "Region.h"
#include "AudioBuffer.h"
#include "AudioSpan.h"
@ -59,6 +60,7 @@ public:
void renderBlock(AudioSpan<float, 2> buffer) noexcept;
bool isFree() const noexcept;
bool canBeStolen() const noexcept;
int getTriggerNumber() const noexcept;
int getTriggerChannel() const noexcept;
uint8_t getTriggerValue() const noexcept;
@ -66,6 +68,9 @@ public:
void reset() noexcept;
void garbageCollect() noexcept;
float getMeanSquaredAverage() const noexcept;
float getSourcePosition() const noexcept;
private:
void fillWithData(AudioSpan<float> buffer) noexcept;
void fillWithGenerator(AudioSpan<float> buffer) noexcept;
@ -126,6 +131,7 @@ private:
LinearEnvelope<float> positionEnvelope;
LinearEnvelope<float> widthEnvelope;
HistoricalBuffer<float> powerHistory { config::powerHistoryLength };
LEAK_DETECTOR(Voice);
};

View file

@ -696,5 +696,19 @@ TEST_CASE("[Helpers] Mean (SIMD vs scalar)")
{
std::vector<float> input(bigBufferSize);
absl::c_iota(input, 0.0);
REQUIRE(mean<float, false>(input) == mean<float, true>(input));
REQUIRE(mean<float, false>(input) == Approx(mean<float, true>(input)).margin(0.001));
}
TEST_CASE("[Helpers] Mean Squared")
{
std::array<float, 10> input { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f };
REQUIRE(meanSquared<float, false>(input) == 38.5f);
REQUIRE(meanSquared<float, true>(input) == 38.5f);
}
TEST_CASE("[Helpers] Mean Squared (SIMD vs scalar)")
{
std::vector<float> input(medBufferSize);
absl::c_iota(input, 0.0);
REQUIRE(meanSquared<float, false>(input) == meanSquared<float, true>(input));
}