fixed high pass filter

This commit is contained in:
Samuel Walker 2025-02-14 18:18:06 -07:00
parent 96c0196306
commit 844e74777f
Signed by: piwalker
GPG Key ID: 616B1928705EA4C9

View File

@ -23,8 +23,8 @@ 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;
static float left_cap = 0.0; static float left_cap = 0.0f;
static float right_cap = 0.0; static float right_cap = 0.0f;
const u8 square_sample_00[8] = { const u8 square_sample_00[8] = {
0x0, 0x0,
@ -83,9 +83,9 @@ static int audio_callback(const void* input_uffer, void *output_buffer,
PaStreamCallbackFlags status_flags, PaStreamCallbackFlags status_flags,
void *userData ) { void *userData ) {
float *out = (float *)output_buffer; float *out = (float *)output_buffer;
float left = 0;
float right = 0;
for(int i = 0; i < framesPerBuffer; i++) { for(int i = 0; i < framesPerBuffer; i++) {
float left = 0;
float right = 0;
audio_time += TIME_PER_SAMPLE; audio_time += TIME_PER_SAMPLE;
wave_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) {
@ -132,52 +132,92 @@ static int audio_callback(const void* input_uffer, void *output_buffer,
float ch3_val = 0; float ch3_val = 0;
float ch4_val = 0; float ch4_val = 0;
if(ctx.audio_enabled){ if(ctx.audio_enabled){
if(ctx.sq1_enable) { if(ctx.ch1_dac){
sq1_val = ((float)ctx.sq1_volume/15.0f) * (((float)(square_sample[ctx.sq1_duty][ctx.sq1_sample]) - 7.5f)/7.5f); sq1_val = -1;
if(ctx.ch1_left) { if(ctx.sq1_enable) {
left += sq1_val; 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_right) { left += sq1_val;
right += sq1_val; }else {
left -= 1;
}
if(ctx.ch1_right) {
right += sq1_val;
}else {
right -= 1;
}
} else {
left -= 1;
right -= 1;
} }
} }
if(ctx.ch2_dac){
if(ctx.sq2_enable) { sq2_val = -1;
sq2_val = ((float)ctx.sq2_volume/15.0f) * (((float)(square_sample[ctx.sq2_duty][ctx.sq2_sample]) - 7.5f)/7.5f); if(ctx.sq2_enable) {
if(ctx.ch2_left) { sq2_val = ((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_left) {
} left += sq2_val;
if(ctx.ch2_right) { }else {
right += sq2_val; left -= 1;
}
if(ctx.ch2_right) {
right += sq2_val;
}else {
right -= 1;
}
} else {
left -= 1;
right -= 1;
} }
} }
if(ctx.ch3_dac){
if(ctx.ch3_enable && ctx.ch3_volume != 0x0) { ch3_val = -1;
u8 shift = 0; if(ctx.ch3_enable) {
if(ctx.ch3_volume == 0b10) { u8 shift = 0;
shift = 1; if(ctx.ch3_volume == 0b10) {
} shift = 1;
if(ctx.ch3_volume == 0b11) { }
shift = 2; if(ctx.ch3_volume == 0b00) {
} shift = 4;
ch3_val = (((float)(ctx.ch3_last_sample >> shift) - 7.5f)/15.0f); }
if(ctx.ch3_left) { if(ctx.ch3_volume == 0b11) {
left += ch3_val; shift = 2;
//printf("left: %d\n", ctx.ch3_volume); }
} ch3_val = (((float)(ctx.ch3_last_sample >> shift) - 7.5f)/15.0f);
if(ctx.ch3_right) { if(ctx.ch3_left) {
right += ch3_val; left += ch3_val;
//printf("left: %d\n", ctx.ch3_volume);
}else {
left -= 1;
}
if(ctx.ch3_right) {
right += ch3_val;
}else {
right -= 1;
}
} else {
left -= 1;
right -= 1;
} }
} }
if(ctx.ch4_dac){
if(ctx.ch4_enable) { ch4_val = -1;
ch4_val = ((float)ctx.ch4_volume/15.0f) * ((ctx.ch4_lfsr & 0b1) ? 0 : 1.0f); if(ctx.ch4_enable) {
if(ctx.ch4_left) { ch4_val = (ctx.ch4_lfsr & 0b1) ? (((float)ctx.ch4_volume - 7.5f)/7.5f) : -1.0f;
left += ch4_val; if(ctx.ch4_left) {
//printf("left: %d\n", ctx.ch3_volume); left += ch4_val;
} //printf("left: %d\n", ctx.ch3_volume);
if(ctx.ch4_right) { }else {
right += ch4_val; left -= 1;
}
if(ctx.ch4_right) {
right += ch4_val;
} else {
right -= 1;
}
} else {
left -= 1;
right -= 1;
} }
} }
} }
@ -205,12 +245,9 @@ static int audio_callback(const void* input_uffer, void *output_buffer,
left *= (float)left_vol/7.0f; left *= (float)left_vol/7.0f;
right *= (float)right_vol/7.0f; right *= (float)right_vol/7.0f;
left /= 4; //left /= 4;
right /= 4; //right /= 4;
if(left > 1.0f) {
printf("Uh Oh! %02X\n", ctx.volume_left);
}
bool dacs = ctx.ch1_dac || ctx.ch2_dac || ctx.ch3_dac || ctx.ch4_dac; bool dacs = ctx.ch1_dac || ctx.ch2_dac || ctx.ch3_dac || ctx.ch4_dac;
float left_out = 0; float left_out = 0;
@ -222,6 +259,13 @@ 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;
} }
left_out /= 4;
right_out /= 4;
if(left_out < -1.0f) {
printf("Uh Oh! %f\n", left_out);
}
ctx.left_history[ctx.left_index++] = left_out; ctx.left_history[ctx.left_index++] = left_out;
if (ctx.left_index == 4410) { if (ctx.left_index == 4410) {
ctx.left_index = 0; ctx.left_index = 0;