;Written by Iain McCurdy, 2006

<CsoundSynthesizer>

<CsOptions>
-idevaudio -odevaudio -b400
</CsOptions>

<CsInstruments>

sr 	= 	44100	;SAMPLE RATE
ksmps 	= 	1	;NUMBER OF AUDIO SAMPLES IN EACH CONTROL CYCLE
nchnls 	= 	2	;NUMBER OF CHANNELS (2=STEREO)
0dbfs	=	1	;MAXIMUM AMPLITUDE REGARDLESS OF BIT DEPTH

;FLTK INTERFACE CODE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FLcolor	255, 255, 255, 0, 0, 0
;		OPCODE | LABEL         | WIDTH | HEIGHT | X | Y
		FLpanel	"follow2 gate",   500,    250,    0,  0

;HANDLE		OPCODE	"LABEL" | WIDTH | HEIGHT | POS_X | POS_Y
idgain		FLvalue	" ",       100,     18,     0,       25
idatt		FLvalue	" ",       100,     18,     0,       75
idrel		FLvalue	" ",       100,     18,     0,      125
idthresh 	FLvalue	" ",       100,     18,     0,      175

;SLIDERS							MIN  |  MAX | EXP | TYPE |  DISP   | WIDTH | HEIGHT | X  |  Y
gkgain, ihgain		FLslider	"Input Gain Control",	0,        5,   0,    23,   idgain,     490,     25,    5,     0
gkatt, ihatt		FLslider	"Attack Time", 		0.0001,   2,   0,    23,   idatt,      490,     25,    5,    50
gkrel, ihrel		FLslider	"Release Time",		0.0001,   2,   0,    23,   idrel,      490,     25,    5,   100
gkthresh, ihthresh	FLslider	"Gate Threshold",	0.0001,   1,  -1,    23,   idthresh,   490,     25,    5,   150
gkmeter, gihmeter	FLslider 	"Meter",		0.0001,   1,  -1,     1,   -1,         490,     25,    5,   200
FLsetColor2	0, 255, 0, gihmeter	;COLOURIZE METERS (GREEN)

;SET INITIAL VALUES FOR VALUATORS	VALUE | HANDLE
		FLsetVal_i	   	1,      ihgain
		FLsetVal_i	   	.0001,  ihatt
		FLsetVal_i	   	.03,    ihrel
		FLsetVal_i	   	0.1,   ihthresh

		FLpanel_end	;END OF PANEL CONTENTS

;INSTRUCTIONS AND INFO PANEL                 WIDTH | HEIGHT | X | Y
				FLpanel	" ", 500,     260,   512, 0
;TEXT BOXES												TYPE | FONT | SIZE | WIDTH | HEIGHT | X | Y
ih		 	FLbox  	"               follow2 - simple audio gate                   ", 	1,      5,     14,    490,    20,     5,  0
ih		 	FLbox  	"-------------------------------------------------------------", 	1,      5,     14,    490,    20,     5,  20
ih		 	FLbox  	"In this example follow2 is used to implement a simple audio  ", 	1,      5,     14,    490,    20,     5,  40
ih		 	FLbox  	"gate. An amplitude following signal is created and then      ", 	1,      5,     14,    490,    20,     5,  60
ih		 	FLbox  	"continuously compared with a threshold value (defined by the ", 	1,      5,     14,    490,    20,     5,  80
ih		 	FLbox  	"'Threshold' slider). If signal amplitude is above the        ", 	1,      5,     14,    490,    20,     5, 100
ih		 	FLbox  	"threshold value then the signal passes through the gate      ", 	1,      5,     14,    490,    20,     5, 120
ih		 	FLbox  	"unaffected, if signal amplitude is below the threshold value ", 	1,      5,     14,    490,    20,     5, 140
ih		 	FLbox  	"then the gate closes and no signal passes through.           ", 	1,      5,     14,    490,    20,     5, 160
ih		 	FLbox  	"An amplitude meter is included to give visual confirmation of", 	1,      5,     14,    490,    20,     5, 180
ih		 	FLbox  	"the gates functioning.                                       ", 	1,      5,     14,    490,    20,     5, 200
ih		 	FLbox  	"The left channel input only is scanned by the gate but the   ", 	1,      5,     14,    490,    20,     5, 220
ih		 	FLbox  	"gate is subsequently applied to both channels.               ", 	1,      5,     14,    490,    20,     5, 240

				FLpanel_end

				FLrun	;RUN THE FLTK WIDGET THREAD
;END OF FLTK INTERFACE CODE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

instr 	1
	aL, aR 		ins			;READ STEREO INPUT
	aL		=		aL * gkgain		;AUDIO SIGNALS ARE SCALED BY THE FLTK GAIN CONTROL
	aR		=		aR * gkgain		;AUDIO SIGNALS ARE SCALED BY THE FLTK GAIN CONTROL
	afollow 	follow2 	aL, gkatt, gkrel	;CREATE A AMPLITUDE FOLLOWING UNIPOLAR SIGNAL
	kfollow		downsamp	afollow			;DOWNSAMPLE TO CREATE A K-RATE VERSION OF THE AMPLITUDE FOLLOWING SIGNAL
	kmeter		portk		kfollow, .05		;SMOOTH THE MOVEMENT OF THE AMPLITUDE FOLLOWING SIGNAL - THIS WILL MAKE THE METERS EASIER TO VIEW
	ktrig		changed		kmeter			;IF THE AMPLIUDE FOLLOWING SIGNAL CHANGES GENERATE A TRIGGER SIGNAL (A MOMENTARY '1' VALUE)
			FLsetVal	ktrig, kmeter, gihmeter	;UPDATE AMPLITUDE METER
	kgate		=		(kfollow<gkthresh?0:1)	;CREATE A GATE SIGNAL (EITHER 1 OR ZERO DEPENDING UPON COMPARISON WITH THE THRESHOLD VALUE
	kgate		port		kgate,.01		;SMOOTH THE OPENING AND CLOSING OF THE GATE TO PREVENT CLICKS
			outs		aL*kgate, aR*kgate	;SEND AUDIO TO THE OUTPUTS AND APPLY THE GATING SIGNAL
endin
</CsInstruments>

<CsScore>
i 1 0 3600	;INSTRUMENT 1 IS ACTIVE FOR 1 HOUR
</CsScore>

</CsoundSynthesizer>