phonic 0.16.0

Audio playback library
Documentation
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
use std::{
    sync::{
        atomic::{AtomicU64, Ordering},
        Arc,
    },
    thread::{self, JoinHandle},
    time::Instant,
};

#[cfg(feature = "assert-allocs")]
use assert_no_alloc::*;

use cpal::{
    traits::{DeviceTrait, HostTrait, StreamTrait},
    Sample,
};
use std::sync::mpsc::{sync_channel, Receiver, SyncSender};

use crate::{
    error::Error,
    output::OutputDevice,
    source::{empty::EmptySource, Source, SourceTime},
    utils::{
        buffer::clear_buffer,
        smoothing::{apply_smoothed_gain, ExponentialSmoothedValue, SmoothedValue},
    },
};

// -------------------------------------------------------------------------------------------------

/// Prefered cpal device config when using the default/auto config.
const PREFERRED_SAMPLE_FORMAT: cpal::SampleFormat = cpal::SampleFormat::F32;
const PREFERRED_SAMPLE_RATE: cpal::SampleRate = 44100;
const PREFERRED_CHANNELS: cpal::ChannelCount = 2;

// -------------------------------------------------------------------------------------------------

/// Available audio backends for [`CpalOutput`].
///
/// Represents different audio backends available on various platforms.
/// The default variant uses the system-preferred audio host.
#[cfg(feature = "cpal-output")]
#[derive(Debug, Default, Clone, Copy)]
pub enum CpalOutputDeviceDriver {
    /// System's default audio host
    #[default]
    Default,
    /// Windows: Audio Stream Input/Output (ASIO)
    #[cfg(target_os = "windows")]
    Asio,
    /// Windows: Windows Audio Session API (WASAPI)
    #[cfg(target_os = "windows")]
    Wasapi,
    /// Linux: Advanced Linux Sound Architecture
    #[cfg(target_os = "linux")]
    Alsa,
    /// macOS: CoreAudio
    #[cfg(target_os = "macos")]
    CoreAudio,
    /// Windows, Linux & macOS: JACK Audio Connection Kit
    #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
    Jack,
}

// -------------------------------------------------------------------------------------------------

/// Unique device id for a [`CpalOutput`] device.
pub type CpalDeviceId = cpal::DeviceId;

// -------------------------------------------------------------------------------------------------

/// Configuration for a [`CpalOutput`] device.
///
/// Use with [`CpalOutput::open_with_config`] to select a specific audio driver, device,
/// sample rate and buffer size from a UI or configuration file.
#[derive(Debug, Default)]
pub struct CpalOutputConfig {
    /// Audio host/driver to use. Defaults to [`CpalOutputDeviceDriver::Default`].
    pub driver: CpalOutputDeviceDriver,
    /// Id of the output device to open. `None` selects the driver's default device.
    pub device_id: Option<CpalDeviceId>,
    /// Desired sample rate in Hz. `None` uses the preferred rate (44100) or device default.
    pub sample_rate: Option<u32>,
    /// Audio buffer size in frames. `None` uses the platform default buffer size.
    pub buffer_size: Option<u32>,
}

// -------------------------------------------------------------------------------------------------

/// Audio output device impl using [cpal](https://github.com/RustAudio/cpal).
pub struct CpalOutput {
    is_running: bool,
    channel_count: cpal::ChannelCount,
    sample_rate: cpal::SampleRate,
    volume: f32,
    playback_pos: Arc<AtomicU64>,
    callback_sender: SyncSender<CallbackMessage>,
    stream_sender: SyncSender<StreamMessage>,
    #[allow(unused)]
    stream_handle: StreamThreadHandle,
}

impl CpalOutput {
    /// Open an audio output device using the default configuration.
    pub fn open() -> Result<Self, Error> {
        Self::open_with_config(CpalOutputConfig::default())
    }

