;PIANO PHASE BY STEVE REICH 
;CSOUND DEMONSTRATION BY IAIN MCCURDY

<CsoundSynthesizer>

<CsOptions>
;INSERT YOUR CSOUND OPTIONS PREFERENCES HERE
-odevaudio -b1000 -d
</CsOptions>

<CsInstruments>

sr 		= 	44100
kr 		= 	441
nchnls 		= 	2

;		LABEL         | WIDTH | HEIGHT | X | Y
	FLpanel	"Piano Phase",  1400,    300,    0,  0

;SWITCHES                                					ON | OFF | TYPE | WIDTH | HEIGHT | X | Y | OPCODE | INS | STARTTIM | DUR
gkOnOff,ihOnOff		FLbutton	"On/Off",				1,    0,    2,     150,     30,    0,  0,    0,      1,      0,      3600

;NUMBER DISPLAY BOXES			WIDTH | HEIGHT | X | Y
idphs1	  	FLvalue   " ",     	80,       20,    0, 80
idphs2	  	FLvalue   " ",     	80,       20,    0,130

;SLIDERS								MIN | MAX | EXP | TYPE |   DISP     | WIDTH | HEIGHT | X   | Y
gkphs1, ihphs1		FLslider	"Phase 1 Offset (semiquavers)",	0,    12,    0,    5,     idphs1,     1400,     30,    0,    50
gkphs2, ihphs2		FLslider	"Phase 2 Offset (semiquavers)",	0,    12,    0,    5,     idphs2,     1400,     30,    0,   100
gkamp1, ihamp1		FLslider	"Piano 1 Gain",			0,    1,     0,    3,     -1,          700,     30,    0,   200
gkamp2, ihamp2		FLslider	"Piano 2 Gain",			0,    1,     0,    3,     -1,          700,     30,  700,   200
gkbal1, ihbal1		FLslider	"Piano 1 Balance",		0,    1,     0,    3,     -1,          700,     30,    0,   250
gkbal2, ihbal2		FLslider	"Piano 2 Balance",		0,    1,     0,    3,     -1,          700,     30,  700,   250

; INITIAL VALUES OF SLIDERS
;OPCODE		VALUE | HANDLE
FLsetVal_i   	0, 	ihphs1
FLsetVal_i   	0, 	ihphs2
FLsetVal_i   	1, 	ihamp1
FLsetVal_i   	1, 	ihamp2
FLsetVal_i   	0.5, 	ihbal1
FLsetVal_i   	0.5, 	ihbal2

	FLpanel_end

	FLrun

	instr	1
if	gkOnOff!=0	kgoto	SKIP
	turnoff
SKIP:

kporttime	linseg		0,.01,.01,1, .01

ilen		=		1.636

kphs1		portk		gkphs1/12, kporttime
aptr1		osciliktp 	1/ilen, 1, kphs1
kptr1		downsamp	aptr1

fsig1L  	pvsfread	kptr1*ilen, "loop1l.pvx"
asig1L 		pvsynth  	fsig1L
fsig1R  	pvsfread	kptr1*ilen, "loop1r.pvx"
asig1R 		pvsynth  	fsig1R

kphs2		portk		gkphs2/12, kporttime
aptr2		osciliktp 	1/ilen, 1, kphs2
kptr2		downsamp	aptr2

fsig2L  	pvsfread	kptr2*ilen, "loop2l.pvx"
asig2L 		pvsynth  	fsig2L
fsig2R  	pvsfread	kptr2*ilen, "loop2r.pvx"
asig2R 		pvsynth  	fsig2R

kamp1		lineto		gkamp1, 0.05
kamp2		lineto		gkamp2, 0.05
kbal1		lineto		gkbal1, 0.05
kbal2		lineto		gkbal2, 0.05

gamixL		sum		asig1L*kamp1*(1-kbal1), asig2L*kamp2*(1-kbal2)
gamixR		sum		asig1R*kamp1*kbal1, asig2L*kamp2*kbal2

		endin

		instr		2	;REVERB
gkRoomSize	=		.85
gkHFDamp	=		.5
gkmix		=		.5
		denorm		gamixL, gamixR		;DENORMALIZE BOTH CHANNELS OF AUDIO SIGNAL
arvbL, arvbR 	freeverb 	gamixL, gamixR, gkRoomSize, gkHFDamp , sr
amixL		ntrpol		gamixL, arvbL, gkmix	;CREATE A DRY/WET MIX BETWEEN THE DRY AND THE REVERBERATED SIGNAL
amixR		ntrpol		gamixR, arvbR, gkmix	;CREATE A DRY/WET MIX BETWEEN THE DRY AND THE REVERBERATED SIGNAL
		outs		amixL, amixR
gamixL		=		0
gamixR		=		0
		endin

</CsInstruments>

<CsScore>
f 1 0 1048576 7 0 1048576 1

i 2 0 3600
</CsScore>

</CsoundSynthesizer>
