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 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122
#![allow(non_camel_case_types, non_upper_case_globals, non_snake_case)] use std::io; use std::marker::PhantomData; use std::mem; use std::cell::RefCell; use std::ops::{Deref, DerefMut}; use std::ptr; use std::slice; use callbacks::*; use channels::{ChannelBehavior, ChannelHints, ChannelInfo, PvsDataExt}; use csound_sys; use csound_sys::RTCLOCK; use enums::{ChannelData, ControlChannelType, Language, MessageType, Status}; use rtaudio::{CsAudioDevice, CsMidiDevice, RtAudioParams}; use std::ffi::{CStr, CString, NulError}; use std::str; use std::str::Utf8Error; use libc::{c_char, c_double, c_int, c_long, c_void}; // the length in bytes of the output type name in csound const OUTPUT_TYPE_LENGTH: usize = 6; // The length in bytes of the output format name in csound const OUTPUT_FORMAT_LENGTH: usize = 8; /// Struct with information about a csound opcode. /// /// Used to get the complete csound opcodes list, so the /// [`Csound::get_opcode_list_entry`](struct.Csound.html#method.get_opcode_list_entry) method will return /// a list of OpcodeListEntry, where each of this struct contain information relative /// a specific csound opcode. #[derive(Default, Debug)] pub struct OpcodeListEntry { /// The opcode name. pub opname: Option<String>, /// The opcode ouput type. pub outypes: Option<String>, /// The opcode input type. pub intypes: Option<String>, pub flags: i32, } #[derive(Default)] pub(crate) struct CallbackHandler<'c> { pub callbacks: Callbacks<'c>, } /// Opaque struct representing an csound object /// /// This is the main struct used to access the libcsound API functions. /// The Engine element is the inner representation of the CSOUND opaque pointer and is /// the object wich talk directly with the libcsound c library. /// #[derive(Debug)] pub struct Csound { /// Inner representation of the CSOUND opaque pointer pub(crate) engine: Inner, } /// Opaque struct representing a csound object #[derive(Debug)] pub(crate) struct Inner { pub(crate) csound: *mut csound_sys::CSOUND, use_msg_buffer: RefCell<bool>, } unsafe impl Send for Inner {} impl Default for Csound { fn default() -> Self { unsafe { // Csound must not handle signals csound_sys::csoundInitialize(csound_sys::CSOUNDINIT_NO_SIGNAL_HANDLER as c_int); csound_sys::csoundInitialize(csound_sys::CSOUNDINIT_NO_ATEXIT as c_int); let callback_handler = Box::new(CallbackHandler { callbacks: Callbacks::default(), }); let host_data_ptr = Box::into_raw(callback_handler) as *mut c_void; let csound_sys = csound_sys::csoundCreate(host_data_ptr); assert!(!csound_sys.is_null()); let engine = Inner { csound: csound_sys, use_msg_buffer: RefCell::new(false), }; Csound { engine } } } } impl Csound { /// Create a new csound object. /// /// This is the core of almost all operations in the csound library. /// A new instance of csound will created by this function, a custom callback handler will be used, /// This custom callback handler will be active only if the user calls some of the /// callbacks setting functions which receive a closure for a specific callback. /// /// # Example /// /// ``` /// // Creates a Csound instance and use a custom callback handler /// let csound = Csound::new(); /// // enable the message callback passing a closure to the custom callback handler /// csound.message_string_callback( |mtype:u32, message:&str| { /// println!("message type: {} message content: {}", mtype, message); /// }); /// csound.compile_csd(csd_filename).unwrap(); /// csound.start(); /// ``` pub fn new() -> Csound { Csound::default() } /// Initializes the csound library with specific flags(see: [anchor text]()). /// This function is called internally by Csound::new(), so there is generally no need to use it explicitly unless /// you need to avoid default initilization that sets signal handlers and atexit() callbacks. /// Return value is Ok() on success or an error message in case of failure pub fn initialize(flags: i32) -> Result<(), &'static str> { unsafe { match csound_sys::csoundInitialize(flags as c_int) as i32 { csound_sys::CSOUND_ERROR => Err("Can't to initialize csound "), csound_sys::CSOUND_SUCCESS => Ok(()), value => { if value > 0 { Err("Initialization was done already") } else { Err("Unknown error - can to initialize") } } } } } /// Sets a single csound option(flag). /// /// NB: blank spaces are not allowed. /// # Returns /// returns Ok on success or an error message in case the option is invalid. pub fn set_option(&self, option: &str) -> Result<(), &'static str> { let op = CString::new(option).map_err(|_| "Error parsing the string")?; unsafe { match csound_sys::csoundSetOption(self.engine.csound, op.as_ptr()) { csound_sys::CSOUND_SUCCESS => Ok(()), _ => Err("Options not valid"), } } } /// Prepares Csound for performance. /// /// Normally called after compiling a csd file or an orc file, in which case score preprocessing is performed and /// performance terminates when the score terminates. /// However, if called before compiling a csd file or an orc file, /// score preprocessing is not performed and "i" statements are dispatched as real-time events, /// the <CsOptions> tag is ignored, and performance continues indefinitely or until ended using the API. /// # Example /// /// ``` /// let csound = Csound::new(); /// csound.compile_csd(csd_filename).unwrap(); /// csound.start(); /// ... /// ``` /// pub fn start(&self) -> Result<(), &'static str> { unsafe { if csound_sys::csoundStart(self.engine.csound) == csound_sys::CSOUND_SUCCESS { Ok(()) } else { Err("Csound is already started, call reset() before starting again.") } } } /// Returns the version number times 1000 /// for example, if the current csound version is 6.12.0 /// this function will return 6120. pub fn version(&self) -> u32 { unsafe { csound_sys::csoundGetVersion() as u32 } } /// Returns the API version number times 100 pub fn api_version(&self) -> u32 { unsafe { csound_sys::csoundGetAPIVersion() as u32 } } /* Engine performance functions implementations ********************************************************* */ /// Stops the performance of a csound's instance /// *Note*: It is not guaranteed that [`Csound::perform`](struct.Csound.html#method.perform) has already stopped when this function returns. pub fn stop(&self) { unsafe { csound_sys::csoundStop(self.engine.csound); } } /// Resets all internal memory and state in preparation for a new performance. /// Enables external software to run successive Csound performances without reloading Csound. pub fn reset(&self) { unsafe { csound_sys::csoundReset(self.engine.csound); } } /// Compiles Csound input files (such as an orchestra and score, or CSD) as directed by the supplied command-line arguments , but does not perform them. /// This function cannot be called during performance, and before a repeated call, csoundReset() needs to be called. /// # Arguments /// * `args` A slice containing the arguments to be passed to csound /// # Returns /// A error message in case of failure pub fn compile<T>(&self, args: &[T]) -> Result<(), &'static str> where T: AsRef<str>, { if args.is_empty() { return Err("Not enough arguments"); } let arguments: Vec<CString> = args .iter() .map(|arg| CString::new(arg.as_ref())) .filter_map(Result::ok) .collect(); let args_raw: Vec<*const c_char> = arguments.iter().map(|arg| arg.as_ptr()).collect(); let argv: *const *const c_char = args_raw.as_ptr(); unsafe { match csound_sys::csoundCompile(self.engine.csound, args_raw.len() as c_int, argv) { csound_sys::CSOUND_SUCCESS => Ok(()), _ => Err("Can't compile carguments"), } } } /// Compiles a Csound input file (CSD, .csd file), but does not perform it. /// If [`Csound::start`](struct.Csound.html#method.start) is called before `compile_csd`, the <CsOptions> element is ignored /// (but set_option can be called any number of times), /// the <CsScore> element is not pre-processed, but dispatched as real-time events; /// and performance continues indefinitely, or until ended by calling [`Csound::stop`](struct.Csound.html#method.stop) or some other logic. /// In this "real-time" mode, the sequence of calls should be: /// ``` /// let csound = Csound::new(); /// csound.set_option("-an_option"); /// csound.set_option("-another_option"); /// csound.start(); /// csound.compile_csd(csd_filename); /// while true{ /// // Send realtime events /// csound.send_score_event("i 1 0 5 4.5 6.2"); /// //... /// // some logic to break the loop after a performance of realtime events /// } /// ``` /// *Note*: this function can be called repeatedly during performance to replace or add new instruments and events. /// But if csoundCompileCsd is called before csoundStart, the <CsOptions> element is used,the <CsScore> section is pre-processed and dispatched normally, /// and performance terminates when the score terminates, or [`Csound::stop`](struct.Csound.html#method.stop) is called. /// In this "non-real-time" mode (which can still output real-time audio and handle real-time events), the sequence of calls should be: /// ``` /// let csound = Csound::new(); /// csound.compile_csd(csd_filename); /// csound.start(); /// while !csound.perform_ksmps() { /// } /// ``` /// # Arguments /// * `csd` A reference to .csd file name pub fn compile_csd<T>(&self, csd: T) -> Result<(), &'static str> where T: AsRef<str>, { let path = Trampoline::convert_str_to_c(csd)?; unsafe { match csound_sys::csoundCompileCsd(self.engine.csound, path.as_ptr()) { csound_sys::CSOUND_SUCCESS => Ok(()), _ => Err("Can't compile the csd file"), } } } /// Behaves the same way as [`Csound::compile_csd`](struct.Csound.html#method.compile_csd), /// except that the content of the CSD is read from a string rather than from a file. /// This is convenient when it is desirable to package the csd as part of an application or a multi-language piece. /// # Arguments /// * `csd_text` A reference to the text to be compiled by csound pub fn compile_csd_text<T>(&self, csdText: T) -> Result<(), &'static str> where T: AsRef<str>, { let path = Trampoline::convert_str_to_c(csdText)?; unsafe { match csound_sys::csoundCompileCsdText(self.engine.csound, path.as_ptr()) { csound_sys::CSOUND_SUCCESS => Ok(()), _ => Err("Can't compile the csd file"), } } } /// Parses and compiles the given orchestra from an ASCII string, also evaluating any global space code (i-time only) /// this can be called during performance to compile a new orchestra. /// ``` /// let csound = Csound::new(); /// let orc_code = "instr 1 /// a1 rand 0dbfs/4 /// out a1"; /// csound.compile_orc(orc_code); /// ``` /// # Arguments /// * `orcPath` A reference to orchestra strings pub fn compile_orc<T>(&self, orc: T) -> Result<(), &'static str> where T: AsRef<str>, { let path = Trampoline::convert_str_to_c(orc)?; unsafe { match csound_sys::csoundCompileOrc(self.engine.csound, path.as_ptr()) { csound_sys::CSOUND_SUCCESS => Ok(()), _ => Err("Can't to compile orc file"), } } } /// Async version of [`Csound::compile_orc`](struct.Csound.html#method.compile_orc). The code is parsed and compiled, /// then placed on a queue for asynchronous merge into the running engine, and evaluation. /// The function returns following parsing and compilation. /// # Arguments /// * `orc` A reference to an csound's orchestra definitions pub fn compile_orc_async<T>(&self, orc: T) -> Result<(), &'static str> where T: AsRef<str>, { let path = Trampoline::convert_str_to_c(orc)?; unsafe { match csound_sys::csoundCompileOrcAsync(self.engine.csound, path.as_ptr()) { csound_sys::CSOUND_SUCCESS => Ok(()), _ => Err("Can't to compile orc file"), } } } /// Parse and compile an orchestra given on a string, /// evaluating any global space code (i-time only). /// # Returns /// On SUCCESS it returns a value passed to the /// 'return' opcode in global space. /// code = "i1 = 2 + 2 \n return i1 \n" /// retval = csound.eval_code(code) pub fn eval_code<T>(&self, code: T) -> Result<f64, &'static str> where T: AsRef<str>, { let cd = Trampoline::convert_str_to_c(code)?; unsafe { Ok(csound_sys::csoundEvalCode(self.engine.csound, cd.as_ptr() as _)) } } // TODO Implement csoundCompileTree functions /// Senses input events and performs audio output. /// /// perform until: 1. the end of score is reached (positive return value), 2. an error occurs (negative return value), /// or 3. performance is stopped by calling *stop()* from another thread (zero return value). /// Note that some csf file, text or score have to be compiled first and then *start()* must be called. /// In the case of zero return value, *perform()* can be called again to continue the stopped performance. /// Otherwise, [`Csound::reset`](struct.Csound.html#method.reset) should be called to clean up after the finished or failed performance. pub fn perform(&self) -> i32 { unsafe { csound_sys::csoundPerform(self.engine.csound) as i32 } } /// Senses input events, and performs one control sample worth ```ksmps * number of channels * size_off::<f64> bytes``` of audio output. /// /// Note that some csd file, text or score have to be compiled first and then [`Csound::start`](struct.Csound.html#method.start). /// Enables external software to control the execution of Csound, and to synchronize /// performance with audio input and output(see: [`Csound::read_spin_buffer`](struct.Csound.html#method.read_spin_buffer), [`Csound::read_spout_buffer`](struct.Csound.html#method.read_spout_buffer)) /// # Returns /// *false* during performance, and true when performance has finished. If called until it returns *true*, will perform an entire score. pub fn perform_ksmps(&self) -> bool { unsafe { csound_sys::csoundPerformKsmps(self.engine.csound) != 0 } } /// Performs Csound, sensing real-time and score events and processing one buffer's worth (-b frames) of interleaved audio. /// Note that some csd file, text or score have to be compiled first and then [`Csound::start`](struct.Csound.html#method.start), /// you could call [`Csound::read_output_buffer`](struct.Csound.html#method.start) or /// [`Csound::write_input_buffer`](struct.Csound.html#method.write_input_buffer) to write/read the csound's I/O buffers content. /// #Returns /// *false* during performance or *true* when performance has finished. pub fn perform_buffer(&self) -> bool { unsafe { csound_sys::csoundPerformBuffer(self.engine.csound) != 0 } } /*********************************** UDP ****************************************************/ /// Starts the UDP server /// /// # Arguments /// * `port` The server port number. /// # Returns /// *Ok* on success or an error code on failure. pub fn udp_server_start(&self, port: u32) -> Result<(), Status> { unsafe { match Status::from( csound_sys::csoundUDPServerStart(self.engine.csound, port as c_int) as i32, ) { Status::CS_SUCCESS => Ok(()), e => Err(e), } } } /// # Returns /// The port number on which the server is running, or None if the server has not been started. pub fn udp_server_status(&self) -> Option<u32> { unsafe { let status = csound_sys::csoundUDPServerStatus(self.engine.csound); if status == csound_sys::CSOUND_ERROR { None } else { Some(status as u32) } } } /// Closes the UDP server /// # Returns /// *Ok* if the running server was successfully closed, Status code otherwise. pub fn udp_server_close(&self) -> Result<(), Status> { unsafe { match Status::from(csound_sys::csoundUDPServerClose(self.engine.csound) as i32) { Status::CS_SUCCESS => Ok(()), status => Err(status), } } } /// Turns on the transmission of console messages /// /// # Arguments /// * `addr` The UDP server destination address. /// * `port` The UDP server port number. /// * `mirror` If it is true, the messages will continue to be sent to the usual destination /// (see [`Csound::message_string_callback`](struct.Csound.html#method.message_string_callback) ) as well as to UDP. /// # Returns /// *Ok* on success or an Status code if the UDP transmission could not be set up. pub fn udp_console(&self, addr: &str, port: u32, mirror: bool) -> Result<(), Status> { unsafe { let ip = CString::new(addr).map_err(|_e| Status::CS_ERROR)?; if csound_sys::csoundUDPConsole( self.engine.csound, ip.as_ptr(), port as c_int, mirror as c_int, ) == csound_sys::CSOUND_SUCCESS { return Ok(()); } Err(Status::CS_ERROR) } } /// Stop transmitting console messages via UDP pub fn udp_stop_console(&self) { unsafe { csound_sys::csoundStopUDPConsole(self.engine.csound); } } /* Engine Attributes functions implmentations ********************************************************* */ /// # Returns /// The number of audio sample frames per second. pub fn get_sample_rate(&self) -> f64 { unsafe { csound_sys::csoundGetSr(self.engine.csound) as f64 } } /// # Returns /// The number of control samples per second. pub fn get_control_rate(&self) -> f64 { unsafe { csound_sys::csoundGetKr(self.engine.csound) as f64 } } /// # Returns /// The number of audio sample frames per control sample. pub fn get_ksmps(&self) -> u32 { unsafe { csound_sys::csoundGetKsmps(self.engine.csound) } } /// # Returns /// The number of audio output channels. Set through the nchnls header variable in the csd file. pub fn output_channels(&self) -> u32 { unsafe { csound_sys::csoundGetNchnls(self.engine.csound) as u32 } } /// # Returns /// The number of audio input channels. /// Set through the **nchnls_i** header variable in the csd file. /// If this variable is not set, the value is taken from nchnls. pub fn input_channels(&self) -> u32 { unsafe { csound_sys::csoundGetNchnlsInput(self.engine.csound) as u32 } } /// # Returns /// The 0dBFS level of the spin/spout buffers. pub fn get_0dBFS(&self) -> f64 { unsafe { csound_sys::csoundGet0dBFS(self.engine.csound) as f64 } } /// # Returns /// The A4 frequency reference pub fn get_freq(&self) -> f64 { unsafe { csound_sys::csoundGetA4(self.engine.csound) as f64 } } /// #Returns /// The current performance time in samples pub fn get_current_sample_time(&self) -> usize { unsafe { csound_sys::csoundGetCurrentTimeSamples(self.engine.csound) as usize } } /// # Returns /// The size of MYFLT in bytes. pub fn get_size_myflt(&self) -> u32 { unsafe { csound_sys::csoundGetSizeOfMYFLT() as u32 } } /// # Returns /// Whether Csound is set to print debug messages. /// sents through the *DebugMsg()* csouns's internal API function. /// Anything different to 0 means true. pub fn get_debug_level(&self) -> u32 { unsafe { csound_sys::csoundGetDebug(self.engine.csound) as u32 } } /// Sets whether Csound prints debug messages from the *DebugMsg()* csouns's internal API function. /// # Arguments /// * `level` The debug level to assign, anything different to 0 means true. pub fn set_debug_level(&self, level: i32) { unsafe { csound_sys::csoundSetDebug(self.engine.csound, level as c_int); } } /* Engine general InputOutput functions implmentations ********************************************************* */ /// Gets the csound's input source name if it has been defined /// otherwise, None is returned pub fn get_input_name(&self) -> Option<String> { unsafe { let ptr = csound_sys::csoundGetInputName(self.engine.csound); Trampoline::ptr_to_string(ptr) } } /// Gets output device name if the realtime output has been defined, /// Otherwise, None is returned pub fn get_output_name(&self) -> Option<String> { unsafe { let ptr = csound_sys::csoundGetOutputName(self.engine.csound); Trampoline::ptr_to_string(ptr) } } /// Set output destination, type and format /// # Arguments /// * `name` The destination/device name, for RT audio use the field [`CsAudioDevice::device_id`](struct.CsAudioDevice.html#field.device_id). /// (see: [`Csound::get_audio_devices`](struct.Csound.html#method.get_audio_devices)) /// * `out_type` can be one of "wav","aiff", "au","raw", "paf", "svx", "nist", "voc", "ircam","w64","mat4", "mat5", "pvf","xi", "htk","sds","avr", /// "wavex","sd2", "flac", "caf","wve","ogg","mpc2k","rf64", or NULL (use default or realtime IO). /// * `format` can be one of "alaw", "schar", "uchar", "float", "double", "long", "short", "ulaw", "24bit", "vorbis", or NULL (use default or realtime IO). pub fn set_output(&self, name: &str, out_type: &str, format: &str) -> Result<(), NulError> { unsafe { let devName = CString::new(name)?; let devType = CString::new(out_type)?; let devFormat = CString::new(format)?; csound_sys::csoundSetOutput( self.engine.csound, devName.as_ptr(), devType.as_ptr(), devFormat.as_ptr(), ); Ok(()) } } /// Get output type and format. /// # Example /// ``` /// let csound = Csound::new(); /// let (output_type, output_format) = csound.get_output_format().unwrap(); /// ``` pub fn get_output_format(&self) -> Result<(String, String), Utf8Error> { let otype = vec![b'\0'; OUTPUT_TYPE_LENGTH]; let format = vec![b'\0'; OUTPUT_FORMAT_LENGTH]; unsafe { let otype = CString::from_vec_unchecked(otype).into_raw(); let format = CString::from_vec_unchecked(format).into_raw(); csound_sys::csoundGetOutputFormat(self.engine.csound, otype, format); let otype = CString::from_raw(otype); let otype = otype.to_str()?; let format = CString::from_raw(format); let format = format.to_str()?; Ok((otype.into(), format.into())) } } /// Sets input source /// # Arguments /// * `name` The source device name. pub fn set_input(&self, name: &str) -> Result<(), NulError> { unsafe { let devName = CString::new(name)?; csound_sys::csoundSetInput(self.engine.csound, devName.as_ptr()); Ok(()) } } /// Set MIDI file input name pub fn set_midi_file_input(&self, name: &str) -> Result<(), NulError> { unsafe { let devName = CString::new(name)?; csound_sys::csoundSetMIDIFileInput(self.engine.csound, devName.as_ptr()); Ok(()) } } /// Set MIDI file output name pub fn set_midi_file_output(&self, name: &str) -> Result<(), NulError> { unsafe { let devName = CString::new(name)?; csound_sys::csoundSetMIDIFileOutput(self.engine.csound, devName.as_ptr()); Ok(()) } } /// Set MIDI input device name/number pub fn set_midi_input(&self, name: &str) -> Result<(), NulError> { unsafe { let devName = CString::new(name)?; csound_sys::csoundSetMIDIInput(self.engine.csound, devName.as_ptr()); Ok(()) } } /// Set MIDI output device name pub fn set_midi_output(&self, name: &str) -> Result<(), NulError> { unsafe { let devName = CString::new(name)?; csound_sys::csoundSetMIDIOutput(self.engine.csound, devName.as_ptr()); Ok(()) } } /* Engine general Realtime Audio I/O functions implmentations ********************************************************* */ /// Sets the current RT audio module pub fn set_rt_audio_module(&self, name: &str) -> Result<(), NulError> { unsafe { let devName = CString::new(name)?; csound_sys::csoundSetRTAudioModule(self.engine.csound, devName.as_ptr()); Ok(()) } } /// # Returns /// The number of samples in Csound's input buffer. pub fn get_input_buffer_size(&self) -> usize { unsafe { csound_sys::csoundGetInputBufferSize(self.engine.csound) as usize } } /// # Returns /// The number of samples in Csound's input buffer. pub fn get_output_buffer_size(&self) -> usize { unsafe { csound_sys::csoundGetOutputBufferSize(self.engine.csound) as usize } } /// Gets the csound's input buffer. /// # Returns /// An Option containing either the [`BufferPtr`](struct.BufferPtr.html) or None if the /// csound's input buffer has not been initialized. The returned *BufferPtr* is Writable, it means that you can modify /// the csound's buffer content in order to write external audio data into csound and process it. /// # Example /// ``` /// let csound = Csound::new(); /// csound.compile_csd("some_file_path"); /// csound.start(); /// let input_buffer_ptr = csound.get_input_buffer(); /// while !csound.perform_buffer() { /// // fills your buffer with audio samples that you want to pass into csound /// foo_fill_buffer(input_buffer_ptr.as_mut_slice()); /// // ... /// } /// ``` pub fn get_input_buffer(&self) -> Option<BufferPtr<Writable>> { unsafe { let ptr = csound_sys::csoundGetInputBuffer(self.engine.csound) as *mut f64; let len = self.get_input_buffer_size(); if !ptr.is_null() { return Some(BufferPtr { ptr, len, phantom: PhantomData, }); } None } } /// Gets the csound's output buffer. /// # Returns /// An Option containing either the [`BufferPtr`](struct.BufferPtr.html) or None if the /// csound's output buffer has not been initialized. The returned *BufferPtr* is only Readable. /// # Example /// ``` /// let csound = Csound::new(); /// csound.compile_csd("some_file_path"); /// csound.start(); /// let output_buffer_ptr = csound.get_output_buffer(); /// let mut data = vec![0f64; input_buffer_ptr.get_size()]; /// while !csound.perform_buffer() { /// // process the data from csound /// foo_process_buffer(output_buffer_ptr.as_slice()); /// } /// ``` pub fn get_output_buffer(&self) -> Option<BufferPtr<Readable>> { unsafe { let ptr = csound_sys::csoundGetOutputBuffer(self.engine.csound) as *mut f64; let len = self.get_output_buffer_size(); if !ptr.is_null() { return Some(BufferPtr { ptr, len, phantom: PhantomData, }); } None } } /// Enables external software to write audio into Csound before calling perform_ksmps. /// # Returns /// An Option containing either the [`BufferPtr`](struct.BufferPtr.html) or None if the /// csound's spin buffer has not been initialized. The returned *BufferPtr* is Writable. /// # Example /// ``` /// let csound = Csound::new(); /// csound.compile_csd("some_file_path"); /// csound.start(); /// let spin = csound.get_spin(); /// while !csound.perform_ksmps() { /// // fills the spin buffer with audio samples that you want to pass into csound /// foo_fill_buffer(spin.as_mut_slice()); /// // ... /// } /// ``` pub fn get_spin(&self) -> Option<BufferPtr<Writable>> { unsafe { let ptr = csound_sys::csoundGetSpin(self.engine.csound) as *mut f64; let len = (self.get_ksmps() * self.input_channels()) as usize; if !ptr.is_null() { return Some(BufferPtr { ptr, len, phantom: PhantomData, }); } None } } /// Enables external software to read audio from Csound before calling perform_ksmps. /// # Returns /// An Option containing either the [`BufferPtr`](struct.BufferPtr.html) or None if the /// csound's spout buffer has not been initialized. The returned *BufferPtr* is only Readable. /// # Example /// ``` /// let csound = Csound::new(); /// csound.compile_csd("some_file_path"); /// csound.start(); /// let spout = csound.get_spout(); /// while !csound.perform_ksmps() { /// // Deref the spout pointer and read its content /// foo_read_buffer(&*spout); /// // ... /// } /// ``` pub fn get_spout(&self) -> Option<BufferPtr<Readable>> { unsafe { let ptr = csound_sys::csoundGetSpout(self.engine.csound) as *mut f64; let len = (self.get_ksmps() * self.output_channels()) as usize; if !ptr.is_null() { return Some(BufferPtr { ptr, len, phantom: PhantomData, }); } None } } /// Method used when you want to copy audio samples from the csound's output buffer. /// # Arguments /// * `out` a reference to a mutable slice where the Csound's output buffer content /// will be copied. This buffer have to has enough memory for at least /// [`Csound::get_output_buffer_size`](struct.Csound.html#method.get_output_buffer_size), samples. /// # Returns /// The number of samples copied into the slice on success, or an /// error message if the internal csound's buffer has not been initialized. /// # Example /// ``` /// let csound = Csound::new(); /// csound.compile_csd("some_file_path"); /// csound.start(); /// let output_buffer_length = csound.get_output_buffer_size(); /// let mut output_buffer = vec![0f64; output_buffer_length]; /// while !csound.perform_buffer() { /// csound.read_output_buffer(&mut output_buffer).unwrap(); /// // ... do some stuff with the buffer /// } /// ``` /// # Deprecated /// Use [`Csound::get_output_buffer`](struct.Csound.html#method.get_output_buffer) to get a [`BufferPtr`](struct.BufferPtr.html) /// object. #[deprecated(since="0.1.5", note="please use Csound::get_output_buffer object instead")] pub fn read_output_buffer(&self, output: &mut [f64]) -> Result<usize, &'static str> { let size = self.get_output_buffer_size(); let obuffer = unsafe { csound_sys::csoundGetOutputBuffer(self.engine.csound) as *const f64 }; let mut len = output.len(); if size < len { len = size; } if !obuffer.is_null() { unsafe { std::ptr::copy(obuffer, output.as_ptr() as *mut f64, len); return Ok(len); } } Err("The output buffer is not initialized, call the 'compile()' and 'start()' methods.") } /// Method used when you want to copy custom audio samples into the csound buffer to be processed. /// # Arguments /// * `input` a reference to a slice with samples which will be copied to /// the Csound's input buffer. /// # Returns /// The number of samples copied into the csound's input buffer or an /// error message if the internal csound's buffer has not been initialized. /// # Example /// ``` /// let csound = Csound::new(); /// csound.compile_csd("some_file_path"); /// csound.start(); /// let input_buffer_length = csound.get_input_buffer_size(); /// let mut input_buffer = vec![0f64; output_buffer_length]; /// while !csound.perform_buffer() { /// // fills your buffer with audio samples you want to pass into csound /// foo_fill_buffer(&mut input_buffer); /// csound.write_input_buffer(&input_buffer); /// // ... /// } /// ``` /// # Deprecated /// Use [`Csound::get_input_buffer`](struct.Csound.html#method.get_input_buffer) to get a [`BufferPtr`](struct.BufferPtr.html) /// object. #[deprecated(since="0.1.5", note="please use Csound::get_input_buffer object instead")] pub fn write_input_buffer(&self, input: &[f64]) -> Result<usize, &'static str> { let size = self.get_input_buffer_size(); let ibuffer = unsafe { csound_sys::csoundGetInputBuffer(self.engine.csound) as *mut f64 }; let mut len = input.len(); if size < len { len = size; } if !ibuffer.is_null() { unsafe { std::ptr::copy(input.as_ptr(), ibuffer, len); return Ok(len); } } Err("The input buffer is not initialized, call the 'compile()' and 'start()' methods.") } /// Enables external software to read audio from Csound after calling [`Csound::perform_ksmps`](struct.Csound.html#method.perform_ksmps) /// # Returns /// The number of samples copied or an /// error message if the internal csound's buffer has not been initialized. /// # Example /// ``` /// let csound = Csound::new(); /// csound.compile_csd("some_file_path"); /// csound.start(); /// let spout_length = csound.get_ksmps() * csound.output_channels(); /// let mut spout_buffer = vec![0f64; spout_length as usize]; /// while !csound.perform_ksmps() { /// // fills your buffer with audio samples you want to pass into csound /// foo_fill_buffer(&mut spout_buffer); /// csound.read_spout_buffer(&spout_buffer); /// // ... /// } /// ``` /// # Deprecated /// Use [`Csound::get_spout`](struct.Csound.html#method.get_spout) to get a [`BufferPtr`](struct.BufferPtr.html) /// object. #[deprecated(since="0.1.5", note="please use Csound::get_spout object instead")] pub fn read_spout_buffer(&self, output: &mut [f64]) -> Result<usize, &'static str> { let size = self.get_ksmps() as usize * self.output_channels() as usize; let spout = unsafe { csound_sys::csoundGetSpout(self.engine.csound) as *const f64 }; let mut len = output.len(); if size < len { len = size; } if !spout.is_null() { unsafe { std::ptr::copy(spout, output.as_mut_ptr(), len); return Ok(len); } } Err("The spout buffer is not initialized, call the 'compile()' and 'start()' methods.") } /// Enables external software to write audio into Csound before calling [`Csound::perform_ksmps`](struct.Csound.html#method.perform_ksmps) /// [`Csound::get_ksmps`](struct.Csound.html#method.get_ksmps) * [`Csound::input_channels`](struct.Csound.html#method.input_channels). /// # Returns /// The number of samples copied or an /// error message if the internal csound's buffer has not been initialized. /// # Example /// ``` /// let csound = Csound::new(); /// csound.compile_csd("some_file_path"); /// csound.start(); /// let spin_length = csound.get_ksmps() * csound.input_channels(); /// let mut spin_buffer = vec![0f64; spin_length as usize]; /// while !csound.perform_ksmps() { /// // fills your buffer with audio samples you want to pass into csound /// foo_fill_buffer(&mut spin_buffer); /// csound.write_spin_buffer(&spin_buffer); /// // ... /// } /// ``` /// # Deprecated /// Use [`Csound::get_spin`](struct.Csound.html#method.get_spin) to get a [`BufferPtr`](struct.BufferPtr.html) /// object. #[deprecated(since="0.1.5", note="please use Csound::get_spin object instead")] pub fn write_spin_buffer(&self, input: &[f64]) -> Result<usize, &'static str> { let size = self.get_ksmps() as usize * self.input_channels() as usize; let spin = unsafe { csound_sys::csoundGetSpin(self.engine.csound) as *mut f64 }; let mut len = input.len(); if size < len { len = size; } if !spin.is_null() { unsafe { std::ptr::copy(input.as_ptr(), spin, len); return Ok(len); } } Err("The spin buffer is not initialized, call the 'compile()' and 'start()' methods.") } /// Clears the spin buffer. pub fn clear_spin(&self) { unsafe { csound_sys::csoundClearSpin(self.engine.csound); } } /// Adds the indicated sample into the audio input working buffer (spin); /// this only ever makes sense before calling [`Csound::perform_ksmps`](struct.Csound.html#method.perform_ksmps). /// The frame and channel must be in bounds relative to ksmps and nchnls. /// *Note*: the spin buffer needs to be cleared at every k-cycle by calling [`Csound::clear_spin`](struct.Csound.html#method.clear_spin). pub fn add_spin_sample(&self, frame: u32, channel: u32, sample: f64) { unsafe { csound_sys::csoundAddSpinSample( self.engine.csound, frame as i32, channel as i32, sample as c_double, ); } } /// Sets the audio input working buffer (spin) to the indicated sample. /// this only ever makes sense before calling [`Csound::perform_ksmps`](struct.Csound.html#method.perform_ksmps). /// The frame and channel must be in bounds relative to ksmps and nchnls. pub fn set_spin_sample(&self, frame: u32, channel: u32, sample: f64) { unsafe { csound_sys::csoundSetSpinSample( self.engine.csound, frame as i32, channel as i32, sample as c_double, ); } } /// Gets an audio sample from the spout buffer. /// only ever makes sense before calling [`Csound::perform_ksmps`](struct.Csound.html#method.perform_ksmps). /// The frame and channel must be in bounds relative to ksmps and nchnls. /// #Returns /// The indicated sample from the Csound audio output working buffer (spout). pub fn get_spout_sample(&self, frame: u32, channel: u32) -> f64 { unsafe { csound_sys::csoundGetSpoutSample(self.engine.csound, frame as i32, channel as i32) as f64 } } /// Enable to host to handle the audio implementation. /// Calling this function with a non-zero 'state' value between [`Csound::create`](struct.Csound.html#method.create) and the start of performance will disable /// all default handling of sound I/O by the Csound library, /// allowing the host application to use the *spin*,*spout*,*input*, *output* buffers directly. /// # Arguments /// * `state` An no zero value will diseable all default handling of sound I/O in csound. /// * `bufSize` For applications using *spin* / *spout*, this argument should be set to 0 but if *bufSize* is greater than zero, the buffer size (-b) in frames will be set to the integer /// multiple of ksmps that is nearest to the value specified. pub fn set_host_implemented_audioIO(&self, state: u32, bufSize: u32) { unsafe { csound_sys::csoundSetHostImplementedAudioIO( self.engine.csound, state as c_int, bufSize as c_int, ); } } /// This function can be called to obtain a list of available input and output audio devices. /// # Returns /// A tuple, being input devices the first element in the returned tuple, output devices the /// second one. pub fn get_audio_devices(&self) -> (Vec<CsAudioDevice>, Vec<CsAudioDevice>) { let mut input_devices = Vec::new(); let mut output_devices = Vec::new(); unsafe { let num_of_idevices = csound_sys::csoundGetAudioDevList(self.engine.csound, ptr::null_mut(), 0); let num_of_odevices = csound_sys::csoundGetAudioDevList(self.engine.csound, ptr::null_mut(), 0); let mut in_vec = vec![csound_sys::CS_AUDIODEVICE::default(); num_of_idevices as usize]; let mut out_vec = vec![csound_sys::CS_AUDIODEVICE::default(); num_of_odevices as usize]; csound_sys::csoundGetAudioDevList(self.engine.csound, in_vec.as_mut_ptr(), 0); csound_sys::csoundGetAudioDevList(self.engine.csound, out_vec.as_mut_ptr(), 1); for dev in &in_vec { input_devices.push(CsAudioDevice { device_name: Trampoline::ptr_to_string(dev.device_name.as_ptr()), device_id: Trampoline::ptr_to_string(dev.device_id.as_ptr()), rt_module: Trampoline::ptr_to_string(dev.rt_module.as_ptr()), max_nchnls: dev.max_nchnls as u32, isOutput: 0, }); } for dev in &out_vec { output_devices.push(CsAudioDevice { device_name: Trampoline::ptr_to_string(dev.device_name.as_ptr()), device_id: Trampoline::ptr_to_string(dev.device_id.as_ptr()), rt_module: Trampoline::ptr_to_string(dev.rt_module.as_ptr()), max_nchnls: dev.max_nchnls as u32, isOutput: 1, }); } } (input_devices, output_devices) } /* Real time MIDI IO functions implementations *************************************************************** */ /// Sets the current MIDI IO module pub fn set_midi_module(&self, name: &str) { unsafe { let devName = CString::new(name); if devName.is_ok() { csound_sys::csoundSetMIDIModule(self.engine.csound, devName.unwrap().as_ptr()); } } } /// call this function with state 1 if the host is going to implement MIDI through the callbacks pub fn set_host_implemented_midiIO(&self, state: u32) { unsafe { csound_sys::csoundSetHostImplementedMIDIIO(self.engine.csound, state as c_int); } } /// This function can be called to obtain a list of available input or output midi devices. /// # Returns /// A tuple with two vectors, beign the first one for input MIDI /// devices and the second one for output MIDI devices pub fn get_midi_devices(&self) -> (Vec<CsMidiDevice>, Vec<CsMidiDevice>) { let mut input_devices = Vec::new(); let mut output_devices = Vec::new(); unsafe { let num_of_idevices = csound_sys::csoundGetMIDIDevList(self.engine.csound, ptr::null_mut(), 0); let num_of_odevices = csound_sys::csoundGetMIDIDevList(self.engine.csound, ptr::null_mut(), 0); let mut in_vec = vec![csound_sys::CS_MIDIDEVICE::default(); num_of_idevices as usize]; let mut out_vec = vec![csound_sys::CS_MIDIDEVICE::default(); num_of_odevices as usize]; csound_sys::csoundGetMIDIDevList(self.engine.csound, in_vec.as_mut_ptr(), 0); csound_sys::csoundGetMIDIDevList(self.engine.csound, out_vec.as_mut_ptr(), 1); for dev in &in_vec { input_devices.push(CsMidiDevice { device_name: Trampoline::ptr_to_string(dev.device_name.as_ptr()), device_id: Trampoline::ptr_to_string(dev.device_id.as_ptr()), midi_module: Trampoline::ptr_to_string(dev.midi_module.as_ptr()), interface_name: Trampoline::ptr_to_string(dev.interface_name.as_ptr()), isOutput: 0, }); } for dev in &out_vec { output_devices.push(CsMidiDevice { device_name: Trampoline::ptr_to_string(dev.device_name.as_ptr()), device_id: Trampoline::ptr_to_string(dev.device_id.as_ptr()), midi_module: Trampoline::ptr_to_string(dev.midi_module.as_ptr()), interface_name: Trampoline::ptr_to_string(dev.interface_name.as_ptr()), isOutput: 1, }); } } (input_devices, output_devices) } /* Score Handling functions implmentations ********************************************************* */ /// Reads, preprocesses, and loads a score from an ASCII string. /// It can be called repeatedly with the new score events being added to the currently scheduled ones. pub fn read_score(&self, score: &str) -> Result<(), &'static str> { unsafe { let s = Trampoline::convert_str_to_c(score)?; if csound_sys::csoundReadScore(self.engine.csound, s.as_ptr()) == csound_sys::CSOUND_SUCCESS { return Ok(()); } Err("Invalid score") } } /// Asynchronous version of [`Csound::read_score`](struct.Csound.html#method.read_score) pub fn read_score_async(&self, score: &str) -> Result<(), &'static str> { unsafe { let s = Trampoline::convert_str_to_c(score)?; csound_sys::csoundReadScoreAsync(self.engine.csound, s.as_ptr()); Ok(()) } } /// # Returns /// The current score time in seconds since the beginning of the performance. pub fn get_score_time(&self) -> f64 { unsafe { csound_sys::csoundGetScoreTime(self.engine.csound) as f64 } } /// Sets whether Csound score events are performed or not. /// Independently of real-time MIDI events (see [`Csound::set_score_pending`](struct.Csound.html#method.set_score_pending)). pub fn is_score_pending(&self) -> i32 { unsafe { csound_sys::csoundIsScorePending(self.engine.csound) as i32 } } /// Sets whether Csound score events are performed or not (real-time events will continue to be performed). /// Can be used by external software, such as a VST host, to turn off performance of score events (while continuing to perform real-time events), /// for example to mute a Csound score while working on other tracks of a piece, or to play the Csound instruments live. pub fn set_score_pending(&self, pending: i32) { unsafe { csound_sys::csoundSetScorePending(self.engine.csound, pending as c_int); } } /// Gets the current score's time. /// # Returns /// The score time beginning at which score events will actually immediately be performed /// (see [`Csound::set_score_offset_seconds`](struct.Csound.html#method.set_score_offset_seconds)). pub fn get_score_offset_seconds(&self) -> f64 { unsafe { csound_sys::csoundGetScoreOffsetSeconds(self.engine.csound) as f64 } } /// Csound score events prior to the specified time are not performed. /// And performance begins immediately at the specified time /// (real-time events will continue to be performed as they are received). /// Can be used by external software, such as a VST host, to begin score performance midway through a Csound score, /// for example to repeat a loop in a sequencer or to synchronize other events with the Csound score. pub fn set_score_offset_seconds(&self, offset: f64) { unsafe { csound_sys::csoundSetScoreOffsetSeconds(self.engine.csound, offset as c_double); } } /// Rewinds a compiled Csound score to the time specified with [`Csound::set_score_offset_seconds`](struct.Csound.html#method.set_score_offset_seconds) pub fn rewind_score(&self) { unsafe { csound_sys::csoundRewindScore(self.engine.csound); } } // TODO SCORE SORT FUNCTIONS /* Engine general messages functions implmentations ********************************************************* */ /// # Returns /// The Csound message level (from 0 to 231). pub fn get_message_level(&self) -> u8 { unsafe { csound_sys::csoundGetMessageLevel(self.engine.csound) as u8 } } /// Sets the Csound message level (from 0 to 231). pub fn set_message_level(&self, level: u8) { unsafe { csound_sys::csoundSetMessageLevel(self.engine.csound, level as c_int); } } /// Creates a buffer for storing messages printed by Csound. Should be called after creating a Csound instance and the buffer can be freed by /// calling [`Csound::destroy_message_buffer`](struct.Csound.html#method.destroy_message_buffer) or it will freed when the csound instance is dropped. /// You will generally want to call [`Csound::cleanup`](struct.Csound.html#method.cleanup) to make sure the last messages are flushed to the message buffer before destroying Csound. /// # Arguments /// * `stdout` If is non-zero, the messages are also printed to stdout and stderr (depending on the type of the message), in addition to being stored in the buffer. /// *Note*: Using the message buffer ties up the internal message callback, /// so [`Csound::message_string_callback`](struct.Csound.html#method.message_string_callback) should not be called after creating the message buffer. pub fn create_message_buffer(&self, stdout: i32) { unsafe { csound_sys::csoundCreateMessageBuffer(self.engine.csound, stdout as c_int); let mut msg_buff = self.engine.use_msg_buffer.borrow_mut(); *msg_buff = true; } } /// Releases all memory used by the message buffer. /// If this buffer is created, the Drop method /// will call this function when the Csound instance were dropped. pub fn destroy_message_buffer(&self) { unsafe { csound_sys::csoundDestroyMessageBuffer(self.engine.csound); let mut msg_buff = self.engine.use_msg_buffer.borrow_mut(); *msg_buff = false; } } /// # Returns /// The first message from the buffer. pub fn get_first_message(&self) -> Option<String> { unsafe { match CStr::from_ptr(csound_sys::csoundGetFirstMessage(self.engine.csound)).to_str() { Ok(m) => Some(m.to_owned()), _ => None, } } } /// # Returns /// The attribute parameter ([`MessageType`](enum.MessageType.html)) of the first message in the buffer. pub fn get_first_message_attr(&self) -> MessageType { unsafe { MessageType::from(csound_sys::csoundGetFirstMessageAttr(self.engine.csound) as u32) } } /// Removes the first message from the buffer. pub fn pop_first_message(&self) { unsafe { csound_sys::csoundPopFirstMessage(self.engine.csound); } } /// # Returns /// The number of pending messages in the buffer. pub fn get_message_count(&self) -> u32 { unsafe { csound_sys::csoundGetMessageCnt(self.engine.csound) as u32 } } /* Engine general Channels, Control and Events implementations ********************************************** */ /// Requests a list of all control channels. /// # Returns /// A vector with all control channels info or None if there are not control channels. see: [`ChannelInfo`](struct.ChannelInfo.html) pub fn list_channels(&self) -> Option<Vec<ChannelInfo>> { let mut ptr = ptr::null_mut() as *mut csound_sys::controlChannelInfo_t; let ptr2: *mut *mut csound_sys::controlChannelInfo_t = &mut ptr as *mut *mut _; unsafe { let count = csound_sys::csoundListChannels(self.engine.csound, ptr2) as i32; let mut ptr = *ptr2; if count > 0 { let mut list = Vec::new(); for _ in 0..count { let name = match Trampoline::ptr_to_string((*ptr).name) { Some(string) => string, None => "".into(), }; let ctype = (*ptr).type_ as i32; let hints = (*ptr).hints; let attributes = match Trampoline::ptr_to_string(hints.attributes) { Some(string) => string, None => "".into(), }; list.push(ChannelInfo { name, type_: ctype, hints: ChannelHints { behav: ChannelBehavior::from_u32(hints.behav as u32), dflt: hints.dflt as f64, min: hints.min as f64, max: hints.max as f64, x: hints.x as i32, y: hints.y as i32, width: hints.width as i32, height: hints.height as i32, attributes, }, }); ptr = ptr.add(1); } csound_sys::csoundDeleteChannelList(self.engine.csound, *ptr2); return Some(list); } None } } /// Return a [`ControlChannelPtr`](struct.ControlChannelPtr.html) which represent a csound's channel ptr. /// creating the channel first if it does not exist yet. /// # Arguments /// * `name` The channel name. /// * `channel_type` must be the bitwise OR of exactly one of the following values: /// - CSOUND_CONTROL_CHANNEL /// control data (one MYFLT value) /// - CSOUND_AUDIO_CHANNEL /// audio data (get_ksmps() f64 values) /// - CSOUND_STRING_CHANNEL /// string data (f64 values with enough space to store /// get_channel_data_size() characters, including the /// NULL character at the end of the string) /// and at least one of these: /// - CSOUND_INPUT_CHANNEL /// - CSOUND_OUTPUT_CHANNEL /// If the channel already exists, it must match the data type /// (control, audio, or string), however, the input/output bits are /// OR'd with the new value. Note that audio and string channels /// can only be created after calling Compile(), because the /// storage size is not known until then. /// # Returns /// The ControlChannelPtr on success or a Status code, /// "Not enough memory for allocating the channel" (CS_MEMORY) /// "The specified name or type is invalid" (CS_ERROR) /// or, if a channel with the same name but incompatible type /// already exists, the type of the existing channel. /// * Note:* to find out the type of a channel without actually /// creating or changing it, set 'channel_type' argument to CSOUND_UNKNOWN_CHANNEL, so that the error /// value will be either the type of the channel, or CSOUND_ERROR /// if it does not exist. /// Operations on the channel pointer are not thread-safe by default. The host is /// required to take care of threadsafety by /// 1) with control channels use __sync_fetch_and_add() or /// __sync_fetch_and_or() gcc atomic builtins to get or set a channel, /// if available. /// 2) For string and audio channels (and controls if option 1 is not /// available), retrieve the channel lock with ChannelLock() /// and use SpinLock() and SpinUnLock() to protect access /// to the channel. /// See Top/threadsafe.c in the Csound library sources for /// examples. Optionally, use the channel get/set functions /// which are threadsafe by default. #[deprecated(since="0.1.6", note="please use `get_input_channel` or `get_output_channel` instead")] pub fn get_channel_ptr<'a>( &'a self, name: &str, channel_type: ControlChannelType, ) -> Result<ControlChannelPtr<'a>, Status> { let cname = CString::new(name).map_err(|_| Status::CS_ERROR)?; let mut ptr = ptr::null_mut() as *mut f64; let ptr = &mut ptr as *mut *mut _; let channel = ControlChannelType::from_bits( channel_type.bits() & ControlChannelType::CSOUND_CHANNEL_TYPE_MASK.bits(), ) .unwrap(); let len: usize = match channel { ControlChannelType::CSOUND_CONTROL_CHANNEL => std::mem::size_of::<f64>(), ControlChannelType::CSOUND_AUDIO_CHANNEL => self.get_ksmps() as usize, /*ControlChannelType::CSOUND_STRING_CHANNEL => { self.get_channel_data_size(name) / std::mem::size_of::<f64>() }*/ _ => return Err(Status::CS_ERROR), }; unsafe { let result = Status::from(csound_sys::csoundGetChannelPtr( self.engine.csound, ptr, cname.as_ptr(), channel_type.bits() as c_int, )); match result { Status::CS_SUCCESS => Ok(ControlChannelPtr { ptr: *ptr, channel_type: channel, len, phantom: PhantomData, }), Status::CS_OK(channel) => Err(Status::CS_OK(channel)), result => Err(result), } } } pub(crate) fn get_raw_channel_ptr( &self, name: &str, ptr: *mut *mut f64, channel_type: c_int, ) -> c_int { let cname = match CString::new(name) { Ok(c) => c, Err(_) => return -1, }; unsafe { csound_sys::csoundGetChannelPtr(self.engine.csound, ptr, cname.as_ptr(), channel_type) } } /// Set parameters hints for a control channel. /// These hints have no internal function but can be used by front ends to construct GUIs or to constrain values. /// # Returns /// CS_SUCCESS on success, or CS_ERROR on failure: the channel does not exist, is not a control channel, /// or the specified parameters are invalid or CS_MEMORY: could not allocate memory for the /// channel. see: ([`Status`](enum.Status.html)) pub fn set_channel_hints(&self, name: &str, hint: &ChannelHints) -> Result<(), Status> { let attr = &hint.attributes[..]; let attr = CString::new(attr).map_err(|_| Status::CS_ERROR)?; let cname = CString::new(name).map_err(|_| Status::CS_ERROR)?; let channel_hint = csound_sys::controlChannelHints_t { behav: ChannelBehavior::to_u32(&hint.behav), dflt: hint.dflt, min: hint.min, max: hint.max, x: hint.x, y: hint.y, width: hint.width as c_int, height: hint.height as c_int, attributes: attr.as_ptr() as *mut c_char, }; unsafe { match Status::from(csound_sys::csoundSetControlChannelHints( self.engine.csound, cname.as_ptr(), channel_hint, ) as i32) { Status::CS_SUCCESS => Ok(()), status => Err(status), } } } /// Returns special parameters (or None if there are not any) of a control channel. /// Previously set with csoundSetControlChannelHints() or the /// [chnparams](http://www.csounds.com/manualOLPC/chnparams.html) opcode. pub fn get_channel_hints(&self, name: &str) -> Result<ChannelHints, Status> { let cname = CString::new(name).map_err(|_| Status::CS_ERROR)?; let mut hint = csound_sys::controlChannelHints_t::default(); unsafe { match csound_sys::csoundGetControlChannelHints( self.engine.csound, cname.as_ptr() as *mut c_char, &mut hint as *mut _, ) { csound_sys::CSOUND_SUCCESS => { let attributes = match Trampoline::ptr_to_string(hint.attributes) { Some(name) => name, None => "".into(), }; let hints = ChannelHints { behav: ChannelBehavior::from_u32(hint.behav as u32), dflt: hint.dflt, min: hint.min, max: hint.max, x: hint.x as i32, y: hint.y as i32, width: hint.width as i32, height: hint.height as i32, attributes, }; Ok(hints) } status => Err(Status::from(status)), } } } /// Retrieves the value of a control channel. /// # Arguments /// * `name` The channel name. /// An error message will be returned if the channel is not a control channel, /// the channel not exist or if the name is invalid. pub fn get_control_channel(&self, name: &str) -> Result<f64, &'static str> { let cname = CString::new(name).map_err(|_| "invalid channel name")?; let mut err: c_int = 0; unsafe { let ret = csound_sys::csoundGetControlChannel( self.engine.csound, cname.as_ptr(), &mut err as *mut _, ) as f64; if (err) == csound_sys::CSOUND_SUCCESS { Ok(ret) } else { Err("channel not exist or is not a control channel") } } } /// Sets the value of a control channel. /// # Arguments /// * `name` The channel name. pub fn set_control_channel(&self, name: &str, value: f64) { let cname = CString::new(name).unwrap(); unsafe { csound_sys::csoundSetControlChannel(self.engine.csound, cname.as_ptr(), value); } } /// Copies samples from an audio channel. /// # Arguments /// * `name` The channel name. /// * `out` The slice where the date contained in the internal audio channel buffer /// will be copied. Should contain enough memory for ksmps f64 samples. /// # Panic /// If the buffer passed to this function doesn't have enough memory. pub fn read_audio_channel(&self, name: &str, output: &mut [f64]) { let size = self.get_ksmps() as usize; let bytes = output.len(); let cname = CString::new(name).unwrap(); assert!( size <= bytes, "The audio channel's capacity is {} so, it isn't possible to copy {} samples", size, bytes ); unsafe { csound_sys::csoundGetAudioChannel( self.engine.csound, cname.as_ptr(), output.as_ptr() as *mut c_double, ); } } /// Writes data into an audio channel buffer. audio channel identified by *name* with data from slice *input* which should /// contain at least ksmps f64 samples, if not, this method will panic. /// # Arguments /// * `input` The slice with data to be copied into the audio channel buffer. Could contain up to ksmps samples. /// # panic /// This method will panic if input.len() > ksmps. pub fn write_audio_channel(&self, name: &str, input: &[f64]) { let size = self.get_ksmps() as usize * self.input_channels() as usize; let bytes = input.len(); let cname = CString::new(name).unwrap(); assert!( size <= bytes, "The audio channel's capacity is {} so, it isn't possible to copy {} bytes", size, bytes ); unsafe { csound_sys::csoundSetAudioChannel( self.engine.csound, cname.as_ptr(), input.as_ptr() as *mut c_double, ); } } /// Returns the content of the string channel identified by *name* pub fn get_string_channel(&self, name: &str) -> String { let cname = CString::new(name).unwrap(); let mut data = String::with_capacity(self.get_channel_data_size(name)); unsafe { let ptr = data.as_mut_vec(); csound_sys::csoundGetStringChannel( self.engine.csound, cname.as_ptr(), ptr.as_ptr() as *mut _, ); } data } /// Sets the string channel identified by *name* with *content* pub fn set_string_channel(&self, name: &str, content: &str) { let cname = CString::new(name).unwrap(); let content = CString::new(content).unwrap(); unsafe { csound_sys::csoundSetStringChannel( self.engine.csound, cname.as_ptr(), content.as_ptr() as *mut _, ); } } /// returns the size of data stored in the channel identified by *name* pub fn get_channel_data_size(&self, name: &str) -> usize { let cname = CString::new(name).unwrap(); unsafe { csound_sys::csoundGetChannelDatasize(self.engine.csound, cname.as_ptr()) as usize } } /// Receives a PVSDAT fout from the [*pvsout*](http://www.csounds.com/manual/html/pvsout.html) opcode. /// This method will return Ok on success, /// [`Status::CS_ERROR`](enum.Status.html#member.CS_ERROR) if the channel name is not valid or the channel doesn't /// exist or [`Status::CS_MEMORY`](enum.Status.html#member.CS_MEMORY) if the frame buffer lengths haven't the same size /// as the requested table /// # Arguments /// * `name` The channel identifier. /// * `pvs_data` Reference to tha struct which will be filled with the pvs data. /// # Example /// ``` /// let mut pvs = PvsDataExt::new(512); /// cs.get_pvs_channel("1", &mut pvs); /// ``` pub fn get_pvs_channel(&self, name: &str, pvs_data: &mut PvsDataExt) -> Result<(), Status> { let cname = CString::new(name).map_err(|_| Status::CS_ERROR)?; let mut ptr = ptr::null_mut() as *mut f64; unsafe { if csound_sys::csoundGetChannelPtr( self.engine.csound, &mut ptr as *mut *mut _, cname.as_ptr(), (csound_sys::CSOUND_PVS_CHANNEL | csound_sys::CSOUND_INPUT_CHANNEL) as c_int, ) == csound_sys::CSOUND_SUCCESS { // Same data buffer size? if (*(ptr as *mut csound_sys::PVSDATEXT)).N == pvs_data.N as c_int { let data = &mut csound_sys::PVSDATEXT::default(); data.frame = pvs_data.frame.as_mut_slice().as_ptr() as *mut f32; let result = csound_sys::csoundGetPvsChannel( self.engine.csound, &mut *data, cname.as_ptr(), ); match result { csound_sys::CSOUND_SUCCESS => { pvs_data.N = data.N as u32; pvs_data.sliding = data.sliding as u32; pvs_data.NB = data.NB as i32; pvs_data.overlap = data.overlap as u32; pvs_data.winsize = data.winsize as u32; pvs_data.wintype = data.wintype as u32; pvs_data.format = data.format as u32; pvs_data.framecount = data.framecount as u32; Ok(()) } err => Err(Status::from(err)), } } else { Err(Status::CS_MEMORY) } } else { Err(Status::CS_ERROR) } } } pub fn set_pvs_channel(&self, name: &str, pvs_data: &PvsDataExt) { unsafe { let cname = CString::new(name); if cname.is_ok() { let data = &mut csound_sys::PVSDATEXT { N: pvs_data.N as _, sliding: pvs_data.sliding as _, NB: pvs_data.NB as _, overlap: pvs_data.overlap as _, winsize: pvs_data.winsize as _, wintype: pvs_data.wintype as _, format: pvs_data.format as _, framecount: pvs_data.framecount as _, frame: pvs_data.frame.as_slice().as_ptr() as *mut f32, }; csound_sys::csoundSetPvsChannel( self.engine.csound, &*data, cname.unwrap().as_ptr(), ); } } } /// Send a new score event. /// # Arguments /// * `event_type` is the score event type ('a', 'i', 'q', 'f', or 'e'). /// * `pfields` is a slice of f64 values with all the pfields for this event. /// # Example /// ``` /// let cs = Csound::new(); /// let pFields = [1.0, 1.0, 5.0]; /// while cs.perform_ksmps() == false { /// cs.send_score_event('i', &pFields); /// } /// ``` pub fn send_score_event(&self, event_type: char, pfields: &[f64]) -> Status { unsafe { Status::from(csound_sys::csoundScoreEvent( self.engine.csound, event_type as c_char, pfields.as_ptr() as *const c_double, pfields.len() as c_long, ) as i32) } } /// Like [`Csound::send_score_event`](struct.Csound.html#method.send_score_event). /// This function inserts a score event, /// but at absolute time with respect to the start of performance, /// or from an offset set with *time_offset* pub fn send_score_event_absolute( &self, event_type: char, pfields: &[f64], time_offset: f64, ) -> Status { unsafe { Status::from(csound_sys::csoundScoreEventAbsolute( self.engine.csound, event_type as c_char, pfields.as_ptr() as *const c_double, pfields.len() as c_long, time_offset as c_double, ) as i32) } } /// Asynchronous version of [`Csound::send_score_event`](struct.Csound.html#method.send_score_event) pub fn send_score_event_async(&self, event_type: char, pfields: &[f64]) -> Status { unsafe { Status::from(csound_sys::csoundScoreEventAsync( self.engine.csound, event_type as c_char, pfields.as_ptr() as *const c_double, pfields.len() as c_long, ) as i32) } } /// Asynchronous version of [`Csound::send_score_event_absolute`](struct.Csound.html#method.send_score_event_absolute) pub fn send_score_event_absolute_async( &self, event_type: char, pfields: &[f64], time_offset: f64, ) -> Status { unsafe { Status::from(csound_sys::csoundScoreEventAbsoluteAsync( self.engine.csound, event_type as c_char, pfields.as_ptr() as *const c_double, pfields.len() as c_long, time_offset as c_double, ) as i32) } } /// Input a string (as if from a console), used for line events. /// # Example /// ``` /// let cs = Csound::new(); /// let pFields = [1.0, 1.0, 5.0]; /// while cs.perform_ksmps() == false { /// cs.send_input_message("i 2 0 0.75 1"); /// } /// ``` pub fn send_input_message(&self, message: &str) -> Result<(), NulError> { let cmessage = CString::new(message)?; unsafe { csound_sys::csoundInputMessage(self.engine.csound, cmessage.as_ptr() as *const c_char); Ok(()) } } /// Asynchronous version of [`Csound::send_input_message`](struct.Csound.html#method.send_input_message) pub fn send_input_message_async(&self, message: &str) -> Result<(), NulError> { let cmessage = CString::new(message)?; unsafe { csound_sys::csoundInputMessageAsync( self.engine.csound, cmessage.as_ptr() as *const c_char, ); Ok(()) } } /// Kills off one or more running instances of an instrument. /// # Arguments /// * `instr` The numeric identifier of the instrument. /// * `name` The string identifier of the instrument or name. If it is None, the instrument /// numeric identifier is used. /// * `mode` is a sum of the following values: 0,1,2: kill all instances (1), oldest only (1), or newest (2) /// 4: only turnoff notes with exactly matching (fractional) instr number /// 8: only turnoff notes with indefinite duration (p3 < 0 or MIDI). /// * `allow_release` if true, the killed instances are allowed to release. pub fn kill_instrument( &self, instr: f64, name: Option<&str>, mode: u32, allow_release: bool, ) -> Status { let cname = CString::new(name.unwrap_or_else(|| "")).unwrap(); unsafe { Status::from(csound_sys::csoundKillInstance( self.engine.csound, instr as c_double, cname.as_ptr() as *const c_char, mode as c_int, allow_release as c_int, ) as i32) } } /// Set the ASCII code of the most recent key pressed. /// # Arguments /// * `key` The ASCII identifier for the key pressed. pub fn key_press(&self, key: char) { unsafe { csound_sys::csoundKeyPress(self.engine.csound, key as c_char); } } /* Engine general Table function implementations **************************************************************************************** */ /// Returns the length of a function table (not including the guard point), or an error /// message if the table doens't exist. /// # Arguments /// * `table` The function table identifier. pub fn table_length(&self, table: u32) -> Result<usize, &'static str> { unsafe { let value = csound_sys::csoundTableLength(self.engine.csound, table as c_int) as i32; if value > 0 { Ok(value as usize) } else { Err("Table doesn't exist") } } } /// Returns the value of a slot in a function table. /// If the Table or index are not valid, an error message will be returned. /// # Arguments /// * `table` The function table identifier. /// * `index` The value at table[index] which will be read. pub fn table_get(&self, table: u32, index: u32) -> Result<f64, &'static str> { unsafe { let size = self.table_length(table)?; if index < size as u32 { Ok( csound_sys::csoundTableGet(self.engine.csound, table as c_int, index as c_int) as f64, ) } else { Err("index out of range") } } } /// Sets the value of a slot in a function table. /// # Arguments /// * `table` The function table identifier. /// * `index` The slot at table[index] where value will be added. /// # Returns /// An error message if the index or table are no valid pub fn table_set(&self, table: u32, index: u32, value: f64) -> Result<(), &'static str> { unsafe { let size = self.table_length(table)?; if index < size as u32 { csound_sys::csoundTableSet( self.engine.csound, table as c_int, index as c_int, value, ); Ok(()) } else { Err("index out of range") } } } /// Copies the content of a function table into a slice. /// # Arguments /// * `table` The function table identifier. /// # Returns /// An error message if the table doesn't exist or the passed slice /// doesn't have enough memory to content the table values. pub fn table_copy_out(&self, table: u32, output: &mut [f64]) -> Result<(), &'static str> { unsafe { let size = self.table_length(table)?; if output.len() < size { Err("Not enough memory to copy the table") } else { csound_sys::csoundTableCopyOut( self.engine.csound, table as c_int, output.as_ptr() as *mut c_double, ); Ok(()) } } } /// Asynchronous version of [`Csound:: table_copy_out`](struct.Csound.html#method.table_copy_out) pub fn table_copy_out_async(&self, table: u32, output: &mut [f64]) -> Result<(), &'static str> { unsafe { let size = self.table_length(table)?; if output.len() < size { Err("Not enough memory to copy the table") } else { csound_sys::csoundTableCopyOutAsync( self.engine.csound, table as c_int, output.as_ptr() as *mut c_double, ); Ok(()) } } } /// Copy the contents of an array into a given function table. /// # Arguments /// * `table` The function table identifier. /// * `src` Slice with the values to be copied into the function table /// # Returns /// An error message if the table doesn't exist or doesn't have enough /// capacity. pub fn table_copy_in(&self, table: u32, src: &[f64]) -> Result<(), &'static str> { let size = self.table_length(table)?; if size < src.len() { Err("Table doesn't have enough capacity") } else { unsafe { csound_sys::csoundTableCopyIn( self.engine.csound, table as c_int, src.as_ptr() as *const c_double, ); Ok(()) } } } /// Asynchronous version of [`Csound:: table_copy_in`](struct.Csound.html#method.table_copy_in) pub fn table_copy_in_async(&self, table: u32, src: &[f64]) -> Result<(), &'static str> { let size = self.table_length(table)?; if size < src.len() { Err("Table doesn't have enough capacity") } else { unsafe { csound_sys::csoundTableCopyInAsync( self.engine.csound, table as c_int, src.as_ptr() as *const c_double, ); Ok(()) } } } /// Returns a [`Csound::Table`](struct.Table.html). /// which could be used to read/write the table content /// directly( not using [`Csound:: table_copy_in`](struct.Csound.html#method.table_copy_in) or [`Csound::table_copy_out`](struct.Csound.html#method.table_copy_out)). /// this table will be valid along the csound instance. Returns None if the table doesn't /// exist. /// # Arguments /// * `table` The function table identifier. /// # Example /// ``` /// let cs = Csound::new(); /// cs.compile_csd("some.csd"); /// cs.start().unwrap(); /// while cs.perform_ksmps() == false { /// let mut table_buff = vec![0f64; cs.table_length(1).unwrap() as usize]; /// // Gets the function table 1 /// let mut table = cs.get_table(1).unwrap(); /// // Copies the table content into table_buff /// table.read( table_buff.as_mut_slice() ).unwrap(); /// // Do some stuffs /// table.write(&table_buff.into_iter().map(|x| x*2.5).collect::<Vec<f64>>().as_mut_slice()); /// // Do some stuffs /// } /// ``` /// see [`Table::read`](struct.Table.html#method.read) or [`Table::write`](struct.Table.html#method.write). pub fn get_table(&self, table: u32) -> Option<Table> { let mut ptr = ptr::null_mut() as *mut c_double; let length; unsafe { length = csound_sys::csoundGetTable( self.engine.csound, &mut ptr as *mut *mut c_double, table as c_int, ) as i32; } match length { -1 => None, _ => Some(Table { ptr, length: length as usize, phantom: PhantomData, }), } } /// Gets the arguments used to construct or define a function table /// # Arguments /// * `table` The function table identifier. /// # Returns /// A vector containing the table's arguments. /// * Note:* the argument list starts with the GEN number and is followed by its parameters. /// eg. f 1 0 1024 10 1 0.5 yields the list {10.0,1.0,0.5}. pub fn get_table_args(&self, table: u32) -> Option<Vec<f64>> { let mut ptr = ptr::null_mut() as *mut c_double; unsafe { let length = csound_sys::csoundGetTableArgs( self.engine.csound, &mut ptr as *mut *mut c_double, table as c_int, ); if length < 0 { None } else { let mut result = Vec::with_capacity(length as usize); for pos in 0..length as isize { result.push(*ptr.offset(pos)); } Some(result) } } } /// Gets the arguments used to construct or define a function table /// Similar to [`Csound::get_table_args`](struct.Csound.html#method.get_table_args) /// but no memory will be allocated, instead a slice is returned. pub fn get_table_args_slice(&self, table: u32) -> Option<&[f64]> { let mut ptr = ptr::null_mut() as *mut c_double; unsafe { let length = csound_sys::csoundGetTableArgs( self.engine.csound, &mut ptr as *mut *mut c_double, table as c_int, ); if length < 0 { None } else { Some(slice::from_raw_parts(ptr as *const _, length as usize)) } } } /// Checks if a given *gen* number is a named GEN /// # Arguments /// * `gen` The GEN number identifier. /// # Returns /// The GEN names's length pub fn is_named_gen(&self, gen: u32) -> usize { unsafe { csound_sys::csoundIsNamedGEN(self.engine.csound, gen as c_int) as usize } } /// Returns the GEN name if it exist ans is named, else, returns None /// # Arguments /// * `gen` The GEN number identifier. /// # Returns /// A option with the GEN name or None if the GEN is not a named one /// or not exist. pub fn get_gen_name(&self, gen: u32) -> Option<String> { unsafe { let len = self.is_named_gen(gen); if len > 0 { let name = vec![0u8; len]; let name_raw = CString::from_vec_unchecked(name).into_raw(); csound_sys::csoundGetNamedGEN( self.engine.csound, gen as c_int, name_raw, len as c_int, ); let name = CString::from_raw(name_raw); match name.to_str() { Ok(str) => Some(str.to_owned()), Err(_) => None, } } else { None } } } /* Engine general Opcode function implementations **************************************************************************************** */ /// Gets an alphabetically sorted list of all opcodes. /// Should be called after externals are loaded by csoundCompile(). /// The opcode information is contained in a [`Csound::OpcodeListEntry`](struct.Csound.html#struct.OpcodeListEntry) pub fn get_opcode_list_entry(&self) -> Option<Vec<OpcodeListEntry>> { let mut ptr = ptr::null_mut() as *mut csound_sys::opcodeListEntry; let length; unsafe { length = csound_sys::csoundNewOpcodeList( self.engine.csound, &mut ptr as *mut *mut csound_sys::opcodeListEntry, ); } if length < 0 { None } else { let mut result: Vec<OpcodeListEntry> = Vec::with_capacity(length as usize); for pos in 0..length as isize { unsafe { let opname = Trampoline::ptr_to_string((*ptr.offset(pos)).opname); let outypes = Trampoline::ptr_to_string((*ptr.offset(pos)).outypes); let intypes = Trampoline::ptr_to_string((*ptr.offset(pos)).intypes); let flags = (*ptr.offset(pos)).flags as i32; result.push(OpcodeListEntry { opname, outypes, intypes, flags, }); } } unsafe { csound_sys::csoundDisposeOpcodeList(self.engine.csound, ptr); Some(result) } } } /** TODO genName and appendOpcode functions *****/ /* Engine miscellaneous functions **************************************************************************************** */ /// # Argument /// * `lang_code` can be for example any of [`Language`](enum.Language.html) variants. /// This affects all Csound instances running in the address /// space of the current process. The special language code /// *Language::CSLANGUAGE_DEFAULT* can be used to disable translation of messages and /// free all memory allocated by a previous call to this function. /// set_language() loads all files for the selected language from the directory specified by the **CSSTRNGS** environment /// variable. pub fn set_language(lang_code: Language) { unsafe { csound_sys::csoundSetLanguage(lang_code as u32); } } /// Generates a random seed from time /// # Returns /// A 32-bit unsigned integer to be used as random seed. pub fn get_random_seed_from_time() -> u32 { unsafe { csound_sys::csoundGetRandomSeedFromTime() as u32 } } /// Simple linear congruential random number generator: seed = seed * 742938285 % 2147483647 /// # Returns /// The next number from the pseudo-random sequence, in the range 1 to 2147483646. /// if the value of seed is not in the range 1 to 2147483646 an error message will /// be returned. pub fn get_rand31(seed: &mut u32) -> Result<u32, &'static str> { unsafe { match seed { 1..=2_147_483_646 => { let ptr: *mut u32 = &mut *seed; let res = csound_sys::csoundRand31(ptr as *mut c_int) as u32; Ok(res) } _ => Err("invalid seed value"), } } } /// Returns an initialised timer structure. pub fn init_timer() -> RTCLOCK { let mut timer = RTCLOCK::default(); unsafe { let ptr: *mut RTCLOCK = &mut timer as *mut RTCLOCK; csound_sys::csoundInitTimerStruct(ptr); } timer } /// Calculates a time offset /// # Arguments /// * `timer` time struct since the elapsed time will be calculated. /// # Returns /// The elapsed real time (in seconds) since the specified timer pub fn get_real_time(timer: &RTCLOCK) -> f64 { unsafe { let ptr: *mut csound_sys::RTCLOCK = &mut csound_sys::RTCLOCK { starttime_real: timer.starttime_real as c_long, starttime_CPU: timer.starttime_CPU as c_long, }; csound_sys::csoundGetRealTime(ptr) as f64 } } /// Return the elapsed CPU time (in seconds) since the specified *timer* structure was initialised. /// # Arguments /// * `gen` The GEN number identifier. pub fn get_cpu_time(timer: &mut RTCLOCK) -> f64 { unsafe { csound_sys::csoundGetCPUTime(timer as *mut RTCLOCK) as f64 } } /// Creates a circular buffer. /// # Arguments /// * `len` The buffer length. /// # Returns /// A CircularBuffer /// # Example /// ``` /// let csound = Csound::new(); /// let circular_buffer = csound.create_circular_buffer::<f64>(1024); /// ``` pub fn create_circular_buffer<'a, T: 'a + Copy>(&'a self, len: u32) -> CircularBuffer<T> { unsafe { let ptr: *mut T = csound_sys::csoundCreateCircularBuffer( self.engine.csound, len as c_int, mem::size_of::<T>() as c_int, ) as *mut T; CircularBuffer { csound: self.engine.csound, ptr, phantom: PhantomData, } } } // Threading function pub fn sleep(&self, milli_seconds: usize) { unsafe { csound_sys::csoundSleep(milli_seconds); } } // TODO global variables functions /********************************** Callback settings using the custom callback Handler implementation******/ /// Sets a function that is called to obtain a list of audio devices. /// This should be set by rtaudio modules and should not be set by hosts. pub fn audio_device_list_callback<'c, F>(&self, f: F) where F: FnMut(CsAudioDevice) + 'c, { unsafe { (*(csound_sys::csoundGetHostData(self.engine.csound) as *mut CallbackHandler)) .callbacks .audio_dev_list_cb = Some(Box::new(f)); } self.enable_callback(AUDIO_DEV_LIST); } /// Sets a function to be called by Csound for opening real-time audio playback. /// This callback is used to inform the user about the current audio device Which /// Csound will use to play the audio samples. /// # Arguments /// * `user_func` A function/closure which will receive a reference /// to a RtAudioParams struct. pub fn play_open_audio_callback<'c, F>(&self, f: F) where F: FnMut(&RtAudioParams) -> Status + 'c, { unsafe { (*(csound_sys::csoundGetHostData(self.engine.csound) as *mut CallbackHandler)) .callbacks .play_open_cb = Some(Box::new(f)); } self.enable_callback(PLAY_OPEN); } /// Sets a function to be called by Csound for opening real-time audio recording. /// This callback is used to inform the user about the current audio device Which /// Csound will use for opening realtime audio recording. You have to return Status::CS_SUCCESS pub fn rec_open_audio_callback<'c, F>(&self, f: F) where F: FnMut(&RtAudioParams) -> Status + 'c, { unsafe { (*(csound_sys::csoundGetHostData(self.engine.csound) as *mut CallbackHandler)) .callbacks .play_open_cb = Some(Box::new(f)); } self.enable_callback(REC_OPEN); } /// Sets a function to be called by Csound for performing real-time audio playback. /// A reference to a buffer with audio samples is passed /// to the user function in the callback. These samples have to be processed and sent /// to a proper audio device. pub fn rt_audio_play_callback<'c, F>(&self, f: F) where F: FnMut(&[f64]) + 'c, { unsafe { (*(csound_sys::csoundGetHostData(self.engine.csound) as *mut CallbackHandler)) .callbacks .rt_play_cb = Some(Box::new(f)); } self.enable_callback(REAL_TIME_PLAY); } /// Sets a function to be called by Csound for performing real-time audio recording. /// With this callback the user can fill a buffer with samples from a custom /// audio module, and pass it into csound. pub fn rt_audio_rec_callback<'c, F>(&self, f: F) where F: FnMut(&mut [f64]) -> usize + 'c, { unsafe { (*(csound_sys::csoundGetHostData(self.engine.csound) as *mut CallbackHandler)) .callbacks .rt_rec_cb = Some(Box::new(f)); } self.enable_callback(REAL_TIME_REC); } /// Indicates to the user when csound has closed the rtaudio device. pub fn rt_close_callback<'c, F>(&self, f: F) where F: FnMut() + 'c, { unsafe { (*(csound_sys::csoundGetHostData(self.engine.csound) as *mut CallbackHandler)) .callbacks .rt_close_cb = Some(Box::new(f)); } self.enable_callback(RT_CLOSE_CB); } /// Sets callback to be called once in every control period. /// This facility can be used to ensure a function is called synchronously /// before every csound control buffer processing. /// It is important to make sure no blocking operations are performed in the callback. pub fn sense_event_callback<'c, F>(&self, f: F) where F: FnMut() + 'c, { unsafe { (*(csound_sys::csoundGetHostData(self.engine.csound) as *mut CallbackHandler)) .callbacks .sense_event_cb = Some(Box::new(f)); } self.enable_callback(SENSE_EVENT); } /*fn cscore_callback<'c, F>(&mut self, f:F) where F: FnMut() + 'c { self.engine.inner.handler.callbacks.cscore_cb = Some(Box::new(f)); self.engine.enable_callback(CSCORE_CB); }*/ /// Sets a callback which will be called by csound to print an informational message. /// # Arguments /// * ´f´ Function which implement the FnMut trait. /// The callback arguments are *u32* which indicates the message atributte, /// and a reference to the message content. /// # Example /// ``` /// let mut cs = Csound::new(); /// cs.message_string_callback(|att: MessageType, message: &str| print!("{}", message)); /// ``` pub fn message_string_callback<'c, F>(&self, f: F) where F: FnMut(MessageType, &str) + 'c, { unsafe { (*(csound_sys::csoundGetHostData(self.engine.csound) as *mut CallbackHandler)) .callbacks .message_cb = Some(Box::new(f)); } self.enable_callback(MESSAGE_CB); } /*fn keyboard_callback<'c, F>(&self, f: F) where F: FnMut() -> char + 'c, { unsafe{(&mut *(csound_sys::csoundGetHostData(self.engine.csound) as *mut CallbackHandler)).callbacks.keyboard_cb = Some(Box::new(f));} self.enable_callback(KEYBOARD_CB); }*/ /// Sets the function which will be called whenever the [*invalue*](http://www.csounds.com/manual/html/invalue.html) opcode is used. /// # Arguments /// * ´f´ Function which implement the FnMut trait. The invalue opcode will trigger this callback passing /// the channel name which requiere the data. This function/closure have to return the data which will be /// passed to that specific channel if not only return ChannelData::CS_UNKNOWN_CHANNEL. Only *String* and *control* Channels /// are supported. /// # Example /// ``` /// let input_channel = |name: &str|->ChannelData { /// if name == "myStringChannel"{ /// let myString = "my data".to_owned(); /// ChannelData::CS_STRING_CHANNEL(myString) /// } /// ChannelData::CS_UNKNOWN_CHANNEL /// }; /// let mut cs = Csound::new(); /// cs.input_channel_callback(input_channel); /// ``` pub fn input_channel_callback<'c, F>(&self, f: F) where F: FnMut(&str) -> ChannelData + 'c, { unsafe { (*(csound_sys::csoundGetHostData(self.engine.csound) as *mut CallbackHandler)) .callbacks .input_channel_cb = Some(Box::new(f)); } self.enable_callback(CHANNEL_INPUT_CB); } /// Sets the function which will be called whenever the [*outvalue*](http://www.csounds.com/manual/html/outvalue.html) opcode is used. /// # Arguments /// * ´f´ Function which implement the FnMut trait. The outvalue opcode will trigger this callback passing /// the channel ##name and the channel's output data encoded in the ChannelData. Only *String* and *control* Channels /// are supported. /// # Example /// ``` /// let output_channel = |name: &str, data:ChannelData|{ /// print!("channel name:{} data: {:?}", name, data); /// }; /// let mut cs = Csound::new(); /// cs.output_channel_callback(output_channel); /// ``` pub fn output_channel_callback<'c, F>(&self, f: F) where F: FnMut(&str, ChannelData) + 'c, { unsafe { (*(csound_sys::csoundGetHostData(self.engine.csound) as *mut CallbackHandler)) .callbacks .output_channel_cb = Some(Box::new(f)); } self.enable_callback(CHANNEL_OUTPUT_CB); } /// Sets an external callback for receiving notices whenever Csound opens a file. /// The callback is made after the file is successfully opened. /// The following information is passed to the callback: /// ## `file_info` /// A [`FileInfo`](struct.FileInfo.html) struct containing the relevant file info. pub fn file_open_callback<'c, F>(&self, f: F) where F: FnMut(&FileInfo) + 'c, { unsafe { (*(csound_sys::csoundGetHostData(self.engine.csound) as *mut CallbackHandler)) .callbacks .file_open_cb = Some(Box::new(f)); } self.enable_callback(FILE_OPEN_CB); } /// Sets a function to be called by Csound for opening real-time MIDI input. /// This callback is used to inform to the user about the current MIDI input device. /// # Arguments /// * `user_func` A function/closure which will receive a reference /// to a str with the device name. pub fn midi_in_open_callback<'c, F>(&self, f: F) where F: FnMut(&str) + 'c, { unsafe { (*(csound_sys::csoundGetHostData(self.engine.csound) as *mut CallbackHandler)) .callbacks .midi_in_open_cb = Some(Box::new(f)); } self.enable_callback(MIDI_IN_OPEN_CB); } /// Sets a function to be called by Csound for opening real-time MIDI output. /// This callback is used to inform to the user about the current MIDI output device. /// # Arguments /// * `user_func` A function/closure which will receive a reference /// to a str with the device name. pub fn midi_out_open_callback<'c, F>(&self, f: F) where F: FnMut(&str) + 'c, { unsafe { (*(csound_sys::csoundGetHostData(self.engine.csound) as *mut CallbackHandler)) .callbacks .midi_out_open_cb = Some(Box::new(f)); } self.enable_callback(MIDI_OUT_OPEN_CB); } /// Sets a function to be called by Csound for reading from real time MIDI input. /// A reference to a buffer with audio samples is passed /// to the user function in the callback. The callback have to return the number of elements written to the buffer. pub fn midi_read_callback<'c, F>(&self, f: F) where F: FnMut(&mut [u8]) -> usize + 'c, { unsafe { (*(csound_sys::csoundGetHostData(self.engine.csound) as *mut CallbackHandler)) .callbacks .midi_read_cb = Some(Box::new(f)); } self.enable_callback(MIDI_READ_CB); } /// Sets a function to be called by Csound for Writing to real time MIDI input. /// A reference to the device buffer is passed /// to the user function in the callback. The passed buffer have the max length that /// the user is able to use, and the callback have to return the number of element written into the buffer. pub fn midi_write_callback<'c, F>(&self, f: F) where F: FnMut(&[u8]) -> usize + 'c, { unsafe { (*(csound_sys::csoundGetHostData(self.engine.csound) as *mut CallbackHandler)) .callbacks .midi_write_cb = Some(Box::new(f)); } self.enable_callback(MIDI_WRITE_CB); } /// Indicates to the user when csound has closed the midi input device. pub fn midi_in_close_callback<'c, F>(&self, f: F) where F: FnMut() + 'c, { unsafe { (*(csound_sys::csoundGetHostData(self.engine.csound) as *mut CallbackHandler)) .callbacks .midi_in_close_cb = Some(Box::new(f)); } self.enable_callback(MIDI_IN_CLOSE); } /// Indicates to the user when csound has closed the midi output device. pub fn midi_out_close_callback<'c, F>(&self, f: F) where F: FnMut() + 'c, { unsafe { (*(csound_sys::csoundGetHostData(self.engine.csound) as *mut CallbackHandler)) .callbacks .midi_out_close_cb = Some(Box::new(f)); } self.enable_callback(MIDI_OUT_CLOSE); } /// Called by external software to set a function for checking system events, yielding cpu time for coopertative multitasking, etc /// This function is optional. It is often used as a way to 'turn off' Csound, allowing it to exit gracefully. /// In addition, some operations like utility analysis routines are not reentrant /// and you should use this function to do any kind of updating during the operation. /// # Returns /// If this callback returns *false* it wont be called anymore pub fn yield_callback<'c, F>(&self, f: F) where F: FnMut() -> bool + 'c, { unsafe { (*(csound_sys::csoundGetHostData(self.engine.csound) as *mut CallbackHandler)) .callbacks .yield_cb = Some(Box::new(f)); } self.enable_callback(YIELD_CB); } fn enable_callback(&self, callback_type: u32) { match callback_type { SENSE_EVENT => unsafe { csound_sys::csoundRegisterSenseEventCallback( self.engine.csound, Some(Trampoline::senseEventCallback), ::std::ptr::null_mut() as *mut c_void, ); }, MESSAGE_CB => unsafe { csound_sys::csoundSetMessageStringCallback( self.engine.csound, Trampoline::message_string_cb, ) }, AUDIO_DEV_LIST => unsafe { csound_sys::csoundSetAudioDeviceListCallback( self.engine.csound, Some(Trampoline::audioDeviceListCallback), ); }, PLAY_OPEN => unsafe { csound_sys::csoundSetPlayopenCallback( self.engine.csound, Some(Trampoline::playOpenCallback), ); }, REC_OPEN => unsafe { csound_sys::csoundSetRecopenCallback( self.engine.csound, Some(Trampoline::recOpenCallback), ); }, REAL_TIME_PLAY => unsafe { csound_sys::csoundSetRtplayCallback( self.engine.csound, Some(Trampoline::rtplayCallback), ); }, REAL_TIME_REC => unsafe { csound_sys::csoundSetRtrecordCallback( self.engine.csound, Some(Trampoline::rtrecordCallback), ); }, /*KEYBOARD_CB => unsafe { let host_data_ptr = &*self.engine as *const _ as *const _; csound_sys::csoundRegisterKeyboardCallback( self.engine.csound, Some(keyboard_callback::<H>), host_data_ptr as *mut c_void, csound_sys::CSOUND_CALLBACK_KBD_EVENT | csound_sys::CSOUND_CALLBACK_KBD_TEXT, ); csound_sys::csoundKeyPress(self.engine.csound, '\n' as i8); },*/ RT_CLOSE_CB => unsafe { csound_sys::csoundSetRtcloseCallback( self.engine.csound, Some(Trampoline::rtcloseCallback), ); }, CSCORE_CB => unsafe { csound_sys::csoundSetCscoreCallback( self.engine.csound, Some(Trampoline::scoreCallback), ); }, CHANNEL_INPUT_CB => unsafe { csound_sys::csoundSetInputChannelCallback( self.engine.csound, Some(Trampoline::inputChannelCallback), ); }, CHANNEL_OUTPUT_CB => unsafe { csound_sys::csoundSetOutputChannelCallback( self.engine.csound, Some(Trampoline::outputChannelCallback), ); }, FILE_OPEN_CB => unsafe { csound_sys::csoundSetFileOpenCallback( self.engine.csound, Some(Trampoline::fileOpenCallback), ); }, MIDI_IN_OPEN_CB => unsafe { csound_sys::csoundSetExternalMidiInOpenCallback( self.engine.csound, Some(Trampoline::midiInOpenCallback), ); }, MIDI_OUT_OPEN_CB => unsafe { csound_sys::csoundSetExternalMidiOutOpenCallback( self.engine.csound, Some(Trampoline::midiOutOpenCallback), ); }, MIDI_READ_CB => unsafe { csound_sys::csoundSetExternalMidiReadCallback( self.engine.csound, Some(Trampoline::midiReadCallback), ); }, MIDI_WRITE_CB => unsafe { csound_sys::csoundSetExternalMidiWriteCallback( self.engine.csound, Some(Trampoline::midiWriteCallback), ); }, MIDI_IN_CLOSE => unsafe { csound_sys::csoundSetExternalMidiInCloseCallback( self.engine.csound, Some(Trampoline::midiInCloseCallback), ); }, MIDI_OUT_CLOSE => unsafe { csound_sys::csoundSetExternalMidiOutCloseCallback( self.engine.csound, Some(Trampoline::midiOutCloseCallback), ); }, YIELD_CB => unsafe { csound_sys::csoundSetYieldCallback( self.engine.csound, Some(Trampoline::yieldCallback), ); }, _ => {} } } } //End impl block // Drop method to free the memory using during the csound performance and instantiation impl Drop for Csound { fn drop(&mut self) { unsafe { csound_sys::csoundStop(self.engine.csound); csound_sys::csoundCleanup(self.engine.csound); let _ = Box::from_raw( csound_sys::csoundGetHostData(self.engine.csound) as *mut CallbackHandler ); // Checks if a message buffer exists and destroy it. let msg_buffer = self.engine.use_msg_buffer.borrow(); if *msg_buffer == true { csound_sys::csoundDestroyMessageBuffer(self.engine.csound); } csound_sys::csoundDestroy(self.engine.csound); } } } /// Csound's Circular Buffer object. /// This struct wraps a *mut T pointer to a circular buffer /// allocated by csound. This Circular buffer won't outlive /// the csound instance that allocated the buffer. pub struct CircularBuffer<'a, T: 'a + Copy> { csound: *mut csound_sys::CSOUND, ptr: *mut T, phantom: PhantomData<&'a T>, } impl<'a, T> CircularBuffer<'a, T> where T: Copy, { /// Read from circular buffer. /// # Arguments /// * `out` A mutable slice where the items will be copied. /// * `items` The number of elements to read and remove from the buffer. /// # Returns /// The number of items read **(0 <= n <= items)**. /// or an Error if the output buffer doesn't have enough capacity. pub fn read(&self, out: &mut [T], items: u32) -> Result<usize, &'static str> { if items as usize <= out.len() { return Err("your buffer has not enough capacity"); } unsafe { Ok(csound_sys::csoundReadCircularBuffer( self.csound, self.ptr as *mut c_void, out.as_mut_ptr() as *mut c_void, items as c_int, ) as usize) } } /// Read from circular buffer without removing them from the buffer. /// # Arguments /// * `out` A mutable slice where the items will be copied. /// * `items` The number of elements to peek from the buffer. /// # Returns /// The actual number of items read **(0 <= n <= items)**, or an error if the number of items /// to read/write exceeds the buffer's capacity. pub fn peek(&self, out: &mut [T], items: u32) -> Result<usize, &'static str> { if items as usize <= out.len() { return Err("your buffer has not enough capacity"); } unsafe { Ok(csound_sys::csoundPeekCircularBuffer( self.csound, self.ptr as *mut c_void, out.as_mut_ptr() as *mut c_void, items as c_int, ) as usize) } } /// Write to the circular buffer. /// # Arguments /// * `input` A slice with the date which will be copied into the buffer. /// * `items` The number of elements to wrtie into the buffer. /// # Returns /// The actual number of items written *(0 <= n <= items)**, or an error if the number of items /// to read/write exceeds the buffer's capacity. pub fn write(&self, input: &[T], items: u32) -> Result<usize, &'static str> { if items as usize <= input.len() { return Err("your buffer has not enough capacity"); } unsafe { Ok(csound_sys::csoundWriteCircularBuffer( self.csound, self.ptr as *mut c_void, input.as_ptr() as *const c_void, items as c_int, ) as usize) } } /// Empty circular buffer of any remaining data. /// This function should only be used if there is no reader actively getting data from the buffer. pub fn flush(&self) { unsafe { csound_sys::csoundFlushCircularBuffer(self.csound, self.ptr as *mut c_void); } } } impl<'a, T> Drop for CircularBuffer<'a, T> where T: Copy, { fn drop(&mut self) { unsafe { csound_sys::csoundDestroyCircularBuffer(self.csound, self.ptr as *mut c_void); } } } /// Csound table representation. /// This struct is build up to manipulate directly a csound's table. #[derive(Debug)] pub struct Table<'a> { ptr: *mut f64, length: usize, phantom: PhantomData<&'a f64>, } impl<'a> Table<'a> { /// # Returns /// The table length pub fn get_size(&self) -> usize { self.length } /// # Returns /// A slice representation with the table's internal data pub fn as_slice(&self) -> &[f64] { unsafe { slice::from_raw_parts(self.ptr, self.length) } } /// # Returns /// A mutable slice representation with the table's internal data pub fn as_mut_slice(&mut self) -> &mut [f64] { unsafe { slice::from_raw_parts_mut(self.ptr, self.length) } } /// method used to copy data from the table internal buffer /// into an user buffer. A error message is returned if the Table is not longer valid. /// # Arguments /// * `slice` A slice where out.len() elements from the table will be copied. /// # Returns /// The number of elements copied into the output slice. /// # Example /// ``` /// let cs = Csound::new(); /// cs.compile_csd("some.csd"); /// cs.start().unwrap(); /// while cs.perform_ksmps() == false { /// let mut table = cs.get_table(1).unwrap(); /// let mut table_buff = vec![0f64; table.length]; /// // copy Table::length elements from the table's internal buffer /// table.copy_to_slice( table_buff.as_mut_slice() ).unwrap(); /// // Do some stuffs /// } /// ``` pub fn copy_to_slice(&self, slice: &mut [f64]) -> usize { let mut len = slice.len(); let size = self.get_size(); if size < len { len = size; } unsafe { std::ptr::copy(self.ptr, slice.as_mut_ptr(), len); len } } /// method used to copy data into the table internal buffer /// from an user slice. /// # Arguments /// * `slice` A slice where input.len() elements will be copied. /// # Returns /// The number of elements copied into the table /// # Example /// ``` /// let cs = Csound::new(); /// cs.compile_csd("some.csd"); /// cs.start().unwrap(); /// while cs.perform_ksmps() == false { /// let mut table = cs.get_table(1).unwrap(); /// let mut table_buff = vec![0f64; table.length]; /// // copy Table::length elements from the table's internal buffer /// table.read( table_buff.as_mut_slice() ).unwrap(); /// // Do some stuffs /// table.copy_from_slice(&table_buff.into_iter().map(|x| x*2.5).collect::<Vec<f64>>().as_mut_slice()); /// // Do some stuffs /// } /// ``` pub fn copy_from_slice(&self, slice: &[f64]) -> usize { let mut len = slice.len(); let size = self.get_size(); if size < len { len = size; } unsafe { std::ptr::copy(slice.as_ptr(), self.ptr, len); len } } } impl<'a> AsRef<[f64]> for Table<'a> { fn as_ref(&self) -> &[f64] { self.as_slice() } } impl<'a> AsMut<[f64]> for Table<'a> { fn as_mut(&mut self) -> &mut [f64] { self.as_mut_slice() } } impl<'a> Deref for Table<'a> { type Target = [f64]; fn deref(&self) -> &[f64] { self.as_slice() } } impl<'a> DerefMut for Table<'a> { fn deref_mut(&mut self) -> &mut [f64] { self.as_mut_slice() } } pub enum Readable {} pub enum Writable {} /// Csound buffer pointer representation. /// This struct is build up to manipulate directly csound's buffers. pub struct BufferPtr<'a, T> { ptr: *mut f64, len: usize, phantom: PhantomData<&'a T>, } impl<'a, T> BufferPtr<'a, T> { /// # Returns /// The buffer length pub fn get_size(&self) -> usize { self.len } /// This method is used to copy data from the csound's buffer /// into another slice. /// # Arguments /// * `slice` A mutable slice where the data will be copy /// # Returns /// The number of elements copied into the slice. pub fn copy_to_slice(&self, slice: &mut [f64]) -> usize { let mut len = slice.len(); let size = self.get_size(); if size < len { len = size; } unsafe { std::ptr::copy(self.ptr, slice.as_mut_ptr(), len); len } } /// # Returns /// A slice to the buffer internal data pub fn as_slice(&self) -> &[f64] { unsafe { slice::from_raw_parts(self.ptr, self.len) } } } impl<'a> BufferPtr<'a, Writable> { /// # Returns /// This buffer pointer as a mutable slice. pub fn as_mut_slice(&mut self) -> &mut [f64] { unsafe { slice::from_raw_parts_mut(self.ptr, self.len) } } /// method used to copy data into this buffer /// # Arguments /// * `slice` A slice with samples to copy /// # Returns /// The number of elements copied into the csound's buffer. pub fn copy_from_slice(&self, slice: &[f64]) -> usize { let mut len = slice.len(); let size = self.get_size(); if size < len { len = size; } unsafe { std::ptr::copy(slice.as_ptr(), self.ptr, len); len } } } impl<'a, T> AsRef<[f64]> for BufferPtr<'a, T> { fn as_ref(&self) -> &[f64] { self.as_slice() } } impl<'a> AsMut<[f64]> for BufferPtr<'a, Writable> { fn as_mut(&mut self) -> &mut [f64] { self.as_mut_slice() } } impl<'a, T> Deref for BufferPtr<'a, T> { type Target = [f64]; fn deref(&self) -> &[f64] { self.as_slice() } } impl<'a> DerefMut for BufferPtr<'a, Writable> { fn deref_mut(&mut self) -> &mut [f64] { self.as_mut_slice() } } /// Rust representation for a raw csound channel pointer /// /// Still in high development so changes might occur. /// currently String channel is not supported. /// # Deprecated /// use [`ChannelPtr`](struct.ChannelPtr.html) instead #[derive(Debug)] pub struct ControlChannelPtr<'a> { ptr: *mut f64, len: usize, channel_type: ControlChannelType, phantom: PhantomData<&'a f64>, } impl<'a> ControlChannelPtr<'a> { /// # Returns /// The channel length pub fn get_size(&self) -> usize { self.len } pub fn read<T: Copy>(&self, dest: &mut [T]) -> Result<usize, io::Error> { let mut len: usize = dest.len(); if self.len < len { len = self.len; } if self.len == 0 { return Err(io::Error::new( io::ErrorKind::Other, format!( "Missing data: requesting {} but only got {}.", len, self.len ), )); } unsafe { std::ptr::copy_nonoverlapping(self.ptr as *const T, dest.as_mut_ptr(), len); } Ok(len) } pub fn write<T: Copy>(&self, src: &[T]) -> Result<usize, io::Error> { let mut len: usize = src.len(); if self.len < len { len = self.len; } if self.len == 0 { return Err(io::Error::new( io::ErrorKind::Other, format!( "Not memory for data: writing {} but only got {}.", len, self.len ), )); } unsafe { std::ptr::copy_nonoverlapping(src.as_ptr(), self.ptr as *mut T, len); } Ok(len) } }