Skip to main content

dioxus_audio/
types.rs

1//! Platform-neutral audio value types.
2
3use std::fmt;
4use std::time::Duration;
5
6#[derive(Clone, Debug, PartialEq, Eq)]
7pub struct AudioData {
8    pub bytes: Vec<u8>,
9    pub mime_type: String,
10}
11
12impl AudioData {
13    pub fn new(bytes: Vec<u8>, mime_type: impl Into<String>) -> Self {
14        Self {
15            bytes,
16            mime_type: mime_type.into(),
17        }
18    }
19}
20
21/// Opaque identity assigned to one Recording by its Recorder.
22#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
23pub struct RecordingId(u64);
24
25impl RecordingId {
26    pub(crate) fn from_generation(generation: u64) -> Self {
27        Self(generation)
28    }
29}
30
31/// An ordered encoded fragment emitted during a Recording.
32///
33/// A Recording Chunk is not guaranteed to be independently playable.
34#[derive(Clone, Debug, PartialEq, Eq)]
35pub struct RecordingChunk {
36    pub recording_id: RecordingId,
37    pub sequence: u64,
38    pub bytes: Vec<u8>,
39    pub media_type: String,
40}
41
42#[derive(Clone, Debug, PartialEq, Eq, Hash)]
43pub struct AudioInputId(String);
44
45impl AudioInputId {
46    pub fn new(value: impl Into<String>) -> Self {
47        Self(value.into())
48    }
49
50    pub fn as_str(&self) -> &str {
51        &self.0
52    }
53}
54
55impl fmt::Display for AudioInputId {
56    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
57        formatter.write_str(&self.0)
58    }
59}
60
61#[derive(Clone, Debug, PartialEq, Eq)]
62pub struct AudioInputDevice {
63    pub id: AudioInputId,
64    pub label: String,
65    pub is_default: bool,
66}
67
68impl AudioInputDevice {
69    pub fn new(id: AudioInputId, label: impl Into<String>, is_default: bool) -> Self {
70        Self {
71            id,
72            label: label.into(),
73            is_default,
74        }
75    }
76
77    pub fn display_label(&self, index: usize) -> String {
78        if self.label.trim().is_empty() {
79            format!("Microphone {}", index + 1)
80        } else {
81            self.label.clone()
82        }
83    }
84}
85
86#[derive(Clone, Debug, PartialEq, Eq)]
87pub struct RecordedAudio {
88    pub recording_id: RecordingId,
89    pub audio: AudioData,
90    pub duration: Duration,
91    pub peaks: Vec<u8>,
92    pub input_device: Option<AudioInputId>,
93}