Banjo API 0.0.1
Multi-purpose C99 API
|
Provide basic PCM audio playback. More...
The audio component offers 1-channel Pulse-Code Modulation (PCM) playback with a callback-based interface, suitable for retro-style games and basic procedural sound generation.
Data Structures | |
struct | bj_audio_properties_t |
Describe properties of an audio device. More... | |
struct | bj_audio_play_note_data_t |
Define parameters for generating simple waveforms. More... | |
struct | bj_audio_layer_t |
Represent an audio backend (ALSA, MME, etc). More... | |
struct | bj_audio_layer_create_info |
Declare a backend and its constructor function. More... | |
Macros | |
#define | BJ_AUDIO_FORMAT_WIDTH(x) |
#define | BJ_AUDIO_FORMAT_FLOAT(x) |
#define | BJ_AUDIO_FORMAT_INT(x) |
#define | BJ_AUDIO_FORMAT_BIG_ENDIAN(x) |
#define | BJ_AUDIO_FORMAT_SIGNED(x) |
Typedefs | |
typedef struct bj_audio_device_t | bj_audio_device |
Opaque handle to an audio device instance. | |
typedef enum bj_audio_format_t | bj_audio_format |
Describes how and audio device encodes sound samples. | |
typedef struct bj_audio_properties_t | bj_audio_properties |
Describe properties of an audio device. | |
typedef void(* | bj_audio_callback_t) (void *buffer, unsigned frames, const bj_audio_properties *audio, void *user_data, uint64_t base_sample_index) |
Define a callback for generating audio samples. | |
typedef struct bj_audio_play_note_data_t | bj_audio_play_note_data |
Define parameters for generating simple waveforms. | |
typedef struct bj_audio_layer_t | bj_audio_layer |
Represent an audio backend (ALSA, MME, etc). | |
Enumerations | |
enum | bj_audio_format_t { BJ_AUDIO_FORMAT_UNKNOWN = 0x0000 , BJ_AUDIO_FORMAT_INT16 = 0x8010 , BJ_AUDIO_FORMAT_F32 = 0x8120 } |
Describes how and audio device encodes sound samples. More... | |
Functions | |
bj_audio_device * | bj_open_audio_device (const bj_audio_properties *p_properties, bj_audio_callback_t p_callback, void *p_callback_user_data, bj_error **p_error) |
Open the default audio device for playback. | |
void | bj_close_audio_device (bj_audio_device *p_device) |
Close an audio device and release all associated resources. | |
void | bj_audio_device_play (bj_audio_device *p_device) |
Resume audio playback. | |
void | bj_audio_device_pause (bj_audio_device *p_device) |
Pause audio playback. | |
void | bj_audio_device_reset (bj_audio_device *p_device) |
Reset the playback stream sample index to 0. | |
void | bj_audio_device_stop (bj_audio_device *p_device) |
Stop playback and reset the sample stream. | |
bj_bool | bj_audio_device_is_playing (const bj_audio_device *p_device) |
Query whether the device is currently playing audio. | |
void | bj_audio_play_note (void *buffer, unsigned frames, const bj_audio_properties *audio, void *user_data, uint64_t base_sample_index) |
Generate a basic waveform tone using a built-in callback. | |
struct bj_audio_properties_t |
This structure is passed to audio callbacks to inform them about the format and limits of the current playback device.
Data Fields | ||
---|---|---|
int16_t | amplitude | Maximum amplitude of the output samples. |
unsigned int | channels | Number of channels (currently always 1). |
bj_audio_format | format | Sampling format. |
unsigned int | sample_rate | Number of samples per second (Hz). |
struct bj_audio_layer_t |
This internal structure handles platform-specific device control and is used to abstract away the OS details.
Data Fields | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
void(*)(struct bj_audio_layer_t *self, bj_audio_device *device) | close_device |
Close an audio device managed by this backend. Used to stop and destroy a specific audio device instance.
| ||||||||
struct bj_audio_layer_data_t * | data |
Backend-specific data pointer. Used internally by the backend to store platform-specific state. | ||||||||
void(*)(struct bj_audio_layer_t *self, bj_error **p_error) | end |
Shutdown and clean up the backend. Called during deinitialization to release all backend-related resources.
| ||||||||
bj_audio_device *(*)(struct bj_audio_layer_t *self, const bj_audio_properties *p_properties, bj_audio_callback_t callback, void *user_data, bj_error **p_error) | open_device |
Open an audio device through this backend. Initializes a new audio device using the given callback and context.
|
struct bj_audio_layer_create_info |
Used at initialization time to register available backends such as ALSA, MME, or WebAudio.
Data Fields | ||
---|---|---|
bj_audio_layer *(*)(bj_error **) | create | Factory function to instantiate the backend. |
const char * | name | Name of the backend (e.g., "alsa", "mme"). |
#define BJ_AUDIO_FORMAT_BIG_ENDIAN | ( | x | ) |
#define BJ_AUDIO_FORMAT_FLOAT | ( | x | ) |
#define BJ_AUDIO_FORMAT_INT | ( | x | ) |
#define BJ_AUDIO_FORMAT_SIGNED | ( | x | ) |
#define BJ_AUDIO_FORMAT_WIDTH | ( | x | ) |
typedef void(* bj_audio_callback_t) (void *buffer, unsigned frames, const bj_audio_properties *audio, void *user_data, uint64_t base_sample_index) |
This callback is called periodically from a dedicated audio thread to generate PCM audio data.
buffer | Output buffer to write int16_t samples into. |
frames | Number of audio frames to generate. |
audio | Pointer to audio device properties (readonly). |
user_data | User-defined pointer passed at device creation. |
base_sample_index | Index of the first sample in the current buffer. |
typedef struct bj_audio_device_t bj_audio_device |
typedef enum bj_audio_format_t bj_audio_format |
typedef struct bj_audio_layer_t bj_audio_layer |
This internal structure handles platform-specific device control and is used to abstract away the OS details.
typedef struct bj_audio_play_note_data_t bj_audio_play_note_data |
Used in conjunction with bj_audio_play_note to synthesize basic musical tones such as sine or square waves.
typedef struct bj_audio_properties_t bj_audio_properties |
This structure is passed to audio callbacks to inform them about the format and limits of the current playback device.
enum bj_audio_format_t |
Enumerator | |
---|---|
BJ_AUDIO_FORMAT_INT16 | !< Unknown |
BJ_AUDIO_FORMAT_F32 | !< 16-bit signed integer |
bj_bool bj_audio_device_is_playing | ( | const bj_audio_device * | p_device | ) |
p_device | Pointer to the audio device. |
void bj_audio_device_pause | ( | bj_audio_device * | p_device | ) |
While paused, the audio thread continues running and outputs silence.
p_device | Pointer to the audio device. |
void bj_audio_device_play | ( | bj_audio_device * | p_device | ) |
Playback resumes from where it was previously paused.
p_device | Pointer to the audio device. |
void bj_audio_device_reset | ( | bj_audio_device * | p_device | ) |
This does not stop or pause playback, but restarts timing from zero.
p_device | Pointer to the audio device. |
void bj_audio_device_stop | ( | bj_audio_device * | p_device | ) |
This is equivalent to calling pause followed by reset.
p_device | Pointer to the audio device. |
void bj_audio_play_note | ( | void * | buffer, |
unsigned | frames, | ||
const bj_audio_properties * | audio, | ||
void * | user_data, | ||
uint64_t | base_sample_index ) |
This function can be passed as a bj_audio_callback_t and uses the data provided in bj_audio_play_note_data_t to generate tones.
buffer | Output buffer to write samples into. |
frames | Number of frames to generate. |
audio | Audio device properties. |
user_data | Pointer to a bj_audio_play_note_data_t instance. |
base_sample_index | Starting sample index (used to compute phase). |
void bj_close_audio_device | ( | bj_audio_device * | p_device | ) |
This stops playback and joins the audio thread before cleanup.
p_device | Pointer to the audio device to close. |
bj_audio_device * bj_open_audio_device | ( | const bj_audio_properties * | p_properties, |
bj_audio_callback_t | p_callback, | ||
void * | p_callback_user_data, | ||
bj_error ** | p_error ) |
This initializes the audio backend and starts playback immediately using the provided callback.
p_properties | An optional pointer to required properties, can be 0. |
p_callback | Function pointer to the user-provided audio callback. |
p_callback_user_data | User-defined pointer passed to the callback. |
p_error | Pointer to receive error information. |
If p_properties
is set, the driver will attempt to open a device with the provided properties. The opened device may not have the requested requirements.