Search code examples
audiovolumepitch

How to preserve the volume of a dynamic signal that varies in pitch?


I'm creating some audio dynamically (with WebAudio), by starting with basic oscillators (sine/square/etc) and applying various modulations. Naturally, as I change the pitch of the signals the apparent volume changes, with higher pitches sounding louder and lower pitches quieter.

My question is, is there some standard way of processing the signal to keep a (vaguely) constant volume as its pitch changes? Of course I could just add a gain node and twiddle it up and down in some ad-hoc way, but I'm guessing there's a standard thing to do here (which I've looked for but can't find).

Thanks!


Solution

  • Since there's apparently no standard approach to this, here's the "solution" I hacked out:

    function getVolumeAdjustment(freq) {
        var x = Math.log(freq)
        var db = 3.4 * x * x - 49.3 * x + 217
        return db - 45
    }
    

    db vaguely approximates the 40-phon Equal Loudness contour, at least as I eyeballed it from the picture on wikipedia. I then subtract 45 so that I have a volume adjustment (ranging from -6 to 15 or so) to be applied to each given tone based on its frequency.

    It seems to basically work - still happy to hear any better answers though.