useSound 🔊

a custom React hook for sound effects, music, and all things web audio

here's a link to the repo if you want to play with the instrument code

a little bit about me...
me playing music on the computerableton generative music

I like making generative music - writing rules & procedures to determine musical elements, instead of writing them explicitly - in Ableton Live, a powerful and creative digital audio workstation.

In Ableton Live, I can use "MIDI Devices" to modify a static, one-note input MIDI signal into melodies, rhythmic patterns, chords, arpeggios, and more. These MIDI devices control musical elements like pitch, rhythm, scale, velocity, note length, delay, and chance, with a musician-friendly UI more similar to a physical effects pedal than a scripting terminal.

Check out the generative music I make at masonlynass.bandcamp.com

useSound is a custom React Hook developed by Josh Comeau.

useSound docs

Josh says "I defer most of the hard audio work to Howler".

Howler

import useSound and audio assets

imports for useSound

declare variables with array destructuring

variables for useSound

In this example, [soundHorn] is a variable which plays a goal horn we've passed down as a prop - then that variable is accessed inside an onClick event handler to play the goal horn.

IceHonky code bit

In my sumo website, sound variables are defined on the App level, then passed down as props to various components that use them. When a user submits a team they drafted, tachiai() plays the [tachiai] sound.

FS code bitFS code bitFS code bit

check it out for yourself at icehonky.net & fantasysumo.net

code for this component

In the second element of the array, you can use "stop" or "pause" functions to stop the audio.

click the record to start & pause the music:

Amen Break - from "Amen, Brother" by the Winstons

Instead of importing sounds individually, you can use "sprites" to extract multiple samples from the same audio file

sprites for drum machine

8 voice Drum Machine

click on the buttons to hear each sound

then use "1, 2, 3, 4, q, w, e, & r" keys on the keyboard to finger drum!

clap

rack tom

floor tom

hi hat

kick

snare

ride

crash

Random note generator

When calling useSound, you can pass options like "playbackRate," "volume," "interrupt," and "sprite" as a second argument:

sprites codevariables code
sprites codesprites code
sprites code