cubeb 0.0.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
cubeb

Introduction

This is the documentation for the libcubeb C API. libcubeb is a callback-based audio API library allowing the authoring of portable multiplatform audio playback and recording.

Example code

This example shows how to create a duplex stream that pipes the microphone to the speakers, with minimal latency and the proper sample-rate for the platform.

cubeb * app_ctx;
cubeb_init(&app_ctx, "Example Application", NULL);
int rv;
uint32_t rate;
uint32_t latency_frames;
uint64_t ts;
rv = cubeb_get_preferred_sample_rate(app_ctx, &rate);
if (rv != CUBEB_OK) {
fprintf(stderr, "Could not get preferred sample-rate");
return rv;
}
cubeb_stream_params output_params;
output_params.format = CUBEB_SAMPLE_FLOAT32NE;
output_params.rate = rate;
output_params.channels = 2;
output_params.layout = CUBEB_LAYOUT_UNDEFINED;
output_params.prefs = CUBEB_STREAM_PREF_NONE;
rv = cubeb_get_min_latency(app_ctx, &output_params, &latency_frames);
if (rv != CUBEB_OK) {
fprintf(stderr, "Could not get minimum latency");
return rv;
}
cubeb_stream_params input_params;
input_params.format = CUBEB_SAMPLE_FLOAT32NE;
input_params.rate = rate;
input_params.channels = 1;
input_params.layout = CUBEB_LAYOUT_UNDEFINED;
rv = cubeb_stream_init(app_ctx, &stm, "Example Stream 1",
NULL, &input_params,
NULL, &output_params,
latency_frames,
data_cb, state_cb,
NULL);
if (rv != CUBEB_OK) {
fprintf(stderr, "Could not open the stream");
return rv;
}
if (rv != CUBEB_OK) {
fprintf(stderr, "Could not start the stream");
return rv;
}
for (;;) {
printf("time=%llu\n", ts);
sleep(1);
}
if (rv != CUBEB_OK) {
fprintf(stderr, "Could not stop the stream");
return rv;
}
cubeb_destroy(app_ctx);
CUBEB_EXPORT int cubeb_get_preferred_sample_rate(cubeb *context, uint32_t *rate)
Get the preferred sample rate for this backend: this is hardware and platform dependent,...
struct cubeb cubeb
Opaque handle referencing the application state.
Definition cubeb.h:125
CUBEB_EXPORT int cubeb_stream_start(cubeb_stream *stream)
Start playback.
CUBEB_EXPORT int cubeb_stream_init(cubeb *context, cubeb_stream **stream, char const *stream_name, cubeb_devid input_device, cubeb_stream_params *input_stream_params, cubeb_devid output_device, cubeb_stream_params *output_stream_params, uint32_t latency_frames, cubeb_data_callback data_callback, cubeb_state_callback state_callback, void *user_ptr)
Initialize a stream associated with the supplied application context.
@ CUBEB_STREAM_PREF_NONE
No stream preferences are requested.
Definition cubeb.h:233
CUBEB_EXPORT int cubeb_init(cubeb **context, char const *context_name, char const *backend_name)
Initialize an application context.
struct cubeb_stream cubeb_stream
Opaque handle referencing the stream state.
Definition cubeb.h:127
CUBEB_EXPORT int cubeb_stream_stop(cubeb_stream *stream)
Stop playback.
CUBEB_EXPORT int cubeb_stream_get_position(cubeb_stream *stream, uint64_t *position)
Get the current stream playback position.
CUBEB_EXPORT void cubeb_stream_destroy(cubeb_stream *stream)
Destroy a stream.
@ CUBEB_OK
Success.
Definition cubeb.h:304
CUBEB_EXPORT void cubeb_destroy(cubeb *context)
Destroy an application context.
CUBEB_EXPORT int cubeb_get_min_latency(cubeb *context, cubeb_stream_params *params, uint32_t *latency_frames)
Get the minimal latency value, in frames, that is guaranteed to work when creating a stream for the s...
Stream format initialization parameters.
Definition cubeb.h:274
cubeb_sample_format format
Requested sample format.
Definition cubeb.h:275
cubeb_channel_layout layout
Requested channel layout.
Definition cubeb.h:280
cubeb_stream_prefs prefs
Requested preferences.
Definition cubeb.h:282
uint32_t rate
Requested sample rate.
Definition cubeb.h:277
uint32_t channels
Requested channel count.
Definition cubeb.h:278
long data_cb(cubeb_stream * stm, void * user,
const void * input_buffer, void * output_buffer, long nframes)
{
const float * in = input_buffer;
float * out = output_buffer;
for (int i = 0; i < nframes; ++i) {
for (int c = 0; c < 2; ++c) {
out[2 * i + c] = in[i];
}
}
return nframes;
}
void state_cb(cubeb_stream * stm, void * user, cubeb_state state)
{
printf("state=%d\n", state);
}
cubeb_state
Stream states signaled via state_callback.
Definition cubeb.h:295