;Written by Iain McCurdy, 2006

<CsoundSynthesizer>

<CsOptions>
-odevaudio -b100
</CsOptions>

<CsInstruments>

;DEMONSTRATION OF THE FLpanel OPCODE
;HORIZONTAL CLICK-AND-DRAGGING CONTROLS THE FREQUENCY OF THE OSCILLATOR
;VERTICAL CLICK-AND-DRAGGING CONTROLS THE AMPLITUDE OF THE OSCILLATOR

sr		=	44100
ksmps		=	256
nchnls		=	2

;FLTK INTERFACE CODE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FLcolor	255, 255, 255, 0, 0, 0
;		OPCODE	LABEL       | WIDTH | HEIGHT | X | Y
		FLpanel	"Theremin",   1225,    700,    0,  0


;gkfreqx,gkampy,ihandlex,ihandley	FLjoy	LABEL, IMINX, IMAXX, IMINY, IMAXY, IEXPX, IEXPY, IDISPX, IDISPY, IWIDTH, IHEIGHT, IX, IY
gkoctx, gky, ihandlex, ihandley		FLjoy	" ",    7,      11,    0,    1024,    0,     0,     -1,     -1,   1200,     150,   13, 100

;					WIDTH | HEIGHT | X  | Y
iddlytim		FLvalue	" ",	60,      20,     5,  325
iddlyFB			FLvalue	" ",	60,      20,     5,  375
iddlymix		FLvalue	" ",	60,      20,     5,  425
idRoomSize		FLvalue	" ",	60,      20,     5,  475
idHFDamp		FLvalue	" ",	60,      20,     5,  525
idrvbmix		FLvalue	" ",	60,      20,     5,  575

idmodfrq		FLvalue	" ",	60,      20,   505,  325
idvibdep		FLvalue	" ",	60,      20,   505,  375
idtrmdep		FLvalue	" ",	60,      20,   505,  425
idoctport		FLvalue	" ",	60,      20,   505,  475
idampport		FLvalue	" ",	60,      20,   505,  525
idoutamp		FLvalue	" ",	60,      20,   505,  575
idporttime		FLvalue	" ",	60,      20,   505,  625

;					     			MIN   |  MAX  | EXP | TYPE |  DISP  |  WIDTH | HEIGHT | X  | Y
gkdlytim, ihdlytim	FLslider	"Delay Time",		.0001,    2,     0,    23,  iddlytim,   490,     25,    5, 300
gkdlyFB,  ihdlyFB	FLslider	"Delay Feedback",	0,        1,     0,    23,  iddlyFB,    490,     25,    5, 350
gkdlymix, ihdlymix	FLslider	"Delay Mix",		0,        1,     0,    23,  iddlymix,   490,     25,    5, 400
gkRoomSize, ihRoomSize	FLslider	"Room Size",		0,        1,     0,    23,  idRoomSize, 490,     25,    5, 450
gkHFDamp, ihHFDamp	FLslider	"H.F. Diff,",		0,        1,     0,    23,  idHFDamp,   490,     25,    5, 500
gkrvbmix, ihrvbmix	FLslider	"Reverb Mix",		0,        1,     0,    23,  idrvbmix,   490,     25,    5, 550

gkmodfrq, ihmodfrq	FLslider	"Modulation Frequency",	0,       30,     0,    23,  idmodfrq,   490,     25,   505, 300
gkvibdep, ihvibdep	FLslider	"Vibrato Depth",	0,        1,     0,    23,  idvibdep,   490,     25,   505, 350
gktrmdep, ihtrmdep	FLslider	"Tremolo Depth",	0,        1,     0,    23,  idtrmdep,   490,     25,   505, 400
gkoctport, ihoctport	FLslider	"Pitch Portamento",	0,        1,     0,    23,  idoctport,  490,     25,   505, 450
gkampport, ihampport	FLslider	"Amplitude Portamento",	0,        1,     0,    23,  idampport,  490,     25,   505, 500
gkoutamp, ihoutamp	FLslider	"Output Amplitude",	0,        1,     0,    23,  idoutamp,   490,     25,   505, 550
gkporttime, ihporttime	FLslider	"Portamento Time",	0,        1,     0,    23,  idporttime, 490,     25,   505, 600

;INITIALISE VALUATORS		VALUE | HANDLE
		FLsetVal_i	.6, 	ihdlytim
		FLsetVal_i	.3, 	ihdlyFB
		FLsetVal_i	.2, 	ihdlymix
		FLsetVal_i	.9, 	ihRoomSize
		FLsetVal_i	.5, 	ihHFDamp    
		FLsetVal_i	.1, 	ihrvbmix      

		FLsetVal_i	5.5, 	ihmodfrq
		FLsetVal_i	.05, 	ihvibdep      
		FLsetVal_i	0, 	ihtrmdep      
		FLsetVal_i	.17, 	ihoctport      
		FLsetVal_i	.2, 	ihampport      
		FLsetVal_i	.5, 	ihoutamp
		FLsetVal_i	.1, 	ihporttime

;		FLcolor ired, igreen, iblue
		FLcolor 255,   255,    255

