Skip to content

Commit

Permalink
added Mega Drive / Genesis FM channels volume core options (libretro#223
Browse files Browse the repository at this point in the history
)
  • Loading branch information
eadmaster authored and eadmaster committed Nov 30, 2020
1 parent 0359e04 commit e431fc4
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 33 deletions.
21 changes: 8 additions & 13 deletions core/sound/psg.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ static struct
int chanDelta[4][2];
int chanOut[4][2];
int chanAmp[4][2];
int chanUserVolume[4];
} psg;

static void psg_update(unsigned int clocks);
Expand Down Expand Up @@ -379,14 +378,6 @@ void psg_write(unsigned int clocks, unsigned int data)
psg.regs[index] = data;
}

void psg_set_user_channel_volume(unsigned int channel, unsigned int volume)
{
if(channel > 3 || volume > 100) return; // invalid args
// else

psg.chanUserVolume[channel] = volume;
}

void psg_config(unsigned int clocks, unsigned int preamp, unsigned int panning)
{
int i;
Expand All @@ -407,8 +398,11 @@ void psg_config(unsigned int clocks, unsigned int preamp, unsigned int panning)
int volume = psg.regs[i*2+1];

/* update channel stereo amplification */
psg.chanAmp[i][0] = preamp * (psg.chanUserVolume[i]/100) * ((panning >> (i + 4)) & 1);
psg.chanAmp[i][1] = preamp * (psg.chanUserVolume[i]/100) * ((panning >> (i + 0)) & 1);
//psg.chanAmp[i][0] = preamp * ((panning >> (i + 4)) & 1);
//psg.chanAmp[i][1] = preamp * ((panning >> (i + 0)) & 1);
psg.chanAmp[i][0] = preamp * (config.psg_ch_volumes[i] / 100) * ((panning >> (i + 4)) & 1);
psg.chanAmp[i][1] = preamp * (config.psg_ch_volumes[i] / 100) * ((panning >> (i + 0)) & 1);


/* tone channels */
if (i < 3)
Expand All @@ -435,8 +429,9 @@ void psg_config(unsigned int clocks, unsigned int preamp, unsigned int panning)
}

/* update channel volume */
psg.chanOut[i][0] = (volume * psg.chanAmp[i][0]) / 100;
psg.chanOut[i][1] = (volume * psg.chanAmp[i][1]) / 100;
// 2FIX: not working here?
psg.chanOut[i][0] = (volume * psg.chanAmp[i][0] * (config.psg_ch_volumes[i] / 100) ) / 100;
psg.chanOut[i][1] = (volume * psg.chanAmp[i][1] * (config.psg_ch_volumes[i] / 100) ) / 100;
}
}

Expand Down
8 changes: 8 additions & 0 deletions core/sound/ym2612.c
Original file line number Diff line number Diff line change
Expand Up @@ -2089,6 +2089,14 @@ void YM2612Update(int *buffer, int length)
else if (out_fm[4] < -8192) out_fm[4] = -8192;
if (out_fm[5] > 8191) out_fm[5] = 8191;
else if (out_fm[5] < -8192) out_fm[5] = -8192;

/* user volume scaling */
out_fm[0] *= (config.md_ch_volumes[0] / 100);
out_fm[1] *= (config.md_ch_volumes[1] / 100);
out_fm[2] *= (config.md_ch_volumes[2] / 100);
out_fm[3] *= (config.md_ch_volumes[3] / 100);
out_fm[4] *= (config.md_ch_volumes[4] / 100);
out_fm[5] *= (config.md_ch_volumes[5] / 100);

/* stereo DAC output panning & mixing */
lt = ((out_fm[0]) & ym2612.OPN.pan[0]);
Expand Down
51 changes: 31 additions & 20 deletions libretro/libretro.c
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,7 @@ static void config_default(void)
config.ym2612 = YM2612_DISCRETE;
config.ym2413 = 2; /* AUTO */
config.mono = 0; /* STEREO output */
//config.psg_chs_volume = {100, 100, 100, 100}; /* individual channels volume */
#ifdef HAVE_YM3438_CORE
config.ym3438 = 0;
#endif
Expand Down Expand Up @@ -1540,26 +1541,6 @@ static void check_variables(bool first_run)
}
}

char sound_channel_volume_base_str[] = "genesis_plus_gx_psg_channel_0_volume";
var.key = sound_channel_volume_base_str;
for (unsigned c = 0; c < 4; c++) {
sound_channel_volume_base_str[28] = c+'0';
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
{
psg_set_user_channel_volume(c, atoi(var.value));
// need to recall config to have the settings applied
if ((system_hw & SYSTEM_PBC) == SYSTEM_MD)
{
psg_config(0, config.psg_preamp, 0xff);
}
else
{
psg_config(0, config.psg_preamp, io_reg[6]);
}
}
}


