1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
extern crate libsoundio_sys as raw;

use super::error::*;
use super::format::*;
use super::instream::*;
use super::layout::*;
use super::outstream::*;
use super::types::*;
use super::util::*;

use std::marker::PhantomData;
use std::os::raw::c_int;
use std::slice;

/// Device represents an input or output device.
///
/// It is obtained from a `Context` using `Context::input_device()` or `Context::output_device()`.
/// You can use it to open an input stream or output stream.
pub struct Device<'a> {
    /// The raw pointer to the device.
    pub device: *mut raw::SoundIoDevice,

    /// This is just here to say that Device cannot outlive the Context it was created from.
    /// 'a is the lifetime of that Context.
    pub phantom: PhantomData<&'a ()>,
}

impl<'a> Device<'a> {
    /// A string that uniquely identifies this device.
    ///
    /// If the same physical device supports both input and output, it is split
    /// into one `Device` for the input and another for the output.
    ///
    /// In this case, the `id` of each `Device` will be the same, and
    /// `Device::aim()` will be different. Additionally, if the device
    /// supports raw mode, there may be up to four devices with the same `id`:
    /// one for each value of `Device::is_raw()` and one for each value of
    /// `Device::aim()`.
    pub fn id(&self) -> String {
        // This is not explicitly latin1 but it is described as 'a string of bytes' so
        // it may contain invalid UTF-8 sequences.
        latin1_to_string(unsafe { (*self.device).id })
    }

    /// User-friendly UTF-8 encoded text to describe the device.
    pub fn name(&self) -> String {
        // This is explicitly UTF-8.
        utf8_to_string(unsafe { (*self.device).name })
    }

    /// Tells whether this device is an input device or an output device.
    ///
    /// If a physical device supports input and output it is split into two
    /// `Device`s, with the same `Device::id()` but different `Device::aim()`s.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut ctx = soundio::Context::new();
    /// ctx.connect_backend(soundio::Backend::Dummy).expect("Couldn't connect to backend");
    /// for dev in ctx.input_devices().expect("Couldn't get input devices") {
    ///     assert_eq!(dev.aim(), soundio::DeviceAim::Input);
    /// }
    /// for dev in ctx.output_devices().expect("Couldn't get output devices") {
    ///     assert_eq!(dev.aim(), soundio::DeviceAim::Output);
    /// }
    /// ```
    pub fn aim(&self) -> DeviceAim {
        unsafe { (*self.device).aim.into() }
    }

    /// Returns the list of channel layouts supported by this device.
    /// A channel layout has a name, and a list of channels with a channel ID.
    /// For examples `ChannelLayout { name: "Stereo", channels: vec![ChannelId::Left, ChannelId::Right] }`.
    ///
    /// Devices are guaranteed to have at least 1 channel layout.
    ///
    /// If you call `sort_channel_layouts()` before this function, the layouts will
    /// be sorted by the number of channels in decreasing order.
    pub fn layouts(&self) -> Vec<ChannelLayout> {
        let layouts_slice = unsafe {
            slice::from_raw_parts::<raw::SoundIoChannelLayout>(
                (*self.device).layouts,
                (*self.device).layout_count as _,
            )
        };

        layouts_slice.iter().map(|&x| x.into()).collect()
    }

    /// Get the current channel layout. This behaves similarly to the current format
    /// - this value is only meaningful for raw devices that have a sample
    /// rate defined before a stream is opened. See `Device::current_format()` for
    /// more information.
    pub fn current_layout(&self) -> ChannelLayout {
        unsafe { (*self.device).current_layout.into() }
    }

    /// List of formats this device supports.
    ///
    /// Devices are guaranteed to support at least one format.
    pub fn formats(&self) -> Vec<Format> {
        let formats_slice = unsafe {
            slice::from_raw_parts::<raw::SoundIoFormat>(
                (*self.device).formats,
                (*self.device).format_count as _,
            )
        };

        formats_slice.iter().map(|&x| x.into()).collect()
    }

