Skip to content

Commit

Permalink
first implmentation of per-channel volume option (libretro#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
eadmaster authored and eadmaster committed Nov 24, 2020
1 parent 93e27de commit fc79bb4
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 4 deletions.
2 changes: 2 additions & 0 deletions libgambatte/include/gambatte.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "serial_io.h"
#endif
#include "gbint.h"
#include "sound.h"
#include <string>
#include <cstddef>

Expand Down Expand Up @@ -140,6 +141,7 @@ class GB {
void *rombank1_ptr() const;
void *zeropage_ptr() const;
void *oamram_ptr() const;
PSG *getPSG() const;
#endif

private:
Expand Down
13 changes: 13 additions & 0 deletions libgambatte/libretro/libretro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,19 @@ static void check_variables(void)

/* Interframe blending option has its own handler */
check_frame_blend_variable();

char sound_channel_volume_base_str[] = "gambatte_channel_1_volume";
var.key = sound_channel_volume_base_str;
for (unsigned c = 0; c < 4; c++) {
sound_channel_volume_base_str[17] = c+'1';
if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
{
//psg_channels_volume[c] = ;
gb.getPSG()->setSoChanVolume(unsigned(atoi(var.value)), c);
//printf("ch %d: %u\n", c, unsigned(atoi(var.value)));
}
}


#ifdef HAVE_NETWORK

Expand Down
80 changes: 80 additions & 0 deletions libgambatte/libretro/libretro_core_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,86 @@ struct retro_core_option_definition option_defs_us[] = {
},
"10"
},
{
"gambatte_channel_1_volume",
"Square 1 Channel volume %",
"Modify Square 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"
},
{
"gambatte_channel_2_volume",
"Square 2 Channel volume %",
"Modify Square 2 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"
},
{
"gambatte_channel_3_volume",
"Wave Channel volume %",
"Modify Wave 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"
},
{
"gambatte_channel_4_volume",
"Noise Channel volume %",
"Modify Noise 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"
},
#ifdef HAVE_NETWORK
{
"gambatte_show_gb_link_settings",
Expand Down
1 change: 1 addition & 0 deletions libgambatte/src/gambatte-memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class Memory {
void *rombank1_ptr() const { return cart_.romdata(0) + 0x4000; }
void *zeropage_ptr() const { return (void*)(ioamhram_ + 0x0180); }
void *oamram_ptr() const { return (void*)ioamhram_; }
PSG *psg_ptr() const { return (PSG*)&psg_; }
#else
void loadSavedata() { cart_.loadSavedata(); }
void saveSavedata() { cart_.saveSavedata(); }
Expand Down
4 changes: 4 additions & 0 deletions libgambatte/src/gambatte.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ void *GB::zeropage_ptr() const {
void *GB::oamram_ptr() const {
return p_->cpu.oamram_ptr();
}

PSG *GB::getPSG() const {
return p_->cpu.mem_.psg_ptr();
}
#endif

}
Expand Down
19 changes: 15 additions & 4 deletions libgambatte/src/sound.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ namespace gambatte
uint_least32_t *const buf = buffer_ + bufferPos_;

std::memset(buf, 0, cycles * sizeof(uint_least32_t));
ch1_.update(buf, soVol_, cycles);
ch2_.update(buf, soVol_, cycles);
ch3_.update(buf, soVol_, cycles);
ch4_.update(buf, soVol_, cycles);
ch1_.update(buf, soChVol_[0], cycles);
ch2_.update(buf, soChVol_[1], cycles);
ch3_.update(buf, soChVol_[2], cycles);
ch4_.update(buf, soChVol_[3], cycles);
}

void PSG::generateSamples(unsigned long const cycleCounter, bool const doubleSpeed)
Expand Down Expand Up @@ -180,6 +180,17 @@ namespace gambatte
soVol_ = (((nr50 & 0x7) + 1) * so1Mul
+ ((nr50 >> 4 & 0x7) + 1) * so2Mul) * 64;
}

void PSG::setSoChanVolume(const unsigned nr50, const unsigned ch)
{
if( ch < 0 || ch > 3 )
return; // invalid channel number

unsigned long soChVol = (((nr50 & 0x7) + 1) * so1Mul
+ ((nr50 >> 4 & 0x7) + 1) * so2Mul) * 64;

soChVol_[ch] = soChVol;
}

void PSG::mapSo(const unsigned nr51)
{
Expand Down
2 changes: 2 additions & 0 deletions libgambatte/src/sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class PSG {
void setNr44(unsigned data) { ch4_.setNr4(data); }

void setSoVolume(unsigned nr50);
void setSoChanVolume(unsigned nr50, unsigned ch);
void mapSo(unsigned nr51);
unsigned getStatus() const;

Expand All @@ -83,6 +84,7 @@ class PSG {
std::size_t bufferPos_;
unsigned long lastUpdate_;
unsigned long soVol_;
unsigned long soChVol_[5];
uint_least32_t rsum_;
bool enabled_;

Expand Down

0 comments on commit fc79bb4

Please sign in to comment.