# misc/beepplayer (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/misc/beepplayer/

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:
  ```text
    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.

Example: Play a confirm sound with the engine's audio manager

```rust
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");
}
```

## BeepTone (struct) (Clone, Copy, Debug, PartialEq)

One tone of a sequence. (the original { frequency, duration, waveform?, volume_scale? })

Example: A default tone and a tone with a set waveform

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

- `pub frequency: f64` (field, since 0.3.2): Frequency. (Hz)
  - returns f64
- `pub duration: f64` (field, since 0.3.2): Duration. (seconds)
  - returns f64
- `pub waveform: BeepWaveform` (field, since 0.3.2): Waveform.
  - returns BeepWaveform
- `pub volume_scale: f64` (field, since 0.3.2): Volume multiplier for this tone only.
  - returns f64
- `pub const fn create(frequency: f64, duration: f64) -> BeepTone` (static, since 0.3.2): Constructor. (sine wave, multiplier 1. The original form with waveform and volume_scale left out)
  - `frequency` f64: Frequency. (Hz)
  - `duration` f64: Duration. (seconds)
  - returns BeepTone: 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` (static, since 0.3.2): Creates with the waveform and multiplier specified too.
  - `frequency` f64: Frequency. (Hz)
  - `duration` f64: Duration. (seconds)
  - `waveform` BeepWaveform: Waveform.
  - `volume_scale` f64: Volume multiplier for this tone only. (multiplied by the player volume)
  - returns BeepTone: A tone holding the given values as is.

## BeepPlayer (struct)

Procedural beep player.

Example: Play a preset sound effect on every press

```rust
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();
```

Example: Volume and mute (while muted, the sound device is not touched)

```rust
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");
```

- `pub const DEFAULT_VOLUME: f64 = 0.35` (static, since 0.3.2): Default volume of the original constructor.
  - returns f64
- `pub fn new(audio_manager: &AudioManager, volume: f64) -> BeepPlayer` (constructor, since 0.3.2): Constructor. (volume: 0 ~ 1. Holds the manager by reference like the original)
  - `audio_manager` &AudioManager: Audio manager that plays the sound. (&engine.get_audio_manager(), shares its state)
  - `volume` f64: Base volume. (0 ~ 1, not clamped here and kept as is, the original default is DEFAULT_VOLUME)
  - returns BeepPlayer: A new player with mute off.
- `pub fn play_tone(&self, frequency: f64, duration_seconds: f64, waveform: BeepWaveform, delay_seconds: f64, volume_scale: f64)` (method, since 0.3.2): 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)
  - `frequency` f64: Frequency. (Hz)
  - `duration_seconds` f64: Duration. (seconds)
  - `waveform` BeepWaveform: Waveform.
  - `delay_seconds` f64: Start delay. (seconds, from now)
  - `volume_scale` f64: Volume multiplier for this tone only. (multiplied by the base volume, 0.0001 if the product is less than 0.0001)

  Example: One low square wave after 0.2 seconds

  ```rust
  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);
  }
  ```

- `pub fn play_sequence(&self, tone_list: &[BeepTone])` (method, since 0.3.2): 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)

  Example: Play the three notes C, E, G in sequence

  ```rust
  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)]);
  }
  ```

- `pub fn play_preset(&self, preset_name: &str)` (method, since 0.3.2): Plays a preset. ("click" | "confirm" | "cancel" | "error" | "success" | "warning" | "notification", click for an unknown name)
  - `preset_name` &str: Preset name. (case-sensitive)
- `pub fn set_volume(&mut self, volume: f64)` (method, since 0.3.2): Sets the base volume. (0 ~ 1)
  - `volume` f64: Base volume. (clamped to 0 ~ 1)
- `pub fn get_volume(&self) -> f64` (method, since 0.3.2): Returns the default volume.
  - returns f64: Base volume. (0 ~ 1 if it went through set_volume)
- `pub fn set_muted(&mut self, is_muted: bool)` (method, since 0.3.2): Sets mute.
  - `is_muted` bool: If true, plays no sound.
- `pub fn is_muted(&self) -> bool` (method, since 0.3.2): Whether muted.
  - returns bool: true if muted.

## BeepWaveform (enum) (Clone, Copy, Debug, PartialEq, Eq)

Waveform. (the original OscillatorNode.type string)

- `Sine`: Sine wave. (original "sine")
- `Square`: Square wave. (original "square")
- `Sawtooth`: Sawtooth wave. (original "sawtooth")
- `Triangle`: Triangle wave. (original "triangle")
