Skip to content

Commit

Permalink
fix grace effect key
Browse files Browse the repository at this point in the history
  • Loading branch information
agourlay committed Dec 7, 2024
1 parent 6f4b92f commit 34527d1
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 61 deletions.
28 changes: 17 additions & 11 deletions src/audio/midi_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ impl MidiBuilder {
let strings = &track.strings;
let mut prev_tempo = song_tempo;
assert_eq!(track.measures.len(), measure_headers.len());
let mut uses_triplet_feel = false;
for (measure, measure_header) in track.measures.iter().zip(measure_headers) {
// add song info events once for all tracks
if track_id == 0 {
Expand All @@ -90,7 +91,13 @@ impl MidiBuilder {
measure_header,
midi_channel,
strings,
)
);
if measure_header.triplet_feel != TripletFeel::None {
uses_triplet_feel = true;
}
}
if uses_triplet_feel {
log::warn!("Triplet feel not supported on track {track_id}");
}
}

Expand Down Expand Up @@ -142,9 +149,6 @@ impl MidiBuilder {
next_beat: Option<&Beat>,
strings: &[(i32, i32)],
) {
if measure_header.triplet_feel != TripletFeel::None {
log::warn!("Triplet feel not supported");
}
let _stroke = &beat.effect.stroke;
let mut start = beat.start as usize;
let channel_id = midi_channel.channel_id;
Expand All @@ -158,9 +162,6 @@ impl MidiBuilder {
let (string_id, string_tuning) = strings[note.string as usize - 1];
assert_eq!(string_id, note.string as i32);

// compute key without effect
let initial_key = track_offset + note.value as i32 + string_tuning;

// surrounding notes on the same string on the previous & next beat
let previous_note =
previous_beat.and_then(|b| b.notes.iter().find(|n| n.string == note.string));
Expand All @@ -177,13 +178,14 @@ impl MidiBuilder {
// apply effects on key
if let Some(key) = self.add_key_effect(
track_id,
track_offset,
string_tuning,
&mut start,
&mut duration,
tempo,
note,
next_note,
next_beat,
initial_key,
velocity,
midi_channel,
) {
Expand All @@ -197,19 +199,23 @@ impl MidiBuilder {
fn add_key_effect(
&mut self,
track_id: usize,
track_offset: i32,
string_tuning: i32,
start: &mut usize,
duration: &mut usize,
tempo: i32,
note: &Note,
next_note: Option<&Note>,
next_beat: Option<&Beat>,
initial_key: i32,
velocity: i16,
midi_channel: &MidiChannel,
) -> Option<i32> {
let channel_id = midi_channel.channel_id as i32;
let is_percussion = midi_channel.is_percussion();

// compute key without effect
let initial_key = track_offset + note.value as i32 + string_tuning;

// key with effect
let mut key = initial_key;

Expand All @@ -229,7 +235,7 @@ impl MidiBuilder {

// grace note
if let Some(grace) = &note.effect.grace {
let grace_key = initial_key + grace.fret as i32;
let grace_key = track_offset + grace.fret as i32 + string_tuning;
let grace_length = grace.duration_time() as usize;
let grace_velocity = grace.velocity;
let grace_duration = if grace.is_dead {
Expand All @@ -255,7 +261,7 @@ impl MidiBuilder {
// trill
if let Some(trill) = &note.effect.trill {
if !is_percussion {
let trill_key = trill.fret as i32 + initial_key - note.value as i32;
let trill_key = track_offset + trill.fret as i32 + string_tuning;
let mut trill_length = trill.duration.time() as usize;

let trill_tick_limit = *start + *duration;
Expand Down
8 changes: 4 additions & 4 deletions test-files/gold-generated-midi/Demo v5.gp5.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1423,12 +1423,12 @@
71520 NoteOff(9, 42) Some(4)
71520 NoteOff(9, 38) Some(4)
71520 NoteOn(9, 42, 95) Some(4)
71880 NoteOn(2, 77, 95) Some(1)
71880 NoteOn(2, 65, 95) Some(1)
72000 NoteOff(0, 60) Some(0)
72000 NoteOff(0, 55) Some(0)
72000 NoteOff(0, 48) Some(0)
72000 NoteOn(0, 48, 95) Some(0)
72000 NoteOff(2, 77) Some(1)
72000 NoteOff(2, 65) Some(1)
72000 NoteOn(2, 67, 95) Some(1)
72000 NoteOff(4, 36) Some(3)
72000 NoteOn(4, 36, 95) Some(3)
Expand Down Expand Up @@ -3704,12 +3704,12 @@
163680 NoteOff(9, 42) Some(4)
163680 NoteOff(9, 38) Some(4)
163680 NoteOn(9, 42, 95) Some(4)
164040 NoteOn(2, 77, 95) Some(1)
164040 NoteOn(2, 65, 95) Some(1)
164160 NoteOff(0, 60) Some(0)
164160 NoteOff(0, 55) Some(0)
164160 NoteOff(0, 48) Some(0)
164160 NoteOn(0, 48, 95) Some(0)
164160 NoteOff(2, 77) Some(1)
164160 NoteOff(2, 65) Some(1)
164160 NoteOn(2, 67, 95) Some(1)
164160 NoteOff(4, 36) Some(3)
164160 NoteOn(4, 36, 95) Some(3)
Expand Down
68 changes: 34 additions & 34 deletions test-files/gold-generated-midi/Ghost - Cirice.gp5.txt
Original file line number Diff line number Diff line change
Expand Up @@ -950,8 +950,8 @@
72960 NoteOff(9, 49) Some(11)
72960 NoteOff(9, 36) Some(11)
72960 NoteOff(9, 36) Some(11)
72960 NoteOn(9, 82, 127) Some(11)
73020 NoteOff(9, 82) Some(11)
72960 NoteOn(9, 41, 127) Some(11)
73020 NoteOff(9, 41) Some(11)
73020 NoteOn(9, 41, 127) Some(11)
73020 NoteOn(9, 36, 127) Some(11)
73045 NoteOff(8, 47) Some(3)
Expand All @@ -965,9 +965,9 @@
73440 NoteOff(4, 28) Some(6)
73440 NoteOn(4, 27, 127) Some(6)
73440 NoteOff(9, 41) Some(11)
73440 NoteOn(9, 82, 127) Some(11)
73440 NoteOn(9, 41, 127) Some(11)
73500 NoteOff(9, 36) Some(11)
73500 NoteOff(9, 82) Some(11)
73500 NoteOff(9, 41) Some(11)
73500 NoteOn(9, 41, 127) Some(11)
73500 NoteOn(9, 36, 127) Some(11)
73525 NoteOff(8, 46) Some(3)
Expand Down Expand Up @@ -1118,8 +1118,8 @@
80640 NoteOff(9, 49) Some(11)
80640 NoteOff(9, 36) Some(11)
80640 NoteOff(9, 36) Some(11)
80640 NoteOn(9, 82, 127) Some(11)
80700 NoteOff(9, 82) Some(11)
80640 NoteOn(9, 41, 127) Some(11)
80700 NoteOff(9, 41) Some(11)
80700 NoteOn(9, 41, 127) Some(11)
80700 NoteOn(9, 36, 127) Some(11)
80725 NoteOff(8, 47) Some(3)
Expand All @@ -1133,9 +1133,9 @@
81120 NoteOff(4, 28) Some(6)
81120 NoteOn(4, 27, 127) Some(6)
81120 NoteOff(9, 41) Some(11)
81120 NoteOn(9, 82, 127) Some(11)
81120 NoteOn(9, 41, 127) Some(11)
81180 NoteOff(9, 36) Some(11)
81180 NoteOff(9, 82) Some(11)
81180 NoteOff(9, 41) Some(11)
81180 NoteOn(9, 41, 127) Some(11)
81180 NoteOn(9, 36, 127) Some(11)
81205 NoteOff(8, 46) Some(3)
Expand Down Expand Up @@ -1301,8 +1301,8 @@
88320 NoteOff(9, 49) Some(11)
88320 NoteOff(9, 36) Some(11)
88320 NoteOff(9, 36) Some(11)
88320 NoteOn(9, 82, 127) Some(11)
88380 NoteOff(9, 82) Some(11)
88320 NoteOn(9, 41, 127) Some(11)
88380 NoteOff(9, 41) Some(11)
88380 NoteOn(9, 41, 127) Some(11)
88380 NoteOn(9, 36, 127) Some(11)
88405 NoteOff(8, 47) Some(3)
Expand All @@ -1316,9 +1316,9 @@
88800 NoteOff(4, 28) Some(6)
88800 NoteOn(4, 27, 127) Some(6)
88800 NoteOff(9, 41) Some(11)
88800 NoteOn(9, 82, 127) Some(11)
88800 NoteOn(9, 41, 127) Some(11)
88860 NoteOff(9, 36) Some(11)
88860 NoteOff(9, 82) Some(11)
88860 NoteOff(9, 41) Some(11)
88860 NoteOn(9, 41, 127) Some(11)
88860 NoteOn(9, 36, 127) Some(11)
88885 NoteOff(8, 46) Some(3)
Expand Down Expand Up @@ -1832,8 +1832,8 @@
114720 NoteOn(4, 32, 127) Some(6)
114720 NoteOff(9, 49) Some(11)
114720 NoteOff(9, 36) Some(11)
114720 NoteOn(9, 80, 127) Some(11)
114780 NoteOff(9, 80) Some(11)
114720 NoteOn(9, 40, 127) Some(11)
114780 NoteOff(9, 40) Some(11)
114780 NoteOn(9, 40, 127) Some(11)
115200 NoteOff(8, 44) Some(3)
115200 NoteOn(8, 38, 127) Some(3)
Expand Down Expand Up @@ -2148,10 +2148,10 @@
125280 NoteOff(9, 38) Some(11)
125280 NoteOff(9, 36) Some(11)
125280 NoteOn(9, 46, 127) Some(11)
125700 NoteOn(15, 72, 127) Some(0)
125700 NoteOn(15, 67, 127) Some(0)
125760 NoteOff(15, 62) Some(0)
125760 NoteOff(15, 57) Some(0)
125760 NoteOff(15, 72) Some(0)
125760 NoteOff(15, 67) Some(0)
125760 NoteOn(15, 62, 127) Some(0)
125760 NoteOn(15, 57, 79) Some(0)
125760 NoteOff(8, 50) Some(3)
Expand Down Expand Up @@ -2290,8 +2290,8 @@
130080 NoteOn(4, 32, 127) Some(6)
130080 NoteOff(9, 49) Some(11)
130080 NoteOff(9, 36) Some(11)
130080 NoteOn(9, 80, 127) Some(11)
130140 NoteOff(9, 80) Some(11)
130080 NoteOn(9, 40, 127) Some(11)
130140 NoteOff(9, 40) Some(11)
130140 NoteOn(9, 40, 127) Some(11)
130560 NoteOff(8, 44) Some(3)
130560 NoteOn(8, 38, 127) Some(3)
Expand Down Expand Up @@ -2726,8 +2726,8 @@
145440 NoteOn(4, 27, 127) Some(6)
145440 NoteOff(9, 49) Some(11)
145440 NoteOff(9, 36) Some(11)
145440 NoteOn(9, 80, 127) Some(11)
145500 NoteOff(9, 80) Some(11)
145440 NoteOn(9, 40, 127) Some(11)
145500 NoteOff(9, 40) Some(11)
145500 NoteOn(9, 40, 127) Some(11)
145920 NoteOff(8, 46) Some(3)
145920 NoteOff(8, 39) Some(3)
Expand Down Expand Up @@ -3578,8 +3578,8 @@
176160 NoteOn(4, 32, 127) Some(6)
176160 NoteOff(9, 49) Some(11)
176160 NoteOff(9, 36) Some(11)
176160 NoteOn(9, 80, 127) Some(11)
176220 NoteOff(9, 80) Some(11)
176160 NoteOn(9, 40, 127) Some(11)
176220 NoteOff(9, 40) Some(11)
176220 NoteOn(9, 40, 127) Some(11)
176640 NoteOff(8, 44) Some(3)
176640 NoteOn(8, 38, 127) Some(3)
Expand Down Expand Up @@ -4014,8 +4014,8 @@
191520 NoteOn(4, 27, 127) Some(6)
191520 NoteOff(9, 49) Some(11)
191520 NoteOff(9, 36) Some(11)
191520 NoteOn(9, 80, 127) Some(11)
191580 NoteOff(9, 80) Some(11)
191520 NoteOn(9, 40, 127) Some(11)
191580 NoteOff(9, 40) Some(11)
191580 NoteOn(9, 40, 127) Some(11)
192000 NoteOff(8, 46) Some(3)
192000 NoteOff(8, 39) Some(3)
Expand Down Expand Up @@ -4474,13 +4474,13 @@
225120 NoteOn(11, 63, 127) Some(7)
225600 NoteOff(11, 60) Some(7)
225600 NoteOff(11, 48) Some(7)
225960 NoteOn(9, 80, 127) Some(11)
225960 NoteOn(9, 40, 127) Some(11)
226080 NoteOff(15, 62) Some(0)
226080 NoteOff(15, 58) Some(0)
226080 NoteOn(15, 60, 127) Some(0)
226080 NoteOn(15, 55, 47) Some(0)
226080 NoteOn(11, 48, 127) Some(7)
226080 NoteOff(9, 80) Some(11)
226080 NoteOff(9, 40) Some(11)
226080 NoteOn(9, 40, 127) Some(11)
226560 NoteOn(11, 58, 127) Some(7)
226560 NoteOff(9, 40) Some(11)
Expand Down Expand Up @@ -4864,8 +4864,8 @@
241440 NoteOn(6, 40, 127) Some(4)
241440 NoteOff(4, 33) Some(6)
241440 NoteOn(4, 32, 127) Some(6)
241440 NoteOn(9, 80, 127) Some(11)
241500 NoteOff(9, 80) Some(11)
241440 NoteOn(9, 40, 127) Some(11)
241500 NoteOff(9, 40) Some(11)
241500 NoteOn(9, 40, 127) Some(11)
241920 NoteOff(8, 44) Some(3)
241920 NoteOn(8, 38, 127) Some(3)
Expand Down Expand Up @@ -5754,8 +5754,8 @@
272160 NoteOn(4, 27, 127) Some(6)
272160 NoteOff(9, 49) Some(11)
272160 NoteOff(9, 36) Some(11)
272160 NoteOn(9, 80, 127) Some(11)
272220 NoteOff(9, 80) Some(11)
272160 NoteOn(9, 40, 127) Some(11)
272220 NoteOff(9, 40) Some(11)
272220 NoteOn(9, 40, 127) Some(11)
272640 NoteOff(8, 46) Some(3)
272640 NoteOff(8, 39) Some(3)
Expand Down Expand Up @@ -6216,8 +6216,8 @@
287520 NoteOn(4, 32, 127) Some(6)
287520 NoteOff(9, 49) Some(11)
287520 NoteOff(9, 36) Some(11)
287520 NoteOn(9, 80, 127) Some(11)
287580 NoteOff(9, 80) Some(11)
287520 NoteOn(9, 40, 127) Some(11)
287580 NoteOff(9, 40) Some(11)
287580 NoteOn(9, 40, 127) Some(11)
288000 NoteOff(8, 44) Some(3)
288000 NoteOff(6, 40) Some(4)
Expand Down Expand Up @@ -13375,8 +13375,8 @@
458400 NoteOn(4, 41, 127) Some(6)
458400 NoteOff(11, 69) Some(7)
458400 NoteOn(11, 55, 127) Some(7)
458400 NoteOn(9, 80, 127) Some(11)
458460 NoteOff(9, 80) Some(11)
458400 NoteOn(9, 40, 127) Some(11)
458460 NoteOff(9, 40) Some(11)
458460 NoteOn(9, 40, 127) Some(11)
458640 NoteOff(9, 40) Some(11)
458640 NoteOn(9, 40, 127) Some(11)
Expand Down
Loading

0 comments on commit 34527d1

Please sign in to comment.