API / platform/audio/web
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 처럼 그것을 멈추고 튼다.
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.
예제Examplefetch 와 디코딩이 끝나기를 기다린 뒤 길이 보기 (웹은 끝나야 버퍼가 생긴다, 데스크탑에서도 같은 코드가 돈다)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)
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);
}
});AudioContexttype
공유 오디오 컨텍스트.
Shared audio context.
pub type AudioContext = web_sys::AudioContextAudioBuffertype
디코딩한 소리.
Decoded sound.
pub type AudioBuffer = web_sys::AudioBufferAudioGainNodetype
버스의 GainNode. (원본 AudioBus.getGainNode)
The bus's GainNode. (the original AudioBus.getGainNode)
pub type AudioGainNode = web_sys::GainNodeAudioOutputNodetype
플레이어가 소리를 내보낼 곳. (원본 AudioPlayer 생성자의 outputNode, 플레이어의 GainNode 를 거기에 잇는다. AudioGainNode 의 as_deref 나 다른 AudioNode)
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)
pub type AudioOutputNode<'a> = &'a web_sys::AudioNode