    /// Open an audio output device using the given configuration.
    ///
    /// Use [`CpalOutput::available_drivers`], [`CpalOutput::available_devices`] and
    /// [`CpalOutput::supported_sample_rates`] to enumerate available options dynamically.
    pub fn open_with_config(config: CpalOutputConfig) -> Result<Self, Error> {
        let host = Self::open_host(config.driver)?;

        // Find device by name or use the host default.
        let device = if let Some(device_id) = &config.device_id {
            host.output_devices()
                .map_err(|err| Error::OutputDeviceError(Box::new(err)))?
                .find(|d| d.id().ok().as_ref() == Some(device_id))
                .ok_or(cpal::DefaultStreamConfigError::DeviceNotAvailable)?
        } else {
            host.default_output_device()
                .ok_or(cpal::DefaultStreamConfigError::DeviceNotAvailable)?
        };

        if let Ok(description) = device.description() {
            log::info!("Using audio device: {description}");
        }

        // Get the preferred stream config for the requested (or default) sample rate.
        let output_config = Self::select_output_config(&device, config.sample_rate)?;

        // Shared playback position counter
        let playback_pos = Arc::new(AtomicU64::new(0));
        // Default volume
        let volume = 1.0;

        // Channel to send and receive callback messages
        const MESSAGE_QUEUE_SIZE: usize = 16;
        let (callback_sender, callback_receiver) = sync_channel(MESSAGE_QUEUE_SIZE);

        // Channel to send stream messages (pause/resume/close)
        const STREAM_MESSAGE_QUEUE_SIZE: usize = 32;
        let (stream_sender, stream_receiver) = sync_channel(STREAM_MESSAGE_QUEUE_SIZE);

        // Try opening the stream
        let stream = Stream::open(
            device,
            cpal::StreamConfig {
                buffer_size: match config.buffer_size {
                    Some(frames) => cpal::BufferSize::Fixed(frames),
                    None => cpal::BufferSize::Default,
                },
                ..output_config.config()
            },
            output_config.sample_format(),
            Arc::clone(&playback_pos),
            volume,
            callback_receiver,
            stream_sender.clone(),
        )?;

        // Run the stream on a new thread
        let stream_handle = StreamThreadHandle {
            sender: stream_sender,
            thread: thread::Builder::new()
                .name("audio_output".to_string())
                .spawn(move || stream.process_messages(stream_receiver))
                .expect("failed to spawn audio thread"),
        };

        let is_running = false;
        let channel_count = output_config.channels();
        let sample_rate = output_config.sample_rate();
        let stream_sender = stream_handle.sender.clone();

        Ok(Self {
            is_running,
            channel_count,
            sample_rate,
            volume,
            playback_pos,
            stream_sender,
            callback_sender,
            stream_handle,
        })
    }

    /// Returns all audio drivers available on this platform.
    ///
    /// Always includes [`CpalOutputDeviceDriver::Default`], followed by any named drivers that are
    /// currently available (e.g. ASIO, WASAPI on Windows; ALSA, JACK on Linux).
    pub fn available_drivers() -> Vec<CpalOutputDeviceDriver> {
        let hosts = cpal::available_hosts();
        let mut drivers = vec![CpalOutputDeviceDriver::Default];
        #[cfg(target_os = "windows")]
        if hosts.contains(&cpal::HostId::Asio) {
            drivers.push(CpalOutputDeviceDriver::Asio);
        }
        #[cfg(target_os = "windows")]
        if hosts.contains(&cpal::HostId::Wasapi) {
            drivers.push(CpalOutputDeviceDriver::Wasapi);
        }
        #[cfg(target_os = "linux")]
        if hosts.contains(&cpal::HostId::Alsa) {
            drivers.push(CpalOutputDeviceDriver::Alsa);
        }
        #[cfg(target_os = "macos")]
        if hosts.contains(&cpal::HostId::CoreAudio) {
            drivers.push(CpalOutputDeviceDriver::CoreAudio);
        }
        #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
        if hosts.contains(&cpal::HostId::Jack) {
            drivers.push(CpalOutputDeviceDriver::Jack);
        }
        drivers
    }

