diff --git a/include/audio.h b/include/audio.h index 8f8b659..2625ada 100644 --- a/include/audio.h +++ b/include/audio.h @@ -19,6 +19,8 @@ typedef struct { bool ch2_dac; bool ch4_dac; + bool ch1_len_fz; + u8 volume_left; u8 volume_right; bool vin_left; diff --git a/lib/audio.c b/lib/audio.c index b234813..75d0198 100644 --- a/lib/audio.c +++ b/lib/audio.c @@ -6,7 +6,7 @@ #include #include -#define SAMPLE_RATE 192000 +#define SAMPLE_RATE 44100 #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) @@ -18,6 +18,9 @@ static float audio_time = 0; static float lfsr_timer = 0; static float lfsr_clock = LFSR_BASE_CLOCK; +static float left_cap = 0.0; +static float right_cap = 0.0; + const u8 square_sample_00[8] = { 0x0, 0xF, @@ -172,8 +175,19 @@ static int audio_callback(const void* input_uffer, void *output_buffer, if(left > 1.0f) { printf("Uh Oh! %02X\n", ctx.volume_left); } - *out++ = left; - *out++ = right; + + bool dacs = ctx.ch1_dac || ctx.ch2_dac || ctx.ch3_dac || ctx.ch4_dac; + float left_out = 0; + float right_out = 0; + if(dacs) { + left_out = left - left_cap; + right_out = right - right_cap; + left_cap = left - left_out * 0.996; + right_cap = right - right_out * 0.996; + } + + *out++ = left_out; + *out++ = right_out; } return paContinue; } @@ -281,6 +295,7 @@ void audio_tick(){ if(ctx.sq1_len >= 64) { ctx.sq1_len = 0; ctx.sq1_enable = false; + ctx.ch1_len_fz = true; } } @@ -695,19 +710,26 @@ void audio_write(u16 address, u8 value){ ctx.sq1_period_reset = (ctx.sq1_period_reset & 0xF00) | value; } if(address == 0xFF14) { - static bool unfreeze; + if(ctx.sq1_len == 64) { + ctx.sq1_len = 0; + } ctx.sq1_period_reset = (ctx.sq1_period_reset & 0x0FF) | ((value & 0b111) << 8); bool len_en = (value & 0x40) == 0x40; - if((ticks % 2) == 0 && ctx.sq1_len != 0 && !ctx.sq1_len_enable && len_en){ + if((ticks % 2) == 0 && !ctx.ch1_len_fz && !ctx.sq1_len_enable && len_en){ ctx.sq1_len++; if(ctx.sq1_len >= 64) { ctx.sq1_len = 0; ctx.sq1_enable = false; + ctx.ch1_len_fz = true; + } + } + if((ticks % 2) == 0 && ctx.ch1_len_fz && (value & 0x80) == 0x80){ + ctx.ch1_len_fz = false; + if(len_en){ + ctx.sq1_len++; } - } else if(ctx.sq1_len == 0 && (value & 0x80) == 0x80){ - printf("Value: %02X\n", value); - ctx.sq1_len = 1; } + printf("Value: %02X\n", value); ctx.sq1_len_enable = len_en; if(value & 0x80) { enable_square1();