Search code examples
audiowebkithtml5-audioweb-audio-api

Adding master gain in to an Audio Graph


I have a simple implementation of an audio mixer using the web audio API. I'm basically creating a buffer for each source, adding some filters and then connecting to the destination:

this.track.connect(this.highPassFilter);
this.highPassFilter.connect(this.lowShelfFilter)
this.lowShelfFilter.connect(this.highShelfFilter)
this.highShelfFilter.connect(this.midFilter);
this.midFilter.connect(this.panner)
this.panner.connect(this.gain)
this.gain.connect(this.ctx.destination)

What I want to do is be able to create a master gain and master compressor. Seeing as all my 'tracks' are connected directly to the context.destination and then all essentially played in unison when the play button is pressed, how can I connect a master gain in to the chain?


Solution

  • I think you already know how to do this. What you need to do is to create your master gain and compressor, connect those two together and then connect all your tracks to them, like so:

    [track] -> masterGain -> masterCompressor -> ctx.destination
    

    Or the reverse, however you want it (masterCompressor -> masterGain -> ctx.destination).