Skip to content

Commit

Permalink
id_sd_sdl: Make register write delays configurable
Browse files Browse the repository at this point in the history
Currently, we delay a few samples after every register write. This can
be confusing, particularly with Nuked_OPL3, which also delays a couple
of samples when OPL3_WriteRegBuffered() is used. It's also ugly to have
to track all of the extra samples queued here and there.

Make the number of delayed samples configurable with:
sd_oplWriteDelay = <num>

It's still based of the sample rate by default, otherwise put the number
of samples you want, or 0 to disable it entirely.

You can also disable/enable the OPL3_WriteRegBuffered() function with
sd_nukedBufferWrites. By default, it's only enabled when
sd_oplWriteDelay is nonzero.

A PR to make NukedOPL3's buffering configurable exists here:
nukeykt/Nuked-OPL3#27

Note that NukedOPL3's buffering is based in the 49716Hz samples, not the
resampled values, like our sd_oplWriteDelay.
  • Loading branch information
sulix committed Jan 26, 2023
1 parent 6de0283 commit 53b7037
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/id_sd_sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ static bool sd_sdl_queueAudio = false;
#endif
static int sd_sdl_queueAudioMinBufSize = 0;

static int sd_oplDelaySamples;
static bool sd_nukedBufferWrites;

#define PC_PIT_RATE 1193182
#define SD_SFX_PART_RATE 140
/* In the original exe, upon setting a rate of 140Hz or 560Hz for some
Expand Down Expand Up @@ -141,9 +144,16 @@ static inline bool YM3812Init(int numChips, int clock, int rate)
static inline void YM3812Write(Chip *which, Bit32u reg, Bit8u val)
{
if (sd_oplEmulator == SD_OPL_EMULATOR_DBOPL)
{
Chip__WriteReg(which, reg, val);
}
else if (sd_oplEmulator == SD_OPL_EMULATOR_NUKED)
OPL3_WriteRegBuffered(&nuked_oplChip, reg, val);
{
if (sd_nukedBufferWrites)
OPL3_WriteRegBuffered(&nuked_oplChip, reg, val);
else
OPL3_WriteReg(&nuked_oplChip, reg, val);
}
}

static inline void YM3812UpdateOne(Chip *which, int16_t *stream, int length)
Expand Down Expand Up @@ -278,7 +288,7 @@ void SD_SDL_alOut(uint8_t reg, uint8_t val)
// at the least. For now a hack is implied.
YM3812Write(&oplChip, reg, val);
// Hack comes with a "magic number" that appears to make it work better
int length = SD_SDL_AudioSpec.freq / 10000;
int length = sd_oplDelaySamples;
if (length)
{
#ifdef SD_SDL_WITH_QUEUEAUDIO
Expand Down Expand Up @@ -506,6 +516,9 @@ void SD_SDL_Startup(void)
#endif
sd_sdl_queueAudioMinBufSize = CFG_GetConfigInt("sd_sdl_queueAudioMinBufSize", 0);

sd_oplDelaySamples = CFG_GetConfigInt("sd_oplDelaySamples", SD_SDL_AudioSpec.freq / 10000);
sd_nukedBufferWrites = CFG_GetConfigBool("sd_nukedBufferWrites", sd_oplDelaySamples == 0);

// Setup a condition variable to signal threads waiting for timer updates.
SD_SDL_WaitTicksSpin = CFG_GetConfigBool("sd_sdl_waitTicksSpin", false);
if (!SD_SDL_WaitTicksSpin)
Expand Down

0 comments on commit 53b7037

Please sign in to comment.