;Written by Iain McCurdy, 2006

<CsoundSynthesizer>

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

<CsInstruments>

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


;FLTK INTERFACE CODE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FLcolor	255, 255, 255, 0, 0, 0
;					LABEL   | WIDTH | HEIGHT | X | Y
				FLpanel	"wguide1", 500,    250,    0,  0
 
;SWITCHES                       				ON | OFF | TYPE | WIDTH | HEIGHT | X | Y | OPCODE | INS | STARTTIM | IDUR
gkOnOff,ihOnOff			FLbutton	"On/Off",	1,    0,    22,    150,     28,    5,  5,    0,      1,      0,       -1
FLsetColor2	255, 255, 50, ihOnOff	;SET SECONDARY COLOUR TO YELLOW

;GENERAL_TEXT_SETTINGS			SIZE | FONT |  ALIGN | RED | GREEN | BLUE
			FLlabel		13,      1,      3,    255,   255,   255		;LABELS MADE INVISIBLE (I.E. SAME COLOR AS PANEL)

;BUTTON BANKS					TYPE | NUMX | NUMY | WIDTH | HEIGHT | X | Y | OPCODE | INS | STARTTIM | DUR
gkinput, ihinput		FLbutBank	14,     1,     3,     18,      45,   250, 0,   -1

;GENERAL_TEXT_SETTINGS			SIZE | FONT |  ALIGN | RED | GREEN | BLUE
			FLlabel		13,      1,      3,     0,     0,     0			;LABELS MADE VISIBLE AGAIN

;TEXT BOXES						TYPE | FONT | SIZE | WIDTH | HEIGHT | X |  Y
ih		 	FLbox  	"Input", 		1,       5,    14,     50,      20, 200,   0
ih		 	FLbox  	"Seashore", 		1,       5,    11,     60,      13, 270,   0
ih		 	FLbox  	"Live    ", 		1,       5,    11,     60,      13, 270,  15
ih		 	FLbox  	"Impulses", 		1,       5,    11,     60,      13, 270,  30

;VALUE DISPLAY BOXES			LABEL  | WIDTH | HEIGHT | X | Y
idfreq				FLvalue	" ",       80,     20,    5,  75
idcutoff			FLvalue	" ",       80,     20,    5, 125
idfeedback			FLvalue	" ",       80,     20,    5, 175
idamp				FLvalue	" ",       80,     20,    5, 225

;SLIDERS					            			MIN | MAX | EXP | TYPE |  DISP     | WIDTH | HEIGHT | X   | Y
gkfreq,ihfreq			FLslider 	"Frequency",			20,  10000, -1,    23,   idfreq,      490,      25,   5,    50
gkcutoff,ihcutoff		FLslider 	"LPF Cutoff Frequency",		20,  20000, -1,    23,   idcutoff,    490,      25,   5,   100
gkfeedback,ihfeedback		FLslider 	"Feedback",			0,    .999,  0,    23,   idfeedback,  490,      25,   5,   150
gkamp,ihamp			FLslider 	"Output Amplitude Scaling",	0,       1,  0,    23,   idamp,       490,      25,   5,   200
gkingain,ihingain		FLslider 	"Live Input Gain",		0,       1,  0,    23,      -1,         140,      20, 350,     5

;SET INITIAL VALUES OF FLTK VALUATORS
;						VALUE | HANDLE
				FLsetVal_i	400, 	ihfreq
				FLsetVal_i	10000, 	ihcutoff
				FLsetVal_i	.9, 	ihfeedback
				FLsetVal_i	.7, 	ihamp
			
				FLpanel_end
;INSTRUCTIONS AND INFO PANEL
				FLpanel	" ", 500, 740, 512, 0