    /// Get the current format.
    ///
    /// A device is either a raw device or it is a virtual device that is
    /// provided by a software mixing service such as dmix or PulseAudio (see
    /// `Device::is_raw()`). If it is a raw device, `current_format()` is meaningless;
    /// the device has no current format until you open it. On the other hand,
    /// if it is a virtual device, `current_format()` describes the
    /// destination sample format that your audio will be converted to. Or,
    /// if you're the lucky first application to open the device, you might
    /// cause the `current_format()` to change to your format.
    /// Generally, you want to ignore `current_format()` and use
    /// whatever format is most convenient for you which is supported by the device,
    /// because when you are the only application left, the mixer might decide to switch
    /// `current_format()` to yours. You can learn the supported formats via
    /// `Device::formats()`.
    ///
    /// If `current_format()` is unavailable, it will be set to `Format::Invalid`.
    pub fn current_format(&self) -> Format {
        unsafe { (*self.device).current_format.into() }
    }

    /// Sample rate is the number of frames per second (a frame is one sample from all channels).
    /// Sample rate is handled very similar to `formats()`.
    ///
    /// Devices are guaranteed to have at least 1 sample rate available.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut ctx = soundio::Context::new();
    /// ctx.connect_backend(soundio::Backend::Dummy).expect("Couldn't connect to backend");
    /// let out_dev = ctx.default_output_device().expect("Couldn't open default output");
    /// for rate in out_dev.sample_rates() {
    ///     println!("Sample rate min: {} max {}", rate.min, rate.max);
    /// }
    /// ```
    pub fn sample_rates(&self) -> Vec<SampleRateRange> {
        let sample_rates_slice = unsafe {
            slice::from_raw_parts::<raw::SoundIoSampleRateRange>(
                (*self.device).sample_rates,
                (*self.device).sample_rate_count as _,
            )
        };

        sample_rates_slice.iter().map(|&x| x.into()).collect()
    }

    /// Get the current sample rate. This behaves similarly to the current format
    /// - this value is only meaningful for raw devices that have a sample
    /// rate defined before a stream is opened. See `Device::current_format()` for
    /// more information.
    ///
    /// If `current_sample_rate()` is unavailable it will return 0.
    pub fn current_sample_rate(&self) -> i32 {
        unsafe { (*self.device).sample_rate_current as _ }
    }

    /// Software latency (current, minimum, maximum) in seconds. If this value is unknown or
    /// irrelevant, it is set to 0.0.
    ///
    /// For PulseAudio and WASAPI this value is unknown until you open a stream.
    pub fn software_latency(&self) -> SoftwareLatency {
        unsafe {
            SoftwareLatency {
                min: (*self.device).software_latency_min,
                max: (*self.device).software_latency_max,
                current: (*self.device).software_latency_current,
            }
        }
    }

    /// Return whether the device has raw access.
    ///
    /// Raw means that you are directly opening the hardware device and not
    /// going through a proxy such as dmix, PulseAudio, or JACK. When you open a
    /// raw device, other applications on the computer are not able to
    /// simultaneously access the device. Raw devices do not perform automatic
    /// resampling and thus tend to have fewer formats available.
    ///
    /// Physical devices will often have a raw `Device` and a virtual one. If the
    /// device supports input and output you will get four `Device`s.
    pub fn is_raw(&self) -> bool {
        unsafe { (*self.device).is_raw != 0 }
    }

    /// Sorts the channels returned by `layouts()` by channel count, descending.
    ///
    /// This mutates the internal list of layouts, which is why it takes `&mut self`.
    pub fn sort_channel_layouts(&mut self) {
        // It may be a good idea to remove this function. I don't think it adds to the API.
        unsafe {
            raw::soundio_device_sort_channel_layouts(self.device);
        }
    }

