libobs_wrapper\data/
audio.rs1use libobs::obs_audio_info2;
2
3use crate::{
4 enums::{ObsSamplesPerSecond, ObsSpeakerLayout},
5 unsafe_send::Sendable,
6};
7
8#[repr(C)]
11#[derive(Clone, Debug, PartialEq, Eq)]
12pub struct ObsAudioInfo {
13 samples_per_sec: ObsSamplesPerSecond,
14 speakers: ObsSpeakerLayout,
15 max_buffering_ms: u32,
16 fixed_buffering: bool,
17}
18
19impl ObsAudioInfo {
20 pub fn new(
27 samples_per_second: ObsSamplesPerSecond,
28 speakers: ObsSpeakerLayout,
29 max_buffering_ms: u32,
30 fixed_buffering: bool,
31 ) -> Self {
32 Self {
33 samples_per_sec: samples_per_second,
34 speakers,
35 max_buffering_ms,
36 fixed_buffering,
37 }
38 }
39
40 pub fn new_low_latency(
43 samples_per_second: ObsSamplesPerSecond,
44 speakers: ObsSpeakerLayout,
45 ) -> Self {
46 Self::new(samples_per_second, speakers, 20, true)
47 }
48
49 pub fn samples_per_second(&self) -> ObsSamplesPerSecond {
50 self.samples_per_sec
51 }
52
53 pub fn speakers(&self) -> ObsSpeakerLayout {
54 self.speakers
55 }
56
57 pub fn max_buffering_ms(&self) -> u32 {
58 self.max_buffering_ms
59 }
60
61 pub fn fixed_buffering(&self) -> bool {
62 self.fixed_buffering
63 }
64
65 pub(crate) fn as_ptr(&self) -> Sendable<*const obs_audio_info2> {
66 Sendable(self as *const Self as *const obs_audio_info2)
67 }
68}
69
70impl Default for ObsAudioInfo {
71 fn default() -> Self {
72 Self {
73 samples_per_sec: ObsSamplesPerSecond::F44100,
74 speakers: ObsSpeakerLayout::Stereo,
75 max_buffering_ms: 0,
76 fixed_buffering: false,
77 }
78 }
79}
80
81#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
86pub struct AudioMixIndex(u8);
87
88impl AudioMixIndex {
89 pub fn new(index: usize) -> Result<Self, crate::utils::ObsError> {
90 if index >= libobs::MAX_AUDIO_MIXES as usize {
91 return Err(crate::utils::ObsError::AudioMixerIndexOutOfBounds {
92 index,
93 max: (libobs::MAX_AUDIO_MIXES - 1) as usize,
94 });
95 }
96 Ok(Self(index as u8))
97 }
98
99 pub const fn index(self) -> usize {
100 self.0 as usize
101 }
102
103 pub const fn bit(self) -> u32 {
104 1_u32 << self.0
105 }
106}
107
108impl TryFrom<usize> for AudioMixIndex {
109 type Error = crate::utils::ObsError;
110
111 fn try_from(value: usize) -> Result<Self, Self::Error> {
112 Self::new(value)
113 }
114}
115
116#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
121pub struct AudioEncoderSlot(u8);
122
123impl AudioEncoderSlot {
124 pub fn new(index: usize) -> Result<Self, crate::utils::ObsError> {
125 if index >= libobs::MAX_AUDIO_MIXES as usize {
126 return Err(crate::utils::ObsError::AudioMixerIndexOutOfBounds {
127 index,
128 max: (libobs::MAX_AUDIO_MIXES - 1) as usize,
129 });
130 }
131 Ok(Self(index as u8))
132 }
133
134 pub const fn index(self) -> usize {
135 self.0 as usize
136 }
137}
138
139impl TryFrom<usize> for AudioEncoderSlot {
140 type Error = crate::utils::ObsError;
141
142 fn try_from(value: usize) -> Result<Self, Self::Error> {
143 Self::new(value)
144 }
145}
146
147#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
149pub struct AudioMixMask(u32);
150
151impl AudioMixMask {
152 pub const fn empty() -> Self {
153 Self(0)
154 }
155
156 pub const fn all() -> Self {
157 Self((1_u32 << libobs::MAX_AUDIO_MIXES) - 1)
158 }
159
160 pub fn from_bits(bits: u32) -> Result<Self, crate::utils::ObsError> {
161 let allowed = Self::all().0;
162 if bits & !allowed != 0 {
163 return Err(crate::utils::ObsError::InvalidOperation(format!(
164 "audio mix mask {bits:#x} references mixes outside 0..{}",
165 libobs::MAX_AUDIO_MIXES
166 )));
167 }
168 Ok(Self(bits))
169 }
170
171 pub const fn bits(self) -> u32 {
172 self.0
173 }
174
175 pub const fn contains(self, mix: AudioMixIndex) -> bool {
176 self.0 & mix.bit() != 0
177 }
178
179 pub const fn with_mix(self, mix: AudioMixIndex) -> Self {
180 Self(self.0 | mix.bit())
181 }
182
183 pub const fn without_mix(self, mix: AudioMixIndex) -> Self {
184 Self(self.0 & !mix.bit())
185 }
186}
187
188#[derive(Clone, Copy, Debug, PartialEq)]
191pub struct LinearVolume(f32);
192
193impl LinearVolume {
194 pub fn new(multiplier: f32) -> Result<Self, crate::utils::ObsError> {
195 if !multiplier.is_finite() || multiplier < 0.0 {
196 return Err(crate::utils::ObsError::InvalidOperation(format!(
197 "linear volume must be finite and non-negative, got {multiplier}"
198 )));
199 }
200 Ok(Self(multiplier))
201 }
202
203 pub const fn silence() -> Self {
204 Self(0.0)
205 }
206
207 pub const fn unity() -> Self {
208 Self(1.0)
209 }
210
211 pub const fn multiplier(self) -> f32 {
212 self.0
213 }
214}
215
216#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
218pub struct AudioSyncOffset(i64);
219
220impl AudioSyncOffset {
221 pub const fn from_nanoseconds(nanoseconds: i64) -> Self {
222 Self(nanoseconds)
223 }
224
225 pub fn from_milliseconds(milliseconds: i64) -> Result<Self, crate::utils::ObsError> {
226 milliseconds
227 .checked_mul(1_000_000)
228 .map(Self)
229 .ok_or_else(|| {
230 crate::utils::ObsError::InvalidOperation(
231 "audio sync offset overflow converting milliseconds to nanoseconds".into(),
232 )
233 })
234 }
235
236 pub const fn nanoseconds(self) -> i64 {
237 self.0
238 }
239}
240
241#[cfg(test)]
242mod typed_audio_tests {
243 use super::*;
244
245 #[test]
246 fn mix_and_output_slot_are_bounded_and_distinct_types() {
247 assert_eq!(AudioMixIndex::new(0).unwrap().index(), 0);
248 assert_eq!(AudioEncoderSlot::new(0).unwrap().index(), 0);
249 assert!(AudioMixIndex::new(libobs::MAX_AUDIO_MIXES as usize).is_err());
250 assert!(AudioEncoderSlot::new(libobs::MAX_AUDIO_MIXES as usize).is_err());
251 }
252
253 #[test]
254 fn mix_mask_rejects_unknown_bits() {
255 let mix0 = AudioMixIndex::new(0).unwrap();
256 let mix2 = AudioMixIndex::new(2).unwrap();
257 let mask = AudioMixMask::empty().with_mix(mix0).with_mix(mix2);
258 assert_eq!(mask.bits(), 0b0101);
259 assert!(mask.contains(mix0));
260 assert!(AudioMixMask::from_bits(1 << libobs::MAX_AUDIO_MIXES).is_err());
261 }
262
263 #[test]
264 fn typed_volume_and_sync_units_validate_boundaries() {
265 assert_eq!(LinearVolume::unity().multiplier(), 1.0);
266 assert!(LinearVolume::new(-0.1).is_err());
267 assert!(LinearVolume::new(f32::NAN).is_err());
268 assert_eq!(
269 AudioSyncOffset::from_milliseconds(250)
270 .unwrap()
271 .nanoseconds(),
272 250_000_000
273 );
274 }
275}