Skip to content

Commit

Permalink
Format docs
Browse files Browse the repository at this point in the history
  • Loading branch information
rhargreaves committed Jul 17, 2024
1 parent 05fb4c3 commit 3c235e4
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 262 deletions.
19 changes: 7 additions & 12 deletions src/midi_fm.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ void midi_fm_reset(void);
static const FmChannel** presets;
static const PercussionPreset** percussionPresets;

void midi_fm_init(const FmChannel** defaultPresets,
const PercussionPreset** defaultPercussionPresets)
void midi_fm_init(
const FmChannel** defaultPresets, const PercussionPreset** defaultPercussionPresets)
{
presets = defaultPresets;
percussionPresets = defaultPercussionPresets;
Expand Down Expand Up @@ -101,16 +101,12 @@ u16 midi_fm_pitchToFreqNum(u8 pitch, u16 pitchBend)
s16 approxFreqDelta;
if (bendRelative < 0) {
// bend down
u16 boundFreq
= lookupFreqNum(pitch, -GENERAL_MIDI_PITCH_BEND_SEMITONE_RANGE);
approxFreqDelta
= (((freq - boundFreq) * bendRelative) / DEFAULT_MIDI_PITCH_BEND);
u16 boundFreq = lookupFreqNum(pitch, -GENERAL_MIDI_PITCH_BEND_SEMITONE_RANGE);
approxFreqDelta = (((freq - boundFreq) * bendRelative) / DEFAULT_MIDI_PITCH_BEND);
} else {
// bend up
u16 boundFreq
= lookupFreqNum(pitch, GENERAL_MIDI_PITCH_BEND_SEMITONE_RANGE);
approxFreqDelta
= (((boundFreq - freq) * bendRelative) / DEFAULT_MIDI_PITCH_BEND);
u16 boundFreq = lookupFreqNum(pitch, GENERAL_MIDI_PITCH_BEND_SEMITONE_RANGE);
approxFreqDelta = (((boundFreq - freq) * bendRelative) / DEFAULT_MIDI_PITCH_BEND);
}
return freq + approxFreqDelta;
}
Expand Down Expand Up @@ -166,8 +162,7 @@ u8 midi_fm_pitchToOctave(u8 pitch)

static u16 lookupFreqNum(u8 pitch, s16 offset)
{
u16 index = FREQ_NORMAL_RANGE_OFFSET
+ ((u8)(pitch - MIN_MIDI_PITCH) % SEMITONES) + offset;
u16 index = FREQ_NORMAL_RANGE_OFFSET + ((u8)(pitch - MIN_MIDI_PITCH) % SEMITONES) + offset;
u16 freq = FREQS[index];
return freq;
}
Expand Down
65 changes: 24 additions & 41 deletions src/synth.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
#include <ym2612.h>
#include "debug.h"

static Global global
= { .lfoEnable = 1, .lfoFrequency = 0, .specialMode = false };
static Global global = { .lfoEnable = 1, .lfoFrequency = 0, .specialMode = false };
static FmChannel fmChannels[MAX_FM_CHANS];
static u8 noteOn;
static u8 volumes[MAX_FM_CHANS];
Expand All @@ -15,23 +14,20 @@ static ParameterUpdatedCallback* parameterUpdatedCallback = NULL;

static const u8 MAX_VOLUME = 0x7F;

