Fixing audio glitches with wave and noise channels

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

View File

@ -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);
}