Skip to content

Commit b30c58e

Browse files
authored
Merge pull request #348 from b-ma/docs/options-and-destination
Add note on missing `AudioNodeOptions` in sources and properly document `AudioDestination`
2 parents eb72d34 + 557dc86 commit b30c58e

File tree

7 files changed

+77
-24
lines changed

7 files changed

+77
-24
lines changed

src/node/audio_buffer_source.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ use super::{AudioNode, AudioScheduledSourceNode, ChannelConfig};
1818
// double loopStart = 0;
1919
// float playbackRate = 1;
2020
// };
21+
//
22+
// @note - Does extend AudioNodeOptions but they are useless for source nodes as
23+
// they instruct how to upmix the inputs.
24+
// This is a common source of confusion, see e.g. https://github.com/mdn/content/pull/18472, and
25+
// an issue in the spec, see discussion in https://github.com/WebAudio/web-audio-api/issues/2496
2126
#[derive(Clone, Debug)]
2227
pub struct AudioBufferSourceOptions {
2328
pub buffer: Option<AudioBuffer>,

src/node/constant_source.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ use super::{AudioNode, AudioScheduledSourceNode, ChannelConfig};
1111
// dictionary ConstantSourceOptions {
1212
// float offset = 1;
1313
// };
14+
//
15+
// @note - Does not extend AudioNodeOptions because AudioNodeOptions are
16+
// useless for source nodes as they instruct how to upmix the inputs.
17+
// This is a common source of confusion, see e.g. https://github.com/mdn/content/pull/18472
1418
#[derive(Clone, Debug)]
1519
pub struct ConstantSourceOptions {
1620
pub offset: f32,

src/node/destination.rs

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,36 @@ use super::{
55
AudioNode, ChannelConfig, ChannelConfigOptions, ChannelCountMode, ChannelInterpretation,
66
};
77

8-
/// Representing the final audio destination and is what the user will ultimately hear.
8+
/// The AudioDestinationNode interface represents the terminal node of an audio
9+
/// graph in a given context. usually the speakers of your device, or the node that
10+
/// will "record" the audio data with an OfflineAudioContext.
11+
///
12+
/// The output of a AudioDestinationNode is produced by summing its input, allowing to capture
13+
/// the output of an AudioContext into, for example, a MediaStreamAudioDestinationNode, or a
14+
/// MediaRecorder.
15+
///
16+
/// - MDN documentation: <https://developer.mozilla.org/en-US/docs/Web/API/AudioDestinationNode>
17+
/// - specification: <https://webaudio.github.io/web-audio-api/#AudioDestinationNode>
18+
/// - see also: [`BaseAudioContext::destination`](crate::context::BaseAudioContext::destination)
19+
///
20+
/// # Usage
21+
///
22+
/// ```no_run
23+
/// use web_audio_api::context::{BaseAudioContext, AudioContext};
24+
/// use web_audio_api::node::{AudioNode, AudioScheduledSourceNode};
25+
///
26+
/// let context = AudioContext::default();
27+
///
28+
/// let osc = context.create_oscillator();
29+
/// osc.connect(&context.destination());
30+
/// osc.start();
31+
/// ```
32+
///
933
pub struct AudioDestinationNode {
1034
registration: AudioContextRegistration,
1135
channel_config: ChannelConfig,
1236
}
1337

14-
struct DestinationRenderer {}
15-
16-
impl AudioProcessor for DestinationRenderer {
17-
fn process(
18-
&mut self,
19-
inputs: &[AudioRenderQuantum],
20-
outputs: &mut [AudioRenderQuantum],
21-
_params: AudioParamValues<'_>,
22-
_scope: &RenderScope,
23-
) -> bool {
24-
// single input/output node
25-
let input = &inputs[0];
26-
let output = &mut outputs[0];
27-
28-
// just move input to output
29-
*output = input.clone();
30-
31-
true
32-
}
33-
}
34-
3538
impl AudioNode for AudioDestinationNode {
3639
fn registration(&self) -> &AudioContextRegistration {
3740
&self.registration
@@ -109,3 +112,24 @@ impl AudioDestinationNode {
109112
self.registration.context().base().max_channel_count()
110113
}
111114
}
115+
116+
struct DestinationRenderer {}
117+
118+
impl AudioProcessor for DestinationRenderer {
119+
fn process(
120+
&mut self,
121+
inputs: &[AudioRenderQuantum],
122+
outputs: &mut [AudioRenderQuantum],
123+
_params: AudioParamValues<'_>,
124+
_scope: &RenderScope,
125+
) -> bool {
126+
// single input/output node
127+
let input = &inputs[0];
128+
let output = &mut outputs[0];
129+
130+
// just move input to output
131+
*output = input.clone();
132+
133+
true
134+
}
135+
}

src/node/media_element_source.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ use crate::RENDER_QUANTUM_SIZE;
66
use super::{AudioNode, ChannelConfig, MediaStreamRenderer};
77

88
/// Options for constructing a [`MediaElementAudioSourceNode`]
9+
// dictionary MediaElementAudioSourceOptions {
10+
// required HTMLMediaElement mediaElement;
11+
// };
12+
//
13+
// @note - Does not extend AudioNodeOptions because AudioNodeOptions are
14+
// useless for source nodes as they instruct how to upmix the inputs.
15+
// This is a common source of confusion, see e.g. https://github.com/mdn/content/pull/18472
916
pub struct MediaElementAudioSourceOptions<'a> {
1017
pub media_element: &'a mut MediaElement,
1118
}

src/node/media_stream_source.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ use super::{AudioNode, ChannelConfig, MediaStreamRenderer};
99
// dictionary MediaStreamAudioSourceOptions {
1010
// required MediaStream mediaStream;
1111
// };
12+
//
13+
// @note - Does not extend AudioNodeOptions because AudioNodeOptions are
14+
// useless for source nodes as they instruct how to upmix the inputs.
15+
// This is a common source of confusion, see e.g. https://github.com/mdn/content/pull/18472
1216
pub struct MediaStreamAudioSourceOptions<'a> {
1317
pub media_stream: &'a MediaStream,
1418
}

src/node/media_stream_track_source.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ use crate::RENDER_QUANTUM_SIZE;
66
use super::{AudioNode, ChannelConfig, MediaStreamRenderer};
77

88
/// Options for constructing a [`MediaStreamTrackAudioSourceNode`]
9-
// dictionary MediaStreamAudioSourceOptions {
10-
// required MediaStream mediaStream;
9+
// dictionary MediaStreamTrackAudioSourceOptions {
10+
// required MediaStreamTrack mediaStreamTrack;
1111
// };
12+
//
13+
// @note - Does not extend AudioNodeOptions because AudioNodeOptions are
14+
// useless for source nodes as they instruct how to upmix the inputs.
15+
// This is a common source of confusion, see e.g. https://github.com/mdn/content/pull/18472
1216
pub struct MediaStreamTrackAudioSourceOptions<'a> {
1317
pub media_stream_track: &'a MediaStreamTrack,
1418
}

src/node/oscillator.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ use super::{
2121
// float detune = 0;
2222
// PeriodicWave periodicWave;
2323
// };
24+
//
25+
// @note - Does extend AudioNodeOptions but they are useless for source nodes as
26+
// they instruct how to upmix the inputs.
27+
// This is a common source of confusion, see e.g. https://github.com/mdn/content/pull/18472, and
28+
// an issue in the spec, see discussion in https://github.com/WebAudio/web-audio-api/issues/2496
2429
#[derive(Clone, Debug)]
2530
pub struct OscillatorOptions {
2631
/// The shape of the periodic waveform

0 commit comments

Comments
 (0)