iOS 0.3.1: engine, model and file formats in C++, verified against Kotlin; longer settings

Engine (sequencer, mixer, FX chains, MIDI effects) renders four whole songs like
AudioEngine.kt (two bit-exact, two within 9e-6). ProjectIo / SngFormat / PresetIo
round-trip the Kotlin-written files; Float.toString is JDK 17's, checked on all
2^32 floats. Params now holds 255-character values (sample ids were cut).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Reactorcoremeltdown
2026-11-16 01:50:51 +01:00
co-authored by Claude Opus 5.5
parent b1dc5ed8db
commit 7bc54e6466
20 changed files with 3746 additions and 41 deletions
+141
View File
@@ -0,0 +1,141 @@
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
// The sequencer and mixer engine: a port of audio/AudioEngine.kt (plus its
// ChannelArp, the MIDI LFO, the transposer, the arrangement playlist with loop
// regions, note-offs, mutes/solo, per-channel FX chains, the master limiter).
// Checked against whole songs rendered by the Kotlin engine (core/tests/engine_test).
//
// Not here: the audio output (the app calls fillBlock from its AVAudioEngine
// render callback), WAV writing for master recording (plug in a MasterSink),
// CoreMIDI (the engine fills an outgoing MIDI queue; the app drains pollMidiOut).
//
// Threads, as in the Kotlin: setProject / play / pause / stop / rebuild* run on
// the main thread, fillBlock on the audio thread (no allocation, no locks). The
// live note calls (liveNoteOn, auditionSlotOn, busNoteOn, panic, ...) touch the
// voices directly, as the Kotlin does: call them from the audio thread (e.g. drain
// a queue before fillBlock) or otherwise serialise them with it.
#include <cstdint>
#include <memory>
#include <string>
namespace sizzle::model {
struct Project;
}
namespace sizzle::sampler {
class SampleStore;
}
namespace sizzle::engine {
/** TransportState.kt: where playback is, for the playhead. */
struct TransportState {
bool isPlaying = false;
/** Absolute line index within the currently-playing pattern. */
int currentLine = 0;
/** Beat index within the arrangement. */
int currentBeat = 0;
};
/** Kotlin's process-wide voice state at the moment the engine is built: the Morph
* Pad's shared clock (FrasilClock.seconds) and the seed counters every new
* FrasilVoice / GrainCloud draws from. The defaults are a fresh process's. */
struct EngineGlobals {
double clockSeconds = 0.0;
uint32_t frasilSeeds = 0x2545F491u;
uint32_t grainSeeds = 0x1234567u;
};
/** Where a master recording goes (MasterRecorder.kt, minus the file): begin() when
* an armed take starts (false = can't record), write() per output frame on the
* audio thread (must not block), endCapture() after the FX tail. */
class MasterSink {
public:
virtual ~MasterSink() = default;
virtual bool isActive() const = 0;
virtual bool begin() = 0;
virtual void write(float left, float right) = 0;
virtual void endCapture() = 0;
};
class Engine {
public:
static constexpr int BLOCK_FRAMES = 192; // the Kotlin's block (the MIDI LFO steps by it)
static constexpr int POLYPHONY = 8;
static constexpr int LIVE_POLYPHONY = 8;
static constexpr int AUDITION_POLYPHONY = 8;
static constexpr int BUS_POLYPHONY = 8;
/** [store]: where Sampler / SF2 / Grain Cloud samples are looked up (may be null:
* no samples). Builds every voice in AudioEngine's order from [globals]. */
explicit Engine(int sampleRate = 48000, sampler::SampleStore *store = nullptr,
const EngineGlobals &globals = {});
~Engine();
Engine(const Engine &) = delete;
Engine &operator=(const Engine &) = delete;
int sampleRate() const;
// ---- song (main thread)
/** The song to play; it must outlive the engine or the next setProject. */
void setProject(model::Project *p);
/** Re-resolve each channel's FX slots into effect processors (routing changed). */
void rebuildFxChains();
/** Rebuild the arrangement playlist and reset the position. */
void rebuildArrangement();
/** Rebuild the playlist (loop region edits) without moving the position. */
void refreshLoopPlaylist();
// ---- transport (main thread)
void play(int startBeat = -1);
void pause();
/** Stop; if already stopped, rewind to the top. */
void stop();
/** Hard-silence every voice and cancel the arpeggiators. */
void panic();
TransportState transport() const;
bool isPlaying() const;
// ---- audio thread
/** Render [frames] interleaved stereo frames into [out] (frames * 2 floats). */
void fillBlock(float *out, int frames);
// ---- live notes (see the thread note above)
void liveNoteOn(int pitch, int velocity);
void liveNoteOff(int pitch);
void auditionSlotOn(int slotIndex, int midi);
void auditionSlotOff(int midi);
void busNoteOn(int midiChannel, int midi, int velocity);
void busNoteOff(int midiChannel, int midi);
/** For a MIDI trace: what busNoteOn would play (allocates; not for the audio thread). */
std::string describeBusTargets(int midiChannel, int midi) const;
// ---- mixer meters (any thread, display only)
float channelMeter(int ch) const;
bool channelClipped(int ch) const;
/** Effect visualisation isn't ported: always -1 (nothing to draw). */
int effectScope(int slotIndex, float *out, int capacity) const;
float effectPlayhead(int slotIndex) const;
// ---- outgoing MIDI: (status << 16) | (data1 << 8) | data2, or -1 when empty
void setMidiOutEnabled(bool on);
bool midiOutEnabled() const;
int pollMidiOut();
// ---- master recording
void setMasterSink(MasterSink *sink);
void setRecordingArm(bool armed, int tailBars);
void setRecordTailBars(int tailBars);
bool recordArmed() const;
void externalClockStart();
void externalClockStop();
class Impl;
private:
std::unique_ptr<Impl> impl;
};
} // namespace sizzle::engine
+6 -1
View File
@@ -133,7 +133,7 @@ struct ToolboxSlot {
void clearSlot();
float f(const char *key, float fallback = 0.f) const { return values.f(key, fallback); }
const char *s(const char *key, const char *fallback = "") const { return values.s(key, fallback); }
void set(const char *key, float v); // stored as Kotlin's Float.toString text
void set(const char *key, float v); // stored as Kotlin's Float.toString text (no allocation)
void set(const char *key, const char *v);
};
@@ -160,5 +160,10 @@ struct Project {
/** Kotlin's Float.toString (JDK 17's algorithm), e.g. "120.0", "0.8", "1.0E-4":
* what Android writes into settings and files. */
std::string kotlinFloatToString(float v);
/** Buffer size for kotlinFloatToChars (at most 26 chars + NUL). */
inline constexpr int KOTLIN_FLOAT_CHARS = 32;
/** The same text written into [out] (KOTLIN_FLOAT_CHARS bytes), NUL-terminated;
* returns its length. No allocation: safe on the audio thread. */
int kotlinFloatToChars(float v, char *out);
} // namespace sizzle::model
+3 -3
View File
@@ -17,9 +17,9 @@ namespace sizzle {
*/
class Params {
public:
static constexpr int kMax = 96;
static constexpr int kKeyLen = 24;
static constexpr int kValLen = 32;
static constexpr int kMax = 128;
static constexpr int kKeyLen = 32;
static constexpr int kValLen = 256; // sample ids and file paths
/** All of [spec]'s defaults, like ToolboxSlot.fill(). */
void fill(const DeviceSpec &spec);