speech_synthesis/
audio.rs

1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2#[non_exhaustive]
3pub enum AudioCodec {
4	Opus,
5	Vorbis
6}
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9#[non_exhaustive]
10pub enum AudioEncoding {
11	/// Signed 16-bit PCM encoding.
12	PcmI16,
13	/// 32-bit floating point PCM encoding.
14	PcmF32,
15	/// 8-bit A-law encoding.
16	ALaw,
17	/// 8-bit μ-law encoding.
18	MuLaw
19}
20
21#[derive(Debug, Clone, Copy, PartialEq, Eq)]
22#[non_exhaustive]
23pub enum AudioChannels {
24	/// Single channel (mono) audio.
25	Mono,
26	/// 2 channel (stereo) audio.
27	Stereo
28}
29
30#[derive(Debug, Clone, Copy, PartialEq, Eq)]
31#[non_exhaustive]
32pub enum AudioContainer {
33	/// Containerless audio, only used with PCM, A-law, and mu-law encodings.
34	Raw(AudioEncoding),
35	/// RIFF format, aka .WAV lossless audio.
36	Riff(AudioEncoding),
37	/// MP3 format audio.
38	Mp3,
39	/// OGG format audio.
40	Ogg(AudioCodec),
41	/// WEBM format audio.
42	Webm(AudioCodec)
43}
44
45/// Struct used for negotiating an audio format supported by both the application and the speech synthesiser.
46#[derive(Debug, Default, Clone)]
47#[non_exhaustive]
48pub struct AudioFormatPreference {
49	pub sample_rates: Option<Vec<u32>>,
50	pub channels: Option<Vec<AudioChannels>>,
51	pub bitrates: Option<Vec<u16>>,
52	pub containers: Option<Vec<AudioContainer>>
53}
54
55impl AudioFormatPreference {
56	pub fn with_prefer_sample_rates(mut self, pref: impl IntoIterator<Item = u32>) -> Self {
57		match self.sample_rates.as_mut() {
58			None => self.sample_rates = Some(pref.into_iter().collect()),
59			Some(sample_rates) => sample_rates.extend(pref)
60		}
61		self
62	}
63
64	pub fn with_prefer_channels(mut self, pref: impl IntoIterator<Item = AudioChannels>) -> Self {
65		match self.channels.as_mut() {
66			None => self.channels = Some(pref.into_iter().collect()),
67			Some(channels) => channels.extend(pref)
68		}
69		self
70	}
71
72	pub fn with_prefer_bitrates(mut self, pref: impl IntoIterator<Item = u16>) -> Self {
73		match self.bitrates.as_mut() {
74			None => self.bitrates = Some(pref.into_iter().collect()),
75			Some(bitrates) => bitrates.extend(pref)
76		}
77		self
78	}
79
80	pub fn with_prefer_containers(mut self, pref: impl IntoIterator<Item = AudioContainer>) -> Self {
81		match self.containers.as_mut() {
82			None => self.containers = Some(pref.into_iter().collect()),
83			Some(containers) => containers.extend(pref)
84		}
85		self
86	}
87}
88
89#[derive(Debug, Clone)]
90pub struct AudioFormat {
91	sample_rate: u32,
92	channels: AudioChannels,
93	bitrate: Option<u16>,
94	container: AudioContainer
95}
96
97impl AudioFormat {
98	pub fn new(sample_rate: u32, channels: AudioChannels, bitrate: Option<u16>, container: AudioContainer) -> Self {
99		AudioFormat {
100			sample_rate,
101			channels,
102			bitrate,
103			container
104		}
105	}
106
107	pub fn sample_rate(&self) -> u32 {
108		self.sample_rate
109	}
110
111	pub fn channels(&self) -> AudioChannels {
112		self.channels
113	}
114
115	pub fn bitrate(&self) -> Option<u16> {
116		self.bitrate
117	}
118
119	pub fn container(&self) -> AudioContainer {
120		self.container
121	}
122}