    /// Returns `(id, name)`s of all output devices available for the given driver.
    pub fn available_devices(
        driver: CpalOutputDeviceDriver,
    ) -> Result<Vec<(cpal::DeviceId, String)>, Error> {
        let host = Self::open_host(driver)?;
        let mut devices = Vec::new();
        for device in host
            .output_devices()
            .map_err(|err| Error::OutputDeviceError(Box::new(err)))?
        {
            match (device.id(), device.description()) {
                (Ok(id), Ok(description)) => {
                    devices.push((id, description.to_string()));
                }
                (Ok(id), Err(_)) => {
                    devices.push((id.clone(), id.to_string()));
                }
                (Err(err), _) => {
                    log::warn!("Failed to query audio device id {err}")
                }
            }
        }
        Ok(devices)
    }

    /// Returns unique sample rates supported by an output device, sorted ascending.
    ///
    /// Pass `device_name = None` to query the driver's default device.
    pub fn supported_sample_rates(
        driver: CpalOutputDeviceDriver,
        device_id: Option<CpalDeviceId>,
    ) -> Result<Vec<u32>, Error> {
        let host = Self::open_host(driver)?;
        let device = if let Some(device_id) = &device_id {
            host.output_devices()
                .map_err(|err| Error::OutputDeviceError(Box::new(err)))?
                .find(|d| d.id().ok().as_ref() == Some(device_id))
                .ok_or(cpal::DefaultStreamConfigError::DeviceNotAvailable)?
        } else {
            host.default_output_device()
                .ok_or(cpal::DefaultStreamConfigError::DeviceNotAvailable)?
        };
        let mut rates: Vec<u32> = device
            .supported_output_configs()?
            .flat_map(|s| [s.min_sample_rate(), s.max_sample_rate()])
            .collect();
        rates.sort_unstable();
        rates.dedup();
        Ok(rates)
    }

    fn open_host(driver: CpalOutputDeviceDriver) -> Result<cpal::Host, Error> {
        Ok(match driver {
            CpalOutputDeviceDriver::Default => cpal::default_host(),
            #[cfg(target_os = "windows")]
            CpalOutputDeviceDriver::Asio => cpal::host_from_id(cpal::HostId::Asio)
                .map_err(|err| Error::OutputDeviceError(Box::new(err)))?,
            #[cfg(target_os = "windows")]
            CpalOutputDeviceDriver::Wasapi => cpal::host_from_id(cpal::HostId::Wasapi)
                .map_err(|err| Error::OutputDeviceError(Box::new(err)))?,
            #[cfg(target_os = "linux")]
            CpalOutputDeviceDriver::Alsa => cpal::host_from_id(cpal::HostId::Alsa)
                .map_err(|err| Error::OutputDeviceError(Box::new(err)))?,
            #[cfg(target_os = "macos")]
            CpalOutputDeviceDriver::CoreAudio => cpal::host_from_id(cpal::HostId::CoreAudio)
                .map_err(|err| Error::OutputDeviceError(Box::new(err)))?,
            #[cfg(any(target_os = "windows", target_os = "linux", target_os = "macos"))]
            CpalOutputDeviceDriver::Jack => cpal::host_from_id(cpal::HostId::Jack)
                .map_err(|err| Error::OutputDeviceError(Box::new(err)))?,
        })
    }

    fn select_output_config(
        device: &cpal::Device,
        sample_rate: Option<u32>,
    ) -> Result<cpal::SupportedStreamConfig, Error> {
        let target_rate = sample_rate.unwrap_or(PREFERRED_SAMPLE_RATE);
        // Get supported configs and sort them in terms of their priority of use as a default stream format.
        let mut configs = device.supported_output_configs()?.collect::<Vec<_>>();
        configs.sort_by(|a, b| b.cmp_default_heuristics(a));
        // Match preferred 'rate + format + channels' first, then 'rate + channels' then 'rate' only
        let supports_rate = |s: &cpal::SupportedStreamConfigRange| {
            (s.min_sample_rate()..=s.max_sample_rate()).contains(&target_rate)
        };
        let best_match = configs
            .iter()
            .find(|s| {
                supports_rate(s)
                    && s.channels() == PREFERRED_CHANNELS
                    && s.sample_format() == PREFERRED_SAMPLE_FORMAT
            })
            .or_else(|| {
                configs
                    .iter()
                    .find(|s| supports_rate(s) && s.channels() == PREFERRED_CHANNELS)
            })
            .or_else(|| configs.iter().find(|s| supports_rate(s)));
        match best_match {
            Some(s) => Ok(s.with_sample_rate(target_rate)),
            None => {
                log::warn!("Found no matching audio device config which fits the prefered one. Using the device's default config instead...");
                Ok(device.default_output_config()?)
            }
        }
    }

