;Written by Iain McCurdy

<CsoundSynthesizer>

<CsOptions>
-odevaudio -b400
</CsOptions>

<CsInstruments>

sr			=	44100
kr			=	4410
ksmps			=	10
nchnls			=	2
0dbfs			=	1	;MAXIMUM AMPLITUDE REGARDLESS OF BIT DEPTH

;FLTK INTERFACE CODE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FLcolor	255, 255, 255, 0, 0, 0	;SET INTERFACE COLOURS
			FLpanel	"Randomly Moving Slider", 500, 300, 0, 0
;VALUE_DISPLAY_BOXES			 	WIDTH | HEIGHT | X | Y
idFreq				FLvalue	" ",     50,      18,    5, 175

;SLIDER					        				MIN | MAX | EXP | TYPE |  DISP   | WIDTH | HEIGHT | X   | Y
gkslider,gihslider	FLslider 	"",					7,     11,   0,    3,        -1,    490,     25,    5,    30                             
gkMin,	ihMin		FLslider 	"",					7,     11,   0,    3,        -1,    490,     15,    5,   100
gkMax,	ihMax		FLslider 	"Range",				7,     11,   0,    3,        -1,    490,     15,    5,   115
gkFreq,	ihFreq		FLslider 	"Frequency",				.001,  10,   0,   23,    idFreq,    490,     25,    5,   150
gki_h,	ihi_h		FLslider 	"Interpolating <-> Sample and Hold",	0,      1,   0,   23,        -1,    490,     25,    5,   200
gkgain,	ihgain		FLslider 	"Output Gain",				0,      1,   0,   23,        -1,    490,     25,    5,   250

;SET INITIAL VALUES FOR VALUATORS
			FLsetVal_i   	7, 	ihMin	
			FLsetVal_i   	11, 	ihMax	
			FLsetVal_i   	2, 	ihFreq	
			FLsetVal_i   	0.5, 	ihi_h
			FLsetVal_i   	0.1, 	ihgain
			FLpanel_end

;INSTRUCTIONS AND INFO PANEL
				FLpanel	" ", 500, 220, 512, 0
;TEXT BOXES												TYPE | FONT | SIZE | WIDTH | HEIGHT | X | Y
ih		 	FLbox  	"               FLsetVal - Random Moving Slider               ", 	1,      5,     14,    490,    15,     5,  0
ih		 	FLbox  	"-------------------------------------------------------------", 	1,      5,     14,    490,    15,     5,  20
ih		 	FLbox  	"This example generates a random function within the orchestra", 	1,      5,     14,    490,    15,     5,  40
ih		 	FLbox  	"the output of which drives the uppermost slider in the       ", 	1,      5,     14,    490,    15,     5,  60
ih		 	FLbox  	"adjacent GUI. This function is also used to control the      ", 	1,      5,     14,    490,    15,     5,  80
ih		 	FLbox  	"frequency of a sine wave oscillator. The range covered by the", 	1,      5,     14,    490,    15,     5, 100
ih		 	FLbox  	"random function can be defined by the user using the two     ", 	1,      5,     14,    490,    15,     5, 120
ih		 	FLbox  	"narrow range sliders and its frequency can be similarly      ", 	1,      5,     14,    490,    15,     5, 140
ih		 	FLbox  	"controlled. The user can also continuously vary the function ", 	1,      5,     14,    490,    15,     5, 160
ih		 	FLbox  	"between a sample and hold type function and an interpolating ", 	1,      5,     14,    490,    15,     5, 180
ih		 	FLbox  	"random function.                                             ", 	1,      5,     14,    490,    15,     5, 200

				FLpanel_end
			FLrun
			
gisine	ftgen	0,0,131072,10,1	
		
instr 1
	ksliderh		randomh		gkMin, gkMax, gkFreq		;GENERATE SAMPLE AND HOLD TYPE RANDOM VALUES
	kslideri		lineto		ksliderh, 1/gkFreq		;INTERPOLATE VALUES
	kslider			ntrpol		kslideri, ksliderh, gki_h   	;CROSSFADE BETWEEN INTERPOLATING AND SAMPLE AND HOLD TYPE RANDOM VALUES
	asig			oscil		gkgain, cpsoct(kslider), gisine	;SINE WAVE OSCILLATOR
				outs		asig, asig			;SEND AUDIO TO OUTPUTS
	ktrig			changed		kslider   			;CREATE A TRIGGER (MOMENTARY 1 VALUE) EACH TIME kx CHANGES
			FLsetVal	ktrig, kslider, gihslider  	;SEND VALUE kslider TO FLTK SLIDER
endin


</CsInstruments>

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

</CsoundSynthesizer>