# misc/beepplayer (vanilla.js 0.4.0)

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

Procedural beep player.

## BeepPlayer extends Object

Procedural beep player.
- Synthesizes UI sound effects with OscillatorNode + GainNode, without audio files.
  Schedules with an attack / sustain / release envelope to avoid clicks.
- Provides click / confirm / cancel / error / success / warning / notification presets and
  arbitrary tone sequence playback (playSequence).
- Usage:
    const beepPlayer = new BeepPlayer(engine.getAudioManager());
    beepPlayer.playPreset("confirm");

Example: Play a preset sound effect on every press

```js
import { Engine, EngineConfiguration, Scene, BeepPlayer } from "./build/vanilla.js";

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
const audioManager = engine.getAudioManager();
const beepPlayer = new BeepPlayer(audioManager, 0.3);

class MainScene extends Scene {
	touchPress(viewInputPosition) {
		super.touchPress(viewInputPosition);
		beepPlayer.playPreset("confirm");
	}
}

engine.run(new MainScene());
```

- `constructor(audioManager, volume = 0.35)` (constructor, since 0.3.0): Constructor.
  - `audioManager` AudioManager: Audio manager that plays the sound. (engine.getAudioManager())
  - `volume` number: 0 ~ 1.
- `playTone(frequency, durationSeconds = 0.08, waveform = "sine", delaySeconds = 0, volumeScale = 1)` (method, since 0.3.0): Plays a single tone.
  - `frequency` number: Frequency. (Hz)
  - `durationSeconds` number: Duration. (seconds)
  - `waveform` string: "sine" | "square" | "sawtooth" | "triangle"
  - `delaySeconds` number: Start delay. (seconds)
  - `volumeScale` number: Volume multiplier for this tone only.
- `playSequence(toneList)` (method, since 0.3.0): Plays a tone sequence.
  - Plays [{ frequency, duration, waveform?, volumeScale? }, ...] one after another.
  - `toneList` object[]: List of tones to play in sequence. ({ frequency, duration, waveform?, volumeScale? }, duration in seconds)

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

  ```js
  import { Engine, EngineConfiguration, Scene, BeepPlayer } from "./build/vanilla.js";
  
  const engineConfiguration = new EngineConfiguration();
  const engine = new Engine(engineConfiguration);
  const audioManager = engine.getAudioManager();
  const beepPlayer = new BeepPlayer(audioManager);
  
  class MainScene extends Scene {
  	touchPress(viewInputPosition) {
  		super.touchPress(viewInputPosition);
  		beepPlayer.playSequence([
  			{ frequency: 523, duration: 0.1 },
  			{ frequency: 659, duration: 0.1, waveform: "triangle" },
  			{ frequency: 784, duration: 0.2, volumeScale: 0.6 },
  		]);
  	}
  }
  
  engine.run(new MainScene());
  ```

- `playPreset(presetName)` (method, since 0.3.0): Plays a preset.
  - `presetName` string: "click" | "confirm" | "cancel" | "error" | "success" | "warning" | "notification"
- `setVolume(volume)` (method, since 0.3.0): Sets the default volume.
  - `volume` number: 0 ~ 1.
- `getVolume()` (method, since 0.3.0): Returns the default volume.
  - returns number: Base volume. (0 ~ 1)
- `setMuted(isMuted)` (method, since 0.3.0): Sets mute.
  - `isMuted` boolean: If true, plays no sound.
- `isMuted()` (method, since 0.3.0): Whether muted.
  - returns boolean: True if muted.