    fn send_to_callback(&self, msg: CallbackMessage) {
        if let Err(err) = self.callback_sender.send(msg) {
            log::error!("Failed to send callback message: {err}");
        }
    }

    fn send_to_stream(&self, msg: StreamMessage) {
        if let Err(err) = self.stream_sender.send(msg) {
            log::error!("Failed to send stream message: {err}");
        }
    }
}

impl OutputDevice for CpalOutput {
    fn channel_count(&self) -> usize {
        self.channel_count as usize
    }

    fn sample_rate(&self) -> u32 {
        self.sample_rate
    }

    fn sample_position(&self) -> u64 {
        self.playback_pos.load(Ordering::Relaxed)
    }

    fn volume(&self) -> f32 {
        self.volume
    }
    fn set_volume(&mut self, volume: f32) {
        self.volume = volume;
        self.send_to_callback(CallbackMessage::SetVolume(volume));
    }

    fn is_suspended(&self) -> bool {
        false
    }

    fn is_running(&self) -> bool {
        self.is_running
    }
    fn pause(&mut self) {
        self.is_running = false;
        self.send_to_stream(StreamMessage::Pause);
        self.send_to_callback(CallbackMessage::Pause);
    }

    fn resume(&mut self) {
        self.send_to_stream(StreamMessage::Resume);
        self.send_to_callback(CallbackMessage::Resume);
        self.is_running = true;
    }

    fn play(&mut self, source: Box<dyn Source>) {
        // ensure source has our sample rate and channel layout
        assert_eq!(source.channel_count(), self.channel_count());
        assert_eq!(source.sample_rate(), self.sample_rate());
        // send message to activate it in the writer
        self.send_to_callback(CallbackMessage::PlaySource(source));
        // auto-start with the first set source
        if !self.is_running {
            self.resume();
        }
    }

    fn stop(&mut self) {
        self.send_to_callback(CallbackMessage::PlaySource(Box::new(EmptySource::new(
            self.channel_count(),
            self.sample_rate(),
        ))));
    }

    fn close(&mut self) {
        self.send_to_stream(StreamMessage::Close);
    }
}

// -------------------------------------------------------------------------------------------------

struct StreamThreadHandle {
    sender: SyncSender<StreamMessage>,
    #[allow(dead_code)]
    thread: JoinHandle<()>,
}

// -------------------------------------------------------------------------------------------------

#[derive(PartialEq)]
enum StreamMessage {
    Pause,
    Resume,
    Close,
}

// -------------------------------------------------------------------------------------------------

enum CallbackMessage {
    PlaySource(Box<dyn Source>),
    SetVolume(f32),
    Pause,
    Resume,
}

// -------------------------------------------------------------------------------------------------

#[derive(PartialEq)]
enum CallbackState {
    Playing,
    Paused,
}

// -------------------------------------------------------------------------------------------------

struct Stream {
    stream: cpal::Stream,
    // keep device alive with the stream
    #[allow(dead_code)]
    device: cpal::Device,
}

