From 4b6230c31890a9c7c2a45590863577f8f83629c2 Mon Sep 17 00:00:00 2001 From: Chris Xiong Date: Fri, 16 Dec 2022 18:36:49 -0500 Subject: Write file with midi running status. --- src/midifile.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/midifile.rs b/src/midifile.rs index aaa2dfd..00e20f5 100644 --- a/src/midifile.rs +++ b/src/midifile.rs @@ -105,14 +105,16 @@ fn write_varlen(f: &mut W, v: u32) -> io::Result<()> where W: Write f.write_all(&buf[..]) } -fn write_raw_event(f: &mut W, re: &RawMidiEvent) -> io::Result<()> where W: Write +fn write_raw_event(f: &mut W, re: &RawMidiEvent, runst: u8) -> io::Result where W: Write { let mut buf: Vec = Vec::new(); - buf.push(re.ty); + if re.ty != runst || runst >= 0xf0 || re.ty >= 0xf0 + { buf.push(re.ty); } buf.push(re.p1); if re.p2 < 0x80 { buf.push(re.p2); } for d in &re.data { buf.push(*d); } - f.write_all(&buf[..]) + f.write_all(&buf[..])?; + Ok(re.ty) } fn write_track(f: &mut W, trk: &MidiTrack) -> io::Result<()> where W: Write @@ -121,13 +123,14 @@ fn write_track(f: &mut W, trk: &MidiTrack) -> io::Result<()> where W: Write f.write_all(header)?; let mut buf: Vec = Vec::new(); let mut curt = 0u32; + let mut runst = 0u8; for te in trk { let TimedMidiEvent{t, e} = te; write_varlen(&mut buf, t - curt)?; curt = *t; let re = RawMidiEvent::from(e); - write_raw_event(&mut buf, &re)?; + runst = write_raw_event(&mut buf, &re, runst)?; } write_u32be(f, buf.len() as u32)?; f.write_all(&buf[..])?; -- cgit v1.2.3