Skip to main content

Howl

Added in 6.5.0

Howl from soundhub/howler has the constructor and the methods of the Howler.js class of the same name. Each Howl is one sound in the shared hub that Howler.hub hands out. See Migrating from Howler.js for the overview.

import { Howl } from 'soundhub/howler';

const sound = new Howl(options: HowlOptions);

HowlOptions​

OptionTypeDefaultDescription
srcstring | string[]requiredThe url, or a list the browser picks the first playable format from.
volumenumber1Volume from 0 to 1.
loopbooleanfalseStart over at the end.
ratenumber1Playback rate.
mutebooleanfalseStart muted.
spriteRecord<string, [offset, duration, loop?]>noneNamed ranges, in milliseconds. The third value makes one sprite loop.
autoplaybooleanfalsePlay as soon as the file has loaded.
preloadboolean | 'metadata'truefalse waits for load(). 'metadata' counts as true.
html5booleanfalseStream the file with loadStream, for long files. One voice at a time, no sprites.
onload ... onunlockfunctionsnoneThe callbacks, see Events.

Voice ids​

play() returns a number, the voice id, as in Howler. Every call makes a new voice, so the same Howl can play over itself. Pass the id as the last argument to reach one voice, or leave it out to reach every voice of the Howl.

const laser = new Howl({ src: '/audio/laser.mp3' });

const a = laser.play();
const b = laser.play();

laser.volume(0.3, a); // only the first
laser.stop(); // both

play() returns the id before the file has loaded as well. The voice starts once the file is in.

Methods​

Playback​

MethodReturnsWhat it does
play(sprite?: string | number)numberPlay the whole file, a sprite by name, or resume a paused voice by its id.
pause(id?)thisPause one voice or all of them.
stop(id?)thisStop one voice or all of them.
playing(id?)booleanWhether that voice, or any voice, plays.
duration(id?)numberSeconds. For a sprite voice the sprite's length, otherwise the file's. 0 before the file has loaded.
seek() / seek(id)numberThe position in seconds, of that voice or the first one.
seek(seconds, id?)thisJump to a position in seconds.

Properties​

Each of these is a getter without a value and a setter with one.

MethodUnitWhat it does
volume(volume?, id?)0 to 1Volume of one voice, or of the Howl and every voice.
fade(from, to, duration, id?)0 to 1, millisecondsFade between two volumes. onfade fires at the end.
mute(muted?, id?)booleanMute or unmute.
rate(rate?, id?)factorPlayback rate.
loop(loop?, id?)booleanLoop or not.
stereo(pan?, id?)-1 to 1Stereo pan, left to right.
pos(x, y, z, id?)units in the scenePlace the voice in 3D. It switches that voice to spatial panning.

volume, fade, mute, rate and loop without an id also set the value for voices that start later. With an id they only reach that voice. stereo and pos only reach voices that have started.

Loading​

MethodReturnsWhat it does
load()thisStart loading. Only needed with preload: false.
state()'unloaded' | 'loading' | 'loaded'Where loading stands.
unload()nullStop every voice, remove the listeners and unload the sound from the hub.

soundhubId​

howl.soundhubId is a read-only string, the id of this Howl in the shared hub, such as 'howl1'. Use it with Howler.hub for anything Howler has no method for.

Events​

Listen with the on* options, or with on, once and off:

sound.on('end', (id) => console.log('voice finished', id));
sound.once('load', () => sound.play());
sound.off('end'); // every end listener
sound.off(); // every listener
sound.on('end', fn, id); // only for one voice
EventFires whenArguments
loadthe file has loadednone
loaderrorloading failednull, the error
playerrora voice could not startvoice id, the error
playa voice starts or resumesvoice id
enda voice reaches its end, or finishes one pass of a loopvoice id
pausea voice pausesvoice id
stopa voice stopsvoice id
mutemute() sets a valuevoice id
volumevolume() sets a valuevoice id
raterate() sets a valuevoice id
seekseek() jumpsvoice id
fadea fade() has run its durationvoice id
unlockthe browser has released audio after the first touchnone

Example​

import { Howl } from 'soundhub/howler';

const sfx = new Howl({
src: ['/audio/sfx.webm', '/audio/sfx.mp3'],
sprite: {
laser: [0, 400],
coin: [500, 300],
engine: [1000, 2000, true], // loops
},
volume: 0.8,
onload: () => console.log('ready'),
});

const engine = sfx.play('engine');
sfx.rate(1.2, engine);
sfx.fade(0.8, 0, 1000, engine); // one second

sfx.play('coin');

Good to know​

  • A listener error is caught and logged, so the other listeners still run.
  • Setting volume, rate or mute for one voice before its file has loaded does not stick. Set them without an id, or after onplay.
  • mute, volume, rate and seek fire their event right away, in the same call.

See also​