Debian Codegraph @

src/host/chuck_main.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_main.cpp
28 // desc: chuck entry point | command line host
29 //
30 // v1.3.5.3: migration to chuck_system.* (see for more details)
31 //
32 // author: Ge Wang | http://www.gewang.com/
33 // (see chuck_system.*)
34 //-----------------------------------------------------------------------------
35 #include "chuck.h"
36 #include "chuck_audio.h"
37 #include "chuck_console.h"
38 #include "chuck_otf.h"
39 #include "util_platforms.h"
40 #include "util_string.h"
41 #include <signal.h>
42
43 #if defined(__PLATFORM_WINDOWS__)
44 #include <windows.h>
45 #include <processthreadsapi.h> // GetCurrentProcessId()
46 #else
47 #include <unistd.h>
48 #include <pthread.h>
49 #endif
50
51 using namespace std;
52
53
54
55
56 //-----------------------------------------------------------------------------
57 // function prototypes
58 //-----------------------------------------------------------------------------
59 t_CKBOOL init_shell( Chuck_Shell * shell, Chuck_Shell_UI * ui, Chuck_VM * vm );
60 void * shell_cb( void * p );
61 t_CKBOOL go( int argc, const char ** argv );
62 void EXIT_with_global_cleanup( int exitCode );
63 t_CKBOOL global_cleanup();
64 void all_stop();
65 void all_detach();
66 void usage();
67 void usage_query();
68 void uh();
69 t_CKBOOL get_count( const char * arg, t_CKUINT * out );
70 static void audio_cb( SAMPLE * in, SAMPLE * out, t_CKUINT numFrames,
71 t_CKUINT numInChans, t_CKUINT numOutChans, void * data );
72
73 // C functions
74 extern "C" void signal_int( int sig_num );
75 extern "C" void signal_pipe( int sig_num );
76
77
78
79 //-----------------------------------------------------------------------------
80 // global variables
81 //-----------------------------------------------------------------------------
82 // the one ChucK, for command line host
83 ChucK * the_chuck;
84 // thread id for shell
85 CHUCK_THREAD g_tid_shell = 0;
86 // default destination host name
87 char g_otf_dest[256] = "127.0.0.1";
88 // otf listener port
89 t_CKINT g_otf_port = 8888;
90
91 // the shell
92 Chuck_Shell * g_shell = NULL;
93 // shell shutdown flag
94 t_CKBOOL g_shutdown_shell = FALSE;
95 // global variables
96 t_CKUINT g_sigpipe_mode = 0;
97 // flag for Std.system( string )
98 t_CKBOOL g_enable_system_cmd = FALSE;
99 // flag for enabling shell, ge: 1.3.5.3
100 t_CKBOOL g_enable_shell = FALSE;
101 // flag for audio enable, ge: 1.3.5.3
102 t_CKBOOL g_enable_realtime_audio = TRUE;
103 // added 1.3.2.0 // moved from VM 1.3.5.3
104 f_mainthreadhook g_main_thread_hook = NULL;
105 f_mainthreadquit g_main_thread_quit = NULL;
106 void * g_main_thread_bindle = NULL;
107 // num vms running; used to quit le_cb thread // added 1.3.6.0
108 t_CKUINT g_num_vms_running = 0;
109
110
111
112 // priority stuff
113 #if defined(__PLATFORM_APPLE__)
114 t_CKINT g_priority = XThreadUtil::get_max_priority(); // was 80
115 t_CKINT g_priority_low = XThreadUtil::get_min_priority(); // was 60
116 #elif defined(__PLATFORM_WINDOWS__) && !defined(__WINDOWS_PTHREAD__)
117 t_CKINT g_priority = THREAD_PRIORITY_HIGHEST;
118 t_CKINT g_priority_low = THREAD_PRIORITY_HIGHEST;
119 #else
120 t_CKINT g_priority = 0x7fffffff;
121 t_CKINT g_priority_low = 0x7fffffff;
122 #endif
123
124
125
126
127 //-----------------------------------------------------------------------------
128 // name: main()
129 // desc: command line chuck entry point
130 //-----------------------------------------------------------------------------
131 #ifndef __CHUCK_NO_MAIN__
132 int main( int argc, const char ** argv )
133 #else
134 int chuck_main( int argc, const char ** argv )
135 #endif // __CHUCK_NO_MAIN__
136 {
137 // parse, init, run!
138 go( argc, argv );
139
140 // clean up
141 if( !global_cleanup() )
142 {
143 // we got here possibly because global_cleanup() is underway on
144 // another thread; wait a bit and give actual cleaner a chance to
145 // finish | 1.5.0.4 (ge) added
146 ck_usleep( 50000 );
147 }
148
149 return 0;
150 }
151
152
153
154
155 //-----------------------------------------------------------------------------
156 // name: audio_cb()
157 // desc: audio callback; on audio thread, called through RtAudio;
158 // in turn, this function calls up to chuck to compute next chunk
159 //-----------------------------------------------------------------------------
160 static void audio_cb( SAMPLE * in, SAMPLE * out, t_CKUINT numFrames,
161 t_CKUINT numInChans, t_CKUINT numOutChans, void * data )
162 {
163 // TODO: check channel numbers
164
165 // call up to ChucK
166 the_chuck->run( in, out, numFrames );
167 }
168
169
170
171
172 //-----------------------------------------------------------------------------
173 // name: version()
174 // desc: ...
175 //-----------------------------------------------------------------------------
176 static void version()
177 {
178 CK_FPRINTF_STDERR( "\n" );
179 CK_FPRINTF_STDERR( "%s", TC::reset().c_str() );
180 CK_FPRINTF_STDERR( "chuck version: %s\n", TC::green(ChucK::version(),true).c_str() );
181
182 // platform string
183 string platform = "";
184 // drivers string
185 string drivers = "";
186 // binary architecture string;
187 string archs = "";
188 // print space
189 t_CKINT space = 0;
190
191 #if defined(__PLATFORM_WINDOWS__)
192 platform = "Microsoft Windows";
193 #elif defined(__PLATFORM_LINUX__)
194 platform = "Linux";
195 #elif defined(__PLATFORM_APPLE__)
196 platform = "macOS";
197 #elif defined(__PLATFORM_EMSCRIPTEN__)
198 platform = "web";
199 #endif
200
201 // windows related
202 #if defined(__WINDOWS_ASIO__)
203 drivers += string( space ? " | " : "" ) + "ASIO"; space++;
204 #endif
205 #if defined(__WINDOWS_DS__)
206 drivers += string(space ? " | " : "") + "DirectSound"; space++;
207 #endif
208 #if defined(__WINDOWS_WASAPI__)
209 drivers += string(space ? " | " : "") + "WASAPI"; space++;
210 #endif
211
212 // mac
213 #if defined(__MACOSX_CORE__)
214 drivers += string( space ? " | " : "" ) + "CoreAudio"; space++;
215 #endif
216
217 // linux related
218 #if defined(__LINUX_ALSA__)
219 drivers += string(space ? " | " : "") + "ALSA"; space++;
220 #endif
221 #if defined(__LINUX_OSS__)
222 drivers += string(space ? " | " : "") + "OSS"; space++;
223 #endif
224 #if defined(__LINUX_PULSE__)
225 drivers += string(space ? " | " : "") + "PulseAudio"; space++;
226 #endif
227
228 // linux / neutral
229 #if defined(__LINUX_JACK__) || defined(__UNIX_JACK__)
230 drivers += string( space ? " | " : "" ) + "JACK"; space++;
231 #endif
232
233 // no drivers
234 if( space == 0 ) drivers = "(none)";
235
236 // check for universal binary
237 #if defined(__MACOS_UB__)
238 archs = " [universal binary]";
239 #endif
240
241 CK_FPRINTF_STDERR( " %s | %ld-bit%s\n", platform.c_str(), machine_intsize(), archs.c_str() );
242 CK_FPRINTF_STDERR( " audio driver%s: %s\n", space>1 ? "s" : "", drivers.c_str() );
243 CK_FPRINTF_STDERR( " http://chuck.cs.princeton.edu/\n" );
244 CK_FPRINTF_STDERR( " http://chuck.stanford.edu/\n\n" );
245 }
246
247
248
249
250 //-----------------------------------------------------------------------------
251 // name: usage()
252 // desc: print command line options
253 //-----------------------------------------------------------------------------
254 void usage()
255 {
256 // (note: optional colon added 1.3.0.0)
257 CK_FPRINTF_STDERR( "%s", TC::reset().c_str() );
258 CK_FPRINTF_STDERR( "\n%s %s%s %s", TC::bold("ChucK").c_str(), TC::color(TC::FG_GREEN,"=",TRUE).c_str(),
259 TC::color(TC::FG_LIGHT_GREEN,">",TRUE).c_str(), TC::bold("Music Programming Language").c_str() );
260 CK_FPRINTF_STDERR( "\nusage: %s --[%s|%s] [%s] file1 file2 ...\n\n", "chuck", TC::orange("options").c_str(), TC::blue("commands").c_str(), TC::blue("+-=^").c_str() );
261 CK_FPRINTF_STDERR( "%s", TC::set_orange().c_str() );
262 CK_FPRINTF_STDERR( " [options] = halt|loop|audio|silent|dump|nodump|about|probe\n" );
263 CK_FPRINTF_STDERR( " channels:<N>|out:<N>|in:<N>|dac:<N>|adc:<N>|driver:<name>\n" );
264 CK_FPRINTF_STDERR( " srate:<N>|bufsize:<N>|bufnum:<N>|shell|empty\n" );
265 CK_FPRINTF_STDERR( " remote:<hostname>|port:<N>|verbose:<N>|level:<N>\n" );
266 CK_FPRINTF_STDERR( " callback|deprecate:{stop|warn|ignore}|chugin-probe\n" );
267 CK_FPRINTF_STDERR( " chugin-load:{on|off}|chugin-path:<path>|chugin:<name>\n" );
268 CK_FPRINTF_STDERR( " color:{on|off}|pid-file:<path>|cmd-listener:{on|off}\n" );
269 CK_FPRINTF_STDERR( " query|query:<name>\n" );
270 CK_FPRINTF_STDERR( "%s", TC::set_blue().c_str() );
271 CK_FPRINTF_STDERR( " [commands] = add|replace|remove|remove.all|status|time|\n" );
272 CK_FPRINTF_STDERR( " clear.vm|reset.id|abort.shred|exit\n" );
273 CK_FPRINTF_STDERR( " [+-=^] = shortcuts for add, remove, replace, status\n" );
274 version();
275 }
276
277
278
279
280 //-----------------------------------------------------------------------------
281 // name: usage_query() | added 1.5.2.5 (nshaheed, ge)
282 // desc: print command line options for --query:<name>
283 //-----------------------------------------------------------------------------
284 void usage_query()
285 {
286 CK_FPRINTF_STDERR( "possible names for --query:<name>: \n" );
287 CK_FPRINTF_STDERR( " VERSION_LANGUAGE : language version (e.g., 1.5.2.0 (chai))\n" );
288 CK_FPRINTF_STDERR( " VERSION_CHUGIN_API : chugin API version (e.g., 10.1)\n" );
289 CK_FPRINTF_STDERR( " VERSION : (same as VERSION_LANGUAGE)\n" );
290 }
291
292
293
294
295 // non-NULL when a pid file has been written
296 const char * g_ck_pidfile = NULL;
297 // current process id
298 unsigned long g_ck_pid = 0;
299 //-----------------------------------------------------------------------------
300 // name: write_pid(filename) | 1.5.1.0 (@ynohtna)
301 // desc: writes the host's process id to the given file path
302 // returns true if successful, false otherwise.
303 //-----------------------------------------------------------------------------
304 bool write_pid( const char * filename )
305 {
306 #if defined(__PLATFORM_WIN32__)
307 // get process id and store in global variable
308 g_ck_pid = (unsigned long)GetCurrentProcessId();
309 #else
310 // get process id and store in global variable
311 g_ck_pid = (unsigned long)getpid();
312 #endif
313
314 // open file to write to
315 FILE * pidfile = fopen( filename, "w" );
316 // check file
317 if( !pidfile )
318 {
319 // problem
320 CK_FPRINTF_STDERR( "[chuck]: failed to store process ID %lu in %s\n", g_ck_pid, filename );
321 // clear id
322 g_ck_pid = 0;
323 // report
324 return false;
325 }
326
327 // write to file
328 fprintf( pidfile, "%lu", g_ck_pid );
329 // close the file
330 fclose( pidfile );
331 // done
332 return true;
333 }
334
335
336
337
338 //-----------------------------------------------------------------------------
339 // name: get_count()
340 // desc: get the thing after -- or - as number
341 //-----------------------------------------------------------------------------
342 t_CKBOOL get_count( const char * arg, t_CKUINT * out )
343 {
344 // no comment
345 if( !strncmp( arg, "--", 2 ) ) arg += 2;
346 else if( !strncmp( arg, "-", 1 ) ) arg += 1;
347 else return FALSE;
348
349 // end of string
350 if( *arg == '\0' ) return FALSE;
351 // not digit
352 if( *arg < '0' || *arg > '9' ) return FALSE;
353
354 // number
355 *out = (t_CKUINT)atoi( arg );
356
357 return TRUE;
358 }
359
360
361
362
363 //-----------------------------------------------------------------------------
364 // name: signal_int()
365 // desc: handler for ctrl-c (SIGINT). NB: on windows this is triggered
366 // in a separate thread. global_cleanup requires a mutex.
367 // ... or another synchronization mechanism
368 //-----------------------------------------------------------------------------
369 extern "C" void signal_int( int sig_num )
370 {
371 // log
372 CK_FPRINTF_STDERR( "[chuck]: cleaning up...\n" );
373
374 // clean up
375 if( !global_cleanup() )
376 {
377 // we got here possibly because global_cleanup() is underway on
378 // another thread; wait a bit and give actual cleaner a chance to
379 // finish | 1.5.0.4 (ge) added
380 ck_usleep( 50000 );
381 }
382
383 // exit with code
384 exit( 130 ); // 130 -> terminated by ctrl-c
385 }
386
387
388
389
390 //-----------------------------------------------------------------------------
391 // name: signal_pipe()
392 // desc: ...
393 //-----------------------------------------------------------------------------
394 extern "C" void signal_pipe( int sig_num )
395 {
396 CK_FPRINTF_STDERR( "[chuck]: sigpipe handled - broken pipe (no connection)...\n" );
397 if( g_sigpipe_mode )
398 {
399 // log
400 CK_FPRINTF_STDERR( "[chuck]: cleaning up...\n" );
401 // done
402 EXIT_with_global_cleanup( 141 ); // 141 -> SIGPIPE
403 }
404 }
405
406
407
408
409 //-----------------------------------------------------------------------------
410 // name: global_cleanup()
411 // desc: try to leave the campground better than you found it
412 //-----------------------------------------------------------------------------
413 t_CKBOOL global_cleanup()
414 {
415 // make sure we don't double clean
416 static bool s_already_cleaning = false;
417
418 // check | 1.5.0.0 (ge) added
419 if( s_already_cleaning )
420 {
421 // log | 1.5.0.0 (ge) added
422 EM_log( CK_LOG_INFO, "additional global cleanup not needed..." );
423 // done
424 return FALSE;
425 }
426
427 // log | 1.5.0.0 (ge) added
428 EM_log( CK_LOG_SYSTEM, "global cleanup initiating..." );
429 // set flag
430 s_already_cleaning = true;
431
432 // static XMutex s_mutex;
433 // s_mutex.acquire();
434
435 // REFACTOR-2017: shut down audio system
436 all_stop();
437
438 // request MIDI, etc. files be closed
439 // REFACTOR-2017: TODO; RESOLVED: used to be all_detach()
440 // need to call this before deleting the_chuck/the VM
441 ChucK::globalCleanup();
442
443 // delete the chuck
444 CK_SAFE_DELETE( the_chuck );
445
446 // delete shell and set to NULL | 1.4.1.0
447 if( g_enable_shell ) CK_SAFE_DELETE( g_shell );
448
449 // wait for the shell, if it is running
450 // does the VM reset its priority to normal before exiting?
451 //if( g_enable_shell )
452 // while( g_shell != NULL )
453 // ck_usleep(10000);
454
455 // REFACTOR-2017 TODO: Cancel otf, le_cb threads? Does this happen in ~ChucK()?
456 // things don't work so good on windows...
457 #if !defined(__PLATFORM_WINDOWS__) || defined(__WINDOWS_PTHREAD__)
458 // if( g_tid_otf ) pthread_cancel( g_tid_otf );
459 // if( g_tid_whatever ) pthread_cancel( g_tid_whatever );
460 #else
461 // close handle
462 // if( g_tid_otf ) CloseHandle( g_tid_otf );
463 #endif
464
465 // 1.5.0.0 (ge) | commented
466 // s_mutex.release();
467
468 // delete process id file
469 if( g_ck_pidfile )
470 {
471 // log
472 EM_log( CK_LOG_INFO, "cleaning up pid-file '%s'...", g_ck_pidfile );
473 // remove directory entry
474 unlink( g_ck_pidfile );
475 // reset
476 g_ck_pidfile = NULL;
477 }
478 // reset
479 g_ck_pid = 0;
480
481 return TRUE;
482 }
483
484
485
486
487 //-----------------------------------------------------------------------------
488 // name: EXIT_with_global_cleanup()
489 // desc: exit program after invoking global_cleanup()
490 //-----------------------------------------------------------------------------
491 void EXIT_with_global_cleanup( int exitCode )
492 {
493 // clean up after ourselves, the best we can
494 global_cleanup();
495 // exit program
496 exit( exitCode );
497 }
498
499
500
501
502 //-----------------------------------------------------------------------------
503 // name: all_stop()
504 // desc: ...
505 //-----------------------------------------------------------------------------
506 void all_stop()
507 {
508 if( g_enable_realtime_audio )
509 {
510 // argument is time to wait in ms | 1.5.1.3 (ge)
511 // this also seems to get rid of audio stutter on some macOS audio devices
512 ChuckAudio::shutdown( 100 );
513 }
514 // REFACTOR-2017: TODO: other things? le_cb?
515 }
516
517
518
519
520 //-----------------------------------------------------------------------------
521 // name: init_shell()
522 // desc: ...
523 //-----------------------------------------------------------------------------
524 t_CKBOOL init_shell( Chuck_Shell * shell, Chuck_Shell_UI * ui, Chuck_VM * vm )
525 {
526 // initialize shell UI
527 if( !ui->init() )
528 {
529 CK_FPRINTF_STDERR( "[chuck]: error starting shell UI...\n" );
530 return FALSE;
531 }
532
533 // initialize
534 if( !shell->init( vm, ui ) )
535 {
536 CK_FPRINTF_STDERR( "[chuck]: error starting shell...\n" );
537 return FALSE;
538 }
539
540 return TRUE;
541 }
542
543
544
545
546 //-----------------------------------------------------------------------------
547 // name: check_shell_shutdown()
548 // desc: check if we need to take action on shell
549 //-----------------------------------------------------------------------------
550 t_CKBOOL check_shell_shutdown()
551 {
552 if( g_enable_shell && g_shutdown_shell )
553 {
554 // set to false so we don't do this multiple time
555 g_shutdown_shell = FALSE;
556 // cleanup
557 global_cleanup();
558 // return yes
559 return TRUE;
560 }
561
562 return FALSE;
563 }
564
565
566
567
568 //-----------------------------------------------------------------------------
569 // name: shell_cb
570 // desc: thread routine
571 //-----------------------------------------------------------------------------
572 void * shell_cb( void * p )
573 {
574 Chuck_Shell * shell;
575 // log
576 EM_log( CK_LOG_INFO, "starting thread routine for shell..." );
577
578 // assuming this is absolutely necessary, an assert may be better
579 assert( p != NULL );
580 // get shell object pointer
581 shell = (Chuck_Shell *)p;
582
583 //atexit( wait_for_shell );
584
585 // run the shell
586 shell->run();
587
588 // flag to effect global shutdown from main thread
589 g_shutdown_shell = TRUE;
590
591 // log
592 EM_log( CK_LOG_INFO, "exiting thread routine for shell..." );
593
594 return NULL;
595 }
596
597
598
599
600 //-----------------------------------------------------------------------------
601 // name: go()
602 // desc: parse args, init stuff, run!
603 //-----------------------------------------------------------------------------
604 t_CKBOOL go( int argc, const char ** argv )
605 {
606 t_CKBOOL vm_halt = TRUE;
607 t_CKINT srate = CK_SAMPLE_RATE_DEFAULT;
608 t_CKBOOL force_srate = FALSE; // added 1.3.1.2
609 t_CKINT buffer_size = CK_BUFFER_SIZE_DEFAULT;
610 t_CKINT num_buffers = CK_NUM_BUFFERS_DEFAULT;
611 t_CKINT dac = 0;
612 t_CKINT adc = 0;
613 std::string dac_name = ""; // added 1.3.0.0
614 std::string adc_name = ""; // added 1.3.0.0
615 t_CKINT dac_chans = 2;
616 t_CKINT adc_chans = 2;
617 t_CKINT adc_chans_before_rtaudio = adc_chans; // added 1.5.0.0
618 t_CKBOOL dump = FALSE;
619 t_CKBOOL probe = FALSE;
620 t_CKBOOL probe_chugs = FALSE;
621 t_CKBOOL doAbout = FALSE;
622 t_CKBOOL doVersion = FALSE;
623 t_CKBOOL set_priority = FALSE;
624 t_CKBOOL auto_depend = FALSE;
625 t_CKBOOL block = FALSE;
626 // t_CKBOOL enable_shell = FALSE;
627 t_CKBOOL syntax_check_only = FALSE;
628 t_CKBOOL no_vm = FALSE;
629 t_CKBOOL load_hid = FALSE;
630 t_CKBOOL enable_server = TRUE;
631 t_CKBOOL do_watchdog = TRUE;
632 t_CKINT adaptive_size = 0;
633 t_CKINT log_level = CK_LOG_CORE;
634 t_CKINT deprecate_level = 1; // 1 == warn
635 t_CKINT chugin_load = 1; // 1 == auto (variable added 1.3.0.0)
636 // whether to enable this VM to receive OTF commands over network
637 t_CKBOOL enableOTF = TRUE;
638 // whether to print code quotes for compiler message | 1.5.0.5 (ge) added
639 t_CKBOOL suppress_error_quote = FALSE;
640 // color terminal output | 1.5.0.5 (ge) added
641 t_CKBOOL colorTerminal = TRUE;
642 // always output color terminal, regardless of TTY status | 1.5.1.8
643 t_CKBOOL colorTerminalAlways = FALSE;
644 string filename = "";
645 vector<string> args;
646 // audio driver | 1.5.0.0
647 string audio_driver = "";
648 // dac and adc devices names | 1.5.0.0
649 string dac_device_name = "";
650 string adc_device_name = "";
651 // default driver name | 1.5.0.1
652 if( g_enable_realtime_audio )
653 audio_driver = ChuckAudio::defaultDriverName();
654 // error message string
655 string errorMessage1;
656 string errorMessage2;
657
658 // do any ANSI code config | 1.5.0.5 (ge) added
659 // (e.g., windows need to explicitly be configured to process
660 // escape sequences, e.g., for color console output)
661 // NOTE: do this early in case printing is desired
662 ck_configANSI_ESCcodes();
663
664 // initial chug-in path (added 1.3.0.0)
665 // separate into system, packages, user | 1.5.4.0 (ge)
666 std::string import_path_system, import_path_packages, import_path_user;
667 // list of search paths (added 1.3.0.0)
668 std::list<std::string> dl_search_path_system, dl_search_path_packages, dl_search_path_user;
669 // list of potential replacement search paths (from command line) | 1.5.5.7 (ge) added
670 std::list<std::string> dl_search_path_system_replace, dl_search_path_packages_replace, dl_search_path_user_replace;
671 // remember whether replacements were specified
672 t_CKBOOL path_replace_system=FALSE, path_replace_packages=FALSE, path_replace_user=FALSE;
673
674 // 1.3.0.0 | if set as environment variable, get it; otherwise use default
675 // 1.5.4.0 | separate out into system, user, packages paths
676 if( getenv( g_envvar_path_system ) )
677 { import_path_system = getenv( g_envvar_path_system ); }
678 else { import_path_system = g_default_path_system; }
679
680 if( getenv( g_envvar_path_packages ) )
681 { import_path_packages = getenv( g_envvar_path_packages ); }
682 else { import_path_packages = g_default_path_packages; }
683
684 if( getenv( g_envvar_path_user ) )
685 { import_path_user = getenv( g_envvar_path_user ); }
686 else { import_path_user = g_default_path_user; }
687
688 // test for deprecation
689 if( getenv( g_envvar_path_deprecated ) )
690 {
691 // issue warning
692 EM_error2( 0, "(warning) environment variable '%s' is deprecated", g_envvar_path_deprecated );
693 EM_error2( 0, "...please use '%s' '%s' '%s' to set import search paths",
694 g_envvar_path_system, g_envvar_path_packages, g_envvar_path_user );
695 }
696
697 // parse the colon list into STL list (added 1.3.0.0)
698 parse_path_list( import_path_system, dl_search_path_system );
699 parse_path_list( import_path_packages, dl_search_path_packages );
700 parse_path_list( import_path_user, dl_search_path_user );
701 // list of individually named chug-ins (added 1.3.0.0)
702 std::list<std::string> named_dls;
703
704 #if defined(__DISABLE_WATCHDOG__)
705 do_watchdog = FALSE;
706 #elif defined(__PLATFORM_APPLE__)
707 do_watchdog = TRUE;
708 #elif defined(__PLATFORM_WINDOWS__) && !defined(__WINDOWS_PTHREAD__)
709 do_watchdog = TRUE;
710 #else
711 do_watchdog = FALSE;
712 #endif
713
714 t_CKUINT files = 0;
715 t_CKUINT count = 1;
716 t_CKINT i;
717
718 //------------------------- COMMAND-LINE ARGUMENTS -----------------------------
719
720 // parse command line args
721 for( i = 1; i < argc; i++ )
722 {
723 if( argv[i][0] == '-' || argv[i][0] == '+' ||
724 argv[i][0] == '=' || argv[i][0] == '^' || argv[i][0] == '@' )
725 {
726 if( !strcmp(argv[i], "--dump") || !strcmp(argv[i], "+d")
727 || !strcmp(argv[i], "--nodump") || !strcmp(argv[i], "-d") )
728 continue;
729 else if( get_count( argv[i], &count ) )
730 continue;
731 else if( !strcmp(argv[i], "--audio") || !strcmp(argv[i], "-a") )
732 g_enable_realtime_audio = TRUE;
733 else if( !strcmp(argv[i], "--silent") || !strcmp(argv[i], "-s") )
734 g_enable_realtime_audio = FALSE;
735 else if( !strcmp(argv[i], "--halt") || !strcmp(argv[i], "-t") )
736 vm_halt = TRUE;
737 else if( !strcmp(argv[i], "--loop") || !strcmp(argv[i], "-l") )
738 { vm_halt = FALSE; enable_server = TRUE; }
739 else if( !strcmp(argv[i], "--server") )
740 enable_server = TRUE;
741 else if( !strcmp(argv[i], "--standalone") )
742 enable_server = FALSE;
743 else if( !strcmp(argv[i], "--callback") )
744 block = FALSE;
745 // blocking removed, ge: 1.3.5.3
746 // else if( !strcmp(argv[i], "--blocking") )
747 // block = TRUE;
748 else if( !strcmp(argv[i], "--syntax") ) {
749 syntax_check_only = TRUE;
750 g_enable_realtime_audio = FALSE;
751 }
752 else if( !strcmp(argv[i], "--hid") )
753 load_hid = TRUE;
754 else if( !strcmp(argv[i], "--shell") || !strcmp( argv[i], "-e" ) )
755 { g_enable_shell = TRUE; vm_halt = FALSE; }
756 else if( !strcmp(argv[i], "--empty") )
757 no_vm = TRUE;
758 else if( !strncmp(argv[i], "--srate:", 8) ) // (added 1.3.0.0)
759 { srate = atoi( argv[i]+8 ) > 0 ? atoi( argv[i]+8 ) : srate; force_srate = TRUE; }
760 else if( !strncmp(argv[i], "--srate", 7) )
761 { srate = atoi( argv[i]+7 ) > 0 ? atoi( argv[i]+7 ) : srate; force_srate = TRUE; }
762 else if( !strncmp(argv[i], "-r", 2) )
763 { srate = atoi( argv[i]+2 ) > 0 ? atoi( argv[i]+2 ) : srate; force_srate = TRUE; }
764 else if( !strncmp(argv[i], "--bufsize:", 10) ) // (added 1.3.0.0)
765 buffer_size = atoi( argv[i]+10 ) > 0 ? atoi( argv[i]+10 ) : buffer_size;
766 else if( !strncmp(argv[i], "--bufsize", 9) )
767 buffer_size = atoi( argv[i]+9 ) > 0 ? atoi( argv[i]+9 ) : buffer_size;
768 else if( !strncmp(argv[i], "-b", 2) )
769 buffer_size = atoi( argv[i]+2 ) > 0 ? atoi( argv[i]+2 ) : buffer_size;
770 else if( !strncmp(argv[i], "--bufnum:", 9) ) // (added 1.3.0.0)
771 num_buffers = atoi( argv[i]+9 ) > 0 ? atoi( argv[i]+9 ) : num_buffers;
772 else if( !strncmp(argv[i], "--bufnum", 8) )
773 num_buffers = atoi( argv[i]+8 ) > 0 ? atoi( argv[i]+8 ) : num_buffers;
774 else if( !strncmp(argv[i], "-n", 2) )
775 num_buffers = atoi( argv[i]+2 ) > 0 ? atoi( argv[i]+2 ) : num_buffers;
776 else if( !strncmp(argv[i], "--driver:", 9))
777 audio_driver = argv[i]+9;
778 else if( !strncmp(argv[i], "--dac:", 6) ) // (added 1.3.0.0)
779 {
780 // advance pointer to beginning of argument
781 const char * str = argv[i]+6;
782 char * endptr = NULL;
783 long dev = strtol(str, &endptr, 10);
784
785 // check if arg was a number (added 1.3.0.0)
786 if( endptr != NULL && *endptr == '\0' )
787 {
788 // successful conversion to # -- clear adc_name
789 dac = dev;
790 dac_name = "";
791 }
792 // check if arg was not a number and not empty (added 1.3.0.0)
793 else if( *str != '\0' )
794 {
795 // incomplete conversion to #; see if its a possible device name
796 dac_name = std::string(str);
797 }
798 else
799 {
800 errorMessage1 = "invalid arguments for '--dac:'";
801 errorMessage2 = "(see 'chuck --help' for more info)";
802 break;
803 }
804 }
805 else if( !strncmp(argv[i], "--dac", 5) )
806 dac = atoi( argv[i]+5 ) > 0 ? atoi( argv[i]+5 ) : 0;
807 else if( !strncmp(argv[i], "--adc:", 6) ) // (added 1.3.0.0)
808 {
809 // advance pointer to beginning of argument
810 const char * str = argv[i]+6;
811 char * endptr = NULL;
812 long dev = strtol(str, &endptr, 10);
813
814 // check if arg was a number (added 1.3.0.0)
815 if( endptr != NULL && *endptr == '\0' )
816 {
817 // successful conversion to # -- clear adc_name
818 adc = dev;
819 adc_name = "";
820 }
821 // check if arg was number a number and not empty (added 1.3.0.0)
822 else if( *str != '\0' )
823 {
824 // incomplete conversion to #; see if its a possible device name
825 adc_name = std::string(str);
826 }
827 else
828 {
829 // error
830 errorMessage1 = "invalid arguments for '--adc:'";
831 errorMessage2 = "(see 'chuck --help' for more info)";
832 break;
833 }
834 }
835 else if( !strncmp(argv[i], "--adc", 5) )
836 adc = atoi( argv[i]+5 ) > 0 ? atoi( argv[i]+5 ) : 0;
837 else if( !strncmp(argv[i], "--channels:", 11) ) // (added 1.3.0.0)
838 dac_chans = adc_chans = atoi( argv[i]+11 ) > 0 ? atoi( argv[i]+11 ) : 2;
839 else if( !strncmp(argv[i], "--channels", 10) )
840 dac_chans = adc_chans = atoi( argv[i]+10 ) > 0 ? atoi( argv[i]+10 ) : 2;
841 else if( !strncmp(argv[i], "-c", 2) )
842 dac_chans = adc_chans = atoi( argv[i]+2 ) > 0 ? atoi( argv[i]+2 ) : 2;
843 else if( !strncmp(argv[i], "--out:", 6) ) // (added 1.3.0.0)
844 dac_chans = atoi( argv[i]+6 ) > 0 ? atoi( argv[i]+6 ) : 2;
845 else if( !strncmp(argv[i], "--out", 5) )
846 dac_chans = atoi( argv[i]+5 ) > 0 ? atoi( argv[i]+5 ) : 2;
847 else if( !strncmp(argv[i], "-o", 2) )
848 dac_chans = atoi( argv[i]+2 ) > 0 ? atoi( argv[i]+2 ) : 2;
849 else if( !strncmp(argv[i], "--in:", 5) ) // (added 1.3.0.0)
850 adc_chans = atoi( argv[i]+5 ) > 0 ? atoi( argv[i]+5 ) : 2;
851 else if( !strncmp(argv[i], "--in", 4) )
852 adc_chans = atoi( argv[i]+4 ) > 0 ? atoi( argv[i]+4 ) : 2;
853 else if( !strncmp(argv[i], "-i", 2) )
854 adc_chans = atoi( argv[i]+2 ) > 0 ? atoi( argv[i]+2 ) : 2;
855 else if( !strncmp(argv[i], "--level:", 8) ) // (added 1.3.0.0)
856 { g_priority = atoi( argv[i]+8 ); set_priority = TRUE; }
857 else if( !strncmp(argv[i], "--level", 7) )
858 { g_priority = atoi( argv[i]+7 ); set_priority = TRUE; }
859 else if( !strncmp(argv[i], "--watchdog", 10) )
860 { g_watchdog_timeout = atof( argv[i]+10 );
861 if( g_watchdog_timeout <= 0 ) g_watchdog_timeout = 0.5;
862 do_watchdog = TRUE; }
863 else if( !strncmp(argv[i], "--nowatchdog", 12) )
864 do_watchdog = FALSE;
865 else if( !strncmp(argv[i], "--remote:", 9) ) // (added 1.3.0.0)
866 strcpy( g_otf_dest, argv[i]+9 );
867 else if( !strncmp(argv[i], "--remote", 8) )
868 strcpy( g_otf_dest, argv[i]+8 );
869 else if( !strncmp(argv[i], "@", 1) )
870 strcpy( g_otf_dest, argv[i]+1 );
871 else if( !strncmp(argv[i], "--port:", 7) ) // (added 1.3.0.0)
872 g_otf_port = atoi( argv[i]+7 );
873 else if( !strncmp(argv[i], "--port", 6) )
874 g_otf_port = atoi( argv[i]+6 );
875 else if( !strncmp(argv[i], "-p", 2) )
876 g_otf_port = atoi( argv[i]+2 );
877 else if( !strncmp(argv[i], "--auto", 6) )
878 auto_depend = TRUE;
879 else if( !strncmp(argv[i], "-u", 2) )
880 auto_depend = TRUE;
881 else if( !strncmp(argv[i], "--log:", 6) ) // (added 1.3.0.0)
882 log_level = argv[i][6] ? atoi( argv[i]+6 ) : CK_LOG_INFO;
883 else if( !strncmp(argv[i], "--log", 5) )
884 log_level = argv[i][5] ? atoi( argv[i]+5 ) : CK_LOG_INFO;
885 else if( !strncmp(argv[i], "--verbose:", 10) ) // (added 1.3.0.0)
886 log_level = argv[i][10] ? atoi( argv[i]+10 ) : CK_LOG_INFO;
887 else if( !strncmp(argv[i], "--verbose", 9) )
888 log_level = argv[i][9] ? atoi( argv[i]+9 ) : CK_LOG_INFO;
889 else if( !strncmp(argv[i], "-v", 2) )
890 log_level = argv[i][2] ? atoi( argv[i]+2 ) : CK_LOG_INFO;
891 else if( !strncmp(argv[i], "--adaptive:", 11) ) // (added 1.3.0.0)
892 adaptive_size = argv[i][11] ? atoi( argv[i]+11 ) : -1;
893 else if( !strncmp(argv[i], "--adaptive", 10) )
894 adaptive_size = argv[i][10] ? atoi( argv[i]+10 ) : -1;
895 else if( !strncmp(argv[i], "--deprecate", 11) )
896 {
897 // get the rest
898 string arg = tolower(argv[i]+11);
899 if( arg == ":stop" ) deprecate_level = 0;
900 else if( arg == ":warn" ) deprecate_level = 1;
901 else if( arg == ":ignore" ) deprecate_level = 2;
902 else
903 {
904 // error
905 errorMessage1 = "invalid arguments for '--deprecate:'";
906 errorMessage2 = " |- looking for STOP, WARN, or IGNORE";
907 break;
908 }
909 }
910 // (added 1.3.0.0)
911 else if( !strncmp(argv[i], "--chugin-load:", sizeof("--chugin-load:")-1) )
912 {
913 // get the rest
914 string arg = tolower(argv[i]+sizeof("--chugin-load:")-1);
915 if( arg == "off" ) chugin_load = 0;
916 else if( arg == "on" || arg == "auto") chugin_load = 1;
917 else
918 {
919 // error
920 errorMessage1 = "invalid arguments for '--chugin-load:'";
921 errorMessage2 = " |- looking for ON, OFF, or AUTO";
922 break;
923 }
924 }
925 // set system search path | 1.5.5.7 (ge) added
926 // if set, this will replace chuck's system search paths
927 // NOTE: this is useful for deploying self-contained chuck programs
928 // sandboxed from any installed chugins/chuck-based libraries on host computer
929 else if( !strncmp(argv[i], "--chugin-path-system:", sizeof("--chugin-path-system:")-1) )
930 {
931 // flag it
932 path_replace_system = TRUE;
933 // get the argument payload
934 string s = argv[i]+sizeof("--chugin-path-system:")-1;
935 // if not empty string, add to list
936 if( trim(s) != "" ) parse_path_list( s, dl_search_path_system_replace );
937 }
938 // set packages search path | 1.5.5.7 (ge) added
939 // if set, this will replace chuck's packages search paths
940 // NOTE: this is useful for deploying self-contained chuck programs
941 // sandboxed from any installed chugins/chuck-based libraries on host computer
942 else if( !strncmp(argv[i], "--chugin-path-packages:", sizeof("--chugin-path-packages:")-1) )
943 {
944 // flag it
945 path_replace_packages = TRUE;
946 // get the argument payload
947 string s = argv[i]+sizeof("--chugin-path-packages:")-1;
948 // if not empty string, add to list
949 if( trim(s) != "" ) parse_path_list( s, dl_search_path_packages_replace );
950 }
951 // set user search path | 1.5.5.7 (ge) added
952 // if set, this will replace chuck's user search paths
953 // NOTE: this is useful for deploying self-contained chuck programs
954 // sandboxed from any installed chugins/chuck-based libraries on host computer
955 else if( !strncmp(argv[i], "--chugin-path-user:", sizeof("--chugin-path-user:")-1) )
956 {
957 // flag it
958 path_replace_user = TRUE;
959 // get the argument payload
960 string s = argv[i]+sizeof("--chugin-path-user:")-1;
961 // if not empty string, add to list
962 if( trim(s) != "" ) parse_path_list( s, dl_search_path_user_replace );
963 }
964 // add to system path(s); chugins in system paths are auto-loaded...
965 // .ck files can be @imported | 1.5.4.2 (ge) added
966 else if( !strncmp(argv[i], "--chugin-path-auto-load:", sizeof("--chugin-path-auto-load:")-1) )
967 {
968 // get the argument payload
969 string s = argv[i]+sizeof("--chugin-path-auto-load:")-1;
970 // if not empty string, add to list
971 if( trim(s) != "" ) parse_path_list( s, dl_search_path_system );
972 }
973 // (added 1.3.0.0)
974 else if( !strncmp(argv[i], "--chugin-path:", sizeof("--chugin-path:")-1) )
975 {
976 // get the argument payload
977 string s = argv[i]+sizeof("--chugin-path:")-1;
978 // if not empty string, add to list
979 if( trim(s) != "" ) parse_path_list( s, dl_search_path_user );
980 }
981 // (added 1.3.0.0)
982 else if( !strncmp(argv[i], "-G", sizeof("-G")-1) )
983 {
984 // get the argument payload
985 string s = argv[i]+sizeof("-G")-1;
986 // if not empty string, add to list
987 if( trim(s) != "" ) parse_path_list( s, dl_search_path_user );
988 }
989 // (added 1.3.0.0)
990 else if( !strncmp(argv[i], "--chugin:", sizeof("--chugin:")-1) )
991 {
992 named_dls.push_back(argv[i]+sizeof("--chugin:")-1);
993 }
994 else if( !strncmp(argv[i], "-g", sizeof("-g")-1) )
995 {
996 named_dls.push_back(argv[i]+sizeof("-g")-1);
997 }
998 // (added 1.5.0.5)
999 else if( !strncmp(argv[i], "--color:", sizeof("--color:")-1) )
1000 {
1001 // get the rest
1002 string arg = tolower(argv[i]+sizeof("--color:")-1);
1003 if( arg == "off" ) colorTerminal = false;
1004 else if( arg == "on" || arg == "auto")
1005 {
1006 colorTerminal = TRUE;
1007 // if explicitly ON, then always output ANSI color codes | 1.5.1.8
1008 if( arg == "on" ) colorTerminalAlways = TRUE;
1009 }
1010 else
1011 {
1012 // error
1013 errorMessage1 = "invalid arguments for '--color:'...";
1014 errorMessage2 = "...(looking for ON, OFF, or AUTO)";
1015 break;
1016 }
1017 }
1018 else if( tolower(argv[i]) == "--color" )
1019 {
1020 colorTerminal = TRUE;
1021 colorTerminalAlways = TRUE;
1022 }
1023 else if( tolower(argv[i]) == "--no-color" )
1024 colorTerminal = FALSE;
1025 else if( !strncmp(argv[i], "--pid-file:", sizeof("--pid-file:")-1) )
1026 {
1027 // get argument for writing PID file | 1.5.1.0 (@ynohtna) added
1028 const char * filename = argv[i] + sizeof("--pid-file:")-1;
1029 // write the process ID file |
1030 if( write_pid(filename) ) {
1031 // save file name
1032 g_ck_pidfile = filename;
1033 }
1034 }
1035 else if( !strcmp( argv[i], "--probe" ) )
1036 probe = TRUE;
1037 else if( !strcmp( argv[i], "--chugin-probe" ) )
1038 probe_chugs = TRUE;
1039 else if( !strcmp( argv[i], "--disable-error-show-code" ) )
1040 suppress_error_quote = TRUE;
1041 else if( !strncmp(argv[i], "--cmd-listener:", sizeof("--cmd-listener:")-1) )
1042 {
1043 // get the rest
1044 string arg = tolower(argv[i]+sizeof("--cmd-listener:")-1);
1045 if( arg == "off" ) enableOTF = false;
1046 else if( arg == "on" || arg == "auto") enableOTF = true;
1047 else
1048 {
1049 // error
1050 errorMessage1 = "invalid arguments for '--cmd-listener:'...";
1051 errorMessage2 = "...(looking for ON, OFF, or AUTO)";
1052 break;
1053 }
1054 }
1055 // (added 1.3.0.0)
1056 else if( !strcmp( argv[i], "--no-cmd-listener" ) )
1057 {
1058 // don't use this new vm for otf commands (use the previous one)
1059 enableOTF = FALSE;
1060 }
1061 else if( !strcmp( argv[i], "--poop" ) )
1062 uh();
1063 else if( !strcmp( argv[i], "--caution-to-the-wind" ) )
1064 g_enable_system_cmd = TRUE;
1065 else if( !strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")
1066 || !strcmp(argv[i], "--about") )
1067 {
1068 doAbout= TRUE;
1069 }
1070 else if( !strcmp( argv[i], "--version" ) )
1071 {
1072 doVersion = TRUE;
1073 }
1074 else if( !strncmp( argv[i], "--query:", 8 ) ) // added 1.5.2.5 (nshaheed)
1075 {
1076 // advance pointer to beginning of argument
1077 string str = ::tolower(argv[i]+8);
1078
1079 // check <name>
1080 if( str == "version_language" || str == "version" )
1081 {
1082 CK_FPRINTF_STDOUT( "%s\n", CHUCK_VERSION_STRING );
1083 }
1084 else if( str == "version_chugin_api" )
1085 {
1086 CK_FPRINTF_STDOUT( "%d.%d\n", CK_DLL_VERSION_MAJOR, CK_DLL_VERSION_MINOR );
1087 }
1088 else // no match
1089 {
1090 // print error
1091 EM_error2( 0, "invalid argument '%s' for '--query:<name>'", str.c_str() );
1092 // print --query usage
1093 usage_query();
1094 }
1095
1096 // clean up and exit
1097 EXIT_with_global_cleanup( 0 );
1098 }
1099 else if( !strncmp( argv[i], "--query", 7 ) ) // added 1.5.2.5 (nshaheed)
1100 {
1101 // print --query:<name> usage
1102 usage_query();
1103 // clean up and exit
1104 EXIT_with_global_cleanup( 0 );
1105 }
1106 else
1107 {
1108 // boost log level
1109 g_otf_log = CK_LOG_CORE;
1110 // flag
1111 t_CKINT is_otf = FALSE;
1112 // log level
1113 EM_setlog( log_level );
1114
1115 // do it
1116 if( otf_send_cmd( argc, argv, i, g_otf_dest, g_otf_port, &is_otf ) )
1117 {
1118 // done
1119 EXIT_with_global_cleanup( 0 );
1120 }
1121
1122 // is otf
1123 if( is_otf )
1124 {
1125 // done
1126 EXIT_with_global_cleanup( 1 );
1127 }
1128
1129 // done
1130 errorMessage1 = string("invalid flag '") + argv[i] + "' (see --help/-h)";
1131 break;
1132 }
1133 }
1134 else // doesn't look like an argument
1135 {
1136 // increase number of files
1137 files++;
1138 }
1139 }
1140
1141 // check if we output to TTY (teletype interactive terminal)
1142 // if not disable color teriminal to avoid outputing
1143 // ANSI escape codes to the output stream, which would
1144 // show up in | and >
1145 if( !colorTerminalAlways && !ck_isatty() ) colorTerminal = false;
1146
1147 //------------------ CHUCK PRE-INITIALIZATION ---------------------
1148 // instantiate a ChucK!
1149 the_chuck = new ChucK();
1150
1151 // set the color terminal global setting
1152 the_chuck->setParam( CHUCK_PARAM_TTY_COLOR, colorTerminal );
1153
1154 // set log level so things can start logging
1155 the_chuck->setLogLevel( log_level );
1156
1157 // log PID file output
1158 if( g_ck_pidfile )
1159 { EM_log( CK_LOG_INFO, "process ID '%lu' stored in '%s'...", g_ck_pid, g_ck_pidfile ); }
1160 //-----------------------------------------------------------------
1161
1162 // check for error message from command-line arguments
1163 if( errorMessage1 != "" ) EM_error2( 0, errorMessage1.c_str() );
1164 if( errorMessage2 != "" ) EM_error2( 0, errorMessage2.c_str() );
1165 // if either
1166 if( errorMessage1.length() || errorMessage2.length() )
1167 {
1168 // see if need to print usage
1169 if( doAbout ) usage();
1170 // done
1171 EXIT_with_global_cleanup( 1 );
1172 }
1173
1174 //-----------------------------------------------------------------
1175 // about
1176 //-----------------------------------------------------------------
1177 if( doAbout )
1178 {
1179 // print usage info
1180 usage();
1181 // done
1182 EXIT_with_global_cleanup( 0 );
1183 }
1184 //-----------------------------------------------------------------
1185 // version
1186 //-----------------------------------------------------------------
1187 if( doVersion )
1188 {
1189 // print version
1190 version();
1191 // done
1192 EXIT_with_global_cleanup( 0 );
1193 }
1194 //-----------------------------------------------------------------
1195 // probe
1196 //-----------------------------------------------------------------
1197 if( probe )
1198 {
1199 // ensure log level is at least SYSTEM
1200 if( the_chuck->getLogLevel() < CK_LOG_SYSTEM )
1201 the_chuck->setLogLevel( CK_LOG_SYSTEM );
1202
1203 // probe default/selected audio driver
1204 ChuckAudio::probe( audio_driver.c_str() );
1205
1206 #ifndef __DISABLE_MIDI__
1207 // probe MIDI input and output devices
1208 EM_error2b( 0, "" );
1209 probeMidiIn();
1210 EM_error2b( 0, "" );
1211 probeMidiOut();
1212 EM_error2b( 0, "" );
1213 #endif // __DISABLE_MIDI__
1214
1215 // probe HID devices
1216 HidInManager::probeHidIn();
1217
1218 // restore log level
1219 the_chuck->setLogLevel( log_level );
1220 // done
1221 EXIT_with_global_cleanup( 0 );
1222 }
1223
1224 // set caution to wind
1225 ChucK::enableSystemCall = g_enable_system_cmd;
1226
1227 // check buffer size
1228 buffer_size = ck_ensurepow2( buffer_size );
1229 // check mode and blocking
1230 if( !g_enable_realtime_audio && !block ) block = TRUE;
1231 // audio, boost 1.4.1.0 (ge) commented out
1232 // if( !set_priority && !block ) g_priority = g_priority_low;
1233 if( !set_priority && !g_enable_realtime_audio ) g_priority = 0x7fffffff;
1234 // set priority
1235 XThreadUtil::our_priority = g_priority;
1236 // set watchdog
1237 g_do_watchdog = do_watchdog;
1238 // set adaptive size
1239 if( adaptive_size < 0 ) adaptive_size = buffer_size;
1240
1241 // no files or --loop
1242 if( !files && vm_halt && !g_enable_shell && !probe_chugs )
1243 {
1244 // print error
1245 EM_error2( 0, "no input files... (try --help/-h)" );
1246 // done
1247 EXIT_with_global_cleanup( 1 );
1248 }
1249
1250 // shell initialization without vm
1251 if( g_enable_shell && no_vm )
1252 {
1253 // instantiate
1254 g_shell = new Chuck_Shell;
1255 // initialize
1256 if( !init_shell( g_shell, new Chuck_Console(), NULL ) )
1257 {
1258 // done
1259 EXIT_with_global_cleanup( 1 );
1260 }
1261 // no vm is needed, just start running the shell now
1262 g_shell->run();
1263 // clean up
1264 CK_SAFE_DELETE( g_shell );
1265 // done
1266 EXIT_with_global_cleanup( 0 );
1267 }
1268
1269 // make sure vm
1270 if( no_vm )
1271 {
1272 // error
1273 CK_FPRINTF_STDERR( "[chuck]: '--empty' can only be used with shell...\n" );
1274 // done
1275 EXIT_with_global_cleanup( 1 );
1276 }
1277
1278 // find dac_name if appropriate (added 1.3.0.0)
1279 if( !probe_chugs && dac_name.size() > 0 )
1280 {
1281 // check with RtAudio
1282 t_CKINT dev = ChuckAudio::device_named( audio_driver.c_str(), dac_name, TRUE, FALSE );
1283 if( dev >= 0 )
1284 {
1285 dac = dev;
1286 }
1287 else
1288 {
1289 CK_FPRINTF_STDERR( "[chuck]: unable to find dac '%s'...\n", dac_name.c_str() );
1290 CK_FPRINTF_STDERR( "[chuck]: | (try --probe to enumerate audio device info)\n" );
1291 // done
1292 EXIT_with_global_cleanup( 1 );
1293 }
1294 }
1295
1296 // find adc_name if appropriate (added 1.3.0.0)
1297 if( !probe_chugs && adc_name.size() > 0 )
1298 {
1299 // check with RtAudio
1300 t_CKINT dev = ChuckAudio::device_named( audio_driver.c_str(), adc_name, FALSE, TRUE );
1301 if( dev >= 0 )
1302 {
1303 adc = dev;
1304 }
1305 else
1306 {
1307 CK_FPRINTF_STDERR( "[chuck]: unable to find adc '%s'...\n", adc_name.c_str() );
1308 CK_FPRINTF_STDERR( "[chuck]: | (try --probe to enumerate audio device info)\n" );
1309 // done
1310 EXIT_with_global_cleanup( 1 );
1311 }
1312 }
1313
1314
1315 //-------------------- VIRTUAL MACHINE SETUP (PART 1) ---------------------
1316 // set chugins parameters
1317 the_chuck->setParam( CHUCK_PARAM_CHUGIN_ENABLE, chugin_load );
1318 the_chuck->setParam( CHUCK_PARAM_USER_CHUGINS, named_dls );
1319
1320 // if at least one specified, replace | 1.5.5.7 (ge)
1321 if( path_replace_system ) // system
1322 {
1323 // if empty list then clear
1324 if( dl_search_path_system_replace.size() == 0 ) { dl_search_path_system.clear(); }
1325 else { dl_search_path_system = dl_search_path_system_replace; }
1326 }
1327 if( path_replace_packages ) // packages
1328 {
1329 // if empty list then clear
1330 if( dl_search_path_packages_replace.size() == 0 ) { dl_search_path_packages.clear(); }
1331 else { dl_search_path_packages = dl_search_path_packages_replace; }
1332 }
1333 if( path_replace_user ) // user
1334 {
1335 // if empty list then clear
1336 if( dl_search_path_user_replace.size() == 0 ) { dl_search_path_user.clear(); }
1337 else { dl_search_path_user = dl_search_path_user_replace; }
1338 }
1339 // set chugin search paths
1340 the_chuck->setParam( CHUCK_PARAM_IMPORT_PATH_SYSTEM, dl_search_path_system );
1341 the_chuck->setParam( CHUCK_PARAM_IMPORT_PATH_PACKAGES, dl_search_path_packages );
1342 the_chuck->setParam( CHUCK_PARAM_IMPORT_PATH_USER, dl_search_path_user );
1343
1344 //-----------------------------------------------------------------
1345 // probe chugins | 1.5.0.4 (ge) added
1346 //-----------------------------------------------------------------
1347 if( probe_chugs )
1348 {
1349 // ensure log level is at least SYSTEM
1350 if( the_chuck->getLogLevel() < CK_LOG_SYSTEM )
1351 the_chuck->setLogLevel( CK_LOG_SYSTEM );
1352
1353 // probe chugins (.chug and .ck modules)
1354 // print/log what ChucK would load with current settings
1355 the_chuck->probeChugins();
1356 // done
1357 EXIT_with_global_cleanup( 0 );
1358 }
1359
1360 // log
1361 EM_log( CK_LOG_SYSTEM, "initializing chuck..." );
1362 // push
1363 EM_pushlog();
1364
1365 //--------------------------- AUDIO I/O SETUP -----------------------------
1366 // log
1367 EM_log( CK_LOG_SYSTEM, "initializing system settings..." );
1368 // push
1369 EM_pushlog();
1370 // log
1371 // EM_log( CK_LOG_SYSTEM, "setting up for '%s' audio I/O...", g_enable_realtime_audio ? "real-time" : "silent mode" );
1372
1373 // remember this, since adc_chans could be altered when initialization real-time audio
1374 adc_chans_before_rtaudio = adc_chans;
1375
1376 // initialize audio system
1377 if( g_enable_realtime_audio )
1378 {
1379 // initialize real-time audio
1380 t_CKBOOL retval = ChuckAudio::initialize( dac, adc, dac_chans, adc_chans,
1381 srate, buffer_size, num_buffers, audio_cb, (void *)the_chuck, force_srate, audio_driver.c_str() );
1382 // check return code
1383 if( !retval )
1384 {
1385 // 1.5.2.5 (ge) update to always print (not log) on error
1386 CK_FPRINTF_STDERR( "[chuck]: attempting to initialize real-time audio I/O...\n" );
1387 // log
1388 CK_FPRINTF_STDERR( "[chuck]: | real-time audio: %s\n", g_enable_realtime_audio ? "YES" : "NO" );
1389 CK_FPRINTF_STDERR( "[chuck]: | mode: %s\n", block ? "BLOCKING" : "CALLBACK" );
1390 CK_FPRINTF_STDERR( "[chuck]: | sample rate: %ld\n", srate );
1391 CK_FPRINTF_STDERR( "[chuck]: | buffer size: %ld\n", buffer_size );
1392 CK_FPRINTF_STDERR( "[chuck]: | num buffers: %ld\n", num_buffers );
1393 CK_FPRINTF_STDERR( "[chuck]: | adaptive block processing: %ld\n", adaptive_size > 1 ? adaptive_size : 0 );
1394 CK_FPRINTF_STDERR( "[chuck]: | audio driver: %s\n", audio_driver != "" ? audio_driver.c_str() : "(unspecified)");
1395 CK_FPRINTF_STDERR( "[chuck]: | adc:%d \"%s\"\n", adc, ChuckAudio::m_adc_name.c_str() );
1396 CK_FPRINTF_STDERR( "[chuck]: | dac:%d \"%s\"\n", dac, ChuckAudio::m_dac_name.c_str() );
1397 CK_FPRINTF_STDERR( "[chuck]: | channels in: %ld out: %ld\n", adc_chans, dac_chans );
1398
1399 EM_error2( 0, "cannot initialize audio device..." );
1400 CK_FPRINTF_STDERR( "[chuck]: | (use --probe see list of available audio devices)\n" );
1401 CK_FPRINTF_STDERR( "[chuck]: | (use --dac/--adc to explicitly select output/input devices)\n" );
1402 CK_FPRINTF_STDERR( "[chuck]: | (use --out/--in to explicitly select # of output/input channels)\n" );
1403 CK_FPRINTF_STDERR( "[chuck]: | (use --verbose/-v to see log messages)\n" );
1404 CK_FPRINTF_STDERR( "[chuck]: | (use --silent/-s for non-realtime)\n" );
1405 CK_FPRINTF_STDERR( "[chuck]: | (use --help for more details)\n" );
1406 // EM_log( CK_LOG_SYSTEM, "cannot initialize audio device..." );
1407 // EM_log( CK_LOG_SYSTEM, "| (use --probe see list of available audio devices)" );
1408 // EM_log( CK_LOG_SYSTEM, "| (use --dac/--adc to explicitly select output/input devices)" );
1409 // EM_log( CK_LOG_SYSTEM, "| (use --silent/-s for non-realtime)" );
1410 // EM_log( CK_LOG_SYSTEM, "| (use --help for more details)" );
1411 // pop
1412 EM_poplog();
1413
1414 // done
1415 EXIT_with_global_cleanup( 1 );
1416 }
1417
1418 // these could have been updated during initialization
1419 adc = ChuckAudio::m_adc_n+1;
1420 dac = ChuckAudio::m_dac_n+1;
1421 adc_chans = ChuckAudio::m_num_channels_in;
1422 dac_chans = ChuckAudio::m_num_channels_out;
1423 audio_driver = ChuckAudio::m_driver_name;
1424 adc_device_name = ChuckAudio::m_adc_name;
1425 dac_device_name = ChuckAudio::m_dac_name;
1426 srate = ChuckAudio::m_sample_rate;
1427 buffer_size = ChuckAudio::buffer_size();
1428 num_buffers = ChuckAudio::num_buffers();
1429 }
1430
1431 // pop
1432 EM_poplog();
1433
1434
1435 //-------------------- VIRTUAL MACHINE SETUP (PART 2) ---------------------
1436 // set params (some, like sample rate, could have been updated during
1437 // real-time audio initialization, so we are setting actual params here)
1438 // set the actual sample rate; needed to map to in-language durations
1439 the_chuck->setParam( CHUCK_PARAM_SAMPLE_RATE, srate );
1440 // use the request number for adc # channels
1441 the_chuck->setParam( CHUCK_PARAM_INPUT_CHANNELS, adc_chans_before_rtaudio );
1442 the_chuck->setParam( CHUCK_PARAM_OUTPUT_CHANNELS, dac_chans );
1443 the_chuck->setParam( CHUCK_PARAM_VM_ADAPTIVE, adaptive_size );
1444 the_chuck->setParam( CHUCK_PARAM_VM_HALT, (t_CKINT)(vm_halt) );
1445 the_chuck->setParam( CHUCK_PARAM_OTF_PORT, g_otf_port );
1446 the_chuck->setParam( CHUCK_PARAM_OTF_ENABLE, (t_CKINT)enableOTF );
1447 // only print warning if say --loop is specified, signalling interest in using this VM as a server | 1.5.1.2
1448 the_chuck->setParam( CHUCK_PARAM_OTF_PRINT_WARNINGS, (t_CKINT)(vm_halt == FALSE) );
1449 the_chuck->setParam( CHUCK_PARAM_DUMP_INSTRUCTIONS, (t_CKINT)dump );
1450 the_chuck->setParam( CHUCK_PARAM_AUTO_DEPEND, (t_CKINT)auto_depend );
1451 the_chuck->setParam( CHUCK_PARAM_DEPRECATE_LEVEL, deprecate_level );
1452 // set hint, so internally can advise things like async data writes etc.
1453 the_chuck->setParam( CHUCK_PARAM_IS_REALTIME_AUDIO_HINT, g_enable_realtime_audio );
1454 // enable or disable highlighting code on compiler error
1455 the_chuck->setParam( CHUCK_PARAM_COMPILER_HIGHLIGHT_ON_ERROR, (t_CKINT)!suppress_error_quote );
1456
1457 // initialize
1458 if( !the_chuck->init() )
1459 {
1460 // print error
1461 CK_FPRINTF_STDERR( "[chuck]: failed to initialize...\n" );
1462 // done
1463 EXIT_with_global_cleanup( 1 );
1464 }
1465
1466 // timestamp
1467 EM_log( CK_LOG_SYSTEM, "local time: %s", timestamp_formatted().c_str() );
1468
1469
1470 //------------------ PRINT VERIFIED AUDIO SETTING -------------------------
1471 // log
1472 EM_log( CK_LOG_SYSTEM, "initializing audio I/O..." );
1473 // push
1474 EM_pushlog();
1475 // log
1476 EM_log( CK_LOG_SYSTEM, "real-time audio: %s", g_enable_realtime_audio ? "YES" : "NO" );
1477 EM_log( CK_LOG_SYSTEM, "mode: %s", block ? "BLOCKING" : "CALLBACK" );
1478 EM_log( CK_LOG_SYSTEM, "sample rate: %ld", srate );
1479 EM_log( CK_LOG_SYSTEM, "buffer size: %ld", buffer_size );
1480 if( g_enable_realtime_audio )
1481 {
1482 EM_log( CK_LOG_SYSTEM, "num buffers: %ld", num_buffers );
1483 EM_log( CK_LOG_SYSTEM, "adaptive block processing: %ld", adaptive_size > 1 ? adaptive_size : 0 );
1484 EM_log( CK_LOG_SYSTEM, "audio driver: %s", audio_driver != "" ? audio_driver.c_str() : "(unspecified)");
1485 EM_log( CK_LOG_SYSTEM, "adc:%d \"%s\"", adc, adc_device_name.c_str() );
1486 EM_log( CK_LOG_SYSTEM, "dac:%d \"%s\"", dac, dac_device_name.c_str() );
1487 // EM_log( CK_LOG_SYSTEM, "adc: %ld dac: %d", adc, dac );
1488 }
1489 EM_log( CK_LOG_SYSTEM, "channels in: %ld out: %ld", adc_chans, dac_chans );
1490 // pop
1491 EM_poplog();
1492
1493
1494 //------------------------- HID RELATED SETUP -----------------------------
1495 #ifndef __ALTER_HID__
1496 // pre-load hid
1497 if( load_hid ) HidInManager::init();
1498 #endif // __ALTER_HID__
1499
1500
1501 //------------------------- CLI RELATED SETUP -----------------------------
1502 // catch SIGINT
1503 signal( SIGINT, signal_int );
1504 #ifndef __PLATFORM_WINDOWS__
1505 // catch SIGPIPE
1506 signal( SIGPIPE, signal_pipe );
1507 #endif
1508
1509 // boost priority
1510 if( XThreadUtil::our_priority != 0x7fffffff )
1511 {
1512 // try
1513 if( !XThreadUtil::set_priority( XThreadUtil::our_priority ) )
1514 {
1515 // error
1516 CK_FPRINTF_STDERR( "[chuck]: error setting thread priority...\n" );
1517 // done
1518 EXIT_with_global_cleanup( 1 );
1519 }
1520 }
1521
1522 // -------------------------- SHELL SETUP ---------------------------------
1523 // shell initialization
1524 if( g_enable_shell )
1525 {
1526 // instantiate
1527 g_shell = new Chuck_Shell;
1528 // initialize
1529 if( !init_shell( g_shell, new Chuck_Console(), the_chuck->vm() ) )
1530 {
1531 // done
1532 EXIT_with_global_cleanup( 1 );
1533 }
1534
1535 // flag
1536 g_shutdown_shell = FALSE;
1537
1538 // NOTE: shell to be started later below
1539 }
1540
1541 // version
1542 EM_log( CK_LOG_SYSTEM, "chuck version: %s", TC::green(the_chuck->version(), true).c_str() );
1543
1544 // pop log
1545 EM_poplog();
1546
1547
1548 //----------------------- ENTERING CHUCK RUNTIME --------------------------
1549 // log
1550 EM_log( CK_LOG_SYSTEM, "starting chuck..." );
1551
1552 // start it!
1553 the_chuck->start();
1554
1555 // push indent
1556 EM_pushlog();
1557
1558
1559 //------------------------- SOURCE COMPILATION ----------------------------
1560 // log
1561 EM_log( CK_LOG_SYSTEM, "starting compilation..." );
1562 // push indent
1563 EM_pushlog();
1564
1565 // reset count
1566 count = 1;
1567
1568 // loop through and process each file
1569 for( i = 1; i < argc; i++ )
1570 {
1571 // make sure
1572 if( argv[i][0] == '-' || argv[i][0] == '+' )
1573 {
1574 if( !strcmp(argv[i], "--dump") || !strcmp(argv[i], "+d" ) )
1575 the_chuck->compiler()->emitter->dump = TRUE;
1576 else if( !strcmp(argv[i], "--nodump") || !strcmp(argv[i], "-d" ) )
1577 the_chuck->compiler()->emitter->dump = FALSE;
1578 else
1579 get_count( argv[i], &count );
1580
1581 continue;
1582 }
1583
1584 // compile it!
1585 the_chuck->compileFile( argv[i], "", count );
1586
1587 // reset count
1588 count = 1;
1589 }
1590
1591 // pop indent
1592 EM_poplog();
1593
1594 // syntax check only mode
1595 if( syntax_check_only )
1596 {
1597 // pop log
1598 EM_poplog();
1599 // log message
1600 EM_log( CK_LOG_SYSTEM, "syntax checking mode complete; exiting..." );
1601 // done
1602 return TRUE;
1603 }
1604
1605 // debug print: print all vm objects
1606 CK_VM_DEBUGGER( print_all_objects() );
1607 // debug print: print vm objects stats
1608 CK_VM_DEBUGGER( print_stats() );
1609
1610
1611 //------------------------- STARTING AUDIO I/O ----------------------------
1612 // silent mode buffers
1613 SAMPLE * input = new SAMPLE[buffer_size*adc_chans];
1614 SAMPLE * output = new SAMPLE[buffer_size*dac_chans];
1615 // zero out
1616 memset( input, 0, sizeof(SAMPLE)*buffer_size*adc_chans );
1617 memset( output, 0, sizeof(SAMPLE)*buffer_size*dac_chans );
1618
1619 // if real-time audio mode
1620 if( g_enable_realtime_audio )
1621 {
1622 // log
1623 EM_log( CK_LOG_SYSTEM, "starting real-time audio..." );
1624 // start real-time audio I/O
1625 ChuckAudio::start();
1626 }
1627
1628
1629 //------------------------- MAIN CHUCK LOOP!!! ----------------------------
1630 // log
1631 EM_log( CK_LOG_SYSTEM, "starting virtual machine..." );
1632 EM_log( CK_LOG_SYSTEM, "starting main loop..." );
1633
1634 // pop indent
1635 EM_poplog();
1636
1637 // chuck shell
1638 // (deferring as late as possible to minimize output conflict with log)
1639 if( g_enable_shell )
1640 {
1641 // start shell on separate thread | REFACTOR-2017: per-VM?!?
1642 #if !defined(__PLATFORM_WINDOWS__) || defined(__WINDOWS_PTHREAD__)
1643 pthread_create( &g_tid_shell, NULL, shell_cb, g_shell );
1644 #else
1645 g_tid_shell = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)shell_cb, g_shell, 0, 0 );
1646 #endif
1647 }
1648
1649 // run loop
1650 while( the_chuck->vm_running() )
1651 {
1652 // real-time audio
1653 if( g_enable_realtime_audio )
1654 {
1655 // NOTE: with real-time audio, chuck VM computation
1656 // is driven from the audio callback function, not
1657 // on this main-thread (keep-alive) loop.
1658
1659 // get main thread hook, call it if there is one
1660 Chuck_DL_MainThreadHook * hook = the_chuck->getMainThreadHook();
1661 if( hook ) {
1662 hook->m_hook( hook->m_bindle );
1663 } else {
1664 ck_usleep( 10000 );
1665 }
1666 }
1667 else // silent mode
1668 {
1669 // keep running as fast as possible
1670 the_chuck->run( input, output, buffer_size );
1671 }
1672
1673 // shell relate activity
1674 if( check_shell_shutdown() ) break;
1675 }
1676
1677 // if chuck is still available; could have been cleaned up by shell_shutdown
1678 if( the_chuck != NULL )
1679 {
1680 // get main thread hook
1681 Chuck_DL_MainThreadHook * hook = the_chuck->getMainThreadHook();
1682 // call it if there is one
1683 if( hook ) { hook->m_quit(hook->m_bindle); }
1684 }
1685
1686 return TRUE;
1687 }