aboutsummaryrefslogtreecommitdiff
path: root/site/udo/interop.udo
blob: 84a3f00a250b4d954f0ac9362ea99a0d8f2bea2b (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
#ifndef UDO_INTEROP
#define UDO_INTEROP ##
/*
	SONICS container interoperation
	
	This file is part of the SONICS UDO collection by Richard Knight 2021
		License: GPL-2.0-or-later
		http://1bpm.net
*/



/*
	Send string to channel at i-time
	
	io_sendstring Schannel, Svalue

	Schannel	channel name
	Svalue		string to send
*/
#ifdef LOCALINTEROPINSTR
opcode io_sendstring, 0, SS
	Schannel, Svalue xin
	schedule("$LOCALINTEROPINSTR", 0, 1, Schannel, Svalue)
endop
#else
opcode io_sendstring, 0, SS
	Schannel, Svalue xin
	if (timeinstk() == -1) then
		outvalue Schannel, Svalue
	endif
endop
#end

/*
	Send value to channel at i-time
	
	io_send Schannel, ivalue

	Schannel	channel name
	ivalue		value to send
*/
opcode io_send, 0, Si
	Schannel, ivalue xin
	outvalue Schannel, ivalue
endop


/*
	Send value to channel at k-rate, if changed
	
	io_send Schannel, kvalue

	Schannel	channel name
	kvalue		value to send
*/
opcode io_send, 0, Sk
	Schannel, kvalue xin
	if (changed:k(kvalue) == 1) then
		outvalue Schannel, kvalue
	endif
endop


instr io_callback
	icbid = p4
	if (qnan(p5) == 1) then
		Sextra = sprintf(",%s", strget(p5))
	else
		Sextra = ""
	endif
	io_sendstring("callback", sprintf("{\"cbid\": %d%s}", icbid, Sextra))
	turnoff
endin


instr io_compile_channel
	icbid = p4
	Schannel = p5
	prints sprintf("including from channel %s \n\n", Schannel)
	Sdata chnget Schannel
	ires = compilestr(Sdata)
	Sres = (ires == -1) ? "false" : "true"
	io_sendstring("callback", sprintf("{\"cbid\": %d, \"success\": \"%s\"}", icbid, Sres))
	turnoff
endin

/*
	Stop instrument if playing; schedule instrument indefinitely if not playing
	at k-rate with optional trigger (trigger resets after one iteration)

	toggle_instrumentk instrnum [, ktrigger = 1]
	toggle_instrumentk Sinstrument [, ktrigger = 1]
	
	instrnum		instrument number
	Sinstrument		instrument name to toggle
	ktrigger		perform action when 1
*/
opcode toggle_instrumentk, 0, iP
	instrnum, ktrigger xin
	if (ktrigger == 1) then
		if (active:k(instrnum) > 0) then
			turnoff2 instrnum, 0, 1
		else
			schedulek instrnum, 0, -1
		endif
		ktrigger = 0
	endif
endop

; override for named instrument
opcode toggle_instrumentk, 0, SP
	Sinstrument, ktrigger xin
	toggle_instrumentk(nstrnum(Sinstrument), ktrigger)
endop


/*
	Stop instrument if playing; schedule instrument indefinitely if not playing, at init time

	toggle_instrument instrnum
	toggle_instrument Sinstrument
	
	instrnum		instrument number
	Sinstrument		instrument name to toggle
*/
opcode toggle_instrument, 0, i
	instrnum xin
	if (active:i(instrnum) > 0) then
		turnoff2 instrnum, 0, 1
	else
		schedule instrnum, 0, -1
	endif
endop

; override for named instrument
opcode toggle_instrument, 0, S
	Sinstrument xin
	toggle_instrument(nstrnum(Sinstrument))
endop



/*
	Stop instrument if playing; schedule instrument indefinitely if not playing, at init time
	For host invocation.

	p4		instrument number or name
*/
instr toggle_instrument
	if (qnan(p4) == 1) then
		Sinstrument = p4
		toggle_instrument(Sinstrument) 
	else
		instrnum = p4
		toggle_instrument(instrnum)
	endif
	turnoff
endin

instr turnonoff_instrument
	if (qnan(p4) == 1) then
		Sinstrument = strget(p4)
		instrnum = nstrnum(Sinstrument)
	else
		instrnum = p4
	endif
	istate = p5
	iruntime = (p6 == 0) ? 36000 : p6
	if (istate == 1) then
		schedule instrnum, 0, iruntime
	else
		turnoff2 instrnum, 0, 1
	endif
endin


#end