From 15a04a5dad595fa774da1085253159a6e71b9a05 Mon Sep 17 00:00:00 2001 From: Julian Hurst Date: Thu, 21 Nov 2024 14:56:36 +0100 Subject: Add writeevents and writemidievents --- cmd/example/cli.ha | 5 +++-- cmd/example/simple.ha | 4 ++-- midi/midi.ha | 18 +++++++++++++++--- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/cmd/example/cli.ha b/cmd/example/cli.ha index edd45be..b981e17 100644 --- a/cmd/example/cli.ha +++ b/cmd/example/cli.ha @@ -62,8 +62,9 @@ export fn main() void = { }; const note = runetonote(runes[0]); const pitch = strconv::stou32(spl[2])!; - midi::noteon(&tc, 0, note, 64, pitch); - midi::noteoff(&tc, dt, note, 64, pitch); + const tp = midi::playnote(dt, note, 64, pitch); + append(tc.ev, tp.0); + append(tc.ev, tp.1); }; }; diff --git a/cmd/example/simple.ha b/cmd/example/simple.ha index 98aa607..733baec 100644 --- a/cmd/example/simple.ha +++ b/cmd/example/simple.ha @@ -12,8 +12,8 @@ export fn main() void = { let tc = midi::newtrack(); defer midi::finishtrack(tc); - midi::noteon(&tc, 0, midi::note::C); - midi::noteoff(&tc, 4, midi::note::C); + append(tc.ev, midi::noteon(0, midi::note::C)); + append(tc.ev, midi::noteoff(4, midi::note::C)); midi::writeheader(os::stdout, th); midi::writechunk(os::stdout, tc); }; diff --git a/midi/midi.ha b/midi/midi.ha index fd7a843..cdcb997 100644 --- a/midi/midi.ha +++ b/midi/midi.ha @@ -141,7 +141,12 @@ export fn writechunk(h: io::handle, c: trchunk) void = { endian::beputu32(buf, l); io::writeall(h, buf)!; - for (const ev .. c.ev) { + writeevents(h, c.ev...); +}; + +export fn writeevents(h: io::handle, evs: event...) void = { + let buf: [4]u8 = [0...]; + for (const ev .. evs) { let dt = encodevlen(ev.deltatime); endian::beputu32(buf, dt); if (dt <= 127) { @@ -155,14 +160,21 @@ export fn writechunk(h: io::handle, c: trchunk) void = { }; match (ev.ev) { case let m: midi => - io::writeall(h, [m.status])!; - io::writeall(h, m.data)!; + writemidievents(h, m); case => abort(); }; }; }; +export fn writemidievents(h: io::handle, evs: midi...) void = { + let buf: [4]u8 = [0...]; + for (const ev .. evs) { + io::writeall(h, [ev.status])!; + io::writeall(h, ev.data)!; + }; +}; + fn calclength(c: trchunk) u32 = { let length = 0u32; for (const ev .. c.ev) { -- cgit v1.2.3