playChime (lib/chime.ts)

atlas · src/lib/chime.ts · typescript · Apache-2.0
voice, tag:knn-classified

Function that plays a short synthesized chime sound in browsers without external assets.

// Source: https://github.com/pacifio/atlas | License: Apache-2.0 | Copyright You must retain, in the Source form of any Derivative Works
// Provided via justclaw.it.com; keep this notice with the code.
/**
 * A short two-tone chime, synthesised — no bundled asset, no network.
 *
 * WKWebView may refuse to start an AudioContext before a user gesture has
 * happened in the page; `resume()` is attempted and any failure is swallowed,
 * so a chime that cannot play costs nothing.
 */
let ctx: AudioContext | null = null;

export function playChime(): void {
  try {
    ctx ??= new AudioContext();
    const ac = ctx;
    void ac.resume().catch(() => {});
    const now = ac.currentTime;
    const tone = (freq: number, at: number) => {
      const osc = ac.createOscillator();
      const gain = ac

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key