Skip to main content

browser_protocol/webaudio/
mod.rs

1//! This domain allows inspection of Web Audio API.
2//! <https://webaudio.github.io/web-audio-api/>
3
4use serde::{Serialize, Deserialize};
5
6/// An unique ID for a graph object (AudioContext, AudioNode, AudioParam) in Web Audio API
7
8pub type GraphObjectId = String;
9
10/// Enum of BaseAudioContext types
11
12#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
13pub enum ContextType {
14    #[default]
15    Realtime,
16    Offline,
17}
18
19/// Enum of AudioContextState from the spec
20
21#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
22pub enum ContextState {
23    #[default]
24    Suspended,
25    Running,
26    Closed,
27    Interrupted,
28}
29
30/// Enum of AudioNode types
31
32pub type NodeType = String;
33
34/// Enum of AudioNode::ChannelCountMode from the spec
35
36#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
37pub enum ChannelCountMode {
38    #[default]
39    ClampedMax,
40    Explicit,
41    Max,
42}
43
44/// Enum of AudioNode::ChannelInterpretation from the spec
45
46#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
47pub enum ChannelInterpretation {
48    #[default]
49    Discrete,
50    Speakers,
51}
52
53/// Enum of AudioParam types
54
55pub type ParamType = String;
56
57/// Enum of AudioParam::AutomationRate from the spec
58
59#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
60pub enum AutomationRate {
61    #[default]
62    ARate,
63    KRate,
64}
65
66/// Fields in AudioContext that change in real-time.
67
68#[derive(Debug, Clone, Serialize, Deserialize, Default)]
69#[serde(rename_all = "camelCase")]
70pub struct ContextRealtimeData {
71    /// The current context time in second in BaseAudioContext.
72
73    pub currentTime: f64,
74    /// The time spent on rendering graph divided by render quantum duration,
75    /// and multiplied by 100. 100 means the audio renderer reached the full
76    /// capacity and glitch may occur.
77
78    pub renderCapacity: f64,
79    /// A running mean of callback interval.
80
81    pub callbackIntervalMean: f64,
82    /// A running variance of callback interval.
83
84    pub callbackIntervalVariance: f64,
85}
86
87/// Protocol object for BaseAudioContext
88
89#[derive(Debug, Clone, Serialize, Deserialize, Default)]
90#[serde(rename_all = "camelCase")]
91pub struct BaseAudioContext {
92
93    pub contextId: GraphObjectId,
94
95    pub contextType: ContextType,
96
97    pub contextState: ContextState,
98
99    #[serde(skip_serializing_if = "Option::is_none")]
100    pub realtimeData: Option<ContextRealtimeData>,
101    /// Platform-dependent callback buffer size.
102
103    pub callbackBufferSize: f64,
104    /// Number of output channels supported by audio hardware in use.
105
106    pub maxOutputChannelCount: f64,
107    /// Context sample rate.
108
109    pub sampleRate: f64,
110}
111
112/// Protocol object for AudioListener
113
114#[derive(Debug, Clone, Serialize, Deserialize, Default)]
115#[serde(rename_all = "camelCase")]
116pub struct AudioListener {
117
118    pub listenerId: GraphObjectId,
119
120    pub contextId: GraphObjectId,
121}
122
123/// Protocol object for AudioNode
124
125#[derive(Debug, Clone, Serialize, Deserialize, Default)]
126#[serde(rename_all = "camelCase")]
127pub struct AudioNode {
128
129    pub nodeId: GraphObjectId,
130
131    pub contextId: GraphObjectId,
132
133    pub nodeType: NodeType,
134
135    pub numberOfInputs: f64,
136
137    pub numberOfOutputs: f64,
138
139    pub channelCount: f64,
140
141    pub channelCountMode: ChannelCountMode,
142
143    pub channelInterpretation: ChannelInterpretation,
144}
145
146/// Protocol object for AudioParam
147
148#[derive(Debug, Clone, Serialize, Deserialize, Default)]
149#[serde(rename_all = "camelCase")]
150pub struct AudioParam {
151
152    pub paramId: GraphObjectId,
153
154    pub nodeId: GraphObjectId,
155
156    pub contextId: GraphObjectId,
157
158    pub paramType: ParamType,
159
160    pub rate: AutomationRate,
161
162    pub defaultValue: f64,
163
164    pub minValue: f64,
165
166    pub maxValue: f64,
167}
168
169/// Fetch the realtime data from the registered contexts.
170
171#[derive(Debug, Clone, Serialize, Deserialize, Default)]
172#[serde(rename_all = "camelCase")]
173pub struct GetRealtimeDataParams {
174
175    pub contextId: GraphObjectId,
176}
177
178/// Fetch the realtime data from the registered contexts.
179
180#[derive(Debug, Clone, Serialize, Deserialize, Default)]
181#[serde(rename_all = "camelCase")]
182pub struct GetRealtimeDataReturns {
183
184    pub realtimeData: ContextRealtimeData,
185}