Skip to main content

ferrin_spec/shared/
audio_format.rs

1//! Audio format descriptor for streaming audio inputs and outputs.
2
3use serde::Deserialize;
4use serde::Serialize;
5
6/// Audio encoding and sample rate.
7///
8/// `kind` is a provider-understood format name such as `pcm16`, `g711_ulaw`
9/// or `mp3`; `rate` is the sample rate in hertz when applicable.
10#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
11pub struct AudioFormat {
12    /// Format name.
13    #[serde(rename = "type")]
14    pub kind: String,
15    /// Sample rate in hertz.
16    #[serde(default, skip_serializing_if = "Option::is_none")]
17    pub rate: Option<u32>,
18}
19
20impl AudioFormat {
21    /// Creates a format without a sample rate.
22    #[must_use]
23    pub fn new(kind: impl Into<String>) -> Self {
24        Self {
25            kind: kind.into(),
26            rate: None,
27        }
28    }
29
30    /// Creates a format with a sample rate.
31    #[must_use]
32    pub fn with_rate(kind: impl Into<String>, rate: u32) -> Self {
33        Self {
34            kind: kind.into(),
35            rate: Some(rate),
36        }
37    }
38}