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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
|
/**********************************************************************
nyx.c
Nyx: A very simple external interface to Nyquist
Dominic Mazzoni
**********************************************************************/
/* system includes */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#ifndef WIN32
#include <unistd.h>
#endif
/* nyx includes */
#include "nyx.h"
/* xlisp includes */
#include "switches.h"
#include "xlisp.h"
#include "cext.h"
/* nyquist includes */
#include "sound.h"
#include "samples.h"
#include "falloc.h"
/* use full copy */
#define NYX_FULL_COPY 1
/* show memory stats */
// #define NYX_MEMORY_STATS 1
/* show details of obarray copy */
// #define NYX_DEBUG_COPY 1
/* macro to compute the size of a segment (taken from xldmem.h) */
#define segsize(n) (sizeof(SEGMENT)+((n)-1)*sizeof(struct node))
/* xldmem external variables */
extern long nnodes;
extern long nfree;
extern long total;
extern int nsegs;
extern SEGMENT *segs;
extern SEGMENT *lastseg;
extern LVAL fnodes;
/* nyquist externs */
extern LVAL a_sound;
extern snd_list_type zero_snd_list;
/* globals */
LOCAL nyx_os_callback nyx_os_cb = NULL;
LOCAL void *nyx_os_ud;
LOCAL nyx_output_callback nyx_output_cb;
LOCAL void *nyx_output_ud;
LOCAL int nyx_expr_pos;
LOCAL int nyx_expr_len;
LOCAL const char *nyx_expr_string;
LOCAL LVAL nyx_result;
LOCAL nyx_rval nyx_result_type = nyx_error;
LOCAL XLCONTEXT nyx_cntxt;
LOCAL int nyx_first_time = 1;
LOCAL LVAL nyx_obarray;
LOCAL FLOTYPE nyx_warp_stretch;
/* Suspension node */
typedef struct nyx_susp_struct {
snd_susp_node susp; // Must be first
nyx_audio_callback callback;
void *userdata;
long len;
int channel;
} nyx_susp_node, *nyx_susp_type;
#if defined(NYX_DEBUG_COPY) && NYX_DEBUG_COPY
static const char *_types_[] =
{
"FREE_NODE",
"SUBR",
"FSUBR",
"CONS",
"SYMBOL",
"FIXNUM",
"FLONUM",
"STRING",
"OBJECT",
"STREAM",
"VECTOR",
"CLOSURE",
"CHAR",
"USTREAM",
"EXTERN"
};
// Dump the contents of the obarray
LOCAL void nyx_show_obarray()
{
LVAL array = getvalue(obarray);
LVAL sym;
int i;
for (i = 0; i < HSIZE; i++) {
for (sym = getelement(array, i); sym; sym = cdr(sym)) {
LVAL syma = car(sym);
printf("_sym_ = ");
xlprint(getvalue(s_stdout), syma, TRUE);
if (getvalue(syma)) {
printf(" _type_ = %s _val_ = ", _types_[ntype(getvalue(syma))]);
xlprint(getvalue(s_stdout), getvalue(syma), TRUE);
}
if (getfunction(syma)) {
printf(" _type_ = %s _fun_ = ", _types_[ntype(getfunction(syma))]);
xlprint(getvalue(s_stdout), getfunction(syma), TRUE);
}
printf("\n");
}
}
}
#endif
//
// Free empty segments
//
LOCAL void freesegs()
{
SEGMENT *seg;
SEGMENT *next;
// Free up as many nodes as possible
gc();
// Reset free node tracking
fnodes = NIL;
nfree = 0L;
// Reset the last segment pointer
lastseg = NULL;
// Scan all segments
for (seg = segs; seg != NULL; seg = next) {
int n = seg->sg_size;
int empty = TRUE;
int i;
LVAL p;
// Check this segment for in-use nodes
p = &seg->sg_nodes[0];
for (i = n; --i >= 0; ++p) {
if (ntype(p) != FREE_NODE) {
empty = FALSE;
break;
}
}
// Retain pointer to next segment
next = seg->sg_next;
// Was the current segment empty?
if (empty) {
// Free the segment;
free((void *) seg);
// Unlink it from the list. No need to worry about a NULL lastseg
// pointer here since the fixnum and char segments will always exist
// at the head of the list and they will always have nodes. So, lastseg
// will have been set before we find any empty nodes.
lastseg->sg_next = next;
// Reduce the stats
total -= (long) segsize(n);
nsegs--;
nnodes -= n;
}
else {
// Not empty, so remember this node as the last segment
lastseg = seg;
// Add all of the free nodes in this segment to the free list
p = &seg->sg_nodes[0];
for (i = n; --i >= 0; ++p) {
if (ntype(p) == FREE_NODE) {
rplaca(p, NIL);
rplacd(p, fnodes);
fnodes = p;
nfree++;
}
}
}
}
}
#if defined(NYX_FULL_COPY) && NYX_FULL_COPY
// Copy a node (recursively if appropriate)
LOCAL LVAL nyx_dup_value(LVAL val)
{
LVAL nval = val;
// Protect old and new values
xlprot1(val);
xlprot1(nval);
// Copy the node
if (val != NIL) {
switch (ntype(val))
{
case FIXNUM:
nval = cvfixnum(getfixnum(val));
break;
case FLONUM:
nval = cvflonum(getflonum(val));
break;
case CHAR:
nval = cvchar(getchcode(val));
break;
case STRING:
nval = cvstring((char *) getstring(val));
break;
case VECTOR:
{
int len = getsize(val);
int i;
nval = newvector(len);
nval->n_type = ntype(val);
for (i = 0; i < len; i++) {
if (getelement(val, i) == val) {
setelement(nval, i, val);
}
else {
setelement(nval, i, nyx_dup_value(getelement(val, i)));
}
}
}
break;
case CONS:
nval = cons(nyx_dup_value(car(val)), nyx_dup_value(cdr(val)));
break;
case SUBR:
case FSUBR:
nval = cvsubr(getsubr(val), ntype(val), getoffset(val));
break;
// Symbols should never be copied since their addresses are cached
// all over the place.
case SYMBOL:
nval = val;
break;
// Streams are not copied (although USTREAM could be) and reference
// the original value.
case USTREAM:
case STREAM:
nval = val;
break;
// Externals aren't copied because I'm not entirely certain they can be.
case EXTERN:
nval = val;
break;
// For all other types, just allow them to reference the original
// value. Probably not the right thing to do, but easier.
case OBJECT:
case CLOSURE:
default:
nval = val;
break;
}
}
xlpop();
xlpop();
return nval;
}
// Make a copy of the original obarray, leaving the original in place
LOCAL void nyx_save_obarray()
{
LVAL newarray;
int i;
// This provide permanent protection for nyx_obarray as we do not want it
// to be garbage-collected.
xlprot1(nyx_obarray);
nyx_obarray = getvalue(obarray);
// Create and set the new vector. This allows us to use xlenter() to
// properly add the new symbol. Probably slower than adding directly,
// but guarantees proper hashing.
newarray = newvector(HSIZE);
setvalue(obarray, newarray);
// Scan all obarray vectors
for (i = 0; i < HSIZE; i++) {
LVAL sym;
// Scan all elements
for (sym = getelement(nyx_obarray, i); sym; sym = cdr(sym)) {
LVAL syma = car(sym);
char *name = (char *) getstring(getpname(syma));
LVAL nsym = xlenter(name);
// Ignore *OBARRAY* since there's no need to copy it
if (strcmp(name, "*OBARRAY*") == 0) {
continue;
}
// Duplicate the symbol's values
setvalue(nsym, nyx_dup_value(getvalue(syma)));
setplist(nsym, nyx_dup_value(getplist(syma)));
setfunction(nsym, nyx_dup_value(getfunction(syma)));
}
}
// Swap the obarrays, so that the original is put back into service
setvalue(obarray, nyx_obarray);
nyx_obarray = newarray;
}
// Restore the symbol values to their original value and remove any added
// symbols.
LOCAL void nyx_restore_obarray()
{
LVAL obvec = getvalue(obarray);
int i;
// Scan all obarray vectors
for (i = 0; i < HSIZE; i++) {
LVAL last = NULL;
LVAL dcon;
for (dcon = getelement(obvec, i); dcon; dcon = cdr(dcon)) {
LVAL dsym = car(dcon);
char *name = (char *)getstring(getpname(dsym));
LVAL scon;
// Ignore *OBARRAY* since setting it causes the input array to be
// truncated.
if (strcmp(name, "*OBARRAY*") == 0) {
continue;
}
// Find the symbol in the original obarray.
for (scon = getelement(nyx_obarray, hash(name, HSIZE)); scon; scon = cdr(scon)) {
LVAL ssym = car(scon);
// If found, then set the current symbols value to the original.
if (strcmp(name, (char *)getstring(getpname(ssym))) == 0) {
setvalue(dsym, nyx_dup_value(getvalue(ssym)));
setplist(dsym, nyx_dup_value(getplist(ssym)));
setfunction(dsym, nyx_dup_value(getfunction(ssym)));
break;
}
}
// If we didn't find the symbol in the original obarray, then it must've
// been added since and must be removed from the current obarray.
if (scon == NULL) {
if (last) {
rplacd(last, cdr(dcon));
}
else {
setelement(obvec, i, cdr(dcon));
}
}
// Must track the last dcon for symbol removal
last = dcon;
}
}
}
#else
LOCAL LVAL copylist(LVAL from)
{
LVAL nsym;
if (from == NULL) {
return NULL;
}
return cons(car(from), copylist(cdr(from)));
}
/* Make a copy of the obarray so that we can erase any
changes the user makes to global variables */
LOCAL void nyx_copy_obarray()
{
LVAL newarray;
int i;
// Create and set the new vector.
newarray = newvector(HSIZE);
setvalue(obarray, newarray);
for (i = 0; i < HSIZE; i++) {
LVAL from = getelement(nyx_obarray, i);
if (from) {
setelement(newarray, i, copylist(from));
}
}
}
#endif
void nyx_init()
{
if (nyx_first_time) {
char *argv[1];
argv[0] = "nyquist";
xlisp_main_init(1, argv);
nyx_os_cb = NULL;
nyx_output_cb = NULL;
nyx_first_time = 0;
#if defined(NYX_FULL_COPY) && NYX_FULL_COPY
// Save a copy of the original obarray's contents.
nyx_save_obarray();
#else
// Permanently protect the original obarray value. This is needed since
// it would be unreferenced in the new obarray and would be garbage
// collected. We want to keep it around so we can make copies of it to
// refresh the execution state.
xlprot1(nyx_obarray);
nyx_obarray = getvalue(obarray);
#endif
}
#if !defined(NYX_FULL_COPY) || !NYX_FULL_COPY
// Create a copy of the original obarray
nyx_copy_obarray();
#endif
// Keep nyx_result from being garbage-collected
xlprot1(nyx_result);
#if defined(NYX_MEMORY_STATS) && NYX_MEMORY_STATS
printf("\nnyx_init\n");
xmem();
#endif
}
void nyx_cleanup()
{
// Garbage-collect nyx_result
xlpop();
#if defined(NYX_FULL_COPY) && NYX_FULL_COPY
// Restore the original symbol values
nyx_restore_obarray();
#else
// Restore obarray to original state...but not the values
setvalue(obarray, nyx_obarray);
#endif
// Make sure the sound nodes can be garbage-collected. Sounds are EXTERN
// nodes whose value does not get copied during a full copy of the obarray.
setvalue(xlenter("S"), NIL);
// Free excess memory segments - does a gc()
freesegs();
// No longer need the callbacks
nyx_output_cb = NULL;
nyx_os_cb = NULL;
#if defined(NYX_MEMORY_STATS) && NYX_MEMORY_STATS
printf("\nnyx_cleanup\n");
xmem();
#endif
}
LOCAL void nyx_susp_fetch(register nyx_susp_type susp, snd_list_type snd_list)
{
sample_block_type out;
sample_block_values_type out_ptr;
long n;
int err;
falloc_sample_block(out, "nyx_susp_fetch");
out_ptr = out->samples;
snd_list->block = out;
n = max_sample_block_len;
if (susp->susp.current + n > susp->len)
n = susp->len - susp->susp.current;
err = susp->callback(out_ptr, susp->channel,
susp->susp.current, n, 0, susp->userdata);
if (err) {
// The user canceled or some other error occurred, so we use
// xlsignal() to jump back to our error handler.
xlsignal(NULL, NULL);
// never get here.
}
snd_list->block_len = (short)n;
susp->susp.current += n;
if (n == 0) {
/* we didn't read anything, but can't return length zero, so
convert snd_list to pointer to zero block */
snd_list_terminate(snd_list);
}
else if (n < max_sample_block_len) {
/* should free susp */
snd_list_unref(snd_list->u.next);
/* if something is in buffer, terminate by pointing to zero block */
snd_list->u.next = zero_snd_list;
}
}
LOCAL void nyx_susp_free(nyx_susp_type susp)
{
ffree_generic(susp, sizeof(nyx_susp_node), "nyx_susp_free");
}
LOCAL void nyx_susp_print_tree(nyx_susp_type susp, int n)
{
}
void nyx_capture_output(nyx_output_callback callback, void *userdata)
{
nyx_output_cb = callback;
nyx_output_ud = userdata;
}
void nyx_set_audio_params(double rate, long len)
{
double stretch_len = (len > 0 ? len / rate : 1.0);
LVAL warp;
/* Bind the sample rate to the "*sound-srate*" global */
setvalue(xlenter("*SOUND-SRATE*"), cvflonum(rate));
/* Bind selection len to "len" global */
setvalue(xlenter("LEN"), cvflonum(len));
/* Set the "*warp*" global based on the length of the audio */
xlprot1(warp);
warp = cons(cvflonum(0), /* time offset */
cons(cvflonum(stretch_len), /* time stretch */
cons(NULL, /* cont. time warp */
NULL)));
setvalue(xlenter("*WARP*"), warp);
xlpop();
}
void nyx_set_input_audio(nyx_audio_callback callback,
void *userdata,
int num_channels,
long len, double rate)
{
sample_type scale_factor = 1.0;
time_type t0 = 0.0;
nyx_susp_type *susp;
sound_type *snd;
int ch;
nyx_set_audio_params(rate, len);
susp = (nyx_susp_type *)malloc(num_channels * sizeof(nyx_susp_type));
snd = (sound_type *)malloc(num_channels * sizeof(sound_type));
for(ch=0; ch < num_channels; ch++) {
falloc_generic(susp[ch], nyx_susp_node, "nyx_set_input_audio");
susp[ch]->callback = callback;
susp[ch]->userdata = userdata;
susp[ch]->len = len;
susp[ch]->channel = ch;
susp[ch]->susp.fetch = nyx_susp_fetch;
susp[ch]->susp.keep_fetch = NULL;
susp[ch]->susp.free = nyx_susp_free;
susp[ch]->susp.mark = NULL;
susp[ch]->susp.print_tree = nyx_susp_print_tree;
susp[ch]->susp.name = "nyx";
susp[ch]->susp.toss_cnt = 0;
susp[ch]->susp.current = 0;
susp[ch]->susp.sr = rate;
susp[ch]->susp.t0 = t0;
susp[ch]->susp.log_stop_cnt = 0;
snd[ch] = sound_create((snd_susp_type)susp[ch], t0,
rate,
scale_factor);
}
if (num_channels > 1) {
LVAL array = newvector(num_channels);
for(ch=0; ch<num_channels; ch++)
setelement(array, ch, cvsound(snd[ch]));
setvalue(xlenter("S"), array);
}
else {
LVAL s = cvsound(snd[0]);
setvalue(xlenter("S"), s);
}
}
LOCAL int nyx_is_labels(LVAL expr)
{
/* make sure that we have a list whose first element is a
list of the form (time "label") */
LVAL label;
LVAL first;
LVAL second;
LVAL third;
if (expr == NULL) {
return 0;
}
while (expr != NULL) {
if (!consp(expr))
return 0;
label = car(expr);
if (!consp(label))
return 0;
first = car(label);
if (!(floatp(first) || fixp(first)))
return 0;
if (!consp(cdr(label)))
return 0;
second = car(cdr(label));
if (floatp(second) || fixp(second)) {
if (!consp(cdr(cdr(label))))
return 0;
third = car(cdr(cdr(label)));
if (!(stringp(third)))
return 0;
}
else
if (!(stringp(second)))
return 0;
expr = cdr(expr);
}
return 1;
}
nyx_rval nyx_get_type(LVAL expr)
{
if (nyx_result_type != nyx_error) {
return nyx_result_type;
}
nyx_result_type = nyx_error;
if (expr==NULL) {
return nyx_result_type;
}
switch(ntype(expr))
{
case FIXNUM:
nyx_result_type = nyx_int;
break;
case FLONUM:
nyx_result_type = nyx_double;
break;
case STRING:
nyx_result_type = nyx_string;
break;
case VECTOR:
{
/* make sure it's a vector of sounds */
int i;
nyx_result_type = nyx_audio;
for(i=0; i<getsize(expr); i++) {
if (!soundp(getelement(expr, i))) {
nyx_result_type = nyx_error;
break;
}
}
}
break;
case CONS:
{
/* see if it's a list of time/string pairs representing a
label track */
if (nyx_is_labels(expr))
nyx_result_type = nyx_labels;
}
break;
case EXTERN:
{
if (soundp(expr))
nyx_result_type = nyx_audio;
}
break;
} /* switch */
return nyx_result_type;
}
nyx_rval nyx_eval_expression(const char *expr_string)
{
LVAL expr = NULL;
#if defined(NYX_MEMORY_STATS) && NYX_MEMORY_STATS
printf("\nnyx_eval_expression before\n");
xmem();
#endif
nyx_expr_string = expr_string;
nyx_expr_len = strlen(nyx_expr_string);
nyx_expr_pos = 0;
nyx_result = NULL;
nyx_result_type = nyx_error;
xlprot1(expr);
/* Setup a new context */
xlbegin(&nyx_cntxt, CF_TOPLEVEL|CF_CLEANUP|CF_BRKLEVEL|CF_ERROR, s_true);
/* setup the error return */
if (setjmp(nyx_cntxt.c_jmpbuf)) {
// If the script is cancelled or some other condition occurs that causes
// the script to exit and return to this level, then we don't need to
// restore the previous context.
goto finish;
}
while(nyx_expr_pos < nyx_expr_len) {
expr = NULL;
/* read an expression */
if (!xlread(getvalue(s_stdin), &expr, FALSE))
break;
#if 0
/* save the input expression (so the user can refer to it
as +, ++, or +++) */
xlrdsave(expr);
#endif
/* evaluate the expression */
nyx_result = xleval(expr);
}
xlflush();
xltoplevel();
finish:
xlpop(); /* unprotect expr */
#if defined(NYX_MEMORY_STATS) && NYX_MEMORY_STATS
printf("\nnyx_eval_expression after\n");
xmem();
#endif
return nyx_get_type(nyx_result);
}
int nyx_get_audio_num_channels()
{
if (nyx_get_type(nyx_result) != nyx_audio)
return 0;
if (vectorp(nyx_result))
return getsize(nyx_result);
else
return 1;
}
int nyx_get_audio(nyx_audio_callback callback, void *userdata)
{
sample_block_type block;
sound_type snd;
sound_type *snds = NULL;
float *buffer = NULL;
long bufferlen = 0;
long *totals = NULL;
long *lens = NULL;
long cnt;
int result = 0;
int num_channels;
int ch, i;
int success = FALSE;
if (nyx_get_type(nyx_result) != nyx_audio)
return success;
#if defined(NYX_MEMORY_STATS) && NYX_MEMORY_STATS
printf("\nnyx_get_audio before\n");
xmem();
#endif
num_channels = nyx_get_audio_num_channels();
snds = (sound_type *)malloc(num_channels * sizeof(sound_type));
if (snds == NULL) {
goto finish;
}
totals = (long *)malloc(num_channels * sizeof(long));
if (totals == NULL) {
goto finish;
}
lens = (long *)malloc(num_channels * sizeof(long));
if (lens == NULL) {
goto finish;
}
/* Setup a new context */
xlbegin(&nyx_cntxt, CF_TOPLEVEL|CF_CLEANUP|CF_BRKLEVEL|CF_ERROR, s_true);
/* setup the error return */
if (setjmp(nyx_cntxt.c_jmpbuf)) {
// If the script is cancelled or some other condition occurs that causes
// the script to exit and return to this level, then we don't need to
// restore the previous context.
goto finish;
}
for(ch=0; ch<num_channels; ch++) {
if (num_channels == 1)
snd = getsound(nyx_result);
else
snd = getsound(getelement(nyx_result, ch));
snds[ch] = snd;
totals[ch] = 0;
lens[ch] = snd_length(snd, snd->stop);
}
while(result==0) {
for(ch=0; ch<num_channels; ch++) {
snd = snds[ch];
cnt = 0;
block = snd->get_next(snd, &cnt);
if (block == zero_block || cnt == 0) {
result = -1;
break;
}
/* copy the data to a temporary buffer and scale it
by the appropriate scale factor */
if (cnt > bufferlen) {
if (buffer)
free(buffer);
buffer = (float *)malloc(cnt * sizeof(float));
if (buffer == NULL) {
goto finish;
}
bufferlen = cnt;
}
memcpy(buffer, block->samples, cnt * sizeof(float));
for(i=0; i<cnt; i++)
buffer[i] *= snd->scale;
result = callback(buffer, ch,
totals[ch], cnt, lens[ch], userdata);
if (result != 0) {
// The user canceled or some other error occurred, so we use
// xlsignal() to jump back to our error handler.
xlsignal(NULL, NULL);
// never get here.
}
totals[ch] += cnt;
}
}
success = TRUE;
xltoplevel();
finish:
gc();
if (buffer) {
free(buffer);
}
if (lens) {
free(lens);
}
if (totals) {
free(totals);
}
if (snds) {
free(snds);
}
#if defined(NYX_MEMORY_STATS) && NYX_MEMORY_STATS
printf("\nnyx_get_audio after\n");
xmem();
#endif
return success;
}
int nyx_get_int()
{
if (nyx_get_type(nyx_result) != nyx_int)
return -1;
return getfixnum(nyx_result);
}
double nyx_get_double()
{
if (nyx_get_type(nyx_result) != nyx_double)
return -1.0;
return getflonum(nyx_result);
}
const char *nyx_get_string()
{
if (nyx_get_type(nyx_result) != nyx_string)
return NULL;
return (const char *)getstring(nyx_result);
}
unsigned int nyx_get_num_labels()
{
LVAL s = nyx_result;
int count = 0;
if (nyx_get_type(nyx_result) != nyx_labels)
return 0;
while(s) {
count++;
s = cdr(s);
}
return count;
}
void nyx_get_label(unsigned int index,
double *start_time,
double *end_time,
const char **label)
{
LVAL s = nyx_result;
LVAL label_expr;
LVAL t0_expr;
LVAL t1_expr;
LVAL str_expr;
if (nyx_get_type(nyx_result) != nyx_labels)
return;
while(index) {
index--;
s = cdr(s);
if (s == NULL) {
// index was larger than number of labels
return;
}
}
/* We either have (t0 "label") or (t0 t1 "label") */
label_expr = car(s);
t0_expr = car(label_expr);
t1_expr = car(cdr(label_expr));
if (stringp(t1_expr)) {
str_expr = t1_expr;
t1_expr = t0_expr;
}
else
str_expr = car(cdr(cdr(label_expr)));
if (floatp(t0_expr))
*start_time = getflonum(t0_expr);
else if (fixp(t0_expr))
*start_time = (double)getfixnum(t0_expr);
if (floatp(t1_expr))
*end_time = getflonum(t1_expr);
else if (fixp(t1_expr))
*end_time = (double)getfixnum(t1_expr);
*label = (const char *)getstring(str_expr);
}
const char *nyx_get_error_str()
{
return NULL;
}
void nyx_set_os_callback(nyx_os_callback callback, void *userdata)
{
nyx_os_cb = callback;
nyx_os_ud = userdata;
}
void nyx_stop()
{
xlflush();
xltoplevel();
}
void nyx_break()
{
xlflush();
xlbreak("BREAK", s_unbound);
}
void nyx_continue()
{
xlflush();
xlcontinue();
}
int ostgetc()
{
if (nyx_expr_pos < nyx_expr_len) {
fflush(stdout);
return (nyx_expr_string[nyx_expr_pos++]);
}
else if (nyx_expr_pos == nyx_expr_len) {
/* Add whitespace at the end so that the parser
knows that this is the end of the expression */
nyx_expr_pos++;
return '\n';
}
else
return EOF;
}
/* osinit - initialize */
void osinit(char *banner)
{
}
/* osfinish - clean up before returning to the operating system */
void osfinish(void)
{
}
/* oserror - print an error message */
void oserror(char *msg)
{
printf("nyx error: %s\n", msg);
}
long osrand(long n)
{
return (((int) rand()) % n);
}
/* cd ..
open - open an ascii file */
FILE *osaopen(name,mode) char *name,*mode;
{
FILE *fp;
fp = fopen(name,mode);
return fp;
}
/* osbopen - open a binary file */
FILE *osbopen(char *name, char *mode)
{
char bmode[10];
FILE *fp;
strncpy(bmode, mode, 8);
strcat(bmode,"b");
fp = fopen(name,bmode);
return fp;
}
/* osclose - close a file */
int osclose(FILE *fp)
{
return (fclose(fp));
}
/* osagetc - get a character from an ascii file */
int osagetc(FILE *fp)
{
return (getc(fp));
}
/* osaputc - put a character to an ascii file */
int osaputc(int ch, FILE *fp)
{
return (putc(ch,fp));
}
/* osoutflush - flush output to a file */
void osoutflush(FILE *fp) { fflush(fp); }
extern int dbgflg;
/* osbgetc - get a character from a binary file */
/* int osbgetc(fp) FILE *fp; {return (getc(fp));} */
#ifndef WIN32 // duplicated in winfun.c, per James Crook, 7/4/2003
int osbgetc(FILE *fp)
{
return (getc(fp));
}
#endif
/* osbputc - put a character to a binary file */
int osbputc(int ch, FILE *fp)
{
return (putc(ch,fp));
}
/* ostputc - put a character to the terminal */
void ostputc(int ch)
{
oscheck(); /* check for control characters */
if (nyx_output_cb)
nyx_output_cb(ch, nyx_output_ud);
else
putchar(((char) ch));
}
/* ostoutflush - flush output buffer */
void ostoutflush()
{
if (!nyx_output_cb)
fflush(stdout);
}
/* osflush - flush the terminal input buffer */
void osflush(void)
{
}
/* oscheck - check for control characters during execution */
void oscheck(void)
{
if (nyx_os_cb) {
nyx_os_cb(nyx_os_ud);
}
/* if they hit control-c:
xflush(); xltoplevel(); return;
*/
}
/* xsystem - execute a system command */
#ifndef WIN32 // duplicated in winfun.c, per James Crook, 7/4/2003
LVAL xsystem()
{
if (moreargs()) {
unsigned char *cmd;
cmd = (unsigned char *)getstring(xlgastring());
fprintf(stderr, "Will not execute system command: %s\n", cmd);
}
return s_true;
}
#endif
#ifndef WIN32
/* xsetdir -- set current directory of the process */
LVAL xsetdir()
{
char *dir = (char *)getstring(xlgastring());
int result;
LVAL cwd = NULL;
xllastarg();
result = chdir(dir);
if (result) {
perror("SETDIR");
}
dir = getcwd(NULL, 1000);
if (dir) {
cwd = cvstring(dir);
free(dir);
}
return cwd;
}
#endif
/* xgetkey - get a key from the keyboard */
#ifndef WIN32 // duplicated in winfun.c, per James Crook, 7/4/2003
LVAL xgetkey() {xllastarg(); return (cvfixnum((FIXTYPE)getchar()));}
#endif
/* ossymbols - enter os specific symbols */
#ifndef WIN32 // duplicated in winfun.c, per James Crook, 7/4/2003
void ossymbols(void) {}
#endif
/* xsetupconsole -- used to configure window in Win32 version */
#ifndef WIN32 // duplicated in winfun.c, per James Crook, 7/4/2003
LVAL xsetupconsole() { return NULL; }
#endif
const char os_pathchar = '/';
const char os_sepchar = ':';
/* control-C handling */
void ctcinit() {}
/* xechoenabled -- set/clear echo_enabled flag (unix only) */
LVAL xechoenabled() { return NULL; }
/* osdir_list_start -- open a directory listing */
int osdir_list_start(char *path) { return FALSE; }
/* osdir_list_next -- read the next entry from a directory */
char *osdir_list_next() { return NULL; }
/* osdir_list_finish -- close an open directory */
void osdir_list_finish() { return; }
#ifndef WIN32
/* xget_temp_path -- get a path to create temp files */
LVAL xget_temp_path()
{
char *tmp = getenv("TMPDIR");
if (!tmp || !*tmp) {
tmp = getenv("TMP");
if (!tmp || !*tmp) {
tmp = "/tmp/";
}
}
return cvstring(tmp);
}
#endif
#ifndef WIN32
/* xget_user -- get a string identifying the user, for use in file names */
LVAL xget_user()
{
char *user = getenv("USER");
if (!user || !*user) {
user = getenv("USERNAME");
if (!user || !*user) {
errputstr("Warning: could not get user ID, using 'nyquist'\n");
user = "nyquist";
}
}
return cvstring(user);
}
#endif
|