nps_bench/
nps_bench.rs

1use std::time::Instant;
2
3use midi_toolkit::{
4    events::{Event, MIDIEventEnum},
5    io::MIDIFile,
6    pipe,
7    sequence::{
8        event::{cancel_tempo_events, scale_event_time},
9        unwrap_items, TimeCaster,
10    },
11};
12
13pub fn main() {
14    println!("Opening midi...");
15    let midi = MIDIFile::open_in_ram(
16        "/mnt/fat/Midis/Ra Ra Rasputin Ultimate Black MIDI Final.mid",
17        None,
18    )
19    .unwrap();
20
21    let ppq = midi.ppq();
22    let merged = pipe!(
23        midi.iter_all_track_events_merged_batches()
24        |>TimeCaster::<f64>::cast_event_delta()
25        |>cancel_tempo_events(250000)
26        |>scale_event_time(1.0 / ppq as f64)
27        |>unwrap_items()
28    );
29
30    println!("Tracks: {}", midi.track_count());
31
32    let start = Instant::now();
33    let mut note_count = 0;
34    for batch in merged {
35        for e in batch.iter_events() {
36            if let Event::NoteOn(_) = e.as_event() {
37                note_count += 1;
38            }
39        }
40    }
41
42    println!("Note count: {}   \tTime: {:?}", note_count, start.elapsed());
43    println!(
44        "Notes per second: {}",
45        note_count as f64 / start.elapsed().as_secs_f64()
46    );
47}