Skip to content

Commit

Permalink
Implement portamento for all MIDI channels
Browse files Browse the repository at this point in the history
  • Loading branch information
rhargreaves committed Jul 23, 2024
1 parent cf80a46 commit 5ba5c7a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 41 deletions.
51 changes: 26 additions & 25 deletions src/midi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_midi.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include "cmocka_inc.h"

#include "debug.h"
#include "midi.h"
#include "midi_fm.h"
#include "midi_psg.h"
Expand Down
33 changes: 18 additions & 15 deletions tests/unit/test_midi_portamento.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5ba5c7a

Please sign in to comment.