Skip to content

Commit

Permalink
Update GlitchSprinkler (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryukau committed Jul 19, 2024
1 parent 14ddef7 commit 9bd8ab9
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
39 changes: 28 additions & 11 deletions GlitchSprinkler/source/dsp/dspcore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ void DSPCore::setup(double sampleRate_)

SmootherCommon<double>::setTime(double(0.2));

terminationLength = uint_fast32_t(double(0.002) * sampleRate);
lowpassInterpRate = sampleRate / double(48000 * 64);

reset();
Expand Down Expand Up @@ -105,6 +106,7 @@ void Voice::reset()
arpeggioTie = 1;
arpeggioTimer = 0;
arpeggioLoopCounter = 0;
terminationCounter = 0;

scheduleUpdateNote = false;
phasePeriod = 0;
Expand Down Expand Up @@ -133,6 +135,9 @@ void DSPCore::reset()

previousBeatsElapsed = 0;

currentBeat = 0;
pitchModifier = double(1);

polynomial.updateCoefficients(true);
isPolynomialUpdated = true;

Expand Down Expand Up @@ -183,10 +188,15 @@ std::array<double, 2> Voice::processFrame()
constexpr double epsf32 = double(std::numeric_limits<float>::epsilon());
if (
(state == State::terminate && phaseCounter == 0)
|| (state == State::release && lastGain <= epsf32))
|| (state == State::release && lastGain <= epsf32) || (terminationCounter > 0))
{
state = State::rest;
return {double(0), double(0)};
if (terminationCounter < core.terminationLength && pv[ID::filterSwitch]->getInt()) {
++terminationCounter;
} else {
terminationCounter = core.terminationLength;
state = State::rest;
return {double(0), double(0)};
}
}

// Oscillator.
Expand All @@ -206,11 +216,6 @@ std::array<double, 2> Voice::processFrame()
// Saturation.
sig = std::clamp(saturationGain * sig, double(-1), double(1));

// Envelope.
decayGain *= decayRatio;
lastGain = decayGain * noteVelocity;
sig *= lastGain;

// Lowpass.
if (pv[ID::filterSwitch]->getInt()) {
sig = lowpass.process(
Expand All @@ -220,6 +225,13 @@ std::array<double, 2> Voice::processFrame()
filterDecayRatio *= filterDecayGain;
}

// Envelope.
decayGain *= decayRatio;
const auto terminationDecay = double(core.terminationLength - terminationCounter)
/ double(core.terminationLength);
lastGain = decayGain * noteVelocity * terminationDecay;
sig *= lastGain;

if (++phaseCounter >= phasePeriod) {
phaseCounter = 0;
if (scheduleUpdateNote) {
Expand Down Expand Up @@ -314,17 +326,20 @@ void DSPCore::noteOn(NoteInfo &info)
vc.unisonRatio = double(idx) / double(nUnison - 1);
}

vc.unisonGain = double(1) / double(nUnison);
vc.unisonGain = pv[ID::unisonGainSqrt]->getInt()
? std::sqrt(double(1) / double(nUnison))
: double(1) / double(nUnison);
vc.rngArpeggio.seed(pv[ID::seed]->getInt() + vc.unisonIndex);

if (vc.state == Voice::State::rest && vc.phaseCounter == 0) {
if (vc.state == Voice::State::rest) {
vc.arpeggioTimer = 0;
vc.arpeggioLoopCounter = 0;
vc.lowpass.reset();
vc.updateNote();
} else if (!param.value[ParameterID::arpeggioSwitch]->getInt()) {
vc.scheduleUpdateNote = true;
}
vc.terminationCounter = 0;
vc.state = Voice::State::active;
}
}
Expand Down Expand Up @@ -364,7 +379,9 @@ template<typename Rng, typename Scale, typename Tuning>
inline double getRandomPitch(Rng &rng, const Scale &scale, const Tuning &tuning)
{
std::uniform_int_distribution<size_t> indexDist{0, scale.size() - 1};
return tuning[scale[indexDist(rng)]];
const size_t index = scale[indexDist(rng)];
const size_t octave = size_t(1) << (index / tuning.size());
return octave * tuning[index % tuning.size()];
}

void Voice::updateNote()
Expand Down
2 changes: 2 additions & 0 deletions GlitchSprinkler/source/dsp/dspcore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class Voice {
uint_fast32_t arpeggioTie = 1;
uint_fast32_t arpeggioTimer = 0;
uint_fast32_t arpeggioLoopCounter = 0;
uint_fast32_t terminationCounter = 0;

bool scheduleUpdateNote = false;
uint_fast32_t phasePeriod = 0;
Expand Down Expand Up @@ -116,6 +117,7 @@ class DSPCore {

uint_fast32_t arpeggioDuration = std::numeric_limits<uint_fast32_t>::max();
uint_fast32_t arpeggioLoopLength = std::numeric_limits<uint_fast32_t>::max();
uint_fast32_t terminationLength = 0;

double lowpassInterpRate = double(1) / double(64);
DecibelScale<double> velocityMap{-60, 0, true};
Expand Down
3 changes: 3 additions & 0 deletions GlitchSprinkler/source/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@ bool Editor::prepareUI()
addCheckbox(
unisonLeft0, unisonTop4, labelWidth, labelHeight, uiTextSize, "Scatter Arpeggio",
ID::unisonScatterArpeggio);
addCheckbox(
unisonLeft1, unisonTop4, labelWidth, labelHeight, uiTextSize, "Gain Sqrt.",
ID::unisonGainSqrt);

// Plugin name.
constexpr auto splashMargin = uiMargin;
Expand Down
2 changes: 1 addition & 1 deletion GlitchSprinkler/source/parameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ UIntScale<double> Scales::arpeggioScale(31);
LinearScale<double> Scales::arpeggioPicthDriftCent(0, 100);
UIntScale<double> Scales::arpeggioOctave(7);

UIntScale<double> Scales::unisonVoice(127);
UIntScale<double> Scales::unisonVoice(255);
LinearScale<double> Scales::unisonDetuneCent(0, 1200);

} // namespace Synth
Expand Down
3 changes: 3 additions & 0 deletions GlitchSprinkler/source/parameter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ enum ID {
unisonDetuneCent,
unisonPanSpread,
unisonScatterArpeggio,
unisonGainSqrt,

reservedParameter0,
reservedGuiParameter0 = reservedParameter0 + nReservedParameter,
Expand Down Expand Up @@ -297,6 +298,8 @@ struct GlobalParameter : public ParameterInterface {
Info::kCanAutomate);
value[ID::unisonScatterArpeggio] = std::make_unique<UIntValue>(
1, Scales::boolScale, "unisonScatterArpeggio", Info::kCanAutomate);
value[ID::unisonGainSqrt] = std::make_unique<UIntValue>(
1, Scales::boolScale, "unisonGainSqrt", Info::kCanAutomate);

for (size_t idx = 0; idx < nReservedParameter; ++idx) {
auto indexStr = std::to_string(idx);
Expand Down

0 comments on commit 9bd8ab9

Please sign in to comment.