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
//! Structures to use an audio device by pushing methods.

use std::io;

use super::{MicrophoneDevice, SpeakerDevice};
use crate::{bind, Result, Sdl, SdlError};

/// A queue to push data to play the sound with [`SpeakerDevice`].
#[derive(Debug)]
pub struct QueuedAudio<'device> {
    device: &'device mut SpeakerDevice,
}

impl<'device> QueuedAudio<'device> {
    /// Constructs a queue from a [`SpeakerDevice`].
    pub fn new(device: &'device mut SpeakerDevice) -> Self {
        Self { device }
    }

    /// Queues the `data` to play.
    ///
    /// # Errors
    ///
    /// Returns `Err` if failed to queue `data`.
    pub fn queue<T>(&self, data: &[T]) -> Result<()> {
        let size = std::mem::size_of_val(data);
        let ret =
            unsafe { bind::SDL_QueueAudio(self.device.id, data.as_ptr().cast(), size as u32) };
        if ret < 0 {
            Err(SdlError::Others { msg: Sdl::error() })
        } else {
            Ok(())
        }
    }

    /// Clears all audio data in the queue to stop to play, but it does not stop immediately because of drivers' implementation.
    pub fn clear(&self) {
        unsafe { bind::SDL_ClearQueuedAudio(self.device.id) }
    }

    /// Returns the size of the queue in bytes.
    #[must_use]
    pub fn queue_bytes_size(&self) -> usize {
        unsafe { bind::SDL_GetQueuedAudioSize(self.device.id) as usize }
    }
}

impl Drop for QueuedAudio<'_> {
    fn drop(&mut self) {
        unsafe { bind::SDL_ClearQueuedAudio(self.device.id) }
    }
}

/// A queue to read data to record the sound with [`MicrophoneDevice`]. To dequeue from this, `use` the implementation of [`std::io::Read`] for this.
#[derive(Debug)]
pub struct DequeueAudio<'device> {
    device: &'device mut MicrophoneDevice,
}

impl<'device> DequeueAudio<'device> {
    /// Constructs a queue from [`MicrophoneDevice`].
    pub fn new(device: &'device mut MicrophoneDevice) -> Self {
        Self { device }
    }

    /// Clears all audio data in the queue to prevent queue from unnecessary data.
    pub fn clear(&self) {
        unsafe { bind::SDL_ClearQueuedAudio(self.device.id) }
    }
}

impl Drop for DequeueAudio<'_> {
    fn drop(&mut self) {
        unsafe { bind::SDL_ClearQueuedAudio(self.device.id) }
    }
}

impl io::Read for DequeueAudio<'_> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        let bytes = unsafe {
            bind::SDL_DequeueAudio(self.device.id, buf.as_mut_ptr().cast(), buf.len() as u32)
        };
        Ok(bytes as usize)
    }
}