working on audio buffer stuff

This commit is contained in:
Samuel Walker 2025-02-17 21:59:17 -07:00
parent 020feb2a6f
commit 5668389488
Signed by: piwalker
GPG Key ID: 616B1928705EA4C9
3 changed files with 44 additions and 10 deletions

View File

@ -117,6 +117,11 @@ typedef struct {
float right_history[384]; float right_history[384];
u32 right_index; u32 right_index;
u8 sq1_sample_val;
u32 sq1_write_index;
u32 sq1_read_index;
u8 sq1_audio_buffer[4096];
} audio_context; } audio_context;
void audio_init(); void audio_init();

View File

@ -5,11 +5,13 @@
#include <portaudio.h> #include <portaudio.h>
#include <pa_ringbuffer.h> #include <pa_ringbuffer.h>
#include <math.h>
#define SAMPLE_RATE 44100 #define SAMPLE_RATE 44100
#define FRAMES_PER_BUFFER 64 #define FRAMES_PER_BUFFER 64
#define TIME_PER_SAMPLE 1.0f / (SAMPLE_RATE/1000.0f/1000.0f) #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 TIME_PER_AUDIO_TICK 1.0f / (1048576.0f/1000.0f/1000.0f)
#define AUDIO_TICKS_PER_SAMPLE 1048576.0f / SAMPLE_RATE
#define TIME_PER_WAVE_TICK 1.0f / (2097152.0f/1000.0f/1000.0f) #define TIME_PER_WAVE_TICK 1.0f / (2097152.0f/1000.0f/1000.0f)
#define LFSR_BASE_CLOCK 262144.0f #define LFSR_BASE_CLOCK 262144.0f
@ -31,6 +33,7 @@ static float right_cap = 0.0f;
static int history_timer = 0; static int history_timer = 0;
const int history_interval = 5; const int history_interval = 5;
static int lfsr_clocks = 0; static int lfsr_clocks = 0;
static double write_index = 0;
const u8 square_sample_00[8] = { const u8 square_sample_00[8] = {
0x0, 0x0,
@ -90,6 +93,12 @@ static int audio_callback(const void* input_uffer, void *output_buffer,
void *userData ) { void *userData ) {
float *out = (float *)output_buffer; float *out = (float *)output_buffer;
for(int i = 0; i < framesPerBuffer; i++) { for(int i = 0; i < framesPerBuffer; i++) {
write_index = write_index + AUDIO_TICKS_PER_SAMPLE;
ctx.sq1_read_index = (int)write_index;
if(ctx.sq1_read_index > ctx.sq1_write_index) {
ctx.sq1_read_index = ctx.sq1_write_index-1;
write_index = ctx.sq1_read_index;
}
if(ppu_get_context()->paused) { if(ppu_get_context()->paused) {
*out++ = 0; *out++ = 0;
*out++ = 0; *out++ = 0;
@ -100,13 +109,13 @@ static int audio_callback(const void* input_uffer, void *output_buffer,
audio_time += TIME_PER_SAMPLE; audio_time += TIME_PER_SAMPLE;
wave_time += TIME_PER_SAMPLE; wave_time += TIME_PER_SAMPLE;
for(;audio_time >= TIME_PER_AUDIO_TICK;audio_time -= TIME_PER_AUDIO_TICK) { for(;audio_time >= TIME_PER_AUDIO_TICK;audio_time -= TIME_PER_AUDIO_TICK) {
ctx.sq1_period_timer++; //ctx.sq1_period_timer++;
ctx.sq2_period_timer++; ctx.sq2_period_timer++;
if(ctx.sq1_period_timer >= 0x800) { //if(ctx.sq1_period_timer >= 0x800) {
ctx.sq1_period_timer = ctx.sq1_period_reset; // ctx.sq1_period_timer = ctx.sq1_period_reset;
ctx.sq1_sample = (ctx.sq1_sample + 1) % 8; // ctx.sq1_sample = (ctx.sq1_sample + 1) % 8;
} //}
if(ctx.sq2_period_timer >= 0x800) { if(ctx.sq2_period_timer >= 0x800) {
ctx.sq2_period_timer = ctx.sq2_period_reset; ctx.sq2_period_timer = ctx.sq2_period_reset;
ctx.sq2_sample = (ctx.sq2_sample + 1) % 8; ctx.sq2_sample = (ctx.sq2_sample + 1) % 8;
@ -158,7 +167,8 @@ static int audio_callback(const void* input_uffer, void *output_buffer,
if(ctx.ch1_dac){ if(ctx.ch1_dac){
sq1_val = -1; sq1_val = -1;
if(ctx.sq1_enable) { 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)(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);
if(ctx.ch1_left) { if(ctx.ch1_left) {
left += sq1_val; left += sq1_val;
}else { }else {
@ -177,7 +187,7 @@ static int audio_callback(const void* input_uffer, void *output_buffer,
if(ctx.ch2_dac){ if(ctx.ch2_dac){
sq2_val = -1; sq2_val = -1;
if(ctx.sq2_enable) { 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)(square_sample[ctx.sq2_duty][ctx.sq2_sample]) - 7.5f)/7.5f);
if(ctx.ch2_left) { if(ctx.ch2_left) {
left += sq2_val; left += sq2_val;
}else { }else {
@ -206,7 +216,7 @@ static int audio_callback(const void* input_uffer, void *output_buffer,
if(ctx.ch3_volume == 0b11) { if(ctx.ch3_volume == 0b11) {
shift = 2; shift = 2;
} }
ch3_val = (((float)(ctx.ch3_last_sample >> shift) - 7.5f)/7.5f); //ch3_val = (((float)(ctx.ch3_last_sample >> shift) - 7.5f)/7.5f);
if(ctx.ch3_left) { if(ctx.ch3_left) {
left += ch3_val; left += ch3_val;
//printf("left: %d\n", ctx.ch3_volume); //printf("left: %d\n", ctx.ch3_volume);
@ -226,7 +236,7 @@ static int audio_callback(const void* input_uffer, void *output_buffer,
if(ctx.ch4_dac){ if(ctx.ch4_dac){
ch4_val = -1; ch4_val = -1;
if(ctx.ch4_enable) { 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_lfsr & 0b1) == 0b1) ? (((float)ctx.ch4_volume - 7.5f)/7.5f) : -1.0f;
if(ctx.ch4_left) { if(ctx.ch4_left) {
left += ch4_val; left += ch4_val;
//printf("left: %d\n", ctx.ch3_volume); //printf("left: %d\n", ctx.ch3_volume);
@ -368,6 +378,7 @@ void audio_init(){
ctx.sq2_env_direction = false; ctx.sq2_env_direction = false;
ctx.sq2_env_pace = 0x0; ctx.sq2_env_pace = 0x0;
ctx.sq2_env_timer = 0x0; ctx.sq2_env_timer = 0x0;
ctx.sq1_write_index = 1;
err = Pa_Initialize(); err = Pa_Initialize();
if (err != paNoError) goto error; if (err != paNoError) goto error;
@ -412,6 +423,24 @@ void sq1_sweep() {
static int change = 1; static int change = 1;
static u32 ticks = 0; static u32 ticks = 0;
static u32 start, end = 0; static u32 start, end = 0;
static u32 period_tick;
void audio_period_tick() {
period_tick++;
if(period_tick % 2 == 0){
ctx.sq1_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");
}
}
}
void audio_tick(){ void audio_tick(){
u32 prev_ticks = ticks; u32 prev_ticks = ticks;

View File

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