Debian Codegraph @

src/core/chuck.cpp

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.cpp
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 // author: Ge Wang (https://ccrma.stanford.edu/~ge/)
40 // date: fall 2017
41 //
42 // additional authors:
43 // Jack Atherton ([email protected])
44 // Spencer Salazar ([email protected])
45 //
46 // (part of The Big Refactor of 2017; codename `numchucks`; #REFACTOR-2017)
47 //-----------------------------------------------------------------------------
48 #include "chuck.h"
49 #include "chuck_errmsg.h"
50 #include "chuck_io.h"
51 #include "chuck_globals.h" // added 1.4.1.0
52
53 #ifndef __DISABLE_OTF_SERVER__
54 #include "chuck_otf.h"
55 #include "ulib_machine.h"
56 #endif
57
58 #ifndef __DISABLE_NETWORK__
59 #include "util_network.h"
60 #endif
61
62 #include "ulib_std.h"
63 #include "util_platforms.h"
64 #include "util_string.h"
65 #include "ugen_stk.h"
66
67 #ifndef __PLATFORM_WINDOWS__
68 #include <unistd.h>
69 #include <netinet/in.h>
70 #include <arpa/inet.h>
71 #else
72 #include <direct.h> // added 1.3.0.0
73 #endif // #ifndef __PLATFORM_WINDOWS__
74
75
76
77
78 // chuck param defaults
79 #define CHUCK_PARAM_SAMPLE_RATE_DEFAULT "44100"
80 #define CHUCK_PARAM_INPUT_CHANNELS_DEFAULT "2"
81 #define CHUCK_PARAM_OUTPUT_CHANNELS_DEFAULT "2"
82 #define CHUCK_PARAM_VM_ADAPTIVE_DEFAULT "0"
83 #define CHUCK_PARAM_VM_HALT_DEFAULT "0"
84 #define CHUCK_PARAM_OTF_ENABLE_DEFAULT "0"
85 #define CHUCK_PARAM_OTF_PORT_DEFAULT "8888"
86 #define CHUCK_PARAM_OTF_PRINT_WARNINGS_DEFAULT "0"
87 #define CHUCK_PARAM_DUMP_INSTRUCTIONS_DEFAULT "0"
88 #define CHUCK_PARAM_AUTO_DEPEND_DEFAULT "0"
89 #define CHUCK_PARAM_DEPRECATE_LEVEL_DEFAULT "1"
90 #define CHUCK_PARAM_WORKING_DIRECTORY_DEFAULT ""
91 #define CHUCK_PARAM_IS_REALTIME_AUDIO_HINT_DEFAULT "0"
92 #define CHUCK_PARAM_COMPILER_HIGHLIGHT_ON_ERROR_DEFAULT "1"
93 #define CHUCK_PARAM_TTY_COLOR_DEFAULT "0"
94 #define CHUCK_PARAM_TTY_WIDTH_HINT_DEFAULT "80"
95 // chugin-relate param defaults
96 #define CHUCK_PARAM_CHUGIN_ENABLE_DEFAULT "1"
97 #define CHUCK_PARAM_USER_CHUGINS_DEFAULT std::list<std::string>()
98 // import search paths defaults
99 #define CHUCK_PARAM_IMPORT_PATH_SYSTEM_DEFAULT std::list<std::string>()
100 #define CHUCK_PARAM_IMPORT_PATH_PACKAGES_DEFAULT std::list<std::string>()
101 #define CHUCK_PARAM_IMPORT_PATH_USER_DEFAULT std::list<std::string>()
102
103
104
105 // chuck statics
106 const char ChucK::VERSION[] = CHUCK_VERSION_STRING;
107 t_CKUINT ChucK::o_numVMs = 0;
108 t_CKBOOL ChucK::o_isGlobalInit = FALSE;
109 t_CKBOOL ChucK::enableSystemCall = FALSE;
110
111
112
113
114 //-----------------------------------------------------------------------------
115 // name: version()
116 // desc: get chuck version
117 //-----------------------------------------------------------------------------
118 const char * ChucK::version()
119 {
120 return VERSION;
121 }
122
123
124
125
126 //-----------------------------------------------------------------------------
127 // name: intSize()
128 // desc: get chuck int size (in bits)
129 //-----------------------------------------------------------------------------
130 t_CKUINT ChucK::intSize()
131 {
132 return machine_intsize();
133 }
134
135
136
137
138 //-----------------------------------------------------------------------------
139 // name: ChucK()
140 // desc: constructor
141 //-----------------------------------------------------------------------------
142 ChucK::ChucK()
143 {
144 // instantiate the carrier!
145 m_carrier = new Chuck_Carrier();
146 // set reference back to this
147 m_carrier->chuck = this;
148 // increment the numChucKs
149 o_numVMs++;
150 // initialize default params
151 initDefaultParams();
152 // did user init?
153 m_init = FALSE;
154 // did user start ChucK?
155 m_started = FALSE;
156 // zero out the hook
157 m_hook = NULL; // m_hook = nullptr
158
159 // global init, if needed
160 if( !o_isGlobalInit ) globalInit();
161 }
162
163
164
165
166 //-----------------------------------------------------------------------------
167 // name: ~ChucK()
168 // desc: destructor
169 //-----------------------------------------------------------------------------
170 ChucK::~ChucK()
171 {
172 // shutdown
173 shutdown();
174 // decrement the numChucKs
175 o_numVMs--;
176 // clean up the carrier
177 CK_SAFE_DELETE( m_carrier );
178 }
179
180
181
182
183 //-----------------------------------------------------------------------------
184 // name: initDefaultParams()
185 // desc: initialize default params
186 //-----------------------------------------------------------------------------
187 void ChucK::initDefaultParams()
188 {
189 initParam( CHUCK_PARAM_VERSION, CHUCK_VERSION_STRING, ck_param_string, TRUE );
190 initParam( CHUCK_PARAM_SAMPLE_RATE, CHUCK_PARAM_SAMPLE_RATE_DEFAULT, ck_param_int );
191 initParam( CHUCK_PARAM_INPUT_CHANNELS, CHUCK_PARAM_INPUT_CHANNELS_DEFAULT, ck_param_int );
192 initParam( CHUCK_PARAM_OUTPUT_CHANNELS, CHUCK_PARAM_OUTPUT_CHANNELS_DEFAULT, ck_param_int );
193 initParam( CHUCK_PARAM_VM_ADAPTIVE, CHUCK_PARAM_VM_ADAPTIVE_DEFAULT, ck_param_int );
194 initParam( CHUCK_PARAM_VM_HALT, CHUCK_PARAM_VM_HALT_DEFAULT, ck_param_int );
195 initParam( CHUCK_PARAM_OTF_ENABLE, CHUCK_PARAM_OTF_ENABLE_DEFAULT, ck_param_int );
196 initParam( CHUCK_PARAM_OTF_PORT, CHUCK_PARAM_OTF_PORT_DEFAULT, ck_param_int );
197 initParam( CHUCK_PARAM_OTF_PRINT_WARNINGS, CHUCK_PARAM_OTF_PRINT_WARNINGS_DEFAULT, ck_param_int );
198 initParam( CHUCK_PARAM_DUMP_INSTRUCTIONS, CHUCK_PARAM_DUMP_INSTRUCTIONS_DEFAULT, ck_param_int );
199 initParam( CHUCK_PARAM_AUTO_DEPEND, CHUCK_PARAM_AUTO_DEPEND_DEFAULT, ck_param_int );
200 initParam( CHUCK_PARAM_DEPRECATE_LEVEL, CHUCK_PARAM_DEPRECATE_LEVEL_DEFAULT, ck_param_int );
201 initParam( CHUCK_PARAM_WORKING_DIRECTORY, CHUCK_PARAM_WORKING_DIRECTORY_DEFAULT, ck_param_string );
202 initParam( CHUCK_PARAM_CHUGIN_ENABLE, CHUCK_PARAM_CHUGIN_ENABLE_DEFAULT, ck_param_int );
203 initParam( CHUCK_PARAM_IS_REALTIME_AUDIO_HINT, CHUCK_PARAM_IS_REALTIME_AUDIO_HINT_DEFAULT, ck_param_int );
204 initParam( CHUCK_PARAM_COMPILER_HIGHLIGHT_ON_ERROR, CHUCK_PARAM_COMPILER_HIGHLIGHT_ON_ERROR_DEFAULT, ck_param_int );
205 initParam( CHUCK_PARAM_TTY_COLOR, CHUCK_PARAM_TTY_COLOR_DEFAULT, ck_param_int );
206 initParam( CHUCK_PARAM_TTY_WIDTH_HINT, CHUCK_PARAM_TTY_WIDTH_HINT_DEFAULT, ck_param_int );
207
208 // initialize list params manually (take care to use tolower())
209 m_listParams[tolower(CHUCK_PARAM_USER_CHUGINS)] = CHUCK_PARAM_USER_CHUGINS_DEFAULT;
210 m_param_types[tolower(CHUCK_PARAM_USER_CHUGINS)] = ck_param_string_list;
211 // import search paths
212 m_listParams[tolower(CHUCK_PARAM_IMPORT_PATH_SYSTEM)] = CHUCK_PARAM_IMPORT_PATH_SYSTEM_DEFAULT;
213 m_param_types[tolower(CHUCK_PARAM_IMPORT_PATH_SYSTEM)] = ck_param_string_list;
214 m_listParams[tolower(CHUCK_PARAM_IMPORT_PATH_PACKAGES)] = CHUCK_PARAM_IMPORT_PATH_PACKAGES_DEFAULT;
215 m_param_types[tolower(CHUCK_PARAM_IMPORT_PATH_PACKAGES)] = ck_param_string_list;
216 m_listParams[tolower(CHUCK_PARAM_IMPORT_PATH_USER)] = CHUCK_PARAM_IMPORT_PATH_USER_DEFAULT;
217 m_param_types[tolower(CHUCK_PARAM_IMPORT_PATH_USER)] = ck_param_string_list;
218 }
219
220
221
222
223 //-----------------------------------------------------------------------------
224 // name: initParam()
225 // desc: internal helper -- initialize a param
226 //-----------------------------------------------------------------------------
227 void ChucK::initParam( const std::string & name, const std::string & value,
228 ck_param_type typeEnum, t_CKBOOL isReadOnly )
229 {
230 // get lowercase version
231 std::string n = tolower(name);
232 // insert into map
233 m_params[n] = value;
234 // remember type
235 m_param_types[n] = typeEnum;
236 // read only?
237 if( isReadOnly ) m_readOnly[n] = isReadOnly;
238 }
239
240
241
242
243 //-----------------------------------------------------------------------------
244 // name: readOnlyParam()
245 // desc: internal helper -- check read-only
246 //-----------------------------------------------------------------------------
247 t_CKBOOL ChucK::readOnlyParam( const std::string & name )
248 {
249 // check
250 return m_readOnly.find(tolower(name)) != m_readOnly.end();
251 }
252
253
254
255
256 //-----------------------------------------------------------------------------
257 // name: matchParam()
258 // desc: internal helper -- match parameter name (case-insensitive)
259 //-----------------------------------------------------------------------------
260 t_CKBOOL ChucK::matchParam( const std::string & lhs, const std::string & rhs )
261 {
262 // return lower-case equal
263 return tolower(lhs) == tolower(rhs);
264 }
265
266
267
268
269 //-----------------------------------------------------------------------------
270 // name: enactParam()
271 // desc: enact processing, as needed, for certain params
272 //-----------------------------------------------------------------------------
273 void ChucK::enactParam( const std::string & name, t_CKINT value )
274 {
275 // check and set
276 if( matchParam(name,CHUCK_PARAM_SAMPLE_RATE) )
277 {
278 // check
279 if( value <= 0 )
280 {
281 EM_error2( 0, "(warning) attempt to set CHUCK_PARAM_SAMPLE_RATE to invalid value '%d'; sample rate unchanged...", value );
282 return;
283 }
284 // update VM to new sample rate
285 // (NOTE: this could be pre-initialization, so need to check VM pointer)
286 if( vm() ) vm()->update_srate( value );
287 }
288 if( matchParam(name,CHUCK_PARAM_TTY_COLOR) )
289 {
290 // set the global override switch
291 TC::globalDisableOverride( !value );
292 }
293 else if( matchParam(name,CHUCK_PARAM_TTY_WIDTH_HINT) )
294 {
295 // set default
296 ck_ttywidth_setdefault( value );
297 }
298 }
299
300
301
302
303 //-----------------------------------------------------------------------------
304 // name: setParam()
305 // desc: set an int param by name
306 //-----------------------------------------------------------------------------
307 t_CKBOOL ChucK::setParam( const std::string & name, t_CKINT value )
308 {
309 // lower case for consistency
310 std::string key = tolower(name);
311 // check read-only
312 if( readOnlyParam(key) ) return FALSE;
313 // check key
314 if( m_params.count(key) > 0 && m_param_types[key] == ck_param_int )
315 {
316 // enact processing, as needed
317 enactParam( key, value );
318 // insert into map
319 m_params[key] = ck_itoa(value);
320 return TRUE;
321 }
322 else
323 {
324 return FALSE;
325 }
326 }
327
328
329
330
331 //-----------------------------------------------------------------------------
332 // name: setParamFloat()
333 // desc: set a float param by name
334 //-----------------------------------------------------------------------------
335 t_CKBOOL ChucK::setParamFloat( const std::string & name, t_CKFLOAT value )
336 {
337 // lower case for consistency
338 std::string key = tolower(name);
339 // check read-only
340 if( readOnlyParam(key) ) return FALSE;
341 // check param type
342 if( m_params.count( key ) > 0 && m_param_types[key] == ck_param_float )
343 {
344 // insert into map
345 m_params[key] = ck_ftoa( value, 32 );
346 return TRUE;
347 }
348 // if param is an int, e.g., sample rate | 1.5.4.2 (ge)
349 else if( m_params.count( key ) > 0 && m_param_types[key] == ck_param_int )
350 {
351 // round and redirect to setParam() as int
352 return setParam( name, (t_CKINT)(value+.05) );
353 }
354 else
355 {
356 return FALSE;
357 }
358 }
359
360
361
362
363 //-----------------------------------------------------------------------------
364 // name: setParam()
365 // desc: set a string param by name
366 //-----------------------------------------------------------------------------
367 t_CKBOOL ChucK::setParam( const std::string & name, const std::string & value )
368 {
369 // lower case for consistency
370 std::string key = tolower(name);
371 // check read-only
372 if( readOnlyParam(key) ) return FALSE;
373 // check
374 if( m_params.count(key) > 0 && m_param_types[key] == ck_param_string )
375 {
376 m_params[key] = value;
377 return TRUE;
378 }
379 else
380 {
381 return FALSE;
382 }
383 }
384
385
386
387
388 //-----------------------------------------------------------------------------
389 // name: setParam()
390 // desc: set a string list param by name
391 //-----------------------------------------------------------------------------
392 t_CKBOOL ChucK::setParam( const std::string & name, const std::list< std::string > & value )
393 {
394 // lower case for consistency
395 std::string key = tolower(name);
396 // check read-only
397 if( readOnlyParam(key) ) return FALSE;
398 // check
399 if( m_listParams.count( key ) > 0 &&
400 m_param_types[key] == ck_param_string_list )
401 {
402 m_listParams[key] = value;
403 return TRUE;
404 }
405 else
406 {
407 return FALSE;
408 }
409 }
410
411
412
413
414 //-----------------------------------------------------------------------------
415 // name: getParamInt()
416 // desc: get an int param
417 //-----------------------------------------------------------------------------
418 t_CKINT ChucK::getParamInt( const std::string & name )
419 {
420 // lower case for consistency
421 std::string key = tolower(name);
422 t_CKINT result = 0;
423 // check
424 if( m_params.count( key ) > 0 && m_param_types[key] == ck_param_int )
425 {
426 std::istringstream s( m_params[key] );
427 s >> result;
428 }
429 return result;
430 }
431
432
433
434
435 //-----------------------------------------------------------------------------
436 // name: getParamFloat()
437 // desc: get a float param
438 //-----------------------------------------------------------------------------
439 t_CKFLOAT ChucK::getParamFloat( const std::string & name )
440 {
441 // lower case for consistency
442 std::string key = tolower(name);
443 t_CKFLOAT result = 0;
444 // check
445 if( m_params.count( key ) > 0 && m_param_types[key] == ck_param_float )
446 {
447 std::istringstream s( m_params[key] );
448 s >> result;
449 }
450 else if( m_params.count( key ) > 0 && m_param_types[key] == ck_param_int )
451 {
452 // get int and return as float | 1.5.4.2 (ge)
453 return (t_CKFLOAT)getParamInt(name);
454 }
455 return result;
456 }
457
458
459
460
461 //-----------------------------------------------------------------------------
462 // name: getParamString()
463 // desc: get a string param
464 //-----------------------------------------------------------------------------
465 std::string ChucK::getParamString( const std::string & name )
466 {
467 // lower case for consistency
468 std::string key = tolower(name);
469 // check
470 if( m_params.count( key ) > 0 && m_param_types[key] == ck_param_string )
471 {
472 return m_params[key];
473 }
474 else
475 {
476 return "";
477 }
478 }
479
480
481
482
483 //-----------------------------------------------------------------------------
484 // name: getParamStringList()
485 // desc: get a string param
486 //-----------------------------------------------------------------------------
487 std::list< std::string > ChucK::getParamStringList( const std::string & name )
488 {
489 // lower case for consistency
490 std::string key = tolower(name);
491 // check
492 if( m_listParams.count( key ) > 0 &&
493 m_param_types[key] == ck_param_string_list )
494 {
495 return m_listParams[key];
496 }
497 else
498 {
499 return std::list< std::string >();
500 }
501 }
502
503
504
505
506 //-----------------------------------------------------------------------------
507 // name: init()
508 // desc: initialize ChucK (using params)
509 //-----------------------------------------------------------------------------
510 t_CKBOOL ChucK::init()
511 {
512 // sanity check
513 if( m_init == TRUE )
514 {
515 EM_error2( 0, "VM already initialized..." );
516 return false;
517 }
518
519 // initialize VM
520 if( !initVM() ) goto cleanup;
521
522 // initialize compiler
523 if( !initCompiler() ) goto cleanup;
524
525 // initialize chugin system
526 if( !initChugins() ) goto cleanup;
527
528 // initialize OTF programming system
529 if( !initOTF() ) goto cleanup;
530
531 // did user init?
532 m_init = TRUE;
533
534 return true;
535
536 cleanup:
537 // shutdown, dealloc
538 shutdown();
539
540 return false;
541 }
542
543
544
545
546 //-----------------------------------------------------------------------------
547 // name: initVM()
548 // desc: initialize VM
549 //-----------------------------------------------------------------------------
550 t_CKBOOL ChucK::initVM()
551 {
552 // get VM params
553 t_CKUINT srate = getParamInt( CHUCK_PARAM_SAMPLE_RATE );
554 t_CKUINT outs = getParamInt( CHUCK_PARAM_OUTPUT_CHANNELS );
555 t_CKUINT ins = getParamInt( CHUCK_PARAM_INPUT_CHANNELS );
556 t_CKUINT adaptiveSize = getParamInt( CHUCK_PARAM_VM_ADAPTIVE );
557 t_CKBOOL halt = getParamInt( CHUCK_PARAM_VM_HALT ) != 0;
558
559 // instantiate VM
560 m_carrier->vm = new Chuck_VM();
561 // add reference (this will be released on shutdown)
562 CK_SAFE_ADD_REF( m_carrier->vm );
563 // reference back to carrier
564 m_carrier->vm->setCarrier( m_carrier );
565 // initialize VM
566 if( !m_carrier->vm->initialize( srate, outs, ins, adaptiveSize, halt ) )
567 {
568 EM_error2( 0, "%s", m_carrier->vm->last_error() );
569 return false;
570 }
571
572 return true;
573 }
574
575
576
577
578 //-----------------------------------------------------------------------------
579 // name: initCompiler()
580 // desc: initialize compiler
581 //-----------------------------------------------------------------------------
582 t_CKBOOL ChucK::initCompiler()
583 {
584 // get compiler params
585 t_CKBOOL dump = getParamInt( CHUCK_PARAM_DUMP_INSTRUCTIONS ) != 0;
586 t_CKBOOL auto_depend = getParamInt( CHUCK_PARAM_AUTO_DEPEND ) != 0;
587 t_CKUINT deprecate = getParamInt( CHUCK_PARAM_DEPRECATE_LEVEL );
588 std::string workingDir = getParamString( CHUCK_PARAM_WORKING_DIRECTORY );
589
590 // log
591 EM_log( CK_LOG_SYSTEM, "initializing compiler..." );
592 // push indent level
593 EM_pushlog();
594
595 // instantiate compiler
596 m_carrier->compiler = new Chuck_Compiler();
597 // reference back to carrier
598 m_carrier->compiler->setCarrier( m_carrier );
599
600 // initialize compiler
601 if( !m_carrier->compiler->initialize() )
602 {
603 EM_error2( 0, "compiler failed to initialize..." );
604 return false;
605 }
606 // set dump flag
607 m_carrier->compiler->emitter->dump = dump;
608 // set auto depend flag (for type checker) | currently must be FALSE
609 m_carrier->compiler->setAutoDepend( auto_depend );
610 // set deprecation level
611 m_carrier->env->deprecate_level = deprecate;
612
613 // pop indent for compiler
614 EM_poplog();
615 // log
616 EM_log( CK_LOG_SYSTEM, "initializing synthesis engine..." );
617 // push indent for synthesis
618 EM_pushlog();
619
620 // VM + type system integration (needs to be done after compiler)
621 if( !m_carrier->vm->initialize_synthesis() )
622 {
623 EM_error2( 0, "%s", m_carrier->vm->last_error() );
624 return false;
625 }
626
627 // pop indent
628 EM_poplog();
629
630 std::string cwd;
631 char * cstr_cwd = NULL; // 1.5.0.0 (barak) | was: char cstr_cwd[MAXPATHLEN];
632
633 // figure out current working directory (added 1.3.0.0)
634 // is this needed for current path to work correctly?!
635 // was: if( getcwd(cstr_cwd, MAXPATHLEN) == NULL )
636 // let getcwd allocate memory | 1.5.0.0 (barak)
637 if( (cstr_cwd = getcwd(NULL, 0)) == NULL )
638 {
639 // uh...
640 EM_log( CK_LOG_HERALD, "error: unable to determine current working directory!" );
641 }
642 else
643 {
644 // make c++ string
645 cwd = std::string(cstr_cwd);
646 // reclaim memory from getcwd
647 CK_SAFE_FREE(cstr_cwd);
648 // add trailing "/"
649 cwd += '/';
650 // deferring this step until later, and only for Windows
651 // cwd = normalize_directory_separator(cwd) + "/";
652
653 // 1.4.1.1 (ge) added -- expand for things like ~
654 workingDir = expand_filepath( workingDir, TRUE );
655
656 // 1.4.1.0 (ge) added -- test for absolute path
657 // 1.4.1.1 (ge) deals with windows absolute paths: e.g., "C:\"
658 // TODO: deals with windows network paths: e.g., "\\server\"???
659 if( is_absolute_path(workingDir) )
660 {
661 // do nothing here
662 }
663 #ifdef __ANDROID__
664 else if( workingDir.rfind("jar:", 0) == 0 )
665 {
666 // if workingDir is a JAR URL, make sure it ends on /
667 if( *workingDir.rbegin() != '/' )
668 {
669 workingDir = workingDir + "/";
670 }
671 // log it
672 // EM_log( CK_LOG_INFO, "setting current working directory: " );
673 }
674 #endif
675 else
676 {
677 // update
678 workingDir = cwd + workingDir;
679 }
680
681 #ifdef __PLATFORM_WINDOWS__
682 // normalize path separators, only for windows; in case UNIX-style paths contains actual backslashes
683 workingDir = normalize_directory_separator(workingDir);
684 #endif
685 // check if need to add /
686 // (note if workingDir is empty string, then this leaves it alone)
687 if( workingDir.length() > 0 && (workingDir[workingDir.length() - 1] != '/') )
688 {
689 // append
690 workingDir = workingDir + "/";
691 }
692 // update to current working directory
693 setParam( CHUCK_PARAM_WORKING_DIRECTORY, workingDir );
694 // log it
695 EM_log( CK_LOG_INFO, "setting current working directory: " );
696
697 // push log
698 EM_pushlog();
699 // print the working directory
700 EM_log( CK_LOG_INFO, "%s", workingDir.c_str() );
701 // pop log
702 EM_poplog();
703 }
704
705 return true;
706 }
707
708
709
710
711 //-----------------------------------------------------------------------------
712 // name: initChugin()
713 // desc: initialize chugin system (auto-load system chugins)
714 //-----------------------------------------------------------------------------
715 t_CKBOOL ChucK::initChugins()
716 {
717 // Chuck_VM_Code * code = NULL;
718 // Chuck_VM_Shred * shred = NULL;
719
720 // print whether chugins enabled
721 EM_log( CK_LOG_SYSTEM, "chugin system auto-load: %s", getParamInt( CHUCK_PARAM_CHUGIN_ENABLE ) ? "ON" : "OFF" );
722 EM_pushlog();
723 // print host version
724 EM_log( CK_LOG_HERALD, (std::string("host API version: ")+TC::green("%d.%d", true)).c_str(), CK_DLL_VERSION_MAJOR, CK_DLL_VERSION_MINOR );
725 EM_poplog();
726
727 // whether to auto-load chugins
728 t_CKINT auto_load = getParamInt( CHUCK_PARAM_CHUGIN_ENABLE );
729 // list of search pathes (added 1.3.0.0)
730 std::list<std::string> dl_search_path = getParamStringList( CHUCK_PARAM_IMPORT_PATH_SYSTEM );
731 // list of individually named chug-ins (added 1.3.0.0)
732 std::list<std::string> named_dls = getParamStringList( CHUCK_PARAM_USER_CHUGINS );
733
734 // whether or not chugins should be enabled (added 1.3.0.0)
735 // modified to load named chugins (--chugin) if present | 1.5.5.7 (ge) modified
736 if( auto_load || named_dls.size() )
737 {
738 //---------------------------------------------------------------------
739 // set origin hint | 1.5.0.0 (ge) added
740 m_carrier->compiler->m_originHint = ckte_origin_CHUGIN;
741 //---------------------------------------------------------------------
742
743 // log
744 EM_log( CK_LOG_SYSTEM, "loading%schugins...", (!auto_load && named_dls.size()) ? " (specific) " : " " );
745
746 // if not auto-load | 1.5.5.7 (ge) added
747 if( !auto_load )
748 {
749 // empty search paths to load in this function
750 dl_search_path.clear();
751 }
752
753 // chugin extension
754 std::string extension = ".chug";
755 #ifdef __EMSCRIPTEN__
756 // webchugins have extension ".chug.wasm" | 1.5.2.0 (terryzfeng) added
757 extension = "chug.wasm";
758 #endif
759 // load external libs | 1.5.0.4 (ge) enabled recursive search
760 if( !compiler()->load_external_modules( extension.c_str(), dl_search_path, named_dls, TRUE ) )
761 {
762 // clean up
763 goto error;
764 }
765
766 //---------------------------------------------------------------------
767 // 1.5.4.0 | .ck files are no longer auto compiled; need to be @import
768 /*---------------------------------------------------------------------
769 //---------------------------------------------------------------------
770 // set origin hint | 1.5.0.0 (ge) added
771 m_carrier->compiler->m_originHint = ckte_origin_IMPORT;
772 //---------------------------------------------------------------------
773 // log
774 EM_log( CK_LOG_SYSTEM, "loading chuck extensions..." );
775 EM_pushlog();
776
777 // iterate over list of ck files that the compiler found
778 for( std::list<std::string>::iterator j =
779 compiler()->m_cklibs_to_preload.begin();
780 j != compiler()->m_cklibs_to_preload.end(); j++)
781 {
782 // the filename
783 std::string filename = *j;
784
785 // log
786 EM_log( CK_LOG_HERALD, "preloading '%s'...", filename.c_str() );
787 // push indent
788 EM_pushlog();
789
790 // parse, type-check, and emit
791 // NOTE: filename here should already be a fullpath
792 if( compiler()->compileFile( filename ) )
793 {
794 // preserve op overloads | 1.5.1.5
795 compiler()->env()->op_registry.preserve();
796 // get the code
797 code = compiler()->output();
798 // name it | 1.5.2.0 (ge)
799 code->name = std::string("pre-load ck file: ") + filename;
800
801 // spork it
802 shred = vm()->spork( code, NULL, TRUE );
803 }
804 else // did not compile
805 {
806 // undo any op overloads | 1.5.1.5
807 compiler()->env()->op_registry.reset2local();
808 }
809
810 // pop indent
811 EM_poplog();
812 }
813
814 // clear the list of chuck files to preload
815 compiler()->m_cklibs_to_preload.clear();
816
817 // pop log
818 EM_poplog();
819 ---------------------------------------------------------------------*/
820 }
821
822 // unset origin hint | 1.5.0.0 (ge) added
823 m_carrier->compiler->m_originHint = ckte_origin_UNKNOWN;
824
825 return true;
826
827 error: // 1.4.1.0 (ge) added
828
829 // unset origin hint | 1.5.0.0 (ge) added
830 m_carrier->compiler->m_originHint = ckte_origin_UNKNOWN;
831
832 return false;
833 }
834
835
836
837
838 //-----------------------------------------------------------------------------
839 // name: probeChugins()
840 // desc: probe chugin system and print output
841 //-----------------------------------------------------------------------------
842 void ChucK::probeChugins()
843 {
844 // ck files to pre load
845 std::list<std::string> ck_libs_to_preload;
846 // host verison
847 std::ostringstream ostr; ostr << CK_DLL_VERSION_MAJOR << "." << CK_DLL_VERSION_MINOR;
848 // chugin extension
849 std::string extension = ".chug";
850 #ifdef __EMSCRIPTEN__
851 // webchugins have extension ".chug.wasm" | 1.5.2.0 (terryzfeng) added
852 extension = "chug.wasm";
853 #endif
854
855 // print whether chugins enabled
856 EM_log( CK_LOG_SYSTEM, "chugin system: %s", getParamInt( CHUCK_PARAM_CHUGIN_ENABLE ) ? "ON" : "OFF" );
857 // print host version
858 EM_log( CK_LOG_SYSTEM, "chugin host API version: %s", TC::green(ostr.str(),true).c_str() );
859 // push
860 EM_pushlog();
861 // print version compatibility information
862 EM_log( CK_LOG_SYSTEM, "chugin major version must == host major version" );
863 EM_log( CK_LOG_SYSTEM, "chugin minor version must <= host minor version" );
864 // print chuck language version
865 EM_log( CK_LOG_SYSTEM, "language version: %s", TC::green(CHUCK_VERSION_STRING,true).c_str() );
866 // pop
867 EM_poplog();
868
869 // list of search pathes (added 1.3.0.0; revisited 1.5.4.0)
870 // start with system paths (auto-load chugins; .ck files must be @imported)
871 std::list<std::string> dl_search_path = getParamStringList( CHUCK_PARAM_IMPORT_PATH_SYSTEM );
872 // next, process packages paths (e.g., as managed by ChuMP; no auto-load; all must be @imported)
873 std::list<std::string> packages_paths = getParamStringList( CHUCK_PARAM_IMPORT_PATH_PACKAGES);
874 // append packages paths to search paths
875 append_path_list( dl_search_path, packages_paths );
876 // iterate over packages paths | 1.5.4.1 (ge & nshaheed) added
877 for( std::list<std::string>::iterator it = packages_paths.begin(); it != packages_paths.end(); it++ )
878 {
879 // scan for subdirs, but only one-level in each packages path
880 scan_for_dirs_in_directory( *it, extension, FALSE, dl_search_path );
881 }
882 // finally, add user-managed search paths (no auto-load; all must be @imported)
883 append_path_list( dl_search_path, getParamStringList( CHUCK_PARAM_IMPORT_PATH_USER) );
884
885 // list of individually named chug-ins (added 1.3.0.0)
886 std::list<std::string> named_dls = getParamStringList( CHUCK_PARAM_USER_CHUGINS );
887
888 // log
889 EM_log( CK_LOG_SYSTEM, "probing chugins (.chug)..." );
890 // push indent level
891 // EM_pushlog();
892
893 // load external libs; recurse changed to FALSE in 1.5.4.0 (ge)
894 if( !Chuck_Compiler::probe_external_modules( extension.c_str(), dl_search_path, named_dls, FALSE, ck_libs_to_preload ) )
895 {
896 // warning
897 EM_log( CK_LOG_SYSTEM, "error probing chugins..." );
898 }
899 // pop log
900 // EM_poplog();
901
902 //-------------------------------------------------------------------------
903 // 1.5.4.0 | .ck files are no longer auto compiled; need to be @import
904 //-------------------------------------------------------------------------
905 // log
906 EM_log( CK_LOG_SYSTEM, "probing chuck files (.ck)..." );
907 EM_pushlog();
908
909 // iterate over list of ck files that the compiler found
910 for( std::list<std::string>::iterator j = ck_libs_to_preload.begin();
911 j != ck_libs_to_preload.end(); j++ )
912 {
913 // the filename
914 std::string filename = *j;
915 // log
916 logCKFileFound( filename, CK_LOG_SYSTEM );
917 // EM_log( CK_LOG_SYSTEM, "[%s] '%s'...", TC::green("FOUND",true).c_str(), filename.c_str() );
918 }
919
920 // check
921 if( ck_libs_to_preload.size() == 0 )
922 EM_log( CK_LOG_INFO, "(no chuck files found)" );
923
924 // pop log
925 EM_poplog();
926 }
927
928
929
930
931 //-----------------------------------------------------------------------------
932 // name: initOTF()
933 // desc: init OTF programming system
934 //-----------------------------------------------------------------------------
935 t_CKBOOL ChucK::initOTF()
936 {
937 #ifndef __DISABLE_OTF_SERVER__
938 // server
939 if( getParamInt( CHUCK_PARAM_OTF_ENABLE ) != 0 )
940 {
941 // get the port
942 m_carrier->otf_port = getParamInt( CHUCK_PARAM_OTF_PORT );
943 // log
944 EM_log( CK_LOG_SYSTEM, "starting OTF command listener on port: %d...",
945 m_carrier->otf_port );
946
947 // start tcp server
948 m_carrier->otf_socket = ck_tcp_create( 1 );
949 if( !m_carrier->otf_socket ||
950 !ck_bind( m_carrier->otf_socket, (int)m_carrier->otf_port ) ||
951 !ck_listen( m_carrier->otf_socket, 10 ) )
952 {
953 // get how to handle the warning | 1.5.1.2
954 t_CKINT printInsteadOfLog = getParamInt( CHUCK_PARAM_OTF_PRINT_WARNINGS )
955 && getLogLevel() < CK_LOG_SYSTEM;
956 // check
957 if( printInsteadOfLog )
958 {
959 // old school print warning
960 EM_error2( 0, "cannot bind to TCP port %li...", m_carrier->otf_port );
961 // provide more information
962 EM_error2( 0, " |- (cause: another chuck VM may currently have port %li open)", m_carrier->otf_port );
963 EM_error2( 0, " |- (implication: this VM cannot receive OTF commands)" );
964 }
965 else
966 {
967 // push log
968 EM_pushlog();
969
970 // new school log warning, as the warning can be confusing / cause automation issues | 1.5.1.2
971 // FYI see: https://github.com/ccrma/chuck/issues/352 (thanks @barak)
972 EM_log( CK_LOG_SYSTEM, TC::orange("cannot bind to TCP port %li...", true).c_str(), m_carrier->otf_port );
973 // log more information
974 EM_log( CK_LOG_INFO, "possible cause: network device unavailable..." );
975 EM_log( CK_LOG_INFO, "or another program currently has port %li open...", m_carrier->otf_port );
976 EM_pushlog();
977 EM_log( CK_LOG_INFO, "(e.g., another chuck VM)" );
978 EM_poplog();
979 EM_log( CK_LOG_INFO, "implication: this VM cannot receive OTF commands" );
980
981 // pop log
982 EM_poplog();
983 }
984
985 ck_close( m_carrier->otf_socket );
986 m_carrier->otf_socket = NULL;
987 }
988 else
989 {
990 #if !defined(__PLATFORM_WINDOWS__) || defined(__WINDOWS_PTHREAD__)
991 pthread_create( &m_carrier->otf_thread, NULL, otf_recv_cb, m_carrier );
992 #else
993 m_carrier->otf_thread = CreateThread( NULL, 0,
994 (LPTHREAD_START_ROUTINE)otf_recv_cb,
995 m_carrier, 0, 0 );
996 #endif
997 }
998 }
999 else
1000 {
1001 // log
1002 EM_log( CK_LOG_SYSTEM, "OTF command listener: OFF" );
1003 }
1004 #endif
1005 return true;
1006 }
1007
1008
1009
1010
1011 //-----------------------------------------------------------------------------
1012 // name: shutdown()
1013 // desc: shutdown ChucK instance
1014 //-----------------------------------------------------------------------------
1015 t_CKBOOL ChucK::shutdown()
1016 {
1017 // log
1018 EM_log( CK_LOG_SYSTEM, "shutting down ChucK instance..." );
1019 // push
1020 EM_pushlog();
1021
1022 // stop VM
1023 if( m_carrier != NULL && m_carrier->vm != NULL )
1024 {
1025 m_carrier->vm->stop();
1026
1027 // wait for it to stop...
1028 while( m_carrier->vm->running() )
1029 {
1030 ck_usleep(1000);
1031 }
1032 }
1033
1034 // free vm, compiler, friends
1035 // first, otf
1036 // REFACTOR-2017 TODO: le_cb?
1037
1038 // STK-specific
1039 stk_detach( m_carrier );
1040
1041 #ifndef __DISABLE_OTF_SERVER__
1042 // cancel otf thread
1043 if( m_carrier!= NULL )
1044 {
1045 // stop otf thread
1046 if( m_carrier->otf_thread )
1047 {
1048 #if !defined(__PLATFORM_WINDOWS__) || defined(__WINDOWS_PTHREAD__)
1049 pthread_cancel( m_carrier->otf_thread );
1050 #else
1051 CloseHandle( m_carrier->otf_thread ); // doesn't cancel thread
1052 #endif
1053 m_carrier->otf_thread = 0; // signals thread shutdown
1054 }
1055
1056 // close otf socket
1057 if( m_carrier->otf_socket )
1058 {
1059 ck_close( m_carrier->otf_socket );
1060 }
1061
1062 // reset
1063 m_carrier->otf_socket = NULL;
1064 m_carrier->otf_port = 0;
1065 }
1066 #endif // __DISABLE_OTF_SERVER__
1067
1068 // TODO: a different way to unlock?
1069 // unlock all objects to delete chout, cherr
1070 Chuck_VM_Object::unlock_all();
1071 // ensure we have a carrier
1072 if( m_carrier != NULL )
1073 {
1074 CK_SAFE_RELEASE( m_carrier->chout );
1075 CK_SAFE_RELEASE( m_carrier->cherr );
1076 }
1077 // relock
1078 Chuck_VM_Object::lock_all();
1079
1080 // ensure we have a carrier
1081 if( m_carrier != NULL )
1082 {
1083 // initiate VM shutdown but don't delete VM yet
1084 if( m_carrier->vm ) m_carrier->vm->shutdown();
1085 // delete compiler, including type system (m_carrier->env)
1086 CK_SAFE_DELETE( m_carrier->compiler );
1087 // verify the env pointer is NULL
1088 assert( m_carrier->env == NULL );
1089 // release VM (itself an Object)
1090 CK_SAFE_RELEASE( m_carrier->vm );
1091 }
1092
1093 // clear flag
1094 m_init = FALSE;
1095 // clear flag
1096 m_started = FALSE;
1097
1098 // log
1099 EM_log( CK_LOG_SYSTEM, "ChucK instance shutdown complete." );
1100 // pop
1101 EM_poplog();
1102
1103 // done
1104 return true;
1105 }
1106
1107
1108
1109
1110 //-----------------------------------------------------------------------------
1111 // name: compileFile()
1112 // desc: compile a file -> chuck bytecode -> spork as new shred
1113 // `argsTogether` is appended to `path` as additional arguments
1114 // the format of `argsTogether` is ':'-separated items e.g., "1:foo:5"
1115 // NOTE: `path` could have arguments already, e.g., "program.ck:5.5:bar"
1116 // if `immediate` == TRUE, the new shred(s) is shreduled immediately
1117 // (on the calling thread)
1118 // if `immediate` == FALSE, the new shreds(s) is queued and shreduled on
1119 // the next time step (by the VM compute/audio thread)
1120 // `count` specifies how many instances of the new shred to spork
1121 // returns TRUE if compilation successful (even if count == 0)
1122 // returns FALSE if compilation unsuccessful
1123 //-----------------------------------------------------------------------------
1124 t_CKBOOL ChucK::compileFile( const std::string & path,
1125 const std::string & argsTogether,
1126 t_CKUINT count, t_CKBOOL immediate,
1127 std::vector<t_CKUINT> * shredIDs)
1128 {
1129 // clear | 1.5.0.8
1130 if( shredIDs ) shredIDs->clear();
1131
1132 // sanity check
1133 if( !m_carrier->compiler )
1134 {
1135 // error
1136 EM_error2( 0, "compileFile() invoked before initialization..." );
1137 return FALSE;
1138 }
1139
1140 std::string filename;
1141 std::vector<std::string> args;
1142 Chuck_VM_Code * code = NULL;
1143 Chuck_VM_Shred * shred = NULL;
1144 std::string thePath;
1145 std::string full_path;
1146
1147 //-------------------------------------------------------------------------
1148 // set origin hint | 1.5.0.0 (ge) added
1149 m_carrier->compiler->m_originHint = ckte_origin_USERDEFINED;
1150 //-------------------------------------------------------------------------
1151
1152 // log
1153 EM_log( CK_LOG_HERALD, "compiling '%s'...", path.c_str() );
1154 // push indent
1155 EM_pushlog();
1156
1157 // path expansion (e.g., ~ for unix systems)
1158 thePath = expand_filepath(path);
1159 // log if the path exansion is different
1160 if( path != thePath )
1161 { EM_log( CK_LOG_FINE, "expanding path '%s'...", thePath.c_str() ); }
1162
1163 // append
1164 std::string theThing = thePath + ":" + argsTogether;
1165 #ifdef __ANDROID__
1166 if( theThing.rfind("jar:", 0) == 0 )
1167 {
1168 const size_t jarPrefixLen = strlen("jar:");
1169 const size_t secondColon = path.find(':', jarPrefixLen);
1170 if( secondColon == std::string::npos )
1171 {
1172 theThing = std::string("jar\\:") + theThing.substr(jarPrefixLen);
1173 }
1174 else
1175 {
1176 theThing = std::string("jar\\:") + theThing.substr(jarPrefixLen,
1177 secondColon - jarPrefixLen) + std::string("\\:") +
1178 theThing.substr(secondColon + 1);
1179 }
1180 }
1181 #endif // __ANDROID__
1182
1183 // parse out command line arguments
1184 if( !extract_args( theThing, filename, args ) )
1185 {
1186 // error
1187 EM_error2( 0, "malformed filename with argument list..." );
1188 EM_error2( 0, " --> '%s'", theThing.c_str() );
1189 goto error;
1190 }
1191
1192 // parse, type-check, and emit (full_path added 1.3.0.0)
1193 if( !m_carrier->compiler->compileFile( filename ) )
1194 goto error;
1195
1196 // get the code
1197 code = m_carrier->compiler->output();
1198 // (re) name it | 1.5.0.5 (ge) update from '+=' to '='
1199 code->name = path;
1200
1201 // log
1202 EM_log( CK_LOG_FINE, "sporking %d %s...", count,
1203 count == 1 ? "instance" : "instances" );
1204
1205 // spork it
1206 while( count > 0 )
1207 {
1208 // spork shred from code; shredule immediately or deferred;
1209 // as of 1.5.0.8, spork allocates the shred ID regardless...
1210 // previously for emscripten `immediate` => TRUE; this is no longer necessary
1211 shred = m_carrier->vm->spork( code, NULL, immediate );
1212
1213 // add args
1214 shred->args = args;
1215 // append the new ID | 1.5.0.8
1216 if( shredIDs ) shredIDs->push_back( shred->xid );
1217 // decrement count
1218 count--;
1219 }
1220
1221 // pop indent
1222 EM_poplog();
1223 // unset origin hint | 1.5.0.0 (ge) added
1224 m_carrier->compiler->m_originHint = ckte_origin_UNKNOWN;
1225
1226 return TRUE;
1227
1228 error: // 1.5.0.0 (ge) added
1229
1230 // pop indent
1231 EM_poplog();
1232 // unset origin hint | 1.5.0.0 (ge) added
1233 m_carrier->compiler->m_originHint = ckte_origin_UNKNOWN;
1234
1235 return FALSE;
1236 }
1237
1238
1239
1240
1241 //-----------------------------------------------------------------------------
1242 // name: compileCode()
1243 // desc: compile code/text -> chuck bytecode -> spork as new shred
1244 // `argsTogether` is appended to `path` as arguments
1245 // the format of `argsTogether` is ':'-separated items e.g., "1:foo:5"
1246 // if `immediate` == TRUE, the new shred(s) is shreduled immediately
1247 // (on the calling thread)
1248 // if `immediate` == FALSE, the new shreds(s) is queued and shreduled on
1249 // the next time step (on the VM compute/audio thread)
1250 // returns TRUE if compilation successful (even if count == 0)
1251 // returns FALSE if compilation unsuccessful
1252 // 'optFilepath' optionally specifies a file (name, or path ending in '/')
1253 // as basis for path-related operations, e.g., @import
1254 //-----------------------------------------------------------------------------
1255 t_CKBOOL ChucK::compileCode( const std::string & code,
1256 const std::string & argsTogether,
1257 t_CKUINT count, t_CKBOOL immediate,
1258 std::vector<t_CKUINT> * shredIDs,
1259 const std::string & optFilepath )
1260 {
1261 // clear | 1.5.0.8
1262 if( shredIDs ) shredIDs->clear();
1263
1264 // sanity check
1265 if( !m_carrier->compiler )
1266 {
1267 // error
1268 EM_error2( 0, "compileCode() invoked before initialization..." );
1269 return FALSE;
1270 }
1271
1272 std::vector<std::string> args;
1273 Chuck_VM_Code * vm_code = NULL;
1274 Chuck_VM_Shred * shred = NULL;
1275 std::string workingDir;
1276 std::string full_path;
1277
1278 //-------------------------------------------------------------------------
1279 // set origin hint | 1.5.0.0 (ge) added
1280 m_carrier->compiler->m_originHint = ckte_origin_USERDEFINED;
1281 //-------------------------------------------------------------------------
1282
1283 // log
1284 EM_log( CK_LOG_FINE, "compiling code from string..." );
1285 // push indent
1286 EM_pushlog();
1287
1288 // falsify filename / path for various logs
1289 std::string theThing = std::string(CHUCK_CODE_LITERAL_SIGNIFIER) + ":" + argsTogether;
1290 std::string fakefakeFilename = "<result file name goes here>";
1291
1292 // parse out command line arguments
1293 if( !extract_args( theThing, fakefakeFilename, args ) )
1294 {
1295 // error
1296 EM_error2( 0, "malformed filename with argument list..." );
1297 EM_error2( 0, " --> '%s'", theThing.c_str() );
1298 goto error;
1299 }
1300
1301 // parse, type-check, and emit
1302 if( !m_carrier->compiler->compileCode( code, optFilepath ) )
1303 goto error;
1304
1305 // get the code
1306 vm_code = m_carrier->compiler->output();
1307 // (re) name it (no path to append) | 1.5.0.5 (ge) update from '+=' to '='
1308 vm_code->name = CHUCK_CODE_LITERAL_SIGNIFIER;
1309
1310 // log
1311 EM_log( CK_LOG_FINE, "sporking %d %s...", count,
1312 count == 1 ? "instance" : "instances" );
1313
1314 // spork it
1315 while( count > 0 )
1316 {
1317 // spork shred from code; shredule immediately or deferred;
1318 // as of 1.5.0.8, spork allocates the shred ID regardless...
1319 // previously for emscripten `immediate` => TRUE; this is no longer necessary
1320 shred = m_carrier->vm->spork( vm_code, NULL, immediate );
1321
1322 // add args
1323 shred->args = args;
1324 // append the new ID | 1.5.0.8
1325 if( shredIDs ) shredIDs->push_back( shred->xid );
1326 // decrement count
1327 count--;
1328 }
1329
1330 // pop indent
1331 EM_poplog();
1332 // unset origin hint | 1.5.0.0 (ge) added
1333 m_carrier->compiler->m_originHint = ckte_origin_UNKNOWN;
1334
1335 return TRUE;
1336
1337 error: // 1.5.0.0 (ge) added
1338
1339 // pop indent
1340 EM_poplog();
1341 // unset origin hint | 1.5.0.0 (ge) added
1342 m_carrier->compiler->m_originHint = ckte_origin_UNKNOWN;
1343
1344 return FALSE;
1345 }
1346
1347
1348
1349
1350 //-----------------------------------------------------------------------------
1351 // name: start()
1352 // desc: start chuck instance
1353 //-----------------------------------------------------------------------------
1354 t_CKBOOL ChucK::start()
1355 {
1356 // sanity check
1357 if( m_carrier->vm == NULL )
1358 {
1359 EM_error2( 0, "cannot start, VM not initialized..." );
1360 return false;
1361 }
1362
1363 // if already started
1364 if( m_started )
1365 {
1366 // return VM running state
1367 return m_carrier->vm->running();
1368 }
1369
1370 // start the VM!
1371 if( !m_carrier->vm->running() ) m_carrier->vm->start();
1372
1373 // set flag
1374 m_started = TRUE;
1375
1376 // return state
1377 return m_carrier->vm->running();
1378 }
1379
1380
1381
1382
1383 //-----------------------------------------------------------------------------
1384 // name: run()
1385 // desc: run ChucK and synthesize audio for `numFrames`
1386 // `input`: the incoming input array of audio samples
1387 // `output`: the outgoing output array of audio samples
1388 // `numFrames` : the number of audio frames to run
1389 //-----------------------------------------------------------------------------
1390 void ChucK::run( const SAMPLE * input, SAMPLE * output, t_CKINT numFrames )
1391 {
1392 // make sure we started
1393 if( !m_started && !this->start() ) return;
1394
1395 // call the callback
1396 m_carrier->vm->run( numFrames, input, output );
1397 }
1398
1399
1400
1401
1402 //-----------------------------------------------------------------------------
1403 // name: removeAllShreds() | 1.5.4.4 (ge) added
1404 // desc: remove all shreds currently in the VM
1405 // |- (NOTE: not synchronous or truly immediate but is thread-safe;
1406 // |- this will happen at the top of the next VM compute() call)
1407 //-----------------------------------------------------------------------------
1408 void ChucK::removeAllShreds()
1409 {
1410 // check
1411 if( !m_carrier->vm ) return;
1412
1413 // request VM to remove all shreds asap (thread-safe)
1414 m_carrier->vm->remove_all_shreds();
1415 }
1416
1417
1418
1419
1420 //-----------------------------------------------------------------------------
1421 // name: globals()
1422 // desc: returns VM Globals Manager
1423 //-----------------------------------------------------------------------------
1424 Chuck_Globals_Manager * ChucK::globals()
1425 {
1426 // check pointer
1427 if( !m_carrier->vm ) return NULL;
1428 // check if running
1429 if( !m_carrier->vm->running() ) return NULL;
1430
1431 // return the thing!
1432 return m_carrier->vm->globals_manager();
1433 }
1434
1435
1436
1437
1438 //-----------------------------------------------------------------------------
1439 // name: setChoutCallback()
1440 // desc: provide a callback where Chout print statements are routed
1441 //-----------------------------------------------------------------------------
1442 t_CKBOOL ChucK::setChoutCallback( void (* callback)(const char *) )
1443 {
1444 if( !m_init || m_carrier->chout == NULL ) return FALSE;
1445 m_carrier->chout->set_output_callback( callback );
1446 return TRUE;
1447 }
1448
1449
1450
1451
1452 //-----------------------------------------------------------------------------
1453 // name: setCherrCallback()
1454 // desc: provide a callback where Cherr print statements are routed
1455 //-----------------------------------------------------------------------------
1456 t_CKBOOL ChucK::setCherrCallback( void (* callback)(const char *) )
1457 {
1458 if( !m_init || m_carrier->cherr == NULL ) return FALSE;
1459 m_carrier->cherr->set_output_callback( callback );
1460 return TRUE;
1461 }
1462
1463
1464
1465
1466 //-----------------------------------------------------------------------------
1467 // name: setStdoutCallback()
1468 // desc: provide a callback where stdout print statements are routed
1469 //-----------------------------------------------------------------------------
1470 t_CKBOOL ChucK::setStdoutCallback( void (* callback)(const char *) )
1471 {
1472 ck_set_stdout_callback( callback );
1473 return TRUE;
1474 }
1475
1476
1477
1478
1479 //-----------------------------------------------------------------------------
1480 // name: setStderrCallback()
1481 // desc: provide a callback where <<< >>> and stdout print statements are routed
1482 //-----------------------------------------------------------------------------
1483 t_CKBOOL ChucK::setStderrCallback( void (* callback)(const char *) )
1484 {
1485 ck_set_stderr_callback( callback );
1486 return TRUE;
1487 }
1488
1489
1490
1491
1492 //-----------------------------------------------------------------------------
1493 // name: globalInit()
1494 // desc: init all things once (for all ChucK instances)
1495 //-----------------------------------------------------------------------------
1496 t_CKBOOL ChucK::globalInit()
1497 {
1498 // sanity check
1499 if( o_isGlobalInit ) return FALSE;
1500
1501 // nothing to do, for now
1502
1503 // set flag
1504 o_isGlobalInit = TRUE;
1505
1506 // done
1507 return TRUE;
1508 }
1509
1510
1511
1512
1513 //-----------------------------------------------------------------------------
1514 // name: globalCleanup()
1515 // desc: clean up all things
1516 //-----------------------------------------------------------------------------
1517 void ChucK::globalCleanup()
1518 {
1519 // sanity check
1520 if( !o_isGlobalInit ) return;
1521 // set flag
1522 o_isGlobalInit = FALSE;
1523
1524 // log
1525 EM_log( CK_LOG_INFO, "detaching all resources..." );
1526 // push
1527 EM_pushlog();
1528
1529 //#ifndef __DISABLE_MIDI__
1530 // close midi file handles
1531 // midirw_detach();
1532 //#endif // __DISABLE_MIDI__
1533
1534 #ifndef __ALTER_HID__
1535 // shutdown HID
1536 HidInManager::cleanup( 100 );
1537 #endif // __ALTER_HID__
1538
1539 #ifndef __DISABLE_SERIAL__
1540 // shutdown serial
1541 Chuck_IO_Serial::shutdown();
1542 #endif
1543
1544 #ifndef __DISABLE_KBHIT__
1545 // shutdown kb loop
1546 KBHitManager::shutdown();
1547 #endif // __DISABLE_KBHIT__
1548
1549 // pop
1550 EM_poplog();
1551
1552 // REFACTOR-2017 TODO Ge:
1553 // stop le_cb, ...?
1554 }
1555
1556
1557
1558
1559 //-----------------------------------------------------------------------------
1560 // set log level -- this should eventually be per-VM?
1561 //-----------------------------------------------------------------------------
1562 void ChucK::setLogLevel( t_CKINT level )
1563 {
1564 EM_setlog( level );
1565 }
1566
1567
1568
1569
1570 //-----------------------------------------------------------------------------
1571 // get log level -- also per-VM?
1572 //-----------------------------------------------------------------------------
1573 t_CKINT ChucK::getLogLevel()
1574 {
1575 return g_loglevel;
1576 }
1577
1578
1579
1580
1581 //-----------------------------------------------------------------------------
1582 // name: poop()
1583 // desc: --poop
1584 //-----------------------------------------------------------------------------
1585 void ChucK::poop()
1586 {
1587 #ifndef __DISABLE_OTF_SERVER__
1588 ::uh();
1589 #endif
1590 }
1591
1592
1593
1594
1595 //-----------------------------------------------------------------------------
1596 // name: bind()
1597 // desc: additional native chuck bindings/types (use with extra caution)
1598 //-----------------------------------------------------------------------------
1599 t_CKBOOL ChucK::bind( f_ck_query queryFunc, const std::string & name )
1600 {
1601 // check if we have initialized a compiler
1602 if( !compiler() )
1603 {
1604 // error message
1605 EM_error2( 0, "cannot bind() -- ChucK/compiler is not initialized..." );
1606 // done
1607 return FALSE;
1608 }
1609
1610 // perform the bind
1611 return compiler()->bind( queryFunc, name );
1612 }
1613
1614
1615
1616
1617 //-----------------------------------------------------------------------------
1618 // name: setMainThreadHook()
1619 // desc: set a function pointer to call from the main thread loop
1620 // e.g., for graphics, MAUI, potentially from a chug-in
1621 //-----------------------------------------------------------------------------
1622 t_CKBOOL ChucK::setMainThreadHook(Chuck_DL_MainThreadHook * hook)
1623 {
1624 m_hook = hook;
1625 return true;
1626 }
1627
1628
1629
1630
1631 //-----------------------------------------------------------------------------
1632 // name: getMainThreadHook()
1633 // desc: get pointer to current function to be called from main loop
1634 //-----------------------------------------------------------------------------
1635 Chuck_DL_MainThreadHook * ChucK::getMainThreadHook()
1636 {
1637 return m_hook;
1638 }
1639
1640
1641
1642
1643 //-----------------------------------------------------------------------------
1644 // name: toggleGlobalColorTextoutput
1645 // set whether chuck will generate color output for its messages
1646 // this will set the corresponding parameter as well
1647 //-----------------------------------------------------------------------------
1648 void ChucK::toggleGlobalColorTextoutput( t_CKBOOL onOff )
1649 {
1650 this->setParam( CHUCK_PARAM_TTY_COLOR, onOff );
1651 }