-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathminiAudicle.cpp
1908 lines (1587 loc) · 58.9 KB
/
miniAudicle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*----------------------------------------------------------------------------
miniAudicle:
integrated developement environment for ChucK audio programming language
Copyright (c) 2005-2013 Spencer Salazar. All rights reserved.
http://chuck.cs.princeton.edu/
http://soundlab.cs.princeton.edu/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
U.S.A.
-----------------------------------------------------------------------------*/
//-----------------------------------------------------------------------------
// file: miniAudicle.cpp
// desc: platform-independent miniAudicle interface
//
// author: Spencer Salazar ([email protected])
// date: Autumn 2005
//-----------------------------------------------------------------------------
#include <stdio.h>
#include <errno.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include "version.h"
#include "miniAudicle.h"
#include "chuck.h"
#include "chuck_globals.h" // for access to global manager
#include "chuck_errmsg.h"
#include "chuck_otf.h"
#include "chuck_audio.h"
#include "util_string.h"
#include "util_platforms.h"
#ifndef __PLATFORM_WINDOWS__
#include "git-rev.h"
#endif // __PLATFORM_WINDOWS__
#include "miniAudicle_ui_elements.h"
#include "miniAudicle_import.h"
#include "util_rterror.h"
using namespace std;
//using namespace miniAudicle;
// check if need to implicitly enable MAUI
#ifndef __MA_IMPORT_MAUI__
#if defined( __MACOSX_CORE__ ) && !defined(__CHIP_MODE__) && !defined(QT_GUI_LIB)
#define __MA_IMPORT_MAUI__
#endif // defined( __MACOSX_CORE__ )
#endif // __MA_IMPORT_MAUI__
// default destination host name
// extern char g_host[256];
// not used
// t_CKBOOL g_forked = FALSE;
// scheduling priorities
#if defined(__MACOSX_CORE__)
t_CKINT priority = 80;
t_CKINT priority_low = 60;
#elif defined(__PLATFORM_WINDOWS__)
t_CKINT priority = 0;
t_CKINT priority_low = 0;
#else
t_CKINT priority = 0x7fffffff;
t_CKINT priority_low = 0x7fffffff;
#endif
// set mA default sample rate to CK default
#define MA_SAMPLE_RATE_DEFAULT CK_SAMPLE_RATE_DEFAULT
// miniAudicle version text
extern const char MA_VERSION[] = ENV_MA_VERSION " (latte)";
#ifndef __PLATFORM_WINDOWS__
extern const char MA_ABOUT[] = "version %s\n\
git: " GIT_REVISION "\n\
Copyright (c) Spencer Salazar\n\n\
ChucK: version %s %lu-bit\n\
Copyright (c) Ge Wang and Perry Cook\nhttps://chuck.stanford.edu/";
#else
extern const char MA_ABOUT[] = "version %s\n\
Copyright (c) Spencer Salazar\n\n\
ChucK: version %s %lu-bit\n\
Copyright (c) Ge Wang and Perry Cook\nhttps://chuck.stanford.edu/";
#endif // __PLATFORM_WINDOWS__
// miniAudicle help
extern const char MA_HELP[] =
"usage: miniAudicle [options] [files] \n\
options: \n\
--dacN use audio output device N\n\
--adcN use audio input device N\n\
--outN/-oN use N output channels\n\
--inN/-iN use N input channels\n\
--channelsN/-cN use N input/output channels\n\
--srateN/-sN set sample rate to N\n\
--bufsizeN/-bN set sample buffer size to N (rounded to nearest power of 2)\n\
--bufnumN/-nN use N sample buffers\n\
--verboseN/-vN set log level to N (0-10:none-everything)\n\
--probe list available audio devices and properties\n\
--help/--about print this message\n\
\n\
miniAudicle-%s\n\
http://audicle.cs.princeton.edu/mini/\n\
\n\
ChucK-%s\n\
http://chuck.cs.princeton.edu/\n";
//-----------------------------------------------------------------------------
// name: audio_cb()
// desc: audio callback
//-----------------------------------------------------------------------------
void audio_cb( t_CKSAMPLE * in, t_CKSAMPLE * out, t_CKUINT numFrames,
t_CKUINT numInChans, t_CKUINT numOutChans, void * data )
{
// get ChucK instance
ChucK * chuck = (ChucK *) data;
// call up to ChucK
chuck->run( in, out, numFrames );
}
//-----------------------------------------------------------------------------
// name: miniAudicle()
// desc: constructor
//-----------------------------------------------------------------------------
miniAudicle::miniAudicle()
: m_console_callback(NULL)
{
// zero oiut
vm = NULL;
m_chuck = NULL;
compiler = NULL;
vm_on = FALSE;
next_document_id = 0;
// create class names map
class_names = new map< string, t_CKINT >;
// VM sleep
vm_sleep_time = 10000;
vm_sleep_max = 1;
// VM status
vm_status_timeouts = 0;
vm_status_timeouts_max = 20;
// VM options (default)
vm_options.enable_audio = TRUE;
vm_options.enable_network = FALSE;
vm_options.driver = ChuckAudio::defaultDriverName();
vm_options.dac = 0;
vm_options.adc = 0;
vm_options.srate = 0;
vm_options.buffer_size = CK_BUFFER_SIZE_DEFAULT;
vm_options.num_buffers = CK_NUM_BUFFERS_DEFAULT;
vm_options.num_inputs = CK_NUM_CHANNELS_DEFAULT;
vm_options.num_outputs = CK_NUM_CHANNELS_DEFAULT;
vm_options.enable_block = FALSE;
vm_options.force_srate = FALSE;
// probe audio
probe( vm_options.driver.c_str() );
}
//-----------------------------------------------------------------------------
// name: ~miniAudicle()
// desc: desturctor
//-----------------------------------------------------------------------------
miniAudicle::~miniAudicle()
{
// stop the VM
if( vm_on )
stop_vm();
// clear | 1.5.0.1 (ge) using SAFE_DELETE macro
CK_SAFE_DELETE( class_names );
// log
EM_log( CK_LOG_INFO, "miniAudicle instance destroyed..." );
}
//-----------------------------------------------------------------------------
// name: run_code()
// desc: ...
//-----------------------------------------------------------------------------
t_OTF_RESULT miniAudicle::run_code( string & code, string & name,
vector< string > & args, string & filepath,
t_CKUINT docid, t_CKUINT & shred_id,
string & out )
{
// check for invalid document id
if( documents.find( docid ) == documents.end() )
{
out += "internal error at miniAudicle::run_code\n";
return OTF_MINI_ERROR;
}
// compile -- always compile the code in the buffer
// even if the buffer has been saved to file;
// however, the filepath can be provided as directory basis
if( !compiler->compileCode( code, filepath ) )
{
last_result[docid].result = OTF_COMPILE_ERROR;
last_result[docid].output = string( EM_lasterror() ) + "\n";
last_result[docid].line = EM_lineNum;
out += last_result[docid].output;
return last_result[docid].result;
}
// allocate the VM message struct
Chuck_Msg * msg = new Chuck_Msg;
// fill in the VM message
msg->code = compiler->output();
msg->code->name = name;
msg->type = CK_MSG_ADD;
msg->args = new vector< string >( args );
// set flag for VM to queue message
msg->reply_queue = TRUE; // 1.5.0.8 (ge) updated VM msg processing system
// execute
vm->queue_msg( msg, 1 );
// check results
t_OTF_RESULT result = handle_reply( docid, out );
_doc_otf_result otf_result;
t_CKBOOL gotit = get_last_result( docid, &otf_result );
if( gotit && result == OTF_SUCCESS )
shred_id = otf_result.shred_id;
else
shred_id = 0;
return result;
}
//-----------------------------------------------------------------------------
// name: replace_code()
// desc: ...
//-----------------------------------------------------------------------------
t_OTF_RESULT miniAudicle::replace_code( string & code, string & name,
vector< string > & args, string & filepath,
t_CKUINT docid, t_CKUINT & shred_id,
string & out )
{
if( documents.find( docid ) == documents.end() )
{
out += "internal error at miniAudicle::replace_code\n";
return OTF_MINI_ERROR;
}
while( documents[docid]->size() && documents[docid]->back() == 0 )
documents[docid]->pop_back();
if( documents[docid]->size() == 0 )
{
out += "no shred to replace\n";
return OTF_MINI_ERROR;
}
// compile -- always compile the code in the buffer
// even if the buffer has been saved to file;
// however, the filepath can be provided as directory basis
if( !compiler->compileCode( code, filepath ) )
{
last_result[docid].result = OTF_COMPILE_ERROR;
last_result[docid].output = string( EM_lasterror() ) + "\n";
last_result[docid].line = EM_lineNum;
out += last_result[docid].output;
return last_result[docid].result;
}
Chuck_Msg * msg = new Chuck_Msg;
msg->code = compiler->output();
msg->code->name = name;
msg->type = CK_MSG_REPLACE;
msg->param = documents[docid]->back();
msg->args = new vector< string >( args );
// set flag for VM to queue message
msg->reply_queue = TRUE; // 1.5.0.8 (ge) updated VM msg processing system
// execute
vm->queue_msg( msg, 1 );
// check results
t_OTF_RESULT result = handle_reply( docid, out );
_doc_otf_result otf_result;
t_CKBOOL gotit = get_last_result( docid, &otf_result );
if( gotit && result == OTF_SUCCESS )
shred_id = otf_result.shred_id;
else
shred_id = 0;
return result;
}
//-----------------------------------------------------------------------------
// name: remove_code()
// desc: ...
//-----------------------------------------------------------------------------
t_OTF_RESULT miniAudicle::remove_code( t_CKUINT docid, t_CKUINT & shred_id,
string & out, const string & globalEventName )
{
if( documents.find( docid ) == documents.end() )
{
out += "internal error at miniAudicle::remove_code\n";
return OTF_MINI_ERROR;
}
while( documents[docid]->size() && documents[docid]->back() == 0 )
documents[docid]->pop_back();
if( documents[docid]->size() == 0 )
{
out += "no shred to remove\n";
return OTF_MINI_ERROR;
}
// return code
t_OTF_RESULT result = OTF_UNDEFINED;
// get shred ID to remove
shred_id = documents[docid]->back();
// check which method to remove code
if( globalEventName == "" ) // no named global event to trigger
{
// remove the shred from ChucK VM
result = remove_shred( docid, shred_id, out );
// check result code
if( result != OTF_SUCCESS ) { shred_id = 0; }
}
else // broadcast to named global event | 1.5.4.4 (ge) added
{
// broadcast global event by name
vm->globals_manager()->broadcastGlobalEvent( globalEventName.c_str() );
// set result code; assume this worked
result = OTF_SUCCESS;
}
return result;
}
//-----------------------------------------------------------------------------
// name: remove_shred()
// desc: ...
//-----------------------------------------------------------------------------
t_OTF_RESULT miniAudicle::remove_shred( t_CKUINT docid, t_CKUINT shred_id,
string & out )
{
Chuck_Msg * msg = new Chuck_Msg;
msg->type = CK_MSG_REMOVE;
msg->param = shred_id;
// set flag for VM to queue message
msg->reply_queue = TRUE; // 1.5.0.8 (ge) updated VM msg processing system
// execute
vm->queue_msg( msg, 1 );
// check results
return handle_reply( docid, out );
}
//-----------------------------------------------------------------------------
// name: removeall()
// desc: ...
//-----------------------------------------------------------------------------
t_OTF_RESULT miniAudicle::removeall( t_CKUINT docid, string & out )
{
Chuck_Msg * msg = new Chuck_Msg;
msg->type = CK_MSG_REMOVEALL;
// set flag for VM to queue message
msg->reply_queue = TRUE; // 1.5.0.8 (ge) updated VM msg processing system
// execute
vm->queue_msg( msg, 1 );
// check results
return handle_reply( docid, out );
}
//-----------------------------------------------------------------------------
// name: removelast()
// desc: ...
//-----------------------------------------------------------------------------
t_OTF_RESULT miniAudicle::removelast( t_CKUINT docid, string & out )
{
Chuck_Msg * msg = new Chuck_Msg;
msg->type = CK_MSG_REMOVE;
msg->param = 0xffffffff;
// set flag for VM to queue message
msg->reply_queue = TRUE; // 1.5.0.8 (ge) updated VM msg processing system
// execute
vm->queue_msg( msg, 1 );
// get reply
return handle_reply( docid, out );
}
//-----------------------------------------------------------------------------
// name: clearvm()
// desc: ...
//-----------------------------------------------------------------------------
t_OTF_RESULT miniAudicle::clearvm( t_CKUINT docid, string & out )
{
Chuck_Msg * msg = new Chuck_Msg;
// set message type
msg->type = CK_MSG_CLEARVM;
// set flag for VM to queue message
msg->reply_queue = TRUE; // 1.5.0.8 (ge) updated VM msg processing system
// execute
vm->queue_msg( msg, 1 );
// check results
return handle_reply( docid, out );
}
//-----------------------------------------------------------------------------
// name: handle_reply()
// desc: ...
//-----------------------------------------------------------------------------
t_OTF_RESULT miniAudicle::handle_reply( t_CKUINT docid, string & out )
{
last_result[docid].result = OTF_UNDEFINED;
otf_docids.push( docid );
int sleep_count = 0;
// process
while( last_result[docid].result == OTF_UNDEFINED )
{
if( !process_reply() )
{
if( sleep_count < vm_sleep_max )
{
ck_usleep( vm_sleep_time );
sleep_count++;
}
else
{
return OTF_VM_TIMEOUT;
}
}
}
out += last_result[docid].output;
return last_result[docid].result;
}
//-----------------------------------------------------------------------------
// name: Chuck_VM_Status_copy()
// desc: ...
//-----------------------------------------------------------------------------
Chuck_VM_Status & Chuck_VM_Status_copy( Chuck_VM_Status & a, const Chuck_VM_Status & b )
{
a = b;
a.list.clear();
Chuck_VM_Shred_Status * shred;
for( t_CKUINT i = 0; i < b.list.size(); i++ )
{
shred = b.list[i];
a.list.push_back( new Chuck_VM_Shred_Status( shred->xid,
shred->name,
shred->start,
shred->has_event ) );
}
return a;
}
//-----------------------------------------------------------------------------
// name: status()
// desc: ...
//-----------------------------------------------------------------------------
t_OTF_RESULT miniAudicle::status( Chuck_VM_Status * status )
{
if( vm_on != TRUE || vm == NULL )
return OTF_MINI_ERROR;
t_CKBOOL do_copy_status = TRUE;
while( process_reply() == TRUE )
;
if( do_copy_status && status_bufs[status_bufs_read] &&
status_bufs[status_bufs_read]->now_system >= status->now_system )
{
status->clear();
Chuck_VM_Status_copy( *status, *( status_bufs[status_bufs_read] ) );
do_copy_status = FALSE;
}
for( size_t i = 0; i < num_status_bufs; i++ )
{
if( i != status_bufs_read && status_bufs[i] )
{
if( do_copy_status )
{
status->clear();
Chuck_VM_Status_copy( *status, *( status_bufs[i] ) );
}
// create a new chuck message
Chuck_Msg * msg = new Chuck_Msg;
// set message type
msg->type = CK_MSG_STATUS;
// set vm status struct pointer to rotating status buffers
msg->status = status_bufs[i];
status_bufs[i] = NULL;
// set flag for VM to queue message
msg->reply_queue = TRUE; // 1.5.0.8 (ge) updated VM msg
// execute
vm->queue_msg( msg, 1 );
return OTF_SUCCESS;
}
}
//EM_log( CK_LOG_HERALD, "(miniAudicle): insufficient buffers for status query" );
return OTF_MINI_ERROR;
}
//-----------------------------------------------------------------------------
// name: process_reply()
// desc: ...
//-----------------------------------------------------------------------------
t_CKBOOL miniAudicle::process_reply()
{
Chuck_Msg * msg;
if( ( msg = vm->get_reply() ) == NULL )
return FALSE;
if( msg->type == CK_MSG_STATUS )
{
vm_status_timeouts = 0;
size_t i = 0;
for( i = 0; i < num_status_bufs; i++ )
{
if( !status_bufs[i] )
{
status_bufs[i] = msg->status;
status_bufs_read = i;
break;
}
}
if( i == num_status_bufs )
EM_log( CK_LOG_HERALD, "(miniAudicle): insufficient buffers for status query, leaking memory" );
CK_SAFE_DELETE( msg );
return TRUE;
}
t_CKUINT shred_id;
t_CKUINT docid;
docid = otf_docids.front();
otf_docids.pop();
// if associated document id no longer exists,
// then no one cares about this message
if( !documents.count( docid ) )
{
CK_SAFE_DELETE( msg );
return TRUE;
}
switch( msg->type )
{
case CK_MSG_ADD:
shred_id = msg->replyA;
if( shred_id == 0 )
{
// if the docid is still valid
if( documents.count( docid ) )
{
last_result[docid].result = OTF_VM_ERROR;
last_result[docid].output = string( EM_lasterror() ) + "\n";
}
}
else
{
// if the docid is still valid
if( documents.count( docid ) )
{
documents[docid]->push_back( shred_id );
shreds[shred_id].docid = docid;
shreds[shred_id].index = documents[docid]->size() - 1;
last_result[docid].result = OTF_SUCCESS;
last_result[docid].output = string( EM_lasterror() ) + "\n";
last_result[docid].shred_id = shred_id;
}
}
break;
case CK_MSG_REPLACE:
shred_id = msg->replyA;
if( shred_id == 0 )
{
shreds.erase( documents[docid]->back() );
documents[docid]->pop_back(); // shred probably no longer exists
last_result[docid].result = OTF_VM_ERROR;
last_result[docid].output = string( EM_lasterror() ) + "\n";
last_result[docid].shred_id = shred_id;
}
else
{
// in case the shred id changed, recreate the various map entries
shreds.erase( documents[docid]->back() );
documents[docid]->back() = shred_id;
shreds[shred_id].docid = docid;
shreds[shred_id].index = documents[docid]->size() - 1;
last_result[docid].result = OTF_SUCCESS;
last_result[docid].output = string( EM_lasterror() ) + "\n";
}
break;
case CK_MSG_REMOVE:
/*
if( msg->param == 0xffffffff )
// remove last
{
if( msg->replyA == 0 )
{
last_result[docid].result = OTF_VM_ERROR;
last_result[docid].output = string( EM_lasterror() ) + "\n";
}
else
{
shred_id = msg->replyA;
// set the corresponding document-shred association to 0, so future calls
// to document-based otf functions will ignore that association
if( shreds.count( shred_id ) )
{
if( documents.count( shreds[shred_id].docid ) )
documents[shreds[shred_id].docid]->at( shreds[shred_id].index ) = 0;
shreds.erase( shred_id );
}
last_result[docid].result = OTF_SUCCESS;
last_result[docid].output = "";
}
}
else
{
shred_id = msg->param;
// set the corresponding document-shred association to 0, so future calls
// to document-based otf functions will ignore that association
if( shreds.count( shred_id ) )
{
if( documents.count( shreds[shred_id].docid ) )
documents[shreds[shred_id].docid]->at( shreds[shred_id].index ) = 0;
shreds.erase( shred_id );
}
if( msg->replyA == 0 )
{
last_result[docid].result = OTF_VM_ERROR;
last_result[docid].output = string( EM_lasterror() ) + "\n";
}
else
{
last_result[docid].result = OTF_SUCCESS;
last_result[docid].output = "";
}
}
*/
shred_id = msg->replyA;
// set the corresponding document-shred association to 0, so future calls
// to document-based otf functions will ignore that association
if( shreds.count( shred_id ) )
{
if( documents.count( shreds[shred_id].docid ) )
documents[shreds[shred_id].docid]->at( shreds[shred_id].index ) = 0;
shreds.erase( shred_id );
}
if( shred_id == 0 )
{
last_result[docid].result = OTF_VM_ERROR;
last_result[docid].output = string( EM_lasterror() ) + "\n";
}
else
{
last_result[docid].result = OTF_SUCCESS;
last_result[docid].output = "";
}
break;
case CK_MSG_REMOVEALL:
if( msg->replyA == 0 )
{
last_result[docid].result = OTF_VM_ERROR;
last_result[docid].output = string( EM_lasterror() ) + "\n";
}
else
{
last_result[docid].result = OTF_SUCCESS;
last_result[docid].output = "";
}
break;
}
// clean up the message that we initially created
CK_SAFE_DELETE( msg );
return TRUE;
}
//-----------------------------------------------------------------------------
// name: get_last_result()
// desc: ...
//-----------------------------------------------------------------------------
t_CKBOOL miniAudicle::get_last_result( t_CKUINT docid, t_OTF_RESULT * result,
string * out, t_CKINT * line_num )
{
if( last_result.count( docid ) == 0 )
return FALSE;
if( result )
*result = last_result[docid].result;
if( out )
*out = last_result[docid].output;
if( line_num )
*line_num = last_result[docid].line;
return TRUE;
}
//-----------------------------------------------------------------------------
// name: get_last_result()
// desc: ...
//-----------------------------------------------------------------------------
t_CKBOOL miniAudicle::get_last_result( t_CKUINT docid, _doc_otf_result * result )
{
if( last_result.count( docid ) == 0 )
return FALSE;
*result = last_result[docid];
return TRUE;
}
//-----------------------------------------------------------------------------
// name: abort_current_shred()
// desc: ...
//-----------------------------------------------------------------------------
t_CKINT miniAudicle::abort_current_shred()
{
vm->abort_current_shred();
return 0;
}
//-----------------------------------------------------------------------------
// name: allocate_document_id()
// desc: ...
//-----------------------------------------------------------------------------
t_CKUINT miniAudicle::allocate_document_id()
{
next_document_id++;
// ensure that we have a next_document_id that isnt actually in use
// if there are more than INT_MAX documents this will infinite loop
while( documents.find( next_document_id ) != documents.end() )
next_document_id++;
documents[next_document_id] = new vector< t_CKUINT >;
return next_document_id;
}
//-----------------------------------------------------------------------------
// name: free_document_id()
// desc: ...
//-----------------------------------------------------------------------------
void miniAudicle::free_document_id( t_CKUINT docid )
{
if( documents.find( docid ) != documents.end() )
{
last_result.erase( docid );
// delete associated shred records
// (they still exist in the vm but we don't care)
vector< t_CKUINT > * doc_shreds = documents[docid];
vector< t_CKUINT >::size_type i = 0, len = doc_shreds->size();
for( ; i < len; i++ )
if( shreds.count( doc_shreds->at( i ) ) )
shreds.erase( doc_shreds->at( i ) );
// delete data
CK_SAFE_DELETE( documents[docid] );
// remove from map
documents.erase( docid );
}
}
//-----------------------------------------------------------------------------
// name: get_log_level()
// desc: ...
//-----------------------------------------------------------------------------
t_CKINT miniAudicle::get_log_level()
{
return ChucK::getLogLevel();
}
//-----------------------------------------------------------------------------
// name: set_log_level()
// desc: ...
//-----------------------------------------------------------------------------
void miniAudicle::set_log_level( t_CKINT n )
{
ChucK::setLogLevel( n );
}
//-----------------------------------------------------------------------------
// name: start_vm()
// desc: ...
//-----------------------------------------------------------------------------
t_CKBOOL miniAudicle::start_vm()
{
// allocate status buffers
// allocate alternating buffers for VM status messages
num_status_bufs = 4;
status_bufs = new Chuck_VM_Status * [num_status_bufs];
for( size_t i = 0; i < num_status_bufs; i++ )
status_bufs[i] = new Chuck_VM_Status;
status_bufs_read = 0;
status_bufs_write = 0;
// for saving log level
t_CKINT savedLogLevel = CK_LOG_SYSTEM;
// clear shred management structures
last_result.clear();
while( !otf_docids.empty() )
otf_docids.pop();
map< t_CKUINT, vector< t_CKUINT > * >::iterator iter = documents.begin(),
end = documents.end();
for( ; iter != end; iter++ )
iter->second->clear();
shreds.clear();
// clear the class name existence map
class_names->clear();
// dac and adc devices names | chuck-1.5.0.0 (ge) added
string dac_device_name = "";
string adc_device_name = "";
// time stamp
EM_log( CK_LOG_SYSTEM, "-------( %s )-------", timestamp_formatted().c_str() );
// log
EM_log( CK_LOG_SYSTEM, "starting chuck virtual machine..." );
// push log
EM_pushlog();
if( m_chuck == NULL )
{
// log
EM_log( CK_LOG_INFO, "allocating VM..." );
t_CKBOOL enable_audio = vm_options.enable_audio;
t_CKBOOL vm_halt = FALSE;
t_CKUINT srate = vm_options.srate != 0 ? vm_options.srate : CK_SAMPLE_RATE_DEFAULT;
t_CKUINT buffer_size = vm_options.buffer_size;
t_CKUINT num_buffers = CK_NUM_BUFFERS_DEFAULT;
string driverName = vm_options.driver;
t_CKUINT dac = vm_options.dac;
t_CKUINT adc = vm_options.adc;
t_CKBOOL force_srate = vm_options.force_srate && vm_options.srate != 0;
t_CKBOOL set_priority = FALSE;
t_CKBOOL block = vm_options.enable_block;
t_CKUINT output_channels = vm_options.num_outputs;
t_CKUINT input_channels = vm_options.num_inputs;
t_CKUINT adaptive_size = 0;
// lets make up some magic numbers...
vm_sleep_time = vm_options.buffer_size * 1000000 / vm_options.srate;
vm_sleep_max = 20;
vm_status_timeouts_max = vm_options.buffer_size / 100;
vm_status_timeouts = 0;
// check buffer size
// buffer_size = next_power_2( buffer_size-1 );
// audio, boost
if( !set_priority && !block ) priority = priority_low;
if( !set_priority && !enable_audio ) priority = 0x7fffffff;
// set priority
// Chuck_VM::our_priority = priority;
// set watchdog
#ifdef __MACOSX_CORE__
g_do_watchdog = TRUE;
g_watchdog_timeout = .5;
#else
g_do_watchdog = FALSE;
#endif
std::list<std::string> dl_search_path_user = vm_options.library_paths;
std::list<std::string> named_chugins = vm_options.named_chugins;
// normalize paths
for(std::list<std::string>::iterator i = dl_search_path_user.begin();
i != dl_search_path_user.end(); i++)
*i = expand_filepath(*i);
for(std::list<std::string>::iterator j = named_chugins.begin();
j != named_chugins.end(); j++)
*j = expand_filepath(*j);
m_chuck = new ChucK();
// set chuck chout/cherr redirect handlers
if( m_console_callback )
{
m_chuck->setChoutCallback( m_console_callback) ;
m_chuck->setCherrCallback( m_console_callback );
}
#ifdef __MA_COLOR_CONSOLE__
// enable chuck color text output, using ANSI escape codes to be processed and rendered by mA console
m_chuck->setParam( CHUCK_PARAM_TTY_COLOR, TRUE );
#endif
//--------------------------- AUDIO I/O SETUP ---------------------------------
// log
EM_log( CK_LOG_SYSTEM, "initializing audio subsystem..." );
// push
EM_pushlog();
// temporarily lower the verbose level; starting the VM with a high log level
// (e.g., FINEST) can result in A LOT of log within chuck initialization, which on
// on some systems (linux/macOS-qt) could result in DEADLOCK \[o]/ after writing sufficiently
// enough output to stderr but with no mechanism to drain the buffer--possible cause:
// for non-windows platforms, we've set up stderr redirect to be consumed through Qt's