diff --git a/source/sound/sn76489.c b/source/sound/sn76489.c index 52bbc47..08b3b72 100644 --- a/source/sound/sn76489.c +++ b/source/sound/sn76489.c @@ -325,7 +325,7 @@ int SN76489_Update(INT16 *buffer, int clock_length) return blip_read_samples(blip, buffer); } -int SN76489_Sync(unsigned int samples) +int SN76489_Clocks(int length) { - return blip_clocks_needed(blip, samples); -} + return blip_clocks_needed(blip, length); +} \ No newline at end of file diff --git a/source/sound/sn76489.h b/source/sound/sn76489.h index 91ee243..7dfe110 100644 --- a/source/sound/sn76489.h +++ b/source/sound/sn76489.h @@ -17,6 +17,7 @@ extern uint8 *SN76489_GetContextPtr(void); extern int SN76489_GetContextSize(void); extern void SN76489_Write(int data); extern int SN76489_Update(INT16 *buffer, int clock_length); -extern int SN76489_Sync(unsigned int samples); +extern int SN76489_Clocks(int length); extern void SN76489_BoostNoise(int boost); + #endif /* _SN76489_H_ */ \ No newline at end of file diff --git a/source/sound/sound.c b/source/sound/sound.c index 387683a..ac76fc4 100644 --- a/source/sound/sound.c +++ b/source/sound/sound.c @@ -271,14 +271,14 @@ int sound_context_load(uint8 *state) /* End of frame update, return the number of samples run so far. */ int sound_update(unsigned int cycles) { - int size, delta; + int size, avail; /* run PSG & FM chips until end of frame */ cycles <<= 11; psg_update(cycles); fm_update(cycles); - /* return available FM samples */ + /* check number of available FM samples */ if (config.hq_fm) { size = Fir_Resampler_avail(); @@ -291,22 +291,25 @@ int sound_update(unsigned int cycles) error("%d FM samples available\n",size); #endif - /* compare with number of PSG samples available */ - delta = size - (snd.psg.pos - snd.psg.buffer); + /* check number of available PSG samples */ + avail = snd.psg.pos - snd.psg.buffer; #ifdef LOGSOUND - error("%d PSG samples available\n", snd.psg.pos - snd.psg.buffer); + error("%d PSG samples available\n", avail); #endif /* resynchronize FM & PSG chips */ - if (delta > 0) + if (size > avail) { - /* PSG chip is late, get needed samples */ - snd.psg.pos += SN76489_Update(snd.psg.pos, SN76489_Sync(delta)); + /* FM chip is ahead */ + fm_cycles_count += SN76489_Clocks(size - avail) * psg_cycles_ratio; + + /* return number of available samples */ + size = avail; } else { - /* PSG chip is ahead, adjust clocks count */ - psg_cycles_count += (SN76489_Sync(-delta) * psg_cycles_ratio); + /* PSG chip is ahead */ + psg_cycles_count += SN76489_Clocks(avail - size) * psg_cycles_ratio; } #ifdef LOGSOUND