cubeb: cubeb

cubeb 0.0.0

Loading...
Searching...
No Matches
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;
output_params.input_params = CUBEB_INPUT_PROCESSING_PARAM_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;
input_params.prefs = CUBEB_STREAM_PREF_NONE;
input_params.input_params = CUBEB_INPUT_PROCESSING_PARAM_NONE;
cubeb_stream * stm;
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;
}
rv = cubeb_stream_start(stm);
if (rv != CUBEB_OK) {
fprintf(stderr, "Could not start the stream");
return rv;
}
for (;;) {
cubeb_stream_get_position(stm, &ts);
printf("time=%llu\n", ts);
sleep(1);
}
rv = cubeb_stream_stop(stm);
if (rv != CUBEB_OK) {
fprintf(stderr, "Could not stop the stream");
return rv;
}
cubeb_stream_destroy(stm);
cubeb_destroy(app_ctx);
cubeb_get_preferred_sample_rate
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,...
CUBEB_OK
@ CUBEB_OK
Success.
Definition cubeb.h:306
cubeb
struct cubeb cubeb
Opaque handle referencing the application state.
Definition cubeb.h:127
cubeb_stream_start
CUBEB_EXPORT int cubeb_stream_start(cubeb_stream *stream)
Start playback.
cubeb_stream_init
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
@ CUBEB_STREAM_PREF_NONE
No stream preferences are requested.
Definition cubeb.h:235
cubeb_init
CUBEB_EXPORT int cubeb_init(cubeb **context, char const *context_name, char const *backend_name)
Initialize an application context.
cubeb_stream
struct cubeb_stream cubeb_stream
Opaque handle referencing the stream state.
Definition cubeb.h:129
cubeb_stream_stop
CUBEB_EXPORT int cubeb_stream_stop(cubeb_stream *stream)
Stop playback.
cubeb_stream_get_position
CUBEB_EXPORT int cubeb_stream_get_position(cubeb_stream *stream, uint64_t *position)
Get the current stream playback position.
cubeb_stream_destroy
CUBEB_EXPORT void cubeb_stream_destroy(cubeb_stream *stream)
Destroy a stream.
cubeb_destroy
CUBEB_EXPORT void cubeb_destroy(cubeb *context)
Destroy an application context.
cubeb_get_min_latency
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...
cubeb_stream_params
Stream format initialization parameters.
Definition cubeb.h:276
cubeb_stream_params::input_params
cubeb_input_processing_params input_params
Requested input processing params.
Definition cubeb.h:285
cubeb_stream_params::format
cubeb_sample_format format
Requested sample format.
Definition cubeb.h:277
cubeb_stream_params::layout
cubeb_channel_layout layout
Requested channel layout.
Definition cubeb.h:282
cubeb_stream_params::prefs
cubeb_stream_prefs prefs
Requested preferences.
Definition cubeb.h:284
cubeb_stream_params::rate
uint32_t rate
Requested sample rate.
Definition cubeb.h:279
cubeb_stream_params::channels
uint32_t channels
Requested channel count.
Definition cubeb.h:280
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
cubeb_state
Stream states signaled via state_callback.
Definition cubeb.h:297