Skip to content

Commit f04e19c

Browse files
committed
ScriptProcessorNode: expose public constructor and options
1 parent c82d706 commit f04e19c

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

src/context/base.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,19 +215,27 @@ pub trait BaseAudioContext {
215215
}
216216

217217
/// Creates an `ScriptProcessorNode` for custom audio processing (deprecated);
218+
///
219+
/// # Panics
220+
///
221+
/// This function panics if:
222+
/// - `buffer_size` is not 256, 512, 1024, 2048, 4096, 8192, or 16384
223+
/// - the number of input and output channels are both zero
224+
/// - either of the channel counts exceed [`MAX_CHANNELS`]
218225
#[must_use]
219226
fn create_script_processor(
220227
&self,
221228
buffer_size: usize,
222229
number_of_input_channels: usize,
223230
number_of_output_channels: usize,
224231
) -> node::ScriptProcessorNode {
225-
node::ScriptProcessorNode::new(
226-
self.base(),
232+
let options = node::ScriptProcessorOptions {
227233
buffer_size,
228234
number_of_input_channels,
229235
number_of_output_channels,
230-
)
236+
};
237+
238+
node::ScriptProcessorNode::new(self.base(), options)
231239
}
232240

233241
/// Creates an `StereoPannerNode` to pan a stereo output

src/node/script_processor.rs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1+
use super::{AudioNode, AudioNodeOptions, ChannelConfig, ChannelCountMode, ChannelInterpretation};
12
use crate::context::{AudioContextRegistration, BaseAudioContext};
23
use crate::events::{AudioProcessingEvent, EventHandler, EventPayload, EventType};
3-
use crate::node::{ChannelCountMode, ChannelInterpretation};
44
use crate::render::{
55
AudioParamValues, AudioProcessor, AudioRenderQuantum, AudioWorkletGlobalScope,
66
};
77
use crate::{AudioBuffer, RENDER_QUANTUM_SIZE};
88

9-
use super::{AudioNode, AudioNodeOptions, ChannelConfig};
10-
119
use std::any::Any;
1210

11+
/// Options for constructing an [`ScriptProcessorNode`]
12+
#[derive(Clone, Debug)]
13+
pub struct ScriptProcessorOptions {
14+
pub buffer_size: usize,
15+
pub number_of_input_channels: usize,
16+
pub number_of_output_channels: usize,
17+
}
18+
1319
/// An AudioNode which can generate, process, or analyse audio directly using a script (deprecated)
1420
#[derive(Debug)]
1521
pub struct ScriptProcessorNode {
@@ -56,12 +62,26 @@ impl AudioNode for ScriptProcessorNode {
5662
}
5763

5864
impl ScriptProcessorNode {
59-
pub(crate) fn new<C: BaseAudioContext>(
60-
context: &C,
61-
buffer_size: usize,
62-
number_of_input_channels: usize,
63-
number_of_output_channels: usize,
64-
) -> Self {
65+
/// Creates a `ScriptProcessorNode`
66+
///
67+
/// # Arguments
68+
///
69+
/// - `context` - Audio context in which the node will live
70+
/// - `options` - node options
71+
///
72+
/// # Panics
73+
///
74+
/// This function panics if:
75+
/// - `buffer_size` is not 256, 512, 1024, 2048, 4096, 8192, or 16384
76+
/// - the number of input and output channels are both zero
77+
/// - either of the channel counts exceed [`MAX_CHANNELS`]
78+
pub fn new<C: BaseAudioContext>(context: &C, options: ScriptProcessorOptions) -> Self {
79+
let ScriptProcessorOptions {
80+
buffer_size,
81+
number_of_input_channels,
82+
number_of_output_channels,
83+
} = options;
84+
6585
assert!(
6686
(buffer_size / 256).is_power_of_two() && buffer_size <= 16384,
6787
"IndexSizeError - bufferSize must be one of: 256, 512, 1024, 2048, 4096, 8192, 16384",

0 commit comments

Comments
 (0)