aboutsummaryrefslogtreecommitdiff
path: root/sonics/uniqueid.udo
blob: 84799ec4b5d40150819cd6e6fe83bec5c963d8d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#ifndef UDO_UNIQUEID
#define UDO_UNIQUEID ##
/*
	Unique ID assignments
	Slim excerpt for Partial Emergence

	This file is part of the SONICS UDO collection by Richard Knight 2021, 2022
		License: GPL-2.0-or-later
		http://1bpm.net
*/


; globals for internal use
giUniqueID = 0
giUniqueFrac = 0


/*
	Get a unique integer ID

	id uniqueid

	id	the ID
*/
opcode uniqueid, i, 0
	id = giUniqueID
	giUniqueID += 1
	xout id
endop


/*
	Get a unique decimal/fractional ID

	id uniquefrac

	id the ID
*/
opcode uniquefrac, i, 0
	id = giUniqueFrac
	giUniqueFrac += 0.0000001 ; smallest for 32bit
	if (giUniqueFrac >= 1) then
		giUniqueFrac = 0
	endif
	xout id
endop


/*
	Get an array of unique fractional instrument numbers given a base instrument number

	instrs[] uniqueinstrnums instrnum, inum
	instrs[] uniqueinstrnums Sinstr, inum

	instrs[]	array of unique fractional numbers for the instrument number instrnum
	Sinstr		the base instrument name
	instrnum	the base instrument number
	inum		how many references to generate
*/
opcode uniqueinstrnums, i[], ii
	instrnum, inum xin
	instrs[] init inum
	index = 0
	while (index < inum) do
		instrs[index] = instrnum + uniquefrac()
		index += 1
	od
	xout instrs
endop

; overload for named instrument
opcode uniqueinstrnums, i[], Si
	Sinstr, inum xin
	instrs[] uniqueinstrnums nstrnum(Sinstr), inum
	xout instrs
endop

#endif