Friday 26 February 2021

Update on the concept chapter: Music

In my last post 2 weeks ago, I talked about making a test chapter for the multimedia book project. I anticipated being done a week ago, but sorting out music took me much longer than I thought.

The audio library I'd planned on using turned out to be not very suitable for my purposes, and I ended up deciding to work with the Web Audio API directly.

But after all my fiddling around, the audio system for my custom engine is basically complete. There are some little details left to work out, but the test chapter plays through, and it sounds beautiful.

My main frustration with other audio queueing systems I've used is transitioning to the next track when the current one is playing in a loop. The two typically available options are:

1. start the next track immediately, interrupting the current track

2. wait for the end of the current loop, then start the next track

Option 1 allows us to be very responsive, and update our music immediately for events in our game, but it interrupts the musical experience. Option 2 preserves the flow of the music, but introduces a delay in our responsiveness - and possibly quite a long one, depending on the length of our loop.

Neither of these options appealed to me. I wanted to do something more like

3. wait for the end of the current musical phrase, then start the next track

Of course, this necessitates having some different data.

I decided to save each phrase as a separate audio file, and sequence them dynamically.

Scheduling was my biggest roadblock with that approach. I spent a lot of hours banging my head against the wall trying to figure out how to play the next phrase exactly at the end of the current one, with a seamless transition. Finally, just a few days ago, I found a wonderful article by Chris Wilson about using javascript timers to schedule sounds on the Web Audio clock.

After that, I was able to get my "phrase sequencer" working, and I spent my morning today composing music instead of struggling futilely to get sounds to line up properly.

The concept chapter is now very close to done. What's left is mostly engine tweaks.

After a week and a half of frustration, I feel like this is really coming together.

No comments:

Post a Comment