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
4
5use serde::{Serialize, Deserialize};
6use serde_json::Value as JsonValue;
7use std::borrow::Cow;
8
9/// An unique ID for a graph object (AudioContext, AudioNode, AudioParam) in Web Audio API
10
11pub type GraphObjectId<'a> = Cow<'a, str>;
12
13/// Enum of BaseAudioContext types
14
15#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
16pub enum ContextType {
17    #[default]
18    #[serde(rename = "realtime")]
19    Realtime,
20    #[serde(rename = "offline")]
21    Offline,
22}
23
24/// Enum of AudioContextState from the spec
25
26#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
27pub enum ContextState {
28    #[default]
29    #[serde(rename = "suspended")]
30    Suspended,
31    #[serde(rename = "running")]
32    Running,
33    #[serde(rename = "closed")]
34    Closed,
35    #[serde(rename = "interrupted")]
36    Interrupted,
37}
38
39/// Enum of AudioNode types
40
41pub type NodeType<'a> = Cow<'a, str>;
42
43/// Enum of AudioNode::ChannelCountMode from the spec
44
45#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
46pub enum ChannelCountMode {
47    #[default]
48    #[serde(rename = "clamped-max")]
49    ClampedMax,
50    #[serde(rename = "explicit")]
51    Explicit,
52    #[serde(rename = "max")]
53    Max,
54}
55
56/// Enum of AudioNode::ChannelInterpretation from the spec
57
58#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
59pub enum ChannelInterpretation {
60    #[default]
61    #[serde(rename = "discrete")]
62    Discrete,
63    #[serde(rename = "speakers")]
64    Speakers,
65}
66
67/// Enum of AudioParam types
68
69pub type ParamType<'a> = Cow<'a, str>;
70
71/// Enum of AudioParam::AutomationRate from the spec
72
73#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
74pub enum AutomationRate {
75    #[default]
76    #[serde(rename = "a-rate")]
77    ARate,
78    #[serde(rename = "k-rate")]
79    KRate,
80}
81
82/// Fields in AudioContext that change in real-time.
83
84#[derive(Debug, Clone, Serialize, Deserialize, Default)]
85#[serde(rename_all = "camelCase")]
86pub struct ContextRealtimeData {
87    /// The current context time in second in BaseAudioContext.
88    #[serde(rename = "currentTime")]
89    current_time: f64,
90    /// The time spent on rendering graph divided by render quantum duration,
91    /// and multiplied by 100. 100 means the audio renderer reached the full
92    /// capacity and glitch may occur.
93    #[serde(rename = "renderCapacity")]
94    render_capacity: f64,
95    /// A running mean of callback interval.
96    #[serde(rename = "callbackIntervalMean")]
97    callback_interval_mean: f64,
98    /// A running variance of callback interval.
99    #[serde(rename = "callbackIntervalVariance")]
100    callback_interval_variance: f64,
101}
102
103impl ContextRealtimeData {
104    /// Creates a builder for this type with the required parameters:
105    /// * `current_time`: The current context time in second in BaseAudioContext.
106    /// * `render_capacity`: The time spent on rendering graph divided by render quantum duration, and multiplied by 100. 100 means the audio renderer reached the full capacity and glitch may occur.
107    /// * `callback_interval_mean`: A running mean of callback interval.
108    /// * `callback_interval_variance`: A running variance of callback interval.
109    pub fn builder(current_time: f64, render_capacity: f64, callback_interval_mean: f64, callback_interval_variance: f64) -> ContextRealtimeDataBuilder {
110        ContextRealtimeDataBuilder {
111            current_time: current_time,
112            render_capacity: render_capacity,
113            callback_interval_mean: callback_interval_mean,
114            callback_interval_variance: callback_interval_variance,
115        }
116    }
117    /// The current context time in second in BaseAudioContext.
118    pub fn current_time(&self) -> f64 { self.current_time }
119    /// The time spent on rendering graph divided by render quantum duration,
120    /// and multiplied by 100. 100 means the audio renderer reached the full
121    /// capacity and glitch may occur.
122    pub fn render_capacity(&self) -> f64 { self.render_capacity }
123    /// A running mean of callback interval.
124    pub fn callback_interval_mean(&self) -> f64 { self.callback_interval_mean }
125    /// A running variance of callback interval.
126    pub fn callback_interval_variance(&self) -> f64 { self.callback_interval_variance }
127}
128
129
130pub struct ContextRealtimeDataBuilder {
131    current_time: f64,
132    render_capacity: f64,
133    callback_interval_mean: f64,
134    callback_interval_variance: f64,
135}
136
137impl ContextRealtimeDataBuilder {
138    pub fn build(self) -> ContextRealtimeData {
139        ContextRealtimeData {
140            current_time: self.current_time,
141            render_capacity: self.render_capacity,
142            callback_interval_mean: self.callback_interval_mean,
143            callback_interval_variance: self.callback_interval_variance,
144        }
145    }
146}
147
148/// Protocol object for BaseAudioContext
149
150#[derive(Debug, Clone, Serialize, Deserialize, Default)]
151#[serde(rename_all = "camelCase")]
152pub struct BaseAudioContext<'a> {
153    #[serde(rename = "contextId")]
154    context_id: GraphObjectId<'a>,
155    #[serde(rename = "contextType")]
156    context_type: ContextType,
157    #[serde(rename = "contextState")]
158    context_state: ContextState,
159    #[serde(skip_serializing_if = "Option::is_none", rename = "realtimeData")]
160    realtime_data: Option<ContextRealtimeData>,
161    /// Platform-dependent callback buffer size.
162    #[serde(rename = "callbackBufferSize")]
163    callback_buffer_size: f64,
164    /// Number of output channels supported by audio hardware in use.
165    #[serde(rename = "maxOutputChannelCount")]
166    max_output_channel_count: f64,
167    /// Context sample rate.
168    #[serde(rename = "sampleRate")]
169    sample_rate: f64,
170}
171
172impl<'a> BaseAudioContext<'a> {
173    /// Creates a builder for this type with the required parameters:
174    /// * `context_id`: 
175    /// * `context_type`: 
176    /// * `context_state`: 
177    /// * `callback_buffer_size`: Platform-dependent callback buffer size.
178    /// * `max_output_channel_count`: Number of output channels supported by audio hardware in use.
179    /// * `sample_rate`: Context sample rate.
180    pub fn builder(context_id: impl Into<GraphObjectId<'a>>, context_type: impl Into<ContextType>, context_state: impl Into<ContextState>, callback_buffer_size: f64, max_output_channel_count: f64, sample_rate: f64) -> BaseAudioContextBuilder<'a> {
181        BaseAudioContextBuilder {
182            context_id: context_id.into(),
183            context_type: context_type.into(),
184            context_state: context_state.into(),
185            realtime_data: None,
186            callback_buffer_size: callback_buffer_size,
187            max_output_channel_count: max_output_channel_count,
188            sample_rate: sample_rate,
189        }
190    }
191    pub fn context_id(&self) -> &GraphObjectId<'a> { &self.context_id }
192    pub fn context_type(&self) -> &ContextType { &self.context_type }
193    pub fn context_state(&self) -> &ContextState { &self.context_state }
194    pub fn realtime_data(&self) -> Option<&ContextRealtimeData> { self.realtime_data.as_ref() }
195    /// Platform-dependent callback buffer size.
196    pub fn callback_buffer_size(&self) -> f64 { self.callback_buffer_size }
197    /// Number of output channels supported by audio hardware in use.
198    pub fn max_output_channel_count(&self) -> f64 { self.max_output_channel_count }
199    /// Context sample rate.
200    pub fn sample_rate(&self) -> f64 { self.sample_rate }
201}
202
203
204pub struct BaseAudioContextBuilder<'a> {
205    context_id: GraphObjectId<'a>,
206    context_type: ContextType,
207    context_state: ContextState,
208    realtime_data: Option<ContextRealtimeData>,
209    callback_buffer_size: f64,
210    max_output_channel_count: f64,
211    sample_rate: f64,
212}
213
214impl<'a> BaseAudioContextBuilder<'a> {
215    pub fn realtime_data(mut self, realtime_data: ContextRealtimeData) -> Self { self.realtime_data = Some(realtime_data); self }
216    pub fn build(self) -> BaseAudioContext<'a> {
217        BaseAudioContext {
218            context_id: self.context_id,
219            context_type: self.context_type,
220            context_state: self.context_state,
221            realtime_data: self.realtime_data,
222            callback_buffer_size: self.callback_buffer_size,
223            max_output_channel_count: self.max_output_channel_count,
224            sample_rate: self.sample_rate,
225        }
226    }
227}
228
229/// Protocol object for AudioListener
230
231#[derive(Debug, Clone, Serialize, Deserialize, Default)]
232#[serde(rename_all = "camelCase")]
233pub struct AudioListener<'a> {
234    #[serde(rename = "listenerId")]
235    listener_id: GraphObjectId<'a>,
236    #[serde(rename = "contextId")]
237    context_id: GraphObjectId<'a>,
238}
239
240impl<'a> AudioListener<'a> {
241    /// Creates a builder for this type with the required parameters:
242    /// * `listener_id`: 
243    /// * `context_id`: 
244    pub fn builder(listener_id: impl Into<GraphObjectId<'a>>, context_id: impl Into<GraphObjectId<'a>>) -> AudioListenerBuilder<'a> {
245        AudioListenerBuilder {
246            listener_id: listener_id.into(),
247            context_id: context_id.into(),
248        }
249    }
250    pub fn listener_id(&self) -> &GraphObjectId<'a> { &self.listener_id }
251    pub fn context_id(&self) -> &GraphObjectId<'a> { &self.context_id }
252}
253
254
255pub struct AudioListenerBuilder<'a> {
256    listener_id: GraphObjectId<'a>,
257    context_id: GraphObjectId<'a>,
258}
259
260impl<'a> AudioListenerBuilder<'a> {
261    pub fn build(self) -> AudioListener<'a> {
262        AudioListener {
263            listener_id: self.listener_id,
264            context_id: self.context_id,
265        }
266    }
267}
268
269/// Protocol object for AudioNode
270
271#[derive(Debug, Clone, Serialize, Deserialize, Default)]
272#[serde(rename_all = "camelCase")]
273pub struct AudioNode<'a> {
274    #[serde(rename = "nodeId")]
275    node_id: GraphObjectId<'a>,
276    #[serde(rename = "contextId")]
277    context_id: GraphObjectId<'a>,
278    #[serde(rename = "nodeType")]
279    node_type: NodeType<'a>,
280    #[serde(rename = "numberOfInputs")]
281    number_of_inputs: f64,
282    #[serde(rename = "numberOfOutputs")]
283    number_of_outputs: f64,
284    #[serde(rename = "channelCount")]
285    channel_count: f64,
286    #[serde(rename = "channelCountMode")]
287    channel_count_mode: ChannelCountMode,
288    #[serde(rename = "channelInterpretation")]
289    channel_interpretation: ChannelInterpretation,
290}
291
292impl<'a> AudioNode<'a> {
293    /// Creates a builder for this type with the required parameters:
294    /// * `node_id`: 
295    /// * `context_id`: 
296    /// * `node_type`: 
297    /// * `number_of_inputs`: 
298    /// * `number_of_outputs`: 
299    /// * `channel_count`: 
300    /// * `channel_count_mode`: 
301    /// * `channel_interpretation`: 
302    pub fn builder(node_id: impl Into<GraphObjectId<'a>>, context_id: impl Into<GraphObjectId<'a>>, node_type: impl Into<NodeType<'a>>, number_of_inputs: f64, number_of_outputs: f64, channel_count: f64, channel_count_mode: impl Into<ChannelCountMode>, channel_interpretation: impl Into<ChannelInterpretation>) -> AudioNodeBuilder<'a> {
303        AudioNodeBuilder {
304            node_id: node_id.into(),
305            context_id: context_id.into(),
306            node_type: node_type.into(),
307            number_of_inputs: number_of_inputs,
308            number_of_outputs: number_of_outputs,
309            channel_count: channel_count,
310            channel_count_mode: channel_count_mode.into(),
311            channel_interpretation: channel_interpretation.into(),
312        }
313    }
314    pub fn node_id(&self) -> &GraphObjectId<'a> { &self.node_id }
315    pub fn context_id(&self) -> &GraphObjectId<'a> { &self.context_id }
316    pub fn node_type(&self) -> &NodeType<'a> { &self.node_type }
317    pub fn number_of_inputs(&self) -> f64 { self.number_of_inputs }
318    pub fn number_of_outputs(&self) -> f64 { self.number_of_outputs }
319    pub fn channel_count(&self) -> f64 { self.channel_count }
320    pub fn channel_count_mode(&self) -> &ChannelCountMode { &self.channel_count_mode }
321    pub fn channel_interpretation(&self) -> &ChannelInterpretation { &self.channel_interpretation }
322}
323
324
325pub struct AudioNodeBuilder<'a> {
326    node_id: GraphObjectId<'a>,
327    context_id: GraphObjectId<'a>,
328    node_type: NodeType<'a>,
329    number_of_inputs: f64,
330    number_of_outputs: f64,
331    channel_count: f64,
332    channel_count_mode: ChannelCountMode,
333    channel_interpretation: ChannelInterpretation,
334}
335
336impl<'a> AudioNodeBuilder<'a> {
337    pub fn build(self) -> AudioNode<'a> {
338        AudioNode {
339            node_id: self.node_id,
340            context_id: self.context_id,
341            node_type: self.node_type,
342            number_of_inputs: self.number_of_inputs,
343            number_of_outputs: self.number_of_outputs,
344            channel_count: self.channel_count,
345            channel_count_mode: self.channel_count_mode,
346            channel_interpretation: self.channel_interpretation,
347        }
348    }
349}
350
351/// Protocol object for AudioParam
352
353#[derive(Debug, Clone, Serialize, Deserialize, Default)]
354#[serde(rename_all = "camelCase")]
355pub struct AudioParam<'a> {
356    #[serde(rename = "paramId")]
357    param_id: GraphObjectId<'a>,
358    #[serde(rename = "nodeId")]
359    node_id: GraphObjectId<'a>,
360    #[serde(rename = "contextId")]
361    context_id: GraphObjectId<'a>,
362    #[serde(rename = "paramType")]
363    param_type: ParamType<'a>,
364    rate: AutomationRate,
365    #[serde(rename = "defaultValue")]
366    default_value: f64,
367    #[serde(rename = "minValue")]
368    min_value: f64,
369    #[serde(rename = "maxValue")]
370    max_value: f64,
371}
372
373impl<'a> AudioParam<'a> {
374    /// Creates a builder for this type with the required parameters:
375    /// * `param_id`: 
376    /// * `node_id`: 
377    /// * `context_id`: 
378    /// * `param_type`: 
379    /// * `rate`: 
380    /// * `default_value`: 
381    /// * `min_value`: 
382    /// * `max_value`: 
383    pub fn builder(param_id: impl Into<GraphObjectId<'a>>, node_id: impl Into<GraphObjectId<'a>>, context_id: impl Into<GraphObjectId<'a>>, param_type: impl Into<ParamType<'a>>, rate: impl Into<AutomationRate>, default_value: f64, min_value: f64, max_value: f64) -> AudioParamBuilder<'a> {
384        AudioParamBuilder {
385            param_id: param_id.into(),
386            node_id: node_id.into(),
387            context_id: context_id.into(),
388            param_type: param_type.into(),
389            rate: rate.into(),
390            default_value: default_value,
391            min_value: min_value,
392            max_value: max_value,
393        }
394    }
395    pub fn param_id(&self) -> &GraphObjectId<'a> { &self.param_id }
396    pub fn node_id(&self) -> &GraphObjectId<'a> { &self.node_id }
397    pub fn context_id(&self) -> &GraphObjectId<'a> { &self.context_id }
398    pub fn param_type(&self) -> &ParamType<'a> { &self.param_type }
399    pub fn rate(&self) -> &AutomationRate { &self.rate }
400    pub fn default_value(&self) -> f64 { self.default_value }
401    pub fn min_value(&self) -> f64 { self.min_value }
402    pub fn max_value(&self) -> f64 { self.max_value }
403}
404
405
406pub struct AudioParamBuilder<'a> {
407    param_id: GraphObjectId<'a>,
408    node_id: GraphObjectId<'a>,
409    context_id: GraphObjectId<'a>,
410    param_type: ParamType<'a>,
411    rate: AutomationRate,
412    default_value: f64,
413    min_value: f64,
414    max_value: f64,
415}
416
417impl<'a> AudioParamBuilder<'a> {
418    pub fn build(self) -> AudioParam<'a> {
419        AudioParam {
420            param_id: self.param_id,
421            node_id: self.node_id,
422            context_id: self.context_id,
423            param_type: self.param_type,
424            rate: self.rate,
425            default_value: self.default_value,
426            min_value: self.min_value,
427            max_value: self.max_value,
428        }
429    }
430}
431
432#[derive(Debug, Clone, Serialize, Deserialize, Default)]
433pub struct EnableParams {}
434
435impl EnableParams { pub const METHOD: &'static str = "WebAudio.enable"; }
436
437impl<'a> crate::CdpCommand<'a> for EnableParams {
438    const METHOD: &'static str = "WebAudio.enable";
439    type Response = crate::EmptyReturns;
440}
441
442#[derive(Debug, Clone, Serialize, Deserialize, Default)]
443pub struct DisableParams {}
444
445impl DisableParams { pub const METHOD: &'static str = "WebAudio.disable"; }
446
447impl<'a> crate::CdpCommand<'a> for DisableParams {
448    const METHOD: &'static str = "WebAudio.disable";
449    type Response = crate::EmptyReturns;
450}
451
452/// Fetch the realtime data from the registered contexts.
453
454#[derive(Debug, Clone, Serialize, Deserialize, Default)]
455#[serde(rename_all = "camelCase")]
456pub struct GetRealtimeDataParams<'a> {
457    #[serde(rename = "contextId")]
458    context_id: GraphObjectId<'a>,
459}
460
461impl<'a> GetRealtimeDataParams<'a> {
462    /// Creates a builder for this type with the required parameters:
463    /// * `context_id`: 
464    pub fn builder(context_id: impl Into<GraphObjectId<'a>>) -> GetRealtimeDataParamsBuilder<'a> {
465        GetRealtimeDataParamsBuilder {
466            context_id: context_id.into(),
467        }
468    }
469    pub fn context_id(&self) -> &GraphObjectId<'a> { &self.context_id }
470}
471
472
473pub struct GetRealtimeDataParamsBuilder<'a> {
474    context_id: GraphObjectId<'a>,
475}
476
477impl<'a> GetRealtimeDataParamsBuilder<'a> {
478    pub fn build(self) -> GetRealtimeDataParams<'a> {
479        GetRealtimeDataParams {
480            context_id: self.context_id,
481        }
482    }
483}
484
485/// Fetch the realtime data from the registered contexts.
486
487#[derive(Debug, Clone, Serialize, Deserialize, Default)]
488#[serde(rename_all = "camelCase")]
489pub struct GetRealtimeDataReturns {
490    #[serde(rename = "realtimeData")]
491    realtime_data: ContextRealtimeData,
492}
493
494impl GetRealtimeDataReturns {
495    /// Creates a builder for this type with the required parameters:
496    /// * `realtime_data`: 
497    pub fn builder(realtime_data: ContextRealtimeData) -> GetRealtimeDataReturnsBuilder {
498        GetRealtimeDataReturnsBuilder {
499            realtime_data: realtime_data,
500        }
501    }
502    pub fn realtime_data(&self) -> &ContextRealtimeData { &self.realtime_data }
503}
504
505
506pub struct GetRealtimeDataReturnsBuilder {
507    realtime_data: ContextRealtimeData,
508}
509
510impl GetRealtimeDataReturnsBuilder {
511    pub fn build(self) -> GetRealtimeDataReturns {
512        GetRealtimeDataReturns {
513            realtime_data: self.realtime_data,
514        }
515    }
516}
517
518impl<'a> GetRealtimeDataParams<'a> { pub const METHOD: &'static str = "WebAudio.getRealtimeData"; }
519
520impl<'a> crate::CdpCommand<'a> for GetRealtimeDataParams<'a> {
521    const METHOD: &'static str = "WebAudio.getRealtimeData";
522    type Response = GetRealtimeDataReturns;
523}