<CsoundSynthesizer>
<CsOptions>
-+P
</CsOptions>
<CsInstruments>

;*** BMSCAN
;*** shows the usage of bmscan, FLsetImage, FLsetOverlay and vectorial opcodes.

sr=44100
;kr=441
ksmps=128
nchnls=2
GLfps	15, .5

#include "OpenGL.h"

giTabWidths	ftgen	51,0,128,2,0
giTabHeights	ftgen	52,0,128,2,0
giTabTextures	ftgen	53,0,128,2,0

giPicture		init	1

giwidth,giheight,ibpp bmopen	"test.png", 1, 2 ;*** you can use another image
				    ;*** be sure that the directory path is correct

                       tabw_i	giwidth,	1, giTabWidths
		   tabw_i	giheight, 1,  giTabHeights
iwidth,iheight,ibpp bmopen	"Coloured Spices.png",2 ,2
                       tabw_i	iwidth,	2, giTabWidths
		   tabw_i	iheight, 2, giTabHeights
iwidth,iheight,ibpp bmopen	"nasca6.png",3 ,2
                       tabw_i	iwidth,	3, giTabWidths
		   tabw_i	iheight, 3, giTabHeights
iwidth,iheight,ibpp bmopen	"nasca7.png",4 ,2
                       tabw_i	iwidth,	4, giTabWidths
		   tabw_i	iheight, 4, giTabHeights
iwidth,iheight,ibpp bmopen	"nasca7b.png",5 ,2
                       tabw_i	iwidth,	5, giTabWidths
		   tabw_i	iheight, 5, giTabHeights
iwidth,iheight,ibpp bmopen	"nasca7c.png",6 ,2
                       tabw_i	iwidth,	6, giTabWidths
		   tabw_i	iheight, 6, giTabHeights
iwidth,iheight,ibpp bmopen	"nasca7d.png",7 ,2
                       tabw_i	iwidth,	7, giTabWidths
		   tabw_i	iheight, 7, giTabHeights
iwidth,iheight,ibpp bmopen	"nasca7e.png",8 ,2
                       tabw_i	iwidth,	8, giTabWidths
		   tabw_i	iheight, 8, giTabHeights
iwidth,iheight,ibpp bmopen	"QT2.png",9 ,2
                       tabw_i	iwidth,	9, giTabWidths
		   tabw_i	iheight, 9, giTabHeights
iwidth,iheight,ibpp bmopen	"spirali1.png",10 ,2
                       tabw_i	iwidth,	10, giTabWidths
		   tabw_i	iheight, 10, giTabHeights
iwidth,iheight,ibpp bmopen	"spirali2.png",11 ,2
                       tabw_i	iwidth,	11, giTabWidths
		   tabw_i	iheight, 11, giTabHeights
iwidth,iheight,ibpp bmopen	"test2.png",12 ,2
                       tabw_i	iwidth,	12, giTabWidths
		   tabw_i	iheight, 12, giTabHeights
iwidth,iheight,ibpp bmopen	"test3.png",13 ,2
                       tabw_i	iwidth,	13, giTabWidths
		   tabw_i	iheight, 13, giTabHeights
iwidth,iheight,ibpp bmopen	"test4.png",14 ,2
                       tabw_i	iwidth,	14, giTabWidths
		   tabw_i	iheight, 14, giTabHeights
iwidth,iheight,ibpp bmopen	"test5.png",15 ,2
                       tabw_i	iwidth,	15, giTabWidths
		   tabw_i	iheight, 15, giTabHeights

	FLpanel	"Example of BMSCAN and VECTORIAL OPCODES with several different images",800,560
	FLscroll  800,560,0,0
;gih2	FLbox	" ",3,1,20,150,150,5,10
;	FLsetAlign 3,gih2
;	FLsetBox	1,gih2
;	FLsetImage 	1,gih2,1  ;*** display image

	GLpanel	"OpenGL panel", 640, 480
	GLpanel_end
	
;kout, ihandle	FLknob	"label", imin, imax, iexp, itype, idisp, iwidth, ix, iy

gkscanrate, ih1	FLknob	"scan rate", 1/256,3,-1,1,-1,60,730,10
gkfreq,  	  ih2	FLknob	"pitch",     8,512,-1,1,-1,60,730,90
gkrange, 	  ih3	FLknob	"scale pitch range",     .08,16,-1,1,-1,60,730,170
gkport, 	  ih4	FLknob	"portamento amount",     .0001,.5,-1,1,-1,60,730,280
gkVol, 	  ih9	FLknob	"output level",     .01,16,-1,1,-1,60,660,280

