;Written by Iain McCurdy, 2009

<CsoundSynthesizer>

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

<CsInstruments>

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

;FLTK INTERFACE CODE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FLcolor	255, 255, 255, 0, 0, 0
;					LABEL        | WIDTH | HEIGHT | X | Y
				FLpanel	"lposcilsa",    500,    300,    0,  0

;BUTTONS					ON | OFF | TYPE | WIDTH | HEIGHT | X | Y | OPCODE | INS | STARTTIM | IDUR
gkOnOff,ihOnOff	FLbutton	"On/Off",	1,    0,    22,    150,     25,    5,  5,    0,      1,      0,      3600
FLsetColor2	255, 255, 50, ihOnOff		;SET SECONDARY COLOUR TO YELLOW

;VALUE DISPLAY BOXES			LABEL  | WIDTH | HEIGHT | X | Y
idamp				FLvalue	" ",       70,    15,     5,  75
idfreqratio			FLvalue	" ",      100,    20,     5, 125
idloop				FLvalue	" ",      100,    20,     5, 175
idend				FLvalue	" ",      100,    20,     5, 225
idphs				FLvalue	" ",      100,    20,     5, 275

;SLIDERS					            				MIN | MAX | EXP | TYPE |   DISP     | WIDTH | HEIGHT | X | Y
gkamp, ihamp			FLslider 	"Amplitude",				0,      1,   0,    23,        idamp,   490,     25,    5,  50
gkfreqratio, ihfreqratio	FLslider 	"Frequency Ratio",			-2,     2,   0,    23,  idfreqratio,   490,     25,    5, 100
gkloop, ihloop			FLslider 	"Loop Begin Point",			0,      1,   0,    23,       idloop,   490,     25,    5, 150
gkend, ihend			FLslider 	"Loop End Point",			0,      1,   0,    23,        idend,   490,     25,    5, 200
gkphs, ihphs			FLslider 	"Initial Phase (i-rate)",		0,      1,   0,    23,        idphs,   490,     25,    5, 250

;SET INITIAL VALUES OF FLTK VALUATORS		VALUE | HANDLE
				FLsetVal_i	0.3, 	ihamp
				FLsetVal_i	1, 	ihfreqratio
				FLsetVal_i	0, 	ihloop
				FLsetVal_i	1, 	ihend
				FLsetVal_i	0, 	ihphs

				FLpanel_end

;INSTRUCTIONS AND INFO PANEL
				FLpanel	" ", 500, 420, 512, 0
;TEXT BOXES												TYPE | FONT | SIZE | WIDTH | HEIGHT | X | Y
ih		 	FLbox  	"                         lposcilsa                           ", 	1,      5,     14,    490,     20,    5,   0
ih		 	FLbox  	"-------------------------------------------------------------", 	1,      5,     14,    490,     20,    5,  20
ih		 	FLbox  	"'lposcilsa' provides stereo sound file playback (stored in   ", 	1,      5,     14,    490,     20,    5,  40
ih		 	FLbox  	"RAM) with interpolation with normal forward looping with     ", 	1,      5,     14,    490,     20,    5,  60
ih		 	FLbox  	"k-rate variable start and and loop points.                   ", 	1,      5,     14,    490,     20,    5,  80
ih		 	FLbox  	"'lposcilsa' can read from stereo function tables and the     ", 	1,      5,     14,    490,     20,    5, 100
ih		 	FLbox  	"size of those function tables can be deferred (given as zero)", 	1,      5,     14,    490,     20,    5, 120
ih		 	FLbox  	"meaning that Csound calculates the required function table   ", 	1,      5,     14,    490,     20,    5, 140
ih		 	FLbox  	"size to store the entire sound file.                         ", 	1,      5,     14,    490,     20,    5, 160
ih		 	FLbox  	"'Loop Start Point', 'Loop End Point' and 'Initial Phase' are ", 	1,      5,     14,    490,     20,    5, 180
ih		 	FLbox  	"all given to the opcode in samples but in this example, this ", 	1,      5,     14,    490,     20,    5, 200
ih		 	FLbox  	"calculation is performed within the instrument.              ", 	1,      5,     14,    490,     20,    5, 220
ih		 	FLbox  	"Negative values for 'Frequency Ratio' result in reversed     ", 	1,      5,     14,    490,     20,    5, 240
ih		 	FLbox  	"playback.                                                    ", 	1,      5,     14,    490,     20,    5, 260
ih		 	FLbox  	"Similar to this opcode are lposcila (mono version), lposcil  ", 	1,      5,     14,    490,     20,    5, 280
ih		 	FLbox  	"(mono with k-rate amplitude argument, lposcil3 (mono with    ", 	1,      5,     14,    490,     20,    5, 300
ih		 	FLbox  	"cubic interpolation and k-rate amplitude control), lposcilsa2", 	1,      5,     14,    490,     20,    5, 320
ih		 	FLbox  	"(high speed version that only allows integer values for      ", 	1,      5,     14,    490,     20,    5, 340
ih		 	FLbox  	"frequency ratio).                                            ", 	1,      5,     14,    490,     20,    5, 360
ih		 	FLbox  	"If 'Loop Start Point' is beyond 'Loop End Point', no sound is", 	1,      5,     14,    490,     20,    5, 380
ih		 	FLbox  	"produced.                                                    ", 	1,      5,     14,    490,     20,    5, 400

				FLpanel_end

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

0dbfs	=	1	;SET MAXIMUM AMPLITUDE VALUE AND INDEPENDENCE FROM BIT-DEPTH

gift	ftgen	0, 0, 0, 1, "Songpan.wav", 0, 0, 0	;STORED STEREO SOUND FILE. TABLE SIZE IS DEFERRED - CSOUND CALCULATES THE SIZE OF TABLE NEEDED.

instr	1
	if	gkOnOff=0	then	;IF On/Off SWITCH IS SET TO 'OFF'...
	turnoff			;TURN THIS INSTRUMENT OFF
	endif				;END OF CONDITIONAL BRANCHING

	kSwitch		changed	gkphs	;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:

	aamp	interp	gkamp			;CREATE A-RATE VERSION OF AMPLITUDE SLIDER VALUE (THIS IS DEMANDED BY lposcilsa)
	ifilelen	=	nsamp(gift)	;DERIVE SOUNDFILE LENGTH (IN SAMPLES)
	ar1, ar2 lposcilsa aamp, gkfreqratio, gkloop*ifilelen, gkend*ifilelen, gift , i(gkphs)*ifilelen 
	rireturn	;RETURN FROM REINITIALISATION PASS TO PERFORMANCE TIME PASSES
	outs	ar1, ar2	;SEND AUDIO TO OUTPUTS
endin

</CsInstruments>                         
                                         
<CsScore>
f 0 3600	;DUMMY SCORE EVENT SUSTAINS REALTIME PERFORMANCE FOR 1 HOUR
</CsScore>                               
                                         
</CsoundSynthesizer>