From 24b957fc53ea94aead4428b33fee909466010fe5 Mon Sep 17 00:00:00 2001 From: Simon Gellis Date: Mon, 21 Oct 2024 20:52:51 -0400 Subject: [PATCH] Audio! --- audio.c | 37 +++++++++++++++++++++++++++++++++++++ audio.h | 16 ++++++++++++++++ game.c | 19 ++++++++++++++----- makefile | 2 +- shrooms-vb-core | 2 +- 5 files changed, 69 insertions(+), 7 deletions(-) create mode 100644 audio.c create mode 100644 audio.h diff --git a/audio.c b/audio.c new file mode 100644 index 0000000..c45f761 --- /dev/null +++ b/audio.c @@ -0,0 +1,37 @@ +#include +#include + +int audioInit(AudioContext *aud) { + SDL_AudioSpec spec; + spec.freq = 41700; + spec.format = AUDIO_S16; + spec.channels = 2; + spec.samples = 1024; + spec.callback = NULL; + spec.userdata = NULL; + + aud->id = SDL_OpenAudioDevice(NULL, 0, &spec, NULL, 0); + aud->paused = true; + if (!aud->id) { + fprintf(stderr, "could not open audio device: %s\n", SDL_GetError()); + return -1; + } + aud->output = fopen("output.bin", "w"); + return 0; +} + +int audioUpdate(AudioContext *aud, void *data, uint32_t bytes) { + if (!aud->id) return -1; + + if (SDL_QueueAudio(aud->id, data, bytes)) { + fprintf(stderr, "could not write audio: %s\n", SDL_GetError()); + return -1; + } + + if (aud->paused) { + SDL_PauseAudioDevice(aud->id, false); + aud->paused = false; + } + + return 0; +} \ No newline at end of file diff --git a/audio.h b/audio.h new file mode 100644 index 0000000..3d474cc --- /dev/null +++ b/audio.h @@ -0,0 +1,16 @@ +#ifndef SHROOMS_VB_NATIVE_AUDIO_ +#define SHROOMS_VB_NATIVE_AUDIO_ + +#include +#include + +typedef struct { + SDL_AudioDeviceID id; + bool paused; + FILE *output; +} AudioContext; + +int audioInit(AudioContext *aud); +int audioUpdate(AudioContext *aud, void *data, uint32_t bytes); + +#endif \ No newline at end of file diff --git a/game.c b/game.c index 6ecb687..b818d75 100644 --- a/game.c +++ b/game.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -20,21 +21,27 @@ long int tickNs() { typedef struct { GraphicsContext *gfx; + AudioContext aud; + int16_t audioBuffer[834 * 2]; } GameState; int onFrame(VB *sim) { static uint8_t leftEye[384*224]; static uint8_t rightEye[384*224]; GameState *state; - GraphicsContext *gfx; + void *samples; + uint32_t samplePairs; state = vbGetUserData(sim); - gfx = state->gfx; vbGetPixels(sim, leftEye, 1, 384, rightEye, 1, 384); - gfxUpdateLeftEye(gfx, leftEye); - gfxUpdateRightEye(gfx, rightEye); - gfxRender(gfx); + gfxUpdateLeftEye(state->gfx, leftEye); + gfxUpdateRightEye(state->gfx, rightEye); + + samples = vbGetSamples(sim, NULL, &samplePairs); + audioUpdate(&state->aud, samples, samplePairs * 4); + vbSetSamples(sim, samples, 834); + gfxRender(state->gfx); return 1; } @@ -48,6 +55,8 @@ int runGame(VB *sim, GraphicsContext *gfx) { uint64_t ticks, prevTicks; state.gfx = gfx; + audioInit(&state.aud); + vbSetSamples(sim, &state.audioBuffer, 834); vbSetUserData(sim, &state); vbSetFrameCallback(sim, &onFrame); diff --git a/makefile b/makefile index ea33f24..85f346f 100644 --- a/makefile +++ b/makefile @@ -11,7 +11,7 @@ else endif build: - @$(CC) cli.c controller.c game.c graphics.c main.c assets/assets.s -I . \ + @$(CC) audio.c cli.c controller.c game.c graphics.c main.c assets/assets.s -I . \ $(SHROOMSFLAGS) $(SDL2FLAGS) \ -D POSIX_C_SOURCE=199309L \ -o shrooms-vb \ diff --git a/shrooms-vb-core b/shrooms-vb-core index 50903c7..c06da32 160000 --- a/shrooms-vb-core +++ b/shrooms-vb-core @@ -1 +1 @@ -Subproject commit 50903c7513b00cc62cb20a1578e7f6484914a9da +Subproject commit c06da323218071719c1500379e84008c369169d7