misc/beepplayer
절차적 비프 재생기.
- 원본 misc/beepplayer.js. 오디오 파일 없이 UI 효과음을 합성한다. attack / sustain / release 엔벨로프로 클릭음이 나지 않게 한다.
- click / confirm / cancel / error / success / warning / notification 프리셋과 임의 톤 시퀀스 재생(play_sequence)을 제공한다.
- 사용:
let beep_player = BeepPlayer::new(&engine.get_audio_manager(), BeepPlayer::DEFAULT_VOLUME);
beep_player.play_preset("confirm");- 소리를 내는 곳:
웹: 원본처럼 OscillatorNode + GainNode 를 만들어 오디오 컨텍스트의 출력에 바로 잇는다.
데스크탑: 믹서에 같은 그래프(크롬과 같은 셈의 OscillatorNode → GainNode AudioParam 자동화 → 출력)를 짓는다. (platform/audio/oscillator.rs, mixer.rs)- 원본과 다른 점: 파형 이름은 문자열 대신 BeepWaveform, 시퀀스의 톤은 BeepTone 이다. 프리셋 이름은 원본처럼 문자열이다.
Procedural beep player.
- The original misc/beepplayer.js. Synthesizes UI sound effects without audio files. An attack / sustain / release envelope avoids clicks.
- Provides click / confirm / cancel / error / success / warning / notification presets and arbitrary tone sequence playback (play_sequence).
- Usage:
let beep_player = BeepPlayer::new(&engine.get_audio_manager(), BeepPlayer::DEFAULT_VOLUME);
beep_player.play_preset("confirm");- Where the sound is made:
Web: like the original, creates an OscillatorNode + GainNode and connects them straight to the audio context output.
Desktop: builds the same graph in the mixer (OscillatorNode → GainNode AudioParam automation → output, computed the same way as Chrome). (platform/audio/oscillator.rs, mixer.rs)- Differences from the original: Waveform names are BeepWaveform instead of strings, and sequence tones are BeepTone. Preset names are strings like the original.
use vanilla::{BeepPlayer, Engine};
fn play_confirm(engine: &Engine) {
let beep_player = BeepPlayer::new(&engine.get_audio_manager(), BeepPlayer::DEFAULT_VOLUME);
beep_player.play_preset("confirm");
}BeepTonestruct traits Clone, Copy, Debug, PartialEq
시퀀스의 톤 하나. (원본 { frequency, duration, waveform?, volume_scale? })
One tone of a sequence. (the original { frequency, duration, waveform?, volume_scale? })
use vanilla::{BeepTone, BeepWaveform};
let tone = BeepTone::create(440.0, 0.1);
assert_eq!(tone.waveform, BeepWaveform::Sine);
assert_eq!(tone.volume_scale, 1.0);
let buzz_tone = BeepTone::create_with_waveform(220.0, 0.14, BeepWaveform::Square, 0.7);
assert_eq!(buzz_tone.frequency, 220.0);
assert_eq!(buzz_tone.waveform, BeepWaveform::Square);생성. (사인파, 배율 1. 원본에서 waveform, volume_scale 을 비운 꼴)
Constructor. (sine wave, multiplier 1. The original form with waveform and volume_scale left out)
- frequencyf64
- 주파수. (Hz)Frequency. (Hz)
- durationf64
- 길이. (초)Duration. (seconds)
반환ReturnsBeepTone 사인파, 음량 배율 1 인 톤.A tone with a sine wave and volume multiplier 1.
pub const fn create_with_waveform(frequency: f64, duration: f64, waveform: BeepWaveform, volume_scale: f64) -> BeepTone#파형과 배율까지 정해 생성.
Creates with the waveform and multiplier specified too.
- frequencyf64
- 주파수. (Hz)Frequency. (Hz)
- durationf64
- 길이. (초)Duration. (seconds)
- waveformBeepWaveform
- 파형.Waveform.
- volume_scalef64
- 이 톤만의 음량 배율. (재생기 음량에 곱한다)Volume multiplier for this tone only. (multiplied by the player volume)
반환ReturnsBeepTone 받은 값을 그대로 든 톤.A tone holding the given values as is.
BeepPlayerstruct
절차적 비프 재생기.
Procedural beep player.
use vanilla::{BeepPlayer, Engine, EngineConfiguration, Scene, SceneBase, Vector2};
struct MainScene {
scene_base: SceneBase,
beep_player: Option<BeepPlayer>,
}
impl Scene for MainScene {
fn get_scene_base(&self) -> &SceneBase {
&self.scene_base
}
fn get_scene_base_mut(&mut self) -> &mut SceneBase {
&mut self.scene_base
}
fn load(&mut self, engine: &Engine) {
self.beep_player = Some(BeepPlayer::new(&engine.get_audio_manager(), 0.3));
}
fn touch_press(&mut self, _view_input_position: Vector2) {
if let Some(beep_player) = &self.beep_player {
beep_player.play_preset("confirm");
}
}
}
let engine_configuration = EngineConfiguration::new();
let engine = Engine::new(engine_configuration);
let main_scene = MainScene {
scene_base: SceneBase::new(),
beep_player: None,
};
engine.run(Box::new(main_scene)).unwrap();use vanilla::{AudioManager, BeepPlayer};
let audio_manager = AudioManager::new();
let mut beep_player = BeepPlayer::new(&audio_manager, BeepPlayer::DEFAULT_VOLUME);
beep_player.set_volume(1.5);
assert_eq!(beep_player.get_volume(), 1.0);
beep_player.set_muted(true);
assert!(beep_player.is_muted());
beep_player.play_preset("success");생성. (volume: 0 ~ 1. 원본처럼 매니저를 참조로 든다)
Constructor. (volume: 0 ~ 1. Holds the manager by reference like the original)
- audio_manager&AudioManager
- 소리를 낼 오디오 매니저. (&engine.get_audio_manager(), 상태를 같이 든다)Audio manager that plays the sound. (&engine.get_audio_manager(), shares its state)
- volumef64
- 기본 음량. (0 ~ 1, 여기서는 자르지 않고 그대로 둔다, 원본 기본값 DEFAULT_VOLUME)Base volume. (0 ~ 1, not clamped here and kept as is, the original default is DEFAULT_VOLUME)
반환ReturnsBeepPlayer 음소거가 꺼진 새 재생기.A new player with mute off.
짝Counterpart: vanilla.js BeepPlayer.constructor
원본 생성자의 기본 음량.
Default volume of the original constructor.
반환Returnsf64
pub fn play_tone(&self, frequency: f64, duration_seconds: f64, waveform: BeepWaveform, delay_seconds: f64, volume_scale: f64)#단일 톤 재생.
- frequency: 주파수(Hz), duration_seconds: 길이(초), delay_seconds: 시작 지연(초), volume_scale: 이 톤만의 음량 배율.
- 컨텍스트가 없거나 돌고 있지 않으면 아무것도 하지 않는다.
- 음소거 중이면 컨텍스트를 깨우지도 않고 돌아간다. 아니면 먼저 컨텍스트를 재개한다. (데스크탑은 소리 장치를 이때 연다)
Plays a single tone.
- frequency: frequency (Hz), duration_seconds: length (seconds), delay_seconds: start delay (seconds), volume_scale: volume multiplier for this tone only.
- Does nothing if there is no context or it is not running.
- While muted, it returns without even waking the context. Otherwise it resumes the context first. (desktop opens the sound device at this point)
- frequencyf64
- 주파수. (Hz)Frequency. (Hz)
- duration_secondsf64
- 길이. (초)Duration. (seconds)
- waveformBeepWaveform
- 파형.Waveform.
- delay_secondsf64
- 시작 지연. (초, 지금부터)Start delay. (seconds, from now)
- volume_scalef64
- 이 톤만의 음량 배율. (기본 음량에 곱한다, 곱이 0.0001 보다 작으면 0.0001)Volume multiplier for this tone only. (multiplied by the base volume, 0.0001 if the product is less than 0.0001)
짝Counterpart: vanilla.js BeepPlayer.playTone
use vanilla::{BeepPlayer, BeepWaveform, Engine};
fn play_low_buzz(engine: &Engine) {
let beep_player = BeepPlayer::new(&engine.get_audio_manager(), BeepPlayer::DEFAULT_VOLUME);
beep_player.play_tone(220.0, 0.1, BeepWaveform::Square, 0.2, 0.7);
}톤 시퀀스 재생. (차례로 이어 튼다)
- 톤마다 앞 톤들의 길이 합만큼 늦춰 시작한다. (간격 없음)
Plays a tone sequence. (plays them one after another)
- Each tone starts delayed by the total length of the tones before it. (no gaps)
- tone_list&[BeepTone]
- 차례로 이어 틀 톤 목록. (길이는 초)List of tones to play one after another. (lengths in seconds)
짝Counterpart: vanilla.js BeepPlayer.playSequence
use vanilla::{BeepPlayer, BeepTone, Engine};
fn play_chord_arpeggio(engine: &Engine) {
let beep_player = BeepPlayer::new(&engine.get_audio_manager(), BeepPlayer::DEFAULT_VOLUME);
beep_player.play_sequence(&[BeepTone::create(523.0, 0.1), BeepTone::create(659.0, 0.1), BeepTone::create(784.0, 0.2)]);
}프리셋 재생. ("click" | "confirm" | "cancel" | "error" | "success" | "warning" | "notification", 모르는 이름은 click)
Plays a preset. ("click" | "confirm" | "cancel" | "error" | "success" | "warning" | "notification", click for an unknown name)
- preset_name&str
- 프리셋 이름. (대소문자를 가린다)Preset name. (case-sensitive)
짝Counterpart: vanilla.js BeepPlayer.playPreset
기본 음량 설정. (0 ~ 1)
Sets the base volume. (0 ~ 1)
- volumef64
- 기본 음량. (0 ~ 1 로 자른다)Base volume. (clamped to 0 ~ 1)
짝Counterpart: vanilla.js BeepPlayer.setVolume
기본 음량 반환.
Returns the default volume.
반환Returnsf64 기본 음량. (set_volume 을 거쳤으면 0 ~ 1)Base volume. (0 ~ 1 if it went through set_volume)
짝Counterpart: vanilla.js BeepPlayer.getVolume
음소거 설정.
Sets mute.
- is_mutedbool
- 참이면 소리를 내지 않는다.If true, plays no sound.
짝Counterpart: vanilla.js BeepPlayer.setMuted
음소거 여부.
Whether muted.
반환Returnsbool 음소거 중이면 true.true if muted.
짝Counterpart: vanilla.js BeepPlayer.isMuted
BeepWaveformenum traits Clone, Copy, Debug, PartialEq, Eq
파형. (원본 OscillatorNode.type 문자열)
Waveform. (the original OscillatorNode.type string)
| 이름Name | 설명Description |
|---|---|
Sine | 사인파. (원본 "sine")Sine wave. (original "sine") |
Square | 사각파. (원본 "square")Square wave. (original "square") |
Sawtooth | 톱니파. (원본 "sawtooth")Sawtooth wave. (original "sawtooth") |
Triangle | 삼각파. (원본 "triangle")Triangle wave. (original "triangle") |