var.key = "genesis_plus_gx_fm_preamp";
environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var);
{
Expand Down Expand Up @@ -1835,6 +1816,36 @@ static void check_variables(bool first_run)
config.no_sprite_limit = 1;
}

char psg_channel_volume_base_str[] = "genesis_plus_gx_psg_channel_0_volume";
var.key = psg_channel_volume_base_str;
for (unsigned c = 0; c < 4; c++) {
psg_channel_volume_base_str[28] = c+'0';
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
{
config.psg_ch_volumes[c] = atoi(var.value);
// need to recall config to have the settings applied
if ((system_hw & SYSTEM_PBC) == SYSTEM_MD)
{
psg_config(0, config.psg_preamp, 0xff);
}
else
{
psg_config(0, config.psg_preamp, io_reg[6]);
}
}
}

char md_fm_channel_volume_base_str[] = "genesis_plus_gx_md_channel_0_volume";
var.key = md_fm_channel_volume_base_str;
for (unsigned c = 0; c < 6; c++) {
md_fm_channel_volume_base_str[27] = c+'0';
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var))
{
config.md_ch_volumes[c] = atoi(var.value);

}
}

if (reinit)
{
#ifdef HAVE_OVERCLOCK
Expand Down
120 changes: 120 additions & 0 deletions libretro/libretro_core_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,126 @@ struct retro_core_option_definition option_defs_us[] = {
},
"100"
},
{
"genesis_plus_gx_md_channel_0_volume",
"Mega Drive FM Channel 0 Volume %",
"",
{
{ "0", NULL },
{ "10", NULL },
{ "20", NULL },
{ "30", NULL },
{ "40", NULL },
{ "50", NULL },
{ "60", NULL },
{ "70", NULL },
{ "80", NULL },
{ "90", NULL },
{ "100", NULL },
{ NULL, NULL },
},
"100"
},
{
"genesis_plus_gx_md_channel_1_volume",
"Mega Drive FM Channel 1 Volume %",
"",
{
{ "0", NULL },
{ "10", NULL },
{ "20", NULL },
{ "30", NULL },
{ "40", NULL },
{ "50", NULL },
{ "60", NULL },
{ "70", NULL },
{ "80", NULL },
{ "90", NULL },
{ "100", NULL },
{ NULL, NULL },
},
"100"
},
{
"genesis_plus_gx_md_channel_2_volume",
"Mega Drive FM Channel 1 Volume %",
"",
{
{ "0", NULL },
{ "10", NULL },
{ "20", NULL },
{ "30", NULL },
{ "40", NULL },
{ "50", NULL },
{ "60", NULL },
{ "70", NULL },
{ "80", NULL },
{ "90", NULL },
{ "100", NULL },
{ NULL, NULL },
},
"100"
},
{
"genesis_plus_gx_md_channel_3_volume",
"Mega Drive FM Channel 3 Volume %",
"",
{
{ "0", NULL },
{ "10", NULL },
{ "20", NULL },
{ "30", NULL },
{ "40", NULL },
{ "50", NULL },
{ "60", NULL },
{ "70", NULL },
{ "80", NULL },
{ "90", NULL },
{ "100", NULL },
{ NULL, NULL },
},
"100"
},
{
"genesis_plus_gx_md_channel_4_volume",
"Mega Drive FM Channel 4 Volume %",
"",
{
{ "0", NULL },
{ "10", NULL },
{ "20", NULL },
{ "30", NULL },
{ "40", NULL },
{ "50", NULL },
{ "60", NULL },
{ "70", NULL },
{ "80", NULL },
{ "90", NULL },
{ "100", NULL },
{ NULL, NULL },
},
"100"
},
{
"genesis_plus_gx_md_channel_5_volume",
"Mega Drive FM Channel 5 Volume %",
"",
{
{ "0", NULL },
{ "10", NULL },
{ "20", NULL },
{ "30", NULL },
{ "40", NULL },
{ "50", NULL },
{ "60", NULL },
{ "70", NULL },
{ "80", NULL },
{ "90", NULL },
{ "100", NULL },
{ NULL, NULL },
},
"100"
},
{ NULL, NULL, NULL, {{0}}, NULL },
};

Expand Down
2 changes: 2 additions & 0 deletions libretro/osd.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ typedef struct
uint8 gun_cursor;
uint32 overclock;
uint8 no_sprite_limit;
uint8 psg_ch_volumes[4];
uint8 md_ch_volumes[6];
} t_config;

extern t_config config;
Expand Down

0 comments on commit e431fc4

Please sign in to comment.