Files
sizzletracker-android/core/include/sizzle/Engine.hpp
T
ReactorcoremeltdownandClaude Opus 5.5 2bc5cb1cbb 0.37.0 (71): one engine, one version for Android and iOS
- Three source trees: core/ (shared C++ engine, moved from ios/core),
  app/ (Android UI), ios/app/ (iOS UI)
- Android plays through core/ via JNI (EngineHost, jni_engine.cpp): the Oboe
  callback renders natively; the Kotlin model is mirrored as project text and
  applied as a diff, so playback continues through edits
- The Kotlin engine moves to the unit tests as ReferenceEngine (golden reference)
- Shared MasterRecorder in core/src/host, used by both apps; host_test
- versions.properties: one versionName/versionCode for both platforms,
  continuing Android's numbering (iOS 0.6.0/11 -> 0.37.0/71)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 14:49:46 +01:00

146 lines
5.6 KiB
C++

// 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 (AudioEngine.effectScope / effectPlayhead), any thread:
* toolbox [slotIndex]'s live scope (oldest → newest, up to [capacity] samples)
* and read head (0..1) from the first channel chain that runs it, or -1 when it
* isn't a scope effect placed on a mixer channel (nothing to draw). Lock-free;
* chains swapped out meanwhile are kept alive until the read is done. */
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