;Written by Iain McCurdy, 2006

<CsoundSynthesizer>

<CsOptions>
-odevaudio -b400
</CsOptions>

<CsInstruments>

sr 		= 	44100	;SAMPLE RATE
ksmps 		= 	10	;NUMBER OF AUDIO SAMPLES IN EACH CONTROL CYCLE (MAY NEED TO BE LOW WHEN WORKING WITH SHORT DELAY TIMES DEFINED INITIALLY AT KRATE)
nchnls 		= 	2	;NUMBER OF CHANNELS (2=STEREO)
0dbfs	=	1	;MAXIMUM AMPLITUDE REGARDLESS OF BIT DEPTH

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

;                                                      		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

;VALUE_DISPLAY_BOXES			 	WIDTH | HEIGHT | X | Y
idloopstart			FLvalue	" ",     100,    20,     5, 125
idloopend			FLvalue	" ",     100,    20,     5, 175
idpitch				FLvalue	" ",     100,    20,     5, 225
idcrossfade			FLvalue	" ",     100,    20,     5, 275
idstart				FLvalue	" ",     100,    20,     5, 325
idamp				FLvalue	" ",     100,    20,     5, 375

;SLIDERS					            			MIN | MAX | EXP | TYPE | DISP       | WIDTH | HEIGHT | X | Y
gkloopstart,ihloopstart		FLslider 	"Loop Start",			0,     1,    0,    23,  idloopstart,   490,     25,    5, 100
gkloopend,ihloopend		FLslider 	"Loop End",			0,     1,    0,    23,  idloopend,     490,     25,    5, 150
gkpitch,ihpitch			FLslider 	"Pitch",			.25,   4,   -1,    23,  idpitch,       490,     25,    5, 200
gkcrossfade,ihcrossfade		FLslider 	"Crossfade (seconds)",		0,     1,    0,    23,  idcrossfade,   490,     25,    5, 250
gkstart,ihstart			FLslider 	"Start Position (i-rate)",	0,     1,    0,    23,  idstart,       490,     25,    5, 300
gkamp,ihamp			FLslider 	"Amplitude",			0,     1,    0,    23,  idamp,         490,     25,    5, 350

;COUNTERS							MIN | MAX | STEP1 | STEP2 | TYPE | WIDTH | HEIGHT |  X  | Y | OPCODE
gkfn, ihfn 			FLcount  "Soundfile", 		1,     2,     1,      1,      2,    145,     25,   175,   5,   -1
gkmode, ihmode 			FLcount  "Loop Mode (i-rate)", 	0,     2,     1,      1,      2,    145,     25,   350,   5,   -1

;TEXT BOXES              					TYPE | FONT | SIZE | WIDTH | HEIGHT | X | Y 
ih		 FLbox  "1=808loop.wav 2=AndItsAll.wav", 	1,     9,      12,   150,     30,    175, 50
ih		 FLbox  "0=forward 1=backward 2=back-and-forth",1,     9,      12,   150,     30,    350, 50


;SET_INITIAL_VALUES		VALUE | HANDLE
		FLsetVal_i	0, 	ihloopstart
		FLsetVal_i	1, 	ihloopend
		FLsetVal_i	1, 	ihpitch
		FLsetVal_i	.001, 	ihcrossfade
		FLsetVal_i	1, 	ihfn
		FLsetVal_i	0, 	ihstart
		FLsetVal_i	0.5, 	ihamp

		FLpanel_end	;END OF PANEL CONTENTS

;INSTRUCTIONS AND INFO PANEL
				FLpanel	" ", 500, 580, 512, 0