    /// Returns whether or not a given sample `Format` is supported by this device.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut ctx = soundio::Context::new();
    /// ctx.connect_backend(soundio::Backend::Dummy).expect("Couldn't connect to backend");
    /// let out_dev = ctx.default_output_device().expect("Couldn't open default output");
    /// println!("Default output device {} unsigned 16 bit little endian", if out_dev.supports_format(soundio::Format::S16LE) { "supports" } else { "doesn't support" });
    /// ```
    pub fn supports_format(&self, format: Format) -> bool {
        unsafe { raw::soundio_device_supports_format(self.device, format.into()) != 0 }
    }

    /// Returns whether or not a given channel layout is supported by this device.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut ctx = soundio::Context::new();
    /// ctx.connect_backend(soundio::Backend::Dummy).expect("Couldn't connect to backend");
    /// let out_dev = ctx.default_output_device().expect("Couldn't open default output");
    /// println!("Default output device {} stereo", if out_dev.supports_layout(soundio::ChannelLayout::get_builtin(soundio::ChannelLayoutId::Stereo)) { "supports" } else { "doesn't support" });
    /// ```
    pub fn supports_layout(&self, layout: ChannelLayout) -> bool {
        unsafe { raw::soundio_device_supports_layout(self.device, &layout.into() as *const _) != 0 }
    }

    /// Returns true if the given sample rate is supported by this device.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut ctx = soundio::Context::new();
    /// ctx.connect_backend(soundio::Backend::Dummy).expect("Couldn't connect to backend");
    /// let out_dev = ctx.default_output_device().expect("Couldn't open default output");
    /// println!("Default output device {} 44.1 kHz", if out_dev.supports_sample_rate(44100) { "supports" } else { "doesn't support" });
    /// ```
    pub fn supports_sample_rate(&self, sample_rate: i32) -> bool {
        unsafe { raw::soundio_device_supports_sample_rate(self.device, sample_rate as c_int) != 0 }
    }

    /// Returns the nearest supported sample rate of this device. Devices are guaranteed
    /// to support at least one sample rate.
    ///
    /// # Examples
    ///
    /// ```
    /// let mut ctx = soundio::Context::new();
    /// ctx.connect_backend(soundio::Backend::Dummy).expect("Couldn't connect to backend");
    /// let out_dev = ctx.default_output_device().expect("Couldn't open default output");
    /// println!("Nearest sample rate to 44000: {}", out_dev.nearest_sample_rate(44000));
    /// ```
    pub fn nearest_sample_rate(&self, sample_rate: i32) -> i32 {
        unsafe { raw::soundio_device_nearest_sample_rate(self.device, sample_rate as c_int) as i32 }
    }

    /// After you call this function, SoundIoOutStream::software_latency is set to
    /// the correct value.
    ///
    /// The next thing to do is call ::soundio_outstream_start.
    /// If this function returns an error, the outstream is in an invalid state and
    /// you must call ::soundio_outstream_destroy on it.
    ///