gkPict,ih5	FLbutBank	1, 5,3, 100, 60,   680, 380,            0, 5, 0,.2
ih7		FLbox	"select image",1,1,14,100,20,680,440
gkPart,gih7	FLtext	"number of partials",  10, 500, 1, 1, 60, 20, 660, 10
gkSine,ih8	FLbutBank	1,4,8,80,140,650, 100,                   -1,0,0
ih		FLbox	"audio table", 1,1,14,40,40,650,240
gkOnOff,ih6	FLbutton	"@UpArrow", 0,1,12,100,40,680,470,      0,1,0,3600
gk,gih10		FLtext	"X",0, 5000, 0, 3, 50, 20, 10, 500
gk,gih11		FLtext	"Y",0, 5000, 0, 3, 50, 20, 80,500

	FLscroll_end
	FLpanel_end

FLsetVal_i 1/10, ih1
FLsetVal_i 128, ih2
FLsetVal_i 2, ih3
FLsetVal_i .0001, ih4
FLsetVal_i 240, gih7
FLsetVal_i 1, ih9
FLsetVal_i giwidth, gih10
FLsetVal_i giheight,gih11

	FLrun

iTex1	GLloadTexture "test.png", $PNG_ALPHA,$GL_REPEAT, $PNG_NOMIPMAPS, $GL_NEAREST
	
iTex2	GLloadTexture "Coloured Spices.png", $PNG_ALPHA,$GL_REPEAT, $PNG_NOMIPMAPS, $GL_NEAREST
iTex3	GLloadTexture "nasca6.png", $PNG_ALPHA,$GL_REPEAT, $PNG_NOMIPMAPS, $GL_NEAREST
iTex4	GLloadTexture "nasca7.png", $PNG_ALPHA,$GL_REPEAT, $PNG_NOMIPMAPS, $GL_NEAREST
iTex5	GLloadTexture "nasca7b.png", $PNG_ALPHA,$GL_REPEAT, $PNG_NOMIPMAPS, $GL_NEAREST
iTex6	GLloadTexture "nasca7c.png", $PNG_ALPHA,$GL_REPEAT, $PNG_NOMIPMAPS, $GL_NEAREST
iTex7	GLloadTexture "nasca7d.png", $PNG_ALPHA,$GL_REPEAT, $PNG_NOMIPMAPS, $GL_NEAREST
iTex8	GLloadTexture "nasca7e.png", $PNG_ALPHA,$GL_REPEAT, $PNG_NOMIPMAPS, $GL_NEAREST
iTex9	GLloadTexture "QT2.png", $PNG_ALPHA,$GL_REPEAT, $PNG_NOMIPMAPS, $GL_NEAREST
iTex10	GLloadTexture "spirali1.png", $PNG_ALPHA,$GL_REPEAT, $PNG_NOMIPMAPS, $GL_NEAREST
iTex11	GLloadTexture "spirali2.png", $PNG_ALPHA,$GL_REPEAT, $PNG_NOMIPMAPS, $GL_NEAREST
iTex12	GLloadTexture "test2.png", $PNG_ALPHA,$GL_REPEAT, $PNG_NOMIPMAPS, $GL_NEAREST
iTex13	GLloadTexture "test3.png", $PNG_ALPHA,$GL_REPEAT, $PNG_NOMIPMAPS, $GL_NEAREST
iTex14	GLloadTexture "test4.png", $PNG_ALPHA,$GL_REPEAT, $PNG_NOMIPMAPS, $GL_NEAREST
iTex15	GLloadTexture "test5.png", $PNG_ALPHA,$GL_REPEAT, $PNG_NOMIPMAPS, $GL_NEAREST

GLinsert_i $GL_INIT
	glMatrixMode	$GL_PROJECTION 
	glLoadIdentity
	glOrtho 		-1, 1, -1, 1, -20, 20
	glMatrixMode	$GL_MODELVIEW 
	glLoadIdentity
	glClearColor	0,0,0,1
	glEnable	$GL_TEXTURE_2D	
GLinsert_i $GL_NOT_VALID

	glClear	$GL_COLOR_BUFFER_BIT + $GL_DEPTH_BUFFER_BIT 
GLinsert_i 1



;gkPart	init	240 ;*** number of partials. 
		    ;*** reduce this number if your 
		    ;*** computer is not fast enough
		    ;*** must be <= to the vertical dimension of the image
		    ;*** otherwise Csound will crash

giTabFreq1 ftgen	1,0,512,-7, 1,i(gkPart),5,128,5 ;*** table containing frequency ratios (exponential)
giTabFreq2 ftgen	2,0,512,-5, 1,i(gkPart),5,128,5 ;*** table containing frequency ratios (linear)
giTaScale ftgen	60,0,512,5, 1,i(gkPart),.08,128,.08 ;*** scale the amplutude of partials

