audio buffer still pops

This commit is contained in:
Samuel Walker 2025-02-18 15:25:40 -07:00
parent 2af25cc87c
commit 8e5c3234ef

View File

@ -15,7 +15,7 @@
#define AUDIO_TICKS_PER_SAMPLE 2157281.28 / SAMPLE_RATE #define AUDIO_TICKS_PER_SAMPLE 2157281.28 / SAMPLE_RATE
#define TIME_PER_WAVE_TICK 1.0f / (2104718.0f/1000.0f/1000.0f) #define TIME_PER_WAVE_TICK 1.0f / (2104718.0f/1000.0f/1000.0f)
#define LFSR_BASE_CLOCK 262144.0f #define LFSR_BASE_CLOCK 262144.0f
#define AUDIO_BUFFER_SIZE 10240 #define AUDIO_BUFFER_SIZE (int)(SAMPLE_RATE / 10)
static audio_context ctx; static audio_context ctx;
@ -37,6 +37,10 @@ const int history_interval = 5;
static int lfsr_clocks = 0; static int lfsr_clocks = 0;
static double samples_occured = 0; static double samples_occured = 0;
float LPF_Beta = 0.025;
float smooth_left = 0;
float smooth_right = 0;
const u8 square_sample_00[8] = { const u8 square_sample_00[8] = {
0x0, 0x0,
0xF, 0xF,
@ -100,7 +104,7 @@ static int audio_callback(const void* input_uffer, void *output_buffer,
for(int i = 0; i < framesPerBuffer; i++) { for(int i = 0; i < framesPerBuffer; i++) {
//samples_occured = samples_occured + AUDIO_TICKS_PER_SAMPLE-1; //samples_occured = samples_occured + AUDIO_TICKS_PER_SAMPLE-1;
while(ctx.buffer_cnt == 0) { while(ctx.buffer_cnt == 0) {
//delay(1); delay(1);
} }
if(ctx.buffer_cnt == 0) { if(ctx.buffer_cnt == 0) {
//samples_occured = samples_occured - ((int)samples_occured - ctx.buffer_cnt); //samples_occured = samples_occured - ((int)samples_occured - ctx.buffer_cnt);
@ -344,6 +348,7 @@ void audio_init(){
output_parameters.sampleFormat = paFloat32; output_parameters.sampleFormat = paFloat32;
output_parameters.suggestedLatency = Pa_GetDeviceInfo(output_parameters.device)->defaultLowOutputLatency; output_parameters.suggestedLatency = Pa_GetDeviceInfo(output_parameters.device)->defaultLowOutputLatency;
output_parameters.hostApiSpecificStreamInfo = NULL; output_parameters.hostApiSpecificStreamInfo = NULL;
printf("default sample rate: %lf\n", Pa_GetDeviceInfo(output_parameters.device)->defaultSampleRate);
err = Pa_OpenStream(&stream, err = Pa_OpenStream(&stream,
NULL, NULL,
@ -553,14 +558,20 @@ void audio_period_tick() {
} }
} }
smooth_left = smooth_left - (LPF_Beta * (smooth_left - left));
smooth_right = smooth_right - (LPF_Beta * (smooth_right - right));
if((period_tick % (int)(AUDIO_TICKS_PER_SAMPLE)) == 0){ if((period_tick % (int)(AUDIO_TICKS_PER_SAMPLE)) == 0){
//printf("tick\n"); //printf("tick\n");
while(ctx.buffer_cnt == AUDIO_BUFFER_SIZE){ while(ctx.buffer_cnt == AUDIO_BUFFER_SIZE){
//delay(1); //ctx.buffer_cnt--;
//ctx.sq1_read_index++;
//ctx.sq1_read_index %= AUDIO_BUFFER_SIZE;
delay(10);
//printf("overflow\n"); //printf("overflow\n");
} }
ctx.sq1_audio_buffer[(ctx.sq1_write_index)] = left; ctx.sq1_audio_buffer[(ctx.sq1_write_index)] = smooth_left;
ctx.sq2_audio_buffer[(ctx.sq1_write_index)] = right; ctx.sq2_audio_buffer[(ctx.sq1_write_index)] = smooth_right;
//ctx.ch3_audio_buffer[(ctx.sq1_write_index)] = (((float)(ctx.ch3_last_sample >> shift)-7.5f)/7.5f); //ctx.ch3_audio_buffer[(ctx.sq1_write_index)] = (((float)(ctx.ch3_last_sample >> shift)-7.5f)/7.5f);
//ctx.ch4_audio_buffer[(ctx.sq1_write_index)] = (ctx.ch4_lfsr & 0b1) ? (float)(ctx.ch4_volume-7.5f)/7.5f : -1.0f; //ctx.ch4_audio_buffer[(ctx.sq1_write_index)] = (ctx.ch4_lfsr & 0b1) ? (float)(ctx.ch4_volume-7.5f)/7.5f : -1.0f;
ctx.sq1_write_index = (ctx.sq1_write_index + 1) % AUDIO_BUFFER_SIZE; ctx.sq1_write_index = (ctx.sq1_write_index + 1) % AUDIO_BUFFER_SIZE;