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>
170 lines
6.0 KiB
C++
170 lines
6.0 KiB
C++
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
#pragma once
|
|
|
|
// The song model: the C++ twin of the Android app's model/ package (Project,
|
|
// Pattern, Cell, Arrangement, LoopRegion, Mixer, MixerChannel, ToolboxSlot,
|
|
// TimeSignature, Scale, Pitch). Plain data with the same names, fields, defaults
|
|
// and constants, so the file formats (core/src/io) and the engine
|
|
// (core/src/engine) read like their Kotlin originals.
|
|
//
|
|
// Threads: like Android's, the model is edited by the UI and read by the audio
|
|
// thread. Toolbox values are text (sizzle::Params), as ToolboxSlot's map is.
|
|
|
|
#include <array>
|
|
#include <string>
|
|
#include <vector>
|
|
#include "sizzle/Params.hpp"
|
|
|
|
namespace sizzle::model {
|
|
|
|
struct Pitch {
|
|
static constexpr int LOWEST = 0, HIGHEST = 127, MIDDLE_C = 60;
|
|
};
|
|
|
|
struct TimeSignatureInfo {
|
|
const char *name; // enum name, as files store it ("FOUR_FOUR")
|
|
const char *label; // "4/4"
|
|
int linesPerBeat;
|
|
int beatsPerBar;
|
|
int lengthOptions[3];
|
|
int linesPerBar() const { return linesPerBeat * beatsPerBar; }
|
|
};
|
|
|
|
enum class TimeSignature { THREE_FOUR, FOUR_FOUR, FIVE_FOUR };
|
|
inline constexpr int TIME_SIGNATURE_COUNT = 3;
|
|
const TimeSignatureInfo &info(TimeSignature t);
|
|
|
|
struct ScaleInfo {
|
|
const char *name; // "MINOR"
|
|
const char *label; // "Natural Minor"
|
|
int intervals[12];
|
|
int count;
|
|
};
|
|
|
|
enum class Scale { CHROMATIC, MAJOR, MINOR, HARMONIC_MINOR, DORIAN, PHRYGIAN, MIXOLYDIAN,
|
|
PENTATONIC_MAJOR, PENTATONIC_MINOR, BLUES };
|
|
inline constexpr int SCALE_COUNT = 10;
|
|
const ScaleInfo &info(Scale s);
|
|
|
|
struct Cell {
|
|
static constexpr int EMPTY = -1, OFF = -2, MAX_VELOCITY = 0x7F, MAX_CHANNEL = 16;
|
|
int note = EMPTY;
|
|
int velocity = MAX_VELOCITY;
|
|
int channel = 1;
|
|
bool isEmpty() const { return note == EMPTY; }
|
|
bool isNoteOff() const { return note == OFF; }
|
|
bool isPlayable() const { return note >= Pitch::LOWEST && note <= Pitch::HIGHEST; }
|
|
void clear() { note = EMPTY; velocity = MAX_VELOCITY; }
|
|
};
|
|
|
|
struct Pattern {
|
|
static constexpr int TRACK_COUNT = 4;
|
|
int id = 0;
|
|
std::string name = "PTN";
|
|
int length = 16;
|
|
/** tracks[track][line]; cells start with channel 0, as Kotlin's do. */
|
|
std::array<std::vector<Cell>, TRACK_COUNT> tracks;
|
|
|
|
Pattern(int id_ = 0, std::string name_ = "PTN", int length_ = 16);
|
|
Cell &cell(int track, int line) { return tracks[track][line]; }
|
|
const Cell &cell(int track, int line) const { return tracks[track][line]; }
|
|
void resize(int newLength);
|
|
};
|
|
|
|
struct LoopRegion {
|
|
bool enabled = false;
|
|
int start = 0, end = 0, repeats = 2;
|
|
bool isValid() const { return enabled && end > start; }
|
|
};
|
|
|
|
struct Arrangement {
|
|
static constexpr int LANE_COUNT = 8, MAX_BARS = 256, MAX_BEATS = MAX_BARS * 5;
|
|
static constexpr int EMPTY = -1, NOTE_OFF = -2;
|
|
/** slots[lane][beat]: a pattern id, EMPTY or NOTE_OFF. */
|
|
std::array<std::vector<int>, LANE_COUNT> slots;
|
|
std::array<bool, LANE_COUNT> laneMuted{};
|
|
int lengthBars = MAX_BARS;
|
|
bool loopEnabled = false;
|
|
LoopRegion regionA, regionB;
|
|
|
|
Arrangement();
|
|
int canvasBeats(TimeSignature sig) const;
|
|
int patternAt(int lane, int beat) const;
|
|
bool hasBlockAt(int lane, int beat) const { return patternAt(lane, beat) >= 0; }
|
|
int lastFilledBeat() const;
|
|
void set(int lane, int beat, int patternId);
|
|
};
|
|
|
|
struct MixerChannel {
|
|
static constexpr int FX_SLOTS = 4;
|
|
int index = 0;
|
|
int instrumentSlot = -1;
|
|
std::array<int, FX_SLOTS> fxSlots{-1, -1, -1, -1};
|
|
int midiChannel = 1;
|
|
float volume = 0.8f;
|
|
bool mute = false, solo = false, limiter = false;
|
|
bool audible(bool anySolo) const { return !mute && (!anySolo || solo); }
|
|
};
|
|
|
|
struct Mixer {
|
|
static constexpr int CHANNEL_COUNT = Pattern::TRACK_COUNT, FX_SLOTS = MixerChannel::FX_SLOTS;
|
|
static constexpr int NO_DEVICE = -1;
|
|
static constexpr float DEFAULT_VOLUME = 0.8f;
|
|
std::array<MixerChannel, CHANNEL_COUNT> channels;
|
|
Mixer(); // channel i: index i, midiChannel 0 (as Kotlin's Mixer builds them)
|
|
bool anySolo() const;
|
|
};
|
|
|
|
/** One toolbox slot: a device type (ToolboxType name; empty = no device), its
|
|
* display name and its settings as text. */
|
|
struct ToolboxSlot {
|
|
int index = 0;
|
|
std::string type;
|
|
std::string name;
|
|
Params values;
|
|
/** Bumped on every settings write (Kotlin's `version`), so the engine can
|
|
* skip re-reading unchanged slots. */
|
|
uint32_t version = 0;
|
|
|
|
bool isEmpty() const { return type.empty(); }
|
|
/** ToolboxSlot.fill: the type's defaults (spec order), its display name. */
|
|
void fill(const std::string &newType);
|
|
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 (no allocation)
|
|
void set(const char *key, const char *v);
|
|
};
|
|
|
|
struct Project {
|
|
static constexpr int TOOLBOX_SLOTS = 16;
|
|
std::string name = "untitled";
|
|
float tempoBpm = 120.f;
|
|
TimeSignature timeSignature = TimeSignature::FOUR_FOUR;
|
|
Scale scale = Scale::CHROMATIC;
|
|
int rootNote = 0;
|
|
/** LANE_COUNT patterns, ids 0..7, named BLK1..BLK8, first length option. */
|
|
std::vector<Pattern> patterns;
|
|
Arrangement arrangement;
|
|
Mixer mixer;
|
|
std::array<ToolboxSlot, TOOLBOX_SLOTS> toolbox;
|
|
int activePatternId = 0;
|
|
|
|
Project();
|
|
Pattern *pattern(int id);
|
|
const Pattern *pattern(int id) const;
|
|
Pattern &activePattern();
|
|
};
|
|
|
|
/** 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
|