From ff9f336be3c3e182a14152c844aad2d9b669aed8 Mon Sep 17 00:00:00 2001 From: Gwilym Inzani Date: Wed, 15 Nov 2023 15:29:55 +0000 Subject: [PATCH] Do pitch bend correctly --- tracker/agb-midi-core/src/lib.rs | 12 +++--------- tracker/agb-tracker/src/lib.rs | 4 +++- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/tracker/agb-midi-core/src/lib.rs b/tracker/agb-midi-core/src/lib.rs index 0852a660..e9f03446 100644 --- a/tracker/agb-midi-core/src/lib.rs +++ b/tracker/agb-midi-core/src/lib.rs @@ -273,16 +273,10 @@ pub fn parse_midi(midi_info: &MidiInfo) -> TokenStream { } midly::MidiMessage::Aftertouch { .. } => {} midly::MidiMessage::PitchBend { bend } => { - // bend is between 0 and 8192 where 0 = -2 semitones and 8193 is +2 semitones (I think) - let amount = (bend.0.as_int() as f64 - (8192.0 / 2.0)) / (8192.0 / 2.0); + // bend is between 0 and 16383 where 0 = -2 semitones and 16384 is +2 semitones (I think) + let amount = (bend.0.as_int() as f64 - (16384.0 / 2.0)) / (16384.0 / 2.0); - // amount is now between -1 and 1 - let two_semitones_multiplier = 493.88 / 440.0; // B4 / A4 - let amount = if amount < 0.0 { - 1.0 / two_semitones_multiplier * (-amount) - } else { - two_semitones_multiplier * amount - }; + let amount = 2.0f64.powf((amount * 2.0) / 12.0); pattern.push(PatternSlot { speed: 0.into(), diff --git a/tracker/agb-tracker/src/lib.rs b/tracker/agb-tracker/src/lib.rs index 85254b90..8735df45 100644 --- a/tracker/agb-tracker/src/lib.rs +++ b/tracker/agb-tracker/src/lib.rs @@ -112,6 +112,7 @@ pub struct Tracker { #[derive(Default)] struct TrackerChannel { channel_id: Option, + original_speed: Num, base_speed: Num, volume: Num, } @@ -328,6 +329,7 @@ impl TrackerChannel { { if speed != 0.into() { self.base_speed = speed.change_base(); + self.original_speed = self.base_speed; } channel.playback(self.base_speed.change_base()); @@ -430,7 +432,7 @@ impl TrackerChannel { } PatternEffect::PitchBend(amount) => { if tick == 0 { - self.base_speed *= amount.change_base(); + self.base_speed = self.original_speed * amount.change_base(); channel.playback(self.base_speed.change_base()); } }