From 243e13b19e250fcf0ed7c9f4d1ccc746a2fa86a2 Mon Sep 17 00:00:00 2001 From: Samuel Walker Date: Sun, 2 Feb 2025 12:35:53 -0700 Subject: [PATCH] starting work on apu --- include/audio.h | 3 ++- lib/audio.c | 19 ++++++++++++++----- lib/timer.c | 5 +++++ 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/include/audio.h b/include/audio.h index b7556f9..28a1cb7 100644 --- a/include/audio.h +++ b/include/audio.h @@ -11,4 +11,5 @@ typedef struct { float sq2_amp; } audio_context; -void audio_init(); \ No newline at end of file +void audio_init(); +void audio_tick(); \ No newline at end of file diff --git a/lib/audio.c b/lib/audio.c index ddf11d3..f98a1da 100644 --- a/lib/audio.c +++ b/lib/audio.c @@ -77,13 +77,13 @@ void audio_init(){ PaStream *stream; PaError err; - ctx.sq1_amp = 0; - ctx.sq1_duty = 0; - ctx.sq1_freq = 0; + ctx.sq1_amp = 0.2; + ctx.sq1_duty = 0.5; + ctx.sq1_freq = 200; ctx.sq2_amp = 0; - ctx.sq2_duty = 0; - ctx.sq2_freq = 0; + ctx.sq2_duty = 0.5; + ctx.sq2_freq = 400; for(int i = 0; i < TABLE_SIZE; i++) { sine[i] = (float) sin(((double)i/(double)TABLE_SIZE) * M_PI * 2.); @@ -116,4 +116,13 @@ void audio_init(){ error: Pa_Terminate(); fprintf(stderr, "portaudio stream error\n\tError Number: %d\n\tError Message: %s\n", err, Pa_GetErrorText(err)); +} + +static int change = 1; + +void audio_tick(){ + ctx.sq1_freq += change; + if(ctx.sq1_freq >= 600 || ctx.sq1_freq <= 200) { + change = -change; + } } \ No newline at end of file diff --git a/lib/timer.c b/lib/timer.c index 5c35e97..376a021 100644 --- a/lib/timer.c +++ b/lib/timer.c @@ -1,6 +1,7 @@ #include #include +#include static timer_context ctx = {0}; @@ -37,6 +38,10 @@ void timer_tick() { cpu_request_interrupt(IT_TIMER); } } + + if((prev_div & (1 << 12)) && (!(ctx.div & (1 << 12)))){ + audio_tick(); + } } void timer_write(u16 address, u8 value){