Skip to main content

staged_write/
staged_write.rs

1use std::{io::Write, thread};
2
3use midi_toolkit::io::StagedMIDIWriter;
4
5fn main() {
6    let writer = StagedMIDIWriter::new("./staged-output.mid", 480).unwrap();
7    println!(
8        "Temporary parts file: {}",
9        writer.temporary_parts_path().display()
10    );
11
12    let mut handles = Vec::new();
13    for track_id in 0..3 {
14        let mut track = writer.try_open_track(track_id).unwrap();
15        handles.push(thread::spawn(move || {
16            for step in 0..8 {
17                let pitch = 60 + step as u8;
18                track
19                    .write_all(&[0x00, 0x90 | track_id as u8, pitch, 0x40])
20                    .unwrap();
21                track
22                    .write_all(&[0x10, 0x80 | track_id as u8, pitch, 0x00])
23                    .unwrap();
24            }
25            track.end().unwrap();
26        }));
27    }
28
29    for handle in handles {
30        handle.join().unwrap();
31    }
32
33    writer.end().unwrap();
34    println!(
35        "Final MIDI written to {}",
36        writer.destination_path().display()
37    );
38}