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
resumeContextandsuspendContextrather than callingresume()andsuspend()on it yourself, so the hub dispatches itscontext_resumedandcontext_suspendedevents. destroycloses the context. Do not keep using it after that.
See also
getMasterInput: where to connect audio you generate yourself.getMasterOutput: where to tap the mix for an analyser.resumeContext: wake the context up.