;TEXT BOXES												TYPE | FONT | SIZE | WIDTH | HEIGHT | X | Y
ih		 	FLbox  	"                           wguide1                           ", 	1,      5,     14,    490,    15,     5,   0
ih		 	FLbox  	"-------------------------------------------------------------", 	1,      5,     14,    490,    15,     5,  20
ih		 	FLbox  	"Wguide1 implements a delay and first order lowpass filter    ", 	1,      5,     14,    490,    15,     5,  40
ih		 	FLbox  	"with feedback in a single unit opcode. This arrangement is as", 	1,      5,     14,    490,    15,     5,  60
ih		 	FLbox  	"shown below:                                                 ", 	1,      5,     14,    490,    15,     5,  80
ih		 	FLbox  	"                +-----+  +--------------+                    ", 	1,      5,     14,    490,    15,     5, 100
ih		 	FLbox  	"        IN->-+->+DELAY+->+LOWPASS FILTER+->+--->OUT          ", 	1,      5,     14,    490,    15,     5, 120
ih		 	FLbox  	"             ^  +-----+  +--------------+  |                 ", 	1,      5,     14,    490,    15,     5, 140
ih		 	FLbox  	"             |                             |                 ", 	1,      5,     14,    490,    15,     5, 160
ih		 	FLbox  	"             +-----<-----[FEEDBACK]<-------+                 ", 	1,      5,     14,    490,    15,     5, 180
ih		 	FLbox  	"Delay time is not directly addressed as delay time are       ", 	1,      5,     14,    490,    15,     5, 200
ih		 	FLbox  	"expected to be small thus producing a comb filtering effect. ", 	1,      5,     14,    490,    15,     5, 220
ih		 	FLbox  	"Instead the frequency of the tone produced by this comb      ", 	1,      5,     14,    490,    15,     5, 240
ih		 	FLbox  	"filtering effect is given to the opcode. (Delay time could be", 	1,      5,     14,    490,    15,     5, 260
ih		 	FLbox  	"derived as 1/freq.) The minimum allowed value for 'Frequency'", 	1,      5,     14,    490,    15,     5, 280
ih		 	FLbox  	"is 5 hertz, this translates as a delay time of .2 second.    ", 	1,      5,     14,    490,    15,     5, 300
ih		 	FLbox  	"'Feedback' is expressed as a ratio of the entire signal. A   ", 	1,      5,     14,    490,    15,     5, 320
ih		 	FLbox  	"value of 0 means that none of the signal will be fed back    ", 	1,      5,     14,    490,    15,     5, 340
ih		 	FLbox  	"into the system, a value of 1 means that all of the signal   ", 	1,      5,     14,    490,    15,     5, 360
ih		 	FLbox  	"will be fed back into the system.                            ", 	1,      5,     14,    490,    15,     5, 380
ih		 	FLbox  	"The use of feedback can create dramatic rises in output      ", 	1,      5,     14,    490,    15,     5, 400
ih		 	FLbox  	"amplitude, therefore in this example I have included a slider", 	1,      5,     14,    490,    15,     5, 420
ih		 	FLbox  	"that controls output amplitude.                              ", 	1,      5,     14,    490,    15,     5, 440
ih		 	FLbox  	"Wguide's audible result is comparable to that of the         ", 	1,      5,     14,    490,    15,     5, 460
ih		 	FLbox  	"'streson' opcode.                                            ", 	1,      5,     14,    490,    15,     5, 480
ih		 	FLbox  	"Note that tuning inaccuracies are increasingly exhibited as  ", 	1,      5,     14,    490,    15,     5, 500
ih		 	FLbox  	"'Frequency' is increased. If precise tuning is required then ", 	1,      5,     14,    490,    15,     5, 520
ih		 	FLbox  	"it will probably not be suffucient to rely on the value given", 	1,      5,     14,    490,    15,     5, 540
ih		 	FLbox  	"for 'Frequency' and instead this should be checked by ear.   ", 	1,      5,     14,    490,    15,     5, 560
ih		 	FLbox  	"In this example the user can choose between a sound file     ", 	1,      5,     14,    490,    15,     5, 580
ih		 	FLbox  	"input, the computer's live input or a stream of click        ", 	1,      5,     14,    490,    15,     5, 600
ih		 	FLbox  	"impulses. The risk of feedback is greatly increased through  ", 	1,      5,     14,    490,    15,     5, 620
ih		 	FLbox  	"the use of wguide1 processing so for this reason a gain      ", 	1,      5,     14,    490,    15,     5, 640
ih		 	FLbox  	"control on the live input, which is initially zero, is       ", 	1,      5,     14,    490,    15,     5, 660
ih		 	FLbox  	"included. Use with caution! It might actually be better to   ", 	1,      5,     14,    490,    15,     5, 680
ih		 	FLbox  	"monitor through headphones to further reduce the risk of     ", 	1,      5,     14,    490,    15,     5, 700
ih		 	FLbox  	"feedback when using the live input.                          ", 	1,      5,     14,    490,    15,     5, 720
				FLpanel_end

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

