Debian Codegraph @

src/core/chuck.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.h
28 // desc: interface for ChucK core: compiler + virtual machine + synthesis
29 // |- possible to instantiate multiple ChucK instances
30 // |- each ChucK instance contains a fully functioning ChucK runtime
31 // |- the ChucK class interface is primary point of integration between
32 // ChucK core and ChucK hosts (e.g., command-line ChucK,
33 // miniAudicle, WebChucK, Chunity, Chunreal, and any C++ program
34 // using ChucK core as a component).
35 // |- see src/host-examples/ for sample integration code for ChucK core
36 // |- see chuck, miniAudicle, WebChucK, Chunity etc. for their
37 // respective integration of ChucK core
38 // ------
39 // |- [chugins] "chuck.h" is also a single-header include for
40 // chugins, including ChuGL (ChucK Graphics Library), standard
41 // chugins library, and 3rd-party chugins
42 // |- [chugins] are dynamically loaded and thus should never directly
43 // instantiate objects in chuck core; instead chugins must access
44 // chuck core function through the DL runtime API
45 // (defined in "chuck_dl.h", which is included from this header)
46 // ----------------------------------------------------------------------
47 //
48 // author: Ge Wang (https://ccrma.stanford.edu/~ge/)
49 // date: Fall 2017
50 //
51 // additional authors:
52 // Jack Atherton ([email protected])
53 // Spencer Salazar ([email protected])
54 //
55 // (part of The Big Refactor of 2017; codename `numchucks`; #REFACTOR-2017)
56 //-----------------------------------------------------------------------------
57 #ifndef __CHUCK_H__
58 #define __CHUCK_H__
59
60 #include "chuck_carrier.h"
61 #include "chuck_compile.h"
62 #include "chuck_dl.h"
63 #include "chuck_vm.h"
64 #include "util_math.h"
65 #include "util_string.h"
66 #include <string>
67 #include <map>
68
69 #ifndef __DISABLE_SHELL__
70 #include "chuck_shell.h"
71 #endif
72
73 #ifndef __DISABLE_OTF_SERVER
74 #include "ulib_machine.h"
75 #endif
76
77 #ifndef __DISABLE_HID__
78 #include "hidio_sdl.h"
79 #endif
80
81 #ifndef __DISABLE_MIDI__
82 #include "midiio_rtmidi.h"
83 #endif
84
85 // ChucK param names -- used in setParam(...) and getParam*(...)
86 #define CHUCK_PARAM_VERSION "VERSION"
87 #define CHUCK_PARAM_SAMPLE_RATE "SAMPLE_RATE"
88 #define CHUCK_PARAM_INPUT_CHANNELS "INPUT_CHANNELS"
89 #define CHUCK_PARAM_OUTPUT_CHANNELS "OUTPUT_CHANNELS"
90 #define CHUCK_PARAM_VM_ADAPTIVE "VM_ADAPTIVE"
91 #define CHUCK_PARAM_VM_HALT "VM_HALT"
92 #define CHUCK_PARAM_OTF_ENABLE "OTF_ENABLE"
93 #define CHUCK_PARAM_OTF_PORT "OTF_PORT"
94 #define CHUCK_PARAM_OTF_PRINT_WARNINGS "OTF_PRINT_WARNINGS"
95 #define CHUCK_PARAM_DUMP_INSTRUCTIONS "DUMP_INSTRUCTIONS"
96 #define CHUCK_PARAM_AUTO_DEPEND "AUTO_DEPEND"
97 #define CHUCK_PARAM_DEPRECATE_LEVEL "DEPRECATE_LEVEL"
98 #define CHUCK_PARAM_WORKING_DIRECTORY "WORKING_DIRECTORY"
99 #define CHUCK_PARAM_IS_REALTIME_AUDIO_HINT "IS_REALTIME_AUDIO_HINT"
100 #define CHUCK_PARAM_COMPILER_HIGHLIGHT_ON_ERROR "COMPILER_HIGHLIGHT_ON_ERROR"
101 #define CHUCK_PARAM_TTY_COLOR "TTY_COLOR"
102 #define CHUCK_PARAM_TTY_WIDTH_HINT "TTY_WIDTH_HINT"
103 // chugin-relate param names
104 #define CHUCK_PARAM_CHUGIN_ENABLE "CHUGIN_ENABLE"
105 #define CHUCK_PARAM_USER_CHUGINS "USER_CHUGINS"
106 // import paths
107 #define CHUCK_PARAM_IMPORT_PATH_SYSTEM "IMPORT_PATH_SYSTEM"
108 #define CHUCK_PARAM_IMPORT_PATH_PACKAGES "IMPORT_PATH_PACKAGES"
109 #define CHUCK_PARAM_IMPORT_PATH_USER "IMPORT_PATH_USER"
110
111 // code literal signifier
112 #define CHUCK_CODE_LITERAL_SIGNIFIER "<compiled.code>"
113
114
115
116
117 //-----------------------------------------------------------------------------
118 // name: class ChucK
119 // desc: ChucK system encapsulation
120 //-----------------------------------------------------------------------------
121 class ChucK
122 {
123 public:
124 // constructor
125 ChucK();
126 // desctructor
127 virtual ~ChucK();
128
129 public:
130 // set parameter by name (CHUCK_PARAM_*)
131 // |- (in general, parameters have reasonable defaults)
132 t_CKBOOL setParam( const std::string & name, t_CKINT value );
133 t_CKBOOL setParamFloat( const std::string & name, t_CKFLOAT value );
134 t_CKBOOL setParam( const std::string & name, const std::string & value );
135 t_CKBOOL setParam( const std::string & name, const std::list< std::string > & value );
136 // get parameter by type
137 t_CKINT getParamInt( const std::string & key );
138 t_CKFLOAT getParamFloat( const std::string & key );
139 std::string getParamString( const std::string & key );
140 // if a parameter is actually a list of value
141 std::list< std::string > getParamStringList( const std::string & key );
142
143 public:
144 // initialize ChucK (using params)
145 t_CKBOOL init();
146 // explicit start (optionally -- implicitly called as needed from run())
147 t_CKBOOL start();
148
149 public:
150 // compile a file -> generate chuck bytecode -> spork as new shred(s)
151 // `argsTogether` is appended to `path` as additional arguments, separated by ':' e.g., "1:foo:bar"
152 // `count` specifies how many instances of the new shred to spork
153 // if `immediate` == TRUE, the new shred(s) is shreduled immediately (on the calling thread)
154 // if `immediate` == FALSE, the new shreds(s) is queued and shreduled on the next time step (on the VM compute/audio thread)
155 // if `shredIDS` is not NULL, it will be filled with the ID(s) of the new resulting shreds
156 // returns TRUE if compilation successful (even if count == 0)
157 // returns FALSE if compilation unsuccessful
158 t_CKBOOL compileFile( const std::string & path, const std::string & argsTogether = "",
159 t_CKUINT count = 1, t_CKBOOL immediate = FALSE, std::vector<t_CKUINT> * shredIDs = NULL );
160 // compile code/text -> genereate chuck bytecode -> spork as new shred(s)
161 // `argsTogether` is appended to `path` as arguments, separated by ':' e.g., "1:foo:bar"
162 // `count` specifies how many instances of the new shred to spork
163 // if `immediate` == TRUE, the new shred(s) is shreduled immediately (on the calling thread)
164 // if `immediate` == FALSE, the new shreds(s) is queued and shreduled on the next time step (on the VM compute/audio thread)
165 // if `shredIDS` is not NULL, it will be filled with the ID(s) of the new resulting shreds
166 // returns TRUE if compilation successful (even if count == 0)
167 // returns FALSE if compilation unsuccessful
168 // 'optFilepath' optionally specifies a file (name, or path ending in '/') as basis for path-related operations, e.g., @import
169 t_CKBOOL compileCode( const std::string & code, const std::string & argsTogether = "",
170 t_CKUINT count = 1, t_CKBOOL immediate = FALSE, std::vector<t_CKUINT> * shredIDs = NULL,
171 const std::string & optFilepath = "" );
172
173 public:
174 // run ChucK and synthesize audio for `numFrames`...
175 // |- (NOTE this function is often called from audio callback)
176 // `input`: the incoming input array of audio samples
177 // |- input array size expected to == `numFrames` * number-of-input-chanels, as initialized in setParam(CHUCK_PARAM_INPUT_CHANNELS, ...)
178 // `output`: the outgoing output array of audio samples
179 // |- output array size expected to == `numFrames` * number-of-output-chanels, as initialized in setParam(CHUCK_PARAM_OUTPUT_CHANNELS, ...)
180 // `numFrames` : the number of audio frames to run
181 // |- each audio frame corresponds to one point in time, and contains values for every audio channel
182 void run( const SAMPLE * input, SAMPLE * output, t_CKINT numFrames );
183
184 public:
185 // remove all shreds currently in the VM | 1.5.4.4 (ge) added
186 // |- (NOTE: not synchronous or truly immediate, but is thread-safe;
187 // |- this will happen at the top of the next VM compute() call)
188 void removeAllShreds();
189
190 public:
191 // get globals (needed to access Globals Manager)
192 // |- useful for communication between C++ and ChucK using chuck global variables
193 Chuck_Globals_Manager * globals();
194
195 public:
196 // get VM (use with care)
197 Chuck_VM * vm() { return m_carrier->vm; }
198 // get compiler (use with care)
199 Chuck_Compiler * compiler() { return m_carrier->compiler; }
200 // is the VM running
201 t_CKBOOL vm_running() const { return m_carrier->vm && m_carrier->vm->running(); }
202 // is this ChucK instance initialized
203 t_CKBOOL isInit() const { return m_init; }
204 // get current chuck time
205 t_CKTIME now() const { return m_carrier->vm ? m_carrier->vm->now() : 0; }
206
207 public:
208 // set whether chuck will generate color output for its messages
209 // this will set the corresponding parameter as well
210 void toggleGlobalColorTextoutput( t_CKBOOL onOff );
211
212 public:
213 // probe chugins (print info on all chugins as seen under current config)
214 void probeChugins(); // 1.5.0.4 (ge) added
215
216 public:
217 // additional native chuck bindings/types (use with extra caution)
218 t_CKBOOL bind( f_ck_query queryFunc, const std::string & name );
219 // set a function pointer to call from the main thread loop
220 t_CKBOOL setMainThreadHook( Chuck_DL_MainThreadHook * hook );
221 // get pointer to current function to be called from main loop
222 Chuck_DL_MainThreadHook * getMainThreadHook();
223
224 public:
225 // global callback functions: replace printing to command line with a callback function
226 t_CKBOOL setChoutCallback( void (* callback)(const char *) );
227 t_CKBOOL setCherrCallback( void (* callback)(const char *) );
228 // static I/O redirects, for alternate printing of chuck oputput and system message
229 // e.g., miniAudicle console, Unity console, etc.
230 static t_CKBOOL setStdoutCallback( void (* callback)(const char *) );
231 static t_CKBOOL setStderrCallback( void (* callback)(const char *) );
232
233 public: // static functions
234 // chuck version
235 static const char * version();
236 // chuck int size (in bits)
237 // (this depends on machine, which depends on OTF, so disable it if we don't have OTF)
238 // 1.5.0.0 (ge) | reinstated for non-OTF, along with parts of machine
239 static t_CKUINT intSize();
240 // number of ChucK's
241 static t_CKUINT numVMs() { return o_numVMs; };
242 // --poop compatibilty
243 static void poop();
244 // set log level -- this should eventually be per-VM?
245 static void setLogLevel( t_CKINT level );
246 // get log level -- also per-VM?
247 static t_CKINT getLogLevel();
248 // use with care: if true, this enables system() calls from code
249 static t_CKBOOL enableSystemCall;
250
251 public:
252 // global cleanup for all instances
253 // |- (called once at end when all instances done, usually by host)
254 static void globalCleanup();
255
256 protected:
257 // global initialization for all instances
258 // |- (called once at beginning internally by ChucK)
259 static t_CKBOOL globalInit();
260 // flag for global init
261 static t_CKBOOL o_isGlobalInit;
262
263 protected:
264 // chuck version
265 static const char VERSION[];
266 // number of VMs -- managed from VM constructor/destructors
267 static t_CKUINT o_numVMs;
268
269 protected:
270 // core elements: compiler, VM, etc.
271 Chuck_Carrier * m_carrier;
272 // chuck params
273 std::map<std::string, std::string> m_params;
274 // chuck list params
275 std::map< std::string, std::list<std::string> > m_listParams;
276 // read-only
277 std::map< std::string, t_CKBOOL > m_readOnly;
278 // enum for type of param (int, float, string)
279 enum ck_param_type
280 { ck_param_int, ck_param_float, ck_param_string, ck_param_string_list };
281 // param types
282 std::map< std::string, ck_param_type> m_param_types;
283 // did user init?
284 t_CKBOOL m_init;
285 // did user start?
286 t_CKBOOL m_started;
287 // main thread "hook" (if there is a main thread)
288 Chuck_DL_MainThreadHook * m_hook;
289
290 protected:
291 // init VM
292 t_CKBOOL initVM();
293 // init compiler
294 t_CKBOOL initCompiler();
295 // init chugin system
296 t_CKBOOL initChugins();
297 // init OTF programming system
298 t_CKBOOL initOTF();
299 // shutdown
300 t_CKBOOL shutdown();
301
302 // initialize default params
303 void initDefaultParams();
304 // check read-only
305 t_CKBOOL readOnlyParam( const std::string & name );
306 // init param helper
307 void initParam( const std::string & name, const std::string & value,
308 ck_param_type typeEnum, t_CKBOOL isReadyOnly = FALSE );
309 // match param (case-insensitive)
310 t_CKBOOL matchParam( const std::string & lhs, const std::string & rhs );
311 // enact processing, as needed, for certain params
312 void enactParam( const std::string & name, t_CKINT value );
313 };
314
315
316
317
318 #endif // #ifndef __CHUCK_H__