;Written by Iain McCurdy

<CsoundSynthesizer>

<CsOptions>
-idevaudio -odevaudio -b400
</CsOptions>

<CsInstruments>

sr 		= 	44100	;SAMPLE RATE
ksmps 		= 	1	;NUMBER OF AUDIO SAMPLES IN EACH CONTROL CYCLE
nchnls 		= 	2	;NUMBER OF CHANNELS (2=STEREO)

;FLTK INTERFACE CODE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
FLcolor	255, 255, 255, 0, 0, 0	;SETUP BASIC COLOURS
;		LABEL                  | WIDTH | HEIGHT | X | Y
	FLpanel	"Bit Depth Reduction",    500,    170,    0,  0

;SWITCHES  	                                  	ON | OFF | TYPE | WIDTH | HEIGHT | X | Y | OPCODE | INS | STARTTIM | DUR
gkOnOff,ihOnOff	FLbutton	"On/Off",		1,    0,    22,    100,     25,    5,  5,     0,     1,      0,      3600
FLsetColor2	255, 255, 50, ihOnOff	;SET SECONDARY COLOUR TO YELLOW

;VALUE DISPLAY BOXES		LABEL  | WIDTH | HEIGHT | X | Y
idbitdepth		FLvalue	" ",      70,      20,    5,  95
idgain			FLvalue	" ",      70,      20,    5, 145

;SLIDERS				            	MIN | MAX | EXP | TYPE |   DISP    | WIDTH | HEIGHT | X  | Y
gkbitdepth, ihbitdepth	FLslider	"Bit Depth",  	1,    16,    0,   23,   idbitdepth,   490,     25,    5,   70
gkgain,ihgain		FLslider 	"Output Gain",	0,     2,    0,   23,   idgain,       490,     25,    5,  120

;GENERAL_TEXT_SETTINGS			SIZE | FONT |  ALIGN | RED | GREEN | BLUE
			FLlabel		13,      1,      1,    255,   255,   255		;NUMBERS MADE INVISIBLE

;			FLBUTBANK 	TYPE | NUMX | NUMY | WIDTH | HEIGHT | X | Y | OPCODE
gkinput, ihinput	FLbutBank	4 ,     1,     3,     18,      60,   150, 0,   -1

;GENERAL_TEXT_SETTINGS			SIZE | FONT |  ALIGN | RED | GREEN | BLUE
			FLlabel		13,      5,      4,     0,     0,     0			;LABELS MADE VISIBLE AGAIN

;TEXT BOXES						TYPE | FONT | SIZE | WIDTH | HEIGHT | X |  Y
ih		 	FLbox  	"Voice     ", 		1,       5,    12,    70,      18,   170,   0
ih		 	FLbox  	"Drums     ", 		1,       5,    12,    70,      18,   170,  20
ih		 	FLbox  	"Live Input", 		1,       5,    12,    70,      18,   170,  40

;SET INITIAL VALUES FOR SLIDERS |VALUE | HANDLE
		FLsetVal_i	   16, 	ihbitdepth
		FLsetVal_i	   1,  	ihgain
		FLsetVal_i	   1,  	ihinput

		FLpanel_end	; END OF PANEL CONTENTS

;INSTRUCTIONS AND INFO PANEL                 WIDTH | HEIGHT | X | Y
				FLpanel	" ", 515,      140,  512, 0
;TEXT BOXES												TYPE | FONT | SIZE | WIDTH | HEIGHT | X | Y
ih		 	FLbox  	"              Artificial Bit Depth Reduction                 ", 	1,      5,     14,    490,    15,     5,  0
ih		 	FLbox  	"-------------------------------------------------------------", 	1,      5,     14,    490,    15,     5,  20
ih		 	FLbox  	"This example uses a mathematical quantizing procedure upon an", 	1,      5,     14,    490,    15,     5,  40
ih		 	FLbox  	"audio signal to create the effect of lower bit depth sample  ", 	1,      5,     14,    490,    15,     5,  60
ih		 	FLbox  	"storage. The example is designed to work with 16 bit samples ", 	1,      5,     14,    490,    15,     5,  80
ih		 	FLbox  	"but could easily be adapted to work with samples of other    ", 	1,      5,     14,    490,    15,     5, 100
ih		 	FLbox  	"depths.                                                      ", 	1,      5,     14,    490,    15,     5, 120
				FLpanel_end

				FLrun	;RUN THE FLTK WIDGET THREAD
;END OF FLTK INTERFACE CODE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
		
instr 1
	if	gkOnOff=0	then	;IF ON/OFF SWITCH IS OFF...
		turnoff			;TURN THIS INSTRUMENT OFF
	endif				;END OF CONDITIONAL BRANCHING
	if		gkinput=0	then					;CHECK STATUS OF gkinput (FLTK BUTTON BANK)
		asig		diskin2		"AndItsAll.wav", 1, 0, 1	;READ IN SOUND FILE FROM DISK
	elseif		gkinput=1	then					;CHECK STATUS OF gkinput (FLTK BUTTON BANK)
		asig		diskin2		"808loopmono.wav", 1, 0, 1	;READ IN SOUND FILE FROM DISK
	elseif		gkinput=2	then					;CHECK STATUS OF gkinput (FLTK BUTTON BANK)
		asig	inch		1					;READ THE COMPUTER'S LIVE AUDIO INPUT
	endif									;END OF if... BRANCHING
	kvalues		pow		2, gkbitdepth	;RAISES 2 TO THE POWER OF kbitdepth. THE OUTPUT VALUE REPRESENTS THE NUMBER OF POSSIBLE VALUES AT THAT PARTICULAR BIT DEPTH
	k16bit		pow		2, 16		;RAISES 2 TO THE POWER OF 16
	ksig		downsamp	asig		;CREATE A K-RATE VERSION OF asig
	ksig		=		int(ksig * (kvalues/k16bit))	;QUANTISE ksig-
	ksig		=		ksig * (k16bit/kvalues)		;-AND RESCALE IT TO MATCH THE ORIGINAL AMPLITUDE
	asig		interp		ksig		;CREATE AN AUDIO-RATE VERSION OF THE QUANTISED SIGNAL
			outs		asig*gkgain, asig*gkgain
endin
		
</CsInstruments>

<CsScore>
f 0 3600
</CsScore>

</CsoundSynthesizer>