Csound Csound-dev Csound-tekno Search About

[Csnd] Table Utilities UDO

Date2008-08-17 20:10
Fromjoachim heintz
Subject[Csnd] Table Utilities UDO
I'd like to share two UDO's. One prints the content of a function  
table, the other writes it to a file. The number of values in a line  
can be specified (due to the limitation in sprintf up to 30). Both  
work at init-time.
I hope this can be useful for someone. Thanks to Oeyvind for his  
encouragement. Any suggestions or corrections are welcome.
Best -
	joachim




-n



gitab		ftgen	1, 0, -7, -2, 0, 1, 2, 3, 4, 5, 6
gisin		ftgen	2, 0, 128, 10, 1


	opcode TableDump, 0, iiiii
	;prints a function table with formatting options
	
;;INPUT:
;ifn 	= number of the function table
;istart 	= first index which is being print
;iend 	= last index which is being print (0 = up to the last index in  
the table)
;iprec 	= precision of floats in the printout
;ippr 	= number of parameters per row (maximum = 30)

ifn, istart, iend, iprec, ippr   xin

ilen		=	ftlen(ifn) ;length of the table (= last index + 1)
iend		=	(iend > ilen-1 || iend == 0 ? ilen-1 : iend) ;set end to last  
index if 0 or larger than last index
		prints	"Printing index %d to %d of ftable %d:%n", istart, iend,  
ifn ;saying hello
		if istart > iend igoto error ;break if the start index is larger  
than the end index

indx		=	istart
Sformat		sprintf	"%%.%d f\t", iprec
istep		=	0
		
newline:
Sdump		sprintf	"Index %d ff:\t", istep*ippr+istart
istep		=	istep + 1
icount		=	0

loop:
ival		tab_i	indx, ifn
Snew		sprintf	Sformat, ival
Sdump		strcat	Sdump, Snew
indx		=	indx + 1
icount		=	icount + 1
imod		=	icount % ippr
	if imod == 0 then
Sout		strcat	Sdump, "%n"
		prints	Sout
		goto	newline
	endif
	if indx <= iend igoto loop
Sout		strcat	Sdump, "%n"
		prints	Sout
		goto	end
		
error: 		prints	"Error! Index istart is larger than index iend.%n"
end:
	endop
	
	
instr 1
		TableDump	p4, p5, p6, p7, p8
endin



;i1	st	dur	ftab	start	end	prec	ppr
i1	0	.1	1	2	5	0	3	;printing index 2 to 5
i1 	+	.	2	0	0	3	10	;printing all
i1	+	.	1	5	2	0	3	;error







-n



gitab		ftgen	1, 0, -7, -2, 0, 1, 2, 3, 4, 5, 6
gisin		ftgen	2, 0, 128, 10, 1


	opcode TableToFile, 0, Siiiii
	;stores the content of a function table to a file
	
;;INPUT:
;Sfilnam	= filename in double quotes
;ifn 	= number of the function table
;istart 	= first index which is being written
;iend 	= last index which is being written (0 = up to the last index  
in the table)
;iprec 	= precision of floats
;ippr 	= number of parameters per line (maximum = 30)

Sfilnam, ifn, istart, iend, iprec, ippr   xin

ilen		=	ftlen(ifn) ;length of the table (= last index + 1)
iend		=	(iend > ilen-1 || iend == 0 ? ilen-1 : iend) ;set end to last  
index if 0 or larger than last index
Shello		sprintf	"Writing index %d to %d of ftable %d to file %s",  
istart, iend, ifn, Sfilnam ;saying hello
		puts	Shello, 1
		if istart > iend igoto error ;break if the start index is larger  
than the end index

indx		=	istart
Sformat		sprintf	"%%.%d f\t", iprec
istep		=	0
		
newline:
Sdump		=	""
istep		=	istep + 1
icount		=	0

loop:
ival		tab_i	indx, ifn
Snew		sprintf	Sformat, ival
Sdump		strcat	Sdump, Snew
indx		=	indx + 1
icount		=	icount + 1
imod		=	icount % ippr
	if imod == 0 then
Sout		strcat	Sdump, "%n"
		fprints	Sfilnam, Sout
		goto	newline
	endif
	if indx <= iend igoto loop
Sout		strcat	Sdump, "%n"
		fprints	Sfilnam, Sout
		goto	end
		
error: 		prints	"Error! Index istart is larger than index iend.%n"
end:
	endop
	
	
instr 1
Sfil		=		p4
		TableToFile	Sfil, p5, p6, p7, p8, p9
endin



;i1	st	dur	fil			ftab	start	end	prec	ppr
i1	0	.1	"test1.txt"	1	2	5	0	3	;saving index 2 to 5
i1 	+	.	"test2.txt"	2	0	0	3	10	;saving all
i1	+	.	"test3.txt"	1	5	2	0	3	;error