|
| 1 | +use crate::context::{AudioContextRegistration, BaseAudioContext}; |
| 2 | +use crate::events::{AudioProcessingEvent, EventHandler, EventPayload, EventType}; |
| 3 | +use crate::node::{ChannelCountMode, ChannelInterpretation}; |
| 4 | +use crate::render::{ |
| 5 | + AudioParamValues, AudioProcessor, AudioRenderQuantum, AudioWorkletGlobalScope, |
| 6 | +}; |
| 7 | +use crate::{AudioBuffer, RENDER_QUANTUM_SIZE}; |
| 8 | + |
| 9 | +use super::{AudioNode, ChannelConfig, ChannelConfigOptions}; |
| 10 | + |
| 11 | +use std::any::Any; |
| 12 | + |
| 13 | +/// An AudioNode which can generate, process, or analyse audio directly using a script (deprecated) |
| 14 | +#[derive(Debug)] |
| 15 | +pub struct ScriptProcessorNode { |
| 16 | + registration: AudioContextRegistration, |
| 17 | + channel_config: ChannelConfig, |
| 18 | + // bufferSize MUST be one of the following values: 256, 512, 1024, 2048, 4096, 8192, 16384 |
| 19 | + buffer_size: usize, |
| 20 | +} |
| 21 | + |
| 22 | +impl AudioNode for ScriptProcessorNode { |
| 23 | + fn registration(&self) -> &AudioContextRegistration { |
| 24 | + &self.registration |
| 25 | + } |
| 26 | + |
| 27 | + fn channel_config(&self) -> &ChannelConfig { |
| 28 | + &self.channel_config |
| 29 | + } |
| 30 | + |
| 31 | + fn number_of_inputs(&self) -> usize { |
| 32 | + 1 |
| 33 | + } |
| 34 | + |
| 35 | + fn number_of_outputs(&self) -> usize { |
| 36 | + 1 |
| 37 | + } |
| 38 | + |
| 39 | + // TODO channel config constraints |
| 40 | +} |
| 41 | + |
| 42 | +impl ScriptProcessorNode { |
| 43 | + pub(crate) fn new<C: BaseAudioContext>( |
| 44 | + context: &C, |
| 45 | + buffer_size: usize, |
| 46 | + number_of_input_channels: usize, |
| 47 | + number_of_output_channels: usize, |
| 48 | + ) -> Self { |
| 49 | + // TODO assert valid arguments |
| 50 | + |
| 51 | + context.base().register(move |registration| { |
| 52 | + let render = ScriptProcessorRenderer { |
| 53 | + buffer: None, |
| 54 | + buffer_size, |
| 55 | + number_of_output_channels, |
| 56 | + }; |
| 57 | + |
| 58 | + let channel_config = ChannelConfigOptions { |
| 59 | + count: number_of_input_channels, |
| 60 | + count_mode: ChannelCountMode::Explicit, |
| 61 | + interpretation: ChannelInterpretation::Speakers, |
| 62 | + }; |
| 63 | + |
| 64 | + let node = ScriptProcessorNode { |
| 65 | + registration, |
| 66 | + channel_config: channel_config.into(), |
| 67 | + buffer_size, |
| 68 | + }; |
| 69 | + |
| 70 | + (node, Box::new(render)) |
| 71 | + }) |
| 72 | + } |
| 73 | + |
| 74 | + pub fn buffer_size(&self) -> usize { |
| 75 | + self.buffer_size |
| 76 | + } |
| 77 | + |
| 78 | + /// Register callback to run when the AudioProcessingEvent is dispatched |
| 79 | + /// |
| 80 | + /// The event handler processes audio from the input (if any) by accessing the audio data from |
| 81 | + /// the inputBuffer attribute. The audio data which is the result of the processing (or the |
| 82 | + /// synthesized data if there are no inputs) is then placed into the outputBuffer. |
| 83 | + /// |
| 84 | + /// Only a single event handler is active at any time. Calling this method multiple times will |
| 85 | + /// override the previous event handler. |
| 86 | + pub fn set_onaudioprocess<F: FnMut(&mut AudioProcessingEvent) + Send + 'static>( |
| 87 | + &self, |
| 88 | + mut callback: F, |
| 89 | + ) { |
| 90 | + let registration = self.registration.clone(); |
| 91 | + let callback = move |v| { |
| 92 | + let mut payload = match v { |
| 93 | + EventPayload::AudioProcessing(v) => v, |
| 94 | + _ => unreachable!(), |
| 95 | + }; |
| 96 | + callback(&mut payload); |
| 97 | + registration.post_message(payload.output_buffer); |
| 98 | + }; |
| 99 | + |
| 100 | + self.context().set_event_handler( |
| 101 | + EventType::AudioProcessing(self.registration().id()), |
| 102 | + EventHandler::Multiple(Box::new(callback)), |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + /// Unset the callback to run when the AudioProcessingEvent is dispatched |
| 107 | + pub fn clear_onaudioprocess(&self) { |
| 108 | + self.context() |
| 109 | + .clear_event_handler(EventType::AudioProcessing(self.registration().id())); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +struct ScriptProcessorRenderer { |
| 114 | + buffer: Option<AudioRenderQuantum>, // TODO buffer_size |
| 115 | + buffer_size: usize, |
| 116 | + number_of_output_channels: usize, |
| 117 | +} |
| 118 | + |
| 119 | +// SAFETY: |
| 120 | +// AudioRenderQuantums are not Send but we promise the `buffer` is None before we ship it to the |
| 121 | +// render thread. |
| 122 | +#[allow(clippy::non_send_fields_in_send_ty)] |
| 123 | +unsafe impl Send for ScriptProcessorRenderer {} |
| 124 | + |
| 125 | +impl AudioProcessor for ScriptProcessorRenderer { |
| 126 | + fn process( |
| 127 | + &mut self, |
| 128 | + inputs: &[AudioRenderQuantum], |
| 129 | + outputs: &mut [AudioRenderQuantum], |
| 130 | + _params: AudioParamValues<'_>, |
| 131 | + scope: &AudioWorkletGlobalScope, |
| 132 | + ) -> bool { |
| 133 | + // single input/output node |
| 134 | + let input = &inputs[0]; |
| 135 | + let output = &mut outputs[0]; |
| 136 | + |
| 137 | + let mut silence = input.clone(); |
| 138 | + silence.make_silent(); |
| 139 | + if let Some(buffer) = self.buffer.replace(silence) { |
| 140 | + *output = buffer; |
| 141 | + } |
| 142 | + |
| 143 | + // TODO buffer_size |
| 144 | + let input_samples = input.channels().iter().map(|c| c.to_vec()).collect(); |
| 145 | + let input_buffer = AudioBuffer::from(input_samples, scope.sample_rate); |
| 146 | + let output_samples = vec![vec![0.; RENDER_QUANTUM_SIZE]; self.number_of_output_channels]; |
| 147 | + let output_buffer = AudioBuffer::from(output_samples, scope.sample_rate); |
| 148 | + |
| 149 | + let playback_time = |
| 150 | + scope.current_time + (RENDER_QUANTUM_SIZE as f32 / scope.sample_rate) as f64; // TODO |
| 151 | + scope.send_audio_processing_event(input_buffer, output_buffer, playback_time); |
| 152 | + |
| 153 | + true // TODO - spec says false but that seems weird |
| 154 | + } |
| 155 | + |
| 156 | + fn onmessage(&mut self, msg: &mut dyn Any) { |
| 157 | + if let Some(buffer) = msg.downcast_mut::<AudioBuffer>() { |
| 158 | + if let Some(render_quantum) = &mut self.buffer { |
| 159 | + buffer |
| 160 | + .channels() |
| 161 | + .iter() |
| 162 | + .zip(render_quantum.channels_mut()) |
| 163 | + .for_each(|(i, o)| o.copy_from_slice(i.as_slice())); // TODO bounds check |
| 164 | + } |
| 165 | + return; |
| 166 | + }; |
| 167 | + |
| 168 | + log::warn!("ScriptProcessorRenderer: Dropping incoming message {msg:?}"); |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +#[cfg(test)] |
| 173 | +mod tests { |
| 174 | + use super::*; |
| 175 | + use crate::context::OfflineAudioContext; |
| 176 | + use float_eq::assert_float_eq; |
| 177 | + |
| 178 | + #[test] |
| 179 | + fn test() { |
| 180 | + // TODO how to test? |
| 181 | + } |
| 182 | +} |
0 commit comments