From 5ba5c7abd5deb5093b8728eb4b9bb1a2d0e4bf42 Mon Sep 17 00:00:00 2001 From: Robert Hargreaves Date: Tue, 23 Jul 2024 20:56:05 +0100 Subject: [PATCH] Implement portamento for all MIDI channels --- src/midi.c | 51 ++++++++++++++++--------------- tests/unit/test_midi.h | 2 +- tests/unit/test_midi_portamento.c | 33 +++++++++++--------- 3 files changed, 45 insertions(+), 41 deletions(-) diff --git a/src/midi.c b/src/midi.c index 80582a2..1df1180 100644 --- a/src/midi.c +++ b/src/midi.c @@ -923,32 +923,33 @@ void midi_reset(void) void midi_tick(void) { - u8 chan = 0; - MidiChannel* midiChannel = &midiChannels[chan]; - if (midiChannel->portamento) { - - for (DeviceChannel* state = &deviceChannels[0]; state < &deviceChannels[DEV_CHANS]; - state++) { - if (state->midiChannel == chan && state->noteOn) { - - const s8 increment = 10; - - if (state->glideTargetPitch > state->pitch) { - state->cents += increment; - if (state->cents == 100) { - state->pitch++; - state->cents = 0; - } - state->ops->pitch(state->number, state->pitch, state->cents); - } else if (state->glideTargetPitch < state->pitch - || (state->glideTargetPitch == state->pitch && state->cents > 0)) { - state->cents -= increment; - if (state->cents == (0 - increment)) { - state->pitch--; - state->cents = 100 - increment; + for (u8 chan = 0; chan < MIDI_CHANNELS; chan++) { + MidiChannel* midiChannel = &midiChannels[chan]; + if (midiChannel->portamento) { + + for (DeviceChannel* state = &deviceChannels[0]; state < &deviceChannels[DEV_CHANS]; + state++) { + if (state->midiChannel == chan && state->noteOn) { + + const s8 increment = 10; + + if (state->glideTargetPitch > state->pitch) { + state->cents += increment; + if (state->cents == 100) { + state->pitch++; + state->cents = 0; + } + state->ops->pitch(state->number, state->pitch, state->cents); + } else if (state->glideTargetPitch < state->pitch + || (state->glideTargetPitch == state->pitch && state->cents > 0)) { + state->cents -= increment; + if (state->cents == (0 - increment)) { + state->pitch--; + state->cents = 100 - increment; + } + state->ops->pitch(state->number, state->pitch, state->cents); + } else { } - state->ops->pitch(state->number, state->pitch, state->cents); - } else { } } } diff --git a/tests/unit/test_midi.h b/tests/unit/test_midi.h index 6cf8af2..405b4b4 100644 --- a/tests/unit/test_midi.h +++ b/tests/unit/test_midi.h @@ -1,6 +1,6 @@ #pragma once #include "cmocka_inc.h" - +#include "debug.h" #include "midi.h" #include "midi_fm.h" #include "midi_psg.h" diff --git a/tests/unit/test_midi_portamento.c b/tests/unit/test_midi_portamento.c index 9dc6a8c..f79579b 100644 --- a/tests/unit/test_midi_portamento.c +++ b/tests/unit/test_midi_portamento.c @@ -2,26 +2,29 @@ static void test_midi_portamento_glides_note_up(UNUSED void** state) { - __real_midi_cc(0, CC_PORTAMENTO_ENABLE, 127); + for (u8 chan = 0; chan < MAX_FM_CHANS; chan++) { + debug_message("channel %d\n", chan); + __real_midi_cc(chan, CC_PORTAMENTO_ENABLE, 127); - expect_synth_pitch(0, 2, 0x439); - expect_synth_volume_any(); - expect_synth_noteOn(0); - __real_midi_note_on(0, MIDI_PITCH_A2, MAX_MIDI_VOLUME); - __real_midi_note_on(0, MIDI_PITCH_C4, MAX_MIDI_VOLUME); + expect_synth_pitch(chan, 2, 0x439); + expect_synth_volume_any(); + expect_synth_noteOn(chan); + __real_midi_note_on(chan, MIDI_PITCH_A2, MAX_MIDI_VOLUME); + __real_midi_note_on(chan, MIDI_PITCH_C4, MAX_MIDI_VOLUME); - expect_synth_pitch(0, 2, 0x43f); - midi_tick(); - for (u16 i = 0; i < 148; i++) { - expect_synth_pitch_any(); + expect_synth_pitch(chan, 2, 0x43f); midi_tick(); - } + for (u16 i = 0; i < 148; i++) { + expect_synth_pitch_any(); + midi_tick(); + } - expect_synth_pitch(0, 4, 644); - midi_tick(); + expect_synth_pitch(chan, 4, 644); + midi_tick(); - midi_tick(); - midi_tick(); + midi_tick(); + midi_tick(); + } } static void test_midi_portamento_glides_note_down(UNUSED void** state)