Banjo API 1.0.0-rc.2
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_PLAY_SINE creates smooth
117 // sine wave tones. Other options include square, triangle, and sawtooth.
118 data.function = BJ_AUDIO_PLAY_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.
127 .format = BJ_AUDIO_FORMAT_F32, /* float buffer */
128 .amplitude = 16000, /* used only for INT16 path */
129 .sample_rate = 44100,
130 .channels = 2,
131 }, bj_play_audio_note, &data, 0);
132
133 if (!p_device) {
134 bj_quit_app(app, 1);
135 return 0;
136 }
137
138 // Start audio playback. The callback will now run in a separate thread,
139 // continuously filling audio buffers.
141 return 0;
142}
143
144static void step(struct bj_app* app, struct bj_tick_info tick, void* user_data) {
145 (void)tick;
146 (void)user_data;
147
148 // Simple melody: C-D-E-F-G-F-E-D-C (frequencies in Hz).
149 // Middle C (C4) = 261.63 Hz, D4 = 293.66 Hz, etc.
150 static const double melody[] = {
151 261.63, 293.66, 329.63,
152 349.23, 392.00, 349.23,
153 329.63, 293.66, 261.63
154 };
155 enum { MELODY_LEN = 9 };
156
157 // Use runtime as note index - changes once per second.
158 int note = (int)bj_run_time();
159
160 if (note >= MELODY_LEN) {
161 bj_quit_app(app, 0);
162 return;
163 }
164
165 // Update the frequency. The audio callback runs in a separate thread and
166 // will immediately pick up this change, smoothly transitioning to the new note.
167 data.frequency = melody[note];
168}
169
170static void teardown(struct bj_app* app, void* user_data) {
171 (void)user_data;
172 // Stop audio playback and close the device.
174 bj_end();
175}
176
177int main(int argc, char* argv[]) {
178 (void)argc; (void)argv;
179 return bj_run_app(setup, step, 0, teardown, 0);
180}
General-purpose definitions for Banjo API.
Application lifecycle: callback-driven setup, step, and teardown.
Basic audio library interface.
int main(int argc, char *argv[])
Definition audio_pcm.c:177
static void step(struct bj_app *app, struct bj_tick_info tick, void *user_data)
Definition audio_pcm.c:144
bj_audio_device * p_device
Definition audio_pcm.c:105
static void teardown(struct bj_app *app, void *user_data)
Definition audio_pcm.c:170
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:106
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_close_audio_device(struct bj_audio_device *device)
Close an audio device and release all associated resources.
struct bj_audio_device * bj_open_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_FORMAT_F32
32-bit IEEE-754 float PCM.
Definition audio.h:132
Describe properties of an audio device.
Definition audio.h:186
struct bj_audio_device bj_audio_device
Definition api.h:325
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.
Define parameters for generating simple waveforms.
Definition audio.h:332
Header file for system interactions.
Header file for time manipulation utilities.