Skip to content

Commit

Permalink
Update GlitchSprinkler to 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ryukau committed Aug 11, 2024
1 parent 7cef7c4 commit 7c48e7e
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 57 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
64 changes: 33 additions & 31 deletions GlitchSprinkler/source/dsp/dspcore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void DSPCore::setup(double sampleRate_)
filterSwitch = pv[ID::filterSwitch]->getInt(); \
softEnvelopeSwitch = pv[ID::softEnvelopeSwitch]->getInt(); \
pwmSwitch = pv[ID::pulseWidthModulation]->getInt(); \
pwmBidrection = pv[ID::pulseWidthModBidirectionSwitch]->getInt(); \
pwmBidrection = pv[ID::pulseWidthModBidirectionalSwitch]->getInt(); \
bitmaskSwitch = pv[ID::pulseWidthBitwiseAnd]->getInt(); \
pwmRatio = double(1) - pv[ID::pulseWidthRatio]->getDouble(); \
\
Expand Down Expand Up @@ -168,6 +168,7 @@ void DSPCore::reset()
polynomial.updateCoefficients(true);
isPolynomialUpdated = true;

modifierNotes.resize(0);
midiNotes.resize(0);
activeNote.resize(0);
activeModifier.resize(0);
Expand Down Expand Up @@ -327,6 +328,7 @@ void DSPCore::process(const size_t length, float *out0, float *out1)
const auto beatPerSample = tempo / (double(60) * sampleRate);
std::array<double, 2> frame{};
for (size_t i = 0; i < length; ++i) {
processModifierNote(i);
processMidiNote(i);

pitchModifier = activeModifier.empty()
Expand Down Expand Up @@ -357,10 +359,6 @@ void DSPCore::noteOn(NoteInfo &info)
using ID = ParameterID::ID;
auto &pv = param.value;

if (info.channel == pitchModifierChannel) {
activeModifier.push_back(info);
return;
}
activeNote.push_back(info);

const size_t nUnison = 1 + pv[ID::unisonVoice]->getInt();
Expand Down Expand Up @@ -545,13 +543,13 @@ void Voice::updateNote()
auto transposeRatio = getPitchRatio(
transposeSemitone + noteNumber - 69, tuningRootSemitone, transposeOctave,
transposeCent + noteCent + unisonDetuneCent, tuning);
return core.pitchModifier * transposeRatio * double(440);
return transposeRatio * double(440);
};
auto getDiscreteFreq = [&]() {
auto semitones = double(transposeSemitone + noteNumber - 127) / double(12);
auto cents = (transposeCent + noteCent + unisonDetuneCent) / double(1200);
auto transposeRatio = std::exp2(transposeOctave + semitones + cents);
auto freqHz = core.pitchModifier * transposeRatio * core.sampleRate;
auto freqHz = transposeRatio * core.sampleRate;
switch (tuning) {
default:
case Tuning::discrete2:
Expand Down Expand Up @@ -640,7 +638,7 @@ void Voice::updateNote()
arpeggioTie = 1;
}

freqHz = std::min(double(0.5) * core.sampleRate, freqHz);
freqHz = std::min(double(0.5) * core.sampleRate, core.pitchModifier * freqHz);
phasePeriod = freqHz <= 0 ? 1 : int_fast32_t(std::ceil(core.sampleRate / freqHz));
phaseCounter = 0;

Expand Down Expand Up @@ -719,32 +717,36 @@ void DSPCore::noteOff(int_fast32_t noteId)
using ID = ParameterID::ID;
auto &pv = param.value;

{ // Handling pitch modifiers.
auto it = std::find_if(
activeModifier.begin(), activeModifier.end(),
[&](const NoteInfo &info) { return info.id == noteId; });
if (it != activeModifier.end()) {
activeModifier.erase(it);
return;
}
auto itNote
= std::find_if(activeNote.begin(), activeNote.end(), [&](const NoteInfo &info) {
return info.id == noteId;
});
if (itNote == activeNote.end()) return;
activeNote.erase(itNote);

if (!isPolyphonic && !activeNote.empty()) return;
const auto targetId = isPolyphonic ? noteId : -1;
for (auto &vc : voices) {
if (vc.noteId != targetId) continue;
vc.noteId = -1;
vc.state = noteOffState;
vc.resetArpeggio(pv[ID::seed]->getInt() + vc.unisonIndex);
}
}

{ // Handling notes.
auto itNote
= std::find_if(activeNote.begin(), activeNote.end(), [&](const NoteInfo &info) {
return info.id == noteId;
});
if (itNote == activeNote.end()) return;
activeNote.erase(itNote);
void DSPCore::modNoteOn(NoteInfo &info) { activeModifier.push_back(info); }

if (!isPolyphonic && !activeNote.empty()) return;
const auto targetId = isPolyphonic ? noteId : -1;
for (auto &vc : voices) {
if (vc.noteId != targetId) continue;
vc.noteId = -1;
vc.state = noteOffState;
vc.resetArpeggio(pv[ID::seed]->getInt() + vc.unisonIndex);
}
void DSPCore::modNoteOff(int_fast32_t noteId)
{
using ID = ParameterID::ID;
auto &pv = param.value;

auto it = std::find_if(
activeModifier.begin(), activeModifier.end(),
[&](const NoteInfo &info) { return info.id == noteId; });
if (it != activeModifier.end()) {
activeModifier.erase(it);
return;
}
}

Expand Down
45 changes: 26 additions & 19 deletions GlitchSprinkler/source/dsp/dspcore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ class DSPCore {
// Maybe make it possible to change the pitch modifier channel.
static constexpr size_t pitchModifierChannel = 15;

std::vector<NoteInfo> modifierNotes;
std::vector<NoteInfo> midiNotes;
std::vector<NoteInfo> activeNote;
std::vector<NoteInfo> activeModifier;
Expand All @@ -158,6 +159,7 @@ class DSPCore {

DSPCore()
{
modifierNotes.reserve(2048);
midiNotes.reserve(2048);
activeNote.reserve(2048);
activeModifier.reserve(2048);
Expand All @@ -176,6 +178,8 @@ class DSPCore {
void process(const size_t length, float *out0, float *out1);
void noteOn(NoteInfo &info);
void noteOff(int_fast32_t noteId);
void modNoteOn(NoteInfo &info);
void modNoteOff(int_fast32_t noteId);

void pushMidiNote(
bool isNoteOn,
Expand All @@ -195,27 +199,30 @@ class DSPCore {
note.cent = tuning;
note.velocity = velocity;

if (
midiNotes.back().channel == pitchModifierChannel && midiNotes.back().frame == frame
&& channel != pitchModifierChannel)
{
std::swap(note, midiNotes.back());
if (note.channel == pitchModifierChannel) {
modifierNotes.push_back(note);
} else {
midiNotes.push_back(note);
}
midiNotes.push_back(note);
}

void processMidiNote(size_t frame)
{
while (true) {
auto it = std::find_if(midiNotes.begin(), midiNotes.end(), [&](const NoteInfo &nt) {
return nt.frame == frame;
});
if (it == std::end(midiNotes)) return;
if (it->isNoteOn)
noteOn(*it);
else
noteOff(it->id);
midiNotes.erase(it);
}
#define DEFINE_NOTE_PROC_FUNC(FUNC_NAME, VECTOR, ON_FUNC, OFF_FUNC) \
void FUNC_NAME(size_t frame) \
{ \
while (true) { \
auto it = std::find_if(VECTOR.begin(), VECTOR.end(), [&](const NoteInfo &nt) { \
return nt.frame == frame; \
}); \
if (it == std::end(VECTOR)) return; \
if (it->isNoteOn) { \
ON_FUNC(*it); \
} else { \
OFF_FUNC(it->id); \
} \
VECTOR.erase(it); \
} \
}

DEFINE_NOTE_PROC_FUNC(processMidiNote, midiNotes, noteOn, noteOff);
DEFINE_NOTE_PROC_FUNC(processModifierNote, modifierNotes, modNoteOn, modNoteOff);
};
5 changes: 3 additions & 2 deletions GlitchSprinkler/source/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ bool Editor::prepareUI()
ID::filterSwitch);

addLabel(
filterLeft0, filterTop1, labelWidth, labelHeight, uiTextSize, "Decay Time Ratio");
filterLeft0, filterTop1, labelWidth, labelHeight, uiTextSize,
"Decay Time Ratio [dB]");
addTextKnob(
filterLeft0 + labelWidth + 2 * margin, filterTop1, labelWidth, labelHeight,
uiTextSize, ID::filterDecayRatio, Scales::filterDecayRatio, false, 5);
Expand Down Expand Up @@ -309,7 +310,7 @@ bool Editor::prepareUI()
ID::pulseWidthModulation);
addCheckbox(
waveformLeft0 + labelWidthOneThird * 1, waveformTop8, labelWidthOneThird, labelHeight,
uiTextSize, "Bidirection", ID::pulseWidthModBidirectionSwitch);
uiTextSize, "Bidirectional", ID::pulseWidthModBidirectionalSwitch);
addCheckbox(
waveformLeft0 + labelWidthOneThird * 2, waveformTop8, labelWidthOneThird, labelHeight,
uiTextSize, "Bitwise And", ID::pulseWidthBitwiseAnd);
Expand Down
24 changes: 24 additions & 0 deletions GlitchSprinkler/source/gui/polynomialxypad.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,22 @@ class PolynomialXYPad : public ArrayControl {
editAndUpdateValue();
}

template<typename xFunc, typename yFunc>
void setControlPointsRelative(xFunc fx, yFunc fy)
{
using ID = Steinberg::Synth::ParameterID::ID;

for (size_t idx = 0; idx < nControlPoint; ++idx) {
auto &point = controlPoints[idx];
const auto ratio = double(idx + 1) / double(nControlPoint + 1);
setValueAt(ID::polynomialPointX0 + idx, fx(ratio) + point.x / getWidth());
setValueAt(ID::polynomialPointY0 + idx, fy(ratio) + point.y / getHeight());
}

invalid();
editAndUpdateValue();
}

double triangle(double t)
{
t += double(0.75);
Expand Down Expand Up @@ -367,6 +383,14 @@ class PolynomialXYPad : public ArrayControl {
std::uniform_real_distribution<double> dist{double(0), double(1)};
return dist(dev);
});
} else if (event.character == 't') { // Subtle randomize.
setControlPointsRelative(
[&](double ratio) { return 0; },
[&](double ratio) {
std::random_device dev;
std::uniform_real_distribution<double> dist{double(-0.01), double(0.01)};
return dist(dev);
});
}

event.consumed = true;
Expand Down
6 changes: 3 additions & 3 deletions GlitchSprinkler/source/parameter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ enum ID {
pulseWidthRatio,
pulseWidthModRate,
pulseWidthModulation,
pulseWidthModBidirectionSwitch,
pulseWidthModBidirectionalSwitch,
pulseWidthBitwiseAnd,

randomizeFmIndex,
Expand Down Expand Up @@ -237,8 +237,8 @@ struct GlobalParameter : public ParameterInterface {
Info::kCanAutomate);
value[ID::pulseWidthModulation] = std::make_unique<UIntValue>(
0, Scales::boolScale, "pulseWidthModulation", Info::kCanAutomate);
value[ID::pulseWidthModBidirectionSwitch] = std::make_unique<UIntValue>(
1, Scales::boolScale, "pulseWidthModBidirectionSwitch", Info::kCanAutomate);
value[ID::pulseWidthModBidirectionalSwitch] = std::make_unique<UIntValue>(
1, Scales::boolScale, "pulseWidthModBidirectionalSwitch", Info::kCanAutomate);
value[ID::pulseWidthBitwiseAnd] = std::make_unique<UIntValue>(
0, Scales::boolScale, "pulseWidthBitwiseAnd", Info::kCanAutomate);

Expand Down
4 changes: 2 additions & 2 deletions GlitchSprinkler/source/version.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
#define SUB_VERSION_STR "0"
#define SUB_VERSION_INT 0

#define RELEASE_NUMBER_STR "0"
#define RELEASE_NUMBER_INT 0
#define RELEASE_NUMBER_STR "1"
#define RELEASE_NUMBER_INT 1

#define BUILD_NUMBER_STR "0"
#define BUILD_NUMBER_INT 0
Expand Down

0 comments on commit 7c48e7e

Please sign in to comment.