audio buffer implemented but poppy

This commit is contained in:
Samuel Walker 2025-02-17 23:18:17 -07:00
parent 5668389488
commit ea91ebf5c3
Signed by: piwalker
GPG Key ID: 616B1928705EA4C9
3 changed files with 100 additions and 69 deletions

View File

@ -118,9 +118,14 @@ typedef struct {
u32 right_index;
u8 sq1_sample_val;
u32 sq1_write_index;
u32 sq1_read_index;
u8 sq1_audio_buffer[4096];
u64 sq1_write_index;
u64 sq1_read_index;
u8 *sq1_audio_buffer;
u8 sq2_sample_val;
u8 *sq2_audio_buffer;
u8 *ch3_audio_buffer;
u8 *ch4_audio_buffer;
} audio_context;

View File

@ -7,13 +7,14 @@
#include <pa_ringbuffer.h>
#include <math.h>
#define SAMPLE_RATE 44100
#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 1048576.0f / SAMPLE_RATE
#define AUDIO_TICKS_PER_SAMPLE 2157281.28 / SAMPLE_RATE
#define TIME_PER_WAVE_TICK 1.0f / (2097152.0f/1000.0f/1000.0f)
#define LFSR_BASE_CLOCK 262144.0f
#define AUDIO_BUFFER_SIZE 102400
static audio_context ctx;
@ -33,7 +34,7 @@ static float right_cap = 0.0f;
static int history_timer = 0;
const int history_interval = 5;
static int lfsr_clocks = 0;
static double write_index = 0;
static double samples_occured = 0;
const u8 square_sample_00[8] = {
0x0,
@ -93,11 +94,13 @@ static int audio_callback(const void* input_uffer, void *output_buffer,
void *userData ) {
float *out = (float *)output_buffer;
for(int i = 0; i < framesPerBuffer; i++) {
write_index = write_index + AUDIO_TICKS_PER_SAMPLE;
ctx.sq1_read_index = (int)write_index;
samples_occured = samples_occured + AUDIO_TICKS_PER_SAMPLE;
ctx.sq1_read_index += (int)samples_occured;
samples_occured -= (int)samples_occured;
if(ctx.sq1_read_index > ctx.sq1_write_index) {
ctx.sq1_read_index = ctx.sq1_write_index-1;
write_index = ctx.sq1_read_index;
//printf("buffer underflow\n");
return paContinue;
}
if(ppu_get_context()->paused) {
*out++ = 0;
@ -116,49 +119,24 @@ static int audio_callback(const void* input_uffer, void *output_buffer,
// ctx.sq1_period_timer = ctx.sq1_period_reset;
// ctx.sq1_sample = (ctx.sq1_sample + 1) % 8;
//}
if(ctx.sq2_period_timer >= 0x800) {
ctx.sq2_period_timer = ctx.sq2_period_reset;
ctx.sq2_sample = (ctx.sq2_sample + 1) % 8;
}
}
for(;wave_time >= TIME_PER_WAVE_TICK;wave_time -= TIME_PER_WAVE_TICK) {
ctx.ch3_period_timer++;
if(ctx.ch3_period_timer >= 0x800) {
ctx.ch3_period_timer = ctx.ch3_period_reset;
ctx.ch3_sample = (ctx.ch3_sample + 1) % 32;
if((ctx.ch3_sample & 0b1) == 0b1) {
ctx.ch3_last_sample = ctx.wave_ram[ctx.ch3_sample >> 1] & 0xF;
} else {
ctx.ch3_last_sample = ctx.wave_ram[ctx.ch3_sample >> 1] >> 4;
}
}
}
lfsr_timer += TIME_PER_SAMPLE;
for(;lfsr_timer >= lfsr_clock;lfsr_timer -= lfsr_clock) {
//if(ctx.ch4_enable) {
lfsr_clocks++;
lfsr_clock = lfsr_clock_buffer;
//lfsr_timer = 0;
//u16 new = ~((ctx.ch4_lfsr & 0b1) ^ ((ctx.ch4_lfsr & 0b10) >> 1)) & 0b1;
//ctx.ch4_lfsr &= ~(1 << 15);
//ctx.ch4_lfsr |= ((new << 15) & 0x8000);
//printf("new bit: %02X\n", (new << 15));
//if(ctx.ch4_lfsr_width) {
// ctx.ch4_lfsr &= ~(1 << 7);
// ctx.ch4_lfsr |= (new << 7);
//}
//ctx.ch4_lfsr = ctx.ch4_lfsr >> 1;
//printf("lfsr: %02X, bit: %d\n", ctx.ch4_lfsr, new);
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;
if(new_bit) {
ctx.ch4_lfsr |= bit_mask;
} else {
ctx.ch4_lfsr &= ~bit_mask;
}
//if(ctx.sq2_period_timer >= 0x800) {
// ctx.sq2_period_timer = ctx.sq2_period_reset;
// ctx.sq2_sample = (ctx.sq2_sample + 1) % 8;
//}
}
for(;wave_time >= TIME_PER_WAVE_TICK;wave_time -= TIME_PER_WAVE_TICK) {
//ctx.ch3_period_timer++;
//if(ctx.ch3_period_timer >= 0x800) {
// ctx.ch3_period_timer = ctx.ch3_period_reset;
// ctx.ch3_sample = (ctx.ch3_sample + 1) % 32;
// if((ctx.ch3_sample & 0b1) == 0b1) {
// ctx.ch3_last_sample = ctx.wave_ram[ctx.ch3_sample >> 1] & 0xF;
// } else {
// ctx.ch3_last_sample = ctx.wave_ram[ctx.ch3_sample >> 1] >> 4;
// }
//}
}
float sq1_val = 0;
float sq2_val = 0;
float ch3_val = 0;
@ -168,7 +146,7 @@ static int audio_callback(const void* input_uffer, void *output_buffer,
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 = ((float)ctx.sq1_volume/15.0f) * (((float)(ctx.sq1_audio_buffer[ctx.sq1_read_index % 4096]) - 7.5f)/7.5f);
sq1_val = ((float)ctx.sq1_volume/15.0f) * (((float)(ctx.sq1_audio_buffer[ctx.sq1_read_index % AUDIO_BUFFER_SIZE]) - 7.5f)/7.5f);
if(ctx.ch1_left) {
left += sq1_val;
}else {
@ -188,6 +166,7 @@ static int audio_callback(const void* input_uffer, void *output_buffer,
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 = ((float)ctx.sq2_volume/15.0f) * (((float)(ctx.sq2_audio_buffer[ctx.sq1_read_index % AUDIO_BUFFER_SIZE]) - 7.5f)/7.5f);
if(ctx.ch2_left) {
left += sq2_val;
}else {
@ -206,17 +185,8 @@ static int audio_callback(const void* input_uffer, void *output_buffer,
if(ctx.ch3_dac){
ch3_val = -1;
if(ctx.ch3_enable) {
u8 shift = 0;
if(ctx.ch3_volume == 0b10) {
shift = 1;
}
if(ctx.ch3_volume == 0b00) {
shift = 4;
}
if(ctx.ch3_volume == 0b11) {
shift = 2;
}
//ch3_val = (((float)(ctx.ch3_last_sample >> shift) - 7.5f)/7.5f);
ch3_val = (((float)(ctx.ch3_audio_buffer[ctx.sq1_read_index % AUDIO_BUFFER_SIZE]) - 7.5f)/7.5f);
if(ctx.ch3_left) {
left += ch3_val;
//printf("left: %d\n", ctx.ch3_volume);
@ -237,6 +207,7 @@ static int audio_callback(const void* input_uffer, void *output_buffer,
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 = ((float)ctx.ch4_volume/15.0f) * (((float)(ctx.ch4_audio_buffer[ctx.sq1_read_index % AUDIO_BUFFER_SIZE]) - 7.5f)/7.5f);
if(ctx.ch4_left) {
left += ch4_val;
//printf("left: %d\n", ctx.ch3_volume);
@ -335,6 +306,11 @@ void audio_init(){
PaStreamParameters output_parameters;
PaError err;
ctx.sq1_audio_buffer = malloc(sizeof(u8) * AUDIO_BUFFER_SIZE);
ctx.sq2_audio_buffer = malloc(sizeof(u8) * AUDIO_BUFFER_SIZE);
ctx.ch3_audio_buffer = malloc(sizeof(u8) * AUDIO_BUFFER_SIZE);
ctx.ch4_audio_buffer = malloc(sizeof(u8) * AUDIO_BUFFER_SIZE);
ctx.audio_enabled = false;
ctx.ch1_left = false;
ctx.ch1_right = false;
@ -422,24 +398,75 @@ void sq1_sweep() {
static int change = 1;
static u32 ticks = 0;
static u32 start, end = 0;
static u32 start = 0;
static u32 period_tick;
static u32 samples_per_sec = 0;
void audio_period_tick() {
period_tick++;
u32 end = get_ticks();
samples_per_sec++;
if (end - start >= 1000) {
printf("Samples Per Second: %d\n", samples_per_sec);
start = end;
samples_per_sec = 0;
}
if(period_tick % 2 == 0){
ctx.sq1_period_timer++;
ctx.sq2_period_timer++;
if(ctx.sq1_period_timer >= 0x800) {
ctx.sq1_period_timer = ctx.sq1_period_reset;
ctx.sq1_sample = (ctx.sq1_sample + 1) % 8;
}
if(ctx.sq1_write_index > ctx.sq1_read_index){
ctx.sq1_audio_buffer[(ctx.sq1_write_index++)%4096] = square_sample[ctx.sq1_duty][ctx.sq1_sample];
} else {
printf("buffer overflow\n");
if(ctx.sq2_period_timer >= 0x800) {
ctx.sq2_period_timer = ctx.sq2_period_reset;
ctx.sq2_sample = (ctx.sq2_sample + 1) % 8;
}
}
u8 shift = 0;
if(ctx.ch3_volume == 0b10) {
shift = 1;
}
if(ctx.ch3_volume == 0b00) {
shift = 4;
}
if(ctx.ch3_volume == 0b11) {
shift = 2;
}
ctx.ch3_period_timer++;
if(ctx.ch3_period_timer >= 0x800) {
ctx.ch3_period_timer = ctx.ch3_period_reset;
ctx.ch3_sample = (ctx.ch3_sample + 1) % 32;
if((ctx.ch3_sample & 0b1) == 0b1) {
ctx.ch3_last_sample = ctx.wave_ram[ctx.ch3_sample >> 1] & 0xF;
} else {
ctx.ch3_last_sample = ctx.wave_ram[ctx.ch3_sample >> 1] >> 4;
}
}
lfsr_timer--;
if(lfsr_timer == 0) {
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;
if(new_bit) {
ctx.ch4_lfsr |= bit_mask;
} else {
ctx.ch4_lfsr &= ~bit_mask;
}
}
if(ctx.sq1_write_index < (ctx.sq1_read_index + AUDIO_BUFFER_SIZE)){
ctx.sq1_audio_buffer[(ctx.sq1_write_index)%AUDIO_BUFFER_SIZE] = square_sample[ctx.sq1_duty][ctx.sq1_sample];
ctx.sq2_audio_buffer[(ctx.sq1_write_index)%AUDIO_BUFFER_SIZE] = square_sample[ctx.sq2_duty][ctx.sq2_sample];
ctx.ch3_audio_buffer[(ctx.sq1_write_index)%AUDIO_BUFFER_SIZE] = ctx.ch3_last_sample >> shift;
ctx.ch4_audio_buffer[(ctx.sq1_write_index)%AUDIO_BUFFER_SIZE] = (ctx.ch4_lfsr & 0b1) ? 0xF : 0x0;
ctx.sq1_write_index++;
} else {
printf("buffer overflow\n");
}
}
void audio_tick(){
@ -588,7 +615,6 @@ void enable_noise() {
if(!ctx.ch4_dac){
return;
}
start = get_ticks();
ctx.ch4_enable = true;
ctx.ch4_env_timer = ctx.ch4_env_pace_buffer != 0 ? ctx.ch4_env_pace_buffer : 8;
ctx.ch4_env_direction = ctx.ch4_env_direction_buffer;
@ -596,7 +622,7 @@ void enable_noise() {
ctx.ch4_volume = ctx.ch4_initial_volume;
ctx.ch4_lfsr = 0;
lfsr_clock = lfsr_clock_buffer;
lfsr_timer = 0;
lfsr_timer = (ctx.ch4_clock_divider > 0 ? (ctx.ch4_clock_divider << 8) : 8) << ctx.ch4_clock_shift;
}
u8 audio_read(u16 address) {

View File

@ -43,7 +43,7 @@ void timer_tick() {
audio_tick();
}
if((prev_div & (1 << 1)) && (!(ctx.div & (1 << 1)))){
if((prev_div & (1 << 0)) && (!(ctx.div & (1 << 0)))){
audio_period_tick();
}
}