;Written by Iain McCurdy, 2006

<CsoundSynthesizer>

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

<CsInstruments>

sr 		= 	44100	;SAMPLE RATE
ksmps 		= 	100	;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	"pchbend", 515,    440,    0,  0
;TEXT BOXES												TYPE | FONT | SIZE | WIDTH | HEIGHT | X | Y
ih		 	FLbox  	"              pchbend - Scanning MIDI Pitch Bend             ", 	1,      5,     14,    490,    20,     5,  0
ih		 	FLbox  	"-------------------------------------------------------------", 	1,      5,     14,    490,    20,     5,  20
ih		 	FLbox  	"This example demonstrates how to scan the MIDI pitch bend    ", 	1,      5,     14,    490,    20,     5,  40
ih		 	FLbox  	"wheel/joystick of a connected MIDI keyboard and to use the   ", 	1,      5,     14,    490,    20,     5,  60
ih		 	FLbox  	"incoming data to implement the traditional pitch bend        ", 	1,      5,     14,    490,    20,     5,  80
ih		 	FLbox  	"functionality.                                               ", 	1,      5,     14,    490,    20,     5, 100
ih		 	FLbox  	"Building upon the simple sine tone synthesizer of previous   ", 	1,      5,     14,    490,    20,     5, 120
ih		 	FLbox  	"examples the opcode 'pchbend' is employed to allow the player", 	1,      5,     14,    490,    20,     5, 140
ih		 	FLbox  	"to bend the pitch up or down by 2 semitones. (This interval  ", 	1,      5,     14,    490,    20,     5, 160
ih		 	FLbox  	"can be changed by modifying the code but it could equally be ", 	1,      5,     14,    490,    20,     5, 180
ih		 	FLbox  	"brought out to an FLTK counter.)                             ", 	1,      5,     14,    490,    20,     5, 200
ih		 	FLbox  	"Some additional mathematics and use of 'oct' format pitches  ", 	1,      5,     14,    490,    20,     5, 220
ih		 	FLbox  	"are employed in the implementation of pitch bend in this     ", 	1,      5,     14,    490,    20,     5, 240
ih		 	FLbox  	"example but I would recommend that you study the code for    ", 	1,      5,     14,    490,    20,     5, 260
ih		 	FLbox  	"further information.                                         ", 	1,      5,     14,    490,    20,     5, 280
ih		 	FLbox  	"There is nothing to say that the pitch bend wheel must be    ", 	1,      5,     14,    490,    20,     5, 300
ih		 	FLbox  	"used to drive the pitch of something in Csound - it could    ", 	1,      5,     14,    490,    20,     5, 320
ih		 	FLbox  	"also be used to control panning, sample playback speed and   ", 	1,      5,     14,    490,    20,     5, 340
ih		 	FLbox  	"many other parameters in Csound. The crucial element in the  ", 	1,      5,     14,    490,    20,     5, 360
ih		 	FLbox  	"design of the pitch bend wheel is in its used of a central   ", 	1,      5,     14,    490,    20,     5, 380
ih		 	FLbox  	"spring loaded equilibrium position and ideally this feature  ", 	1,      5,     14,    490,    20,     5, 400
ih		 	FLbox  	"should be capitalised upon when it is used in Csound.        ", 	1,      5,     14,    490,    20,     5, 420
				FLpanel_end
				FLrun	;RUN THE FLTK WIDGET THREAD
;END OF FLTK INTERFACE CODE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

instr	1	; SIMPLE SINE TONE SYNTHESIZER WITH PITCH BEND
	ioct	octmidi
	iamp	ampmidi	0.2
	
	;PITCH BEND INFORMATION IS READ
	iSemitoneBendRange = 2		;PITCH BEND RANGE IN SEMITONES (WILL BE DEFINED FURTHER LATER) - SUGGESTION - THIS COULD BE CONTROLLED BY AN FLTK COUNTER
	imin = 0			;EQUILIBRIUM POSITION
	imax = iSemitoneBendRange * .0833333	;MAX PITCH DISPLACEMENT (IN oct FORMAT)
	kbend	pchbend	imin, imax	;PITCH BEND VARIABLE (IN oct FORMAT)
	
	;               INITIAL_LEVEL | ATTACK_TIME | ATTACK_LEVEL | DECAY_TIME | SUSTAIN_LEVEL |  RELEASE_TIME | RELEASE_LEVEL
	aenv	linsegr	      0,           (.01),          1,           (.1),          .5,            (.2),            0 
	
	;OUTPUT	OPCODE	AMPLITUDE |     FREQUENCY      | FUNCTION_TABLE
	asig	oscili	iamp*aenv,  cpsoct(ioct+kbend),        1
		outs	asig, asig	;SEND AUDIO OUTPUT OF OSCILLATOR TO THE SPEAKERS
endin

</CsInstruments>

<CsScore>
f 1 0 129 10 1	; SINE WAVE
f 0 600	; THIS LINE FUNCTIONS AS A DUMMY SCORE EVENT AND ALLOWS REALTIME MIDI PLAYING FOR 10 MINUTES
</CsScore>

</CsoundSynthesizer>