diff options
Diffstat (limited to 'src/midifile.rs')
-rw-r--r-- | src/midifile.rs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/midifile.rs b/src/midifile.rs index 219a931..aaa2dfd 100644 --- a/src/midifile.rs +++ b/src/midifile.rs @@ -134,6 +134,37 @@ fn write_track<W>(f: &mut W, trk: &MidiTrack) -> io::Result<()> where W: Write Ok(()) } +pub fn lint_track(trk: &MidiTrack) -> bool +{ + let mut ret = true; + let mut notest = [false; 128]; + for te in trk + { + let TimedMidiEvent{t, e} = te; + match e + { + MidiEvent::NoteOff{ch: _, key: n, vel: _} | MidiEvent::NoteOn{ch: _, key: n, vel: 0} => { + if !notest[*n as usize] { ret = false; println!("Invalid note off {} @ {}", n, t); } + notest[*n as usize] = false; + }, + MidiEvent::NoteOn{ch: _, key: n, vel: _} => { + if notest[*n as usize] { ret = false; println!("Unterminated note {} @ {}", n, t); } + notest[*n as usize] = true; + }, + MidiEvent::MetaTrackName(s) => { + println!("Linting track {}", s); + }, + _ => {} + } + } + if ! matches!(trk.last().unwrap_or(&TimedMidiEvent{t: 0, e: MidiEvent::NoteOn{ch: 0, key: 0, vel: 0}}).e , MidiEvent::MetaEndOfTrack) + { + println!("Missing end of track event"); + ret = false; + } + ret +} + pub fn write_file(filename: &str, mf: &MidiFile) -> io::Result<()> { let mut f = File::create(filename)?; |