Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split track build into two threads #58

Merged
merged 1 commit into from
Sep 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 97 additions & 71 deletions midi-file/src/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,77 +68,41 @@ impl MidiTrack {
track_events: &[TrackEvent],
pulses_per_quarter_note: u16,
) -> Self {
let notes = build_notes(
track_id,
track_color_id,
tempo_track,
track_events,
pulses_per_quarter_note,
);

let mut programs = Vec::new();
let mut has_drums = false;
let mut has_other_than_drums = false;

let mut pulses: u64 = 0;
let events = track_events
.iter()
.filter_map(|event| {
pulses += event.delta.as_int() as u64;
match event.kind {
TrackEventKind::Midi { channel, message } => {
let timestamp =
tempo_track.pulses_to_duration(pulses, pulses_per_quarter_note);

let message = match message {
midly::MidiMessage::NoteOn { key, vel } => {
if channel == 9 || channel == 15 {
has_drums = true;
} else {
has_other_than_drums = true;
}

if vel.as_int() > 0 {
message
} else {
midly::MidiMessage::NoteOff { key, vel }
}
}
midly::MidiMessage::ProgramChange { program } => {
programs.push(ProgramEvent {
timestamp,
channel: channel.as_int(),
program: program.as_int(),
});
message
}
message => message,
};

Some(MidiEvent {
channel: channel.as_int(),
delta: event.delta.as_int(),
timestamp,
message,
track_id,
track_color_id,
})
}
_ => None,
}
})
.collect();

Self {
track_id,
track_color_id,
notes,
events,

programs,
has_drums,
has_other_than_drums,
}
std::thread::scope(|tb| {
let notes = tb.spawn(|| {
build_notes(
track_id,
track_color_id,
tempo_track,
track_events,
pulses_per_quarter_note,
)
});

let events = tb.spawn(|| {
build_events(
track_id,
track_color_id,
tempo_track,
track_events,
pulses_per_quarter_note,
)
});

let notes = notes.join().unwrap();
let (events, programs, has_drums, has_other_than_drums) = events.join().unwrap();

Self {
track_id,
track_color_id,
notes,
events,

programs,
has_drums,
has_other_than_drums,
}
})
}
}

Expand Down Expand Up @@ -209,3 +173,65 @@ fn build_notes(

notes
}

fn build_events(
track_id: usize,
track_color_id: usize,
tempo_track: &TempoTrack,
track_events: &[TrackEvent],
pulses_per_quarter_note: u16,
) -> (Vec<MidiEvent>, Vec<ProgramEvent>, bool, bool) {
let mut programs = Vec::new();
let mut has_drums = false;
let mut has_other_than_drums = false;

let mut pulses: u64 = 0;
let events = track_events
.iter()
.filter_map(|event| {
pulses += event.delta.as_int() as u64;
match event.kind {
TrackEventKind::Midi { channel, message } => {
let timestamp = tempo_track.pulses_to_duration(pulses, pulses_per_quarter_note);

let message = match message {
midly::MidiMessage::NoteOn { key, vel } => {
if channel == 9 || channel == 15 {
has_drums = true;
} else {
has_other_than_drums = true;
}

if vel.as_int() > 0 {
message
} else {
midly::MidiMessage::NoteOff { key, vel }
}
}
midly::MidiMessage::ProgramChange { program } => {
programs.push(ProgramEvent {
timestamp,
channel: channel.as_int(),
program: program.as_int(),
});
message
}
message => message,
};

Some(MidiEvent {
channel: channel.as_int(),
delta: event.delta.as_int(),
timestamp,
message,
track_id,
track_color_id,
})
}
_ => None,
}
})
.collect();

(events, programs, has_drums, has_other_than_drums)
}