static const u8 VOLUME_TO_TOTAL_LEVELS[] = { 127, 122, 117, 113, 108, 104, 100,
97, 93, 89, 86, 83, 80, 77, 74, 71, 68, 66, 63, 61, 58, 56, 54, 52, 50, 48,
46, 44, 43, 41, 40, 38, 37, 35, 34, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23,
22, 21, 20, 19, 19, 18, 17, 17, 16, 15, 15, 14, 13, 13, 12, 12, 11, 11, 11,
10, 10, 9, 9, 9, 8, 8, 7, 7, 7, 7, 6, 6, 6, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 3,
3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
static const u8 VOLUME_TO_TOTAL_LEVELS[] = { 127, 122, 117, 113, 108, 104, 100, 97, 93, 89, 86, 83,
80, 77, 74, 71, 68, 66, 63, 61, 58, 56, 54, 52, 50, 48, 46, 44, 43, 41, 40, 38, 37, 35, 34, 32,
31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 19, 18, 17, 17, 16, 15, 15, 14, 13, 13, 12,
12, 11, 11, 11, 10, 10, 9, 9, 9, 8, 8, 7, 7, 7, 7, 6, 6, 6, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 3, 3,
3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0 };

static void updateChannel(u8 chan);
static void writeGlobalLfo(void);
static void writeAlgorithmAndFeedback(u8 channel);
static void writeOperatorMultipleAndDetune(u8 channel, u8 op);
static void writeOperatorRateScalingAndAttackRate(u8 channel, u8 operator);
static void writeOperatorAmplitudeModulationAndFirstDecayRate(
u8 channel, u8 operator);
static void writeOperatorReleaseRateAndSecondaryAmplitude(
u8 channel, u8 operator);
static void writeOperatorAmplitudeModulationAndFirstDecayRate(u8 channel, u8 operator);
static void writeOperatorReleaseRateAndSecondaryAmplitude(u8 channel, u8 operator);
static void writeOperatorTotalLevel(u8 channel, u8 operator);
static void writeOperatorSecondaryDecayRate(u8 channel, u8 operator);
static void writeOperatorSsgEg(u8 channel, u8 operator);
Expand All @@ -47,8 +43,7 @@ static u8 effectiveTotalLevel(u8 channel, u8 operator, u8 totalLevel);
static bool isOutputOperator(u8 algorithm, u8 operator);
static u8 volumeAdjustedTotalLevel(u8 channel, u8 totalLevel);
static void channelParameterUpdated(u8 channel);
static void otherParameterUpdated(
u8 channel, ParameterUpdated parameterUpdated);
static void otherParameterUpdated(u8 channel, ParameterUpdated parameterUpdated);
static void writeRegSafe(u8 part, u8 reg, u8 data);

void synth_init(const FmChannel* initialPreset)
Expand Down Expand Up @@ -207,8 +202,7 @@ void synth_operatorFirstDecayRate(u8 channel, u8 op, u8 firstDecayRate)
channelParameterUpdated(channel);
}

void synth_operatorAmplitudeModulation(
u8 channel, u8 op, u8 amplitudeModulation)
void synth_operatorAmplitudeModulation(u8 channel, u8 op, u8 amplitudeModulation)
{
getOperator(channel, op)->amplitudeModulation = amplitudeModulation;
writeOperatorAmplitudeModulationAndFirstDecayRate(channel, op);
Expand Down Expand Up @@ -315,8 +309,7 @@ static void writeGlobalLfo(void)
static void writeOctaveAndFrequency(u8 channel)
{
FmChannel* chan = fmChannel(channel);
writeChannelReg(
channel, 0xA4, (chan->freqNumber >> 8) | (chan->octave << 3));
writeChannelReg(channel, 0xA4, (chan->freqNumber >> 8) | (chan->octave << 3));
writeChannelReg(channel, 0xA0, chan->freqNumber);
}

Expand All @@ -329,8 +322,7 @@ static void writeAlgorithmAndFeedback(u8 channel)
static void writeStereoAmsFms(u8 channel)
{
FmChannel* chan = fmChannel(channel);
writeChannelReg(
channel, 0xB4, (chan->stereo << 6) + (chan->ams << 4) + chan->fms);
writeChannelReg(channel, 0xB4, (chan->stereo << 6) + (chan->ams << 4) + chan->fms);
}

static void writeOperatorMultipleAndDetune(u8 channel, u8 operator)
Expand All @@ -342,16 +334,13 @@ static void writeOperatorMultipleAndDetune(u8 channel, u8 operator)
static void writeOperatorRateScalingAndAttackRate(u8 channel, u8 operator)
{
Operator* op = getOperator(channel, operator);
writeOperatorReg(
channel, operator, 0x50, op->attackRate + (op->rateScaling << 6));
writeOperatorReg(channel, operator, 0x50, op->attackRate + (op->rateScaling << 6));
}

static void writeOperatorAmplitudeModulationAndFirstDecayRate(
u8 channel, u8 operator)
static void writeOperatorAmplitudeModulationAndFirstDecayRate(u8 channel, u8 operator)
{
Operator* op = getOperator(channel, operator);
writeOperatorReg(channel, operator, 0x60,
op->firstDecayRate + (op->amplitudeModulation << 7));
writeOperatorReg(channel, operator, 0x60, op->firstDecayRate + (op->amplitudeModulation << 7));
}

static void writeOperatorSecondaryDecayRate(u8 channel, u8 operator)
Expand All @@ -360,12 +349,10 @@ static void writeOperatorSecondaryDecayRate(u8 channel, u8 operator)
writeOperatorReg(channel, operator, 0x70, op->secondaryDecayRate);
}

