Debian Codegraph @

src/core/chuck_globals.h

raw symbols SCIP
1 /*----------------------------------------------------------------------------
2 ChucK Strongly-timed Audio Programming Language
3 Compiler, Virtual Machine, and Synthesis Engine
4
5 Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.stanford.edu/
7 http://chuck.cs.princeton.edu/
8
9 This program is free software; you can redistribute it and/or modify
10 it under the dual-license terms of EITHER the MIT License OR the GNU
11 General Public License (the latter as published by the Free Software
12 Foundation; either version 2 of the License or, at your option, any
13 later version).
14
15 This program is distributed in the hope that it will be useful and/or
16 interesting, but WITHOUT ANY WARRANTY; without even the implied warranty
17 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 MIT Licence and/or the GNU General Public License for details.
19
20 You should have received a copy of the MIT License and the GNU General
21 Public License (GPL) along with this program; a copy of the GPL can also
22 be obtained by writing to the Free Software Foundation, Inc., 59 Temple
23 Place, Suite 330, Boston, MA 02111-1307 U.S.A.
24 -----------------------------------------------------------------------------*/
25
26 //-----------------------------------------------------------------------------
27 // file: chuck_globals.h
28 // desc: system to support in-language globals; added 1.4.1.0
29 // historically, the old chuck_globals.h/cpp supported implementation
30 // globals; these files were removed during REFACTOR-2017, which moved
31 // away from a monolithic VM model to support for many VM; implementation
32 // globals were refactored to be per-VM; the new chuck_globals.h/cpp,
33 // reinstated in 2021, supports in-language globals (using the "global"
34 // keyword) and integration with external hosts (such as Chunity).
35 //
36 // author: Jack Atherton ([email protected])
37 // Ge Wang ([email protected])
38 // date: Spring 2021
39 //-----------------------------------------------------------------------------
40 #ifndef __CHUCK_GLOBALS_H__
41 #define __CHUCK_GLOBALS_H__
42
43 #include "chuck_def.h"
44 #include "chuck_type.h"
45 #include "chuck_ugen.h"
46 #include <string>
47 #include <map>
48 #include <vector>
49
50 // forward reference for ChucK VM
51 struct Chuck_VM;
52 struct Chuck_VM_Shred;
53 struct Chuck_Msg;
54
55
56
57 // global variables & management -- added 1.4.1.0 (jack)
58 // forward references for global messages
59 struct Chuck_Set_Global_Int_Request;
60 struct Chuck_Get_Global_Int_Request;
61 struct Chuck_Set_Global_Float_Request;
62 struct Chuck_Get_Global_Float_Request;
63 struct Chuck_Signal_Global_Event_Request;
64 struct Chuck_Listen_For_Global_Event_Request;
65 struct Chuck_Set_Global_String_Request;
66 struct Chuck_Get_Global_String_Request;
67 struct Chuck_Set_Global_Int_Array_Request;
68 struct Chuck_Get_Global_Int_Array_Request;
69 struct Chuck_Set_Global_Int_Array_Value_Request;
70 struct Chuck_Get_Global_Int_Array_Value_Request;
71 struct Chuck_Set_Global_Associative_Int_Array_Value_Request;
72 struct Chuck_Get_Global_Associative_Int_Array_Value_Request;
73 struct Chuck_Set_Global_Float_Array_Request;
74 struct Chuck_Get_Global_Float_Array_Request;
75 struct Chuck_Set_Global_Float_Array_Value_Request;
76 struct Chuck_Get_Global_Float_Array_Value_Request;
77 struct Chuck_Set_Global_Associative_Float_Array_Value_Request;
78 struct Chuck_Get_Global_Associative_Float_Array_Value_Request;
79 struct Chuck_Get_Global_All_Request; // 1.5.1.0
80 struct Chuck_Execute_Chuck_Msg_Request;
81
82 // forward references for global storage
83 struct Chuck_Global_Int_Container;
84 struct Chuck_Global_Float_Container;
85 struct Chuck_Global_String_Container;
86 struct Chuck_Global_Event_Container;
87 struct Chuck_Global_UGen_Container;
88 struct Chuck_Global_Array_Container;
89 struct Chuck_Global_Object_Container;
90
91
92
93 //-----------------------------------------------------------------------------
94 // name: enum Global_Request_Type
95 // desc: what kind of global message request is this? (REFACTOR-2017)
96 //-----------------------------------------------------------------------------
97 enum Chuck_Global_Request_Type
98 {
99 // none
100 global_request_none,
101 // primitives
102 set_global_int_request,
103 get_global_int_request,
104 set_global_float_request,
105 get_global_float_request,
106 set_global_string_request,
107 get_global_string_request,
108 // events
109 signal_global_event_request,
110 listen_for_global_event_request,
111 // int arrays
112 set_global_int_array_request,
113 get_global_int_array_request,
114 set_global_int_array_value_request,
115 get_global_int_array_value_request,
116 set_global_associative_int_array_value_request,
117 get_global_associative_int_array_value_request,
118 // float arrays
119 set_global_float_array_request,
120 get_global_float_array_request,
121 set_global_float_array_value_request,
122 get_global_float_array_value_request,
123 set_global_associative_float_array_value_request,
124 get_global_associative_float_array_value_request,
125 // 1.5.1.0 (ge) get all global variables
126 get_global_all_request,
127 // shreds
128 spork_shred_request,
129 // chuck_msg
130 execute_chuck_msg_request
131 };
132
133
134
135
136 //-----------------------------------------------------------------------------
137 // name: struct Global_Request
138 // desc: a global request (REFACTOR-2017)
139 //-----------------------------------------------------------------------------
140 struct Chuck_Global_Request
141 {
142 Chuck_Global_Request_Type type;
143 t_CKUINT retries;
144 union {
145 // primitives
146 Chuck_Set_Global_Int_Request* setIntRequest;
147 Chuck_Get_Global_Int_Request* getIntRequest;
148 Chuck_Set_Global_Float_Request* setFloatRequest;
149 Chuck_Get_Global_Float_Request* getFloatRequest;
150 Chuck_Set_Global_String_Request* setStringRequest;
151 Chuck_Get_Global_String_Request* getStringRequest;
152 // events
153 Chuck_Signal_Global_Event_Request* signalEventRequest;
154 Chuck_Listen_For_Global_Event_Request* listenForEventRequest;
155 // int arrays
156 Chuck_Set_Global_Int_Array_Request* setIntArrayRequest;
157 Chuck_Get_Global_Int_Array_Request* getIntArrayRequest;
158 Chuck_Set_Global_Int_Array_Value_Request* setIntArrayValueRequest;
159 Chuck_Get_Global_Int_Array_Value_Request* getIntArrayValueRequest;
160 Chuck_Set_Global_Associative_Int_Array_Value_Request* setAssociativeIntArrayValueRequest;
161 Chuck_Get_Global_Associative_Int_Array_Value_Request* getAssociativeIntArrayValueRequest;
162 // float arrays
163 Chuck_Set_Global_Float_Array_Request* setFloatArrayRequest;
164 Chuck_Get_Global_Float_Array_Request* getFloatArrayRequest;
165 Chuck_Set_Global_Float_Array_Value_Request* setFloatArrayValueRequest;
166 Chuck_Get_Global_Float_Array_Value_Request* getFloatArrayValueRequest;
167 Chuck_Set_Global_Associative_Float_Array_Value_Request* setAssociativeFloatArrayValueRequest;
168 Chuck_Get_Global_Associative_Float_Array_Value_Request* getAssociativeFloatArrayValueRequest;
169 // get all global variables | 1.5.1.0
170 Chuck_Get_Global_All_Request* getAllRequest;
171 // shreds
172 Chuck_VM_Shred* shred;
173 // chuck_msg
174 Chuck_Execute_Chuck_Msg_Request* executeChuckMsgRequest;
175 };
176
177 Chuck_Global_Request()
178 {
179 type = global_request_none;
180 retries = 0;
181 executeChuckMsgRequest = NULL;
182 }
183 };
184
185
186
187
188 //-----------------------------------------------------------------------------
189 // name: struct Chuck_Globals_TypeValue | 1.5.1.0 (ge) added
190 // desc: a type-value pair representing a global value in the system
191 // used to retrive a list of all current global variables
192 //-----------------------------------------------------------------------------
193 struct Chuck_Globals_TypeValue
194 {
195 // type string of global variable
196 std::string type;
197 // global variable name
198 std::string name;
199
200 // constructor
201 Chuck_Globals_TypeValue()
202 { }
203 Chuck_Globals_TypeValue( const std::string & t, const std::string & n )
204 : type(t), name(n) { }
205 };
206
207
208
209
210 //-----------------------------------------------------------------------------
211 // name: struct Chuck_Globals_Manager
212 // desc: manager for globals storage | added 1.4.1.0 (jack)
213 //-----------------------------------------------------------------------------
214 struct Chuck_Globals_Manager
215 {
216 public:
217 Chuck_Globals_Manager( Chuck_VM * vm );
218 ~Chuck_Globals_Manager();
219
220 public:
221 // REFACTOR-2017: externally accessible + global variables.
222 // use these getters and setters from outside the audio thread
223 t_CKBOOL getGlobalInt( const char * name, void (*callback)(t_CKINT) );
224 t_CKBOOL getGlobalInt( const char * name, void (*callback)(const char*, t_CKINT) );
225 t_CKBOOL getGlobalInt( const char * name, t_CKINT callbackID, void (*callback)(t_CKINT, t_CKINT) );
226 t_CKBOOL setGlobalInt( const char * name, t_CKINT val );
227
228 t_CKBOOL getGlobalFloat( const char * name, void (*callback)(t_CKFLOAT) );
229 t_CKBOOL getGlobalFloat( const char * name, void (*callback)(const char*, t_CKFLOAT) );
230 t_CKBOOL getGlobalFloat( const char * name, t_CKINT callbackID, void (*callback)(t_CKINT, t_CKFLOAT) );
231 t_CKBOOL setGlobalFloat( const char * name, t_CKFLOAT val );
232
233 t_CKBOOL getGlobalString( const char * name, void (*callback)(const char*) );
234 t_CKBOOL getGlobalString( const char * name, void (*callback)(const char*, const char*) );
235 t_CKBOOL getGlobalString( const char * name, t_CKINT callbackID, void (*callback)(t_CKINT, const char*) );
236 t_CKBOOL setGlobalString( const char * name, const char * val );
237
238 t_CKBOOL signalGlobalEvent( const char * name );
239 t_CKBOOL broadcastGlobalEvent( const char * name );
240 t_CKBOOL listenForGlobalEvent( const char * name, void (*callback)(void), t_CKBOOL listen_forever );
241 t_CKBOOL listenForGlobalEvent( const char * name, void (*callback)(const char*), t_CKBOOL listen_forever );
242 t_CKBOOL listenForGlobalEvent( const char * name, t_CKINT callbackID, void (*callback)(t_CKINT), t_CKBOOL listen_forever );
243 t_CKBOOL stopListeningForGlobalEvent( const char * name, void (*callback)(void) );
244 t_CKBOOL stopListeningForGlobalEvent( const char * name, void (*callback)(const char*) );
245 t_CKBOOL stopListeningForGlobalEvent( const char * name, t_CKINT callbackID, void (*callback)(t_CKINT) );
246
247 t_CKBOOL getGlobalUGenSamples( const char * name, SAMPLE * buffer, int numFrames );
248 t_CKBOOL getGlobalUGenSamplesMulti( const char * name, SAMPLE * buffer, int numFrames, int numChannels );
249
250 t_CKBOOL setGlobalIntArray( const char * name, t_CKINT arrayValues[], t_CKUINT numValues );
251 t_CKBOOL getGlobalIntArray( const char * name, void (*callback)(t_CKINT[], t_CKUINT) );
252 t_CKBOOL getGlobalIntArray( const char * name, void (*callback)(const char*, t_CKINT[], t_CKUINT) );
253 t_CKBOOL getGlobalIntArray( const char * name, t_CKINT callbackID, void (*callback)(t_CKINT, t_CKINT[], t_CKUINT) );
254 t_CKBOOL setGlobalIntArrayValue( const char * name, t_CKUINT index, t_CKINT value );
255 t_CKBOOL getGlobalIntArrayValue( const char * name, t_CKUINT index, void (*callback)(t_CKINT) );
256 t_CKBOOL getGlobalIntArrayValue( const char * name, t_CKUINT index, void (*callback)(const char*, t_CKINT) );
257 t_CKBOOL getGlobalIntArrayValue( const char * name, t_CKINT callbackID, t_CKUINT index, void (*callback)(t_CKINT, t_CKINT) );
258 t_CKBOOL setGlobalAssociativeIntArrayValue( const char * name, const char * key, t_CKINT value );
259 t_CKBOOL getGlobalAssociativeIntArrayValue( const char * name, const char * key, void (*callback)(t_CKINT) );
260 t_CKBOOL getGlobalAssociativeIntArrayValue( const char * name, const char * key, void (*callback)(const char*, t_CKINT) );
261 t_CKBOOL getGlobalAssociativeIntArrayValue( const char * name, t_CKINT callbackID, const char * key, void (*callback)(t_CKINT, t_CKINT) );
262
263 t_CKBOOL setGlobalFloatArray( const char * name, t_CKFLOAT arrayValues[], t_CKUINT numValues );
264 t_CKBOOL getGlobalFloatArray( const char * name, void (*callback)(t_CKFLOAT[], t_CKUINT) );
265 t_CKBOOL getGlobalFloatArray( const char * name, void (*callback)(const char*, t_CKFLOAT[], t_CKUINT) );
266 t_CKBOOL getGlobalFloatArray( const char * name, t_CKINT callbackID, void (*callback)(t_CKINT, t_CKFLOAT[], t_CKUINT) );
267 t_CKBOOL setGlobalFloatArrayValue( const char * name, t_CKUINT index, t_CKFLOAT value );
268 t_CKBOOL getGlobalFloatArrayValue( const char * name, t_CKUINT index, void (*callback)(t_CKFLOAT) );
269 t_CKBOOL getGlobalFloatArrayValue( const char * name, t_CKUINT index, void (*callback)(const char*, t_CKFLOAT) );
270 t_CKBOOL getGlobalFloatArrayValue( const char * name, t_CKINT callbackID, t_CKUINT index, void (*callback)(t_CKINT, t_CKFLOAT) );
271 t_CKBOOL setGlobalAssociativeFloatArrayValue( const char * name, const char * key, t_CKFLOAT value );
272 t_CKBOOL getGlobalAssociativeFloatArrayValue( const char * name, const char * key, void (*callback)(t_CKFLOAT) );
273 t_CKBOOL getGlobalAssociativeFloatArrayValue( const char * name, const char * key, void (*callback)(const char*, t_CKFLOAT) );
274 t_CKBOOL getGlobalAssociativeFloatArrayValue( const char * name, t_CKINT callbackID, const char * key, void (*callback)(t_CKINT, t_CKFLOAT) );
275
276 // 1.5.1.0 (ge) get all global variables
277 t_CKBOOL getAllGlobalVariables( void (*callback)( const std::vector<Chuck_Globals_TypeValue> & list, void * data ),
278 void * data = NULL );
279
280 public:
281 // run Chuck_Msg in the globals order
282 t_CKBOOL execute_chuck_msg_with_globals( Chuck_Msg* msg );
283
284 public:
285 // REFACTOR-2017: externally accessible + global variables.
286 // WARNING: these internal functions are to be used only by other
287 // chuck code in the audio thread...
288 t_CKBOOL init_global_int( const std::string & name );
289 t_CKINT get_global_int_value( const std::string & name );
290 t_CKINT* get_ptr_to_global_int( const std::string & name );
291
292 t_CKBOOL init_global_float( const std::string & name );
293 t_CKFLOAT get_global_float_value( const std::string & name );
294 t_CKFLOAT* get_ptr_to_global_float( const std::string & name );
295
296 t_CKBOOL init_global_string( const std::string & name );
297 Chuck_String* get_global_string( const std::string & name );
298 Chuck_String** get_ptr_to_global_string( const std::string & name );
299
300 t_CKBOOL init_global_event( const std::string & name, Chuck_Type* type );
301 Chuck_Event* get_global_event( const std::string & name );
302 Chuck_Event** get_ptr_to_global_event( const std::string & name );
303
304 t_CKBOOL init_global_ugen( const std::string & name, Chuck_Type* type );
305 t_CKBOOL is_global_ugen_init( const std::string & name );
306 t_CKBOOL is_global_ugen_valid( const std::string & name );
307 Chuck_UGen* get_global_ugen( const std::string & name );
308 Chuck_UGen** get_ptr_to_global_ugen( const std::string & name );
309
310 // global array (internal; audio thread)
311 t_CKBOOL init_global_array( const std::string & name, Chuck_Type* type, te_GlobalType arr_type );
312 // global array get (internal; audio thread)
313 Chuck_Object* get_global_array( const std::string & name );
314 Chuck_Object** get_ptr_to_global_array( const std::string & name );
315 t_CKINT get_global_int_array_value( const std::string & name, t_CKUINT index );
316 t_CKINT get_global_associative_int_array_value( const std::string & name, const std::string & key );
317 t_CKFLOAT get_global_float_array_value( const std::string & name, t_CKUINT index );
318 t_CKFLOAT get_global_associative_float_array_value( const std::string & name, const std::string & key );
319 // global array set (internal; audio thread)
320 t_CKBOOL set_global_int_array( const std::string & name, t_CKINT arrayValues[], t_CKUINT numValues );
321 t_CKBOOL set_global_int_array_value( const std::string & name, t_CKUINT index, t_CKINT value );
322 t_CKBOOL set_global_float_array( const std::string & name, t_CKFLOAT arrayValues[], t_CKUINT numValues );
323 t_CKBOOL set_global_float_array_value( const std::string & name, t_CKUINT index, t_CKFLOAT value );
324
325 t_CKBOOL init_global_object( const std::string & name, Chuck_Type* type );
326 t_CKBOOL is_global_object_init( const std::string & name );
327 t_CKBOOL is_global_object_valid( const std::string & name );
328 Chuck_Object* get_global_object( const std::string & name );
329 Chuck_Object** get_ptr_to_global_object( const std::string & name );
330
331 // 1.5.1.0 (ge) get all global variables
332 void get_all_global_variables( std::vector<Chuck_Globals_TypeValue> & list );
333
334 t_CKBOOL should_call_global_ctor( const std::string & name, te_GlobalType type );
335 void global_ctor_was_called( const std::string & name, te_GlobalType type );
336
337 // global variables -- clean up
338 void cleanup_global_variables();
339
340 public: // these should ever ONLY be called from within the VM
341 // request queue: add, query for size
342 t_CKBOOL add_request( Chuck_Global_Request request );
343 t_CKBOOL more_requests();
344 // REFACTOR-2017: execute the messages from the global queue
345 void handle_global_queue_messages();
346
347 private:
348 // ptr to my vm
349 Chuck_VM * m_vm;
350
351 // global variables -- storage
352 std::map< std::string, Chuck_Global_Int_Container* > m_global_ints;
353 std::map< std::string, Chuck_Global_Float_Container* > m_global_floats;
354 std::map< std::string, Chuck_Global_String_Container* > m_global_strings;
355 std::map< std::string, Chuck_Global_Event_Container* > m_global_events;
356 std::map< std::string, Chuck_Global_UGen_Container* > m_global_ugens;
357 std::map< std::string, Chuck_Global_Array_Container* > m_global_arrays;
358 std::map< std::string, Chuck_Global_Object_Container* > m_global_objects;
359
360 // global variables -- messages for set, get, etc.
361 FinalRingBuffer< Chuck_Global_Request > m_global_request_queue;
362
363 // retry queue: sometimes a request comes through exactly 1 sample
364 // before the relevant global variable is constructed
365 // (e.g. signalEvent before the compileCode that would construct the event)
366 // in these cases, retrying 1 sample later usually works.
367 // this is ok because the external host has no guarantee of sample-level
368 // determinism, like we have within the ChucK VM
369 FinalRingBuffer< Chuck_Global_Request > m_global_request_retry_queue;
370 };
371
372
373
374
375 #endif