# platform/audio/web (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/platform/audio/web/

Web audio backend. (wasm32, calls WebAudio directly through web-sys)
- Ports the WebAudio calls of the original audiomanager.js, audioplayer.js, audioasset.js, beepplayer.js as they are.
- AudioContext: as in the original, AudioContext, or webkitAudioContext if missing. Created after a user gesture. (create_audio_player creates it even before that)
- resume: calls resume() only when "suspended" or "interrupted" (a nonstandard state of iOS WebKit) and ignores rejection.
  The state is read as a string. (web-sys's AudioContextState has no "interrupted")
- iOS clock check: the original verifyContextClock as is. (2026-10-22 03:41, for the iOS 26.6.2 update)
  If after restoring it is "running" but current_time advances less than 1/4 of real time over 0.5 seconds, calls suspend() then resume() and checks again.
  If that resume() is rejected, restores again with resume_context() after the same interval. Only one runs at a time.
- Decoding: done with OfflineAudioContext(1, 1, 44100) as in the original. If missing, done with a temporary AudioContext which is then closed.
- Context suspend: like the original SoundSystem.applyContextPause, calls suspend() only when "running", and reports and ignores rejection.
- Pan: like the original SoundSystem.playEffect, inserts a StereoPannerNode between the GainNode and the output if nonzero. (not inserted if createStereoPanner is missing)
- Bus: like the original AudioBus, one GainNode connected to the output. Players made on a bus connect their GainNode to the bus's GainNode instead of the output.
- The player's output (AudioOutputNode): like the outputNode of the original AudioPlayer constructor, takes an AudioNode and connects the player's GainNode to it. (the output if none)
- If the fetch response has a failure status, it counts as a failure before decoding, as in the original.
- Async: promises are awaited with spawn_local and JsFuture of wasm-bindgen-futures. (in place of the original then / await)
  Callbacks hold the manager's or player's state as Weak, and do nothing if it was dropped in the meantime.
- Differences from the original:
  Dropping the player lets the playing source node play to the end, as in the original. A play that was waiting for a resume also starts after the resume. (in the original that callback holds the player)
  onended is replaced with a self-releasing callback (Closure::once_into_js) so a dropped closure is never called, and the GainNodes are disconnected when it ends.
- One player plays one sound. If another source started while waiting for the resume, it stops that one and plays, like the original startPlayback.

Example: Wait for fetch and decoding to finish, then check the length (on the web the buffer exists only once finished, and the same code runs on desktop)

```rust
use std::rc::Rc;
use vanilla::{AudioAsset, Engine, EngineConfiguration};

let engine = Engine::new(EngineConfiguration::new());
let audio_asset = Rc::new(AudioAsset::new());
let audio_load_handle = audio_asset.load_from_asset(&engine, "tests/assets/audio/wav_s16_stereo.wav");
engine.spawn_task(move |_engine_context| async move {
	if audio_load_handle.await.is_ok() {
		assert!(audio_asset.get_duration() > 0.0);
	}
});
```

## AudioContext (type)

Shared audio context.

```rust
pub type AudioContext = web_sys::AudioContext
```

## AudioBuffer (type)

Decoded sound.

```rust
pub type AudioBuffer = web_sys::AudioBuffer
```

## AudioGainNode (type)

The bus's GainNode. (the original AudioBus.getGainNode)

```rust
pub type AudioGainNode = web_sys::GainNode
```

## AudioOutputNode (type)

Where the player sends its sound. (the outputNode of the original AudioPlayer constructor, the player's GainNode is connected to it. as_deref of AudioGainNode or another AudioNode)

```rust
pub type AudioOutputNode<'a> = &'a web_sys::AudioNode
```

