Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(audio): add biquad metadata and preserve offline graph silence

ldm05a29c6f2d506
diff --git a/moli-renderer-v8/src/context_bootstrap/assets/constructor_templates.rs b/moli-renderer-v8/src/context_bootstrap/assets/constructor_templates.rs
index a36ade629..e82e3f33f 100644
--- a/moli-renderer-v8/src/context_bootstrap/assets/constructor_templates.rs
+++ b/moli-renderer-v8/src/context_bootstrap/assets/constructor_templates.rs
@@ -616,6 +616,7 @@ pub(in crate::context_bootstrap) fn build_constructor_template<'s>(
| ConstructorKind::OscillatorNode
| ConstructorKind::DynamicsCompressorNode
| ConstructorKind::AnalyserNode
+ | ConstructorKind::BiquadFilterNode
| ConstructorKind::AudioParam
| ConstructorKind::AudioBuffer => {
v8::FunctionTemplate::builder(illegal_constructor_callback)
diff --git a/moli-renderer-v8/src/context_bootstrap/runtime_state.rs b/moli-renderer-v8/src/context_bootstrap/runtime_state.rs
index 12f1c4919..1a689469e 100644
--- a/moli-renderer-v8/src/context_bootstrap/runtime_state.rs
+++ b/moli-renderer-v8/src/context_bootstrap/runtime_state.rs
@@ -1745,6 +1745,7 @@ pub(crate) fn finish_context_bootstrap(
("OscillatorNode", "OscillatorNode"),
("DynamicsCompressorNode", "DynamicsCompressorNode"),
("AnalyserNode", "AnalyserNode"),
+ ("BiquadFilterNode", "BiquadFilterNode"),
("AudioParam", "AudioParam"),
("AudioBuffer", "AudioBuffer"),
("AbortSignal", "AbortSignal"),
diff --git a/moli-renderer-v8/src/context_bootstrap/specs/registry.rs b/moli-renderer-v8/src/context_bootstrap/specs/registry.rs
index cff6b3256..2422dbe58 100644
--- a/moli-renderer-v8/src/context_bootstrap/specs/registry.rs
+++ b/moli-renderer-v8/src/context_bootstrap/specs/registry.rs
@@ -1376,6 +1376,11 @@ const CONSTRUCTOR_SPECS_AFTER_STREAMS: &[ConstructorSpec] = &[
parent: None,
kind: ConstructorKind::AudioParam,
},
+ ConstructorSpec {
+ name: "BiquadFilterNode",
+ parent: None,
+ kind: ConstructorKind::BiquadFilterNode,
+ },
ConstructorSpec {
name: "AudioBuffer",
parent: None,
diff --git a/moli-renderer-v8/src/context_bootstrap/specs/types.rs b/moli-renderer-v8/src/context_bootstrap/specs/types.rs
index ef48b7be3..8e5bbf3da 100644
--- a/moli-renderer-v8/src/context_bootstrap/specs/types.rs
+++ b/moli-renderer-v8/src/context_bootstrap/specs/types.rs
@@ -162,6 +162,7 @@ pub(in crate::context_bootstrap) enum ConstructorKind {
OscillatorNode,
DynamicsCompressorNode,
AnalyserNode,
+ BiquadFilterNode,
AudioParam,
AudioBuffer,
Text,
diff --git a/moli-renderer-v8/src/context_bootstrap/web_audio_runtime.rs b/moli-renderer-v8/src/context_bootstrap/web_audio_runtime.rs
index b3ac57e5e..e70a5cbc0 100644
--- a/moli-renderer-v8/src/context_bootstrap/web_audio_runtime.rs
+++ b/moli-renderer-v8/src/context_bootstrap/web_audio_runtime.rs
@@ -12,6 +12,12 @@ use crate::util::{
use crate::webidl;
use moli_webapi_declare::{WebApiFunctionTemplate, WebApiObject};
+mod audio_param;
+mod biquad;
+mod graph;
+
+use audio_param::{audio_param, detune_param};
+
const AUDIO_CONTEXT_LISTENERS_SLOT: &str = "__moliAudioContextListeners";
const AUDIO_CONTEXT_MODULES_SLOT: &str = "__moliAudioContextModules";
const AUDIO_CONTEXT_MODULE_LIST_SLOT: &str = "__moliAudioContextModuleList";
@@ -165,6 +171,8 @@ struct OscillatorNodeObjectDeclaration<'scope> {
kind: &'static str,
#[webapi(data_property)]
frequency: v8::Local<'scope, v8::Object>,
+ #[webapi(data_property, readonly)]
+ detune: v8::Local<'scope, v8::Object>,
#[webapi(method, length = 1, callback = audio_node_connect_callback)]
connect: (),
#[webapi(method, length = 0, callback = audio_node_disconnect_callback)]
@@ -253,15 +261,6 @@ struct OfflineAudioCompletionEventDeclaration<'scope> {
rendered_buffer: v8::Local<'scope, v8::Object>,
}
-#[derive(WebApiObject)]
-#[webapi(interface = "AudioParam")]
-struct AudioParamObjectDeclaration {
- #[webapi(data_property)]
- value: f64,
- #[webapi(method, length = 2, callback = audio_param_set_value_at_time_callback)]
- set_value_at_time: (),
-}
-
#[derive(Default, WebApiObject)]
#[webapi(interface = "AudioDestinationNode", allow_empty)]
struct AudioDestinationNodeObjectDeclaration {}
@@ -414,6 +413,9 @@ struct AudioWorkletNodeTemplateDeclaration {
#[derive(WebApiFunctionTemplate)]
#[webapi(name = "BaseAudioContext", enumerable)]
struct BaseAudioContextPrototypeDeclaration {
+ #[webapi(method = "createBiquadFilter", length = 0, callback = biquad::create_biquad_filter)]
+ create_biquad_filter: (),
+
#[webapi(
method = "createOscillator",
length = 0,
@@ -464,7 +466,19 @@ pub(in crate::context_bootstrap) fn install_web_audio_template_bindings<'s>(
template: v8::Local<'s, v8::FunctionTemplate>,
interface_name: &str,
) {
+ if matches!(
+ interface_name,
+ "OscillatorNode"
+ | "DynamicsCompressorNode"
+ | "AnalyserNode"
+ | "BiquadFilterNode"
+ | "AudioDestinationNode"
+ ) {
+ graph::install(scope, template);
+ }
match interface_name {
+ "AudioParam" => audio_param::install(scope, template),
+ "BiquadFilterNode" => biquad::install(scope, template),
"BaseAudioContext" => {
BaseAudioContextPrototypeDeclaration::initialize_prototype_template(
scope,
@@ -507,7 +521,7 @@ fn audio_context_constructor_callback<'s>(
}
let context = args.this();
- let destination = audio_destination_node(scope);
+ let destination = audio_destination_node(scope, context);
let modules = new_web_audio_map_object(scope);
let module_list = v8::Array::new(scope, 0);
let processors = new_web_audio_map_object(scope);
@@ -842,6 +856,7 @@ fn audio_worklet_node_constructor_callback<'s>(
AudioWorkletNodeObjectDeclaration::new(context, port1)
.initialize(scope, node)
.expect("AudioWorkletNode declaration should initialize object");
+ graph::initialize_node(scope, node, context);
if let Some(worker) =
web_audio_object_slot(scope, module_state, AUDIO_WORKLET_MODULE_WORKER_SLOT)
{
@@ -1248,7 +1263,7 @@ pub(in crate::context_bootstrap) fn offline_audio_context_constructor_callback<'
}
let context = args.this();
- let destination = audio_destination_node(scope);
+ let destination = audio_destination_node(scope, context);
let compressors = v8::Array::new(scope, 0);
OfflineAudioContextObjectDeclaration::new(
0.0,
@@ -1312,10 +1327,14 @@ fn audio_context_create_oscillator_callback<'s>(
if !require_base_audio_context(scope, args.this()) {
return;
}
- let frequency = audio_param(scope, 440.0);
- let node = OscillatorNodeObjectDeclaration::new("sine", frequency)
+ let nyquist = audio_context_sample_rate(scope, args.this()) / 2.0;
+ let frequency = audio_param(scope, 440.0, -nyquist, nyquist);
+ let detune = detune_param(scope);
+ let node = OscillatorNodeObjectDeclaration::new("sine", frequency, detune)
.bind(scope)
.expect("OscillatorNode declaration should bind");
+ graph::initialize_node(scope, node, args.this());
+ graph::initialize_source(scope, node);
rv.set(node.into());
}
@@ -1327,15 +1346,16 @@ fn audio_context_create_dynamics_compressor_callback<'s>(
if !require_base_audio_context(scope, args.this()) {
return;
}
- let threshold = audio_param(scope, -24.0);
- let knee = audio_param(scope, 30.0);
- let ratio = audio_param(scope, 12.0);
- let attack = audio_param(scope, 0.003);
- let release = audio_param(scope, 0.25);
+ let threshold = audio_param(scope, -24.0, -100.0, 0.0);
+ let knee = audio_param(scope, 30.0, 0.0, 40.0);
+ let ratio = audio_param(scope, 12.0, 1.0, 20.0);
+ let attack = audio_param(scope, 0.003, 0.0, 1.0);
+ let release = audio_param(scope, 0.25, 0.0, 1.0);
let node =
DynamicsCompressorNodeObjectDeclaration::new(threshold, knee, ratio, attack, release, 0.0)
.bind(scope)
.expect("DynamicsCompressorNode declaration should bind");
+ graph::initialize_node(scope, node, args.this());
remember_context_compressor(scope, args.this(), node);
rv.set(node.into());
}
@@ -1351,6 +1371,7 @@ fn audio_context_create_analyser_callback<'s>(
let node = AnalyserNodeObjectDeclaration::new(2048.0, -100.0, -30.0, 0.8)
.bind(scope)
.expect("AnalyserNode declaration should bind");
+ graph::initialize_node(scope, node, args.this());
rv.set(node.into());
}
@@ -1365,7 +1386,8 @@ fn offline_audio_context_start_rendering_callback<'s>(
.unwrap_or(44_100);
let sample_rate =
web_audio_number_slot(scope, context, OFFLINE_AUDIO_SAMPLE_RATE_SLOT).unwrap_or(44_100.0);
- let rendered_buffer = build_audio_buffer(scope, length, sample_rate);
+ let has_input = graph::prepare_offline_render(scope, context, length as f64 / sample_rate);
+ let rendered_buffer = build_audio_buffer(scope, length, sample_rate, has_input);
define_non_enumerable_string_property(scope, context, "state", "closed");
let payload = OfflineAudioCompletePayloadDeclaration::new(context, rendered_buffer)
@@ -1453,12 +1475,12 @@ fn mark_context_compressors_rendered<'s>(
let Ok(node) = v8::Local::<v8::Object>::try_from(value) else {
continue;
};
- set_web_audio_number_slot(
- scope,
- node,
- DYNAMICS_COMPRESSOR_REDUCTION_SLOT,
- SYNTHETIC_COMPRESSOR_REDUCTION,
- );
+ let reduction = if graph::rendered_with_input(scope, node) {
+ SYNTHETIC_COMPRESSOR_REDUCTION
+ } else {
+ 0.0
+ };
+ set_web_audio_number_slot(scope, node, DYNAMICS_COMPRESSOR_REDUCTION_SLOT, reduction);
}
}
@@ -1467,19 +1489,17 @@ fn audio_node_connect_callback<'s>(
args: v8::FunctionCallbackArguments<'s>,
mut rv: v8::ReturnValue<'s, v8::Value>,
) {
- if args.length() > 0 {
- rv.set(args.get(0));
- } else {
- rv.set(v8::undefined(scope).into());
+ if let Some(destination) = graph::connect(scope, &args) {
+ rv.set(destination.into());
}
}
fn audio_node_disconnect_callback<'s>(
scope: &mut v8::PinScope<'s, '_>,
- _args: v8::FunctionCallbackArguments<'s>,
- mut rv: v8::ReturnValue<'s, v8::Value>,
+ args: v8::FunctionCallbackArguments<'s>,
+ _rv: v8::ReturnValue<'s, v8::Value>,
) {
- rv.set(v8::undefined(scope).into());
+ graph::disconnect(scope, &args);
}
fn analyser_get_float_frequency_data_callback<'s>(
@@ -1628,10 +1648,12 @@ fn copy_analyser_data<'s>(
// Use the intrinsic view length, not a user-overridable JS property. The
// validated fftSize bounds the work, and excess destination entries stay intact.
let length = array.length().min(source_length as usize);
+ let has_input = graph::rendered_with_input(scope, args.this());
for index in 0..length {
- // Preserve the existing compatibility profile. This is synthetic data,
- // not an FFT of connected nodes; real audio graph processing is separate.
+ // The existing connected-input profile is still synthetic, not an FFT.
+ // Never expose it before rendering or for a graph with no started source.
let fill = match kind {
+ AnalyserData::FloatFrequency if !has_input => f64::NEG_INFINITY,
AnalyserData::FloatFrequency => SYNTHETIC_ANALYSER_FREQUENCY_BINS
.get(index)
.or_else(|| SYNTHETIC_ANALYSER_FREQUENCY_BINS.last())
@@ -1649,10 +1671,10 @@ fn copy_analyser_data<'s>(
fn oscillator_start_callback<'s>(
scope: &mut v8::PinScope<'s, '_>,
- _args: v8::FunctionCallbackArguments<'s>,
- mut rv: v8::ReturnValue<'s, v8::Value>,
+ args: v8::FunctionCallbackArguments<'s>,
+ _rv: v8::ReturnValue<'s, v8::Value>,
) {
- rv.set(v8::undefined(scope).into());
+ graph::start_source(scope, &args);
}
fn audio_param_set_value_at_time_callback<'s>(
@@ -1669,10 +1691,13 @@ fn audio_param_set_value_at_time_callback<'s>(
rv.set(param.into());
}
-fn audio_param<'s>(scope: &mut v8::PinScope<'s, '_>, value: f64) -> v8::Local<'s, v8::Object> {
- AudioParamObjectDeclaration::new(value)
- .bind(scope)
- .expect("AudioParam declaration should bind")
+fn audio_context_sample_rate<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ context: v8::Local<'s, v8::Object>,
+) -> f64 {
+ // Realtime contexts currently have a fixed 44.1 kHz sample rate. Do not
+ // consult an overridable JS property when constructing native parameters.
+ web_audio_number_slot(scope, context, OFFLINE_AUDIO_SAMPLE_RATE_SLOT).unwrap_or(44_100.0)
}
fn web_audio_number_slot<'s>(
@@ -1711,18 +1736,25 @@ fn set_web_audio_number_slot(
set_private_value(scope, object, slot, value.into());
}
-fn audio_destination_node<'s>(scope: &mut v8::PinScope<'s, '_>) -> v8::Local<'s, v8::Object> {
- AudioDestinationNodeObjectDeclaration::default()
+fn audio_destination_node<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ context: v8::Local<'s, v8::Object>,
+) -> v8::Local<'s, v8::Object> {
+ let node = AudioDestinationNodeObjectDeclaration::default()
.bind(scope)
- .expect("AudioDestinationNode declaration should bind")
+ .expect("AudioDestinationNode declaration should bind");
+ graph::initialize_node(scope, node, context);
+ graph::set_destination(scope, context, node);
+ node
}
fn build_audio_buffer<'s>(
scope: &mut v8::PinScope<'s, '_>,
length: usize,
sample_rate: f64,
+ has_input: bool,
) -> v8::Local<'s, v8::Object> {
- let channel_data = build_channel_data_view(scope, length);
+ let channel_data = build_channel_data_view(scope, length, has_input);
AudioBufferObjectDeclaration::new(
length as f64,
sample_rate,
@@ -1736,8 +1768,13 @@ fn build_audio_buffer<'s>(
fn build_channel_data_view<'s>(
scope: &mut v8::PinScope<'s, '_>,
length: usize,
+ has_input: bool,
) -> v8::Local<'s, v8::Object> {
- let samples = synthetic_audio_samples(length);
+ let samples = if has_input {
+ synthetic_audio_samples(length)
+ } else {
+ vec![0.0; length]
+ };
let mut bytes = Vec::with_capacity(samples.len() * std::mem::size_of::<f32>());
for sample in samples {
bytes.extend_from_slice(&sample.to_le_bytes());
diff --git a/moli-renderer-v8/src/context_bootstrap/web_audio_runtime/audio_param.rs b/moli-renderer-v8/src/context_bootstrap/web_audio_runtime/audio_param.rs
new file mode 100644
index 000000000..5e860b9d3
--- /dev/null
+++ b/moli-renderer-v8/src/context_bootstrap/web_audio_runtime/audio_param.rs
@@ -0,0 +1,95 @@
+use super::*;
+
+const DEFAULT_VALUE: &str = "__moliAudioParamDefaultValue";
+const MIN_VALUE: &str = "__moliAudioParamMinValue";
+const MAX_VALUE: &str = "__moliAudioParamMaxValue";
+
+#[derive(WebApiObject)]
+#[webapi(interface = "AudioParam")]
+struct AudioParamObjectDeclaration {
+ #[webapi(data_property)]
+ value: f64,
+ #[webapi(slot = DEFAULT_VALUE)]
+ default_value: f64,
+ #[webapi(slot = MIN_VALUE)]
+ min_value: f64,
+ #[webapi(slot = MAX_VALUE)]
+ max_value: f64,
+ #[webapi(method, length = 2, callback = audio_param_set_value_at_time_callback)]
+ set_value_at_time: (),
+}
+
+#[derive(WebApiFunctionTemplate)]
+#[webapi(name = "AudioParam", enumerable)]
+struct AudioParamPrototypeDeclaration {
+ #[webapi(accessor_property, getter = default_value)]
+ default_value: (),
+ #[webapi(accessor_property, getter = min_value)]
+ min_value: (),
+ #[webapi(accessor_property, getter = max_value)]
+ max_value: (),
+}
+
+pub(super) fn install<'s>(
+ scope: &mut v8::PinScope<'s, '_, ()>,
+ template: v8::Local<'s, v8::FunctionTemplate>,
+) {
+ AudioParamPrototypeDeclaration::initialize_prototype_template(
+ scope,
+ template.prototype_template(scope),
+ );
+}
+
+pub(super) fn audio_param<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ value: f64,
+ min: f64,
+ max: f64,
+) -> v8::Local<'s, v8::Object> {
+ let value = value as f32 as f64;
+ AudioParamObjectDeclaration::new(value, value, min as f32 as f64, max as f32 as f64)
+ .bind(scope)
+ .expect("AudioParam declaration should bind")
+}
+
+pub(super) fn detune_param<'s>(scope: &mut v8::PinScope<'s, '_>) -> v8::Local<'s, v8::Object> {
+ let limit = (1200.0_f32 * f32::MAX.log2()) as f64;
+ audio_param(scope, 0.0, -limit, limit)
+}
+
+fn read_metadata<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'s, v8::Value>,
+ slot: &'static str,
+) {
+ let Some(value) = web_audio_number_slot(scope, args.this(), slot) else {
+ throw_type_error(scope, "Illegal invocation: expected an AudioParam.");
+ return;
+ };
+ rv.set(v8::Number::new(scope, value).into());
+}
+
+fn default_value<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ rv: v8::ReturnValue<'s, v8::Value>,
+) {
+ read_metadata(scope, args, rv, DEFAULT_VALUE);
+}
+
+fn min_value<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ rv: v8::ReturnValue<'s, v8::Value>,
+) {
+ read_metadata(scope, args, rv, MIN_VALUE);
+}
+
+fn max_value<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ rv: v8::ReturnValue<'s, v8::Value>,
+) {
+ read_metadata(scope, args, rv, MAX_VALUE);
+}
diff --git a/moli-renderer-v8/src/context_bootstrap/web_audio_runtime/biquad.rs b/moli-renderer-v8/src/context_bootstrap/web_audio_runtime/biquad.rs
new file mode 100644
index 000000000..1897ed7dd
--- /dev/null
+++ b/moli-renderer-v8/src/context_bootstrap/web_audio_runtime/biquad.rs
@@ -0,0 +1,83 @@
+use super::*;
+
+const BIQUAD_FREQUENCY_SLOT: &str = "__moliBiquadFrequency";
+
+#[derive(WebApiObject)]
+#[webapi(interface = "BiquadFilterNode")]
+struct BiquadFilterNodeObjectDeclaration<'scope> {
+ #[webapi(data_property = "type")]
+ kind: &'static str,
+ #[webapi(data_property, readonly)]
+ frequency: v8::Local<'scope, v8::Object>,
+ #[webapi(slot = BIQUAD_FREQUENCY_SLOT)]
+ internal_frequency: v8::Local<'scope, v8::Object>,
+ #[webapi(data_property, readonly)]
+ detune: v8::Local<'scope, v8::Object>,
+ #[webapi(data_property = "Q", readonly)]
+ q: v8::Local<'scope, v8::Object>,
+ #[webapi(data_property, readonly)]
+ gain: v8::Local<'scope, v8::Object>,
+ #[webapi(method, length = 1, callback = audio_node_connect_callback)]
+ connect: (),
+ #[webapi(method, length = 0, callback = audio_node_disconnect_callback)]
+ disconnect: (),
+}
+
+#[derive(WebApiFunctionTemplate)]
+#[webapi(name = "BiquadFilterNode", enumerable)]
+struct BiquadFilterNodePrototypeDeclaration {
+ #[webapi(method, length = 3, callback = get_frequency_response)]
+ get_frequency_response: (),
+}
+
+pub(super) fn install<'s>(
+ scope: &mut v8::PinScope<'s, '_, ()>,
+ template: v8::Local<'s, v8::FunctionTemplate>,
+) {
+ BiquadFilterNodePrototypeDeclaration::initialize_prototype_template(
+ scope,
+ template.prototype_template(scope),
+ );
+}
+
+pub(super) fn create_biquad_filter<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'s, v8::Value>,
+) {
+ if !require_base_audio_context(scope, args.this()) {
+ return;
+ }
+ // Match Blink's BiquadFilterNode parameter defaults and float bounds.
+ let nyquist = audio_context_sample_rate(scope, args.this()) / 2.0;
+ let frequency = audio_param(scope, 350.0, 0.0, nyquist);
+ let detune = detune_param(scope);
+ let q = audio_param(scope, 1.0, f32::MIN as f64, f32::MAX as f64);
+ let gain_max = (40.0_f32 * f32::MAX.log10()) as f64;
+ let gain = audio_param(scope, 0.0, f32::MIN as f64, gain_max);
+ let node =
+ BiquadFilterNodeObjectDeclaration::new("lowpass", frequency, frequency, detune, q, gain)
+ .bind(scope)
+ .expect("BiquadFilterNode declaration should bind");
+ graph::initialize_node(scope, node, args.this());
+ rv.set(node.into());
+}
+
+fn get_frequency_response<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ _rv: v8::ReturnValue<'s, v8::Value>,
+) {
+ if get_private_value(scope, args.this(), BIQUAD_FREQUENCY_SLOT).is_none() {
+ throw_type_error(scope, "Illegal invocation: expected a BiquadFilterNode.");
+ return;
+ }
+ // Node/AudioParam metadata is available independently of the DSP backend.
+ // Do not silently return fabricated response curves for an unimplemented filter.
+ throw_dom_exception(
+ scope,
+ "NotSupportedError",
+ 9,
+ "Biquad frequency response calculation is not implemented.",
+ );
+}
diff --git a/moli-renderer-v8/src/context_bootstrap/web_audio_runtime/graph.rs b/moli-renderer-v8/src/context_bootstrap/web_audio_runtime/graph.rs
new file mode 100644
index 000000000..9093e469b
--- /dev/null
+++ b/moli-renderer-v8/src/context_bootstrap/web_audio_runtime/graph.rs
@@ -0,0 +1,342 @@
+//! Minimal connection/start bookkeeping for the existing audio backend.
+//!
+//! Reachability can establish silence without pretending to implement DSP. Keep
+//! connections in native slots, handle cycles, and snapshot input availability
+//! before completion callbacks can disconnect or reconnect nodes.
+
+use super::*;
+
+const CONTEXT: &str = "__moliAudioNodeContext";
+const ANALYSERS: &str = "__moliAudioContextActiveAnalysers";
+const DESTINATION: &str = "__moliAudioContextDestination";
+const INPUTS: &str = "__moliAudioNodeInputs";
+const OUTPUTS: &str = "__moliAudioNodeOutputs";
+const START_TIME: &str = "__moliAudioSourceStartTime";
+const RENDERED_INPUT: &str = "__moliAudioNodeRenderedInput";
+
+#[derive(WebApiFunctionTemplate)]
+#[webapi(name = "AudioNode", enumerable)]
+struct AudioNodePrototypeDeclaration {
+ #[webapi(accessor_property, getter = context_getter)]
+ context: (),
+}
+
+pub(super) fn install<'s>(
+ scope: &mut v8::PinScope<'s, '_, ()>,
+ template: v8::Local<'s, v8::FunctionTemplate>,
+) {
+ AudioNodePrototypeDeclaration::initialize_prototype_template(
+ scope,
+ template.prototype_template(scope),
+ );
+}
+
+fn context_getter<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'s, v8::Value>,
+) {
+ if let Some(context) = require_node_context(scope, args.this()) {
+ rv.set(context.into());
+ }
+}
+
+pub(super) fn initialize_node<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ node: v8::Local<'s, v8::Object>,
+ context: v8::Local<'s, v8::Object>,
+) {
+ set_private_value(scope, node, CONTEXT, context.into());
+ for slot in [INPUTS, OUTPUTS] {
+ let array = v8::Array::new(scope, 0);
+ set_private_value(scope, node, slot, array.into());
+ }
+}
+
+pub(super) fn initialize_source<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ node: v8::Local<'s, v8::Object>,
+) {
+ set_web_audio_number_slot(scope, node, START_TIME, f64::INFINITY);
+}
+
+pub(super) fn set_destination<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ context: v8::Local<'s, v8::Object>,
+ node: v8::Local<'s, v8::Object>,
+) {
+ set_private_value(scope, context, DESTINATION, node.into());
+}
+
+fn require_node_context<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ node: v8::Local<'s, v8::Object>,
+) -> Option<v8::Local<'s, v8::Object>> {
+ let context = web_audio_object_slot(scope, node, CONTEXT);
+ if context.is_none() {
+ throw_type_error(scope, "Illegal invocation: expected an AudioNode.");
+ }
+ context
+}
+
+fn objects<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ owner: v8::Local<'s, v8::Object>,
+ slot: &'static str,
+) -> Vec<v8::Local<'s, v8::Object>> {
+ let Some(array) = web_audio_array_slot(scope, owner, slot) else {
+ return Vec::new();
+ };
+ (0..array.length())
+ .filter_map(|index| {
+ array
+ .get_index(scope, index)
+ .and_then(|value| v8::Local::<v8::Object>::try_from(value).ok())
+ })
+ .collect()
+}
+
+fn add_edge<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ node: v8::Local<'s, v8::Object>,
+ slot: &'static str,
+ other: v8::Local<'s, v8::Object>,
+) {
+ if objects(scope, node, slot).contains(&other) {
+ return;
+ }
+ let array = web_audio_array_slot(scope, node, slot).unwrap_or_else(|| {
+ let array = v8::Array::new(scope, 0);
+ set_private_value(scope, node, slot, array.into());
+ array
+ });
+ let _ = array.set_index(scope, array.length(), other.into());
+}
+
+fn remove_edge<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ node: v8::Local<'s, v8::Object>,
+ slot: &'static str,
+ other: v8::Local<'s, v8::Object>,
+) {
+ let values: Vec<v8::Local<v8::Value>> = objects(scope, node, slot)
+ .into_iter()
+ .filter(|value| *value != other)
+ .map(Into::into)
+ .collect();
+ let array = v8::Array::new_with_elements(scope, &values);
+ set_private_value(scope, node, slot, array.into());
+}
+
+pub(super) fn connect<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: &v8::FunctionCallbackArguments<'s>,
+) -> Option<v8::Local<'s, v8::Object>> {
+ let source = args.this();
+ let context = require_node_context(scope, source)?;
+ let Ok(destination) = v8::Local::<v8::Object>::try_from(args.get(0)) else {
+ throw_type_error(
+ scope,
+ "AudioNode.connect requires an AudioNode destination.",
+ );
+ return None;
+ };
+ let target_context = require_node_context(scope, destination)?;
+ if context != target_context {
+ throw_dom_exception(
+ scope,
+ "InvalidAccessError",
+ 15,
+ "Audio nodes must belong to the same context.",
+ );
+ return None;
+ }
+ if web_audio_object_slot(scope, context, DESTINATION) == Some(source)
+ || get_private_value(scope, destination, START_TIME).is_some()
+ {
+ throw_dom_exception(
+ scope,
+ "IndexSizeError",
+ 1,
+ "The audio node has no port in this direction.",
+ );
+ return None;
+ }
+ // The remaining node implementations each expose a single output/input port.
+ for index in 1..args.length().min(3) {
+ if args.get(index).uint32_value(scope)? != 0 {
+ throw_dom_exception(
+ scope,
+ "IndexSizeError",
+ 1,
+ "Audio node port index is out of range.",
+ );
+ return None;
+ }
+ }
+ add_edge(scope, source, OUTPUTS, destination);
+ add_edge(scope, destination, INPUTS, source);
+ if get_private_value(scope, destination, ANALYSER_FFT_SIZE_SLOT).is_some() {
+ // Track candidates for offline automatic pull: analysers with input
+ // can process even without an output connection.
+ add_edge(scope, context, ANALYSERS, destination);
+ }
+ Some(destination)
+}
+
+pub(super) fn disconnect<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: &v8::FunctionCallbackArguments<'s>,
+) {
+ let source = args.this();
+ let Some(context) = require_node_context(scope, source) else {
+ return;
+ };
+ let outputs = objects(scope, source, OUTPUTS);
+ let selected = if args.length() == 0 {
+ None
+ } else if args.get(0).is_object() {
+ let destination = v8::Local::<v8::Object>::try_from(args.get(0)).unwrap();
+ if require_node_context(scope, destination).is_none() {
+ return;
+ }
+ if !outputs.contains(&destination) {
+ throw_dom_exception(
+ scope,
+ "InvalidAccessError",
+ 15,
+ "The audio nodes are not connected.",
+ );
+ return;
+ }
+ Some(destination)
+ } else {
+ let Some(port) = args.get(0).uint32_value(scope) else {
+ return;
+ };
+ if port != 0 {
+ throw_dom_exception(
+ scope,
+ "IndexSizeError",
+ 1,
+ "Audio node port index is out of range.",
+ );
+ return;
+ }
+ None
+ };
+ for index in 1..args.length().min(3) {
+ let Some(port) = args.get(index).uint32_value(scope) else {
+ return;
+ };
+ if port != 0 {
+ throw_dom_exception(
+ scope,
+ "IndexSizeError",
+ 1,
+ "Audio node port index is out of range.",
+ );
+ return;
+ }
+ }
+ for destination in outputs {
+ if selected.is_none_or(|selected| selected == destination) {
+ remove_edge(scope, source, OUTPUTS, destination);
+ remove_edge(scope, destination, INPUTS, source);
+ if get_private_value(scope, destination, ANALYSER_FFT_SIZE_SLOT).is_some()
+ && objects(scope, destination, INPUTS).is_empty()
+ {
+ remove_edge(scope, context, ANALYSERS, destination);
+ }
+ }
+ }
+}
+
+pub(super) fn start_source<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: &v8::FunctionCallbackArguments<'s>,
+) {
+ let node = args.this();
+ let Some(start) = web_audio_number_slot(scope, node, START_TIME) else {
+ throw_type_error(scope, "Illegal invocation: expected an OscillatorNode.");
+ return;
+ };
+ let when = if args.get(0).is_undefined() {
+ 0.0
+ } else {
+ let Some(when) = args.get(0).number_value(scope) else {
+ return;
+ };
+ when
+ };
+ if !when.is_finite() {
+ throw_type_error(scope, "Audio source start time must be finite.");
+ return;
+ }
+ if when < 0.0 {
+ throw_range_error(scope, "Audio source start time must not be negative.");
+ return;
+ }
+ if start.is_finite() {
+ throw_dom_exception(
+ scope,
+ "InvalidStateError",
+ 11,
+ "The audio source has already been started.",
+ );
+ return;
+ }
+ set_web_audio_number_slot(scope, node, START_TIME, when);
+}
+
+fn has_started_source<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ node: v8::Local<'s, v8::Object>,
+ end_time: f64,
+) -> bool {
+ let mut pending = vec![node];
+ let mut visited = Vec::new();
+ while let Some(node) = pending.pop() {
+ if visited.contains(&node) {
+ continue;
+ }
+ visited.push(node);
+ if web_audio_number_slot(scope, node, START_TIME).is_some_and(|start| start < end_time) {
+ return true;
+ }
+ pending.extend(objects(scope, node, INPUTS));
+ }
+ false
+}
+
+pub(super) fn prepare_offline_render<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ context: v8::Local<'s, v8::Object>,
+ end_time: f64,
+) -> bool {
+ let destination = web_audio_object_slot(scope, context, DESTINATION);
+ let mut pending: Vec<_> = objects(scope, context, ANALYSERS)
+ .into_iter()
+ .filter(|node| objects(scope, *node, OUTPUTS).is_empty())
+ .collect();
+ pending.extend(destination);
+ let mut visited = Vec::new();
+ while let Some(node) = pending.pop() {
+ if visited.contains(&node) {
+ continue;
+ }
+ visited.push(node);
+ pending.extend(objects(scope, node, INPUTS));
+ let has_input = has_started_source(scope, node, end_time);
+ let flag = v8::Boolean::new(scope, has_input);
+ set_private_value(scope, node, RENDERED_INPUT, flag.into());
+ }
+ destination.is_some_and(|node| rendered_with_input(scope, node))
+}
+
+pub(super) fn rendered_with_input<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ node: v8::Local<'s, v8::Object>,
+) -> bool {
+ get_private_value(scope, node, RENDERED_INPUT).is_some_and(|value| value.boolean_value(scope))
+}
diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/media.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/media.rs
index 2596b18b0..a638e16c4 100644
--- a/moli-renderer-v8/src/script_vm/tests/browser_api/media.rs
+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/media.rs
@@ -2301,7 +2301,7 @@ fn zhihu_probe_offline_audio_context_supports_fingerprintjs2_audio_flow() {
}
#[test]
-fn offline_audio_context_short_buffers_expose_nonzero_samples() {
+fn offline_audio_context_without_connected_sources_renders_silence() {
let mut vm = new_storage_test_vm("https://short-audio-fingerprint.test/");
vm.exec(
@@ -2315,7 +2315,7 @@ fn offline_audio_context_short_buffers_expose_nonzero_samples() {
done: true,
length: data.length,
nonzero: nonzero.length,
- sum: nonzero.reduce((acc, value) => acc + Math.abs(value))
+ sum: nonzero.reduce((acc, value) => acc + Math.abs(value), 0)
};
};
ctx.startRendering();
@@ -2331,14 +2331,8 @@ fn offline_audio_context_short_buffers_expose_nonzero_samples() {
serde_json::from_str(&result).expect("short offline audio probe should return valid json");
assert_eq!(value["done"], true);
assert_eq!(value["length"], 500);
- assert!(
- value["nonzero"].as_u64().unwrap_or_default() > 0,
- "short offline audio probe should expose non-zero samples: {result}"
- );
- assert!(
- value["sum"].as_f64().unwrap_or_default() > 0.0,
- "short offline audio probe should produce a positive sample sum: {result}"
- );
+ assert_eq!(value["nonzero"], 0, "an empty graph must render silence");
+ assert_eq!(value["sum"], 0);
}
#[test]
@@ -2352,6 +2346,7 @@ fn offline_audio_context_updates_compressor_reduction_on_complete() {
const comp = ctx.createDynamicsCompressor();
osc.connect(comp);
comp.connect(ctx.destination);
+ osc.start(0);
globalThis.__compressorReductionProbe = {
before: comp.reduction,
afterStart: null,
@@ -2600,7 +2595,7 @@ fn web_audio_private_backing_slots_ignore_public_spoofing() {
assert_eq!(
result,
- r#"{"ctxOwnInternalBefore":"","compOwnInternalBefore":"","ctxOwnInternalAfterSpoof":"__moliOfflineAudioChannelCount,__moliOfflineAudioCompleteBuffer,__moliOfflineAudioCompleteContext,__moliOfflineAudioCompressors,__moliOfflineAudioLength,__moliOfflineAudioSampleRate","reductionBefore":0,"complete":{"bufferOwnInternalBefore":"","bufferOwnInternalAfterSpoof":"__moliOfflineAudioBuffer","targetStable":true,"currentTargetStable":true,"renderedBufferStable":true,"bufferLength":32,"bufferSampleRate":8000,"dataTag":"[object Float32Array]","dataLength":32,"reductionAfter":-82.26815795898438}}"#
+ r#"{"ctxOwnInternalBefore":"","compOwnInternalBefore":"","ctxOwnInternalAfterSpoof":"__moliOfflineAudioChannelCount,__moliOfflineAudioCompleteBuffer,__moliOfflineAudioCompleteContext,__moliOfflineAudioCompressors,__moliOfflineAudioLength,__moliOfflineAudioSampleRate","reductionBefore":0,"complete":{"bufferOwnInternalBefore":"","bufferOwnInternalAfterSpoof":"__moliOfflineAudioBuffer","targetStable":true,"currentTargetStable":true,"renderedBufferStable":true,"bufferLength":32,"bufferSampleRate":8000,"dataTag":"[object Float32Array]","dataLength":32,"reductionAfter":0}}"#
);
}
@@ -2644,7 +2639,7 @@ fn offline_audio_context_analyser_supports_probe_data_methods() {
typeof analyser.getFloatTimeDomainData,
typeof analyser.getByteTimeDomainData
],
- floats: Array.from(floats),
+ floats: Array.from(floats, String),
time: Array.from(time),
bytes: Array.from(bytes),
byteTime: Array.from(byteTime)
@@ -2656,7 +2651,7 @@ fn offline_audio_context_analyser_supports_probe_data_methods() {
assert_eq!(
result,
- r#"{"ctorType":"function","tag":"[object AnalyserNode]","ctor":"AnalyserNode","fftSize":2048,"frequencyBinCount":1024,"minDecibels":-100,"maxDecibels":-30,"smoothingTimeConstant":0.8,"connectResultCtor":"AudioDestinationNode","methods":["function","function","function","function","function"],"floats":[-90.25955200195312,-90.22233581542969,-90.11856842041016,-89.96821594238281],"time":[0,0,0,0],"bytes":[0,0,0,0],"byteTime":[128,128,128,128]}"#
+ r#"{"ctorType":"function","tag":"[object AnalyserNode]","ctor":"AnalyserNode","fftSize":2048,"frequencyBinCount":1024,"minDecibels":-100,"maxDecibels":-30,"smoothingTimeConstant":0.8,"connectResultCtor":"AudioDestinationNode","methods":["function","function","function","function","function"],"floats":["-Infinity","-Infinity","-Infinity","-Infinity"],"time":[0,0,0,0],"bytes":[0,0,0,0],"byteTime":[128,128,128,128]}"#
);
}
diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/web_audio.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/web_audio.rs
index 758438f7d..da7e9e7c5 100644
--- a/moli-renderer-v8/src/script_vm/tests/browser_api/web_audio.rs
+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/web_audio.rs
@@ -9,7 +9,7 @@ fn web_audio_factories_are_shared_by_realtime_and_offline_contexts() {
(() => {
const realtime = new AudioContext();
const offline = new OfflineAudioContext(1, 64, 44100);
- return JSON.stringify(["createOscillator", "createAnalyser", "createDynamicsCompressor"].map(name => {
+ return JSON.stringify(["createOscillator", "createAnalyser", "createDynamicsCompressor", "createBiquadFilter"].map(name => {
const method = BaseAudioContext.prototype[name];
const liveNode = realtime[name]();
const offlineNode = offline[name]();
@@ -30,7 +30,7 @@ fn web_audio_factories_are_shared_by_realtime_and_offline_contexts() {
.expect("both audio context kinds should expose shared factories");
assert_eq!(
result,
- r#"[[true,true,false,false,0,"OscillatorNode","OscillatorNode",true],[true,true,false,false,0,"AnalyserNode","AnalyserNode",true],[true,true,false,false,0,"DynamicsCompressorNode","DynamicsCompressorNode",true]]"#
+ r#"[[true,true,false,false,0,"OscillatorNode","OscillatorNode",true],[true,true,false,false,0,"AnalyserNode","AnalyserNode",true],[true,true,false,false,0,"DynamicsCompressorNode","DynamicsCompressorNode",true],[true,true,false,false,0,"BiquadFilterNode","BiquadFilterNode",true]]"#
);
}
@@ -76,7 +76,7 @@ fn web_audio_factories_reject_forged_contexts_and_keep_nodes_independent() {
(() => {
const errors = [];
for (const context of [new AudioContext(), new OfflineAudioContext(1, 64, 44100)]) {
- for (const name of ['createOscillator', 'createAnalyser', 'createDynamicsCompressor']) {
+ for (const name of ['createOscillator', 'createAnalyser', 'createDynamicsCompressor', 'createBiquadFilter']) {
for (const receiver of [{}, BaseAudioContext.prototype, Object.create(context), new Proxy(context, {})]) {
try { context[name].call(receiver); errors.push(name + ': accepted forged context'); }
catch (error) { if (error.name !== 'TypeError') errors.push(name + ': ' + error.name); }
@@ -146,7 +146,7 @@ fn web_audio_analyser_writes_only_available_samples_in_the_destination_view() {
for (const size of [32, 2048, 32768]) {
analyser.fftSize = size;
for (const [method, ArrayType, count, expected] of [
- ['getFloatFrequencyData', Float32Array, size / 2, null],
+ ['getFloatFrequencyData', Float32Array, size / 2, -Infinity],
['getFloatTimeDomainData', Float32Array, size, 0],
['getByteFrequencyData', Uint8Array, size / 2, 0],
['getByteTimeDomainData', Uint8Array, size, 128]
@@ -159,7 +159,7 @@ fn web_audio_analyser_writes_only_available_samples_in_the_destination_view() {
for (let i = 0; i < storage.length; i++) {
const written = i >= 2 && i < 2 + Math.min(count, length);
if (!written && storage[i] !== 57) errors.push(method + ': out-of-range write ' + i);
- if (written && (expected === null ? !Number.isFinite(storage[i]) || storage[i] >= 0 : storage[i] !== expected)) errors.push(method + ': sample ' + i);
+ if (written && storage[i] !== expected) errors.push(method + ': sample ' + i);
}
}
}
@@ -212,3 +212,133 @@ fn web_audio_analyser_checks_receiver_and_typed_array_without_duck_typing() {
"#).expect("analyser data methods should reject incompatible receivers and buffers");
assert_eq!(result, "[]");
}
+
+#[test]
+fn web_audio_biquad_and_existing_params_expose_native_readonly_metadata() {
+ let mut vm = new_storage_test_vm("https://audio-parameter-metadata.test/");
+ let result = vm.eval(r#"
+(() => {
+ const errors = [];
+ for (const rate of [8000, 44100, 48000]) {
+ const ctx = new OfflineAudioContext(1, 64, rate);
+ ctx.sampleRate = 123; // Native bounds must not depend on an overridable property.
+ const filter = ctx.createBiquadFilter(), other = ctx.createBiquadFilter();
+ if (!(filter instanceof BiquadFilterNode) || Object.prototype.toString.call(filter) !== '[object BiquadFilterNode]') errors.push('brand');
+ if (filter.type !== 'lowpass' || filter.frequency !== filter.frequency || filter.frequency === other.frequency) errors.push('node state');
+ if (Reflect.set(filter, 'frequency', {}) !== false) errors.push('writable frequency');
+ const osc = ctx.createOscillator(), comp = ctx.createDynamicsCompressor();
+ for (const node of [filter, osc, comp, ctx.createAnalyser(), ctx.destination]) {
+ if (node.context !== ctx || Reflect.set(node, 'context', {}) !== false) errors.push('context');
+ }
+ const rows = [
+ [filter.frequency, 350, 0, rate / 2],
+ [filter.Q, 1, -3.4028234663852886e38, 3.4028234663852886e38],
+ [filter.detune, 0, -153600, 153600],
+ [osc.frequency, 440, -rate / 2, rate / 2],
+ [osc.detune, 0, -153600, 153600],
+ [comp.threshold, -24, -100, 0], [comp.knee, 30, 0, 40],
+ [comp.ratio, 12, 1, 20], [comp.attack, Math.fround(.003), 0, 1],
+ [comp.release, .25, 0, 1]
+ ];
+ for (const [p, value, min, max] of rows) {
+ if (!(p instanceof AudioParam) || p.value !== value || p.defaultValue !== value || p.minValue !== min || p.maxValue !== max) errors.push('metadata ' + value);
+ p.value = 123;
+ if (p.defaultValue !== value) errors.push('default changed');
+ for (const key of ['defaultValue', 'minValue', 'maxValue']) {
+ const descriptor = Object.getOwnPropertyDescriptor(AudioParam.prototype, key);
+ if (!descriptor || !descriptor.enumerable || descriptor.set || Reflect.set(p, key, 999) !== false) errors.push('readonly ' + key);
+ for (const receiver of [{}, AudioParam.prototype, Object.create(p), new Proxy(p, {})]) {
+ try { descriptor.get.call(receiver); errors.push('accepted forged param'); }
+ catch (e) { if (e.name !== 'TypeError') errors.push(e.name); }
+ }
+ }
+ }
+ if (filter.gain.defaultValue !== 0 || filter.gain.minValue !== Math.fround(-3.4028234663852886e38) || !(filter.gain.maxValue > 1541 && filter.gain.maxValue < 1542)) errors.push('gain range');
+ try { filter.getFrequencyResponse(new Float32Array(1), new Float32Array(1), new Float32Array(1)); errors.push('fabricated frequency response'); }
+ catch (e) { if (e.name !== 'NotSupportedError') errors.push('response ' + e.name); }
+ }
+ return JSON.stringify(errors);
+})()
+"#).expect("BiquadFilter and AudioParam metadata should be native and context-specific");
+ assert_eq!(result, "[]");
+}
+
+#[test]
+fn offline_audio_silence_depends_on_reachable_started_sources() {
+ let mut vm = new_storage_test_vm("https://audio-graph-silence.test/");
+ vm.exec(r#"
+globalThis.__audioSilenceResults = [];
+for (const mode of ['empty', 'unconnected', 'not-started', 'future', 'disconnected-source', 'disconnected-sink', 'cycle', 'connected', 'duplicate', 'selected-disconnect']) {
+ const ctx = new OfflineAudioContext(1, 100, 44100);
+ const osc = ctx.createOscillator(), comp = ctx.createDynamicsCompressor();
+ if (mode !== 'empty' && mode !== 'unconnected') {
+ osc.connect(comp);
+ comp.connect(ctx.destination);
+ }
+ if (mode !== 'empty' && mode !== 'not-started') osc.start(mode === 'future' ? 1 : 0);
+ if (mode === 'disconnected-source') osc.disconnect();
+ if (mode === 'disconnected-sink') comp.disconnect(ctx.destination);
+ if (mode === 'cycle') { osc.disconnect(); const a = ctx.createAnalyser(); comp.connect(a); a.connect(comp); }
+ if (mode === 'duplicate') { osc.connect(comp); osc.connect(comp); }
+ if (mode === 'selected-disconnect') { const a = ctx.createAnalyser(); osc.connect(a); osc.disconnect(a); }
+ ctx.startRendering().then(buffer => {
+ __audioSilenceResults.push([mode, buffer.getChannelData(0).some(x => x !== 0), comp.reduction < 0]);
+ });
+}
+"#, None).expect("offline graph scenarios should render without hanging on a cycle");
+ let result = vm.eval("JSON.stringify(__audioSilenceResults)").unwrap();
+ assert_eq!(
+ result,
+ r#"[["empty",false,false],["unconnected",false,false],["not-started",false,false],["future",false,false],["disconnected-source",false,false],["disconnected-sink",false,false],["cycle",false,false],["connected",true,true],["duplicate",true,true],["selected-disconnect",true,true]]"#
+ );
+}
+
+#[test]
+fn offline_analyser_starts_silent_and_retains_the_rendered_input_snapshot() {
+ let mut vm = new_storage_test_vm("https://audio-analyser-silence.test/");
+ vm.exec(r#"
+const ctx = new OfflineAudioContext(1, 5000, 44100);
+const osc = ctx.createOscillator(), comp = ctx.createDynamicsCompressor();
+const analyser = ctx.createAnalyser(), silent = ctx.createAnalyser();
+const isSilent = node => { const data = new Float32Array(16); node.getFloatFrequencyData(data); return data.every(x => x === -Infinity); };
+globalThis.__analyserSilence = [isSilent(analyser)];
+osc.connect(comp); comp.connect(analyser); comp.connect(ctx.destination); osc.start();
+__analyserSilence.push(isSilent(analyser));
+ctx.oncomplete = () => {
+ comp.disconnect(); osc.disconnect();
+ __analyserSilence.push(isSilent(analyser), isSilent(silent));
+};
+ctx.startRendering();
+"#, None).expect("analyser silence and retained rendering state should evaluate");
+ assert_eq!(
+ vm.eval("JSON.stringify(__analyserSilence)").unwrap(),
+ "[true,true,false,true]"
+ );
+}
+
+#[test]
+fn web_audio_connections_validate_context_and_source_receivers() {
+ let mut vm = new_storage_test_vm("https://audio-connection-receivers.test/");
+ let result = vm.eval(r#"
+(() => {
+ const ctx = new AudioContext(), other = new AudioContext(), osc = ctx.createOscillator();
+ const errors = [];
+ const check = (fn, expected) => { try { fn(); errors.push('accepted ' + expected); } catch (e) { if (e.name !== expected) errors.push(e.name); } };
+ check(() => osc.connect(other.destination), 'InvalidAccessError');
+ check(() => osc.connect({}), 'TypeError');
+ check(() => osc.connect(ctx.destination, 1), 'IndexSizeError');
+ check(() => osc.connect(new Proxy(ctx.destination, {})), 'TypeError');
+ for (const receiver of [{}, Object.create(osc), new Proxy(osc, {})]) {
+ check(() => osc.connect.call(receiver, ctx.destination), 'TypeError');
+ check(() => osc.disconnect.call(receiver), 'TypeError');
+ check(() => osc.start.call(receiver), 'TypeError');
+ }
+ check(() => osc.start(-1), 'RangeError');
+ check(() => osc.start(Infinity), 'TypeError');
+ osc.start();
+ check(() => osc.start(), 'InvalidStateError');
+ return JSON.stringify(errors);
+})()
+"#).expect("audio graph operations should validate receivers and context ownership");
+ assert_eq!(result, "[]");
+}
diff --git a/moli/tests/fetch_cli/eval.rs b/moli/tests/fetch_cli/eval.rs
index 56495b7d4..f14289cc3 100644
--- a/moli/tests/fetch_cli/eval.rs
+++ b/moli/tests/fetch_cli/eval.rs
@@ -77,11 +77,13 @@ fn eval_can_use_realtime_audio_oscillator_and_analyser_shims() -> Result<()> {
oscillator.frequency.setValueAtTime(880, context.currentTime);
analyser.fftSize = 32;
const connected = oscillator.connect(analyser).connect(context.destination);
- oscillator.start(0);
+ // Before starting the source, the analyser must be silent. Reading
+ // after start would depend on whether an audio quantum has run yet.
const frequencies = new Float32Array(analyser.frequencyBinCount + 1).fill(42);
const readResult = analyser.getFloatFrequencyData(frequencies);
const waveform = new Uint8Array(analyser.fftSize);
analyser.getByteTimeDomainData(waveform);
+ const startResult = oscillator.start(0);
oscillator.disconnect();
analyser.disconnect();
await context.close();
@@ -91,8 +93,9 @@ fn eval_can_use_realtime_audio_oscillator_and_analyser_shims() -> Result<()> {
analyser: analyser instanceof AnalyserNode,
connected: connected === context.destination,
readReturnedUndefined: readResult === undefined,
+ startReturnedUndefined: startResult === undefined,
bins: analyser.frequencyBinCount,
- filled: frequencies.slice(0, -1).every(value => Number.isFinite(value) && value < 0),
+ silentFrequencies: frequencies.slice(0, -1).every(value => value === -Infinity),
untouchedTail: frequencies[analyser.frequencyBinCount],
silentWaveform: waveform.every(value => value === 128),
state: context.state
@@ -110,8 +113,9 @@ fn eval_can_use_realtime_audio_oscillator_and_analyser_shims() -> Result<()> {
"analyser": true,
"connected": true,
"readReturnedUndefined": true,
+ "startReturnedUndefined": true,
"bins": 16,
- "filled": true,
+ "silentFrequencies": true,
"untouchedTail": 42,
"silentWaveform": true,
"state": "closed"