;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	"phaser2",     500,    350,    0,  0
;SWITCHES						ON | OFF | TYPE | WIDTH | HEIGHT | X | Y | OPCODE | INS | STARTTIM | DUR
gkOnOff,ihOnOff	FLbutton	"On/Off",		1,    0,    22,    120,     25,    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		WIDTH | HEIGHT | X | Y
idfreq		FLvalue	" ",      50,     20,    5,  75
idq		FLvalue	" ",      50,     20,    5, 125
idsep		FLvalue	" ",      50,     20,    5, 175
idfeedback	FLvalue	" ",      50,     20,    5, 225
idampscale	FLvalue	" ",      50,     20,    5, 275

;SLIDERS							MIN  |   MAX   | EXP | TYPE |   DISP     | WIDTH | HEIGHT | X |  Y
gkfreq, ihfreq		FLslider	"Frequency",  		20,    20000,    -1,    23,    idfreq,       490,     25,    5,   50
gkq, ihq		FLslider	"Q",           		.0001,     4,     0,    23,    idq,          490,     25,    5,  100
gksep, ihsep		FLslider 	"Separation",         	0,         2,     0,    23,    idsep,        490,     25,    5,  150
gkfeedback, ihfeedback	FLslider	"Feedback",   		-.99,    .99,     0,    23,    idfeedback,   490,     25,    5,  200
gkampscale, ihampscale	FLslider	"Amplitude",  		0,         1,     0,    23,    idampscale,   490,     25,    5,  250
gkingain,ihingain	FLslider 	"Live Input Gain",	0,         1,   0,    23,   -1,          140,     20,  350,   5

;COUNTERS					MIN |  MAX | STEP1 | STEP2 | TYPE | WIDTH | HEIGHT | X | Y | OPCODE 
gkord, ihord	FLcount  "No. of Ordinates",	1,    2499,    1,     10,     1,     150,    25,     50,300,   -1
gkmode, ihmode	FLcount  "Mode",		1,       2,    1,     10,     2,     150,    25,    300,300,   -1

		FLsetVal_i	1174, ihfreq
		FLsetVal_i	   3, ihq
		FLsetVal_i	   8, ihord
		FLsetVal_i	   2, ihmode
		FLsetVal_i	 .75, ihsep
		FLsetVal_i	 .87, ihfeedback
		FLsetVal_i	   1, ihampscale

		FLpanel_end	;END OF PANEL CONTENTS

;INSTRUCTIONS AND INFO PANEL
				FLpanel	" ", 500, 760, 512, 0
