Skip to main content

statistics/
statistics.rs

1use std::time::{Duration, Instant};
2
3use midi_toolkit::{io::MIDIFile, prelude::*};
4
5fn duration_to_minutes_seconds(duration: Duration) -> String {
6    format!(
7        "{:02}:{:02}",
8        duration.as_secs() / 60,
9        duration.as_secs() % 60
10    )
11}
12
13fn main() {
14    println!("Opening midi...");
15    let file = MIDIFile::open_in_ram("D:/Midis/tau2.5.9.mid", None).unwrap();
16    println!("Parsing midi...");
17
18    let now = Instant::now();
19    let stats1 = file
20        .iter_all_tracks()
21        .merge_all()
22        .channel_statistics()
23        .unwrap();
24
25    println!("Calculated merged stats in {:?}", now.elapsed());
26    println!(
27        "MIDI length: {}",
28        duration_to_minutes_seconds(stats1.calculate_total_duration(file.ppq()))
29    );
30    println!("Other stats: {stats1:#?}\n\n");
31
32    let now = Instant::now();
33    let stats2 = file.iter_all_tracks().channel_statistics().unwrap();
34    println!("Calculated multithreaded stats in {:?}", now.elapsed());
35    println!(
36        "MIDI length: {}",
37        duration_to_minutes_seconds(stats2.calculate_total_duration(file.ppq()))
38    );
39    println!("Other stats: {stats2:#?}");
40}