;nyquist plug-in
;version 3
;type process
;name "AmpFade..."
;action "Amplifying..."
;info "By Steve Daulton (http://easyspacepro.com).\nReleased under GPL v2.\n"

;; AmpFade.ny by Steve Daulton. May 2011.
;; Released under terms of the GNU General Public License version 2:
;; http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
;; version 1.0 May 2011
;; Note: For logarithmic fade, 'silence' is set at -96 dB

;control type "Fade type" choice "Linear taper,Log taper (dB),Round Curve,Cosine ('S' curve)" 0 
;control preset "Preset fades" choice "Manual Settings,Fade In, Fade Out" 0 
;control iamp "Initial amplification" real "dB" 0 -60 60
;control famp "Final amplification" real "dB" -0 -60 60

(if (= type 1)
	(progn ; dB scale
		(setq silence -96)
		(setq iamp 
			(case preset
				(0 iamp); linear amplification
				(1 silence); fade in
				(2 0))); fade out
		(setq famp 
			(case preset
				(0 famp); linear amplification
				(1 0); fade in
				(2 silence)))); fade out
	(progn ; linear scale
		(setq iamp 
			(case preset
				(0 (db-to-linear iamp))
				(1 0.0)
				(2 1.0)))
		(setq famp 
			(case preset
				(0 (db-to-linear famp))
				(1 1.0)
				(2 0.0)))))

		
(defun fade (s-in)
	(control-srate-abs *sound-srate*
		(case type
			(0 (setf env ; linear amplify envelope
				(pwlv iamp 1 famp)))
			(1 (setf env ; log amplify envelope
				(db-to-linear (pwlv iamp 1 famp))))
			(2 (setf env ; round curve envelope
				(if (> famp iamp)
					(sum iamp (mult (- famp iamp)
						(s-sqrt (osc (hz-to-step (/ (get-duration 4)))))))
					(if (> iamp famp)
						(sum famp (mult (- iamp famp)
						 (s-sqrt (osc (hz-to-step (/ (get-duration 4))) 1 *table* 90))))
					(const iamp)))))
			(3 (setf env ; cosine envelope
				(sum famp (mult 0.5 (- iamp famp)
					(sum 1
						(osc (hz-to-step (/ (get-duration 2))) 1 *table* 90))))))))					
	(setf s-in (mult s-in env)))

(fade s); fade linked stereo or mono



