aboutsummaryrefslogtreecommitdiff
path: root/PureData/PureData.cs
blob: 1d01f75409b68c2cc103634fd7f8bdd4978981be (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
using System;
using System.Runtime.CompilerServices; // for extern import
using System.Runtime.InteropServices; // for structures

namespace PureData
{
    // PD core functions
    public unsafe class Internal 
    {
        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        internal extern static void *SymGen(string sym);        

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        internal extern static string SymEval(void *sym);        

        // --------------------------------------------------------------------------

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        internal extern static void AddInlet(void *obj,Symbol sel,Symbol to_sel);

        // map to data member
        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        internal extern static void AddInlet(void *obj,ref float f); 

        // map to data member
        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        internal extern static void AddInlet(void *obj,ref Symbol s);

        // map to data member
//        [MethodImplAttribute (MethodImplOptions.InternalCall)]
//        internal extern static void AddInlet(void *obj,ref Pointer f);

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        internal extern static void AddInlet(void *obj,Symbol type); // create proxy inlet (typed)

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        internal extern static void AddInlet(void *obj); // create proxy inlet (anything)

        // --------------------------------------------------------------------------

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        internal extern static void AddOutlet(void *obj,Symbol type);

        // --------------------------------------------------------------------------

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        internal extern static void Outlet(void *obj,int n);

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        internal extern static void Outlet(void *obj,int n,float f);

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        internal extern static void Outlet(void *obj,int n,Symbol s);

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        internal extern static void Outlet(void *obj,int n,Pointer p);

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        internal extern static void Outlet(void *obj,int n,Atom a);

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        internal extern static void Outlet(void *obj,int n,Symbol s,AtomList l);

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        internal extern static void Outlet(void *obj,int n,Symbol s,Atom[] l);

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        internal extern static void Outlet(void *obj,int n,object o);

        // --------------------------------------------------------------------------

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        internal extern static void Bind(void *obj,Symbol dst);

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        internal extern static void Unbind(void *obj,Symbol dst);
    }
    
    // This is the base class for a PD/CLR external
    public unsafe abstract class External
    {
        // PD object pointer
        private readonly void *ptr;

        // to be returned by Setup function
        protected enum ClassType { Default = 0,PD = 1,GObj = 2,Patchable = 3,NoInlet = 8 }

        // --------------------------------------------------------------------------

        protected readonly static Symbol _ = new Symbol("");
        protected readonly static Symbol _bang = new Symbol("bang");
        protected readonly static Symbol _float = new Symbol("float");
        protected readonly static Symbol _symbol = new Symbol("symbol");
        protected readonly static Symbol _pointer = new Symbol("pointer");
        protected readonly static Symbol _list = new Symbol("list");
        protected readonly static Symbol _anything = new Symbol("anything");

        // --------------------------------------------------------------------------

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        protected extern static void Post(string message);        

        protected static void Post(string format,object arg0) { Post(String.Format(format,arg0)); }
        protected static void Post(string format,object arg0,object arg1) { Post(String.Format(format,arg0,arg1)); }
        protected static void Post(string format,object arg0,object arg1,object arg2) { Post(String.Format(format,arg0,arg1,arg2)); }
        protected static void Post(string format,params object[] args) { Post(String.Format(format,args)); }

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        protected extern static void PostError(string message);        

        protected static void PostError(string format,object arg0) { PostError(String.Format(format,arg0)); }
        protected static void PostError(string format,object arg0,object arg1) { PostError(String.Format(format,arg0,arg1)); }
        protected static void PostError(string format,object arg0,object arg1,object arg2) { PostError(String.Format(format,arg0,arg1,arg2)); }
        protected static void PostError(string format,params object[] args) { PostError(String.Format(format,args)); }

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        protected extern static void PostVerbose(int lvl,string message);        

        protected static void PostVerbose(int lvl,string format,object arg0) { PostVerbose(lvl,String.Format(format,arg0)); }
        protected static void PostVerbose(int lvl,string format,object arg0,object arg1) { PostVerbose(lvl,String.Format(format,arg0,arg1)); }
        protected static void PostVerbose(int lvl,string format,object arg0,object arg1,object arg2) { PostVerbose(lvl,String.Format(format,arg0,arg1,arg2)); }
        protected static void PostVerbose(int lvl,string format,params object[] args) { PostVerbose(lvl,String.Format(format,args)); }

        // --------------------------------------------------------------------------

        protected delegate void Method();
        protected delegate void MethodFloat(float f);
        protected delegate void MethodSymbol(Symbol s);
        protected delegate void MethodPointer(Pointer p);
        protected delegate void MethodList(AtomList lst);
        protected delegate void MethodAnything(int inlet,Symbol tag,AtomList lst);
        protected delegate void MethodObject(int inlet,object o);

        // --------------------------------------------------------------------------

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        protected extern static void AddMethod(int inlet,Method m);

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        protected extern static void AddMethod(int inlet,MethodFloat m);

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        protected extern static void AddMethod(int inlet,MethodSymbol m);

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        protected extern static void AddMethod(int inlet,MethodPointer m);

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        protected extern static void AddMethod(int inlet,MethodList m);

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        protected extern static void AddMethod(int inlet,MethodAnything m);

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        protected extern static void AddMethod(int inlet,Symbol sel,Method m);

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        protected extern static void AddMethod(int inlet,Symbol sel,MethodFloat m);

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        protected extern static void AddMethod(int inlet,Symbol sel,MethodSymbol m);

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        protected extern static void AddMethod(int inlet,Symbol sel,MethodPointer m);

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        protected extern static void AddMethod(int inlet,Symbol sel,MethodList m);

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        protected extern static void AddMethod(int inlet,Symbol sel,MethodAnything m);

        protected static void AddMethod(int inlet,string sel,Method m) { AddMethod(inlet,new Symbol(sel),m); }
        protected static void AddMethod(int inlet,string sel,MethodFloat m) { AddMethod(inlet,new Symbol(sel),m); }
        protected static void AddMethod(int inlet,string sel,MethodSymbol m) { AddMethod(inlet,new Symbol(sel),m); }
        protected static void AddMethod(int inlet,string sel,MethodPointer m) { AddMethod(inlet,new Symbol(sel),m); }
        protected static void AddMethod(int inlet,string sel,MethodList m) { AddMethod(inlet,new Symbol(sel),m); }
        protected static void AddMethod(int inlet,string sel,MethodAnything m) { AddMethod(inlet,new Symbol(sel),m); }

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        protected extern static void AddMethod(int inlet,MethodObject m);

        // --------------------------------------------------------------------------

        protected void AddInlet(ref float f) { Internal.AddInlet(ptr,ref f); } // map to data member
        protected void AddInlet(ref Symbol s) { Internal.AddInlet(ptr,ref s); } // map to data member
//        protected void AddInlet(ref Pointer p) { Internal.AddInlet(ptr,ref p); } // map to data member
//        protected void AddInlet(Symbol type) { Internal.AddInlet(ptr,type); } // create typed inlet
        protected void AddInlet() { Internal.AddInlet(ptr); } // create inlet responding to any message
        protected void AddInlet(Symbol sel,Symbol to_sel) { Internal.AddInlet(ptr,sel,to_sel); } // redirect messages to defined selector

        // --------------------------------------------------------------------------

        protected void AddOutlet(Symbol type) { Internal.AddOutlet(ptr,type); }

        protected void AddOutletBang() { AddOutlet(_bang); }
        protected void AddOutletFloat() { AddOutlet(_float); }
        protected void AddOutletSymbol() { AddOutlet(_symbol); }
        protected void AddOutletPointer() { AddOutlet(_pointer); }
        protected void AddOutletList() { AddOutlet(_list); }
        protected void AddOutletAnything() { AddOutlet(_anything); }

        // --------------------------------------------------------------------------

        protected void Outlet(int n) { Internal.Outlet(ptr,n); }
        protected void Outlet(int n,float f) { Internal.Outlet(ptr,n,f); }
        protected void Outlet(int n,Symbol s) { Internal.Outlet(ptr,n,s); }
        protected void Outlet(int n,Pointer p) { Internal.Outlet(ptr,n,p); }
        protected void Outlet(int n,Atom a) { Internal.Outlet(ptr,n,a); }
        protected void Outlet(int n,AtomList l) { Internal.Outlet(ptr,n,_list,l); }
        protected void Outlet(int n,Atom[] l) { Internal.Outlet(ptr,n,_list,l); }
        protected void Outlet(int n,Symbol s,AtomList l) { Internal.Outlet(ptr,n,s,l); }
        protected void Outlet(int n,Symbol s,Atom[] l) { Internal.Outlet(ptr,n,s,l); }

        protected void OutletEx(int n,object o) { Internal.Outlet(ptr,n,o); }

        // --------------------------------------------------------------------------

        // bind to symbol
        protected void Bind(Symbol sym) { Internal.Bind(ptr,sym); }
        protected void Unbind(Symbol sym) { Internal.Unbind(ptr,sym); }

        // send to receiver symbol
        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        protected extern static void Send(Symbol sym,Atom a);

        protected static void Send(Symbol sym) { Send(sym,_bang,null); }
        protected static void Send(Symbol sym,float f) { Send(sym,new Atom(f)); }
        protected static void Send(Symbol sym,Symbol s) { Send(sym,new Atom(s)); }
        protected static void Send(Symbol sym,Pointer p) { Send(sym,new Atom(p)); }
        protected static void Send(Symbol sym,AtomList l) { Send(sym,_list,l); }
        protected static void Send(Symbol sym,Atom[] l) { Send(sym,_list,l); }

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        protected extern static void Send(Symbol sym,Symbol s,AtomList l);

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        protected extern static void Send(Symbol sym,Symbol s,Atom[] l);

        [MethodImplAttribute (MethodImplOptions.InternalCall)]
        protected extern static void SendEx(Symbol sym,object o);
    }
}