feat(rivet): storyboard composition with flexible lengths and meters
Check and Windows preview / check (patina) (push) Successful in 3m59s
Check and Windows preview / check (rivet) (push) Successful in 2m2s
Check and Windows preview / previews (push) Successful in 1m8s
Independent plugin release / plan (push) Successful in 7s
Check and Windows preview / windows (push) Successful in 2m41s
Independent plugin release / build (push) Successful in 3m0s
Independent plugin release / publish (push) Successful in 9s

Rivet 0.4.0 tells a motif as a story. A timeline of blocks with roles
(statement, answer, departure, return, rest) replaces tasks, pins and
branches. Rivet writes each block from a motif and joins the seams with
a cut, breath, pickup or approach.

- Motifs of 1-8 bars in a bin of 16; storyboards up to 32 bars.
- Meters 2/4, 3/4, 4/4, 5/4, 6/8, 7/8 and 12/8 for import, writing,
  audition and export. Phrygian, harmonic minor and Mixolydian scales.
- Per-block options, Another take and Keep; templates for new boards.
- Editor rebuilt: motif bin, timeline, large piano roll, inspector,
  tabs for generation, harmony and sound roles, 32-step undo/redo.
- Sessions from 0.1-0.3 open as a storyboard from their source motif.
This commit is contained in:
nvrl committed 2026-11-06 02:15:18 +01:00
1 parent abd37fd4fe
commit f1366280ff
37 files changed
+3763 -2549

No files matched your search

