aboutsummaryrefslogtreecommitdiff
path: root/site/udo/sequencing_scheduled.udo
blob: 17cb865d6a660bdb2cac2d0cfc7bd3b5e4142cec (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#ifndef UDO_SEQUENCING_SCHEDULING
#define UDO_SEQUENCING_SCHEDULING ##
/*
	Sequenced scheduling

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

#include "sequencing.udo"


/*
	Helper for sequenced scheduling calls
*/
opcode _seq_form_pfields, S, i[]
	ipfields[] xin
	Sdata = ""
	index = 0
	while (index < lenarray(ipfields)) do
		Sdata strcat Sdata, sprintf("%f ", ipfields[index])
		index += 1
	od
	xout Sdata
endop

/*
	schedule a scoreline for the next beat, bar or bargroup
*/
instr _seq_next
	imode = p4
	Scoreline = p5

	if ((imode == 0 && gkseq_beat == 1) || (imode == 1 && gkseq_bar_trig == 1) || (imode == 2 && gkseq_bargroup_trig == 1)) then
		scoreline Scoreline, 1
		turnoff
	endif
endin

opcode _seq_next, 0, iSi[]
	imode, Sinstrument, ipfields[] xin
	scoreline_i sprintf("i\"_seq_next\" %d 0 3600 \"i \\\"%s\\\" %s\"", imode, Sinstrument, _seq_form_pfields(ipfields))
endop


instr _seq_turnoff
	imode = p4
	instrnum = p5

	if ((imode == 0 && gkseq_beat == 1) || (imode == 1 && gkseq_bar_trig == 1) || (imode == 2 && gkseq_bargroup_trig == 1)) then
		turnoff2 instrnum, 4, 0
		turnoff
	endif
endin

opcode _seq_turnoff, 0, ii
	imode, instrnum xin
	event_i "i", "_seq_turnoff", 0, 600, imode, instrnum
endop


/*
	Schedule an instrument for the next beat
	
	beat_next Sinstrument, ipfields[]

	Sinstrument		instrument name
	ipfields[]		p fields
*/
opcode beat_next, 0, Si[]
	Sinstrument, ipfields[] xin
	_seq_next 0, Sinstrument, ipfields
endop



/*
	Schedule an instrument for the next bar
	
	bar_next Sinstrument, ipfields[]

	Sinstrument		instrument name
	ipfields[]		p fields
*/
opcode bar_next, 0, Si[]
	Sinstrument, ipfields[] xin
	_seq_next 1, Sinstrument, ipfields
endop



/*
	Schedule an instrument for the next bar group
	
	bargroup_next Sinstrument, ipfields[]

	Sinstrument		instrument name
	ipfields[]		p fields
*/
opcode bargroup_next, 0, Si[]
	Sinstrument, ipfields[] xin
	_seq_next 2, Sinstrument, ipfields
endop




/*
	Turn off an instrument at the next beat
	
	beat_next_turnoff instrnum

	instrnum	the instrument number; fractional if required
*/
opcode beat_next_turnoff, 0, i
	instrnum xin
	_seq_turnoff 0, instrnum
endop


/*
	Turn off an instrument at the next bar
	
	next_bar_turnoff instrnum

	instrnum	the instrument number; fractional if required
*/
opcode bar_next_turnoff, 0, i
	instrnum xin
	_seq_turnoff 1, instrnum
endop


/*
	Turn off an instrument at the next bar group
	
	next_bargroup_turnoff instrnum

	instrnum	the instrument number; fractional if required
*/
opcode bargroup_next_turnoff, 0, i
	instrnum xin
	_seq_turnoff 2, instrnum
endop




instr bargroup_lastof
	Scoreline = p4
	p3 = 600
	if (gkseq_bargroup == giseq_bargrouplength - 1) then
		scoreline Scoreline, 1
		turnoff
	endif
endin


opcode nextbeatxof, k, i
	ibeats xin
	kbeatnum init ibeats
	ktrig init 0
	kactive init 1
	if (gkseq_beat == 1 && kactive == 1) then
		if (kbeatnum == 0) then
			kactive = 0
			ktrig = 1
		else
			kbeatnum -= 1
		endif
	endif
	xout ktrig
endop


opcode lastbeatxof, k, ii
	itotalbeats, ibeats xin
	ktrig = 0
	kbeatnum init itotalbeats
	kactive init 1
	if (kactive == 1) then
		if (gkseq_beat == 1) then
			if (kbeatnum - 1 == ibeats) then
				ktrig = 1
				kactive = 0
			else
				kbeatnum -= 1
			endif
		endif
	endif

	xout ktrig
endop


opcode bar_lastbeatxof, k, i
	ibeats xin
	kactive init 1
	ktrig = 0
	
	kbeats init ibeats
	if (kactive == 1) then
		kreducing = (kbeats > giseq_barlength) ? 1 : 0
	
		if (kreducing == 1 && gkseq_beat == 1 && gkseq_barbeat == giseq_barlength - 1) then
			kbeats -= giseq_barlength
		endif

		if (kreducing == 0 && gkseq_beat == 1 && (gkseq_barbeat+kbeats) == giseq_barlength) then
			ktrig = 1
			kactive = 0
		endif
	endif

	xout ktrig
endop

opcode bargroup_lastbeatxof, k, io
	ibeats, icontinuous xin
	kactive init 1
	ktrig = 0
	
	ibargroupbeats = giseq_bargrouplength * giseq_barlength
	kbeats init ibeats
	if (kactive == 1) then
		kreducing = (kbeats > ibargroupbeats) ? 1 : 0

		if (kreducing == 1 && gkseq_bar_trig == 1 && gkseq_bargroup == giseq_bargrouplength - 1) then
			kbeats -= ibargroupbeats
		endif

		if (kreducing == 0 && gkseq_beat == 1 && (((gkseq_bargroup)*giseq_bargrouplength)+gkseq_barbeat+kbeats) == ibargroupbeats) then
			ktrig = 1

			if (icontinuous == 0) then
				kactive = 0
			else
				kbeats = ibeats
			endif
		endif

	endif

	xout ktrig
endop


opcode bar_lastbeatxof_launch, 0, iSi[]
	ibeats, Sinstrument, ipfields[] xin
	scoreline_i sprintf("i\"_bar_lastbeatxof\" 0 600 %d \"i \\\"%s\\\" %s\"", ibeats, Sinstrument, _seq_form_pfields(ipfields))
endop


opcode bargroup_lastbeatxof_launch, 0, iSi[]
	ibeats, Sinstrument, ipfields[] xin
	scoreline_i sprintf("i\"_bargroup_lastbeatxof\" 0 600 %d \"i \\\"%s\\\" %s\"", ibeats, Sinstrument, _seq_form_pfields(ipfields))
endop


; launch an instrument on the next relevant X beats before a bar start
instr _bar_lastbeatxof
	ibeats = p4
	Scoreline = p5
	p3 = 600

	if (bar_lastbeatxof(ibeats) == 1) then
		scoreline Scoreline, 1
		turnoff
	endif
endin


;  launch an instrument on the next relevant X beats before a bar group start
instr _bargroup_lastbeatxof
	ibeats = p4
	Scoreline = p5
	p3 = 600

	if (bargroup_lastbeatxof(ibeats) == 1) then
		scoreline Scoreline, 1
		turnoff
	endif
endin

#end