Skip to content

Commit

Permalink
Unify pitch bending calcs for all FM usages
Browse files Browse the repository at this point in the history
  • Loading branch information
rhargreaves committed Jun 27, 2024
1 parent 36a067c commit 9cfee02
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 19 deletions.
16 changes: 7 additions & 9 deletions src/midi_fm.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ static u8 pitchIsOutOfRange(u8 pitch);
static u8 effectiveVolume(MidiFmChannel* channelState);
static void updatePan(u8 chan);
void midi_fm_reset(void);
static u16 effectiveFreq(MidiFmChannel* fmChan);

static const FmChannel** presets;
static const PercussionPreset** percussionPresets;
Expand Down Expand Up @@ -67,9 +66,8 @@ void midi_fm_note_on(u8 chan, u8 pitch, u8 velocity)
fmChan->velocity = velocity;
synth_volume(chan, effectiveVolume(fmChan));
fmChan->pitch = pitch;

u16 freq = effectiveFreq(fmChan);
synth_pitch(chan, midi_fm_pitchToOctave(fmChan->pitch), freq);
synth_pitch(chan, midi_fm_pitchToOctave(fmChan->pitch),
midi_fm_pitchAndPitchBendToFreqNum(fmChan->pitch, fmChan->pitchBend));
synth_noteOn(chan);
}

Expand All @@ -86,19 +84,19 @@ void midi_fm_channel_volume(u8 chan, u8 volume)
synth_volume(chan, effectiveVolume(fmChan));
}

static u16 effectiveFreq(MidiFmChannel* fmChan)
u16 midi_fm_pitchAndPitchBendToFreqNum(u8 pitch, u16 pitchBend)
{
u16 freq = midi_fm_pitchToFreqNumber(fmChan->pitch);
s16 bendRelative = fmChan->pitchBend - MIDI_PITCH_BEND_CENTRE;
u16 freq = midi_fm_pitchToFreqNumber(pitch);
s16 bendRelative = pitchBend - MIDI_PITCH_BEND_CENTRE;
return freq + (bendRelative / 75);
}

void midi_fm_pitch_bend(u8 chan, u16 bend)
{
MidiFmChannel* fmChan = &fmChannels[chan];
fmChan->pitchBend = bend;
u16 freq = effectiveFreq(fmChan);
synth_pitch(chan, midi_fm_pitchToOctave(fmChan->pitch), freq);
synth_pitch(chan, midi_fm_pitchToOctave(fmChan->pitch),
midi_fm_pitchAndPitchBendToFreqNum(fmChan->pitch, fmChan->pitchBend));
}

void midi_fm_program(u8 chan, u8 program)
Expand Down
3 changes: 2 additions & 1 deletion src/midi_fm.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ void midi_fm_program(u8 chan, u8 program);
void midi_fm_all_notes_off(u8 chan);
void midi_fm_percussive(u8 chan, bool enabled);
u8 midi_fm_pitchToOctave(u8 pitch);
u16 midi_fm_pitchToFreqNumber(u8 pitch);
u16 midi_fm_pitchToFreqNumber(u8 pitch);
u16 midi_fm_pitchAndPitchBendToFreqNum(u8 pitch, u16 pitchBend);
11 changes: 2 additions & 9 deletions src/midi_fm_sm.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,12 @@ struct SpecialModeOperator {

static SpecialModeOperator smOperators[SM_OP_LEN];

static u16 effectiveFreq(u8 pitch, u16 pitchBend)
{
u16 freq = midi_fm_pitchToFreqNumber(pitch);
s16 bendRelative = pitchBend - MIDI_PITCH_BEND_CENTRE;
return freq + (bendRelative / 75);
}

void midi_fm_sm_note_on(u8 op, u8 pitch, u8 velocity)
{
SpecialModeOperator* smOp = &smOperators[op];
smOp->pitch = pitch;
synth_specialModePitch(op, midi_fm_pitchToOctave(smOp->pitch),
effectiveFreq(smOp->pitch, smOp->pitchBend));
midi_fm_pitchAndPitchBendToFreqNum(smOp->pitch, smOp->pitchBend));
synth_specialModeVolume(op, velocity);
}

Expand All @@ -34,7 +27,7 @@ void midi_fm_sm_pitch_bend(u8 op, u16 bend)
SpecialModeOperator* smOp = &smOperators[op];
smOp->pitchBend = bend;
synth_specialModePitch(op, midi_fm_pitchToOctave(smOp->pitch),
effectiveFreq(smOp->pitch, smOp->pitchBend));
midi_fm_pitchAndPitchBendToFreqNum(smOp->pitch, smOp->pitchBend));
}

void midi_fm_sm_reset(void)
Expand Down

0 comments on commit 9cfee02

Please sign in to comment.