;BORDERS				TYPE | FONT | SIZE | WIDTH | HEIGHT | X | Y
ih		 	FLbox  	"C ", 	6,       9,    15,    25,    100,       1,  0
ih		 	FLbox  	"D ", 	6,       9,    15,    25,    100,    50+1,  0
ih		 	FLbox  	"E ", 	6,       9,    15,    25,    100,   100+1,  0
ih		 	FLbox  	"F ", 	6,       9,    15,    25,    100,   125+1,  0
ih		 	FLbox  	"G ", 	6,       9,    15,    25,    100,   175+1,  0
ih		 	FLbox  	"A ", 	6,       9,    15,    25,    100,   225+1,  0
ih		 	FLbox  	"B ", 	6,       9,    15,    25,    100,   275+1,  0
ih		 	FLbox  	"C ", 	6,       9,    15,    25,    100,   300+1,  0
ih		 	FLbox  	"D ", 	6,       9,    15,    25,    100,   350+1,  0
ih		 	FLbox  	"E ", 	6,       9,    15,    25,    100,   400+1,  0
ih		 	FLbox  	"F ", 	6,       9,    15,    25,    100,   425+1,  0
ih		 	FLbox  	"G ", 	6,       9,    15,    25,    100,   475+1,  0
ih		 	FLbox  	"A ", 	6,       9,    15,    25,    100,   525+1,  0
ih		 	FLbox  	"B ", 	6,       9,    15,    25,    100,   575+1,  0
ih		 	FLbox  	"C ", 	6,       9,    15,    25,    100,   600+1,  0
ih		 	FLbox  	"D ", 	6,       9,    15,    25,    100,   650+1,  0
ih		 	FLbox  	"E ", 	6,       9,    15,    25,    100,   700+1,  0
ih		 	FLbox  	"F ", 	6,       9,    15,    25,    100,   725+1,  0
ih		 	FLbox  	"G ", 	6,       9,    15,    25,    100,   775+1,  0
ih		 	FLbox  	"A ", 	6,       9,    15,    25,    100,   825+1,  0
ih		 	FLbox  	"B ", 	6,       9,    15,    25,    100,   875+1,  0
ih		 	FLbox  	"C ", 	6,       9,    15,    25,    100,   900+1,  0
ih		 	FLbox  	"D ", 	6,       9,    15,    25,    100,   950+1,  0
ih		 	FLbox  	"E ", 	6,       9,    15,    25,    100,  1000+1,  0
ih		 	FLbox  	"F ", 	6,       9,    15,    25,    100,  1025+1,  0
ih		 	FLbox  	"G ", 	6,       9,    15,    25,    100,  1075+1,  0
ih		 	FLbox  	"A ", 	6,       9,    15,    25,    100,  1125+1,  0
ih		 	FLbox  	"B ", 	6,       9,    15,    25,    100,  1175+1,  0
ih		 	FLbox  	"C ", 	6,       9,    15,    25,    100,  1200+1,  0

;		FLcolor ired, igreen, iblue
		FLcolor 0,      0,      0

ih		 	FLbox  	"C#", 	6,       9,    15,    25,     60,    25+1,  0
ih		 	FLbox  	"D#", 	6,       9,    15,    25,     60,    75+1,  0
ih		 	FLbox  	"F#", 	6,       9,    15,    25,     60,   150+1,  0
ih		 	FLbox  	"G#", 	6,       9,    15,    25,     60,   200+1,  0
ih		 	FLbox  	"A#", 	6,       9,    15,    25,     60,   250+1,  0
ih		 	FLbox  	"C#", 	6,       9,    15,    25,     60,   325+1,  0
ih		 	FLbox  	"D#", 	6,       9,    15,    25,     60,   375+1,  0
ih		 	FLbox  	"F#", 	6,       9,    15,    25,     60,   450+1,  0
ih		 	FLbox  	"G#", 	6,       9,    15,    25,     60,   500+1,  0
ih		 	FLbox  	"A#", 	6,       9,    15,    25,     60,   550+1,  0
ih		 	FLbox  	"C#", 	6,       9,    15,    25,     60,   625+1,  0
ih		 	FLbox  	"D#", 	6,       9,    15,    25,     60,   675+1,  0
ih		 	FLbox  	"F#", 	6,       9,    15,    25,     60,   750+1,  0
ih		 	FLbox  	"G#", 	6,       9,    15,    25,     60,   800+1,  0
ih		 	FLbox  	"A#", 	6,       9,    15,    25,     60,   850+1,  0
ih		 	FLbox  	"C#", 	6,       9,    15,    25,     60,   925+1,  0
ih		 	FLbox  	"D#", 	6,       9,    15,    25,     60,   975+1,  0
ih		 	FLbox  	"F#", 	6,       9,    15,    25,     60,  1050+1,  0
ih		 	FLbox  	"G#", 	6,       9,    15,    25,     60,  1100+1,  0
ih		 	FLbox  	"A#", 	6,       9,    15,    25,     60,  1150+1,  0

		FLpanel_end	;END OF PANEL CONTENTS
		FLrun		;RUN THE WIDGET THREAD!

		zakinit	1,1

		instr 	1