impl Stream {
    fn open(
        device: cpal::Device,
        config: cpal::StreamConfig,
        sample_format: cpal::SampleFormat,
        playback_pos: Arc<AtomicU64>,
        volume: f32,
        callback_receiver: Receiver<CallbackMessage>,
        stream_sender: SyncSender<StreamMessage>,
    ) -> Result<Self, Error> {
        let mut callback = StreamCallback {
            stream_sender,
            callback_receiver,
            source: Box::new(EmptySource::new(
                config.channels as usize,
                config.sample_rate,
            )),
            playback_pos,
            playback_pos_instant: Instant::now(),
            temp_buffer: Vec::with_capacity(StreamCallback::required_buffer_size(
                sample_format,
                &config,
            )),
            state: CallbackState::Paused,
            volume: ExponentialSmoothedValue::new(volume, config.sample_rate),
        };

        log::info!("Opening output stream: {:?}", &config);
        let stream = match sample_format {
            cpal::SampleFormat::I8 => {
                Self::build_output_stream::<i8, _>(&device, &config, move |output| {
                    callback.write_samples(output)
                })
            }
            cpal::SampleFormat::I16 => {
                Self::build_output_stream::<i16, _>(&device, &config, move |output| {
                    callback.write_samples(output)
                })
            }
            cpal::SampleFormat::I32 => {
                Self::build_output_stream::<i32, _>(&device, &config, move |output| {
                    callback.write_samples(output)
                })
            }
            cpal::SampleFormat::I64 => {
                Self::build_output_stream::<i64, _>(&device, &config, move |output| {
                    callback.write_samples(output)
                })
            }
            cpal::SampleFormat::U8 => {
                Self::build_output_stream::<u8, _>(&device, &config, move |output| {
                    callback.write_samples(output)
                })
            }
            cpal::SampleFormat::U16 => {
                Self::build_output_stream::<u16, _>(&device, &config, move |output| {
                    callback.write_samples(output)
                })
            }
            cpal::SampleFormat::U32 => {
                Self::build_output_stream::<u32, _>(&device, &config, move |output| {
                    callback.write_samples(output)
                })
            }
            cpal::SampleFormat::U64 => {
                Self::build_output_stream::<u64, _>(&device, &config, move |output| {
                    callback.write_samples(output)
                })
            }
            cpal::SampleFormat::F32 => {
                Self::build_output_stream::<f32, _>(&device, &config, move |output| {
                    callback.write_samples_f32(output) // use specialized write function
                })
            }
            cpal::SampleFormat::F64 => {
                Self::build_output_stream::<f64, _>(&device, &config, move |output| {
                    callback.write_samples(output)
                })
            }
            sample_format => panic!("Unsupported/unexpected sample format '{sample_format}'"),
        }?;

        Ok(Self { device, stream })
    }

    fn process_messages(self, receiver: Receiver<StreamMessage>) {
        while let Ok(msg) = receiver.recv() {
            match msg {
                StreamMessage::Pause => {
                    log::debug!("Pausing audio output stream...");
                    if let Err(err) = self.stream.pause() {
                        log::error!("Failed to stop stream: {err}");
                    }
                }
                StreamMessage::Resume => {
                    log::debug!("Resuming audio output stream...");
                    if let Err(err) = self.stream.play() {
                        log::error!("Failed to start stream: {err}");
                    }
                }
                StreamMessage::Close => {
                    log::debug!("Closing audio output stream...");
                    if let Err(err) = self.stream.pause() {
                        log::error!("Failed to pause stream before stopping: {err}");
                    }
                    break;
                }
            }
        }
    }

    fn build_output_stream<T, F>(
        device: &cpal::Device,
        config: &cpal::StreamConfig,
        mut writer: F,
    ) -> Result<cpal::Stream, cpal::BuildStreamError>
    where
        T: cpal::SizedSample,
        F: FnMut(&mut [T]) + Send + 'static,
    {
        device.build_output_stream(
            config,
            move |output: &mut [T], _: &cpal::OutputCallbackInfo| {
                writer(output);
            },
            |err| {
                log::error!("Audio output error: {err}");
            },
            None,
        )
    }
}

// -------------------------------------------------------------------------------------------------

struct StreamCallback {
    #[allow(dead_code)]
    stream_sender: SyncSender<StreamMessage>,
    callback_receiver: Receiver<CallbackMessage>,
    source: Box<dyn Source>,
    playback_pos: Arc<AtomicU64>,
    playback_pos_instant: Instant,
    temp_buffer: Vec<f32>,
    state: CallbackState,
    volume: ExponentialSmoothedValue,
}