;TEXT BOXES												TYPE | FONT | SIZE | WIDTH | HEIGHT | X | Y
ih		 	FLbox  	"                           flooper2                          ", 	1,      5,     14,    490,    20,     5,  0
ih		 	FLbox  	"-------------------------------------------------------------", 	1,      5,     14,    490,    20,     5,  20
ih		 	FLbox  	"Flooper2 plays a soundfile with a user definable loop.       ", 	1,      5,     14,    490,    20,     5,  40
ih		 	FLbox  	"Loop start and end points can be changed at k-rate (i.e.     ", 	1,      5,     14,    490,    20,     5,  60
ih		 	FLbox  	"while the loop is already playing).                          ", 	1,      5,     14,    490,    20,     5,  80
ih		 	FLbox  	"Pitch is defined as a ratio of the original pitch of the     ", 	1,      5,     14,    490,    20,     5, 100
ih		 	FLbox  	"sample. I.e. a pitch value of 1 plays the soundfile back at  ", 	1,      5,     14,    490,    20,     5, 120
ih		 	FLbox  	"its original speed, a pitch value of 2 plays it back at twice", 	1,      5,     14,    490,    20,     5, 140
ih		 	FLbox  	"its original speed, a pitch value of 0.5 plays it back at    ", 	1,      5,     14,    490,    20,     5, 160
ih		 	FLbox  	"half its original speed and so on...                         ", 	1,      5,     14,    490,    20,     5, 180
ih		 	FLbox  	"The start time parameter allows the user to specify from     ", 	1,      5,     14,    490,    20,     5, 200
ih		 	FLbox  	"where the soundfile will begin playback, i.e. before entering", 	1,      5,     14,    490,    20,     5, 220
ih		 	FLbox  	"looping.                                                     ", 	1,      5,     14,    490,    20,     5, 240
ih		 	FLbox  	"In the opcode definition loop start and end points and start ", 	1,      5,     14,    490,    20,     5, 260
ih		 	FLbox  	"time are given in seconds. For convenience in this example   ", 	1,      5,     14,    490,    20,     5, 280
ih		 	FLbox  	"the relevant sliders define these parameters as fractions of ", 	1,      5,     14,    490,    20,     5, 300
ih		 	FLbox  	"the entire duration of the soundfile. (Subsequent rescaling  ", 	1,      5,     14,    490,    20,     5, 320
ih		 	FLbox  	"is done within the instrument code.) This allows the user to ", 	1,      5,     14,    490,    20,     5, 340
ih		 	FLbox  	"swap between soundfiles of differring lengths without the    ", 	1,      5,     14,    490,    20,     5, 360
ih		 	FLbox  	"need to re-adjust the FLTK slider ranges.                    ", 	1,      5,     14,    490,    20,     5, 380
ih		 	FLbox  	"The source sound for flooper2 should be stored in a GEN01    ", 	1,      5,     14,    490,    20,     5, 400
ih		 	FLbox  	"function table.                                              ", 	1,      5,     14,    490,    20,     5, 420
ih		 	FLbox  	"A parameter is provided to define a crossfade time for when  ", 	1,      5,     14,    490,    20,     5, 440
ih		 	FLbox  	"the file loops. Note that crossfade times are limited to the ", 	1,      5,     14,    490,    20,     5, 460
ih		 	FLbox  	"current duration of the loop.                                ", 	1,      5,     14,    490,    20,     5, 480
ih		 	FLbox  	"Three looping modes are offered. Details are provided in the ", 	1,      5,     14,    490,    20,     5, 500
ih		 	FLbox  	"interface opposite.                                          ", 	1,      5,     14,    490,    20,     5, 520
ih		 	FLbox  	"This example allows the user to choose between two source    ", 	1,      5,     14,    490,    20,     5, 540
ih		 	FLbox  	"samples.                                                     ", 	1,      5,     14,    490,    20,     5, 560

				FLpanel_end

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

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	gkfn, gkstart, 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:
	ifn		=	i(gkfn)
	ifilelength	=	nsamp(ifn)/sr
	kloopstart	=	ifilelength*gkloopstart
	kloopend	=	ifilelength*gkloopend
	asig 		flooper2 gkamp, gkpitch, kloopstart, kloopend, gkcrossfade, ifn, i(gkstart), i(gkmode);, ifenv, iskip
	rireturn	;RETURN FROM REINITIALISATION PASS TO PERFORMANCE TIME PASSES
	outs		asig, asig
endin

</CsInstruments>

<CsScore>
; THE SOUND FILES USED
; NUM | INIT_TIME | SIZE | GEN_ROUTINE | FILE_PATH     | IN_SKIP | FORMAT | CHANNEL
f  1        0      524288       1       "808loop.wav"       0         4        1 
f  2        0      524288       1       "AndItsAll.wav"     0         4        1 

f 0 3600	;DUMMY SCORE EVENT - ALLOWS REAL-TIME PERFORMANCE FOR 1 HOUR
</CsScore>

</CsoundSynthesizer>