;TEXT BOXES												TYPE | FONT | SIZE | WIDTH | HEIGHT | X | Y
ih		 	FLbox  	"                          phaser2                            ", 	1,      5,     14,    490,    15,     5,  0
ih		 	FLbox  	"-------------------------------------------------------------", 	1,      5,     14,    490,    15,     5,  20
ih		 	FLbox  	"The phaser2 opcode is an implementation of an algorithm      ", 	1,      5,     14,    490,    15,     5,  40
ih		 	FLbox  	"consisting of a user definable number of second order allpass", 	1,      5,     14,    490,    15,     5,  60
ih		 	FLbox  	"filters arranged in series.                                  ", 	1,      5,     14,    490,    15,     5,  80
ih		 	FLbox  	"'Frequency' (kfreq) (in Hz) controls the centre frequency of ", 	1,      5,     14,    490,    15,     5, 100
ih		 	FLbox  	"the first filter in the series. Frequencies of subsequent    ", 	1,      5,     14,    490,    15,     5, 120
ih		 	FLbox  	"filters in the series are derived from this base frequency   ", 	1,      5,     14,    490,    15,     5, 140
ih		 	FLbox  	"using the setting given by Separation (ksep).                ", 	1,      5,     14,    490,    15,     5, 160
ih		 	FLbox  	"'Q' (kq) controls the amount of Q. Higher values result in   ", 	1,      5,     14,    490,    15,     5, 180
ih		 	FLbox  	"narrower filter notches.                                     ", 	1,      5,     14,    490,    15,     5, 200
ih		 	FLbox  	"'No. of Ordinates' (kord) allows the user to define the      ", 	1,      5,     14,    490,    15,     5, 220
ih		 	FLbox  	"number of allpass filter stages present in the algorithm     ", 	1,      5,     14,    490,    15,     5, 240
ih		 	FLbox  	"(range: 1 -2499).                                            ", 	1,      5,     14,    490,    15,     5, 260
ih		 	FLbox  	"'Mode' (kmode) selects between 2 methods of spacing the      ", 	1,      5,     14,    490,    15,     5, 280
ih		 	FLbox  	"frequencies of filters. When Mode is given a value of 1 the  ", 	1,      5,     14,    490,    15,     5, 300
ih		 	FLbox  	"frequencies are spaced at equal intervals in Hz. When Mode is", 	1,      5,     14,    490,    15,     5, 320
ih		 	FLbox  	"given a value of 2 each frequency is a power of 'Separation' ", 	1,      5,     14,    490,    15,     5, 340
ih		 	FLbox  	"(ksep) multiplied to the previous frequency value.           ", 	1,      5,     14,    490,    15,     5, 360
ih		 	FLbox  	"Separation (ksep) controls the frequency separation between  ", 	1,      5,     14,    490,    15,     5, 380
ih		 	FLbox  	"subsequent filters in the series. Its precise functioning is ", 	1,      5,     14,    490,    15,     5, 400
ih		 	FLbox  	"also dependent upon the setting given to 'Mode'. Refer to the", 	1,      5,     14,    490,    15,     5, 420
ih		 	FLbox  	"Csound manual for further information regarding this         ", 	1,      5,     14,    490,    15,     5, 440
ih		 	FLbox  	"parameter.                                                   ", 	1,      5,     14,    490,    15,     5, 460
ih		 	FLbox  	"Feedback (kfeedback) controls the amount of the signal which ", 	1,      5,     14,    490,    15,     5, 480
ih		 	FLbox  	"is fed back from the output back into the input of the chain ", 	1,      5,     14,    490,    15,     5, 500
ih		 	FLbox  	"of filters. This value should be less than 1 and more than   ", 	1,      5,     14,    490,    15,     5, 520
ih		 	FLbox  	"-1.                                                          ", 	1,      5,     14,    490,    15,     5, 540
ih		 	FLbox  	"I have included an attenuating amplitude control as this     ", 	1,      5,     14,    490,    15,     5, 560
ih		 	FLbox  	"opcode can produce increases in amplitude.                   ", 	1,      5,     14,    490,    15,     5, 580
ih		 	FLbox  	"In this example the user can choose between a sound file     ", 	1,      5,     14,    490,    15,     5, 600
ih		 	FLbox  	"input, the computer's live input or a stream of click        ", 	1,      5,     14,    490,    15,     5, 620
ih		 	FLbox  	"impulses. The risk of feedback is greatly increased through  ", 	1,      5,     14,    490,    15,     5, 640
ih		 	FLbox  	"the use of phaser1 processing so for this reason a gain      ", 	1,      5,     14,    490,    15,     5, 660
ih		 	FLbox  	"control on the live input, which is initially zero, is       ", 	1,      5,     14,    490,    15,     5, 680
ih		 	FLbox  	"included. Use with caution! It might actually be better to   ", 	1,      5,     14,    490,    15,     5, 700
ih		 	FLbox  	"monitor through headphones to further reduce the risk of     ", 	1,      5,     14,    490,    15,     5, 720
ih		 	FLbox  	"feedback when using the live input.                          ", 	1,      5,     14,    490,    15,     5, 740

				FLpanel_end

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

instr	1	;PLAYS FILE AND SENSES FADER MOVEMENT AND RESTARTS INSTR 2 FOR I-RATE CONTROLLERS
	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
	kSwitch		changed		gkord, gkmode	;GENERATE A MOMENTARY '1' PULSE IN OUTPUT 'kSwitch' IF ANY OF THE SCANNED INPUT VARIABLES CHANGE. (OUTPUT 'kSwitch' IS NORMALLY ZERO)
	if	kSwitch=1	then		;IF I-RATE VARIABLE CHANGE TRIGGER IS '1'...
		reinit	UPDATE			;BEGIN A REINITIALISATION PASS FROM LABEL 'UPDATE'
	endif
	UPDATE:
	aphaserl	phaser2		gasigL, gkfreq, gkq, gkord, gkmode, gksep, gkfeedback	; PHASER2 IS APPLIED TO THE LEFT CHANNEL
	aphaserr	phaser2		gasigR, gkfreq, gkq, gkord, gkmode, gksep, gkfeedback	; PHASER1 IS APPLIED TO THE RIGHT CHANNEL
	rireturn	;RETURN FROM REINITIALISATION PASS TO PERFORMANCE TIME PASSES
	outs		aphaserl * gkampscale, aphaserr * gkampscale
	clear	gasigL, gasigR	;CLEAR GLOBAL AUDIO VARIABLES
endin
		
</CsInstruments>

<CsScore>
i 2 0 -1	;INSTRUMENT 2 (PHASER INSTRUMENT) PLAYS A HELD NOTE
f 0 3600	;'DUMMY' SCORE EVENT: KEEP THE PERFORMANCE GOING FOR 1 HOUR
</CsScore>


</CsoundSynthesizer>



























