0.21.0 • Published 5 years ago

virtual-webaudio v0.21.0

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

virtual-webaudio

A virtual representation of the web audio api elements with added diffing and patching capabilities.

Donate Build Status semantic-release Commitizen friendly JavaScript Style Guide

Installation

npm install virtual-webaudio

Note: The package doesn't contain a bundled version of the code, so you need to have webpack, rollup or browserify to bundle it in your own project.

Usage

The virtual-webaudio library offers you the VirtualAudioContext object, which can be used as a drop in replacement for the AudioContext object. Use it, like you would use the regular AudioContext object to build some sounds. The VirtualAudioContext will store all the created nodes and params in an internal storage. Use it only to create nodes and to set their params, but don't use it to modify values in the future. We will use another VirtualAudioContext for that and we'll let the diffing tool find the differences for us.

The VirtualAudioContext on it's own will not emit any sounds, we need to render it to a real AudioContext. To do this, we need to import the render method and pass it the virtual context and the real context. This will apply all the internally stored calls and methods on the real context.

If you have 2 virtual contexts, then you can diff them using the diff method of the library and apply it's returned value to the real context using the patch method.

Note: previously assembled virtual contexts can be re-used as many times as you want, since it contains relative timing. The timings get set at rendering/patching.

Example

import { VirtualAudioContext, diff, patch, render } from 'virtual-webaudio'

// create a basic, 300Hz sine wave with 0.5 gain
const create = () => {
  const ctx = new VirtualAudioContext()

  const osc = ctx.createOscillator()
  const gain = ctx.createGain()

  osc.frequency.value = 300

  gain.gain.value = 0.5

  osc.connect(gain)
  gain.connect(ctx.destination)

  osc.start()

  return ctx
}

// create a basic, 300Hz sine wave with 0 gain
const modify = () => {
  const ctx = new VirtualAudioContext()

  const osc = ctx.createOscillator()
  const gain = ctx.createGain()

  osc.frequency.value = 300

  gain.gain.value = 0

  osc.connect(gain)
  gain.connect(ctx.destination)

  osc.start()

  return ctx
}

// get the 2 virtual contexts and reserve variable for the real AudioContext
const a = create()
const b = modify()
let ctx

// AudioContext needs to be initialized through user interaction
const demo = () => {
  if(!ctx){
    ctx = new AudioContext()
  }

  // render the 1st virtual context
  render(a, ctx)

  // wait for a second
  setTimeout(() => {
    // find all the stuff, that have changed in b and apply the changes to the real context
    patch(diff(a, b), ctx)
  }, 1000)
}

// bind the demo to clicking on the webpage
document.body.addEventListener('click', demo)

Disabling diff for certain lines with __disableDiff

By default, the diff tool removes every event from the new virtual context, which is identical to what you had in the first virtual context. But sometimes you can't define some changes via additions, but by introducing unnecessary complexity to the code.

An example of an issue with this can be seen in poc/timed-sequencing, where a virtual context contains the instructions necessary to trigger a note. We have scheduled every step of the gain envelope, which would be played on every trigger call. Viewing the method calls, their parameters and their order of execution from a diffing tool's perspective shows us, that it will never change, so the diff tool will remove it.

To solve this and mark some method calls always run, we can call the virtual context's __disableDiff metho to mark subsequent calls always different from the previous ones. Calling __enableDiff will re-enable diffing.

const ctx = new VirtualAudioContext()

// ...

const gain = ctx.createGain()
gain.gain.value = 0

ctx.__disableDiff()
gain.gain.cancelAndHoldAtTime(ctx.currentTime)
gain.gain.linearRampToValueAtTime(1, ctx.currentTime + 0.1)
gain.gain.lineraRampToValueAtTime(0, ctx.currentTime + 1)
ctx.__enableDiff()

gain.connect(ctx.destination)

// ...

Demos

The repo contains some examples, which can be viewed by installing the repo locally and by executing npm run poc. This will create a local webserver, allowing you to open the demos via http://localhost:3000

0.21.0

5 years ago

0.20.0

5 years ago

0.19.0

5 years ago

0.18.0

5 years ago

0.17.0

5 years ago

0.16.0

5 years ago

0.15.0

5 years ago

0.14.0

5 years ago

0.13.0

5 years ago

0.12.0

5 years ago

0.11.0

5 years ago

0.10.0

5 years ago

0.9.0

6 years ago

0.8.5

6 years ago

0.8.4

6 years ago

0.8.3

6 years ago

0.8.2

6 years ago

0.8.1

6 years ago

0.8.0

6 years ago

0.7.1

6 years ago

0.7.0

6 years ago

0.6.0

6 years ago

0.5.0

6 years ago

0.4.0

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago