;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
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	"Distance Emulator",   500,    430,    0,  0
 
;SWITCHES                       				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,        -1
FLsetColor2	255, 255, 50, ihOnOff		;SET SECONDARY COLOUR TO YELLOW

;VALUE DISPLAY BOXES			LABEL  | WIDTH | HEIGHT | X | Y
idLPFcf				FLvalue	" ",       80,     20,    5,  75
idHPFcf				FLvalue	" ",       80,     20,    5, 125
idRvbMix			FLvalue	" ",       80,     20,    5, 175
idRvbSze			FLvalue	" ",       80,     20,    5, 225
idRvbHFD			FLvalue	" ",       80,     20,    5, 275
idamp				FLvalue	" ",       80,     20,    5, 325
idMorph				FLvalue	" ",       80,     20,    5, 410

;SLIDERS					            				MIN | MAX | EXP | TYPE |  DISP   | WIDTH | HEIGHT | X   | Y
gkLPFcf,gihLPFcf		FLslider 	"Lowpass Filter Cutoff Frequency",	20,  20000, -1,   23,   idLPFcf,    490,     25,   5,    50
gkHPFcf,gihHPFcf		FLslider 	"Highpass Filter Cutoff Frequency",	20,  20000, -1,   23,   idHPFcf,    490,     25,   5,   100
gkRvbMix,gihRvbMix		FLslider 	"Reverb Mix",				0,       1,  0,   23,  idRvbMix,    490,     25,   5,   150
gkRvbSze,ihRvbSze		FLslider 	"Reverb Size",				0,       1,  0,   23,  idRvbSze,    490,     25,   5,   200
gkRvbHFD,ihRvbHFD		FLslider 	"Reverb High Frequency Damping",	0,       1,  0,   23,  idRvbHFD,    490,     25,   5,   250
gkamp,gihamp			FLslider 	"Amplitude",				0,       1,  0,   23,  idamp,       490,     25,   5,   300
				FLcolor		0, 255, 0, 255, 0, 0		;FUNKY COLOURS
gkMorph,ihMorph			FLslider 	"Morph Slider",				0,       1,  0,   1,   idMorph,     490,     60,   5,   350
				FLcolor		255, 255, 255, 0, 0, 0		;NORMAL COLOURS

;SET INITIAL VALUES OF FLTK VALUATORS
;						VALUE | HANDLE
				FLsetVal_i	20000, 	gihLPFcf
				FLsetVal_i	20, 	gihHPFcf
				FLsetVal_i	0, 	gihRvbMix
				FLsetVal_i	.85, 	ihRvbSze
				FLsetVal_i	.5, 	ihRvbHFD
				FLsetVal_i	1, 	gihamp
				FLsetVal_i	0.1, 	ihMorph
			
				FLpanel_end

;INSTRUCTIONS AND INFO PANEL                 WIDTH | HEIGHT | X | Y
				FLpanel	" ", 515,      300,  512, 0
;TEXT BOXES												TYPE | FONT | SIZE | WIDTH | HEIGHT | X | Y
ih		 	FLbox  	"                     Distance Emulator                       ", 	1,      5,     14,    490,    20,     5,  0
ih		 	FLbox  	"-------------------------------------------------------------", 	1,      5,     14,    490,    20,     5,  20
ih		 	FLbox  	"This example is operated by activating 'On/Off' button and   ", 	1,      5,     14,    490,    20,     5,  40
ih		 	FLbox  	"then operating the 'morph' slider. The output of the 'Morph' ", 	1,      5,     14,    490,    20,     5,  60
ih		 	FLbox  	"slider feeds back into the 'Lowpass Filter Cutoff Frequency',", 	1,      5,     14,    490,    20,     5,  80
ih		 	FLbox  	" 'Highpass Filter Cutoff Frequency', 'Reverb Mix' and        ", 	1,      5,     14,    490,    20,     5, 100
ih		 	FLbox  	"'Amplitude' sliders to allow synchronised modulation of those", 	1,      5,     14,    490,    20,     5, 120
ih		 	FLbox  	"paramters from a single slider and in a single movement.     ", 	1,      5,     14,    490,    20,     5, 140
ih		 	FLbox  	"The result of this particular process is that a more         ", 	1,      5,     14,    490,    20,     5, 160
ih		 	FLbox  	"elaborate imitation of a sound receding into the distance is ", 	1,      5,     14,    490,    20,     5, 180
ih		 	FLbox  	"created than by merely implementing a fade out.              ", 	1,      5,     14,    490,    20,     5, 200
ih		 	FLbox  	"The ranges of fader movements in the feedback process can be ", 	1,      5,     14,    490,    20,     5, 220
ih		 	FLbox  	"adjusted in the function tables in the score section of this ", 	1,      5,     14,    490,    20,     5, 240
ih		 	FLbox  	"Csound file. This technique could be used to create other    ", 	1,      5,     14,    490,    20,     5, 260
ih		 	FLbox  	"interesting types of sound metamorphosis.                    ", 	1,      5,     14,    490,    20,     5, 280
				FLrun	;RUN THE FLTK WIDGET THREAD
;END OF FLTK INTERFACE CODE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

