BALANCE, RMS, GAIN

Balance, Rms and Gain are used to trace and modify the rms value of a signal or signals. Rms traces the rms of a signal. Gain modifies a signal so that it has a specified rms power. Balance is a combination of the two which modifies a signal so that it has rms power equal to that of a compare signal. Balance is useful for restoring power to a signal after it has passed through a filter.
  make-balance &optional (hp 10)
  make-rms &optional (hp 10)
  make-gain &optional (hp 10)

  balance b signal compare
  rms r signal
  gain g signal rms
hp is the half-power point (in cps) of a special internal low-pass filter. Balance and gain return a modified signal, rms returns the rms of the signal.

Extreme cases will cause spikes in the audio - if this occurs try using a gain multiplier to approximate the target rms before applying balance or gain to it. It is a good idea to use a multiplier in any case because balance and gain work better the less they have to gain.

The average gain that was applied can be retrieved:
  balance-avg b
  gain-avg g

Use Run* and the average functions to suggest a user-supplied gain for the next time.

For Example:
(definstrument formnt-noise (start dur amp 
		             &key
                             (freq 440) (r .9) (g 1.0)
                             (postfilter-gain nil))

  (let* ((st (floor (* start *srate*)))
	 (nd (+ st (floor (* *srate* dur))))
	 (rd (make-rand))
	 (ft (make-formant freq r))
	 (postfilter-amp (* g (if postfilter-gain postfilter-gain 1.0)))
	 (bal (make-balance)))
    
    (run* (bal)
     (loop for i from st to nd do
       (let ((sig (* amp (rand rd))))
	 (outa i (balance bal (* postfilter-amp (formant ft sig)) sig)))))
    
    (if (not postfilter-gain) 
	(format t "~&formnt-noise: specify a :postfiler-gain of ~,5F for best results.~%" (balance-avg bal)))))