giTabRed	ftgen	4,0,512,2, 0 ;*** table receiving red channel
giTaGreen	ftgen	5,0,512,2, 0 ;*** table receiving green channel
giTaBlue	ftgen	6,0,512,2, 0 ;*** table receiving blue channel

giTemp	ftgen	61,0,512,2,0  ;*** temporary buffer table

isine	ftgen	12,0,2048,  10,1  ;*** audio function
isine	ftgen	13,0,2048,  10,1,0,1,0,0,0,.3  ;*** audio function
isine	ftgen	14,0,2048,  10,3,0,0,0,.6,0,0,0,.4,0,0,0,0,0,0,0,.1,0,0,0,0,0,0,.05  ;*** audio function
isine	ftgen	15,0,2048,  10,0,0,0,0,0,0,1,1,1,1,1  ;*** audio function

isine	ftgen	16,0,2048,  10,1,0,.1,0,.01,.008,.007,.006,.005,.004,.003,.002,.001  ;*** audio function
isine	ftgen	17,0,2048,  10,0,0,0,1,1,1  ;*** audio function
isine	ftgen	18,0,2048,  10,20,0,0,.6,0,0,0,.4,0,0,0,0,0,0,0,.1,0,0,0,0,0,0,.05  ;*** audio function
isine	ftgen	19,0,2048,  10,0,20,0,0,0,0,1,1,1,1,1  ;*** audio function

isine	ftgen	20,0,2048,  10,10,0,1,0,1,0,.3,0,.1,0,.05  ;*** audio function
isine	ftgen	21,0,2048,  10,0,1,1  ;*** audio function
isine	ftgen	22,0,2048,  10,0,0,3,1;*** audio function
isine	ftgen	23,0,2048,  10,0,0,0,3,1 ;*** audio function

isine	ftgen	24,0,2048,  10,20,1,10,1,4,1,2,1,0,1  ;*** audio function
isine	ftgen	25,0,2048,  10,0,0,0,1,1  ;*** audio function
isine	ftgen	26,0,2048,  10,50,0,0,0,.6,0,0,0,.4,0,0,0,0,0,0,0,.1,0,0,2,0,0,1,0,0,.05, 1  ;*** audio function
isine	ftgen	27,0,2048,  10,0,0,0,0,0,0,1,1,1,1,1  ;*** audio function

isine	ftgen	28,0,2048,  10,0,0,0,0,0,0,5,1 ;*** audio function
isine	ftgen	29,0,2048,  10,0,0,0,0,10,1,5,1  ;*** audio function
isine	ftgen	30,0,2048,  10,50,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1;*** audio function
isine	ftgen	31,0,2048,  10,120,1,2,3,4,5,6,7,8,9,10,11,12,13  ;*** audio function

isine	ftgen	32,0,2048,  10,0,0,50,1,1,1,1  ;*** audio function
isine	ftgen	33,0,2048,  10,0,0,0,50,1,1,1  ;*** audio function
isine	ftgen	34,0,2048,  10,0,0,0,0,50,1,1,1  ;*** audio function
isine	ftgen	35,0,2048,  10,0,50,0,0,1,1,1,1,1,1,1  ;*** audio function

isine	ftgen	36,0,2048,  10,500,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1  ;*** audio function
isine	ftgen	37,0,2048,  10,1,2  ;*** audio function
isine	ftgen	38,0,2048,  10,5,0,0,0,0,0,0,1;*** audio function
isine	ftgen	39,0,2048,  10,40,0,0,0,0,0,0,0,0,0,0,0,0,0,1  ;*** audio function

isine	ftgen	40,0,2048,  10,0,0,0,10,2,1  ;*** audio function
isine	ftgen	41,0,2048,  10,0,0,0,0,0,0,0,2,0,10,1  ;*** audio function
isine	ftgen	42,0,2048,  10,500,20,10,1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1;*** audio function
isine	ftgen	43,0,2048,  10,0,0,0,0,0,0,100,1,1,1,1  ;*** audio function


gkscanrate	init	.05

GLwaitInitStage
giTabTextures	ftgen	53,0,128, -2,0, 	iTex1, iTex2, iTex3, iTex4, iTex5, iTex6, iTex7, iTex8,\
					iTex9, iTex10, iTex11, iTex12, iTex13, iTex14, iTex15
giTex	init	iTex1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
	instr	1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
