Skip to content

Commit

Permalink
some pitch bend fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vampirefrog committed Nov 21, 2024
1 parent 1aed75e commit d094545
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
8 changes: 3 additions & 5 deletions fm_midi_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ void fm_midi_driver_set_pms_ams(struct fm_driver *driver, int channel, uint8_t p
void fm_midi_driver_set_pitch(struct fm_driver *driver, int channel, int pitch) {
struct fm_midi_driver *mididrv = (struct fm_midi_driver *)driver;
if(mididrv->channels[channel].on && pitch != mididrv->channels[channel].pitch) {
pitch >>= 8;
pitch -= 5;
int root_pitch = mididrv->channels[channel].note << 6;
int detune = (pitch - root_pitch);
int detune = ((pitch >> 8) - 5 - root_pitch);

if(detune != mididrv->channels[channel].detune) {
midi_track_write_pitch_bend(&mididrv->midi_file->tracks[channel + 1], mididrv->channels[channel].ticks, channel, detune);
midi_track_write_pitch_bend(&mididrv->midi_file->tracks[channel + 1], mididrv->channels[channel].ticks, channel, 8192 + detune * 4);
mididrv->channels[channel].detune = detune;
mididrv->channels[channel].ticks = 0;
}
Expand All @@ -40,7 +38,7 @@ void fm_midi_driver_note_on(struct fm_driver *driver, int channel, uint8_t op_ma
int note = pitch >> 6;
mididrv->channels[channel].note = note;
if(detune != mididrv->channels[channel].detune) {
midi_track_write_pitch_bend(&mididrv->midi_file->tracks[channel + 1], mididrv->channels[channel].ticks, channel, detune);
midi_track_write_pitch_bend(&mididrv->midi_file->tracks[channel + 1], mididrv->channels[channel].ticks, channel, 8192 + detune * 4);
mididrv->channels[channel].ticks = 0;
}
midi_track_write_note_on(&mididrv->midi_file->tracks[channel + 1], mididrv->channels[channel].ticks, channel, note, 127);
Expand Down
16 changes: 16 additions & 0 deletions mdx2midi.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ int main(int argc, char **argv) {
struct midi_file midi_file;
midi_file_init(&midi_file, MIDI_FORMAT_MULTI_TRACKS, 16, 48);

for(int t = 0; t < 16; t++) {
struct midi_track *track = midi_file.tracks + t;
char buf[256];
snprintf(buf, sizeof(buf), "Channel %c (%s)", mdx_track_name(t), t < 8 ? "FM" : "ADPCM");
midi_track_write_track_name(track, 0, buf, -1);
midi_track_write_bank_select(track, 0, t, 0);
midi_track_write_control_change(track, 0, t, 126, 127); // Monophonic mode
if(t >= 8) { // OPM only
midi_track_write_nrpn_msb(track, 0, t, 0, 112); // OPM Clock = 4MHz
}
midi_track_write_control_change(track, 0, t, 12, 0); // Amplitude LFO level
midi_track_write_control_change(track, 0, t, 13, 0); // Pitch LFO level
midi_track_write_rpn_msb(track, 0, t, 0, 32); // Pitch Bend Sensitivity
midi_track_write_pitch_bend(track, 0, t, 0x2000);
}

struct midi_track *first_track = midi_file_prepend_empty_track(&midi_file);
midi_track_write_time_signature(first_track, 0, 4, 2, 0, 0);
char buf[PATH_MAX];
Expand Down

0 comments on commit d094545

Please sign in to comment.