2012-01-13 01:38:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2011 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* This program is made available under an ISC-style license. See the
|
|
|
|
* accompanying file LICENSE for details.
|
|
|
|
*/
|
2013-03-13 18:36:46 +00:00
|
|
|
#if !defined(CUBEB_c2f983e9_c96f_e71c_72c3_bbf62992a382)
|
|
|
|
#define CUBEB_c2f983e9_c96f_e71c_72c3_bbf62992a382
|
2012-01-13 01:38:39 +00:00
|
|
|
|
|
|
|
#include <cubeb/cubeb-stdint.h>
|
|
|
|
|
2013-03-13 18:36:46 +00:00
|
|
|
#if defined(__cplusplus)
|
2012-01-13 01:38:39 +00:00
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** @mainpage
|
|
|
|
|
|
|
|
@section intro Introduction
|
|
|
|
|
|
|
|
This is the documentation for the <tt>libcubeb</tt> C API.
|
|
|
|
<tt>libcubeb</tt> is a callback-based audio API library allowing the
|
|
|
|
authoring of portable multiplatform audio playback.
|
|
|
|
|
|
|
|
@section example Example code
|
|
|
|
|
|
|
|
@code
|
|
|
|
cubeb * app_ctx;
|
|
|
|
cubeb_init(&app_ctx, "Example Application");
|
|
|
|
|
|
|
|
cubeb_stream_params params;
|
|
|
|
params.format = CUBEB_SAMPLE_S16NE;
|
|
|
|
params.rate = 48000;
|
|
|
|
params.channels = 2;
|
|
|
|
|
|
|
|
unsigned int latency_ms = 250;
|
|
|
|
|
|
|
|
cubeb_stream * stm;
|
|
|
|
cubeb_stream_init(app_ctx, &stm, "Example Stream 1", params,
|
|
|
|
latency_ms, data_cb, state_cb, NULL);
|
|
|
|
|
|
|
|
cubeb_stream_start(stm);
|
|
|
|
for (;;) {
|
2012-07-16 21:15:24 +00:00
|
|
|
cubeb_stream_get_position(stm, &ts);
|
|
|
|
printf("time=%llu\n", ts);
|
2012-01-13 01:38:39 +00:00
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
cubeb_stream_stop(stm);
|
|
|
|
|
|
|
|
cubeb_stream_destroy(stm);
|
|
|
|
cubeb_destroy(app_ctx);
|
|
|
|
@endcode
|
|
|
|
|
|
|
|
@code
|
|
|
|
long data_cb(cubeb_stream * stm, void * user, void * buffer, long nframes)
|
|
|
|
{
|
|
|
|
short * buf = buffer;
|
|
|
|
for (i = 0; i < nframes; ++i) {
|
|
|
|
for (c = 0; c < params.channels; ++c) {
|
|
|
|
buf[i][c] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nframes;
|
|
|
|
}
|
|
|
|
@endcode
|
|
|
|
|
|
|
|
@code
|
2012-07-16 21:15:24 +00:00
|
|
|
void state_cb(cubeb_stream * stm, void * user, cubeb_state state)
|
2012-01-13 01:38:39 +00:00
|
|
|
{
|
|
|
|
printf("state=%d\n", state);
|
|
|
|
}
|
|
|
|
@endcode
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file
|
|
|
|
The <tt>libcubeb</tt> C API. */
|
|
|
|
|
|
|
|
typedef struct cubeb cubeb; /**< Opaque handle referencing the application state. */
|
|
|
|
typedef struct cubeb_stream cubeb_stream; /**< Opaque handle referencing the stream state. */
|
|
|
|
|
|
|
|
/** Sample format enumeration. */
|
|
|
|
typedef enum {
|
|
|
|
/**< Little endian 16-bit signed PCM. */
|
|
|
|
CUBEB_SAMPLE_S16LE,
|
|
|
|
/**< Big endian 16-bit signed PCM. */
|
|
|
|
CUBEB_SAMPLE_S16BE,
|
|
|
|
/**< Little endian 32-bit IEEE floating point PCM. */
|
|
|
|
CUBEB_SAMPLE_FLOAT32LE,
|
|
|
|
/**< Big endian 32-bit IEEE floating point PCM. */
|
|
|
|
CUBEB_SAMPLE_FLOAT32BE,
|
2012-08-06 03:26:41 +00:00
|
|
|
#if defined(WORDS_BIGENDIAN) || defined(__BIG_ENDIAN__)
|
2012-01-13 01:38:39 +00:00
|
|
|
/**< Native endian 16-bit signed PCM. */
|
|
|
|
CUBEB_SAMPLE_S16NE = CUBEB_SAMPLE_S16BE,
|
|
|
|
/**< Native endian 32-bit IEEE floating point PCM. */
|
|
|
|
CUBEB_SAMPLE_FLOAT32NE = CUBEB_SAMPLE_FLOAT32BE
|
|
|
|
#else
|
|
|
|
/**< Native endian 16-bit signed PCM. */
|
|
|
|
CUBEB_SAMPLE_S16NE = CUBEB_SAMPLE_S16LE,
|
|
|
|
/**< Native endian 32-bit IEEE floating point PCM. */
|
|
|
|
CUBEB_SAMPLE_FLOAT32NE = CUBEB_SAMPLE_FLOAT32LE
|
|
|
|
#endif
|
|
|
|
} cubeb_sample_format;
|
|
|
|
|
2013-03-12 03:46:32 +00:00
|
|
|
#if defined(__ANDROID__)
|
|
|
|
typedef enum {
|
|
|
|
CUBEB_STREAM_TYPE_VOICE_CALL = 0,
|
|
|
|
CUBEB_STREAM_TYPE_SYSTEM = 1,
|
|
|
|
CUBEB_STREAM_TYPE_RING = 2,
|
|
|
|
CUBEB_STREAM_TYPE_MUSIC = 3,
|
|
|
|
CUBEB_STREAM_TYPE_ALARM = 4,
|
|
|
|
CUBEB_STREAM_TYPE_NOTIFICATION = 5,
|
|
|
|
CUBEB_STREAM_TYPE_BLUETOOTH_SCO = 6,
|
2014-05-22 07:08:05 +00:00
|
|
|
CUBEB_STREAM_TYPE_SYSTEM_ENFORCED = 7,
|
2013-03-12 03:46:32 +00:00
|
|
|
CUBEB_STREAM_TYPE_DTMF = 8,
|
|
|
|
CUBEB_STREAM_TYPE_TTS = 9,
|
|
|
|
CUBEB_STREAM_TYPE_FM = 10,
|
|
|
|
|
|
|
|
CUBEB_STREAM_TYPE_MAX
|
|
|
|
} cubeb_stream_type;
|
|
|
|
#endif
|
|
|
|
|
2012-01-13 01:38:39 +00:00
|
|
|
/** Stream format initialization parameters. */
|
|
|
|
typedef struct {
|
|
|
|
cubeb_sample_format format; /**< Requested sample format. One of
|
|
|
|
#cubeb_sample_format. */
|
|
|
|
unsigned int rate; /**< Requested sample rate. Valid range is [1, 192000]. */
|
|
|
|
unsigned int channels; /**< Requested channel count. Valid range is [1, 32]. */
|
2013-03-12 03:46:32 +00:00
|
|
|
#if defined(__ANDROID__)
|
|
|
|
cubeb_stream_type stream_type; /**< Used to map Android audio stream types */
|
|
|
|
#endif
|
2012-01-13 01:38:39 +00:00
|
|
|
} cubeb_stream_params;
|
|
|
|
|
2014-07-29 16:45:03 +00:00
|
|
|
/** Output device description */
|
|
|
|
typedef struct {
|
2014-07-18 17:21:27 +00:00
|
|
|
char * output_name; /**< The name of the output device */
|
|
|
|
char * input_name; /**< The name of the input device */
|
|
|
|
} cubeb_device;
|
2014-07-29 16:45:03 +00:00
|
|
|
|
2012-01-13 01:38:39 +00:00
|
|
|
/** Stream states signaled via state_callback. */
|
|
|
|
typedef enum {
|
|
|
|
CUBEB_STATE_STARTED, /**< Stream started. */
|
|
|
|
CUBEB_STATE_STOPPED, /**< Stream stopped. */
|
2012-04-16 03:00:40 +00:00
|
|
|
CUBEB_STATE_DRAINED, /**< Stream drained. */
|
|
|
|
CUBEB_STATE_ERROR /**< Stream disabled due to error. */
|
2012-01-13 01:38:39 +00:00
|
|
|
} cubeb_state;
|
|
|
|
|
|
|
|
/** Result code enumeration. */
|
|
|
|
enum {
|
2013-03-08 04:51:59 +00:00
|
|
|
CUBEB_OK = 0, /**< Success. */
|
|
|
|
CUBEB_ERROR = -1, /**< Unclassified error. */
|
|
|
|
CUBEB_ERROR_INVALID_FORMAT = -2, /**< Unsupported #cubeb_stream_params requested. */
|
|
|
|
CUBEB_ERROR_INVALID_PARAMETER = -3 /**< Invalid parameter specified. */
|
2012-01-13 01:38:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/** User supplied data callback.
|
|
|
|
@param stream
|
|
|
|
@param user_ptr
|
|
|
|
@param buffer
|
|
|
|
@param nframes
|
|
|
|
@retval Number of frames written to buffer, which must equal nframes except at end of stream.
|
|
|
|
@retval CUBEB_ERROR on error, in which case the data callback will stop
|
|
|
|
and the stream will enter a shutdown state. */
|
|
|
|
typedef long (* cubeb_data_callback)(cubeb_stream * stream,
|
|
|
|
void * user_ptr,
|
|
|
|
void * buffer,
|
|
|
|
long nframes);
|
|
|
|
|
|
|
|
/** User supplied state callback.
|
|
|
|
@param stream
|
|
|
|
@param user_ptr
|
2012-07-16 21:15:24 +00:00
|
|
|
@param state */
|
|
|
|
typedef void (* cubeb_state_callback)(cubeb_stream * stream,
|
|
|
|
void * user_ptr,
|
|
|
|
cubeb_state state);
|
2012-01-13 01:38:39 +00:00
|
|
|
|
2014-07-29 16:45:03 +00:00
|
|
|
/**
|
|
|
|
* User supplied callback called when the underlying device changed.
|
|
|
|
* @param user */
|
|
|
|
typedef void (* cubeb_device_changed_callback)(void * user_ptr);
|
|
|
|
|
2012-01-13 01:38:39 +00:00
|
|
|
/** Initialize an application context. This will perform any library or
|
|
|
|
application scoped initialization.
|
|
|
|
@param context
|
|
|
|
@param context_name
|
|
|
|
@retval CUBEB_OK
|
|
|
|
@retval CUBEB_ERROR */
|
|
|
|
int cubeb_init(cubeb ** context, char const * context_name);
|
|
|
|
|
2012-07-16 21:15:24 +00:00
|
|
|
/** Get a read-only string identifying this context's current backend.
|
|
|
|
@param context
|
|
|
|
@retval Read-only string identifying current backend. */
|
|
|
|
char const * cubeb_get_backend_id(cubeb * context);
|
|
|
|
|
2013-06-10 17:32:28 +00:00
|
|
|
/** Get the maximum possible number of channels.
|
|
|
|
@param context
|
|
|
|
@param max_channels The maximum number of channels.
|
|
|
|
@retval CUBEB_OK
|
|
|
|
@retval CUBEB_ERROR_INVALID_PARAMETER
|
|
|
|
@retval CUBEB_ERROR */
|
|
|
|
int cubeb_get_max_channel_count(cubeb * context, uint32_t * max_channels);
|
|
|
|
|
2013-10-17 13:44:52 +00:00
|
|
|
/** Get the minimal latency value, in milliseconds, that is guaranteed to work
|
2013-10-17 13:44:52 +00:00
|
|
|
when creating a stream for the specified sample rate. This is platform and
|
2013-10-17 13:44:52 +00:00
|
|
|
backend dependant.
|
|
|
|
@param context
|
|
|
|
@param params On some backends, the minimum achievable latency depends on
|
|
|
|
the characteristics of the stream.
|
2014-09-29 00:19:09 +00:00
|
|
|
@param latency_ms The latency value, in ms, to pass to cubeb_stream_init.
|
2013-10-17 13:44:52 +00:00
|
|
|
@retval CUBEB_ERROR_INVALID_PARAMETER
|
|
|
|
@retval CUBEB_OK */
|
|
|
|
int cubeb_get_min_latency(cubeb * context, cubeb_stream_params params, uint32_t * latency_ms);
|
|
|
|
|
2013-10-17 13:44:52 +00:00
|
|
|
/** Get the preferred sample rate for this backend: this is hardware and platform
|
|
|
|
dependant, and can avoid resampling, and/or trigger fastpaths.
|
|
|
|
@param context
|
2014-09-29 00:19:09 +00:00
|
|
|
@param rate The samplerate (in Hz) the current configuration prefers.
|
2013-10-17 13:44:52 +00:00
|
|
|
@return CUBEB_ERROR_INVALID_PARAMETER
|
|
|
|
@return CUBEB_OK */
|
|
|
|
int cubeb_get_preferred_sample_rate(cubeb * context, uint32_t * rate);
|
|
|
|
|
2012-01-13 01:38:39 +00:00
|
|
|
/** Destroy an application context.
|
|
|
|
@param context */
|
|
|
|
void cubeb_destroy(cubeb * context);
|
|
|
|
|
|
|
|
/** Initialize a stream associated with the supplied application context.
|
|
|
|
@param context
|
|
|
|
@param stream
|
|
|
|
@param stream_name
|
|
|
|
@param stream_params
|
|
|
|
@param latency Approximate stream latency in milliseconds. Valid range is [1, 2000].
|
|
|
|
@param data_callback Will be called to preroll data before playback is
|
|
|
|
started by cubeb_stream_start.
|
|
|
|
@param state_callback
|
|
|
|
@param user_ptr
|
|
|
|
@retval CUBEB_OK
|
|
|
|
@retval CUBEB_ERROR
|
|
|
|
@retval CUBEB_ERROR_INVALID_FORMAT */
|
|
|
|
int cubeb_stream_init(cubeb * context, cubeb_stream ** stream, char const * stream_name,
|
|
|
|
cubeb_stream_params stream_params, unsigned int latency,
|
|
|
|
cubeb_data_callback data_callback,
|
|
|
|
cubeb_state_callback state_callback,
|
|
|
|
void * user_ptr);
|
|
|
|
|
|
|
|
/** Destroy a stream.
|
|
|
|
@param stream */
|
|
|
|
void cubeb_stream_destroy(cubeb_stream * stream);
|
|
|
|
|
|
|
|
/** Start playback.
|
|
|
|
@param stream
|
|
|
|
@retval CUBEB_OK
|
|
|
|
@retval CUBEB_ERROR */
|
|
|
|
int cubeb_stream_start(cubeb_stream * stream);
|
|
|
|
|
|
|
|
/** Stop playback.
|
|
|
|
@param stream
|
|
|
|
@retval CUBEB_OK
|
|
|
|
@retval CUBEB_ERROR */
|
|
|
|
int cubeb_stream_stop(cubeb_stream * stream);
|
|
|
|
|
|
|
|
/** Get the current stream playback position.
|
|
|
|
@param stream
|
|
|
|
@param position Playback position in frames.
|
|
|
|
@retval CUBEB_OK
|
|
|
|
@retval CUBEB_ERROR */
|
|
|
|
int cubeb_stream_get_position(cubeb_stream * stream, uint64_t * position);
|
|
|
|
|
2013-09-17 06:39:30 +00:00
|
|
|
/** Get the latency for this stream, in frames. This is the number of frames
|
|
|
|
between the time cubeb acquires the data in the callback and the listener
|
|
|
|
can hear the sound.
|
|
|
|
@param stream
|
2014-06-16 23:45:00 +00:00
|
|
|
@param latency Current approximate stream latency in frames.
|
2013-09-17 06:39:30 +00:00
|
|
|
@retval CUBEB_OK
|
|
|
|
@retval CUBEB_ERROR */
|
|
|
|
int cubeb_stream_get_latency(cubeb_stream * stream, uint32_t * latency);
|
|
|
|
|
2014-07-29 16:45:02 +00:00
|
|
|
/**
|
|
|
|
* Set the volume for a stream.
|
|
|
|
* @param stream the stream for which to adjust the volume.
|
2014-09-29 00:19:09 +00:00
|
|
|
* @param volume a float between 0.0 (muted) and 1.0 (maximum volume)
|
2014-07-29 16:45:02 +00:00
|
|
|
* @return CUBEB_ERROR_INVALID_PARAMETER if volume is outside [0.0; 1.0]
|
|
|
|
* @return CUBEB_ERROR_INVALID_PARAMETER if stream is an invalid pointer
|
|
|
|
* @return CUBEB_OK otherwise
|
|
|
|
*/
|
|
|
|
int cubeb_stream_set_volume(cubeb_stream * stream, float volume);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If the stream is stereo, set the left/right panning. If the stream is mono,
|
|
|
|
* this has no effect.
|
|
|
|
* @param stream the stream for which to change the panning
|
|
|
|
* @param panning a number from -1.0 to 1.0. -1.0 means that the stream is fully
|
|
|
|
* mixed in the left channel, 1.0 means the stream is fully mixed in the right
|
|
|
|
* channel. 0.0 is equal power in the right and left channel (default).
|
|
|
|
* @return CUBEB_ERROR_INVALID_PARAMETER if stream is null or if panning is outside
|
|
|
|
* the [-1.0; 1.0] range.
|
|
|
|
* @return CUBEB_ERROR if this stream is not mono nor stereo.
|
|
|
|
* @return CUBEB_OK otherwise.
|
|
|
|
*/
|
|
|
|
int cubeb_stream_set_panning(cubeb_stream * stream, float panning);
|
|
|
|
|
2014-07-29 16:45:03 +00:00
|
|
|
/**
|
|
|
|
* Get the current output device for this stream.
|
|
|
|
* @param stm the stream for which to query the current output device
|
|
|
|
* @param device a pointer in which the current output device will be stored.
|
|
|
|
* @return CUBEB_OK in case of success
|
|
|
|
* @return CUBEB_ERROR_INVALID_PARAMETER if either stm, device or count are
|
|
|
|
* invalid pointers
|
|
|
|
*/
|
2014-07-18 17:21:27 +00:00
|
|
|
int cubeb_stream_get_current_device(cubeb_stream * stm,
|
|
|
|
cubeb_device ** const device);
|
2014-07-29 16:45:03 +00:00
|
|
|
|
|
|
|
/**
|
2014-07-18 17:21:27 +00:00
|
|
|
* Destroy a cubeb_device structure.
|
|
|
|
* @param stream the stream passed in cubeb_stream_get_current_device
|
2014-07-29 16:45:03 +00:00
|
|
|
* @param devices the devices to destroy
|
|
|
|
* @return CUBEB_OK in case of success
|
|
|
|
* @return CUBEB_ERROR_INVALID_PARAMETER if devices is an invalid pointer
|
|
|
|
*/
|
2014-07-18 17:21:27 +00:00
|
|
|
int cubeb_stream_device_destroy(cubeb_stream * stream,
|
|
|
|
cubeb_device * devices);
|
2014-07-29 16:45:03 +00:00
|
|
|
|
2014-07-29 16:45:03 +00:00
|
|
|
/**
|
|
|
|
* Set a callback to be notified when the output device changes.
|
|
|
|
* @param stream the stream for which to set the callback.
|
|
|
|
* @param device_changed_callback a function called whenever the device has
|
|
|
|
* changed. Passing NULL allow to unregister a function
|
|
|
|
* @return CUBEB_ERROR_INVALID_PARAMETER if either stream or
|
|
|
|
* device_changed_callback are invalid pointers.
|
|
|
|
* @return CUBEB_OK
|
|
|
|
*/
|
|
|
|
int cubeb_stream_register_device_changed_callback(cubeb_stream * stream,
|
|
|
|
cubeb_device_changed_callback device_changed_callback);
|
|
|
|
|
2013-03-13 18:36:46 +00:00
|
|
|
#if defined(__cplusplus)
|
2012-01-13 01:38:39 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* CUBEB_c2f983e9_c96f_e71c_72c3_bbf62992a382 */
|