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

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

웹 오디오 백엔드. (wasm32, WebAudio 를 web-sys 로 직접 부른다)
- 원본 audiomanager.js, audioplayer.js, audioasset.js, beepplayer.js 의 WebAudio 호출을 그대로 옮겼다.
- AudioContext: 원본처럼 AudioContext, 없으면 webkitAudioContext. user gesture 뒤에 만든다. (create_audio_player 는 그 전에도 만든다)
- resume: "suspended" 나 "interrupted"(iOS WebKit 의 비표준 상태) 일 때만 resume() 하고 거부는 넘긴다.
  상태는 문자열로 읽는다. (web-sys 의 AudioContextState 에 "interrupted" 가 없다)
- iOS 시계 확인: 원본 verifyContextClock 그대로. (2026-10-22 03:41, iOS 26.6.2 버전업 대응)
  복원 뒤 "running" 인데 0.5초 동안 current_time 이 실제 시간의 1/4 보다 덜 흐르면 suspend() 후 resume() 하고 다시 확인한다.
  그 resume() 이 거부되면 같은 간격 뒤 resume_context() 로 다시 복원한다. 한 번에 하나만 돈다.
- 디코딩: 원본처럼 OfflineAudioContext(1, 1, 44100) 로 한다. 없으면 잠시 쓰는 AudioContext 로 하고 닫는다.
- 컨텍스트 멈춤: 원본 SoundSystem.applyContextPause 처럼 "running" 일 때만 suspend() 하고, 거부되면 알리고 넘긴다.
- 좌우(pan): 원본 SoundSystem.playEffect 처럼 0 이 아니면 StereoPannerNode 를 GainNode 와 출력 사이에 끼운다. (createStereoPanner 가 없으면 끼우지 않는다)
- 버스: 원본 AudioBus 처럼 출력에 이은 GainNode 하나다. 버스로 만든 플레이어는 GainNode 를 출력 대신 버스의 GainNode 에 잇는다.
- 플레이어의 내보낼 곳(AudioOutputNode): 원본 AudioPlayer 생성자의 outputNode 처럼 AudioNode 를 받아 플레이어의 GainNode 를 잇는다. (없으면 출력)
- fetch 응답이 실패 코드면 원본처럼 디코딩 전에 실패로 친다.
- 비동기: 프로미스는 wasm-bindgen-futures 의 spawn_local 과 JsFuture 로 기다린다. (원본 then / await 자리)
  콜백은 매니저나 플레이어의 상태를 Weak 로 들어, 그 사이 버려졌으면 아무것도 하지 않는다.
- 원본과 다른 점:
  플레이어를 버려도 원본처럼 트는 소스 노드는 끝까지 운다. 재개를 기다리던 재생도 재개 뒤에 튼다. (원본은 그 콜백이 플레이어를 붙든다)
  onended 를 스스로 풀리는 콜백(Closure::once_into_js)으로 바꿔 버린 클로저를 부르지 않고, 끝나면 GainNode 들을 뗀다.
- 한 플레이어는 소리 하나다. 재개를 기다리는 사이 다른 소스가 시작됐으면 원본 startPlayback 처럼 그것을 멈추고 튼다.

예제: fetch 와 디코딩이 끝나기를 기다린 뒤 길이 보기 (웹은 끝나야 버퍼가 생긴다, 데스크탑에서도 같은 코드가 돈다)

```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)

공유 오디오 컨텍스트.

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

## AudioBuffer (type)

디코딩한 소리.

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

## AudioGainNode (type)

버스의 GainNode. (원본 AudioBus.getGainNode)

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

## AudioOutputNode (type)

플레이어가 소리를 내보낼 곳. (원본 AudioPlayer 생성자의 outputNode, 플레이어의 GainNode 를 거기에 잇는다. AudioGainNode 의 as_deref 나 다른 AudioNode)

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

