In my last post I made a scrolling turkey but quickly realized, it needed to gobble.
I started by getting a few audio files of turkeys gobbling. I assembled the samples into a single file so that I could load them with a single http request but play them as audio sprites. I used OcenAudio to do it manually but I probably should have used the NPM package audiosprite which uses FFMPEG to automatically create audio sprites with associated metadata.
I grabbed the Sprite class from this repo and in its simplicity it works wonders!
An instance of the class loads the audio file as an audio buffer. You pass in an options object with a path to the audio file in src, and a sprite object with named sprite keys with start times and duration in milliseconds. I just plugged in my audio file and the metadata for the three gobbles named a, b, and c:
I then add an event listener after the page loads to handle when someone clicks the turkey: