Do pitch bend correctly

This commit is contained in:
Gwilym Inzani 2023-11-15 15:29:55 +00:00
parent a36b06b35c
commit ff9f336be3
2 changed files with 6 additions and 10 deletions

View file

@ -273,16 +273,10 @@ pub fn parse_midi(midi_info: &MidiInfo) -> TokenStream {
} }
midly::MidiMessage::Aftertouch { .. } => {} midly::MidiMessage::Aftertouch { .. } => {}
midly::MidiMessage::PitchBend { bend } => { midly::MidiMessage::PitchBend { bend } => {
// bend is between 0 and 8192 where 0 = -2 semitones and 8193 is +2 semitones (I think) // 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 - (8192.0 / 2.0)) / (8192.0 / 2.0); let amount = (bend.0.as_int() as f64 - (16384.0 / 2.0)) / (16384.0 / 2.0);
// amount is now between -1 and 1 let amount = 2.0f64.powf((amount * 2.0) / 12.0);
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
};
pattern.push(PatternSlot { pattern.push(PatternSlot {
speed: 0.into(), speed: 0.into(),

View file

@ -112,6 +112,7 @@ pub struct Tracker {
#[derive(Default)] #[derive(Default)]
struct TrackerChannel { struct TrackerChannel {
channel_id: Option<ChannelId>, channel_id: Option<ChannelId>,
original_speed: Num<u32, 16>,
base_speed: Num<u32, 16>, base_speed: Num<u32, 16>,
volume: Num<i32, 8>, volume: Num<i32, 8>,
} }
@ -328,6 +329,7 @@ impl TrackerChannel {
{ {
if speed != 0.into() { if speed != 0.into() {
self.base_speed = speed.change_base(); self.base_speed = speed.change_base();
self.original_speed = self.base_speed;
} }
channel.playback(self.base_speed.change_base()); channel.playback(self.base_speed.change_base());
@ -430,7 +432,7 @@ impl TrackerChannel {
} }
PatternEffect::PitchBend(amount) => { PatternEffect::PitchBend(amount) => {
if tick == 0 { 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()); channel.playback(self.base_speed.change_base());
} }
} }