Banjo API 1.1.0-alpha
Low-level C99 game development API
Loading...
Searching...
No Matches
audio_pcm.c
Go to the documentation of this file.
1
93
94#include <banjo/api.h>
95#include <banjo/app.h>
96#include <banjo/main.h>
97#include <banjo/audio.h>
98#include <banjo/log.h>
99#include <banjo/system.h>
100#include <banjo/time.h>
101
102// Data passed to the audio callback. The callback reads this to know what
103// frequency to generate. We modify it in real-time to change the pitch.
106
107static void* setup(struct bj_app* app, void* init_data) {
108 (void)init_data;
109
110 // Initialize the audio subsystem.
111 if (!bj_begin(BJ_AUDIO_SYSTEM, 0)) {
112 bj_quit_app(app, 1);
113 return 0;
114 }
115
116 // Choose which waveform to generate. BJ_AUDIO_WAVEFORM_SINE creates smooth
117 // sine wave tones. Other options include square, triangle, and sawtooth.
118 data.function = BJ_AUDIO_WAVEFORM_SINE;
119
120 // Open an audio device with specific properties:
121 // - format: BJ_AUDIO_FORMAT_F32 uses 32-bit float samples (-1.0 to 1.0)
122 // - sample_rate: 44100 Hz (CD quality)
123 // - channels: 2 (stereo output)
124 // The callback bj_play_audio_note will be called repeatedly to fill buffers.
125 // We pass &data so the callback knows what frequency to generate.
126 bj_audio_properties audio_props = {
127 .format = BJ_AUDIO_FORMAT_F32, .amplitude = 16000, .channels = 2, .sample_rate = 44100,
128 };
130
131 if (!p_device) {
132 bj_quit_app(app, 1);
133 return 0;
134 }
135
136 // Start audio playback. The callback will now run in a separate thread,
137 // continuously filling audio buffers.
139 return 0;
140}
141
142static void step(struct bj_app* app, struct bj_tick_info tick, void* user_data) {
143 (void)tick;
144 (void)user_data;
145
146 // Simple melody: C-D-E-F-G-F-E-D-C (frequencies in Hz).
147 // Middle C (C4) = 261.63 Hz, D4 = 293.66 Hz, etc.
148 static const double melody[] = {
149 261.63, 293.66, 329.63,
150 349.23, 392.00, 349.23,
151 329.63, 293.66, 261.63
152 };
153 enum { MELODY_LEN = 9 };
154
155 // Use runtime as note index - changes once per second.
156 int note = (int)bj_run_time();
157
158 if (note >= MELODY_LEN) {
159 bj_quit_app(app, 0);
160 return;
161 }
162
163 // Update the frequency. The audio callback runs in a separate thread and
164 // picks up this change at its next buffer, smoothly transitioning to the new note.
165 data.frequency = melody[note];
166}
167
168static void teardown(struct bj_app* app, void* user_data) {
169 (void)user_data;
170 // Stop audio playback and close the device.
172 bj_end();
173}
174
175int main(int argc, char* argv[]) {
176 (void)argc; (void)argv;
177 return bj_run_app(setup, step, 0, teardown, 0);
178}
General-purpose definitions for Banjo API.
Application lifecycle: callback-driven setup, step, and teardown.
Basic audio library interface.
static void step(struct bj_app *app, struct bj_tick_info tick, void *user_data)
Definition audio_pcm.c:142
bj_audio_device * p_device
Definition audio_pcm.c:105
static void teardown(struct bj_app *app, void *user_data)
Definition audio_pcm.c:168
bj_audio_play_note_data data
Definition audio_pcm.c:104
static void * setup(struct bj_app *app, void *init_data)
Definition audio_pcm.c:107
int bj_run_app(bj_app_setup_fn setup, bj_app_step_fn step, bj_app_fixed_step_fn fixed_step, bj_app_teardown_fn teardown, void *init_data)
Drive the application lifecycle.
void bj_quit_app(struct bj_app *app, int exit_code)
Signal the given application to exit on the next iteration.
Timing snapshot handed to the step and fixed-step callbacks.
Definition app.h:107
void bj_play_audio_note(void *buffer, unsigned frames, const struct bj_audio_properties *audio, void *user_data, uint64_t base_sample_index)
Generate a basic waveform tone using a built-in callback.
void bj_destroy_audio_device(struct bj_audio_device *device)
Close an audio device and release all associated resources.
struct bj_audio_device * bj_create_audio_device(const struct bj_audio_properties *properties, bj_audio_callback_fn callback, void *callback_user_data, struct bj_error **error)
Open the default audio device for playback.
void bj_play_audio_device(struct bj_audio_device *device)
Resume audio playback.
@ BJ_AUDIO_WAVEFORM_SINE
Generate a sine wave.
Definition audio.h:375
@ BJ_AUDIO_FORMAT_F32
32-bit IEEE-754 float PCM.
Definition audio.h:139
Define parameters for generating simple waveforms.
Definition audio.h:400
Describe properties of an audio device.
Definition audio.h:193
struct bj_audio_device bj_audio_device
Definition api.h:337
bj_bool bj_begin(int systems, struct bj_error **error)
Initialises the system.
void bj_end(void)
De-initialises the system.
@ BJ_AUDIO_SYSTEM
Definition system.h:80
double bj_run_time(void)
Gets the current time in seconds since Banjo initialisation.
Logging utility functions.
Portable main() replacement with platform-aware entry shim.
Header file for system interactions.
int main(void)
Header file for time manipulation utilities.