From 1820da8c64d8ef8aa519b56d16c1a52bb359d968 Mon Sep 17 00:00:00 2001 From: Mateusz Faderewski Date: Fri, 18 Aug 2023 14:01:05 +0200 Subject: [PATCH] audio: fix 48 kHz bug on NTSC consoles and missing static variable initialization --- src/audio.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/audio.c b/src/audio.c index dbd284ba38..fd112fa4e7 100644 --- a/src/audio.c +++ b/src/audio.c @@ -252,9 +252,14 @@ void audio_init(const int frequency, int numbuffers) numbuffers = sizeof(buf_full) * 8; } - /* Remember frequency */ - AI_regs->dacrate = ((2 * clockrate / frequency) + 1) / 2 - 1; - AI_regs->samplesize = 15; + /* Calculate DAC dacrate and sample size */ + int dacrate = ((2 * clockrate / frequency) + 1) / 2; + /* Sample size must be less than or equal to dacrate divided by 66 and not exceed 16 */ + int samplesize = MIN(dacrate / 66, 16); + + /* Setup DAC parameters */ + AI_regs->dacrate = dacrate - 1; + AI_regs->samplesize = samplesize - 1; /* Real frequency */ _frequency = 2 * clockrate / ((2 * clockrate / frequency) + 1); @@ -289,6 +294,7 @@ void audio_init(const int frequency, int numbuffers) /* Set up ring buffer pointers */ now_playing = 0; + playing_queue = 0; now_empty = 0; now_writing = 0; buf_full = 0; @@ -326,6 +332,11 @@ void audio_close() set_AI_interrupt(0); unregister_AI_handler(audio_callback); + /* Stop audio DMA and clocks */ + AI_regs->control = 0; + AI_regs->dacrate = 0; + AI_regs->samplesize = 0; + if(buffers) { for(int i = 0; i < _num_buf; i++)