use biquad_filter_node::{BiquadFilterNodeMessage, BiquadFilterNodeOptions};
use block::{Block, Chunk, Tick};
use buffer_source_node::{AudioBufferSourceNodeMessage, AudioBufferSourceNodeOptions};
use channel_node::ChannelNodeOptions;
use constant_source_node::ConstantSourceNodeOptions;
use gain_node::GainNodeOptions;
use iir_filter_node::IIRFilterNodeOptions;
use media_element_source_node::MediaElementSourceNodeMessage;
use oscillator_node::{OscillatorNodeMessage, OscillatorNodeOptions};
use panner_node::{PannerNodeMessage, PannerNodeOptions};
use param::{Param, ParamRate, ParamType, UserAutomationEvent};
use servo_media_streams::{MediaSocket, MediaStreamId};
use std::cmp::min;
use std::sync::mpsc::Sender;
use stereo_panner::StereoPannerOptions;
use wave_shaper_node::{WaveShaperNodeMessage, WaveShaperNodeOptions};
pub enum AudioNodeInit {
AnalyserNode(Box<dyn FnMut(Block) + Send>),
BiquadFilterNode(BiquadFilterNodeOptions),
AudioBuffer,
AudioBufferSourceNode(AudioBufferSourceNodeOptions),
ChannelMergerNode(ChannelNodeOptions),
ChannelSplitterNode,
ConstantSourceNode(ConstantSourceNodeOptions),
ConvolverNode,
DelayNode,
DynamicsCompressionNode,
GainNode(GainNodeOptions),
IIRFilterNode(IIRFilterNodeOptions),
MediaElementSourceNode,
MediaStreamDestinationNode(Box<dyn MediaSocket>),
MediaStreamSourceNode(MediaStreamId),
OscillatorNode(OscillatorNodeOptions),
PannerNode(PannerNodeOptions),
PeriodicWave,
ScriptProcessorNode,
StereoPannerNode(StereoPannerOptions),
WaveShaperNode(WaveShaperNodeOptions),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AudioNodeType {
AudioListenerNode,
AnalyserNode,
BiquadFilterNode,
AudioBuffer,
AudioBufferSourceNode,
ChannelMergerNode,
ChannelSplitterNode,
ConstantSourceNode,
ConvolverNode,
DelayNode,
DestinationNode,
DynamicsCompressionNode,
GainNode,
IIRFilterNode,
MediaElementSourceNode,
MediaStreamDestinationNode,
MediaStreamSourceNode,
OscillatorNode,
PannerNode,
PeriodicWave,
ScriptProcessorNode,
StereoPannerNode,
WaveShaperNode,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum ChannelCountMode {
Max,
ClampedMax,
Explicit,
}
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum ChannelInterpretation {
Discrete,
Speakers,
}
#[derive(Copy, Clone)]
pub struct BlockInfo {
pub sample_rate: f32,
pub frame: Tick,
pub time: f64,
}
impl BlockInfo {
pub fn absolute_tick(&self, tick: Tick) -> Tick {
self.frame + tick
}
}
pub struct ChannelInfo {
pub count: u8,
pub mode: ChannelCountMode,
pub interpretation: ChannelInterpretation,
pub context_channel_count: u8,
}
impl Default for ChannelInfo {
fn default() -> Self {
ChannelInfo {
count: 2,
mode: ChannelCountMode::Max,
interpretation: ChannelInterpretation::Speakers,
context_channel_count: 2,
}
}
}
impl ChannelInfo {
pub fn computed_number_of_channels(&self) -> u8 {
match self.mode {
ChannelCountMode::Max => self.context_channel_count,
ChannelCountMode::ClampedMax => min(self.count, self.context_channel_count),
ChannelCountMode::Explicit => self.count,
}
}
}
pub(crate) trait AudioNodeCommon {
fn channel_info(&self) -> &ChannelInfo;
fn channel_info_mut(&mut self) -> &mut ChannelInfo;
}
pub(crate) trait AudioNodeEngine: Send + AudioNodeCommon {
fn node_type(&self) -> AudioNodeType;
fn process(&mut self, inputs: Chunk, info: &BlockInfo) -> Chunk;
fn message(&mut self, msg: AudioNodeMessage, sample_rate: f32) {
match msg {
AudioNodeMessage::GetParamValue(id, tx) => {
let _ = tx.send(self.get_param(id).value());
}
AudioNodeMessage::SetChannelCount(c) => self.set_channel_count(c),
AudioNodeMessage::SetChannelMode(c) => self.set_channel_count_mode(c),
AudioNodeMessage::SetChannelInterpretation(c) => self.set_channel_interpretation(c),
AudioNodeMessage::SetParam(id, event) => {
self.get_param(id).insert_event(event.to_event(sample_rate))
}
AudioNodeMessage::SetParamRate(id, rate) => self.get_param(id).set_rate(rate),
_ => self.message_specific(msg, sample_rate),
}
}
fn message_specific(&mut self, _: AudioNodeMessage, _sample_rate: f32) {}
fn input_count(&self) -> u32 {
1
}
fn output_count(&self) -> u32 {
1
}
fn channel_count(&self) -> u8 {
self.channel_info().count
}
fn channel_count_mode(&self) -> ChannelCountMode {
self.channel_info().mode
}
fn channel_interpretation(&self) -> ChannelInterpretation {
self.channel_info().interpretation
}
fn set_channel_interpretation(&mut self, i: ChannelInterpretation) {
self.channel_info_mut().interpretation = i
}
fn set_channel_count(&mut self, c: u8) {
self.channel_info_mut().count = c;
}
fn set_channel_count_mode(&mut self, m: ChannelCountMode) {
self.channel_info_mut().mode = m;
}
fn destination_data(&mut self) -> Option<Chunk> {
None
}
fn get_param(&mut self, _: ParamType) -> &mut Param {
panic!("No params on node {:?}", self.node_type())
}
fn set_listenerdata(&mut self, _: Block) {
panic!("can't accept listener connections")
}
}
pub enum AudioNodeMessage {
AudioBufferSourceNode(AudioBufferSourceNodeMessage),
AudioScheduledSourceNode(AudioScheduledSourceNodeMessage),
BiquadFilterNode(BiquadFilterNodeMessage),
GetParamValue(ParamType, Sender<f32>),
MediaElementSourceNode(MediaElementSourceNodeMessage),
OscillatorNode(OscillatorNodeMessage),
PannerNode(PannerNodeMessage),
SetChannelCount(u8),
SetChannelMode(ChannelCountMode),
SetChannelInterpretation(ChannelInterpretation),
SetParam(ParamType, UserAutomationEvent),
SetParamRate(ParamType, ParamRate),
WaveShaperNode(WaveShaperNodeMessage),
}
pub struct OnEndedCallback(pub Box<dyn FnOnce() + Send + 'static>);
impl OnEndedCallback {
pub fn new<F: FnOnce() + Send + 'static>(callback: F) -> Self {
OnEndedCallback(Box::new(callback))
}
}
pub enum AudioScheduledSourceNodeMessage {
Start(f64),
Stop(f64),
RegisterOnEndedCallback(OnEndedCallback),
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum ShouldPlay {
No,
Between(Tick, Tick),
}