From 96c01963064ebc7403b550dc76521b239d7198a0 Mon Sep 17 00:00:00 2001 From: Samuel Walker Date: Fri, 14 Feb 2025 16:44:22 -0700 Subject: [PATCH] Fixing audio glitches with wave and noise channels --- CMakeLists.txt | 1 + include/audio.h | 22 +++++++++++++- include/ppu.h | 3 +- lib/audio.c | 79 ++++++++++++++++++++++++++++++++++++++---------- lib/ppu.c | 1 + lib/ppu_sm.c | 5 ++-- lib/ui.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++--- 7 files changed, 166 insertions(+), 25 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5c656c1..89c905e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,7 @@ cmake_minimum_required(VERSION 3.10) set(BUILD_SHARED_LIBS OFF) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") add_subdirectory(deps/SDL) add_subdirectory(deps/SDL_ttf) add_subdirectory(deps/portaudio) diff --git a/include/audio.h b/include/audio.h index a99a309..9883c1c 100644 --- a/include/audio.h +++ b/include/audio.h @@ -99,6 +99,24 @@ typedef struct { u8 wave_ram[16]; + float sq1_history[4410]; + u32 sq1_index; + + float sq2_history[4410]; + u32 sq2_index; + + float ch3_history[4410]; + u32 ch3_index; + + float ch4_history[4410]; + u32 ch4_index; + + float left_history[4410]; + u32 left_index; + + float right_history[4410]; + u32 right_index; + } audio_context; void audio_init(); @@ -106,4 +124,6 @@ void audio_tick(); void audio_period_tick(); u8 audio_read(u16 address); -void audio_write(u16 address, u8 value); \ No newline at end of file +void audio_write(u16 address, u8 value); + +audio_context *audio_get_context(); \ No newline at end of file diff --git a/include/ppu.h b/include/ppu.h index fd3b72a..2cec42a 100644 --- a/include/ppu.h +++ b/include/ppu.h @@ -7,8 +7,6 @@ static const int TICKS_PER_LINE = 456; static const int YRES = 144; static const int XRES = 160; -static u32 target_frame_time; - bool window_visible(); typedef enum { @@ -81,6 +79,7 @@ typedef struct { u32 *video_buffer; bool rendering_window; + u32 target_frame_time; } ppu_context; void ppu_init(); diff --git a/lib/audio.c b/lib/audio.c index 829feb2..0e7ee4f 100644 --- a/lib/audio.c +++ b/lib/audio.c @@ -9,11 +9,17 @@ #define FRAMES_PER_BUFFER 64 #define TIME_PER_SAMPLE 1.0f / (SAMPLE_RATE/1000.0f/1000.0f) #define TIME_PER_AUDIO_TICK 1.0f / (1048576.0f/1000.0f/1000.0f) +#define TIME_PER_WAVE_TICK 1.0f / (2097152.0f/1000.0f/1000.0f) #define LFSR_BASE_CLOCK 262144.0f static audio_context ctx; +audio_context *audio_get_context() { + return &ctx; +} + static float audio_time = 0; +static float wave_time = 0; static float lfsr_timer = 0; static float lfsr_clock = LFSR_BASE_CLOCK; @@ -81,10 +87,10 @@ static int audio_callback(const void* input_uffer, void *output_buffer, float right = 0; for(int i = 0; i < framesPerBuffer; i++) { audio_time += TIME_PER_SAMPLE; + wave_time += TIME_PER_SAMPLE; for(;audio_time >= TIME_PER_AUDIO_TICK;audio_time -= TIME_PER_AUDIO_TICK) { ctx.sq1_period_timer++; ctx.sq2_period_timer++; - ctx.ch3_period_timer++; if(ctx.sq1_period_timer >= 0x800) { ctx.sq1_period_timer = ctx.sq1_period_reset; @@ -94,6 +100,9 @@ static int audio_callback(const void* input_uffer, void *output_buffer, ctx.sq2_period_timer = ctx.sq2_period_reset; ctx.sq2_sample = (ctx.sq2_sample + 1) % 8; } + } + for(;wave_time >= TIME_PER_WAVE_TICK;wave_time -= TIME_PER_WAVE_TICK) { + ctx.ch3_period_timer++; if(ctx.ch3_period_timer >= 0x800) { ctx.ch3_period_timer = ctx.ch3_period_reset; ctx.ch3_sample = (ctx.ch3_sample + 1) % 32; @@ -111,32 +120,39 @@ static int audio_callback(const void* input_uffer, void *output_buffer, u8 new = !((ctx.ch4_lfsr & 0b1) ^ ((ctx.ch4_lfsr >> 1) & 0b1)) & 0b1; ctx.ch4_lfsr |= (new << 15); if(ctx.ch4_lfsr_width) { + ctx.ch4_lfsr &= ~(1 << 7); ctx.ch4_lfsr |= (new << 7); } ctx.ch4_lfsr = ctx.ch4_lfsr >> 1; //printf("lfsr: %02X, bit: %d\n", ctx.ch4_lfsr, new); } } + float sq1_val = 0; + float sq2_val = 0; + float ch3_val = 0; + float ch4_val = 0; if(ctx.audio_enabled){ if(ctx.sq1_enable) { + sq1_val = ((float)ctx.sq1_volume/15.0f) * (((float)(square_sample[ctx.sq1_duty][ctx.sq1_sample]) - 7.5f)/7.5f); if(ctx.ch1_left) { - left += ((float)ctx.sq1_volume/15.0f) * (((float)(square_sample[ctx.sq1_duty][ctx.sq1_sample]) - 7.5f)/7.5f); + left += sq1_val; } if(ctx.ch1_right) { - right += ((float)ctx.sq1_volume/15.0f) * (((float)square_sample[ctx.sq1_duty][ctx.sq1_sample] - 7.5f)/7.5f); + right += sq1_val; } } if(ctx.sq2_enable) { + sq2_val = ((float)ctx.sq2_volume/15.0f) * (((float)(square_sample[ctx.sq2_duty][ctx.sq2_sample]) - 7.5f)/7.5f); if(ctx.ch2_left) { - left += ((float)ctx.sq2_volume/15.0f) * (((float)(square_sample[ctx.sq2_duty][ctx.sq2_sample]) - 7.5f)/7.5f); + left += sq2_val; } if(ctx.ch2_right) { - right += ((float)ctx.sq2_volume/15.0f) * (((float)(square_sample[ctx.sq2_duty][ctx.sq2_sample]) - 7.5f)/7.5f); + right += sq2_val; } } - if(ctx.ch3_enable && ctx.ch3_volume != 00) { + if(ctx.ch3_enable && ctx.ch3_volume != 0x0) { u8 shift = 0; if(ctx.ch3_volume == 0b10) { shift = 1; @@ -144,25 +160,46 @@ static int audio_callback(const void* input_uffer, void *output_buffer, if(ctx.ch3_volume == 0b11) { shift = 2; } + ch3_val = (((float)(ctx.ch3_last_sample >> shift) - 7.5f)/15.0f); if(ctx.ch3_left) { - left += (((float)(ctx.ch3_last_sample >> shift) - 7.5f)/7.5f); + left += ch3_val; //printf("left: %d\n", ctx.ch3_volume); } if(ctx.ch3_right) { - right += (((float)(ctx.ch3_last_sample >> shift) - 7.5f)/7.5f); + right += ch3_val; } } if(ctx.ch4_enable) { + ch4_val = ((float)ctx.ch4_volume/15.0f) * ((ctx.ch4_lfsr & 0b1) ? 0 : 1.0f); if(ctx.ch4_left) { - left += ((float)ctx.ch4_volume/15.0f) * (ctx.ch4_lfsr & 0b1); + left += ch4_val; //printf("left: %d\n", ctx.ch3_volume); } if(ctx.ch4_right) { - right += ((float)ctx.ch4_volume/15.0f) * (ctx.ch4_lfsr & 0b1); + right += ch4_val; } } } + ctx.sq1_history[ctx.sq1_index++] = sq1_val; + if (ctx.sq1_index == 4410) { + ctx.sq1_index = 0; + } + + ctx.sq2_history[ctx.sq2_index++] = sq2_val; + if (ctx.sq2_index == 4410) { + ctx.sq2_index = 0; + } + + ctx.ch3_history[ctx.ch3_index++] = ch3_val; + if (ctx.ch3_index == 4410) { + ctx.ch3_index = 0; + } + + ctx.ch4_history[ctx.ch4_index++] = ch4_val; + if (ctx.ch4_index == 4410) { + ctx.ch4_index = 0; + } u8 left_vol = ctx.volume_left == 0 ? 1 : ctx.volume_left; u8 right_vol = ctx.volume_right == 0 ? 1 : ctx.volume_right; left *= (float)left_vol/7.0f; @@ -185,6 +222,16 @@ static int audio_callback(const void* input_uffer, void *output_buffer, right_cap = right - right_out * 0.996f; } + ctx.left_history[ctx.left_index++] = left_out; + if (ctx.left_index == 4410) { + ctx.left_index = 0; + } + + ctx.right_history[ctx.right_index++] = right_out; + if (ctx.right_index == 4410) { + ctx.right_index = 0; + } + *out++ = left_out; *out++ = right_out; } @@ -426,7 +473,7 @@ void enable_wave() { } ctx.ch3_enable = true; ctx.ch3_sample = 0; - ctx.ch3_volume = ctx.ch3_initial_volume; + //ctx.ch3_volume = ctx.ch3_initial_volume; ctx.ch3_period_timer = ctx.ch3_period_reset; } @@ -521,7 +568,7 @@ u8 audio_read(u16 address) { } if(address == 0xFF1C) { - return ((ctx.ch3_initial_volume & 0b11) << 5) | 0b10011111; + return ((ctx.ch3_volume & 0b11) << 5) | 0b10011111; } if(address == 0xFF1D) { @@ -783,9 +830,10 @@ void audio_write(u16 address, u8 value){ } if(address == 0xFF1A) { - ctx.ch3_dac = value & 0x80; + ctx.ch3_dac = value >> 7; if(!ctx.ch3_dac){ ctx.ch3_enable = false; + //printf("ch3 off\n"); } } @@ -796,7 +844,8 @@ void audio_write(u16 address, u8 value){ } if(address == 0xFF1C) { - ctx.ch3_initial_volume = (value >> 5) & 0b11; + ctx.ch3_volume = (value >> 5) & 0b11; + //printf("Ch3 vol: %d\n", ctx.ch3_volume); } if(address == 0xFF1D) { @@ -850,7 +899,7 @@ void audio_write(u16 address, u8 value){ ctx.ch4_lfsr_width = (value & 0x08); ctx.ch4_clock_divider = value & 0b111; float div = (ctx.ch4_clock_divider == 0 ? 0.5f : ctx.ch4_clock_divider); - float lfsr_rate = (LFSR_BASE_CLOCK) / div * (1 << ctx.ch4_clock_shift); + float lfsr_rate = (LFSR_BASE_CLOCK) / (div * (1 << ctx.ch4_clock_shift)); lfsr_clock = 1.0f / (lfsr_rate/1000.0f/1000.0f); } diff --git a/lib/ppu.c b/lib/ppu.c index 80252cf..9d57e79 100644 --- a/lib/ppu.c +++ b/lib/ppu.c @@ -16,6 +16,7 @@ void ppu_init() { ctx.current_frame = 0; ctx.line_ticks = 0; ctx.video_buffer = malloc(YRES * XRES * sizeof(u32)); + ctx.target_frame_time = 1000/60; ctx.pfc.line_x = 0; ctx.pfc.pushed_x = 0; diff --git a/lib/ppu_sm.c b/lib/ppu_sm.c index f355ec9..81b4750 100644 --- a/lib/ppu_sm.c +++ b/lib/ppu_sm.c @@ -131,7 +131,6 @@ void ppu_mode_vblank() { } } -static u32 target_frame_time = 1000/60; static long prev_frame_time = 0; static long start_timer = 0; static long frame_count = 0; @@ -155,8 +154,8 @@ void ppu_mode_hblank() { u32 end = get_ticks(); u32 frame_time = end - prev_frame_time; - if(frame_time < target_frame_time) { - delay((target_frame_time - frame_time)); + if(frame_time < ppu_get_context()->target_frame_time) { + delay((ppu_get_context()->target_frame_time - frame_time)); } if (end - start_timer >= 1000) { diff --git a/lib/ui.c b/lib/ui.c index 6c66679..98c3627 100644 --- a/lib/ui.c +++ b/lib/ui.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -127,10 +128,81 @@ void ui_update() { SDL_FillRect(screen, &rc, video_buffer[x + (line_num * XRES)]); } } - SDL_UpdateTexture(sdlTexture, NULL, screen->pixels, screen->pitch); SDL_RenderClear(sdlRenderer); SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL); + int yOffset = 50; + int lastY = (yOffset) + (audio_get_context()->sq1_history[0] * 50); + int xOffset = XRES*scale; + SDL_SetRenderDrawColor(sdlRenderer, 0, 255, 0, 255); + for(int x = 1; x < 882; x++){ + int y = (yOffset) + (audio_get_context()->sq1_history[x*5] * 50); + SDL_RenderDrawLine(sdlRenderer, x-1+xOffset, lastY, x+xOffset, y); + lastY = y; + if(x+xOffset == SCREEN_WIDTH) { + break; + } + } + + yOffset = 150; + lastY = (yOffset) + (audio_get_context()->sq2_history[0] * 50); + SDL_SetRenderDrawColor(sdlRenderer, 0, 255, 0, 255); + for(int x = 1; x < 882; x++){ + int y = (yOffset) + (audio_get_context()->sq2_history[x*5] * 50); + SDL_RenderDrawLine(sdlRenderer, x-1+xOffset, lastY, x+xOffset, y); + lastY = y; + if(x+xOffset == SCREEN_WIDTH) { + break; + } + } + + yOffset = 250; + lastY = (yOffset) + (audio_get_context()->ch3_history[0] * 50); + SDL_SetRenderDrawColor(sdlRenderer, 0, 255, 0, 255); + for(int x = 1; x < 882; x++){ + int y = (yOffset) + (audio_get_context()->ch3_history[x*5] * 50); + SDL_RenderDrawLine(sdlRenderer, x-1+xOffset, lastY, x+xOffset, y); + lastY = y; + if(x+xOffset == SCREEN_WIDTH) { + break; + } + } + + yOffset = 350; + lastY = (yOffset) + (audio_get_context()->ch4_history[0] * 50); + SDL_SetRenderDrawColor(sdlRenderer, 0, 255, 0, 255); + for(int x = 1; x < 882; x++){ + int y = (yOffset) + (audio_get_context()->ch4_history[x*5] * 50); + SDL_RenderDrawLine(sdlRenderer, x-1+xOffset, lastY, x+xOffset, y); + lastY = y; + if(x+xOffset == SCREEN_WIDTH) { + break; + } + } + + yOffset = 450; + lastY = (yOffset) + (audio_get_context()->left_history[0] * 50); + SDL_SetRenderDrawColor(sdlRenderer, 0, 0, 255, 255); + for(int x = 1; x < 882; x++){ + int y = (yOffset) + (audio_get_context()->left_history[x*5] * 50); + SDL_RenderDrawLine(sdlRenderer, x-1+xOffset, lastY, x+xOffset, y); + lastY = y; + if(x+xOffset == SCREEN_WIDTH) { + break; + } + } + + yOffset = 550; + lastY = (yOffset) + (audio_get_context()->right_history[0] * 50); + SDL_SetRenderDrawColor(sdlRenderer, 0, 0, 255, 255); + for(int x = 1; x < 882; x++){ + int y = (yOffset) + (audio_get_context()->right_history[x*5] * 50); + SDL_RenderDrawLine(sdlRenderer, x-1+xOffset, lastY, x+xOffset, y); + lastY = y; + if(x+xOffset == SCREEN_WIDTH) { + break; + } + } SDL_RenderPresent(sdlRenderer); update_debug_window(); @@ -142,11 +214,11 @@ void ui_on_key(bool down, u32 key_code) { if(key_code == SDLK_SPACE && down == true) { ff = !ff; if(ff){ - target_frame_time = 1000/300; + ppu_get_context()->target_frame_time = 1000/300; } else { - target_frame_time = 1000/60; + ppu_get_context()->target_frame_time = 1000/60; } - printf("target frame time: %d\n", target_frame_time); + printf("target frame time: %d\n", ppu_get_context()->target_frame_time); } switch(key_code){ case SDLK_z: gamepad_get_state()->b = down; break;