Skip to content
Docs

The sonido service

Reference for the sonido service. Synthesized sound: presets, tones, noise and note sequences.

DescribesFormato 1, API 1 For those who write code This page is generated from the SDK sources.

On this page

Synthesized sound: presets, tones, noise and note sequences.

The types used in the signatures (Vec3, Texto, the selector) are in the API reference.

Functions and properties of sonido#

sonido.tocar#

ts
sonido.tocar(nombre: SonidoPredefinido, opciones?: { volumen?: number; tono?: number; para?: Jugador }): void

Plays a preset sound.

From docs/EJEMPLOS.md, «sonido»:

ts
mundo.alTocar('#moneda', (_m, j) => sonido.tocar('moneda', { para: j }));

sonido.tono#

ts
sonido.tono(def: { frecuencia: number; duracion: number; onda?: 'seno' | 'cuadrada' | 'sierra' | 'triangulo'; hasta?: number; volumen?: number; para?: Jugador }): void

Synthesizes a tone.

From docs/EJEMPLOS.md, «sonido»:

ts
mundo.alTocar('#laser', () => sonido.tono({ frecuencia: 900, hasta: 120, duracion: 0.3, onda: 'sierra' }));

sonido.secuencia#

ts
sonido.secuencia(notas: { nota: string | number | null; pulsos?: number }[], opciones?: { pulsosPorMinuto?: number; repetir?: boolean; onda?; volumen?: number; para?: Jugador }): number

Plays a sequence of notes. Returns an id to stop it.

From docs/EJEMPLOS.md, «sonido»:

ts
const musica = sonido.secuencia(
  [{ nota: 'do4' }, { nota: 'mi4' }, { nota: 'sol4' }, { nota: null }, { nota: 'la4', pulsos: 2 }],
  { pulsosPorMinuto: 240, repetir: true, onda: 'triangulo', volumen: 0.3 }
);

sonido.detener#

ts
sonido.detener(id: number): void

Stops a sequence. Takes the id returned by sonido.secuencia.

From docs/EJEMPLOS.md, «sonido»:

ts
mundo.alEntrar('meta', () => sonido.detener(musica));

sonido.ruido#

ts
sonido.ruido(def: { duracion: number; volumen?: number; filtro?: number; hasta?: number; ataque?: number; para?: Jugador }): void

Noise (explosion, wind, rain, footsteps). filtro, in Hz, makes it deeper; hasta sweeps the filter.

From docs/EJEMPLOS.md, «mundo: luces, ambiente y zonas creadas por el script»:

ts
// Anochece en 20 segundos y se encienden los faroles (luces con "id" en la escena).
reloj.despues(30, () => {
  mundo.ambiente({ cielo: 'noche', luzAmbiente: 0.25, sol: { intensidad: 0.1 }, niebla: { desde: 10, hasta: 60 } }, 20);
  for (const luz of mundo.luces()) luz.encendida = true;
  sonido.ruido({ duracion: 3, filtro: 600, volumen: 0.4, ataque: 1 });
});

sonido.muestra#

ts
sonido.muestra(id: string, opciones?: OpcionesSonido): void

Plays a recorded sound from the game (sonidos/<id>.wav or .mp3). Added in version 1.2.

Behavior notes#

sonido.tono also accepts ataque and caida (seconds of the envelope). sonido.ruido is filtered noise: filtro, in Hz, makes it deeper and hasta sweeps the filter (an explosion: { duracion: 0.6, filtro: 2000, hasta: 100 }).

All sound is synthesized. Presets: moneda, salto, golpe, caida, control, victoria, derrota, clic, alerta, tic, luz, poder. Notes are written 'do4', 'fa#5', 'la3' (solfège names); null is a rest.

Complete examples#

Complete scripts from docs/EJEMPLOS.md. Each one assumes a scene containing what it names, and the text keys (interfaz.t('...')) must exist in textos of juego.json.

sonido#

scripts/principal.ts
import { juego, mundo, sonido } from '@greengames/sdk';

juego.alIniciar(() => {
  mundo.alTocar('#moneda', (_m, j) => sonido.tocar('moneda', { para: j }));
  mundo.alTocar('#laser', () => sonido.tono({ frecuencia: 900, hasta: 120, duracion: 0.3, onda: 'sierra' }));

  const musica = sonido.secuencia(
    [{ nota: 'do4' }, { nota: 'mi4' }, { nota: 'sol4' }, { nota: null }, { nota: 'la4', pulsos: 2 }],
    { pulsosPorMinuto: 240, repetir: true, onda: 'triangulo', volumen: 0.3 }
  );
  mundo.alEntrar('meta', () => sonido.detener(musica));
});

Next#