use crate::context::{AudioContextRegistration, BaseAudioContext};
use crate::render::{
AudioParamValues, AudioProcessor, AudioRenderQuantum, AudioWorkletGlobalScope,
};
use super::{AudioNode, AudioNodeOptions, ChannelConfig, ChannelCountMode, ChannelInterpretation};
#[derive(Debug)]
pub struct AudioDestinationNode {
registration: AudioContextRegistration,
channel_config: ChannelConfig,
}
impl AudioNode for AudioDestinationNode {
fn registration(&self) -> &AudioContextRegistration {
&self.registration
}
fn channel_config(&self) -> &ChannelConfig {
&self.channel_config
}
fn number_of_inputs(&self) -> usize {
1
}
fn number_of_outputs(&self) -> usize {
1
}
fn set_channel_count(&self, v: usize) {
assert!(
!self.registration.context().offline() || v == self.max_channel_count(),
"NotSupportedError - not allowed to change OfflineAudioContext destination channel count"
);
assert!(
v <= self.max_channel_count(),
"IndexSizeError - channel count cannot be greater than maxChannelCount ({})",
self.max_channel_count()
);
self.channel_config.set_count(v, self.registration());
}
fn set_channel_count_mode(&self, v: ChannelCountMode) {
assert!(
!self.registration.context().offline() || v == ChannelCountMode::Explicit,
"InvalidStateError - AudioDestinationNode has channel count mode constraints",
);
self.channel_config.set_count_mode(v, self.registration());
}
}
impl AudioDestinationNode {
pub(crate) fn new<C: BaseAudioContext>(context: &C, channel_count: usize) -> Self {
context.base().register(move |registration| {
let channel_config = AudioNodeOptions {
channel_count,
channel_count_mode: ChannelCountMode::Explicit,
channel_interpretation: ChannelInterpretation::Speakers,
}
.into();
let node = Self {
registration,
channel_config,
};
let proc = DestinationRenderer {};
(node, Box::new(proc))
})
}
pub(crate) fn into_channel_config(self) -> ChannelConfig {
self.channel_config
}
pub(crate) fn from_raw_parts(
registration: AudioContextRegistration,
channel_config: ChannelConfig,
) -> Self {
Self {
registration,
channel_config,
}
}
pub fn max_channel_count(&self) -> usize {
self.registration.context().base().max_channel_count()
}
}
struct DestinationRenderer {}
impl AudioProcessor for DestinationRenderer {
fn process(
&mut self,
inputs: &[AudioRenderQuantum],
outputs: &mut [AudioRenderQuantum],
_params: AudioParamValues<'_>,
_scope: &AudioWorkletGlobalScope,
) -> bool {
let input = &inputs[0];
let output = &mut outputs[0];
*output = input.clone();
true
}
fn has_side_effects(&self) -> bool {
true }
}