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 aurorafw.audio.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 Little 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 
529 	/// Defaults to NULL. Put whatever you want here.
530 	void* userdata;
531 	/// In this callback, you call ::soundio_outstream_begin_write and
532 	/// ::soundio_outstream_end_write as many times as necessary to write
533 	/// at minimum `frame_count_min` frames and at maximum `frame_count_max`
534 	/// frames. `frame_count_max` will always be greater than 0. Note that you
535 	/// should write as many frames as you can; `frame_count_min` might be 0 and
536 	/// you can still get a buffer underflow if you always write
537 	/// `frame_count_min` frames.
538 	///
539 	/// For Dummy, ALSA, and PulseAudio, `frame_count_min` will be 0. For JACK
540 	/// and CoreAudio `frame_count_min` will be equal to `frame_count_max`.
541 	///
542 	/// The code in the supplied function must be suitable for real-time
543 	/// execution. That means that it cannot call functions that might block
544 	/// for a long time. This includes all I/O functions (disk, TTY, network),
545 	/// malloc, free, printf, pthread_mutex_lock, sleep, wait, poll, select,
546 	/// pthread_join, pthread_cond_wait, etc.
547 	void function(SoundIoOutStream*, int frame_count_min, int frame_count_max) write_callback;
548 	/// This optional callback happens when the sound device runs out of
549 	/// buffered audio data to play. After this occurs, the outstream waits
550 	/// until the buffer is full to resume playback.
551 	/// This is called from the SoundIoOutStream::write_callback thread context.
552 	void function(SoundIoOutStream*) underflow_callback;
553 	/// Optional callback. `err` is always SoundIoErrorStreaming.
554 	/// SoundIoErrorStreaming is an unrecoverable error. The stream is in an
555 	/// invalid state and must be destroyed.
556 	/// If you do not supply error_callback, the default callback will print
557 	/// a message to stderr and then call `abort`.
558 	/// This is called from the SoundIoOutStream::write_callback thread context.
559 	void function(SoundIoOutStream*, int err) error_callback;
560 
561 	/// Optional: Name of the stream. Defaults to "SoundIoOutStream"
562 	/// PulseAudio uses this for the stream name.
563 	/// JACK uses this for the client name of the client that connects when you
564 	/// open the stream.
565 	/// WASAPI uses this for the session display name.
566 	/// Must not contain a colon (":").
567 	const(char)* name;
568 
569 	/// Optional: Hint that this output stream is nonterminal. This is used by
570 	/// JACK and it means that the output stream data originates from an input
571 	/// stream. Defaults to `false`.
572 	bool non_terminal_hint;
573 
574 	/// computed automatically when you call ::soundio_outstream_open
575 	int bytes_per_frame;
576 	/// computed automatically when you call ::soundio_outstream_open
577 	int bytes_per_sample;
578 
579 	/// If setting the channel layout fails for some reason, this field is set
580 	/// to an error code. Possible error codes are:
581 	/// * #SoundIoErrorIncompatibleDevice
582 	int layout_error;
583 }
584 
585 /// The size of this struct is not part of the API or ABI.
586 struct SoundIoInStream
587 {
588 	/// Populated automatically when you call ::soundio_outstream_create.
589 	SoundIoDevice* device;
590 
591 	/// Defaults to #SoundIoFormatFloat32NE, followed by the first one
592 	/// supported.
593 	SoundIoFormat format;
594 
595 	/// Sample rate is the number of frames per second.
596 	/// Defaults to max(sample_rate_min, min(sample_rate_max, 48000))
597 	int sample_rate;
598 
599 	/// Defaults to Stereo, if available, followed by the first layout
600 	/// supported.
601 	SoundIoChannelLayout layout;
602 
603 	/// Ignoring hardware latency, this is the number of seconds it takes for a
604 	/// captured sample to become available for reading.
605 	/// After you call ::soundio_instream_open, this value is replaced with the
606 	/// actual software latency, as near to this value as possible.
607 	/// A higher value means less CPU usage. Defaults to a large value,
608 	/// potentially upwards of 2 seconds.
609 	/// If the device has unknown software latency min and max values, you may
610 	/// still set this, but you might not get the value you requested.
611 	/// For PulseAudio, if you set this value to non-default, it sets
612 	/// `PA_STREAM_ADJUST_LATENCY` and is the value used for `fragsize`.
613 	/// For JACK, this value is always equal to
614 	/// SoundIoDevice::software_latency_current
615 	double software_latency;
616 
617 	/// Defaults to NULL. Put whatever you want here.
618 	void* userdata;
619 	/// In this function call ::soundio_instream_begin_read and
620 	/// ::soundio_instream_end_read as many times as necessary to read at
621 	/// minimum `frame_count_min` frames and at maximum `frame_count_max`
622 	/// frames. If you return from read_callback without having read
623 	/// `frame_count_min`, the frames will be dropped. `frame_count_max` is how
624 	/// many frames are available to read.
625 	///
626 	/// The code in the supplied function must be suitable for real-time
627 	/// execution. That means that it cannot call functions that might block
628 	/// for a long time. This includes all I/O functions (disk, TTY, network),
629 	/// malloc, free, printf, pthread_mutex_lock, sleep, wait, poll, select,
630 	/// pthread_join, pthread_cond_wait, etc.
631 	void function(SoundIoInStream*, int frame_count_min, int frame_count_max) read_callback;
632 	/// This optional callback happens when the sound device buffer is full,
633 	/// yet there is more captured audio to put in it.
634 	/// This is never fired for PulseAudio.
635 	/// This is called from the SoundIoInStream::read_callback thread context.
636 	void function(SoundIoInStream*) overflow_callback;
637 	/// Optional callback. `err` is always SoundIoErrorStreaming.
638 	/// SoundIoErrorStreaming is an unrecoverable error. The stream is in an
639 	/// invalid state and must be destroyed.
640 	/// If you do not supply `error_callback`, the default callback will print
641 	/// a message to stderr and then abort().
642 	/// This is called from the SoundIoInStream::read_callback thread context.
643 	void function(SoundIoInStream*, int err) error_callback;
644 
645 	/// Optional: Name of the stream. Defaults to "SoundIoInStream";
646 	/// PulseAudio uses this for the stream name.
647 	/// JACK uses this for the client name of the client that connects when you
648 	/// open the stream.
649 	/// WASAPI uses this for the session display name.
650 	/// Must not contain a colon (":").
651 	const(char)* name;
652 
653 	/// Optional: Hint that this input stream is nonterminal. This is used by
654 	/// JACK and it means that the data received by the stream will be
655 	/// passed on or made available to another stream. Defaults to `false`.
656 	bool non_terminal_hint;
657 
658 	/// computed automatically when you call ::soundio_instream_open
659 	int bytes_per_frame;
660 	/// computed automatically when you call ::soundio_instream_open
661 	int bytes_per_sample;
662 
663 	/// If setting the channel layout fails for some reason, this field is set
664 	/// to an error code. Possible error codes are: #SoundIoErrorIncompatibleDevice
665 	int layout_error;
666 }
667 
668 /// See also ::soundio_version_major, ::soundio_version_minor, ::soundio_version_patch
669 const(char)* soundio_version_string();
670 /// See also ::soundio_version_string, ::soundio_version_minor, ::soundio_version_patch
671 int soundio_version_major();
672 /// See also ::soundio_version_major, ::soundio_version_string, ::soundio_version_patch
673 int soundio_version_minor();
674 /// See also ::soundio_version_major, ::soundio_version_minor, ::soundio_version_string
675 int soundio_version_patch();
676 
677 /// Create a SoundIo context. You may create multiple instances of this to
678 /// connect to multiple backends. Sets all fields to defaults.
679 /// Returns `NULL` if and only if memory could not be allocated.
680 /// See also ::soundio_destroy
681 SoundIo* soundio_create();
682 void soundio_destroy(SoundIo* soundio);
683 
684 /// Tries ::soundio_connect_backend on all available backends in order.
685 /// Possible errors:
686 /// * #SoundIoErrorInvalid - already connected
687 /// * #SoundIoErrorNoMem
688 /// * #SoundIoErrorSystemResources
689 /// * #SoundIoErrorNoSuchClient - when JACK returns `JackNoSuchClient`
690 /// See also ::soundio_disconnect
691 int soundio_connect(SoundIo* soundio);
692 /// Instead of calling ::soundio_connect you may call this function to try a
693 /// specific backend.
694 /// Possible errors:
695 /// * #SoundIoErrorInvalid - already connected or invalid backend parameter
696 /// * #SoundIoErrorNoMem
697 /// * #SoundIoErrorBackendUnavailable - backend was not compiled in
698 /// * #SoundIoErrorSystemResources
699 /// * #SoundIoErrorNoSuchClient - when JACK returns `JackNoSuchClient`
700 /// * #SoundIoErrorInitAudioBackend - requested `backend` is not active
701 /// * #SoundIoErrorBackendDisconnected - backend disconnected while connecting
702 /// See also ::soundio_disconnect
703 int soundio_connect_backend(SoundIo* soundio, SoundIoBackend backend);
704 void soundio_disconnect(SoundIo* soundio);
705 
706 /// Get a string representation of a #SoundIoError
707 const(char)* soundio_strerror(int error);
708 /// Get a string representation of a #SoundIoBackend
709 const(char)* soundio_backend_name(SoundIoBackend backend);
710 
711 /// Returns the number of available backends.
712 int soundio_backend_count(SoundIo* soundio);
713 /// get the available backend at the specified index
714 /// (0 <= index < ::soundio_backend_count)
715 SoundIoBackend soundio_get_backend(SoundIo* soundio, int index);
716 
717 /// Returns whether libsoundio was compiled with backend.
718 bool soundio_have_backend(SoundIoBackend backend);
719 
720 /// Atomically update information for all connected devices. Note that calling
721 /// this function merely flips a pointer; the actual work of collecting device
722 /// information is done elsewhere. It is performant to call this function many
723 /// times per second.
724 ///
725 /// When you call this, the following callbacks might be called:
726 /// * SoundIo::on_devices_change
727 /// * SoundIo::on_backend_disconnect
728 /// This is the only time those callbacks can be called.
729 ///
730 /// This must be called from the same thread as the thread in which you call
731 /// these functions:
732 /// * ::soundio_input_device_count
733 /// * ::soundio_output_device_count
734 /// * ::soundio_get_input_device
735 /// * ::soundio_get_output_device
736 /// * ::soundio_default_input_device_index
737 /// * ::soundio_default_output_device_index
738 ///
739 /// Note that if you do not care about learning about updated devices, you
740 /// might call this function only once ever and never call
741 /// ::soundio_wait_events.
742 void soundio_flush_events(SoundIo* soundio);
743 
744 /// This function calls ::soundio_flush_events then blocks until another event
745 /// is ready or you call ::soundio_wakeup. Be ready for spurious wakeups.
746 void soundio_wait_events(SoundIo* soundio);
747 
748 /// Makes ::soundio_wait_events stop blocking.
749 void soundio_wakeup(SoundIo* soundio);
750 
751 /// If necessary you can manually trigger a device rescan. Normally you will
752 /// not ever have to call this function, as libsoundio listens to system events
753 /// for device changes and responds to them by rescanning devices and preparing
754 /// the new device information for you to be atomically replaced when you call
755 /// ::soundio_flush_events. However you might run into cases where you want to
756 /// force trigger a device rescan, for example if an ALSA device has a
757 /// SoundIoDevice::probe_error.
758 ///
759 /// After you call this you still have to use ::soundio_flush_events or
760 /// ::soundio_wait_events and then wait for the
761 /// SoundIo::on_devices_change callback.
762 ///
763 /// This can be called from any thread context except for
764 /// SoundIoOutStream::write_callback and SoundIoInStream::read_callback
765 void soundio_force_device_scan(SoundIo* soundio);
766 
767 // Channel Layouts
768 
769 /// Returns whether the channel count field and each channel id matches in
770 /// the supplied channel layouts.
771 bool soundio_channel_layout_equal(const(SoundIoChannelLayout)* a, const(SoundIoChannelLayout)* b);
772 
773 const(char)* soundio_get_channel_name(SoundIoChannelId id);
774 /// Given UTF-8 encoded text which is the name of a channel such as
775 /// "Front Left", "FL", or "front-left", return the corresponding
776 /// SoundIoChannelId. Returns SoundIoChannelIdInvalid for no match.
777 SoundIoChannelId soundio_parse_channel_id(const(char)* str, int str_len);
778 
779 /// Returns the number of builtin channel layouts.
780 int soundio_channel_layout_builtin_count();
781 /// Returns a builtin channel layout. 0 <= `index` < ::soundio_channel_layout_builtin_count
782 ///
783 /// Although `index` is of type `int`, it should be a valid
784 /// #SoundIoChannelLayoutId enum value.
785 const(SoundIoChannelLayout)* soundio_channel_layout_get_builtin(int index);
786 
787 /// Get the default builtin channel layout for the given number of channels.
788 const(SoundIoChannelLayout)* soundio_channel_layout_get_default(int channel_count);
789 
790 /// Return the index of `channel` in `layout`, or `-1` if not found.
791 int soundio_channel_layout_find_channel(const(SoundIoChannelLayout)* layout,
792 		SoundIoChannelId channel);
793 
794 /// Populates the name field of layout if it matches a builtin one.
795 /// returns whether it found a match
796 bool soundio_channel_layout_detect_builtin(SoundIoChannelLayout* layout);
797 
798 /// Iterates over preferred_layouts. Returns the first channel layout in
799 /// preferred_layouts which matches one of the channel layouts in
800 /// available_layouts. Returns NULL if none matches.
801 const(SoundIoChannelLayout)* soundio_best_matching_channel_layout(const(SoundIoChannelLayout)* preferred_layouts,
802 		int preferred_layout_count, const(SoundIoChannelLayout)* available_layouts,
803 		int available_layout_count);
804 
805 /// Sorts by channel count, descending.
806 void soundio_sort_channel_layouts(SoundIoChannelLayout* layouts, int layout_count);
807 
808 // Sample Formats
809 
810 /// Returns -1 on invalid format.
811 int soundio_get_bytes_per_sample(SoundIoFormat format);
812 
813 /// A frame is one sample per channel.
814 int soundio_get_bytes_per_frame(SoundIoFormat format, int channel_count);
815 
816 /// Sample rate is the number of frames per second.
817 int soundio_get_bytes_per_second(SoundIoFormat format, int channel_count, int sample_rate);
818 
819 /// Returns string representation of `format`.
820 const(char)* soundio_format_string(SoundIoFormat format);
821 
822 // Devices
823 
824 /// When you call ::soundio_flush_events, a snapshot of all device state is
825 /// saved and these functions merely access the snapshot data. When you want
826 /// to check for new devices, call ::soundio_flush_events. Or you can call
827 /// ::soundio_wait_events to block until devices change. If an error occurs
828 /// scanning devices in a background thread, SoundIo::on_backend_disconnect is called
829 /// with the error code.
830 
831 /// Get the number of input devices.
832 /// Returns -1 if you never called ::soundio_flush_events.
833 int soundio_input_device_count(SoundIo* soundio);
834 /// Get the number of output devices.
835 /// Returns -1 if you never called ::soundio_flush_events.
836 int soundio_output_device_count(SoundIo* soundio);
837 
838 /// Always returns a device. Call ::soundio_device_unref when done.
839 /// `index` must be 0 <= index < ::soundio_input_device_count
840 /// Returns NULL if you never called ::soundio_flush_events or if you provide
841 /// invalid parameter values.
842 SoundIoDevice* soundio_get_input_device(SoundIo* soundio, int index);
843 /// Always returns a device. Call ::soundio_device_unref when done.
844 /// `index` must be 0 <= index < ::soundio_output_device_count
845 /// Returns NULL if you never called ::soundio_flush_events or if you provide
846 /// invalid parameter values.
847 SoundIoDevice* soundio_get_output_device(SoundIo* soundio, int index);
848 
849 /// returns the index of the default input device
850 /// returns -1 if there are no devices or if you never called
851 /// ::soundio_flush_events.
852 int soundio_default_input_device_index(SoundIo* soundio);
853 
854 /// returns the index of the default output device
855 /// returns -1 if there are no devices or if you never called
856 /// ::soundio_flush_events.
857 int soundio_default_output_device_index(SoundIo* soundio);
858 
859 /// Add 1 to the reference count of `device`.
860 void soundio_device_ref(SoundIoDevice* device);
861 /// Remove 1 to the reference count of `device`. Clean up if it was the last
862 /// reference.
863 void soundio_device_unref(SoundIoDevice* device);
864 
865 /// Return `true` if and only if the devices have the same SoundIoDevice::id,
866 /// SoundIoDevice::is_raw, and SoundIoDevice::aim are the same.
867 bool soundio_device_equal(const(SoundIoDevice)* a, const(SoundIoDevice)* b);
868 
869 /// Sorts channel layouts by channel count, descending.
870 void soundio_device_sort_channel_layouts(SoundIoDevice* device);
871 
872 /// Convenience function. Returns whether `format` is included in the device's
873 /// supported formats.
874 bool soundio_device_supports_format(SoundIoDevice* device, SoundIoFormat format);
875 
876 /// Convenience function. Returns whether `layout` is included in the device's
877 /// supported channel layouts.
878 bool soundio_device_supports_layout(SoundIoDevice* device, const(SoundIoChannelLayout)* layout);
879 
880 /// Convenience function. Returns whether `sample_rate` is included in the
881 /// device's supported sample rates.
882 bool soundio_device_supports_sample_rate(SoundIoDevice* device, int sample_rate);
883 
884 /// Convenience function. Returns the available sample rate nearest to
885 /// `sample_rate`, rounding up.
886 int soundio_device_nearest_sample_rate(SoundIoDevice* device, int sample_rate);
887 
888 // Output Streams
889 /// Allocates memory and sets defaults. Next you should fill out the struct fields
890 /// and then call ::soundio_outstream_open. Sets all fields to defaults.
891 /// Returns `NULL` if and only if memory could not be allocated.
892 /// See also ::soundio_outstream_destroy
893 SoundIoOutStream* soundio_outstream_create(SoundIoDevice* device);
894 /// You may not call this function from the SoundIoOutStream::write_callback thread context.
895 void soundio_outstream_destroy(SoundIoOutStream* outstream);
896 
897 /// After you call this function, SoundIoOutStream::software_latency is set to
898 /// the correct value.
899 ///
900 /// The next thing to do is call ::soundio_instream_start.
901 /// If this function returns an error, the outstream is in an invalid state and
902 /// you must call ::soundio_outstream_destroy on it.
903 ///
904 /// Possible errors:
905 /// * #SoundIoErrorInvalid
906 ///   * SoundIoDevice::aim is not #SoundIoDeviceAimOutput
907 ///   * SoundIoOutStream::format is not valid
908 ///   * SoundIoOutStream::channel_count is greater than #SOUNDIO_MAX_CHANNELS
909 /// * #SoundIoErrorNoMem
910 /// * #SoundIoErrorOpeningDevice
911 /// * #SoundIoErrorBackendDisconnected
912 /// * #SoundIoErrorSystemResources
913 /// * #SoundIoErrorNoSuchClient - when JACK returns `JackNoSuchClient`
914 /// * #SoundIoErrorOpeningDevice
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 // Input Streams
1026 /// Allocates memory and sets defaults. Next you should fill out the struct fields
1027 /// and then call ::soundio_instream_open. Sets all fields to defaults.
1028 /// Returns `NULL` if and only if memory could not be allocated.
1029 /// See also ::soundio_instream_destroy
1030 SoundIoInStream* soundio_instream_create(SoundIoDevice* device);
1031 /// You may not call this function from SoundIoInStream::read_callback.
1032 void soundio_instream_destroy(SoundIoInStream* instream);
1033 
1034 /// After you call this function, SoundIoInStream::software_latency is set to the correct
1035 /// value.
1036 /// The next thing to do is call ::soundio_instream_start.
1037 /// If this function returns an error, the instream is in an invalid state and
1038 /// you must call ::soundio_instream_destroy on it.
1039 ///
1040 /// Possible errors:
1041 /// * #SoundIoErrorInvalid
1042 ///   * device aim is not #SoundIoDeviceAimInput
1043 ///   * format is not valid
1044 ///   * requested layout channel count > #SOUNDIO_MAX_CHANNELS
1045 /// * #SoundIoErrorOpeningDevice
1046 /// * #SoundIoErrorNoMem
1047 /// * #SoundIoErrorBackendDisconnected
1048 /// * #SoundIoErrorSystemResources
1049 /// * #SoundIoErrorNoSuchClient
1050 /// * #SoundIoErrorIncompatibleBackend
1051 /// * #SoundIoErrorIncompatibleDevice
1052 int soundio_instream_open(SoundIoInStream* instream);
1053 
1054 /// After you call this function, SoundIoInStream::read_callback will be called.
1055 ///
1056 /// Possible errors:
1057 /// * #SoundIoErrorBackendDisconnected
1058 /// * #SoundIoErrorStreaming
1059 /// * #SoundIoErrorOpeningDevice
1060 /// * #SoundIoErrorSystemResources
1061 int soundio_instream_start(SoundIoInStream* instream);
1062 
1063 /// Call this function when you are ready to begin reading from the device
1064 /// buffer.
1065 /// * `instream` - (in) The input stream you want to read from.
1066 /// * `areas` - (out) The memory addresses you can read data from. It is OK
1067 ///   to modify the pointers if that helps you iterate. There might be a "hole"
1068 ///   in the buffer. To indicate this, `areas` will be `NULL` and `frame_count`
1069 ///   tells how big the hole is in frames.
1070 /// * `frame_count` - (in/out) - Provide the number of frames you want to read;
1071 ///   returns the number of frames you can actually read. The returned value
1072 ///   will always be less than or equal to the provided value. If the provided
1073 ///   value is less than `frame_count_min` from SoundIoInStream::read_callback this function
1074 ///   returns with #SoundIoErrorInvalid.
1075 /// It is your responsibility to call this function no more and no fewer than the
1076 /// correct number of times according to the `frame_count_min` and
1077 /// `frame_count_max` criteria from SoundIoInStream::read_callback.
1078 /// You must call this function only from the SoundIoInStream::read_callback thread context.
1079 /// After calling this function, read data from `areas` and then use
1080 /// ::soundio_instream_end_read` to actually remove the data from the buffer
1081 /// and move the read index forward. ::soundio_instream_end_read should not be
1082 /// called if the buffer is empty (`frame_count` == 0), but it should be called
1083 /// if there is a hole.
1084 ///
1085 /// Possible errors:
1086 /// * #SoundIoErrorInvalid
1087 ///   * `*frame_count` < `frame_count_min` or `*frame_count` > `frame_count_max`
1088 /// * #SoundIoErrorStreaming
1089 /// * #SoundIoErrorIncompatibleDevice - in rare cases it might just now
1090 ///   be discovered that the device uses non-byte-aligned access, in which
1091 ///   case this error code is returned.
1092 int soundio_instream_begin_read(SoundIoInStream* instream,
1093 		SoundIoChannelArea** areas, int* frame_count);
1094 /// This will drop all of the frames from when you called
1095 /// ::soundio_instream_begin_read.
1096 /// You must call this function only from the SoundIoInStream::read_callback thread context.
1097 /// You must call this function only after a successful call to
1098 /// ::soundio_instream_begin_read.
1099 ///
1100 /// Possible errors:
1101 /// * #SoundIoErrorStreaming
1102 int soundio_instream_end_read(SoundIoInStream* instream);
1103 
1104 /// If the underyling device supports pausing, this pauses the stream and
1105 /// prevents SoundIoInStream::read_callback from being called. Otherwise this returns
1106 /// #SoundIoErrorIncompatibleDevice.
1107 /// This function may be called from any thread.
1108 /// Pausing when already paused or unpausing when already unpaused has no
1109 /// effect and always returns #SoundIoErrorNone.
1110 ///
1111 /// Possible errors:
1112 /// * #SoundIoErrorBackendDisconnected
1113 /// * #SoundIoErrorStreaming
1114 /// * #SoundIoErrorIncompatibleDevice - device does not support pausing/unpausing
1115 int soundio_instream_pause(SoundIoInStream* instream, bool pause);
1116 
1117 /// Obtain the number of seconds that the next frame of sound being
1118 /// captured will take to arrive in the buffer, plus the amount of time that is
1119 /// represented in the buffer. This includes both software and hardware latency.
1120 ///
1121 /// This function must be called only from within SoundIoInStream::read_callback.
1122 ///
1123 /// Possible errors:
1124 /// * #SoundIoErrorStreaming
1125 int soundio_instream_get_latency(SoundIoInStream* instream, double* out_latency);
1126 
1127 /// A ring buffer is a single-reader single-writer lock-free fixed-size queue.
1128 /// libsoundio ring buffers use memory mapping techniques to enable a
1129 /// contiguous buffer when reading or writing across the boundary of the ring
1130 /// buffer's capacity.
1131 struct SoundIoRingBuffer;
1132 /// `requested_capacity` in bytes.
1133 /// Returns `NULL` if and only if memory could not be allocated.
1134 /// Use ::soundio_ring_buffer_capacity to get the actual capacity, which might
1135 /// be greater for alignment purposes.
1136 /// See also ::soundio_ring_buffer_destroy
1137 SoundIoRingBuffer* soundio_ring_buffer_create(SoundIo* soundio, int requested_capacity);
1138 void soundio_ring_buffer_destroy(SoundIoRingBuffer* ring_buffer);
1139 
1140 /// When you create a ring buffer, capacity might be more than the requested
1141 /// capacity for alignment purposes. This function returns the actual capacity.
1142 int soundio_ring_buffer_capacity(SoundIoRingBuffer* ring_buffer);
1143 
1144 /// Do not write more than capacity.
1145 char* soundio_ring_buffer_write_ptr(SoundIoRingBuffer* ring_buffer);
1146 /// `count` in bytes.
1147 void soundio_ring_buffer_advance_write_ptr(SoundIoRingBuffer* ring_buffer, int count);
1148 
1149 /// Do not read more than capacity.
1150 char* soundio_ring_buffer_read_ptr(SoundIoRingBuffer* ring_buffer);
1151 /// `count` in bytes.
1152 void soundio_ring_buffer_advance_read_ptr(SoundIoRingBuffer* ring_buffer, int count);
1153 
1154 /// Returns how many bytes of the buffer is used, ready for reading.
1155 int soundio_ring_buffer_fill_count(SoundIoRingBuffer* ring_buffer);
1156 
1157 /// Returns how many bytes of the buffer is free, ready for writing.
1158 int soundio_ring_buffer_free_count(SoundIoRingBuffer* ring_buffer);
1159 
1160 /// Must be called by the writer.
1161 void soundio_ring_buffer_clear(SoundIoRingBuffer* ring_buffer);