Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wavetables hq #287

Merged
merged 5 commits into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/sfizz/Defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ namespace Default
constexpr Range<int> oscillatorMultiRange { 1, config::oscillatorsPerVoice };
constexpr float oscillatorDetune { 0 };
constexpr Range<float> oscillatorDetuneRange { -9600, 9600 };
constexpr int oscillatorQuality { 1 };
constexpr Range<int> oscillatorQualityRange { 0, 3 };

// Instrument setting: voice lifecycle
constexpr uint32_t group { 0 };
Expand Down
2 changes: 2 additions & 0 deletions src/sfizz/Interpolators.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
namespace sfz {

enum InterpolatorModel : int {
// a nearest interpolator
kInterpolatorNearest,
// a linear interpolator
kInterpolatorLinear,
// a Hermite 3rd order interpolator
Expand Down
13 changes: 13 additions & 0 deletions src/sfizz/Interpolators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ inline R interpolate(const R* values, R coeff)
return Interpolator<M, R>::process(values, coeff);
}

//------------------------------------------------------------------------------
// Nearest

template <class R>
class Interpolator<kInterpolatorNearest, R>
{
public:
static inline R process(const R* values, R coeff)
{
return values[coeff > static_cast<R>(0.5)];
}
};

//------------------------------------------------------------------------------
// Linear

Expand Down
6 changes: 6 additions & 0 deletions src/sfizz/Region.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ bool sfz::Region::parseOpcode(const Opcode& rawOpcode)
case hash("oscillator_detune"):
setValueFromOpcode(opcode, oscillatorDetune, Default::oscillatorDetuneRange);
break;
case hash("oscillator_quality"):
if (opcode.value == "-1")
oscillatorQuality.reset();
else
setValueFromOpcode(opcode, oscillatorQuality, Default::oscillatorQualityRange);
break;

// Instrument settings: voice lifecycle
case hash("group"): // also polyphony_group
Expand Down
1 change: 1 addition & 0 deletions src/sfizz/Region.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ struct Region {
bool oscillator = false;
int oscillatorMulti = Default::oscillatorMulti;
float oscillatorDetune = Default::oscillatorDetune;
absl::optional<int> oscillatorQuality;

// Instrument settings: voice lifecycle
uint32_t group { Default::group }; // group
Expand Down
10 changes: 8 additions & 2 deletions src/sfizz/Voice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,22 @@ void sfz::Voice::startVoice(Region* region, int delay, int number, float value,
wave = resources.wavePool.getWaveSaw();
break;
}
const float phase = region->getPhase();
const int quality = region->oscillatorQuality.value_or(Default::oscillatorQuality);
for (WavetableOscillator& osc : waveOscillators) {
osc.setWavetable(wave);
osc.setPhase(region->getPhase());
osc.setPhase(phase);
osc.setQuality(quality);
}
setupOscillatorUnison();
} else if (region->oscillator) {
const WavetableMulti* wave = resources.wavePool.getFileWave(region->sampleId.filename());
const float phase = region->getPhase();
const int quality = region->oscillatorQuality.value_or(Default::oscillatorQuality);
for (WavetableOscillator& osc : waveOscillators) {
osc.setWavetable(wave);
osc.setPhase(region->getPhase());
osc.setPhase(phase);
osc.setQuality(quality);
}
setupOscillatorUnison();
} else {
Expand Down
158 changes: 148 additions & 10 deletions src/sfizz/Wavetables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "Wavetables.h"
#include "FilePool.h"
#include "Interpolators.h"
#include "MathHelpers.h"
#include "absl/meta/type_traits.h"
#include <kiss_fftr.h>
Expand Down Expand Up @@ -35,7 +36,8 @@ void WavetableOscillator::setPhase(float phase)
_phase = phase;
}

void WavetableOscillator::process(float frequency, float detuneRatio, float* output, unsigned nframes)
template <InterpolatorModel M>
void WavetableOscillator::processSingle(float frequency, float detuneRatio, float* output, unsigned nframes)
{
float phase = _phase;
float phaseInc = frequency * (detuneRatio * _sampleInterval);
Expand All @@ -48,7 +50,7 @@ void WavetableOscillator::process(float frequency, float detuneRatio, float* out
float position = phase * tableSize;
unsigned index = static_cast<unsigned>(position);
float frac = position - index;
output[i] = interpolate(&table[index], frac);
output[i] = interpolate<M>(&table[index], frac);

phase += phaseInc;
phase -= static_cast<int>(phase);
Expand All @@ -57,7 +59,8 @@ void WavetableOscillator::process(float frequency, float detuneRatio, float* out
_phase = phase;
}

void WavetableOscillator::processModulated(const float* frequencies, float detuneRatio, float* output, unsigned nframes)
template <InterpolatorModel M>
void WavetableOscillator::processModulatedSingle(const float* frequencies, float detuneRatio, float* output, unsigned nframes)
{
float phase = _phase;
float sampleInterval = _sampleInterval;
Expand All @@ -73,7 +76,7 @@ void WavetableOscillator::processModulated(const float* frequencies, float detun
float position = phase * tableSize;
unsigned index = static_cast<unsigned>(position);
float frac = position - index;
output[i] = interpolate(&table[index], frac);
output[i] = interpolate<M>(&table[index], frac);

phase += phaseInc;
phase -= static_cast<int>(phase);
Expand All @@ -82,9 +85,98 @@ void WavetableOscillator::processModulated(const float* frequencies, float detun
_phase = phase;
}

float WavetableOscillator::interpolate(const float* x, float delta)
template <InterpolatorModel M>
void WavetableOscillator::processDual(float frequency, float detuneRatio, float* output, unsigned nframes)
{
return x[0] + delta * (x[1] - x[0]);
float phase = _phase;
float phaseInc = frequency * (detuneRatio * _sampleInterval);

const WavetableMulti& multi = *_multi;
unsigned tableSize = multi.tableSize();
WavetableMulti::DualTable dt = multi.getInterpolationPairForFrequency(frequency);

for (unsigned i = 0; i < nframes; ++i) {
float position = phase * tableSize;
unsigned index = static_cast<unsigned>(position);
float frac = position - index;
output[i] =
(1 - dt.delta) * interpolate<M>(&dt.table1[index], frac) +
dt.delta * interpolate<M>(&dt.table2[index], frac);

phase += phaseInc;
phase -= static_cast<int>(phase);
}

_phase = phase;
}

template <InterpolatorModel M>
void WavetableOscillator::processModulatedDual(const float* frequencies, float detuneRatio, float* output, unsigned nframes)
{
float phase = _phase;
float sampleInterval = _sampleInterval;

const WavetableMulti& multi = *_multi;
unsigned tableSize = multi.tableSize();

for (unsigned i = 0; i < nframes; ++i) {
float frequency = frequencies[i];
float phaseInc = frequency * (detuneRatio * sampleInterval);

WavetableMulti::DualTable dt = multi.getInterpolationPairForFrequency(frequency);

float position = phase * tableSize;
unsigned index = static_cast<unsigned>(position);
float frac = position - index;
output[i] =
(1 - dt.delta) * interpolate<M>(&dt.table1[index], frac) +
dt.delta * interpolate<M>(&dt.table2[index], frac);

phase += phaseInc;
phase -= static_cast<int>(phase);
}

_phase = phase;
}

void WavetableOscillator::process(float frequency, float detuneRatio, float* output, unsigned nframes)
{
int quality = clamp(_quality, 0, 3);

switch (quality) {
case 0:
processSingle<kInterpolatorNearest>(frequency, detuneRatio, output, nframes);
break;
case 1:
processSingle<kInterpolatorLinear>(frequency, detuneRatio, output, nframes);
break;
case 2:
processSingle<kInterpolatorBspline3>(frequency, detuneRatio, output, nframes);
break;
case 3:
processDual<kInterpolatorBspline3>(frequency, detuneRatio, output, nframes);
break;
}
}

void WavetableOscillator::processModulated(const float* frequencies, float detuneRatio, float* output, unsigned nframes)
{
int quality = clamp(_quality, 0, 3);

switch (quality) {
case 0:
processModulatedSingle<kInterpolatorNearest>(frequencies, detuneRatio, output, nframes);
break;
case 1:
processModulatedSingle<kInterpolatorLinear>(frequencies, detuneRatio, output, nframes);
break;
case 2:
processModulatedSingle<kInterpolatorBspline3>(frequencies, detuneRatio, output, nframes);
break;
case 3:
processModulatedDual<kInterpolatorBspline3>(frequencies, detuneRatio, output, nframes);
break;
}
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -200,6 +292,39 @@ unsigned WavetableRange::getOctaveForFrequency(float f)
return clamp<int>(oct, 0, countOctaves - 1);
}

static const auto octaveForFrequencyTable = []()
{
static constexpr unsigned N = 1024;
std::array<float, N> table;

constexpr double fmin = 1 / WavetableRange::frequencyScaleFactor;
constexpr double fmax = (1 << (WavetableRange::countOctaves - 1)) / WavetableRange::frequencyScaleFactor;

for (unsigned i = 0; i < N; ++i) {
double f = fmin + (i * (1.0 / (N - 1))) * (fmax - fmin);
table[i] = std::log2(f * WavetableRange::frequencyScaleFactor);
}

return table;
}();

float WavetableRange::getFractionalOctaveForFrequency(float f)
{
static constexpr unsigned N = octaveForFrequencyTable.size();

constexpr double fmin = 1 / WavetableRange::frequencyScaleFactor;
constexpr double fmax = (1 << (WavetableRange::countOctaves - 1)) / WavetableRange::frequencyScaleFactor;

float pos = (f - fmin) * ((N - 1) / static_cast<float>(fmax - fmin));
int index1 = static_cast<int>(pos);
float frac = pos - index1;
index1 = clamp<int>(index1, 0, N - 1);
int index2 = std::min<int>(index1 + 1, N - 1);

return (1.0f - frac) * octaveForFrequencyTable[index1] +
frac * octaveForFrequencyTable[index2];
}

WavetableRange WavetableRange::getRangeForOctave(int o)
{
WavetableRange range;
Expand Down Expand Up @@ -271,7 +396,7 @@ const WavetableMulti* WavetableMulti::getSilenceWavetable()

void WavetableMulti::allocateStorage(unsigned tableSize)
{
_multiData.resize((tableSize + _tableExtra) * numTables());
_multiData.resize((tableSize + 2 * _tableExtra) * numTables());
_tableSize = tableSize;
}

Expand All @@ -282,9 +407,22 @@ void WavetableMulti::fillExtra()
constexpr unsigned numTables = WavetableMulti::numTables();

for (unsigned m = 0; m < numTables; ++m) {
float* ptr = const_cast<float*>(getTablePointer(m));
for (unsigned i = 0; i < tableExtra; ++i)
ptr[tableSize + i] = ptr[i % tableSize];
float* beg = const_cast<float*>(getTablePointer(m));
float* end = beg + tableSize;
// fill right
float* src = beg;
float* dst = end;
for (unsigned i = 0; i < tableExtra; ++i) {
*dst++ = *src;
src = (src + 1 != end) ? (src + 1) : beg;
}
// fill left
src = end - 1;
dst = beg - 1;
for (unsigned i = 0; i < tableExtra; ++i) {
*dst-- = *src;
src = (src != beg) ? (src - 1) : (end - 1);
}
}
}

Expand Down
Loading