From 86d6fb9a2f232c8e83ebdcdae5357cf3fa66d565 Mon Sep 17 00:00:00 2001 From: kub Date: Sat, 2 Oct 2021 09:32:57 +0200 Subject: [PATCH] sound, increase resolution for calculating psg sound --- pico/memory.c | 7 ++----- pico/pico_int.h | 6 +++--- pico/sound/sound.c | 36 ++++++++++++++++++++++-------------- 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/pico/memory.c b/pico/memory.c index 25e78310..04875465 100644 --- a/pico/memory.c +++ b/pico/memory.c @@ -394,13 +394,11 @@ void NOINLINE ctl_write_z80reset(u32 d) } } -static int get_scanline(int is_from_z80); - static void psg_write_68k(u32 d) { // look for volume write and update if needed if ((d & 0x90) == 0x90) - PsndDoPSG(Pico.m.scanline); + PsndDoPSG(z80_cycles_from_68k()); SN76496Write(d); } @@ -408,8 +406,7 @@ static void psg_write_68k(u32 d) static void psg_write_z80(u32 d) { if ((d & 0x90) == 0x90) { - int scanline = get_scanline(1); - PsndDoPSG(scanline); + PsndDoPSG(z80_cyclesDone()); } SN76496Write(d); diff --git a/pico/pico_int.h b/pico/pico_int.h index 2038b6d9..f4dcef3c 100644 --- a/pico/pico_int.h +++ b/pico/pico_int.h @@ -914,9 +914,9 @@ PICO_INTERNAL void PsndExit(void); PICO_INTERNAL void PsndReset(void); PICO_INTERNAL void PsndStartFrame(void); PICO_INTERNAL void PsndDoDAC(int cycle_to); -PICO_INTERNAL void PsndDoPSG(int line_to); -PICO_INTERNAL void PsndDoYM2413(int line_to); -PICO_INTERNAL void PsndDoFM(int line_to); +PICO_INTERNAL void PsndDoPSG(int cyc_to); +PICO_INTERNAL void PsndDoYM2413(int cyc_to); +PICO_INTERNAL void PsndDoFM(int cyc_to); PICO_INTERNAL void PsndClear(void); PICO_INTERNAL void PsndGetSamples(int y); PICO_INTERNAL void PsndGetSamplesMS(int y); diff --git a/pico/sound/sound.c b/pico/sound/sound.c index 5cb6832e..69cd4fb3 100644 --- a/pico/sound/sound.c +++ b/pico/sound/sound.c @@ -161,20 +161,24 @@ PICO_INTERNAL void PsndDoDAC(int cyc_to) Pico.snd.dac_val = dout; } -PICO_INTERNAL void PsndDoPSG(int line_to) +PICO_INTERNAL void PsndDoPSG(int cyc_to) { int pos, len; int stereo = 0; - // Q16, number of samples since last call - len = ((line_to+1) * Pico.snd.smpl_mult) - Pico.snd.psg_pos; - if (len <= 0) - return; + // number of samples to fill in buffer (Q20) + len = (cyc_to * Pico.snd.clkl_mult) - Pico.snd.psg_pos; // update position and calculate buffer offset and length - pos = (Pico.snd.psg_pos+0x8000) >> 16; + pos = (Pico.snd.psg_pos+0x80000) >> 20; Pico.snd.psg_pos += len; - len = ((Pico.snd.psg_pos+0x8000) >> 16) - pos; + len = ((Pico.snd.psg_pos+0x80000) >> 20) - pos; + + // avoid loss of the 1st sample of a new block (Q rounding issues) + if (pos+len == 0) + len = 1, Pico.snd.psg_pos += 0x80000; + if (len <= 0) + return; if (!PicoIn.sndOut || !(PicoIn.opt & POPT_EN_PSG)) return; @@ -187,21 +191,25 @@ PICO_INTERNAL void PsndDoPSG(int line_to) } #if 0 -PICO_INTERNAL void PsndDoYM2413(int line_to) +PICO_INTERNAL void PsndDoYM2413(int cyc_to) { int pos, len; int stereo = 0; short *buf; - // Q16, number of samples since last call - len = ((line_to+1) * Pico.snd.smpl_mult) - Pico.snd.ym2413_pos; - if (len <= 0) - return; + // number of samples to fill in buffer (Q20) + len = (cyc_to * Pico.snd.clkl_mult) - Pico.snd.ym2413_pos; // update position and calculate buffer offset and length - pos = (Pico.snd.ym2413_pos+0x8000) >> 16; + pos = (Pico.snd.ym2413_pos+0x80000) >> 20; Pico.snd.ym2413_pos += len; - len = ((Pico.snd.ym2413_pos+0x8000) >> 16) - pos; + len = ((Pico.snd.ym2413_pos+0x80000) >> 20) - pos; + + // avoid loss of the 1st sample of a new block (Q rounding issues) + if (pos+len == 0) + len = 1, Pico.snd.ym2413_pos += 0x80000; + if (len <= 0) + return; if (!PicoIn.sndOut || !(PicoIn.opt & POPT_EN_YM2413)) return; -- 2.39.5