// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later #pragma once // AudioEngine.ChannelArp: per-(lane,track) arpeggiator. Notes are kept sorted // ascending; the linear step sequence is octave-major (all chord notes in octave 0, // then octave 1, ...), walked up / down / up-down / randomly by advance(). genStamp // identifies the line whose chord this holds (the engine's lineTriggerGen). #include #include namespace sizzle::engine { class ChannelArp { public: int velocity = 100; double stepSamples = 0.0, samplesLeft = 0.0; bool resetOnBar = false; int genStamp = -1; /** Active while it holds at least one note. */ bool active() const { return noteCount_ > 0; } /** Start a fresh chord on [note], resetting the phase to step 0. */ void startChord(int note, int vel, int octs, const char *modeStr, double stepSamp, bool resetBar) { noteCount_ = 0; addNote(note); velocity = vel; octaves = clamp(octs, 1, 4); mode = modeOf(modeStr); stepSamples = stepSamp < 1.0 ? 1.0 : stepSamp; samplesLeft = stepSamples; step = 0; dir = 1; resetOnBar = resetBar; pendingReset = false; } /** Replace the chord with [note] while keeping the running phase. */ void resetChord(int note, int vel, int octs, const char *modeStr, double stepSamp, bool resetBar) { noteCount_ = 0; addNote(note); velocity = vel; refresh(octs, modeStr, stepSamp, resetBar); } /** Add a note to the held chord (sorted ascending, de-duplicated). */ void addNote(int note) { const int n = clamp(note, 0, 127); int i = 0; while (i < noteCount_ && notes[i] < n) i++; if (i < noteCount_ && notes[i] == n) return; if (noteCount_ >= MAX_CHORD) return; for (int j = noteCount_; j >= i + 1; --j) notes[j] = notes[j - 1]; notes[i] = n; noteCount_++; } /** Update params without resetting the running phase. */ void refresh(int octs, const char *modeStr, double stepSamp, bool resetBar) { octaves = clamp(octs, 1, 4); mode = modeOf(modeStr); stepSamples = stepSamp < 1.0 ? 1.0 : stepSamp; resetOnBar = resetBar; } void resetToBar() { step = 0; dir = 1; samplesLeft = 0.0; pendingReset = true; } bool consumePendingReset() { bool p = pendingReset; pendingReset = false; return p; } void stop() { noteCount_ = 0; genStamp = -1; } int currentNote() const { if (noteCount_ <= 0) return 0; const int idx = clamp(step, 0, total() - 1); const int oct = idx / noteCount_; return clamp(notes[idx % noteCount_] + 12 * oct, 0, 127); } void advance() { const int t = total(); switch (mode) { case 1: step = (step - 1 + t) % t; break; case 2: if (t > 1) { step += dir; if (step >= t - 1) { step = t - 1; dir = -1; } else if (step <= 0) { step = 0; dir = 1; } } break; case 3: step = t > 1 ? rnd(t) : 0; break; default: step = (step + 1) % t; break; } } private: static constexpr int MAX_CHORD = 8; static int clamp(int v, int lo, int hi) { return v < lo ? lo : (v > hi ? hi : v); } static int modeOf(const char *m) { if (std::strcmp(m, "Down") == 0) return 1; if (std::strcmp(m, "UpDown") == 0) return 2; if (std::strcmp(m, "Random") == 0) return 3; return 0; } int total() const { int t = noteCount_ * octaves; return t < 1 ? 1 : t; } int rnd(int bound) { uint32_t s = rng; s ^= s << 13; s ^= s >> 17; s ^= s << 5; rng = s; return (int)(s % (uint32_t)bound); } int notes[MAX_CHORD] = {}; int noteCount_ = 0; int octaves = 1, mode = 0; int step = 0, dir = 1; bool pendingReset = false; uint32_t rng = 0x2545F491u; }; } // namespace sizzle::engine