SPB Git

Repository Public

sound.ts 3.5 KB 104 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : UQO Éval — Le Grand Défi Immobilier8 * File             : apps/web/src/lib/sound.ts9 * Module           : lib10 * Repository       : https://git.spboucher.ai/uqo-eval-jeux11 * Project Website  : https://jeux.uqo-eval.app12 * Created          : 2026-11-2513 * Last Modified    : 2026-11-2514 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243/** Retours sonores synthétisés (aucun fichier audio). Désactivés par défaut. */44import { getPrefs } from "./store";4546let context: AudioContext | null = null;4748function tone(frequency: number, start: number, duration: number, gain: number, type: OscillatorType = "sine"): void {49  if (!context) return;50  const osc = context.createOscillator();51  const amp = context.createGain();52  osc.type = type;53  osc.frequency.value = frequency;54  const t0 = context.currentTime + start;55  amp.gain.setValueAtTime(0, t0);56  amp.gain.linearRampToValueAtTime(gain, t0 + 0.015);57  amp.gain.exponentialRampToValueAtTime(0.0001, t0 + duration);58  osc.connect(amp).connect(context.destination);59  osc.start(t0);60  osc.stop(t0 + duration + 0.02);61}6263export type Cue = "select" | "correct" | "wrong" | "door" | "milestone" | "finale";6465export function play(cue: Cue): void {66  if (!getPrefs().sound) return;67  try {68    context ??= new AudioContext();69    if (context.state === "suspended") void context.resume();70    switch (cue) {71      case "select":72        tone(660, 0, 0.07, 0.05, "triangle");73        break;74      case "correct":75        tone(523.25, 0, 0.16, 0.08);76        tone(783.99, 0.09, 0.26, 0.08);77        break;78      case "wrong":79        tone(196, 0, 0.22, 0.07, "triangle");80        break;81      case "door":82        tone(140, 0, 0.18, 0.04, "sine");83        break;84      case "milestone":85        [523.25, 659.25, 783.99].forEach((f, i) => tone(f, i * 0.09, 0.3, 0.07));86        break;87      case "finale":88        [392, 523.25, 659.25, 783.99, 1046.5].forEach((f, i) => tone(f, i * 0.11, 0.5, 0.07));89        break;90    }91  } catch {92    /* audio indisponible */93  }94}9596/** Retour haptique discret, là où il est pris en charge. */97export function haptic(pattern: number | number[]): void {98  try {99    navigator.vibrate?.(pattern);100  } catch {101    /* non pris en charge */102  }103}104
Files main
Menu

Theme