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 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759
|
/*
insert.c:
Copyright (C) 1991, 1997, 1999, 2002, 2005
Barry Vercoe, Istvan Varga, John ffitch,
Gabriel Maldonado, matt ingalls
This file is part of Csound.
The Csound Library is free software; you can redistribute it
and/or modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
Csound 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Csound; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "csoundCore.h" /* INSERT.C */
#include "oload.h"
#include "insert.h" /* for goto's */
#include "aops.h" /* for cond's */
#include "midiops.h"
#include "namedins.h" /* IV - Oct 31 2002 */
#include "pstream.h"
#include "interlocks.h"
#include "csound_type_system.h"
#include "csound_standard_types.h"
#include <inttypes.h>
static void showallocs(CSOUND *);
static void deact(CSOUND *, INSDS *);
static void schedofftim(CSOUND *, INSDS *);
void beatexpire(CSOUND *, double);
void timexpire(CSOUND *, double);
static void instance(CSOUND *, int);
extern int argsRequired(char* argString);
static int insert_midi(CSOUND *csound, int insno, MCHNBLK *chn,
MEVENT *mep);
static int insert_event(CSOUND *csound, int insno, EVTBLK *newevtp);
static void print_messages(CSOUND *csound, int attr, const char *str){
#if defined(WIN32)
switch (attr & CSOUNDMSG_TYPE_MASK) {
case CSOUNDMSG_ERROR:
case CSOUNDMSG_WARNING:
case CSOUNDMSG_REALTIME:
fprintf(stderr, str);
break;
default:
fprintf(stdout, str);
}
#else
FILE *fp = stderr;
if ((attr & CSOUNDMSG_TYPE_MASK) == CSOUNDMSG_STDOUT)
fp = stdout;
if (!attr || !csound->enableMsgAttr) {
fprintf(fp, "%s", str);
return;
}
if ((attr & CSOUNDMSG_TYPE_MASK) == CSOUNDMSG_ORCH)
if (attr & CSOUNDMSG_BG_COLOR_MASK)
fprintf(fp, "\033[4%cm", ((attr & 0x70) >> 4) + '0');
if (attr & CSOUNDMSG_FG_ATTR_MASK) {
if (attr & CSOUNDMSG_FG_BOLD)
fprintf(fp, "\033[1m");
if (attr & CSOUNDMSG_FG_UNDERLINE)
fprintf(fp, "\033[4m");
}
if (attr & CSOUNDMSG_FG_COLOR_MASK)
fprintf(fp, "\033[3%cm", (attr & 7) + '0');
fprintf(fp, "%s", str);
fprintf(fp, "\033[m");
#endif
}
#define QUEUESIZ 64
static void message_string_enqueue(CSOUND *csound, int attr,
const char *str) {
unsigned long wp = csound->message_string_queue_wp;
csound->message_string_queue[wp].attr = attr;
strNcpy(csound->message_string_queue[wp].str, str, MAX_MESSAGE_STR);
//csound->message_string_queue[wp].str[MAX_MESSAGE_STR-1] = '\0';
csound->message_string_queue_wp = wp + 1 < QUEUESIZ ? wp + 1 : 0;
ATOMIC_INCR(csound->message_string_queue_items);
}
static void no_op(CSOUND *csound, int attr,
const char *format, va_list args) {
IGN(csound);
IGN(attr);
IGN(format);
IGN(args);
};
/* do init pass for this instr */
static int init_pass(CSOUND *csound, INSDS *ip) {
int error = 0;
if(csound->oparms->realtime)
csoundLockMutex(csound->init_pass_threadlock);
csound->curip = ip;
csound->ids = (OPDS *)ip;
csound->mode = 1;
while (error == 0 && (csound->ids = csound->ids->nxti) != NULL) {
csound->op = csound->ids->optext->t.oentry->opname;
if (UNLIKELY(csound->oparms->odebug))
csound->Message(csound, "init %s:\n", csound->op);
error = (*csound->ids->iopadr)(csound, csound->ids);
}
csound->mode = 0;
if(csound->oparms->realtime)
csoundUnlockMutex(csound->init_pass_threadlock);
return error;
}
int rireturn(CSOUND *csound, void *p);
/* do reinit pass */
static int reinit_pass(CSOUND *csound, INSDS *ip, OPDS *ids) {
int error = 0;
if(csound->oparms->realtime) {
csoundLockMutex(csound->init_pass_threadlock);
}
csound->curip = ip;
csound->ids = ids;
csound->mode = 1;
while (error == 0 && (csound->ids = csound->ids->nxti) != NULL &&
(csound->ids->iopadr != (SUBR) rireturn)){
csound->op = csound->ids->optext->t.oentry->opname;
if (UNLIKELY(csound->oparms->odebug))
csound->Message(csound, "reinit %s:\n", csound->op);
error = (*csound->ids->iopadr)(csound, csound->ids);
}
csound->mode = 0;
ATOMIC_SET8(ip->actflg, 1);
csound->reinitflag = ip->reinitflag = 0;
if(csound->oparms->realtime)
csoundUnlockMutex(csound->init_pass_threadlock);
return error;
}
/*
* creates a thread to process instance allocations
*/
uintptr_t event_insert_thread(void *p) {
CSOUND *csound = (CSOUND *) p;
ALLOC_DATA *inst = csound->alloc_queue;
float wakeup = (1000*csound->ksmps/csound->esr);
unsigned long rp = 0, items, rpm = 0;
message_string_queue_t *mess = NULL;
void (*csoundMessageStringCallback)(CSOUND *csound,
int attr,
const char *str) = NULL;
void (*csoundMessageCallback)(CSOUND *csound,
int attr,
const char *format,
va_list args)
= csound->csoundMessageCallback_;
if(csound->oparms_.msglevel){
if(csound->message_string_queue == NULL)
csound->message_string_queue = (message_string_queue_t *)
csound->Calloc(csound, QUEUESIZ*sizeof(message_string_queue_t));
mess = csound->message_string_queue;
if(csound->csoundMessageStringCallback)
csoundMessageStringCallback = csound->csoundMessageStringCallback;
else csoundMessageStringCallback = print_messages;
csoundSetMessageStringCallback(csound, message_string_enqueue);
} else {
csoundSetMessageCallback(csound, no_op);
}
while(csound->event_insert_loop) {
// get the value of items_to_alloc
items = ATOMIC_GET(csound->alloc_queue_items);
if(items == 0)
csoundSleep((int) ((int) wakeup > 0 ? wakeup : 1));
else while(items) {
if (inst[rp].type == 3) {
INSDS *ip = inst[rp].ip;
OPDS *ids = inst[rp].ids;
csoundSpinLock(&csound->alloc_spinlock);
reinit_pass(csound, ip, ids);
csoundSpinUnLock(&csound->alloc_spinlock);
ATOMIC_SET(ip->init_done, 1);
}
if (inst[rp].type == 2) {
INSDS *ip = inst[rp].ip;
ATOMIC_SET(ip->init_done, 0);
csoundSpinLock(&csound->alloc_spinlock);
init_pass(csound, ip);
csoundSpinUnLock(&csound->alloc_spinlock);
ATOMIC_SET(ip->init_done, 1);
}
if(inst[rp].type == 1) {
csoundSpinLock(&csound->alloc_spinlock);
insert_midi(csound, inst[rp].insno, inst[rp].chn, &inst[rp].mep);
csoundSpinUnLock(&csound->alloc_spinlock);
}
if(inst[rp].type == 0) {
csoundSpinLock(&csound->alloc_spinlock);
insert_event(csound, inst[rp].insno, &inst[rp].blk);
csoundSpinUnLock(&csound->alloc_spinlock);
}
// decrement the value of items_to_alloc
ATOMIC_DECR(csound->alloc_queue_items);
items--;
rp = rp + 1 < MAX_ALLOC_QUEUE ? rp + 1 : 0;
}
items = ATOMIC_GET(csound->message_string_queue_items);
while(items) {
if(mess != NULL)
csoundMessageStringCallback(csound, mess[rpm].attr, mess[rpm].str);
ATOMIC_DECR(csound->message_string_queue_items);
items--;
rpm = rpm + 1 < QUEUESIZ ? rpm + 1 : 0;
}
}
csoundSetMessageCallback(csound, csoundMessageCallback);
return (uintptr_t) NULL;
}
int init0(CSOUND *csound)
{
INSTRTXT *tp = csound->engineState.instrtxtp[0];
INSDS *ip;
instance(csound, 0); /* allocate instr 0 */
csound->curip = ip = tp->act_instance;
tp->act_instance = ip->nxtact;
csound->ids = (OPDS*) ip;
tp->active++;
ip->actflg++;
ip->ksmps = csound->ksmps;
ip->ekr = csound->ekr;
ip->kcounter = csound->kcounter;
ip->onedksmps = csound->onedksmps;
ip->onedkr = csound->onedkr;
ip->kicvt = csound->kicvt;
csound->inerrcnt = 0;
csound->mode = 1;
while ((csound->ids = csound->ids->nxti) != NULL) {
csound->op = csound->ids->optext->t.oentry->opname;
(*csound->ids->iopadr)(csound, csound->ids); /* run all i-code */
}
csound->mode = 0;
return csound->inerrcnt; /* return errcnt */
}
static void putop(CSOUND *csound, TEXT *tp)
{
int n, nn;
if ((n = tp->outlist->count) != 0) {
nn = 0;
while (n--)
csound->Message(csound, "%s\t", tp->outlist->arg[nn++]);
}
else
csound->Message(csound, "\t");
csound->Message(csound, "%s\t", tp->opcod);
if ((n = tp->inlist->count) != 0) {
nn = 0;
while (n--)
csound->Message(csound, "%s\t", tp->inlist->arg[nn++]);
}
csound->Message(csound, "\n");
}
static void set_xtratim(CSOUND *csound, INSDS *ip)
{
if (UNLIKELY(ip->relesing))
return;
ip->offtim = (csound->icurTime +
ip->ksmps * (double) ip->xtratim)/csound->esr;
ip->offbet = csound->curBeat + (csound->curBeat_inc * (double) ip->xtratim);
ip->relesing = 1;
csound->engineState.instrtxtp[ip->insno]->pending_release++;
}
/* insert an instr copy into active list */
/* then run an init pass */
int insert(CSOUND *csound, int insno, EVTBLK *newevtp) {
if(csound->oparms->realtime) {
unsigned long wp = csound->alloc_queue_wp;
csound->alloc_queue[wp].insno = insno;
csound->alloc_queue[wp].blk = *newevtp;
csound->alloc_queue[wp].type = 0;
csound->alloc_queue_wp = wp + 1 < MAX_ALLOC_QUEUE ? wp + 1 : 0;
ATOMIC_INCR(csound->alloc_queue_items);
return 0;
}
else return insert_event(csound, insno, newevtp);
}
int insert_event(CSOUND *csound, int insno, EVTBLK *newevtp)
{
INSTRTXT *tp;
INSDS *ip, *prvp, *nxtp;
OPARMS *O = csound->oparms;
CS_VAR_MEM *pfields = NULL; /* *** was uninitialised *** */
int tie=0, i;
int n, error = 0;
MYFLT *flp, *fep;
if (UNLIKELY(csound->advanceCnt))
return 0;
if (UNLIKELY(O->odebug)) {
char *name = csound->engineState.instrtxtp[insno]->insname;
if (UNLIKELY(name))
csound->Message(csound, Str("activating instr %s at %"PRIi64"\n"),
name, csound->icurTime);
else
csound->Message(csound, Str("activating instr %d at %"PRIi64"\n"),
insno, csound->icurTime);
}
csound->inerrcnt = 0;
tp = csound->engineState.instrtxtp[insno];
if (UNLIKELY(tp->muted == 0)) {
char *name = csound->engineState.instrtxtp[insno]->insname;
if (UNLIKELY(name))
csound->Warning(csound, Str("Instrument %s muted\n"), name);
else
csound->Warning(csound, Str("Instrument %d muted\n"), insno);
return 0;
}
if (tp->cpuload > FL(0.0)) {
csound->cpu_power_busy += tp->cpuload;
/* if there is no more cpu processing time*/
if (UNLIKELY(csound->cpu_power_busy > FL(100.0))) {
csound->cpu_power_busy -= tp->cpuload;
csoundWarning(csound, Str("cannot allocate last note because "
"it exceeds 100%% of cpu time"));
return(0);
}
}
if (UNLIKELY(tp->maxalloc > 0 && tp->active >= tp->maxalloc)) {
csoundWarning(csound, Str("cannot allocate last note because it exceeds "
"instr maxalloc"));
return(0);
}
/* If named ensure we have the fraction */
if (csound->engineState.instrtxtp[insno]->insname && newevtp->strarg)
newevtp->p[1] = named_instr_find(csound, newevtp->strarg);
/* if find this insno, active, with indef (tie) & matching p1 */
for (ip = tp->instance; ip != NULL; ip = ip->nxtinstance) {
if (ip->actflg && ip->offtim < 0.0 && ip->p1.value == newevtp->p[1]) {
csound->tieflag++;
ip->tieflag = 1;
tie = 1;
/* goto init; */ /* continue that event */
break;
}
}
if(!tie) {
/* alloc new dspace if needed */
if (tp->act_instance == NULL || tp->isNew) {
if (UNLIKELY(O->msglevel & CS_RNGEMSG)) {
char *name = csound->engineState.instrtxtp[insno]->insname;
if (UNLIKELY(name))
csound->ErrorMsg(csound, Str("new alloc for instr %s:\n"), name);
else
csound->ErrorMsg(csound, Str("new alloc for instr %d:\n"), insno);
}
instance(csound, insno);
tp->isNew=0;
}
/* pop from free instance chain */
csoundDebugMsg(csound, "insert(): tp->act_instance = %p\n", tp->act_instance);
ip = tp->act_instance;
ATOMIC_SET(ip->init_done, 0);
tp->act_instance = ip->nxtact;
ip->insno = (int16) insno;
ip->ksmps = csound->ksmps;
ip->ekr = csound->ekr;
ip->kcounter = csound->kcounter;
ip->onedksmps = csound->onedksmps;
ip->onedkr = csound->onedkr;
ip->kicvt = csound->kicvt;
ip->pds = NULL;
/* Add an active instrument */
tp->active++;
tp->instcnt++;
csound->dag_changed++; /* Need to remake DAG */
nxtp = &(csound->actanchor); /* now splice into activ lst */
while ((prvp = nxtp) && (nxtp = prvp->nxtact) != NULL) {
if (nxtp->insno > insno ||
(nxtp->insno == insno && nxtp->p1.value > newevtp->p[1])) {
nxtp->prvact = ip;
break;
}
}
ip->nxtact = nxtp;
ip->prvact = prvp;
prvp->nxtact = ip;
ip->tieflag = 0;
ip->actflg++; /* and mark the instr active */
}
/* init: */
pfields = (CS_VAR_MEM*)&ip->p0;
if (tp->psetdata) {
int i;
CS_VAR_MEM* pfields = (CS_VAR_MEM*) &ip->p0;
MYFLT *pdat = tp->psetdata + 2;
int32 nn = tp->pmax - 2; /* put cur vals in pflds */
for (i = 0; i < nn; i++) {
CS_VAR_MEM* pfield = (pfields + i + 3);
pfield->value = *(pdat + i);
}
}
n = tp->pmax;
if (UNLIKELY((tp->nocheckpcnt == 0) &&
n != newevtp->pcnt &&
!tp->psetdata)) {
char *name = csound->engineState.instrtxtp[insno]->insname;
if (UNLIKELY(name))
csoundWarning(csound, Str("instr %s uses %d p-fields but is given %d"),
name, n, newevtp->pcnt);
else
csoundWarning(csound, Str("instr %d uses %d p-fields but is given %d"),
insno, n, newevtp->pcnt);
}
if (newevtp->p3orig >= FL(0.0))
ip->offbet = csound->beatOffs
+ (double) newevtp->p2orig + (double) newevtp->p3orig;
else
ip->offbet = -1.0;
flp = &ip->p1.value;
fep = &newevtp->p[0];
if (UNLIKELY(O->odebug))
csound->Message(csound, "psave beg at %p\n", (void*) flp);
if (n > newevtp->pcnt) n = newevtp->pcnt; /* IV - Oct 20 2002 */
for (i = 1; i < n + 1; i++) {
CS_VAR_MEM* pfield = pfields + i;
pfield->varType = (CS_TYPE*)&CS_VAR_TYPE_P;
pfield->value = fep[i];
}
if (n < tp->pmax && tp->psetdata==NULL) {
for (i = 0; i < tp->pmax - n; i++) {
CS_VAR_MEM* pfield = pfields + i + n + 1;
pfield->varType = (CS_TYPE*)&CS_VAR_TYPE_P;
pfield->value = 0;
}
}
if (UNLIKELY(O->odebug))
csound->Message(csound, " ending at %p\n", (void*) flp);
if (O->Beatmode)
ip->p2.value = (MYFLT) (csound->icurTime/csound->esr - csound->timeOffs);
ip->offtim = (double) ip->p3.value; /* & duplicate p3 for now */
ip->m_chnbp = (MCHNBLK*) NULL;
ip->xtratim = 0;
ip->relesing = 0;
ip->m_sust = 0;
ip->nxtolap = NULL;
ip->opcod_iobufs = NULL;
ip->strarg = newevtp->strarg; /* copy strarg so it does not get lost */
// current event needs to be reset here
csound->init_event = newevtp;
error = init_pass(csound, ip);
if(error == 0)
ATOMIC_SET(ip->init_done, 1);
if (UNLIKELY(csound->inerrcnt || ip->p3.value == FL(0.0))) {
xturnoff_now(csound, ip);
return csound->inerrcnt;
}
/* new code for sample-accurate timing, not for tied notes */
if (O->sampleAccurate && !tie) {
int64_t start_time_samps, start_time_kcycles;
double duration_samps;
start_time_samps = (int64_t) (ip->p2.value * csound->esr);
duration_samps = ip->p3.value * csound->esr;
start_time_kcycles = start_time_samps/csound->ksmps;
ip->ksmps_offset = start_time_samps - start_time_kcycles*csound->ksmps;
/* with no p3 or xtratim values, can't set the sample accur duration */
if (ip->p3.value > 0 && ip->xtratim == 0 ){
int tmp = ((int)duration_samps+ip->ksmps_offset)%csound->ksmps;
if (tmp != 0)ip->no_end = csound->ksmps - tmp; else ip->no_end = 0;
//ip->no_end = (csound->ksmps -
// ((int)duration_samps+ip->ksmps_offset)%csound->ksmps)%csound->ksmps;
/* the ksmps_no_end field is initially 0, set to no_end in the last
perf cycle */
// printf("*** duration_samps %d ip->ksmps_offset %d csound->ksmps %d ==> %d\n",
// (int)duration_samps, ip->ksmps_offset, csound->ksmps, ip->no_end);
}
else ip->no_end = 0;
ip->ksmps_no_end = 0;
}
else {
/* ksmps_offset = */
ip->ksmps_offset = 0;
ip->ksmps_no_end = 0;
ip->no_end = 0;
}
#ifdef BETA
if (UNLIKELY(O->odebug))
csound->Message(csound, "In insert: %d %lf %lf\n",
__LINE__, ip->p3.value, ip->offtim); /* *********** */
#endif
if (ip->p3.value > FL(0.0) && ip->offtim > 0.0) { /* if still finite time, */
double p2 = (double) ip->p2.value + csound->timeOffs;
ip->offtim = p2 + (double) ip->p3.value;
if (O->sampleAccurate && !tie &&
ip->p3.value > 0 &&
ip->xtratim == 0) /* ceil for sample-accurate ending */
ip->offtim = CEIL(ip->offtim*csound->ekr) / csound->ekr;
else /* normal : round */
ip->offtim = FLOOR(ip->offtim * csound->ekr +0.5)/csound->ekr;
if (O->Beatmode) {
p2 = ((p2*csound->esr - csound->icurTime) / csound->ibeatTime)
+ csound->curBeat;
ip->offbet = p2 + ((double) ip->p3.value*csound->esr / csound->ibeatTime);
}
#ifdef BETA
if (UNLIKELY(O->odebug))
csound->Message(csound,
"Calling schedofftim line %d; offtime= %lf (%lf)\n",
__LINE__, ip->offtim, ip->offtim*csound->ekr);
#endif
if(csound->oparms->realtime) // compensate for possible late starts
{
double p2 = (double) ip->p2.value + csound->timeOffs;
ip->offtim += (csound->icurTime/csound->esr - p2);
}
//printf("%lf\n", );
schedofftim(csound, ip); /* put in turnoff list */
}
else {
ip->offbet = -1.0;
ip->offtim = -1.0; /* else mark indef */
}
if (UNLIKELY(O->odebug)) {
char *name = csound->engineState.instrtxtp[insno]->insname;
if (UNLIKELY(name))
csound->Message(csound, Str("instr %s now active:\n"), name);
else
csound->Message(csound, Str("instr %d now active:\n"), insno);
showallocs(csound);
}
if (newevtp->pinstance != NULL) {
*((MYFLT *)newevtp->pinstance) = (MYFLT) ((uintptr_t) ip);
}
return 0;
}
/* insert a MIDI instr copy into active list */
/* then run an init pass */
int MIDIinsert(CSOUND *csound, int insno, MCHNBLK *chn, MEVENT *mep) {
if(csound->oparms->realtime) {
unsigned long wp = csound->alloc_queue_wp;
csound->alloc_queue[wp].insno = insno;
csound->alloc_queue[wp].chn = chn;
csound->alloc_queue[wp].mep = *mep;
csound->alloc_queue[wp].type = 1;
csound->alloc_queue_wp = wp + 1 < MAX_ALLOC_QUEUE ? wp + 1 : 0;
ATOMIC_INCR(csound->alloc_queue_items);
return 0;
}
else return insert_midi(csound, insno, chn, mep);
}
int insert_midi(CSOUND *csound, int insno, MCHNBLK *chn, MEVENT *mep)
{
INSTRTXT *tp;
INSDS *ip, **ipp, *prvp, *nxtp;
OPARMS *O = csound->oparms;
CS_VAR_MEM *pfields;
EVTBLK *evt;
int pmax = 0, error = 0;
if (UNLIKELY(csound->advanceCnt))
return 0;
if (UNLIKELY(insno <= 0 || csound->engineState.instrtxtp[insno]->muted == 0))
return 0; /* muted */
tp = csound->engineState.instrtxtp[insno];
if (tp->cpuload > FL(0.0)) {
csound->cpu_power_busy += tp->cpuload;
if (UNLIKELY(csound->cpu_power_busy > FL(100.0))) {
/* if there is no more cpu time */
csound->cpu_power_busy -= tp->cpuload;
csoundWarning(csound, Str("cannot allocate last note because "
"it exceeds 100%% of cpu time"));
return(0);
}
}
if (UNLIKELY(tp->maxalloc > 0 && tp->active >= tp->maxalloc)) {
csoundWarning(csound, Str("cannot allocate last note because it exceeds "
"instr maxalloc"));
return(0);
}
tp->active++;
tp->instcnt++;
csound->dag_changed++; /* Need to remake DAG */
if (UNLIKELY(O->odebug)) {
char *name = csound->engineState.instrtxtp[insno]->insname;
if (UNLIKELY(name))
csound->Message(csound, Str("MIDI activating instr %s\n"), name);
else
csound->Message(csound, Str("MIDI activating instr %d\n"), insno);
}
csound->inerrcnt = 0;
ipp = &chn->kinsptr[mep->dat1]; /* key insptr ptr */
/* alloc new dspace if needed */
if (tp->act_instance == NULL || tp->isNew) {
if (UNLIKELY(O->msglevel & CS_RNGEMSG)) {
char *name = csound->engineState.instrtxtp[insno]->insname;
if (UNLIKELY(name))
csound->Message(csound, Str("new MIDI alloc for instr %s:\n"), name);
else
csound->Message(csound, Str("new MIDI alloc for instr %d:\n"), insno);
}
instance(csound, insno);
tp->isNew = 0;
}
/* pop from free instance chain */
ip = tp->act_instance;
ATOMIC_SET(ip->init_done, 0);
tp->act_instance = ip->nxtact;
ip->insno = (int16) insno;
if (UNLIKELY(O->odebug))
csound->Message(csound, "Now %d active instr %d\n", tp->active, insno);
if (UNLIKELY((prvp = *ipp) != NULL)) { /* if key currently activ */
csoundWarning(csound,
Str("MIDI note overlaps with key %d on same channel"),
(int) mep->dat1);
while (prvp->nxtolap != NULL) /* append to overlap list */
prvp = prvp->nxtolap;
prvp->nxtolap = ip;
}
else
*ipp = ip;
/* of overlapping notes, the one that was turned on first will be */
/* turned off first as well */
ip->nxtolap = NULL;
nxtp = &(csound->actanchor); /* now splice into activ lst */
while ((prvp = nxtp) && (nxtp = prvp->nxtact) != NULL) {
if (nxtp->insno > insno) {
nxtp->prvact = ip;
break;
}
}
ip->nxtact = nxtp;
ip->prvact = prvp;
prvp->nxtact = ip;
ip->actflg++; /* and mark the instr active */
ip->m_chnbp = chn; /* rec address of chnl ctrl blk */
ip->m_pitch = (unsigned char) mep->dat1; /* rec MIDI data */
ip->m_veloc = (unsigned char) mep->dat2;
ip->xtratim = 0;
ip->m_sust = 0;
ip->relesing = 0;
ip->offbet = -1.0;
ip->offtim = -1.0; /* set indef duration */
ip->opcod_iobufs = NULL; /* IV - Sep 8 2002: */
ip->p1.value = (MYFLT) insno; /* set these required p-fields */
ip->p2.value = (MYFLT) (csound->icurTime/csound->esr - csound->timeOffs);
ip->p3.value = FL(-1.0);
ip->ksmps = csound->ksmps;
ip->ekr = csound->ekr;
ip->kcounter = csound->kcounter;
ip->onedksmps = csound->onedksmps;
ip->onedkr = csound->onedkr;
ip->kicvt = csound->kicvt;
ip->pds = NULL;
pfields = (CS_VAR_MEM*)&ip->p0;
if (tp->psetdata != NULL) {
int i;
MYFLT *pdat = tp->psetdata + 2;
int32 nn = tp->pmax - 2; /* put cur vals in pflds */
for (i = 0; i < nn; i++) {
CS_VAR_MEM* pfield = (pfields + i + 3);
pfield->value = *(pdat + i);
}
pmax = tp->pmax;
}
/* MIDI channel message note on routing overrides pset: */
if (O->midiKey) {
int pfield_index = O->midiKey;
CS_VAR_MEM* pfield = (pfields + pfield_index);
MYFLT value = (MYFLT) ip->m_pitch;
pfield->value = value;
if (UNLIKELY(O->msglevel & CS_WARNMSG)) {
csound->Message(csound, " midiKey: pfield: %3d value: %3d\n",
pfield_index, (int) pfield->value);
}
if (pmax < pfield_index) pmax = pfield_index;
}
else if (O->midiKeyCps) {
int pfield_index = O->midiKeyCps;
CS_VAR_MEM* pfield = (pfields + pfield_index);
MYFLT value = (MYFLT) ip->m_pitch;
value = value / FL(12.0) + FL(3.0);
value = value * OCTRES;
value = (MYFLT) CPSOCTL((int32) value);
pfield->value = value;
if (UNLIKELY(O->msglevel & CS_WARNMSG)) {
csound->Message(csound, " midiKeyCps: pfield: %3d value: %3d\n",
pfield_index, (int) pfield->value);
}
if (pmax < pfield_index) pmax = pfield_index;
}
else if (O->midiKeyOct) {
int pfield_index = O->midiKeyOct;
CS_VAR_MEM* pfield = (pfields + pfield_index);
MYFLT value = (MYFLT) ip->m_pitch;
value = value / FL(12.0) + FL(3.0);
pfield->value = value;
if (UNLIKELY(O->msglevel & CS_WARNMSG)) {
csound->Message(csound, " midiKeyOct: pfield: %3d value: %3d\n",
pfield_index, (int) pfield->value);
}
if (pmax < pfield_index) pmax = pfield_index;
}
else if (O->midiKeyPch) {
int pfield_index = O->midiKeyPch;
CS_VAR_MEM* pfield = (pfields + pfield_index);
MYFLT value = (MYFLT) ip->m_pitch;
double octave = 0;
double fraction = 0.0;
value = value / FL(12.0) + FL(3.0);
fraction = modf(value, &octave);
fraction *= 0.12;
value = octave + fraction;
pfield->value = value;
if (UNLIKELY(O->msglevel & CS_WARNMSG)) {
csound->Message(csound, " midiKeyPch: pfield: %3d value: %3d\n",
pfield_index, (int) pfield->value);
}
if (pmax < pfield_index) pmax = pfield_index;
}
if (O->midiVelocity) {
int pfield_index = O->midiVelocity;
CS_VAR_MEM* pfield = (pfields + pfield_index);
MYFLT value = (MYFLT) ip->m_veloc;
pfield->value = value;
if (UNLIKELY(O->msglevel & CS_WARNMSG)) {
csound->Message(csound, " midiVelocity: pfield: %3d value: %3d\n",
pfield_index, (int) pfield->value);
}
if (pmax < pfield_index) pmax = pfield_index;
}
else if (O->midiVelocityAmp) {
int pfield_index = O->midiVelocityAmp;
CS_VAR_MEM* pfield = (pfields + pfield_index);
MYFLT value = (MYFLT) ip->m_veloc;
value = value * value / FL(16239.0);
value = value * csound->e0dbfs;
pfield->value = value;
if (UNLIKELY(O->msglevel & CS_WARNMSG)) {
csound->Message(csound, " midiVelocityAmp: pfield: %3d value: %.3f\n",
pfield_index, pfield->value);
}
if (pmax < pfield_index) pmax = pfield_index;
}
if (pmax > 0) {
int i;
if (csound->currevent == NULL) {
evt = (EVTBLK *) csound->Calloc(csound, sizeof(EVTBLK));
csound->currevent = evt;
}
else evt = csound->currevent;
evt->pcnt = pmax+1;
for (i =0; i < evt->pcnt; i++) {
evt->p[i] = pfields[i].value;
}
}
csound->init_event = csound->currevent;
error = init_pass(csound, ip);
if(error == 0)
ATOMIC_SET(ip->init_done, 1);
if (UNLIKELY(csound->inerrcnt)) {
xturnoff_now(csound, ip);
return csound->inerrcnt;
}
ip->tieflag = ip->reinitflag = 0;
csound->tieflag = csound->reinitflag = 0;
if (UNLIKELY(O->odebug)) {
char *name = csound->engineState.instrtxtp[insno]->insname;
if (UNLIKELY(name))
csound->Message(csound, Str("instr %s now active:\n"), name);
else
csound->Message(csound, Str("instr %d now active:\n"), insno);
showallocs(csound);
}
return 0;
}
static void showallocs(CSOUND *csound) /* debugging aid */
{
INSTRTXT *txtp;
INSDS *p;
csound->Message(csound, "insno\tinstanc\tnxtinst\tprvinst\tnxtact\t"
"prvact\tnxtoff\tactflg\tofftim\n");
for (txtp = &(csound->engineState.instxtanchor);
txtp != NULL;
txtp = txtp->nxtinstxt)
if ((p = txtp->instance) != NULL) {
/*
* On Alpha, we print pointers as pointers. heh 981101
* and now on all platforms (JPff)
*/
do {
csound->Message(csound, "%d\t%p\t%p\t%p\t%p\t%p\t%p\t%d\t%3.1f\n",
(int) p->insno, (void*) p,
(void*) p->nxtinstance, (void*) p->prvinstance,
(void*) p->nxtact, (void*) p->prvact,
(void*) p->nxtoff, p->actflg, p->offtim);
} while ((p = p->nxtinstance) != NULL);
}
}
static void schedofftim(CSOUND *csound, INSDS *ip)
{ /* put an active instr into offtime list */
INSDS *prvp, *nxtp; /* called by insert() & midioff + xtratim */
if ((nxtp = csound->frstoff) == NULL ||
nxtp->offtim > ip->offtim) { /* set into */
csound->frstoff = ip; /* firstoff chain */
ip->nxtoff = nxtp;
/* IV - Feb 24 2006: check if this note already needs to be turned off */
/* the following comparisons must match those in sensevents() */
#ifdef BETA
if (UNLIKELY(csound->oparms->odebug))
csound->Message(csound,"schedofftim: %lf %lf %f\n",
ip->offtim, csound->icurTime/csound->esr,
csound->curTime_inc);
#endif
if (csound->oparms_.Beatmode) {
double tval = csound->curBeat + (0.505 * csound->curBeat_inc);
if (ip->offbet <= tval) beatexpire(csound, tval);
}
else {
double tval = (csound->icurTime + (0.505 * csound->ksmps))/csound->esr;
if (ip->offtim <= tval) timexpire(csound, tval);
}
#ifdef BETA
if (UNLIKELY(csound->oparms->odebug))
csound->Message(csound,"schedofftim: %lf %lf %lf\n", ip->offtim,
(csound->icurTime + (0.505 * csound->ksmps))/csound->esr,
csound->ekr*((csound->icurTime +
(0.505 * csound->ksmps))/csound->esr));
#endif
}
else {
while ((prvp = nxtp)
&& (nxtp = nxtp->nxtoff) != NULL
&& ip->offtim >= nxtp->offtim);
prvp->nxtoff = ip;
ip->nxtoff = nxtp;
}
}
/* csound.c */
extern int csoundDeinitialiseOpcodes(CSOUND *csound, INSDS *ip);
int useropcd(CSOUND *, UOPCODE*);
static void deact(CSOUND *csound, INSDS *ip)
{ /* unlink single instr from activ chain */
INSDS *nxtp; /* and mark it inactive */
/* close any files in fd chain */
if (ip->nxtd != NULL)
csoundDeinitialiseOpcodes(csound, ip);
/* remove an active instrument */
csound->engineState.instrtxtp[ip->insno]->active--;
if (ip->xtratim > 0)
csound->engineState.instrtxtp[ip->insno]->pending_release--;
csound->cpu_power_busy -= csound->engineState.instrtxtp[ip->insno]->cpuload;
/* IV - Sep 8 2002: free subinstr instances */
/* that would otherwise result in a memory leak */
if (ip->opcod_deact) {
UOPCODE *p = (UOPCODE*) ip->opcod_deact; /* IV - Oct 26 2002 */
deact(csound, p->ip); /* deactivate */
p->ip = NULL;
/* IV - Oct 26 2002: set perf routine to "not initialised" */
p->h.opadr = (SUBR) useropcd;
ip->opcod_deact = NULL;
}
if (ip->subins_deact) {
deact(csound, ((SUBINST*) ip->subins_deact)->ip); /* IV - Oct 24 2002 */
((SUBINST*) ip->subins_deact)->ip = NULL;
ip->subins_deact = NULL;
}
if (UNLIKELY(csound->oparms->odebug)) {
char *name = csound->engineState.instrtxtp[ip->insno]->insname;
if (UNLIKELY(name))
csound->Message(csound, Str("removed instance of instr %s\n"), name);
else
csound->Message(csound, Str("removed instance of instr %d\n"), ip->insno);
}
/* IV - Oct 24 2002: ip->prvact may be NULL, so need to check */
if (ip->prvact && (nxtp = ip->prvact->nxtact = ip->nxtact) != NULL) {
nxtp->prvact = ip->prvact;
}
ip->actflg = 0;
/* link into free instance chain */
/* This also destroys ip->nxtact causing loops */
if (csound->engineState.instrtxtp[ip->insno] == ip->instr){
ip->nxtact = csound->engineState.instrtxtp[ip->insno]->act_instance;
csound->engineState.instrtxtp[ip->insno]->act_instance = ip;
}
if (ip->fdchp != NULL)
fdchclose(csound, ip);
csound->dag_changed++;
}
int kill_instance(CSOUND *csound, KILLOP *p) {
if (LIKELY(*p->inst)) xturnoff(csound, (INSDS *) ((uintptr_t)*p->inst));
else csound->Warning(csound, Str("instance not valid\n"));
return OK;
}
/* Turn off a particular insalloc, also remove from list of active */
/* MIDI notes. Allows for releasing if ip->xtratim > 0. */
void xturnoff(CSOUND *csound, INSDS *ip) /* turnoff a particular insalloc */
{ /* called by inexclus on ctrl 111 */
MCHNBLK *chn;
if (UNLIKELY(ip->relesing))
return; /* already releasing: nothing to do */
chn = ip->m_chnbp;
if (chn != NULL) { /* if this was a MIDI note */
INSDS *prvip;
prvip = chn->kinsptr[ip->m_pitch]; /* remov from activ lst */
if (ip->m_sust && chn->ksuscnt)
chn->ksuscnt--;
ip->m_sust = 0; /* force turnoff even if sustaining */
if (prvip != NULL) {
if (prvip == ip)
chn->kinsptr[ip->m_pitch] = ip->nxtolap;
else {
while (prvip != NULL && prvip->nxtolap != ip)
prvip = prvip->nxtolap;
if (prvip != NULL)
prvip->nxtolap = ip->nxtolap;
}
}
}
/* remove from schedoff chain first if finite duration */
if (csound->frstoff != NULL && ip->offtim >= 0.0) {
INSDS *prvip;
prvip = csound->frstoff;
if (prvip == ip)
csound->frstoff = ip->nxtoff;
else {
while (prvip != NULL && prvip->nxtoff != ip)
prvip = prvip->nxtoff;
if (prvip != NULL)
prvip->nxtoff = ip->nxtoff;
}
}
/* if extra time needed: schedoff at new time */
if (ip->xtratim > 0) {
set_xtratim(csound, ip);
#ifdef BETA
if (UNLIKELY(csound->oparms->odebug))
csound->Message(csound, "Calling schedofftim line %d\n", __LINE__);
#endif
schedofftim(csound, ip);
}
else {
/* no extra time needed: deactivate immediately */
deact(csound, ip);
csound->dag_changed++; /* Need to remake DAG */
}
}
/* Turn off instrument instance immediately, without releasing. */
/* Removes alloc from list of active MIDI notes. */
void xturnoff_now(CSOUND *csound, INSDS *ip)
{
ip->xtratim = 0;
ip->relesing = 0;
xturnoff(csound, ip);
}
extern void free_instrtxt(CSOUND *csound, INSTRTXT *instrtxt);
void free_instr_var_memory(CSOUND* csound, INSDS* ip) {
INSTRTXT* instrDef = ip->instr;
CS_VAR_POOL* pool = instrDef->varPool;
CS_VARIABLE* current = pool->head;
while (current != NULL) {
CS_TYPE* varType = current->varType;
if (varType->freeVariableMemory != NULL) {
varType->freeVariableMemory(csound,
ip->lclbas + current->memBlockIndex);
}
current = current->next;
}
}
void orcompact(CSOUND *csound) /* free all inactive instr spaces */
{
INSTRTXT *txtp;
INSDS *ip, *nxtip, *prvip, **prvnxtloc;
int cnt = 0;
for (txtp = &(csound->engineState.instxtanchor);
txtp != NULL; txtp = txtp->nxtinstxt) {
if ((ip = txtp->instance) != NULL) { /* if instance exists */
prvip = NULL;
prvnxtloc = &txtp->instance;
do {
if (!ip->actflg) {
cnt++;
if (ip->opcod_iobufs && ip->insno > csound->engineState.maxinsno)
csound->Free(csound, ip->opcod_iobufs); /* IV - Nov 10 2002 */
if (ip->fdchp != NULL)
fdchclose(csound, ip);
if (ip->auxchp != NULL)
auxchfree(csound, ip);
free_instr_var_memory(csound, ip);
if ((nxtip = ip->nxtinstance) != NULL)
nxtip->prvinstance = prvip;
*prvnxtloc = nxtip;
csound->Free(csound, (char *)ip);
}
else {
prvip = ip;
prvnxtloc = &ip->nxtinstance;
}
}
while ((ip = *prvnxtloc) != NULL);
}
/* IV - Oct 31 2002 */
if (!txtp->instance)
txtp->lst_instance = NULL; /* find last alloc */
else {
ip = txtp->instance;
while (ip->nxtinstance) ip = ip->nxtinstance;
txtp->lst_instance = ip;
}
txtp->act_instance = NULL; /* no free instances */
}
/* check current items in deadpool to see if they need deleting */
{
int i;
for (i=0; i < csound->dead_instr_no; i++) {
if (csound->dead_instr_pool[i] != NULL) {
INSDS *active = csound->dead_instr_pool[i]->instance;
while (active != NULL) {
if (active->actflg) {
// add_to_deadpool(csound,csound->dead_instr_pool[i]);
break;
}
active = active->nxtinstance;
}
/* no active instances */
if (active == NULL) {
free_instrtxt(csound, csound->dead_instr_pool[i]);
csound->dead_instr_pool[i] = NULL;
}
}
}
}
if (UNLIKELY(cnt)) {
if(csound->oparms->msglevel ||csound->oparms->odebug)
csound->Message(csound, Str("inactive allocs returned to freespace\n"));
}
}
void infoff(CSOUND *csound, MYFLT p1) /* turn off an indef copy of instr p1 */
{ /* called by musmon */
INSDS *ip;
int insno;
insno = (int) p1;
if (LIKELY((ip = (csound->engineState.instrtxtp[insno])->instance) != NULL)) {
do {
if (ip->insno == insno /* if find the insno */
&& ip->actflg /* active */
&& ip->offtim < 0.0 /* but indef, VL: currently this condition
cannot be removed, as it breaks turning
off extratime instances */
&& ip->p1.value == p1) {
if (UNLIKELY(csound->oparms->odebug))
csound->Message(csound, "turning off inf copy of instr %d\n",
insno);
xturnoff(csound, ip);
return; /* turn it off */
}
} while ((ip = ip->nxtinstance) != NULL);
}
csound->Message(csound,
Str("could not find playing instr %f\n"),
p1);
}
void do_baktrace(CSOUND *, uint64_t);
int csoundInitError(CSOUND *csound, const char *s, ...)
{
va_list args;
INSDS *ip;
char buf[512];
/* RWD: need this! */
if (UNLIKELY(csound->ids == NULL)) {
va_start(args, s);
csoundErrMsgV(csound, Str("\nINIT ERROR: "), s, args);
va_end(args);
csound->LongJmp(csound, 1);
}
if (csound->mode != 1)
csoundErrorMsg(csound, Str("InitError in wrong mode %d\n"), csound->mode);
/* IV - Oct 16 2002: check for subinstr and user opcode */
ip = csound->ids->insdshead;
if (ip->opcod_iobufs) {
OPCODINFO *op = ((OPCOD_IOBUFS*) ip->opcod_iobufs)->opcode_info;
/* find top level instrument instance */
do {
ip = ((OPCOD_IOBUFS*) ip->opcod_iobufs)->parent_ip;
} while (ip->opcod_iobufs);
if (op)
snprintf(buf, 512, Str("INIT ERROR in instr %d (opcode %s) line %d: "),
ip->insno, op->name, csound->ids->optext->t.linenum);
else
snprintf(buf, 512, Str("INIT ERROR in instr %d (subinstr %d) line %d: "),
ip->insno, csound->ids->insdshead->insno,
csound->ids->optext->t.linenum);
}
else
snprintf(buf, 512, Str("INIT ERROR in instr %d (opcode %s) line %d: "),
ip->insno, csound->op, csound->ids->optext->t.linenum);
va_start(args, s);
csoundErrMsgV(csound, buf, s, args);
va_end(args);
do_baktrace(csound, csound->ids->optext->t.locn);
putop(csound, &(csound->ids->optext->t));
return ++(csound->inerrcnt);
}
int csoundPerfError(CSOUND *csound, OPDS *h, const char *s, ...)
{
va_list args;
char buf[512];
INSDS *ip = h->insdshead;
TEXT t = h->optext->t;
if (csound->mode != 2)
csoundErrorMsg(csound, Str("PerfError in wrong mode %d\n"), csound->mode);
if (ip->opcod_iobufs) {
OPCODINFO *op = ((OPCOD_IOBUFS*) ip->opcod_iobufs)->opcode_info;
/* find top level instrument instance */
do {
ip = ((OPCOD_IOBUFS*) ip->opcod_iobufs)->parent_ip;
} while (ip->opcod_iobufs);
if (op)
snprintf(buf, 512, Str("PERF ERROR in instr %d (opcode %s) line %d: "),
ip->insno, op->name, t.linenum);
else
snprintf(buf, 512, Str("PERF ERROR in instr %d (subinstr %d) line %d: "),
ip->insno, ip->insno, t.linenum);
}
else
snprintf(buf, 512, Str("PERF ERROR in instr %d (opcode %s) line %d: "),
ip->insno, csound->op, t.linenum);
va_start(args, s);
csoundErrMsgV(csound, buf, s, args);
va_end(args);
do_baktrace(csound, t.locn);
if (ip->pds)
putop(csound, &(ip->pds->optext->t));
csoundErrorMsg(csound, "%s", Str(" note aborted\n"));
csound->perferrcnt++;
xturnoff_now((CSOUND*) csound, ip); /* rm ins fr actlist */
return csound->perferrcnt; /* contin from there */
}
int subinstrset_(CSOUND *csound, SUBINST *p, int instno)
{
OPDS *saved_ids = csound->ids;
INSDS *saved_curip = csound->curip;
CS_VAR_MEM *pfield;
int n, init_op, inarg_ofs;
INSDS *pip = p->h.insdshead;
init_op = (p->h.opadr == NULL ? 1 : 0);
inarg_ofs = (init_op ? 0 : SUBINSTNUMOUTS);
if (UNLIKELY(instno < 0)) return NOTOK;
/* IV - Oct 9 2002: need this check */
if (UNLIKELY(!init_op && p->OUTOCOUNT > csound->nchnls)) {
return csoundInitError(csound, Str("subinstr: number of output "
"args greater than nchnls"));
}
/* IV - Oct 9 2002: copied this code from useropcdset() to fix some bugs */
if (!(pip->reinitflag | pip->tieflag) || p->ip == NULL) {
/* get instance */
if (csound->engineState.instrtxtp[instno]->act_instance == NULL)
instance(csound, instno);
p->ip = csound->engineState.instrtxtp[instno]->act_instance;
csound->engineState.instrtxtp[instno]->act_instance = p->ip->nxtact;
p->ip->insno = (int16) instno;
p->ip->actflg++; /* and mark the instr active */
csound->engineState.instrtxtp[instno]->active++;
csound->engineState.instrtxtp[instno]->instcnt++;
p->ip->p1.value = (MYFLT) instno;
/* VL 21-10-16: iobufs are not used here and
are causing trouble elsewhere. Commenting
it out */
/* p->ip->opcod_iobufs = (void*) &p->buf; */
/* link into deact chain */
p->ip->subins_deact = saved_curip->subins_deact;
p->ip->opcod_deact = NULL;
saved_curip->subins_deact = (void*) p;
p->parent_ip = p->buf.parent_ip = saved_curip;
}
p->ip->ksmps = CS_KSMPS;
p->ip->kcounter = CS_KCNT;
p->ip->ekr = CS_EKR;
p->ip->onedkr = CS_ONEDKR;
p->ip->onedksmps = CS_ONEDKSMPS;
p->ip->kicvt = CS_KICVT;
/* copy parameters from this instrument into our subinstrument */
p->ip->xtratim = saved_curip->xtratim;
p->ip->m_sust = 0;
p->ip->relesing = saved_curip->relesing;
p->ip->offbet = saved_curip->offbet;
p->ip->offtim = saved_curip->offtim;
p->ip->nxtolap = NULL;
p->ip->p2 = saved_curip->p2;
p->ip->p3 = saved_curip->p3;
p->ip->ksmps = CS_KSMPS;
/* IV - Oct 31 2002 */
p->ip->m_chnbp = saved_curip->m_chnbp;
p->ip->m_pitch = saved_curip->m_pitch;
p->ip->m_veloc = saved_curip->m_veloc;
p->ip->ksmps_offset = saved_curip->ksmps_offset;
p->ip->ksmps_no_end = saved_curip->ksmps_no_end;
p->ip->tieflag = saved_curip->tieflag;
p->ip->reinitflag = saved_curip->reinitflag;
/* copy remainder of pfields */
pfield = (CS_VAR_MEM*)&p->ip->p3;
/* by default all inputs are i-rate mapped to p-fields */
if (UNLIKELY(p->INOCOUNT >
(unsigned int)(csound->engineState.instrtxtp[instno]->pmax + 1)))
return csoundInitError(csound, Str("subinstr: too many p-fields"));
union {
MYFLT d;
int32 i;
} ch;
int str_cnt = 0, len = 0;
char *argstr;
for (n = 1; (unsigned int) n < p->INOCOUNT; n++){
if (IS_STR_ARG(p->ar[inarg_ofs + n])) {
ch.d = SSTRCOD;
ch.i = str_cnt & 0xffff;
(pfield + n)->value = ch.d;
argstr = ((STRINGDAT *)p->ar[inarg_ofs + n])->data;
if (str_cnt == 0)
p->ip->strarg = csound->Calloc(csound, strlen(argstr)+1);
else
p->ip->strarg = csound->ReAlloc(csound, p->ip->strarg,
len+strlen(argstr)+1);
strcpy(p->ip->strarg + len, argstr);
len += strlen(argstr)+1;
str_cnt++;
}
else (pfield + n)->value = *p->ar[inarg_ofs + n];
}
/* allocate memory for a temporary store of spout buffers */
if (!init_op && !(pip->reinitflag | pip->tieflag))
csoundAuxAlloc(csound,
(int32) csound->nspout * sizeof(MYFLT), &p->saved_spout);
/* do init pass for this instr */
csound->curip = p->ip; /* **** NEW *** */
p->ip->init_done = 0;
csound->ids = (OPDS *)p->ip;
csound->mode = 1;
while ((csound->ids = csound->ids->nxti) != NULL) {
csound->op = csound->ids->optext->t.oentry->opname;
(*csound->ids->iopadr)(csound, csound->ids);
}
csound->mode = 0;
p->ip->init_done = 1;
/* copy length related parameters back to caller instr */
saved_curip->xtratim = csound->curip->xtratim;
saved_curip->relesing = csound->curip->relesing;
saved_curip->offbet = csound->curip->offbet;
saved_curip->offtim = csound->curip->offtim;
saved_curip->p3 = csound->curip->p3;
/* restore globals */
csound->ids = saved_ids;
csound->curip = saved_curip;
return OK;
}
int subinstrset_S(CSOUND *csound, SUBINST *p){
int instno, init_op, inarg_ofs;
/* check if we are using subinstrinit or subinstr */
init_op = (p->h.opadr == NULL ? 1 : 0);
inarg_ofs = (init_op ? 0 : SUBINSTNUMOUTS);
instno = strarg2insno(csound, ((STRINGDAT *)p->ar[inarg_ofs])->data, 1);
if (UNLIKELY(instno==NOT_AN_INSTRUMENT)) instno = -1;
return subinstrset_(csound,p,instno);
}
int subinstrset(CSOUND *csound, SUBINST *p){
int instno, init_op, inarg_ofs;
/* check if we are using subinstrinit or subinstr */
init_op = (p->h.opadr == NULL ? 1 : 0);
inarg_ofs = (init_op ? 0 : SUBINSTNUMOUTS);
instno = (int) *(p->ar[inarg_ofs]);
return subinstrset_(csound,p,instno);
}
/* IV - Sep 8 2002: new functions for user defined opcodes (based */
/* on Matt J. Ingalls' subinstruments, but mostly rewritten) */
/*
UDOs now use the local ksmps stored in lcurip->ksmps
all the other dependent parameters are calculated in relation to
this.
lcurip->ksmps is set to the caller ksmps (CS_KSMPS), unless a new
local ksmps is used, in which case it is set to that value.
If local ksmps differs from CS_KSMPS, we set useropcd1() to
deal with the perf-time code. Otherwise useropcd2() is used.
For recursive calls when the local ksmps is set to differ from
the calling instrument ksmps, the top-level call
will use useropcd1(), whereas all the other recursive calls
will use useropdc2(), since their local ksmps will be the same
as the caller.
Also in case of a local ksmps that differs from the caller,
the local kcounter value, obtained from the caller is
scaled to denote the correct kcount in terms of local
kcycles.
*/
int useropcd1(CSOUND *, UOPCODE*), useropcd2(CSOUND *, UOPCODE*);
int useropcdset(CSOUND *csound, UOPCODE *p)
{
OPDS *saved_ids = csound->ids;
INSDS *parent_ip = csound->curip, *lcurip;
INSTRTXT *tp;
unsigned int instno;
unsigned int pcnt;
unsigned int i, n;
OPCODINFO *inm;
OPCOD_IOBUFS *buf = NULL;
MYFLT ksmps_scale;
unsigned int local_ksmps;
/* default ksmps */
local_ksmps = CS_KSMPS;
ksmps_scale = 1;
/* look up the 'fake' instr number, and opcode name */
inm = (OPCODINFO*) p->h.optext->t.oentry->useropinfo;
instno = inm->instno;
tp = csound->engineState.instrtxtp[instno];
if (tp == NULL)
return csound->InitError(csound, Str("Cannot find instr %d (UDO %s)\n"),
instno, inm->name);
/* set local ksmps if defined by user */
n = p->OUTOCOUNT + p->INCOUNT - 1;
if (*(p->ar[n]) != FL(0.0)) {
i = (unsigned int) *(p->ar[n]);
if (UNLIKELY(i < 1 || i > csound->ksmps ||
((CS_KSMPS / i) * i) != CS_KSMPS)) {
return csoundInitError(csound, Str("%s: invalid local ksmps value: %d"),
inm->name, i);
}
local_ksmps = i;
}
if (!p->ip) {
/* search for already allocated, but not active instance */
/* if none was found, allocate a new instance */
tp = csound->engineState.instrtxtp[instno];
if (tp == NULL) {
return csound->InitError(csound, Str("Cannot find instr %d (UDO %s)\n"),
instno, inm->name);
}
if (!tp->act_instance)
instance(csound, instno);
lcurip = tp->act_instance; /* use free instance, and */
tp->act_instance = lcurip->nxtact; /* remove from chain */
if (lcurip->opcod_iobufs==NULL)
return csound->InitError(csound, "Broken redefinition of UDO %d (UDO %s)\n",
instno, inm->name);
lcurip->actflg++; /* and mark the instr active */
tp->active++;
tp->instcnt++;
/* link into deact chain */
lcurip->opcod_deact = parent_ip->opcod_deact;
lcurip->subins_deact = NULL;
parent_ip->opcod_deact = (void*) p;
p->ip = lcurip;
/* IV - Nov 10 2002: set up pointers to I/O buffers */
buf = p->buf = (OPCOD_IOBUFS*) lcurip->opcod_iobufs;
buf->opcode_info = inm;
/* initialise perf time address lists */
/* **** Could be a memset **** */
buf->iobufp_ptrs[0] = buf->iobufp_ptrs[1] = NULL;
buf->iobufp_ptrs[2] = buf->iobufp_ptrs[3] = NULL;
buf->iobufp_ptrs[4] = buf->iobufp_ptrs[5] = NULL;
buf->iobufp_ptrs[6] = buf->iobufp_ptrs[7] = NULL;
buf->iobufp_ptrs[8] = buf->iobufp_ptrs[9] = NULL;
buf->iobufp_ptrs[10] = buf->iobufp_ptrs[11] = NULL;
/* store parameters of input and output channels, and parent ip */
buf->uopcode_struct = (void*) p;
buf->parent_ip = p->parent_ip = parent_ip;
}
/* copy parameters from the caller instrument into our subinstrument */
lcurip = p->ip;
/* set the local ksmps values */
if (local_ksmps != CS_KSMPS) {
/* this is the case when p->ip->ksmps != p->h.insdshead->ksmps */
lcurip->ksmps = local_ksmps;
ksmps_scale = CS_KSMPS / local_ksmps;
lcurip->onedksmps = FL(1.0) / (MYFLT) local_ksmps;
lcurip->ekr = csound->esr / (MYFLT) local_ksmps;
lcurip->onedkr = FL(1.0) / lcurip->ekr;
lcurip->kicvt = (MYFLT) FMAXLEN /lcurip->ekr;
lcurip->kcounter = (CS_KCNT)*ksmps_scale;
} else {
lcurip->ksmps = CS_KSMPS;
lcurip->kcounter = CS_KCNT;
lcurip->ekr = CS_EKR;
lcurip->onedkr = CS_ONEDKR;
lcurip->onedksmps = CS_ONEDKSMPS;
lcurip->kicvt = CS_KICVT;
}
/* VL 13-12-13 */
/* this sets ksmps and kr local variables */
/* create local ksmps variable and init with ksmps */
if (lcurip->lclbas != NULL) {
CS_VARIABLE *var =
csoundFindVariableWithName(csound, lcurip->instr->varPool, "ksmps");
*((MYFLT *)(var->memBlockIndex + lcurip->lclbas)) = lcurip->ksmps;
/* same for kr */
var =
csoundFindVariableWithName(csound, lcurip->instr->varPool, "kr");
*((MYFLT *)(var->memBlockIndex + lcurip->lclbas)) = lcurip->ekr;
}
lcurip->m_chnbp = parent_ip->m_chnbp; /* MIDI parameters */
lcurip->m_pitch = parent_ip->m_pitch;
lcurip->m_veloc = parent_ip->m_veloc;
lcurip->xtratim = parent_ip->xtratim * ksmps_scale;
lcurip->m_sust = 0;
lcurip->relesing = parent_ip->relesing;
lcurip->offbet = parent_ip->offbet;
lcurip->offtim = parent_ip->offtim;
lcurip->nxtolap = NULL;
lcurip->ksmps_offset = parent_ip->ksmps_offset;
lcurip->ksmps_no_end = parent_ip->ksmps_no_end;
lcurip->tieflag = parent_ip->tieflag;
lcurip->reinitflag = parent_ip->reinitflag;
/* copy all p-fields, including p1 (will this work ?) */
if (tp->pmax > 3) { /* requested number of p-fields */
n = tp->pmax; pcnt = 0;
while (pcnt < n) {
if ((i = csound->engineState.instrtxtp[parent_ip->insno]->pmax) > pcnt) {
if (i > n) i = n;
/* copy next block of p-fields */
memcpy(&(lcurip->p1) + pcnt, &(parent_ip->p1) + pcnt,
(size_t) ((i - pcnt) * sizeof(CS_VAR_MEM)));
pcnt = i;
}
/* top level instr reached */
if (parent_ip->opcod_iobufs == NULL) break;
parent_ip = ((OPCOD_IOBUFS*) parent_ip->opcod_iobufs)->parent_ip;
}
}
else
memcpy(&(lcurip->p1), &(parent_ip->p1), 3 * sizeof(CS_VAR_MEM));
/* do init pass for this instr */
csound->curip = lcurip;
csound->ids = (OPDS *) (lcurip->nxti);
ATOMIC_SET(p->ip->init_done, 0);
csound->mode = 1;
while (csound->ids != NULL) {
csound->op = csound->ids->optext->t.oentry->opname;
(*csound->ids->iopadr)(csound, csound->ids);
csound->ids = csound->ids->nxti;
}
csound->mode = 0;
ATOMIC_SET(p->ip->init_done, 1);
/* copy length related parameters back to caller instr */
parent_ip->relesing = lcurip->relesing;
parent_ip->offbet = lcurip->offbet;
parent_ip->offtim = lcurip->offtim;
parent_ip->p3 = lcurip->p3;
local_ksmps = lcurip->ksmps;
/* restore globals */
csound->ids = saved_ids;
csound->curip = parent_ip;
/* select perf routine and scale xtratim accordingly */
if (local_ksmps != CS_KSMPS) {
ksmps_scale = CS_KSMPS / local_ksmps;
parent_ip->xtratim = lcurip->xtratim / ksmps_scale;
p->h.opadr = (SUBR) useropcd1;
}
else {
parent_ip->xtratim = lcurip->xtratim;
p->h.opadr = (SUBR) useropcd2;
}
if (UNLIKELY(csound->oparms->odebug))
csound->Message(csound, "EXTRATIM=> cur(%p): %d, parent(%p): %d\n",
lcurip, lcurip->xtratim, parent_ip, parent_ip->xtratim);
return OK;
}
/* IV - Sep 17 2002: dummy user opcode function for not initialised case */
int useropcd(CSOUND *csound, UOPCODE *p)
{
if (UNLIKELY(p->h.nxtp))
return csoundPerfError(csound, &(p->h), Str("%s: not initialised"),
p->h.optext->t.opcod);
else
return OK;
}
/* IV - Sep 1 2002: new opcodes: xin, xout */
int xinset(CSOUND *csound, XIN *p)
{
OPCOD_IOBUFS *buf;
OPCODINFO *inm;
MYFLT **bufs, **tmp;
int i;
CS_VARIABLE* current;
(void) csound;
buf = (OPCOD_IOBUFS*) p->h.insdshead->opcod_iobufs;
inm = buf->opcode_info;
bufs = ((UOPCODE*) buf->uopcode_struct)->ar + inm->outchns;
tmp = buf->iobufp_ptrs; // this is used to record the UDO's internal vars
// for copying at perf-time
current = inm->in_arg_pool->head;
for (i = 0; i < inm->inchns; i++) {
void* in = (void*)bufs[i];
void* out = (void*)p->args[i];
tmp[i + inm->outchns] = out;
current->varType->copyValue(csound, out, in);
current = current->next;
}
return OK;
}
int xoutset(CSOUND *csound, XOUT *p)
{
OPCOD_IOBUFS *buf;
OPCODINFO *inm;
MYFLT **bufs, **tmp;
CS_VARIABLE* current;
int i;
(void) csound;
buf = (OPCOD_IOBUFS*) p->h.insdshead->opcod_iobufs;
inm = buf->opcode_info;
bufs = ((UOPCODE*) buf->uopcode_struct)->ar;
tmp = buf->iobufp_ptrs; // this is used to record the UDO's internal vars
// for copying at perf-time
current = inm->out_arg_pool->head;
for (i = 0; i < inm->outchns; i++) {
void* in = (void*) p->args[i];
void* out = (void*)bufs[i];
tmp[i] = in;
// DO NOT COPY K or A vars
// Fsigs need to be copied for initialization purposes.
if (inm->outtypes[i] != 'k' && csoundGetTypeForArg(in) != &CS_VAR_TYPE_A)
current->varType->copyValue(csound, out, in);
//printf("%c \n", inm->outtypes[i]);
current = current->next;
}
return OK;
}
/* IV - Sep 8 2002: new opcode: setksmps */
/*
This opcode sets the local ksmps for an instrument
it can be used on any instrument with the implementation
of a mechanism to perform at local ksmps (in kperf etc)
*/
//#include "typetabl.h"
#include "csound_standard_types.h"
int setksmpsset(CSOUND *csound, SETKSMPS *p)
{
unsigned int l_ksmps, n;
l_ksmps = (unsigned int) *(p->i_ksmps);
if (!l_ksmps) return OK; /* zero: do not change */
if (UNLIKELY(l_ksmps < 1 || l_ksmps > CS_KSMPS ||
((CS_KSMPS / l_ksmps) * l_ksmps != CS_KSMPS))) {
return csoundInitError(csound,
Str("setksmps: invalid ksmps value: %d, original: %d"),
l_ksmps, CS_KSMPS);
}
n = CS_KSMPS / l_ksmps;
p->h.insdshead->xtratim *= n;
CS_KSMPS = l_ksmps;
CS_ONEDKSMPS = FL(1.0) / (MYFLT) CS_KSMPS;
CS_EKR = csound->esr / (MYFLT) CS_KSMPS;
CS_ONEDKR = FL(1.0) / CS_EKR;
CS_KICVT = (MYFLT) FMAXLEN / CS_EKR;
CS_KCNT *= n;
/* VL 13-12-13 */
/* this sets ksmps and kr local variables */
/* lookup local ksmps variable and init with ksmps */
INSTRTXT *ip = p->h.insdshead->instr;
CS_VARIABLE *var =
csoundFindVariableWithName(csound, ip->varPool, "ksmps");
MYFLT *varmem = p->h.insdshead->lclbas + var->memBlockIndex;
*varmem = CS_KSMPS;
/* same for kr */
var =
csoundFindVariableWithName(csound, ip->varPool, "kr");
varmem = p->h.insdshead->lclbas + var->memBlockIndex;
*varmem = CS_EKR;
return OK;
}
/* IV - Oct 16 2002: nstrnum opcode (returns the instrument number of a */
/* named instrument) */
int nstrnumset(CSOUND *csound, NSTRNUM *p)
{
/* IV - Oct 31 2002 */
int res = strarg2insno(csound, p->iname, 0);
if (UNLIKELY(res == NOT_AN_INSTRUMENT)) {
*p->i_insno = -FL(1.0); return NOTOK;
}
else {
*p->i_insno = (MYFLT)res; return OK;
}
}
int nstrnumset_S(CSOUND *csound, NSTRNUM *p)
{
/* IV - Oct 31 2002 */
int res = strarg2insno(csound, ((STRINGDAT *)p->iname)->data, 1);
if (UNLIKELY(res == NOT_AN_INSTRUMENT)) {
*p->i_insno = -FL(1.0); return NOTOK;
}
else {
*p->i_insno = (MYFLT)res; return OK;
}
}
int nstrstr(CSOUND *csound, NSTRSTR *p)
{
char *ss;
if (csound->engineState.instrumentNames) {
ss = cs_inverse_hash_get(csound,
csound->engineState.instrumentNames,
(int)*p->num);
}
else ss= "";
mfree(csound,p->ans->data);
p->ans->data = cs_strdup(csound, ss);
p->ans->size = strlen(ss);
return OK;
}
/* unlink expired notes from activ chain */
/* and mark them inactive */
/* close any files in each fdchain */
/* IV - Feb 05 2005: changed to double */
void beatexpire(CSOUND *csound, double beat)
{
INSDS *ip;
strt:
if ((ip = csound->frstoff) != NULL && ip->offbet <= beat) {
do {
if (!ip->relesing && ip->xtratim) {
/* IV - Nov 30 2002: */
/* allow extra time for finite length (p3 > 0) score notes */
set_xtratim(csound, ip); /* enter release stage */
csound->frstoff = ip->nxtoff; /* update turnoff list */
#ifdef BETA
if (UNLIKELY(csound->oparms->odebug))
csound->Message(csound, "Calling schedofftim line %d\n", __LINE__);
#endif
schedofftim(csound, ip);
goto strt; /* and start again */
}
else
deact(csound, ip); /* IV - Sep 5 2002: use deact() as it also */
} /* deactivates subinstrument instances */
while ((ip = ip->nxtoff) != NULL && ip->offbet <= beat);
csound->frstoff = ip;
if (UNLIKELY(csound->oparms->odebug)) {
csound->Message(csound, "deactivated all notes to beat %7.3f\n", beat);
csound->Message(csound, "frstoff = %p\n", (void*) csound->frstoff);
}
}
}
/* unlink expired notes from activ chain */
/* and mark them inactive */
/* close any files in each fdchain */
/* IV - Feb 05 2005: changed to double */
void timexpire(CSOUND *csound, double time)
{
INSDS *ip;
strt:
if ((ip = csound->frstoff) != NULL && ip->offtim <= time) {
do {
if (!ip->relesing && ip->xtratim) {
/* IV - Nov 30 2002: */
/* allow extra time for finite length (p3 > 0) score notes */
set_xtratim(csound, ip); /* enter release stage */
csound->frstoff = ip->nxtoff; /* update turnoff list */
#ifdef BETA
if (UNLIKELY(csound->oparms->odebug))
csound->Message(csound, "Calling schedofftim line %d\n", __LINE__);
#endif
schedofftim(csound, ip);
goto strt; /* and start again */
}
else {
deact(csound, ip); /* IV - Sep 5 2002: use deact() as it also */
}
} /* deactivates subinstrument instances */
while ((ip = ip->nxtoff) != NULL && ip->offtim <= time);
csound->frstoff = ip;
if (UNLIKELY(csound->oparms->odebug)) {
csound->Message(csound, "deactivated all notes to time %7.3f\n", time);
csound->Message(csound, "frstoff = %p\n", (void*) csound->frstoff);
}
}
}
/**
this was rewritten for Csound 6 to allow
PARCS and local ksmps instruments
*/
int subinstr(CSOUND *csound, SUBINST *p)
{
OPDS *saved_pds = CS_PDS;
MYFLT *pbuf;
uint32_t frame, chan;
unsigned int nsmps = CS_KSMPS;
INSDS *ip = p->ip;
int done = ATOMIC_GET(p->ip->init_done);
if (UNLIKELY(!done)) /* init not done, exit */
return OK;
//printf("%s\n", p->ip->strarg);
if (UNLIKELY(p->ip == NULL)) { /* IV - Oct 26 2002 */
return csoundPerfError(csound, &(p->h),
Str("subinstr: not initialised"));
}
/* copy current spout buffer and clear it */
ip->spout = (MYFLT*) p->saved_spout.auxp;
memset(ip->spout, 0, csound->nspout*sizeof(MYFLT));
csound->spoutactive = 0;
/* update release flag */
ip->relesing = p->parent_ip->relesing; /* IV - Nov 16 2002 */
/* run each opcode */
if (csound->ksmps == ip->ksmps) {
int error = 0;
ip->kcounter++;
if ((CS_PDS = (OPDS *) (ip->nxtp)) != NULL) {
CS_PDS->insdshead->pds = NULL;
do {
error = (*CS_PDS->opadr)(csound, CS_PDS);
if (CS_PDS->insdshead->pds != NULL) {
CS_PDS = CS_PDS->insdshead->pds;
CS_PDS->insdshead->pds = NULL;
}
} while (error == 0 && (CS_PDS = CS_PDS->nxtp));
}
}
else {
int i, n = csound->nspout, start = 0;
int lksmps = ip->ksmps;
int incr = csound->nchnls*lksmps;
int offset = ip->ksmps_offset;
int early = ip->ksmps_no_end;
ip->spin = csound->spin;
ip->kcounter = csound->kcounter*csound->ksmps/lksmps;
/* we have to deal with sample-accurate code
whole CS_KSMPS blocks are offset here, the
remainder is left to each opcode to deal with.
*/
while (offset >= lksmps) {
offset -= lksmps;
start += csound->nchnls;
}
ip->ksmps_offset = offset;
if (early) {
n -= (early*csound->nchnls);
ip->ksmps_no_end = early % lksmps;
}
for (i=start; i < n; i+=incr, ip->spin+=incr, ip->spout+=incr) {
ip->kcounter++;
if ((CS_PDS = (OPDS *) (ip->nxtp)) != NULL) {
int error = 0;
CS_PDS->insdshead->pds = NULL;
do {
if(UNLIKELY(!ATOMIC_GET8(p->ip->actflg))){
memset(p->ar, 0, sizeof(MYFLT)*CS_KSMPS*p->OUTCOUNT);
goto endin;
}
error = (*CS_PDS->opadr)(csound, CS_PDS);
if (CS_PDS->insdshead->pds != NULL) {
CS_PDS = CS_PDS->insdshead->pds;
CS_PDS->insdshead->pds = NULL;
}
} while (error == 0 && (CS_PDS = CS_PDS->nxtp));
}
}
ip->spout = (MYFLT*) p->saved_spout.auxp;
}
/* copy outputs */
for (chan = 0; chan < p->OUTOCOUNT; chan++) {
for (pbuf = ip->spout + chan*nsmps, frame = 0;
frame < nsmps; frame++) {
p->ar[chan][frame] = pbuf[frame];
//printf("%f\n", p->ar[chan][frame]);
//pbuf += csound->nchnls;
}
}
endin:
CS_PDS = saved_pds;
/* check if instrument was deactivated (e.g. by perferror) */
if (!p->ip) { /* loop to last opds */
while (CS_PDS->nxtp) {
CS_PDS = CS_PDS->nxtp;
}
}
return OK;
}
/* IV - Sep 17 2002 -- case 1: local ksmps is used */
int useropcd1(CSOUND *csound, UOPCODE *p)
{
OPDS *saved_pds = CS_PDS;
int g_ksmps, ofs, early, offset, i;
OPCODINFO *inm;
CS_VARIABLE* current;
INSDS *this_instr = p->ip;
MYFLT** internal_ptrs = p->buf->iobufp_ptrs;
MYFLT** external_ptrs = p->ar;
int done;
done = ATOMIC_GET(p->ip->init_done);
if (UNLIKELY(!done)) /* init not done, exit */
return OK;
p->ip->relesing = p->parent_ip->relesing; /* IV - Nov 16 2002 */
early = p->h.insdshead->ksmps_no_end;
offset = p->h.insdshead->ksmps_offset;
p->ip->spin = p->parent_ip->spin;
p->ip->spout = p->parent_ip->spout;
inm = p->buf->opcode_info;
/* global ksmps is the caller instr ksmps minus sample-accurate end */
g_ksmps = CS_KSMPS - early;
/* sample-accurate offset */
ofs = offset;
/* clear offsets, since with CS_KSMPS=1
they don't apply to opcodes, but to the
calling code (ie. this code)
*/
this_instr->ksmps_offset = 0;
this_instr->ksmps_no_end = 0;
if (this_instr->ksmps == 1) { /* special case for local kr == sr */
do {
this_instr->kcounter++; /*kcounter needs to be incremented BEFORE perf */
/* copy inputs */ current = inm->in_arg_pool->head;
for (i = 0; i < inm->inchns; i++) {
// this hardcoded type check for non-perf time vars needs to change
//to use generic code...
// skip a-vars for now, handle uniquely within performance loop
if (current->varType != &CS_VAR_TYPE_I &&
current->varType != &CS_VAR_TYPE_b &&
current->varType != &CS_VAR_TYPE_A &&
current->subType != &CS_VAR_TYPE_I &&
current->subType != &CS_VAR_TYPE_A) {
// This one checks if an array has a subtype of 'i'
void* in = (void*)external_ptrs[i + inm->outchns];
void* out = (void*)internal_ptrs[i + inm->outchns];
current->varType->copyValue(csound, out, in);
} else if (current->varType == &CS_VAR_TYPE_A) {
MYFLT* in = (void*)external_ptrs[i + inm->outchns];
MYFLT* out = (void*)internal_ptrs[i + inm->outchns];
*out = *(in + ofs);
} else if (current->varType == &CS_VAR_TYPE_ARRAY &&
current->subType == &CS_VAR_TYPE_A) {
ARRAYDAT* src = (ARRAYDAT*)external_ptrs[i + inm->outchns];
ARRAYDAT* target = (ARRAYDAT*)internal_ptrs[i + inm->outchns];
int count = src->sizes[0];
int j;
if (src->dimensions > 1) {
for (j = 0; j < src->dimensions; j++) {
count *= src->sizes[j];
}
}
for (j = 0; j < count; j++) {
int memberOffset = j * (src->arrayMemberSize / sizeof(MYFLT));
MYFLT* in = src->data + memberOffset;
MYFLT* out = target->data + memberOffset;
*out = *(in + ofs);
}
}
current = current->next;
}
if ((CS_PDS = (OPDS *) (this_instr->nxtp)) != NULL) {
int error = 0;
CS_PDS->insdshead->pds = NULL;
do {
if(UNLIKELY(!ATOMIC_GET8(p->ip->actflg))) goto endop;
error = (*CS_PDS->opadr)(csound, CS_PDS);
if (CS_PDS->insdshead->pds != NULL &&
CS_PDS->insdshead->pds->insdshead) {
CS_PDS = CS_PDS->insdshead->pds;
CS_PDS->insdshead->pds = NULL;
}
} while (error == 0 && p->ip != NULL
&& (CS_PDS = CS_PDS->nxtp));
}
/* copy a-sig outputs, accounting for offset */
current = inm->out_arg_pool->head;
for (i = 0; i < inm->outchns; i++) {
if (current->varType == &CS_VAR_TYPE_A) {
MYFLT* in = (void*)internal_ptrs[i];
MYFLT* out = (void*)external_ptrs[i];
*(out + ofs) = *in;
} else if (current->varType == &CS_VAR_TYPE_ARRAY &&
current->subType == &CS_VAR_TYPE_A) {
ARRAYDAT* src = (ARRAYDAT*)internal_ptrs[i];
ARRAYDAT* target = (ARRAYDAT*)external_ptrs[i];
int count = src->sizes[0];
int j;
if (src->dimensions > 1) {
for (j = 0; j < src->dimensions; j++) {
count *= src->sizes[j];
}
}
for (j = 0; j < count; j++) {
int memberOffset = j * (src->arrayMemberSize / sizeof(MYFLT));
MYFLT* in = src->data + memberOffset;
MYFLT* out = target->data + memberOffset;
*(out + ofs) = *in;
}
}
current = current->next;
}
this_instr->spout += csound->nchnls;
this_instr->spin += csound->nchnls;
} while (++ofs < g_ksmps);
}
else {
/* generic case for local kr != sr */
/* we have to deal with sample-accurate code
whole CS_KSMPS blocks are offset here, the
remainder is left to each opcode to deal with.
*/
int start = 0;
int lksmps = this_instr->ksmps;
while (ofs >= lksmps) {
ofs -= lksmps;
start++;
}
this_instr->ksmps_offset = ofs;
ofs = start;
if (UNLIKELY(early)) this_instr->ksmps_no_end = early % lksmps;
do {
this_instr->kcounter++;
/* copy a-sig inputs, accounting for offset */
size_t asigSize = (this_instr->ksmps * sizeof(MYFLT));
current = inm->in_arg_pool->head;
for (i = 0; i < inm->inchns; i++) {
// this hardcoded type check for non-perf time vars needs to change
// to use generic code...
// skip a-vars for now, handle uniquely within performance loop
if (current->varType != &CS_VAR_TYPE_I &&
current->varType != &CS_VAR_TYPE_b &&
current->varType != &CS_VAR_TYPE_A &&
current->subType != &CS_VAR_TYPE_I &&
current->subType != &CS_VAR_TYPE_A) {
// This one checks if an array has a subtype of 'i'
void* in = (void*)external_ptrs[i + inm->outchns];
void* out = (void*)internal_ptrs[i + inm->outchns];
current->varType->copyValue(csound, out, in);
} else if (current->varType == &CS_VAR_TYPE_A) {
MYFLT* in = (void*)external_ptrs[i + inm->outchns];
MYFLT* out = (void*)internal_ptrs[i + inm->outchns];
memcpy(out, in + ofs, asigSize);
} else if (current->varType == &CS_VAR_TYPE_ARRAY &&
current->subType == &CS_VAR_TYPE_A) {
ARRAYDAT* src = (ARRAYDAT*)external_ptrs[i + inm->outchns];
ARRAYDAT* target = (ARRAYDAT*)internal_ptrs[i + inm->outchns];
int count = src->sizes[0];
int j;
if (src->dimensions > 1) {
for (j = 0; j < src->dimensions; j++) {
count *= src->sizes[j];
}
}
for (j = 0; j < count; j++) {
int memberOffset = j * (src->arrayMemberSize / sizeof(MYFLT));
MYFLT* in = src->data + memberOffset;
MYFLT* out = target->data + memberOffset;
memcpy(out, in + ofs, asigSize);
}
}
current = current->next;
}
/* run each opcode */
if ((CS_PDS = (OPDS *) (this_instr->nxtp)) != NULL) {
int error = 0;
CS_PDS->insdshead->pds = NULL;
do {
if(UNLIKELY(!ATOMIC_GET8(p->ip->actflg))) goto endop;
error = (*CS_PDS->opadr)(csound, CS_PDS);
if (CS_PDS->insdshead->pds != NULL &&
CS_PDS->insdshead->pds->insdshead) {
CS_PDS = CS_PDS->insdshead->pds;
CS_PDS->insdshead->pds = NULL;
}
} while (error == 0 && p->ip != NULL
&& (CS_PDS = CS_PDS->nxtp));
}
/* copy a-sig outputs, accounting for offset */
current = inm->out_arg_pool->head;
for (i = 0; i < inm->outchns; i++) {
if (current->varType == &CS_VAR_TYPE_A) {
MYFLT* in = (void*)internal_ptrs[i];
MYFLT* out = (void*)external_ptrs[i];
memcpy(out + ofs, in, asigSize);
} else if (current->varType == &CS_VAR_TYPE_ARRAY &&
current->subType == &CS_VAR_TYPE_A) {
ARRAYDAT* src = (ARRAYDAT*)internal_ptrs[i];
ARRAYDAT* target = (ARRAYDAT*)external_ptrs[i];
int count = src->sizes[0];
int j;
if (src->dimensions > 1) {
for (j = 0; j < src->dimensions; j++) {
count *= src->sizes[j];
}
}
for (j = 0; j < count; j++) {
int memberOffset = j * (src->arrayMemberSize / sizeof(MYFLT));
MYFLT* in = src->data + memberOffset;
MYFLT* out = target->data + memberOffset;
memcpy(out + ofs, in, asigSize);
}
}
current = current->next;
}
this_instr->spout += csound->nchnls*lksmps;
this_instr->spin += csound->nchnls*lksmps;
} while ((ofs += this_instr->ksmps) < g_ksmps);
}
/* copy outputs */
current = inm->out_arg_pool->head;
for (i = 0; i < inm->outchns; i++) {
// this hardcoded type check for non-perf time vars needs to change
// to use generic code...
if (current->varType != &CS_VAR_TYPE_I &&
current->varType != &CS_VAR_TYPE_b &&
current->subType != &CS_VAR_TYPE_I) {
void* in = (void*)internal_ptrs[i];
void* out = (void*)external_ptrs[i];
if (current->varType == &CS_VAR_TYPE_A) {
/* clear the beginning portion of outputs for sample accurate end */
if (offset) {
memset(out, '\0', sizeof(MYFLT) * offset);
}
/* clear the end portion of outputs for sample accurate end */
if (early) {
memset((char*)out + g_ksmps, '\0', sizeof(MYFLT) * early);
}
} else if (current->varType == &CS_VAR_TYPE_ARRAY &&
current->subType == &CS_VAR_TYPE_A) {
if (offset || early) {
ARRAYDAT* outDat = (ARRAYDAT*)out;
int count = outDat->sizes[0];
int j;
if (outDat->dimensions > 1) {
for (j = 0; j < outDat->dimensions; j++) {
count *= outDat->sizes[j];
}
}
if (offset) {
for (j = 0; j < count; j++) {
int memberOffset = j * (outDat->arrayMemberSize / sizeof(MYFLT));
MYFLT* outMem = outDat->data + memberOffset;
memset(outMem, '\0', sizeof(MYFLT) * offset);
}
}
if (early) {
for (j = 0; j < count; j++) {
int memberOffset = j * (outDat->arrayMemberSize / sizeof(MYFLT));
MYFLT* outMem = outDat->data + memberOffset;
memset(outMem + g_ksmps, '\0', sizeof(MYFLT) * early);
}
}
}
} else {
current->varType->copyValue(csound, out, in);
}
}
current = current->next;
}
endop:
CS_PDS = saved_pds;
/* check if instrument was deactivated (e.g. by perferror) */
if (!p->ip) /* loop to last opds */
while (CS_PDS && CS_PDS->nxtp) CS_PDS = CS_PDS->nxtp;
return OK;
}
/* IV - Sep 17 2002 -- case 2: simplified routine for no local ksmps */
int useropcd2(CSOUND *csound, UOPCODE *p)
{
OPDS *saved_pds = CS_PDS;
MYFLT **tmp;
OPCODINFO *inm;
CS_VARIABLE* current;
int i, done;
inm = (OPCODINFO*) p->h.optext->t.oentry->useropinfo; /* FIXME value not used */
done = ATOMIC_GET(p->ip->init_done);
if (UNLIKELY(!done)) /* init not done, exit */
return OK;
p->ip->spin = p->parent_ip->spin;
p->ip->spout = p->parent_ip->spout;
p->ip->kcounter++; /* kcount should be incremented BEFORE perf */
if (UNLIKELY(!(CS_PDS = (OPDS*) (p->ip->nxtp))))
goto endop; /* no perf code */
/* IV - Nov 16 2002: update release flag */
p->ip->relesing = p->parent_ip->relesing;
tmp = p->buf->iobufp_ptrs;
inm = p->buf->opcode_info;
MYFLT** internal_ptrs = tmp;
MYFLT** external_ptrs = p->ar;
/* copy inputs */
current = inm->in_arg_pool->head;
for (i = 0; i < inm->inchns; i++) {
// this hardcoded type check for non-perf time vars needs to
//change to use generic code...
if (current->varType != &CS_VAR_TYPE_I &&
current->varType != &CS_VAR_TYPE_b &&
current->subType != &CS_VAR_TYPE_I) {
if (current->varType == &CS_VAR_TYPE_A && CS_KSMPS == 1) {
*internal_ptrs[i + inm->outchns] = *external_ptrs[i + inm->outchns];
} else {
void* in = (void*)external_ptrs[i + inm->outchns];
void* out = (void*)internal_ptrs[i + inm->outchns];
current->varType->copyValue(csound, out, in);
// memcpy(out, in, p->buf->in_arg_sizes[i]);
}
}
current = current->next;
}
/* run each opcode */
{
int error = 0;
CS_PDS->insdshead->pds = NULL;
do {
if(UNLIKELY(!ATOMIC_GET8(p->ip->actflg))) goto endop;
error = (*CS_PDS->opadr)(csound, CS_PDS);
if (CS_PDS->insdshead->pds != NULL &&
CS_PDS->insdshead->pds->insdshead) {
CS_PDS = CS_PDS->insdshead->pds;
CS_PDS->insdshead->pds = NULL;
}
} while (error == 0 && p->ip != NULL
&& (CS_PDS = CS_PDS->nxtp));
}
/* copy outputs */
current = inm->out_arg_pool->head;
for (i = 0; i < inm->outchns; i++) {
// this hardcoded type check for non-perf time vars needs to change to
// use generic code...
if (current->varType != &CS_VAR_TYPE_I &&
current->varType != &CS_VAR_TYPE_b &&
current->subType != &CS_VAR_TYPE_I) {
if (current->varType == &CS_VAR_TYPE_A && CS_KSMPS == 1) {
*external_ptrs[i] = *internal_ptrs[i];
} else {
void* in = (void*)internal_ptrs[i];
void* out = (void*)external_ptrs[i];
// memcpy(out, in, p->buf->out_arg_sizes[i]);
current->varType->copyValue(csound, out, in);
}
}
current = current->next;
}
endop:
/* restore globals */
CS_PDS = saved_pds;
/* check if instrument was deactivated (e.g. by perferror) */
if (!p->ip) { /* loop to last opds */
while (CS_PDS && CS_PDS->nxtp) {
CS_PDS = CS_PDS->nxtp;
}
}
return OK;
}
/* UTILITY FUNCTIONS FOR LABELS */
int findLabelMemOffset(CSOUND* csound, INSTRTXT* ip, char* labelName) {
IGN(csound);
OPTXT* optxt = (OPTXT*) ip;
int offset = 0;
while ((optxt = optxt->nxtop) != NULL) {
TEXT* t = &optxt->t;
if (strcmp(t->oentry->opname, "$label") == 0 &&
strcmp(t->opcod, labelName) == 0) {
break;
}
offset += t->oentry->dsblksiz;
}
return offset;
}
/* create instance of an instr template */
/* allocates and sets up all pntrs */
static void instance(CSOUND *csound, int insno)
{
INSTRTXT *tp;
INSDS *ip;
OPTXT *optxt;
OPDS *opds, *prvids, *prvpds;
const OENTRY *ep;
int i, n, pextent, pextra, pextrab;
char *nxtopds, *opdslim;
MYFLT **argpp, *lclbas;
CS_VAR_MEM *lcloffbas; // start of pfields
char* opMemStart;
OPARMS *O = csound->oparms;
int odebug = O->odebug;
ARG* arg;
int argStringCount;
CS_VARIABLE* current;
tp = csound->engineState.instrtxtp[insno];
n = 3;
if (O->midiKey>n) n = O->midiKey;
if (O->midiKeyCps>n) n = O->midiKeyCps;
if (O->midiKeyOct>n) n = O->midiKeyOct;
if (O->midiKeyPch>n) n = O->midiKeyPch;
if (O->midiVelocity>n) n = O->midiVelocity;
if (O->midiVelocityAmp>n) n = O->midiVelocityAmp;
pextra = n-3;
pextrab = ((i = tp->pmax - 3L) > 0 ? (int) i * sizeof(CS_VAR_MEM) : 0);
/* alloc new space, */
pextent = sizeof(INSDS) + pextrab + pextra*sizeof(CS_VAR_MEM);
ip =
(INSDS*) csound->Calloc(csound,
(size_t) pextent + tp->varPool->poolSize +
(tp->varPool->varCount *
CS_FLOAT_ALIGN(CS_VAR_TYPE_OFFSET)) +
(tp->varPool->varCount * sizeof(CS_VARIABLE*)) +
tp->opdstot);
ip->csound = csound;
ip->m_chnbp = (MCHNBLK*) NULL;
ip->instr = tp;
/* IV - Oct 26 2002: replaced with faster version (no search) */
ip->prvinstance = tp->lst_instance;
if (tp->lst_instance)
tp->lst_instance->nxtinstance = ip;
else
tp->instance = ip;
tp->lst_instance = ip;
/* link into free instance chain */
ip->nxtact = tp->act_instance;
tp->act_instance = ip;
ip->insno = insno;
csoundDebugMsg(csound,"instance(): tp->act_instance = %p\n",
tp->act_instance);
if (insno > csound->engineState.maxinsno) {
// size_t pcnt = (size_t) tp->opcode_info->perf_incnt;
// pcnt += (size_t) tp->opcode_info->perf_outcnt;
OPCODINFO* info = tp->opcode_info;
size_t pcnt = sizeof(OPCOD_IOBUFS) +
sizeof(MYFLT*) * (info->inchns + info->outchns);
ip->opcod_iobufs = (void*) csound->Malloc(csound, pcnt);
}
/* gbloffbas = csound->globalVarPool; */
lcloffbas = (CS_VAR_MEM*)&ip->p0;
lclbas = (MYFLT*) ((char*) ip + pextent); /* split local space */
initializeVarPool((void *)csound, lclbas, tp->varPool);
opMemStart = nxtopds = (char*) lclbas + tp->varPool->poolSize +
(tp->varPool->varCount * CS_FLOAT_ALIGN(CS_VAR_TYPE_OFFSET));
opdslim = nxtopds + tp->opdstot;
if (UNLIKELY(odebug))
csound->Message(csound,
Str("instr %d allocated at %p\n\tlclbas %p, opds %p\n"),
insno, ip, lclbas, nxtopds);
optxt = (OPTXT*) tp;
prvids = prvpds = (OPDS*) ip;
// prvids->insdshead = ip;
/* initialize vars for CS_TYPE */
for (current = tp->varPool->head; current != NULL; current = current->next) {
char* ptr = (char*)(lclbas + current->memBlockIndex);
CS_TYPE** typePtr = (CS_TYPE**)(ptr - CS_VAR_TYPE_OFFSET);
*typePtr = current->varType;
}
while ((optxt = optxt->nxtop) != NULL) { /* for each op in instr */
TEXT *ttp = &optxt->t;
ep = ttp->oentry;
opds = (OPDS*) nxtopds; /* take reqd opds */
nxtopds += ep->dsblksiz;
if (UNLIKELY(strcmp(ep->opname, "endin") == 0 /* (until ENDIN) */
|| strcmp(ep->opname, "endop") == 0)) /* (or ENDOP) */
break;
if (UNLIKELY(strcmp(ep->opname, "pset") == 0)) {
ip->p1.value = (MYFLT) insno;
continue;
}
if (UNLIKELY(odebug))
csound->Message(csound, Str("op (%s) allocated at %p\n"),
ep->opname, opds);
opds->optext = optxt; /* set common headata */
opds->insdshead = ip;
if (strcmp(ep->opname, "$label") == 0) { /* LABEL: */
LBLBLK *lblbp = (LBLBLK *) opds;
lblbp->prvi = prvids; /* save i/p links */
lblbp->prvp = prvpds;
continue; /* for later refs */
}
// ******** This needs revisipn with no distinction between k- and a- rate ****
if ((ep->thread & 03) == 0) { /* thread 1 OR 2: */
if (ttp->pftype == 'b') {
prvids = prvids->nxti = opds;
opds->iopadr = ep->iopadr;
}
else {
prvpds = prvpds->nxtp = opds;
opds->opadr = ep->kopadr;
}
goto args;
}
if ((ep->thread & 01) != 0) { /* thread 1: */
prvids = prvids->nxti = opds; /* link into ichain */
opds->iopadr = ep->iopadr; /* & set exec adr */
if (UNLIKELY(opds->iopadr == NULL))
csoundDie(csound, Str("null iopadr"));
}
if ((n = ep->thread & 02) != 0) { /* thread 2 : */
prvpds = prvpds->nxtp = opds; /* link into pchain */
/* if (!(n & 04) || */
/* ((ttp->pftype == 'k' || ttp->pftype == 'c') && ep->kopadr != NULL)) */
opds->opadr = ep->kopadr; /* krate or */
/* else opds->opadr = ep->aopadr; /\* arate *\/ */
if (UNLIKELY(odebug))
csound->Message(csound, "opadr = %p\n", (void*) opds->opadr);
if (UNLIKELY(opds->opadr == NULL))
csoundDie(csound, Str("null opadr"));
}
args:
if (ep->useropinfo == NULL)
argpp = (MYFLT **) ((char *) opds + sizeof(OPDS));
else /* user defined opcodes are a special case */
argpp = &(((UOPCODE *) ((char *) opds))->ar[0]);
arg = ttp->outArgs;
for (n = 0; arg != NULL; n++) {
MYFLT *fltp;
CS_VARIABLE* var = (CS_VARIABLE*)arg->argPtr;
if (arg->type == ARG_GLOBAL) {
fltp = &(var->memBlock->value); /* gbloffbas + var->memBlockIndex; */
}
else if (arg->type == ARG_LOCAL) {
fltp = lclbas + var->memBlockIndex;
}
else if (arg->type == ARG_PFIELD) {
CS_VAR_MEM* pfield = lcloffbas + arg->index;
fltp = &(pfield->value);
}
else {
csound->Message(csound, Str("FIXME: Unhandled out-arg type: %d\n"),
arg->type);
fltp = NULL;
}
argpp[n] = fltp;
arg = arg->next;
}
for (argStringCount = argsRequired(ep->outypes);
n < argStringCount;
n++) /* if more outypes, pad */
argpp[n] = NULL;
arg = ttp->inArgs;
ip->lclbas = lclbas;
for (; arg != NULL; n++, arg = arg->next) {
CS_VARIABLE* var = (CS_VARIABLE*)(arg->argPtr);
if (arg->type == ARG_CONSTANT) {
CS_VAR_MEM *varMem = (CS_VAR_MEM*)arg->argPtr;
argpp[n] = &varMem->value;
}
else if (arg->type == ARG_STRING) {
argpp[n] = (MYFLT*)(arg->argPtr);
}
else if (arg->type == ARG_PFIELD) {
CS_VAR_MEM* pfield = lcloffbas + arg->index;
argpp[n] = &(pfield->value);
}
else if (arg->type == ARG_GLOBAL) {
argpp[n] = &(var->memBlock->value); /*gbloffbas + var->memBlockIndex; */
}
else if (arg->type == ARG_LOCAL){
argpp[n] = lclbas + var->memBlockIndex;
}
else if (arg->type == ARG_LABEL) {
argpp[n] = (MYFLT*)(opMemStart +
findLabelMemOffset(csound, tp, (char*)arg->argPtr));
}
else {
csound->Message(csound, Str("FIXME: instance unexpected arg: %d\n"),
arg->type);
}
}
}
/* VL 13-12-13: point the memory to the local ksmps & kr variables,
and initialise them */
CS_VARIABLE* var = csoundFindVariableWithName(csound,
ip->instr->varPool, "ksmps");
if (var) {
char* temp = (char*)(lclbas + var->memBlockIndex);
var->memBlock = (CS_VAR_MEM*)(temp - CS_VAR_TYPE_OFFSET);
var->memBlock->value = csound->ksmps;
}
var = csoundFindVariableWithName(csound, ip->instr->varPool, "kr");
if (var) {
char* temp = (char*)(lclbas + var->memBlockIndex);
var->memBlock = (CS_VAR_MEM*)(temp - CS_VAR_TYPE_OFFSET);
var->memBlock->value = csound->ekr;
}
if (UNLIKELY(nxtopds > opdslim))
csoundDie(csound, Str("inconsistent opds total"));
}
int prealloc_(CSOUND *csound, AOP *p, int instname)
{
int n, a;
if (instname)
n = (int) strarg2opcno(csound, ((STRINGDAT*)p->r)->data, 1,
(*p->b == FL(0.0) ? 0 : 1));
else {
if (csound->ISSTRCOD(*p->r))
n = (int) strarg2opcno(csound, get_arg_string(csound,*p->r), 1,
(*p->b == FL(0.0) ? 0 : 1));
else n = *p->r;
}
if (UNLIKELY(n == NOT_AN_INSTRUMENT)) return NOTOK;
if (csound->oparms->realtime)
csoundSpinLock(&csound->alloc_spinlock);
a = (int) *p->a - csound->engineState.instrtxtp[n]->active;
for ( ; a > 0; a--)
instance(csound, n);
if (csound->oparms->realtime)
csoundSpinUnLock(&csound->alloc_spinlock);
return OK;
}
int prealloc(CSOUND *csound, AOP *p){
return prealloc_(csound,p,0);
}
int prealloc_S(CSOUND *csound, AOP *p){
return prealloc_(csound,p,1);
}
int delete_instr(CSOUND *csound, DELETEIN *p)
{
int n;
INSTRTXT *ip;
INSDS *active;
INSTRTXT *txtp;
if (IS_STR_ARG(p->insno))
n = csound->strarg2insno(csound, ((STRINGDAT *)p->insno)->data, 1);
else
n = (int) (*p->insno + FL(0.5));
if (UNLIKELY(n == NOT_AN_INSTRUMENT ||
n > csound->engineState.maxinsno ||
csound->engineState.instrtxtp[n] == NULL))
return OK; /* Instrument does not exist so noop */
ip = csound->engineState.instrtxtp[n];
active = ip->instance;
while (active != NULL) { /* Check there are no active instances */
INSDS *nxt = active->nxtinstance;
if (UNLIKELY(active->actflg)) { /* Can only remove non-active instruments */
char *name = csound->engineState.instrtxtp[n]->insname;
if (name)
return csound->InitError(csound,
Str("Instrument %s is still active"), name);
else
return csound->InitError(csound,
Str("Instrument %d is still active"), n);
}
#if 0
if (active->opcod_iobufs && active->insno > csound->engineState.maxinsno)
csound->Free(csound, active->opcod_iobufs); /* IV - Nov 10 2002 */
#endif
if (active->fdchp != NULL)
fdchclose(csound, active);
if (active->auxchp != NULL)
auxchfree(csound, active);
free_instr_var_memory(csound, active);
csound->Free(csound, active);
active = nxt;
}
csound->engineState.instrtxtp[n] = NULL;
/* Now patch it out */
for (txtp = &(csound->engineState.instxtanchor);
txtp != NULL;
txtp = txtp->nxtinstxt)
if (txtp->nxtinstxt == ip) {
OPTXT *t = ip->nxtop;
txtp->nxtinstxt = ip->nxtinstxt;
while (t) {
OPTXT *s = t->nxtop;
csound->Free(csound, t);
t = s;
}
csound->Free(csound, ip);
return OK;
}
return NOTOK;
}
void killInstance_enqueue(CSOUND *csound, MYFLT instr, int insno,
INSDS *ip, int mode,
int allow_release);
void killInstance(CSOUND *csound, MYFLT instr, int insno, INSDS *ip,
int mode, int allow_release) {
INSDS *ip2 = NULL, *nip;
do { /* This loop does not terminate in mode=0 */
nip = ip->nxtact;
if (((mode & 8) && ip->offtim >= 0.0) ||
((mode & 4) && ip->p1.value != instr) ||
(allow_release && ip->relesing)) {
ip = nip;
continue;
}
if (!(mode & 3)) {
if (allow_release) {
xturnoff(csound, ip);
}
else {
nip = ip->nxtact;
xturnoff_now(csound, ip);
}
}
else {
ip2 = ip;
if ((mode & 3) == 1)
break;
}
ip = nip;
} while (ip != NULL && (int) ip->insno == insno);
if (ip2 != NULL) {
if (allow_release) {
xturnoff(csound, ip2);
}
else {
xturnoff_now(csound, ip2);
}
}
}
int csoundKillInstanceInternal(CSOUND *csound, MYFLT instr, char *instrName,
int mode, int allow_release, int async)
{
INSDS *ip;
int insno;
if (instrName) {
instr = named_instr_find(csound, instrName);
insno = (int) instr;
} else insno = instr;
if (UNLIKELY(insno < 1 || insno > (int) csound->engineState.maxinsno ||
csound->engineState.instrtxtp[insno] == NULL)) {
return CSOUND_ERROR;
}
if (UNLIKELY(mode < 0 || mode > 15 || (mode & 3) == 3)) {
csoundUnlockMutex(csound->API_lock);
return CSOUND_ERROR;
}
ip = &(csound->actanchor);
while ((ip = ip->nxtact) != NULL && (int) ip->insno != insno);
if (UNLIKELY(ip == NULL)) {
return CSOUND_ERROR;
}
if (!async) {
csoundLockMutex(csound->API_lock);
killInstance(csound, instr, insno, ip, mode, allow_release);
csoundUnlockMutex(csound->API_lock);
}
else
killInstance_enqueue(csound, instr, insno, ip, mode, allow_release);
return CSOUND_SUCCESS;
}
|