| 1 | /* |
| 2 | This is a simple API for low-level audio playback and capture. A reference implementation using |
| 3 | miniaudio is provided in osaudio.c which can be found alongside this file. Consider all code |
| 4 | public domain. |
| 5 | |
| 6 | The idea behind this project came about after considering the absurd complexity of audio APIs on |
| 7 | various platforms after years of working on miniaudio. This project aims to disprove the idea that |
| 8 | complete and flexible audio solutions and simple APIs are mutually exclusive and that it's possible |
| 9 | to have both. The idea of reliability through simplicity is the first and foremost goal of this |
| 10 | project. The difference between this project and miniaudio is that this project is designed around |
| 11 | the idea of what I would build if I was building an audio API for an operating system, such as at |
| 12 | the level of WASAPI or ALSA. A cross-platform and cross-backend library like miniaudio is |
| 13 | necessarily different in design, but there are indeed things that I would have done differently if |
| 14 | given my time again, some of those ideas of which I'm expressing in this project. |
| 15 | |
| 16 | --- |
| 17 | |
| 18 | The concept of low-level audio is simple - you have a device, such as a speaker system or a |
| 19 | micrphone system, and then you write or read audio data to/from it. So in the case of playback, you |
| 20 | need only write your raw audio data to the device which then emits it from the speakers when it's |
| 21 | ready. Likewise, for capture you simply read audio data from the device which is filled with data |
| 22 | by the microphone. |
| 23 | |
| 24 | A complete low-level audio solution requires the following: |
| 25 | |
| 26 | 1) The ability to enumerate devices that are connected to the system. |
| 27 | 2) The ability to open and close a connection to a device. |
| 28 | 3) The ability to start and stop the device. |
| 29 | 4) The ability to write and read audio data to/from the device. |
| 30 | 5) The ability to query the device for its data configuration. |
| 31 | 6) The ability to notify the application when certain events occur, such as the device being |
| 32 | stopped, or rerouted. |
| 33 | |
| 34 | The API presented here aims to meet all of the above requirements. It uses a single-threaded |
| 35 | blocking read/write model for data delivery instead of a callback model. This makes it a bit more |
| 36 | flexible since it gives the application full control over the audio thread. It might also make it |
| 37 | more feasible to use this API on single-threaded systems. |
| 38 | |
| 39 | Device enumeration is achieved with a single function: osaudio_enumerate(). This function returns |
| 40 | an array of osaudio_info_t structures which contain information about each device. The array is |
| 41 | allocated must be freed with free(). Contained within the osaudio_info_t struct is, most |
| 42 | importantly, the device ID, which is used to open a connection to the device, and the name of the |
| 43 | device which can be used to display to the user. For advanced users, it also includes information |
| 44 | about the device's native data configuration. |
| 45 | |
| 46 | Opening and closing a connection to a device is achieved with osaudio_open() and osaudio_close(). |
| 47 | An important concept is that of the ability to configure the device. This is achieved with the |
| 48 | osaudio_config_t structure which is passed to osaudio_open(). In addition to the ID of the device, |
| 49 | this structure includes information about the desired format, channel count and sample rate. You |
| 50 | can also configure the latency of the device, or the buffer size, which is specified in frames. A |
| 51 | flags member is used for specifying additional options, such as whether or not to disable automatic |
| 52 | rerouting. Finally a callback can be specified for notifications. When osaudio_open() returns, the |
| 53 | config structure will be filled with the device's actual configuration. You can inspect the channel |
| 54 | map from this structure to know how to arrange the channels in your audio data. |
| 55 | |
| 56 | This API uses a blocking write/read model for pushing and pulling data to/from the device. This |
| 57 | is done with the osaudio_write() and osaudio_read() functions. These functions will block until |
| 58 | the requested number of frames have been processed or the device is drained or flushed with |
| 59 | osaudio_drain() or osaudio_flush() respectively. It is from these functions that the device is |
| 60 | started. As soon as you start writing data with osaudio_write() or reading data with |
| 61 | osaudio_read(), the device will start. When the device is drained of flushed with osaudio_drain() |
| 62 | or osaudio_flush(), the device will be stopped. osaudio_drain() will block until the device has |
| 63 | been drained, whereas osaudio_flush() will stop playback immediately and return. You can also pause |
| 64 | and resume the device with osaudio_pause() and osaudio_resume(). Since reading and writing is |
| 65 | blocking, it can be useful to know how many frames can be written/read without blocking. This is |
| 66 | achieved with osaudio_get_avail(). |
| 67 | |
| 68 | Querying the device's configuration is achieved with osaudio_get_info(). This function will return |
| 69 | a pointer to an osaudio_info_t structure which contains information about the device, most |
| 70 | importantly its name and data configuration. The name is important for displaying on a UI, and |
| 71 | the data configuration is important for knowing how to format your audio data. The osaudio_info_t |
| 72 | structure will contain an array of osaudio_config_t structures. This will contain one entry, which |
| 73 | will contain the exact information that was returned in the config structure that was passed to |
| 74 | osaudio_open(). |
| 75 | |
| 76 | A common requirement is to open a device that represents the operating system's default device. |
| 77 | This is done easily by simply passing in NULL for the device ID. Below is an example for opening a |
| 78 | default device: |
| 79 | |
| 80 | int result; |
| 81 | osaudio_t audio; |
| 82 | osaudio_config_t config; |
| 83 | |
| 84 | osaudio_config_init(&config, OSAUDIO_OUTPUT); |
| 85 | config.format = OSAUDIO_FORMAT_F32; |
| 86 | config.channels = 2; |
| 87 | config.rate = 48000; |
| 88 | |
| 89 | result = osaudio_open(&audio, &config); |
| 90 | if (result != OSAUDIO_SUCCESS) { |
| 91 | printf("Failed to open device."); |
| 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | ... |
| 96 | |
| 97 | osaudio_close(audio); |
| 98 | |
| 99 | In the above example, the default device is opened for playback (OSAUDIO_OUTPUT). The format is |
| 100 | set to 32-bit floating point (OSAUDIO_FORMAT_F32), the channel count is set to stereo (2), and the |
| 101 | sample rate is set to 48kHz. The device is then closed when we're done with it. |
| 102 | |
| 103 | If instead we wanted to open a specific device, we can do that by passing in the device ID. Below |
| 104 | is an example for how to do this: |
| 105 | |
| 106 | int result; |
| 107 | osaudio_t audio; |
| 108 | osaudio_config_t config; |
| 109 | unsigned int infoCount; |
| 110 | osaudio_info_t* info; |
| 111 | |
| 112 | result = osaudio_enumerate(&infoCount, &info); |
| 113 | if (result != OSAUDIO_SUCCESS) { |
| 114 | printf("Failed to enumerate devices.\n"); |
| 115 | return -1; |
| 116 | } |
| 117 | |
| 118 | // ... Iterate over the `info` array and find the device you want to open. Use the `direction` member to discriminate between input and output ... |
| 119 | |
| 120 | osaudio_config_init(&config, OSAUDIO_OUTPUT); |
| 121 | config.id = &info[indexOfYourChosenDevice].id; |
| 122 | config.format = OSAUDIO_FORMAT_F32; |
| 123 | config.channels = 2; |
| 124 | config.rate = 48000; |
| 125 | |
| 126 | osaudio_open(&audio, &config); |
| 127 | |
| 128 | ... |
| 129 | |
| 130 | osaudio_close(audio); |
| 131 | free(info); // The pointer returned by osaudio_enumerate() must be freed with free(). |
| 132 | |
| 133 | The id structure is just a 256 byte array that uniquely identifies the device. Implementations may |
| 134 | have different representations for device IDs, and A 256 byte array should accommodates all |
| 135 | device ID representations. Implementations are required to zero-fill unused bytes. The osaudio_id_t |
| 136 | structure can be copied which makes it suitable for serialization and deserialization in situations |
| 137 | where you may want to save the device ID to permanent storage so it can be stored in a config file. |
| 138 | |
| 139 | Implementations need to do their own data conversion between the device's native data configuration |
| 140 | and the requested configuration. In this case, when the format, channels and rate are specified in |
| 141 | the config, they should be unchanged when osaudio_open() returns. If this is not possible, |
| 142 | osaudio_open() will return OSAUDIO_FORMAT_NOT_SUPPORTED. However, there are cases where it's useful |
| 143 | for a program to use the device's native configuration instead of some fixed configuration. This is |
| 144 | achieved by setting the format, channels and rate to 0. Below is an example: |
| 145 | |
| 146 | int result; |
| 147 | osaudio_t audio; |
| 148 | osaudio_config_t config; |
| 149 | |
| 150 | osaudio_config_init(&config, OSAUDIO_OUTPUT); |
| 151 | |
| 152 | result = osaudio_open(&audio, &config); |
| 153 | if (result != OSAUDIO_SUCCESS) { |
| 154 | printf("Failed to open device."); |
| 155 | return -1; |
| 156 | } |
| 157 | |
| 158 | // ... `config` will have been updated by osaudio_open() to contain the *actual* format/channels/rate ... |
| 159 | |
| 160 | osaudio_close(audio); |
| 161 | |
| 162 | In addition to the code above, you can explicitly call `osaudio_get_info()` to retrieve the format |
| 163 | configuration. If you need to know the native configuration before opening the device, you can use |
| 164 | enumeration. The format, channels and rate will be continued in the first item in the configs array. |
| 165 | |
| 166 | The examples above all use playback, but the same applies for capture. The only difference is that |
| 167 | the direction is set to OSAUDIO_INPUT instead of OSAUDIO_OUTPUT. |
| 168 | |
| 169 | To output audio from the speakers you need to call osaudio_write(). Likewise, to capture audio from |
| 170 | a microphone you need to call osaudio_read(). These functions will block until the requested number |
| 171 | of frames have been written or read. The device will start automatically. Below is an example for |
| 172 | writing some data to a device: |
| 173 | |
| 174 | int result = osaudio_write(audio, myAudioData, myAudioDataFrameCount); |
| 175 | if (result == OSAUDIO_SUCCESS) { |
| 176 | printf("Successfully wrote %d frames of audio data.\n", myAudioDataFrameCount); |
| 177 | } else { |
| 178 | printf("Failed to write audio data.\n"); |
| 179 | } |
| 180 | |
| 181 | osaudio_write() and osaudio_read() will return OSAUDIO_SUCCESS if the requested number of frames |
| 182 | were written or read. You cannot call osaudio_close() while a write or read operation is in |
| 183 | progress. |
| 184 | |
| 185 | If you want to write or read audio data without blocking, you can use osaudio_get_avail() to |
| 186 | determine how many frames are available for writing or reading. Below is an example: |
| 187 | |
| 188 | unsigned int framesAvailable = osaudio_get_avail(audio); |
| 189 | if (result > 0) { |
| 190 | printf("There are %d frames available for writing.\n", framesAvailable); |
| 191 | } else { |
| 192 | printf("There are no frames available for writing.\n"); |
| 193 | } |
| 194 | |
| 195 | If you want to abort a blocking write or read, you can use osaudio_flush(). This will result in any |
| 196 | pending write or read operation being aborted. |
| 197 | |
| 198 | There are several ways of pausing a device. The first is to just drain or flush the device and |
| 199 | simply don't do any more read/write operations. A drain and flush will put the device into a |
| 200 | stopped state until the next call to either read or write, depending on the device's direction. |
| 201 | If, however, this does not suit your requirements, you can use osaudio_pause() and |
| 202 | osaudio_resume(). Take note, however, that these functions will result in osaudio_drain() never |
| 203 | returning because it'll result in the device being in a stopped state which in turn results in the |
| 204 | buffer never being read and therefore never drained. |
| 205 | |
| 206 | Everything is thread safe with a few minor exceptions which has no practical issues for the client: |
| 207 | |
| 208 | * You cannot call any function while osaudio_open() is still in progress. |
| 209 | * You cannot call osaudio_close() while any other function is still in progress. |
| 210 | * You can only call osaudio_write() and osaudio_read() from one thread at a time. |
| 211 | |
| 212 | None of these issues should be a problem for the client in practice. You won't have a valid |
| 213 | osaudio_t object until osaudio_open() has returned. For osaudio_close(), it makes no sense to |
| 214 | destroy the object while it's still in use, and doing so would mean the client is using very poor |
| 215 | form. For osaudio_write() and osaudio_read(), you wouldn't ever want to call this simultaneously |
| 216 | across multiple threads anyway because otherwise you'd end up with garbage audio. |
| 217 | |
| 218 | The rules above only apply when working with a single osaudio_t object. You can have multiple |
| 219 | osaudio_t objects open at the same time, and you can call any function on different osaudio_t |
| 220 | objects simultaneously from different threads. |
| 221 | |
| 222 | --- |
| 223 | |
| 224 | # Feedback |
| 225 | |
| 226 | I'm looking for feedback on the following: |
| 227 | |
| 228 | * Are the supported formats enough? If not, what other formats are needed, and what is the |
| 229 | justification for including it? Just because it's the native format on one particular |
| 230 | piece of hardware is not enough. Big-endian and little-endian will never be supported. All |
| 231 | formats are native-endian. |
| 232 | * Are the available channel positions enough? What other positions are needed? |
| 233 | * Just some general criticism would be appreciated. |
| 234 | |
| 235 | */ |
| 236 | #ifndef osaudio_h |
| 237 | #define osaudio_h |
| 238 | |
| 239 | #ifdef __cplusplus |
| 240 | extern "C" { |
| 241 | #endif |
| 242 | |
| 243 | /* |
| 244 | Support far pointers on relevant platforms (DOS, in particular). The version of this file |
| 245 | distributed with an operating system wouldn't need this because they would just have an |
| 246 | OS-specific version of this file, but as a reference it's useful to use far pointers here. |
| 247 | */ |
| 248 | #if defined(__MSDOS__) || defined(_MSDOS) || defined(__DOS__) |
| 249 | #define OSAUDIO_FAR far |
| 250 | #else |
| 251 | #define OSAUDIO_FAR |
| 252 | #endif |
| 253 | |
| 254 | typedef struct _osaudio_t* osaudio_t; |
| 255 | typedef struct osaudio_config_t osaudio_config_t; |
| 256 | typedef struct osaudio_id_t osaudio_id_t; |
| 257 | typedef struct osaudio_info_t osaudio_info_t; |
| 258 | typedef struct osaudio_notification_t osaudio_notification_t; |
| 259 | |
| 260 | /* Results codes. */ |
| 261 | typedef int osaudio_result_t; |
| 262 | #define OSAUDIO_SUCCESS 0 |
| 263 | #define OSAUDIO_ERROR -1 |
| 264 | #define OSAUDIO_INVALID_ARGS -2 |
| 265 | #define OSAUDIO_INVALID_OPERATION -3 |
| 266 | #define OSAUDIO_OUT_OF_MEMORY -4 |
| 267 | #define OSAUDIO_FORMAT_NOT_SUPPORTED -101 /* The requested format is not supported. */ |
| 268 | #define OSAUDIO_XRUN -102 /* An underrun or overrun occurred. Can be returned by osaudio_read() or osaudio_write(). */ |
| 269 | #define OSAUDIO_DEVICE_STOPPED -103 /* The device is stopped. Can be returned by osaudio_drain(). It is invalid to call osaudio_drain() on a device that is not running because otherwise it'll get stuck. */ |
| 270 | |
| 271 | /* Directions. Cannot be combined. Use separate osaudio_t objects for bidirectional setups. */ |
| 272 | typedef int osaudio_direction_t; |
| 273 | #define OSAUDIO_INPUT 1 |
| 274 | #define OSAUDIO_OUTPUT 2 |
| 275 | |
| 276 | /* All formats are native endian and interleaved. */ |
| 277 | typedef int osaudio_format_t; |
| 278 | #define OSAUDIO_FORMAT_UNKNOWN 0 |
| 279 | #define OSAUDIO_FORMAT_F32 1 |
| 280 | #define OSAUDIO_FORMAT_U8 2 |
| 281 | #define OSAUDIO_FORMAT_S16 3 |
| 282 | #define OSAUDIO_FORMAT_S24 4 /* Tightly packed. */ |
| 283 | #define OSAUDIO_FORMAT_S32 5 |
| 284 | |
| 285 | /* Channel positions. */ |
| 286 | typedef unsigned char osaudio_channel_t; |
| 287 | #define OSAUDIO_CHANNEL_NONE 0 |
| 288 | #define OSAUDIO_CHANNEL_MONO 1 |
| 289 | #define OSAUDIO_CHANNEL_FL 2 |
| 290 | #define OSAUDIO_CHANNEL_FR 3 |
| 291 | #define OSAUDIO_CHANNEL_FC 4 |
| 292 | #define OSAUDIO_CHANNEL_LFE 5 |
| 293 | #define OSAUDIO_CHANNEL_BL 6 |
| 294 | #define OSAUDIO_CHANNEL_BR 7 |
| 295 | #define OSAUDIO_CHANNEL_FLC 8 |
| 296 | #define OSAUDIO_CHANNEL_FRC 9 |
| 297 | #define OSAUDIO_CHANNEL_BC 10 |
| 298 | #define OSAUDIO_CHANNEL_SL 11 |
| 299 | #define OSAUDIO_CHANNEL_SR 12 |
| 300 | #define OSAUDIO_CHANNEL_TC 13 |
| 301 | #define OSAUDIO_CHANNEL_TFL 14 |
| 302 | #define OSAUDIO_CHANNEL_TFC 15 |
| 303 | #define OSAUDIO_CHANNEL_TFR 16 |
| 304 | #define OSAUDIO_CHANNEL_TBL 17 |
| 305 | #define OSAUDIO_CHANNEL_TBC 18 |
| 306 | #define OSAUDIO_CHANNEL_TBR 19 |
| 307 | #define OSAUDIO_CHANNEL_AUX0 20 |
| 308 | #define OSAUDIO_CHANNEL_AUX1 21 |
| 309 | #define OSAUDIO_CHANNEL_AUX2 22 |
| 310 | #define OSAUDIO_CHANNEL_AUX3 23 |
| 311 | #define OSAUDIO_CHANNEL_AUX4 24 |
| 312 | #define OSAUDIO_CHANNEL_AUX5 25 |
| 313 | #define OSAUDIO_CHANNEL_AUX6 26 |
| 314 | #define OSAUDIO_CHANNEL_AUX7 27 |
| 315 | #define OSAUDIO_CHANNEL_AUX8 28 |
| 316 | #define OSAUDIO_CHANNEL_AUX9 29 |
| 317 | #define OSAUDIO_CHANNEL_AUX10 30 |
| 318 | #define OSAUDIO_CHANNEL_AUX11 31 |
| 319 | #define OSAUDIO_CHANNEL_AUX12 32 |
| 320 | #define OSAUDIO_CHANNEL_AUX13 33 |
| 321 | #define OSAUDIO_CHANNEL_AUX14 34 |
| 322 | #define OSAUDIO_CHANNEL_AUX15 35 |
| 323 | #define OSAUDIO_CHANNEL_AUX16 36 |
| 324 | #define OSAUDIO_CHANNEL_AUX17 37 |
| 325 | #define OSAUDIO_CHANNEL_AUX18 38 |
| 326 | #define OSAUDIO_CHANNEL_AUX19 39 |
| 327 | #define OSAUDIO_CHANNEL_AUX20 40 |
| 328 | #define OSAUDIO_CHANNEL_AUX21 41 |
| 329 | #define OSAUDIO_CHANNEL_AUX22 42 |
| 330 | #define OSAUDIO_CHANNEL_AUX23 43 |
| 331 | #define OSAUDIO_CHANNEL_AUX24 44 |
| 332 | #define OSAUDIO_CHANNEL_AUX25 45 |
| 333 | #define OSAUDIO_CHANNEL_AUX26 46 |
| 334 | #define OSAUDIO_CHANNEL_AUX27 47 |
| 335 | #define OSAUDIO_CHANNEL_AUX28 48 |
| 336 | #define OSAUDIO_CHANNEL_AUX29 49 |
| 337 | #define OSAUDIO_CHANNEL_AUX30 50 |
| 338 | #define OSAUDIO_CHANNEL_AUX31 51 |
| 339 | |
| 340 | /* The maximum number of channels supported. */ |
| 341 | #define OSAUDIO_MAX_CHANNELS 64 |
| 342 | |
| 343 | /* Notification types. */ |
| 344 | typedef int osaudio_notification_type_t; |
| 345 | #define OSAUDIO_NOTIFICATION_STARTED 0 /* The device was started in response to a call to osaudio_write() or osaudio_read(). */ |
| 346 | #define OSAUDIO_NOTIFICATION_STOPPED 1 /* The device was stopped in response to a call to osaudio_drain() or osaudio_flush(). */ |
| 347 | #define OSAUDIO_NOTIFICATION_REROUTED 2 /* The device was rerouted. Not all implementations need to support rerouting. */ |
| 348 | #define OSAUDIO_NOTIFICATION_INTERRUPTION_BEGIN 3 /* The device was interrupted due to something like a phone call. */ |
| 349 | #define OSAUDIO_NOTIFICATION_INTERRUPTION_END 4 /* The interruption has been ended. */ |
| 350 | |
| 351 | /* Flags. */ |
| 352 | #define OSAUDIO_FLAG_NO_REROUTING 1 /* When set, will tell the implementation to disable automatic rerouting if possible. This is a hint and may be ignored by the implementation. */ |
| 353 | #define OSAUDIO_FLAG_REPORT_XRUN 2 /* When set, will tell the implementation to report underruns and overruns via osaudio_write() and osaudio_read() by aborting and returning OSAUDIO_XRUN. */ |
| 354 | |
| 355 | struct osaudio_notification_t |
| 356 | { |
| 357 | osaudio_notification_type_t type; /* OSAUDIO_NOTIFICATION_* */ |
| 358 | union |
| 359 | { |
| 360 | struct |
| 361 | { |
| 362 | int _unused; |
| 363 | } started; |
| 364 | struct |
| 365 | { |
| 366 | int _unused; |
| 367 | } stopped; |
| 368 | struct |
| 369 | { |
| 370 | int _unused; |
| 371 | } rerouted; |
| 372 | struct |
| 373 | { |
| 374 | int _unused; |
| 375 | } interruption; |
| 376 | } data; |
| 377 | }; |
| 378 | |
| 379 | struct osaudio_id_t |
| 380 | { |
| 381 | char data[256]; |
| 382 | }; |
| 383 | |
| 384 | struct osaudio_config_t |
| 385 | { |
| 386 | osaudio_id_t* device_id; /* Set to NULL to use default device. When non-null, automatic routing will be disabled. */ |
| 387 | osaudio_direction_t direction; /* OSAUDIO_INPUT or OSAUDIO_OUTPUT. Cannot be combined. Use separate osaudio_t objects for bidirectional setups. */ |
| 388 | osaudio_format_t format; /* OSAUDIO_FORMAT_* */ |
| 389 | unsigned int channels; /* Number of channels. */ |
| 390 | unsigned int rate; /* Sample rate in seconds. */ |
| 391 | osaudio_channel_t channel_map[OSAUDIO_MAX_CHANNELS]; /* Leave all items set to 0 for defaults. */ |
| 392 | unsigned int buffer_size; /* In frames. Set to 0 to use the system default. */ |
| 393 | unsigned int flags; /* A combination of OSAUDIO_FLAG_* */ |
| 394 | void (* notification)(void* user_data, const osaudio_notification_t* notification); /* Called when some kind of event occurs, such as a device being closed. Never called from the audio thread. */ |
| 395 | void* user_data; /* Passed to notification(). */ |
| 396 | }; |
| 397 | |
| 398 | struct osaudio_info_t |
| 399 | { |
| 400 | osaudio_id_t id; |
| 401 | char name[256]; |
| 402 | osaudio_direction_t direction; /* OSAUDIO_INPUT or OSAUDIO_OUTPUT. */ |
| 403 | unsigned int config_count; |
| 404 | osaudio_config_t* configs; |
| 405 | }; |
| 406 | |
| 407 | |
| 408 | /* |
| 409 | Enumerates the available devices. |
| 410 | |
| 411 | On output, `count` will contain the number of items in the `info` array. The array must be freed |
| 412 | with free() when it's no longer needed. |
| 413 | |
| 414 | Use the `direction` member to discriminate between input and output devices. Below is an example: |
| 415 | |
| 416 | unsigned int count; |
| 417 | osaudio_info_t* info; |
| 418 | osaudio_enumerate(&count, &info); |
| 419 | |
| 420 | for (int i = 0; i < count; ++i) { |
| 421 | if (info[i].direction == OSAUDIO_OUTPUT) { |
| 422 | printf("Output device: %s\n", info[i].name); |
| 423 | } else { |
| 424 | printf("Input device: %s\n", info[i].name); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | You can use the `id` member to open a specific device with osaudio_open(). You do not need to do |
| 429 | device enumeration if you only want to open the default device. |
| 430 | */ |
| 431 | osaudio_result_t osaudio_enumerate(unsigned int* count, osaudio_info_t** info); |
| 432 | |
| 433 | /* |
| 434 | Initializes a default config. |
| 435 | |
| 436 | The config object will be cleared to zero, with the direction set to `direction`. This will result |
| 437 | in a configuration that uses the device's native format, channels and rate. |
| 438 | |
| 439 | osaudio_config_t is a transparent struct. Just set the relevant fields to the desired values after |
| 440 | calling this function. Example: |
| 441 | |
| 442 | osaudio_config_t config; |
| 443 | osaudio_config_init(&config, OSAUDIO_OUTPUT); |
| 444 | config.format = OSAUDIO_FORMAT_F32; |
| 445 | config.channels = 2; |
| 446 | config.rate = 48000; |
| 447 | */ |
| 448 | void osaudio_config_init(osaudio_config_t* config, osaudio_direction_t direction); |
| 449 | |
| 450 | /* |
| 451 | Opens a connection to a device. |
| 452 | |
| 453 | On input, config must be filled with the desired configuration. On output, it will be filled with |
| 454 | the actual configuration. |
| 455 | |
| 456 | Initialize the config with osaudio_config_init() and then fill in the desired configuration. Below |
| 457 | is an example: |
| 458 | |
| 459 | osaudio_config_t config; |
| 460 | osaudio_config_init(&config, OSAUDIO_OUTPUT); |
| 461 | config.format = OSAUDIO_FORMAT_F32; |
| 462 | config.channels = 2; |
| 463 | config.rate = 48000; |
| 464 | |
| 465 | When the format, channels or rate are left at their default values, or set to 0 (or |
| 466 | OSAUDIO_FORMAT_UNKNOWN for format), the native format, channels or rate will use the device's |
| 467 | native configuration: |
| 468 | |
| 469 | osaudio_config_t config; |
| 470 | osaudio_config_init(&config, OSAUDIO_OUTPUT); |
| 471 | config.format = OSAUDIO_FORMAT_UNKNOWN; |
| 472 | config.channels = 0; |
| 473 | config.rate = 0; |
| 474 | |
| 475 | The code above is equivalent to this: |
| 476 | |
| 477 | osaudio_config_t config; |
| 478 | osaudio_config_init(&config, OSAUDIO_OUTPUT); |
| 479 | |
| 480 | On output the config will be filled with the actual configuration. The implementation will perform |
| 481 | any necessary data conversion between the requested data configuration and the device's native |
| 482 | configuration. If it cannot, the function will return a OSAUDIO_FORMAT_NOT_SUPPORTED error. In this |
| 483 | case the caller can decide to reinitialize the device to use its native configuration and do its |
| 484 | own data conversion, or abort if it cannot do so. Use the channel map to determine the ordering of |
| 485 | your channels. Automatic channel map conversion is not performed - that must be done manually by |
| 486 | the caller when transferring data to/from the device. |
| 487 | |
| 488 | Close the device with osaudio_close(). |
| 489 | |
| 490 | Returns 0 on success, any other error code on failure. |
| 491 | */ |
| 492 | osaudio_result_t osaudio_open(osaudio_t* audio, osaudio_config_t* config); |
| 493 | |
| 494 | /* |
| 495 | Closes a connection to a device. |
| 496 | |
| 497 | As soon as this function is called, the device should be considered invalid and unusable. Do not |
| 498 | attempt to use the audio object once this function has been called. |
| 499 | |
| 500 | It's invalid to call this while any other function is still running. You can use osaudio_flush() to |
| 501 | quickly abort any pending writes or reads. You can also use osaudio_drain() to wait for all pending |
| 502 | writes or reads to complete. |
| 503 | |
| 504 | Returns 0 on success, < 0 on failure. |
| 505 | */ |
| 506 | osaudio_result_t osaudio_close(osaudio_t audio); |
| 507 | |
| 508 | /* |
| 509 | Writes audio data to the device. |
| 510 | |
| 511 | This will block until all data has been written or the device is closed. |
| 512 | |
| 513 | You can only write from a single thread at any given time. If you want to write from multiple |
| 514 | threads, you need to use your own synchronization mechanism. |
| 515 | |
| 516 | This will automatically start the device if frame_count is > 0 and it's not in a paused state. |
| 517 | |
| 518 | Use osaudio_get_avail() to determine how much data can be written without blocking. |
| 519 | |
| 520 | Returns 0 on success, < 0 on failure. |
| 521 | */ |
| 522 | osaudio_result_t osaudio_write(osaudio_t audio, const void OSAUDIO_FAR* data, unsigned int frame_count); |
| 523 | |
| 524 | /* |
| 525 | Reads audio data from the device. |
| 526 | |
| 527 | This will block until the requested number of frames has been read or the device is closed. |
| 528 | |
| 529 | You can only read from a single thread at any given time. If you want to read from multiple |
| 530 | threads, you need to use your own synchronization mechanism. |
| 531 | |
| 532 | This will automatically start the device if frame_count is > 0 and it's not in a paused state. |
| 533 | |
| 534 | Use osaudio_get_avail() to determine how much data can be read without blocking. |
| 535 | |
| 536 | Returns 0 on success, < 0 on failure. |
| 537 | */ |
| 538 | osaudio_result_t osaudio_read(osaudio_t audio, void OSAUDIO_FAR* data, unsigned int frame_count); |
| 539 | |
| 540 | /* |
| 541 | Drains the device. |
| 542 | |
| 543 | This will block until all pending reads or writes have completed. |
| 544 | |
| 545 | If after calling this function another call to osaudio_write() or osaudio_read() is made, the |
| 546 | device will be resumed like normal. |
| 547 | |
| 548 | It is invalid to call this while the device is paused. |
| 549 | |
| 550 | Returns 0 on success, < 0 on failure. |
| 551 | */ |
| 552 | osaudio_result_t osaudio_drain(osaudio_t audio); |
| 553 | |
| 554 | /* |
| 555 | Flushes the device. |
| 556 | |
| 557 | This will immediately flush any pending reads or writes. It will not block. Any in-progress reads |
| 558 | or writes will return immediately. |
| 559 | |
| 560 | If after calling this function another thread starts reading or writing, the device will be resumed |
| 561 | like normal. |
| 562 | |
| 563 | Returns 0 on success, < 0 on failure. |
| 564 | */ |
| 565 | osaudio_result_t osaudio_flush(osaudio_t audio); |
| 566 | |
| 567 | /* |
| 568 | Pauses or resumes the device. |
| 569 | |
| 570 | Pausing a device will trigger a OSAUDIO_NOTIFICATION_STOPPED notification. Resuming a device will |
| 571 | trigger a OSAUDIO_NOTIFICATION_STARTED notification. |
| 572 | |
| 573 | Returns 0 on success, < 0 on failure. |
| 574 | */ |
| 575 | osaudio_result_t osaudio_pause(osaudio_t audio); |
| 576 | |
| 577 | /* |
| 578 | Resumes the device. |
| 579 | |
| 580 | Returns 0 on success, < 0 on failure. |
| 581 | */ |
| 582 | osaudio_result_t osaudio_resume(osaudio_t audio); |
| 583 | |
| 584 | /* |
| 585 | Returns the number of frames that can be read or written without blocking. |
| 586 | */ |
| 587 | unsigned int osaudio_get_avail(osaudio_t audio); |
| 588 | |
| 589 | /* |
| 590 | Gets information about the device. |
| 591 | |
| 592 | There will be one item in the configs array which will contain the device's current configuration, |
| 593 | the contents of which will match that of the config that was returned by osaudio_open(). |
| 594 | |
| 595 | Returns NULL on failure. Do not free the returned pointer. It's up to the implementation to manage |
| 596 | the memory of this object. |
| 597 | */ |
| 598 | const osaudio_info_t* osaudio_get_info(osaudio_t audio); |
| 599 | |
| 600 | |
| 601 | #ifdef __cplusplus |
| 602 | } |
| 603 | #endif |
| 604 | #endif /* osaudio_h */ |