diff --git a/lib/audio.c b/lib/audio.c index faa9cb4..f6e94eb 100644 --- a/lib/audio.c +++ b/lib/audio.c @@ -15,7 +15,7 @@ #define AUDIO_TICKS_PER_SAMPLE 2157281.28 / SAMPLE_RATE #define TIME_PER_WAVE_TICK 1.0f / (2104718.0f/1000.0f/1000.0f) #define LFSR_BASE_CLOCK 262144.0f -#define AUDIO_BUFFER_SIZE 10240 +#define AUDIO_BUFFER_SIZE (int)(SAMPLE_RATE / 10) static audio_context ctx; @@ -37,6 +37,10 @@ const int history_interval = 5; static int lfsr_clocks = 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] = { 0x0, 0xF, @@ -100,7 +104,7 @@ static int audio_callback(const void* input_uffer, void *output_buffer, for(int i = 0; i < framesPerBuffer; i++) { //samples_occured = samples_occured + AUDIO_TICKS_PER_SAMPLE-1; while(ctx.buffer_cnt == 0) { - //delay(1); + delay(1); } if(ctx.buffer_cnt == 0) { //samples_occured = samples_occured - ((int)samples_occured - ctx.buffer_cnt); @@ -344,6 +348,7 @@ void audio_init(){ output_parameters.sampleFormat = paFloat32; output_parameters.suggestedLatency = Pa_GetDeviceInfo(output_parameters.device)->defaultLowOutputLatency; output_parameters.hostApiSpecificStreamInfo = NULL; + printf("default sample rate: %lf\n", Pa_GetDeviceInfo(output_parameters.device)->defaultSampleRate); err = Pa_OpenStream(&stream, NULL, @@ -552,15 +557,21 @@ void audio_period_tick() { ctx.ch4_index = 0; } } + + 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){ //printf("tick\n"); 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"); } - ctx.sq1_audio_buffer[(ctx.sq1_write_index)] = left; - ctx.sq2_audio_buffer[(ctx.sq1_write_index)] = right; + ctx.sq1_audio_buffer[(ctx.sq1_write_index)] = smooth_left; + 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.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;