Files
sizzletracker-android/core/src/engine/ChannelArp.hpp
T
ReactorcoremeltdownandClaude Opus 5 ba98ec56f4 0.41.2 (78): cleanup and optimisation round (Android and iOS)
Nothing user-visible changes, except that the Android app is half the size and
both are lighter on the battery.

R8 is on for release builds (21.5 MB -> 10.1 MB); only the two JNI classes are
pinned by name, since the app reflects over nothing of its own, and `make dist`
keeps the mapping beside the APK. The Kotlin DSP that core/ replaced in 0.37.0
was still compiled into every APK with nothing calling it — it moves to the test
source set, where it belongs as the reference the C++ is checked against — and
the golden tests' TestVoice wrappers leave core/src for core/tests/voices, so
neither app compiles them either.

Speed: channel gain, mute/solo and the FX chain are read once per audio block
instead of once per frame in Engine::fillBlock (both platforms, bit-identical —
all 61 reference scenarios still match); the transport pollers idle at 100 ms
(Android) and 10 Hz (iOS) instead of polling flat out while a song is stopped,
woken by any transport call; ProjectIo.save builds into one pre-sized buffer and
a burst of edits is coalesced into one engine sync; the Kotlin grain sources and
factory wavetables are built when their editor opens rather than at launch, where
they duplicated ~6 MB the engine already renders in core/; and Xcode builds the
DSP core at -O2, the level the golden tests measure, not -Os.

Fix: `make lint` failed on a manifest asking for ACCESS_FINE_LOCATION without
ACCESS_COARSE_LOCATION (the BLE MIDI scan on Android 8-11), so `make check`
passes again.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-11-18 21:01:29 +01:00

112 lines
3.9 KiB
C++

// 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 <cstdint>
#include <cstring>
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