;Writen 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	"linsegr", 515,    580,    0,  0
;TEXT BOXES												TYPE | FONT | SIZE | WIDTH | HEIGHT | X | Y
ih		 	FLbox  	"   linsegr (expsegr) - envelopes with MIDI release stages    ", 	1,      5,     14,    490,    20,     5,  0
ih		 	FLbox  	"-------------------------------------------------------------", 	1,      5,     14,    490,    20,     5,  20
ih		 	FLbox  	"We can still use the traditional envelope generating opcodes ", 	1,      5,     14,    490,    20,     5,  40
ih		 	FLbox  	"line/expon/linseg/expseg in MIDI activated instruments but   ", 	1,      5,     14,    490,    20,     5,  60
ih		 	FLbox  	"the popular practise of employing 'p3' (note duration derived", 	1,      5,     14,    490,    20,     5,  80
ih		 	FLbox  	"from the score) within the formation of an envelope, in order", 	1,      5,     14,    490,    20,     5, 100
ih		 	FLbox  	"to make that envelope's segment durations at least partially ", 	1,      5,     14,    490,    20,     5, 120
ih		 	FLbox  	"dependent upon note duration, will not be possible as Csound ", 	1,      5,     14,    490,    20,     5, 140
ih		 	FLbox  	"will not know note duration until a MIDI note-off is         ", 	1,      5,     14,    490,    20,     5, 160
ih		 	FLbox  	"received.                                                    ", 	1,      5,     14,    490,    20,     5, 180
ih		 	FLbox  	"The opcodes 'linsegr' (for straight line segments) and       ", 	1,      5,     14,    490,    20,     5, 200
ih		 	FLbox  	"'expsegr' (for exponential segments) will hold their         ", 	1,      5,     14,    490,    20,     5, 220
ih		 	FLbox  	"penultimate break-point value until a MIDI note off is       ", 	1,      5,     14,    490,    20,     5, 240
ih		 	FLbox  	"received at which point the final segment will proceed.      ", 	1,      5,     14,    490,    20,     5, 260
ih		 	FLbox  	"The final three arguments in a linsegr/expsegr can, from the ", 	1,      5,     14,    490,    20,     5, 280
ih		 	FLbox  	"perspective of a playable instrument, be regarded as         ", 	1,      5,     14,    490,    20,     5, 300
ih		 	FLbox  	"'sustain time', 'release time' and 'release level'.          ", 	1,      5,     14,    490,    20,     5, 320
ih		 	FLbox  	"On with expon and expseg, values of zero are illegal with    ", 	1,      5,     14,    490,    20,     5, 340
ih		 	FLbox  	"expsegr - values close to zero can be used instead.          ", 	1,      5,     14,    490,    20,     5, 360
ih		 	FLbox  	"Where the user desires to modify envelope parameters in this ", 	1,      5,     14,    490,    20,     5, 380
ih		 	FLbox  	"example the code should for the linsegr line can be modified.", 	1,      5,     14,    490,    20,     5, 400
ih		 	FLbox  	"Some of the more developed MIDI examples in this catalogue   ", 	1,      5,     14,    490,    20,     5, 420
ih		 	FLbox  	"include MIDI envelopes where the user can modify the times   ", 	1,      5,     14,    490,    20,     5, 440
ih		 	FLbox  	"and values of the envelopes using FLTK widgets.              ", 	1,      5,     14,    490,    20,     5, 460
ih		 	FLbox  	"The maximum allowed value for the release time is 32768/kr.  ", 	1,      5,     14,    490,    20,     5, 480
ih		 	FLbox  	"Exceeding this limit will cause the envelope to not function ", 	1,      5,     14,    490,    20,     5, 500
ih		 	FLbox  	"properly.                                                    ", 	1,      5,     14,    490,    20,     5, 520
ih		 	FLbox  	"More information on this opcode is given in the actual code  ", 	1,      5,     14,    490,    20,     5, 540
ih		 	FLbox  	"of this example.                                             ", 	1,      5,     14,    490,    20,     5, 560
				FLpanel_end
				FLrun	;RUN THE FLTK WIDGET THREAD
;END OF FLTK INTERFACE CODE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

instr	1	; SIMPLE BEEP INSTRUMENT - CAN BE ACTIVATED VIA A CONNECTED MIDI KEYBOARD
	icps	cpsmidi
	iamp	ampmidi	0.2
	
	; linsegr IS USED TO CREATE AN ADSR STYLE ENVELOPE
	; ATTACK LEVEL IS HIGHER THAN SUSTAIN LEVEL GIVING THE NOTE A HARD ATTACK
	;               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,      icps,        1
		outs	asig, asig	;SEND AUDIO OUTPUT OF OSCILLATOR TO THE SPEAKERS
endin

</CsInstruments>

<CsScore>
f 1 0 1024 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>