joriszwart.nl

I code dreams™

CCC - A code pattern for Web Audio

Web Audio Code pattern CCC TLA Mnemonic

The CCC-pattern for Web Audio

In software unit testing we have the AAA-pattern (Arrange, Act, Assert).

Now I want to introduce the CCC-pattern for Web Audio.

  1. Create
  2. Configure
  3. Connect
  4. Command

An example

const ctx = new AudioContext()

// create
const tone = ctx.createOscillator()
const gain = ctx.createGain()

// configure
tone.frequency.value = 432
gain.gain.value = 1

// connect
tone.connect(gain)
    .connect(ctx.destination)

// command
tone.start()

Also note the method chaining for connecting audio nodes. Supported in modern browsers.

What can be improved?

A functional approach can prevent leakage between the three phases.

See developers.mews.com/aaa-pattern-a-functional-approach.

Next up?

The Arrange-Act-Animate (AAA) pattern for animations.