gkphs	init	0
if i(gkOnOff) != 0 goto end
gkphs	phasor	gkscanrate         
kphs	=	gkphs*giwidth ;*** point of table scanning
;kphs	vdelayk	kphs1, .5, 1
;	call	5, 0, .1
if p3==0 goto then ;*** test if MIDI or non-MIDI activated
		turnoffk	gkOnOff   ;*** score activated
	iamp	=	8   	;*** amplitude
	kfreq	=	gkfreq  	;*** frequency
	ktrig	metro	10 	;*** the cursor of the image is updated 10 times per second
	;***	MANUAL		ktrig, kx,ky,kwidth,kheight,ihandle;
	;	FLsetOverlay	ktrig,kphs,2,2,giheight,gih2 	;*** display moving cursor of the image
	goto endif
then:
	iamp	ampmidi	10
	ifreq	notnum
	kfreq	=	gkfreq * semitone(ifreq-60)
	
	;kscanrate init	1/120
	;kscanrate ctrl7	1,7,1/120,1
endif:

print giheight, giwidth


;iportament=  .0001	    ;*** portamento time to smooth transients (you can increase this value for a reverb-like effect)

;***	IMAGE SCANNING OPCODE
;***	MANUAL	kphs, ihorLines,      ifn,  istartLine, ifnR, ifnG, ifnB;
kSineOld	init	12

reinit_here:
gkphs	init	0
giElems	init	i(gkPart)
	bmscani	kphs, giheight/giElems, giPicture,  0,    giTabRed,giTaGreen,giTaBlue
; print giheight, giElems, giheight/giElems

;***      VECTORIAL OPCODES PROVIDE TRANSFORMATIONS OF IMAGE VECTORS	
	vaddv	giTabRed,giTaGreen, giElems ;*** mix Green amplitudes to Red channel
	vaddv	giTabRed,giTaBlue,  giElems ;*** mix Blue amplitudes to Red channel
	vmultv	giTabRed,giTaScale, giElems ;*** scale the amplutude of high partials
	vport	giTabRed,gkport,    giElems ;*** add portamento to smooth transients (you can reduce or increase this value)
	vcopy	giTemp,giTabFreq1,  giElems ;*** copy the frequency table to a temporary table
	vpow	giTemp,gkrange,     giElems ;*** expand frequency range

kenv	linsegr	0,  1   ,iamp,1,iamp,.1,0

if gkSine != kSineOld || gkPart !=giElems kgoto then2
	kgoto endif2
then2:	
	schedk    1, 0, 5, 0, 0
	reinit 	reinit_here
endif2:		; reinit pass starts here

a1	adsynt2	kenv,kfreq/gkrange,i(gkSine)+12,giTemp,giTabRed,giElems,1
	rireturn	; reinit pass ends here
kSineOld	=	gkSine
a1	tone	a1,3000
a1	delay	a1*gkVol, .5

	outs	a1, a1
end:
	endin


;////////////////////////
	instr	5
;////////////////////////
gkphs	init	0
;if i(gkOnOff) != 0 goto end
giPicture  init	i(gkPict)+1
giTex	tab_i	giPicture, giTabTextures
giwidth	tab_i	giPicture, giTabWidths 
giheight	tab_i	giPicture, giTabHeights 
FLsetVal_i giwidth, gih10
FLsetVal_i giheight,gih11

print i(gkPart), giwidth, giheight

giTabFreq1 ftgen	1,0,512,-7, 1,i(gkPart),5,128,5 ;*** table containing frequency ratios (exponential)
giTabFreq2 ftgen	2,0,512,-5, 1,i(gkPart),5,128,5 ;*** table containing frequency ratios (linear)
giTaScale ftgen	32,0,512,5, 1,i(gkPart),.08,128,.08 ;*** scale the amplutude of partials
ktrig	metro	30
;	FLsetOverlay	ktrig,0,0,0,0,gih2 	;*** display moving cursor of the image
;	FLsetImage 	giPicture,gih2,1    ;*** display image
end:
	endin



;////////////////////////
	instr	10
;////////////////////////
;////// OPENGL BEGIN
	glLoadIdentity
	glBindTexture	giTex
	glColor	1,1,1,1
;	glScale 	1,1/giratio,1
	glScale 	1,giheight/giwidth,1
	GLtexSquare 1,  0,1,  1,1,   1,0,  0,0
glDisable	$GL_TEXTURE_2D
	glutCube 	2,0
	glLoadIdentity
	glDisable	$GL_TEXTURE_2D
	tphs = t(gkphs) * 2 - 1
	glTranslate tphs,0,0	
	glScale 	1/150, 3,1
	
	glColor	1,0,0,1
	glutCube 	1,0	
	glEnable  $GL_TEXTURE_2D
	GLinsert	1.1
;////// OPENGL END

	endin


</CsInstruments>
<CsScore>
;i100 0 3600
;i1 0 3600
i10 0 3600
f0 3600

</CsScore>
</CsoundSynthesizer>
