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 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432
|
/*
SuperCollider real time audio synthesis system
Copyright (c) 2002 James McCartney. All rights reserved.
http://www.audiosynth.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "SC_PlugIn.h"
#include "SIMD_Unit.hpp"
#include <limits.h>
#include <cstdio>
#include "function_attributes.h"
#include <boost/align/is_aligned.hpp>
static InterfaceTable* ft;
struct Vibrato : public Unit {
double mPhase, m_attackSlope, m_attackLevel;
float mFreqMul, m_scaleA, m_scaleB, mFreq;
int m_delay, m_attack;
float trig;
};
struct LFPulse : public Unit {
double mPhase;
float mFreqMul, mDuty;
};
struct LFSaw : public Unit {
double mPhase;
float mFreqMul;
};
struct LFPar : public Unit {
double mPhase;
float mFreqMul;
};
struct LFCub : public Unit {
double mPhase;
float mFreqMul;
};
struct LFTri : public Unit {
double mPhase;
float mFreqMul;
};
struct LFGauss : public Unit {
double mPhase;
float mDurMul;
};
struct Impulse : public Unit {
double mPhase, mPhaseOffset, mPhaseIncrement;
float mFreqMul;
};
struct VarSaw : public Unit {
double mPhase;
float mFreqMul, mDuty, mInvDuty, mInv1Duty;
};
struct SyncSaw : public Unit {
double mPhase1, mPhase2;
float mFreqMul;
};
struct Line : public Unit {
double mLevel, mSlope;
float mEndLevel;
int mCounter;
};
struct XLine : public Unit {
double mLevel, mGrowth;
float mEndLevel;
int mCounter;
};
struct Cutoff : public Unit {
double mLevel, mSlope;
int mWaitCounter;
};
struct LinExp : public Unit {
float m_dstratio, m_rsrcrange, m_rrminuslo, m_dstlo;
};
struct Clip : public Unit {
float m_lo, m_hi;
};
struct Wrap : public Unit {
float m_lo, m_hi;
};
struct Fold : public Unit {
float m_lo, m_hi, m_range;
};
struct Unwrap : public Unit {
float m_range, m_half, m_offset, m_prev;
};
struct ModDif : public Unit {
float m_dif, m_mod;
};
struct AmpComp : public Unit {
float m_rootmul, m_exponent;
};
struct AmpCompA : public Unit {
double m_scale, m_offset;
};
struct InRange : public Unit {
// nothing
};
struct InRect : public Unit {
// nothing
};
// struct Trapezoid : public Unit
//{
// float m_leftScale, m_rightScale, m_a, m_b, m_c, m_d;
//};
struct A2K : public Unit {};
struct T2K : public Unit {};
struct T2A : public Unit {
float mLevel;
};
struct EnvGen : public Unit {
double m_a1, m_a2, m_b1, m_y1, m_y2, m_grow, m_level, m_endLevel;
int m_counter, m_stage, m_shape, m_releaseNode;
float m_prevGate;
bool m_released;
};
struct Linen : public Unit {
float m_endLevel;
double m_slope, m_level;
int m_counter, m_stage;
float m_prevGate;
};
//////////////////////////////////////////////////////////////////////////////////////////////////
extern "C" {
void Vibrato_next(Vibrato* unit, int inNumSamples);
void Vibrato_Ctor(Vibrato* unit);
void LFPulse_next_a(LFPulse* unit, int inNumSamples);
void LFPulse_next_k(LFPulse* unit, int inNumSamples);
void LFPulse_Ctor(LFPulse* unit);
void LFSaw_next_a(LFSaw* unit, int inNumSamples);
void LFSaw_next_k(LFSaw* unit, int inNumSamples);
void LFSaw_Ctor(LFSaw* unit);
void LFTri_next_a(LFTri* unit, int inNumSamples);
void LFTri_next_k(LFTri* unit, int inNumSamples);
void LFTri_Ctor(LFTri* unit);
void LFPar_next_a(LFPar* unit, int inNumSamples);
void LFPar_next_k(LFPar* unit, int inNumSamples);
void LFPar_Ctor(LFPar* unit);
void LFCub_next_a(LFCub* unit, int inNumSamples);
void LFCub_next_k(LFCub* unit, int inNumSamples);
void LFCub_Ctor(LFCub* unit);
void LFGauss_next_a(LFGauss* unit, int inNumSamples);
void LFGauss_next_k(LFGauss* unit, int inNumSamples);
void LFGauss_next_aa(LFGauss* unit, int inNumSamples);
void LFGauss_Ctor(LFGauss* unit);
void VarSaw_next_a(VarSaw* unit, int inNumSamples);
void VarSaw_next_k(VarSaw* unit, int inNumSamples);
void VarSaw_Ctor(VarSaw* unit);
void Impulse_next_aa(Impulse* unit, int inNumSamples);
void Impulse_next_ak(Impulse* unit, int inNumSamples);
void Impulse_next_ai(Impulse* unit, int inNumSamples);
void Impulse_next_kk(Impulse* unit, int inNumSamples);
void Impulse_next_ki(Impulse* unit, int inNumSamples);
void Impulse_next_ik(Impulse* unit, int inNumSamples);
void Impulse_next_ii(Impulse* unit, int inNumSamples);
void Impulse_Ctor(Impulse* unit);
void SyncSaw_next_aa(SyncSaw* unit, int inNumSamples);
void SyncSaw_next_ak(SyncSaw* unit, int inNumSamples);
void SyncSaw_next_ka(SyncSaw* unit, int inNumSamples);
void SyncSaw_next_kk(SyncSaw* unit, int inNumSamples);
void SyncSaw_Ctor(SyncSaw* unit);
void A2K_next(A2K* unit, int inNumSamples);
void A2K_Ctor(A2K* unit);
void T2K_next(T2K* unit, int inNumSamples);
void T2K_Ctor(T2K* unit);
void T2A_next(T2A* unit, int inNumSamples);
void T2A_Ctor(T2A* unit);
void Line_next(Line* unit, int inNumSamples);
void Line_Ctor(Line* unit);
void XLine_next(XLine* unit, int inNumSamples);
void XLine_Ctor(XLine* unit);
void Wrap_next_kk(Wrap* unit, int inNumSamples);
void Wrap_next_ak(Wrap* unit, int inNumSamples);
void Wrap_next_ka(Wrap* unit, int inNumSamples);
void Wrap_next_aa(Wrap* unit, int inNumSamples);
void Wrap_Ctor(Wrap* unit);
void Fold_next_kk(Fold* unit, int inNumSamples);
void Fold_next_ak(Fold* unit, int inNumSamples);
void Fold_next_ka(Fold* unit, int inNumSamples);
void Fold_next_aa(Fold* unit, int inNumSamples);
void Fold_Ctor(Fold* unit);
void Clip_next_kk(Clip* unit, int inNumSamples);
void Clip_next_ka(Clip* unit, int inNumSamples);
void Clip_next_ak(Clip* unit, int inNumSamples);
void Clip_next_aa(Clip* unit, int inNumSamples);
void Clip_Ctor(Clip* unit);
void Unwrap_next(Unwrap* unit, int inNumSamples);
void Unwrap_Ctor(Unwrap* unit);
void ModDif_next_kk(ModDif* unit, int inNumSamples);
void ModDif_next_ak(ModDif* unit, int inNumSamples);
void ModDif_next_ka(ModDif* unit, int inNumSamples);
void ModDif_next_aa(ModDif* unit, int inNumSamples);
void ModDif_Ctor(ModDif* unit);
void AmpComp_next(AmpComp* unit, int inNumSamples);
void AmpComp_Ctor(AmpComp* unit);
void AmpCompA_next(AmpCompA* unit, int inNumSamples);
void AmpCompA_Ctor(AmpCompA* unit);
void InRange_next(InRange* unit, int inNumSamples);
void InRange_Ctor(InRange* unit);
void InRect_next(InRect* unit, int inNumSamples);
void InRect_Ctor(InRect* unit);
void LinExp_next(LinExp* unit, int inNumSamples);
void LinExp_next_kk(LinExp* unit, int inNumSamples);
void LinExp_next_ak(LinExp* unit, int inNumSamples);
void LinExp_next_ka(LinExp* unit, int inNumSamples);
void LinExp_Ctor(LinExp* unit);
void EnvGen_next_k(EnvGen* unit, int inNumSamples);
void EnvGen_next_aa(EnvGen* unit, int inNumSamples);
void EnvGen_next_ak(EnvGen* unit, int inNumSamples);
void EnvGen_Ctor(EnvGen* unit);
void Linen_next_k(Linen* unit, int inNumSamples);
void Linen_Ctor(Linen* unit);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// in, rate, depth, rateVariation, depthVariation
// 0 1 2 3 4
void Vibrato_next(Vibrato* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float curtrig = ZIN0(8);
if (unit->trig <= 0.f && curtrig > 0.f) {
unit->mFreqMul = 4.0 * SAMPLEDUR;
unit->mPhase = 4.0 * sc_wrap(ZIN0(7), 0.f, 1.f) - 1.0;
RGen& rgen = *unit->mParent->mRGen;
float rate = ZIN0(1) * unit->mFreqMul;
float depth = ZIN0(2);
float rateVariation = ZIN0(5);
float depthVariation = ZIN0(6);
unit->mFreq = rate * (1.f + rateVariation * rgen.frand2());
unit->m_scaleA = depth * (1.f + depthVariation * rgen.frand2());
unit->m_scaleB = depth * (1.f + depthVariation * rgen.frand2());
unit->m_delay = (int)(ZIN0(3) * SAMPLERATE);
unit->m_attack = (int)(ZIN0(4) * SAMPLERATE);
unit->m_attackSlope = 1. / (double)(1 + unit->m_attack);
unit->m_attackLevel = unit->m_attackSlope;
}
unit->trig = curtrig;
double ffreq = unit->mFreq;
double phase = unit->mPhase;
float scaleA = unit->m_scaleA;
float scaleB = unit->m_scaleB;
if (unit->m_delay > 0) {
int remain = sc_min(inNumSamples, unit->m_delay);
unit->m_delay -= remain;
inNumSamples -= remain;
LOOP(remain, ZXP(out) = ZXP(in););
if (unit->m_delay <= 0 && inNumSamples > 0) {
if (unit->m_attack > 0)
goto doAttack;
else
goto doNormal;
}
} else if (unit->m_attack) {
doAttack:
int remain = sc_min(inNumSamples, unit->m_attack);
unit->m_attack -= remain;
inNumSamples -= remain;
double attackSlope = unit->m_attackSlope;
double attackLevel = unit->m_attackLevel;
LOOP(
remain,
if (phase < 1.f) {
float z = phase;
ZXP(out) = ZXP(in) * (1.f + (float)attackLevel * scaleA * (1.f - z * z));
} else if (phase < 3.f) {
float z = phase - 2.f;
ZXP(out) = ZXP(in) * (1.f + (float)attackLevel * scaleB * (z * z - 1.f));
} else {
phase -= 4.f;
float z = phase;
float depth = ZIN0(2);
float rateVariation = ZIN0(5);
float depthVariation = ZIN0(6);
float rate = ZIN0(1) * unit->mFreqMul;
RGen& rgen = *unit->mParent->mRGen;
ffreq = rate * (1.f + rateVariation * rgen.frand2());
scaleA = depth * (1.f + depthVariation * rgen.frand2());
scaleB = depth * (1.f + depthVariation * rgen.frand2());
ZXP(out) = ZXP(in) * (1.f + (float)attackLevel * scaleA * (1.f - z * z));
} phase += ffreq;
attackLevel += attackSlope;);
unit->m_attackLevel = attackLevel;
if (unit->m_attack <= 0 && inNumSamples > 0)
goto doNormal;
} else {
doNormal:
LOOP1(
inNumSamples,
if (phase < 1.f) {
float z = phase;
ZXP(out) = ZXP(in) * (1.f + scaleA * (1.f - z * z));
} else if (phase < 3.f) {
float z = phase - 2.f;
ZXP(out) = ZXP(in) * (1.f + scaleB * (z * z - 1.f));
} else {
phase -= 4.f;
float z = phase;
float depth = ZIN0(2);
float rateVariation = ZIN0(5);
float depthVariation = ZIN0(6);
float rate = ZIN0(1) * unit->mFreqMul;
RGen& rgen = *unit->mParent->mRGen;
ffreq = rate * (1.f + rateVariation * rgen.frand2());
scaleA = depth * (1.f + depthVariation * rgen.frand2());
scaleB = depth * (1.f + depthVariation * rgen.frand2());
ZXP(out) = ZXP(in) * (1.f + scaleA * (1.f - z * z));
} phase += ffreq;);
}
unit->mPhase = phase;
unit->mFreq = ffreq;
unit->m_scaleA = scaleA;
unit->m_scaleB = scaleB;
}
void Vibrato_Ctor(Vibrato* unit) {
unit->mFreqMul = 4.0 * SAMPLEDUR;
unit->mPhase = 4.0 * sc_wrap(ZIN0(7), 0.f, 1.f) - 1.0;
RGen& rgen = *unit->mParent->mRGen;
float rate = ZIN0(1) * unit->mFreqMul;
float depth = ZIN0(2);
float rateVariation = ZIN0(5);
float depthVariation = ZIN0(6);
unit->mFreq = rate * (1.f + rateVariation * rgen.frand2());
unit->m_scaleA = depth * (1.f + depthVariation * rgen.frand2());
unit->m_scaleB = depth * (1.f + depthVariation * rgen.frand2());
unit->m_delay = (int)(ZIN0(3) * SAMPLERATE);
unit->m_attack = (int)(ZIN0(4) * SAMPLERATE);
unit->m_attackSlope = 1. / (double)(1 + unit->m_attack);
unit->m_attackLevel = unit->m_attackSlope;
unit->trig = 0.0f;
SETCALC(Vibrato_next);
Vibrato_next(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void LFPulse_next_a(LFPulse* unit, int inNumSamples) {
float* out = ZOUT(0);
float* freq = ZIN(0);
float nextDuty = ZIN0(2);
float duty = unit->mDuty;
float freqmul = unit->mFreqMul;
double phase = unit->mPhase;
LOOP1(
inNumSamples, float z; if (phase >= 1.f) {
phase -= 1.f;
duty = unit->mDuty = nextDuty;
// output at least one sample from the opposite polarity
z = duty <= 0.5f ? 1.f : 0.f;
} else { z = phase < duty ? 1.f : 0.f; } phase += ZXP(freq) * freqmul;
ZXP(out) = z;);
unit->mPhase = phase;
}
void LFPulse_next_k(LFPulse* unit, int inNumSamples) {
float* out = ZOUT(0);
float freq = ZIN0(0) * unit->mFreqMul;
float nextDuty = ZIN0(2);
float duty = unit->mDuty;
double phase = unit->mPhase;
LOOP1(
inNumSamples, float z; if (phase >= 1.f) {
phase -= 1.f;
duty = unit->mDuty = nextDuty;
// output at least one sample from the opposite polarity
z = duty <= 0.5f ? 1.f : 0.f;
} else { z = phase < duty ? 1.f : 0.f; } phase += freq;
ZXP(out) = z;);
unit->mPhase = phase;
}
void LFPulse_Ctor(LFPulse* unit) {
if (INRATE(0) == calc_FullRate) {
SETCALC(LFPulse_next_a);
} else {
SETCALC(LFPulse_next_k);
}
unit->mFreqMul = unit->mRate->mSampleDur;
unit->mPhase = ZIN0(1);
unit->mDuty = ZIN0(2);
LFPulse_next_k(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void LFSaw_next_a(LFSaw* unit, int inNumSamples) {
float* out = ZOUT(0);
float* freq = ZIN(0);
float freqmul = unit->mFreqMul;
double phase = unit->mPhase;
LOOP1(inNumSamples,
float z = phase; // out must be written last for in place operation
phase += ZXP(freq) * freqmul; if (phase >= 1.f) phase -= 2.f; else if (phase <= -1.f) phase += 2.f;
ZXP(out) = z;);
unit->mPhase = phase;
}
void LFSaw_next_k(LFSaw* unit, int inNumSamples) {
float* out = ZOUT(0);
float freq = ZIN0(0) * unit->mFreqMul;
double phase = unit->mPhase;
if (freq >= 0.f) {
LOOP1(inNumSamples, ZXP(out) = phase; phase += freq; if (phase >= 1.f) phase -= 2.f;);
} else {
LOOP1(inNumSamples, ZXP(out) = phase; phase += freq; if (phase <= -1.f) phase += 2.f;);
}
unit->mPhase = phase;
}
void LFSaw_Ctor(LFSaw* unit) {
if (INRATE(0) == calc_FullRate)
SETCALC(LFSaw_next_a);
else
SETCALC(LFSaw_next_k);
unit->mFreqMul = 2.0 * unit->mRate->mSampleDur;
unit->mPhase = ZIN0(1);
LFSaw_next_k(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void LFPar_next_a(LFPar* unit, int inNumSamples) {
float* out = ZOUT(0);
float* freq = ZIN(0);
float freqmul = unit->mFreqMul;
double phase = unit->mPhase;
float z, y;
LOOP1(
inNumSamples,
if (phase < 1.f) {
z = phase;
y = 1.f - z * z;
} else if (phase < 3.f) {
z = phase - 2.f;
y = z * z - 1.f;
} else {
phase -= 4.f;
z = phase;
y = 1.f - z * z;
}
// Note: the following two lines were originally one, but seems to compile wrong on mac
float phaseadd = ZXP(freq);
phase += phaseadd * freqmul; ZXP(out) = y;);
unit->mPhase = phase;
}
void LFPar_next_k(LFPar* unit, int inNumSamples) {
float* out = ZOUT(0);
float freq = ZIN0(0) * unit->mFreqMul;
double phase = unit->mPhase;
LOOP1(
inNumSamples,
if (phase < 1.f) {
float z = phase;
ZXP(out) = 1.f - z * z;
} else if (phase < 3.f) {
float z = phase - 2.f;
ZXP(out) = z * z - 1.f;
} else {
phase -= 4.f;
float z = phase;
ZXP(out) = 1.f - z * z;
} phase += freq;);
unit->mPhase = phase;
}
void LFPar_Ctor(LFPar* unit) {
if (INRATE(0) == calc_FullRate)
SETCALC(LFPar_next_a);
else
SETCALC(LFPar_next_k);
unit->mFreqMul = 4.0 * unit->mRate->mSampleDur;
unit->mPhase = ZIN0(1);
LFPar_next_k(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void LFCub_next_a(LFCub* unit, int inNumSamples) {
float* out = ZOUT(0);
float* freq = ZIN(0);
float freqmul = unit->mFreqMul;
double phase = unit->mPhase;
LOOP1(
inNumSamples, float z; if (phase < 1.f) { z = phase; } else if (phase < 2.f) { z = 2.f - phase; } else {
phase -= 2.f;
z = phase;
} float phaseadd = ZXP(freq);
phase += phaseadd * freqmul; ZXP(out) = z * z * (6.f - 4.f * z) - 1.f;);
unit->mPhase = phase;
}
void LFCub_next_k(LFCub* unit, int inNumSamples) {
float* out = ZOUT(0);
float freq = ZIN0(0) * unit->mFreqMul;
double phase = unit->mPhase;
LOOP1(
inNumSamples, float z; if (phase < 1.f) { z = phase; } else if (phase < 2.f) { z = 2.f - phase; } else {
phase -= 2.f;
z = phase;
} ZXP(out) = z * z * (6.f - 4.f * z) - 1.f;
phase += freq;);
unit->mPhase = phase;
}
void LFCub_Ctor(LFCub* unit) {
if (INRATE(0) == calc_FullRate)
SETCALC(LFCub_next_a);
else
SETCALC(LFCub_next_k);
unit->mFreqMul = 2.0 * unit->mRate->mSampleDur;
unit->mPhase = ZIN0(1) + 0.5;
LFCub_next_k(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void LFTri_next_a(LFTri* unit, int inNumSamples) {
float* out = ZOUT(0);
float* freq = ZIN(0);
float freqmul = unit->mFreqMul;
double phase = unit->mPhase;
LOOP1(inNumSamples, float z = phase > 1.f ? 2.f - phase : phase; phase += ZXP(freq) * freqmul;
if (phase >= 3.f) phase -= 4.f; ZXP(out) = z;);
unit->mPhase = phase;
}
void LFTri_next_k(LFTri* unit, int inNumSamples) {
float* out = ZOUT(0);
float freq = ZIN0(0) * unit->mFreqMul;
double phase = unit->mPhase;
LOOP1(inNumSamples, float z = phase > 1.f ? 2.f - phase : phase; phase += freq; if (phase >= 3.f) phase -= 4.f;
ZXP(out) = z;);
unit->mPhase = phase;
}
void LFTri_Ctor(LFTri* unit) {
if (INRATE(0) == calc_FullRate) {
SETCALC(LFTri_next_a);
} else {
SETCALC(LFTri_next_k);
}
unit->mFreqMul = 4.0 * unit->mRate->mSampleDur;
unit->mPhase = ZIN0(1);
LFTri_next_k(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void LFGauss_next_k(LFGauss* unit, int inNumSamples) {
float* out = ZOUT(0);
float dur = ZIN0(0);
float c = ZIN0(1);
float b = ZIN0(2);
float loop = ZIN0(3);
// offset phase by b
double x = unit->mPhase - b;
// for a full cycle from -1 to 1 in duration, double the step.
float step = 2.f / (dur * unit->mRate->mSampleRate);
// calculate exponent only once per loop
float factor = -1.f / (2.f * c * c);
LOOP1(
inNumSamples,
if (x > 1.f) {
if (loop) {
x -= 2.f;
} else {
DoneAction(ZIN0(4), unit);
}
} ZXP(out) = exp(x * x * factor);
x += step;);
unit->mPhase = x + b;
}
void LFGauss_next_a(LFGauss* unit, int inNumSamples) {
float* out = ZOUT(0);
float* dur = ZIN(0);
float c = ZIN0(1);
float b = ZIN0(2);
float loop = ZIN0(3);
float sr = unit->mRate->mSampleRate;
// offset phase by b
double x = unit->mPhase - b;
float factor = -1.f / (2.f * c * c);
LOOP1(
inNumSamples,
if (x > 1.f) {
if (loop) {
x -= 2.f;
} else {
DoneAction(ZIN0(4), unit);
}
}
// for a full cycle from -1 to 1 in duration, double the step.
float step = 2.f / (ZXP(dur) * sr);
ZXP(out) = exp(x * x * factor);
x += step;);
unit->mPhase = x + b;
}
void LFGauss_next_aa(LFGauss* unit, int inNumSamples) {
float* out = ZOUT(0);
float* dur = ZIN(0);
float* c = ZIN(1);
float b = ZIN0(2);
float loop = ZIN0(3);
float sr = unit->mRate->mSampleRate;
// offset phase by b
double x = unit->mPhase - b;
LOOP1(
inNumSamples,
if (x > 1.f) {
if (loop) {
x -= 2.f;
} else {
DoneAction(ZIN0(4), unit);
}
}
// for a full cycle from -1 to 1 in duration, double the step.
float step = 2.f / (ZXP(dur) * sr);
float cval = ZXP(c);
float factor = -1.f / (2.f * cval * cval); ZXP(out) = exp(x * x * factor);
x += step;);
unit->mPhase = x + b;
}
void LFGauss_Ctor(LFGauss* unit) {
if (INRATE(0) == calc_FullRate) {
if (INRATE(1) == calc_FullRate) {
SETCALC(LFGauss_next_aa);
} else {
SETCALC(LFGauss_next_a);
}
} else {
SETCALC(LFGauss_next_k);
}
unit->mPhase = -1.0;
LFGauss_next_k(unit, 1);
// reset phase
unit->mPhase = -1.0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// detect if phasor is out-of-bounds, trigger and wrap [0, 1]
static inline float Impulse_testWrapPhase(double prev_inc, double& phase) {
if (prev_inc < 0.f) { // negative freqs
if (phase <= 0.f) {
phase += 1.f;
if (phase <= 0.f) { // catch large phase jumps
phase -= sc_ceil(phase);
}
return 1.f;
} else {
return 0.f;
}
} else { // positive freqs
if (phase >= 1.f) {
phase -= 1.f;
if (phase >= 1.f) {
phase -= sc_floor(phase);
}
return 1.f;
} else {
return 0.f;
}
}
}
void Impulse_next_ii(Impulse* unit, int inNumSamples) {
float* out = ZOUT(0);
double phase = unit->mPhase;
double inc = unit->mPhaseIncrement;
LOOP1(inNumSamples, ZXP(out) = Impulse_testWrapPhase(inc, phase); phase += inc;);
unit->mPhase = phase;
}
void Impulse_next_ik(Impulse* unit, int inNumSamples) {
float* out = ZOUT(0);
double phase = unit->mPhase;
double inc = unit->mPhaseIncrement;
double prev_off = unit->mPhaseOffset;
double off = ZIN0(1);
double phaseSlope = CALCSLOPE(off, prev_off);
bool phOffChanged = phaseSlope != 0.f;
LOOP1(
inNumSamples, ZXP(out) = Impulse_testWrapPhase(inc, phase);
if (phOffChanged) {
phase += phaseSlope;
Impulse_testWrapPhase(inc, phase);
} phase += inc;);
unit->mPhase = phase;
unit->mPhaseOffset = off;
}
void Impulse_next_ki(Impulse* unit, int inNumSamples) {
float* out = ZOUT(0);
double phase = unit->mPhase;
double prev_inc = unit->mPhaseIncrement;
double inc = ZIN0(0) * unit->mFreqMul;
double incSlope = CALCSLOPE(inc, prev_inc);
LOOP1(inNumSamples, ZXP(out) = Impulse_testWrapPhase(prev_inc, phase);
prev_inc += incSlope; phase += prev_inc;);
unit->mPhase = phase;
unit->mPhaseIncrement = inc;
}
void Impulse_next_kk(Impulse* unit, int inNumSamples) {
float* out = ZOUT(0);
double phase = unit->mPhase;
double prev_inc = unit->mPhaseIncrement;
double inc = ZIN0(0) * unit->mFreqMul;
double incSlope = CALCSLOPE(inc, prev_inc);
double prev_off = unit->mPhaseOffset;
double off = ZIN0(1);
double phaseSlope = CALCSLOPE(off, prev_off);
bool phOffChanged = phaseSlope != 0.f;
LOOP1(
inNumSamples, ZXP(out) = Impulse_testWrapPhase(prev_inc, phase);
if (phOffChanged) {
phase += phaseSlope;
Impulse_testWrapPhase(prev_inc, phase);
} prev_inc += incSlope;
phase += prev_inc;);
unit->mPhase = phase;
unit->mPhaseOffset = off;
unit->mPhaseIncrement = inc;
}
void Impulse_next_ak(Impulse* unit, int inNumSamples) {
float* out = ZOUT(0);
double phase = unit->mPhase;
double inc = unit->mPhaseIncrement;
float* freqIn = ZIN(0);
float freqMul = unit->mFreqMul;
double prev_off = unit->mPhaseOffset;
double off = ZIN0(1);
double offSlope = CALCSLOPE(off, prev_off);
bool offChanged = offSlope != 0.f;
LOOP1(
inNumSamples, float z = Impulse_testWrapPhase(inc, phase); if (offChanged) {
phase += offSlope;
Impulse_testWrapPhase(inc, phase);
} inc = ZXP(freqIn) * freqMul;
ZXP(out) = z; phase += inc;);
unit->mPhase = phase;
unit->mPhaseOffset = off;
unit->mPhaseIncrement = inc;
}
void Impulse_next_aa(Impulse* unit, int inNumSamples) {
float* out = ZOUT(0);
double phase = unit->mPhase;
double inc = unit->mPhaseIncrement;
float* freqin = ZIN(0);
float freqmul = unit->mFreqMul;
double prev_off = unit->mPhaseOffset;
float* offIn = ZIN(1);
LOOP1(inNumSamples, float z = Impulse_testWrapPhase(inc, phase); float off = ZXP(offIn);
float offInc = off - prev_off; phase += offInc; Impulse_testWrapPhase(inc, phase);
inc = ZXP(freqin) * freqmul; ZXP(out) = z;
phase += inc; prev_off = off;);
unit->mPhase = phase;
unit->mPhaseOffset = prev_off;
unit->mPhaseIncrement = inc;
}
void Impulse_next_ai(Impulse* unit, int inNumSamples) {
float* out = ZOUT(0);
double phase = unit->mPhase;
double inc = unit->mPhaseIncrement;
float* freqin = ZIN(0);
float freqmul = unit->mFreqMul;
LOOP1(inNumSamples, float z = Impulse_testWrapPhase(inc, phase); inc = ZXP(freqin) * freqmul; ZXP(out) = z;
phase += inc;);
unit->mPhase = phase;
unit->mPhaseIncrement = inc;
}
// Impulse is based on a wrapping phasor. When the phase wraps, an impulse is
// output. Phase _increments_ according to its frequency and an additional phase
// _offset_ is applied.
// Order of operations:
// 1. Phase _offset_ is applied to the current phase (if offset has changed).
// 2. Phase is wrapped into range.
// 3. Phase _increment_ is added (according to the frequency).
// 4. Phase is checked for being out of range, in which case a trigger is fired
// and the phase is again wrapped.
// Therefore, phase increment (freq) triggers an impulse, but not phase offset.
void Impulse_Ctor(Impulse* unit) {
unit->mPhaseOffset = ZIN0(1);
unit->mFreqMul = unit->mRate->mSampleDur;
unit->mPhaseIncrement = ZIN0(0) * unit->mFreqMul;
double initOff = unit->mPhaseOffset;
double initInc = unit->mPhaseIncrement;
double initPhase = sc_wrap(initOff, 0.0, 1.0);
// Initial phase offset of 0 means output of 1 on first sample.
// Set phase to wrap point to trigger impulse on first sample
if (initPhase == 0.0 && initInc >= 0.0) {
initPhase = 1.0; // positive frequency trigger/wrap position
}
unit->mPhase = initPhase;
UnitCalcFunc func;
switch (INRATE(0)) {
case calc_FullRate:
switch (INRATE(1)) {
case calc_ScalarRate:
func = (UnitCalcFunc)Impulse_next_ai;
break;
case calc_BufRate:
func = (UnitCalcFunc)Impulse_next_ak;
break;
case calc_FullRate:
func = (UnitCalcFunc)Impulse_next_aa;
break;
}
break;
case calc_BufRate:
if (INRATE(1) == calc_ScalarRate) {
func = (UnitCalcFunc)Impulse_next_ki;
} else {
func = (UnitCalcFunc)Impulse_next_kk;
}
break;
case calc_ScalarRate:
if (INRATE(1) == calc_ScalarRate) {
func = (UnitCalcFunc)Impulse_next_ii;
} else {
func = (UnitCalcFunc)Impulse_next_ik;
}
break;
}
unit->mCalcFunc = func;
func(unit, 1);
unit->mPhase = initPhase;
unit->mPhaseOffset = initOff;
unit->mPhaseIncrement = initInc;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void VarSaw_next_a(VarSaw* unit, int inNumSamples) {
float* out = ZOUT(0);
float* freq = ZIN(0);
float nextDuty = ZIN0(2);
float duty = unit->mDuty;
float invduty = unit->mInvDuty;
float inv1duty = unit->mInv1Duty;
float freqmul = unit->mFreqMul;
double phase = unit->mPhase;
LOOP1(
inNumSamples,
if (phase >= 1.f) {
phase -= 1.f;
duty = unit->mDuty = sc_clip(nextDuty, 0.001, 0.999);
invduty = unit->mInvDuty = 2.f / duty;
inv1duty = unit->mInv1Duty = 2.f / (1.f - duty);
} float z = phase < duty ? phase * invduty : (1.f - phase) * inv1duty;
phase += ZXP(freq) * freqmul; ZXP(out) = z - 1.f;);
unit->mPhase = phase;
}
void VarSaw_next_k(VarSaw* unit, int inNumSamples) {
float* out = ZOUT(0);
float freq = ZIN0(0) * unit->mFreqMul;
float nextDuty = ZIN0(2);
float duty = unit->mDuty;
float invduty = unit->mInvDuty;
float inv1duty = unit->mInv1Duty;
double phase = unit->mPhase;
LOOP1(
inNumSamples,
if (phase >= 1.f) {
phase -= 1.f;
duty = unit->mDuty = sc_clip(nextDuty, 0.001, 0.999);
invduty = unit->mInvDuty = 2.f / duty;
inv1duty = unit->mInv1Duty = 2.f / (1.f - duty);
} float z = phase < duty ? phase * invduty : (1.f - phase) * inv1duty;
phase += freq; ZXP(out) = z - 1.f;);
unit->mPhase = phase;
}
void VarSaw_Ctor(VarSaw* unit) {
if (INRATE(0) == calc_FullRate) {
SETCALC(VarSaw_next_a);
} else {
SETCALC(VarSaw_next_k);
}
unit->mFreqMul = unit->mRate->mSampleDur;
unit->mPhase = ZIN0(1);
float duty = ZIN0(2);
duty = unit->mDuty = sc_clip(duty, 0.001, 0.999);
unit->mInvDuty = 2.f / duty;
unit->mInv1Duty = 2.f / (1.f - duty);
ZOUT0(0) = 0.f;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void SyncSaw_next_aa(SyncSaw* unit, int inNumSamples) {
float freqmul = unit->mFreqMul;
float* out = ZOUT(0);
float* freq1 = ZIN(0);
float* freq2 = ZIN(1);
double phase1 = unit->mPhase1;
double phase2 = unit->mPhase2;
LOOP1(
inNumSamples, float freq1x = ZXP(freq1) * freqmul; float freq2x = ZXP(freq2) * freqmul; float z = phase2;
phase2 += freq2x; if (phase2 >= 1.f) phase2 -= 2.f; phase1 += freq1x; if (phase1 >= 1.f) {
phase1 -= 2.f;
phase2 = (phase1 + 1.f) * freq2x / freq1x - 1.f;
} ZXP(out) = z;);
unit->mPhase1 = phase1;
unit->mPhase2 = phase2;
}
void SyncSaw_next_ak(SyncSaw* unit, int inNumSamples) {
float freqmul = unit->mFreqMul;
float* out = ZOUT(0);
float* freq1 = ZIN(0);
float freq2x = ZIN0(1) * freqmul;
double phase1 = unit->mPhase1;
double phase2 = unit->mPhase2;
LOOP1(
inNumSamples, float freq1x = ZXP(freq1) * freqmul; float z = phase2; phase2 += freq2x;
if (phase2 >= 1.f) phase2 -= 2.f; phase1 += freq1x; if (phase1 >= 1.f) {
phase1 -= 2.f;
phase2 = (phase1 + 1.f) * freq2x / freq1x - 1.f;
} ZXP(out) = z;);
unit->mPhase1 = phase1;
unit->mPhase2 = phase2;
}
void SyncSaw_next_ka(SyncSaw* unit, int inNumSamples) {
float freqmul = unit->mFreqMul;
float* out = ZOUT(0);
float freq1x = ZIN0(0) * freqmul;
float* freq2 = ZIN(1);
double phase1 = unit->mPhase1;
double phase2 = unit->mPhase2;
LOOP1(
inNumSamples, float freq2x = ZXP(freq2) * freqmul; float z = phase2; phase2 += freq2x;
if (phase2 >= 1.f) phase2 -= 2.f; phase1 += freq1x; if (phase1 >= 1.f) {
phase1 -= 2.f;
phase2 = (phase1 + 1.f) * freq2x / freq1x - 1.f;
} ZXP(out) = z;);
unit->mPhase1 = phase1;
unit->mPhase2 = phase2;
}
void SyncSaw_next_kk(SyncSaw* unit, int inNumSamples) {
float* out = ZOUT(0);
float freq1x = ZIN0(0) * unit->mFreqMul;
float freq2x = ZIN0(1) * unit->mFreqMul;
double phase1 = unit->mPhase1;
double phase2 = unit->mPhase2;
LOOP1(
inNumSamples, float z = phase2; phase2 += freq2x; if (phase2 >= 1.f) phase2 -= 2.f; phase1 += freq1x;
if (phase1 >= 1.f) {
phase1 -= 2.f;
phase2 = (phase1 + 1.f) * freq2x / freq1x - 1.f;
} ZXP(out) = z;);
unit->mPhase1 = phase1;
unit->mPhase2 = phase2;
}
void SyncSaw_Ctor(SyncSaw* unit) {
if (INRATE(0) == calc_FullRate) {
if (INRATE(1) == calc_FullRate) {
SETCALC(SyncSaw_next_aa);
} else {
SETCALC(SyncSaw_next_ak);
}
} else {
if (INRATE(1) == calc_FullRate) {
SETCALC(SyncSaw_next_ka);
} else {
SETCALC(SyncSaw_next_kk);
}
}
unit->mFreqMul = 2.0 * unit->mRate->mSampleDur;
unit->mPhase1 = 0.;
unit->mPhase2 = 0.;
SyncSaw_next_kk(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
struct K2A : SIMD_Unit {
ControlRateInput<0> mLevel;
K2A(void) {
mLevel.init(this);
if (inRate(0) == calc_ScalarRate)
set_unrolled_calc_function<K2A, &K2A::next_i<unrolled_64>, &K2A::next_i<unrolled>, &K2A::next_i<scalar>>();
else
set_unrolled_calc_function<K2A, &K2A::next_k<unrolled_64>, &K2A::next_k<unrolled>, &K2A::next_k<scalar>>();
}
template <int type> void next_k(int inNumSamples) {
if (mLevel.changed(this))
slope_vec<type>(out(0), mLevel.slope(this), inNumSamples);
else
next_i<type>(inNumSamples);
}
template <int type> void next_i(int inNumSamples) { set_vec<type>(out(0), mLevel, inNumSamples); }
};
//////////////////////////////////////////////////////////////////////////////////////////////////
void A2K_next(A2K* unit, int inNumSamples) {
ZOUT0(0) = ZIN0(0); // return first sample in block
}
void A2K_Ctor(A2K* unit) {
SETCALC(A2K_next);
A2K_next(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void T2K_next(T2K* unit, int inNumSamples) {
float out = 0.f, val;
float* in = ZIN(0);
int n = unit->mWorld->mBufLength;
LOOP1(n, val = ZXP(in); if (val > out) out = val;);
ZOUT0(0) = out;
}
void T2K_Ctor(T2K* unit) {
SETCALC(T2K_next);
ZOUT0(0) = ZIN0(0);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
static inline void T2A_write_trigger(T2A* unit, float level) {
float* out = OUT(0);
int offset = (int)IN0(1);
out[offset] = level;
}
void T2A_next(T2A* unit, int inNumSamples) {
float level = IN0(0);
ZClear(inNumSamples, ZOUT(0));
if ((unit->mLevel <= 0.f && level > 0.f))
T2A_write_trigger(unit, level);
unit->mLevel = level;
}
#ifdef NOVA_SIMD
FLATTEN void T2A_next_nova(T2A* unit, int inNumSamples) {
float level = IN0(0);
nova::zerovec_simd(OUT(0), inNumSamples);
if ((unit->mLevel <= 0.f && level > 0.f))
T2A_write_trigger(unit, level);
unit->mLevel = level;
}
FLATTEN void T2A_next_nova_64(T2A* unit, int inNumSamples) {
float level = IN0(0);
nova::zerovec_simd<64>(OUT(0));
if ((unit->mLevel <= 0.f && level > 0.f))
T2A_write_trigger(unit, level);
unit->mLevel = level;
}
#endif
void T2A_Ctor(T2A* unit) {
#ifdef NOVA_SIMD
if (BUFLENGTH == 64)
SETCALC(T2A_next_nova_64);
else if (boost::alignment::is_aligned(BUFLENGTH, 16))
SETCALC(T2A_next_nova);
else
#endif
SETCALC(T2A_next);
T2A_next(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
struct DC : SIMD_Unit {
float value;
DC(void) {
value = in0(0);
if (value == 0)
set_unrolled_calc_function<DC, &DC::next_i<unrolled_64, true>, &DC::next_i<unrolled, true>,
&DC::next_i<scalar, true>>();
else
set_unrolled_calc_function<DC, &DC::next_i<unrolled_64, false>, &DC::next_i<unrolled, false>,
&DC::next_i<scalar, false>>();
}
template <int type, bool isZero> void next_i(int inNumSamples) {
if (isZero)
zero_vec<type>(out(0), inNumSamples);
else
set_vec<type>(out(0), value, inNumSamples);
}
};
//////////////////////////////////////////////////////////////////////////////////////////////////
static inline void Line_next_loop(Line* unit, int& counter, int remain, double& level) {
float* out = ZOUT(0);
double slope = unit->mSlope;
do {
if (counter == 0) {
int nsmps = remain;
remain = 0;
float endlevel = unit->mEndLevel;
LOOP(nsmps, ZXP(out) = endlevel;);
} else {
int nsmps = sc_min(remain, counter);
counter -= nsmps;
remain -= nsmps;
LOOP(nsmps, ZXP(out) = level; level += slope;);
if (counter == 0) {
unit->mDone = true;
int doneAction = (int)ZIN0(3);
DoneAction(doneAction, unit);
}
}
} while (remain);
}
void Line_next(Line* unit, int inNumSamples) {
double level = unit->mLevel;
int counter = unit->mCounter;
Line_next_loop(unit, counter, inNumSamples, level);
unit->mCounter = counter;
unit->mLevel = level;
}
#ifdef NOVA_SIMD
FLATTEN void Line_next_nova(Line* unit, int inNumSamples) {
double level = unit->mLevel;
int counter = unit->mCounter;
if (counter == 0) {
nova::setvec_simd(OUT(0), unit->mEndLevel, inNumSamples);
return;
}
if (counter > inNumSamples) {
double slope = unit->mSlope;
nova::set_slope_vec_simd(OUT(0), (float)level, (float)slope, inNumSamples);
unit->mLevel = level + inNumSamples * slope;
unit->mCounter = counter - inNumSamples;
return;
}
Line_next_loop(unit, counter, inNumSamples, level);
unit->mCounter = counter;
unit->mLevel = level;
}
FLATTEN void Line_next_nova_64(Line* unit, int inNumSamples) {
double level = unit->mLevel;
int counter = unit->mCounter;
if (counter == 0) {
nova::setvec_simd<64>(OUT(0), unit->mEndLevel);
return;
}
if (counter > inNumSamples) {
double slope = unit->mSlope;
nova::set_slope_vec_simd(OUT(0), (float)level, (float)slope, 64);
unit->mLevel = level + inNumSamples * slope;
unit->mCounter = counter - inNumSamples;
return;
}
Line_next_loop(unit, counter, inNumSamples, level);
unit->mCounter = counter;
unit->mLevel = level;
}
#endif
void Line_Ctor(Line* unit) {
#ifdef NOVA_SIMD
if (BUFLENGTH == 64)
SETCALC(Line_next_nova);
else if (boost::alignment::is_aligned(BUFLENGTH, 16))
SETCALC(Line_next_nova);
else
#endif
SETCALC(Line_next);
double start = ZIN0(0);
double end = ZIN0(1);
double dur = ZIN0(2);
int counter = (int)(dur * unit->mRate->mSampleRate + .5f);
unit->mCounter = sc_max(1, counter);
if (counter == 0) {
unit->mLevel = end;
unit->mSlope = 0.;
} else {
unit->mLevel = start;
unit->mSlope = (end - start) / unit->mCounter;
unit->mLevel += unit->mSlope;
}
unit->mEndLevel = end;
ZOUT0(0) = unit->mLevel;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
static inline void Xline_next_loop(XLine* unit, int& counter, int remain, double& level) {
float* out = ZOUT(0);
double grow = unit->mGrowth;
do {
if (counter == 0) {
int nsmps = remain;
remain = 0;
LOOP(nsmps, ZXP(out) = level;);
} else {
int nsmps = sc_min(remain, counter);
counter -= nsmps;
remain -= nsmps;
LOOP(nsmps, ZXP(out) = level; level *= grow;);
if (counter == 0) {
level = unit->mEndLevel;
unit->mDone = true;
int doneAction = (int)ZIN0(3);
DoneAction(doneAction, unit);
}
}
} while (remain);
}
void XLine_next(XLine* unit, int inNumSamples) {
double level = unit->mLevel;
int counter = unit->mCounter;
Xline_next_loop(unit, counter, inNumSamples, level);
unit->mCounter = counter;
unit->mLevel = level;
}
#ifdef NOVA_SIMD
FLATTEN void XLine_next_nova(XLine* unit, int inNumSamples) {
double level = unit->mLevel;
int counter = unit->mCounter;
if (counter == 0) {
nova::setvec_simd(OUT(0), (float)level, inNumSamples);
return;
}
if (counter > inNumSamples) {
double grow = unit->mGrowth;
nova::set_exp_vec_simd(OUT(0), (float)level, (float)grow, inNumSamples);
level *= sc_powi(grow, inNumSamples);
counter -= inNumSamples;
} else
Xline_next_loop(unit, counter, inNumSamples, level);
unit->mCounter = counter;
unit->mLevel = level;
}
FLATTEN void XLine_next_nova_64(XLine* unit, int inNumSamples) {
double level = unit->mLevel;
int counter = unit->mCounter;
if (counter == 0) {
nova::setvec_simd<64>(OUT(0), (float)level);
return;
}
if (counter > 64) {
double grow = unit->mGrowth;
nova::set_exp_vec_simd(OUT(0), (float)level, (float)grow, 64);
level *= sc_powi(grow, inNumSamples);
counter -= inNumSamples;
} else
Xline_next_loop(unit, counter, inNumSamples, level);
unit->mCounter = counter;
unit->mLevel = level;
}
#endif
void XLine_Ctor(XLine* unit) {
#ifdef NOVA_SIMD
if (BUFLENGTH == 64)
SETCALC(XLine_next_nova_64);
else if (boost::alignment::is_aligned(BUFLENGTH, 16))
SETCALC(XLine_next_nova);
else
#endif
SETCALC(XLine_next);
double start = ZIN0(0);
double end = ZIN0(1);
double dur = ZIN0(2);
int counter = (int)(dur * unit->mRate->mSampleRate + .5f);
unit->mEndLevel = end;
if (counter == 0) {
ZOUT0(0) = end;
unit->mLevel = end;
unit->mCounter = 0;
unit->mGrowth = 0;
} else {
ZOUT0(0) = start;
unit->mCounter = counter;
unit->mGrowth = pow(end / start, 1.0 / counter);
unit->mLevel = start * unit->mGrowth;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
/*
void Wrap_next(Wrap* unit, int inNumSamples)
{
float *out = ZOUT(0);
float *in = ZIN(0);
float lo = unit->m_lo;
float hi = unit->m_hi;
float range = unit->m_range;
LOOP1(inNumSamples,
ZXP(out) = sc_wrap(ZXP(in), lo, hi, range);
);
}
void Wrap_Ctor(Wrap* unit)
{
SETCALC(Wrap_next);
unit->m_lo = ZIN0(1);
unit->m_hi = ZIN0(2);
if (unit->m_lo > unit->m_hi) {
float temp = unit->m_lo;
unit->m_lo = unit->m_hi;
unit->m_hi = temp;
}
unit->m_range = unit->m_hi - unit->m_lo;
Wrap_next(unit, 1);
}
*/
void Wrap_next_kk(Wrap* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float next_lo = ZIN0(1);
float next_hi = ZIN0(2);
float lo = unit->m_lo;
float lo_slope = CALCSLOPE(next_lo, lo);
float hi = unit->m_hi;
float hi_slope = CALCSLOPE(next_hi, hi);
LOOP1(inNumSamples, float range = hi - lo; ZXP(out) = sc_wrap(ZXP(in), lo, hi, range); lo += lo_slope;
hi += hi_slope;);
unit->m_lo = lo;
unit->m_hi = hi;
}
void Wrap_next_ka(Wrap* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float next_lo = ZIN0(1);
float* hi = ZIN(2);
float lo = unit->m_lo;
float lo_slope = CALCSLOPE(next_lo, lo);
LOOP1(inNumSamples, float curhi = ZXP(hi); ZXP(out) = sc_wrap(ZXP(in), lo, curhi, curhi - lo); lo += lo_slope;);
unit->m_lo = lo;
}
void Wrap_next_ak(Wrap* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float* lo = ZIN(1);
float next_hi = ZIN0(2);
float hi = unit->m_hi;
float hi_slope = CALCSLOPE(next_hi, hi);
LOOP1(inNumSamples, float curlo = ZXP(lo); ZXP(out) = sc_wrap(ZXP(in), curlo, hi, hi - curlo); hi += hi_slope;);
unit->m_hi = hi;
}
void Wrap_next_aa(Wrap* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float* lo = ZIN(1);
float* hi = ZIN(2);
LOOP1(inNumSamples, float curhi = ZXP(hi); float curlo = ZXP(lo);
ZXP(out) = sc_wrap(ZXP(in), curlo, curhi, curhi - curlo););
}
void Wrap_Ctor(Wrap* unit) {
if (BUFLENGTH == 1) {
// _aa? Well, yes - that calc func doesn't interpolate
// and interpolation is not needed for kr (1 sample/block)
SETCALC(Wrap_next_aa);
} else {
if (INRATE(1) == calc_FullRate) {
if (INRATE(2) == calc_FullRate)
SETCALC(Wrap_next_aa);
else
SETCALC(Wrap_next_ak);
} else {
if (INRATE(2) == calc_FullRate)
SETCALC(Wrap_next_ka);
else
SETCALC(Wrap_next_kk);
}
}
unit->m_lo = ZIN0(1);
unit->m_hi = ZIN0(2);
Wrap_next_kk(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
/*
void Fold_next(Fold* unit, int inNumSamples)
{
float *out = ZOUT(0);
float *in = ZIN(0);
float lo = unit->m_lo;
float hi = unit->m_hi;
float range = unit->m_range;
float range2 = unit->m_range2;
LOOP1(inNumSamples,
ZXP(out) = sc_fold(ZXP(in), lo, hi, range, range2);
);
}
void Fold_Ctor(Fold* unit)
{
SETCALC(Fold_next);
unit->m_lo = ZIN0(1);
unit->m_hi = ZIN0(2);
if (unit->m_lo > unit->m_hi) {
float temp = unit->m_lo;
unit->m_lo = unit->m_hi;
unit->m_hi = temp;
}
unit->m_range = unit->m_hi - unit->m_lo;
unit->m_range2 = 2.f * unit->m_range;
Fold_next(unit, 1);
}
*/
void Fold_next_kk(Fold* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float next_lo = ZIN0(1);
float next_hi = ZIN0(2);
float lo = unit->m_lo;
float lo_slope = CALCSLOPE(next_lo, lo);
float hi = unit->m_hi;
float hi_slope = CALCSLOPE(next_hi, hi);
LOOP1(inNumSamples, float range = hi - lo; float range2 = range * 2.f;
ZXP(out) = sc_fold(ZXP(in), lo, hi, range, range2);
lo += lo_slope; hi += hi_slope;);
unit->m_lo = lo;
unit->m_hi = hi;
}
void Fold_next_ka(Fold* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float next_lo = ZIN0(1);
float* hi = ZIN(2);
float lo = unit->m_lo;
float lo_slope = CALCSLOPE(next_lo, lo);
LOOP1(inNumSamples, float curhi = ZXP(hi); float range = curhi - lo; float range2 = range * 2.f;
ZXP(out) = sc_fold(ZXP(in), lo, curhi, range, range2); lo += lo_slope;);
unit->m_lo = lo;
}
void Fold_next_ak(Fold* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float* lo = ZIN(1);
float next_hi = ZIN0(2);
float hi = unit->m_hi;
float hi_slope = CALCSLOPE(next_hi, hi);
LOOP1(inNumSamples, float curlo = ZXP(lo); float range = hi - curlo; float range2 = range * 2.f;
ZXP(out) = sc_fold(ZXP(in), curlo, hi, range, range2); hi += hi_slope;);
unit->m_hi = hi;
}
void Fold_next_aa(Fold* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float* lo = ZIN(1);
float* hi = ZIN(2);
LOOP1(inNumSamples, float curhi = ZXP(hi); float curlo = ZXP(lo); float range = curhi - curlo;
float range2 = range * 2.0; ZXP(out) = sc_fold(ZXP(in), curlo, curhi, range, range2););
}
void Fold_Ctor(Fold* unit) {
if (BUFLENGTH == 1) {
// _aa? Well, yes - that calc func doesn't interpolate
// and interpolation is not needed for kr (1 sample/block)
SETCALC(Fold_next_aa);
} else {
if (INRATE(1) == calc_FullRate) {
if (INRATE(2) == calc_FullRate)
SETCALC(Fold_next_aa);
else
SETCALC(Fold_next_ak);
} else {
if (INRATE(2) == calc_FullRate)
SETCALC(Fold_next_ka);
else
SETCALC(Fold_next_kk);
}
}
unit->m_lo = ZIN0(1);
unit->m_hi = ZIN0(2);
Fold_next_kk(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void Clip_next_ii(Clip* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float lo = unit->m_lo;
float hi = unit->m_hi;
LOOP1(inNumSamples, ZXP(out) = sc_clip(ZXP(in), lo, hi););
}
void Clip_next_kk(Clip* unit, int inNumSamples) {
float next_lo = ZIN0(1);
float next_hi = ZIN0(2);
float lo = unit->m_lo;
float hi = unit->m_hi;
if (lo == next_lo && hi == next_hi) {
Clip_next_ii(unit, inNumSamples);
return;
}
float* out = ZOUT(0);
float* in = ZIN(0);
float lo_slope = CALCSLOPE(next_lo, lo);
float hi_slope = CALCSLOPE(next_hi, hi);
LOOP1(inNumSamples, ZXP(out) = sc_clip(ZXP(in), lo, hi); lo += lo_slope; hi += hi_slope;);
unit->m_lo = lo;
unit->m_hi = hi;
}
void Clip_next_ka(Clip* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float next_lo = ZIN0(1);
float* hi = ZIN(2);
float lo = unit->m_lo;
float lo_slope = CALCSLOPE(next_lo, lo);
LOOP1(inNumSamples, ZXP(out) = sc_clip(ZXP(in), lo, ZXP(hi)); lo += lo_slope;);
unit->m_lo = lo;
}
void Clip_next_ak(Clip* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float* lo = ZIN(1);
float next_hi = ZIN0(2);
float hi = unit->m_hi;
float hi_slope = CALCSLOPE(next_hi, hi);
LOOP1(inNumSamples, ZXP(out) = sc_clip(ZXP(in), ZXP(lo), hi); hi += hi_slope;);
unit->m_hi = hi;
}
void Clip_next_aa(Clip* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float* lo = ZIN(1);
float* hi = ZIN(2);
LOOP1(inNumSamples, ZXP(out) = sc_clip(ZXP(in), ZXP(lo), ZXP(hi)););
}
void Clip_next_k(Clip* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float lo = ZIN0(1);
float hi = ZIN0(2);
ZXP(out) = sc_clip(ZXP(in), lo, hi);
}
#ifdef NOVA_SIMD
void Clip_next_nova_ii(Clip* unit, int inNumSamples) {
float lo = unit->m_lo;
float hi = unit->m_hi;
nova::clip_vec_simd(OUT(0), IN(0), lo, hi, inNumSamples);
}
void Clip_next_nova_ki(Clip* unit, int inNumSamples) {
float next_lo = ZIN0(1);
float lo = unit->m_lo;
float hi = unit->m_hi;
if (lo == next_lo) {
Clip_next_nova_ii(unit, inNumSamples);
return;
}
float lo_slope = CALCSLOPE(next_lo, lo);
nova::clip_vec_simd(OUT(0), IN(0), slope_argument(lo, lo_slope), hi, inNumSamples);
unit->m_lo = next_lo;
}
void Clip_next_nova_ik(Clip* unit, int inNumSamples) {
float next_hi = ZIN0(2);
float lo = unit->m_lo;
float hi = unit->m_hi;
if (hi == next_hi) {
Clip_next_nova_ii(unit, inNumSamples);
return;
}
float hi_slope = CALCSLOPE(next_hi, hi);
nova::clip_vec_simd(OUT(0), IN(0), lo, slope_argument(hi, hi_slope), inNumSamples);
unit->m_hi = next_hi;
}
void Clip_next_nova_kk(Clip* unit, int inNumSamples) {
float next_lo = ZIN0(1);
float next_hi = ZIN0(2);
float lo = unit->m_lo;
float hi = unit->m_hi;
if (lo == next_lo && hi == next_hi) {
Clip_next_nova_ii(unit, inNumSamples);
return;
}
if (lo == next_lo) {
Clip_next_nova_ik(unit, inNumSamples);
return;
}
if (hi == next_hi) {
Clip_next_nova_ki(unit, inNumSamples);
return;
}
float lo_slope = CALCSLOPE(next_lo, lo);
float hi_slope = CALCSLOPE(next_hi, hi);
nova::clip_vec_simd(OUT(0), IN(0), slope_argument(lo, lo_slope), slope_argument(hi, hi_slope), inNumSamples);
unit->m_lo = next_lo;
unit->m_hi = next_hi;
}
void Clip_next_nova_ai(Clip* unit, int inNumSamples) {
float hi = unit->m_hi;
nova::clip_vec_simd(OUT(0), IN(0), IN(1), hi, inNumSamples);
}
void Clip_next_nova_ak(Clip* unit, int inNumSamples) {
float next_hi = ZIN0(2);
float hi = unit->m_hi;
if (hi == next_hi) {
Clip_next_nova_ai(unit, inNumSamples);
return;
}
float hi_slope = CALCSLOPE(next_hi, hi);
nova::clip_vec_simd(OUT(0), IN(0), IN(1), slope_argument(hi, hi_slope), inNumSamples);
unit->m_hi = next_hi;
}
void Clip_next_nova_ia(Clip* unit, int inNumSamples) {
float lo = unit->m_lo;
nova::clip_vec_simd(OUT(0), IN(0), lo, IN(2), inNumSamples);
}
void Clip_next_nova_ka(Clip* unit, int inNumSamples) {
float next_lo = ZIN0(1);
float lo = unit->m_lo;
if (lo == next_lo) {
Clip_next_nova_ia(unit, inNumSamples);
return;
}
float lo_slope = CALCSLOPE(next_lo, lo);
nova::clip_vec_simd(OUT(0), IN(0), slope_argument(lo, lo_slope), IN(2), inNumSamples);
unit->m_lo = next_lo;
}
void Clip_next_nova_aa(Clip* unit, int inNumSamples) { nova::clip_vec_simd(OUT(0), IN(0), IN(1), IN(2), inNumSamples); }
#endif
typedef void (*ClipCalcFunc)(Clip*, int);
static ClipCalcFunc Clip_SelectCalc(Clip* unit) {
if (BUFLENGTH == 1)
return Clip_next_k;
int loRate = INRATE(1);
int hiRate = INRATE(2);
#ifdef NOVA_SIMD
if (boost::alignment::is_aligned(BUFLENGTH, 16)) {
switch (loRate) {
case calc_FullRate:
switch (hiRate) {
case calc_FullRate:
return Clip_next_nova_aa;
case calc_BufRate:
return Clip_next_nova_ak;
case calc_ScalarRate:
return Clip_next_nova_ai;
}
break;
case calc_BufRate:
switch (hiRate) {
case calc_FullRate:
return Clip_next_nova_ka;
case calc_BufRate:
return Clip_next_nova_kk;
case calc_ScalarRate:
return Clip_next_nova_ki;
}
break;
case calc_ScalarRate:
switch (hiRate) {
case calc_FullRate:
return Clip_next_nova_ia;
case calc_BufRate:
return Clip_next_nova_ik;
case calc_ScalarRate:
return Clip_next_nova_ii;
}
break;
}
}
#endif
if (loRate == calc_FullRate && hiRate == calc_FullRate)
return Clip_next_aa;
if (loRate == calc_ScalarRate && hiRate == calc_ScalarRate)
return Clip_next_ii;
if (loRate == calc_FullRate && hiRate != calc_FullRate)
return Clip_next_ak;
if (loRate != calc_FullRate && hiRate == calc_FullRate)
return Clip_next_ak;
return Clip_next_kk;
}
void Clip_Ctor(Clip* unit) {
ClipCalcFunc fn = Clip_SelectCalc(unit);
unit->mCalcFunc = (UnitCalcFunc)fn;
unit->m_lo = ZIN0(1);
unit->m_hi = ZIN0(2);
Clip_next_ii(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void Unwrap_next(Unwrap* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float range = unit->m_range;
float half = unit->m_half;
float prev = unit->m_prev;
float offset = unit->m_offset;
LOOP1(
inNumSamples, float zin = ZXP(in); float diff = zin - prev; if (fabs(diff) > half) {
if (zin < prev)
offset += range;
else
offset -= range;
} ZXP(out) = zin + offset;
prev = zin;);
unit->m_prev = prev;
unit->m_offset = offset;
}
void Unwrap_Ctor(Unwrap* unit) {
SETCALC(Unwrap_next);
float in = ZIN0(0);
float lo = ZIN0(1);
float hi = ZIN0(2);
if (lo > hi) {
float temp = lo;
lo = hi;
hi = temp;
}
unit->m_range = fabs(hi - lo);
unit->m_half = unit->m_range * 0.5f;
if (in < lo || in >= hi)
unit->m_offset = floor((lo - in) / unit->m_range) * unit->m_range;
else
unit->m_offset = 0.f;
Unwrap_next(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void ModDif_next_kk(ModDif* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float next_dif = ZIN0(1);
float next_mod = ZIN0(2);
float dif = unit->m_dif;
float dif_slope = CALCSLOPE(next_dif, dif);
float mod = unit->m_mod;
float mod_slope = CALCSLOPE(next_mod, mod);
LOOP1(inNumSamples, float inval = ZXP(in); float diff = std::fmod(std::abs(inval - dif), mod);
float modhalf = mod * 0.5; ZXP(out) = modhalf - std::fabs(diff - modhalf); dif += dif_slope;
mod += mod_slope;);
unit->m_dif = dif;
unit->m_mod = mod;
}
void ModDif_next_ka(ModDif* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float next_dif = ZIN0(1);
float* mod = ZIN(2);
float dif = unit->m_dif;
float dif_slope = CALCSLOPE(next_dif, dif);
LOOP1(inNumSamples, float inval = ZXP(in); float curmod = ZXP(mod);
float diff = std::fmod(std::abs(inval - dif), curmod); float modhalf = curmod * 0.5;
ZXP(out) = modhalf - std::abs(diff - modhalf); dif += dif_slope;);
unit->m_dif = dif;
}
void ModDif_next_ak(ModDif* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float* dif = ZIN(1);
float next_mod = ZIN0(2);
float mod = unit->m_mod;
float mod_slope = CALCSLOPE(next_mod, mod);
LOOP1(inNumSamples, float inval = ZXP(in); float diff = std::fmod(std::abs(inval - ZXP(dif)), mod);
float modhalf = mod * 0.5; ZXP(out) = modhalf - std::abs(diff - modhalf); mod += mod_slope;);
unit->m_mod = mod;
}
void ModDif_next_aa(ModDif* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float* dif = ZIN(1);
float* mod = ZIN(2);
LOOP1(inNumSamples, float inval = ZXP(in); float curmod = ZXP(mod);
float diff = std::fmod(std::abs(inval - ZXP(dif)), curmod); float modhalf = curmod * 0.5;
ZXP(out) = modhalf - std::abs(diff - modhalf););
}
void ModDif_Ctor(ModDif* unit) {
if (BUFLENGTH == 1) {
// _aa? Well, yes - that calc func doesn't interpolate
// and interpolation is not needed for kr (1 sample/block)
SETCALC(ModDif_next_aa);
} else {
if (INRATE(1) == calc_FullRate) {
if (INRATE(2) == calc_FullRate)
SETCALC(ModDif_next_aa);
else
SETCALC(ModDif_next_ak);
} else {
if (INRATE(2) == calc_FullRate)
SETCALC(ModDif_next_ka);
else
SETCALC(ModDif_next_kk);
}
}
unit->m_dif = ZIN0(1);
unit->m_mod = ZIN0(2);
ModDif_next_kk(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void AmpComp_next(AmpComp* unit, int inNumSamples) {
float* out = ZOUT(0);
float* freq = ZIN(0);
float rootmul = unit->m_rootmul;
float xb = unit->m_exponent;
LOOP1(inNumSamples, float xa = ZXP(freq); ZXP(out) = xa >= 0.f ? pow(xa, xb) * rootmul : -pow(-xa, xb) * rootmul;);
}
void AmpComp_next_kk(AmpComp* unit, int inNumSamples) {
float* out = ZOUT(0);
float* freq = ZIN(0);
float root = ZIN0(1);
float xb = ZIN0(2);
LOOP1(inNumSamples, float xa = root / ZXP(freq); ZXP(out) = xa >= 0.f ? pow(xa, xb) : -pow(-xa, xb););
}
void AmpComp_Ctor(AmpComp* unit) {
if (INRATE(1) != calc_ScalarRate || INRATE(2) != calc_ScalarRate) {
SETCALC(AmpComp_next_kk);
} else {
float exp = ZIN0(2);
unit->m_rootmul = pow(ZIN0(1), exp);
unit->m_exponent = -1.f * exp;
SETCALC(AmpComp_next);
}
AmpComp_next(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
const double AMPCOMP_K = 3.5041384 * 10e15;
const double AMPCOMP_C1 = 20.598997 * 20.598997;
const double AMPCOMP_C2 = 107.65265 * 107.65265;
const double AMPCOMP_C3 = 737.86223 * 737.86223;
const double AMPCOMP_C4 = 12194.217 * 12194.217;
const double AMPCOMP_MINLEVEL = -0.1575371167435;
double AmpCompA_calcLevel(double freq) {
double r = freq * freq;
double level = (AMPCOMP_K * r * r * r * r);
double n1 = AMPCOMP_C1 + r;
double n2 = AMPCOMP_C4 + r;
level = level / (n1 * n1 * (AMPCOMP_C2 + r) * (AMPCOMP_C3 + r) * n2 * n2);
level = 1. - sqrt(level);
return level;
}
void AmpCompA_next(AmpCompA* unit, int inNumSamples) {
float* out = ZOUT(0);
float* freq = ZIN(0);
double scale = unit->m_scale;
double offset = unit->m_offset;
LOOP1(inNumSamples, ZXP(out) = AmpCompA_calcLevel(ZXP(freq)) * scale + offset;);
}
void AmpCompA_Ctor(AmpCompA* unit) {
double rootFreq = ZIN0(1);
double rootLevel = AmpCompA_calcLevel(rootFreq);
float minLevel = ZIN0(2);
unit->m_scale = (ZIN0(3) - minLevel) / (rootLevel - AMPCOMP_MINLEVEL);
unit->m_offset = minLevel - unit->m_scale * AMPCOMP_MINLEVEL;
SETCALC(AmpCompA_next);
AmpCompA_next(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void InRange_next(InRange* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float lo = ZIN0(1);
float hi = ZIN0(2);
LOOP1(inNumSamples, float zin = ZXP(in); ZXP(out) = zin >= lo && zin <= hi ? 1.f : 0.f;);
}
void InRange_Ctor(InRange* unit) {
SETCALC(InRange_next);
InRange_next(unit, 1);
}
//////////////////////////////////////////////////////////////////////////////////////////////////
void InRect_next(InRect* unit, int inNumSamples) {
float* out = ZOUT(0);
float* inx = ZIN(0);
float* iny = ZIN(1);
float left = ZIN0(2);
float top = ZIN0(3);
float right = ZIN0(4);
float bottom = ZIN0(5);
LOOP1(inNumSamples, float x = ZXP(inx); float y = ZXP(iny);
ZXP(out) = x >= left && x <= right && y >= top && y <= bottom ? 1.f : 0.f;);
}
void InRect_Ctor(InRect* unit) {
SETCALC(InRect_next);
InRect_next(unit, 1);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void LinExp_next(LinExp* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float dstlo = unit->m_dstlo;
float dstratio = unit->m_dstratio;
float rsrcrange = unit->m_rsrcrange;
float rrminuslo = unit->m_rrminuslo;
LOOP1(inNumSamples, ZXP(out) = dstlo * pow(dstratio, ZXP(in) * rsrcrange + rrminuslo););
}
#ifdef NOVA_SIMD
static inline void LinExp_next_nova_loop(float* out, const float* in, int inNumSamples, nova::vec<float> dstlo,
nova::vec<float> dstratio, nova::vec<float> rsrcrange,
nova::vec<float> rrminuslo) {
const int vecSize = nova::vec<float>::size;
int unroll = inNumSamples / (2 * vecSize);
do {
nova::vec<float> val0, val1;
val0.load_aligned(in);
val1.load_aligned(in + vecSize);
val0 = dstlo * pow(dstratio, val0 * rsrcrange + rrminuslo);
val1 = dstlo * pow(dstratio, val1 * rsrcrange + rrminuslo);
val0.store_aligned(out);
val1.store_aligned(out + vecSize);
in += 2 * vecSize;
out += 2 * vecSize;
} while (--unroll);
}
FLATTEN static void LinExp_next_nova(LinExp* unit, int inNumSamples) {
float* out = OUT(0);
float* in = IN(0);
LinExp_next_nova_loop(out, in, inNumSamples, unit->m_dstlo, unit->m_dstratio, unit->m_rsrcrange, unit->m_rrminuslo);
}
FLATTEN static void LinExp_next_nova_kk(LinExp* unit, int inNumSamples) {
float* out = OUT(0);
float* in = IN(0);
float srclo = ZIN0(1);
float srchi = ZIN0(2);
float dstlo = ZIN0(3);
float dsthi = ZIN0(4);
float dstratio = dsthi / dstlo;
float rsrcrange = sc_reciprocal(srchi - srclo);
float rrminuslo = rsrcrange * -srclo;
LinExp_next_nova_loop(out, in, inNumSamples, dstlo, dstratio, rsrcrange, rrminuslo);
}
#endif
void LinExp_next_kk(LinExp* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float srclo = ZIN0(1);
float srchi = ZIN0(2);
float dstlo = ZIN0(3);
float dsthi = ZIN0(4);
float dstratio = dsthi * sc_reciprocal(dstlo);
float rsrcrange = sc_reciprocal(srchi - srclo);
float rrminuslo = rsrcrange * -srclo;
LOOP1(inNumSamples, ZXP(out) = dstlo * pow(dstratio, ZXP(in) * rsrcrange + rrminuslo););
}
void LinExp_next_aa(LinExp* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float* srclo = ZIN(1);
float* srchi = ZIN(2);
float* dstlo = ZIN(3);
float* dsthi = ZIN(4);
LOOP1(inNumSamples, float zdsthi = ZXP(dsthi); float zdstlo = ZXP(dstlo); float zsrchi = ZXP(srchi);
float zsrclo = ZXP(srclo); float dstratio = zdsthi / zdstlo; float rsrcrange = sc_reciprocal(zsrchi - zsrclo);
float rrminuslo = rsrcrange * -zsrclo; ZXP(out) = zdstlo * pow(dstratio, ZXP(in) * rsrcrange + rrminuslo););
}
void LinExp_next_ak(LinExp* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float* srclo = ZIN(1);
float* srchi = ZIN(2);
float dstlo = ZIN0(3);
float dsthi = ZIN0(4);
float dstratio = dsthi / dstlo;
LOOP1(inNumSamples, float zsrchi = ZXP(srchi); float zsrclo = ZXP(srclo);
float rsrcrange = sc_reciprocal(zsrchi - zsrclo); float rrminuslo = rsrcrange * -zsrclo;
ZXP(out) = dstlo * pow(dstratio, ZXP(in) * rsrcrange + rrminuslo););
}
void LinExp_next_ka(LinExp* unit, int inNumSamples) {
float* out = ZOUT(0);
float* in = ZIN(0);
float srclo = ZIN0(1);
float srchi = ZIN0(2);
float* dstlo = ZIN(3);
float* dsthi = ZIN(4);
float rsrcrange = sc_reciprocal(srchi - srclo);
float rrminuslo = rsrcrange * -srclo;
LOOP1(inNumSamples, float zdsthi = ZXP(dsthi); float zdstlo = ZXP(dstlo); float dstratio = zdsthi / zdstlo;
ZXP(out) = zdstlo * pow(dstratio, ZXP(in) * rsrcrange + rrminuslo););
}
static void LinExp_SetCalc(LinExp* unit) {
if (INRATE(1) == calc_FullRate || INRATE(2) == calc_FullRate) {
if (INRATE(3) == calc_FullRate || INRATE(4) == calc_FullRate) {
SETCALC(LinExp_next_aa);
return;
} else {
SETCALC(LinExp_next_ak);
return;
}
} else {
if (INRATE(3) == calc_FullRate || INRATE(4) == calc_FullRate) {
SETCALC(LinExp_next_ka);
return;
}
}
bool allScalar = true;
for (int i = 1; i < 5; i++) {
if (INRATE(i) != calc_ScalarRate) {
allScalar = false;
break;
}
};
#ifdef NOVA_SIMD
if ((BUFLENGTH % (2 * nova::vec<float>::size)) == 0)
if (allScalar)
SETCALC(LinExp_next_nova);
else
SETCALC(LinExp_next_nova_kk);
else
#endif
if (allScalar)
SETCALC(LinExp_next);
else
SETCALC(LinExp_next_kk);
if (!allScalar)
return;
float srclo = ZIN0(1);
float srchi = ZIN0(2);
float dstlo = ZIN0(3);
float dsthi = ZIN0(4);
unit->m_dstlo = dstlo;
unit->m_dstratio = dsthi / dstlo;
unit->m_rsrcrange = sc_reciprocal(srchi - srclo);
unit->m_rrminuslo = unit->m_rsrcrange * -srclo;
}
void LinExp_Ctor(LinExp* unit) {
LinExp_SetCalc(unit);
float srclo = ZIN0(1);
float srchi = ZIN0(2);
float dstlo = ZIN0(3);
float dsthi = ZIN0(4);
unit->m_dstlo = dstlo;
unit->m_dstratio = dsthi / dstlo;
unit->m_rsrcrange = 1. / (srchi - srclo);
unit->m_rrminuslo = unit->m_rsrcrange * -srclo;
LinExp_next(unit, 1);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
enum {
kEnvGen_gate,
kEnvGen_levelScale,
kEnvGen_levelBias,
kEnvGen_timeScale,
kEnvGen_doneAction,
kEnvGen_initLevel,
kEnvGen_numStages,
kEnvGen_releaseNode,
kEnvGen_loopNode,
// 'kEnvGen_nodeOffset' must always be last
// if you need to add an arg, put it before this one
kEnvGen_nodeOffset
};
enum {
shape_Step,
shape_Linear,
shape_Exponential,
shape_Sine,
shape_Welch,
shape_Curve,
shape_Squared,
shape_Cubed,
shape_Hold,
shape_Sustain = 9999
};
#ifdef NOVA_SIMD
void EnvGen_next_ak_nova(EnvGen* unit, int inNumSamples);
#endif
#define ENVGEN_NOT_STARTED 1000000000
void EnvGen_Ctor(EnvGen* unit) {
// Print("EnvGen_Ctor A\n");
if (unit->mCalcRate == calc_FullRate) {
if (INRATE(0) == calc_FullRate) {
SETCALC(EnvGen_next_aa);
} else {
#ifdef NOVA_SIMD
if (boost::alignment::is_aligned(BUFLENGTH, 16))
SETCALC(EnvGen_next_ak_nova);
else
#endif
SETCALC(EnvGen_next_ak);
}
} else {
SETCALC(EnvGen_next_k);
}
// gate = 1.0, levelScale = 1.0, levelBias = 0.0, timeScale
// level0, numstages, releaseNode, loopNode,
// [level, dur, shape, curve]
unit->m_endLevel = unit->m_level = ZIN0(kEnvGen_initLevel) * ZIN0(kEnvGen_levelScale) + ZIN0(kEnvGen_levelBias);
unit->m_counter = 0;
unit->m_stage = ENVGEN_NOT_STARTED;
unit->m_shape = shape_Hold;
unit->m_prevGate = 0.f;
unit->m_released = false;
unit->m_releaseNode = (int)ZIN0(kEnvGen_releaseNode);
float** envPtr = unit->mInBuf + kEnvGen_nodeOffset;
const int initialShape = (int32)*envPtr[2];
if (initialShape == shape_Hold)
unit->m_level = *envPtr[0]; // we start at the end level;
EnvGen_next_k(unit, 1);
}
// called by nextSegment and check_gate:
// - counter: num samples to next segment
// - level: current envelope value
// - dur: if supplied and >= 0, stretch segment to last dur seconds (used in forced release)
static bool EnvGen_initSegment(EnvGen* unit, int& counter, double& level, double dur = -1) {
// Print("stage %d\n", unit->m_stage);
// Print("initSegment\n");
// out = unit->m_level;
int stageOffset = (unit->m_stage << 2) + kEnvGen_nodeOffset;
if (stageOffset + 4 > unit->mNumInputs) {
// oops.
Print("envelope went past end of inputs.\n");
ClearUnitOutputs(unit, 1);
NodeEnd(&unit->mParent->mNode);
return false;
}
float previousEndLevel = unit->m_endLevel;
if (unit->m_shape == shape_Hold)
level = previousEndLevel;
float** envPtr = unit->mInBuf + stageOffset;
double endLevel = *envPtr[0] * ZIN0(kEnvGen_levelScale) + ZIN0(kEnvGen_levelBias); // scale levels
if (dur < 0)
dur = *envPtr[1] * ZIN0(kEnvGen_timeScale);
unit->m_shape = (int32)*envPtr[2];
double curve = *envPtr[3];
unit->m_endLevel = endLevel;
counter = (int32)(dur * SAMPLERATE);
counter = sc_max(1, counter);
// Print("counter %d stageOffset %d level %g endLevel %g dur %g shape %d curve %g\n", counter,
// stageOffset, level, endLevel, dur, unit->m_shape, curve); Print("SAMPLERATE %g\n", SAMPLERATE);
if (counter == 1)
unit->m_shape = 1; // shape_Linear
// Print("new counter = %d shape = %d\n", counter, unit->m_shape);
switch (unit->m_shape) {
case shape_Step: {
level = endLevel;
} break;
case shape_Hold: {
level = previousEndLevel;
} break;
case shape_Linear: {
unit->m_grow = (endLevel - level) / counter;
// Print("grow %g\n", unit->m_grow);
} break;
case shape_Exponential: {
unit->m_grow = pow(endLevel / level, 1.0 / counter);
} break;
case shape_Sine: {
double w = pi / counter;
unit->m_a2 = (endLevel + level) * 0.5;
unit->m_b1 = 2. * cos(w);
unit->m_y1 = (endLevel - level) * 0.5;
unit->m_y2 = unit->m_y1 * sin(pi * 0.5 - w);
level = unit->m_a2 - unit->m_y1;
} break;
case shape_Welch: {
double w = (pi * 0.5) / counter;
unit->m_b1 = 2. * cos(w);
if (endLevel >= level) {
unit->m_a2 = level;
unit->m_y1 = 0.;
unit->m_y2 = -sin(w) * (endLevel - level);
} else {
unit->m_a2 = endLevel;
unit->m_y1 = level - endLevel;
unit->m_y2 = cos(w) * (level - endLevel);
}
level = unit->m_a2 + unit->m_y1;
} break;
case shape_Curve: {
if (fabs(curve) < 0.001) {
unit->m_shape = 1; // shape_Linear
unit->m_grow = (endLevel - level) / counter;
} else {
double a1 = (endLevel - level) / (1.0 - exp(curve));
unit->m_a2 = level + a1;
unit->m_b1 = a1;
unit->m_grow = exp(curve / counter);
}
} break;
case shape_Squared: {
unit->m_y1 = sqrt(level);
unit->m_y2 = sqrt(endLevel);
unit->m_grow = (unit->m_y2 - unit->m_y1) / counter;
} break;
case shape_Cubed: {
unit->m_y1 = pow(level, 1.0 / 3.0); // 0.33333333);
unit->m_y2 = pow(endLevel, 1.0 / 3.0);
unit->m_grow = (unit->m_y2 - unit->m_y1) / counter;
} break;
};
return true;
}
static bool check_gate(EnvGen* unit, float prevGate, float gate, int& counter, double level, int counterOffset = 0) {
if (prevGate <= 0.f && gate > 0.f) {
unit->m_stage = -1;
unit->m_released = false;
unit->mDone = false;
counter = counterOffset;
return false;
} else if (gate <= -1.f && prevGate > -1.f) {
// forced release: jump to last segment overriding its duration
double dur = -gate - 1.f;
counter = (int32)(dur * SAMPLERATE);
counter = sc_max(1, counter) + counterOffset;
unit->m_stage = static_cast<int>(ZIN0(kEnvGen_numStages) - 1);
unit->m_released = true;
EnvGen_initSegment(unit, counter, level, dur);
return false;
} else if (prevGate > 0.f && gate <= 0.f && unit->m_releaseNode >= 0 && !unit->m_released) {
counter = counterOffset;
unit->m_stage = unit->m_releaseNode - 1;
unit->m_released = true;
return false;
}
return true;
}
static inline bool check_gate_ar(EnvGen* unit, int i, float& prevGate, float*& gatein, int& nsmps, int& counter,
double level) {
const float gate = ZXP(gatein);
const bool result = check_gate(unit, prevGate, gate, counter, level, i);
if (!result) {
--gatein;
nsmps = i;
}
prevGate = gate;
return result;
}
static inline bool EnvGen_nextSegment(EnvGen* unit, int& counter, double& level) {
// Print("stage %d rel %d\n", unit->m_stage, (int)ZIN0(kEnvGen_releaseNode));
int numstages = (int)ZIN0(kEnvGen_numStages);
// Print("stage %d numstages %d\n", unit->m_stage, numstages);
if (unit->m_stage + 1 >= numstages) { // num stages
// Print("stage+1 > num stages\n");
counter = INT_MAX;
unit->m_shape = 0;
level = unit->m_endLevel;
unit->mDone = true;
int doneAction = (int)ZIN0(kEnvGen_doneAction);
DoneAction(doneAction, unit);
} else if (unit->m_stage == ENVGEN_NOT_STARTED) {
counter = INT_MAX;
return true;
} else if (unit->m_stage + 1 == (int)ZIN0(kEnvGen_releaseNode) && !unit->m_released) { // sustain stage
int loopNode = (int)ZIN0(kEnvGen_loopNode);
if (loopNode >= 0 && loopNode < numstages) {
unit->m_stage = loopNode;
return EnvGen_initSegment(unit, counter, level);
} else {
counter = INT_MAX;
unit->m_shape = shape_Sustain;
level = unit->m_endLevel;
}
// Print("sustain\n");
} else {
unit->m_stage++;
return EnvGen_initSegment(unit, counter, level);
}
return true;
}
template <bool CheckGateOnSustain, typename GateCheck>
static inline void EnvGen_perform(EnvGen* unit, float*& out, double& level, int& nsmps, GateCheck const& gateCheck) {
switch (unit->m_shape) {
case shape_Step:
case shape_Hold: {
for (int i = 0; i < nsmps; ++i) {
if (!gateCheck(i))
break;
ZXP(out) = level;
}
} break;
case shape_Linear: {
double grow = unit->m_grow;
for (int i = 0; i < nsmps; ++i) {
if (!gateCheck(i))
break;
ZXP(out) = level;
level += grow;
}
} break;
case shape_Exponential: {
double grow = unit->m_grow;
for (int i = 0; i < nsmps; ++i) {
if (!gateCheck(i))
break;
ZXP(out) = level;
level *= grow;
}
} break;
case shape_Sine: {
double a2 = unit->m_a2;
double b1 = unit->m_b1;
double y2 = unit->m_y2;
double y1 = unit->m_y1;
for (int i = 0; i < nsmps; ++i) {
if (!gateCheck(i))
break;
ZXP(out) = level;
double y0 = b1 * y1 - y2;
level = a2 - y0;
y2 = y1;
y1 = y0;
}
unit->m_y1 = y1;
unit->m_y2 = y2;
} break;
case shape_Welch: {
double a2 = unit->m_a2;
double b1 = unit->m_b1;
double y2 = unit->m_y2;
double y1 = unit->m_y1;
for (int i = 0; i < nsmps; ++i) {
if (!gateCheck(i))
break;
ZXP(out) = level;
double y0 = b1 * y1 - y2;
level = a2 + y0;
y2 = y1;
y1 = y0;
}
unit->m_y1 = y1;
unit->m_y2 = y2;
} break;
case shape_Curve: {
double a2 = unit->m_a2;
double b1 = unit->m_b1;
double grow = unit->m_grow;
for (int i = 0; i < nsmps; ++i) {
if (!gateCheck(i))
break;
ZXP(out) = level;
b1 *= grow;
level = a2 - b1;
}
unit->m_b1 = b1;
} break;
case shape_Squared: {
double grow = unit->m_grow;
double y1 = unit->m_y1;
for (int i = 0; i < nsmps; ++i) {
if (!gateCheck(i))
break;
ZXP(out) = level;
y1 += grow;
level = y1 * y1;
}
unit->m_y1 = y1;
} break;
case shape_Cubed: {
double grow = unit->m_grow;
double y1 = unit->m_y1;
for (int i = 0; i < nsmps; ++i) {
if (!gateCheck(i))
break;
ZXP(out) = level;
y1 += grow;
y1 = sc_max(y1, 0);
level = y1 * y1 * y1;
}
unit->m_y1 = y1;
} break;
case shape_Sustain: {
for (int i = 0; i < nsmps; ++i) {
if (CheckGateOnSustain) {
if (gateCheck(i))
ZXP(out) = level;
} else
ZXP(out) = level;
}
} break;
}
}
static inline void EnvGen_perform(EnvGen* unit, float*& out, double& level, int nsmps) {
EnvGen_perform<false>(unit, out, level, nsmps, [](int i) { return true; });
}
void EnvGen_next_k(EnvGen* unit, int inNumSamples) {
float gate = ZIN0(kEnvGen_gate);
// Print("->EnvGen_next_k gate %g\n", gate);
int counter = unit->m_counter;
double level = unit->m_level;
check_gate(unit, unit->m_prevGate, gate, counter, level);
unit->m_prevGate = gate;
// gate = 1.0, levelScale = 1.0, levelBias = 0.0, timeScale
// level0, numstages, releaseNode, loopNode,
// [level, dur, shape, curve]
if (counter <= 0) {
bool success = EnvGen_nextSegment(unit, counter, level);
if (!success)
return;
}
float* out = ZOUT(0);
EnvGen_perform(unit, out, level, 1);
// Print("x %d %d %d %g\n", unit->m_stage, counter, unit->m_shape, *out);
unit->m_level = level;
unit->m_counter = counter - 1;
}
void EnvGen_next_ak(EnvGen* unit, int inNumSamples) {
float* out = ZOUT(0);
float gate = ZIN0(kEnvGen_gate);
int counter = unit->m_counter;
double level = unit->m_level;
check_gate(unit, unit->m_prevGate, gate, counter, level);
unit->m_prevGate = gate;
int remain = inNumSamples;
while (remain) {
if (counter <= 0) {
bool success = EnvGen_nextSegment(unit, counter, level);
if (!success)
return;
}
int nsmps = sc_min(remain, counter);
EnvGen_perform(unit, out, level, nsmps);
remain -= nsmps;
counter -= nsmps;
}
// Print("x %d %d %d %g\n", unit->m_stage, counter, unit->m_shape, ZOUT0(0));
unit->m_level = level;
unit->m_counter = counter;
}
#ifdef NOVA_SIMD
FLATTEN void EnvGen_next_ak_nova(EnvGen* unit, int inNumSamples) {
float* out = ZOUT(0);
float gate = ZIN0(kEnvGen_gate);
int counter = unit->m_counter;
double level = unit->m_level;
check_gate(unit, unit->m_prevGate, gate, counter, level);
unit->m_prevGate = gate;
int remain = inNumSamples;
if (counter > inNumSamples) {
switch (unit->m_shape) {
case shape_Step:
case shape_Hold:
case shape_Sustain:
nova::setvec_simd(OUT(0), (float)level, inNumSamples);
remain = 0;
counter -= inNumSamples;
break;
case shape_Linear: {
double slope = unit->m_grow;
nova::set_slope_vec_simd(OUT(0), (float)level, (float)slope, inNumSamples);
level += inNumSamples * slope;
remain = 0;
counter -= inNumSamples;
} break;
case shape_Exponential: {
double grow = unit->m_grow;
nova::set_exp_vec_simd(OUT(0), (float)level, (float)grow, inNumSamples);
level *= sc_powi(grow, inNumSamples);
remain = 0;
counter -= inNumSamples;
} break;
}
}
while (remain) {
if (counter <= 0) {
bool success = EnvGen_nextSegment(unit, counter, level);
if (!success)
return;
}
int nsmps = sc_min(remain, counter);
EnvGen_perform(unit, out, level, nsmps);
remain -= nsmps;
counter -= nsmps;
}
// Print("x %d %d %d %g\n", unit->m_stage, counter, unit->m_shape, ZOUT0(0));
unit->m_level = level;
unit->m_counter = counter;
}
#endif
void EnvGen_next_aa(EnvGen* unit, int inNumSamples) {
float* out = ZOUT(0);
float* gatein = ZIN(kEnvGen_gate);
int counter = unit->m_counter;
double level = unit->m_level;
float gate = unit->m_prevGate;
int remain = inNumSamples;
while (remain) {
if (counter <= 0) {
bool success = EnvGen_nextSegment(unit, counter, level);
if (!success)
return;
}
int nsmps = sc_min(remain, counter);
EnvGen_perform<true>(unit, out, level, nsmps,
[&](int i) { return check_gate_ar(unit, i, gate, gatein, nsmps, counter, level); });
remain -= nsmps;
counter -= nsmps;
}
unit->m_level = level;
unit->m_counter = counter;
unit->m_prevGate = gate;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void Linen_Ctor(Linen* unit) {
// gate attack level release
SETCALC(Linen_next_k);
unit->m_level = 0.f;
unit->m_stage = 4;
unit->m_prevGate = 0.f;
if (ZIN0(0) <= -1.f) {
unit->m_stage = 1;
} // early release
Linen_next_k(unit, 1);
}
void Linen_next_k(Linen* unit, int inNumSamples) {
float gate = ZIN0(0);
float* out = OUT(0);
if (unit->m_prevGate <= 0.f && gate > 0.f) {
unit->mDone = false;
unit->m_stage = 0;
float attackTime = ZIN0(1);
float susLevel = ZIN0(2);
int counter = (int)(attackTime * SAMPLERATE);
counter = sc_max(1, counter);
unit->m_slope = (susLevel - unit->m_level) / counter;
unit->m_counter = counter;
}
switch (unit->m_stage) {
case 0:
case 2:
*out = unit->m_level;
unit->m_level += unit->m_slope;
if (--unit->m_counter == 0)
unit->m_stage++;
break;
case 1:
*out = unit->m_level;
if (gate <= -1.f) {
// cutoff
unit->m_stage = 2;
float releaseTime = -gate - 1.f;
int counter = (int)(releaseTime * SAMPLERATE);
counter = sc_max(1, counter);
unit->m_slope = -unit->m_level / counter;
unit->m_counter = counter;
} else if (gate <= 0.f) {
unit->m_stage = 2;
float releaseTime = ZIN0(3);
int counter = (int)(releaseTime * SAMPLERATE);
counter = sc_max(1, counter);
unit->m_slope = -unit->m_level / counter;
unit->m_counter = counter;
// Print("release %d %d\n", unit->mParent->mNode.mID, counter);
}
break;
case 3: {
*out = 0.f;
// Print("done %d\n", unit->mParent->mNode.mID);
unit->mDone = true;
unit->m_stage++;
int doneAction = (int)ZIN0(4);
DoneAction(doneAction, unit);
} break;
case 4:
*out = 0.f;
break;
}
unit->m_prevGate = gate;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
void EnvFill(World* world, struct SndBuf* buf, struct sc_msg_iter* msg) {
if (buf->channels != 1)
return;
int size = buf->samples;
int byteSize = size * sizeof(float);
float* data = (float*)malloc(byteSize);
double level = msg->getf();
int numStages = msg->geti();
/*int releaseNode =*/msg->geti(); // ignored
/*int loopNode =*/msg->geti(); // ignored
double pos = 0.;
int32 index = 0;
int32 remain = size;
for (int j = 0; j < numStages; ++j) {
double endLevel = msg->getf();
double dur = msg->getf();
int shape = msg->geti();
double curve = msg->getf();
int32 ipos = (int32)pos;
double smpdur = dur * size;
int32 nsmps = (int32)smpdur - ipos;
nsmps = sc_min(nsmps, remain);
switch (shape) {
case shape_Step: {
level = endLevel;
for (int i = 0; i < nsmps; ++i) {
data[index++] = level;
}
} break;
case shape_Hold: {
for (int i = 0; i < nsmps; ++i) {
data[index++] = level;
}
level = endLevel;
} break;
case shape_Linear: {
double grow = (endLevel - level) / nsmps;
for (int i = 0; i < nsmps; ++i) {
data[index++] = level;
level += grow;
}
} break;
case shape_Exponential: {
double grow = pow(endLevel / level, 1.0 / nsmps);
for (int i = 0; i < nsmps; ++i) {
data[index++] = level;
level *= grow;
}
} break;
case shape_Sine: {
double w = pi / nsmps;
double a2 = (endLevel + level) * 0.5;
double b1 = 2. * cos(w);
double y1 = (endLevel - level) * 0.5;
double y2 = y1 * sin(pi * 0.5 - w);
level = a2 - y1;
for (int i = 0; i < nsmps; ++i) {
data[index++] = level;
double y0 = b1 * y1 - y2;
level = a2 - y0;
y2 = y1;
y1 = y0;
}
} break;
case shape_Welch: {
double w = (pi * 0.5) / nsmps;
double b1 = 2. * cos(w);
double a2, y1, y2;
if (endLevel >= level) {
a2 = level;
y1 = 0.;
y2 = -sin(w) * (endLevel - level);
} else {
a2 = endLevel;
y1 = level - endLevel;
y2 = cos(w) * (level - endLevel);
}
level = a2 + y1;
for (int i = 0; i < nsmps; ++i) {
data[index++] = level;
double y0 = b1 * y1 - y2;
level = a2 - y0;
y2 = y1;
y1 = y0;
}
} break;
case shape_Curve: {
if (fabs(curve) < 0.001) {
double grow = (endLevel - level) / nsmps;
for (int i = 0; i < nsmps; ++i) {
data[index++] = level;
level += grow;
}
} else {
double a1 = (endLevel - level) / (1.0 - exp(curve));
double a2 = level + a1;
double b1 = a1;
double grow = exp(curve / nsmps);
for (int i = 0; i < nsmps; ++i) {
data[index++] = level;
b1 *= grow;
level = a2 - b1;
}
}
} break;
case shape_Squared: {
double y1 = sqrt(level);
double y2 = sqrt(endLevel);
double grow = (y2 - y1) / nsmps;
for (int i = 0; i < nsmps; ++i) {
data[index++] = level;
y1 += grow;
level = y1 * y1;
}
} break;
case shape_Cubed: {
double y1 = pow(level, 0.33333333);
double y2 = pow(endLevel, 0.33333333);
double grow = (y2 - y1) / nsmps;
for (int i = 0; i < nsmps; ++i) {
data[index++] = level;
y1 += grow;
level = y1 * y1 * y1;
}
} break;
}
pos += smpdur;
level = endLevel;
remain -= nsmps;
}
memcpy(buf->data, data, byteSize);
free(data);
}
//////////////////// Add IEnvGen 06/06/2007 /////////////////////////////////
struct IEnvGen : public Unit {
float m_level, m_offset;
float m_startpoint, m_numvals, m_pointin;
float* m_envvals;
};
extern "C" {
void IEnvGen_next_a(IEnvGen* unit, int inNumSamples);
void IEnvGen_next_k(IEnvGen* unit, int inNumSamples);
void IEnvGen_Ctor(IEnvGen* unit);
void IEnvGen_Dtor(IEnvGen* unit);
}
#define GET_ENV_VAL \
switch (shape) { \
case shape_Step: \
level = unit->m_level = endLevel; \
break; \
case shape_Hold: \
level = unit->m_level; \
unit->m_level = endLevel; \
break; \
case shape_Linear: \
default: \
level = unit->m_level = pos * (endLevel - begLevel) + begLevel; \
break; \
case shape_Exponential: \
level = unit->m_level = begLevel * pow(endLevel / begLevel, pos); \
break; \
case shape_Sine: \
level = unit->m_level = begLevel + (endLevel - begLevel) * (-cos(pi * pos) * 0.5 + 0.5); \
break; \
case shape_Welch: { \
if (begLevel < endLevel) \
level = unit->m_level = begLevel + (endLevel - begLevel) * sin(pi2 * pos); \
else \
level = unit->m_level = endLevel - (endLevel - begLevel) * sin(pi2 - pi2 * pos); \
break; \
} \
case shape_Curve: \
if (fabs((float)curve) < 0.0001) { \
level = unit->m_level = pos * (endLevel - begLevel) + begLevel; \
} else { \
double denom = 1. - exp((float)curve); \
double numer = 1. - exp((float)(pos * curve)); \
level = unit->m_level = begLevel + (endLevel - begLevel) * (numer / denom); \
} \
break; \
case shape_Squared: { \
double sqrtBegLevel = sqrt(begLevel); \
double sqrtEndLevel = sqrt(endLevel); \
double sqrtLevel = pos * (sqrtEndLevel - sqrtBegLevel) + sqrtBegLevel; \
level = unit->m_level = sqrtLevel * sqrtLevel; \
break; \
} \
case shape_Cubed: { \
double cbrtBegLevel = pow(begLevel, 0.3333333f); \
double cbrtEndLevel = pow(endLevel, 0.3333333f); \
double cbrtLevel = pos * (cbrtEndLevel - cbrtBegLevel) + cbrtBegLevel; \
level = unit->m_level = cbrtLevel * cbrtLevel * cbrtLevel; \
break; \
} \
}
void IEnvGen_Ctor(IEnvGen* unit) {
if (INRATE(0) == calc_FullRate) {
SETCALC(IEnvGen_next_a);
} else {
SETCALC(IEnvGen_next_k);
}
// pointer, offset
// initlevel, numstages, totaldur,
// [dur, shape, curve, level] * numvals
int numStages = (int)IN0(3);
int numvals = numStages * 4; // initlevel + (levels, dur, shape, curves) * stages
float offset = unit->m_offset = IN0(1);
float point = unit->m_pointin = IN0(0) - offset;
unit->m_envvals = (float*)RTAlloc(unit->mWorld, (int)(numvals + 1.) * sizeof(float));
ClearUnitIfMemFailed(unit->m_envvals);
unit->m_envvals[0] = IN0(2);
// Print("offset of and initial values %3,3f, %3.3f\n", offset, unit->m_envvals[0]);
// fill m_envvals with the values;
for (int i = 1; i <= numvals; i++) {
unit->m_envvals[i] = IN0(4 + i);
// Print("val for: %d, %3.3f\n", i, unit->m_envvals[i]);
}
// float out = OUT0(0);
float totalDur = IN0(4);
float level = 0.f;
float newtime = 0.f;
int stage = 0;
float seglen = 0.f;
if (point >= totalDur) {
unit->m_level = level = unit->m_envvals[numStages * 4]; // grab the last value
} else {
if (point <= 0.0) {
unit->m_level = level = unit->m_envvals[0];
} else {
float segpos = point;
// determine which segment the current time pointer needs calculated
for (int j = 0; point >= newtime; j++) {
seglen = unit->m_envvals[(j * 4) + 1];
newtime += seglen;
segpos -= seglen;
stage = j;
}
segpos = segpos + seglen;
float begLevel = unit->m_envvals[(stage * 4)];
int shape = (int)unit->m_envvals[(stage * 4) + 2];
int curve = (int)unit->m_envvals[(stage * 4) + 3];
float endLevel = unit->m_envvals[(stage * 4) + 4];
float pos = (segpos / seglen);
GET_ENV_VAL
}
}
OUT0(0) = level;
}
void IEnvGen_Dtor(IEnvGen* unit) { RTFree(unit->mWorld, unit->m_envvals); }
void IEnvGen_next_a(IEnvGen* unit, int inNumSamples) {
float* out = OUT(0);
float level = unit->m_level;
float* pointin = IN(0);
float offset = unit->m_offset;
int numStages = (int)IN0(3);
float point; // = unit->m_pointin;
float totalDur = IN0(4);
int stagemul;
// pointer, offset
// level0, numstages, totaldur,
// [initval, [dur, shape, curve, level] * N ]
for (int i = 0; i < inNumSamples; i++) {
if (pointin[i] == unit->m_pointin) {
out[i] = level;
} else {
unit->m_pointin = point = sc_max(pointin[i] - offset, 0.0);
float newtime = 0.f;
int stage = 0;
float seglen = 0.f;
if (point >= totalDur) {
unit->m_level = level = unit->m_envvals[numStages * 4]; // grab the last value
} else {
if (point <= 0.0) {
unit->m_level = level = unit->m_envvals[0];
} else {
float segpos = point;
// determine which segment the current time pointer needs
for (int j = 0; point >= newtime; j++) {
seglen = unit->m_envvals[(j * 4) + 1];
newtime += seglen;
segpos -= seglen;
stage = j;
}
stagemul = stage * 4;
segpos = segpos + seglen;
float begLevel = unit->m_envvals[stagemul];
int shape = (int)unit->m_envvals[stagemul + 2];
int curve = (int)unit->m_envvals[stagemul + 3];
float endLevel = unit->m_envvals[stagemul + 4];
float pos = (segpos / seglen);
GET_ENV_VAL
}
}
out[i] = level;
}
}
}
void IEnvGen_next_k(IEnvGen* unit, int inNumSamples) {
float* out = OUT(0);
float level = unit->m_level;
float pointin = IN0(0);
float offset = unit->m_offset;
int numStages = (int)IN0(3);
float point; // = unit->m_pointin;
float totalDur = IN0(4);
int stagemul;
// pointer, offset
// level0, numstages, totaldur,
// [initval, [dur, shape, curve, level] * N ]
for (int i = 0; i < inNumSamples; i++) {
if (pointin == unit->m_pointin) {
out[i] = level;
} else {
unit->m_pointin = point = sc_max(pointin - offset, 0.0);
float newtime = 0.f;
int stage = 0;
float seglen = 0.f;
if (point >= totalDur) {
unit->m_level = level = unit->m_envvals[numStages * 4]; // grab the last value
} else {
if (point <= 0.0) {
unit->m_level = level = unit->m_envvals[0];
} else {
float segpos = point;
// determine which segment the current time pointer needs
for (int j = 0; point >= newtime; j++) {
seglen = unit->m_envvals[(j * 4) + 1];
newtime += seglen;
segpos -= seglen;
stage = j;
}
stagemul = stage * 4;
segpos = segpos + seglen;
float begLevel = unit->m_envvals[stagemul];
int shape = (int)unit->m_envvals[stagemul + 2];
int curve = (int)unit->m_envvals[stagemul + 3];
float endLevel = unit->m_envvals[stagemul + 4];
float pos = (segpos / seglen);
GET_ENV_VAL
}
}
out[i] = level;
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
PluginLoad(LF) {
ft = inTable;
DefineSimpleUnit(Vibrato);
DefineSimpleUnit(LFPulse);
DefineSimpleUnit(LFSaw);
DefineSimpleUnit(LFPar);
DefineSimpleUnit(LFCub);
DefineSimpleUnit(LFTri);
DefineSimpleUnit(LFGauss);
DefineSimpleUnit(Impulse);
DefineSimpleUnit(VarSaw);
DefineSimpleUnit(SyncSaw);
registerUnit<K2A>(ft, "K2A");
DefineSimpleUnit(A2K);
DefineSimpleUnit(T2K);
DefineSimpleUnit(T2A);
registerUnit<DC>(ft, "DC");
DefineSimpleUnit(Line);
DefineSimpleUnit(XLine);
DefineSimpleUnit(Wrap);
DefineSimpleUnit(Fold);
DefineSimpleUnit(Clip);
DefineSimpleUnit(Unwrap);
DefineSimpleUnit(ModDif);
DefineSimpleUnit(AmpComp);
DefineSimpleUnit(AmpCompA);
DefineSimpleUnit(InRange);
DefineSimpleUnit(InRect);
DefineSimpleUnit(LinExp);
DefineSimpleUnit(EnvGen);
DefineSimpleUnit(Linen);
DefineBufGen("env", EnvFill);
DefineDtorUnit(IEnvGen);
}
|