iOS milestone 2 groundwork: song model in C++, whole-song references from Kotlin
IosGoldenSongs exports .szg/.sng/.szp files and full AudioEngine renders of four songs (demo, mix, waltz, midifx); Model.hpp is the C++ twin of model/. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
c6520277b7
commit
b1dc5ed8db
@@ -0,0 +1,151 @@
|
||||
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include "sizzle/Model.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <charconv>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include "sizzle/Specs.hpp"
|
||||
|
||||
namespace sizzle::model {
|
||||
|
||||
static const TimeSignatureInfo kSignatures[TIME_SIGNATURE_COUNT] = {
|
||||
{"THREE_FOUR", "3/4", 3, 3, {12, 24, 48}},
|
||||
{"FOUR_FOUR", "4/4", 4, 4, {16, 32, 64}},
|
||||
{"FIVE_FOUR", "5/4", 5, 5, {20, 40, 80}},
|
||||
};
|
||||
const TimeSignatureInfo &info(TimeSignature t) { return kSignatures[(int)t]; }
|
||||
|
||||
static const ScaleInfo kScales[SCALE_COUNT] = {
|
||||
{"CHROMATIC", "Chromatic", {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 12},
|
||||
{"MAJOR", "Major", {0, 2, 4, 5, 7, 9, 11}, 7},
|
||||
{"MINOR", "Natural Minor", {0, 2, 3, 5, 7, 8, 10}, 7},
|
||||
{"HARMONIC_MINOR", "Harmonic Minor", {0, 2, 3, 5, 7, 8, 11}, 7},
|
||||
{"DORIAN", "Dorian", {0, 2, 3, 5, 7, 9, 10}, 7},
|
||||
{"PHRYGIAN", "Phrygian", {0, 1, 3, 5, 7, 8, 10}, 7},
|
||||
{"MIXOLYDIAN", "Mixolydian", {0, 2, 4, 5, 7, 9, 10}, 7},
|
||||
{"PENTATONIC_MAJOR", "Pentatonic Major", {0, 2, 4, 7, 9}, 5},
|
||||
{"PENTATONIC_MINOR", "Pentatonic Minor", {0, 3, 5, 7, 10}, 5},
|
||||
{"BLUES", "Blues", {0, 3, 5, 6, 7, 10}, 6},
|
||||
};
|
||||
const ScaleInfo &info(Scale s) { return kScales[(int)s]; }
|
||||
|
||||
Pattern::Pattern(int id_, std::string name_, int length_) : id(id_), name(std::move(name_)), length(length_) {
|
||||
for (auto &t : tracks) t.assign((size_t)length, Cell{Cell::EMPTY, Cell::MAX_VELOCITY, 0});
|
||||
}
|
||||
|
||||
void Pattern::resize(int newLength) {
|
||||
if (newLength == length) return;
|
||||
for (auto &t : tracks) t.resize((size_t)newLength, Cell{Cell::EMPTY, Cell::MAX_VELOCITY, 0});
|
||||
length = newLength;
|
||||
}
|
||||
|
||||
Arrangement::Arrangement() { for (auto &l : slots) l.assign(MAX_BEATS, EMPTY); }
|
||||
|
||||
int Arrangement::canvasBeats(TimeSignature sig) const {
|
||||
return std::clamp(lengthBars * info(sig).beatsPerBar, 1, MAX_BEATS);
|
||||
}
|
||||
|
||||
int Arrangement::patternAt(int lane, int beat) const {
|
||||
return beat >= 0 && beat < MAX_BEATS ? slots[lane][beat] : EMPTY;
|
||||
}
|
||||
|
||||
int Arrangement::lastFilledBeat() const {
|
||||
for (int beat = MAX_BEATS - 1; beat >= 0; --beat)
|
||||
for (int lane = 0; lane < LANE_COUNT; ++lane)
|
||||
if (slots[lane][beat] != EMPTY) return beat;
|
||||
return -1;
|
||||
}
|
||||
|
||||
void Arrangement::set(int lane, int beat, int patternId) {
|
||||
if (lane >= 0 && lane < LANE_COUNT && beat >= 0 && beat < MAX_BEATS) slots[lane][beat] = patternId;
|
||||
}
|
||||
|
||||
Mixer::Mixer() {
|
||||
for (int i = 0; i < CHANNEL_COUNT; ++i) { channels[i].index = i; channels[i].midiChannel = 0; }
|
||||
}
|
||||
|
||||
bool Mixer::anySolo() const {
|
||||
for (auto &c : channels) if (c.solo) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
void ToolboxSlot::fill(const std::string &newType) {
|
||||
type = newType;
|
||||
values = Params();
|
||||
const DeviceSpec *spec = findDevice(newType.c_str());
|
||||
name = spec ? spec->displayName : newType;
|
||||
if (!spec) return;
|
||||
for (int i = 0; i < spec->paramCount; ++i) {
|
||||
const ParamSpec &p = spec->params[i];
|
||||
if (p.isEnum()) {
|
||||
int n = p.choiceCount(), idx = std::clamp((int)p.def, 0, n - 1);
|
||||
values.set(p.key, p.choices[idx]);
|
||||
} else {
|
||||
values.set(p.key, kotlinFloatToString(p.def).c_str());
|
||||
}
|
||||
}
|
||||
++version;
|
||||
}
|
||||
|
||||
void ToolboxSlot::clearSlot() { type.clear(); name.clear(); values = Params(); ++version; }
|
||||
void ToolboxSlot::set(const char *key, float v) { values.set(key, kotlinFloatToString(v).c_str()); ++version; }
|
||||
void ToolboxSlot::set(const char *key, const char *v) { values.set(key, v); ++version; }
|
||||
|
||||
Project::Project() {
|
||||
for (int i = 0; i < Arrangement::LANE_COUNT; ++i)
|
||||
patterns.emplace_back(i, "BLK" + std::to_string(i + 1), info(timeSignature).lengthOptions[0]);
|
||||
for (int i = 0; i < TOOLBOX_SLOTS; ++i) toolbox[i].index = i;
|
||||
}
|
||||
|
||||
Pattern *Project::pattern(int id) {
|
||||
for (auto &p : patterns) if (p.id == id) return &p;
|
||||
return nullptr;
|
||||
}
|
||||
const Pattern *Project::pattern(int id) const {
|
||||
for (auto &p : patterns) if (p.id == id) return &p;
|
||||
return nullptr;
|
||||
}
|
||||
Pattern &Project::activePattern() {
|
||||
Pattern *p = pattern(activePatternId);
|
||||
return p ? *p : patterns.front();
|
||||
}
|
||||
|
||||
// Shortest round-trip digits in Java's layout: plain for 1e-3 <= |v| < 1e7
|
||||
// ("120.0", "0.8"), else computerized scientific ("1.0E-4", "1.2345678E7").
|
||||
// JDK 17's FloatingDecimal is not always shortest; the file-format port pins it
|
||||
// down against the reference files.
|
||||
std::string kotlinFloatToString(float v) {
|
||||
if (std::isnan(v)) return "NaN";
|
||||
if (std::isinf(v)) return v > 0 ? "Infinity" : "-Infinity";
|
||||
if (v == 0.f) return std::signbit(v) ? "-0.0" : "0.0";
|
||||
char buf[64];
|
||||
auto r = std::to_chars(buf, buf + sizeof buf, v, std::chars_format::scientific);
|
||||
std::string sci(buf, r.ptr); // d.dddde±XX
|
||||
size_t e = sci.find('e');
|
||||
std::string mant = sci.substr(0, e);
|
||||
int exp = std::stoi(sci.substr(e + 1));
|
||||
bool neg = mant[0] == '-';
|
||||
if (neg) mant.erase(0, 1);
|
||||
std::string digits;
|
||||
for (char c : mant) if (c != '.') digits += c;
|
||||
std::string out;
|
||||
float a = std::fabs(v);
|
||||
if (a >= 1e-3f && a < 1e7f) {
|
||||
if (exp >= 0) {
|
||||
std::string ip = digits.substr(0, std::min<size_t>(digits.size(), (size_t)exp + 1));
|
||||
while ((int)ip.size() < exp + 1) ip += '0';
|
||||
std::string fp = (size_t)exp + 1 < digits.size() ? digits.substr((size_t)exp + 1) : "0";
|
||||
out = ip + "." + fp;
|
||||
} else {
|
||||
out = "0." + std::string((size_t)(-exp - 1), '0') + digits;
|
||||
}
|
||||
} else {
|
||||
out = digits.substr(0, 1) + "." + (digits.size() > 1 ? digits.substr(1) : "0") + "E" + std::to_string(exp);
|
||||
}
|
||||
return neg ? "-" + out : out;
|
||||
}
|
||||
|
||||
} // namespace sizzle::model
|
||||
Reference in New Issue
Block a user