+112 -62
View File
@@ -1,6 +1,6 @@
//! Strict Standard MIDI File import/export. Unsupported data is reported, never flattened.
use rivet_core::{
Error, MessageKind, Note, Performance, PerformanceSettings, Phrase, Result, BAR,
Error, MessageKind, Meter, Note, Performance, PerformanceSettings, Phrase, Result,
MAX_PLAYBACK_NOTES, PPQ,
};
@@ -16,6 +16,8 @@ pub struct MidiTrack {
pub struct MidiFile {
pub tracks: Vec<MidiTrack>,
pub tempo: Option<f64>,
/// The file's time signature; track lengths are rounded to its bars.
pub meter: Option<Meter>,
}
fn error(s: &str) -> Error {
Error(s.into())
@@ -108,6 +110,8 @@ pub fn read(bytes: &[u8]) -> Result<MidiFile> {
r.take(header_len - 6)?;
let mut tracks = Vec::new();
let mut tempo = None;
let mut meter: Option<Meter> = None;
let mut lengths = Vec::new();
for track_index in 0..count {
if r.take(4)? != b"MTrk" {
return Err(error("Missing MIDI track chunk."));
@@ -179,11 +183,17 @@ pub fn read(bytes: &[u8]) -> Result<MidiFile> {
if body.len() != 4 {
return Err(error("Invalid time signature."));
}
if body[0] != 4 || body[1] != 2 {
let found = Meter {
beats: body[0],
unit: 1u8.checked_shl(body[1] as u32).unwrap_or(0),
};
found.validate()?;
if meter.is_some_and(|m| m != found) {
return Err(error(
"Only 4/4 motifs are supported in this version.",
"Meter changes inside one file are unsupported. Export one section per meter.",
));
}
meter = Some(found);
}
_ => {}
}
@@ -250,11 +260,7 @@ pub fn read(bytes: &[u8]) -> Result<MidiFile> {
if !notes.is_empty() {
notes.sort_by_key(|n| (n.start, n.pitch));
let end = ticks(clock, division)?.max(notes.iter().map(Note::end).max().unwrap_or(0));
let length = end
.checked_add(BAR - 1)
.ok_or_else(|| error("MIDI timeline overflow."))?
/ BAR
* BAR;
lengths.push(end);
let warnings = if ignored {
vec!["Controllers, pitch bend, program changes, SysEx, or later tempo events are not imported. Preview uses a single tempo.".into()]
} else {
@@ -262,7 +268,7 @@ pub fn read(bytes: &[u8]) -> Result<MidiFile> {
};
tracks.push(MidiTrack {
name,
phrase: Phrase { notes, length },
phrase: Phrase { notes, length: 0 },
warnings,
channels: (0..16).filter(|c| channels & (1 << c) != 0).collect(),
});
@@ -271,7 +277,20 @@ pub fn read(bytes: &[u8]) -> Result<MidiFile> {
if tracks.is_empty() {
return Err(error("This MIDI file has no note tracks."));
}
Ok(MidiFile { tracks, tempo })
// The time signature may follow the notes in another track, so bars are known only now.
let bar = meter.unwrap_or_default().bar();
for (track, end) in tracks.iter_mut().zip(lengths) {
track.phrase.length = end
.checked_add(bar - 1)
.ok_or_else(|| error("MIDI timeline overflow."))?
/ bar
* bar;
}
Ok(MidiFile {
tracks,
tempo,
meter,
})
}
fn vlq(mut value: u32, out: &mut Vec<u8>) {
@@ -287,13 +306,21 @@ fn vlq(mut value: u32, out: &mut Vec<u8>) {
out.extend_from_slice(&b[i..]);
}
pub fn write(phrase: &Phrase, bpm: f64) -> Result<Vec<u8>> {
phrase.validate(MAX_PLAYBACK_NOTES)?;
if !bpm.is_finite() || !(30.0..=300.0).contains(&bpm) {
return Err(error("Tempo must be 30–300 BPM."));
}
fn time_signature(meter: Meter) -> [u8; 8] {
[
0,
0xff,
0x58,
4,
meter.beats,
meter.unit.trailing_zeros() as u8,
24,
8,
]
}
fn tempo_event(bpm: f64) -> [u8; 7] {
let us = (60_000_000. / bpm).round() as u32;
let mut track = vec![
[
0,
0xff,
0x51,
@@ -301,15 +328,17 @@ pub fn write(phrase: &Phrase, bpm: f64) -> Result<Vec<u8>> {
(us >> 16) as u8,
(us >> 8) as u8,
us as u8,
0,
0xff,
0x58,
4,
4,
2,
24,
8,
];
]
}
pub fn write(phrase: &Phrase, bpm: f64, meter: Meter) -> Result<Vec<u8>> {
phrase.validate(MAX_PLAYBACK_NOTES)?;
meter.validate()?;
if !bpm.is_finite() || !(30.0..=300.0).contains(&bpm) {
return Err(error("Tempo must be 30–300 BPM."));
}
let mut track = tempo_event(bpm).to_vec();
track.extend_from_slice(&time_signature(meter));
let mut events = Vec::new();
for n in &phrase.notes {
events.push((n.start, 1, n.pitch, n.velocity));
@@ -338,31 +367,17 @@ pub fn write_performance(
phrase: &Phrase,
settings: &PerformanceSettings,
bpm: f64,
meter: Meter,
) -> Result<Vec<u8>> {
let p = Performance::compile(phrase, settings, bpm, settings.compensated_export)?;
meter.validate()?;
let p = Performance::compile(phrase, settings, bpm, settings.compensated_export, meter)?;
let shift = if settings.compensated_export && p.lead_in > 0 {
BAR
meter.bar()
} else {
0
};
let us = (60_000_000. / bpm).round() as u32;
let mut conductor = vec![
0,
0xff,
0x51,
3,
(us >> 16) as u8,
(us >> 8) as u8,
us as u8,
0,
0xff,
0x58,
4,
4,
2,
24,
8,
];
let mut conductor = tempo_event(bpm).to_vec();
conductor.extend_from_slice(&time_signature(meter));
vlq(phrase.length + shift, &mut conductor);
conductor.extend_from_slice(&[0xff, 0x2f, 0]);
let mut tracks = vec![conductor];
@@ -432,16 +447,19 @@ mod tests {
#[test]
fn round_trip_source_and_generated_phrase() {
let source = rivet_core::demo_motif();
let bytes = write(&source, 140.).unwrap();
let bytes = write(&source, 140., Meter::default()).unwrap();
let parsed = read(&bytes).unwrap();
assert_eq!(parsed.tracks[0].phrase, source);
assert!((parsed.tempo.unwrap() - 140.).abs() < 0.001);
for c in rivet_core::generate(&source, &rivet_core::Settings::default()).unwrap() {
assert_eq!(
read(&write(&c.phrase, 140.).unwrap()).unwrap().tracks[0].phrase,
c.phrase
);
}
assert_eq!(parsed.meter, Some(Meter::default()));
let story = rivet_core::Session::demo().compose().unwrap();
assert_eq!(
read(&write(&story, 140., Meter::default()).unwrap())
.unwrap()
.tracks[0]
.phrase,
story
);
}
#[test]
fn running_status_and_zero_velocity_off() {
@@ -453,7 +471,7 @@ mod tests {
}
#[test]
fn malformed_and_unsupported_files() {
let good = write(&rivet_core::demo_motif(), 140.).unwrap();
let good = write(&rivet_core::demo_motif(), 140., Meter::default()).unwrap();
for n in 0..good.len() {
assert!(read(&good[..n]).is_err());
}
@@ -462,10 +480,10 @@ mod tests {
assert!(read(&smpte).unwrap_err().0.contains("SMPTE"));
assert!(read(&file(&[0, 0x90, 60, 80, 0, 0xff, 0x2f, 0])).is_err());
assert!(
read(&file(&[0, 0xff, 0x58, 4, 3, 2, 24, 8, 0, 0xff, 0x2f, 0]))
read(&file(&[0, 0xff, 0x58, 4, 11, 3, 24, 8, 0, 0xff, 0x2f, 0]))
.unwrap_err()
.0
.contains("4/4")
.contains("Supported meters")
);
assert!(read(&file(&[0x81, 0x81, 0x81, 0x81, 0, 0xff, 0x2f, 0])).is_err());
}
@@ -477,14 +495,14 @@ mod tests {
.unwrap();
assert!(p.tracks[0]
.phrase
.validate_source()
.validate(rivet_core::MAX_SOURCE_NOTES)
.unwrap_err()
.0
.contains("Overlapping"));
}
#[test]
fn separate_tracks_and_timing_resolution() {
let a = write(&rivet_core::demo_motif(), 140.).unwrap();
let a = write(&rivet_core::demo_motif(), 140., Meter::default()).unwrap();
let mut two = a[..14].to_vec();
two[9] = 1;
two[11] = 2;
@@ -508,7 +526,7 @@ mod tests {
v.controller.rest = 5 + role as u8;
}
let source = rivet_core::demo_motif();
let bytes = write_performance(&source, &settings, 120.).unwrap();
let bytes = write_performance(&source, &settings, 120., Meter::default()).unwrap();
let file = read(&bytes).unwrap();
assert_eq!(file.tracks.len(), 2);
assert_eq!(file.tracks[0].channels, vec![0]);
@@ -536,11 +554,43 @@ mod tests {
let source = rivet_core::demo_motif();
let mut settings = PerformanceSettings::default();
settings.voices[0].attack_ms = 100;
let direct = read(&write_performance(&source, &settings, 120.).unwrap()).unwrap();
let direct =
read(&write_performance(&source, &settings, 120., Meter::default()).unwrap()).unwrap();
assert_eq!(direct.tracks[0].phrase.notes[0].start, 0);
settings.compensated_export = true;
let compensated = read(&write_performance(&source, &settings, 120.).unwrap()).unwrap();
assert_eq!(compensated.tracks[0].phrase.notes[0].start, BAR - 192);
assert_eq!(compensated.tracks[0].phrase.length, source.length + BAR);
let compensated =
read(&write_performance(&source, &settings, 120., Meter::default()).unwrap()).unwrap();
assert_eq!(
compensated.tracks[0].phrase.notes[0].start,
rivet_core::BAR - 192
);
assert_eq!(
compensated.tracks[0].phrase.length,
source.length + rivet_core::BAR
);
}
#[test]
fn compound_meter_round_trips_and_sets_bar_length() {
let meter = Meter { beats: 6, unit: 8 };
let phrase = Phrase {
length: 2 * meter.bar(),
notes: vec![Note {
start: meter.beat(),
duration: 480,
pitch: 62,
velocity: 80,
..Default::default()
}],
};
let parsed = read(&write(&phrase, 120., meter).unwrap()).unwrap();
assert_eq!(parsed.meter, Some(meter));
assert_eq!(parsed.tracks[0].phrase, phrase);
let three = read(&file(&[
0, 0xff, 0x58, 4, 3, 2, 24, 8, 0, 0x90, 60, 80, 0x83, 0x60, 0x80, 60, 0, 0, 0xff, 0x2f,
0,
]))
.unwrap();
assert_eq!(three.meter, Some(Meter { beats: 3, unit: 4 }));
assert_eq!(three.tracks[0].phrase.length, 3 * PPQ);
}
}