    /// Open an output stream on an output device. After opening you can start, pause and stop it
    /// using the functions on the `OutStream` that is returned. Then your write callback
    /// will be called. See the documentation on `OutStreamWriter` for more information.
    ///
    /// The parameters are as follows.
    ///
    /// * `sample_rate` - The requested sample rate. Check supported sample rates first with `Device::sample_rates()`.
    /// * `format` - The requested format. Check supported formats first with `Device::formats()`.
    /// * `layout` - The requested channel layout. Check supported formats first with `Device::layouts()`.
    /// * `latency` - The requested software latency in seconds. With a lower value your write callback will be called more often and work in smaller blocks but latency will be lower.
    /// * `write_callback` - Required callback that is called to allow you to write audio data to the outstream. See `OutStreamWriter` for more details.
    /// * `underflow_callback` - Optional callback that is called when your `write_callback` is too slow and the output skips.
    /// * `error_callback` - Optional error callback.
    ///
    /// Currently it is not possible to set the outstream name, or libsoundio's `non_terminal_hint`.
    ///
    /// # Return Values
    ///
    /// If successful the function returns an `OutStream` which you can call `OutStream::start()` on,
    /// otherwise it returns one of the following errors:
    ///
    /// * `Error::Invalid`
    ///   - `aim()` is not `DeviceAim::Output`
    ///   - `format` is not valid
    ///   - `channel_count` is greater than `SOUNDIO_MAX_CHANNELS` (24).
    /// * `Error::NoMem`
    /// * `Error::OpeningDevice`
    /// * `Error::BackendDisconnected`
    /// * `Error::SystemResources`
    /// * `Error::NoSuchClient` - when JACK returns `JackNoSuchClient`
    /// * `Error::IncompatibleBackend` - `OutStream::channel_count()` is greater than the number of channels the backend can handle.
    /// * `Error::IncompatibleDevice` - stream parameters requested are not compatible with the chosen device.
    ///
    /// # Lifetimes
    ///
    /// `'a` is the lifetime of the `Device`. The `OutStream` lifetime `'b` must be less than or equal to `'a` (indicated by `'b: 'a`).
    /// Also the callbacks must have a lifetime greater than or equal to `'b`. They do not need to be `'static`.
    pub fn open_outstream<'b: 'a, WriteCB, UnderflowCB, ErrorCB>(
        &'a self,
        sample_rate: i32,
        format: Format,
        layout: ChannelLayout,
        latency: f64,
        write_callback: WriteCB,
        underflow_callback: Option<UnderflowCB>,
        error_callback: Option<ErrorCB>,
    ) -> Result<OutStream<'b>>
    where
        WriteCB: 'b + FnMut(&mut OutStreamWriter),
        UnderflowCB: 'b + FnMut(),
        ErrorCB: 'b + FnMut(Error),
    {
        let mut outstream = unsafe { raw::soundio_outstream_create(self.device) };
        if outstream.is_null() {
            // Note that we should really abort() here (that's what the rest of Rust
            // does on OOM), but there is no stable way to abort in Rust that I can see.
            panic!("soundio_outstream_create() failed (out of memory).");
        }

        unsafe {
            (*outstream).sample_rate = sample_rate;
            (*outstream).format = format.into();
            (*outstream).layout = layout.into();
            (*outstream).software_latency = latency;
            (*outstream).write_callback = outstream_write_callback;
            (*outstream).underflow_callback = Some(outstream_underflow_callback);
            (*outstream).error_callback = Some(outstream_error_callback);
        }

        let mut stream = OutStream {
            userdata: Box::new(OutStreamUserData {
                outstream,
                write_callback: Box::new(write_callback),
                underflow_callback: match underflow_callback {
                    Some(cb) => Some(Box::new(cb)),
                    None => None,
                },
                error_callback: match error_callback {
                    Some(cb) => Some(Box::new(cb)),
                    None => None,
                },
            }),
            phantom: PhantomData,
        };

        // Safe userdata pointer.
        unsafe {
            (*stream.userdata.outstream).userdata =
                stream.userdata.as_mut() as *mut OutStreamUserData as *mut _;
        }

        match unsafe { raw::soundio_outstream_open(stream.userdata.outstream) } {
            0 => {}
            x => return Err(x.into()),
        };

        match unsafe { (*stream.userdata.outstream).layout_error } {
            0 => {}
            x => return Err(x.into()),
        }

        Ok(stream)
    }

