From 63a53ad32272a6e5047a6415847e126311d9c69e Mon Sep 17 00:00:00 2001 From: Robert Hargreaves Date: Sun, 30 Jun 2024 14:21:34 +0100 Subject: [PATCH] Optimise code --- src/midi_fm.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/midi_fm.c b/src/midi_fm.c index 6f8a0f8..289f040 100644 --- a/src/midi_fm.c +++ b/src/midi_fm.c @@ -98,25 +98,21 @@ u16 midi_fm_pitchAndPitchBendToFreqNum(u8 pitch, u16 pitchBend) return freq; } s16 bendRelative = pitchBend - MIDI_PITCH_BEND_CENTRE; + s16 approxFreqDelta; if (bendRelative < 0) { // bend down - u16 lowerBoundFreq + u16 boundFreq = pitchToFreq(pitch, -GENERAL_MIDI_PITCH_BEND_SEMITONE_RANGE); - u16 approxFreqDelta - = (u16)(((u32)(freq - lowerBoundFreq) * (u32)(bendRelative * -1)) - / (u32)DEFAULT_MIDI_PITCH_BEND); - u16 approxFreq = freq - approxFreqDelta; - return approxFreq; + approxFreqDelta + = (((freq - boundFreq) * bendRelative) / DEFAULT_MIDI_PITCH_BEND); } else { // bend up - u16 upperBoundFreq + u16 boundFreq = pitchToFreq(pitch, GENERAL_MIDI_PITCH_BEND_SEMITONE_RANGE); - u16 approxFreqDelta - = (u16)(((u32)(upperBoundFreq - freq) * (u32)(bendRelative)) - / (u32)DEFAULT_MIDI_PITCH_BEND); - u16 approxFreq = freq + approxFreqDelta; - return approxFreq; + approxFreqDelta + = (((boundFreq - freq) * bendRelative) / DEFAULT_MIDI_PITCH_BEND); } + return freq + approxFreqDelta; } void midi_fm_pitch_bend(u8 chan, u16 bend) {