silence_core/io/
mod.rs

1//! Traits, functions and type definitions for functioning audio I/O.
2//! The I/O feature provides types and functions for recording and playbacking audio aswell as handling the data.
3
4use ::cpal::{
5    traits::{DeviceTrait, HostTrait},
6    DefaultStreamConfigError, Device, Host, SupportedStreamConfig,
7};
8
9pub mod playback;
10pub mod record;
11
12/// Wrapper type for differentiating [`OutputDevice`] from [`InputDevice`] granted the user passes them in right when creating an [`AudioDevice`].
13pub type OutputDevice = Device;
14
15/// Wrapper type for differentiating [`OutputDevice`] from [`InputDevice`] granted the user passes them in right when creating an [`AudioDevice`].
16pub type InputDevice = Device;
17
18//Re-export the cpal crate.
19pub use cpal;
20
21/// The host's audio input and output devices.
22#[allow(missing_debug_implementations)]
23pub struct HostDevice {
24    /// The output device of the host.
25    pub output: Option<OutputDevice>,
26    /// The input device of the host.
27    pub input: Option<InputDevice>,
28}
29
30impl HostDevice {
31    /// Creates a new [`AudioDevice`] instance.
32    pub fn new(output: Option<Device>, input: Option<Device>) -> Self {
33        Self { output, input }
34    }
35
36    /// Gets the `input` device's default configuration
37    pub fn get_input_config(
38        &self,
39    ) -> Option<Result<SupportedStreamConfig, DefaultStreamConfigError>> {
40        self.input
41            .clone()
42            .map(|input_device| input_device.default_input_config())
43    }
44
45    /// Gets the `output` device's default configuration
46    pub fn get_output_config(
47        &self,
48    ) -> Option<Result<SupportedStreamConfig, DefaultStreamConfigError>> {
49        self.output
50            .clone()
51            .map(|input_device| input_device.default_output_config())
52    }
53}
54
55pub use cpal::{available_hosts, default_host, host_from_id};
56
57/// Get the default audio devices of the host. If one doesn't exist it will get initialized with `None`.
58pub fn get_audio_device(device: Host) -> HostDevice {
59    HostDevice {
60        input: device.default_input_device(),
61        output: device.default_output_device(),
62    }
63}
64
65/// Shows the encoder type of the packet.
66#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
67#[derive(Debug, deepsize::DeepSizeOf)]
68pub enum EncoderType {
69    /// The encoder of this packet was [`opus`].
70    /// The inner value contains whether.
71    Opus(bool),
72}
73
74/// The encoded sound packet.
75/// Contains useful information about the encoded packet.
76#[derive(Debug, deepsize::DeepSizeOf)]
77#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
78pub struct SoundPacket {
79    /// The Encoder's type which this [`SoundPacket`] got encoded with.
80    pub encoder_type: EncoderType,
81    /// The sample rate of the encoded packet.
82    pub sample_rate: u32,
83    /// The channel count of the encoded packet.
84    pub channels: u32,
85    /// The bytes of the encoded sound packet.
86    pub bytes: Vec<u8>,
87    /// The count of samples per frame.
88    pub samples_per_frame: u64,
89}
90