core/audioplayer
오디오 플레이어.
- 원본 core/audioplayer.js. AudioAsset 에 로드된 오디오 버퍼를 재생한다.
- 음량, 음소거, 루프, 재생 위치는 여기서 들고, 소리를 내는 일은 백엔드(platform/audio 의 AudioPlayerBackend)가 한다. 원본의 GainNode 자리: 두 플랫폼 모두 원본처럼 GainNode 하나. (데스크탑은 엔진 믹서의 노드, 셈은 크롬 WebAudio 와 같다)
- 만드는 곳은 AudioManager::create_audio_player() 나 AudioPlayer::new(매니저, 내보낼 곳) 이다. AudioContext 가 없으면 아무 소리도 내지 않는 플레이어가 된다. (원본과 같다) new 는 원본 new AudioPlayer(audioContext, outputNode) 자리다. 첫 인자는 AudioContext 를 든 매니저이고(AudioContext 가 없으면 만든다), 내보낼 곳(AudioOutputNode)은 버스의 get_gain_node()(AudioGainNode)의 as_deref() 다. None 이면 출력이다. 두 플랫폼에서 같은 코드다. AudioPlayer::new(&audio_manager, audio_bus.get_gain_node().as_deref()) (웹은 &AudioNode 라 다른 AudioNode 도 넘길 수 있고, 데스크탑은 믹서 버스의 &MixerGainNode 다) 데스크탑은 내보낼 곳을 직접 넘겨 만든 플레이어도 원본 WebAudio 처럼 이어진 동안 그 버스 노드를 살려 둔다.
- play 는 원본처럼 AudioContext 가 멈춰 있으면 깨운 뒤 튼다. (웹은 resume() 을 기다렸다가, 데스크탑은 그 자리에서)
- 플레이어를 버려도 트는 소리는 원본 WebAudio 소스 노드처럼 끝까지 운다. (데스크탑은 믹서가 소스가 끝날 때까지 노드를 들고, 웹은 소스 노드가 운다) 루프 소리는 원본처럼 멈추지 않으니 버리기 전에 stop 한다.
- 원본과 다른 점: 애셋은
Rc<AudioAsset>로 나눠 든다. 데스크탑의 is_playing() 은 onended 콜백 대신 믹서가 소스를 끝냈는지 읽는다. (끝나면 거짓) get_audio_context 는 만든 매니저의 AudioContext 를 빌려 준다. (웹 WebAudio AudioContext, 데스크탑 엔진 믹서의 AudioContext, 매니저 없이 만든 플레이어는 None) set_playback_rate 는 원본에 없던 것을 러스트 판에서 먼저 만들었다. (원본에도 넣었다) AudioBus::create_audio_player 로 만든 플레이어는 출력 대신 그 버스로 소리를 낸다. (원본 AudioPlayer 의 출력 노드 인자) 버스 손잡이를 하나 들어 버스를 살려 둔다. - get_time 은 원본처럼 재생 중이면 시작 위치에 오디오 컨텍스트 시계로 흐른 시간을 더한 지금 위치다. (웹 currentTime, 데스크탑 믹서 시계)
Audio player.
- The original core/audioplayer.js. Plays the audio buffer loaded in an AudioAsset.
- Volume, mute, loop and playback position are held here, and making the sound is done by the backend (AudioPlayerBackend in platform/audio). In place of the original's GainNode: one GainNode on both platforms, as in the original. (on desktop a node of the engine mixer, the math matches Chrome WebAudio)
- Created with AudioManager::create_audio_player() or AudioPlayer::new(manager, output destination). Without an AudioContext it becomes a player that makes no sound. (same as the original) new stands in for the original new AudioPlayer(audioContext, outputNode). The first argument is the manager holding the AudioContext (it creates the AudioContext if absent), and the output destination (AudioOutputNode) is as_deref() of the bus's get_gain_node() (AudioGainNode). None means the output. The code is the same on both platforms. AudioPlayer::new(&audio_manager, audio_bus.get_gain_node().as_deref()) (on the web it is &AudioNode so other AudioNodes can be passed too, on desktop it is the mixer bus's &MixerGainNode) On desktop, a player created by passing the output destination directly also keeps that bus node alive while connected, like the original WebAudio.
- As in the original, play wakes the AudioContext first if it is suspended, then plays. (the web waits for resume(), desktop does it immediately)
- Dropping the player does not stop what it is playing; like a WebAudio source node in the original, the sound plays to the end. (on desktop the mixer holds the node until the source ends, on the web the source node plays) A looping sound does not stop, as in the original, so call stop before dropping the player.
- Differences from the original:
The asset is shared as `Rc<AudioAsset>`.
On desktop, is_playing() reads whether the mixer has finished the source instead of using the onended callback. (false when finished)
get_audio_context lends the AudioContext of the manager that created it. (the WebAudio AudioContext on the web, the engine mixer's AudioContext on desktop, None for a player created without a manager)
set_playback_rate was not in the original and was made first in the Rust version. (it was added to the original too)
A player created with AudioBus::create_audio_player sends its sound to that bus instead of the output. (the original AudioPlayer's output node argument) It holds one bus handle to keep the bus alive.- get_time, as in the original, is the current position while playing: the start position plus the time elapsed on the audio context clock. (web currentTime, desktop mixer clock)
AudioPlayerstruct
오디오 플레이어.
- 애셋 하나를 실어 틀고, 음량, 음소거, 루프, 재생 위치를 든다. 만든 쪽이 소유하고 버리면 소리도 멈춘다.
Audio player.
- Loads one asset and plays it, and holds volume, mute, loop and playback position. Owned by its creator, dropping it also stops the sound.
use vanilla::{Engine, EngineConfiguration};
let engine = Engine::new(EngineConfiguration::new());
let audio_asset = engine.load_audio_asset("tests/assets/audio/vorbis_stereo.ogg").unwrap();
let mut audio_player = engine.get_audio_manager().create_audio_player();
audio_player.set_audio_asset(audio_asset);
audio_player.set_volume(0.8);
audio_player.set_time(1.0);
audio_player.play(true);
assert!(audio_player.get_duration() > 1.0);
audio_player.mute();
audio_player.stop();pub fn new(audio_manager: &AudioManager, output_node: Option<AudioOutputNode<'_>>) -> AudioPlayer#생성. (원본 new AudioPlayer(audioContext, outputNode), 매니저의 AudioContext 가 없으면 만든다, 내보낼 곳이 None 이면 출력)
Constructor. (the original new AudioPlayer(audioContext, outputNode), creates the manager's AudioContext if absent, the output if the output destination is None)
- audio_manager&AudioManager
- AudioContext 를 든 매니저. (보통 engine.get_audio_manager())Manager holding the AudioContext. (usually engine.get_audio_manager())
- output_nodeOption<AudioOutputNode<'_>>
- 소리를 내보낼 노드. (버스의 get_gain_node().as_deref()) None 이면 출력.Node to send the sound to. (the bus's get_gain_node().as_deref()) The output if None.
반환ReturnsAudioPlayer 새 플레이어. (음량 1, 애셋 없음, AudioContext 를 만들지 못했으면 소리 없는 플레이어)New player. (volume 1, no asset, a silent player if the AudioContext could not be created)
짝Counterpart: vanilla.js AudioPlayer.constructor
use vanilla::{AudioManager, AudioPlayer};
let audio_manager = AudioManager::new();
let audio_bus = audio_manager.create_audio_bus();
let mut audio_player = AudioPlayer::new(&audio_manager, audio_bus.get_gain_node().as_deref());
audio_player.set_volume(0.5);
assert_eq!(audio_player.get_volume(), 0.5);재생. (재생 중이면 멈추고 재생 위치부터 다시 튼다)
- 오디오 컨텍스트가 일시 중단돼 있으면 재개한 뒤에 튼다. (웹은 resume() 을 기다렸다가, 데스크탑은 그 자리에서)
- 소리 없는 플레이어이거나 애셋이 없거나 애셋이 아직 읽히지 않았으면 아무것도 하지 않는다.
Plays. (if playing, stops and plays again from the playback position)
- If the audio context is suspended, resumes it and then plays. (the web waits for resume(), desktop does it immediately)
- Does nothing for a silent player, if there is no asset, or if the asset is not loaded yet.
- is_loopbool
- true 면 끝에서 처음으로 돌아가 반복한다.If true, goes back to the start at the end and repeats.
짝Counterpart: vanilla.js AudioPlayer.play
재생 빠르기 설정. (1 이면 그대로, 음 높이도 같이 바뀐다, 다음 play 부터, 원본 setPlaybackRate)
Sets the playback speed. (1 is unchanged, the pitch changes too, from the next play, the original setPlaybackRate)
- playback_ratef64
- 재생 빠르기 배율. (2 면 두 배 빠르게, 0 이하이거나 유한하지 않으면 1)Playback speed multiplier. (2 is twice as fast, 1 if 0 or less or not finite)
짝Counterpart: vanilla.js AudioPlayer.setPlaybackRate
음소거. (백엔드가 없으면 아무것도 하지 않는다)
Mute. (does nothing if there is no backend)
짝Counterpart: vanilla.js AudioPlayer.mute
음소거 해제. (설정한 음량으로 돌아간다)
Unmute. (returns to the set volume)
짝Counterpart: vanilla.js AudioPlayer.unmute
음량 설정. (0 ~ 1, 음소거 중이면 음소거를 풀 때 적용된다)
Sets the volume. (0 ~ 1, if muted it applies when unmuted)
- volumef64
- 음량. (0 ~ 1 로 자른다, 유한하지 않으면 1)Volume. (clamped to 0 ~ 1, 1 if not finite)
짝Counterpart: vanilla.js AudioPlayer.setVolume
음량 반환.
Returns the volume.
반환Returnsf64 설정한 음량. (0 ~ 1, 음소거해도 바뀌지 않는다)The set volume. (0 ~ 1, does not change when muted)
짝Counterpart: vanilla.js AudioPlayer.getVolume
재생 위치 설정. (초, 재생 중이면 그 위치부터 바로 다시 튼다)
Sets the playback position. (seconds, if playing, plays again from that position right away)
- timef64
- 재생 위치. (초, 애셋 처음부터)Playback position. (seconds, from the start of the asset)
짝Counterpart: vanilla.js AudioPlayer.setTime
오디오 애셋 설정.
- 재생 중인 소리는 바꾸지 않는다. 다음 play 부터 이 애셋을 튼다.
Sets the audio asset.
- Does not change the sound being played. This asset plays from the next play.
- audio_assetRc<AudioAsset>
- 틀 애셋. (engine.load_audio_asset 으로 읽은 것)Asset to play. (loaded with engine.load_audio_asset)
짝Counterpart: vanilla.js AudioPlayer.setAudioAsset
오디오 애셋 반환.
Returns the audio asset.
반환ReturnsOption<&Rc<AudioAsset>> 실은 애셋. (없으면 None)The loaded asset. (None if none)
짝Counterpart: vanilla.js AudioPlayer.getAudioAsset
재생 여부 반환.
Returns whether playing.
반환Returnsbool 지금 소리를 내고 있으면 true. (끝났거나 멈췄으면 false)true if currently making sound. (false if finished or stopped)
짝Counterpart: vanilla.js AudioPlayer.isPlaying
음소거 여부 반환.
Returns whether muted.
반환Returnsbool 음소거 중이면 true. (소리 없는 플레이어는 mute 를 불러도 false)true if muted. (false for a silent player even if mute is called)
짝Counterpart: vanilla.js AudioPlayer.isMuted
재생 위치 반환. (초, 재생 중이면 흐르는 지금 위치, 멈췄거나 끝났으면 다음 play 가 시작할 set_time 의 위치)
- 재생 중이면 시작 위치에 오디오 컨텍스트 시계로 흐른 시간과 재생 빠르기를 곱해 더한다. (컨텍스트가 멈춰 있으면 같이 멈춘다)
- 루프면 길이로 되감고, 아니면 길이에서 멈춘다.
Returns the playback position. (seconds; while playing, the current moving position; if stopped or ended, the set_time position where the next play starts)
- While playing, adds the time elapsed on the audio context clock multiplied by the playback rate to the start position. (it stops too while the context is suspended)
- If looping, wraps around by the length; otherwise stops at the length.
반환Returnsf64 재생 위치. (초, 기본 0)Playback position. (seconds, default 0)
짝Counterpart: vanilla.js AudioPlayer.getTime
AudioContext. (원본 getAudioContext, 만든 매니저의 것을 빌려 준다, 없으면 None)
AudioContext. (the original getAudioContext, lends the one of the manager that created it, None if none)
반환ReturnsOption<Ref<'_, AudioContext>> 빌린 AudioContext. (매니저에 AudioContext 가 없으면 None)The borrowed AudioContext. (None if the manager has no AudioContext)
짝Counterpart: vanilla.js AudioPlayer.getAudioContext
재생 시간 반환. (초, 애셋이 없으면 0)
Returns the playback duration. (seconds, 0 if there is no asset)
반환Returnsf64 실은 애셋의 길이. (초, 애셋이 없거나 아직 읽히지 않았으면 0)Length of the loaded asset. (seconds, 0 if there is no asset or it is not loaded yet)
짝Counterpart: vanilla.js AudioPlayer.getDuration