;Writen by Iain McCurdy, 2006

<CsoundSynthesizer>

<CsOptions>
-odevaudio -M0 -b400 
</CsOptions>    

<CsInstruments>

sr 		= 	44100	;SAMPLE RATE
ksmps 		= 	2	;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
;INSTRUCTIONS AND INFO PANEL                 WIDTH | HEIGHT | X | Y
				FLpanel	" ", 515,     480,    0,  0
;TEXT BOXES												TYPE | FONT | SIZE | WIDTH | HEIGHT | X | Y
ih		 	FLbox  	"  Rescaling a ctrl7 derived variable using a function table  ", 	1,      5,     14,    490,    20,     5,  0
ih		 	FLbox  	"-------------------------------------------------------------", 	1,      5,     14,    490,    20,     5,  20
ih		 	FLbox  	"Move controller 1 on MIDI channel 1 (or the modulation wheel ", 	1,      5,     14,    490,    20,     5,  40
ih		 	FLbox  	"on a keyboard set to channel 1). This pitch of the oscillator", 	1,      5,     14,    490,    20,     5,  60
ih		 	FLbox  	"rises up one octave and then falls by two octaves.           ", 	1,      5,     14,    490,    20,     5,  80
ih		 	FLbox  	"Ctrl7 includes an optional argument which can be used to     ", 	1,      5,     14,    490,    20,     5, 100
ih		 	FLbox  	"reference a function table which will be used to rescale the ", 	1,      5,     14,    490,    20,     5, 120
ih		 	FLbox  	"range of values already defined for the controller derived   ", 	1,      5,     14,    490,    20,     5, 140
ih		 	FLbox  	"varable. This feature can be used to create a non-linear     ", 	1,      5,     14,    490,    20,     5, 160
ih		 	FLbox  	"mapping of values as the controller is moved.                ", 	1,      5,     14,    490,    20,     5, 180
ih		 	FLbox  	"In this example a function table has been created using GEN 7", 	1,      5,     14,    490,    20,     5, 200
ih		 	FLbox  	"(straight line segments) as follows:                         ", 	1,      5,     14,    490,    20,     5, 220
ih		 	FLbox  	";       GEN VAL. DUR. VAL. DUR VAL                           ", 	1,      5,     14,    490,    20,     5, 240
ih		 	FLbox  	"f 2 129 -7   8    64   9    64  7                            ", 	1,      5,     14,    490,    20,     5, 260
ih		 	FLbox  	"A minus sign is placed in front of the the GEN routine number", 	1,      5,     14,    490,    20,     5, 280
ih		 	FLbox  	"to ensure that the values will not be normalised.            ", 	1,      5,     14,    490,    20,     5, 300
ih		 	FLbox  	"When the controller is rescaled using this table its output  ", 	1,      5,     14,    490,    20,     5, 320
ih		 	FLbox  	"will rise from 8 to 9 for the first half of the controller's ", 	1,      5,     14,    490,    20,     5, 340
ih		 	FLbox  	"movement at which point it will change direction and descend ", 	1,      5,     14,    490,    20,     5, 360
ih		 	FLbox  	"to a final value of 7. These values are used as 'oct' format ", 	1,      5,     14,    490,    20,     5, 380
ih		 	FLbox  	"values for the oscillators frequency paramenter.             ", 	1,      5,     14,    490,    20,     5, 400
ih		 	FLbox  	"Another common use of this feature is to remap logarithmic   ", 	1,      5,     14,    490,    20,     5, 420
ih		 	FLbox  	"parameters such as frequency so that the MIDI slider         ", 	1,      5,     14,    490,    20,     5, 440
ih		 	FLbox  	"controlling them give a more useful response.                ", 	1,      5,     14,    490,    20,     5, 460
				FLpanel_end
				FLrun	;RUN THE FLTK WIDGET THREAD
;END OF FLTK INTERFACE CODE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

instr	1	;BEEP INSTRUMENT - ACTIVATED BY A SCORE NOTE - FREQUENCY VARIABLE USING CONTROLLER 1
	
	;OUTPUT		OPCODE	CHANNEL | CTRLNUMBER | MINIMUM | MAXIMUM | F.N._USED_TO_RESCALE_SLIDER_VALUES
	koct		ctrl7	1,            1,          0,        1,      2 					;READ CONTROLLER INFORMATION FROM CONTINUOUS CONTROLLER NUMBER 1 ON MIDI CHANNEL 1, RESCALE TO BE WITHIN THE RANGE 0 - 1 THEN RESCALE AGAIN ACCORDING TO FUNCTION TABLE NO. 2
	
	;OUTPUT	OPCODE	AMPLITUDE |    FREQ.    | F.N.
	asig	oscili	0.2,       cpsoct(koct),   1	;FREQUENCY IS CONVERTED FROM 'OCT' FORMAT INTO 'CPS' (CYCLES PER SECOND) FORMAT AS OSCILI EXPECTS	
	
		outs	asig, asig	;SEND AUDIO SIGNAL TO THE OUTPUTS
endin

</CsInstruments>

<CsScore>S
f 1 0 129 10 1			;SINE WAVE
f 2 0 129 -7 8 64 9 64 7	;RESCALING FUNCTION (NOTE: UN-NORMALIZED FUNCTION - THE USE OF A MINUS SIGN IN THE GEN ROUTINE NUMBER). REFER TO CSOUND MANUAL REGARDING GEN 07 FOR MORE INFORMATION
i 1 0 3600			;INSTRUMENT 1 PLAYS A NOTE FOR 1 HOUR
</CsScore>

</CsoundSynthesizer>