zng_view_api/
audio.rs

1//! Audio device types.
2
3use std::num::NonZeroU16;
4
5use bitflags::bitflags;
6use serde::{Deserialize, Serialize};
7use zng_txt::Txt;
8
9crate::declare_id! {
10    /// Audio device ID in channel.
11    ///
12    /// In the View Process this is mapped to a system id.
13    ///
14    /// In the App Process this is mapped to an unique id, but does not survived View crashes.
15    ///
16    /// The View Process defines the ID.
17    pub struct AudioDeviceId(_);
18
19    /// Id of a decoded audio in the cache.
20    ///
21    /// The View Process defines the ID.
22    pub struct AudioId(_);
23
24    /// Audio playback ID.
25    ///
26    /// The View Process defines the ID.
27    pub struct PlaybackId(_);
28}
29
30/// Info about an input or output device.
31#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
32#[non_exhaustive]
33pub struct AudioDeviceInfo {
34    /// Device display name.
35    pub name: Txt,
36    /// Device input/output capabilities.
37    pub capabilities: AudioDeviceCapability,
38    /// Input stream modes this device can produce.
39    pub input_modes: Vec<AudioStreamMode>,
40    /// Output stream modes this device can consume.
41    pub output_modes: Vec<AudioStreamMode>,
42}
43
44bitflags! {
45    /// Represents audio device input/output capabilities.
46    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
47    pub struct AudioDeviceCapability: u8 {
48        /// Device can generate audio streams.
49        const INPUT = 0b01;
50        /// Device can consume audio streams.
51        const OUTPUT = 0b11;
52    }
53}
54
55/// Represents steam capability of an audio device.
56#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
57#[non_exhaustive]
58pub struct AudioStreamMode {
59    /// Number of audio channels.
60    pub channels: NonZeroU16,
61    /// Minimum and maximum sample rate.
62    pub sample_rate: SampleRate,
63    /// Minimum and maximum supported buffer size.
64    pub buffer_size: BufferSize,
65}
66
67/// Represents the minimum and maximum sample rate per audio channel.
68///
69/// Values are in samples processed per second.
70#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
71pub struct SampleRate {
72    /// Minimum, inclusive.
73    pub min: u32,
74    /// Maximum, inclusive.
75    pub max: u32,
76}
77
78/// Represents the minimum and maximum supported buffer size for the device.
79#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
80#[non_exhaustive]
81pub enum BufferSize {
82    /// Range in frames per second.
83    Range {
84        /// Minimum, inclusive.
85        min: u32,
86        /// Maximum, inclusive.
87        max: u32,
88    },
89    /// Platform cannot describe buffer size for this device.
90    Unknown,
91}
92
93/// Represent an audio load/decode request.
94///
95/// # Unimplemented
96///
97/// This type is a stub for a future API, it is not implemented by app-process nor the default view-process.
98#[derive(Debug, Clone, Serialize, Deserialize)]
99#[non_exhaustive]
100pub struct AudioRequest<D> {
101    /// Audio data.
102    pub data: D,
103}
104
105/// Represents an audio playback request.
106///
107/// # Unimplemented
108///
109/// This type is a stub for a future API, it is not implemented by app-process nor the default view-process.
110#[derive(Debug, Clone, Serialize, Deserialize)]
111#[non_exhaustive]
112pub struct PlaybackRequest {
113    // app-process will define a timeline of AudioId clips, with effects and such
114    // this will allow the view-process to synchronize stuff
115}
116
117/// Represents an audio playback update request.
118///
119/// # Unimplemented
120///
121/// This type is a stub for a future API, it is not implemented by app-process nor the default view-process.
122#[derive(Debug, Clone, Serialize, Deserialize)]
123#[non_exhaustive]
124pub struct PlaybackUpdateRequest {
125    // pause, stop
126}