aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Chris Xiong <chirs241097@gmail.com> 2022-12-16 18:36:49 -0500
committerGravatar Chris Xiong <chirs241097@gmail.com> 2022-12-16 18:36:49 -0500
commit4b6230c31890a9c7c2a45590863577f8f83629c2 (patch)
treebbfe15adc0faaaff5de47644c5d426faba310fe5
parent19560c3f39110fbff910091b5b25e69a044ed18e (diff)
downloadit2midi-4b6230c31890a9c7c2a45590863577f8f83629c2.tar.xz
Write file with midi running status.
-rw-r--r--src/midifile.rs11
1 files 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<W>(f: &mut W, v: u32) -> io::Result<()> where W: Write
f.write_all(&buf[..])
}
-fn write_raw_event<W>(f: &mut W, re: &RawMidiEvent) -> io::Result<()> where W: Write
+fn write_raw_event<W>(f: &mut W, re: &RawMidiEvent, runst: u8) -> io::Result<u8> where W: Write
{
let mut buf: Vec<u8> = 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<W>(f: &mut W, trk: &MidiTrack) -> io::Result<()> where W: Write
@@ -121,13 +123,14 @@ fn write_track<W>(f: &mut W, trk: &MidiTrack) -> io::Result<()> where W: Write
f.write_all(header)?;
let mut buf: Vec<u8> = 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[..])?;