platform/audio/desktop
데스크탑 오디오 백엔드. (윈도우, 맥, 리눅스, cpal 출력 위의 엔진 믹서)
- AudioContext 자리: cpal 로 기본 출력 장치를 열고 그 표본율로 도는 믹서(mixer.rs). 장치가 없으면 한 번 알리고 다시 열지 않는다. 브라우저 자동재생 정책이 없어서 user gesture 는 처음부터 있는 것으로 둔다. sample_rate(), current_time() 은 웹 AudioContext 와 같은 이름이다.
- 노드 그래프는 원본 WebAudio 와 같고 셈도 크롬과 같다. (mixer.rs 머리 주석) AudioPlayer 하나 = 소스 + GainNode (+ 0 이 아닌 좌우를 처음 걸면 StereoPannerNode), 내보낼 곳(AudioOutputNode)을 주면 그 버스로, 없으면 출력으로. AudioBus 하나 = 출력에 이은 GainNode. (AudioGainNode, 원본 getGainNode) 버스로 만든 플레이어는 버스 손잡이를 들고 있어, 원본 WebAudio 처럼 이어진 플레이어가 있는 동안 버스가 살아 있다. 손잡이가 모두 없어지면 노드를 뺀다.
- 컨텍스트 멈춤(SoundSystem 이 음소거 동안 부른다): 믹서가 그리기를 멈춰 소리와 시계가 그 자리에서 선다. (원본 AudioContext.suspend) resume 이 다시 돌린다.
- 컨텍스트 시계(get_audio_context_time)는 그린 시간이다. (원본 currentTime, 128 프레임 퀀텀마다 128 / 표본율 초씩 오른다)
- 스트림은 관리 스레드가 든다. 장치가 빠지면(맥이 아니면 기본 장치가 바뀌어도) 같은 믹서로 기본 장치를 다시 연다. (원본 크롬도 기본 장치를 따라간다)
- AudioBuffer 자리: 푼 소리를 Arc 로 든다. 복제는 값싸다. 해석은 decode 가 원본의 크롬 decodeAudioData 와 같게 한다. (webm, ogg 의 Opus 와 Vorbis, mp3, wav, flac, m4a 와 aac 의 AAC. 앞뒤 다듬기, 44100Hz, WebAudio 스피커 규칙으로 내린 1 채널이나 2 채널)
- AudioStream 자리: 스트리밍으로 읽은 소리. (원본 AudioLoadType.streaming, 압축된 자리(파일 경로나 바이트)와 다듬기 계획만 든다) 틀 때 플레이어마다 풀개 스레드가 조각마다 풀어 링 버퍼(128 KiB)를 채우고 믹서의 스트리밍 소스가 읽는다. (streaming.rs, 표본은 통째로 푼 버퍼와 같다)
- 음량, 좌우, 정지는 다음 렌더 퀀텀부터 바로 바뀐다. 재생 빠르기는 다음 재생부터다. (원본과 같다)
- 음량은 GainNode 의 AudioParam 이다. 음량 설정은 원본 gain.value 대입, SoundSystem 은 원본처럼 자동화(setValueAtTime 들)와 stop(when)을 건다. 지금 gain.value(get_gain_value)는 소리 스레드가 퀀텀마다 셈한 내재 값이다.
- BeepPlayer 의 톤은 원본 playTone 처럼 믹서에 OscillatorNode → GainNode(엔벨로프 자동화) → 출력을 짓는다. (셈은 크롬과 같다, oscillator.rs) 파형의 대역 제한 표(PeriodicWave)는 원본 컨텍스트처럼 표본율마다 한 번 지어 둔다.
Desktop audio backend. (Windows, Mac, Linux, the engine mixer on top of cpal output)
- In place of AudioContext: opens the default output device with cpal and runs a mixer at its sample rate (mixer.rs). If there is no device it reports once and does not open again. There is no browser autoplay policy, so the user gesture is treated as present from the start. sample_rate(), current_time() have the same names as the web AudioContext.
- The node graph is the same as the original WebAudio and the math matches Chrome. (header comment of mixer.rs) One AudioPlayer = source + GainNode (+ StereoPannerNode when a nonzero pan is first set), to that bus if an output (AudioOutputNode) is given, otherwise to the output. One AudioBus = a GainNode connected to the output. (AudioGainNode, the original getGainNode) Players made on a bus hold a bus handle, so as in the original WebAudio the bus stays alive while connected players exist. The node is removed once all handles are gone.
- Context suspend (SoundSystem calls it while muted): the mixer stops rendering, so the sound and clock stop in place. (the original AudioContext.suspend) resume starts it again.
- The context clock (get_audio_context_time) is the rendered time. (the original currentTime, rises by 128 / sample rate seconds every 128-frame quantum)
- A management thread holds the stream. When the device is removed (or, except on Mac, when the default device changes), it reopens the default device with the same mixer. (the original Chrome also follows the default device)
- In place of AudioBuffer: holds decoded sound in an Arc. Cloning is cheap. decode decodes it the same way as the original Chrome decodeAudioData. (Opus and Vorbis in webm, ogg, mp3, wav, flac, AAC in m4a and aac. Trimming of both ends, 44100Hz, downmixed to 1 or 2 channels by the WebAudio speaker rules)
- In place of AudioStream: a sound loaded for streaming. (the original AudioLoadType.streaming, holds only the compressed source (a file path or bytes) and the trim plan) When played, a decoder thread per player decodes chunk by chunk into a ring buffer (128 KiB) that the mixer's streaming source reads. (streaming.rs, the samples are the same as the fully decoded buffer)
- Volume, pan and stop change right from the next render quantum. Playback rate applies from the next playback. (same as the original)
- Volume is the AudioParam of the GainNode. Setting the volume is assigning the original gain.value, and SoundSystem sets automation (setValueAtTime calls) and stop(when) as in the original. The current gain.value (get_gain_value) is the intrinsic value the sound thread computes every quantum.
- BeepPlayer's tones build OscillatorNode → GainNode (envelope automation) → output on the mixer like the original playTone. (the math matches Chrome, oscillator.rs) The waveform's band-limited tables (PeriodicWave) are built once per sample rate, as in the original context.
AudioContextstruct
공유 오디오 컨텍스트. (원본 AudioContext 자리, 장치 스트림과 믹서로 가는 길)
- AudioManager 가 하나 들고, get_audio_context 로 빌려 읽는다. (게임 코드가 직접 만들지 않는다)
Shared audio context. (in place of the original AudioContext, the path to the device stream and mixer)
- AudioManager holds one, and it is borrowed and read with get_audio_context. (game code does not create it directly)
use vanilla::AudioManager;
let audio_manager = AudioManager::new();
audio_manager.resume_context();
let audio_context = audio_manager.get_audio_context();
if let Some(audio_context) = audio_context.as_deref() {
assert!(audio_context.sample_rate() > 0.0);
assert!(audio_context.current_time() >= 0.0);
}표본율. (원본 audioContext.sampleRate, 장치의 표본율)
Sample rate. (the original audioContext.sampleRate, the device's sample rate)
반환Returnsf32 지금 연 출력 장치의 표본율. (Hz, 장치가 바뀌면 새 장치의 것)Sample rate of the currently open output device. (Hz, the new device's when the device changes)
시계. (초, 원본 audioContext.currentTime)
Clock. (seconds, the original audioContext.currentTime)
반환Returnsf64 컨텍스트를 만든 뒤 그린 시간. (초, 128 프레임 퀀텀마다 오르고 멈춰 둔 동안은 선다)Time rendered since the context was created. (seconds, rises every 128-frame quantum and stops while suspended)
AudioBufferstruct traits Clone
푼 소리. (원본 AudioBuffer 자리, 복제는 Arc 하나라 값싸다)
- AudioAsset 이 들고, get_audio_buffer 로 받는다. 소리 장치 없이도 그 자리에서 푼다.
Decoded sound. (in place of the original AudioBuffer, cloning is just one Arc so it is cheap)
- AudioAsset holds it, and it is obtained with get_audio_buffer. Decoded on the spot even without a sound device.
use vanilla::{Engine, EngineConfiguration};
let engine = Engine::new(EngineConfiguration::new());
let audio_asset = engine.load_audio_asset("tests/assets/audio/flac_51.flac").unwrap();
let audio_buffer = audio_asset.get_audio_buffer().unwrap();
assert_eq!(audio_buffer.sample_rate(), 44100.0);
assert_eq!(audio_buffer.number_of_channels(), 2);
assert_eq!(audio_buffer.duration(), audio_buffer.length() as f64 / 44100.0);
let mono_asset = engine.load_audio_asset("tests/assets/audio/mp3_mono22.mp3").unwrap();
let mono_buffer = mono_asset.get_audio_buffer().unwrap();
assert_eq!(mono_buffer.number_of_channels(), 1);
assert_eq!(mono_buffer.sample_rate(), 44100.0);재생 시간. (초, 원본 duration)
Duration. (seconds, the original duration)
반환Returnsf64 프레임 수 / 표본율. (초)Frame count / sample rate. (seconds)
프레임 수. (원본 length)
Frame count. (the original length)
반환Returnsusize 채널 하나의 표본 수. (첫 채널의 길이)Sample count of one channel. (the length of the first channel)
채널 수. (원본 numberOfChannels, 데스크탑은 스피커 규칙으로 내린 1 이나 2)
Channel count. (the original numberOfChannels, on desktop 1 or 2 downmixed by the speaker rules)
반환Returnsusize 1 이나 2.1 or 2.
표본율. (원본 sampleRate, 44100)
Sample rate. (the original sampleRate, 44100)
반환Returnsf32 푼 소리의 표본율. (Hz, 늘 44100)Sample rate of the decoded sound. (Hz, always 44100)
AudioGainNodetype
버스의 GainNode 자리. (원본 AudioBus.getGainNode, 버스를 나눠 든다)
In place of the bus's GainNode. (the original AudioBus.getGainNode, shares the bus)
pub type AudioGainNode = Rc<MixerGainNode>AudioOutputNodetype
플레이어가 소리를 내보낼 곳. (원본 AudioPlayer 생성자의 outputNode 자리, 그 버스로 낸다. AudioGainNode 의 as_deref)
Where the player sends its sound. (in place of the outputNode of the original AudioPlayer constructor, outputs to that bus. as_deref of AudioGainNode)
pub type AudioOutputNode<'a> = &'a MixerGainNode