1 /* 2 * Copyright (c) 2015 Andrew Kelley 3 * 4 * This file is part of libsoundio, which is MIT licensed. 5 * See http://opensource.org/licenses/MIT 6 * 7 * Overview: 8 * 9 * libsoundio is a C library for cross-platform audio input and output. It is 10 * suitable for real-time and consumer software. 11 * 12 * Documentation: soundio.h 13 */ 14 module soundio.soundio; 15 16 extern (C): 17 18 /*/** 19 * Examples: 20 * sio_list_devices.c 21 * List the available input and output devices on the system and their 22 * properties. Supports watching for changes and specifying backend to use. 23 */ 24 25 /*/** 26 * Examples: 27 * sio_sine.c 28 * Play a sine wave over the default output device. 29 * Supports specifying device and backend to use. 30 */ 31 32 /*/** 33 * Examples: 34 * sio_record.c 35 * Record audio to an output file. 36 * Supports specifying device and backend to use. 37 */ 38 39 /*/** 40 * Examples: 41 * sio_microphone.c 42 * Stream the default input device over the default output device. 43 * Supports specifying device and backend to use. 44 */ 45 46 /*/** 47 * Examples: 48 * backend_disconnect_recover.c 49 * Demonstrates recovering from a backend disconnecting. 50 */ 51 52 /// See also ::soundio_strerror 53 enum SoundIoError 54 { 55 SoundIoErrorNone = 0, 56 /// Out of memory. 57 SoundIoErrorNoMem = 1, 58 /// The backend does not appear to be active or running. 59 SoundIoErrorInitAudioBackend = 2, 60 /// A system resource other than memory was not available. 61 SoundIoErrorSystemResources = 3, 62 /// Attempted to open a device and failed. 63 SoundIoErrorOpeningDevice = 4, 64 SoundIoErrorNoSuchDevice = 5, 65 /// The programmer did not comply with the API. 66 SoundIoErrorInvalid = 6, 67 /// libsoundio was compiled without support for that backend. 68 SoundIoErrorBackendUnavailable = 7, 69 /// An open stream had an error that can only be recovered from by 70 /// destroying the stream and creating it again. 71 SoundIoErrorStreaming = 8, 72 /// Attempted to use a device with parameters it cannot support. 73 SoundIoErrorIncompatibleDevice = 9, 74 /// When JACK returns `JackNoSuchClient` 75 SoundIoErrorNoSuchClient = 10, 76 /// Attempted to use parameters that the backend cannot support. 77 SoundIoErrorIncompatibleBackend = 11, 78 /// Backend server shutdown or became inactive. 79 SoundIoErrorBackendDisconnected = 12, 80 SoundIoErrorInterrupted = 13, 81 /// Buffer underrun occurred. 82 SoundIoErrorUnderflow = 14, 83 /// Unable to convert to or from UTF-8 to the native string format. 84 SoundIoErrorEncodingString = 15 85 } 86 87 /// Specifies where a channel is physically located. 88 enum SoundIoChannelId 89 { 90 SoundIoChannelIdInvalid = 0, 91 92 SoundIoChannelIdFrontLeft = 1, ///< First of the more commonly supported ids. 93 SoundIoChannelIdFrontRight = 2, 94 SoundIoChannelIdFrontCenter = 3, 95 SoundIoChannelIdLfe = 4, 96 SoundIoChannelIdBackLeft = 5, 97 SoundIoChannelIdBackRight = 6, 98 SoundIoChannelIdFrontLeftCenter = 7, 99 SoundIoChannelIdFrontRightCenter = 8, 100 SoundIoChannelIdBackCenter = 9, 101 SoundIoChannelIdSideLeft = 10, 102 SoundIoChannelIdSideRight = 11, 103 SoundIoChannelIdTopCenter = 12, 104 SoundIoChannelIdTopFrontLeft = 13, 105 SoundIoChannelIdTopFrontCenter = 14, 106 SoundIoChannelIdTopFrontRight = 15, 107 SoundIoChannelIdTopBackLeft = 16, 108 SoundIoChannelIdTopBackCenter = 17, 109 SoundIoChannelIdTopBackRight = 18, ///< Last of the more commonly supported ids. 110 111 SoundIoChannelIdBackLeftCenter = 19, ///< First of the less commonly supported ids. 112 SoundIoChannelIdBackRightCenter = 20, 113 SoundIoChannelIdFrontLeftWide = 21, 114 SoundIoChannelIdFrontRightWide = 22, 115 SoundIoChannelIdFrontLeftHigh = 23, 116 SoundIoChannelIdFrontCenterHigh = 24, 117 SoundIoChannelIdFrontRightHigh = 25, 118 SoundIoChannelIdTopFrontLeftCenter = 26, 119 SoundIoChannelIdTopFrontRightCenter = 27, 120 SoundIoChannelIdTopSideLeft = 28, 121 SoundIoChannelIdTopSideRight = 29, 122 SoundIoChannelIdLeftLfe = 30, 123 SoundIoChannelIdRightLfe = 31, 124 SoundIoChannelIdLfe2 = 32, 125 SoundIoChannelIdBottomCenter = 33, 126 SoundIoChannelIdBottomLeftCenter = 34, 127 SoundIoChannelIdBottomRightCenter = 35, 128 129 /// Mid/side recording 130 SoundIoChannelIdMsMid = 36, 131 SoundIoChannelIdMsSide = 37, 132 133 /// first order ambisonic channels 134 SoundIoChannelIdAmbisonicW = 38, 135 SoundIoChannelIdAmbisonicX = 39, 136 SoundIoChannelIdAmbisonicY = 40, 137 SoundIoChannelIdAmbisonicZ = 41, 138 139 /// X-Y Recording 140 SoundIoChannelIdXyX = 42, 141 SoundIoChannelIdXyY = 43, 142 143 SoundIoChannelIdHeadphonesLeft = 44, ///< First of the "other" channel ids 144 SoundIoChannelIdHeadphonesRight = 45, 145 SoundIoChannelIdClickTrack = 46, 146 SoundIoChannelIdForeignLanguage = 47, 147 SoundIoChannelIdHearingImpaired = 48, 148 SoundIoChannelIdNarration = 49, 149 SoundIoChannelIdHaptic = 50, 150 SoundIoChannelIdDialogCentricMix = 51, ///< Last of the "other" channel ids 151 152 SoundIoChannelIdAux = 52, 153 SoundIoChannelIdAux0 = 53, 154 SoundIoChannelIdAux1 = 54, 155 SoundIoChannelIdAux2 = 55, 156 SoundIoChannelIdAux3 = 56, 157 SoundIoChannelIdAux4 = 57, 158 SoundIoChannelIdAux5 = 58, 159 SoundIoChannelIdAux6 = 59, 160 SoundIoChannelIdAux7 = 60, 161 SoundIoChannelIdAux8 = 61, 162 SoundIoChannelIdAux9 = 62, 163 SoundIoChannelIdAux10 = 63, 164 SoundIoChannelIdAux11 = 64, 165 SoundIoChannelIdAux12 = 65, 166 SoundIoChannelIdAux13 = 66, 167 SoundIoChannelIdAux14 = 67, 168 SoundIoChannelIdAux15 = 68 169 } 170 171 /// Built-in channel layouts for convenience. 172 enum SoundIoChannelLayoutId 173 { 174 SoundIoChannelLayoutIdMono = 0, 175 SoundIoChannelLayoutIdStereo = 1, 176 SoundIoChannelLayoutId2Point1 = 2, 177 SoundIoChannelLayoutId3Point0 = 3, 178 SoundIoChannelLayoutId3Point0Back = 4, 179 SoundIoChannelLayoutId3Point1 = 5, 180 SoundIoChannelLayoutId4Point0 = 6, 181 SoundIoChannelLayoutIdQuad = 7, 182 SoundIoChannelLayoutIdQuadSide = 8, 183 SoundIoChannelLayoutId4Point1 = 9, 184 SoundIoChannelLayoutId5Point0Back = 10, 185 SoundIoChannelLayoutId5Point0Side = 11, 186 SoundIoChannelLayoutId5Point1 = 12, 187 SoundIoChannelLayoutId5Point1Back = 13, 188 SoundIoChannelLayoutId6Point0Side = 14, 189 SoundIoChannelLayoutId6Point0Front = 15, 190 SoundIoChannelLayoutIdHexagonal = 16, 191 SoundIoChannelLayoutId6Point1 = 17, 192 SoundIoChannelLayoutId6Point1Back = 18, 193 SoundIoChannelLayoutId6Point1Front = 19, 194 SoundIoChannelLayoutId7Point0 = 20, 195 SoundIoChannelLayoutId7Point0Front = 21, 196 SoundIoChannelLayoutId7Point1 = 22, 197 SoundIoChannelLayoutId7Point1Wide = 23, 198 SoundIoChannelLayoutId7Point1WideBack = 24, 199 SoundIoChannelLayoutIdOctagonal = 25 200 } 201 202 enum SoundIoBackend 203 { 204 SoundIoBackendNone = 0, 205 SoundIoBackendJack = 1, 206 SoundIoBackendPulseAudio = 2, 207 SoundIoBackendAlsa = 3, 208 SoundIoBackendCoreAudio = 4, 209 SoundIoBackendWasapi = 5, 210 SoundIoBackendDummy = 6 211 } 212 213 enum SoundIoDeviceAim 214 { 215 SoundIoDeviceAimInput = 0, ///< capture / recording 216 SoundIoDeviceAimOutput = 1 ///< playback 217 } 218 219 /// For your convenience, Native Endian and Foreign Endian constants are defined 220 /// which point to the respective SoundIoFormat values. 221 enum SoundIoFormat 222 { 223 SoundIoFormatInvalid = 0, 224 SoundIoFormatS8 = 1, ///< Signed 8 bit 225 SoundIoFormatU8 = 2, ///< Unsigned 8 bit 226 SoundIoFormatS16LE = 3, ///< Signed 16 bit Little Endian 227 SoundIoFormatS16BE = 4, ///< Signed 16 bit Big Endian 228 SoundIoFormatU16LE = 5, ///< Unsigned 16 bit Little Endian 229 SoundIoFormatU16BE = 6, ///< Unsigned 16 bit Big Endian 230 SoundIoFormatS24LE = 7, ///< Signed 24 bit Little Endian using low three bytes in 32-bit word 231 SoundIoFormatS24BE = 8, ///< Signed 24 bit Big Endian using low three bytes in 32-bit word 232 SoundIoFormatU24LE = 9, ///< Unsigned 24 bit Little Endian using low three bytes in 32-bit word 233 SoundIoFormatU24BE = 10, ///< Unsigned 24 bit Big Endian using low three bytes in 32-bit word 234 SoundIoFormatS32LE = 11, ///< Signed 32 bit Little Endian 235 SoundIoFormatS32BE = 12, ///< Signed 32 bit Big Endian 236 SoundIoFormatU32LE = 13, ///< Unsigned 32 bit Little Endian 237 SoundIoFormatU32BE = 14, ///< Unsigned 32 bit Big Endian 238 SoundIoFormatFloat32LE = 15, ///< Float 32 bit Little Endian, Range -1.0 to 1.0 239 SoundIoFormatFloat32BE = 16, ///< Float 32 bit Big Endian, Range -1.0 to 1.0 240 SoundIoFormatFloat64LE = 17, ///< Float 64 bit Little Endian, Range -1.0 to 1.0 241 SoundIoFormatFloat64BE = 18 ///< Float 64 bit Big Endian, Range -1.0 to 1.0 242 } 243 244 version (BigEndian) 245 { 246 enum SoundIoFormatS16NE = SoundIoFormat.SoundIoFormatS16BE; 247 enum SoundIoFormatU16NE = SoundIoFormat.SoundIoFormatU16BE; 248 enum SoundIoFormatS24NE = SoundIoFormat.SoundIoFormatS24BE; 249 enum SoundIoFormatU24NE = SoundIoFormat.SoundIoFormatU24BE; 250 enum SoundIoFormatS32NE = SoundIoFormat.SoundIoFormatS32BE; 251 enum SoundIoFormatU32NE = SoundIoFormat.SoundIoFormatU32BE; 252 enum SoundIoFormatFloat32NE = SoundIoFormat.SoundIoFormatFloat32BE; 253 enum SoundIoFormatFloat64NE = SoundIoFormat.SoundIoFormatFloat64BE; 254 255 enum SoundIoFormatS16FE = SoundIoFormat.SoundIoFormatS16LE; 256 enum SoundIoFormatU16FE = SoundIoFormat.SoundIoFormatU16LE; 257 enum SoundIoFormatS24FE = SoundIoFormat.SoundIoFormatS24LE; 258 enum SoundIoFormatU24FE = SoundIoFormat.SoundIoFormatU24LE; 259 enum SoundIoFormatS32FE = SoundIoFormat.SoundIoFormatS32LE; 260 enum SoundIoFormatU32FE = SoundIoFormat.SoundIoFormatU32LE; 261 enum SoundIoFormatFloat32FE = SoundIoFormat.SoundIoFormatFloat32LE; 262 enum SoundIoFormatFloat64FE = SoundIoFormat.SoundIoFormatFloat64LE; 263 } 264 else version (LittleEndian) 265 { 266 enum SoundIoFormatS16NE = SoundIoFormat.SoundIoFormatS16LE; 267 enum SoundIoFormatU16NE = SoundIoFormat.SoundIoFormatU16LE; 268 enum SoundIoFormatS24NE = SoundIoFormat.SoundIoFormatS24LE; 269 enum SoundIoFormatU24NE = SoundIoFormat.SoundIoFormatU24LE; 270 enum SoundIoFormatS32NE = SoundIoFormat.SoundIoFormatS32LE; 271 enum SoundIoFormatU32NE = SoundIoFormat.SoundIoFormatU32LE; 272 enum SoundIoFormatFloat32NE = SoundIoFormat.SoundIoFormatFloat32LE; 273 enum SoundIoFormatFloat64NE = SoundIoFormat.SoundIoFormatFloat64LE; 274 275 enum SoundIoFormatS16FE = SoundIoFormat.SoundIoFormatS16BE; 276 enum SoundIoFormatU16FE = SoundIoFormat.SoundIoFormatU16BE; 277 enum SoundIoFormatS24FE = SoundIoFormat.SoundIoFormatS24BE; 278 enum SoundIoFormatU24FE = SoundIoFormat.SoundIoFormatU24BE; 279 enum SoundIoFormatS32FE = SoundIoFormat.SoundIoFormatS32BE; 280 enum SoundIoFormatU32FE = SoundIoFormat.SoundIoFormatU32BE; 281 enum SoundIoFormatFloat32FE = SoundIoFormat.SoundIoFormatFloat32BE; 282 enum SoundIoFormatFloat64FE = SoundIoFormat.SoundIoFormatFloat64BE; 283 } 284 else 285 static assert(false, "unknown byte order"); 286 287 enum SOUNDIO_MAX_CHANNELS = 24; 288 /// The size of this struct is OK to use. 289 struct SoundIoChannelLayout 290 { 291 const(char)* name; 292 int channel_count; 293 SoundIoChannelId[SOUNDIO_MAX_CHANNELS] channels; 294 } 295 296 /// The size of this struct is OK to use. 297 struct SoundIoSampleRateRange 298 { 299 int min; 300 int max; 301 } 302 303 /// The size of this struct is OK to use. 304 struct SoundIoChannelArea 305 { 306 /// Base address of buffer. 307 char* ptr; 308 /// How many bytes it takes to get from the beginning of one sample to 309 /// the beginning of the next sample. 310 int step; 311 } 312 313 /// The size of this struct is not part of the API or ABI. 314 struct SoundIo 315 { 316 /// Optional. Put whatever you want here. Defaults to NULL. 317 void* userdata; 318 /// Optional callback. Called when the list of devices change. Only called 319 /// during a call to ::soundio_flush_events or ::soundio_wait_events. 320 void function(SoundIo*) on_devices_change; 321 /// Optional callback. Called when the backend disconnects. For example, 322 /// when the JACK server shuts down. When this happens, listing devices 323 /// and opening streams will always fail with 324 /// SoundIoErrorBackendDisconnected. This callback is only called during a 325 /// call to ::soundio_flush_events or ::soundio_wait_events. 326 /// If you do not supply a callback, the default will crash your program 327 /// with an error message. This callback is also called when the thread 328 /// that retrieves device information runs into an unrecoverable condition 329 /// such as running out of memory. 330 /// 331 /// Possible errors: 332 /// * #SoundIoErrorBackendDisconnected 333 /// * #SoundIoErrorNoMem 334 /// * #SoundIoErrorSystemResources 335 /// * #SoundIoErrorOpeningDevice - unexpected problem accessing device 336 /// information 337 void function(SoundIo*, int err) on_backend_disconnect; 338 /// Optional callback. Called from an unknown thread that you should not use 339 /// to call any soundio functions. You may use this to signal a condition 340 /// variable to wake up. Called when ::soundio_wait_events would be woken up. 341 void function(SoundIo*) on_events_signal; 342 343 /// Read-only. After calling ::soundio_connect or ::soundio_connect_backend, 344 /// this field tells which backend is currently connected. 345 SoundIoBackend current_backend; 346 347 /// Optional: Application name. 348 /// PulseAudio uses this for "application name". 349 /// JACK uses this for `client_name`. 350 /// Must not contain a colon (":"). 351 const(char)* app_name; 352 353 /// Optional: Real time priority warning. 354 /// This callback is fired when making thread real-time priority failed. By 355 /// default, it will print to stderr only the first time it is called 356 /// a message instructing the user how to configure their system to allow 357 /// real-time priority threads. This must be set to a function not NULL. 358 /// To silence the warning, assign this to a function that does nothing. 359 void function() emit_rtprio_warning; 360 361 /// Optional: JACK info callback. 362 /// By default, libsoundio sets this to an empty function in order to 363 /// silence stdio messages from JACK. You may override the behavior by 364 /// setting this to `NULL` or providing your own function. This is 365 /// registered with JACK regardless of whether ::soundio_connect_backend 366 /// succeeds. 367 void function(const(char)* msg) jack_info_callback; 368 /// Optional: JACK error callback. 369 /// See SoundIo::jack_info_callback 370 void function(const(char)* msg) jack_error_callback; 371 } 372 373 /// The size of this struct is not part of the API or ABI. 374 struct SoundIoDevice 375 { 376 /// Read-only. Set automatically. 377 SoundIo* soundio; 378 379 /// A string of bytes that uniquely identifies this device. 380 /// If the same physical device supports both input and output, that makes 381 /// one SoundIoDevice for the input and one SoundIoDevice for the output. 382 /// In this case, the id of each SoundIoDevice will be the same, and 383 /// SoundIoDevice::aim will be different. Additionally, if the device 384 /// supports raw mode, there may be up to four devices with the same id: 385 /// one for each value of SoundIoDevice::is_raw and one for each value of 386 /// SoundIoDevice::aim. 387 char* id; 388 /// User-friendly UTF-8 encoded text to describe the device. 389 char* name; 390 391 /// Tells whether this device is an input device or an output device. 392 SoundIoDeviceAim aim; 393 394 /// Channel layouts are handled similarly to SoundIoDevice::formats. 395 /// If this information is missing due to a SoundIoDevice::probe_error, 396 /// layouts will be NULL. It's OK to modify this data, for example calling 397 /// ::soundio_sort_channel_layouts on it. 398 /// Devices are guaranteed to have at least 1 channel layout. 399 SoundIoChannelLayout* layouts; 400 int layout_count; 401 /// See SoundIoDevice::current_format 402 SoundIoChannelLayout current_layout; 403 404 /// List of formats this device supports. See also 405 /// SoundIoDevice::current_format. 406 SoundIoFormat* formats; 407 /// How many formats are available in SoundIoDevice::formats. 408 int format_count; 409 /// A device is either a raw device or it is a virtual device that is 410 /// provided by a software mixing service such as dmix or PulseAudio (see 411 /// SoundIoDevice::is_raw). If it is a raw device, 412 /// current_format is meaningless; 413 /// the device has no current format until you open it. On the other hand, 414 /// if it is a virtual device, current_format describes the 415 /// destination sample format that your audio will be converted to. Or, 416 /// if you're the lucky first application to open the device, you might 417 /// cause the current_format to change to your format. 418 /// Generally, you want to ignore current_format and use 419 /// whatever format is most convenient 420 /// for you which is supported by the device, because when you are the only 421 /// application left, the mixer might decide to switch 422 /// current_format to yours. You can learn the supported formats via 423 /// formats and SoundIoDevice::format_count. If this information is missing 424 /// due to a probe error, formats will be `NULL`. If current_format is 425 /// unavailable, it will be set to #SoundIoFormatInvalid. 426 /// Devices are guaranteed to have at least 1 format available. 427 SoundIoFormat current_format; 428 429 /// Sample rate is the number of frames per second. 430 /// Sample rate is handled very similar to SoundIoDevice::formats. 431 /// If sample rate information is missing due to a probe error, the field 432 /// will be set to NULL. 433 /// Devices which have SoundIoDevice::probe_error set to #SoundIoErrorNone are 434 /// guaranteed to have at least 1 sample rate available. 435 SoundIoSampleRateRange* sample_rates; 436 /// How many sample rate ranges are available in 437 /// SoundIoDevice::sample_rates. 0 if sample rate information is missing 438 /// due to a probe error. 439 int sample_rate_count; 440 /// See SoundIoDevice::current_format 441 /// 0 if sample rate information is missing due to a probe error. 442 int sample_rate_current; 443 444 /// Software latency minimum in seconds. If this value is unknown or 445 /// irrelevant, it is set to 0.0. 446 /// For PulseAudio and WASAPI this value is unknown until you open a 447 /// stream. 448 double software_latency_min; 449 /// Software latency maximum in seconds. If this value is unknown or 450 /// irrelevant, it is set to 0.0. 451 /// For PulseAudio and WASAPI this value is unknown until you open a 452 /// stream. 453 double software_latency_max; 454 /// Software latency in seconds. If this value is unknown or 455 /// irrelevant, it is set to 0.0. 456 /// For PulseAudio and WASAPI this value is unknown until you open a 457 /// stream. 458 /// See SoundIoDevice::current_format 459 double software_latency_current; 460 461 /// Raw means that you are directly opening the hardware device and not 462 /// going through a proxy such as dmix, PulseAudio, or JACK. When you open a 463 /// raw device, other applications on the computer are not able to 464 /// simultaneously access the device. Raw devices do not perform automatic 465 /// resampling and thus tend to have fewer formats available. 466 bool is_raw; 467 468 /// Devices are reference counted. See ::soundio_device_ref and 469 /// ::soundio_device_unref. 470 int ref_count; 471 472 /// This is set to a SoundIoError representing the result of the device 473 /// probe. Ideally this will be SoundIoErrorNone in which case all the 474 /// fields of the device will be populated. If there is an error code here 475 /// then information about formats, sample rates, and channel layouts might 476 /// be missing. 477 /// 478 /// Possible errors: 479 /// * #SoundIoErrorOpeningDevice 480 /// * #SoundIoErrorNoMem 481 int probe_error; 482 } 483 484 /// The size of this struct is not part of the API or ABI. 485 struct SoundIoOutStream 486 { 487 /// Populated automatically when you call ::soundio_outstream_create. 488 SoundIoDevice* device; 489 490 /// Defaults to #SoundIoFormatFloat32NE, followed by the first one 491 /// supported. 492 SoundIoFormat format; 493 494 /// Sample rate is the number of frames per second. 495 /// Defaults to 48000 (and then clamped into range). 496 int sample_rate; 497 498 /// Defaults to Stereo, if available, followed by the first layout 499 /// supported. 500 SoundIoChannelLayout layout; 501 502 /// Ignoring hardware latency, this is the number of seconds it takes for 503 /// the last sample in a full buffer to be played. 504 /// After you call ::soundio_outstream_open, this value is replaced with the 505 /// actual software latency, as near to this value as possible. 506 /// On systems that support clearing the buffer, this defaults to a large 507 /// latency, potentially upwards of 2 seconds, with the understanding that 508 /// you will call ::soundio_outstream_clear_buffer when you want to reduce 509 /// the latency to 0. On systems that do not support clearing the buffer, 510 /// this defaults to a reasonable lower latency value. 511 /// 512 /// On backends with high latencies (such as 2 seconds), `frame_count_min` 513 /// will be 0, meaning you don't have to fill the entire buffer. In this 514 /// case, the large buffer is there if you want it; you only have to fill 515 /// as much as you want. On backends like JACK, `frame_count_min` will be 516 /// equal to `frame_count_max` and if you don't fill that many frames, you 517 /// will get glitches. 518 /// 519 /// If the device has unknown software latency min and max values, you may 520 /// still set this, but you might not get the value you requested. 521 /// For PulseAudio, if you set this value to non-default, it sets 522 /// `PA_STREAM_ADJUST_LATENCY` and is the value used for `maxlength` and 523 /// `tlength`. 524 /// 525 /// For JACK, this value is always equal to 526 /// SoundIoDevice::software_latency_current of the device. 527 double software_latency; 528 /// Core Audio and WASAPI only: current output Audio Unit volume. Float, 0.0-1.0. 529 float volume; 530 /// Defaults to NULL. Put whatever you want here. 531 void* userdata; 532 /// In this callback, you call ::soundio_outstream_begin_write and 533 /// ::soundio_outstream_end_write as many times as necessary to write 534 /// at minimum `frame_count_min` frames and at maximum `frame_count_max` 535 /// frames. `frame_count_max` will always be greater than 0. Note that you 536 /// should write as many frames as you can; `frame_count_min` might be 0 and 537 /// you can still get a buffer underflow if you always write 538 /// `frame_count_min` frames. 539 /// 540 /// For Dummy, ALSA, and PulseAudio, `frame_count_min` will be 0. For JACK 541 /// and CoreAudio `frame_count_min` will be equal to `frame_count_max`. 542 /// 543 /// The code in the supplied function must be suitable for real-time 544 /// execution. That means that it cannot call functions that might block 545 /// for a long time. This includes all I/O functions (disk, TTY, network), 546 /// malloc, free, printf, pthread_mutex_lock, sleep, wait, poll, select, 547 /// pthread_join, pthread_cond_wait, etc. 548 void function(SoundIoOutStream*, int frame_count_min, int frame_count_max) write_callback; 549 /// This optional callback happens when the sound device runs out of 550 /// buffered audio data to play. After this occurs, the outstream waits 551 /// until the buffer is full to resume playback. 552 /// This is called from the SoundIoOutStream::write_callback thread context. 553 void function(SoundIoOutStream*) underflow_callback; 554 /// Optional callback. `err` is always SoundIoErrorStreaming. 555 /// SoundIoErrorStreaming is an unrecoverable error. The stream is in an 556 /// invalid state and must be destroyed. 557 /// If you do not supply error_callback, the default callback will print 558 /// a message to stderr and then call `abort`. 559 /// This is called from the SoundIoOutStream::write_callback thread context. 560 void function(SoundIoOutStream*, int err) error_callback; 561 562 /// Optional: Name of the stream. Defaults to "SoundIoOutStream" 563 /// PulseAudio uses this for the stream name. 564 /// JACK uses this for the client name of the client that connects when you 565 /// open the stream. 566 /// WASAPI uses this for the session display name. 567 /// Must not contain a colon (":"). 568 const(char)* name; 569 570 /// Optional: Hint that this output stream is nonterminal. This is used by 571 /// JACK and it means that the output stream data originates from an input 572 /// stream. Defaults to `false`. 573 bool non_terminal_hint; 574 575 /// computed automatically when you call ::soundio_outstream_open 576 int bytes_per_frame; 577 /// computed automatically when you call ::soundio_outstream_open 578 int bytes_per_sample; 579 580 /// If setting the channel layout fails for some reason, this field is set 581 /// to an error code. Possible error codes are: 582 /// * #SoundIoErrorIncompatibleDevice 583 int layout_error; 584 } 585 586 /// The size of this struct is not part of the API or ABI. 587 struct SoundIoInStream 588 { 589 /// Populated automatically when you call ::soundio_outstream_create. 590 SoundIoDevice* device; 591 592 /// Defaults to #SoundIoFormatFloat32NE, followed by the first one 593 /// supported. 594 SoundIoFormat format; 595 596 /// Sample rate is the number of frames per second. 597 /// Defaults to max(sample_rate_min, min(sample_rate_max, 48000)) 598 int sample_rate; 599 600 /// Defaults to Stereo, if available, followed by the first layout 601 /// supported. 602 SoundIoChannelLayout layout; 603 604 /// Ignoring hardware latency, this is the number of seconds it takes for a 605 /// captured sample to become available for reading. 606 /// After you call ::soundio_instream_open, this value is replaced with the 607 /// actual software latency, as near to this value as possible. 608 /// A higher value means less CPU usage. Defaults to a large value, 609 /// potentially upwards of 2 seconds. 610 /// If the device has unknown software latency min and max values, you may 611 /// still set this, but you might not get the value you requested. 612 /// For PulseAudio, if you set this value to non-default, it sets 613 /// `PA_STREAM_ADJUST_LATENCY` and is the value used for `fragsize`. 614 /// For JACK, this value is always equal to 615 /// SoundIoDevice::software_latency_current 616 double software_latency; 617 618 /// Defaults to NULL. Put whatever you want here. 619 void* userdata; 620 /// In this function call ::soundio_instream_begin_read and 621 /// ::soundio_instream_end_read as many times as necessary to read at 622 /// minimum `frame_count_min` frames and at maximum `frame_count_max` 623 /// frames. If you return from read_callback without having read 624 /// `frame_count_min`, the frames will be dropped. `frame_count_max` is how 625 /// many frames are available to read. 626 /// 627 /// The code in the supplied function must be suitable for real-time 628 /// execution. That means that it cannot call functions that might block 629 /// for a long time. This includes all I/O functions (disk, TTY, network), 630 /// malloc, free, printf, pthread_mutex_lock, sleep, wait, poll, select, 631 /// pthread_join, pthread_cond_wait, etc. 632 void function(SoundIoInStream*, int frame_count_min, int frame_count_max) read_callback; 633 /// This optional callback happens when the sound device buffer is full, 634 /// yet there is more captured audio to put in it. 635 /// This is never fired for PulseAudio. 636 /// This is called from the SoundIoInStream::read_callback thread context. 637 void function(SoundIoInStream*) overflow_callback; 638 /// Optional callback. `err` is always SoundIoErrorStreaming. 639 /// SoundIoErrorStreaming is an unrecoverable error. The stream is in an 640 /// invalid state and must be destroyed. 641 /// If you do not supply `error_callback`, the default callback will print 642 /// a message to stderr and then abort(). 643 /// This is called from the SoundIoInStream::read_callback thread context. 644 void function(SoundIoInStream*, int err) error_callback; 645 646 /// Optional: Name of the stream. Defaults to "SoundIoInStream"; 647 /// PulseAudio uses this for the stream name. 648 /// JACK uses this for the client name of the client that connects when you 649 /// open the stream. 650 /// WASAPI uses this for the session display name. 651 /// Must not contain a colon (":"). 652 const(char)* name; 653 654 /// Optional: Hint that this input stream is nonterminal. This is used by 655 /// JACK and it means that the data received by the stream will be 656 /// passed on or made available to another stream. Defaults to `false`. 657 bool non_terminal_hint; 658 659 /// computed automatically when you call ::soundio_instream_open 660 int bytes_per_frame; 661 /// computed automatically when you call ::soundio_instream_open 662 int bytes_per_sample; 663 664 /// If setting the channel layout fails for some reason, this field is set 665 /// to an error code. Possible error codes are: #SoundIoErrorIncompatibleDevice 666 int layout_error; 667 } 668 669 /// See also ::soundio_version_major, ::soundio_version_minor, ::soundio_version_patch 670 const(char)* soundio_version_string(); 671 /// See also ::soundio_version_string, ::soundio_version_minor, ::soundio_version_patch 672 int soundio_version_major(); 673 /// See also ::soundio_version_major, ::soundio_version_string, ::soundio_version_patch 674 int soundio_version_minor(); 675 /// See also ::soundio_version_major, ::soundio_version_minor, ::soundio_version_string 676 int soundio_version_patch(); 677 678 /// Create a SoundIo context. You may create multiple instances of this to 679 /// connect to multiple backends. Sets all fields to defaults. 680 /// Returns `NULL` if and only if memory could not be allocated. 681 /// See also ::soundio_destroy 682 SoundIo* soundio_create(); 683 void soundio_destroy(SoundIo* soundio); 684 685 /// Tries ::soundio_connect_backend on all available backends in order. 686 /// Possible errors: 687 /// * #SoundIoErrorInvalid - already connected 688 /// * #SoundIoErrorNoMem 689 /// * #SoundIoErrorSystemResources 690 /// * #SoundIoErrorNoSuchClient - when JACK returns `JackNoSuchClient` 691 /// See also ::soundio_disconnect 692 int soundio_connect(SoundIo* soundio); 693 /// Instead of calling ::soundio_connect you may call this function to try a 694 /// specific backend. 695 /// Possible errors: 696 /// * #SoundIoErrorInvalid - already connected or invalid backend parameter 697 /// * #SoundIoErrorNoMem 698 /// * #SoundIoErrorBackendUnavailable - backend was not compiled in 699 /// * #SoundIoErrorSystemResources 700 /// * #SoundIoErrorNoSuchClient - when JACK returns `JackNoSuchClient` 701 /// * #SoundIoErrorInitAudioBackend - requested `backend` is not active 702 /// * #SoundIoErrorBackendDisconnected - backend disconnected while connecting 703 /// See also ::soundio_disconnect 704 int soundio_connect_backend(SoundIo* soundio, SoundIoBackend backend); 705 void soundio_disconnect(SoundIo* soundio); 706 707 /// Get a string representation of a #SoundIoError 708 const(char)* soundio_strerror(int error); 709 /// Get a string representation of a #SoundIoBackend 710 const(char)* soundio_backend_name(SoundIoBackend backend); 711 712 /// Returns the number of available backends. 713 int soundio_backend_count(SoundIo* soundio); 714 /// get the available backend at the specified index 715 /// (0 <= index < ::soundio_backend_count) 716 SoundIoBackend soundio_get_backend(SoundIo* soundio, int index); 717 718 /// Returns whether libsoundio was compiled with backend. 719 bool soundio_have_backend(SoundIoBackend backend); 720 721 /// Atomically update information for all connected devices. Note that calling 722 /// this function merely flips a pointer; the actual work of collecting device 723 /// information is done elsewhere. It is performant to call this function many 724 /// times per second. 725 /// 726 /// When you call this, the following callbacks might be called: 727 /// * SoundIo::on_devices_change 728 /// * SoundIo::on_backend_disconnect 729 /// This is the only time those callbacks can be called. 730 /// 731 /// This must be called from the same thread as the thread in which you call 732 /// these functions: 733 /// * ::soundio_input_device_count 734 /// * ::soundio_output_device_count 735 /// * ::soundio_get_input_device 736 /// * ::soundio_get_output_device 737 /// * ::soundio_default_input_device_index 738 /// * ::soundio_default_output_device_index 739 /// 740 /// Note that if you do not care about learning about updated devices, you 741 /// might call this function only once ever and never call 742 /// ::soundio_wait_events. 743 void soundio_flush_events(SoundIo* soundio); 744 745 /// This function calls ::soundio_flush_events then blocks until another event 746 /// is ready or you call ::soundio_wakeup. Be ready for spurious wakeups. 747 void soundio_wait_events(SoundIo* soundio); 748 749 /// Makes ::soundio_wait_events stop blocking. 750 void soundio_wakeup(SoundIo* soundio); 751 752 /// If necessary you can manually trigger a device rescan. Normally you will 753 /// not ever have to call this function, as libsoundio listens to system events 754 /// for device changes and responds to them by rescanning devices and preparing 755 /// the new device information for you to be atomically replaced when you call 756 /// ::soundio_flush_events. However you might run into cases where you want to 757 /// force trigger a device rescan, for example if an ALSA device has a 758 /// SoundIoDevice::probe_error. 759 /// 760 /// After you call this you still have to use ::soundio_flush_events or 761 /// ::soundio_wait_events and then wait for the 762 /// SoundIo::on_devices_change callback. 763 /// 764 /// This can be called from any thread context except for 765 /// SoundIoOutStream::write_callback and SoundIoInStream::read_callback 766 void soundio_force_device_scan(SoundIo* soundio); 767 768 // Channel Layouts 769 770 /// Returns whether the channel count field and each channel id matches in 771 /// the supplied channel layouts. 772 bool soundio_channel_layout_equal(const(SoundIoChannelLayout)* a, const(SoundIoChannelLayout)* b); 773 774 const(char)* soundio_get_channel_name(SoundIoChannelId id); 775 /// Given UTF-8 encoded text which is the name of a channel such as 776 /// "Front Left", "FL", or "front-left", return the corresponding 777 /// SoundIoChannelId. Returns SoundIoChannelIdInvalid for no match. 778 SoundIoChannelId soundio_parse_channel_id(const(char)* str, int str_len); 779 780 /// Returns the number of builtin channel layouts. 781 int soundio_channel_layout_builtin_count(); 782 /// Returns a builtin channel layout. 0 <= `index` < ::soundio_channel_layout_builtin_count 783 /// 784 /// Although `index` is of type `int`, it should be a valid 785 /// #SoundIoChannelLayoutId enum value. 786 const(SoundIoChannelLayout)* soundio_channel_layout_get_builtin(int index); 787 788 /// Get the default builtin channel layout for the given number of channels. 789 const(SoundIoChannelLayout)* soundio_channel_layout_get_default(int channel_count); 790 791 /// Return the index of `channel` in `layout`, or `-1` if not found. 792 int soundio_channel_layout_find_channel(const(SoundIoChannelLayout)* layout, 793 SoundIoChannelId channel); 794 795 /// Populates the name field of layout if it matches a builtin one. 796 /// returns whether it found a match 797 bool soundio_channel_layout_detect_builtin(SoundIoChannelLayout* layout); 798 799 /// Iterates over preferred_layouts. Returns the first channel layout in 800 /// preferred_layouts which matches one of the channel layouts in 801 /// available_layouts. Returns NULL if none matches. 802 const(SoundIoChannelLayout)* soundio_best_matching_channel_layout(const(SoundIoChannelLayout)* preferred_layouts, 803 int preferred_layout_count, const(SoundIoChannelLayout)* available_layouts, 804 int available_layout_count); 805 806 /// Sorts by channel count, descending. 807 void soundio_sort_channel_layouts(SoundIoChannelLayout* layouts, int layout_count); 808 809 // Sample Formats 810 811 /// Returns -1 on invalid format. 812 int soundio_get_bytes_per_sample(SoundIoFormat format); 813 814 /// A frame is one sample per channel. 815 int soundio_get_bytes_per_frame(SoundIoFormat format, int channel_count); 816 817 /// Sample rate is the number of frames per second. 818 int soundio_get_bytes_per_second(SoundIoFormat format, int channel_count, int sample_rate); 819 820 /// Returns string representation of `format`. 821 const(char)* soundio_format_string(SoundIoFormat format); 822 823 // Devices 824 825 /// When you call ::soundio_flush_events, a snapshot of all device state is 826 /// saved and these functions merely access the snapshot data. When you want 827 /// to check for new devices, call ::soundio_flush_events. Or you can call 828 /// ::soundio_wait_events to block until devices change. If an error occurs 829 /// scanning devices in a background thread, SoundIo::on_backend_disconnect is called 830 /// with the error code. 831 832 /// Get the number of input devices. 833 /// Returns -1 if you never called ::soundio_flush_events. 834 int soundio_input_device_count(SoundIo* soundio); 835 /// Get the number of output devices. 836 /// Returns -1 if you never called ::soundio_flush_events. 837 int soundio_output_device_count(SoundIo* soundio); 838 839 /// Always returns a device. Call ::soundio_device_unref when done. 840 /// `index` must be 0 <= index < ::soundio_input_device_count 841 /// Returns NULL if you never called ::soundio_flush_events or if you provide 842 /// invalid parameter values. 843 SoundIoDevice* soundio_get_input_device(SoundIo* soundio, int index); 844 /// Always returns a device. Call ::soundio_device_unref when done. 845 /// `index` must be 0 <= index < ::soundio_output_device_count 846 /// Returns NULL if you never called ::soundio_flush_events or if you provide 847 /// invalid parameter values. 848 SoundIoDevice* soundio_get_output_device(SoundIo* soundio, int index); 849 850 /// returns the index of the default input device 851 /// returns -1 if there are no devices or if you never called 852 /// ::soundio_flush_events. 853 int soundio_default_input_device_index(SoundIo* soundio); 854 855 /// returns the index of the default output device 856 /// returns -1 if there are no devices or if you never called 857 /// ::soundio_flush_events. 858 int soundio_default_output_device_index(SoundIo* soundio); 859 860 /// Add 1 to the reference count of `device`. 861 void soundio_device_ref(SoundIoDevice* device); 862 /// Remove 1 to the reference count of `device`. Clean up if it was the last 863 /// reference. 864 void soundio_device_unref(SoundIoDevice* device); 865 866 /// Return `true` if and only if the devices have the same SoundIoDevice::id, 867 /// SoundIoDevice::is_raw, and SoundIoDevice::aim are the same. 868 bool soundio_device_equal(const(SoundIoDevice)* a, const(SoundIoDevice)* b); 869 870 /// Sorts channel layouts by channel count, descending. 871 void soundio_device_sort_channel_layouts(SoundIoDevice* device); 872 873 /// Convenience function. Returns whether `format` is included in the device's 874 /// supported formats. 875 bool soundio_device_supports_format(SoundIoDevice* device, SoundIoFormat format); 876 877 /// Convenience function. Returns whether `layout` is included in the device's 878 /// supported channel layouts. 879 bool soundio_device_supports_layout(SoundIoDevice* device, const(SoundIoChannelLayout)* layout); 880 881 /// Convenience function. Returns whether `sample_rate` is included in the 882 /// device's supported sample rates. 883 bool soundio_device_supports_sample_rate(SoundIoDevice* device, int sample_rate); 884 885 /// Convenience function. Returns the available sample rate nearest to 886 /// `sample_rate`, rounding up. 887 int soundio_device_nearest_sample_rate(SoundIoDevice* device, int sample_rate); 888 889 // Output Streams 890 /// Allocates memory and sets defaults. Next you should fill out the struct fields 891 /// and then call ::soundio_outstream_open. Sets all fields to defaults. 892 /// Returns `NULL` if and only if memory could not be allocated. 893 /// See also ::soundio_outstream_destroy 894 SoundIoOutStream* soundio_outstream_create(SoundIoDevice* device); 895 /// You may not call this function from the SoundIoOutStream::write_callback thread context. 896 void soundio_outstream_destroy(SoundIoOutStream* outstream); 897 898 /// After you call this function, SoundIoOutStream::software_latency is set to 899 /// the correct value. 900 /// 901 /// The next thing to do is call ::soundio_outstream_start. 902 /// If this function returns an error, the outstream is in an invalid state and 903 /// you must call ::soundio_outstream_destroy on it. 904 /// 905 /// Possible errors: 906 /// * #SoundIoErrorInvalid 907 /// * SoundIoDevice::aim is not #SoundIoDeviceAimOutput 908 /// * SoundIoOutStream::format is not valid 909 /// * SoundIoOutStream::channel_count is greater than #SOUNDIO_MAX_CHANNELS 910 /// * #SoundIoErrorNoMem 911 /// * #SoundIoErrorOpeningDevice 912 /// * #SoundIoErrorBackendDisconnected 913 /// * #SoundIoErrorSystemResources 914 /// * #SoundIoErrorNoSuchClient - when JACK returns `JackNoSuchClient` 915 /// * #SoundIoErrorIncompatibleBackend - SoundIoOutStream::channel_count is 916 /// greater than the number of channels the backend can handle. 917 /// * #SoundIoErrorIncompatibleDevice - stream parameters requested are not 918 /// compatible with the chosen device. 919 int soundio_outstream_open(SoundIoOutStream* outstream); 920 921 /// After you call this function, SoundIoOutStream::write_callback will be called. 922 /// 923 /// This function might directly call SoundIoOutStream::write_callback. 924 /// 925 /// Possible errors: 926 /// * #SoundIoErrorStreaming 927 /// * #SoundIoErrorNoMem 928 /// * #SoundIoErrorSystemResources 929 /// * #SoundIoErrorBackendDisconnected 930 int soundio_outstream_start(SoundIoOutStream* outstream); 931 932 /// Call this function when you are ready to begin writing to the device buffer. 933 /// * `outstream` - (in) The output stream you want to write to. 934 /// * `areas` - (out) The memory addresses you can write data to, one per 935 /// channel. It is OK to modify the pointers if that helps you iterate. 936 /// * `frame_count` - (in/out) Provide the number of frames you want to write. 937 /// Returned will be the number of frames you can actually write, which is 938 /// also the number of frames that will be written when you call 939 /// ::soundio_outstream_end_write. The value returned will always be less 940 /// than or equal to the value provided. 941 /// It is your responsibility to call this function exactly as many times as 942 /// necessary to meet the `frame_count_min` and `frame_count_max` criteria from 943 /// SoundIoOutStream::write_callback. 944 /// You must call this function only from the SoundIoOutStream::write_callback thread context. 945 /// After calling this function, write data to `areas` and then call 946 /// ::soundio_outstream_end_write. 947 /// If this function returns an error, do not call ::soundio_outstream_end_write. 948 /// 949 /// Possible errors: 950 /// * #SoundIoErrorInvalid 951 /// * `*frame_count` <= 0 952 /// * `*frame_count` < `frame_count_min` or `*frame_count` > `frame_count_max` 953 /// * function called too many times without respecting `frame_count_max` 954 /// * #SoundIoErrorStreaming 955 /// * #SoundIoErrorUnderflow - an underflow caused this call to fail. You might 956 /// also get a SoundIoOutStream::underflow_callback, and you might not get 957 /// this error code when an underflow occurs. Unlike #SoundIoErrorStreaming, 958 /// the outstream is still in a valid state and streaming can continue. 959 /// * #SoundIoErrorIncompatibleDevice - in rare cases it might just now 960 /// be discovered that the device uses non-byte-aligned access, in which 961 /// case this error code is returned. 962 int soundio_outstream_begin_write(SoundIoOutStream* outstream, 963 SoundIoChannelArea** areas, int* frame_count); 964 965 /// Commits the write that you began with ::soundio_outstream_begin_write. 966 /// You must call this function only from the SoundIoOutStream::write_callback thread context. 967 /// 968 /// Possible errors: 969 /// * #SoundIoErrorStreaming 970 /// * #SoundIoErrorUnderflow - an underflow caused this call to fail. You might 971 /// also get a SoundIoOutStream::underflow_callback, and you might not get 972 /// this error code when an underflow occurs. Unlike #SoundIoErrorStreaming, 973 /// the outstream is still in a valid state and streaming can continue. 974 int soundio_outstream_end_write(SoundIoOutStream* outstream); 975 976 /// Clears the output stream buffer. 977 /// This function can be called from any thread. 978 /// This function can be called regardless of whether the outstream is paused 979 /// or not. 980 /// Some backends do not support clearing the buffer. On these backends this 981 /// function will return SoundIoErrorIncompatibleBackend. 982 /// Some devices do not support clearing the buffer. On these devices this 983 /// function might return SoundIoErrorIncompatibleDevice. 984 /// Possible errors: 985 /// 986 /// * #SoundIoErrorStreaming 987 /// * #SoundIoErrorIncompatibleBackend 988 /// * #SoundIoErrorIncompatibleDevice 989 int soundio_outstream_clear_buffer(SoundIoOutStream* outstream); 990 991 /// If the underlying backend and device support pausing, this pauses the 992 /// stream. SoundIoOutStream::write_callback may be called a few more times if 993 /// the buffer is not full. 994 /// Pausing might put the hardware into a low power state which is ideal if your 995 /// software is silent for some time. 996 /// This function may be called from any thread context, including 997 /// SoundIoOutStream::write_callback. 998 /// Pausing when already paused or unpausing when already unpaused has no 999 /// effect and returns #SoundIoErrorNone. 1000 /// 1001 /// Possible errors: 1002 /// * #SoundIoErrorBackendDisconnected 1003 /// * #SoundIoErrorStreaming 1004 /// * #SoundIoErrorIncompatibleDevice - device does not support 1005 /// pausing/unpausing. This error code might not be returned even if the 1006 /// device does not support pausing/unpausing. 1007 /// * #SoundIoErrorIncompatibleBackend - backend does not support 1008 /// pausing/unpausing. 1009 /// * #SoundIoErrorInvalid - outstream not opened and started 1010 int soundio_outstream_pause(SoundIoOutStream* outstream, bool pause); 1011 1012 /// Obtain the total number of seconds that the next frame written after the 1013 /// last frame written with ::soundio_outstream_end_write will take to become 1014 /// audible. This includes both software and hardware latency. In other words, 1015 /// if you call this function directly after calling ::soundio_outstream_end_write, 1016 /// this gives you the number of seconds that the next frame written will take 1017 /// to become audible. 1018 /// 1019 /// This function must be called only from within SoundIoOutStream::write_callback. 1020 /// 1021 /// Possible errors: 1022 /// * #SoundIoErrorStreaming 1023 int soundio_outstream_get_latency(SoundIoOutStream* outstream, double* out_latency); 1024 1025 /// 1026 int soundio_outstream_set_volume(SoundIoOutStream* outstream, double volume); 1027 1028 // Input Streams 1029 /// Allocates memory and sets defaults. Next you should fill out the struct fields 1030 /// and then call ::soundio_instream_open. Sets all fields to defaults. 1031 /// Returns `NULL` if and only if memory could not be allocated. 1032 /// See also ::soundio_instream_destroy 1033 SoundIoInStream* soundio_instream_create(SoundIoDevice* device); 1034 /// You may not call this function from SoundIoInStream::read_callback. 1035 void soundio_instream_destroy(SoundIoInStream* instream); 1036 1037 /// After you call this function, SoundIoInStream::software_latency is set to the correct 1038 /// value. 1039 /// The next thing to do is call ::soundio_instream_start. 1040 /// If this function returns an error, the instream is in an invalid state and 1041 /// you must call ::soundio_instream_destroy on it. 1042 /// 1043 /// Possible errors: 1044 /// * #SoundIoErrorInvalid 1045 /// * device aim is not #SoundIoDeviceAimInput 1046 /// * format is not valid 1047 /// * requested layout channel count > #SOUNDIO_MAX_CHANNELS 1048 /// * #SoundIoErrorOpeningDevice 1049 /// * #SoundIoErrorNoMem 1050 /// * #SoundIoErrorBackendDisconnected 1051 /// * #SoundIoErrorSystemResources 1052 /// * #SoundIoErrorNoSuchClient 1053 /// * #SoundIoErrorIncompatibleBackend 1054 /// * #SoundIoErrorIncompatibleDevice 1055 int soundio_instream_open(SoundIoInStream* instream); 1056 1057 /// After you call this function, SoundIoInStream::read_callback will be called. 1058 /// 1059 /// Possible errors: 1060 /// * #SoundIoErrorBackendDisconnected 1061 /// * #SoundIoErrorStreaming 1062 /// * #SoundIoErrorOpeningDevice 1063 /// * #SoundIoErrorSystemResources 1064 int soundio_instream_start(SoundIoInStream* instream); 1065 1066 /// Call this function when you are ready to begin reading from the device 1067 /// buffer. 1068 /// * `instream` - (in) The input stream you want to read from. 1069 /// * `areas` - (out) The memory addresses you can read data from. It is OK 1070 /// to modify the pointers if that helps you iterate. There might be a "hole" 1071 /// in the buffer. To indicate this, `areas` will be `NULL` and `frame_count` 1072 /// tells how big the hole is in frames. 1073 /// * `frame_count` - (in/out) - Provide the number of frames you want to read; 1074 /// returns the number of frames you can actually read. The returned value 1075 /// will always be less than or equal to the provided value. If the provided 1076 /// value is less than `frame_count_min` from SoundIoInStream::read_callback this function 1077 /// returns with #SoundIoErrorInvalid. 1078 /// It is your responsibility to call this function no more and no fewer than the 1079 /// correct number of times according to the `frame_count_min` and 1080 /// `frame_count_max` criteria from SoundIoInStream::read_callback. 1081 /// You must call this function only from the SoundIoInStream::read_callback thread context. 1082 /// After calling this function, read data from `areas` and then use 1083 /// ::soundio_instream_end_read` to actually remove the data from the buffer 1084 /// and move the read index forward. ::soundio_instream_end_read should not be 1085 /// called if the buffer is empty (`frame_count` == 0), but it should be called 1086 /// if there is a hole. 1087 /// 1088 /// Possible errors: 1089 /// * #SoundIoErrorInvalid 1090 /// * `*frame_count` < `frame_count_min` or `*frame_count` > `frame_count_max` 1091 /// * #SoundIoErrorStreaming 1092 /// * #SoundIoErrorIncompatibleDevice - in rare cases it might just now 1093 /// be discovered that the device uses non-byte-aligned access, in which 1094 /// case this error code is returned. 1095 int soundio_instream_begin_read(SoundIoInStream* instream, 1096 SoundIoChannelArea** areas, int* frame_count); 1097 /// This will drop all of the frames from when you called 1098 /// ::soundio_instream_begin_read. 1099 /// You must call this function only from the SoundIoInStream::read_callback thread context. 1100 /// You must call this function only after a successful call to 1101 /// ::soundio_instream_begin_read. 1102 /// 1103 /// Possible errors: 1104 /// * #SoundIoErrorStreaming 1105 int soundio_instream_end_read(SoundIoInStream* instream); 1106 1107 /// If the underyling device supports pausing, this pauses the stream and 1108 /// prevents SoundIoInStream::read_callback from being called. Otherwise this returns 1109 /// #SoundIoErrorIncompatibleDevice. 1110 /// This function may be called from any thread. 1111 /// Pausing when already paused or unpausing when already unpaused has no 1112 /// effect and always returns #SoundIoErrorNone. 1113 /// 1114 /// Possible errors: 1115 /// * #SoundIoErrorBackendDisconnected 1116 /// * #SoundIoErrorStreaming 1117 /// * #SoundIoErrorIncompatibleDevice - device does not support pausing/unpausing 1118 int soundio_instream_pause(SoundIoInStream* instream, bool pause); 1119 1120 /// Obtain the number of seconds that the next frame of sound being 1121 /// captured will take to arrive in the buffer, plus the amount of time that is 1122 /// represented in the buffer. This includes both software and hardware latency. 1123 /// 1124 /// This function must be called only from within SoundIoInStream::read_callback. 1125 /// 1126 /// Possible errors: 1127 /// * #SoundIoErrorStreaming 1128 int soundio_instream_get_latency(SoundIoInStream* instream, double* out_latency); 1129 1130 /// A ring buffer is a single-reader single-writer lock-free fixed-size queue. 1131 /// libsoundio ring buffers use memory mapping techniques to enable a 1132 /// contiguous buffer when reading or writing across the boundary of the ring 1133 /// buffer's capacity. 1134 struct SoundIoRingBuffer; 1135 /// `requested_capacity` in bytes. 1136 /// Returns `NULL` if and only if memory could not be allocated. 1137 /// Use ::soundio_ring_buffer_capacity to get the actual capacity, which might 1138 /// be greater for alignment purposes. 1139 /// See also ::soundio_ring_buffer_destroy 1140 SoundIoRingBuffer* soundio_ring_buffer_create(SoundIo* soundio, int requested_capacity); 1141 void soundio_ring_buffer_destroy(SoundIoRingBuffer* ring_buffer); 1142 1143 /// When you create a ring buffer, capacity might be more than the requested 1144 /// capacity for alignment purposes. This function returns the actual capacity. 1145 int soundio_ring_buffer_capacity(SoundIoRingBuffer* ring_buffer); 1146 1147 /// Do not write more than capacity. 1148 char* soundio_ring_buffer_write_ptr(SoundIoRingBuffer* ring_buffer); 1149 /// `count` in bytes. 1150 void soundio_ring_buffer_advance_write_ptr(SoundIoRingBuffer* ring_buffer, int count); 1151 1152 /// Do not read more than capacity. 1153 char* soundio_ring_buffer_read_ptr(SoundIoRingBuffer* ring_buffer); 1154 /// `count` in bytes. 1155 void soundio_ring_buffer_advance_read_ptr(SoundIoRingBuffer* ring_buffer, int count); 1156 1157 /// Returns how many bytes of the buffer is used, ready for reading. 1158 int soundio_ring_buffer_fill_count(SoundIoRingBuffer* ring_buffer); 1159 1160 /// Returns how many bytes of the buffer is free, ready for writing. 1161 int soundio_ring_buffer_free_count(SoundIoRingBuffer* ring_buffer); 1162 1163 /// Must be called by the writer. 1164 void soundio_ring_buffer_clear(SoundIoRingBuffer* ring_buffer);