;Written by Iain McCurdy, 2006

; DEMONSTRATION OF THE phaser1 OPCODE - NOT TO BE CONFUSED WITH THE phasor OPCODE!

<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	"phaser1",    500,    200,    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	" ",      80,     20,    5,  75
idfeedback		FLvalue	" ",      80,     20,    5, 125

;COUNTERS						MIN | MAX | STEP1 | STEP2 | TYPE | WIDTH | HEIGHT | X | Y | OPCODE 
gkord, ihord	FLcount  "No. of Ordinates (i-rate)",	1,    4999,   1,     10,     1,     150,     25,   345,150,   -1

;SLIDERS							MIN  |  MAX  | EXP | TYPE |   DISP    | WIDTH | HEIGHT | X | Y
gkfreq, ihfreq		FLslider	"Frequency",  		20,    20000,  -1,    23,   idfreq,      490,     25,    5,  50
gkfeedback, ihfeedback	FLslider	"Feedback",   		-.99,    .99,   0,    23,   idfeedback,  490,     25,    5, 100
gkingain,ihingain	FLslider 	"Live Input Gain",	0,         1,   0,    23,   -1,          140,     20,  350,   5

;SET_INITIAL_VALUES		VALUE | HANDLE
		FLsetVal_i	550, 	ihfreq
		FLsetVal_i	50,   	ihord
		FLsetVal_i	.5,  	ihfeedback

		FLpanel_end	; END OF PANEL CONTENTS

;INSTRUCTIONS AND INFO PANEL
				FLpanel	" ", 500, 480, 512, 0
;TEXT BOXES												TYPE | FONT | SIZE | WIDTH | HEIGHT | X | Y
ih		 	FLbox  	"                          phaser1                            ", 	1,      5,     14,    490,    20,     5,  0
ih		 	FLbox  	"-------------------------------------------------------------", 	1,      5,     14,    490,    20,     5,  20
ih		 	FLbox  	"The phaser1 opcode is an implementation of an algorithm      ", 	1,      5,     14,    490,    20,     5,  40
ih		 	FLbox  	"consisting of a user definable number of first order allpass ", 	1,      5,     14,    490,    20,     5,  60
ih		 	FLbox  	"filters arranged in series.                                  ", 	1,      5,     14,    490,    20,     5,  80
ih		 	FLbox  	"Frequency (kfreq) (in Hz) controls the frequency at which    ", 	1,      5,     14,    490,    20,     5, 100
ih		 	FLbox  	"each filter shifts the phase of its output by 90 degrees.    ", 	1,      5,     14,    490,    20,     5, 120
ih		 	FLbox  	"No. of Ordinates (kord) allows the user to define the number ", 	1,      5,     14,    490,    20,     5, 120
ih		 	FLbox  	"of allpass filter stages present in the algorithm (range: 1 -", 	1,      5,     14,    490,    20,     5, 140
ih		 	FLbox  	"4999).                                                       ", 	1,      5,     14,    490,    20,     5, 160
ih		 	FLbox  	"Feedback (kfeedback) controls the amount of the signal which ", 	1,      5,     14,    490,    20,     5, 180
ih		 	FLbox  	"is fed back from the output back into the input of the chain ", 	1,      5,     14,    490,    20,     5, 200
ih		 	FLbox  	"of filters. This value should be less than 1 and more than   ", 	1,      5,     14,    490,    20,     5, 220
ih		 	FLbox  	"-1.                                                          ", 	1,      5,     14,    490,    20,     5, 240
ih		 	FLbox  	"To create a 'classic' phaser effect the number of ordinates  ", 	1,      5,     14,    490,    20,     5, 260
ih		 	FLbox  	"should be less than about 8 and the frequency should be      ", 	1,      5,     14,    490,    20,     5, 280
ih		 	FLbox  	"modulated by an LFO.                                         ", 	1,      5,     14,    490,    20,     5, 300
ih		 	FLbox  	"In this example the user can choose between a sound file     ", 	1,      5,     14,    490,    15,     5, 320
ih		 	FLbox  	"input, the computer's live input or a stream of click        ", 	1,      5,     14,    490,    15,     5, 340
ih		 	FLbox  	"impulses. The risk of feedback is greatly increased through  ", 	1,      5,     14,    490,    15,     5, 360
ih		 	FLbox  	"the use of phaser1 processing so for this reason a gain      ", 	1,      5,     14,    490,    15,     5, 380
ih		 	FLbox  	"control on the live input, which is initially zero, is       ", 	1,      5,     14,    490,    15,     5, 400
ih		 	FLbox  	"included. Use with caution! It might actually be better to   ", 	1,      5,     14,    490,    15,     5, 420
ih		 	FLbox  	"monitor through headphones to further reduce the risk of     ", 	1,      5,     14,    490,    15,     5, 440
ih		 	FLbox  	"feedback when using the live input.                          ", 	1,      5,     14,    490,    15,     5, 460
                                                                                                                                                  
				FLpanel_end                                                                                                       

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

instr	1	;INPUT
	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	;phaser 1 AND OUTPUT
	iporttime	=	.03				; CREATE A VARIABLE THAT WILL BE USED FOR PORTAMENTO TIME
	kporttime	linseg	0,(.01),iporttime,(1),iporttime	; CREATE A VARIABLE THAT WILL BE USED FOR PORTAMENTO TIME
	kfreq		portk		gkfreq, kporttime	; PORTAMENTO IS APPLIED TO 'SMOOTH' SLIDER MOVEMENT
	kSwitch		changed	gkord	;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	phaser1		gasigL, kfreq, gkord, gkfeedback	; PHASER1 IS APPLIED TO THE LEFT CHANNEL
	aphaserr	phaser1		gasigR, kfreq, gkord, gkfeedback	; PHASER1 IS APPLIED TO THE RIGHT CHANNEL
	rireturn	;RETURN FROM REINITIALISATION PASS TO PERFORMANCE TIME PASSES
			outs	aphaserl, aphaserr	; PHASER OUTPUT ARE SENT TO THE SPEAKERS
	clear	gasigL, gasigR	;CLEAR GLOBAL AUDIO VARIABLES
endin
		
</CsInstruments>

<CsScore>
i 2 0 3600	;INSTRUMENT 2 PLAYS FOR 1 HOUR AND ALLOWS REAL-TIME PERFORMANCE FOR 1 HOUR
</CsScore>


</CsoundSynthesizer>



























