diff --git a/include/audio.h b/include/audio.h index 5f35b33..498690e 100644 --- a/include/audio.h +++ b/include/audio.h @@ -115,18 +115,25 @@ typedef struct { u32 left_index; float right_history[384]; - u32 right_index; + u32 right_index; - u8 sq1_sample_val; - u64 sq1_write_index; - u64 sq1_read_index; - u32 buffer_cnt; - float *sq1_audio_buffer; + u64 buffer_write_index; + float *left_audio_buffer; + float *right_audio_buffer; - u8 sq2_sample_val; - float *sq2_audio_buffer; - float *ch3_audio_buffer; - float *ch4_audio_buffer; + u32 lfsr_timer; + + float left_cap; + float right_cap; + + int history_timer; + float smooth_left; + float smooth_right; + + double cycles_needed; + + u32 period_tick; + u32 ticks; } audio_context; diff --git a/lib/audio.c b/lib/audio.c index c796330..ce6c991 100644 --- a/lib/audio.c +++ b/lib/audio.c @@ -10,37 +10,9 @@ #define SAMPLE_RATE 44100.0 #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 AUDIO_TICKS_PER_SAMPLE 4194304.0 / SAMPLE_RATE #define SAMPLES_PER_AUDIO_TICK SAMPLE_RATE / 4194304.0 -#define TIME_PER_WAVE_TICK 1.0f / (2104718.0f/1000.0f/1000.0f) -#define LFSR_BASE_CLOCK 262144.0f -#define AUDIO_BUFFER_SIZE FRAMES_PER_BUFFER - -static audio_context ctx; - -audio_context *audio_get_context() { - return &ctx; -} - -static float audio_time = 0; -static float wave_time = 0; -static u32 lfsr_timer = 0; -static float lfsr_clock = LFSR_BASE_CLOCK; -static float lfsr_clock_buffer = LFSR_BASE_CLOCK; - -static float left_cap = 0.0f; -static float right_cap = 0.0f; - -static int history_timer = 0; -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; +#define LPF_BETA 0.025f +#define HISTORY_INTERVAL 5 const u8 square_sample_00[8] = { 0x0, @@ -93,203 +65,22 @@ const u8 *square_sample[4] = { square_sample_11 }; -static int audio_callback(const void* input_uffer, void *output_buffer, - unsigned long framesPerBuffer, - const PaStreamCallbackTimeInfo *time_info, - PaStreamCallbackFlags status_flags, - void *userData ) { - float *out = (float *)output_buffer; - //while(ctx.buffer_cnt < framesPerBuffer*AUDIO_TICKS_PER_SAMPLE) { - // delay(1); - //} - for(int i = 0; i < framesPerBuffer; i++) { - //samples_occured = samples_occured + AUDIO_TICKS_PER_SAMPLE-1; - //while(ctx.buffer_cnt == 0) { - // delay(1); - //} - if(ctx.buffer_cnt == 0) { - //samples_occured = samples_occured - ((int)samples_occured - ctx.buffer_cnt); - //ctx.sq1_read_index = ctx.sq1_write_index-1; - //printf("buffer underflow\n"); - continue; - //ctx.buffer_cnt++; - //ctx.sq1_read_index--; - //ctx.sq1_read_index %= AUDIO_BUFFER_SIZE; - } else { - //ctx.buffer_cnt -= (u8)samples_occured; - //ctx.sq1_read_index += (u8)samples_occured; - //ctx.sq1_read_index %= AUDIO_BUFFER_SIZE; - //samples_occured -= (u8)samples_occured; - ctx.buffer_cnt--; - } - if(ppu_get_context()->paused) { - *out++ = 0; - *out++ = 0; - continue; - } - float left = 0; - float right = 0; - float sq1_val = 0; - float sq2_val = 0; - float ch3_val = 0; - float ch4_val = 0; - if(ctx.audio_enabled){ - if(ctx.ch1_dac){ - sq1_val = -1; - 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); - sq1_val = ctx.sq1_audio_buffer[ctx.sq1_read_index]; - if(ctx.ch1_left) { - left += sq1_val; - }else { - left -= 1; - } - if(ctx.ch1_right) { - right += sq1_val; - }else { - right -= 1; - } - } else { - left -= 1; - right -= 1; - } - } - if(ctx.ch2_dac){ - sq2_val = -1; - 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); - sq2_val = ctx.sq2_audio_buffer[ctx.sq1_read_index];//((float)ctx.sq2_volume/15.0f) * (((float)(ctx.sq2_audio_buffer[ctx.sq1_read_index]) - 7.5f)/7.5f); - if(ctx.ch2_left) { - left += sq2_val; - }else { - left -= 1; - } - if(ctx.ch2_right) { - right += sq2_val; - }else { - right -= 1; - } - } else { - left -= 1; - right -= 1; - } - } - if(ctx.ch3_dac){ - ch3_val = -1; - if(ctx.ch3_enable) { - //ch3_val = (((float)(ctx.ch3_last_sample >> shift) - 7.5f)/7.5f); - ch3_val = ctx.ch3_audio_buffer[ctx.sq1_index];//(((float)(ctx.ch3_audio_buffer[ctx.sq1_read_index]) - 7.5f)/7.5f); - if(ctx.ch3_left) { - 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){ - ch4_val = -1; - if(ctx.ch4_enable) { - //ch4_val = ((ctx.ch4_lfsr & 0b1) == 0b1) ? (((float)ctx.ch4_volume - 7.5f)/7.5f) : -1.0f; - ch4_val = ctx.ch4_audio_buffer[ctx.sq1_read_index];//((float)ctx.ch4_volume/15.0f) * (((float)(ctx.ch4_audio_buffer[ctx.sq1_read_index]) - 7.5f)/7.5f); - if(ctx.ch4_left) { - left += ch4_val; - //printf("left: %d\n", ctx.ch3_volume); - }else { - left -= 1; - } - if(ctx.ch4_right) { - right += ch4_val; - } else { - right -= 1; - } - } else { - left -= 1; - right -= 1; - } - } - } - 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; - right *= (float)right_vol/7.0f; - - left = ctx.sq1_audio_buffer[ctx.sq1_read_index]; - right = ctx.sq2_audio_buffer[ctx.sq1_read_index]; - ctx.sq1_read_index++; - ctx.sq1_read_index %= AUDIO_BUFFER_SIZE; - - - 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.999958f; - right_cap = right - right_out * 0.999958f; - } - - - left_out /= 4; - right_out /= 4; - - if(left_out < -1.0f) { - left_out = -1; - } - if(left_out > 1.0f) { - left_out = 1; - } - if(right_out < -1.0f) { - right_out = -1; - } - if(right_out > 1.0f) { - right_out = 1; - } - history_timer++; - if(history_timer >= 5){ - history_timer = 0; - ctx.left_history[ctx.left_index++] = left_out; - if (ctx.left_index == 384) { - ctx.left_index = 0; - } - - ctx.right_history[ctx.right_index++] = right_out; - if (ctx.right_index == 384) { - ctx.right_index = 0; - } - } - - *out++ = left_out; - *out++ = right_out; - } - return paContinue; -} +static audio_context ctx; static PaStream *stream; -void audio_sample(); +audio_context *audio_get_context() { + return &ctx; +} void audio_init(){ PaStreamParameters output_parameters; PaError err; - ctx.sq1_audio_buffer = malloc(sizeof(float) * AUDIO_BUFFER_SIZE); - ctx.sq2_audio_buffer = malloc(sizeof(float) * AUDIO_BUFFER_SIZE); - ctx.ch3_audio_buffer = malloc(sizeof(float) * AUDIO_BUFFER_SIZE); - ctx.ch4_audio_buffer = malloc(sizeof(float) * AUDIO_BUFFER_SIZE); + ctx.left_audio_buffer = malloc(sizeof(float) * FRAMES_PER_BUFFER); + ctx.right_audio_buffer = malloc(sizeof(float) * FRAMES_PER_BUFFER); - memset(ctx.sq1_audio_buffer, 0, sizeof(float) * AUDIO_BUFFER_SIZE); - memset(ctx.sq2_audio_buffer, 0, sizeof(float) * AUDIO_BUFFER_SIZE); - memset(ctx.ch3_audio_buffer, 0, sizeof(float) * AUDIO_BUFFER_SIZE); - memset(ctx.ch4_audio_buffer, 0, sizeof(float) * AUDIO_BUFFER_SIZE); + memset(ctx.left_audio_buffer, 0, sizeof(float) * FRAMES_PER_BUFFER); + memset(ctx.right_audio_buffer, 0, sizeof(float) * FRAMES_PER_BUFFER); ctx.audio_enabled = false; ctx.ch1_left = false; @@ -334,9 +125,6 @@ void audio_init(){ ctx.sq2_env_direction = false; ctx.sq2_env_pace = 0x0; ctx.sq2_env_timer = 0x0; - ctx.sq1_write_index = AUDIO_BUFFER_SIZE - 1; - ctx.sq1_read_index = 0; - ctx.buffer_cnt = AUDIO_BUFFER_SIZE - 1; err = Pa_Initialize(); if (err != paNoError) goto error; @@ -349,7 +137,6 @@ void audio_init(){ output_parameters.sampleFormat = paFloat32 | paNonInterleaved; 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, @@ -377,18 +164,12 @@ void sq1_sweep() { if(ctx.sq1_calc_period > 0x7FF) { ctx.sq1_enable = false; } -} - -static int change = 1; -static u32 ticks = 0; -static u32 start = 0; -static u32 period_tick; -static u32 samples_per_sec = 0; +} void lfsr_tick() { - lfsr_timer--; - if(lfsr_timer == 0) { - lfsr_timer = (ctx.ch4_clock_divider > 0 ? (ctx.ch4_clock_divider << 4) : 8) << ctx.ch4_clock_shift; + ctx.lfsr_timer--; + if(ctx.lfsr_timer == 0) { + ctx.lfsr_timer = (ctx.ch4_clock_divider > 0 ? (ctx.ch4_clock_divider << 4) : 8) << ctx.ch4_clock_shift; unsigned bit_mask = ctx.ch4_lfsr_width ? 0x4040 : 0x4000; bool new_bit = (ctx.ch4_lfsr ^ (ctx.ch4_lfsr >> 1) ^ 1) & 1; ctx.ch4_lfsr >>= 1; @@ -400,8 +181,6 @@ void lfsr_tick() { } } -static double cycles_needed = 0; - void audio_sample_tick() { lfsr_tick(); float left = 0; @@ -413,7 +192,7 @@ void audio_sample_tick() { if(ctx.audio_enabled){ if(ctx.ch1_dac){ sq1_val = -1; - if(ctx.sq1_enable) { + if(ctx.sq1_enable && (ctx.ch1_left || ctx.ch1_right)) { 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 += sq1_val; @@ -432,7 +211,7 @@ void audio_sample_tick() { } if(ctx.ch2_dac){ sq2_val = -1; - if(ctx.sq2_enable) { + if(ctx.sq2_enable && (ctx.ch2_left || ctx.ch2_right)) { 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 += sq2_val; @@ -465,7 +244,6 @@ void audio_sample_tick() { ch3_val = (((float)(ctx.ch3_last_sample >> shift) - 7.5f)/7.5f); if(ctx.ch3_left) { left += ch3_val; - //printf("left: %d\n", ctx.ch3_volume); }else { left -= 1; } @@ -481,11 +259,10 @@ void audio_sample_tick() { } if(ctx.ch4_dac){ ch4_val = -1; - if(ctx.ch4_enable) { + if(ctx.ch4_enable && (ctx.ch4_left || ctx.ch4_right)) { ch4_val = ((ctx.ch4_lfsr & 0b1) == 0b1) ? (((float)ctx.ch4_volume - 7.5f)/7.5f) : -1.0f; if(ctx.ch4_left) { left += ch4_val; - //printf("left: %d\n", ctx.ch3_volume); }else { left -= 1; } @@ -505,10 +282,10 @@ void audio_sample_tick() { u8 right_vol = ctx.volume_right == 0 ? 1 : ctx.volume_right; left *= (float)left_vol/7.0f; right *= (float)right_vol/7.0f; - smooth_left = smooth_left - (LPF_Beta * (smooth_left - left)); - smooth_right = smooth_right - (LPF_Beta * (smooth_right - right)); - cycles_needed += SAMPLES_PER_AUDIO_TICK; - if(cycles_needed > 1){ + ctx.smooth_left = ctx.smooth_left - (LPF_BETA * (ctx.smooth_left - left)); + ctx.smooth_right = ctx.smooth_right - (LPF_BETA * (ctx.smooth_right - right)); + ctx.cycles_needed += SAMPLES_PER_AUDIO_TICK; + if(ctx.cycles_needed > 1){ //printf("tick\n"); //if(ctx.buffer_cnt == AUDIO_BUFFER_SIZE){ //ctx.buffer_cnt--; @@ -518,17 +295,17 @@ void audio_sample_tick() { //printf("overflow\n"); //return; //} - cycles_needed -= 1; + ctx.cycles_needed -= 1; 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 = smooth_left - left_cap; - right_out = smooth_right - right_cap; - left_cap = smooth_left - left_out * 0.996f; - right_cap = smooth_right - right_out * 0.996f; + left_out = ctx.smooth_left - ctx.left_cap; + right_out = ctx.smooth_right - ctx.right_cap; + ctx.left_cap = ctx.smooth_left - left_out * 0.999958; + ctx.right_cap = ctx.smooth_right - right_out * 0.999958; } @@ -548,15 +325,13 @@ void audio_sample_tick() { right_out = 1; } - ctx.sq1_audio_buffer[(ctx.sq1_write_index)] = left_out; - ctx.sq2_audio_buffer[(ctx.sq1_write_index)] = right_out; - //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); - //ctx.buffer_cnt++; - history_timer++; - if(history_timer >= history_interval){ - history_timer = 0; + ctx.left_audio_buffer[(ctx.buffer_write_index)] = left_out; + ctx.right_audio_buffer[(ctx.buffer_write_index)] = right_out; + + ctx.buffer_write_index++; + ctx.history_timer++; + if(ctx.history_timer >= HISTORY_INTERVAL){ + ctx.history_timer = 0; ctx.sq1_history[ctx.sq1_index++] = sq1_val; if (ctx.sq1_index == 384) { ctx.sq1_index = 0; @@ -587,13 +362,13 @@ void audio_sample_tick() { } } - if(ctx.sq1_write_index == FRAMES_PER_BUFFER) { - ctx.sq1_write_index = 0; + if(ctx.buffer_write_index == FRAMES_PER_BUFFER) { + ctx.buffer_write_index = 0; float *data[2]; - data[0] = ctx.sq1_audio_buffer; - data[1] = ctx.sq2_audio_buffer; + data[0] = ctx.left_audio_buffer; + data[1] = ctx.right_audio_buffer; int err = Pa_WriteStream(stream, data, FRAMES_PER_BUFFER); - if(err != paNoError) { + if(err != paNoError && err != paOutputUnderflowed) { fprintf(stderr, "portaudio stream error\n\tError Number: %d\n\tError Message: %s\n", err, Pa_GetErrorText(err)); } } @@ -602,15 +377,8 @@ void audio_sample_tick() { bool skipped = false; void audio_period_tick() { - period_tick++; - u32 end = get_ticks(); - samples_per_sec++; - if (end - start >= 1000) { - printf("Samples In Buffer: %ld\n", ctx.buffer_cnt); - start = end; - samples_per_sec = 0; - } - if(period_tick % 2 == 0){ + ctx.period_tick++; + if(ctx.period_tick % 2 == 0){ ctx.sq1_period_timer++; ctx.sq2_period_timer++; @@ -636,10 +404,10 @@ void audio_period_tick() { } } -void audio_tick(){ - u32 prev_ticks = ticks; - ticks++; - if((ticks % 2) == 0) { +void audio_tick(){ + u32 prev_ticks = ctx.ticks; + ctx.ticks++; + if((ctx.ticks % 2) == 0) { if(ctx.sq1_len_enable) { ctx.sq1_len++; if(ctx.sq1_len >= 64) { @@ -668,7 +436,6 @@ void audio_tick(){ if(ctx.ch4_len_enable && ctx.ch4_enable) { ctx.ch4_len++; - //printf("Ch4 len: %02X\n", ctx.ch4_len); if(ctx.ch4_len >= 64) { ctx.ch4_len = 0; ctx.ch4_enable = false; @@ -677,7 +444,7 @@ void audio_tick(){ } } - if((prev_ticks & (1 << 2)) && !(ticks & (1 << 2))) { + if((prev_ticks & (1 << 2)) && !(ctx.ticks & (1 << 2))) { if(ctx.sq1_env_pace != 0){ ctx.sq1_env_timer++; if(ctx.sq1_env_timer >= ctx.sq1_env_pace) { @@ -703,30 +470,26 @@ void audio_tick(){ if(ctx.sq2_volume > 15) ctx.sq2_volume = 15; } - //printf("sq2 vol: %01X\n", ctx.sq2_volume); } } - //if(ctx.ch4_env_pace != 0){ ctx.ch4_env_timer--; if(ctx.ch4_env_timer <= 0) { ctx.ch4_env_timer = ctx.ch4_env_pace_buffer != 0 ? ctx.ch4_env_pace_buffer : 8; if(ctx.ch4_env_pace != 0 && ((ctx.ch4_env_direction && ctx.ch4_volume != 15) || (!ctx.ch4_env_direction && ctx.ch4_volume != 0))){ ctx.ch4_volume += ctx.ch4_env_direction ? 1 : -1; - //printf("Noise Vol: %02X\n", ctx.ch4_volume); if(ctx.ch4_volume < 0) ctx.ch4_volume = 0; if(ctx.ch4_volume > 15) ctx.ch4_volume = 15; } } - //} } - if((prev_ticks & (1 << 1)) && !(ticks & (1 << 1)) && ctx.sq1_sweep_enabled) { + if((prev_ticks & (1 << 1)) && !(ctx.ticks & (1 << 1)) && ctx.sq1_sweep_enabled) { ctx.sq1_sweep_timer--; - if(ctx.sq1_sweep_timer <= 0){//(ctx.sq1_sweep_timer >= ctx.sq1_sweep_pace && ctx.sq1_sweep_pace > 0) || (ctx.sq1_sweep_timer >= 8 && ctx.sq1_sweep_pace == 0)) { + if(ctx.sq1_sweep_timer <= 0){ ctx.sq1_sweep_timer = ctx.sq1_sweep_pace != 0 ? ctx.sq1_sweep_pace : 8; if(ctx.sq1_enable && ctx.sq1_sweep_pace > 0 && ctx.sq1_sweep_enabled){ sq1_sweep(); @@ -774,7 +537,6 @@ void enable_wave() { } ctx.ch3_enable = true; ctx.ch3_sample = 0; - //ctx.ch3_volume = ctx.ch3_initial_volume; ctx.ch3_period_timer = ctx.ch3_period_reset; } @@ -785,8 +547,7 @@ void enable_noise() { ctx.ch4_env_pace = ctx.ch4_env_pace_buffer; ctx.ch4_volume = ctx.ch4_initial_volume; ctx.ch4_lfsr = 0; - lfsr_clock = lfsr_clock_buffer; - lfsr_timer = (ctx.ch4_clock_divider > 0 ? (ctx.ch4_clock_divider << 4) : 8) << ctx.ch4_clock_shift; + ctx.lfsr_timer = (ctx.ch4_clock_divider > 0 ? (ctx.ch4_clock_divider << 4) : 8) << ctx.ch4_clock_shift; if(!ctx.ch4_dac){ ctx.ch4_enable = false; } @@ -999,7 +760,6 @@ void audio_write(u16 address, u8 value){ if(address == 0xFF26) { ctx.audio_enabled = (value & 0x80) == 0x80; if(!ctx.audio_enabled) { - //printf("audio disabled\n"); reset_buffers(); } } @@ -1018,14 +778,11 @@ void audio_write(u16 address, u8 value){ ctx.ch3_right = value & 0b00000100; ctx.ch2_right = value & 0b00000010; ctx.ch1_right = value & 0b00000001; - //printf("write at %02X; Value: %02X\n", address, value); } if(address == 0xFF24) { ctx.volume_left = (value >> 4) & 0b111; - //if(ctx.volume_left == 0) ctx.volume_left = 1; ctx.volume_right = value & 0b111; - //if(ctx.volume_right == 0) ctx.volume_right = 1; ctx.vin_left = value & 0x80; ctx.vin_right = value & 0x8; } @@ -1041,7 +798,6 @@ void audio_write(u16 address, u8 value){ ctx.sq1_initial_len = value & 0x3F; ctx.sq1_len = ctx.sq1_initial_len; ctx.ch1_len_fz = false; - //printf("initial len: %02X\n", ctx.sq1_initial_len); } if(address == 0xFF12) { ctx.sq1_initial_volume = (value >> 4) & 0x0F; @@ -1063,7 +819,7 @@ void audio_write(u16 address, u8 value){ } ctx.sq1_period_reset = (ctx.sq1_period_reset & 0x0FF) | ((value & 0b111) << 8); bool len_en = (value & 0x40) == 0x40; - if((ticks % 2) == 0 && !ctx.ch1_len_fz && !ctx.sq1_len_enable && len_en){ + if((ctx.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; @@ -1071,7 +827,7 @@ void audio_write(u16 address, u8 value){ ctx.ch1_len_fz = true; } } - if((ticks % 2) == 0 && ctx.ch1_len_fz && (value & 0x80) == 0x80){ + if((ctx.ticks % 2) == 0 && ctx.ch1_len_fz && (value & 0x80) == 0x80){ ctx.ch1_len_fz = false; if(len_en){ ctx.sq1_len++; @@ -1104,7 +860,6 @@ void audio_write(u16 address, u8 value){ if(address == 0xFF18) { u16 prev_period = ctx.sq2_period_reset; ctx.sq2_period_reset = (ctx.sq2_period_reset & 0xF00) | value; - //printf("period: %03X, old_period: %03X\n", ctx.sq2_period_reset, prev_period); } if(address == 0xFF19) { if(ctx.sq2_len == 64) { @@ -1112,7 +867,7 @@ void audio_write(u16 address, u8 value){ } ctx.sq2_period_reset = (ctx.sq2_period_reset & 0x0FF) | ((value & 0b111) << 8); bool len_en = (value & 0x40) == 0x40; - if((ticks % 2) == 0 && !ctx.ch2_len_fz && !ctx.sq2_len_enable && len_en){ + if((ctx.ticks % 2) == 0 && !ctx.ch2_len_fz && !ctx.sq2_len_enable && len_en){ ctx.sq2_len++; if(ctx.sq2_len >= 64) { ctx.sq2_len = 0; @@ -1120,7 +875,7 @@ void audio_write(u16 address, u8 value){ ctx.ch2_len_fz = true; } } - if((ticks % 2) == 0 && ctx.ch2_len_fz && (value & 0x80) == 0x80){ + if((ctx.ticks % 2) == 0 && ctx.ch2_len_fz && (value & 0x80) == 0x80){ ctx.ch2_len_fz = false; if(len_en){ ctx.sq2_len++; @@ -1136,7 +891,6 @@ void audio_write(u16 address, u8 value){ ctx.ch3_dac = value >> 7; if(!ctx.ch3_dac){ ctx.ch3_enable = false; - //printf("ch3 off\n"); } } @@ -1148,7 +902,6 @@ void audio_write(u16 address, u8 value){ if(address == 0xFF1C) { ctx.ch3_volume = (value >> 5) & 0b11; - //printf("Ch3 vol: %d\n", ctx.ch3_volume); } if(address == 0xFF1D) { @@ -1158,7 +911,7 @@ void audio_write(u16 address, u8 value){ if(address == 0xFF1E) { ctx.ch3_period_reset = (ctx.ch3_period_reset & 0x0FF) | ((value & 0b111) << 8); bool len_en = (value & 0x40) == 0x40; - if((ticks % 2) == 0 && !ctx.ch3_len_fz && !ctx.ch3_len_enable && len_en){ + if((ctx.ticks % 2) == 0 && !ctx.ch3_len_fz && !ctx.ch3_len_enable && len_en){ if(ctx.ch3_len >= 255) { ctx.ch3_len = 0; ctx.ch3_enable = false; @@ -1167,7 +920,7 @@ void audio_write(u16 address, u8 value){ ctx.ch3_len++; } } - if((ticks % 2) == 0 && ctx.ch3_len_fz && (value & 0x80) == 0x80){ + if((ctx.ticks % 2) == 0 && ctx.ch3_len_fz && (value & 0x80) == 0x80){ ctx.ch3_len_fz = false; if(len_en){ ctx.ch3_len++; @@ -1183,8 +936,6 @@ void audio_write(u16 address, u8 value){ ctx.ch4_initial_len = value & 0b111111; ctx.ch4_len = ctx.ch4_initial_len; ctx.ch4_len_fz = false; - //printf("ch4 len: %02X\n", ctx.ch4_len); - //printf("Write Ch4 Len: %02X\n", value); } if(address == 0xFF21) { @@ -1197,18 +948,12 @@ void audio_write(u16 address, u8 value){ } else { ctx.ch4_dac = true; } - //printf("Write Ch4 env: %02X\n", value); } if(address == 0xFF22) { ctx.ch4_clock_shift = (value >> 4) & 0xF; ctx.ch4_lfsr_width = (value & 0x08); ctx.ch4_clock_divider = value & 0b111; - //float div = (ctx.ch4_clock_divider == 0 ? 0.5f : (float)ctx.ch4_clock_divider); - //float lfsr_rate = (LFSR_BASE_CLOCK) / (div * (float)(1 << ctx.ch4_clock_shift)); - //lfsr_clock_buffer = 1.0f / (lfsr_rate/1000.0f/1000.0f); - //printf("Ch4 clock: Shift: %01X, Width: %d, Div: %f (%01X), Rate: %f, Clock %f\n", ctx.ch4_clock_shift, ctx.ch4_lfsr_width, div, ctx.ch4_clock_divider, lfsr_rate, lfsr_clock); - //printf("Write Ch4 Clock: %02X\n", value); } if(address == 0xFF23) { @@ -1216,7 +961,7 @@ void audio_write(u16 address, u8 value){ ctx.ch4_len = 0; } bool len_en = (value & 0x40) == 0x40; - if((ticks % 2) == 0 && !ctx.ch4_len_fz && !ctx.ch4_len_enable && len_en){ + if((ctx.ticks % 2) == 0 && !ctx.ch4_len_fz && !ctx.ch4_len_enable && len_en){ ctx.ch4_len++; if(ctx.ch4_len >= 64) { ctx.ch4_len = 0; @@ -1224,17 +969,15 @@ void audio_write(u16 address, u8 value){ ctx.ch4_len_fz = true; } } - if((ticks % 2) == 0 && ctx.ch4_len_fz && (value & 0x80) == 0x80){ + if((ctx.ticks % 2) == 0 && ctx.ch4_len_fz && (value & 0x80) == 0x80){ ctx.ch4_len_fz = false; if(len_en){ ctx.ch4_len++; } } ctx.ch4_len_enable = len_en; - //printf("ch4 trigger: LenEn %d\n", len_en); if(value & 0x80) { enable_noise(); } - //printf("Write Ch4 Trigger: %02X\n", value); } }