statistics/
statistics.rs

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