instr	1
	; SENSE FLTK ON/OFF SWITCH
	if	gkOnOff!=0	kgoto	CONTINUE	;IF SWITCH IS *NOT* OFF, JUMP TO 'CONTINUE' LABEL, OTHERWISE...
				turnoff			;...TURN THIS INSTRUMENT OFF IMMEDIATELY
	CONTINUE:  					;'CONTINUE' LABEL
	
	iporttime 	= 	.05				;PORTAMENTO TIME
	kporttime	linseg	0,.1,(iporttime),1,(iporttime)	;USE OF A RAMPING UP ENVELOPE PREVENTS GLIDING PARAMETERS EACH TIME A NOTE IS RESTARTED
	kLPFcf		portk	gkLPFcf,kporttime		;PORTAMENTO APPLIED TO PARAMETER TO SMOOTH CHANGES
	kHPFcf		portk	gkHPFcf,kporttime		;PORTAMENTO APPLIED TO PARAMETER TO SMOOTH CHANGES
	kamp		portk	gkamp,kporttime			;PORTAMENTO APPLIED TO PARAMETER TO SMOOTH CHANGES
	kRvbSze		portk	gkRvbSze,kporttime		;PORTAMENTO APPLIED TO PARAMETER TO SMOOTH CHANGES
	kRvbMix		portk	gkRvbMix,kporttime		;PORTAMENTO APPLIED TO PARAMETER TO SMOOTH CHANGES
	
	;OUTPUTS	OPCODE	FILE                                 | SPEED | INSKIP | LOOPING (0=OFF 1=ON)
	asigL, asigR	diskin2	"Songpan.wav",    1,       0,         1	;GENERATE 2 AUDIO SIGNALS FROM A STEREO SOUNDFILE
	asigL		butlp	asigL, kLPFcf	;LOWPASS FILTER AUDIO SIGNAL
	asigR		butlp	asigR, kLPFcf  	;LOWPASS FILTER AUDIO SIGNAL
	asigL		buthp	asigL, kHPFcf	;HIGHPASS FILTER AUDIO SIGNAL
	asigR		buthp	asigR, kHPFcf  	;HIGHPASS FILTER AUDIO SIGNAL                                  
			denorm		asigL, asigR	;DENORMALIZE BOTH CHANNELS OF AUDIO SIGNAL
	arvbL, arvbR 	freeverb 	asigL, asigR, kRvbSze, gkRvbHFD , sr	;CREATE REVERBERATED SIGNAL FROM THE AUDIO SIGNAL FROM THE HIGHPASS FILTERS
	amixL		ntrpol		asigL, arvbL, kRvbMix	;CREATE A DRY/WET MIX BETWEEN THE DRY AND THE REVERBERATED SIGNAL
	amixR		ntrpol		asigR, arvbR, kRvbMix	;CREATE A DRY/WET MIX BETWEEN THE DRY AND THE REVERBERATED SIGNAL
			outs		amixL*kamp, amixR*gkamp	;SEND WET/DRY MIX TO THE OUTPUTS, RESCALE AMPLITUDE WITH 'Amplitude' (gkamp) SLIDER 
			endin
			
			instr		2	;THIS INSTRUMENT SCANS FOR MOVEMENT OF THE 'Morph' SLIDER AND UPDATES THE FEEDBACK SLIDERS AND VALUES WHEN REQUIRED
	ktrig		changed		gkMorph		;IF 'Morph' SLIDER IS MOVED UPDATE ALL RELEVANT FEEDBACK VALUES
	kLPFcf		tablei		gkMorph, 1, 1	;DERIVE FEEDBACK VALUE FROM THE RELEVANT FUNCTION TABLE
	kHPFcf		tablei		gkMorph, 2, 1      ;DERIVE FEEDBACK VALUE FROM THE RELEVANT FUNCTION TABLE
	kRvbMix		tablei		gkMorph, 3, 1      ;DERIVE FEEDBACK VALUE FROM THE RELEVANT FUNCTION TABLE
	kamp		tablei		gkMorph, 4, 1      ;DERIVE FEEDBACK VALUE FROM THE RELEVANT FUNCTION TABLE
			FLsetVal	ktrig, kLPFcf, gihLPFcf		;UPDATE SLIDERS (AND THEREFORE THE RELEVANT FEEDBACK PARAMETERS)
			FLsetVal	ktrig, kHPFcf, gihHPFcf         ;UPDATE SLIDERS (AND THEREFORE THE RELEVANT FEEDBACK PARAMETERS)
			FLsetVal	ktrig, kRvbMix, gihRvbMix       ;UPDATE SLIDERS (AND THEREFORE THE RELEVANT FEEDBACK PARAMETERS)
			FLsetVal	ktrig, kamp, gihamp             ;UPDATE SLIDERS (AND THEREFORE THE RELEVANT FEEDBACK PARAMETERS)
endin	

</CsInstruments>

<CsScore>
i 2 0 3600	;INSTRUMENT 2 PLAYS A NOTE FOR 1 HOUR AND ALSO SUSTAINS REALTIME PERFORMANCE
f 1 0 1025 -5 20000 	1024 	3000	;LPFcf
f 2 0 1025 -5 20 	1024 	3000	;HPFcf
f 3 0 1025 -7 0 	1024 	1	;RvbMix
f 4 0 1025 -7 1 	1024 	.2	;Amplitude

</CsScore>

</CsoundSynthesizer>