impl StreamCallback {
    fn required_buffer_size(
        sample_format: cpal::SampleFormat,
        config: &cpal::StreamConfig,
    ) -> usize {
        if sample_format != cpal::SampleFormat::F32 {
            let max_frames = match config.buffer_size {
                cpal::BufferSize::Default => 2048,
                cpal::BufferSize::Fixed(fixed) => fixed,
            };
            max_frames as usize * config.channels as usize
        } else {
            0 // no temp buffer needed with write_samples_f32
        }
    }

    fn write_samples_f32(&mut self, output: &mut [f32]) {
        // Handle messages
        self.process_messages();
        // Avoid temp buffers and write directly into the given buffer
        let written = self.write_source(output);
        // Clear remaining output
        clear_buffer(&mut output[written..]);
    }

    fn write_samples<T>(&mut self, output: &mut [T])
    where
        T: cpal::SizedSample + cpal::FromSample<f32>,
    {
        // Handle messages
        self.process_messages();
        // Temporarily take ownership of the output buffer so we avoid borrowing self twice.
        let mut temp_buffer = std::mem::take(&mut self.temp_buffer);
        temp_buffer.resize(output.len(), 0.0);
        // Write into the f32 temp buffer
        let written = self.write_source(&mut temp_buffer);
        // Convert from f32 to the target sample type
        for (o, i) in output.iter_mut().zip(temp_buffer.iter()).take(written) {
            *o = i.to_sample();
        }
        // Clear remaining output
        for o in &mut output[written..] {
            *o = T::EQUILIBRIUM;
        }
        // Give the temp buffer back to self.
        self.temp_buffer = temp_buffer;
    }

    fn process_messages(&mut self) {
        // Process any pending data messages.
        while let Ok(msg) = self.callback_receiver.try_recv() {
            match msg {
                CallbackMessage::PlaySource(src) => {
                    self.source = src;
                }
                CallbackMessage::SetVolume(volume) => {
                    if self.state == CallbackState::Paused {
                        self.volume.init(volume);
                    } else {
                        self.volume.set_target(volume);
                    }
                }
                CallbackMessage::Pause => {
                    self.state = CallbackState::Paused;
                }
                CallbackMessage::Resume => {
                    self.state = CallbackState::Playing;
                }
            }
        }
    }

    fn write_source(&mut self, output: &mut [f32]) -> usize {
        // Only run the source when playing
        if self.state != CallbackState::Playing {
            return 0;
        }
        // Calculate source time from playback position
        let time = SourceTime {
            pos_in_frames: self.playback_pos.load(Ordering::Relaxed)
                / self.source.channel_count().max(1) as u64,
            pos_instant: self.playback_pos_instant,
        };
        // Write out as many samples as possible from the audio source to the output buffer.
        #[cfg(not(feature = "assert-allocs"))]
        let written = self.source.write(output, &time);
        #[cfg(feature = "assert-allocs")]
        let written = assert_no_alloc(|| self.source.write(output, &time));
        // Apply the global volume level
        apply_smoothed_gain(&mut output[..written], &mut self.volume);
        // Advance playback pos
        self.playback_pos
            .fetch_add(output.len() as u64, Ordering::Relaxed);
        // return modified samples
        written
    }
}

// -------------------------------------------------------------------------------------------------

impl From<cpal::DefaultStreamConfigError> for Error {
    fn from(err: cpal::DefaultStreamConfigError) -> Error {
        Error::OutputDeviceError(Box::new(err))
    }
}

impl From<cpal::SupportedStreamConfigsError> for Error {
    fn from(err: cpal::SupportedStreamConfigsError) -> Error {
        Error::OutputDeviceError(Box::new(err))
    }
}

impl From<cpal::BuildStreamError> for Error {
    fn from(err: cpal::BuildStreamError) -> Error {
        Error::OutputDeviceError(Box::new(err))
    }
}

impl From<cpal::PlayStreamError> for Error {
    fn from(err: cpal::PlayStreamError) -> Error {
        Error::OutputDeviceError(Box::new(err))
    }
}

impl From<cpal::PauseStreamError> for Error {
    fn from(err: cpal::PauseStreamError) -> Error {
        Error::OutputDeviceError(Box::new(err))
    }
}