iporttime	=	.05
kporttime	linseg	0,.001,iporttime,1,iporttime
kporttime	=	kporttime * gkampport

iporttime2	=	.2
kporttime2	linseg	0,.001,iporttime2,1,iporttime2
kporttime2	=	kporttime2 * gkoctport

ifn		=	1
kamp		table	gky, 2
kamp		portk	kamp, kporttime
;koct		lineto	gkoctx, kporttime2
koct		portk	gkoctx, kporttime2
kmoddepth	table	gky, 3
kmoddepth	portk	kmoddepth, kporttime
kmod		oscili	kmoddepth, gkmodfrq, 99
koct		=	koct + (kmod*gkvibdep)

aamp		interp	kamp
koutamp		portk	gkoutamp, kporttime

ktrm		oscili	gktrmdep*.5*kmoddepth, gkmodfrq, 99
ktrm		=	ktrm+.5

asig		oscili	aamp*20000*ktrm*koutamp, cpsoct(koct), ifn
		zawm	asig, 1
		endin	

		instr	2	; PING PONG DELAY
kporttime	linseg	0,.001,1,1,1
kporttime	=	kporttime*gkporttime
imaxdelay	=	2		;MAXIMUM DELAY TIME

kdlytim		portk	gkdlytim, kporttime
adlytim		interp	kdlytim

ain		zar	1

;LEFT CHANNEL OFFSETTING DELAY (NO FEEDBACK!)
aBuffer		delayr	imaxdelay*.5
aLeftOffset	deltap3	adlytim*.5
		delayw	ain
		
;LEFT CHANNEL DELAY WITH FEEDBACK
aFBsigL		init	0
aBuffer		delayr	imaxdelay
aDlySigL	deltap3	adlytim
		delayw	aLeftOffset+aFBsigL
aFBsigL		=	aDlySigL * gkdlyFB

;RIGHT CHANNEL DELAY WITH FEEDBACK
aFBsigR		init	0
aBuffer		delayr	imaxdelay
aDlySigR	deltap3	adlytim
		delayw	ain+aFBsigR
aFBsigR		=	aDlySigR * gkdlyFB

amixL		ntrpol	ain, aDlySigL+aLeftOffset, 	gkdlymix
amixR		ntrpol	ain, aDlySigR, 			gkdlymix

		denorm		amixL, amixR	;DENORMALIZE BOTH CHANNELS OF AUDIO SIGNAL
arvbL, arvbR 	freeverb 	amixL, amixR, gkRoomSize, gkHFDamp , sr
amixL		ntrpol		amixL, arvbL, gkrvbmix	;CREATE A DRY/WET MIX BETWEEN THE DRY AND THE REVERBERATED SIGNAL
amixR		ntrpol		amixR, arvbR, gkrvbmix	;CREATE A DRY/WET MIX BETWEEN THE DRY AND THE REVERBERATED SIGNAL
		outs	amixL, amixR	;SEND DELAY OUTPUT SIGNALS TO THE SPEAKERS
		zacl	1,1
		endin

</CsInstruments>

<CsScore>
;f 1 0 129 10 1 .1 .05 .025 .0125 .00625 .003125		;FUNCTION TABLE THAT DEFINES A SINGLE CYCLE OF A SINE WAVE
;f 1 0 129 10 .5 .8 1 .1 .05 .025 .0125 .00625 .003125		;FUNCTION TABLE THAT DEFINES A SINGLE CYCLE OF A SINE WAVE
;f 1 0 129 10 .5 1 .1		;FUNCTION TABLE THAT DEFINES A SINGLE CYCLE OF A SINE WAVE
f 1 0 129 10 1 .1 ;.05 .025 .0125 .00625 .003125		;FUNCTION TABLE THAT DEFINES A SINGLE CYCLE OF A SINE WAVE
f 2 0 1024 7 0 40 0 200 1 [1024-240] 1
f 3 0 1024 7 0 260 0 [1024-260] 1
f99 0 129 10 1
i 1 0 3600		;INSTRUMENT 1 WILL PLAY A NOTE FOR 1 HOUR
i 2 0 3600		;INSTRUMENT 2 WILL PLAY A NOTE FOR 1 HOUR
</CsScore>

</CsoundSynthesizer>
<MacOptions>
Version: 3
Render: Real
Ask: Yes
Functions: ioObject
Listing: Window
WindowBounds: 72 179 400 200
CurrentView: io
IOViewEdit: On
Options:
</MacOptions>

<MacGUI>
ioView nobackground {65535, 65535, 65535}
ioSlider {5, 5} {20, 100} 0.000000 1.000000 0.000000 slider1
ioSlider {45, 5} {20, 100} 0.000000 1.000000 0.000000 slider2
ioSlider {85, 5} {20, 100} 0.000000 1.000000 0.000000 slider3
ioSlider {125, 5} {20, 100} 0.000000 1.000000 0.000000 slider4
ioSlider {165, 5} {20, 100} 0.000000 1.000000 0.000000 slider5
</MacGUI>

