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#[derive(Clone, Debug, PartialEq, Eq, Hash)]
22pub struct AudioInputId(String);
23
24impl AudioInputId {
25    pub fn new(value: impl Into<String>) -> Self {
26        Self(value.into())
27    }
28
29    pub fn as_str(&self) -> &str {
30        &self.0
31    }
32}
33
34impl fmt::Display for AudioInputId {
35    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
36        formatter.write_str(&self.0)
37    }
38}
39
40#[derive(Clone, Debug, PartialEq, Eq)]
41pub struct AudioInputDevice {
42    pub id: AudioInputId,
43    pub label: String,
44    pub is_default: bool,
45}
46
47impl AudioInputDevice {
48    pub fn new(id: AudioInputId, label: impl Into<String>, is_default: bool) -> Self {
49        Self {
50            id,
51            label: label.into(),
52            is_default,
53        }
54    }
55
56    pub fn display_label(&self, index: usize) -> String {
57        if self.label.trim().is_empty() {
58            format!("Microphone {}", index + 1)
59        } else {
60            self.label.clone()
61        }
62    }
63}
64
65#[derive(Clone, Debug, PartialEq, Eq)]
66pub struct RecordedAudio {
67    pub audio: AudioData,
68    pub duration: Duration,
69    pub peaks: Vec<u8>,
70    pub input_device: Option<AudioInputId>,
71}