Skip to content

Commit

Permalink
Optimise code
Browse files Browse the repository at this point in the history
  • Loading branch information
rhargreaves committed Jun 30, 2024
1 parent e1adc70 commit 63a53ad
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/midi_fm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit 63a53ad

Please sign in to comment.