// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown // SPDX-License-Identifier: GPL-3.0-or-later // sizzle/Midi.hpp against the behaviour the Android unit tests pin down // (MidiByteParserTest, MidiSoundModuleTest) and MidiClock's tempo follow. #include "sizzle/Midi.hpp" #include #include #include using namespace sizzle::midi; static int failures = 0; #define CHECK(c) do { if (!(c)) { std::printf("FAIL %s:%d %s\n", __FILE__, __LINE__, #c); ++failures; } } while (0) struct Rec : NoteSink { std::vector log; void busNoteOn(int ch, int n, int v) override { log.push_back("on " + std::to_string(ch) + " " + std::to_string(n) + " " + std::to_string(v)); } void busNoteOff(int ch, int n) override { log.push_back("off " + std::to_string(ch) + " " + std::to_string(n)); } }; int main() { // ---- parser std::vector> msgs; std::vector rt; MidiByteParser p([&](int s, int a, int b) { msgs.push_back({s, a, b}); }, [&](int s) { rt.push_back(s); }); const uint8_t a[] = {0x90, 60, 100, 62, 90}; // running status p.feed(a, sizeof a); CHECK(msgs.size() == 2 && msgs[1] == (std::vector{0x90, 62, 90})); const uint8_t b1[] = {0x80, 60}, b2[] = {0xF8, 0}; // split + realtime inside p.feed(b1, 2); p.feed(b2, 2); CHECK(msgs.size() == 3 && msgs[2] == (std::vector{0x80, 60, 0}) && rt == std::vector{0xF8}); const uint8_t c[] = {0xC3, 5, 0xF0, 1, 2, 3, 0xF7, 7}; // program change, SysEx skipped, stray byte p.feed(c, sizeof c); CHECK(msgs.size() == 4 && msgs[3] == (std::vector{0xC3, 5, 0})); const uint8_t d[] = {0xF2, 1, 2, 64, 0xB0, 123, 0}; // system common cancels running status msgs.clear(); p.feed(d, sizeof d); CHECK(msgs.size() == 1 && msgs[0] == (std::vector{0xB0, 123, 0})); // ---- sound module Rec r; MidiSoundModule m(r); std::vector mirror; m.listener = [&](bool on, int ch, int n, int) { mirror.push_back((on ? "+" : "-") + std::to_string(ch) + ":" + std::to_string(n)); }; m.sourceConnected(1); m.sourceConnected(2); CHECK(m.onChannelMessage(1, 0x90, 60, 100)); CHECK(m.onChannelMessage(2, 0x91, 64, 80)); CHECK(m.onChannelMessage(1, 0x90, 60, 0)); // velocity 0 = off CHECK(m.heldNotes(1).empty() && m.heldNotes(2).size() == 1); CHECK(!m.onChannelMessage(1, 0xB0, 7, 100)); // other CCs are the caller's m.onChannelMessage(1, 0x92, 70, 90); CHECK(m.onChannelMessage(2, 0xB2, 123, 0)); // channel 3 off from any source CHECK(m.heldNotes(1).empty()); r.log.clear(); m.sourceDisconnected(2); // releases its held note CHECK(r.log == std::vector{"off 2 64"} && m.sourceCount() == 1); CHECK(mirror.front() == "+1:60" && mirror.back() == "-2:64"); // ---- clock follow: 24 pulses at 125 BPM = 20 ms each ClockFollower f; float bpm = 0; int starts = 0, stops = 0; f.onTempo = [&](float b) { bpm = b; }; f.onTransport = [&](bool s) { s ? ++starts : ++stops; }; f.realtime(0xFA, 0); for (int i = 0; i <= 24; ++i) f.realtime(0xF8, 1'000'000'000LL + i * 20'000'000LL); f.realtime(0xFC, 0); CHECK(starts == 1 && stops == 1 && bpm > 124.99f && bpm < 125.01f); std::printf(failures ? "midi_test: %d failure(s)\n" : "midi_test: ok\n", failures); return failures ? 1 : 0; }