Skip to content

Commit

Permalink
reduced magic numbers, arp in poly modes is restricted to presets sav…
Browse files Browse the repository at this point in the history
…ed in 4.1 and above.
  • Loading branch information
Alex committed May 22, 2024
1 parent b5731eb commit 5277df1
Show file tree
Hide file tree
Showing 19 changed files with 1,193 additions and 565 deletions.
1 change: 1 addition & 0 deletions include/FM.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ void updateFMifNecessary(byte number);
void fmResetValues();
void fmUpdate();
void fm(byte number, byte data);
void fmMpe(byte channel, byte number, byte data);
void op(byte number);
void setupFM();
void WriteYMData(byte data);
Expand Down
125 changes: 125 additions & 0 deletions include/YM2612.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#define YM_MASTER_ADDR (0x22)
#define YM_CHN_ADDR (0x30)

static byte stagger[]{0, 6, 1, 7, 2, 8, 3, 9, 4, 10, 5, 11, 6, 12};

#define YM_MA_LFO_E offsetof(Master_t, LFO), 1, 3
#define YM_MA_LFO_F offsetof(Master_t, LFO), 3, 0
#define YM_MA_CH3_M offsetof(Master_t, CHAN3_MODE_TIMERS), 2, 6
Expand Down Expand Up @@ -113,6 +115,129 @@ class YM2612 {
void setRateScaling(int value) { setOperatorParameter(YM_OP_RS, value); }
void setSSG_EG(int value) { setOperatorParameter(YM_OP_SSG_EG, value); }

void setAmplitudeModulation(int channel, int value) {
channel = stagger[channel];
if (channel > 5) {
channel -= 6;
chip = 6;
} else {
chip = 2;
}

setOperatorParameter(channel, YM_OP_AM, (value > 0));
chip = 0;
}
void setAttackRate(int channel, int value) {
channel = stagger[channel];
if (channel > 5) {
channel -= 6;
chip = 6;
} else {
chip = 2;
}
setOperatorParameter(channel, YM_OP_AR, value);
chip = 0;
}
void setDecayRate(int channel, int value) {
channel = stagger[channel];
if (channel > 5) {
channel -= 6;
chip = 6;
} else {
chip = 2;
}
setOperatorParameter(channel, YM_OP_D1R, value);
chip = 0;
}
void setSustainRate(int channel, int value) {
channel = stagger[channel];
if (channel > 5) {
channel -= 6;
chip = 6;
} else {
chip = 2;
}
setOperatorParameter(channel, YM_OP_D2R, value);
chip = 0;
}
void setReleaseRate(int channel, int value) {
channel = stagger[channel];
if (channel > 5) {
channel -= 6;
chip = 6;
} else {
chip = 2;
}
setOperatorParameter(channel, YM_OP_RR, value);
chip = 0;
}
void setTotalLevel(int channel, int value) {
channel = stagger[channel];
if (channel > 5) {
channel -= 6;
chip = 6;
} else {
chip = 2;
}
setOperatorParameter(channel, YM_OP_TL, value);
chip = 0;
}
void setSustainLevel(int channel, int value) {
channel = stagger[channel];
if (channel > 5) {
channel -= 6;
chip = 6;
} else {
chip = 2;
}
setOperatorParameter(channel, YM_OP_D1L, value);
chip = 0;
}
void setMultiply(int channel, int value) {
channel = stagger[channel];
if (channel > 5) {
channel -= 6;
chip = 6;
} else {
chip = 2;
}
setOperatorParameter(channel, YM_OP_MUL, value);
chip = 0;
}
void setDetune(int channel, int value) {
channel = stagger[channel];
if (channel > 5) {
channel -= 6;
chip = 6;
} else {
chip = 2;
}
setOperatorParameter(channel, YM_OP_DT1, value);
chip = 0;
}
void setRateScaling(int channel, int value) {
channel = stagger[channel];
if (channel > 5) {
channel -= 6;
chip = 6;
} else {
chip = 2;
}
setOperatorParameter(channel, YM_OP_RS, value);
chip = 0;
}
void setSSG_EG(int channel, int value) {
channel = stagger[channel];
if (channel > 5) {
channel -= 6;
chip = 6;
} else {
chip = 2;
}
setOperatorParameter(channel, YM_OP_SSG_EG, value);
chip = 0;
}

void noteOn(byte chan);
void noteOff(byte chan);
void pitchBend(byte channel, int bend);
Expand Down
55 changes: 53 additions & 2 deletions include/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,59 @@
#ifndef MEGAFM_CONSTANTS_H
#define MEGAFM_CONSTANTS_H

const byte kVersion0 = 3;
const byte kVersion1 = 6;
#define KNOB_ARP_RATE 6
#define KNOB_ARP_RANGE 5
#define KNOB_LFO1_RATE 15
#define KNOB_LFO2_RATE 10
#define KNOB_LFO3_RATE 14
#define KNOB_LFO1_DEPTH 12
#define KNOB_LFO2_DEPTH 9
#define KNOB_LFO3_DEPTH 2
#define KNOB_VIB_RATE 48
#define KNOB_VIB_DEPTH 13
#define KNOB_FAT 28
#define KNOB_VOLUME 1
#define KNOB_FEEDBACK 3
#define KNOB_ALGO 4

#define FADER_DETUNE_1 18
#define FADER_MULT_1 27
#define FADER_LEVEL_1 19
#define FADER_ATTACK_1 29
#define FADER_DECAY_1 21
#define FADER_SUSTAIN_1 25
#define FADER_SUSTAIN_RATE_1 17
#define FADER_RELEASE_1 30

#define FADER_DETUNE_2 31
#define FADER_MULT_2 32
#define FADER_LEVEL_2 40
#define FADER_ATTACK_2 36
#define FADER_DECAY_2 44
#define FADER_SUSTAIN_2 42
#define FADER_SUSTAIN_RATE_2 34
#define FADER_RELEASE_2 11

#define FADER_DETUNE_3 20
#define FADER_MULT_3 24
#define FADER_LEVEL_3 16
#define FADER_ATTACK_3 8
#define FADER_DECAY_3 0
#define FADER_SUSTAIN_3 7
#define FADER_SUSTAIN_RATE_3 45
#define FADER_RELEASE_3 37

#define FADER_DETUNE_4 47
#define FADER_MULT_4 39
#define FADER_LEVEL_4 38
#define FADER_ATTACK_4 46
#define FADER_DECAY_4 33
#define FADER_SUSTAIN_4 41
#define FADER_SUSTAIN_RATE_4 43
#define FADER_RELEASE_4 35

const byte kVersion0 = 4;
const byte kVersion1 = 1;

const byte kDefaultSeq[16] = {0, 0, 0, 0, 12, 12, 12, 12, 0, 0, 12, 0, 0, 12, 12, 0};

Expand Down
17 changes: 12 additions & 5 deletions include/megafm.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
#define YM_IC 30
extern bool loopChanged;
extern bool loopHeld;
extern byte velocityLast; // keep track of velocity for LFO
extern byte v4Preset; // detect with a magic bit in the preset if it's already been saved within 4.x or disable arp in
// non unison mode
extern byte presetChordNumber;
extern bool newWide; // enable new wide modes
extern bool chord;
Expand Down Expand Up @@ -130,9 +131,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 Expand Up @@ -165,15 +167,20 @@ extern VoiceMode voiceMode;
extern bool sync;
extern bool ab;
extern int potLast[64];

extern int lastMpeVoice;
extern int pressureCounter;
extern int lfoDepth[3];
extern bool buttLast[19];
extern byte keyPressure[128];
extern int algoLast;

extern int polyPressure[12];
extern int polyVel[12];
extern byte fmBase[51], fmBaseLast[51], fmBaseLastNumber[51];
extern int fmData[51], fmDataLast[51];
extern bool linked[3][51];
extern byte lfoRandom[3][32];
extern byte valPlusPressureLast[12][36];
extern byte valPlusVelLast[12][36];
extern byte randomIndex[3];
extern byte octOffset; // offset the preset by 0-3 octaves
extern bool pressedUp, pressedDown;
Expand All @@ -195,10 +202,10 @@ extern byte lfoShape[3];
extern byte lfo[3], lfoLast[3];
extern int lfoStep[3];
extern int lfoStepLast[3];
extern int at, atDest, atLast, atGlideCounter;
extern bool lfoNewRand[3];
extern int lfoCounter[3], lfoSpeed[3];
extern bool retrig[3];
extern byte robin; // used for polyphonic ar voice ordering (round robin)
extern int fatLast;
extern float fat;
extern float bendy;
Expand Down
2 changes: 1 addition & 1 deletion include/midi.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void midiOut(byte note);
void pedalUp();
void pedalDown();
void dumpPreset();

void resyncArpLfo();
void sendControlChange(byte number, byte value, byte channel);
void sendNoteOff(byte note, byte velocity, byte channel);
void sendTool(byte note, byte velocity);
Expand Down
42 changes: 22 additions & 20 deletions include/midi_pedal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,29 @@ class MidiPedalAdapter {
: pedal_down{0}, note_on_callback(note_on_callback), note_off_callback(note_off_callback) {}

void set_pedal(uint8_t channel, bool pedal) {

this->pedal_down[channel] = pedal;

// when pedal goes down, we want to transfer the notes that are held to sustained
if (pedal) {
for (int i = 0; i < 128; i++) {
sustainedNotes[channel].set(i, heldNotes[channel].get(i));
}
} else {

// when pedal goes up, we want to kill the notes that are sustained
for (int i = 0; i < 128; i++) {
if (!heldNotes[channel].get(i) && sustainedNotes[channel].get(i)) {
//*(only if not held by fingers)
note_off_callback(channel, i);
if (pedal_downLast[channel] != pedal) {
pedal_downLast[channel] = pedal;
this->pedal_down[channel] = pedal;

// when pedal goes down, we want to transfer the notes that are held to sustained
if (pedal) {
for (int i = 0; i < 128; i++) {
sustainedNotes[channel].set(i, heldNotes[channel].get(i));
}
} else {

// when pedal goes up, we want to kill the notes that are sustained
for (int i = 0; i < 128; i++) {
if (!heldNotes[channel].get(i) && sustainedNotes[channel].get(i)) {
//*(only if not held by fingers)
note_off_callback(channel, i);
}
}
}

// clear sustained notes
for (int i = 0; i < 128; i++) {
sustainedNotes[channel].set(i, 0);
// clear sustained notes
for (int i = 0; i < 128; i++) {
sustainedNotes[channel].set(i, 0);
}
}
}
}
Expand Down Expand Up @@ -109,7 +111,7 @@ class MidiPedalAdapter {
BooleanArray128 heldNotes[16];
BooleanArray128 sustainedNotes[16];
bool pedal_down[16];

bool pedal_downLast[16];
note_on_callback_t note_on_callback;
note_off_callback_t note_off_callback;
};
Loading

0 comments on commit 5277df1

Please sign in to comment.