instr	1
	if	gkOnOff=0	then	;IF On/Off SWITCH IS SET TO 'OFF'...
		turnoff			;TURN THIS INSTRUMENT OFF
	endif				;END OF CONDITIONAL BRANCHING
	if	gkinput==0	then	;IF INPUT 'Seashore' IS SELECTED...
		gasigL, gasigR	diskin2	"Seashore.wav",    1,       0,         1	;GENERATE 2 AUDIO SIGNALS FROM A STEREO SOUND FILE (NOTE THE USE OF GLOBAL VARIABLES)		
	elseif	gkinput==1	then	;IF INPUT 'Live' IS SELECTED...
		asigL, asigR	ins	;TAKE INPUT FROM COMPUTER'S AUDIO INPUT
		gasigL	=	asigL * gkingain	;SCALE USING 'Input Gain' SLIDER
		gasigR	=	asigR * gkingain	;SCALE USING 'Input Gain' SLIDER						;END OF CONDITIONAL BRANCHING
	else	;OTHERWISE...
		kint	random	0.1,0.4	;INTERVAL BETWEEN IMPULSES IS RANDOMISED SLIGHTLY
		a1	mpulse	4,kint	;GENERATE IMPULSES
		kcfoct	randomi	6,12,2	;GENERATE RANDOM VALUES FOR FILTER CUTOFF FREQUENCY (OCT FORMAT)
		gasigL	butlp	a1, cpsoct(kcfoct)	;LOWPASS FILTER STREAM OF CLICKS
		gasigR	=	gasigL			;SET RIGHT CHANNEL SIGNAL TO THE SAME AS THE LEFT CHANNEL SIGNAL
	endif
endin
		
instr	2
	iporttime	=	0.01				;I-RATE VARIABLE DEFINITION OF PORTAMENTO TIME
	kporttime	linseg	0,0.01,iporttime,1,iporttime	;RAMPING UP FROM ZERO VERSION OF PORTAMENTO TIME VARIABLE
	kfreq		portk	gkfreq, kporttime		;SMOOTH VARIABLE CHANGES WITH PORTK
	afreq		interp	kfreq				;INTERPOLATE TO CREATE A-RATE VERSION OF K-RATE VARIABLE
	;OUTPUT		OPCODE | INPUT | DELAY/COMB_FREQ | LPF_CUTOFF_FREQ | FEEDBACK_RATIO
	aresL 		wguide1 gasigL,        afreq,         gkcutoff,       gkfeedback
	aresR 		wguide1 gasigR,        afreq,         gkcutoff,       gkfeedback
	
			outs	aresL * gkamp, aresR * gkamp	;SEND wguide OUTPUT TO THE AUDIO OUTPUTS AND SCALE USING THE FLTK SLIDER VARIABLE gkamp
	clear	gasigL, gasigR	;CLEAR GLOBAL AUDIO VARIABLES
endin
  	
</CsInstruments>

<CsScore>
;INSTR | START | DURATION
i  2       0       3600	;INSTRUMENT 2 PLAYS FOR 1 HOUR (AND KEEPS REALTIME PERFORMANCE GOING)
</CsScore>

</CsoundSynthesizer>