Skip to content

Commit

Permalink
fix crashing arp when starting with rate knob at zero
Browse files Browse the repository at this point in the history
fix stuck notes after arp
vel/polyat now modulates global params (was limited to operator params)
  • Loading branch information
Alex committed May 10, 2024
1 parent 3c07776 commit 6dfd609
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 32 deletions.
3 changes: 2 additions & 1 deletion include/megafm.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ extern int lastNumber;
extern byte lfoClockSpeed[3];
extern byte lfoClockSpeedPending[3];
extern byte lfoClockSpeedPendingLast[3];
extern byte absoluteClockCounter;
extern int masterClockCounter;
extern bool newFat;
extern YM2612 ym;
extern byte latestChannel;//keep track of latest voice for global velocity/aftertouch modulation
extern int heldKeys;
extern byte lastNote, dotTimer;
extern int arpCounter, arpStep, preset;
Expand Down
3 changes: 2 additions & 1 deletion src/MEGAfm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,14 @@ bool resetHeld;
bool shuffled;
bool voiceHeld;
int lastNumber = 255;
byte latestChannel;//keep track of latest voice for global velocity/aftertouch modulation
byte lfoClockSpeed[3];
// To keep the lfo beat in sync with the MIDI master clock,
// this is used to store a new lfo rate until the incoming MIDI clock counter resets,
// whereas changing immediately would make it go out of sync.
byte lfoClockSpeedPending[3];
byte lfoClockSpeedPendingLast[3];
byte absoluteClockCounter;
int masterClockCounter;
YM2612 ym;
int heldKeys;
byte lastNote, dotTimer;
Expand Down
5 changes: 5 additions & 0 deletions src/arp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,9 @@ void arpFire() {
break;
}
}
if (!heldKeys) {
for (int i = 0; i < 12; i++) {
ym.noteOff(i);
}
}
}
24 changes: 24 additions & 0 deletions src/loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ void loop() {
// apply polyPressure
for (int ch = 0; ch < 12; ch++) {
for (int i = 0; i < 37; i++) {
// modulate the operator params (poly)
if (linked[2][i]) {
int valPlusPressure = fmData[i] + map(polyPressure[ch], 0, 255, 0, fmData[41]);
valPlusPressure = constrain(valPlusPressure, 0, 255);
Expand All @@ -30,6 +31,17 @@ void loop() {
}
}
}
if (ch == latestChannel) {
// modulate the global params (mono)
for (int i = 37; i < 51; i++) {
// modulate the operator params (poly)
if (linked[2][i]) {
int valPlusPressure = fmData[i] + map(polyPressure[ch], 0, 255, 0, fmData[41]);
valPlusPressure = constrain(valPlusPressure, 0, 255);
fmData[i] = valPlusPressure;
}
}
}
}
}

Expand All @@ -46,6 +58,18 @@ void loop() {
}
}
}

if (ch == latestChannel) {
// modulate the global params (mono)
for (int i = 37; i < 51; i++) {
// modulate the operator params (poly)
if (linked[0][i]) {
int valPlusVel = fmData[i] + map(polyVel[ch], 0, 255, 0, fmData[37]);
valPlusVel = constrain(valPlusVel, 0, 255);
fmData[i] = valPlusVel;
}
}
}
}
}

Expand Down
56 changes: 26 additions & 30 deletions src/midi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ void handlePolyAT(byte channel, byte note, byte val) {
if (notey[i] == note - 10) {
polyPressure[i] = val << 1;
lastMpeVoice = i;
latestChannel = i;
}
}
}
Expand All @@ -92,10 +93,10 @@ void handleClock() {
}
}

absoluteClockCounter++;
if (absoluteClockCounter >= 48) {
masterClockCounter++;
if (masterClockCounter >= 48) {
leftDot();
absoluteClockCounter = 0;
masterClockCounter = 0;

resyncArpLfo();
}
Expand Down Expand Up @@ -199,7 +200,7 @@ void handleStart() {
if (vibratoClockEnable)
vibIndex = 0;
resyncArpLfo();
absoluteClockCounter = 0;
masterClockCounter = 0;
seqStep = 0;
arpClockCounter = 0;
sync = true;
Expand Down Expand Up @@ -324,6 +325,7 @@ static void handleNoteOn(byte channel, byte note, byte velocity) {
if (i == channel)
polyVel[i] = velocity << 1;
}
latestChannel = channel;
}
} else {

Expand Down Expand Up @@ -356,7 +358,7 @@ static void handleNoteOn(byte channel, byte note, byte velocity) {
case kVoicingWide4:
case kVoicingWide3:

if ((arpMode) && (fmData[46])) {
if (arpMode) {
// ARP

heldKeys++;
Expand Down Expand Up @@ -390,21 +392,25 @@ static void handleNoteOn(byte channel, byte note, byte velocity) {
}

noteOfVoice[voiceSlot] = note;
latestChannel = voiceSlot;

lastNotey[heldKeys] = note;

for (int i = 0; i < ymfChannelsPerVoice; i++) {
setNote(ymfChannelsPerVoice * voiceSlot + i, noteOfVoice[voiceSlot]);
ym.noteOff(ymfChannelsPerVoice * voiceSlot + i);
ym.noteOn(ymfChannelsPerVoice * voiceSlot + i);

}


if (heldKeys < 127)
heldKeys++;
}
if (lfoVel && velocity) {
// set velocity amount to channel
for (int ch = 0; ch < 12; ch++) {
if (notey[ch] == note) {
if ((notey[ch] == note) || arpMode) {
polyVel[ch] = velocity << 1;
}
}
Expand All @@ -417,7 +423,7 @@ static void handleNoteOn(byte channel, byte note, byte velocity) {
// dual CH3
//// //// //// //// //// //// //// //// //// //// //// //// ////
/////// ////
if ((arpMode) && (fmData[46])) {
if (arpMode) {
// ARP

heldKeys++;
Expand Down Expand Up @@ -477,11 +483,12 @@ static void handleNoteOn(byte channel, byte note, byte velocity) {
if (lfoVel && velocity) {
// set velocity amount to channel
for (int ch = 0; ch < 12; ch++) {
if (notey[ch] == note - 10) {
if ((notey[ch] == note - 10) || arpMode) {
polyVel[ch] = velocity << 1;
}
}
}
latestChannel = 0;
break;

case kVoicingUnison:
Expand All @@ -490,7 +497,7 @@ static void handleNoteOn(byte channel, byte note, byte velocity) {
// unison
//// //// //// //// //// //// //// //// //// //// //// //// ////
/////// ////
if ((arpMode) && (fmData[46])) {
if (arpMode) {
// ARP

heldKeys++;
Expand All @@ -516,12 +523,13 @@ static void handleNoteOn(byte channel, byte note, byte velocity) {
addNote(note);

for (int i = 0; i < 12; i++) {
if (lfoVel && velocity)
if ((lfoVel && velocity) || arpMode)
polyVel[i] = velocity << 1;
ym.noteOff(i);
setNote(i, note);
ym.noteOn(i);
}
latestChannel = 0;
}

break;
Expand Down Expand Up @@ -594,24 +602,12 @@ static void handleNoteOff(byte channel, byte note) {
heldKeys--;
removeNote(note);

if (heldKeys < 1) {
heldKeys = 0;

for (int i = 0; i < 12; i++) {
ym.noteOff(i);
}
}

} else {
// no arp
nVoices = nVoicesForMode(voiceMode);
ymfChannelsPerVoice = 12 / nVoices;

heldKeys--;
if (heldKeys < 1) {
heldKeys = 0;
}

// scan through the noteOfVoices and kill the voice associated to it
for (int v = 0; v < nVoices; v++) {
if ((voiceSlots[v]) && (noteOfVoice[v] == note)) {
Expand Down Expand Up @@ -659,14 +655,6 @@ static void handleNoteOff(byte channel, byte note) {
heldKeys--;
removeNote(note);

if (heldKeys < 1) {
heldKeys = 0;

for (int i = 0; i < 12; i++) {
ym.noteOff(i);
}
}

} else {
// NO ARP

Expand Down Expand Up @@ -707,6 +695,14 @@ static void handleNoteOff(byte channel, byte note) {

leftDot();
}
if (heldKeys < 1) {
heldKeys = 0;

clearNotes();
for (int i = 0; i < 12; i++) {
ym.noteOff(i);
}
}
}
}
}
Expand Down

0 comments on commit 6dfd609

Please sign in to comment.