Skip to main content

getContext

Get the AudioContext the hub created and plays everything through. Reach for it when you build your own Web Audio nodes, such as an oscillator or an analyser, and want them in the same graph.

getContext(): AudioContext;

Parameters​

This method takes no parameters.

Returns​

AudioContext: the hub's audio context. It is always the same object for the life of the hub.

Example​

import { SoundHub } from 'soundhub';

const soundHub = new SoundHub();
const context = soundHub.getContext();

const osc = context.createOscillator();
const gain = context.createGain();
gain.gain.value = 0.1;

osc.connect(gain);
gain.connect(soundHub.getMasterInput()); // through master volume, mute and limiter
osc.start();
osc.stop(context.currentTime + 0.5);

console.log(context.state, context.sampleRate);

Good to know​

  • The hub creates one context in its constructor, with latencyHint: 'interactive'. Nodes from another context cannot be connected to it.
  • Browsers start a context suspended until the user interacts with the page. Use resumeContext and suspendContext rather than calling resume() and suspend() on it yourself, so the hub dispatches its context_resumed and context_suspended events.
  • destroy closes the context. Do not keep using it after that.

See also​