Debian Codegraph @

src/host-web/chuck_emscripten.cpp

raw symbols SCIP
1 //-----------------------------------------------------------------------------
2 // name: chuck_emscripten.cpp
3 // desc: emscripten binding agent for web assembly compilation
4 // e.g., used for WebChucK and Chunity web target
5 //
6 // author: Jack Atherton
7 // date: created 4/19/17
8 //-----------------------------------------------------------------------------
9 #include "chuck_emscripten.h"
10 #include "chuck_globals.h"
11
12 #include <iostream>
13 #include <map>
14
15 // #ifndef __PLATFORM_WINDOWS__
16 // #include <unistd.h>
17 // #endif
18
19 // c linkage
20 extern "C"
21 {
22 // chuck instances
23 std::map< unsigned int, ChucK * > chuck_instances;
24 // global data dir
25 std::string chuck_global_data_dir;
26
27 // something to do nothing
28 void dummyFunction()
29 { }
30
31 // a call to do nothing
32 void EMSCRIPTEN_KEEPALIVE dummyCall()
33 {
34 // call dummy function so Browser is included
35 emscripten_async_call( (em_arg_callback_func) dummyFunction, NULL, 1000 );
36 }
37
38
39 // C# "string" corresponds to passing char *
40 t_CKUINT EMSCRIPTEN_KEEPALIVE runChuckCode( unsigned int chuckID, const char * code )
41 {
42 // check to see id has instances
43 if( chuck_instances.count(chuckID) == 0 )
44 { return 0; } // 1.5.0.8 (ge) | was: { return -1; }
45
46 // don't want to replace dac
47 // (a safeguard in case compiler got interrupted while replacing dac)
48 chuck_instances[chuckID]->compiler()->setReplaceDac( FALSE, "" );
49
50 // compile it!
51 // 1.5.0.8 (ge) explicitly set count=1, immediate=TRUE
52 if( chuck_instances[chuckID]->compileCode( code, "", 1, TRUE ) )
53 {
54 // last shred shredded
55 return chuck_instances[chuckID]->vm()->last_id();
56 }
57
58 // failure to compile
59 return 0;
60 }
61
62
63 // run code with dac replacement
64 t_CKUINT EMSCRIPTEN_KEEPALIVE runChuckCodeWithReplacementDac(
65 unsigned int chuckID, const char * code, const char * replacement_dac )
66 {
67 // check to see id has instances
68 if( chuck_instances.count(chuckID) == 0 )
69 { return 0; } // 1.5.0.8 (ge) | was: { return -1; }
70
71 // replace dac
72 chuck_instances[chuckID]->compiler()->setReplaceDac( TRUE,
73 std::string( replacement_dac ) );
74 // compile it!
75 // 1.5.0.8 (ge) explicitly set count=1, immediate=TRUE
76 t_CKBOOL result = chuck_instances[chuckID]->compileCode( code, "", 1, TRUE );
77 // don't replace dac for future compilations
78 chuck_instances[chuckID]->compiler()->setReplaceDac( FALSE, "" );
79
80 // check result
81 if( result ) {
82 // last shred shredded
83 return chuck_instances[chuckID]->vm()->last_id();
84 }
85
86 // failure to compile
87 return 0;
88 }
89
90
91 // run chuck from file
92 t_CKUINT EMSCRIPTEN_KEEPALIVE runChuckFile( unsigned int chuckID, const char * filename )
93 {
94 // run with empty args
95 return runChuckFileWithArgs( chuckID, filename, "" );
96 }
97
98
99 // run chuck from file with args
100 t_CKUINT EMSCRIPTEN_KEEPALIVE runChuckFileWithArgs( unsigned int chuckID,
101 const char * filename, const char * args )
102 {
103 // check to see id has instances
104 if( chuck_instances.count(chuckID) == 0 )
105 { return 0; } // 1.5.0.8 (ge) | was: { return -1; }
106
107 // don't want to replace dac
108 // (a safeguard in case compiler got interrupted while replacing dac)
109 chuck_instances[chuckID]->compiler()->setReplaceDac( FALSE, "" );
110
111 // compile it!
112 // 1.5.0.8 (ge) explicitly set count=1, immediate=TRUE
113 if( chuck_instances[chuckID]->compileFile( filename, args, 1, TRUE ) )
114 {
115 // last shred shredded
116 return chuck_instances[chuckID]->vm()->last_id();
117 }
118
119 // failure to compile
120 return 0;
121 }
122
123
124 // run chuck from file with replacement dac
125 t_CKUINT EMSCRIPTEN_KEEPALIVE runChuckFileWithReplacementDac(
126 unsigned int chuckID, const char * filename,
127 const char * replacement_dac )
128 {
129 // run with empty args
130 return runChuckFileWithArgsWithReplacementDac(
131 chuckID, filename, "", replacement_dac
132 );
133 }
134
135
136 // run chuck from file with replacement dac with args
137 t_CKUINT EMSCRIPTEN_KEEPALIVE runChuckFileWithArgsWithReplacementDac(
138 unsigned int chuckID, const char * filename, const char * args,
139 const char * replacement_dac )
140 {
141 // check to see id has instances
142 if( chuck_instances.count(chuckID) == 0 )
143 { return 0; } // 1.5.0.8 (ge) | was: { return -1; }
144
145 // replace dac
146 chuck_instances[chuckID]->compiler()->setReplaceDac( TRUE,
147 std::string( replacement_dac ) );
148 // compile it!
149 // 1.5.0.8 (ge) explicitly set count=1, immediate=TRUE
150 t_CKBOOL result = chuck_instances[chuckID]->compileFile( filename, args, 1, TRUE );
151 // don't replace dac for future compilations
152 chuck_instances[chuckID]->compiler()->setReplaceDac( FALSE, "" );
153
154 // check result
155 if( result ) {
156 // last shred shredded
157 return chuck_instances[chuckID]->vm()->last_id();
158 }
159
160 // failure to compile
161 return 0;
162 }
163
164
165 // replace code given a chuck instance
166 t_CKUINT replaceCode( ChucK * chuck, t_CKUINT shredID, std::vector<std::string> * args )
167 {
168 Chuck_Msg * msg = new Chuck_Msg;
169 msg->type = CK_MSG_REPLACE;
170 msg->code = chuck->compiler()->output();
171 msg->param = shredID;
172 // set so that VM will delete for us when it's done
173 msg->reply_queue = FALSE;
174 // 1.5.0.8 use set for proper memory management
175 msg->set( args ); // msg->args = args;
176
177 // call the process_msg directly: we need to know the shred ID
178 // and it's JS so we won't be pre-empted
179 // also: return value is the shred ID!
180 return chuck->vm()->process_msg( msg );
181 }
182
183
184 // replace chuck code
185 t_CKUINT EMSCRIPTEN_KEEPALIVE replaceChuckCode( unsigned int chuckID, unsigned int shredID, const char * code )
186 {
187 // check to see id has instances
188 if( chuck_instances.count(chuckID) == 0 )
189 { return 0; } // 1.5.0.8 (ge) | was: { return -1; }
190
191 // don't want to replace dac
192 // (a safeguard in case compiler got interrupted while replacing dac)
193 chuck_instances[chuckID]->compiler()->setReplaceDac( FALSE, "" );
194
195 // compile it! and spork 0 times (ONLY compile)
196 if( chuck_instances[chuckID]->compileCode( code, "", 0 ) )
197 {
198 // replace code
199 return replaceCode( chuck_instances[chuckID], shredID, NULL );
200 }
201
202 // failure to compile
203 return 0;
204 }
205
206
207 // replace chuck code with replacement dac
208 t_CKUINT EMSCRIPTEN_KEEPALIVE replaceChuckCodeWithReplacementDac( unsigned int chuckID, unsigned int shredID,
209 const char * code, const char * replacement_dac )
210 {
211 // check to see id has instances
212 if( chuck_instances.count(chuckID) == 0 )
213 { return 0; } // 1.5.0.8 (ge) | was: { return -1; }
214
215 // replace dac
216 chuck_instances[chuckID]->compiler()->setReplaceDac( TRUE, replacement_dac );
217 // compile it! and spork 0 times. (ONLY compile)
218 t_CKBOOL result = chuck_instances[chuckID]->compileCode( code, "", 0 );
219 // don't replace dac for future compilations
220 chuck_instances[chuckID]->compiler()->setReplaceDac( FALSE, "" );
221 // check result
222 if( result )
223 {
224 // replace code
225 return replaceCode( chuck_instances[chuckID], shredID, NULL );
226 }
227
228 // failure to compile
229 return 0;
230 }
231
232
233 // replace chuck file
234 t_CKUINT EMSCRIPTEN_KEEPALIVE replaceChuckFile( unsigned int chuckID, unsigned int shredID, const char * filename )
235 {
236 // replace with empty args
237 return replaceChuckFileWithArgs( chuckID, shredID, filename, "" );
238 }
239
240
241 // replace chuck file with replacement dac
242 t_CKUINT EMSCRIPTEN_KEEPALIVE replaceChuckFileWithReplacementDac( unsigned int chuckID, unsigned int shredID,
243 const char * filename, const char * replacement_dac )
244 {
245 // replace with empty args
246 return replaceChuckFileWithArgsWithReplacementDac( chuckID, shredID, filename, "", replacement_dac );
247 }
248
249
250 // replace chuck file with args
251 t_CKUINT EMSCRIPTEN_KEEPALIVE replaceChuckFileWithArgs( unsigned int chuckID, unsigned int shredID,
252 const char * filename, const char * args )
253 {
254 // check to see id has instances
255 if( chuck_instances.count(chuckID) == 0 )
256 { return 0; } // 1.5.0.8 (ge) | was: { return -1; }
257
258 // don't want to replace dac
259 // (a safeguard in case compiler got interrupted while replacing dac)
260 chuck_instances[chuckID]->compiler()->setReplaceDac( FALSE, "" );
261
262 std::string filenameS( filename );
263 std::string argsS( args );
264
265 // compile it! and spork 0 times (ONLY compile)
266 if( chuck_instances[chuckID]->compileFile( filenameS, argsS, 0 ) )
267 {
268 // find args
269 std::string found_filename;
270 std::vector<std::string> found_args;
271 std::string together = filenameS + ":" + argsS;
272 // parse out command line arguments
273 if( !extract_args( together, found_filename, found_args ) )
274 {
275 // error
276 CK_FPRINTF_STDERR( "[chuck]: malformed filename with argument list...\n" );
277 CK_FPRINTF_STDERR( " --> '%s'", together.c_str() );
278 return 0;
279 }
280
281 // replace code
282 return replaceCode( chuck_instances[chuckID], shredID, &found_args );
283 }
284
285 // failure to compile
286 return 0;
287 }
288
289
290 // replace chuck by file with args and with replacement dac
291 t_CKUINT EMSCRIPTEN_KEEPALIVE replaceChuckFileWithArgsWithReplacementDac(
292 unsigned int chuckID, unsigned int shredID, const char * filename,
293 const char * args, const char * replacement_dac )
294 {
295 // check to see id has instances
296 if( chuck_instances.count(chuckID) == 0 )
297 { return 0; } // 1.5.0.8 (ge) | was: { return -1; }
298
299 // replace dac
300 chuck_instances[chuckID]->compiler()->setReplaceDac( TRUE, replacement_dac );
301
302 // file name
303 std::string filenameS( filename );
304 // args
305 std::string argsS( args );
306 // compile it! and spork 0 times (ONLY compile)
307 t_CKBOOL result = chuck_instances[chuckID]->compileFile( filenameS, argsS, 0 );
308
309 // don't replace dac for future compilations
310 chuck_instances[chuckID]->compiler()->setReplaceDac( FALSE, "" );
311 // check result
312 if( result )
313 {
314 // find args
315 std::string found_filename;
316 std::vector<std::string> found_args;
317 std::string together = filenameS + ":" + argsS;
318 // parse out command line arguments
319 if( !extract_args( together, found_filename, found_args ) )
320 {
321 // error
322 CK_FPRINTF_STDERR( "[chuck]: malformed filename with argument list...\n" );
323 CK_FPRINTF_STDERR( " --> '%s'", together.c_str() );
324 return 0;
325 }
326
327 // replace code
328 return replaceCode( chuck_instances[chuckID], shredID, &found_args );
329 }
330
331 // failure to compile
332 return 0;
333 }
334
335
336 // is shred alive
337 bool EMSCRIPTEN_KEEPALIVE isShredActive( unsigned int chuckID, unsigned int shredID )
338 {
339 // check if chuck id has instances
340 if( chuck_instances.count( chuckID ) == 0 ) { return false; }
341 // look up shred by id
342 Chuck_VM_Shred * shred = chuck_instances[chuckID]->vm()->shreduler()->lookup(shredID);
343 // return
344 return (shred != NULL);
345 }
346
347
348 // remove shred
349 bool EMSCRIPTEN_KEEPALIVE removeShred( unsigned int chuckID, unsigned int shredID )
350 {
351 // check for chuck and shred id
352 if( chuck_instances.count( chuckID ) == 0 || !isShredActive( chuckID, shredID ) )
353 { return false; }
354
355 // the chuck to clear
356 ChucK * chuck = chuck_instances[chuckID];
357
358 // create a msg asking to remove the shred
359 Chuck_Msg * msg = new Chuck_Msg;
360 msg->type = CK_MSG_REMOVE;
361 msg->param = shredID;
362 // set so that VM will delete for us when it's done
363 msg->reply_queue = FALSE;
364
365 // tell the VM to clear
366 chuck->globals()->execute_chuck_msg_with_globals( msg );
367
368 return true;
369 }
370
371
372 // set chuck global int
373 bool EMSCRIPTEN_KEEPALIVE setChuckInt( unsigned int chuckID, const char * name, t_CKINT val )
374 {
375 // check if chuck id has instances
376 if( chuck_instances.count( chuckID ) == 0 ) { return false; }
377 // set global int value
378 return chuck_instances[chuckID]->globals()->setGlobalInt( name, val );
379 }
380
381
382 // get chuck global int
383 t_CKINT EMSCRIPTEN_KEEPALIVE getChuckInt( unsigned int chuckID, const char * name )
384 {
385 // check if chuck id has instances
386 if( chuck_instances.count(chuckID) == 0 ) { return false; }
387 // set global value
388 return chuck_instances[chuckID]->globals()->get_global_int_value(name);
389 }
390
391
392 // set chuck global float
393 bool EMSCRIPTEN_KEEPALIVE setChuckFloat( unsigned int chuckID, const char * name, t_CKFLOAT val )
394 {
395 // check if chuck id has instances
396 if( chuck_instances.count( chuckID ) == 0 ) { return false; }
397 // set global value
398 return chuck_instances[chuckID]->globals()->setGlobalFloat( name, val );
399 }
400
401
402 // get chuck global float
403 t_CKFLOAT EMSCRIPTEN_KEEPALIVE getChuckFloat( unsigned int chuckID, const char * name )
404 {
405 // check if chuck id has instances
406 if( chuck_instances.count( chuckID ) == 0 ) { return false; }
407 // get global value
408 return chuck_instances[chuckID]->globals()->get_global_float_value( name );
409 }
410
411
412 // set chuck global string
413 bool EMSCRIPTEN_KEEPALIVE setChuckString( unsigned int chuckID, const char * name, const char * val )
414 {
415 // check if chuck id has instances
416 if( chuck_instances.count( chuckID ) == 0 ) { return false; }
417 // set global val
418 return chuck_instances[chuckID]->globals()->setGlobalString( name, val );
419 }
420
421
422 // get chuck global string by callbackk
423 bool EMSCRIPTEN_KEEPALIVE getChuckString( unsigned int chuckID, const char * name, void (* callback)(const char *) )
424 {
425 // check if chuck id has instances
426 if( chuck_instances.count( chuckID ) == 0 ) { return false; }
427 // get global val via callback
428 return chuck_instances[chuckID]->globals()->getGlobalString( name, callback );
429 }
430
431
432 // signal chuck event
433 bool EMSCRIPTEN_KEEPALIVE signalChuckEvent( unsigned int chuckID, const char * name )
434 {
435 // check if chuck id has instances
436 if( chuck_instances.count( chuckID ) == 0 ) { return false; }
437 // signal global event
438 return chuck_instances[chuckID]->globals()->signalGlobalEvent( name );
439 }
440
441
442 // broadcast chuck event
443 bool EMSCRIPTEN_KEEPALIVE broadcastChuckEvent( unsigned int chuckID, const char * name )
444 {
445 // check if chuck id has instances
446 if( chuck_instances.count( chuckID ) == 0 ) { return false; }
447 // broadcast global event
448 return chuck_instances[chuckID]->globals()->broadcastGlobalEvent( name );
449 }
450
451
452 // listen for chuck event once
453 bool EMSCRIPTEN_KEEPALIVE listenForChuckEventOnce( unsigned int chuckID, const char * name, void (* callback)(void) )
454 {
455 // check if chuck id has instances
456 if( chuck_instances.count( chuckID ) == 0 ) { return false; }
457 // listen for global event
458 return chuck_instances[chuckID]->globals()->listenForGlobalEvent( name, callback, FALSE );
459 }
460
461
462 // start listening for chuck event
463 bool EMSCRIPTEN_KEEPALIVE startListeningForChuckEvent( unsigned int chuckID, const char * name, void (* callback)(void) )
464 {
465 // check if chuck id has instances
466 if( chuck_instances.count( chuckID ) == 0 ) { return false; }
467 // listen for global event
468 return chuck_instances[chuckID]->globals()->listenForGlobalEvent( name, callback, TRUE );
469 }
470
471
472 // stop listening for chuck event
473 bool EMSCRIPTEN_KEEPALIVE stopListeningForChuckEvent( unsigned int chuckID, const char * name, void (* callback)(void) )
474 {
475 // check if chuck id has instances
476 if( chuck_instances.count( chuckID ) == 0 ) { return false; }
477 // stop listening for event
478 return chuck_instances[chuckID]->globals()->stopListeningForGlobalEvent( name, callback );
479 }
480
481
482 // get global ugen samples
483 bool EMSCRIPTEN_KEEPALIVE getGlobalUGenSamples( unsigned int chuckID,
484 const char * name, SAMPLE * buffer, int numSamples )
485 {
486 // check if chuck id has instances
487 if( chuck_instances.count( chuckID ) == 0 ) { return false; }
488 // get global ugen samples
489 if( !chuck_instances[chuckID]->globals()->getGlobalUGenSamples( name, buffer, numSamples ) )
490 {
491 // failed; fill with zeroes
492 memset( buffer, 0, sizeof( SAMPLE ) * numSamples );
493 return false;
494 }
495 return true;
496 }
497
498
499 // int array methods
500 bool EMSCRIPTEN_KEEPALIVE setGlobalIntArray( unsigned int chuckID,
501 const char * name, t_CKINT arrayValues[], unsigned int numValues )
502 {
503 // check if chuck id has instances
504 if( chuck_instances.count( chuckID ) == 0 ) { return false; }
505 // set global int array
506 return chuck_instances[chuckID]->globals()->setGlobalIntArray(
507 name, arrayValues, numValues );
508 }
509
510
511 // get global int array
512 bool EMSCRIPTEN_KEEPALIVE getGlobalIntArray( unsigned int chuckID,
513 const char * name, void (* callback)(t_CKINT[], t_CKUINT))
514 {
515 // check if chuck id has instances
516 if( chuck_instances.count( chuckID ) == 0 ) { return false; }
517 // get global int array
518 return chuck_instances[chuckID]->globals()->getGlobalIntArray(
519 name, callback );
520 }
521
522
523 // set global int array
524 bool EMSCRIPTEN_KEEPALIVE setGlobalIntArrayValue( unsigned int chuckID,
525 const char * name, unsigned int index, t_CKINT value )
526 {
527 // check if chuck id has instances
528 if( chuck_instances.count( chuckID ) == 0 ) { return false; }
529 // set global int array
530 return chuck_instances[chuckID]->globals()->setGlobalIntArrayValue(
531 name, index, value );
532 }
533
534
535 // get global int array
536 t_CKINT EMSCRIPTEN_KEEPALIVE getGlobalIntArrayValue( unsigned int chuckID,
537 const char * name, unsigned int index )
538 {
539 // check if chuck id has instances
540 if( chuck_instances.count( chuckID ) == 0 ) { return false; }
541 // get global int array
542 return chuck_instances[chuckID]->globals()->get_global_int_array_value(
543 name, index );
544 }
545
546
547 // set global associative int array
548 bool EMSCRIPTEN_KEEPALIVE setGlobalAssociativeIntArrayValue(
549 unsigned int chuckID, const char * name, char * key, t_CKINT value )
550 {
551 // check if chuck id has instances
552 if( chuck_instances.count( chuckID ) == 0 ) { return false; }
553 // set global associative int array
554 return chuck_instances[chuckID]->globals()->setGlobalAssociativeIntArrayValue(
555 name, key, value );
556 }
557
558
559 // get global associative int array
560 t_CKINT EMSCRIPTEN_KEEPALIVE getGlobalAssociativeIntArrayValue(
561 unsigned int chuckID, const char * name, char * key )
562 {
563 // check if chuck id has instances
564 if( chuck_instances.count( chuckID ) == 0 ) { return false; }
565 // get global associative int array
566 return chuck_instances[chuckID]->globals()->get_global_associative_int_array_value(
567 std::string( name ), std::string( key ) );
568 }
569
570
571 // set float array methods
572 bool EMSCRIPTEN_KEEPALIVE setGlobalFloatArray( unsigned int chuckID,
573 const char * name, t_CKFLOAT arrayValues[], unsigned int numValues )
574 {
575 // check if chuck id has instances
576 if( chuck_instances.count( chuckID ) == 0 ) { return false; }
577 // set global float array
578 return chuck_instances[chuckID]->globals()->setGlobalFloatArray(
579 name, arrayValues, numValues );
580 }
581
582
583 // get global float array
584 bool EMSCRIPTEN_KEEPALIVE getGlobalFloatArray( unsigned int chuckID,
585 const char * name, void (* callback)(t_CKFLOAT[], t_CKUINT))
586 {
587 // check if chuck id has instances
588 if( chuck_instances.count(chuckID) == 0 ) { return false; }
589 // get global float array
590 return chuck_instances[chuckID]->globals()->getGlobalFloatArray(
591 name, callback );
592 }
593
594
595 // set global float array
596 bool EMSCRIPTEN_KEEPALIVE setGlobalFloatArrayValue( unsigned int chuckID,
597 const char * name, unsigned int index, t_CKFLOAT value )
598 {
599 // check if chuck id has instances
600 if( chuck_instances.count( chuckID ) == 0 ) { return false; }
601 // set global float array
602 return chuck_instances[chuckID]->globals()->setGlobalFloatArrayValue(
603 name, index, value );
604 }
605
606
607 // get global float array
608 t_CKFLOAT EMSCRIPTEN_KEEPALIVE getGlobalFloatArrayValue( unsigned int chuckID,
609 const char * name, unsigned int index )
610 {
611 // check if chuck id has instances
612 if( chuck_instances.count( chuckID ) == 0 ) { return false; }
613 // get global float array
614 return chuck_instances[chuckID]->globals()->get_global_float_array_value(
615 name, index );
616 }
617
618
619 // set global associative float array
620 bool EMSCRIPTEN_KEEPALIVE setGlobalAssociativeFloatArrayValue(
621 unsigned int chuckID, const char * name, char * key, t_CKFLOAT value )
622 {
623 // check if chuck id has instances
624 if( chuck_instances.count( chuckID ) == 0 ) { return false; }
625 // set global associative float array
626 return chuck_instances[chuckID]->globals()->setGlobalAssociativeFloatArrayValue(
627 name, key, value );
628 }
629
630
631 // get global associative float array
632 t_CKFLOAT EMSCRIPTEN_KEEPALIVE getGlobalAssociativeFloatArrayValue(
633 unsigned int chuckID, const char * name, char * key )
634 {
635 // check if chuck id has instances
636 if( chuck_instances.count( chuckID ) == 0 ) { return false; }
637 // get global associative float array
638 return chuck_instances[chuckID]->globals()->get_global_associative_float_array_value(
639 std::string( name) , std::string( key ) );
640 }
641
642
643 // set chout callback
644 bool EMSCRIPTEN_KEEPALIVE setChoutCallback( unsigned int chuckID, void (* callback)(const char *) )
645 {
646 // set
647 return chuck_instances[chuckID]->setChoutCallback( callback );
648 }
649
650
651 // set cherr callback
652 bool EMSCRIPTEN_KEEPALIVE setCherrCallback( unsigned int chuckID, void (* callback)(const char *) )
653 {
654 // set
655 return chuck_instances[chuckID]->setCherrCallback( callback );
656 }
657
658
659 // set stdout callback
660 bool EMSCRIPTEN_KEEPALIVE setStdoutCallback( void (* callback)(const char *) )
661 {
662 // set
663 return ChucK::setStdoutCallback( callback );
664 }
665
666
667 // set stderr callback
668 bool EMSCRIPTEN_KEEPALIVE setStderrCallback( void (* callback)(const char *) )
669 {
670 // set
671 return ChucK::setStderrCallback( callback );
672 }
673
674
675 // set data dir
676 bool EMSCRIPTEN_KEEPALIVE setDataDir( const char * dir )
677 {
678 // set
679 chuck_global_data_dir = dir;
680 return true;
681 }
682
683
684 // set log level
685 bool EMSCRIPTEN_KEEPALIVE setLogLevel( unsigned int level )
686 {
687 // set log
688 EM_setlog( level );
689 return true;
690 }
691
692
693 // init chuck instance
694 bool EMSCRIPTEN_KEEPALIVE initChuckInstance( unsigned int chuckID, unsigned int sampleRate,
695 unsigned int inChannels, unsigned int outChannels )
696 {
697 // proceed as normal
698 if( chuck_instances.count( chuckID ) == 0 )
699 {
700 // if we aren't tracking a chuck vm on this ID, create a new one
701 ChucK * chuck = new ChucK();
702
703 // set params: sample rate, 2 in channels, 2 out channels,
704 // don't halt the vm, and use our data directory
705 chuck->setParam( CHUCK_PARAM_SAMPLE_RATE, (t_CKINT)sampleRate );
706 chuck->setParam( CHUCK_PARAM_INPUT_CHANNELS, (t_CKINT)inChannels );
707 chuck->setParam( CHUCK_PARAM_OUTPUT_CHANNELS, (t_CKINT)outChannels );
708 chuck->setParam( CHUCK_PARAM_VM_HALT, (t_CKINT)0 );
709 chuck->setParam( CHUCK_PARAM_DUMP_INSTRUCTIONS, (t_CKINT)0 );
710 // directory for compiled.code
711 chuck->setParam( CHUCK_PARAM_WORKING_DIRECTORY, chuck_global_data_dir );
712 // directories to search for chugins and auto-run ck files
713 std::list< std::string > chugin_search;
714 // chugin_search.push_back( chuck_global_data_dir + "/Chugins" );
715 // chugin_search.push_back( chuck_global_data_dir + "/ChuGins" );
716 chugin_search.push_back( chuck_global_data_dir + "chugins" );
717 chuck->setParam( CHUCK_PARAM_IMPORT_PATH_SYSTEM, chugin_search );
718
719 // default real-time audio is true | chuck-1.4.2.1 (ge) added
720 // set hint, so internally can advise things like async data writes etc.
721 chuck->setParam( CHUCK_PARAM_IS_REALTIME_AUDIO_HINT, (t_CKINT)TRUE );
722
723 // initialize and start
724 chuck->init();
725 chuck->start();
726
727 // put into map
728 chuck_instances[chuckID] = chuck;
729 }
730
731 return true;
732 }
733
734
735 // clear chuck instance
736 bool EMSCRIPTEN_KEEPALIVE clearChuckInstance( unsigned int chuckID )
737 {
738 // check
739 if( chuck_instances.count(chuckID) > 0 )
740 {
741 // the chuck to clear
742 ChucK * chuck = chuck_instances[chuckID];
743
744 // create a msg asking to clear the VM
745 Chuck_Msg * msg = new Chuck_Msg;
746 // set message type
747 msg->type = CK_MSG_CLEARVM;
748 // set so that VM will delete for us when it's done
749 msg->reply_queue = FALSE;
750 // tell the VM to clear
751 chuck->vm()->globals_manager()->execute_chuck_msg_with_globals( msg );
752
753 return true;
754 }
755
756 return false;
757 }
758
759
760 // clear globals
761 bool EMSCRIPTEN_KEEPALIVE clearGlobals( unsigned int chuckID )
762 {
763 // check
764 if( chuck_instances.count( chuckID ) > 0 )
765 {
766 // the chuck to clear
767 ChucK * chuck = chuck_instances[chuckID];
768
769 // create a msg asking to clear the globals
770 Chuck_Msg * msg = new Chuck_Msg;
771 // set message type
772 msg->type = CK_MSG_CLEARGLOBALS;
773 // set so that VM will delete for us when it's done
774 msg->reply_queue = FALSE;
775 // tell the VM to clear
776 chuck->vm()->globals_manager()->execute_chuck_msg_with_globals( msg );
777
778 return true;
779 }
780
781 return false;
782 }
783
784
785 // clean up chuck instance
786 bool EMSCRIPTEN_KEEPALIVE cleanupChuckInstance( unsigned int chuckID )
787 {
788 // check
789 if( chuck_instances.count( chuckID ) > 0 )
790 {
791 // get
792 ChucK * chuck = chuck_instances[chuckID];
793 // don't track it anymore
794 chuck_instances.erase( chuckID );
795
796 // cleanup this chuck early
797 CK_SAFE_DELETE( chuck );
798 }
799
800 return true;
801 }
802
803
804 // chuck manual audio callback
805 bool EMSCRIPTEN_KEEPALIVE chuckManualAudioCallback( unsigned int chuckID,
806 float * inBuffer, float * outBuffer, unsigned int numFrames,
807 unsigned int inChannels, unsigned int outChannels )
808 {
809 // check
810 if( chuck_instances.count( chuckID ) > 0 )
811 {
812 // zero out the output buffer, in case chuck isn't running
813 for( unsigned int n = 0; n < numFrames * outChannels; n++ )
814 {
815 outBuffer[n] = 0;
816 }
817
818 // call callback
819 // TODO: check inChannels, outChannels
820 chuck_instances[chuckID]->run( inBuffer, outBuffer, numFrames );
821 }
822
823 return true;
824 }
825
826
827 // on launch, reset all ids (necessary when relaunching a lot in unity editor)
828 void EMSCRIPTEN_KEEPALIVE cleanRegisteredChucks()
829 {
830 // delete chucks
831 for( std::map< unsigned int, ChucK * >::iterator it =
832 chuck_instances.begin(); it != chuck_instances.end(); it++ )
833 {
834 ChucK * chuck = it->second;
835 CK_SAFE_DELETE( chuck );
836 }
837
838 // delete stored chuck pointers
839 chuck_instances.clear();
840
841 // clear out callbacks also
842 setStdoutCallback( NULL );
843 setStderrCallback( NULL );
844 }
845
846
847 // set param
848 bool EMSCRIPTEN_KEEPALIVE setParamInt( unsigned int chuckID, const char * key, t_CKINT val )
849 {
850 // check
851 if( chuck_instances.count( chuckID ) > 0 )
852 {
853 // call
854 return chuck_instances[chuckID]->setParam( key, val );
855 }
856
857 return false;
858 }
859
860 bool EMSCRIPTEN_KEEPALIVE setParamFloat( unsigned int chuckID, const char * key, t_CKFLOAT val )
861 {
862 // check
863 if( chuck_instances.count( chuckID ) > 0 )
864 {
865 // call
866 return chuck_instances[chuckID]->setParam( key, val );
867 }
868 return false;
869 }
870
871 bool EMSCRIPTEN_KEEPALIVE setParamString( unsigned int chuckID, const char * key, const char * val )
872 {
873 // check
874 if( chuck_instances.count( chuckID ) > 0 )
875 {
876 // call
877 return chuck_instances[chuckID]->setParam( key, val );
878 }
879 return false;
880 }
881
882 // get param
883 t_CKINT EMSCRIPTEN_KEEPALIVE getParamInt( unsigned int chuckID, const char * key )
884 {
885 // check
886 if( chuck_instances.count( chuckID ) > 0 )
887 {
888 // call
889 return chuck_instances[chuckID]->getParamInt( key );
890 }
891 return false;
892 }
893
894 t_CKFLOAT EMSCRIPTEN_KEEPALIVE getParamFloat( unsigned int chuckID, const char * key )
895 {
896 // check
897 if( chuck_instances.count( chuckID ) > 0 )
898 {
899 // call
900 return chuck_instances[chuckID]->getParamFloat( key );
901 }
902 return false;
903 }
904
905 const char * EMSCRIPTEN_KEEPALIVE getParamString( unsigned int chuckID, const char * key )
906 {
907 // hopefully this is ok | assume not reentrant code
908 static std::string theStr;
909
910 // check
911 if( chuck_instances.count( chuckID ) > 0 )
912 {
913 // call
914 theStr = chuck_instances[chuckID]->getParamString( key );
915 // return persistent storage | the recipient should not hold on to the char *
916 return theStr.c_str();
917 }
918 return "";
919 }
920
921 // get chuck time
922 t_CKTIME EMSCRIPTEN_KEEPALIVE getChuckNow( unsigned int chuckID )
923 {
924 // check
925 if( chuck_instances.count( chuckID ) > 0 )
926 {
927 // call
928 return chuck_instances[chuckID]->now();
929 }
930 return 0;
931 }
932
933 // runtime import API (TBD)
934 t_CKBOOL EMSCRIPTEN_KEEPALIVE importModule( unsigned int chuckID, const char * path )
935 {
936 // check
937 if( chuck_instances.count( chuckID ) > 0 )
938 {
939 // call
940 // return chuck_instances[chuckID]->compiler()->loadModule( path );
941 }
942 return FALSE;
943 }
944
945 } // extern "C"