    /// Open an input stream on an input device. After opening you can start, pause and stop it
    /// using the functions on the `InStream` that is returned. Then your read callback
    /// will be called. See the documentation on `InStreamReader` for more information.
    ///
    /// The parameters are as follows.
    ///
    /// * `sample_rate` - The requested sample rate. Check supported sample rates first with `Device::sample_rates()`.
    /// * `format` - The requested format. Check supported formats first with `Device::formats()`.
    /// * `layout` - The requested channel layout. Check supported formats first with `Device::layouts()`.
    /// * `latency` - The requested software latency in seconds. With a lower value your read callback will be called more often and work in smaller blocks but latency will be lower.
    /// * `read_callback` - Required callback that is called to allow you to process audio data from the instream. See `InStreamReader` for more details.
    /// * `overflow_callback` - Optional callback that is called when your `read_callback` is too slow and skips some input.
    /// * `error_callback` - Optional error callback.
    ///
    /// Currently it is not possible to set the outstream name, or libsoundio's `non_terminal_hint`.
    ///
    /// # Return Values
    ///
    /// If successful the function returns an `InStream` which you can call `InStream::start()` on,
    /// otherwise it returns one of the following errors:
    ///
    /// * `Error::Invalid`
    ///   - `aim()` is not `DeviceAim::Input`
    ///   - `format` is not valid
    ///   - `channel_count` is greater than `SOUNDIO_MAX_CHANNELS` (24).
    /// * `Error::NoMem`
    /// * `Error::OpeningDevice`
    /// * `Error::BackendDisconnected`
    /// * `Error::SystemResources`
    /// * `Error::NoSuchClient` - when JACK returns `JackNoSuchClient`
    /// * `Error::IncompatibleBackend` - `OutStream::channel_count()` is greater than the number of channels the backend can handle.
    /// * `Error::IncompatibleDevice` - stream parameters requested are not compatible with the chosen device.
    ///
    /// # Lifetimes
    ///
    /// `'a` is the lifetime of the `Device`. The `InStream` lifetime `'b` must be less than or equal to `'a` (indicated by `'b: 'a`).
    /// Also the callbacks must have a lifetime greater than or equal to `'b`. They do not need to be `'static`.
    pub fn open_instream<'b: 'a, ReadCB, OverflowCB, ErrorCB>(
        &'a self,
        sample_rate: i32,
        format: Format,
        layout: ChannelLayout,
        latency: f64,
        read_callback: ReadCB,
        overflow_callback: Option<OverflowCB>,
        error_callback: Option<ErrorCB>,
    ) -> Result<InStream<'b>>
    where
        ReadCB: 'b + FnMut(&mut InStreamReader),
        OverflowCB: 'b + FnMut(),
        ErrorCB: 'b + FnMut(Error),
    {
        let mut instream = unsafe { raw::soundio_instream_create(self.device) };
        if instream.is_null() {
            // Note that we should really abort() here (that's what the rest of Rust
            // does on OOM), but there is no stable way to abort in Rust that I can see.
            panic!("soundio_instream_create() failed (out of memory).");
        }

        unsafe {
            (*instream).sample_rate = sample_rate;
            (*instream).format = format.into();
            (*instream).layout = layout.into();
            (*instream).software_latency = latency;
            (*instream).read_callback = instream_read_callback;
            (*instream).overflow_callback = Some(instream_overflow_callback);
            (*instream).error_callback = Some(instream_error_callback);
        }

        let mut stream = InStream {
            userdata: Box::new(InStreamUserData {
                instream,
                read_callback: Box::new(read_callback),
                overflow_callback: match overflow_callback {
                    Some(cb) => Some(Box::new(cb)),
                    None => None,
                },
                error_callback: match error_callback {
                    Some(cb) => Some(Box::new(cb)),
                    None => None,
                },
            }),
            phantom: PhantomData,
        };

        // Safe userdata pointer.
        unsafe {
            (*stream.userdata.instream).userdata =
                stream.userdata.as_mut() as *mut InStreamUserData as *mut _;
        }

        match unsafe { raw::soundio_instream_open(stream.userdata.instream) } {
            0 => {}
            x => return Err(x.into()),
        };

        match unsafe { (*stream.userdata.instream).layout_error } {
            0 => {}
            x => return Err(x.into()),
        }

        Ok(stream)
    }
}

impl<'a> Drop for Device<'a> {
    fn drop(&mut self) {
        unsafe {
            raw::soundio_device_unref(self.device);
        }
    }
}