Skip to content

The animation engine for agents: blazingly fast math and 3D videos with Manim's Python API, on a Rust GPU core.

Python

Decode and mix audio natively

apoogdk8b10082f160a
diff --git a/rust/engine/src/aac.rs b/rust/engine/src/aac.rs
new file mode 100644
index 00000000..527d1df5
--- /dev/null
+++ b/rust/engine/src/aac.rs
@@ -0,0 +1,24 @@
+//! Sound out: a film's soundtrack encoded as AAC-LC (rusty_aac, pure Rust), for the MP4's sound
+//! track. The encoder delays the sound by one frame of 1024 samples (its priming), which the MP4's
+//! edit list skips.
+
+use rusty_aac::encode::{AacEncoder, AacEncoderConfig, audio_specific_config_bytes};
+
+use crate::mp4::Audio;
+
+/// The samples an AAC-LC encoder puts before the sound: one frame.
+const PRIMING: u32 = 1024;
+
+/// Interleaved float samples of `channels` channels at `rate`, encoded at about `bitrate` bits a
+/// second.
+pub fn encode(pcm: &[f32], channels: usize, rate: u32, bitrate: u32) -> Result<Audio, String> {
+ let mut encoder = AacEncoder::new(AacEncoderConfig { bitrate_bps: bitrate, ..Default::default() });
+ encoder.push_pcm(pcm, channels as u16, rate).map_err(|e| e.to_string())?;
+ encoder.finish();
+ let mut units = Vec::new();
+ while let Ok(packet) = encoder.next_packet() {
+ units.push(packet.data);
+ }
+ let config = audio_specific_config_bytes(rate, channels as u16);
+ Ok(Audio { rate, channels: channels as u16, config, priming: PRIMING, length: (pcm.len() / channels) as u64, units })
+}
diff --git a/rust/engine/src/audio.rs b/rust/engine/src/audio.rs
new file mode 100644
index 00000000..359db90e
--- /dev/null
+++ b/rust/engine/src/audio.rs
@@ -0,0 +1,85 @@
+//! Sound in: any common audio file decoded by FFmpeg (the `ffmpeg` crate: WAV, AIFF, CAF, MP3,
+//! AAC (with SBR and PS), Opus, Vorbis, FLAC and ALAC, in their files and in MP4, MOV, WebM and
+//! Matroska) to float samples, at most two channels, starting and ending where the file declares;
+//! and resampled — band-limited, a Kaiser-windowed sinc — to the film's rate.
+
+use std::f64::consts::PI;
+
+/// A file's audio: interleaved float samples, its rate and its channel count (1 or 2).
+pub fn decode(data: Vec<u8>) -> Result<(Vec<f32>, u32, usize), String> {
+ ffmpeg::decode(&data).map_err(|e| format!("not an audio file manimgx reads: {e}"))
+}
+
+/// Zero crossings of the sinc on each side of a sample: the filter's reach, in input samples
+/// (more for downsampling, whose cutoff is lower).
+const ZEROS: usize = 16;
+/// The kernel's table: points per zero crossing (linearly interpolated between).
+const DENSITY: usize = 512;
+/// Kaiser's β: about 80 dB of stopband.
+const BETA: f64 = 8.0;
+
+fn bessel_i0(x: f64) -> f64 {
+ let (mut sum, mut term, mut k) = (1.0, 1.0, 1.0);
+ while term > 1e-12 * sum {
+ term *= (x / (2.0 * k)) * (x / (2.0 * k));
+ sum += term;
+ k += 1.0;
+ }
+ sum
+}
+
+/// The filter, `cutoff` × sinc(`cutoff` × d) Kaiser-windowed, tabulated over its argument u =
+/// `cutoff` × d from 0 to ZEROS zero crossings, DENSITY points per crossing.
+fn kernel(cutoff: f64) -> Vec<f32> {
+ let n = ZEROS * DENSITY;
+ (0..=n + 1)
+ .map(|i| {
+ let x = i as f64 / DENSITY as f64; // in the scaled sinc's zero crossings
+ let sinc = if x == 0.0 { 1.0 } else { (PI * x).sin() / (PI * x) };
+ let r = (x / ZEROS as f64).min(1.0);
+ (cutoff * sinc * bessel_i0(BETA * (1.0 - r * r).sqrt()) / bessel_i0(BETA)) as f32
+ })
+ .collect()
+}
+
+/// `input` (interleaved, `channels` channels) at `from` samples a second, resampled to `to`:
+/// each output sample the band-limited interpolation of the input at its instant.
+pub fn resample(input: &[f32], channels: usize, from: f64, to: f64) -> Vec<f32> {
+ let frames = input.len() / channels;
+ if from == to || frames == 0 {
+ return input.to_vec();
+ }
+ let step = from / to; // input samples per output sample
+ let cutoff = 0.95 * (1.0 / step).min(1.0); // below the lower Nyquist
+ let table = kernel(cutoff);
+ let reach = (ZEROS as f64 / cutoff).ceil() as isize; // input samples each side
+ let count = ((frames as f64) / step).round() as usize;
+ let mut out = vec![0.0f32; count * channels];
+ let mut weights = Vec::with_capacity(2 * reach as usize + 1);
+ for n in 0..count {
+ let x = n as f64 * step;
+ let center = x.floor() as isize;
+ let (first, last) = ((center - reach + 1).max(0), (center + reach).min(frames as isize - 1));
+ weights.clear();
+ for k in first..=last {
+ // the kernel at the distance, in its own zero crossings
+ let t = (x - k as f64).abs() * cutoff * DENSITY as f64;
+ let i = t as usize;
+ let w = if i + 1 < table.len() {
+ let f = (t - i as f64) as f32;
+ table[i] * (1.0 - f) + table[i + 1] * f
+ } else {
+ 0.0
+ };
+ weights.push(w);
+ }
+ let o = &mut out[n * channels..(n + 1) * channels];
+ for (w, k) in weights.iter().zip(first..=last) {
+ let s = &input[k as usize * channels..(k as usize + 1) * channels];
+ for c in 0..channels {
+ o[c] += w * s[c];
+ }
+ }
+ }
+ out
+}
diff --git a/rust/engine/src/mp4.rs b/rust/engine/src/mp4.rs
index 71104666..25c525da 100644
--- a/rust/engine/src/mp4.rs
+++ b/rust/engine/src/mp4.rs
@@ -2,7 +2,10 @@
//! faststart.
//!
//! A video sample is a picture as x264 writes it (NAL units, each prefixed by its 4-byte size) and a
-//! held picture is one sample that lasts longer — times are in frames (timescale = fps). An audio
+//! held picture is one sample that lasts longer — times are in frames (timescale = fps). With
+//! B-frames (the slower presets), a picture can be decoded before the one it follows: the samples
+//! are in decode order, each shown at its own time, and an edit list starts the track at the
+//! first picture shown. An audio
//! sample is an AAC access unit of 1024 samples; the encoder's priming before the sound's first
//! sample is skipped by an edit list, so the sound starts with the film, to the sample. Video
//! samples stream to a temporary file; `finish` writes the header (`moov`), then the samples,
@@ -166,15 +169,20 @@ impl Mp4 {
/// The header, with the samples starting at byte `offset` of the file.
fn moov(&self, end: i64, audio: Option<&Audio>, layout: &Layout, offset: u64, wide: bool) -> Vec<u8> {
+ // The media's clock starts at the first decode. With B-frames, x264 decodes ahead of
+ // what it shows (its first dts is negative), so a picture shows at its pts minus that
+ // first dts: an offset of pts − dts (never negative) from its decode, and an edit list
+ // that starts the track at the first picture shown.
let first = self.samples.first().map_or(0, |s| s.dts);
+ let start = self.samples.iter().map(|s| s.pts).min().map_or(0, |pts| pts - first);
let decode: Vec<i64> = self.samples.iter().map(|s| s.dts - first).collect();
let duration = end.max(1) as u32;
+ let media = (end - first).max(1) as u32;
let mut deltas: Vec<u32> = decode.windows(2).map(|w| (w[1] - w[0]) as u32).collect();
if let Some(&last) = decode.last() {
- deltas.push((end - last).max(1) as u32);
+ deltas.push((end - first - last).max(1) as u32);
}
- // composition = decode + offset: the offsets make a sample show at its pts
- let offsets: Vec<u32> = self.samples.iter().zip(&decode).map(|(s, d)| (s.pts - d).max(0) as u32).collect();
+ let offsets: Vec<u32> = self.samples.iter().map(|s| (s.pts - s.dts).max(0) as u32).collect();
let [(video_at, video_counts), (audio_at, audio_counts)] = self.chunks(audio, layout, offset);
// the sound's length in the movie's timescale (frames)
let heard = audio.map_or(0, |a| (a.length * self.fps as u64).div_ceil(a.rate as u64) as u32);
@@ -208,12 +216,23 @@ impl Mp4 {
w.u32(self.width << 16);
w.u32(self.height << 16);
});
+ if start > 0 {
+ w.boxed(b"edts", |w| {
+ w.full(b"elst", 0, 0, |w| {
+ w.u32(1);
+ w.u32(duration); // in the movie's timescale (frames, as the track's)
+ w.u32(start as u32); // where the first picture shows, in the track's
+ w.u16(1);
+ w.u16(0);
+ });
+ });
+ }
w.boxed(b"mdia", |w| {
w.full(b"mdhd", 0, 0, |w| {
w.u32(0);
w.u32(0);
w.u32(self.fps);
- w.u32(duration);
+ w.u32(media);
w.u16(0x55c4); // "und"
w.u16(0);
});
diff --git a/rust/engine/src/speaker.rs b/rust/engine/src/speaker.rs
new file mode 100644
index 00000000..634a58d7
--- /dev/null
+++ b/rust/engine/src/speaker.rs
@@ -0,0 +1,414 @@
+//! The player's sound: the shown take's soundtrack (a WAV, see `take`), played on its screen's
+//! speakers and following the player's clock (see `player`): on and on while the clock runs, and
+//! again from where it is after the clock jumps (a seek, a new rate) or once the two drift `SLACK`
+//! apart. In a window, on this machine's audio output (on Linux, where that would link ALSA, the
+//! window is silent); in a page, through Web Audio, which sounds once the page has been clicked or
+//! typed in (`resume`).
+
+use crate::player::Clock;
+
+/// Seconds the sound may drift from the clock before it starts again where the clock is (the
+/// audio device's crystal and the system's clock part by tens of millionths: minutes apart).
+#[cfg(any(target_arch = "wasm32", all(feature = "window", not(target_os = "linux"))))]
+const SLACK: f64 = 0.03;
+
+/// The player's sound: its volume, and the speaker playing the shown take's soundtrack, opened
+/// once a take has one.
+pub(crate) struct Sound {
+ pub(crate) volume: f32,
+ pub(crate) muted: bool,
+ jumped: bool, // the clock jumped since the speaker last followed it
+ take: Option<u32>, // the take whose sound the speaker has
+ speaker: Option<Speaker>,
+}
+
+impl Sound {
+ pub(crate) fn new() -> Self {
+ Self { volume: 1.0, muted: false, jumped: true, take: None, speaker: None }
+ }
+
+ /// The clock jumped: the sound starts again where it is.
+ pub(crate) fn jumped(&mut self) {
+ self.jumped = true;
+ }
+
+ /// The viewer did something (a key, a click, a tap): a page may sound from now on.
+ pub(crate) fn resume(&self) {
+ #[cfg(target_arch = "wasm32")]
+ web::resume();
+ }
+
+ /// Play take `take`'s soundtrack (none: silence) by `clock`, silent while the film `waits`
+ /// for frames.
+ pub(crate) fn follow(&mut self, take: Option<u32>, sound: Option<&[u8]>, clock: Clock, waits: bool) {
+ let take = take.filter(|_| sound.is_some());
+ if self.take != take {
+ if self.speaker.is_none() && sound.is_some() {
+ self.speaker = Speaker::new();
+ }
+ if let Some(speaker) = &mut self.speaker {
+ speaker.load(sound);
+ }
+ self.jumped = true;
+ }
+ if let Some(speaker) = &mut self.speaker {
+ let volume = if self.muted || waits { 0.0 } else { self.volume };
+ speaker.follow(clock, volume, self.jumped);
+ }
+ (self.take, self.jumped) = (take, false);
+ }
+}
+
+/// A WAV file's sound, as a take carries it (16-bit PCM): its samples (interleaved, as floats),
+/// its rate and its channels. (A page has the browser read it.)
+#[cfg(not(target_arch = "wasm32"))]
+#[cfg_attr(not(all(feature = "window", not(target_os = "linux"))), allow(dead_code))]
+fn wav(file: &[u8]) -> Option<(Vec<f32>, u32, usize)> {
+ let u16_at = |b: &[u8], at: usize| Some(u16::from_le_bytes(b.get(at..at.checked_add(2)?)?.try_into().ok()?));
+ let u32_at = |b: &[u8], at: usize| Some(u32::from_le_bytes(b.get(at..at.checked_add(4)?)?.try_into().ok()?));
+ if file.get(..4)? != b"RIFF" || file.get(8..12)? != b"WAVE" {
+ return None;
+ }
+ let (mut at, mut format) = (12usize, None);
+ while at.checked_add(8).is_some_and(|end| end <= file.len()) {
+ let size = u32_at(file, at + 4)? as usize;
+ let body = &file[at + 8..(at + 8).saturating_add(size).min(file.len())];
+ match &file[at..at + 4] {
+ b"fmt " => format = Some((u16_at(body, 0)?, usize::from(u16_at(body, 2)?), u32_at(body, 4)?, u16_at(body, 14)?)),
+ b"data" => {
+ let (kind, channels, rate, bits) = format?;
+ if kind != 1 || bits != 16 || channels == 0 {
+ return None;
+ }
+ let samples = body.chunks_exact(2).map(|s| f32::from(i16::from_le_bytes([s[0], s[1]])) / 32768.0).collect();
+ return Some((samples, rate, channels));
+ }
+ _ => {}
+ }
+ // the next chunk (a chunk's size is even, a pad byte after an odd one)
+ at = at.checked_add(8)?.checked_add(size)?.checked_add(size & 1)?;
+ }
+ None
+}
+
+#[cfg(all(feature = "window", not(any(target_os = "linux", target_arch = "wasm32"))))]
+use native::Speaker;
+#[cfg(not(any(target_arch = "wasm32", all(feature = "window", not(target_os = "linux")))))]
+use silent::Speaker;
+#[cfg(target_arch = "wasm32")]
+use web::Speaker;
+
+/// No speaker: a window on Linux is silent (the system's audio would be linked: ALSA).
+#[cfg(not(any(target_arch = "wasm32", all(feature = "window", not(target_os = "linux")))))]
+mod silent {
+ use crate::player::Clock;
+
+ pub(super) enum Speaker {}
+
+ impl Speaker {
+ pub(super) fn new() -> Option<Self> {
+ None
+ }
+ pub(super) fn load(&mut self, _: Option<&[u8]>) {}
+ pub(super) fn follow(&mut self, _: Clock, _: f32, _: bool) {}
+ }
+}
+
+/// The system's default audio output, playing a soundtrack by the clock: its thread fills the
+/// device's buffer with the samples for the instant they will be heard.
+#[cfg(all(feature = "window", not(any(target_os = "linux", target_arch = "wasm32"))))]
+mod native {
+ use std::sync::{Arc, Mutex};
+ use std::time::Instant;
+
+ use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
+
+ use super::SLACK;
+ use crate::player::Clock;
+
+ /// What the device's thread plays: the soundtrack (interleaved, at the device's rate and
+ /// channels), and where in it, following the clock.
+ struct Track {
+ samples: Vec<f32>,
+ clock: Clock,
+ volume: f32,
+ position: Option<f64>, // in frames of the soundtrack; None: where the clock is, next
+ loads: u64, // soundtracks asked for: only the newest is played
+ }
+
+ pub(super) struct Speaker {
+ _stream: cpal::Stream,
+ track: Arc<Mutex<Track>>,
+ rate: u32,
+ channels: usize,
+ }
+
+ impl Speaker {
+ /// The default output, playing silence until it is given a soundtrack; None if there is
+ /// none (or none that takes float samples).
+ pub(super) fn new() -> Option<Self> {
+ let device = cpal::default_host().default_output_device()?;
+ let supported = device.default_output_config().ok()?;
+ if supported.sample_format() != cpal::SampleFormat::F32 {
+ return None;
+ }
+ let config: cpal::StreamConfig = supported.into();
+ let (rate, channels) = (config.sample_rate, usize::from(config.channels));
+ let track = Arc::new(Mutex::new(Track { samples: Vec::new(), clock: Clock { time: 0.0, since: None, rate: 1.0 }, volume: 1.0, position: None, loads: 0 }));
+ let playing = track.clone();
+ let stream = device
+ .build_output_stream::<f32, _, _>(
+ config,
+ move |out, info| {
+ let Ok(mut track) = playing.lock() else { return out.fill(0.0) };
+ // these samples are heard this much later
+ let stamp = info.timestamp();
+ let heard = Instant::now() + stamp.playback.duration_since(stamp.callback);
+ fill(&mut track, out, channels, f64::from(rate), heard);
+ },
+ |_| {},
+ None,
+ )
+ .ok()?;
+ stream.play().ok()?;
+ Some(Self { _stream: stream, track, rate, channels })
+ }
+
+ /// Play `file` (a WAV), or nothing: silence until it is read and resampled, on a thread
+ /// of its own (a long one takes a while; the player draws on).
+ pub(super) fn load(&mut self, file: Option<&[u8]>) {
+ let Ok(mut track) = self.track.lock() else { return };
+ track.loads += 1;
+ (track.samples, track.position) = (Vec::new(), None);
+ let Some(file) = file.map(<[u8]>::to_vec) else { return };
+ let (load, playing, rate, out) = (track.loads, self.track.clone(), self.rate, self.channels);
+ std::thread::spawn(move || {
+ let Some((samples, from, channels)) = super::wav(&file) else { return };
+ let resampled = crate::audio::resample(&samples, channels, f64::from(from), f64::from(rate));
+ // its channels onto the device's: one to all, or each to its own (wrapping)
+ let frames = resampled.len() / channels;
+ let samples = (0..frames * out).map(|k| resampled[k / out * channels + k % out % channels]).collect();
+ if let Ok(mut track) = playing.lock()
+ && track.loads == load
+ {
+ (track.samples, track.position) = (samples, None);
+ }
+ });
+ }
+
+ /// Follow `clock` at `volume`; `jumped`: from where the clock is.
+ pub(super) fn follow(&mut self, clock: Clock, volume: f32, jumped: bool) {
+ if let Ok(mut track) = self.track.lock() {
+ if jumped || track.clock.since.is_some() != clock.since.is_some() || track.clock.rate != clock.rate {
+ track.position = None;
+ }
+ (track.clock, track.volume) = (clock, volume);
+ }
+ }
+ }
+
+ /// The device's next samples: the soundtrack from where it is (linearly between its samples
+ /// at a rate other than 1) at its volume, or silence while the clock is held, or past its end.
+ fn fill(track: &mut Track, out: &mut [f32], channels: usize, rate: f64, heard: Instant) {
+ let clock = track.clock;
+ let wanted = clock.at(heard) * rate;
+ let start = match track.position {
+ Some(at) if (at - wanted).abs() <= SLACK * rate => at,
+ _ => wanted,
+ };
+ let frames = track.samples.len() / channels;
+ if clock.since.is_none() || track.volume == 0.0 || frames == 0 {
+ out.fill(0.0);
+ track.position = Some(wanted);
+ return;
+ }
+ let mut position = start;
+ for frame in out.chunks_exact_mut(channels) {
+ let (i, f) = (position.floor(), (position - position.floor()) as f32);
+ for (c, sample) in frame.iter_mut().enumerate() {
+ let get = |k: f64| if k >= 0.0 && (k as usize) < frames { track.samples[k as usize * channels + c] } else { 0.0 };
+ *sample = track.volume * (get(i) * (1.0 - f) + get(i + 1.0) * f);
+ }
+ position += clock.rate;
+ }
+ track.position = Some(position);
+ }
+}
+
+/// The page's sound: the soundtrack in an AudioBuffer (read by the browser, off the page's
+/// thread), played from where the clock is by a source started at a moment of the audio
+/// context's own clock, which the browser keeps exactly (a source plays on the audio thread,
+/// whatever the page's thread is doing). The context's output timestamp ties its clock to the
+/// page's: what it plays at a time is heard at an instant.
+#[cfg(target_arch = "wasm32")]
+mod web {
+ use std::cell::{Cell, RefCell};
+ use std::rc::Rc;
+
+ use wasm_bindgen::prelude::*;
+ use wasm_bindgen_futures::JsFuture;
+ use web_sys::{AudioBuffer, AudioBufferSourceNode, AudioContext, AudioContextState, GainNode};
+ use web_time::{Duration, Instant};
+
+ use super::SLACK;
+ use crate::player::Clock;
+
+ /// Seconds an output timestamp may be old: older, the context's device stalls (the machine
+ /// slept), and nothing is scheduled by it until it plays again.
+ const STALE: f64 = 0.2;
+
+ #[wasm_bindgen]
+ extern "C" {
+ /// An audio context, as its output timestamp (not in web-sys).
+ type Stamped;
+ #[wasm_bindgen(method, js_name = getOutputTimestamp)]
+ fn output_timestamp(this: &Stamped) -> Stamp;
+ /// The context's time being heard, and the page's (`performance.now`) when it is.
+ type Stamp;
+ #[wasm_bindgen(method, getter, js_name = contextTime)]
+ fn context_time(this: &Stamp) -> f64;
+ #[wasm_bindgen(method, getter, js_name = performanceTime)]
+ fn performance_time(this: &Stamp) -> f64;
+ #[wasm_bindgen(js_namespace = performance, js_name = now)]
+ fn performance_now() -> f64;
+ }
+
+ thread_local! {
+ // the page's one audio context, every player's sound through it
+ static CONTEXT: RefCell<Option<AudioContext>> = const { RefCell::new(None) };
+ }
+
+ /// The page's audio context, made when it is first wanted.
+ fn context() -> Option<AudioContext> {
+ CONTEXT.with_borrow_mut(|context| {
+ if context.is_none() {
+ *context = AudioContext::new().ok();
+ }
+ context.clone()
+ })
+ }
+
+ /// The viewer did something: the page's context is made, if it was not, and plays. A
+ /// browser lets a page sound from within such a gesture on.
+ pub(super) fn resume() {
+ if let Some(context) = context().filter(|c| c.state() != AudioContextState::Running) {
+ let _ = context.resume();
+ }
+ }
+
+ /// A source playing: from `offset` seconds of the soundtrack at the context's time `when`, at
+ /// `rate`.
+ struct Playing {
+ source: AudioBufferSourceNode,
+ when: f64,
+ offset: f64,
+ rate: f64,
+ }
+
+ pub(super) struct Speaker {
+ context: AudioContext,
+ gain: GainNode,
+ buffer: Rc<RefCell<Option<AudioBuffer>>>, // the soundtrack, once the browser has read it
+ loads: Rc<Cell<u64>>, // soundtracks asked for: only the newest is kept
+ playing: Option<Playing>,
+ }
+
+ impl Speaker {
+ pub(super) fn new() -> Option<Self> {
+ let context = context()?;
+ let gain = context.create_gain().ok()?;
+ gain.connect_with_audio_node(&context.destination()).ok()?;
+ Some(Self { context, gain, buffer: Rc::default(), loads: Rc::default(), playing: None })
+ }
+
+ /// Play `file` (an audio file), or nothing: silence until the browser has read it.
+ pub(super) fn load(&mut self, file: Option<&[u8]>) {
+ self.stop();
+ self.loads.set(self.loads.get() + 1);
+ self.buffer.replace(None);
+ let Some(file) = file else { return };
+ let Ok(read) = self.context.decode_audio_data(&js_sys::Uint8Array::from(file).buffer()) else { return };
+ let (load, loads, buffer) = (self.loads.get(), self.loads.clone(), self.buffer.clone());
+ wasm_bindgen_futures::spawn_local(async move {
+ if let Ok(read) = JsFuture::from(read).await
+ && loads.get() == load
+ {
+ buffer.replace(Some(read.unchecked_into()));
+ }
+ });
+ }
+
+ fn stop(&mut self) {
+ if let Some(playing) = self.playing.take() {
+ let _ = web_sys::AudioScheduledSourceNode::stop(&playing.source);
+ }
+ }
+
+ pub(super) fn follow(&mut self, clock: Clock, volume: f32, jumped: bool) {
+ self.gain.gain().set_value(volume);
+ let Some(buffer) = self.buffer.borrow().clone() else { return };
+ // the context's time heard now: none until its output has begun, while it is held
+ // (until the page is clicked or typed in), and while its device stalls
+ let stamp = self.context.unchecked_ref::<Stamped>().output_timestamp();
+ let (heard, at, now) = (stamp.context_time(), stamp.performance_time(), performance_now());
+ let running = self.context.state() == AudioContextState::Running && heard > 0.0 && now - at < STALE * 1000.0;
+ if clock.since.is_none() || volume == 0.0 || !running {
+ return self.stop();
+ }
+ let heard = heard + (now - at) / 1000.0;
+ // what is heard now against what the clock says
+ let off = self.playing.as_ref().is_none_or(|p| jumped || p.rate != clock.rate || (p.offset + (heard - p.when) * p.rate - clock.at(Instant::now())).abs() > SLACK);
+ if !off {
+ return;
+ }
+ self.stop();
+ // from where the clock will be when it is heard: a moment after the context's time
+ // now, which is heard later by the context's latency
+ let mut when = self.context.current_time() + 0.01;
+ let mut offset = clock.at(Instant::now() + Duration::from_secs_f64((when - heard).max(0.0)));
+ if offset < 0.0 {
+ when -= offset / clock.rate;
+ offset = 0.0;
+ }
+ if offset >= buffer.duration() {
+ return;
+ }
+ let Ok(source) = self.context.create_buffer_source() else { return };
+ source.set_buffer(Some(&buffer));
+ source.playback_rate().set_value(clock.rate as f32);
+ if source.connect_with_audio_node(&self.gain).is_ok() && source.start_with_when_and_grain_offset(when, offset).is_ok() {
+ self.playing = Some(Playing { source, when, offset, rate: clock.rate });
+ }
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ #[test]
+ fn a_takes_sound_is_read_as_written() {
+ // a WAV as Python's `wave` writes a take's: 16-bit, two channels, 48 kHz
+ let samples: [i16; 6] = [0, 32767, -32768, 16384, -1, 1];
+ let data: Vec<u8> = samples.iter().flat_map(|s| s.to_le_bytes()).collect();
+ let mut file = b"RIFF".to_vec();
+ file.extend((36 + data.len() as u32).to_le_bytes());
+ file.extend(b"WAVEfmt ");
+ file.extend(16u32.to_le_bytes());
+ file.extend([1u16, 2].iter().flat_map(|v| v.to_le_bytes()));
+ file.extend([48000u32, 48000 * 4].iter().flat_map(|v| v.to_le_bytes()));
+ file.extend([4u16, 16].iter().flat_map(|v| v.to_le_bytes()));
+ file.extend(b"data");
+ file.extend((data.len() as u32).to_le_bytes());
+ file.extend(&data);
+ let (read, rate, channels) = super::wav(&file).expect("a WAV");
+ assert_eq!((rate, channels), (48000, 2));
+ assert_eq!(read, samples.map(|s| f32::from(s) / 32768.0));
+ // not a WAV of 16-bit PCM: no sound
+ assert!(super::wav(b"RIFF\0\0\0\0WAVE").is_none());
+ assert!(super::wav(b"OggS").is_none());
+ // a chunk as long as can be: past the end, not around
+ let mut endless = b"RIFF\0\0\0\0WAVEjunk".to_vec();
+ endless.extend(u32::MAX.to_le_bytes());
+ assert!(super::wav(&endless).is_none());
+ }
+}