Fixing audio glitches with wave and noise channels

This commit is contained in:
Samuel Walker 2025-02-14 16:44:22 -07:00
parent 8fc7992ca5
commit 96c0196306
7 changed files with 166 additions and 25 deletions

View File

@ -1,6 +1,7 @@
cmake_minimum_required(VERSION 3.10) cmake_minimum_required(VERSION 3.10)
set(BUILD_SHARED_LIBS OFF) set(BUILD_SHARED_LIBS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
add_subdirectory(deps/SDL) add_subdirectory(deps/SDL)
add_subdirectory(deps/SDL_ttf) add_subdirectory(deps/SDL_ttf)
add_subdirectory(deps/portaudio) add_subdirectory(deps/portaudio)

View File

@ -99,6 +99,24 @@ typedef struct {
u8 wave_ram[16]; 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; } audio_context;
void audio_init(); void audio_init();
@ -106,4 +124,6 @@ void audio_tick();
void audio_period_tick(); void audio_period_tick();
u8 audio_read(u16 address); u8 audio_read(u16 address);
void audio_write(u16 address, u8 value); void audio_write(u16 address, u8 value);
audio_context *audio_get_context();

View File

@ -7,8 +7,6 @@ static const int TICKS_PER_LINE = 456;
static const int YRES = 144; static const int YRES = 144;
static const int XRES = 160; static const int XRES = 160;
static u32 target_frame_time;
bool window_visible(); bool window_visible();
typedef enum { typedef enum {
@ -81,6 +79,7 @@ typedef struct {
u32 *video_buffer; u32 *video_buffer;
bool rendering_window; bool rendering_window;
u32 target_frame_time;
} ppu_context; } ppu_context;
void ppu_init(); void ppu_init();

View File

@ -9,11 +9,17 @@
#define FRAMES_PER_BUFFER 64 #define FRAMES_PER_BUFFER 64
#define TIME_PER_SAMPLE 1.0f / (SAMPLE_RATE/1000.0f/1000.0f) #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_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 #define LFSR_BASE_CLOCK 262144.0f
static audio_context ctx; static audio_context ctx;
audio_context *audio_get_context() {
return &ctx;
}
static float audio_time = 0; static float audio_time = 0;
static float wave_time = 0;
static float lfsr_timer = 0; static float lfsr_timer = 0;
static float lfsr_clock = LFSR_BASE_CLOCK; 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; float right = 0;
for(int i = 0; i < framesPerBuffer; i++) { for(int i = 0; i < framesPerBuffer; i++) {
audio_time += TIME_PER_SAMPLE; audio_time += TIME_PER_SAMPLE;
wave_time += TIME_PER_SAMPLE;
for(;audio_time >= TIME_PER_AUDIO_TICK;audio_time -= TIME_PER_AUDIO_TICK) { for(;audio_time >= TIME_PER_AUDIO_TICK;audio_time -= TIME_PER_AUDIO_TICK) {
ctx.sq1_period_timer++; ctx.sq1_period_timer++;
ctx.sq2_period_timer++; ctx.sq2_period_timer++;
ctx.ch3_period_timer++;
if(ctx.sq1_period_timer >= 0x800) { if(ctx.sq1_period_timer >= 0x800) {
ctx.sq1_period_timer = ctx.sq1_period_reset; 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_period_timer = ctx.sq2_period_reset;
ctx.sq2_sample = (ctx.sq2_sample + 1) % 8; 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) { if(ctx.ch3_period_timer >= 0x800) {
ctx.ch3_period_timer = ctx.ch3_period_reset; ctx.ch3_period_timer = ctx.ch3_period_reset;
ctx.ch3_sample = (ctx.ch3_sample + 1) % 32; 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; u8 new = !((ctx.ch4_lfsr & 0b1) ^ ((ctx.ch4_lfsr >> 1) & 0b1)) & 0b1;
ctx.ch4_lfsr |= (new << 15); ctx.ch4_lfsr |= (new << 15);
if(ctx.ch4_lfsr_width) { if(ctx.ch4_lfsr_width) {
ctx.ch4_lfsr &= ~(1 << 7);
ctx.ch4_lfsr |= (new << 7); ctx.ch4_lfsr |= (new << 7);
} }
ctx.ch4_lfsr = ctx.ch4_lfsr >> 1; ctx.ch4_lfsr = ctx.ch4_lfsr >> 1;
//printf("lfsr: %02X, bit: %d\n", ctx.ch4_lfsr, new); //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.audio_enabled){
if(ctx.sq1_enable) { 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) { 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) { 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) { 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) { 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) { 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; u8 shift = 0;
if(ctx.ch3_volume == 0b10) { if(ctx.ch3_volume == 0b10) {
shift = 1; shift = 1;
@ -144,25 +160,46 @@ static int audio_callback(const void* input_uffer, void *output_buffer,
if(ctx.ch3_volume == 0b11) { if(ctx.ch3_volume == 0b11) {
shift = 2; shift = 2;
} }
ch3_val = (((float)(ctx.ch3_last_sample >> shift) - 7.5f)/15.0f);
if(ctx.ch3_left) { 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); //printf("left: %d\n", ctx.ch3_volume);
} }
if(ctx.ch3_right) { if(ctx.ch3_right) {
right += (((float)(ctx.ch3_last_sample >> shift) - 7.5f)/7.5f); right += ch3_val;
} }
} }
if(ctx.ch4_enable) { if(ctx.ch4_enable) {
ch4_val = ((float)ctx.ch4_volume/15.0f) * ((ctx.ch4_lfsr & 0b1) ? 0 : 1.0f);
if(ctx.ch4_left) { 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); //printf("left: %d\n", ctx.ch3_volume);
} }
if(ctx.ch4_right) { 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 left_vol = ctx.volume_left == 0 ? 1 : ctx.volume_left;
u8 right_vol = ctx.volume_right == 0 ? 1 : ctx.volume_right; u8 right_vol = ctx.volume_right == 0 ? 1 : ctx.volume_right;
left *= (float)left_vol/7.0f; 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; 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++ = left_out;
*out++ = right_out; *out++ = right_out;
} }
@ -426,7 +473,7 @@ void enable_wave() {
} }
ctx.ch3_enable = true; ctx.ch3_enable = true;
ctx.ch3_sample = 0; 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; ctx.ch3_period_timer = ctx.ch3_period_reset;
} }
@ -521,7 +568,7 @@ u8 audio_read(u16 address) {
} }
if(address == 0xFF1C) { if(address == 0xFF1C) {
return ((ctx.ch3_initial_volume & 0b11) << 5) | 0b10011111; return ((ctx.ch3_volume & 0b11) << 5) | 0b10011111;
} }
if(address == 0xFF1D) { if(address == 0xFF1D) {
@ -783,9 +830,10 @@ void audio_write(u16 address, u8 value){
} }
if(address == 0xFF1A) { if(address == 0xFF1A) {
ctx.ch3_dac = value & 0x80; ctx.ch3_dac = value >> 7;
if(!ctx.ch3_dac){ if(!ctx.ch3_dac){
ctx.ch3_enable = false; ctx.ch3_enable = false;
//printf("ch3 off\n");
} }
} }
@ -796,7 +844,8 @@ void audio_write(u16 address, u8 value){
} }
if(address == 0xFF1C) { 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) { if(address == 0xFF1D) {
@ -850,7 +899,7 @@ void audio_write(u16 address, u8 value){
ctx.ch4_lfsr_width = (value & 0x08); ctx.ch4_lfsr_width = (value & 0x08);
ctx.ch4_clock_divider = value & 0b111; ctx.ch4_clock_divider = value & 0b111;
float div = (ctx.ch4_clock_divider == 0 ? 0.5f : ctx.ch4_clock_divider); 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); lfsr_clock = 1.0f / (lfsr_rate/1000.0f/1000.0f);
} }

View File

@ -16,6 +16,7 @@ void ppu_init() {
ctx.current_frame = 0; ctx.current_frame = 0;
ctx.line_ticks = 0; ctx.line_ticks = 0;
ctx.video_buffer = malloc(YRES * XRES * sizeof(u32)); ctx.video_buffer = malloc(YRES * XRES * sizeof(u32));
ctx.target_frame_time = 1000/60;
ctx.pfc.line_x = 0; ctx.pfc.line_x = 0;
ctx.pfc.pushed_x = 0; ctx.pfc.pushed_x = 0;

View File

@ -131,7 +131,6 @@ void ppu_mode_vblank() {
} }
} }
static u32 target_frame_time = 1000/60;
static long prev_frame_time = 0; static long prev_frame_time = 0;
static long start_timer = 0; static long start_timer = 0;
static long frame_count = 0; static long frame_count = 0;
@ -155,8 +154,8 @@ void ppu_mode_hblank() {
u32 end = get_ticks(); u32 end = get_ticks();
u32 frame_time = end - prev_frame_time; u32 frame_time = end - prev_frame_time;
if(frame_time < target_frame_time) { if(frame_time < ppu_get_context()->target_frame_time) {
delay((target_frame_time - frame_time)); delay((ppu_get_context()->target_frame_time - frame_time));
} }
if (end - start_timer >= 1000) { if (end - start_timer >= 1000) {

View File

@ -3,6 +3,7 @@
#include <ppu.h> #include <ppu.h>
#include <gamepad.h> #include <gamepad.h>
#include <bus.h> #include <bus.h>
#include <audio.h>
#include <SDL.h> #include <SDL.h>
#include <SDL_ttf.h> #include <SDL_ttf.h>
@ -127,10 +128,81 @@ void ui_update() {
SDL_FillRect(screen, &rc, video_buffer[x + (line_num * XRES)]); SDL_FillRect(screen, &rc, video_buffer[x + (line_num * XRES)]);
} }
} }
SDL_UpdateTexture(sdlTexture, NULL, screen->pixels, screen->pitch); SDL_UpdateTexture(sdlTexture, NULL, screen->pixels, screen->pitch);
SDL_RenderClear(sdlRenderer); SDL_RenderClear(sdlRenderer);
SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL); 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); SDL_RenderPresent(sdlRenderer);
update_debug_window(); update_debug_window();
@ -142,11 +214,11 @@ void ui_on_key(bool down, u32 key_code) {
if(key_code == SDLK_SPACE && down == true) { if(key_code == SDLK_SPACE && down == true) {
ff = !ff; ff = !ff;
if(ff){ if(ff){
target_frame_time = 1000/300; ppu_get_context()->target_frame_time = 1000/300;
} else { } 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){ switch(key_code){
case SDLK_z: gamepad_get_state()->b = down; break; case SDLK_z: gamepad_get_state()->b = down; break;