static void writeOperatorReleaseRateAndSecondaryAmplitude(
u8 channel, u8 operator)
static void writeOperatorReleaseRateAndSecondaryAmplitude(u8 channel, u8 operator)
{
Operator* op = getOperator(channel, operator);
writeOperatorReg(channel, operator, 0x80,
op->releaseRate + (op->secondaryAmplitude << 4));
writeOperatorReg(channel, operator, 0x80, op->releaseRate + (op->secondaryAmplitude << 4));
}

static void writeOperatorSsgEg(u8 channel, u8 operator)
Expand All @@ -377,8 +364,8 @@ static void writeOperatorSsgEg(u8 channel, u8 operator)
static void writeOperatorTotalLevel(u8 channel, u8 operator)
{
Operator* op = getOperator(channel, operator);
writeOperatorReg(channel, operator, 0x40,
effectiveTotalLevel(channel, operator, op->totalLevel));
writeOperatorReg(
channel, operator, 0x40, effectiveTotalLevel(channel, operator, op->totalLevel));
}

static u8 effectiveTotalLevel(u8 channel, u8 operator, u8 totalLevel)
Expand All @@ -390,8 +377,7 @@ static u8 effectiveTotalLevel(u8 channel, u8 operator, u8 totalLevel)

static bool isOutputOperator(u8 algorithm, u8 op)
{
return (algorithm < 4 && op == 3)
|| (algorithm == 4 && (op == 1 || op == 3))
return (algorithm < 4 && op == 3) || (algorithm == 4 && (op == 1 || op == 3))
|| ((algorithm == 5 || algorithm == 6) && op > 0) || algorithm == 7;
}

Expand All @@ -400,8 +386,7 @@ static u8 volumeAdjustedTotalLevel(u8 channel, u8 totalLevel)
u8 volume = volumes[channel];
u8 logarithmicVolume = 0x7F - VOLUME_TO_TOTAL_LEVELS[volume];
u8 inverseTotalLevel = 0x7F - totalLevel;
u8 inverseNewTotalLevel
= (u16)inverseTotalLevel * (u16)logarithmicVolume / (u16)0x7F;
u8 inverseNewTotalLevel = (u16)inverseTotalLevel * (u16)logarithmicVolume / (u16)0x7F;
return 0x7F - inverseNewTotalLevel;
}

Expand Down Expand Up @@ -448,8 +433,7 @@ void synth_specialModeVolume(u8 operator, u8 volume)

u8 logarithmicVolume = 0x7F - VOLUME_TO_TOTAL_LEVELS[volume];
u8 inverseTotalLevel = 0x7F - op->totalLevel;
u8 inverseNewTotalLevel
= (u16)inverseTotalLevel * (u16)logarithmicVolume / (u16)0x7F;
u8 inverseNewTotalLevel = (u16)inverseTotalLevel * (u16)logarithmicVolume / (u16)0x7F;
u8 newTotalLevel = 0x7F - inverseNewTotalLevel;

writeOperatorReg(CH_SPECIAL_MODE, operator, 0x40,
Expand All @@ -468,8 +452,7 @@ void synth_enableDac(bool enable)

static void writeRegSafe(u8 part, u8 reg, u8 data)
{
debug_message("call: YM2612_writeReg(part=%d, reg=0x%X, data=0x%X)\n", part,
reg, data);
debug_message("call: YM2612_writeReg(part=%d, reg=0x%X, data=0x%X)\n", part, reg, data);

bool takenAlready = Z80_getAndRequestBus(TRUE);
YM2612_writeReg(part, reg, data);
Expand Down
Loading

0 comments on commit 3c235e4

Please sign in to comment.