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 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571 9572 9573 9574 9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595 9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706 9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722 9723 9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 9738 9739 9740 9741 9742 9743 9744 9745 9746 9747 9748 9749 9750 9751 9752 9753 9754 9755 9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769 9770 9771 9772 9773 9774 9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789 9790 9791 9792 9793 9794 9795 9796 9797 9798 9799 9800 9801 9802 9803 9804 9805 9806 9807 9808 9809 9810 9811 9812 9813 9814 9815 9816 9817 9818 9819 9820 9821 9822 9823 9824 9825 9826 9827 9828 9829 9830 9831 9832 9833 9834 9835 9836 9837 9838 9839 9840 9841 9842 9843 9844 9845 9846 9847 9848 9849 9850 9851 9852 9853 9854 9855 9856 9857 9858 9859 9860 9861 9862 9863 9864 9865 9866 9867 9868 9869 9870 9871 9872 9873 9874 9875 9876 9877 9878 9879 9880 9881 9882 9883 9884 9885 9886 9887 9888 9889 9890 9891 9892 9893 9894 9895 9896 9897 9898 9899 9900 9901 9902 9903 9904 9905 9906 9907 9908 9909 9910 9911 9912 9913 9914 9915 9916 9917 9918 9919 9920 9921 9922 9923 9924 9925 9926 9927 9928 9929 9930 9931 9932 9933 9934 9935 9936 9937 9938 9939 9940 9941 9942 9943 9944 9945 9946 9947 9948 9949 9950 9951 9952 9953 9954 9955 9956 9957 9958 9959 9960 9961 9962 9963 9964 9965 9966 9967 9968 9969 9970 9971 9972 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983 9984 9985 9986 9987 9988 9989 9990 9991 9992 9993 9994 9995 9996 9997 9998 9999 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10011 10012 10013 10014 10015 10016 10017 10018 10019 10020 10021 10022 10023 10024 10025 10026 10027 10028 10029 10030 10031 10032 10033 10034 10035 10036 10037 10038 10039 10040 10041 10042 10043 10044 10045 10046 10047 10048 10049 10050 10051 10052 10053 10054 10055 10056 10057 10058 10059 10060 10061 10062 10063 10064 10065 10066 10067 10068 10069 10070 10071 10072 10073 10074 10075 10076 10077 10078 10079 10080 10081 10082 10083 10084 10085 10086 10087 10088 10089 10090 10091 10092 10093 10094 10095 10096 10097 10098 10099 10100 10101 10102 10103 10104 10105 10106 10107 10108 10109 10110 10111 10112 10113 10114 10115 10116 10117 10118 10119 10120 10121 10122 10123 10124 10125 10126 10127 10128 10129 10130 10131 10132 10133 10134 10135 10136 10137 10138 10139 10140 10141 10142 10143 10144 10145 10146 10147 10148 10149 10150 10151 10152 10153 10154 10155 10156 10157 10158 10159 10160 10161 10162 10163 10164 10165 10166 10167 10168 10169 10170 10171 10172 10173 10174 10175 10176 10177 10178 10179 10180 10181 10182 10183 10184 10185 10186 10187 10188 10189 10190 10191 10192 10193 10194 10195 10196 10197 10198 10199 10200 10201 10202 10203 10204 10205 10206 10207 10208 10209 10210 10211 10212 10213 10214 10215 10216 10217 10218 10219 10220 10221 10222 10223 10224 10225 10226 10227 10228 10229 10230 10231 10232 10233 10234 10235 10236 10237 10238 10239 10240 10241 10242 10243 10244 10245 10246 10247 10248 10249 10250 10251 10252 10253 10254 10255 10256 10257 10258 10259 10260 10261 10262 10263 10264 10265 10266 10267 10268 10269 10270 10271 10272 10273 10274 10275 10276 10277 10278 10279 10280 10281 10282 10283 10284 10285 10286 10287 10288 10289 10290 10291 10292 10293 10294 10295 10296 10297 10298 10299 10300 10301 10302 10303 10304 10305 10306 10307 10308 10309 10310 10311 10312 10313 10314 10315 10316 10317 10318 10319 10320 10321 10322 10323 10324 10325 10326 10327 10328 10329 10330 10331 10332 10333 10334 10335 10336 10337 10338 10339 10340 10341 10342 10343 10344 10345 10346 10347 10348 10349 10350 10351 10352 10353 10354 10355 10356 10357 10358 10359 10360 10361 10362 10363 10364 10365 10366 10367 10368 10369 10370 10371 10372 10373 10374 10375 10376 10377 10378 10379 10380 10381 10382 10383 10384 10385 10386 10387 10388 10389 10390 10391 10392 10393 10394 10395 10396 10397 10398 10399 10400 10401 10402 10403 10404 10405 10406 10407 10408 10409 10410 10411 10412 10413 10414 10415 10416 10417 10418 10419 10420 10421 10422 10423 10424 10425 10426 10427 10428 10429 10430 10431 10432 10433 10434 10435 10436 10437 10438 10439 10440 10441 10442 10443 10444 10445 10446 10447 10448 10449 10450 10451 10452 10453 10454 10455 10456 10457 10458 10459 10460 10461 10462 10463 10464 10465 10466 10467 10468 10469 10470 10471 10472 10473 10474 10475 10476 10477 10478 10479 10480 10481 10482 10483 10484 10485 10486 10487 10488 10489 10490 10491 10492 10493 10494 10495 10496 10497 10498 10499 10500 10501 10502 10503 10504 10505 10506 10507 10508 10509 10510 10511 10512 10513 10514 10515 10516 10517 10518 10519 10520 10521 10522 10523 10524 10525 10526 10527 10528 10529 10530 10531 10532 10533 10534 10535 10536 10537 10538 10539 10540 10541 10542 10543 10544 10545 10546 10547 10548 10549 10550 10551 10552 10553 10554 10555 10556 10557 10558 10559 10560 10561 10562 10563 10564 10565 10566 10567 10568 10569 10570 10571 10572 10573 10574 10575 10576 10577 10578 10579 10580 10581 10582 10583 10584 10585 10586 10587 10588 10589 10590 10591 10592 10593 10594 10595 10596 10597 10598 10599 10600 10601 10602 10603 10604 10605 10606 10607 10608 10609 10610 10611 10612 10613 10614 10615 10616 10617 10618 10619 10620 10621 10622 10623 10624 10625 10626 10627 10628 10629 10630 10631 10632 10633 10634 10635 10636 10637 10638 10639 10640 10641 10642 10643 10644 10645 10646 10647 10648 10649 10650 10651 10652 10653 10654 10655 10656 10657 10658 10659 10660 10661 10662 10663 10664 10665 10666 10667 10668 10669 10670 10671 10672 10673 10674 10675 10676 10677 10678 10679 10680 10681 10682 10683 10684 10685 10686 10687 10688 10689 10690 10691 10692 10693 10694 10695 10696 10697 10698 10699 10700 10701 10702 10703 10704 10705 10706 10707 10708 10709 10710 10711 10712 10713 10714 10715 10716 10717 10718 10719 10720 10721 10722 10723 10724 10725 10726 10727 10728 10729 10730 10731 10732 10733 10734 10735 10736 10737 10738 10739 10740 10741 10742 10743 10744 10745 10746 10747 10748 10749 10750 10751 10752 10753 10754 10755 10756 10757 10758 10759 10760 10761 10762 10763 10764 10765 10766 10767 10768 10769 10770 10771 10772 10773 10774 10775 10776 10777 10778 10779 10780 10781 10782 10783 10784 10785 10786 10787 10788 10789 10790 10791 10792 10793 10794 10795 10796 10797 10798 10799 10800 10801 10802 10803 10804 10805 10806 10807 10808 10809 10810 10811 10812 10813 10814 10815 10816 10817 10818 10819 10820 10821 10822 10823 10824 10825 10826 10827 10828 10829 10830 10831 10832 10833 10834 10835 10836 10837 10838 10839 10840 10841 10842 10843 10844 10845 10846 10847 10848 10849 10850 10851 10852 10853 10854 10855 10856 10857 10858 10859 10860 10861 10862 10863 10864 10865 10866 10867 10868 10869 10870 10871 10872 10873 10874 10875 10876 10877 10878 10879 10880 10881 10882 10883 10884 10885 10886 10887 10888 10889 10890 10891 10892 10893 10894 10895 10896 10897 10898 10899 10900 10901 10902 10903 10904 10905 10906 10907 10908 10909 10910 10911 10912 10913 10914 10915 10916 10917
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<title>CLM</title>
<style type="text/css">
EM.red {color:red; font-style: normal}
EM.gen {font-weight: bold; font-style: normal}
EM.error {color:chocolate; font-style: normal}
EM.narg {color:chocolate; font-style: normal}
H1 {text-align: center}
DIV.centered {text-align: center}
UL {list-style-type: none}
EM.emdef {font-weight: bold; font-style: normal; padding-right: 0.2cm}
EM.noem {font-style: normal}
A {text-decoration:none}
A:hover {text-decoration:underline}
A.quiet {color:black; text-decoration:none}
A.quiet:hover {text-decoration:underline}
A.invisible {color:white; text-decoration:none}
A.def {font-weight: bold; font-style: normal; text-decoration:none; text-color:black; padding-right: 0.2cm}
A.olddef {font-style: normal; text-decoration:none; color:gray; padding-right: 0.2cm}
EM.gray {color:gray; font-style: normal}
EM.def {font-weight: bold; font-style: normal; text-decoration:none; text-color:black; padding-right: 0.2cm}
TD.green {background-color: lightgreen}
PRE.bluish {background-color: #f2f4ff}
TD.beige {background-color: beige}
TD.greenish {background-color: #eefdee}
PRE.indented {padding-left: 1.0cm}
IMG.indented {margin-left: 2.0cm}
TH.beige {background-color: beige;
border: 1px solid black;
padding-left: 0.2cm;
padding-right: 0.2cm;
padding-top: 0.1cm;
padding-bottom: 0.1cm;
}
TD.br {border: 1px solid lightgray;
padding-left: 0.2cm;
padding-right: 0.2cm;
padding-top: 0.1cm;
padding-bottom: 0.1cm;
}
TD.hightop {padding-top: 0.5cm;
}
IMG.noborder {border: none}
DIV.center {text-align: center}
DIV.scheme {background-color: #f2f4ff;
border: 1px solid gray;
padding-right: 1.0cm;
margin-bottom: 0.2cm;
}
DIV.ruby {background-color: #fbfbf0;
border: 1px solid gray;
padding-right: 1.0cm;
margin-bottom: 0.2cm;
}
DIV.forth {background-color: #eefdee;
border: 1px solid gray;
padding-right: 1.0cm;
margin-bottom: 0.2cm;
}
DIV.c {background-color: #f0f0f0;
border: 1px solid gray;
padding-right: 1.0cm;
margin-bottom: 0.2cm;
}
DIV.lisp {background-color: aliceblue;
border: 1px solid gray;
padding-right: 1.0cm;
margin-bottom: 0.2cm;
}
BODY.body {background-color: #ffffff; /* white */
margin-left: 0.5cm;
margin-right: 0.5cm;
}
TABLE.borderspaced {margin-top: 0.5cm;
margin-bottom: 0.5cm;
margin-left: 0.5cm;
border: 8px solid gray;
}
TABLE.pb {margin-top: 0.1cm;
margin-bottom: 0.5cm;
margin-left: 2.0cm;
border: 2px solid gray;
padding-left: 0.2cm;
padding-right: 0.2cm;
padding-top: 0.2cm;
padding-bottom: 0.2cm;
}
TABLE.grayborder {margin-top: 0.5cm;
margin-bottom: 0.5cm;
margin-left: 1.0cm;
border: 8px solid gray;
padding-left: 0.1cm;
padding-right: 0.1cm;
padding-top: 0.1cm;
padding-bottom: 0.1cm;
}
TABLE.contents {margin-top: 0.5cm;
margin-bottom: 0.5cm;
margin-left: 1.0cm;
border: 8px solid lightgray;
padding-left: 0.1cm;
padding-right: 0.1cm;
padding-top: 0.1cm;
padding-bottom: 0.1cm;
}
TABLE.method {margin-top: 0.2cm;
margin-bottom: 0.5cm;
margin-left: 1.0cm;
border: 1px solid gray;
padding-left: 0.1cm;
padding-right: 0.1cm;
padding-top: 0.1cm;
padding-bottom: 0.1cm;
}
TD.sumtitle {background-color: #eefdee;
border: 1px solid lightgray;
padding-top: 0.2cm;
padding-bottom: 0.2cm;
text-align: center;
}
TD.methodtitle {background-color: beige;
border: 1px solid gray;
padding-top: 0.2cm;
padding-bottom: 0.2cm;
text-align: center;
}
TD.inner {padding-right: 0.5cm;
padding-top: 0.1cm;
}
TD.center {text-align: center}
DIV.separator {margin-top: 40px;
margin-bottom: 15px;
border: 2px solid #00ff00; /* green */
background-color: #f5f5dc; /* beige */
padding-top: 4px;
width: 30%;
border-radius: 4px;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
}
DIV.topheader {margin-top: 10px;
margin-bottom: 40px;
border: 4px solid #00ff00; /* green */
background-color: #f5f5dc; /* beige */
font-family: 'Helvetica';
font-size: 30px;
text-align: center;
padding-top: 10px;
padding-bottom: 10px;
}
DIV.header {margin-top: 50px;
margin-bottom: 10px;
font-size: 20px;
font-weight: bold;
border: 4px solid #00ff00; /* green */
background-color: #f5f5dc; /* beige */
text-align: center;
padding-top: 20px;
padding-bottom: 20px;
}
DIV.innerheader {margin-top: 60px;
margin-bottom: 30px;
border: 4px solid #00ff00; /* green */
background-color: #eefdee; /* lightgreen */
padding-left: 30px;
width: 50%;
padding-top: 20px;
padding-bottom: 20px;
}
DIV.related {text-align:center;
border: 1px solid lightgray;
margin-top: 1.0cm;
margin-bottom: 1.0cm;
padding-top: 10px;
padding-bottom: 10px;
background-color: #f0f0f0;
}
DIV.inset {margin-left: 2.0cm;
margin-right: 2.0cm;
margin-top: 0.5cm;
margin-bottom: 0.5cm;
background-color: #f0f0f0;
padding-left: 0.25cm;
padding-right: 0.25cm;
padding-top: 0.25cm;
padding-bottom: 0.25cm;
}
DIV.inset_inline {background-color: #f0f0f0;
display: inline;
margin-left: 2.0cm;
padding-left: 0.25cm;
padding-right: 0.25cm;
padding-top: 0.25cm;
padding-bottom: 0.25cm;
}
DIV.contentscenter {text-align: center;
padding-top: 0.25cm;
padding-bottom: 0.25cm;
border: 1px solid gray;
}
TD.ic {
padding-left: 0.5cm;
padding-right: 0.1cm;
}
</style>
<!-- the latex stuff is always embedded in:
\documentclass{amsart}
\begin{document}
\thispagestyle{empty}
\small
\begin{displaymath}
...
\end{displaymath}
\end{document}
where the "displaymath" lines change to fit the situation
in the new (FC9) latex/pdf2png case, I need:
\documentclass[fleqn,11pt]{amsart}
\setlength\paperwidth{1500pt}
\setlength\textwidth{1200pt}
\begin{document}
...
and then in the output PDF file, set the line
/MediaBox [0 0 595.276 841.89]
to something like
/MediaBox [0 0 1595.276 841.89]
before calling pdf2png
pdf2png is in the cairo tarball (cairo/test dir)
-->
</head>
<body class="body">
<div class="topheader" id="sndclmtop">CLM</div>
<p>CLM (originally an acronym for Common Lisp Music) is a sound synthesis
package in the Music V family. This file describes CLM as implemented in Snd,
aiming primarily at the Scheme version.
CLM is based on a set of functions known
as "generators". These can be packaged into "instruments", and instrument calls
can be packaged into "note lists". (These names are just convenient historical artifacts).
The main emphasis here is on the generators;
note lists and instruments are described in <a href="sndscm.html">sndscm.html</a>.
</p>
<div class="center">Bill Schottstaedt (bil@ccrma.stanford.edu)</div>
<div class="related">
related documentation:
<a href="snd.html">snd.html </a>
<a href="extsnd.html">extsnd.html </a>
<a href="grfsnd.html">grfsnd.html </a>
<a href="sndscm.html">sndscm.html </a>
<a href="fm.html">fm.html </a>
<a href="sndlib.html">sndlib.html </a>
<a href="s7.html">s7.html </a>
<a href="s7-ffi.html">s7-ffi.html </a>
<a href="s7-scm.html">s7-scm.html </a>
<a href="index.html">index.html</a>
</div>
<div class="header">Contents</div>
<table class="grayborder">
<tr><td colspan=4><div class="contentscenter"><a href="#introduction">Introduction</a></div></td></tr>
<tr><td colspan=4><div class="contentscenter"><a href="#generators">Built-in Generators</a></div></td></tr>
<tr><td class="ic"><a href="#all-passdoc">all-pass</a></td><td class="ic">all-pass filter</td>
<td class="ic"><a href="#nrxydoc">nrxysin</a></td><td class="ic">n scaled sines</td></tr>
<tr><td class="ic"><a href="#asymmetric-fmdoc">asymmetric-fm</a></td><td class="ic">asymmetric fm</td>
<td class="ic"><a href="#ncosdoc">nsin</a></td><td class="ic">n equal amplitude sines</td></tr>
<tr><td class="ic"><a href="#combdoc">comb</a></td><td class="ic">comb filter</td>
<td class="ic"><a href="#one-poledoc">one-pole</a></td><td class="ic">one pole filter</td></tr>
<tr><td class="ic"><a href="#convolvedoc">convolve</a></td><td class="ic">convolution</td>
<td class="ic"><a href="#one-poledoc">one-zero</a></td><td class="ic">one zero filter</td></tr>
<tr><td class="ic"><a href="#delaydoc">delay</a></td><td class="ic">delay line</td>
<td class="ic"><a href="#oscildoc">oscil</a></td><td class="ic">sine wave and FM</td></tr>
<tr><td class="ic"><a href="#envdoc">env</a></td><td class="ic">line segment envelope</td>
<td class="ic"><a href="#in-anydoc">out-any</a></td><td class="ic">sound output</td></tr>
<tr><td class="ic"><a href="#filetosampledoc">file->sample</a></td><td class="ic">input sample from file</td>
<td class="ic"><a href="#phase-vocoderdoc">phase-vocoder</a></td><td class="ic">vocoder analysis and resynthesis</td></tr>
<tr><td class="ic"><a href="#filetoframple">file->frample</a></td><td class="ic">input frample from file</td>
<td class="ic"><a href="#polywavedoc">polyshape and polywave</a></td><td class="ic">waveshaping</td></tr>
<tr><td class="ic"><a href="#filterdoc">filter</a></td><td class="ic">direct form FIR/IIR filter</td>
<td class="ic"><a href="#sawtoothdoc">pulse-train</a></td><td class="ic">pulse train</td></tr>
<tr><td class="ic"><a href="#combdoc">filtered-comb</a></td><td class="ic">comb filter with filter on feedback</td>
<td class="ic"><a href="#randdoc">rand, rand-interp</a></td><td class="ic">random numbers, noise</td></tr>
<tr><td class="ic"><a href="#filterdoc">fir-filter</a></td><td class="ic">FIR filter</td>
<td class="ic"><a href="#readindoc">readin</a></td><td class="ic">sound input</td></tr>
<tr><td class="ic"><a href="#formantdoc">formant and firmant</a></td><td class="ic">resonance</td>
<td class="ic"><a href="#sampletofile">sample->file</a></td><td class="ic">output sample to file</td></tr>
<tr><td class="ic"><a href="#frampletofile">frample->file</a></td><td class="ic">output frample to file</td>
<td class="ic"><a href="#sawtoothdoc">sawtooth-wave</a></td><td class="ic">sawtooth</td></tr>
<tr><td class="ic"><a href="#granulatedoc">granulate</a></td><td class="ic">granular synthesis</td>
<td class="ic"><a href="#sawtoothdoc">square-wave</a></td><td class="ic">square wave</td></tr>
<tr><td class="ic"><a href="#filterdoc">iir-filter</a></td><td class="ic">IIR filter</td>
<td class="ic"><a href="#srcdoc">src</a></td><td class="ic">sampling rate conversion</td></tr>
<tr><td class="ic"><a href="#in-anydoc">in-any</a></td><td class="ic">sound file input</td>
<td class="ic"><a href="#ssb-amdoc">ssb-am</a></td><td class="ic">single sideband amplitude modulation</td></tr>
<tr><td class="ic"><a href="#locsigdoc">locsig</a></td><td class="ic">static sound placement</td>
<td class="ic"><a href="#table-lookupdoc">table-lookup</a></td><td class="ic">interpolated table lookup</td></tr>
<tr><td class="ic"><a href="#move-sounddoc">move-sound</a></td><td class="ic">sound motion</td>
<td class="ic"><a href="#delaydoc">tap</a></td><td class="ic">delay line tap</td></tr>
<tr><td class="ic"><a href="#moving-averagedoc">moving-average</a></td><td class="ic">moving window average</td>
<td class="ic"><a href="#sawtoothdoc">triangle-wave</a></td><td class="ic">triangle wave</td></tr>
<tr><td class="ic"><a href="#ncosdoc">ncos</a></td><td class="ic">n equal amplitude cosines</td>
<td class="ic"><a href="#one-poledoc">two-pole</a></td><td class="ic">two pole filter</td></tr>
<tr><td class="ic"><a href="#combdoc">notch</a></td><td class="ic">notch filter</td>
<td class="ic"><a href="#one-poledoc">two-zero</a></td><td class="ic">two zero filter</td></tr>
<tr><td class="ic"><a href="#nrxydoc">nrxycos</a></td><td class="ic">n scaled cosines</td>
<td class="ic"><a href="#wave-traindoc">wave-train</a></td><td class="ic">wave train</td></tr>
<tr><td colspan=4><div class="contentscenter"><a href="#genericfunctions">Generic Functions</a></div></td></tr>
<tr><td colspan=4><div class="contentscenter"><a href="#othergenerators">Other Generators</a></div></td></tr>
<tr><td colspan=4><div class="contentscenter"><a href="#otherfunctions">Other Functions</a></div></td></tr>
<tr><td class="ic"><a href="#autocorrelate">autocorrelate</a></td><td class="ic">autocorrelation</td>
<td class="ic"><a href="#dot-product">dot-product</a></td><td class="ic">dot (scalar) product</td></tr>
<tr><td class="ic"><a href="#amplitude-modulate">amplitude-modulate</a></td><td class="ic">sig1 * (car + sig2)</td>
<td class="ic"><a href="#fft">fft</a></td><td class="ic">Fourier transform</td></tr>
<tr><td class="ic"><a href="#array-interp">array-interp</a></td><td class="ic">array interpolation</td>
<td class="ic"><a href="#make-fft-window">make-fft-window</a></td><td class="ic">various standard windows</td></tr>
<tr><td class="ic"><a href="#contrast-enhancement">contrast-enhancement</a></td><td class="ic">modulate signal</td>
<td class="ic"><a href="#polynomial">polynomial</a></td><td class="ic">Horner's rule</td></tr>
<tr><td class="ic"><a href="#convolution">convolution</a></td><td class="ic">convolve signals</td>
<td class="ic"><a href="#ring-modulate">ring-modulate</a></td><td class="ic">sig * sig</td></tr>
<tr><td class="ic"><a href="#correlate">correlate</a></td><td class="ic">cross correlation</td>
<td class="ic"><a href="#spectrum">spectrum</a></td><td class="ic">power spectrum of signal</td></tr>
<tr><td colspan=4><div class="contentscenter"><a href="#instruments">Instruments</a></div></td></tr>
</table>
<div class="header" id="introduction">Introduction</div>
<p>Start Snd, open the listener (choose "Show listener" in the View menu), and:
</p>
<pre class="indented">
> (load "v.scm")
fm-violin
> (with-sound () (fm-violin 0 1 440 .1))
"test.snd"
</pre>
<p>
If all went well, you should see a graph of the fm-violin's output. Click the "play" button to
hear it; click "f" to see its spectrum.
</p>
<p>
In Ruby, we'd do it this way:
</p>
<pre class="indented">
>load "v.rb"
true
>with_sound() do fm_violin_rb(0, 1.0, 440.0, 0.1) end
#<With_CLM: output: "test.snd", channels: 1, srate: 22050>
</pre>
<p>and in Forth:
</p>
<pre class="indented">
snd> "clm-ins.fs" file-eval
0
snd> 0.0 1.0 440.0 0.1 ' fm-violin with-sound
\ filename: test.snd
</pre>
<p>In most of this document, I'll stick with Scheme as implemented by s7.
<a href="extsnd.html">extsnd.html</a> and <a href="sndscm.html">sndscm.html</a> have numerous
Ruby and Forth examples, and I'll toss some in here as I go along.
You can save yourself a lot of typing by using two features of the listener. First, <TAB> (that is, the key marked TAB) tries to complete
the current name, so if you type "fm-<TAB>" the listener completes the name as "fm-violin".
And second, you can back up to a previous expression, edit it, move the cursor to the closing parenthesis, and
type <RETURN>, and that expression will be evaluated as if you had typed all of it in from the start.
Needless to say, you can paste code from this file into the Snd listener.
</p>
<p>with-sound opens an output sound file, evaluates its body, closes the file, and then opens it in Snd.
If the sound is already open, with-sound replaces it with the new version.
The body of with-sound can be any size, and can include anything that you could put in a function body.
For example,
to get an arpeggio:</p>
<pre class="indented">
(with-sound ()
(do ((i 0 (+ i 1)))
((= i 8))
(fm-violin (* i .25) .5 (* 100 (+ i 1)) .1)))
</pre>
<p>with-sound, instruments, CLM itself are all optional, of course. We could
do everything by hand:
</p>
<pre class="indented">
(let ((increment (/ (* 440.0 2.0 pi) 22050.0))
(current-phase 0.0))
(<a class=quiet href="extsnd.html#newsound">new-sound</a> "test.snd" :size 22050)
(<a class=quiet href="extsnd.html#mapchannel">map-channel</a> (lambda (y)
(let ((val (* .1 (sin current-phase))))
(set! current-phase (+ current-phase increment))
val))))
</pre>
<p>This opens a sound file (via <a href="extsnd.html#newsound">new-sound</a>) and fills it with a .1 amplitude sine wave at 440 Hz.
The "increment" calculation turns 440 Hz into a phase increment in radians (we could also use the function <a href="#hztoradians">hz->radians</a>).
The "oscil" generator keeps track of the phase increment for us, so
essentially the same thing using with-sound and oscil is:
</p>
<pre class="indented">
(with-sound ()
(let ((osc (<a class=quiet href="#make-oscil">make-oscil</a> 440.0)))
(do ((i 0 (+ i 1)))
((= i 44100))
(<a class=quiet href="#outa">outa</a> i (* .1 (<a class=quiet href="#oscil">oscil</a> osc)) *output*))))
</pre>
<p>*output* is the file opened by with-sound, and outa is a function that adds its second
argument (the sinusoid) into the current output at the sample given by its first argument
("i" in this case). oscil is our sinusoid generator, created by make-oscil. You don't
need to worry about freeing the oscil; we can depend on the Scheme garbage collector to
deal with that. All the generators are like oscil in that
each is a function that on each call returns the next sample in an infinite stream of samples.
An oscillator, for example, returns an endless sine wave, one sample
at a time.
Each generator consists of a set of functions: make-<gen> sets up the
data structure associated with the generator;
<gen> produces a new sample;
<gen>? checks whether a variable is that kind of generator.
Current generator state is accessible via various generic functions such as mus-frequency:
</p>
<pre class="indented">
(set! oscillator (<a class=quiet href="#make-oscil">make-oscil</a> :frequency 330))
</pre>
<p>prepares "oscillator" to produce a sine wave
when set in motion via</p>
<pre class="indented">
(<a class=quiet href="#oscil">oscil</a> oscillator)
</pre>
<p>
The make-<gen> function
takes a number of optional arguments, setting whatever state the given
generator needs to operate on. The run-time function's first argument is
always its associated structure. Its second argument is nearly always
something like an FM input or whatever run-time modulation might be
desired.
Frequency sweeps of all kinds (vibrato, glissando, breath
noise, FM proper) are all forms of frequency modulation. So, in
normal usage, our oscillator looks something like:</p>
<pre class="indented">
(<a class=quiet href="#oscil">oscil</a> oscillator (+ vibrato glissando frequency-modulation))
</pre>
<p>One special aspect of each make-<gen> function is the way it
reads its arguments. I use parenthesized parameters
in the function definitions to indicate that the argument names are
keywords, but the keywords themselves are optional.
Take the make-oscil call, defined as:</p>
<pre class="indented">
make-oscil (frequency 0.0) (initial-phase 0.0)
</pre>
<p>This says that make-oscil has two optional arguments, frequency (in Hz), and
initial-phase (in radians). The keywords associated with these values are
:frequency and :initial-phase.
When make-oscil is called, it scans its arguments; if a keyword is seen, that
argument and all following arguments are passed unchanged, but if a value is
seen, the corresponding keyword is prepended in the argument list:
</p>
<pre class="indented">
(<a class=quiet href="#make-oscil">make-oscil</a> :frequency 440.0)
(<a class=quiet href="#make-oscil">make-oscil</a> :frequency 440.0 :initial-phase 0.0)
(<a class=quiet href="#make-oscil">make-oscil</a> 440.0)
(<a class=quiet href="#make-oscil">make-oscil</a> 440.0 :initial-phase 0.0)
(<a class=quiet href="#make-oscil">make-oscil</a> 440.0 0.0)
</pre>
<p>are all equivalent, but</p>
<pre class="indented">
(<a class=quiet href="#make-oscil">make-oscil</a> :frequency 440.0 0.0)
(<a class=quiet href="#make-oscil">make-oscil</a> :initial-phase 0.0 440.0)
</pre>
<p>are in error, because once we see any keyword, all the rest of the arguments have
to use keywords too (we can't reliably make any assumptions after that point about argument
ordering).
This style of argument passing is the same as that of s7's define*, and is very similar to the "Optional
Positional and Named Parameters" extension of scheme: <a href="http://srfi.schemers.org/srfi-89/">SRFI-89</a>.
</p>
<p>Since we often want to use a given sound-producing algorithm many times (in a note list,
for example), it is convenient to package up that code into a function. Our sinewave
could be rewritten:
</p>
<pre class="indented">
(define (simp start end freq amp)
(let ((os (<a class=quiet href="#make-oscil">make-oscil</a> freq)))
(do ((i start (+ i 1)))
((= i end))
(<a class=quiet href="#outa">outa</a> i (* amp (<a class=quiet href="#oscil">oscil</a> os)))))) ; outa output defaults to *output* so we can omit it
</pre>
<p>Now to hear our sine wave:</p>
<pre class="indented">
(with-sound (:play #t) (simp 0 44100 330 .1))
</pre>
<p>This version of "simp" forces you to think in terms of sample numbers ("start" and "end") which
are dependent on the sampling rate. Our first enhancement is to use seconds:
</p>
<pre class="indented">
(define (simp beg dur freq amp)
(let ((os (<a class=quiet href="#make-oscil">make-oscil</a> freq))
(<em class=red>start</em> (<a class=quiet href="#secondstosamples">seconds->samples</a> beg))
(<em class=red>end</em> (<a class=quiet href="#secondstosamples">seconds->samples</a> (+ beg dur))))
(do ((i start (+ i 1)))
((= i end))
(<a class=quiet href="#outa">outa</a> i (* amp (<a class=quiet href="#oscil">oscil</a> os))))))
</pre>
<p>Now we can use any sampling rate, and call "simp" using seconds:
</p>
<pre class="indented">
(with-sound (:srate 44100) (simp 0 1.0 440.0 0.1))
</pre>
<p>Next we turn the "simp" function into an "instrument". An instrument is
a function that has a variety of built-in actions within with-sound. The only change
is the word "definstrument":
</p>
<pre class="indented">
(<em class=red>definstrument</em> (simp beg dur freq amp)
(let ((os (<a class=quiet href="#make-oscil">make-oscil</a> freq))
(start (<a class=quiet href="#secondstosamples">seconds->samples</a> beg))
(end (<a class=quiet href="#secondstosamples">seconds->samples</a> (+ beg dur))))
(do ((i start (+ i 1)))
((= i end))
(<a class=quiet href="#outa">outa</a> i (* amp (<a class=quiet href="#oscil">oscil</a> os))))))
</pre>
<p>Now we can simulate a telephone:
</p>
<pre class="indented">
(define (telephone start telephone-number)
(do ((touch-tab-1 '(0 697 697 697 770 770 770 852 852 852 941 941 941))
(touch-tab-2 '(0 1209 1336 1477 1209 1336 1477 1209 1336 1477 1209 1336 1477))
(i 0 (+ i 1)))
((= i (length telephone-number)))
(let* ((num (telephone-number i))
(frq1 (touch-tab-1 num))
(frq2 (touch-tab-2 num)))
(<em class=red>simp</em> (+ start (* i .4)) .3 frq1 .1)
(<em class=red>simp</em> (+ start (* i .4)) .3 frq2 .1))))
(with-sound () (telephone 0.0 '(7 2 3 4 9 7 1)))
</pre>
<p>As a last change, let's add an amplitude envelope:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#definstrument">definstrument</a> (simp beg dur freq amp envelope)
(let ((os (<a class=quiet href="#make-oscil">make-oscil</a> freq))
(<em class=red>amp-env</em> (<a class=quiet href="#make-env">make-env</a> envelope :duration dur :scaler amp))
(start (<a class=quiet href="#secondstosamples">seconds->samples</a> beg))
(end (<a class=quiet href="#secondstosamples">seconds->samples</a> (+ beg dur))))
(do ((i start (+ i 1)))
((= i end))
(<a class=quiet href="#outa">outa</a> i (* (<a class=quiet href="#env">env</a> <em class=red>amp-env</em>) (<a class=quiet href="#oscil">oscil</a> os))))))
</pre>
<p>A CLM envelope is a list of (x y) break-point pairs. The
x-axis bounds are arbitrary, but it is conventional (here at ccrma) to
go from 0 to 1.0. The y-axis values are normally between -1.0 and
1.0, to make it easier to figure out how to apply the envelope in
various different situations.
</p>
<pre class="indented">
(with-sound () (simp 0 2 440 .1 '(0 0 0.1 1.0 1.0 0.0)))
</pre>
<p>Add a few more oscils and envs, and you've got the fm-violin. You can try out a generator or a patch of generators quickly by
plugging it into the following with-sound call:
</p>
<pre class="indented">
(with-sound ()
(let ((sqr (make-square-wave 100))) ; test a square-wave generator
(do ((i 0 (+ i 1)))
((= i 10000))
(outa i (square-wave sqr)))))
</pre>
<p>Many people find the syntax of "do" confusing. It's possible to hide that
away in a macro:
</p>
<pre class="indented">
(define-macro (output beg dur . body)
`(do ((i (seconds->samples ,beg) (+ i 1)))
((= i (seconds->samples (+ ,beg ,dur))))
(outa i (begin ,@body))))
(define (simp beg dur freq amp)
(let ((o (make-oscil freq)))
(output beg dur (* amp (oscil o)))))
(with-sound ()
(simp 0 1 440 .1)
(simp .5 .5 660 .1))
</pre>
<p>It's also possible to use recursion, rather than iteration:
</p>
<pre class="indented">
(define (simp1)
(let ((freq (hz->radians 440.0)))
(let <em class=red>simp-loop</em> ((i 0) (x 0.0))
(outa i (sin x))
(if (< i 44100)
(<em class=red>simp-loop</em> (+ i 1) (+ x freq))))))
(define <em class=red>simp2</em>
(let ((freq (hz->radians 440.0)))
(lambda* ((i 0) (x 0.0))
(outa i (sin x))
(if (< i 44100)
(<em class=red>simp2</em> (+ i 1) (+ x freq))))))
</pre>
<p>but the do-loop is faster.
</p>
<!--
(define samps 44100)
(define (simp1)
(let ((freq (hz->radians 440.0)))
(do ((i 0 (+ i 1))
(x 0.0 (+ x freq)))
((= i samps))
(outa i (sin x)))))
(define (simp2)
(let ((osc (make-oscil 440.0)))
(do ((i 0 (+ i 1)))
((= i samps))
(outa i (oscil osc)))))
(define (simp3)
(let ((freq (hz->radians 440.0)))
(let simp-loop ((i 0) (x 0.0))
(outa i (sin x))
(if (< i samps)
(simp-loop (+ i 1) (+ x freq))))))
(define simp4
(let ((freq (hz->radians 440.0)))
(lambda* ((i 0) (x 0.0))
(outa i (sin x))
(if (< i samps)
(simp4 (+ i 1) (+ x freq))))))
(define-macro (time a)
`(let ((start (get-internal-real-time)))
,a
(* 1.0 (- (get-internal-real-time) start))))
(with-sound (:clipped #f)
(format *stderr* "~,4F~%" (time (simp1)))
(format *stderr* "~,4F~%" (time (simp2)))
(format *stderr* "~,4F~%" (time (simp3)))
(format *stderr* "~,4F~%" (time (simp4))))
#|
0.0044
0.0017
0.0165
0.0276
|#
-->
<!-- INDEX generators:Generators -->
<div class="header" id="generators">Generators</div>
<!-- OSCIL -->
<div class="innerheader" id="oscildoc">oscil</div>
<pre class="indented">
<em class=def id="make-oscil">make-oscil</em> (frequency 0.0) (initial-phase 0.0)
<em class=def id="oscil">oscil</em> os (fm-input 0.0) (pm-input 0.0)
<em class=def id="oscil?">oscil?</em> os
<em class=def id="make-oscil-bank">make-oscil-bank</em> freqs phases amps stable
<em class=def id="oscil-bank">oscil-bank</em> os fms
<em class=def id="oscil-bank?">oscil-bank?</em> os
</pre>
<table class="method">
<tr><td colspan=2 class="methodtitle">oscil methods</td></tr>
<tr><td class="inner"><em class=gen>mus-frequency</em></td> <td class="inner">frequency in Hz</td></tr>
<tr><td class="inner"><em class=gen>mus-phase</em></td> <td class="inner">phase in radians</td></tr>
<tr><td class="inner"><em class=gen>mus-length</em></td> <td class="inner">1 (no set!)</td></tr>
<tr><td class="inner"><em class=gen>mus-increment</em></td> <td class="inner">frequency in radians per sample</td></tr>
</table>
<p>oscil produces a sine wave (using sin) with optional frequency change (FM).
It might be defined:
</p>
<pre class="indented">
(let ((result (sin (+ phase pm-input))))
(set! phase (+ phase (<a class=quiet href="#hztoradians">hz->radians</a> frequency) fm-input))
result)
</pre>
<!-- <img src="pix/sceq9.png" alt="fnm equation"> -->
<!-- LATEX: \cos \, (\omega_{c}t+B\sin \omega_{m}t)\:=\!\!\sum_{n=-\infty}^{\infty} \! \! J_{n}(B)\cos(\omega_{c} + n\omega_{m})t -->
<p>oscil's first argument is an oscil created by make-oscil.
Oscil's second argument is the
frequency change (frequency modulation), and the third argument is the
phase change (phase modulation).
The initial-phase argument to make-oscil is in radians. You can
use <a href="#degreestoradians">degrees->radians</a> to convert from degrees to radians.
To get a cosine (as opposed to sine), set the initial-phase to (/ pi 2).
Here are examples in Scheme, Ruby, and Forth:
</p>
<table><tr><td>
<div class="scheme">
<pre class="indented">
(with-sound (:play #t)
(let ((gen (make-oscil 440.0)))
(do ((i 0 (+ i 1)))
((= i 44100))
(outa i (* 0.5 (oscil gen))))))
</pre>
</div>
</td></tr><tr><td>
<div class="ruby">
<pre class="indented">
with_sound(:play, true) do
gen = make_oscil(440.0);
44100.times do |i|
outa(i, 0.5 * oscil(gen), $output)
end
end.output
</pre>
</div>
</td></tr><tr><td>
<div class="forth">
<pre class="indented">
lambda: ( -- )
440.0 make-oscil { gen }
44100 0 do
i gen 0 0 oscil f2/ *output* outa drop
loop
; :play #t with-sound drop
</pre>
</div>
</td></tr></table>
<p>One slightly confusing aspect of oscil is that glissando has to be turned into a phase-increment envelope.
This means that the frequency envelope y values should be passed through <a href="#hztoradians">hz->radians</a>:
</p>
<pre class="indented">
(define (simp start end freq amp frq-env)
(let ((os (make-oscil freq))
(frqe (<a class=quiet href="#make-env">make-env</a> frq-env :length (- (+ end 1) start) :scaler (<em class=red>hz->radians</em> freq))))
(do ((i start (+ i 1)))
((= i end))
(<a class=quiet href="#outa">outa</a> i (* amp (oscil os (<a class=quiet href="#env">env</a> <em class=red>frqe</em>)))))))
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> () (simp 0 10000 440 .1 '(0 0 1 1))) ; sweep up an octave
</pre>
<p>Here is an example of FM (here the <a class=quiet href="#hztoradians">hz->radians</a> business is folded into the FM index):
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#definstrument">definstrument</a> (simple-fm beg dur freq amp mc-ratio index amp-env index-env)
(let* ((start (<a class=quiet href="#secondstosamples">seconds->samples</a> beg))
(end (+ start (<a class=quiet href="#secondstosamples">seconds->samples</a> dur)))
(cr (<em class=red>make-oscil</em> freq)) ; carrier
(md (<em class=red>make-oscil</em> (* freq mc-ratio))) ; modulator
(fm-index (<a class=quiet href="#hztoradians">hz->radians</a> (* index mc-ratio freq)))
(ampf (<a class=quiet href="#make-env">make-env</a> (or amp-env '(0 0 .5 1 1 0)) :scaler amp :duration dur))
(indf (<a class=quiet href="#make-env">make-env</a> (or index-env '(0 0 .5 1 1 0)) :scaler fm-index :duration dur)))
(do ((i start (+ i 1)))
((= i end))
(<a class=quiet href="#outa">outa</a> i (* (<a class=quiet href="#env">env</a> ampf)
(<em class=red>oscil</em> cr (* (<a class=quiet href="#env">env</a> indf)
(<em class=red>oscil</em> md))))))))
;;; (<a class=quiet href="sndscm.html#wsdoc">with-sound</a> () (simple-fm 0 1 440 .1 2 1.0))
</pre>
<p><a href="fm.html">fm.html</a> has an introduction to FM.
FM and PM behave slightly differently during a glissando; FM is the more "natural" in that, left to its own devices,
it produces a spectrum that varies inversely with the pitch. Compare these two cases. Both involve a slow glissando
up an octave, FM in channel 0, and PM in channel 1. In the first note, I fix up the FM index during the sweep to
keep the spectra steady, and in the second, I fix up the PM index.
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:channels 2)
(let* ((dur 2.0)
(samps (<a class=quiet href="#secondstosamples">seconds->samples</a> dur))
(pitch 1000)
(modpitch 100)
(pm-index 4.0)
(fm-index (<a class=quiet href="#hztoradians">hz->radians</a> (* 4.0 modpitch))))
(let ((car1 (make-oscil pitch))
(mod1 (make-oscil modpitch))
(car2 (make-oscil pitch))
(mod2 (make-oscil modpitch))
(frqf (<a class=quiet href="#make-env">make-env</a> '(0 0 1 1) :duration dur))
(ampf (<a class=quiet href="#make-env">make-env</a> '(0 0 1 1 20 1 21 0) :duration dur :scaler .5)))
(do ((i 0 (+ i 1)))
((= i samps))
(let* ((frq (<a class=quiet href="#env">env</a> frqf))
(rfrq (<a class=quiet href="#hztoradians">hz->radians</a> frq))
(amp (<a class=quiet href="#env">env</a> ampf)))
(<a class=quiet href="#outa">outa</a> i (* amp (oscil car1 (+ (* rfrq pitch)
(* <em class=red>fm-index (+ 1 frq)</em> ; keep spectrum the same
(oscil mod1 (* rfrq modpitch)))))))
(<a class=quiet href="#outa">outb</a> i (* amp (oscil car2 (* rfrq pitch)
(* <em class=red>pm-index</em> (oscil mod2 (* rfrq modpitch)))))))))
(let ((car1 (make-oscil pitch))
(mod1 (make-oscil modpitch))
(car2 (make-oscil pitch))
(mod2 (make-oscil modpitch))
(frqf (<a class=quiet href="#make-env">make-env</a> '(0 0 1 1) :duration dur))
(ampf (<a class=quiet href="#make-env">make-env</a> '(0 0 1 1 20 1 21 0) :duration dur :scaler .5)))
(do ((i 0 (+ i 1)))
((= i samps))
(let* ((frq (<a class=quiet href="#env">env</a> frqf))
(rfrq (<a class=quiet href="#hztoradians">hz->radians</a> frq))
(amp (<a class=quiet href="#env">env</a> ampf)))
(<a class=quiet href="#outa">outa</a> (+ i samps) (* amp (oscil car1 (+ (* rfrq pitch)
(* <em class=red>fm-index</em> ; let spectrum decay
(oscil mod1 (* rfrq modpitch)))))))
(<a class=quiet href="#outa">outb</a> (+ i samps) (* amp (oscil car2 (* rfrq pitch)
(* <em class=red>(/ pm-index (+ 1 frq))</em> (oscil mod2 (* rfrq modpitch)))))))))))
</pre>
<p>And if you read somewhere that PM can't produce a frequency shift:
</p>
<pre class="indented">
(with-sound ()
(let ((o (make-oscil 200.0))
(e (make-env '(0 0 1 1) :scaler 300.0 :duration 1.0)))
(do ((i 0 (+ i 1)))
((= i 44100))
(outa i (oscil o 0.0 (env e))))))
</pre>
<p>To show CLM in its various embodiments, here are the Scheme, Common Lisp, Ruby, Forth, and C versions of the bird instrument;
it produces a sinusoid with (usually very elaborate) amplitude and frequency envelopes.
</p>
<table>
<tr><td>
<div class="scheme">
<pre class="indented">
(define (scheme-bird start dur frequency freqskew amplitude freq-envelope amp-envelope)
(let* ((gls-env (<a class=quiet href="#make-env">make-env</a> freq-envelope (<a class=quiet href="#hztoradians">hz->radians</a> freqskew) dur))
(os (<a class=quiet href="#make-oscil">make-oscil</a> frequency))
(amp-env (<a class=quiet href="#make-env">make-env</a> amp-envelope amplitude dur))
(beg (<a class=quiet href="#secondstosamples">seconds->samples</a> start))
(end (+ beg (<a class=quiet href="#secondstosamples">seconds->samples</a> dur))))
(do ((i beg (+ i 1)))
((= i end))
(<a class=quiet href="#outa">outa</a> i (* (<a class=quiet href="#env">env</a> amp-env)
(<a class=quiet href="#oscil">oscil</a> os (<a class=quiet href="#env">env</a> gls-env)))))))
</pre>
</div>
</td></tr><tr><td>
<div class="lisp">
<pre class="indented">
(definstrument common-lisp-bird (startime dur frequency freq-skew amplitude freq-envelope amp-envelope)
(multiple-value-bind (beg end) (times->samples startime dur)
(let* ((amp-env (make-env amp-envelope amplitude dur))
(gls-env (make-env freq-envelope (<a class=quiet href="#hztoradians">hz->radians</a> freq-skew) dur))
(os (make-oscil frequency)))
(run
(loop for i from beg to end do
(outa i (* (env amp-env)
(oscil os (env gls-env)))))))))
</pre>
</div>
</td></tr><tr><td>
<div class="ruby">
<pre class="indented">
def ruby_bird(start, dur, freq, freqskew, amp, freq_envelope, amp_envelope)
gls_env = make_env(:envelope, freq_envelope, :scaler, hz2radians(freqskew), :duration, dur)
os = make_oscil(:frequency, freq)
amp_env = make_env(:envelope, amp_envelope, :scaler, amp, :duration, dur)
run_instrument(start, dur) do
env(amp_env) * oscil(os, env(gls_env))
end
end
</pre>
</div>
</td></tr><tr><td>
<div class="forth">
<pre class="indented">
instrument: forth-bird { f: start f: dur f: freq f: freq-skew f: amp freqenv ampenv -- }
:frequency freq make-oscil { os }
:envelope ampenv :scaler amp :duration dur make-env { ampf }
:envelope freqenv :scaler freq-skew hz>radians :duration dur make-env { gls-env }
90e random :locsig-degree
start dur run-instrument ampf env gls-env env os oscil-1 f* end-run
os gen-free
ampf gen-free
gls-env gen-free
;instrument
</pre>
</div>
</td></tr><tr><td>
<div class="c">
<pre class="indented">
void c_bird(double start, double dur, double frequency, double freqskew, double amplitude,
mus_float_t *freqdata, int freqpts, mus_float_t *ampdata, int amppts, mus_any *output)
{
mus_long_t beg, end, i;
mus_any *amp_env, *freq_env, *osc;
beg = start * mus_srate();
end = start + dur * mus_srate();
osc = mus_make_oscil(frequency, 0.0);
amp_env = mus_make_env(ampdata, amppts, amplitude, 0.0, 1.0, dur, 0, NULL);
freq_env = mus_make_env(freqdata, freqpts, mus_hz_to_radians(freqskew), 0.0, 1.0, dur, 0, NULL);
for (i = beg; i < end; i++)
mus_sample_to_file(output, i, 0,
mus_env(amp_env) *
mus_oscil(osc, mus_env(freq_env), 0.0));
mus_free(osc);
mus_free(amp_env);
mus_free(freq_env);
}
</pre>
</div>
</td></tr></table>
<p>Many of the CLM synthesis functions try to make it
faster or more convenient to produce a lot of sinusoids, but there
are times when nothing but a ton of oscils will do:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#withsound">with-sound</a> ()
(let* ((peaks (list 23 0.0051914 32 0.0090310 63 0.0623477 123 0.1210755 185 0.1971876
209 0.0033631 247 0.5797809 309 1.0000000 370 0.1713255 432 0.9351965
481 0.0369873 495 0.1335089 518 0.0148626 558 0.1178001 617 0.6353443
629 0.1462804 661 0.0208941 680 0.1739281 701 0.0260423 742 0.1203807
760 0.0070301 803 0.0272111 865 0.0418878 926 0.0090197 992 0.0098687
1174 0.00444 1298 0.0039722 2223 0.0033486 2409 0.0083675 2472 0.0100995
2508 0.004262 2533 0.0216248 2580 0.0047732 2596 0.0088663 2612 0.0040592
2657 0.005971 2679 0.0032541 2712 0.0048836 2761 0.0050938 2780 0.0098877
2824 0.003421 2842 0.0134356 2857 0.0050194 2904 0.0147466 2966 0.0338878
3015 0.004832 3027 0.0095497 3040 0.0041434 3092 0.0044802 3151 0.0038269
3460 0.003633 3585 0.0050849 4880 0.0042301 5121 0.0037906 5136 0.0048349
5158 0.004336 5192 0.0037841 5200 0.0038025 5229 0.0035555 5356 0.0045781
5430 0.003687 5450 0.0055170 5462 0.0057821 5660 0.0041789 5673 0.0044932
5695 0.007370 5748 0.0031716 5776 0.0037921 5800 0.0062308 5838 0.0034629
5865 0.005942 5917 0.0032254 6237 0.0046164 6360 0.0034708 6420 0.0044593
6552 0.005939 6569 0.0034665 6752 0.0041965 7211 0.0039695 7446 0.0031611
7468 0.003330 7482 0.0046322 8013 0.0034398 8102 0.0031590 8121 0.0031972
8169 0.003345 8186 0.0037020 8476 0.0035857 8796 0.0036703 8927 0.0042374
9388 0.003173 9443 0.0035844 9469 0.0053484 9527 0.0049137 9739 0.0032365
9853 0.004297 10481 0.0036424 10490 0.0033786 10606 0.0031366))
(len (/ (length peaks) 2))
(dur 10)
(oscs (make-vector len))
(amps (make-vector len))
(ramps (make-vector len))
(freqs (make-vector len))
(vib (<a class=quiet href="#make-rand-interp">make-rand-interp</a> 50 (<a class=quiet href="#hztoradians">hz->radians</a> .01)))
(ampf (<a class=quiet href="#make-env">make-env</a> '(0 0 1 1 10 1 11 0) :duration dur :scaler .1))
(samps (<a class=quiet href="#secondstosamples">seconds->samples</a> dur)))
(do ((i 0 (+ i 1)))
((= i len))
(set! (freqs i) (peaks (* i 2)))
(set! (oscs i) (<em class=red>make-oscil</em> (freqs i) (random pi)))
(set! (amps i) (peaks (+ 1 (* 2 i))))
(set! (ramps i) (<a class=quiet href="#make-rand-interp">make-rand-interp</a> (+ 1.0 (* i (/ 20.0 len)))
(* (+ .1 (* i (/ 3.0 len))) (amps i)))))
(do ((i 0 (+ i 1)))
((= i samps))
(let ((sum 0.0)
(fm (<a class=quiet href="#rand-interp">rand-interp</a> vib)))
(do ((k 0 (+ k 1)))
((= k len))
(set! sum (+ sum (* (+ (amps k)
(<a class=quiet href="#rand-interp">rand-interp</a> (ramps k)))
(<em class=red>oscil</em> (oscs k) (* (freqs k) fm))))))
(<a class=quiet href="#outa">outa</a> i (* (<a class=quiet href="#env">env</a> ampf) sum))))))
</pre>
<p>oscil-bank here would be faster, or <a href="#mus-chebyshev-tu-sum">mus-chebyshev-t-sum</a>:
</p>
<pre class="indented">
...
(amps (make-float-vector 10607))
(angle 0.0)
(freq (hz->radians 1.0))
...
(do ((i 0 (+ i 1))
(k 0 (+ k 2)))
((= i len))
(set! (amps (peaks k)) (peaks (+ k 1))))
...
(outa i (* (env ampf) (<em class=red>mus-chebyshev-t-sum</em> angle amps)))
(set! angle (+ angle freq (rand-interp vib)))
...
</pre>
<p>Here's a better example: we want to start with a sum of equal amplitude harmonically
related cosines (a sequence of spikes), and move slowly to a waveform with the same
magnitude spectrum, but with the phases chosen to minimize the peak amplitude.
</p>
<pre class="indented">
(let ((98-phases #(0.000000 -0.183194 0.674802 1.163820 -0.147489 1.666302 0.367236 0.494059 0.191339
0.714980 1.719816 0.382307 1.017937 0.548019 0.342322 1.541035 0.966484 0.936993
-0.115147 1.638513 1.644277 0.036575 1.852586 1.211701 1.300475 1.231282 0.026079
0.393108 1.208123 1.645585 -0.152499 0.274978 1.281084 1.674451 1.147440 0.906901
1.137155 1.467770 0.851985 0.437992 0.762219 -0.417594 1.884062 1.725160 -0.230688
0.764342 0.565472 0.612443 0.222826 -0.016453 1.527577 -0.045196 0.585089 0.031829
0.486579 0.557276 -0.040985 1.257633 1.345950 0.061737 0.281650 -0.231535 0.620583
0.504202 0.817304 -0.010580 0.584809 1.234045 0.840674 1.222939 0.685333 1.651765
0.299738 1.890117 0.740013 0.044764 1.547307 0.169892 1.452239 0.352220 0.122254
1.524772 1.183705 0.507801 1.419950 0.851259 0.008092 1.483245 0.608598 0.212267
0.545906 0.255277 1.784889 0.270552 1.164997 -0.083981 0.200818 1.204088))
(freq 10.0)
(dur 5.0)
(n 98))
(with-sound ()
(let ((samps (floor (* dur 44100)))
(1/n (/ 1.0 n))
(freqs (make-float-vector n))
(phases (make-float-vector n (* pi 0.5))))
(do ((i 0 (+ i 1)))
((= i n))
(let ((off (/ (* pi (- 0.5 (98-phases i))) dur 44100))
(h (hz->radians (* freq (+ i 1)))))
(set! (freqs i) (+ h off))))
(let ((ob (<em class=red>make-oscil-bank</em> freqs phases)))
(do ((i 0 (+ i 1))) ; get rid of the distracting initial click
((= i 1000))
(<em class=red>oscil-bank</em> ob))
(do ((k 0 (+ k 1)))
((= k samps))
(outa k (* 1/n (<em class=red>oscil-bank</em> ob))))))))
</pre>
<!--
(with-sound ()
(let* ((peaks (list 23 0.0051914 32 0.0090310 63 0.0623477 123 0.1210755 185 0.1971876
209 0.0033631 247 0.5797809 309 1.0000000 370 0.1713255 432 0.9351965
481 0.0369873 495 0.1335089 518 0.0148626 558 0.1178001 617 0.6353443
629 0.1462804 661 0.0208941 680 0.1739281 701 0.0260423 742 0.1203807
760 0.0070301 803 0.0272111 865 0.0418878 926 0.0090197 992 0.0098687
1174 0.00444 1298 0.0039722 2223 0.0033486 2409 0.0083675 2472 0.0100995
2508 0.004262 2533 0.0216248 2580 0.0047732 2596 0.0088663 2612 0.0040592
2657 0.005971 2679 0.0032541 2712 0.0048836 2761 0.0050938 2780 0.0098877
2824 0.003421 2842 0.0134356 2857 0.0050194 2904 0.0147466 2966 0.0338878
3015 0.004832 3027 0.0095497 3040 0.0041434 3092 0.0044802 3151 0.0038269
3460 0.003633 3585 0.0050849 4880 0.0042301 5121 0.0037906 5136 0.0048349
5158 0.004336 5192 0.0037841 5200 0.0038025 5229 0.0035555 5356 0.0045781
5430 0.003687 5450 0.0055170 5462 0.0057821 5660 0.0041789 5673 0.0044932
5695 0.007370 5748 0.0031716 5776 0.0037921 5800 0.0062308 5838 0.0034629
5865 0.005942 5917 0.0032254 6237 0.0046164 6360 0.0034708 6420 0.0044593
6552 0.005939 6569 0.0034665 6752 0.0041965 7211 0.0039695 7446 0.0031611
7468 0.003330 7482 0.0046322 8013 0.0034398 8102 0.0031590 8121 0.0031972
8169 0.003345 8186 0.0037020 8476 0.0035857 8796 0.0036703 8927 0.0042374
9388 0.003173 9443 0.0035844 9469 0.0053484 9527 0.0049137 9739 0.0032365
9853 0.004297 10481 0.0036424 10490 0.0033786 10606 0.0031366))
(len (/ (length peaks) 2))
(dur 10)
(ramps (make-vector len))
(vib (make-rand-interp 50 (hz->radians .01)))
(ampf (make-env '(0 0 1 1 10 1 11 0) :duration dur :scaler .1))
(samps (seconds->samples dur))
(amps (make-float-vector 10607))
(angle 0.0)
(freq (hz->radians 1.0)))
(do ((i 0 (+ i 1))
(k 0 (+ k 2)))
((= i len))
(set! (amps (peaks k)) (peaks (+ k 1))))
(do ((i 0 (+ i 1)))
((= i samps))
(outa i (* (env ampf)
(mus-chebyshev-t-sum angle amps)))
(set! angle (+ angle freq (rand-interp vib))))))
-->
<p>The last argument to make-oscil-bank, "stable", defaults to false. If it is
true, oscil-bank can assume that the frequency, phase, and amplitude values passed to
make-oscil-bank will not change over the life of the generator.
</p>
<p>
Related generators are
<a href="#ncos">ncos</a>,
<a href="#nsin">nsin</a>,
<a href="#asymmetric-fm">asymmetric-fm</a>, and
<a href="#nrxydoc">nrxysin</a>.
Some instruments that use oscil are
<a href="sndscm.html#birddoc">bird and bigbird</a>,
<a href="sndscm.html#fmviolin">fm-violin</a> (v),
<a href="sndscm.html#lbjpiano">lbj-piano</a> (clm-ins.scm),
<a href="sndscm.html#fmvox">vox</a> (clm-ins.scm), and
<a href="sndscm.html#fmbell">fm-bell</a> (clm-ins.scm).
Interesting extensions of oscil include the various
summation formulas in <a href="#othergenerators">generators.scm</a>.
To goof around with FM from a graphical interface, see bess.scm and bess1.scm.
</p>
<p>When oscil's frequency is high relative to the sampling rate,
the waveform it produces may not look very sinusoidal. Here, for example, is oscil
at 440 Hz when the srate is 1000, 4000, and 16000:
</p>
<img src="pix/srates.png" alt="effect of different srates">
<!--
(set! *selected-graph-color* (make-color 1 1 1))
(set! *selected-data-color* (make-color 0 0 0))
(set! *axis-numbers-font* *tiny-font*)
(set! (x-axis-label 0 0 0) "srate: 1000")
(set! (x-axis-label 1 0 0) "srate: 4000")
(set! (x-axis-label 2 0 0) "srate: 16000")
(set! *axis-label-font* "12x24")
(with-sound (:srate 1000) ; 4000 16000
(let ((gen (make-oscil 440.0)))
(do ((i 0 (+ i 1)))
((= i 20000))
(outa i (oscil gen)))))
-->
<!-- ENV -->
<div class="innerheader" id="envdoc">env</div>
<pre class="indented">
<em class=def id="make-env">make-env</em>
envelope ; list or float-vector of x,y break-point pairs
(scaler 1.0) ; scaler on every y value (before offset is added)
duration ; duration in seconds
(offset 0.0) ; value added to every y value
base ; type of connecting line between break-points
end ; end sample number (obsolete, use length)
length ; duration in samples
<em class=def id="env">env</em> e
<em class=def id="env?">env?</em> e
<em class=def id="env-interp">env-interp</em> x env (base 1.0) ;value of env at x
<em class=def id="env-any">env-any</em> e connecting-function
<em class=def id="envelopeinterp">envelope-interp</em> x env (base 1.0)
<em class=def id="make-pulsed-env">make-pulsed-env</em> envelope duration frequency
<em class=def id="pulsedenv">pulsed-env</em> gen (fm 0.0)
<em class=def id="pulsedenv?">pulsed-env?</em> gen
</pre>
<table class="method">
<tr><td colspan=2 class="methodtitle">env methods</td></tr>
<tr><td class="inner"><em class=gen>mus-location</em></td> <td class="inner">number of calls so far on this env</td></tr>
<tr><td class="inner"><em class=gen>mus-increment</em></td><td class="inner">base</td></tr>
<tr><td class="inner"><em class=gen>mus-data</em></td> <td class="inner">original breakpoint list</td></tr>
<tr><td class="inner"><em class=gen>mus-scaler</em></td> <td class="inner">scaler</td></tr>
<tr><td class="inner"><em class=gen>mus-offset</em></td> <td class="inner">offset</td></tr>
<tr><td class="inner"><em class=gen>mus-length</em></td> <td class="inner">duration in samples</td></tr>
<tr><td class="inner"><em class=gen>mus-channels</em></td> <td class="inner">current position in the break-point list</td></tr>
</table>
<p>An envelope is a list or float-vector of break point pairs: <code>'(0 0 100 1)</code> is
a ramp from 0 to 1 over an x-axis excursion from 0 to 100, as is <code>(float-vector 0 0 100 1)</code>.
This data is passed
to make-env along with the scaler (multiplier)
applied to the y axis, the offset added to every y value,
and the time in samples or seconds that the x axis represents.
make-env returns an env generator.
env then returns the next sample of the envelope each time it is called.
Say we want a ramp moving from .3 to .5 during 1 second.
</p>
<pre class="indented">
(make-env '(0 0 100 1) :scaler .2 :offset .3 :duration 1.0)
(make-env '(0 .3 1 .5) :duration 1.0)
</pre>
<p>
I find the second version easier to read. The first is handy if you have a
bunch of stored envelopes. To specify the breakpoints, you can also use the form <code>'((0 0) (100 1))</code>.
I used "scaler" decades ago because I didn't like the spelling "scalar". According
to the OED, "scalar" goes back to the 17th century, and derives from "scala", a ladder, ultimately from
Latin. "scaler" is also old, and refers to one who scales a mountain or a fish. Well, I still
like "scaler" better: We're staring at a "peak"! "gain" looks like an escapee from the EE lab. "volume" is too specific.
Maybe "scl" or "*"?
</p>
<img src="pix/pyr.png" alt="an envelope">
<br>
<table><tr><td>
<div class="scheme">
<pre class="indented">
(with-sound (:play #t)
(let ((gen (make-oscil 440.0))
(ampf (make-env '(0 0 .01 1 .25 .1 1 0)
:scaler 0.5
:length 44100)))
(do ((i 0 (+ i 1)))
((= i 44100))
(outa i (* (env ampf) (oscil gen))))))
</pre>
</div>
</td></tr><tr><td>
<div class="ruby">
<pre class="indented">
with_sound(:play, true) do
gen = make_oscil(440.0);
ampf = make_env(
[0, 0, 0.01, 1.0, 0.25, 0.1, 1, 0],
:scaler, 0.5,
:length, 44100);
44100.times do |i|
outa(i, env(ampf) * oscil(gen), $output)
end
end.output
</pre>
</div>
</td></tr><tr><td>
<div class="forth">
<pre class="indented">
lambda: ( -- )
440.0 make-oscil { gen }
'( 0 0 0.01 1 0.25 0.1 1 0 )
:scaler 0.5 :length 44100 make-env { ampf }
44100 0 do
i gen 0 0 oscil ampf env f* *output* outa drop
loop
; :play #t with-sound drop
</pre>
</div>
</td></tr></table>
<p>The base argument determines how the break-points are connected. If it is 1.0 (the
default), you get straight line segments. If base is 0.0, you get a step
function (the envelope changes its value suddenly to the new one without any
interpolation). Any other positive value affects the exponent of the exponential curve
connecting the points. A base less than 1.0 gives convex curves (i.e. bowed
out), and a base greater than 1.0 gives concave curves (i.e. sagging).
If you'd rather think in terms of e^-kt, set the base to <code>(exp k)</code>.
</p>
<img src="pix/pyr03.png" alt="base .03 choice">
<img src="pix/pyr32.png" alt="base 32 choice">
<p>
You can get a lot from a couple of envelopes:
</p>
<pre class="indented">
> (load "animals.scm")
#<unspecified>
> (with-sound (:play #t) (pacific-chorus-frog 0 .5))
"test.snd"
> (with-sound (:play #t) (house-finch 0 .5))
"test.snd"
</pre>
<p>
There are several ways to get arbitrary connecting curves between the break points.
The simplest method is to treat
the output of env as the input to the connecting function. Here's an
instrument that maps the line segments into sin x^3:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#definstrument">definstrument</a> (mapenv beg dur frq amp en)
(let* ((start (<a class=quiet href="#secondstosamples">seconds->samples</a> beg))
(end (+ start (<a class=quiet href="#secondstosamples">seconds->samples</a> dur)))
(osc (<a class=quiet href="#make-oscil">make-oscil</a> frq))
(zv (<em class=red>make-env</em> <em class=red>en</em> 1.0 dur)))
(do ((i start (+ i 1)))
((= i end))
(let ((zval (<em class=red>env</em> zv)))
(<a class=quiet href="#outa">outa</a> i
(* amp
(sin (* 0.5 pi zval zval zval))
(<a class=quiet href="#oscil">oscil</a> osc)))))))
(<a class=quiet href="sndscm.html#withsound">with-sound</a> ()
(mapenv 0 1 440 .5 '(0 0 50 1 75 0 86 .5 100 0)))
</pre>
<img src="pix/sincube.png" alt="sin cubed envelope">
<!--
(define (fixup-axes)
(set! *selected-data-color* (make-color 0 0 0))(set! *selected-data-color* (make-color 0 0 0))
(set! *selected-graph-color* (make-color 1 1 1))
(set! (x-axis-label 0 0) "sin^3 of '(0 0 50 1 75 0 86 .5 100 0)"))
-->
<p id="bellcurve">Another method is to write a function that traces out the curve you want.
J.C.Risset's bell curve is:</p>
<pre class="indented">
(define (bell-curve x)
;; x from 0.0 to 1.0 creates bell curve between .64e-4 and nearly 1.0
;; if x goes on from there, you get more bell curves; x can be
;; an envelope (a ramp from 0 to 1 if you want just a bell curve)
(+ .64e-4 (* .1565 (- (exp (- 1.0 (cos (* 2 pi x)))) 1.0))))
</pre>
<p>But the most flexible method is to use <b>env-any</b>.
env-any takes the env generator that produces the underlying envelope,
and a function to "connect the dots", and returns the new envelope
applying that connecting function between the break points.
For example, say we want to square each envelope value:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> ()
(let ((e (<a class=quiet href="#make-env">make-env</a> '(0 0 1 1 2 .25 3 1 4 0)
:duration 0.5)))
(do ((i 0 (+ i 1)))
((= i 44100))
(<a class=quiet href="#outa">outa</a> i (<em class=red>env-any</em> e (lambda (y) (* y y)))))))
;; or connect the dots with a sinusoid:
(define (sine-env e)
(<em class=red>env-any</em> e (lambda (y)
(* 0.5 (+ 1.0 (sin (+ (* -0.5 pi)
(* pi y))))))))
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> ()
(let ((e (<a class=quiet href="#make-env">make-env</a> '(0 0 1 1 2 .25 3 1 4 0)
:duration 0.5)))
(do ((i 0 (+ i 1)))
((= i 44100))
(<a class=quiet href="#outa">outa</a> i (sine-env e)))))
</pre>
<img src="pix/envany.png" alt="env-any pix">
<!--
(define (fixup-axes)
(set! *selected-data-color* (make-color 0 0 0))(set! *selected-data-color* (make-color 0 0 0))
(set! *selected-graph-color* (make-color 1 1 1))
(set! *axis-numbers-font* *tiny-font*)
(set! (x-axis-label 0 0) "(env-any e (lambda (y) (* y y)))")
(set! (x-axis-label 0 1) "(sine-env)"))
-->
<p>The env-any connecting function takes one argument, the current envelope value treated as
going between 0.0 and 1.0 between each two points. It returns a value that is then
fitted back into the original (scaled, offset) envelope. There are a couple more of these
functions in generators.scm, one to apply a blackman4 window between the points, and the
other to cycle through a set of exponents.
</p>
<p>
<a href="#mus-reset">mus-reset</a> of an env causes it
to start all over again from the beginning.
mus-reset is called internally if you use mus-scaler to set an env's scaler (and similarly for offset and length).
To jump to any position in
an env, use <a href="#mus-location">mus-location</a>.
Here's a function that uses these methods to apply an envelope over and over:
</p>
<pre class="indented">
(define (strum e)
(<a class=quiet href="extsnd.html#mapchannel">map-channel</a> (lambda (y)
(if (> (<em class=red>mus-location</em> e) (<em class=red>mus-length</em> e)) ; mus-length = dur
(<em class=red>mus-reset</em> e)) ; start env again (default is to stick at the last value)
(* y (<a class=quiet href="#env">env</a> e)))))
;;; (strum (make-env (list 0 0 1 1 10 .6 25 .3 100 0) :length 2000))
</pre>
<p>To copy an env while changing one aspect (say
duration), it's simplest to use make-env:
</p>
<pre class="indented">
(define (change-env-dur e dur)
(<a class=quiet href="#make-env">make-env</a> (<a class=quiet href="#mus-data">mus-data</a> e) :scaler (<a class=quiet href="#mus-scaler">mus-scaler</a> e) :offset (<a class=quiet href="#mus-offset">mus-offset</a> e) :base (<a class=quiet href="#mus-increment">mus-increment</a> e)
:duration dur))
</pre>
<p>make-env signals an error if the envelope breakpoints are either out of order, or an x axis value
occurs twice. The default error handler in with-sound may not give you the information you need to
track down the offending note, even given the original envelope. Here's one way to trap the error
and get more info (in this case, the begin time and duration of the enclosing note):
</p>
<pre class="indented">
(define* (make-env-with-catch beg dur :rest args)
(catch 'mus-error
(lambda ()
(apply <em class=red>make-env</em> args))
(lambda args
(<a class=quiet>format</a> #t ";~A ~A: ~A~%" beg dur args))))
</pre>
<p>(envelope-interp x env base) returns value of 'env' at 'x'.
If 'base' is 0, 'env' is treated as a step function; if 'base' is 1.0 (the
default), the breakpoints of 'env' are connected by a straight line, and
any other 'base' connects the breakpoints with a kind of exponential
curve:
</p>
<pre class="indented">
> (envelope-interp .1 '(0 0 1 1))
0.1
> (envelope-interp .1 '(0 0 1 1) 32.0)
0.0133617278184869
> (envelope-interp .1 '(0 0 1 1) .012)
0.361774730775292
</pre>
<p>The corresponding function for a CLM env generator is <a href="sndclm.html#env-interp">env-interp</a>.
If you'd rather think in terms of e^-kt, set the 'base' to (exp k).
</p>
<div class="spacer"></div>
<p>
pulsed-env produces a repeating envelope. <a href="#env">env</a> sticks at its last value, but pulsed-env repeats it over and over.
"duration" is the envelope duration, and "frequency" is the repeitition rate, changeable via the "fm" argument to the pulsed-env generator.
</p>
<pre class="indented">
(with-sound ()
(let ((e (make-pulsed-env '(0 0 1 1 2 0) .01 1))
(frq (make-env '(0 0 1 1) :duration 1.0 :scaler (hz->radians 50))))
(do ((i 0 (+ i 1)))
((= i 44100))
(outa i (* .5 (pulsed-env e (env frq)))))))
</pre>
<p>
An envelope applied to the amplitude of a signal is a form of amplitude modulation,
and glissando is frequency modulation. Both cause a broadening of the spectral components:
</p>
<table><tr>
<td>
<img src="pix/ampenvspectrum.png" alt="amp env spectrum">
</td><td>
<img src="pix/frqenvspectrum.png" alt="frq env spectrum">
</td></tr>
<tr>
<td class="center"><small>truncated pyramid amplitude envelope<br>multiplied by sinusoid at 50Hz</small>
</td>
<td class="center"><small>truncated pyramid frquency envelope<br>sinusoid from 100Hz to 300Hz</small>
</td>
</tr></table>
<!--
ampenvspectrum.png:
(with-sound (:srate 10000)
(let* ((ampf (make-env '(0 0 1 1 15 1 16 0) :duration 15))
(osc (make-oscil 50))
(samps (seconds->samples 15))
(start (seconds->samples 7))
(end (+ start samps)))
(do ((i 0 (+ i 1)))
((= i end))
(outa (+ i start) (* (env ampf) (oscil osc))))))
;;; GL/ data cutoff .015 dark 78 jet invert blackman10 65536 db-100
;;; x 299 1.84 y 281 .082 z 342 1.25
;;; hop 3
(set! *spectrum-end* .02)
(set! *selected-graph-color* (make-color 1 1 1))
frqenvspectrum.png:
(with-sound (:srate 10000)
(let* ((ampf (make-env '(0 0 10 0 11 1 25 1 26 0 40 0) :duration 40 :scaler (hz->radians 200)))
(osc (make-oscil 100))
(samps (seconds->samples 40))
(start (seconds->samples 0))
(end (+ start samps)))
(do ((i 0 (+ i 1)))
((= i end))
(outa (+ i start) (oscil osc (env ampf))))))
65536 GL blackman10 -100 dB
x 299 1.84 y 281 0.82 z 10 1.25
hop 3 % 0.11
jet .015 78 invert
-->
<p>The amplitude case reflects the spectrum of the amplitude envelope all by itself, translated (by multiplication)
up to the sinusoid's pitch. The sidebands are about 1 Hz apart (the envelope takes 1 second to go linearly from 0 to 1).
Despite appearances, we hear this (are you sitting down?) as a changing amplitude, not a timbral mess.
Spectra can be tricky to interpret, and I've tried to choose parameters for this display that emphasize
the broadening.
</p>
<br>
<table class="method">
<tr><td class="methodtitle">Envelopes</td></tr>
<tr><td>
<p>Various operations on envelopes:
</p>
<pre class="indented">
<a href="sndscm.html#envdoc">env.scm</a>:
add-envelopes add two envelopes
concatenate-envelopes concatenate a bunch of envelopes
envelope-exp interpolate points to approximate exponential curves
envelope-interp return the value of an envelope given the x position
envelope-last-x return the last x value in an envelope
intergrate-envelope return the area under an envelope
make-power-env exponential curves with multiple exponents (see also multi-expt-env in generators.scm)
map-envelopes apply a function to the breakpoints in two envelopes, returning a new envelope
max-envelope return the maximum y value in an envelope (also min-envelope)
multiply-envelopes multiply two envelopes
normalize-envelope scale the y values of an envelope to peak at 1.0
repeat-envelope concatenate copies of an envelope
reverse-envelope reverse the breakpoints in an envelope
scale-envelope scale and offset the y values of an envelope
stretch-envelope apply attack and decay times to an envelope ("adsr", or "divenv")
window-envelope return the portion of an envelope within given x axis bounds
</pre>
<pre>
envelope sound: <a href="extsnd.html#envchannel">env-channel</a>, <a href="extsnd.html#envsound">env-sound</a>
other enveloping functions: <a href="extsnd.html#rampchannel">ramp-channel</a>, <a href="extsnd.html#xrampchannel">xramp-channel</a>, <a href="extsnd.html#smoothchannel">smooth-channel</a>
envelope editor: <a href="snd.html#editenvelope">Edit or View and Envelope</a>
panning: place-sound in examp.scm
read sound indexed through envelope: <a href="sndscm.html#envsoundinterp">env-sound-interp</a>
repeating envelope: <a href="#pulsedenv">pulsed-env</a>
step envelope in pitch: <a href="#rxyk!cos">brassy</a> in generators.scm
</pre>
</td></tr></table>
<!-- TABLE-LOOKUP -->
<div class="innerheader" id="table-lookupdoc">table-lookup</div>
<pre class="indented">
<em class=def id="make-table-lookup">make-table-lookup</em>
(frequency 0.0) ; table repetition rate in Hz
(initial-phase 0.0) ; starting point in radians (pi = mid-table)
wave ; a float-vector containing the signal
(size *clm-table-size*) ; table size if wave not specified
(type mus-interp-linear) ; interpolation type
<em class=def id="table-lookup">table-lookup</em> tl (fm-input 0.0)
<em class=def id="table-lookup?">table-lookup?</em> tl
<em class=def id="make-table-lookup-with-env">make-table-lookup-with-env</em> frequency env size
</pre>
<table class="method">
<tr><td colspan=2 class="methodtitle">table-lookup methods</td></tr>
<tr><td class="inner"><em class=gen>mus-frequency</em></td><td class="inner">frequency in Hz</td></tr>
<tr><td class="inner"><em class=gen>mus-phase</em></td><td class="inner">phase in radians</td></tr>
<tr><td class="inner"><em class=gen>mus-data</em></td><td class="inner">wave float-vector</td></tr>
<tr><td class="inner"><em class=gen>mus-length</em></td><td class="inner">wave size (no set!)</td></tr>
<tr><td class="inner"><em class=gen>mus-interp-type</em></td><td class="inner">interpolation choice (no set!)</td></tr>
<tr><td class="inner"><em class=gen>mus-increment</em></td><td class="inner">table increment per sample</td></tr>
</table>
<p>table-lookup performs interpolating table lookup with a lookup index that moves
through the table at a speed set by make-table-lookup's "frequency" argument and table-lookup's "fm-input" argument.
That is, the waveform in the table is produced repeatedly, the repetition rate set by the frequency arguments.
Table-lookup scales its
fm-input argument to make its table size appear to be two pi.
The intention here is that table-lookup with a sinusoid in the table and a given FM signal
produces the same output as oscil with that FM signal.
The "type" argument sets the type of interpolation used: <code>mus-interp-none</code>,
<code>mus-interp-linear</code>, <code>mus-interp-lagrange</code>, or <code>mus-interp-hermite</code>.
make-table-lookup-with-env (defined in generators.scm) returns a new table-lookup generator with the envelope 'env' loaded into its table.
table-lookup might be defined:
</p>
<pre class="indented">
(let ((result (<a class=quiet href="#array-interp">array-interp</a> wave phase)))
(set! phase (+ phase
(<a class=quiet href="#hztoradians">hz->radians</a> frequency)
(* fm-input
(/ (length wave)
2 pi))))
result)
</pre>
<table>
<tr><td>
<div class="scheme">
<pre class="indented">
(with-sound (:play #t)
(let ((gen (make-table-lookup 440.0 :wave (partials->wave '(1 .5 2 .5)))))
(do ((i 0 (+ i 1)))
((= i 44100))
(outa i (* 0.5 (table-lookup gen))))))
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="ruby">
<pre class="indented">
with_sound(:play, true) do
gen = make_table_lookup(440.0, :wave, partials2wave([1.0, 0.5, 2.0, 0.5]));
44100.times do |i|
outa(i, 0.5 * table_lookup(gen), $output)
end
end.output
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="forth">
<pre class="indented">
lambda: ( -- )
440.0 :wave '( 1 0.5 2 0.5 ) #f #f partials->wave make-table-lookup { gen }
44100 0 do
i gen 0 table-lookup f2/ *output* outa drop
loop
; :play #t with-sound drop
</pre>
</div>
</td>
</tr>
</table>
<p>
In the past, table-lookup was often used for additive synthesis, so
there are two functions that make it easier to load up
various such waveforms:
</p>
<pre class="indented">
<em class=def id="partialstowave">partials->wave</em> synth-data wave (norm #t)
<em class=def id="phase-partialstowave">phase-partials->wave</em> synth-data wave (norm #t)
</pre>
<p>The "synth-data" argument is a list or float-vector of (partial amp) pairs: '(1 .5 2 .25)
gives a combination of a sine wave at the carrier (partial = 1) at amplitude .5, and
another at the first harmonic (partial = 2) at amplitude .25. The partial amplitudes are
normalized to sum to a total amplitude of 1.0 unless the argument "norm"
is #f. If the initial phases matter (they almost never do), you can use
phase-partials->wave; in this case the synth-data is a list or float-vector of (partial amp phase) triples with phases in radians.
If "wave" is not passed, these functions return a new float-vector.
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#definstrument">definstrument</a> (simple-table dur)
(let ((tab (<em class=red>make-table-lookup</em> :wave (<em class=red>partials->wave</em> '(1 .5 2 .5)))))
(do ((i 0 (+ i 1))) ((= i dur))
(<a class=quiet href="#outa">outa</a> i (* .3 (<em class=red>table-lookup</em> tab))))))
</pre>
<p>table-lookup can also be used as a sort of "freeze" function, looping through a sound repeatedly,
based on some previously chosen loop positions:
</p>
<pre class="indented">
(define (looper start dur sound freq amp)
(let* ((beg (<a class=quiet href="#secondstosamples">seconds->samples</a> start))
(end (+ beg (<a class=quiet href="#secondstosamples">seconds->samples</a> dur)))
(loop-data (<a class=quiet href="extsnd.html#mussoundloopinfo">mus-sound-loop-info</a> sound)))
(if (or (null? loop-data)
(<= (cadr loop-data) (car loop-data)))
(error 'no-loop-positions)
(let* ((loop-start (car loop-data))
(loop-length (- (+ (cadr loop-data) 1) loop-start))
(sound-section (<a class=quiet href="#filetoarray">file->array</a> sound 0 loop-start loop-length (make-float-vector loop-length)))
(original-loop-duration (/ loop-length (srate sound)))
(tbl (<em class=red>make-table-lookup</em> :frequency (/ freq original-loop-duration) :wave sound-section)))
;; "freq" here is how fast we read (transpose) the sound — 1.0 returns the original
(do ((i beg (+ i 1)))
((= i end))
(<a class=quiet href="#outa">outa</a> i (* amp (<em class=red>table-lookup</em> tbl))))))))
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:srate 44100) (looper 0 10 "/home/bil/sf1/forest.aiff" 1.0 0.5))
</pre>
<p>And for total confusion, here's a table-lookup that modulates a sound where we specify the
modulation deviation in samples:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#definstrument">definstrument</a> (fm-table file start dur amp read-speed modulator-freq index-in-samples)
(let* ((beg (<a class=quiet href="#secondstosamples">seconds->samples</a> start))
(end (+ beg (<a class=quiet href="#secondstosamples">seconds->samples</a> dur)))
(table-length (<a class=quiet href="extsnd.html#mussoundframples">mus-sound-framples</a> file))
(tab (<em class=red>make-table-lookup</em> :frequency (/ read-speed (<a class=quiet href="extsnd.html#mussoundduration">mus-sound-duration</a> file))
:wave (<a class=quiet href="#filetoarray">file->array</a> file 0 0 table-length (make-float-vector table-length))))
(osc (<a class=quiet href="#make-oscil">make-oscil</a> modulator-freq))
(index (/ (* (<a class=quiet href="#hztoradians">hz->radians</a> modulator-freq) 2 pi index-in-samples) table-length)))
(do ((i beg (+ i 1)))
((= i end))
(<a class=quiet href="#outa">outa</a> i (* amp (<em class=red>table-lookup</em> tab (* index (<a class=quiet href="#oscil">oscil</a> osc))))))))
</pre>
<p>Lessee.. there's a factor of table-length/(2*pi) in table-lookup, so that a table with a sinusoid
behaves the same as an oscil even with FM; hz->radians
adds a factor of (2*pi)/srate; so we've cancelled the internal 2*pi and table-length, and we have
an actual deviation of mfreq*2*pi*index/srate, which looks like FM; hmmm. See <a href="#srcer">srcer</a>
below for an <a href="#src">src</a>-based way to do the same thing.
</p>
<p>
There is one annoying problem with table-lookup: noise.
Say we have a sine wave in a table with L elements, and we want to read it at a frequency of
f Hz at a sampling rate of Fs. This requires that we read the table at locations that are multiples of
L * f / Fs. This is ordinarily not an integer (that is, we've fallen between the
table elements). We have no data between the elements, but we can make (plenty of)
assumptions about what ought to be there. In the no-interpolation case (type = <code>mus-interp-none</code>), we take the floor of
the table-relative phase, returning a squared-off sine-wave:
</p>
<img src="pix/interp1.png" alt="squared-off sine spectra">
<!--
(with-sound (:srate 1000000 :channels 2)
(let ((gen1 (make-table-lookup 100.0 :wave (partials->wave '(1 1) (make-float-vector 100)) :type mus-interp-none))
(sine (make-oscil 100.0)))
(do ((i 0 (+ i 1)))
((= i 1000000))
(let ((qsine (table-lookup gen1))
(tsine (oscil sine)))
(outa i qsine)
(outb i (- tsine qsine))))))
(set! *selected-graph-color* (make-color 1 1 1))
(set! *selected-data-color* (make-color 0 0 0))
(set! (x-axis-label 0 0 0) "no interpolation, table size=100, sine at 100 Hz")
(set! (x-axis-label 0 1 0) "difference between sine and squared-off sine")
(set! (x-axis-label 0 0 1) "squared-off sine spectrum: srate=1000000")
(set! (x-axis-label 0 1 1) "spectrum of the error")
(set! *axis-label-font* *axis-numbers-font*)
(set! *transform-normalization* normalize-by-sound)
(set! *graphs-horizontal* #f)
-->
<!--
maxamp of sawtooth is 2pi/L (it's a max at 0 when sin x ~ x, and each phase increment is 2pi/L)
-->
<p>
In addition to the sine at 100 Hz, we're getting lots of pairs of components, each pair centered around n * L * f, (10000 = 100 * 100 is the first),
and separated from it by f, (9900 and 10100),
and the amplitude of each pair is 1/(nL): -40 dB is 1/100 for the n=1 case.
This spectrum says "amplitude modulation" (the fast square wave times the slow sinusoid).
After scribbling a bit on the back of an envelope, we announce with a confident air that
the sawtooth error signal gives us the 1/n (it is a sum of sin nx/n), and its amplitude gives us the 1/L.
Now we try linear interpolation (<code>mus-interp-linear</code>), and get the same components as before, but
the amplitude is going (essentially) as 1.0 / (n * n * L * L). So the interpolation
reduces the original problem by a factor of n * L:
</p>
<img src="pix/interp2.png" alt="squared-off sine spectra">
<!--
(with-sound (:srate 1000000 :channels 2)
(let ((gen1 (make-table-lookup 100.0 :wave (partials->wave '(1 1) (make-float-vector 100)) :type mus-interp-linear))
(sine (make-oscil 100.0)))
(do ((i 0 (+ i 1)))
((= i 1000000))
(let ((qsine (table-lookup gen1))
(tsine (oscil sine)))
(outa i qsine)
(outb i (- tsine qsine))))))
(set! (x-axis-label 0 0 0) "linear interpolation, table size=100, sine at 100 Hz")
(set! (x-axis-label 0 1 0) "difference between sine and interpolated sine")
(set! (x-axis-label 0 0 1) "interpolated sine spectrum: srate=1000000")
(set! (x-axis-label 0 1 1) "spectrum of the error")
-->
<p>
We can view this also as amplitude modulation:
the sinusoid at frequency f times the little blip during each table sample at frequency L * f.
Each component
is at n * L * f, as before, and split in half by the modulation.
Since L * f is normally a very high frequency, and sampling rates are not in the megahertz range (as in our examples),
these components
alias to such an extent that they look like noise, but they are noise only in the
sense that we wish they weren't there.
</p>
<p>
The table length (L above) is the "effective" length. If we store an nth harmonic
in the table, each period gets L/n elements (we want to avoid clicks caused by discontinuities between the first and last table elements),
so the amplitude of the nth harmonic's noise components
is higher (by n^2) than the fundamental's. We either have to use enormous
tables or stick to low
numbered partials. To keep the noise components out of sight in 16-bit output (down 90 dB),
we need 180 elements per period. So a table with a 50th harmonic has to be at least length 8192.
It's odd that the cutoff here is so similar to
the waveshaping case; a 50-th harmonic is trouble in either case.
(This leaves an opening for <a href="#ncos">ncos</a> and friends even when dynamic spectra aren't the issue).
</p>
<p>
We can try fancier interpolations. <code>mus-interp-lagrange</code> and <code>mus-interp-hermite</code>
reduce the components (which are at the same frequencies as before) by about another factor of L.
But these interpolations are expensive and ugly.
If you're trying to produce a sum of sinusoids, use polywave — it makes a monkey out of table lookup in every case.
</p>
<!-- an earlier example showing the interpolation waveforms:
(with-sound (:channels 4 :clipped #f :statistics #t)
(let* ((pitch 1000.0)
(size 64)
(tbl1 (make-table-lookup pitch 0.0 (partials->wave '(1 1) (make-float-vector size)) size mus-interp-none))
(tbl2 (make-table-lookup pitch 0.0 (partials->wave '(1 1) (make-float-vector size)) size mus-interp-linear))
(tbl3 (make-table-lookup pitch 0.0 (partials->wave '(1 1) (make-float-vector size)) size mus-interp-lagrange))
(tbl4 (make-table-lookup pitch 0.0 (partials->wave '(1 1) (make-float-vector size)) size mus-interp-hermite)))
(do ((i 0 (+ i 1)))
((= i 100000))
(outa i (table-lookup tbl1))
(outb i (table-lookup tbl2))
(out-any i (table-lookup tbl3) 2)
(out-any i (table-lookup tbl4) 3))))
(with-sound (:channels 5 :clipped #f :statistics #t)
(let* ((pitch 2.0)
(size 64)
(wave (let ((v (make-float-vector size)))
(set! (v (/ size 2)) 1.0)
v))
(tbl1 (make-table-lookup pitch 0.0 wave size mus-interp-none))
(tbl2 (make-table-lookup pitch 0.0 wave size mus-interp-linear))
(tbl3 (make-table-lookup pitch 0.0 wave size mus-interp-lagrange))
(tbl4 (make-table-lookup pitch 0.0 wave size mus-interp-hermite))
(tbl5 (make-table-lookup pitch 0.0 wave size mus-interp-all-pass)))
(do ((i 0 (+ i 1)))
((= i 100000))
(outa i (table-lookup tbl1))
(outb i (table-lookup tbl2))
(out-any i (table-lookup tbl3) 2)
(out-any i (table-lookup tbl4) 3)
(out-any i (table-lookup tbl5) 4))))
-->
<div class="inset">
<p>table-lookup of a sine (or some facsimile thereof) probably predates Ptolemy.
One neat method of generating the table is that of Bhaskara I, AD 600, India, mentioned
in van Brummelen, "The Mathematics of the Heavens and the Earth": use the rational
approximation 4x(180-x)/(40500-x(180-x)), x in degrees, or more readably:
4x(pi-x)/(12.337-x(pi-x)), x in radians. The maximum error is 0.00163 at x=11.54 (degrees)!
</p>
</div>
<!--
(define (bhaskara-sine x) ; x degrees
(list (/ (* 4.0 x (- 180 x))
(- 40500 (* x (- 180 x))))
(sin (degrees->radians x))
(let* ((dx (degrees->radians x))
(px (- pi dx))
(cs (degrees->radians (degrees->radians 40500))))
(/ (* 4 dx px)
(- cs (* dx px))))
(let ((dx (degrees->radians x)))
(/ (* 4 dx (- pi dx))
(- 12.337 (* dx (- pi dx)))))))
Ayyangar: Ganesh; same thing using n=pi/x: 16(n-1)/(5n^2-4n+4)
(define (ganesh-sine x) ; x radians
(list (if (= x 0.0)
0.0
(let ((n (/ pi x)))
(/ (* 16 (- n 1))
(+ (* 5 n n) (* -4 n) 4))))
(if (= x 0.0)
0.0
(let* ((n (/ pi x))
(n2 (* n n))
(n-2 (- n 2))
(n2-2 (* n-2 n-2)))
(/ (- n2 n2-2)
(+ n2 (/ n2-2 4)))))
(sin x)))
(define (pade-sine x) ; x < pi/4, Martin Brown
(list (* x (/ (- 60 (* 7 x x))
(+ 60 (* 3 x x))))
(sin x)))
(define (koren-sine ux) ; |ux| < pi/4, Kren and Zinaty
(let* ((x (/ (* 4 ux) pi))
(x2 (* x x))
(a0 1805490264.6910)
(a1 -164384678.2275)
(a2 3664210.6476)
(a3 -28904.1402)
(a4 76.5690)
(b0 2298821602.6389)
(b1 27037050.1189)
(b2 155791.3885)
(b3 540.5675))
(* x (/ (+ a0 (* x2 (+ a1 (* x2 (+ a2 (* x2 (+ a3 (* x2 a4))))))))
(+ b0 (* x2 (+ b1 (* x2 (+ b2 (* x2 (+ b3 x2)))))))))))
-->
<p><a href="spectr.scm">spectr.scm</a> has a steady state spectra of
several standard orchestral instruments, courtesy of James A. Moorer.
The <a href="sndscm.html#drone">drone</a> instrument in clm-ins.scm uses table-lookup for the
bagpipe drone. <a href="sndscm.html#twotab">two-tab</a> in the same file interpolates between two tables.
See also <a href="sndscm.html#granidoc">grani</a>.
</p>
<!-- POLYWAVE, POLYSHAPE -->
<div class="innerheader" id="polywavedoc">polywave, polyshape</div>
<pre class="indented">
<em class=def id="make-polywave">make-polywave</em>
(frequency 0.0)
(partials '(1 1)) ; a list of harmonic numbers and their associated amplitudes
(type mus-chebyshev-first-kind) ; Chebyshev polynomial choice
xcoeffs ycoeffs ; tn/un for tu-sum case
<em class=def id="polywave">polywave</em> w (fm 0.0)
<em class=def id="polywave?">polywave?</em> w
<em class=def id="make-polyshape">make-polyshape</em>
(frequency 0.0)
(initial-phase 0.0)
coeffs
(partials '(1 1))
(kind mus-chebyshev-first-kind)
<em class=def id="polyshape">polyshape</em> w (index 1.0) (fm 0.0)
<em class=def id="polyshape?">polyshape?</em> w
<em class=def id="partialstopolynomial">partials->polynomial</em> partials (kind mus-chebyshev-first-kind)
<em class=def id="normalizepartials">normalize-partials</em> partials
<em class=def id="mus-chebyshev-tu-sum">mus-chebyshev-tu-sum</em> x t-coeffs u-coeffs
<em class=emdef>mus-chebyshev-t-sum</em> x t-coeffs
<em class=emdef>mus-chebyshev-u-sum</em> x u-coeffs
</pre>
<table class="method">
<tr><td colspan=2 class="methodtitle">polywave methods</td></tr>
<tr><td class="inner"><em class=gen>mus-frequency</em></td><td class="inner">frequency in Hz</td></tr>
<tr><td class="inner"><em class=gen>mus-scaler</em></td><td class="inner">index</td></tr>
<tr><td class="inner"><em class=gen>mus-phase</em></td><td class="inner">phase in radians</td></tr>
<tr><td class="inner"><em class=gen>mus-data</em></td><td class="inner">polynomial coeffs</td></tr>
<tr><td class="inner"><em class=gen>mus-length</em></td><td class="inner">number of partials</td></tr>
<tr><td class="inner"><em class=gen>mus-increment</em></td><td class="inner">frequency in radians per sample</td></tr>
</table>
<p>
These two generators
drive a sum of scaled Chebyshev polynomials with
a cosine, creating a sort of cross between additive synthesis and FM; see
"Digital Waveshaping Synthesis" by Marc Le Brun in JAES 1979 April, vol 27, no 4, p250.
The basic idea is:
</p>
<img class="indented" src="pix/sceq16.png" alt="Cheby eqs">
<p>
We can add scaled Tns (polynomials) to get the spectrum we want, producing
in the simplest case an inexpensive additive synthesis. We can vary the peak amplitude of the
input (cos theta) to get effects similar to those of FM.
polyshape uses a prebuilt sum of Chebyshev polynomials,
whereas polywave uses the underlying Chebyshev recursion.
polywave is stable and noise-free even with high partial numbers (I've tried it with 16384 harmonics).
The "partials" argument to the make function can
be either a list or a float-vector ("vct" in Ruby and Forth).
The "type" or "kind" argument determines which kind of Chebyshev polynomial
is used internally: mus-chebyshev-first-kind (Tn) which produces a sum of cosines,
or mus-chebyshev-second-kind (Un), which produces a sum of sines.
</p>
<table>
<tr>
<td>
<div class="scheme">
<pre class="indented">
(with-sound (:play #t)
(let ((gen (make-polywave 440.0 :partials '(1 .5 2 .5))))
(do ((i 0 (+ i 1)))
((= i 44100))
(outa i (* 0.5 (polywave gen))))))
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="ruby">
<pre class="indented">
with_sound(:play, true) do
gen = make_polywave(440.0, :partials, [1.0, 0.5, 2.0, 0.5]);
44100.times do |i|
outa(i, 0.5 * polywave(gen), $output)
end
end.output
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="forth">
<pre class="indented">
lambda: ( -- )
440.0 :partials '( 1 0.5 2 0.5 ) make-polywave { gen }
44100 0 do
i gen 0 polywave f2/ *output* outa drop
loop
; :play #t with-sound drop
</pre>
</div>
</td>
</tr>
</table>
<!-- LATEX: sceq16
\begin{align*}
&T_{n}(\cos \theta)=\cos n\theta \\
&U_{n}(\cos \theta)=\frac{\sin(n+1) \theta}{\sin \theta}
\end{align*}
A&S 22.3.15
-->
<p>normalize-partials takes the list or float-vector of partial number and amplitudes, and
returns a float-vector with the amplitudes normalized so that their magnitudes add to 1.0.
</p>
<pre class="indented">
> (normalize-partials '(1 1 3 2 6 1))
#(1.0 0.25 3.0 0.5 6.0 0.25);
> (normalize-partials (float-vector 1 .1 2 .1 3 -.2))
#(1.0 0.25 2.0 0.25 3.0 -0.5)
</pre>
<p>
partials->polynomial
takes a list or float-vector of partial numbers and amplitudes
and returns the Chebyshev polynomial coefficients that
produce that spectrum. These coefficients can be passed to
polyshape (the coeffs argument), or used directly by <a href="#polynomial">polynomial</a> (there are examples of both below).
</p>
<pre class="indented">
> (partials->polynomial '(1 1 3 2 6 1))
#(-1.0 -5.0 18.0 8.0 -48.0 0.0 32.0)
> (partials->polynomial '(1 1 3 2 6 1) mus-chebyshev-second-kind)
#(-1.0 6.0 8.0 -32.0 0.0 32.0 0.0)
> (partials->polynomial (float-vector 1 .1 2 .1 3 -.2))
#(-0.1 0.7 0.2 -0.8)
</pre>
<p>mus-chebyshev-tu-sum and friends perform the same function as partials->polynomial, but
use the much more stable and accurate underlying recursion (see below for a long-winded
explanation). They are the innards of the polywave and <a href="#polyoid">polyoid</a> generators.
The arguments are "x" (normally a phase), and one or two
float-vectors of component amplitudes.
These functions makes it easy to do additive synthesis
with any number of harmonics (I've tried 16384), each with arbitrary
initial-phase and amplitude, and each harmonic independently changeable
in phase and amplitude at run-time by setting a float-vector value.
</p>
<pre class="indented">
(let ((result (<a class=quiet href="#polynomial">polynomial</a> wave (cos phase))))
(set! phase (+ phase (<a class=quiet href="#hztoradians">hz->radians</a> frequency) fm))
result)
</pre>
<p>In its simplest use, waveshaping is additive synthesis:
</p>
<table>
<tr><td>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> ()
(let ((wav (<em class=red>make-polyshape</em>
:frequency 500.0
:partials '(1 .5 2 .3 3 .2))))
(do ((i 0 (+ i 1))) ((= i 40000))
(<a class=quiet href="#outa">outa</a> i (<em class=red>polyshape</em> wav)))))
</pre>
</td>
<td>
<img src="pix/polyshape.png" alt="waveshaping">
</td>
</tr></table>
<p>Say we want every third harmonic at amplitude 1/sqrt(harmonic-number) for 5 harmonics total:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:clipped #f :statistics #t :play #t :scaled-to .5)
(let ((gen (<em class=red>make-polywave</em> 200
(do ((harms (make-float-vector (* 5 2))) ; 5 harmonics, 2 numbers for each
(k 1 (+ k 3))
(i 0 (+ i 2)))
((= i 10) harms)
(set! (harms i) k) ; harmonic number (k*freq)
(set! (harms (+ i 1)) (/ 1.0 (sqrt k)))))) ; harmonic amplitude
(ampf (<a class=quiet href="#make-env">make-env</a> '(0 0 1 1 10 1 11 0) :duration 1.0 :scaler .5)))
(do ((i 0 (+ i 1)))
((= i 44100))
(<a class=quiet href="#outa">outa</a> i (* (<a class=quiet href="#env">env</a> ampf) (<em class=red>polywave</em> gen))))))
</pre>
<p>See animals.scm for many more examples along these lines.
normalize-partials makes sure that the component amplitudes (magnitudes) add to 1.0. Its argument can be either a list or float-vector,
but it always returns a float-vector.
The <a href="sndscm.html#fmviolin">fm-violin</a> uses polyshape for the multiple FM section in some cases.
The <a href="sndscm.html#pqw">pqw</a> and <a href="sndscm.html#pqwvox">pqwvox</a> instruments use
both kinds of Chebyshev polynomials to produce single side-band spectra.
Here is a somewhat low-level example:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#definstrument">definstrument</a> (pqw start dur spacing carrier partials)
(let* ((spacing-cos (<a class=quiet href="#make-oscil">make-oscil</a> spacing (/ pi 2.0)))
(spacing-sin (<a class=quiet href="#make-oscil">make-oscil</a> spacing))
(carrier-cos (<a class=quiet href="#make-oscil">make-oscil</a> carrier (/ pi 2.0)))
(carrier-sin (<a class=quiet href="#make-oscil">make-oscil</a> carrier))
(sin-coeffs (<em class=red>partials->polynomial</em>
partials mus-chebyshev-second-kind))
(cos-coeffs (<em class=red>partials->polynomial</em>
partials mus-chebyshev-first-kind))
(beg (<a class=quiet href="#secondstosamples">seconds->samples</a> start))
(end (+ beg (<a class=quiet href="#secondstosamples">seconds->samples</a> dur))))
(do ((i beg (+ i 1)))
((= i end))
(let ((ax (<a class=quiet href="#oscil">oscil</a> spacing-cos)))
(<a class=quiet href="#outa">outa</a> i (- (* (<a class=quiet href="#oscil">oscil</a> carrier-sin)
(<a class=quiet href="#oscil">oscil</a> spacing-sin)
(<em class=red>polynomial</em> sin-coeffs ax))
(* (<a class=quiet href="#oscil">oscil</a> carrier-cos)
(<em class=red>polynomial</em> cos-coeffs ax))))))))
</pre>
<table class="pb">
<tr><td>
<img src="pix/pqw.png" alt="pqw example">
</td></tr>
<tr><td class="br">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> () (pqw 0 1 200.0 1000.0 '(2 .2 3 .3 6 .5)))
</td></tr>
</table>
<p>We can use waveshaping to make a band-limited triangle-wave:
</p>
<pre class="indented">
(define* (make-band-limited-triangle-wave (frequency 0.0) (order 1))
(do ((freqs ())
(i 1 (+ i 1))
(j 1 (+ j 2)))
((> i order)
(<em class=red>make-polywave</em> frequency :partials (reverse freqs)))
(set! freqs (cons (/ 1.0 j j) (cons j freqs)))))
(define* (band-limited-triangle-wave gen (fm 0.0))
(<em class=red>polywave</em> gen fm))
</pre>
<p>Band-limited square or sawtooth waves:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#definstrument">definstrument</a> (bl-saw start dur frequency order)
(let ((norm (cond ((assoc order '((1 . 1.0) (2 . 1.3)) =) => cdr) ; these peak amps were determined empirically
((< order 9) 1.7) ; actual limit is supposed to be pi/2 (G&R 1.441)
(else 1.852))) ; but Gibbs phenomenon pushes it to 1.851
(freqs ()))
(do ((i 1 (+ i 1)))
((> i order))
(set! freqs (cons (/ 1.0 norm i) (cons i freqs))))
(let* ((gen (<em class=red>make-polywave</em> frequency :partials (reverse freqs) :type <em class=red>mus-chebyshev-second-kind</em>))
(beg (<a class=quiet href="#secondstosamples">seconds->samples</a> start))
(end (+ beg (<a class=quiet href="#secondstosamples">seconds->samples</a> dur))))
(do ((i beg (+ i 1)))
((= i end))
(<a class=quiet href="#outa">outa</a> i (<em class=red>polywave</em> gen))))))
</pre>
<p>The "fm" argument to these generators is intended mainly for vibrato and frequency envelopes.
If you use it for frequency modulation, you'll notice that the result is not the necessarily same as applying that
modulation to the equivalent bank of oscillators, but it is the same as (for example) applying it to an ncos
generator, or most of the other generators (table-lookup, nsin, etc). The polynomial in cos(x) produces
a sum of cos(nx) for various "n", but if "x" is itself a sinusoid, its
effective index includes the factor of "n" (the partial number).
This is what you want
if all the components should move together (as in vibrato). If you need better control of the FM spectrum,
use a bank of oscils where you can set each index independently. Here we used '(1 1 2 1 3 1) and polyshape
with sinusoidal FM with an index of 1.
</p>
<img class="indented" src="pix/polyfm.png" alt="polyshape fm">
<p>The same thing happens if you use polyshape or ncos (or whatever) as the (complex) modulating signal to an oscil
(the reverse of the situation above).
The effective index of each partial is divided by the partial number (and in ncos, for example, the output
is scaled to be -1..1, so that adds another layer of confusion). There's a longer discussion of this under
<a href="#ncosdoc">ncos</a>.
</p>
<!-- actually you get either cos(nx) or +/-cos(nx) depending on which algorithm is actually chosen
-->
<!--
(define* (fmsin beg dur freq amp mc-ratio index type)
(let* ((start (seconds->samples beg))
(end (+ start (seconds->samples dur)))
(carrier (if (= type 0)
(let ((oscs (make-vector 3 #f)))
(do ((i 0 (+ i 1)))
((= i 3))
(set! (oscs i) (make-oscil (* freq (+ i 1)))))
oscs)
(if (= type 1)
(make-ncos freq 3)
(if (= type 2)
(make-polyshape freq :coeffs (partials->polynomial '(1 1 2 1 3 1)))
(if (= type 3)
(make-table-lookup freq :wave (partials->wave '(1 1 2 1 3 1)))
(if (= type 4)
(make-waveshape freq :partials '(1 1 2 1 3 1))
(if (= type 5)
(make-nsin freq 3)
(if (= type 6)
(make-ncos2 freq 3)
(if (= type 7)
(make-ncos4 freq 3)
(if (= type 8)
(make-nrcos freq 3 .99)
(if (= type 9)
(make-nkssb freq freq 3)
)))))))))))
(modulator (make-oscil (* freq mc-ratio)))
(fm-index (hz->radians (* index freq mc-ratio)))
(ampf (make-env '(0 0 1 1 10 1 11 0) :scaler amp :duration dur)))
(do ((i start (+ i 1)))
((= i end))
(let ((md (* fm-index (oscil modulator))))
(outa i (* (env ampf)
(if (= type 0)
(let ((sum 0.0))
(do ((k 0 (+ k 1)))
((= k 3))
(set! sum (+ sum (oscil (carrier k) (* (+ k 1) md))))) ; or leave unscaled
(/ sum 3))
(if (= type 1)
(sum-of-cosines carrier md)
(if (= type 2)
(/ (polyshape carrier 1.0 md) 3)
(if (= type 3)
(/ (table-lookup carrier md) 3)
(if (= type 4)
(waveshape carrier 1.0 md)
(if (= type 5)
(sum-of-sines carrier md)
(if (= type 6)
(ncos2 carrier md)
(if (= type 7)
(ncos4 carrier md)
(if (= type 8)
(nrcos carrier md)
(if (= type 9)
(nkssb carrier md)
0.0))))))))))))))))
-->
<p>To get the FM effect of a spectrum centered around a carrier, multiply the waveshaping output by the carrier (the 0Hz term gives us the carrier):
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> ()
(let ((modulator (<em class=red>make-polyshape</em> 100.0 :partials (list 0 .4 1 .4 2 .1 3 .05 4 .05)))
(carrier (<a class=quiet href="#make-oscil">make-oscil</a> 1000.0)))
(do ((i 0 (+ i 1))) ((= i 20000))
(<a class=quiet href="#outa">outa</a> i (* .5 (<a class=quiet href="#oscil">oscil</a> carrier) (<em class=red>polyshape</em> modulator))))))
</pre>
<p>The simplest way to get
changing spectra is to interpolate between two or more sets of coefficients.
</p>
<pre class="indented">
(+ (* interp (polywave p1 ...)) ; see animals.scm for many examples
(* (- 1.0 interp) (polywave p2 ...)))
</pre>
<p>Or use mus-chebyshev-*-sum and set the component amplitudes directly:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> ()
(let* ((dur 1.0)
(samps (<a class=quiet href="#secondstosamples">seconds->samples</a> dur))
(coeffs (float-vector 0.0 0.5 0.25 0.125 0.125))
(x 0.0)
(incr (<a class=quiet href="#hztoradians">hz->radians</a> 100.0))
(ampf (<a class=quiet href="#make-env">make-env</a> '(0 0 1 1 10 1 11 0) :duration dur :scaler .5))
(harmf (<a class=quiet href="#make-env">make-env</a> '(0 .125 1 .25) :duration dur)))
(do ((i 0 (+ i 1)))
((= i samps))
(let ((harm (<a class=quiet href="#env">env</a> harmf)))
(set! (coeffs 3) harm)
(set! (coeffs 4) (- .25 harm)))
(<a class=quiet href="#outa">outa</a> i (* (<a class=quiet href="#env">env</a> ampf)
(<em class=red>mus-chebyshev-t-sum</em> x coeffs)))
(set! x (+ x incr)))))
</pre>
<p>
But we can also vary
the index (the amplitude of the cosine driving the sum of polynomials), much as in FM.
The kth partial's amplitude
at a given index, given a set h[k] of coefficients, is:
</p>
<img class="indented" src="pix/sceq43.png" alt="cheby hka calc">
<!-- LATEX:
&h_{k}(a) = \sum_{j=0}^{\infty} \binom{p}{j} a^{p} \sum_{i=0}^{\infty} (-1)^{i}\Big(\tbinom{p+i}{i} + \tbinom{p+i-1}{i-1}\Big) h_{p+2i}(1) & p=k+2j \\
-->
<p>(This formula is implemented by <a href="sndscm.html#chebyhka">cheby-hka</a> in dsp.scm).
The function traced out by the harmonic (analogous to the role the Bessel function Jn plays in FM)
is a polynomial in the index whose order depends on the number of coefficients. When the index is less than 1.0,
energy appears in lower harmonics even if they are not included in the index=1.0 list:
</p>
<pre class="indented">
> (cheby-hka 3 0.25 (float-vector 0 0 0 0 1.0 1.0))
-0.0732421875
> (cheby-hka 2 0.25 (float-vector 0 0 0 0 1.0 1.0))
-0.234375
> (cheby-hka 1 0.25 (float-vector 0 0 0 0 1.0 1.0))
1.025390625
> (cheby-hka 0 0.25 (float-vector 0 0 0 0 1.0 1.0))
1.5234375
</pre>
<p>
Below we sweep the index from 0.0
to 1.0 (sticking at 1.0 for a moment at the end), with a partials list of '(11 1.0 20 1.0). These numbers were chosen to show that the even and
odd harmonics are independent:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> ()
(let ((gen (<em class=red>make-polyshape</em> 100.0 :partials (list 11 1 20 1)))
(ampf (<a class=quiet href="#make-env">make-env</a> '(0 0 1 1 20 1 21 0) :scaler .4 :length 88200))
(indf (<a class=quiet href="#make-env">make-env</a> '(0 0 1 1 1.1 1) :length 88200)))
(do ((i 0 (+ i 1)))
((= i 88200))
(<a class=quiet href="#outa">outa</a> i (* (<a class=quiet href="#env">env</a> ampf) (<em class=red>polyshape</em> gen (<a class=quiet href="#env">env</a> indf)))))))
</pre>
<table>
<tr><td>
<img src="pix/waver.png" alt="picture of waveshaping sweep">
</td><td>
<img src="pix/waver2.png" alt="time domain">
</td></tr>
</table>
<!--
;;; data cutoff .015 light 56 jet
;;; xangle 292 scale 1.45 yangle 0 scale .68 zangle 0 scale 1.39, % 43
-->
<p>You can see there's
another annoying "gotcha": the DC component can be arbitrarily large.
If we don't counteract it in some way, we lose dynamic range, and we get a big click when the generator stops.
In addition (as the right graph shows, although in this case the effect is minor), the peak amplitude is dependent on the index. We can reduce this
problem somewhat by changing the signs of the harmonics to follow the
pattern + + - -:
</p>
<pre class="indented">
(list 1 .5 2 .25 3 -.125 4 -.125) ; squeeze the amplitude change toward index=0
</pre>
<p>but now the peak amplitude is hard to predict (it's .6242 in this example). Perhaps <a href="sndscm.html#flattenpartials">flatten-partials</a>
would be a better choice here.
To follow an amplitude envelope despite a changing index, we can use a <a href="#moving-max">moving-max</a> generator:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> ()
(let ((gen (<em class=red>make-polyshape</em> 1000.0 :partials (list 1 .25 2 .25 3 .125 4 .125 5 .25)))
(indf (<a class=quiet href="#make-env">make-env</a> '(0 0 1 1 2 0) :duration 2.0)) ; index env
(ampf (<a class=quiet href="#make-env">make-env</a> '(0 0 1 1 2 1 3 0) :duration 2.0)) ; desired amp env
(mx (<a class=quiet href="#moving-max">make-moving-max</a> 256)) ; track actual current amp
(samps (<a class=quiet href="#secondstosamples">seconds->samples</a> 2.0)))
(do ((i 0 (+ i 1)))
((= i samps))
(let ((val (<em class=red>polyshape</em> gen (<a class=quiet href="#env">env</a> indf)))) ; polyshape with index env
(<a class=quiet href="#outa">outa</a> i (/ (* (<a class=quiet href="#env">env</a> ampf) val)
(max 0.001 (<a class=quiet href="#moving-max">moving-max</a> mx val))))))))
</pre>
<p>The harmonic amplitude formula for the Chebyshev polynomials of the second kind is:
</p>
<img class="indented" src="pix/sceq44.png" alt="more cheby hka calcs">
<!-- LATEX:
&h_{k}(a) = \sum_{j=0}^{\infty} \Big(\tbinom{p-1}{j} - \tbinom{p-1}{j-1}\Big) a^p \sum_{i=0}^{\infty} (-1)^{i} \tbinom{p+i-1}{i} h_{p+2i}(1) & p=k+2j \\
-->
<p>
On a related topic, if we drive the sum of Chebyshev polynomials with more than one sinusoid,
we get sum and difference tones, much as in complex FM:
</p>
<!-- LATEX t5sum:
\lefteqn{T_{5}\Big( \frac{\cos(x) + \cos(20x)}{2}\Big) = } \\
\frac{1}{32} \big( & \cos (100x)+5 \cos (81x)+5 \cos (79x)+10 \cos (62x)+5 \cos (60x)+ \\
& 10 \cos (58x)+ 10 \cos (43x)-10 \cos (41x)-10 \cos (39x)+10 \cos (37x)+ \\
& 5 \cos (24x)-10 \cos (22x)- 10 \cos (18x)+5 \cos (16x)+ \cos (5x)+5 \cos (3x) \big)
-->
<table class="method">
<tr><td>
<div class="center">T5 driven with sinusoids at 100Hz and 2000Hz</div>
<hr>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> ()
(let ((pcoeffs (<em class=red>partials->polynomial</em> (float-vector 5 1)))
(gen1 (<a class=quiet href="#make-oscil">make-oscil</a> 100.0))
(gen2 (<a class=quiet href="#make-oscil">make-oscil</a> 2000.0)))
(do ((i 0 (+ i 1)))
((= i 44100))
(<a class=quiet href="#outa">outa</a> i (<em class=red>polynomial</em> pcoeffs
(* 0.5 (+ (<a class=quiet href="#oscil">oscil</a> gen1)
(<a class=quiet href="#oscil">oscil</a> gen2))))))))
</pre>
<hr>
<div class="center"><img src="pix/t5sum.png" alt="t5"></div>
</td>
<td>
<img class="indented" src="pix/crosswave.png" alt="cross">
</td></tr></table>
<p>This kind of output is typical; I get the impression that the cross products are
much more noticeable here than in FM.
Of course, we can take advantage of that:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:channels 2)
(let* ((dur 2.0)
(samps (<a class=quiet href="#secondstosamples">seconds->samples</a> dur))
(p1 (<em class=red>make-polywave</em> 800 (list 1 .1 2 .3 3 .4 5 .2)))
(p2 (<em class=red>make-polywave</em> 400 (list 1 .1 2 .3 3 .4 5 .2)))
(interpf (<a class=quiet href="#make-env">make-env</a> '(0 0 1 1) :duration dur))
(p3 (<em class=red>partials->polynomial</em> (list 1 .1 2 .3 3 .4 5 .2)))
(g1 (<a class=quiet href="#make-oscil">make-oscil</a> 800))
(g2 (<a class=quiet href="#make-oscil">make-oscil</a> 400))
(ampf (<a class=quiet href="#make-env">make-env</a> '(0 0 1 1 10 1 11 0) :duration dur)))
(do ((i 0 (+ i 1)))
((= i samps))
(let ((interp (<a class=quiet href="#env">env</a> interpf))
(amp (<a class=quiet href="#env">env</a> ampf)))
;; chan A: interpolate from one spectrum to the next directly
(<a class=quiet href="#outa">outa</a> i (* amp (+ (* interp (<em class=red>polywave</em> p1))
(* (- 1.0 interp) (<em class=red>polywave</em> p2)))))
;; chan B: interpolate inside the sum of Tns!
(<a class=quiet href="#outa">outb</a> i (* amp (<em class=red>polynomial</em> p3 (+ (* interp (<a class=quiet href="#oscil">oscil</a> g1))
(* (- 1.0 interp) (<a class=quiet href="#oscil">oscil</a> g2))))))))))
</pre>
<p>
If we use an arbitrary sound as the argument
to the polynomial, the output is a brightened or distorted version of the
original:
</p>
<!--
(The comparison in the T5 case above is between
T5(a+b) = 16(a+b)^5 - 20(a+b)^3 + 5(a+b) and the complex FM equation cos(5*(a+b))).
-->
<pre class="indented">
(define (brighten-slightly coeffs)
(let ((pcoeffs (partials->polynomial coeffs))
(mx (<a class=quiet href="extsnd.html#maxamp">maxamp</a>)))
(<a class=quiet href="extsnd.html#mapchannel">map-channel</a>
(lambda (y)
(* mx (<a class=quiet href="#polynomial">polynomial</a> pcoeffs (/ y mx)))))))
</pre>
<p>but watch out for clicks from the DC component if any of the "n" in the Tn are even.
When I use this idea, I either use only odd numbered partials in the partials->polynomial list,
or add an amplitude envelope to make sure the result ends at 0. I suppose you could also subtract out
the DC term (coeffs[0]), but I haven't tried this.
</p>
<!--
(with-sound (:channels 3 :clipped #f :statistics #t)
(let* ((dur 1.0)
(samps (seconds->samples dur))
(coeffs (partials->polynomial '(5 1)))
(gen (make-oscil 100.0))
(gen1 (make-oscil 500.0)))
(do ((i 0 (+ i 1)))
((= i samps))
(let* ((val (oscil gen))
(nval (oscil gen1))
(aval (polynomial coeffs (- 1.0 (* 2 val val))))
(bval (- 1.0 (* 2 nval nval)))
(cval (- aval bval)))
(outa i aval)
(outb i bval)
(outc i cval)))))
-->
<p>If you push the polyshape generator into high harmonics (above say 30), you'll
run into numerical trouble (the polywave generator is immune to this bug).
Where does the trouble lie?
The polynomials are related to each other
via the recursion: <img src="pix/sceq17.png" alt="Cheby recurse">, so the first
few polynomials are:
</p>
<!-- LATEX:
\begin{align*}
&T_{0}(x)=1 \\
&T_{1}(x)=x \\
&T_{2}(x)=2x^{2}-1\\
&T_{3}(x)=4x^{3}-3x \\
&T_{4}(x)=8x^{4}-8x^{2}+1
\end{align*}
\begin{align*}
&U_{0}(x)=1 \\
&U_{1}(x)=2x \\
&U_{2}(x)=4x^{2}-1 \\
&U_{3}(x)=8x^{3}-4x \\
&U_{4}(x)=16x^{4}-12x^{2}+1
\end{align*}
-->
<table>
<tr>
<td><img class="indented" src="pix/sceq18.png" alt="some Chebys"></td>
<td><img class="indented" src="pix/sceq19.png" alt="more Chebys"></td>
</tr></table>
<p>The first coefficient is 2^n or 2^(n-1). This is bad news if "n" is large because
we are expecting a bunch of huge numbers
to add up to something in the vicinity of 0.0 or 1.0.
If we're using 32-bit floats, the first sign of trouble comes when the order is around 26.
If you look at some of the coefficients, you'll see numbers like -129026688.000 (in the 32 bit case), which
should be -129026680.721 — we have run out of bits in the mantissa!
With doubles
we can only push the order up to around 46.
polywave, on the other hand, builds up the sum of sines from the underlying recursion, which is only slightly
slower than using the polynomial, and it is not bothered by these numerical problems.
I have run polywave with 16384 harmonics, and the maximum error compared to the
equivalent sum of sinusoids was around 5.0e-12.
</p>
<p>Since it is primarily used for additive synthesis, and we can always do that with oscils or table-lookup,
we might ask why we'd want polywave at all. Leaving aside
speed (the Chebyshev computation is 10 to 20 times faster than the equivalent sum of oscils)
and memory (the defunct table-lookup based waveshape generator and table-lookup itself use a table that has to be loaded),
the main reason to use polywave is accuracy. polywave
produces output that is as clean as the equivalent sum of oscils, whereas table-lookup
and poor old waveshape, both of which interpolate into a sampled version of the desired function, are noisy.
To make the difference almost appalling, here are spectra comparing a sum of oscils, polyshape,
(table-lookup based) waveshape, and table-lookup.
</p>
<img src="pix/4grfs.png" alt="compare ffts">
<!--
(with-sound (:channels 4)
(let ((poly (make-polyshape 100.0 :partials '(1 1 8 1 16 1)))
(wave (make-waveshape 100.0 :partials '(1 1 8 1 16 1))) ; this is normalized
(gen1 (make-oscil 100))
(gen2 (make-oscil 800))
(gen3 (make-oscil 1600))
(table (make-table-lookup 100.0 :wave (partials->wave '(1 1 8 1 16 1))))
)
(do ((i 0 (+ i 1)))
((= i 500000))
(out-any i (* .3 (+ (oscil gen1) (oscil gen2) (oscil gen3))) 0)
(out-any i (* .3 (polyshape poly)) 1)
(out-any i (* .9 (waveshape wave)) 2)
(out-any i (* .3 (table-lookup table)) 3))))
(set! (x-axis-label 0 0 1) "sum of oscils: frequency")
(set! (x-axis-label 0 1 1) "polyshape: frequency")
(set! (x-axis-label 0 2 1) "waveshape: frequency")
(set! (x-axis-label 0 3 1) "table-lookup: frequency")
-->
<div class="inset">
<p>
The table size is 512, but that almost doesn't matter; you'd have to use a table size of at least 8192
to approach the oscil and polyshape cases. The FFT size is 1048576, with no data window ("rectangular"), and the y-axis
is in dB, going down to -120 dB. The choice of fft window
can make a big difference; using no window, but a huge fft seems like the least confusing
way to present this result.
</p>
<p>
Notice the lower peaks in the table-lookup case. partials->wave puts n periods of the nth harmonic
in the table, so the nth harmonic has an effective table length of table-length/n. n * 1/n = 1, so all
our components have their first interpolation noise peak centered (in this case) around 7100 Hz ((512 * 100) mod 22050).
Since the 1600 Hz component has an effective table size of only 32 samples, it creates big sidebands at 5500 Hz
and 8700 Hz. The 800 Hz component makes smaller peaks (by a factor of 4, since this is proportional to n^2) at
6300 Hz and 7900 Hz, and the 100 Hz
cases are at 7000 Hz and 7200 Hz (down in amplitude by 16^2). The highest peaks are down only 60 dB.
See <a href="#table-lookup">table-lookup</a> for more discussion of interpolation noise (it's actually
amplitude modulation of the stored signal and the linear interpolating signal with severe aliasing).
</p>
<p>
The waveshaping noise is much worse because the polynomial is so
sensitive numerically. Here is a portion of the error signal at the point where the driving sinusoid
is at its maximum:
</p>
<img class="indented" src="pix/errorwave.png" alt="cheby error">
</div>
<!--
;;; omitted 100 component since it is clean and I couldn't get it to cancel...
(with-sound (:srate 1000000)
(let ((wave (make-waveshape 100.0 :partials '(8 1 16 1) :size 512))
(osc1 (make-oscil 1600.0 (* 0.5 pi)))
(osc2 (make-oscil 800.0 (* 0.5 pi))))
(do ((i 0 (+ i 1)))
((= i 1000000))
(outa i (- (waveshape wave)
(* 1/2 (+ (oscil osc1)
(oscil osc2))))))))
-->
<!-- this works: (make-waveshape 100.0 :partials '(1 1) :size 2)
because the initial "polynomial" is a straight line:
:(mus-data (make-waveshape 100.0 :partials '(1 1) :size 2))
#(-1.000 1.000)
and we use array-interp to drive it with a sinusoid, so x=x!
-->
<p>
See also <a href="#polyoid">polyoid and noid</a> in generators.scm.
</p>
<!-- SAWTOOTH ETC -->
<div class="innerheader" id="sawtoothdoc">sawtooth-wave, triangle-wave, pulse-train, square-wave</div>
<pre class="indented">
<em class=def id="make-triangle-wave">make-triangle-wave</em> (frequency 0.0) (amplitude 1.0) (initial-phase pi)
<em class=def id="triangle-wave">triangle-wave</em> s (fm 0.0)
<em class=def id="triangle-wave?">triangle-wave?</em> s
<em class=def id="make-square-wave">make-square-wave</em> (frequency 0.0) (amplitude 1.0) (initial-phase 0)
<em class=def id="square-wave">square-wave</em> s (fm 0.0)
<em class=def id="square-wave?">square-wave?</em> s
<em class=def id="make-sawtooth-wave">make-sawtooth-wave</em> (frequency 0.0) (amplitude 1.0) (initial-phase pi)
<em class=def id="sawtooth-wave">sawtooth-wave</em> s (fm 0.0)
<em class=def id="sawtooth-wave?">sawtooth-wave?</em> s
<em class=def id="make-pulse-train">make-pulse-train</em> (frequency 0.0) (amplitude 1.0) (initial-phase (* 2 pi))
<em class=def id="pulse-train">pulse-train</em> s (fm 0.0)
<em class=def id="pulse-train?">pulse-train?</em> s
</pre>
<table class="method">
<tr><td colspan=2 class="methodtitle">saw-tooth and friends' methods</td></tr>
<tr><td class="inner"><em class=gen>mus-frequency</em></td><td class="inner">frequency in Hz</td></tr>
<tr><td class="inner"><em class=gen>mus-phase</em></td><td class="inner">phase in radians</td></tr>
<tr><td class="inner"><em class=gen>mus-scaler</em></td><td class="inner">amplitude arg used in make-<gen></td></tr>
<tr><td class="inner"><em class=gen>mus-width</em></td><td class="inner">width of square-wave pulse (0.0 to 1.0)</td></tr>
<tr><td class="inner"><em class=gen>mus-increment</em></td><td class="inner">frequency in radians per sample</td></tr>
</table>
<p>These generators produce some standard old-timey wave forms that are still occasionally useful (well, triangle-wave
is useful; the others are silly).
One popular kind of vibrato is:
</p>
<pre class="indented">
(+ (triangle-wave pervib)
(<a class=quiet href="#rand-interp">rand-interp</a> ranvib))
</pre>
<p>sawtooth-wave ramps from -1 to 1, then goes immediately back to -1.
Use a negative frequency to turn the "teeth" the other way.
To get a sawtooth from 0 to 1, you can use modulo:
</p>
<pre class="indented">
(with-sound () (do ((i 0 (+ i 1)) (x 0.0 (+ x .01))) ((= i 22050)) (outa i (modulo x 1.0))))
</pre>
<p>
triangle-wave ramps from -1 to 1, then ramps from 1 to -1.
pulse-train produces a single sample of 1.0, then zeros.
square-wave produces 1 for half a period, then 0. All have a period
of two pi, so the "fm" argument should have an effect comparable to the
same FM applied to the same waveform in <a href="#table-lookup">table-lookup</a>.
</p>
<table>
<tr>
<td>
<div class="scheme">
<pre class="indented">
(with-sound (:play #t)
(let ((gen (make-triangle-wave 440.0)))
(do ((i 0 (+ i 1)))
((= i 44100))
(outa i (* 0.5 (triangle-wave gen))))))
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="ruby">
<pre class="indented">
with_sound(:play, true) do
gen = make_triangle_wave(440.0);
44100.times do |i|
outa(i, 0.5 * triangle_wave(gen), $output)
end
end.output
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="forth">
<pre class="indented">
lambda: ( -- )
440.0 make-triangle-wave { gen }
44100 0 do
i gen 0 triangle-wave f2/ *output* outa drop
loop
; :play #t with-sound drop
</pre>
</div>
</td>
</tr>
</table>
<p>To get a square-wave with control over the "duty-factor":
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> ()
(let* ((duty-factor .25) ; ratio of pulse duration to pulse period
(p-on (<em class=red>make-pulse-train</em> 100 0.5))
(p-off (<em class=red>make-pulse-train</em> 100 -0.5 (* 2 pi (- 1.0 duty-factor)))))
(do ((sum 0.0)
(i 0 (+ i 1)))
((= i 44100))
(set! sum (+ sum (<em class=red>pulse-train</em> p-on) (<em class=red>pulse-train</em> p-off)))
(<a class=quiet href="#outa">outa</a> i sum))))
</pre>
<p>
This is the <a href="#adjustable-square-wave">adjustable-square-wave</a> generator in generators.scm.
That file also defines <a href="#adjustable-triangle-wave">adjustable-triangle-wave</a> and
<a href="#adjustable-sawtooth-wave">adjustable-sawtooth-wave</a>.
All of these generators produce non-band-limited output; if the frequency is too high, you can get foldover.
A more reasonable square-wave can be generated via
<code>(tanh (* B (sin theta)))</code>, where "B" (a float) sets how squared-off it is:
</p>
<!-- the maxima experiments are in maxima.clm -->
<table>
<tr>
<td class="br">B: 1.0</td>
<td class="br">B: 3.0</td>
<td class="br">B: 100.0</td>
</tr>
<tr>
<td class="br">
<img src="pix/tanh1.png" alt="tanh 1">
</td>
<td class="br">
<img src="pix/tanh3.png" alt="tanh 1">
</td>
<td class="br">
<img src="pix/tanh100.png" alt="tanh 1">
</td>
</tr></table>
<!-- LATEX: sceq11
\tanh(x) = x-\frac{x^{3}}{3}+\frac{2x^{5}}{15}-\frac{17x^{7}}{315}+\frac{62x^{9}}{2835}-\frac{1382x^{11}}{155925}\cdots
long form:
\tanh(x) = x-\frac{x^{3}}{3}+\frac{2x^{5}}{15}-\frac{17x^{7}}{315}+\frac{62x^{9}}{2835}-\frac{1382x^{11}}{155925}+\frac{21844x^{13}}{6081075}-\frac{929569x^{15}}{638512875}\cdots
\tanh(x) = x-\frac{x^{3}}{3}+\frac{2x^{5}}{15}-\frac{17x^{7}}{315}+\frac{62x^{9}}{2835}-\frac{1382x^{11}}{155925}+\frac{21844x^{13}}{6081075}-\frac{929569x^{15}}{638512875}+\frac{6404582x^{17}}{10854718875}-\frac{443861162x^{19}}{1856156927625}\cdots
-->
<!-- LATEX: sceq12
\tanh(\sin(x)) = \frac{140069}{172800} \sin(x) + \frac{13319}{241920} \sin(3x) + \frac{1973}{483840} \sin(5x) + \frac{799}{1451520} \sin(7x) - \frac{71}{7257600} \sin(9x) + \frac{691}{79833600} \sin(11x) + \cdots
-->
<!-- LATEX: tanhsum.png
& \tanh(B \sin(x)) = \frac{\sinh(B \sin(x))}{\cosh(B \sin(x))} = \frac{2 \sum_{k=0}^{\infty} (-1)^{k} I_{2k+1}(B) \sin(2k+1)x}{\cosh(B \sin(x))}
second try:
& \tanh(B \sin(x)) = \frac{-i \sin(iB \sin(x))}{\cos(iB \sin(x))} = \frac{2 \sum_{k=0}^{\infty} (-1)^{k} I_{2k+1}(B) \sin(2k+1)x}{\cos(iB \sin(x))} = \frac{e^{2B\sin(x)} - 1}{e^{2B\sin(x)}+1}
third try:
\tanh(B \sin(x)) = \frac{-i \sin(iB \sin(x))}{\cos(iB \sin(x))} = \frac{2 \sum_{k=0}^{\infty} (-1)^{k} I_{2k+1}(B) \sin(2k+1)x}{\cos(iB \sin(x))} = \frac{e^{2B\sin(x)} - 1}{e^{2B\sin(x)}+1}
fourth try (break it in 2!!):
\tanh(B \sin(x)) = \frac{-i \sin(iB \sin(x))}{\cos(iB \sin(x))}
= \frac{2 \sum_{k=0}^{\infty} (-1)^{k} I_{2k+1}(B) \sin(2k+1)x}{\cos(iB \sin(x))} = \frac{e^{2B\sin(x)} - 1}{e^{2B\sin(x)}+1}
fifth try: do it as one, and set the "MediaBox" line in the pdf file by hand!!
\tanh(B \sin(x)) = \frac{-i \sin(iB \sin(x))}{\cos(iB \sin(x))} = \frac{2 \sum_{k=0}^{\infty} (-1)^{k} I_{2k+1}(B) \sin(2k+1)x}{\cos(iB \sin(x))} = \frac{e^{2B\sin(x)} - 1}{e^{2B\sin(x)}+1}
-->
<p>
The spectrum of tanh(sin) can be obtained by expanding tanh as a power series:
</p>
<img class="indented" src="pix/sceq11.png" alt="tanh power series">
<p>
plugging in "sin" for "x", expanding the sine powers, and collecting terms (very tedious —
use maxima!):
</p>
<img class="indented" src="pix/sceq12.png" alt="tanh sin power series">
<p>
which is promising since a square wave is made up of odd harmonics with amplitude 1/n.
As the "B" in tanh(B sin(x)) increases above pi/2, this series doesn't apply.
</p>
<img class="indented" src="pix/tanhsum.png" alt="more tanh">
<p>but I haven't found a completion of this expansion that isn't ugly when B > pi/2.
In any case, we can check the
formula for tanh, and see that the e^-x term will vanish (in the positive x case), giving 1.0.
So we do get a square wave, but it's not band limited. If a complex signal replaces the sin(x),
we get "intermodulation products" (sum and difference tones); this use of tanh as a soft clipper
goes way back — I don't know who invented it.
</p>
<p>If you try to make a square wave by adding harmonics at amplitude 1/n,
you run into "Gibb's phenomenon": although the sum
converges on a square wave, it does so "pointwise" — each point converges to the square wave,
but the sum always has an overshoot. To get something that looks square, we need to round-off the
corners.
Bill Gosper shows one mathematical
way to do this (<a href="http://www.tweedledum.com/rwg/gibbs.html">gibbs.html</a>).
We could also use <a href="sndscm.html#withmixedsound">with-mixed-sound</a> and the Mixes dialog:
</p>
<pre class="indented">
(definstrument (sine-wave start dur freq amp)
(let* ((beg (<a class=quiet href="#secondstosamples">seconds->samples</a> start))
(end (+ beg (<a class=quiet href="#secondstosamples">seconds->samples</a> dur)))
(osc (<a class=quiet href="#make-oscil">make-oscil</a> freq)))
(do ((i beg (+ i 1)))
((= i end))
(<a class=quiet href="#outa">outa</a> i (* amp (<a class=quiet href="#oscil">oscil</a> osc))))))
(<em class=red>with-mixed-sound</em> ()
(sine-wave 0 1 10.0 1.0)
(sine-wave 0 1 30.0 .333)
(sine-wave 0 1 50.0 .2)
(sine-wave 0 1 70.0 .143))
</pre>
<p>
Now we can play with the
individual sinewave amplitudes in the Mixes dialog, seeing "in realtime" what
effect an amplitude has on the waveform. In the graph below, we've taken the original set of four sines
and chosen amplitudes 1.16, .87, .46, .14 (these are multipliers on the original 1/n amps).
The first graph is the original waveform, the last is the result of the
amplitude changes, and the middle one shows 100 sines (it is the usual demo that
the Gibbs overshoot is not reduced by adding lots more components).
The peak amplitude should be pi/4, but the Gibbs phenomenon adds .14.
</p>
<img class="indented" src="pix/smoothsq.png" alt="reduce Gibbs">
<!--
(definstrument (sine-wave start dur freq amp)
(let* ((beg (seconds->samples start))
(end (+ beg (seconds->samples dur)))
(osc (make-oscil freq)))
(do ((i beg (+ i 1)))
((= i end))
(outa i (* amp (oscil osc))))))
(with-sound ("4-sines.snd")
(sine-wave 0 1 10.0 1.0)
(sine-wave 0 1 30.0 .333)
(sine-wave 0 1 50.0 .2)
(sine-wave 0 1 70.0 .143))
(with-sound ("100-sines.snd")
(do ((i 1 (+ i 2)))
((> i 200))
(sine-wave 0 1 (* i 10.0) (/ 1.0 i))))
(with-mixed-sound (:output "4-sines-mixed.snd")
(sine-wave 0 1 10.0 1.0)
(sine-wave 0 1 30.0 .333)
(sine-wave 0 1 50.0 .2)
(sine-wave 0 1 70.0 .143))
-->
<p>But goofing with individual amplitudes quickly becomes tiresome. This "realtime" business
depends on luck; if we have some idea of what we're doing, we don't have to get lucky.
Since
tanh(B sin(x)) produces a nice square wave,
we can truncate its spectrum at the desired number of harmonics:
</p>
<pre class="indented">
(define square-wave->coeffs
(let ((previous-results (make-vector 128 #f)))
(lambda* (n B)
(or (and (< n 128)
(not B)
(previous-results n))
(let* ((coeffs (make-float-vector (* 2 n)))
(size (expt 2 12))
(rl (make-float-vector size)))
(do ((incr (/ (* 2 pi) size))
(index (or B (max 1 (floor (/ n 2)))))
(i 0 (+ i 1))
(x 0.0 (+ x incr)))
((= i size))
(set! (rl i) (<em class=red>tanh</em> (* index (<em class=red>sin</em> x))))) ; make our desired square wave
(<a class=quiet href="#spectrum">spectrum</a> rl (make-float-vector size) #f 2) ; get its spectrum
(do ((i 0 (+ i 1))
(j 0 (+ j 2)))
((= i n))
(set! (coeffs j) (+ j 1))
(set! (coeffs (+ j 1)) (/ (* 2 (rl (+ j 1))) size)))
(if (and (< n 128) ; save this set so we don't have to compute it again
(not B))
(set! (previous-results n) coeffs))
coeffs)))))
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> ()
(let* ((samps (<a class=quiet href="#secondstosamples">seconds->samples</a> 1.0))
(wave (<a class=quiet href="#make-polywave">make-polywave</a> 100.0
:partials (<em class=red>square-wave->coeffs</em> 16)
:type mus-chebyshev-second-kind)))
(do ((i 0 (+ i 1)))
((= i samps))
(<a class=quiet href="#outa">outa</a> i (* 0.5 (<a class=quiet href="#polywave">polywave</a> wave))))))
</pre>
<table class="method"><tr><td>
<img src="pix/tanhexs.png" alt="tanh">
</td></tr></table>
<p>See also <a href="#tanhsin">tanhsin</a> in generators.scm.
Another square-wave choice is <a href="#eoddcos">eoddcos</a> in <a href="#othergenerators">generators.scm</a>,
based on atan; as its "r" parameter approaches 0.0, you get closer to a square wave.
Even more amusing is this algorithm (related to tanh(sin)):
</p>
<img class="indented" src="pix/sceq13.png" alt="square">
<!-- CMJ 37 4 sept 2006 p326 -->
<!-- LATEX: \frac{(c+1)^{\cos t}-(c-1)^{\cos t}}{(c+1)^{\cos t}+(c-1)^{\cos t}} -->
<pre class="indented">
(define (cossq c theta) ; as c -> 1.0+, more of a square wave (try 1.00001)
(let* ((cs (cos theta)) ; (+ theta pi) if matching sin case (or (- ...))
(cm1c (expt (- c 1.0) cs))
(cp1c (expt (+ c 1.0) cs)))
(/ (- cp1c cm1c)
(+ cp1c cm1c)))) ; from "From Squares to Circles..." Lasters and Sharpe, Math Spectrum 38:2
(define (sinsq c theta) (cossq c (- theta (* 0.5 pi))))
(define (sqsq c theta) (sinsq c (- (sinsq c theta)))) ; a sharper square wave
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> ()
(let ((angle 0.0))
(do ((i 0 (+ i 1))
(angle 0.0 (+ angle 0.02)))
((= i 44100))
(outa i (* 0.5 (+ 1.0 (sqsq 1.001 angle)))))))
</pre>
<p>And in the slightly batty category is this method which uses only nested sines:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> ()
(let ((angle 0.0) (z 1.18)
(incr (<a class=quiet href="#hztoradians">hz->radians</a> 100.0)))
(do ((i 0 (+ i 1)))
((= i 20000))
(let ((result (* z (sin angle))))
(do ((k 0 (+ k 1)))
((= k 100)) ; the limit here sets how square it is, and also the overall amplitude
(set! result (* z (sin result))))
(set! angle (+ angle incr))
(<a class=quiet href="#outa">outa</a> i result)))))
</pre>
<p>The continuously variable square-wave, tanh(B sin), can be differentiated to get a variable pulse-train,
or integrated to get a variable triangle-wave.
The derivative is B * cos(x) / (cosh^2(B * sin(x))):
</p>
<pre class="indented">
(with-sound ()
(let ((Benv (make-env '(0 .1 .1 1 .7 2 2 5) :end 10000))
(osc (make-oscil 100)))
(do ((i 0 (+ i 1)))
((= i 10000))
(let* ((B (env Benv))
(num (cos (mus-phase osc)))
(den (cosh (* B (oscil osc)))))
(outa i (/ num den den))))))
</pre>
<img class="indented" src="pix/tanhsinderiv.png" alt="tanh(sin) as pulse train">
<p>
Similar, but simpler is B*cos(x)/(e^(B*cos(x)) - 1):
</p>
<pre class="indented">
(with-sound ()
(let ((gen (make-oscil 40.0))
(Benv (make-env '(0 .75 1 1.5 2 20) :end 10000)))
(do ((i 0 (+ i 1)))
((= i 10000))
(let* ((B (env Benv))
(arg (* B pi (+ 1.0 (oscil gen)))))
(outa i (/ arg (- (exp arg) 1)))))))
</pre>
<img class="indented" src="pix/xex.png" alt="another pulse train">
<p>
When we integrate tanh(B sin), the peak amp depends
on both the frequency and the "B" factor (which sets how close we get to a triangle wave):
</p>
<pre class="indented">
(with-sound ()
(let ((gen (make-oscil 30.0))
(Benv (make-env '(0 .1 .25 1 2 3 3 10)
:end 20000))
(scl (hz->radians 30.0))
(sum 0.0))
(do ((i 0 (+ i 1)))
((= i 20000))
(let* ((B (env Benv))
(val (/ (* scl (max 1.0 (log B))
(tanh (* B (oscil gen))))
B)))
(outa i (- sum 1.0))
(set! sum (+ sum val))))))
</pre>
<img class="indented" src="pix/tanhsininteg.png" alt="tanh(sin) as triangle-wave">
<!--
(set! *selected-graph-color* (make-color 1 1 1))
(set! *selected-data-color* (make-color 0 0 0))
(set! (x-axis-label 0 0 0) "derivative of tanh(B*sin), B from .1 to 5")
(set! (x-axis-label 0 0 0) "integration of tanh(B*sin), B from .1 to 10")
-->
<p>The amplitude scaling is obviously not right (if "B" > 3, it works to use (* (/ scl 1.6) (tanh (* B (oscil gen))))
and (outa i (- sum .83)), but if "B" is following an envelope, the integration makes it hard to keep everything
centered and normalized).
For sawtooth output, see also <a href="#rksin">rksin</a>.
In these generators, the "fm" argument is useful mainly for various sci-fi sound effects:
</p>
<pre class="indented">
(define (tritri start dur freq amp index mcr)
(let* ((beg (<a class=quiet href="#secondstosamples">seconds->samples</a> start))
(end (+ beg (<a class=quiet href="#secondstosamples">seconds->samples</a> dur)))
(carrier (<em class=red>make-triangle-wave</em> freq))
(modulator (<em class=red>make-triangle-wave</em> (* mcr freq))))
(do ((i beg (+ i 1)))
((= i end))
(<a class=quiet href="#outa">outa</a> i (* amp (<em class=red>triangle-wave</em> carrier
(* index (<em class=red>triangle-wave</em> modulator))))))))
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:srate 44100) (tritri 0 1 1000.0 0.5 0.1 0.01)) ; sci-fi laser gun
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:srate 44100) (tritri 0 1 4000.0 0.7 0.1 0.01)) ; a sparrow?
</pre>
<p>On the other hand, animals.scm uses pulse-train's fm argument to track a frequency envelope,
triggering a new peep each time the pulse goes by.
I think just about every combination of oscil/triangle-wave/sawtooth-wave/square-wave has been
used. Even triangle-wave(square-wave) can make funny noises. See <a href="#ncosdoc">ncos</a>
for more dicussion about using these generators as FM modulators.
</p>
<!-- NCOS, NSIN -->
<div class="innerheader" id="ncosdoc">ncos and nsin</div>
<pre class="indented">
<em class=def id="make-ncos">make-ncos</em> (frequency 0.0) (n 1)
<em class=def id="ncos">ncos</em> nc (fm 0.0)
<em class=def id="ncos?">ncos?</em> nc
<em class=def id="make-nsin">make-nsin</em> (frequency 0.0) (n 1)
<em class=def id="nsin">nsin</em> ns (fm 0.0)
<em class=def id="nsin?">nsin?</em> ns
</pre>
<table class="method">
<tr><td colspan=2 class="methodtitle">ncos methods</td></tr>
<tr><td class="inner"><em class=gen>mus-frequency</em></td><td class="inner">frequency in Hz</td></tr>
<tr><td class="inner"><em class=gen>mus-phase</em></td><td class="inner">phase in radians</td></tr>
<tr><td class="inner"><em class=gen>mus-scaler</em></td><td class="inner">(/ 1.0 cosines)</td></tr>
<tr><td class="inner"><em class=gen>mus-length</em></td><td class="inner">n or cosines arg used in make-<gen></td></tr>
<tr><td class="inner"><em class=gen>mus-increment</em></td><td class="inner">frequency in radians per sample</td></tr>
</table>
<p>ncos produces a band-limited pulse train containing
"n" cosines. I think this was originally viewed as a way to get a speech-oriented
pulse train that would then be passed through formant filters (see pulse-voice in examp.scm).
Set "n" to srate/2 to get a pulse-train (a single non-zero sample). These generators are based on the Dirichlet kernel:
</p>
<!-- LATEX: \sum_{k=0}^{n}\cos kx = \frac{1}{2}\Bigg(1+\frac{\sin(n+\frac{1}{2})x}{\sin \frac{x}{2}}\Bigg) -->
<img class="indented" src="pix/sceq2.png" alt="sum of cosines">
<!--
cos(x) + cos(2x) + ... cos(nx) =
(sin((n + .5)x) / (2 * sin(x / 2))) - 1/2
-->
<pre>
</pre>
<table>
<tr>
<td>
<div class="scheme">
<pre class="indented">
(with-sound (:play #t)
(let ((gen (make-ncos 440.0 10)))
(do ((i 0 (+ i 1)))
((= i 44100))
(outa i (* 0.5 (ncos gen))))))
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="ruby">
<pre class="indented">
with_sound(:play, true) do
gen = make_ncos(440.0, 10);
44100.times do |i|
outa(i, 0.5 * ncos(gen), $output)
end
end.output
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="forth">
<pre class="indented">
lambda: ( -- )
440.0 10 make-ncos { gen }
44100 0 do
i gen 0 ncos f2/ *output* outa drop
loop
; :play #t with-sound drop
</pre>
</div>
</td>
</tr>
</table>
<p>There are many similar formulas:
see <a href="#ncos2">ncos2</a> and friends in <a href="#othergenerators">generators.scm</a>. "Trigonometric Delights" by Eli Maor has
a derivation of the nsin formula and a neat
geometric explanation. For a derivation of the ncos formula, see "Fourier
Analysis" by Stein and Shakarchi, or (in the formula given below) multiply the left side (the cosines) by sin(x/2), use the trig
formula 2sin(a)cos(b) = sin(b+a)-sin(b-a), and notice that all the terms in the series
cancel except the last.
</p>
<pre class="indented">
(define (simple-soc beg dur freq amp)
(let* ((os (<em class=red>make-ncos</em> freq 10))
(start (<a class=quiet href="#secondstosamples">seconds->samples</a> beg))
(end (+ start (<a class=quiet href="#secondstosamples">seconds->samples</a> dur))))
(do ((i start (+ i 1))) ((= i end))
(outa i (* amp (<em class=red>ncos</em> os))))))
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> () (simple-soc 0 1 100 1.0))
</pre>
<img class="indented" src="pix/cosines.png" alt="sum of cosines example">
<p>The <a href="#sinc-train">sinc-train</a> generator (in generators.scm) is very similar to ncos.
If you use ncos as the FM modulating signal, you may be surprised and disappointed.
As the modulating signal approaches a spike (as n increases), the bulk of the energy collapses back onto the carrier:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> ()
(for-each
(lambda (arg)
(let ((car1 (<a class=quiet href="#make-oscil">make-oscil</a> 1000))
(mod1 (<em class=red>make-ncos</em> 100 (cadr arg)))
(start (<a class=quiet href="#secondstosamples">seconds->samples</a> (car arg)))
(samps (<a class=quiet href="#secondstosamples">seconds->samples</a> 1.0))
(ampf (<a class=quiet href="#make-env">make-env</a> '(0 0 1 1 20 1 21 0)
:duration 1.0 :scaler .8))
(index (<a class=quiet href="#hztoradians">hz->radians</a> (* 100 3.0))))
(do ((i start (+ i 1)))
((= i (+ start samps)))
(<a class=quiet href="#outa">outa</a> i (* (<a class=quiet href="#env">env</a> ampf)
(<a class=quiet href="#oscil">oscil</a> car1 (* index
(<em class=red>ncos</em> mod1))))))))
'((0.0 1) (2.0 2) (4.0 4) (6.0 8) (8.0 16) (10.0 32) (12.0 64) (14.0 128))))
</pre>
<img class="indented" src="pix/ncosfm.png" alt="ncos as FM">
<!--
ncosfm.png:
jet .001 59 invert
x 317 1.53
y 237 0.65
z 0 1.28
hop 4 % 0.14
blackman2 8192
-->
<!--
pulsefm.png:
(with-sound (:channels 2)
(let* ((car1 (make-oscil 1000))
(mod1 (make-pulse-train 100))
(samps (seconds->samples 1.0))
(ampf (make-env '(0 0 1 1 20 1 21 0) :duration 1.0 :scaler .8))
(index (hz->radians (* 100 3.0))))
(do ((i 0 (+ i 1)))
((= i samps))
(let ((amp (env ampf))
(fm (* index (pulse-train mod1))))
(outa i fm)
(outb i (* amp (oscil car1 fm)))))))
(set! *selected-graph-color* (make-color 1 1 1))
(set! *selected-data-color* (make-color 0 0 0))
(set! (show-transform-peaks 0 0) #f)
cursor 10584
(set! *axis-label-font* *axis-numbers-font*)
(set! (x-axis-label 0 0 0) "pulse train modulating signal, FM index: 3.0")
(set! (x-axis-label 0 1 0) "modulated signal")
-->
<p>If you go all the way and use a pulse-train as the FM source, you get a
large component for the carrier, and all the others are very small.
</p>
<img class="indented" src="pix/pulsefm.png" alt="pulse-train as FM">
<!-- LATEX jprod.png \prod_{i=1}^{k}J_{k_{i}}(B_{i}) -->
<!-- j0j1.png:
(with-sound (:channels 2 :srate 10000)
(do ((x 0.0 (+ x .0001))
(i 0 (+ i 1)))
((= i 40000))
(outa i (bes-j0 x))
(outb i (bes-j1 x))))
(set! (x-axis-label 0 0) "J0 and J1")
(set! (x-axis-label 0 1) "")
(set! *axis-label-font*"9x15")
-->
<img class="indented" src="pix/j0j1.png" alt="j0 and j1">
<pre class="indented">
(define (ncfm freq-we-want wc modfreq baseindex n)
;; get amplitude of "freq-we-want" given ncos as FM,
;; "wc" as carrier, "modfreq" as ncos freq,
;; "baseindex" as FM-index of first harmonic,
;; "n" as number of harmonics
(do ((harms ())
(amps ())
(i 1 (+ i 1)))
((> i n)
(<a class=quiet href="sndscm.html#fmparallelcomponent">fm-parallel-component</a> freq-we-want wc
(reverse harms) (reverse amps) () () #f))
(set! harms (cons (* i modfreq) harms))
(set! amps (cons (/ baseindex i n) amps))))
</pre>
<table class="method">
<tr><td colspan=3 class="methodtitle">4 components: (ncfm x 1000 100 3.0 4)</td></tr>
<tr><td class="br">x=1000</td><td class="br"> 0.81</td><td class="br"> 0.81 from J0(3/(4*k)) '(0 0 0 0)</td></tr>
<tr><td class="br">x=900</td><td class="br">-0.44</td><td class="br">-0.32 from J1(3/4)*J0s '(-1 0 0 0)</td></tr>
<tr><td class="br">x=800</td><td class="br">-0.14</td><td class="br">-0.16 from J1(3/8)*J0s '(0 -1 0 0)</td></tr>
<tr><td class="br">x=700</td><td class="br">-0.06</td><td class="br">-0.10 from J1(3/12)*J0s '(0 0 -1 0)</td></tr>
</table>
<pre>
</pre>
<table class="method">
<tr><td colspan=3 class="methodtitle">24 components: (ncfm x 1000 100 3.0 24)</td></tr>
<tr><td class="br">x=1000</td><td class="br">0.99</td><td class="br"> 0.99 from J0(3/(24*k)) '(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)</td></tr>
<tr><td class="br">x=900</td><td class="br">-0.06</td><td class="br">-0.06 from J1(3/24)*J0s '(-1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)</td></tr>
<tr><td class="br">x=800</td><td class="br">-0.03</td><td class="br">-0.03 from J1(3/48)*J0s '(0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)</td></tr>
<tr><td class="br">x=700</td><td class="br">-0.02</td><td class="br">-0.02 from J1(3/96)*J0s '(0 0 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0)</td></tr>
</table>
<p>You can multiply the index by n to counteract the effect of the n modulators
(in the n=128 case mentioned above, the index becomes 384!).
I find it surprising how smooth the spectral evolution is in this context.
Here we sweep the index from 0 to 48 using n=16:
</p>
<table class="pb">
<tr><td>
<img src="pix/ncossweep.png" alt="ncos">
</td></tr>
<tr><td class="center">ncos (n=16) as FM, index from 0 to 48</td></tr>
</table>
<!--
(with-sound (:statistics #t)
(let* ((dur 10.0)
(samps (seconds->samples dur))
(car1 (make-oscil 1000))
(mod1 (make-sum-of-cosines 16 100))
(ampf (make-env '(0 0 1 1 20 1 21 0) :duration dur :scaler .8))
(index (hz->radians (* 100 16 3.0)))
(indf (make-env '(0 0 1 1) :scaler index :duration dur)))
(do ((i 0 (+ i 1)))
((= i samps))
(outa i (* (env ampf)
(oscil car1 (* (env indf)
(sum-of-cosines mod1))))))))
4096 blackman2 100(dark) 0 cutoff invert jet
319 1.5
289 0.71
0 1.0
4 0.25
-->
<p>But if our second analysis is correct, there's nothing special about the spike waveform that ncos produces.
We only need a lot of components of decreasing effective FM index. If we randomize the initial
phases of the n harmonically related equal amplitude sinusoids, we can minimize the peak amplitude (to reduce the
spike),
getting waveforms and results like these:
</p>
<table class="pb">
<tr><td>
<img src="pix/nsinfm.png" alt="ncos case but random phases">
</td></tr>
<tr><td class="center">FM of sum of n sinusoids</td></tr>
</table>
<table class="pb">
<tr><td>
<img src="pix/randomsins.png" alt="ncos case but random phases">
</td></tr>
<tr><td class="center">sum of n sinusoids minimizing resemblance to pulse-train</td></tr>
</table>
<!-- same setting as above
(defgenerator (ngencos
:make-wrapper (lambda (g)
(let ((n (g 'n))
(frq (g 'frequency))
(phases (g 'phases)))
(set! (g 'arr) (make-vector n 0.0))
(do ((i 0 (+ i 1)))
((= i n))
(if (float-vector? phases)
(set! ((g 'arr) i) (make-oscil (* frq (+ i 1)) (phases i)))
(set! ((g 'arr) i) (make-oscil (* frq (+ i 1)) (random (* 2 pi)))))))
g))
frequency (n 1) (phases #f) (arr #f) fm)
(define* (ngencos gen (fm 0.0)) ; polyoid now, I think
(set! (gen 'fm) fm)
(with-let gen
(let ((sum 0.0))
(do ((i 0 (+ i 1)))
((= i n))
(set! sum (+ sum (oscil (arr i) (* (+ i 1) fm)))))
(/ sum n))))
(with-sound (:channels 1 :clipped #f)
(for-each
(lambda (arg)
(let ((car1 (make-oscil 1000))
(norm (/ 1.0 (caddr arg)))
(mod1 (make-ngencos 100 (cadr arg) (cadddr arg)))
(start (seconds->samples (car arg)))
(samps (seconds->samples 1.0))
(ampf (make-env '(0 0 1 1 20 1 21 0)
:duration 1.0 :scaler .8))
(index (hz->radians (* 100 3.0)))
(mx 0.0))
(do ((i start (+ i 1)))
((= i (+ start samps)))
(let ((md (ngencos mod1)))
(outa i (* (env ampf)
(oscil car1 (* index norm md))))
(set! mx (max mx (abs md)))
))
(snd-display ";~A ~A ~A" (cadr arg) (caddr arg) mx)))
(list
(list 0.0 1 1.0 (float-vector 0))
(list 2.0 2 0.881 (float-vector 0 0))
(list 4.0 4 0.5184 (float-vector 1.295 0.248 0.304 2.785))
(list 6.0 8 0.393 (float-vector 4.515 1.780 4.259 1.771 1.166 0.254 1.419 2.735))
(list 8.0 16 0.302 (float-vector 0.432 2.086 2.763 2.344 2.811 3.409 1.836 6.173 3.770 2.339 6.158 1.530 6.132 3.006 4.967 0.859))
(list 10.0 32 0.266 (float-vector 6.208 4.197 3.109 1.718 5.050 1.317 4.334 3.778 4.936 0.069 3.025 2.115 5.060 1.286 3.499 5.191 1.822 5.985 4.384 1.394 3.453 2.579 3.031 3.255 3.834 2.621 1.390 0.717 0.409 3.370 6.042 6.052))
(list 12.0 64 0.2124 (float-vector 4.913 5.507 5.262 1.926 4.819 3.794 0.355 1.178 4.959 1.012 3.433 2.855 2.191 4.792 3.740 1.865 5.196 1.078 4.139 5.518 3.053 3.958 3.131 6.260 2.157 4.279 2.352 4.314 1.102 5.967 3.551 2.439 5.456 4.833 5.213 3.523 3.263 2.810 0.433 0.639 2.554 3.469 2.682 4.765 0.125 3.824 1.137 6.166 0.019 2.240 4.406 4.734 5.451 6.230 4.943 4.160 3.577 5.086 2.444 0.900 1.952 2.234 4.794 3.424))
(list 14.0 128 0.16121 (float-vector 1.531 4.987 1.847 0.632 6.101 4.309 0.517 1.910 4.921 4.949 6.040 5.611 2.831 5.338 0.891 5.388 0.599 2.677 6.248 5.592 1.977 1.794 3.572 2.638 1.903 3.083 2.412 6.125 3.799 5.619 5.949 1.241 3.044 5.395 5.865 4.846 4.899 2.267 4.537 3.979 1.783 3.826 1.325 5.278 5.799 4.977 2.066 3.029 1.036 4.606 1.691 6.079 4.957 6.138 2.603 1.111 1.335 1.765 5.767 2.730 0.702 1.122 1.628 1.848 0.712 2.338 5.099 6.249 2.009 3.379 1.653 4.831 2.245 1.831 1.113 5.462 5.533 2.944 4.376 4.734 3.285 4.361 1.015 2.100 5.022 3.269 0.796 0.317 5.244 2.613 4.609 3.415 4.454 0.228 2.025 0.216 1.785 3.599 3.207 5.019 3.591 5.138 4.333 3.005 6.208 5.296 0.763 3.741 3.446 3.962 0.204 1.715 4.054 2.402 1.455 1.842 4.637 4.427 0.536 2.700 4.289 3.066 0.574 6.106 1.472 5.793 4.294 2.287)))))
-->
<div class="inset">
<p>Compare the sound of the n=64 and n=128 cases using ncos and random phases: they sound very different
despite having the same spectrum. We confront the burning question: given n equal amplitude
harmonically related sinusoids, what is the minimum peak amplitude?
For my current best results, see <a href="sndscm.html#peakphasesdoc">peak-phases</a>.
</p>
</div>
<p>
If you use ncos (or nsin) as both the carrier and modulator, you get a very similar effect. As n increases,
the ncos(wc + ncos(wm)) output gradually approaches the unmodulated ncos output — the crunch happens on each carrier component,
but most strongly on the earlier ones (the "effective index" is less on those components, as mentioned
under <a href="#polywave">polywave</a>).
And (for some reason this makes me smile), polywave modulated by ncos behaves the same way:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> ()
(let ((modulator (<em class=red>make-ncos</em> 100 :n 128))
(carrier (<em class=red>make-polywave</em> 1000 (list 1 .5 3 .25 6 .25))))
(do ((i 0 (+ i 1)))
((= i 20000))
(<a class=quiet href="#outa">outa</a> i (* .5 (<em class=red>polywave</em> carrier
(* (<a class=quiet href="#hztoradians">hz->radians</a> (* 3 100))
(<em class=red>ncos</em> modulator 0.0))))))))
</pre>
<p>So, a pulse-train modulated by a pulse-train is a pulse-train.
Are there any other cases where gen(wc + gen(wm)) = gen(wc)? My first thought was rand, but that has a hidden
surprise: the modulation obscures the underlying square-wave!
</p>
<img class="indented" src="pix/randfm.png" alt="rand(rand) spectrum">
<!-- randfm.png:
(with-sound (:channels 3)
(let* ((car1 (make-rand 1000))
(car2 (make-rand 1000))
(mod1 (make-rand 100))
(samps (seconds->samples 1.0))
(ampf (make-env '(0 0 1 1 20 1 21 0) :duration 1.0 :scaler .8))
(index (hz->radians (* 100 3.0))))
(do ((i 0 (+ i 1)))
((= i samps))
(let ((amp (env ampf))
(fm (* index (rand mod1))))
(outa i fm)
(outb i (* amp (rand car2)))
(outc i (* amp (rand car1 fm)))))))
(set! *selected-graph-color* (make-color 1 1 1))
(set! *selected-data-color* (make-color 0 0 0))
(set! (x-axis-label 0 0 1) "100Hz rand modulating signal spectrum")
(set! (x-axis-label 0 1 1) "1000Hz rand, no modulation")
(set! (x-axis-label 0 2 1) "1000Hz rand, 100Hz modulation (from chan 0), index: 3")
-->
<!--
compare ncos as FM and direct sum of cos:
(with-sound (:channels 2)
(for-each
(lambda (arg)
(let* ((beg (car arg))
(n (cadr arg))
(car1 (make-oscil 1000))
(mod1 (make-sum-of-cosines n 100 (random pi)))
(start (seconds->samples beg))
(dur 1.0)
(samps (seconds->samples dur))
(stop (+ start samps))
(ampf (make-env '(0 0 1 1 20 1 21 0) :duration dur :scaler .8))
(index (hz->radians (* 100 3.0)))
(car2 (make-oscil 1000))
(mods (make-vector n)))
(do ((i 0 (+ i 1)))
((= i n))
(set! (mods i) (make-oscil (* (+ i 1) 100) (* 0.5 pi))))
(do ((i start (+ i 1)))
((= i stop))
(let ((amp (env ampf)))
(outa i (* amp (oscil car1 (* index (sum-of-cosines mod1)))))
(let ((sum 0.0))
(do ((k 0 (+ k 1)))
((= k n))
(set! sum (+ sum (oscil (mods k)))))
(outb i (* amp (oscil car2 (* (/ index n) sum)))))))))
(list
(list 0.0 1)
(list 2.0 2)
(list 4.0 4)
(list 6.0 8)
(list 8.0 16)
(list 10.0 32)
(list 12.0 64)
(list 14.0 128))))
-->
<p>What FM input (to oscil, for a given index) would give the most dispersed output? My first guess was square-wave, but looking at graphs,
I'd say rand gives it a good contest.
If you sweep ncos upwards in frequency, you'll eventually
get foldover; the generator produces its preset number of cosines no
matter what. It is possible to vary the spectrum smoothly:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> ()
(let ((os (<em class=red>make-ncos</em> 100.0 4))
(pow (<a class=quiet href="#make-env">make-env</a> '(0 1.0 1 30.0) :length 10000))) ; our "index" envelope in FM jargon
(do ((i 0 (+ i 1)))
((= i 10000))
(let ((val (<em class=red>ncos</em> os)))
(<a class=quiet href="#outa">outa</a> i (* (signum val) ; signum is in dsp.scm
(expt (abs val) (<a class=quiet href="#env">env</a> pow))))))))
</pre>
<p>This is not a very polite sound. The same trick works on all the <a href="#ncos2">pulse-train</a> functions in generators.scm (or an oscil for that matter!),
but perhaps a filter is a simpler approach. There are a lot more of these "kernels" in <a href="#othergenerators">generators.scm</a>.
</p>
<table class="method">
<tr>
<td class="methodtitle">ncos2 (Fejer, n=10)</td>
<td class="methodtitle">npcos (Poussin, n=5)</td>
<td class="methodtitle">ncos4 (Jackson, n=10)</td>
</tr><tr>
<td class="br"><img src="pix/fejer.png" alt="fejer sum"></td>
<td class="br"><img src="pix/poussin.png" alt="poussin sum"></td>
<td class="br"><img src="pix/jackson.png" alt="jackson sum"></td>
</tr></table>
<p>nsin produces a sum of equal amplitude sines. It is very similar (good and bad) to <a href="#ncos">ncos</a>.
For n greater than 10 or so, its peak amplitude occurs at approximately 3pi/4n, and is about .7245*n (that is, 8n*(sin^2(3pi/8))/3pi).
The nsin generator scales its output to be between -1 and 1 for any n. We can use nxysin to try any initial-phase in a
sum of equal sinusoids. The peak amp in this case varys sinusoidally from a sum of sines n * 0.7245 to a sum of cosines n * 1.0;
the peak amp is nsin-max(n) + abs(sin(initial-phase))*(1 - nsin-max(n)). nsin is based on the conjugate Dirichlet kernel:
</p>
<!-- LATEX: \sum_{k=1}^{n}\sin kx = \frac{\sin\frac{n+1}{2}x \: \sin\frac{nx}{2}}{\sin\frac{x}{2}} -->
<img class="indented" src="pix/sceq1.png" alt="sum of sines">
<pre>
</pre>
<img class="indented" src="pix/sos.png" alt="sum of sines graphs">
<pre>
</pre>
<table class="method">
<tr><td colspan=2 class="methodtitle">nsin methods</td></tr>
<tr><td class="inner"><em class=gen>mus-frequency</em></td><td class="inner">frequency in Hz</td></tr>
<tr><td class="inner"><em class=gen>mus-phase</em></td><td class="inner">phase in radians</td></tr>
<tr><td class="inner"><em class=gen>mus-scaler</em></td><td class="inner">dependent on number of sines</td></tr>
<tr><td class="inner"><em class=gen>mus-length</em></td><td class="inner">n or sines arg used in make-<gen></td></tr>
<tr><td class="inner"><em class=gen>mus-increment</em></td><td class="inner">frequency in radians per sample</td></tr>
</table>
<p>
As with all the paired cos/sin generators (waveshaping, generators.scm, etc), we can vary
the initial phase by taking advantage of the trig identity:
</p>
<img class="indented" src="pix/fmeq18.png" alt="sin split">
<p>that is,
</p>
<pre class="indented">
(+ (* (ncos nc) (sin initial-phase))
(* (nsin ns) (cos initial-phase)))
</pre>
<p>Or vary it via an envelope at run-time:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> ()
(let ((nc (<em class=red>make-ncos</em> 500.0 6))
(ns (<em class=red>make-nsin</em> 500.0 6))
(phase (<a class=quiet href="#make-env">make-env</a> '(0 0 1 1)
:length 1000 :scaler (/ pi 2))))
(do ((i 0 (+ i 1)))
((= i 1000))
(let ((angle (<a class=quiet href="#env">env</a> phase)))
(<a class=quiet href="#outa">outa</a> i (+ (* (<em class=red>ncos</em> nc) (sin angle))
(* (<em class=red>nsin</em> ns) (cos angle))))))))
</pre>
<img class="indented" src="pix/varyphase.png" alt="ncos+nsin example">
<p>Compared to ncos or nsin, polywave is probably always faster and more accurate, but less convenient to set up.
Both ncos and nsin could be implemented as polynomials in cos x, just as in polyshape; in fact, ncos is
almost the same as the Chebyshev polynomial of the fourth kind.
See also the <a href="#nrxydoc">nrxycos</a> generator,
and <a href="#othergenerators">generators.scm</a>.
</p>
<!-- LATEX:
sceq14:
\small
\begin{align*}
&\sum_{k=0}^{n}\sin(x+ky)=\frac{\sin\Big(x+\frac{n-1}{2}y\Big)\sin\frac{ny}{2}}{\sin\frac{y}{2}} \\
&\sum_{k=0}^{n}\cos(x+ky)=\frac{\cos\Big(x+\frac{n-1}{2}y\Big)\sin\frac{ny}{2}}{\sin\frac{y}{2}} \\
&\sum_{k=0}^{2n-1}(-1)^{k}\cos(x+ky)=\frac{\sin\Big(x+\frac{2n-1}{2}y\Big)\sin ny}{\cos\frac{y}{2}} \\
&\sum_{k=0}^{n-1}(-1)^{k}\sin(x+ky)=\frac{\sin\Big(x+\frac{n-1}{2}(y+\pi)\Big)\sin\frac{n(y+\pi)}{2}}{\cos\frac{y}{2}} \\
&\sum_{k=1}^{n}\sin(2k-1)x=\frac{\sin^{2}nx}{\sin x} \\
&\sum_{k=1}^{n}\cos(2k-1)x=\frac{1}{2}\frac{\sin 2nx}{\sin x} \\
&\sum_{k=1}^{n}(-1)^{k}\cos kx=-\frac{1}{2}+\frac{(-1)^{n}\cos(\frac{2n+1}{2}x)}{2\cos\frac{x}{2}} \\
&\sum_{k=1}^{n}(-1)^{k+1}\sin(2k-1)x = (-1)^{n+1}\frac{\sin 2nx}{2\cos x} \\
&\sum_{k=1}^{n-1}k\sin kx=\frac{\sin nx}{4 \sin^{2}\frac{x}{2}} - \frac{n \cos \frac{2n-1}{2}x}{2\sin\frac{x}{2}} \\
&\sum_{k=1}^{n-1}k\cos kx=\frac{n\sin\frac{2n-1}{2}x}{2\sin\frac{x}{2}} - \frac{1-\cos nx}{4\sin^{2}\frac{x}{2}} \\
&2^{1-n} \sum_{0}^{\lfloor n/2 \rfloor} \binom{n}{k} \cos (n-2k)\theta = \cos^{n}\theta \\
sceq15:
&\sum_{k=1}^{n-1}p^{k}\sin kx=\frac{p\sin x - p^{n}\sin nx + p^{n+1}\sin(n-1)x}{1-2p\cos x+p^{2}} \\
&\sum_{k=0}^{n-1}p^{k}\cos kx=\frac{1-p\cos x - p^{n}\cos nx + p^{n+1}\cos(n-1)x}{1-2p\cos x + p^{2}} \\
&\sum_{k=1}^{\infty}p^{k}\sin kx=\frac{p\sin x}{1-2p\cos x + p^{2}} \\
&\sum_{k=0}^{\infty}p^{k}\cos kx=\frac{1-p\cos x}{1-2p\cos x + p^{2}} \\
&\sum_{k=1}^{\infty}\frac{p^{k}\sin kx}{k} = \arctan \frac{p\sin x}{1-p\cos x} \\
&\sum_{k=1}^{\infty}\frac{p^{k}\cos kx}{k} = \ln \frac{1}{\sqrt{1-2p\cos x + p^{2}}} \\
&\sum_{k=1}^{\infty}\frac{p^{2k-1}\sin(2k-1)x}{2k-1} = \frac{1}{2}\arctan \frac{2p\sin x}{1-p^{2}} \\
&\sum_{k=1}^{\infty}\frac{p^{2k-1}\cos(2k-1)x}{2k-1} = \frac{1}{4}\ln \frac{1+2p\cos x + p^{2}}{1-2p\cos x + p^{2}} \\
&\sum_{k=1}^{\infty}e^{-kt}\sin kx = \frac{1}{2}\frac{\sin x}{\cosh t - \cos x} \\
&1 + 2\sum_{k=1}^{\infty}e^{-kt}\cos kx = \frac{\sinh t}{\cosh t - \cos x} \\
&\sum_{k=0}^{\infty} \frac{(2n+2k)(2n+k-1)!}{k!}J_{2n+2k}(2z \sin \theta) = (z \sin \theta)^{2n} \\
&J_{0}^{2}\Big(\frac{z}{2}\Big)+2\sum_{k=1}^{\infty} J_{k}^{2}\Big(\frac{z}{2}\Big)\cos2k\theta = J_{0}(z \sin \theta) \\
\end{align*}
old in second col (replaced by JO cases):
&\sum_{k=1}^{\infty}\frac{p^{k}\sin kx}{k!} = e^{p\cos x}\sin(p\sin x) \\
&\sum_{k=0}^{\infty}\frac{p^{k}\cos kx}{k!} = e^{p\cos x}\cos(p\sin x) \\
rk!sin and cos
original last of first col:
&\sum_{k=1}^{\infty} \frac{\sin^{2k} x}{k} = -2 \ln \cos x & (x^{2} < \frac{pi^{2}}{4}) \\
sceq20:
\begin{align*}
&\sum_{k=0}^{\infty} p^{k} \cos(x + ky) = \frac{\cos x - p \cos(x - y)}{1 - 2p\cos y + p^{2}} \\
&\sum_{k=0}^{\infty} p^{k} \sin(x + ky) = \frac{\sin x - p \sin(x - y)}{1 - 2p\cos y + p^{2}} \\
&\sum_{k=1}^{\infty} \frac{\sin kx}{2^{k-1}} = \frac{4\sin x}{5 - 4\cos x} \\
&\sum_{k=1}^{\infty} \frac{(-1)^{k} e^{(2k-1)a} \cos(2k-1)x}{2k-1} = \frac{1}{2}\arctan \bigg(\frac{\cos x}{\sinh a}\bigg) \\
&\sum_{k=1}^{\infty} \frac{\sin nx}{n(n^{2}-4)} = -\frac{\pi}{8}\sin^{2}x \\
\end{align*}
sceq21:
\begin{align*}
&\sum_{k=0}^{\infty} \frac{a^{k}}{k!} \cos(x + ky) = e^{a \cos y} \cos (x + a \sin y) \\
&\sum_{k=0}^{\infty} \frac{a^{k}}{k!} \sin(x + ky) = e^{a \cos y} \sin (x + a \sin y) \\
&\sum_{k=0}^{\infty} \frac{a^{2k}\cos 2kx}{(2k)!} = \cosh(a \cos x) \cos (a \sin x) & (a^{2}<1) \\
&\sum_{k=1}^{\infty} \frac{a^{2k}\sin 2kx}{(2k)!} = \sinh(a \cos x) \sin (a \sin x) & (a^{2}<1) \\
&\frac{1}{a} + 2a \sum_{k=1}^{\infty} \frac{\cos kx}{a^{2}+k^{2}} = \pi \frac{\cosh a(\pi-x)}{\sinh a\pi} & (0 \leq x \leq 2\pi)\\
\end{align*}
sceq23 (old):
& \ln (1 - 2xt + t^{2})^{-1} = 2 \sum_{n=1}^{\infty} \frac{t^{n}}{n} T_{n}(x) \\
& (1 - 2xt + t^{2})^{-1} = \frac{1}{\sqrt{1 - x^{2}}} \sum_{n=0}^{\infty} t^{n} U_{n+1}(x) \\
& e^{z cos x}J_{\nu-\frac{1}{2}}(z \sin x) = \frac{\Gamma(\nu)}{\Gamma(\frac{1}{2})}(2 \sin x)^{\nu-\frac{1}{2}} \sum_{k=0}^{\infty}\frac{z^{\nu+k-\frac{1}{2}}}{\Gamma(2\nu + k)} \mathrm{C}^{\nu}_{k}(\cos x)
sceq25 old:
\sum_{n=0}^{\infty} r^{n} \sum_{k=0}^{\infty} \sin (k+1/2)\theta = \frac{(1+r)\sin(\theta / 2)}{(1-r)(1-2r\cos\theta + r^{2})}
sceq26 old:
\sum_{n=0}^{\infty} r^{n} \sum_{k=0}^{\infty} (n+1-k)\sin (k+1)\theta = \frac{\sin\theta}{(1-r)^{2}(1-2r\cos\theta + r^{2})}
sceq7: (p73)
&\sum_{k=1}^{\infty} \frac{\cos kx}{k} = -\ln \big(2 \sin \frac{x}{2}\big) & (0\leq x \leq \pi) \\
sceq27: (p183)
1 + 2 &\sum_{n=1}^{\infty} \frac{(-a + \sqrt{a^{2} - b^{2}})^{n} \cos nx}{b^{n}} = \frac{\sqrt{a^{2} - b^{2}}}{a + b \cos x} & (b < a, a \neq 0) \\
sceq25:
&\sum_{k=1}^{\infty} k r^{k} \sin kx = \frac{r(1-r^{2})\sin x}{(1 - 2r\cos x + r^{2})^{2}}
Z 352
sceq26:
original: & \frac{8}{\pi} \sum_{k=1}^{\infty} \frac{\sin^{2}kx}{4k^{2}-1} = |\sin x|
& \frac{8}{\pi} \sum_{k=1}^{\infty} \frac{\sin^{2}kx}{4k^{2}-1} = | \sin x \, | = \frac{2}{\pi} - \frac{8}{\pi} \sum_{k=1}^{\infty} \frac{\cos 2kx}{4k^{2} - 1}
sceq31:
J_{0}(ka)J_{0}(kr) + 2 \sum_{m=1}^{\infty} J_{m}(kr)J_{m}(ka) \cos m\theta = J_{0}\Big(k \sqrt{r^{2}+a^{2}-2ar\cos \theta}\Big)
sceq32 old form (redundant):
1 + 2 \sum_{n=1}^{\infty} J_{n}(nz)\cos n(x-z\sin x) = \frac{1}{1-z\cos x}
sceq32 new form:
&2^{n} \Gamma(n) \sum_{m=0}^{\infty} (n+m)\frac{J_{n+m}(r)}{r^{n}} \frac{J_{n+m}(a)}{a^{n}} C_{m}^{n}(\cos \theta) = \frac{J_{n}\big(\sqrt{r^{2}+a^{2}-2ar\cos \theta}\big)}{\big(\sqrt{r^{2}+a^{2}-2ar\cos \theta}\big)^{n}} \\
sceq33:
\sum_{m=0}^{\infty}(m+\frac{1}{2})\frac{J_{m+\frac{1}{2}}(ka)}{\sqrt{a}} \frac{J_{m+\frac{1}{2}}(kr)}{\sqrt{r}} P_{m}(\cos \theta) = \frac{\sin\Big(k \sqrt{r^{2}+a^{2}-2ar\cos \theta}\Big)}{\pi \sqrt{r^{2}+a^{2}-2ar\cos \theta}}
Pn(cos x) is p776 A&S (P=Jacobi)
sceq34:
old form: \sum_{m=0}^{\infty} (n+m)\Big(\frac{J_{n+m}(z)}{z^{n}}\Big)^{2} C_{m}^{n}(\cos \theta) = \frac{J_{n}(2z \sin \frac{\theta}{2})}{2^{n}\Gamma(n)(2z\sin \frac{\theta}{2})}
\sum_{m=0}^{\infty} (n+m)\Big(\frac{J_{n+m}(z)}{z^{n}}\Big)^{2} \sum_{k=0}^{m} \frac{(n)_{k}(n)_{m-k}}{k!(m-k)!} \cos(m-2k)\theta = \frac{J_{n}(2z \sin \frac{\theta}{2})}{2^{n}\Gamma(n)(2z\sin \frac{\theta}{2})}
from Gray and Mathews, "A Treatise on Bessel Functions and Their Applications to Physics" p 28, 29, 92, 240
sceq36:
& J_{0}^{2}\Big(\frac{z}{2}\Big)+2\sum_{k=1}^{\infty} (-1)^{k} J_{k}^{2}\Big(\frac{z}{2}\Big)\cos2kx = J_{0}(z \cos x) \\
& 2\sum_{k=0}^{\infty} (-1)^{k} J_{k}\Big(\frac{z}{2}\Big)J_{k+1}\Big(\frac{z}{2}\Big)\cos (2k+1)x = J_{1}(z \cos x) \\
sceq37:
& Y_{0}(b)J_{0}(c) + 2 \sum_{m=1}^{\infty} Y_{m}(b)J_{m}(c) \cos m\theta = Y_{0}\Big(\sqrt{b^{2}+c^{2}-2bc\cos \theta}\Big) & (b > c)\\
& \sum_{n=0}^{\infty} \frac{r^{n}}{n!}P_{n}(\cos \theta) = e^{r \cos \theta}J_{0}(r \sin \theta) \\
from Abramowitz and Stegun, "Handbook of Mathematical Functions"
9.6.34 (p376) (9.1.42 is the FM formula)
sceq39:
I_{0}(z) + 2 \sum_{k=1}^{\infty} I_{k}(z) \cos k\theta = e^{z \cos \theta}
sceq40: 27.8.6 (p1005)
&\sum_{n=1}^{\infty} \frac{\sin n\theta}{n^{3}} = \frac{\pi^{2}\theta}{6} - \frac{\pi \theta^{2}}{4} + \frac{\theta^{3}}{12} & (0 \leq \theta \leq 2\pi)
from Askey "Ramanujan and Hypergeometric Series" in Berndt and Rankin "Ramanujan: Essays and Surveys" p283 (the formula was found by Gauss):
this is r2k!cos in generators.scm [sceq30.png]
&(1 - 2r \cos \theta + r^{2})^{-k} = \frac{{}_{2}F_{1}(k, k; 1; r^{2})}{2} + \sum_{n=1}^{\infty} \frac{(k)_{n}}{n!} r^{n} {}_{2}F_{1}(k, k+n; n+1; r^{2}) \cos n\theta
Montgomery and Vorhauer:
&\sum_{n=1}^{\infty} \frac{\sin \frac{k \pi}{n+1}}{\sin \frac{\pi}{n+1}} \cos k\theta = \frac{(\cos \frac{1}{2}(n+1)\theta)^{2}}{\cos \theta - \cos \frac{\pi}{n+1}}
;;; Bessel funcs as confluent hypergeometric series
&J_{n}(x) = \frac{(\frac{x}{2})^{n}}{\Gamma(n+1)} e^{-ix} \Phi(n+\frac{1}{2}, 2n+1; 2ix) \\
&I_{n}(x) = \frac{(\frac{x}{2})^{n}}{\Gamma(n+1)} e^{-x} \Phi(n+\frac{1}{2}, 2n+1; 2x) \\
from Klapper, Selected Papers on Frequency Modulation, p 156
&\sum_{n=-\infty}^{\infty} (-1)^{n} J_{2n}(x) J_{2n}(y(\omega + B \cos z)) = J_{0}( \sqrt{x^{2} + y^{2} (\omega + B\cos z)^{2}})
another kernel set: binomial coeffs G&R 1.320 etc, not different enough
-->
<table class="grayborder">
<tr><td colspan=2 class="methodtitle">various sums</td></tr>
<tr>
<td>
<img class="noborder" src="pix/sceq14.png" alt="many sums" usemap="#GR1">
<map name="GR1">
<area shape=rect coords="0,0,300,60" href="#nxysin" alt="nxysin">
<area shape=rect coords="0,61,300,100" href="#nxycos" alt="nxycos">
<area shape=rect coords="0,101,300,150" href="#nxy1cos" alt="nxy1cos">
<area shape=rect coords="0,151,300,200" href="#nxy1sin" alt="nxy1sin">
<area shape=rect coords="0,201,300,240" href="#noddsin" alt="noddsin">
<area shape=rect coords="0,241,300,280" href="#noddcos" alt="noddcos">
<area shape=rect coords="0,361,300,410" href="#nkssb" alt="nkssb">
<area shape=rect coords="0,411,300,450" href="#nkssb" alt="nkssb">
<area shape=rect coords="0,450,300,485" href="#nchoosekcos" alt="nchoosekcos">
</map>
</td>
<td>
<img class="noborder" src="pix/sceq15.png" alt="many more sums" usemap="#GR2">
<map name="GR2">
<area shape=rect coords="0,0,300,40" href="#nrsin" alt="nrsin">
<area shape=rect coords="0,41,300,90" href="#nrcos" alt="nrcos">
<area shape=rect coords="0,91,300,140" href="#rssb" alt="rssb">
<area shape=rect coords="0,141,300,180" href="#rcos" alt="rcos">
<area shape=rect coords="0,181,300,220" href="#rksin" alt="rksin">
<area shape=rect coords="0,221,300,250" href="#rkcos" alt="rkcos">
<area shape=rect coords="0,251,300,300" href="#rkoddssb" alt="rkoddssb">
<area shape=rect coords="0,301,300,340" href="#rkoddssb" alt="rkoddssb">
<area shape=rect coords="0,341,300,380" href="#erssb" alt="erssb">
<area shape=rect coords="0,381,300,420" href="#ercos" alt="ercos">
<area shape=rect coords="0,465,300,490" href="#j0evencos" alt="j0evencos">
</map>
</td>
</tr>
<tr><td colspan=2 class="sumtitle">Gradshteyn and Ryzhik, "Table of Integrals, Series, and Products", 1.341.., 1.352.., 1.447.., 1.461, 1.518, 8.516, 8.531</td></tr>
<tr><td class="hightop">
<img class="noborder" src="pix/sceq20.png" alt="more sums" usemap="#J1">
<map name="J1">
<area shape=rect coords="0,0,300,30" href="#rxycos" alt="rxycos">
<area shape=rect coords="0,31,300,60" href="#rxysin" alt="rxysin">
<area shape=rect coords="0,61,300,100" href="#k2sin" alt="k2sin">
<area shape=rect coords="0,101,300,140" href="#eoddcos" alt="eoddcos">
</map>
</td>
<td class="hightop"><img class="noborder" src="pix/sceq21.png" alt="more sums" usemap="#J2">
<map name="J2">
<area shape=rect coords="0,0,300,30" href="#rxyk!cos" alt="rxyk!cos">
<area shape=rect coords="0,31,300,60" href="#rxyk!cos" alt="rxyk!sin">
</map>
</td></tr>
<tr><td colspan=2 class="sumtitle">Jolley, "Summation of Series", 521 587 623 635 638 685 686 691 692 728</td></tr>
<tr><td class="hightop"><a class=invisible href="#izcos"><img src="pix/sceq39.png" alt="I(k) sum"></a></td>
<td class="hightop"><a class=invisible href="#k3sin"><img src="pix/sceq40.png" alt="n3 case"></a></td></tr>
<tr><td colspan=2 class="sumtitle">Abramowitz and Stegun, "Handbook of Mathematical Functions", 9.6.34, 27.8.6</td></tr>
<tr><td class="hightop"><a class=invisible href="#krksin"><img src="pix/sceq25.png" alt="more sums"></a></td>
<td class="hightop"><img src="pix/sceq26.png" alt="more sums"></td></tr>
<tr><td colspan=2 class="sumtitle">Zygmund, "Trigonometric Series" p34, 352</td></tr>
<tr><td class="hightop"><img src="pix/sceq7.png" alt="more sums"></td>
<td class="hightop"><a class=invisible href="#abcos"><img src="pix/sceq27.png" alt="more sums"></a></td></tr>
<tr><td colspan=2 class="sumtitle">Sansone, "Orthogonal Functions"</td></tr>
<tr><td class="hightop"><img class="noborder" src="pix/sceq36.png" alt="more sums" usemap="#GM1">
<map name="GM1">
<area shape=rect coords="0,0,300,50" href="#j0j1cos" alt="j0j1cos">
<area shape=rect coords="0,51,300,80" href="#j0j1cos" alt="j0j1cos">
</map>
</td>
<td class="hightop"><img class="noborder" src="pix/sceq37.png" alt="more sums" usemap="#GM2">
<map name="GM2">
<area shape=rect coords="0,0,300,50" href="#jycos" alt="jycos">
</map>
</td></tr>
<tr><td colspan=2 class="sumtitle">Gray and Mathews, "A Treatise on Bessel Functions and Their Applications to Physics" p 28, 29, 92, 240</td></tr>
<tr>
<td class="hightop"><a class=invisible href="#jncos"><img src="pix/sceq32.png" alt="more sums"></a></td>
<td class="hightop"><a class=invisible href="#jjcos"><img src="pix/sceq31.png" alt="more sums"></a></td>
</tr><tr>
<td><a class=invisible href="#j2cos"><img src="pix/sceq34.png" alt="more sums"></a></td>
<td><a class=invisible href="#jpcos"><img src="pix/sceq33.png" alt="more sums"></a></td>
</tr><tr>
<td colspan=2 class="sumtitle">Watson, "A Treatise on the Theory of Bessel Functions": 4.82, 11.41, 17.31</td></tr>
<tr><td class="hightop"><a class=invisible href="#r2k!cos"><img src="pix/sceq30.png" alt="kosines"></a></td>
<td class="hightop">
<a class=invisible href="#n1cos"><img src="pix/sceq45.png" alt="linear cosines"></a>
</td>
</tr>
<tr><td class="sumtitle">Askey, "Ramanujan and Hypergeometric Series"</td>
<td class="sumtitle">Ramanujan, "On certain Arithmetical Functions"</td>
</tr>
</table>
<!-- LATEX
sceq38:
\sum_{k=-\infty}^{\infty} J_{k}(m\rho) Z_{\nu+k}(mr) e^{ik\phi} = e^{i \nu \psi}Z_{\nu}(mR)
-->
<!-- LATEX
sceq45.png
& 2n-1 + 4\sum_{k=1}^{n-1} (n-k)\cos k\theta + \cos n\theta = \cot^{2}\frac{\theta}{2}\ (1 - \cos n\theta) \\
-->
<p>
There are many formulas that produce exponentially decaying or bell-curve shaped spectra;
I think these all sound about the same, so I have included only a representative sample of them.
A couple of the formulas are special cases of the "Bessel function summation theorem", G&R 8.530:
<img src="pix/sceq38.png" alt="summation formula">,
where Z stands for any of the various Bessel functions (J, Y, etc),
and R stands for the Poisson-like business (or is it Legendre?) in the square root.
Most of the formulas above are implemented as generators in <a href="#othergenerators">generators.scm</a>,
along with the single side-band cases, where possible.
Don't shy away from the sums to infinity just because you've heard shouting about "band-limited waveforms" — FM is an infinite sum:
</p>
<!-- LATEX:
\small
\begin{align*}
& \cos(B \sin x) = J_{0}(B) + 2 \sum_{k=1}^{\infty} J_{2k}(B) \cos 2kx \\
& \cos(B \cos x) = J_{0}(B) + 2 \sum_{k=1}^{\infty} (-1)^{k} J_{2k}(B) \cos 2kx \\
& \sin(B \sin x) = 2 \sum_{k=0}^{\infty} J_{2k+1}(B) \sin (2k+1)x \\
& \sin(B \cos x) = 2 \sum_{k=0}^{\infty} (-1)^{k} J_{2k+1}(B) \cos (2k+1)x \\
\end{align*}
-->
<img src="pix/fmeq49.png" alt="cos cos cases">
<img src="pix/fmeq50.png" alt="cos cos cases">
<br>
<div class="inset_inline">(Is cos(sin(x)) always greater than sin(cos(x))?)</div>
<!-- NRXYSIN and NRXYCOS -->
<div class="innerheader" id="nrxydoc">nrxysin and nrxycos</div>
<pre class="indented">
<em class=def id="make-nrxysin">make-nrxysin</em>
(frequency 0.0)
(ratio 1.0) ; ratio between frequency and the spacing between successive sidebands
(n 1) ; number of sidebands
(r .5) ; amplitude ratio between successive sidebands (-1.0 < r < 1.0)
<em class=def id="nrxysin">nrxysin</em> s (fm 0.0)
<em class=def id="nrxysin?">nrxysin?</em> s
<em class=def id="make-nrxycos">make-nrxycos</em> (frequency 0.0) (ratio 1.0) (n 1) (r .5)
<em class=def id="nrxycos">nrxycos</em> s (fm 0.0)
<em class=def id="nrxycos?">nrxycos?</em> s
</pre>
<br>
<table class="method">
<tr><td colspan=2 class="methodtitle">nrxysin methods</td></tr>
<tr><td class="inner"><em class=gen>mus-frequency</em></td><td class="inner">frequency in Hz</td></tr>
<tr><td class="inner"><em class=gen>mus-phase</em></td><td class="inner">phase in radians</td></tr>
<tr><td class="inner"><em class=gen>mus-scaler</em></td><td class="inner">"r" parameter; sideband scaler</td></tr>
<tr><td class="inner"><em class=gen>mus-length</em></td><td class="inner">"n" parameter</td></tr>
<tr><td class="inner"><em class=gen>mus-increment</em></td><td class="inner">frequency in radians per sample</td></tr>
<tr><td class="inner"><em class=gen>mus-offset</em></td><td class="inner">"ratio" parameter</td></tr>
</table>
<p>These three generators
produce a kind of additive synthesis.
"n" is the number of sidebands (0 gives a sine wave), "r" is the amplitude
ratio between successive sidebands (don't set it to 1.0), and "ratio" is the ratio between the
carrier frequency and the spacing between successive sidebands.
A "ratio" of 2 gives odd-numbered harmonics for a (vaguely) clarinet-like sound.
A negative ratio puts the side-bands below the carrier.
A negative r is the same as shifting the initial phase by pi (instead of lining up
for the spike at multiples of 2*pi, the (-1)^n causes them to line up at (2k-1)*pi,
but the waveform is the same otherwise).
The basic idea is very similar to that used in the
<a href="#ncos">ncos</a> generator, but you have control of the
fall-off of the spectrum and the spacing of the partials.
Here are the underlying formulas:
</p>
<!-- sinesummation.png:
(with-sound (:clipped #f)
(let ((gen (make-sine-summation 400.0 0.0 5 0.5)))
(do ((i 0 (+ i 1)))
((= i 30000))
(outa i (sine-summation gen 0.0)))))
-->
<!-- Jolley 475 is different [it runs 0 .. n-1 in our terminology, whereas Moorer's runs 0 .. n] -->
<!-- LATEX:
& \sum_{k=0}^{n} r^{k}\sin(x+ky) = \frac{\sin(x) - r\sin(x-y) - r^{n+1}\Big(\sin(x+(n+1)y) - r\sin(x+ny)\Big)}{1+r^{2}-2r\cos(y)} \\
& \sum_{k=0}^{n} r^{k}\cos(x+ky) = \frac{\cos(x) - r\cos(x-y) - r^{n+1}\Big(\cos(x+(n+1)y) - r\cos(x+ny)\Big)}{1+r^{2}-2r\cos(y)} \\
-->
<img src="pix/sceq8.png" alt="nxry formulas">
<table class="method">
<tr><td>
<img src="pix/sinesummation.png" alt="nxry formula">
</td></tr>
<tr><td class="center">nrxysin, n=5, r=0.5
</td></tr>
</table>
<table>
<tr>
<td>
<div class="scheme">
<pre class="indented">
(with-sound (:play #t)
(let ((gen (make-nrxycos 440.0 :n 10)))
(do ((i 0 (+ i 1)))
((= i 44100))
(outa i (* 0.5 (nrxycos gen))))))
</pre>
</div>
</td></tr>
<tr>
<td>
<div class="ruby">
<pre class="indented">
with_sound(:play, true) do
gen = make_nrxycos(440.0, 1.0, 10, 0.5);
44100.times do |i|
outa(i, 0.5 * nrxycos(gen), $output)
end
end.output
</pre>
</div>
</td></tr>
<tr>
<td>
<div class="forth">
<pre class="indented">
lambda: ( -- )
440.0 :n 10 make-nrxycos { gen }
44100 0 ?do
i gen 0 nrxycos f2/ *output* outa drop
loop
; :play #t with-sound drop
</pre>
</div>
</td>
</tr>
</table>
<p>The peak amplitude of nrxysin is hard to predict.
I think nrxysin is close to the -1.0..1.0 ideal, and won't go over 1.0.
<a href="#nrxycos">nrxycos</a> is normalized correctly.
Besides the usual FM input, you can also vary the "r" parameter (via mus-scaler) to get changing spectra. In the
next example, we add a glissando envelope, and use the same envelope to vary "r" so that as the frequency
goes up, "r" goes down (to avoid foldover, or whatever).
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#definstrument">definstrument</a> (ss beg dur freq amp (n 1) (r .5) (ratio 1.0) frqf)
(let* ((st (<a class=quiet href="#secondstosamples">seconds->samples</a> beg))
(nd (+ st (<a class=quiet href="#secondstosamples">seconds->samples</a> dur)))
(sgen (<em class=red>make-nrxysin</em> freq ratio n r))
(frq-env (and frqf (<a class=quiet href="#make-env">make-env</a> frqf :scaler (<a class=quiet href="#hztoradians">hz->radians</a> freq) :duration dur)))
(spectr-env (and frqf (<a class=quiet href="#make-env">make-env</a> frqf :duration dur)))
(amp-env (<a class=quiet href="#make-env">make-env</a> '(0 0 1 1 2 1 3 0) :scaler amp :duration dur)))
(do ((i st (+ i 1)))
((= i nd))
(if spectr-env
(set! (<em class=red>mus-scaler</em> sgen) (* r (exp (- (<a class=quiet href="#env">env</a> spectr-env))))))
(<a class=quiet href="#outa">outa</a> i (* (<a class=quiet href="#env">env</a> amp-env)
(<em class=red>nrxysin</em> sgen (if frq-env (<a class=quiet href="#env">env</a> frq-env) 0.0)))))))
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> () (ss 0 1 400.0 1.0 5 0.5 1.0 '(0 0 1 2)))
</pre>
<p>"r" can also be used in the same way as an FM index, but with much simpler spectral evolution (x^n, x between -1.0 and 1.0, rather than Jn(x)).
In the graph, r is 0 at the midpoint, r goes from -1.0 to 1.0 along the horizontal axis — I forgot to label the axes.
</p>
<img class="indented" src="pix/nrxy-r.png" alt="nrxycos changing r">
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> ()
(let ((gen1 (<em class=red>make-nrxycos</em> 400 1 15 0.95))
(indr (<a class=quiet href="#make-env">make-env</a> '(0 -1 1 1)
:length 80000 :scaler 0.9999)))
(do ((i 0 (+ i 1)))
((= i 80000))
(set! (<em class=red>mus-scaler</em> gen1) (env indr)) ; this sets r
(<a class=quiet href="#outa">outa</a> i (* .5 (<em class=red>nrxycos</em> gen1 0.0))))))
</pre>
<!-- MLB's index idea is not so great:
(with-sound (:clipped #f :statistics #t)
(let* ((dur 2.0)
(ampf (make-env '(0 1 10 1 11 0) :duration dur :scaler .5))
(indf (make-env '(0 0 1 .999) :duration dur))
(samps (seconds->samples dur))
(N 60))
(do ((i 0 (+ i 1))
(th 0.0 (+ th (hz->radians 1000.0))))
((= i samps))
(let* ((a .999)
(index (env indf))
(a2 (+ 1.0 (* a a)))
(an (expt a (+ N 1)))
(b 1/10)
(B (* b th))
(thB (- th B))
(divisor (* (- a2 (* 2 a index (cos B)))
(/ (- (expt a N) 1.0)
(- a 1.0))))
(val (/ (- (sin th) (* a (sin thB))
(* an (- (sin (+ th (* (+ N 1) B)))
(* a (sin (+ th (* N B)))))))
divisor)))
(outa i (* (env ampf) val))))))
-->
<!-- SSB-AM -->
<div class="innerheader" id="ssb-amdoc">ssb-am</div>
<pre class="indented">
<em class=def id="make-ssb-am">make-ssb-am</em> (frequency 0.0) (order 40)
<em class=def id="ssb-am">ssb-am</em> gen (insig 0.0) (fm 0.0)
<em class=def id="ssb-am?">ssb-am?</em> gen
</pre>
<table class="method">
<tr><td colspan=2 class="methodtitle">ssb-am methods</td></tr>
<tr><td class="inner"><em class=gen>mus-frequency</em></td><td class="inner">frequency in Hz</td></tr>
<tr><td class="inner"><em class=gen>mus-phase</em></td><td class="inner">phase (of embedded sin osc) in radians</td></tr>
<tr><td class="inner"><em class=gen>mus-order</em></td><td class="inner">embedded delay line size</td></tr>
<tr><td class="inner"><em class=gen>mus-length</em></td><td class="inner">same as mus-order</td></tr>
<tr><td class="inner"><em class=gen>mus-interp-type</em></td><td class="inner"><code>mus-interp-none</code></td></tr>
<tr><td class="inner"><em class=gen>mus-xcoeff</em></td><td class="inner">FIR filter coeff</td></tr>
<tr><td class="inner"><em class=gen>mus-xcoeffs</em></td><td class="inner">embedded Hilbert transform FIR filter coeffs</td></tr>
<tr><td class="inner"><em class=gen>mus-data</em></td><td class="inner">embedded filter state</td></tr>
<tr><td class="inner"><em class=gen>mus-increment</em></td><td class="inner">frequency in radians per sample</td></tr>
</table>
<p>ssb-am provides single sideband suppressed carrier amplitude modulation, normally used for frequency shifting.
The basic notion is to shift a spectrum up or down while cancelling either the upper or lower half of the spectrum.
See <a href="sndscm.html#ssbbank">dsp.scm</a> for a number of curious possibilities (time stretch without pitch shift for example).
When this works, which it does more often than I expected, it is much better than the equivalent
phase-vocoder or granular synthesis kludges.
</p>
<table>
<tr>
<td>
<div class="scheme">
<pre class="indented">
(with-sound (:play #t :srate 44100)
(let ((shifter (make-ssb-am 440.0 20))
(osc (make-oscil 440.0)))
(do ((i 0 (+ i 1)))
((= i 44100))
(outa i (* 0.5 (ssb-am shifter (oscil osc)))))))
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="ruby">
<pre class="indented">
with_sound(:play, true, :srate, 44100) do
shifter = make_ssb_am(440.0, 20);
osc = make_oscil(440.0);
44100.times do |i|
outa(i, 0.5 * ssb_am(shifter, oscil(osc)), $output);
end
end.output
</pre>
</div>
</td></tr>
<tr>
<td>
<div class="forth">
<pre class="indented">
lambda: ( -- )
440.0 20 make-ssb-am { shifter }
440.0 make-oscil { osc }
44100 0 ?do
i shifter osc 0 0 oscil 0 ssb-am f2/ *output* outa drop
loop
; :play #t :srate 44100 with-sound drop
</pre>
</div>
</td>
</tr>
</table>
<pre class="indented">
(define* (ssb-am freq (order 40))
;; higher order = better cancellation
(let* ((car-freq (abs freq))
(cos-car (<a class=quiet href="#make-oscil">make-oscil</a> car-freq (* .5 pi)))
(sin-car (<a class=quiet href="#make-oscil">make-oscil</a> car-freq))
(dly (<a class=quiet href="#make-delay">make-delay</a> order))
(hlb (<a class=quiet href="sndscm.html#makehilberttransform">make-hilbert-transform</a> order)))
(<a class=quiet href="extsnd.html#mapchannel">map-channel</a>
(lambda (y)
(let ((ccos (<a class=quiet href="#oscil">oscil</a> cos-car))
(csin (<a class=quiet href="#oscil">oscil</a> sin-car))
(yh (<a class=quiet href="sndscm.html#hilberttransform">hilbert-transform</a> hlb y))
(yd (<a class=quiet href="#delay">delay</a> dly y)))
(if ((> freq 0.0) - +)
(* ccos yd)
(* csin yh)))))))
(<a class=quiet href="sndscm.html#definstrument">definstrument</a> (shift-pitch beg dur file freq (order 40))
(let* ((st (<a class=quiet href="#secondstosamples">seconds->samples</a> beg))
(nd (+ st (<a class=quiet href="#secondstosamples">seconds->samples</a> dur)))
(gen (<em class=red>make-ssb-am</em> freq order))
(rd (<a class=quiet href="#make-readin">make-readin</a> file)))
(do ((i st (+ i 1)))
((= i nd))
(<a class=quiet href="#outa">outa</a> i (<em class=red>ssb-am</em> gen (<a class=quiet href="#readin">readin</a> rd))))))
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> () (shift-pitch 0 3 "oboe.snd" 1108.0))
</pre>
<p>
Normal amplitude modulation, cos(x) * (amp + Y(t)), where Y is some signal,
produces the carrier (cos(x)), and symmetric sidebands at x+/-frq where frq is each spectral
component of Y. This is just an elaboration of
</p>
<pre class="indented">
cos(x) * (amp + cos(y)) = amp * cos(x) + 1/2(cos(x - y) + cos(x + y))
</pre>
<p>
So, the Y spectrum (the first picture below) is shifted up by cos(x) and mirrored on either side of it (the second picture below; the spectral components
on the left side are folding under 0). In single side-band
AM, we create both the Y spectrum, and, via the hilbert transform, a version of Y in which the phases are shifted too.
Then we can add these two copies, using the phase differences to cancel one side of the symmetric
spectrum (this is the third picture below; the new spectral components are not harmonically related however).
Once we can shift a pitch without creating its symmetric twin, we can split a spectrum
into many bands, shift each band separately, and thereby retain its original harmonic spacing (the fourth picture).
We have the original, but at a higher pitch. If we then use <a href="#src">src</a> to convert it back to
its pre-shift pitch, we have the original, but with a different length.
We have decoupled the pitch from the duration, much as in a phase vocoder (which uses an FFT
rather than a filter bank, and an inverse FFT of the moved spectrum, rather than ssb-am).
</p>
<table class="method">
<tr>
<td><img src="pix/orig-oboe.png" alt="unaltered oboe"></td>
<td><img src="pix/am.png" alt="am oboe"></td>
<td><img src="pix/ssbam.png" alt="ssbam oboe"></td>
<td><img src="pix/ssbambank.png" alt="ssbambank oboe"></td>
</tr>
<tr>
<td class="center">original</td>
<td class="center">amplitude modulation</td>
<td class="center">ssb-am</td>
<td class="center">ssb-am bank</td>
</tr>
</table>
<p>The second picture was created from oboe.snd (the original) via:
</p>
<pre class="indented">
(let ((osc (<a class=quiet href="#make-oscil">make-oscil</a> 1000.0)))
(<a class=quiet href="extsnd.html#mapchannel">map-channel</a>
(lambda (y)
(* .5 (<a class=quiet href="#amplitude-modulate">amplitude-modulate</a> .01 (<a class=quiet href="#oscil">oscil</a> osc) y)))))
</pre>
<p>The third picture was created by:
</p>
<pre class="indented">
(let ((am (<em class=red>make-ssb-am</em> 1000 40)))
(<a class=quiet href="extsnd.html#mapchannel">map-channel</a>
(lambda (y)
(<em class=red>ssb-am</em> am y))))
</pre>
<p>
And the fourth used the ssb-am-bank function in dsp.scm rewritten here for with-sound:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#definstrument">definstrument</a> (repitch beg dur sound old-freq new-freq
(amp 1.0) (pairs 10) (order 40) (bw 50.0))
(let* ((start (<a class=quiet href="#secondstosamples">seconds->samples</a> beg))
(end (+ start (<a class=quiet href="#secondstosamples">seconds->samples</a> dur)))
(ssbs (make-vector pairs))
(bands (make-vector pairs))
(factor (/ (- new-freq old-freq) old-freq))
(rd (<a class=quiet href="#make-readin">make-readin</a> sound)))
(do ((i 1 (+ i 1)))
((> i pairs))
(let ((aff (* i old-freq))
(bwf (* bw (+ 1.0 (/ i 2 pairs)))))
(set! (ssbs (- i 1)) (<em class=red>make-ssb-am</em> (* i factor old-freq)))
(set! (bands (- i 1)) (<a class=quiet href="sndscm.html#makebandpass">make-bandpass</a> (<a class=quiet href="#hztoradians">hz->radians</a> (- aff bwf)) ; bandpass is in dsp.scm
(<a class=quiet href="#hztoradians">hz->radians</a> (+ aff bwf))
order))))
(do ((i start (+ i 1)))
((= i end))
(let ((sum 0.0)
(y (<a class=quiet href="#readin">readin</a> rd)))
(do ((band 0 (+ 1 band)))
((= band pairs))
(set! sum (+ sum (<em class=red>ssb-am</em> (ssbs band)
(<a class=quiet href="sndscm.html#makebandpass">bandpass</a> (bands band) y)))))
(<a class=quiet href="#outa">outa</a> i (* amp sum))))))
(let* ((sound "oboe.snd")
(mx (maxamp sound))
(dur (<a class=quiet href="extsnd.html#mussoundduration">mus-sound-duration</a> sound)))
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:scaled-to mx :srate (srate sound))
(repitch 0 dur sound 554 1000)))
</pre>
<p>If you'd like to move formants independently of the fundamental, add
or subtract integer multiples of the new fundamental from the make-ssb-am
frequency argument. In the repitch instrument above, say we wanted to
add a "stretch" argument to spread out or squeeze down the spectrum.
We would replace the current make-ssb-am line with:
</p>
<pre class="indented">
(set! (ssbs (- i 1)) (<em class=red>make-ssb-am</em> (+ (* i factor old-freq)
(* new-freq (round (* i <em class=red>stretch</em>))))))
</pre>
<!-- WAVE-TRAIN -->
<div class="innerheader" id="wave-traindoc">wave-train</div>
<pre class="indented">
<em class=def id="make-wave-train">make-wave-train</em>
(frequency 0.0)
(initial-phase 0.0)
wave
(size *clm-table-size*)
(type mus-interp-linear)
<em class=def id="wave-train">wave-train</em> w (fm 0.0)
<em class=def id="wave-train?">wave-train?</em> w
<em class=def id="make-wave-train-with-env">make-wave-train-with-env</em> frequency env size
</pre>
<table class="method">
<tr><td colspan=2 class="methodtitle">wave-train methods</td></tr>
<tr><td class="inner"><em class=gen>mus-frequency</em></td><td class="inner">frequency in Hz</td></tr>
<tr><td class="inner"><em class=gen>mus-phase</em></td><td class="inner">phase in radians</td></tr>
<tr><td class="inner"><em class=gen>mus-data</em></td><td class="inner">wave array (no set!)</td></tr>
<tr><td class="inner"><em class=gen>mus-length</em></td><td class="inner">length of wave array (no set!)</td></tr>
<tr><td class="inner"><em class=gen>mus-interp-type</em></td><td class="inner">interpolation choice (no set!)</td></tr>
</table>
<p>wave-train adds a copy of its wave (a "grain" in more modern parlance) into its output at frequency times per second.
These copies can overlap or have long intervals of silence in between, so
wave train can be viewed either as an extension of pulse-train and table-lookup,
or as a primitive form of granular synthesis.
make-wave-train-with-env (defined in generators.scm) returns a new wave-train generator with the envelope 'env' loaded into its table.
</p>
<table>
<tr>
<td>
<div class="scheme">
<pre class="indented">
(with-sound (:play #t)
(let ((gen (make-wave-train 440.0
:wave (let ((v (make-float-vector 64))
(g (make-ncos 400 10)))
(set! (mus-phase g) (* -0.5 pi))
(do ((i 0 (+ i 1)))
((= i 64))
(set! (v i) (ncos g)))
v))))
(do ((i 0 (+ i 1)))
((= i 44100))
(outa i (* 0.5 (wave-train gen))))))
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="ruby">
<pre class="indented">
with_sound(:play, true) do
v = make_vct(64);
g = make_ncos(400, 10);
g.phase = -0.5 * 3.14159;
64.times do |i|
v[i] = ncos(g);
end
gen = make_wave_train(440.0, :wave, v);
44100.times do |i|
outa(i, 0.5 * wave_train(gen), $output)
end
end.output
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="forth">
<pre class="indented">
lambda: ( -- )
400 10 make-ncos { g }
g -0.5 pi f* set-mus-phase drop
64 make-vct map! g 0 ncos end-map { v }
440.0 :wave v make-wave-train { gen }
44100 0 do
i gen 0 wave-train f2/ *output* outa drop
loop
; :play #t with-sound drop
</pre>
</div>
</td>
</tr>
</table>
<img src="pix/wt.png" alt="wave-train example">
<!--
(with-sound (:clipped #f :statistics #t :scaled-to .5 :play #t)
(let ((gen (make-wave-train 300.0 :wave (let ((v (make-float-vector 64))
(g (make-sum-of-cosines 10 400 (* -0.5 pi))))
(do ((i 0 (+ i 1)))
((= i 64))
(set! (v i) (sum-of-cosines g)))
v))))
(do ((i 0 (+ i 1)))
((= i 1000))
(outa i (wave-train gen)))))
-->
<p>
With some simple envelopes or filters, you can
use this for VOSIM and other related techniques.
Here is a FOF instrument based loosely on fof.c of Perry Cook and the article
"Synthesis of the Singing Voice" by Bennett and Rodet in
"Current Directions in Computer Music Research".
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#definstrument">definstrument</a> (fofins beg dur frq amp vib f0 a0 f1 a1 f2 a2 ve ae)
(let* ((start (<a class=quiet href="#secondstosamples">seconds->samples</a> beg))
(end (+ start (<a class=quiet href="#secondstosamples">seconds->samples</a> dur)))
(ampf (<a class=quiet href="#make-env">make-env</a> (or ae '(0 0 25 1 75 1 100 0)) :scaler amp :duration dur))
(frq0 (<a class=quiet href="#hztoradians">hz->radians</a> f0))
(frq1 (<a class=quiet href="#hztoradians">hz->radians</a> f1))
(frq2 (<a class=quiet href="#hztoradians">hz->radians</a> f2))
(foflen (if (= *clm-srate* 22050) 100 200))
(vibr (<a class=quiet href="#make-oscil">make-oscil</a> 6))
(vibenv (<a class=quiet href="#make-env">make-env</a> (or ve '(0 1 100 1)) :scaler vib :duration dur))
(win-freq (/ (* 2 pi) foflen))
(foftab (make-float-vector foflen))
(wt0 (<em class=red>make-wave-train</em> :wave foftab :frequency frq)))
(do ((i 0 (+ i 1)))
((= i foflen))
(set! (foftab i) ;; this is not the pulse shape used by B&R
(* (+ (* a0 (sin (* i frq0)))
(* a1 (sin (* i frq1)))
(* a2 (sin (* i frq2))))
.5 (- 1.0 (cos (* i win-freq))))))
(do ((i start (+ i 1)))
((= i end))
(<a class=quiet href="#outa">outa</a> i (* (<a class=quiet href="#env">env</a> ampf) (<em class=red>wave-train</em> wt0 (* (<a class=quiet href="#env">env</a> vibenv) (<a class=quiet href="#oscil">oscil</a> vibr))))))))
(<a class=quiet href="sndscm.html#withsound">with-sound</a> () (fofins 0 1 270 .2 .001 730 .6 1090 .3 2440 .1)) ; "Ahh"
(<a class=quiet href="sndscm.html#withsound">with-sound</a> () ; one of JC's favorite demos
(fofins 0 4 270 .2 0.005 730 .6 1090 .3 2440 .1 '(0 0 40 0 75 .2 100 1)
'(0 0 .5 1 3 .5 10 .2 20 .1 50 .1 60 .2 85 1 100 0))
(fofins 0 4 (* 6/5 540) .2 0.005 730 .6 1090 .3 2440 .1 '(0 0 40 0 75 .2 100 1)
'(0 0 .5 .5 3 .25 6 .1 10 .1 50 .1 60 .2 85 1 100 0))
(fofins 0 4 135 .2 0.005 730 .6 1090 .3 2440 .1 '(0 0 40 0 75 .2 100 1)
'(0 0 1 3 3 1 6 .2 10 .1 50 .1 60 .2 85 1 100 0)))
</pre>
<p>The wave-trains's wave is a float-vector accessible via mus-data. The "fm" argument affects the frequency of
repetition. Here is a wave-train instrument that increasingly filters its grain (the word "now", for example)
while increasing the repetition rate. We're also using a pulse train as a sort of internal click track,
using the same frequency envelope as the wave-train, so we have some idea when to refilter the grain.
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#definstrument">definstrument</a> (when? start-time duration start-freq end-freq grain-file)
(let* ((beg (<a class=quiet href="#secondstosamples">seconds->samples</a> start-time))
(len (<a class=quiet href="#secondstosamples">seconds->samples</a> duration))
(end (+ beg len))
(grain-dur (<a class=quiet href="extsnd.html#mussoundduration">mus-sound-duration</a> grain-file))
(frqf (<a class=quiet href="#make-env">make-env</a> '(0 0 1 1) :scaler (<a class=quiet href="#hztoradians">hz->radians</a> (- end-freq start-freq)) :duration duration))
(click-track (<em class=red>make-pulse-train</em> start-freq))
(grain-size (<a class=quiet href="#secondstosamples">seconds->samples</a> grain-dur))
(grains (<em class=red>make-wave-train</em> :size grain-size :frequency start-freq))
(ampf (<a class=quiet href="#make-env">make-env</a> '(0 1 1 0) :scaler .7 :offset .3 :duration duration :base 3.0))
(grain (<em class=red>mus-data</em> grains)))
(<a class=quiet href="#filetoarray">file->array</a> grain-file 0 0 grain-size grain)
(let ((original-grain (copy grain)))
(do ((i beg (+ i 1)))
((= i end))
(let ((gliss (<a class=quiet href="#env">env</a> frqf)))
(outa i (* (<a class=quiet href="#env">env</a> ampf) (<em class=red>wave-train</em> grains gliss)))
(let ((click (<em class=red>pulse-train</em> click-track gliss)))
(if (> click 0.0)
(let* ((scaler (max 0.1 (* 1.0 (/ (- i beg) len))))
(comb-len 32)
(c1 (<a class=quiet href="#make-comb">make-comb</a> scaler comb-len))
(c2 (<a class=quiet href="#make-comb">make-comb</a> scaler (floor (* comb-len .75))))
(c3 (<a class=quiet href="#make-comb">make-comb</a> scaler (floor (* comb-len 1.25)))))
(do ((k 0 (+ k 1)))
((= k grain-size))
(let ((x (original-grain k)))
(set! (grain k) (+ (<a class=quiet href="#comb">comb</a> c1 x) (<a class=quiet href="#comb">comb</a> c2 x) (<a class=quiet href="#comb">comb</a> c3 x)))))))))))))
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> () (when? 0 4 2.0 8.0 "right-now.snd"))
</pre>
<p>wave-train is built on table-lookup and shares all of its questionable aspects. See also the <a href="#pulsedenv">pulsed-env</a>e generator
in generators.scm, used in animals.scm. It is often simpler to use <a href="#pulse-train">pulse-train</a> as the repetition
trigger, and mus-reset to restart an envelope.
</p>
<!-- RAND, RAND-INTERP -->
<div class="innerheader" id="randdoc">rand, rand-interp</div>
<pre class="indented">
<em class=def id="make-rand">make-rand</em>
(frequency 0.0) ; frequency at which new random numbers occur
(amplitude 1.0) ; numbers are between -amplitude and amplitude
(envelope '(-1 1 1 1)) ; distribution envelope (uniform distribution is the default)
distribution ; pre-computed distribution
<em class=def id="rand">rand</em> r (sweep 0.0)
<em class=def id="rand?">rand?</em> r
<em class=def id="make-rand-interp">make-rand-interp</em>
(frequency 0.0)
(amplitude 1.0)
(envelope '(-1 1 1 1)
distribution)
<em class=def id="rand-interp">rand-interp</em> r (sweep 0.0)
<em class=def id="rand-interp?">rand-interp?</em> r
<em class=def id="mus-random">mus-random</em> amp
<em class=def id="mus-rand-seed">mus-rand-seed</em>
</pre>
<table class="method">
<tr><td colspan=2 class="methodtitle">rand and rand-interp methods</td></tr>
<tr><td class="inner"><em class=gen>mus-frequency</em></td><td class="inner">frequency in Hz</td></tr>
<tr><td class="inner"><em class=gen>mus-phase</em></td><td class="inner">phase in radians</td></tr>
<tr><td class="inner"><em class=gen>mus-scaler</em></td><td class="inner">amplitude arg used in make-<gen></td></tr>
<tr><td class="inner"><em class=gen>mus-length</em></td><td class="inner">distribution table (float-vector) length</td></tr>
<tr><td class="inner"><em class=gen>mus-data</em></td><td class="inner">distribution table (float-vector), if any</td></tr>
<tr><td class="inner"><em class=gen>mus-increment</em></td><td class="inner">frequency in radians per sample</td></tr>
</table>
<p>rand produces a sequence of random numbers between -amplitude and
amplitude (a sort of step function).
rand-interp interpolates between successive
random numbers.
rand-interp could be defined as (<a class=quiet href="#moving-average">moving-average</a> agen (rand rgen)) where the
averager has the same period (length) as the rand.
In both cases, the "envelope" argument or the "distribution" argument determines the random number distribution.
mus-random returns a random number between -amplitude and amplitude.
</p>
<table>
<tr>
<td>
<div class="scheme">
<pre class="indented">
(with-sound (:channels 2 :play #t)
(let ((ran1 (make-rand 5.0 (hz->radians 220.0)))
(ran2 (make-rand-interp 5.0 (hz->radians 220.0)))
(osc1 (make-oscil 440.0))
(osc2 (make-oscil 1320.0)))
(do ((i 0 (+ i 1)))
((= i 88200))
(outa i (* 0.5 (oscil osc1 (rand ran1))))
(outb i (* 0.5 (oscil osc2 (rand-interp ran2)))))))
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="ruby">
<pre class="indented">
with_sound(:play, true, :channels, 2) do
ran1 = make_rand(5.0, hz2radians(220.0));
ran2 = make_rand_interp(5.0, hz2radians(220.0));
osc1 = make_oscil(440.0);
osc2 = make_oscil(1320.0);
88200.times do |i|
outa(i, 0.5 * oscil(osc1, rand(ran1)), $output);
outb(i, 0.5 * oscil(osc2, rand_interp(ran2)), $output);
end
end.output
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="forth">
<pre class="indented">
lambda: ( -- )
5.0 220.0 hz->radians make-rand { ran1 }
5.0 330.0 hz->radians make-rand-interp { ran2 }
440.0 make-oscil { osc1 }
1320.0 make-oscil { osc2 }
88200 0 do
i osc1 ran1 0 rand 0 oscil f2/ *output* outa drop
i osc2 ran2 0 rand-interp 0 oscil f2/ *output* outb drop
loop
; :channels 2 :play #t with-sound drop
</pre>
</div>
</td>
</tr>
</table>
<p>The "frequency" is the rate at which new values are produced, so it makes sense to request a frequency above srate/2.
If rand's frequency is the current srate, it produces a new random value on every sample.
Since rand is (normally) producing a sequence of square-waves, and rand-interp a sequence of triangle-waves,
both reflect that in their spectra (spectrum y axis is in dB):
</p>
<!--
(with-sound ()
(let ((gen (make-rand 2000)))
(do ((i 0 (+ i 1)))
((= i 50000))
(outa i (* .5 (rand gen))))))
(with-sound ()
(let ((gen (make-rand-interp 2000)))
(do ((i 0 (+ i 1)))
((= i 50000))
(outa i (* .5 (rand-interp gen))))))
(with-sound ()
(let ((gen (make-square-wave 1000)))
(do ((i 0 (+ i 1)))
((= i 50000))
(outa i (* .5 (square-wave gen))))))
(with-sound ()
(let ((gen (make-triangle-wave 1000)))
(do ((i 0 (+ i 1)))
((= i 50000))
(outa i (* .5 (triangle-wave gen))))))
-->
<table class="method">
<tr>
<td><img src="pix/sqsq.png" alt="sqwave spectrum"></td><td><img src="pix/tritri.png" alt="triwave spectrum"></td>
</tr>
<tr>
<td class="center">square-wave (freq=1000)</td><td class="center">triangle-wave (freq=1000)</td>
</tr>
<tr>
<td><img src="pix/randsq.png" alt="rand spectrum"></td><td><img src="pix/randtri.png" alt="rand-interp spectrum"></td>
</tr>
<tr>
<td class="center">rand (freq=2000)</td><td class="center">rand-interp (freq=2000)</td>
</tr>
</table>
<p>There are a variety of ways to get a non-uniform random number distribution:
<code>(random (random 1.0))</code> or <code>(sin (mus-random pi))</code> are examples. Exponential distribution could be:
</p>
<pre class="indented">
(log (max .01 (random 1.0)) .01)
</pre>
<p>where the ".01"'s affect how tightly the resultant values cluster toward 0.0 —
set them to .0001, for example, to get most of the random values close to 0.0.
The central-limit theorem says that you can get closer and closer to gaussian
noise by adding rand's together. Orfanidis in
"Introduction to Signal Processing" says 12 calls on rand will
do perfectly well:
</p>
<pre class="indented">
(define (gaussian-noise)
(do ((val 0.0)
(i 0 (+ i 1)))
((= i 12) (/ val 12.0))
(set! val (+ val (random 1.0)))))
</pre>
<p>You can watch this (or any other distribution) in action via:
</p>
<pre class="indented">
(define (add-rands n)
(let ((bins (make-vector 201 0))
(rands (make-vector n #f)))
(do ((i 0 (+ i 1)))
((= i n))
(set! (rands i) (<em class=red>make-rand</em> :frequency *clm-srate* :amplitude (/ 100 n)))
(rand (rands i)))
(do ((i 0 (+ i 1)))
((= i 100000))
(do ((sum 0.0)
(k 0 (+ k 1)))
((= k n)
(let ((bin (floor (+ 100 (round sum)))))
(set! (bins bin) (+ (bins bin) 1))))
(set! sum (+ sum (<em class=red>rand</em> (rands k))))))
bins))
(let ((ind (<a class=quiet href="extsnd.html#newsound">new-sound</a> "test.snd")))
(do ((n 1 (+ n 1)))
((= n 12))
(let* ((bins (vector->float-vector (add-rands n)))
(pk (maxamp bins)))
(float-vector->channel (float-vector-scale! bins (/ 1.0 pk)))
(set! (<a class=quiet href="extsnd.html#xaxislabel">x-axis-label</a>) (<a class=quiet>format</a> #f "n: ~D" n))
(<a class=quiet href="extsnd.html#updatetimegraph">update-time-graph</a>))))
</pre>
<p>
Another way to get different distributions is the "rejection method" in which we generate random number
pairs until we get a pair that falls within the
desired distribution; see <a href="sndscm.html#anyrandom">any-random</a> in dsp.scm.
The rand and rand-interp generators, however, use the "transformation method".
The make-rand and make-rand-interp "envelope" arguments specify
the desired distribution function; the generator takes the
inverse of the integral of this envelope, loads that into an array, and uses
<code>(array-interp (random array-size))</code>. This gives
random numbers of any arbitrary distribution at a computational cost
equivalent to the old waveshape generator.
The x axis of the envelope sets the output range (before scaling by the "amplitude" argument), and
the y axis sets the relative weight of the corresponding x axis value.
So, the default is <code>'(-1 1 1 1)</code> which says "output numbers between -1 and 1,
each number having the same chance of being chosen".
An envelope of <code>'(0 1 1 0)</code> outputs values between 0 and 1, denser toward 0.
If you already have the distribution table (a float-vector, the result of <code>(inverse-integrate envelope)</code> for example),
you can pass it through the "distribution" argument. Here is gaussian noise
using the "envelope" argument:
</p>
<pre class="indented">
(define (gaussian-envelope s)
(do ((e ())
(den (* 2.0 s s))
(i 0 (+ i 1))
(x -1.0 (+ x .1))
(y -4.0 (+ y .4)))
((= i 21)
(reverse e))
(set! e (cons (exp (- (/ (* y y) den))) (cons x e)))))
(<em class=red>make-rand</em> :envelope (gaussian-envelope 1.0))
</pre>
<p>If you want a particular set of values, it's simplest to fill a float-vector with those values,
then use random as the index into the array. Say we want 0.0, 0.5, and 1.0 at random,
but 0.5 should happen three times as often as either of the others:
</p>
<pre class="indented">
(do ((vals (float-vector 0.0 0.5 0.5 0.5 1.0))
(i 0 (+ i 1)))
((= i 10))
(<a class=quiet>format</a> () ";~A " (vals (random 5))))
</pre>
<p>These "distributions" refer to the values returned by the random number
generator, but all of them produce white noise (all frequencies are equally
likely).
You can, of course, filter the output of rand to get a different
frequency distribution. See, for example, <a href="#round-interp">round-interp</a> in generators.scm.
It uses a <a href="#moving-average">moving-average</a> generator to low-pass filter the output of a rand-interp
generator; the result is a rand-interp signal with rounded corners.
Orfanidis also mentions a clever way to get reasonably good 1/f noise:
sum together n rand's, where each rand is running an octave slower
than the preceding:
</p>
<pre class="indented">
(define (make-1f-noise n)
;; returns an array of rand's ready for the 1f-noise generator
(do ((rans (make-vector n))
(i 0 (+ i 1)))
((= i n) rans)
(set! (rans i) (<em class=red>make-rand</em> :frequency (/ *clm-srate* (expt 2 i))))))
(define (1f-noise rans)
(let ((val 0.0)
(len (length rans)))
(do ((i 0 (+ i 1)))
((= i len) (/ val len))
(set! val (+ val (<em class=red>rand</em> (rans i)))))))
</pre>
<p>This is the <a href="#pink-noise">pink-noise</a> generator in generators.scm.
See also <a href="#green-noise">green-noise</a> — bounded brownian noise that can mimic 1/f noise in some cases.
(The brownian graph below has a different dB range, and the rand graph would be flat if we used a frequency of 44100).
</p>
<table class="method">
<tr>
<td class="center">random</td>
<td class="center">rand</td>
<td class="center">rand-interp</td>
</tr><tr>
<td><img src="pix/random.png" alt="random spectrum"></td>
<td><img src="pix/rand.png" alt="rand spectrum"></td>
<td><img src="pix/randi.png" alt="rand-interp spectrum"></td>
</tr>
<tr>
<td class="center">1/f</td>
<td class="center">brownian</td>
<td class="center">green</td>
</tr><tr>
<td><img src="pix/1f.png" alt="1/f spectrum"></td>
<td><img src="pix/brownian.png" alt="brownian spectrum"></td>
<td><img src="pix/green.png" alt="green spectrum"></td>
</tr></table>
<!-- CLM:
;; 1f.png
(with-sound ()
(let ((noise (make-1f-noise 12)))
(do ((i 0 (+ i 1)))
((= i 10000))
(outa i (1f-noise noise)))))
;; rand.png
(with-sound ()
(let ((noise (make-rand 10000.0)))
(do ((i 0 (+ i 1)))
((= i 10000))
(outa i (rand noise)))))
;; randi.png
(with-sound ()
(let ((noise (make-rand-interp 10000.0)))
(do ((i 0 (+ i 1)))
((= i 10000))
(outa i (rand-interp noise)))))
;; random.png
(with-sound ()
(do ((i 0 (+ i 1)))
((= i 10000))
(outa i (- 0.5 (random 1.0)))))
;; brownian.png
(with-sound ()
(let ((val 0.0))
(do ((i 0 (+ i 1)))
((= i 10000))
(set! val (+ val -.005 (random 0.01)))
(outa i val))))
;; green.png
(with-sound ()
(let ((noise (make-green-noise 10000.0 1)))
(do ((i 0 (+ i 1)))
((= i 10000))
(outa i (green-noise noise 0.0)))))
-->
<p>And we can't talk about noise without mentioning fractals:</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#definstrument">definstrument</a> (fractal start duration m x amp)
;; use formula of M J Feigenbaum
(let* ((beg (<a class=quiet href="#secondstosamples">seconds->samples</a> start))
(end (+ beg (<a class=quiet href="#secondstosamples">seconds->samples</a> duration))))
(do ((i beg (+ i 1)))
((= i end))
(<a class=quiet href="#outa">outa</a> i (* amp x))
(set! x (- 1.0 (* m x x))))))
;;; this quickly reaches a stable point for any m in[0,.75], so:
(<a class=quiet href="sndscm.html#withsound">with-sound</a> () (fractal 0 1 .5 0 .5))
;;; is just a short "ftt"
(<a class=quiet href="sndscm.html#withsound">with-sound</a> () (fractal 0 1 1.5 .20 .2))
</pre>
<p>With this instrument you can hear
the change over from the stable equilibria, to the period doublings,
and finally into the combination of noise and periodicity that
has made these curves famous. See appendix 2 to Ekeland's "Mathematics and the Unexpected" for more details.
Another instrument based on similar ideas is:</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#definstrument">definstrument</a> (attract beg dur amp c) ; c from 1 to 10 or so
;; by James McCartney, from CMJ vol 21 no 3 p 6
(let ((st (<a class=quiet href="#secondstosamples">seconds->samples</a> beg)))
(do ((nd (+ st (<a class=quiet href="#secondstosamples">seconds->samples</a> dur)))
(a .2)
(b .2)
(dt .04)
(scale (/ (* .5 amp) c))
(x1 0.0)
(x -1.0)
(y 0.0)
(z 0.0)
(i st (+ i 1)))
((= i nd))
(set! x1 (- x (* dt (+ y z))))
(set! y (+ y (* dt (+ x (* a y)))))
(set! z (+ z (* dt (- (+ b (* x z)) (* c z)))))
(set! x x1)
(<a class=quiet href="#outa">outa</a> i (* scale x)))))
</pre>
<p>which gives brass-like sounds!
We can also get all the period doublings and so on from sin:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#withsound">with-sound</a> (:clipped #f :scaled-to 0.5)
(do ((x 0.5)
(i 0 (+ i 1)))
((= i 44100))
(outa i x)
(set! x (* 4 (sin (* pi x))))))
</pre>
<p>For an extended discussion of this case, complete with pictures of the
period doublings, see Strogatz, "Nonlinear Dynamics and Chaos".
</p>
<p>
mus-rand-seed provides access to the seed for mus-random's random number generator:
</p>
<pre class="indented">
> (set! (mus-rand-seed) 1234)
1234
> (mus-random 1.0)
-0.7828369138846
> (mus-random 1.0)
-0.880371093652
> (set! (mus-rand-seed) 1234) ; now start again with the same sequence of numbers
1234
> (mus-random 1.0)
-0.7828369138846
> (mus-random 1.0)
-0.880371093652
</pre>
<p>The clm random functions discussed here are different from s7's random function.
The latter has a random-state record to guide the sequence (and uses a different
algorithm), whereas the clm functions just use an integer, mus-rand-seed.
</p>
<p>See also
<a href="sndscm.html#ditherchannel">dither-channel</a> (dithering),
<a href="sndscm.html#maracadoc">maraca.scm</a> (physical modelling),
<a href="sndscm.html#noisedoc">noise.scm, noise.rb</a> (a truly ancient noise-maker),
<a href="sndscm.html#anyrandom">any-random</a> (arbitrary distribution via the rejection method),
and <a href="#green-noise">green-noise</a> (bounded Brownian noise).
</p>
<!-- SIMPLE FILTERS -->
<div class="innerheader" id="one-poledoc">one-pole, one-zero, two-pole, two-zero</div>
<pre class="indented">
<em class=def id="make-one-pole">make-one-pole</em> a0 b1 ; b1 < 0.0 gives lowpass, b1 > 0.0 gives highpass
<em class=def id="one-pole">one-pole</em> f input
<em class=def id="one-pole?">one-pole?</em> f
<em class=def id="make-one-zero">make-one-zero</em> a0 a1 ; a1 > 0.0 gives weak lowpass, a1 < 0.0 highpass
<em class=def id="one-zero">one-zero</em> f input
<em class=def id="one-zero?">one-zero?</em> f
<em class=def id="make-two-pole">make-two-pole</em> frequency [or a0] radius [or b1] b2
<em class=def id="two-pole">two-pole</em> f input
<em class=def id="two-pole?">two-pole?</em> f
<em class=def id="make-two-zero">make-two-zero</em> frequency [or a0] radius [or a1] a2
<em class=def id="two-zero">two-zero</em> f input
<em class=def id="two-zero?">two-zero?</em> f
</pre>
<table class="method">
<tr><td colspan=2 class="methodtitle">simple filter methods</td></tr>
<tr><td class="inner"><em class=gen>mus-xcoeff</em></td><td class="inner">a0, a1, a2 in equations</td></tr>
<tr><td class="inner"><em class=gen>mus-ycoeff</em></td><td class="inner">b1, b2 in equations</td></tr>
<tr><td class="inner"><em class=gen>mus-order</em></td><td class="inner">1 or 2 (no set!)</td></tr>
<tr><td class="inner"><em class=gen>mus-scaler</em></td><td class="inner">two-pole and two-zero radius</td></tr>
<tr><td class="inner"><em class=gen>mus-frequency</em></td><td class="inner">two-pole and two-zero center frequency</td></tr>
</table>
<p>These are the simplest of filters. If you're curious about filters,
Julius Smith's on-line <a href="http://www-ccrma.stanford.edu/~jos/filters/">Introduction to Digital Filters</a> is
excellent.
</p>
<pre class="indented">
one-zero y(n) = a0 x(n) + a1 x(n-1)
one-pole y(n) = a0 x(n) - b1 y(n-1)
two-pole y(n) = a0 x(n) - b1 y(n-1) - b2 y(n-2)
two-zero y(n) = a0 x(n) + a1 x(n-1) + a2 x(n-2)
</pre>
<p>
The "a0, b1" nomenclature is taken from Julius Smith's "An Introduction to Digital
Filter Theory" in Strawn "Digital Audio Signal Processing", and is different
from that used in the more general filters such as <a href="#fir-filter">fir-filter</a>.
In make-two-pole and make-two-zero you can specify either the actual
desired coefficients ("a0" and friends), or the center frequency and radius of the
filter ("frequency" and "radius"). The word "radius" refers to the unit circle,
so it should be between 0.0 and (less than) 1.0.
"frequency" should be between 0 and srate/2.
</p>
<table>
<tr>
<td>
<div class="scheme">
<pre class="indented">
(with-sound (:play #t)
(let ((flt (make-two-pole 1000.0 0.999))
(ran1 (make-rand 10000.0 .002)))
(do ((i 0 (+ i 1)))
((= i 44100))
(outa i (* 0.5 (two-pole flt (rand ran1)))))))
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="ruby">
<pre class="indented">
with_sound(:play, true) do
flt = make_two_pole(1000.0, 0.999);
ran1 = make_rand(10000.0, 0.002);
44100.times do |i|
outa(i, 0.5 * two_pole(flt, rand(ran1)), $output);
end
end.output
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="forth">
<pre class="indented">
lambda: ( -- )
1000.0 0.999 make-two-pole { flt }
10000.0 0.002 make-rand { ran1 }
44100 0 do
i flt ran1 0 rand two-pole f2/ *output* outa drop
loop
; :play #t with-sound drop
</pre>
</div>
</td>
</tr>
</table>
<p>We can use a one-pole filter as an "exponentially weighted moving average":
</p>
<pre class="indented">
(make-one-pole (/ 1.0 order) (/ (- order) (+ 1.0 order)))
</pre>
<p>where "order" is more or less how long an input affects the output.
The <a href="#mus-xcoeff">mus-xcoeff</a> and <a href="#mus-ycoeff">mus-ycoeff</a> functions give access to the filter coefficients.
<a href="sndscm.html#prc95doc">prc95.scm</a> uses them to make "run time"
alterations to the filters:
</p>
<pre class="indented">
(set! (mus-ycoeff p 1) (- val)) ; "p" is a one-pole filter, this is setting "b1"
(set! (mus-xcoeff p 0) (- 1.0 val)) ; this is setting "a0"
</pre>
<p>We can also use <a href="#mus-frequency">mus-frequency</a> and <a href="#mus-scaler">mus-scaler</a> (the pole "radius") as a more intuitive handle on these coefficients:
</p>
<pre class="indented">
> (define p (make-two-pole :radius .9 :frequency 1000.0))
#<unspecified>
>p
#<two-pole: a0: 1.000, b1: -1.727, b2: 0.810, y1: 0.000, y2: 0.000>
> (mus-frequency p)
1000.00025329731
> (mus-scaler p)
0.899999968210856
> (set! (mus-frequency p) 2000.0)
2000.0
>p
#<two-pole: a0: 1.000, b1: -1.516, b2: 0.810, y1: 0.000, y2: 0.000>
</pre>
<p>A quick way to see the frequency response of a filter is to drive it with a sine wave sweeping from
0 Hz to half the sampling rate; if the sound length is 0.5 seconds, you can read off the time axis
as the response at that frequency (in terms of a sampling rate of 1.0):
</p>
<pre class="indented">
(define (test-filter flt)
(let* ((osc (<a class=quiet href="#make-oscil">make-oscil</a>))
(samps (<a class=quiet href="#secondstosamples">seconds->samples</a> 0.5))
(ramp (<a class=quiet href="#make-env">make-env</a> '(0 0 1 1)
:scaler (<a class=quiet href="#hztoradians">hz->radians</a> samps)
:length samps)))
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> ()
(do ((i 0 (+ i 1)))
((= i samps))
(<a class=quiet href="#outa">outa</a> i (flt (<a class=quiet href="#oscil">oscil</a> osc (<a class=quiet href="#env">env</a> ramp))))))))
(test-filter (make-one-zero 0.5 0.5))
(test-filter (make-one-pole 0.1 -0.9))
(test-filter (make-two-pole 0.1 0.1 0.9))
(test-filter (make-two-zero 0.5 0.2 0.3))
</pre>
<img class="indented" src="pix/2pole.png" alt="simple filters">
<!--
(define (test-filter flt chan)
(let* ((osc (make-oscil 0.0))
(samps (seconds->samples 0.5))
(ramp (make-env '(0 0 1 1) :scaler (hz->radians samps) :length samps)))
(do ((i 0 (+ i 1)))
((= i samps))
(out-any i (flt (oscil osc (env ramp))) chan))))
(with-sound (:channels 4)
(test-filter (make-one-zero 0.5 0.5) 0)
(test-filter (make-one-pole 0.1 -0.9) 1)
(test-filter (make-two-pole 0.1 0.1 0.9) 2)
(test-filter (make-two-zero 0.5 0.2 0.3) 3))
(define (fixup-axes)
(set! *selected-data-color* (make-color 0 0 0))
(set! *selected-graph-color* (make-color 1 1 1))
(set! (x-axis-label 0 0) "(make-one-zero 0.5 0.5)")
(set! (x-axis-label 0 1) "(make-one-pole 0.1 -0.9)")
(set! (x-axis-label 0 2) "(make-two-pole 0.1 0.1 0.9)")
(set! (x-axis-label 0 3) "(make-two-zero 0.5 0.2 0.3)"))
-->
<!-- FORMANT -->
<div class="innerheader" id="formantdoc">formant</div>
<pre class="indented">
<em class=def id="make-formant">make-formant</em>
frequency ; resonance center frequency in Hz
radius ; resonance width, indirectly
<em class=def id="formant">formant</em> f input center-frequency-in-radians
<em class=def id="formant?">formant?</em> f
<em class=def id="formantbank">formant-bank</em> filters input
<em class=def id="formantbankp">formant-bank?</em> f
<em class=def id="makeformantbank">make-formant-bank</em> filters amps
<em class=def id="make-firmant">make-firmant</em> frequency radius
<em class=def id="firmant">firmant</em> f input center-frequency-in-radians
<em class=def id="firmant?">firmant?</em> f
;; the next two are optimizations that I may remove
<em class=def>mus-set-formant-frequency</em> f frequency
<em class=def>mus-set-formant-radius-and-frequency</em> f radius frequency
</pre>
<table class="method">
<tr><td colspan=2 class="methodtitle">formant methods</td></tr>
<tr><td class="inner"><em class=gen>mus-phase</em></td><td class="inner">formant radius</td></tr>
<tr><td class="inner"><em class=gen>mus-frequency</em></td><td class="inner">formant center frequency</td></tr>
<tr><td class="inner"><em class=gen>mus-order</em></td><td class="inner">2 (no set!)</td></tr>
</table>
<p>formant and firmant are resonators (two-pole, two-zero bandpass filters) centered at "frequency", with the bandwidth set by "radius".
</p>
<pre class="indented">
formant:
y(n) = x(n) -
r * x(n-2) +
2 * r * cos(frq) * y(n-1) -
r * r * y(n-2)
firmant:
x(n+1) = r * (x(n) - 2 * sin(frq/2) * y(n)) + input
y(n+1) = r * (2 * sin(frq/2) * x(n+1) + y(n))
</pre>
<table>
<tr>
<td>
<div class="scheme">
<pre class="indented">
(with-sound (:play #t)
(let ((flt (make-firmant 1000.0 0.999))
(ran1 (make-rand 10000.0 5.0)))
(do ((i 0 (+ i 1)))
((= i 44100))
(outa i (* 0.5 (firmant flt (rand ran1)))))))
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="ruby">
<pre class="indented">
with_sound(:play, true) do
flt = make_firmant(1000.0, 0.999);
ran1 = make_rand(10000.0, 5.0);
44100.times do |i|
outa(i, 0.5 * firmant(flt, rand(ran1)), $output);
end
end.output
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="forth">
<pre class="indented">
lambda: ( -- )
1000.0 0.999 make-firmant { flt }
10000.0 5.0 make-rand { ran1 }
44100 0 do
i flt ran1 0 rand #f firmant f2/ *output* outa drop
loop
; :play #t with-sound drop
</pre>
</div>
</td>
</tr>
</table>
<p>The formant generator is described in "A Constant-gain Digital Resonator Tuned By a Single Coefficient" by Julius
O. Smith and James B. Angell in Computer Music Journal Vol. 6 No. 4 (winter
1982) and "A note on
Constant-Gain Digital Resonators" by Ken Steiglitz, CMJ vol 18 No. 4 pp.8-10
(winter 1994).
The formant bandwidth is a function of the "radius", and its center frequency is set by "frequency".
As the radius approaches 1.0 (the unit circle), the
resonance gets narrower.
Use <a href="#mus-frequency">mus-frequency</a> to change the center frequency, and <a href="#mus-scaler">mus-scaler</a> to change the radius.
The radius can be set in terms of desired bandwidth in Hz via:
</p>
<pre class="indented">
(exp (* -0.5 (<a class=quiet href="#hztoradians">hz->radians</a> bandwidth)))
</pre>
<p>If you change the radius, the peak amplitude
of the output changes.
The firmant generator is the "modified coupled form" of the formant generator,
developed by Max Mathews and Julius Smith in "Methods for Synthesizing Very High Q Parametrically
Well Behaved Two Pole Filters".
Here are some graphs showing the formant and firmant filtering white noise
as we sweep either the frequency or the radius:
</p>
<img class="indented" src="pix/formant.png" alt="various formant cases">
<!--
(with-sound (:channels 4 :clipped #f :statistics #t)
(let* ((dur 100)
(samps (seconds->samples dur))
(flta (make-formant 100 .999))
(fltb (make-formant 5000 .1))
(fltc (make-firmant 100 .999))
(fltd (make-firmant 5000 .1))
(ampf (make-env '(0 0 1 1 100 1 101 0) :duration dur))
(frqf (make-env '(0 100 1 10000) :scaler (hz->radians 1.0) :duration dur))
(rf (make-env '(0 .6 1 .999) :base .01 :duration dur)))
(do ((i 0 (+ i 1)))
((= i samps))
(let* ((frq (env frqf))
(r (env rf))
(amp (env ampf))
(pulse (- (random 2.0) 1.0)))
(outa i (* amp (formant flta pulse frq)))
(set! (mus-scaler fltb) r)
(outc i (* amp (formant fltb pulse)))
(outb i (* amp (firmant fltc pulse frq)))
(outd i (* amp (firmant fltd pulse)))
(set! (mus-scaler fltd) r)
))))
(define (fixup-axes)
(set! *selected-data-color* (make-color 0 0 0))
(set! *selected-graph-color* (make-color 1 1 1))
(set! (x-axis-label 0 0 0) "formant: sweep frequency from 100 to 10000")
(set! (x-axis-label 0 1 0) "firmant: sweep frequency from 100 to 10000")
(set! (x-axis-label 0 0 1) "formant radius: .999")
(set! (x-axis-label 0 1 1) "firmant radius: .999")
(set! (x-axis-label 0 2 0) "formant: sweep radius from .6 to .999")
(set! (x-axis-label 0 3 0) "firmant: sweep radius from .6 to .999")
(set! (x-axis-label 0 2 1) "formant frequency: 5000")
(set! (x-axis-label 0 3 1) "firmant frequency: 5000"))
-->
<p>formant and firmant are often used to sculpt away unwanted spectral components, or emphasize formant regions.
In animals.scm, the crow, for example,
</p>
<pre class="indented">
(load "animals.scm")
(with-sound (:play #t) (american-crow 0 .5))
</pre>
<p>has three formant filters. Without them, it would sound like this:
</p>
<pre class="indented">
(with-sound (:play #t) (american-crow-no-formants 0 .5))
</pre>
<p>formant generators are also commonly used in a bank of filters to provide a sort of sample-by-sample spectrum.
An example is <a href="sndscm.html#fadedoc">fade.scm</a> which has various functions for frequency domain mixing.
See also
<a href="sndscm.html#grapheq">grapheq</a> (a non-graphic equalizer), and
<a href="sndscm.html#crosssynthesis">cross-synthesis</a>.
Here's an example that moves a set of harmonically related formants through a sound.
If "radius" is .99, you get a glass-harmonica effect; if it's less, you get more of an FM index envelope effect.
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#definstrument">definstrument</a> (move-formants start file amp radius move-env num-formants)
(let* ((frms (make-vector num-formants))
(beg (<a class=quiet href="#secondstosamples">seconds->samples</a> start))
(dur (<a class=quiet href="extsnd.html#mussoundframples">mus-sound-framples</a> file))
(end (+ beg dur))
(rd (<a class=quiet href="#make-readin">make-readin</a> file))
(menv (<a class=quiet href="#make-env">make-env</a> move-env :length dur)))
(let ((start-frq (<a class=quiet href="#env">env</a> menv)))
(do ((i 0 (+ i 1)))
((= i num-formants))
(set! (frms i) (<em class=red>make-formant</em> (* (+ i 1) start-frq) radius))))
(do ((k beg (+ k 1)))
((= k end))
(let ((frq (<a class=quiet href="#env">env</a> menv))
(sum 0.0)
(inp (<a class=quiet href="#readin">readin</a> rd)))
(do ((i 0 (+ i 1)))
((= i num-formants))
(set! sum (+ sum (<em class=red>formant</em> (frms i) inp))))
(outa k (* amp sum))
(do ((i 0 (+ i 1))
(curfrq frq (+ curfrq frq)))
((= i num-formants))
(if (< (* 2 curfrq) *clm-srate*)
(set! (<em class=red>mus-frequency</em> (frms i)) curfrq)))))))
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> ()
(move-formants 0 "oboe.snd" 2.0 0.99 '(0 1200 1.6 2400 2 1400) 4))
</pre>
<p>make-formant-bank creates a formant-bank generator, an array of formant generators that is summed in parallel. The
explicit do loop:
</p>
<pre class="indented">
(do ((sum 0.0) ; say we have n formant generators in the formants vector, and we're passing each a signal x
(i 0 (+ i 1)))
((= i n) sum)
(set! sum (+ sum (formant (formants i) x))))
</pre>
<p>can be replaced with:
</p>
<pre class="indented">
(let ((fb (make-formant-bank formants)))
...
(formant-bank fb x))
</pre>
<p>make-formant-bank takes a vector of formant generators as its first argument. Its optional second argument
is a float-vector of gains (amplitudes) to scale each formant's contribution to the sum. Similarly, formant-bank's
second argument is either a real number or a float-vector. If a float-vector, each element is treated as the input to the
corresponding formant in the bank. (formant-bank ignores its constituent formant generator's radius and frequency
after make-formant-bank; see move-formant above for a slightly less compact workaround if you want a bank of moving formants).
</p>
<br>
<p>The clm-3 formant gain calculation was incorrect. To translate from the old
formant to the new one, multiply the old gain by (* 2 (sin (<a class=quiet href="#hztoradians">hz->radians</a> frequency))).
</p>
<p>If you change the radius or frequency rapidly, the formant generator will either produce
clicks or overflow, but firmant gives good output. Here's an
example that puts formant on the edge of disaster (the glitch is about to explode), but firmant plugs away happily:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:channels 2)
(let* ((samps (<a class=quiet href="#secondstosamples">seconds->samples</a> 3))
(flta (<em class=red>make-formant</em> 100 .999))
(fltc (<em class=red>make-firmant</em> 100 .999))
(vibosc (<a class=quiet href="#make-oscil">make-oscil</a> 10))
(index (<a class=quiet href="#hztoradians">hz->radians</a> 100))
(click (<a class=quiet href="#make-ncos">make-ncos</a> 40 500)))
(do ((i 0 (+ i 1)))
((= i samps))
(let ((vib (* index (+ 1 (<a class=quiet href="#oscil">oscil</a> vibosc))))
(pulse (<a class=quiet href="#ncos">ncos</a> click)))
(<a class=quiet href="#outa">outa</a> i (* 10 (<em class=red>formant</em> flta pulse vib)))
(<a class=quiet href="#outa">outb</a> i (* 10 (<em class=red>firmant</em> fltc pulse vib)))))))
</pre>
<img class="indented" src="pix/firmant.png" alt="firmant is happy">
<!-- FILTERS -->
<div class="innerheader" id="filterdoc">filter, iir-filter, fir-filter</div>
<pre class="indented">
<em class=def id="make-filter">make-filter</em> order xcoeffs ycoeffs
<em class=def id="filter">filter</em> fl inp
<em class=def id="filter?">filter?</em> fl
<em class=def id="make-fir-filter">make-fir-filter</em> order xcoeffs
<em class=def id="fir-filter">fir-filter</em> fl inp
<em class=def id="fir-filter?">fir-filter?</em> fl
<em class=def id="make-iir-filter">make-iir-filter</em> order ycoeffs
<em class=def id="iir-filter">iir-filter</em> fl inp
<em class=def id="iir-filter?">iir-filter?</em> fl
<em class=def id="make-fir-coeffs">make-fir-coeffs</em> order v
</pre>
<table class="method">
<tr><td colspan=2 class="methodtitle">general filter methods</td></tr>
<tr><td class="inner"><em class=gen>mus-order</em></td><td class="inner">filter order</td></tr>
<tr><td class="inner"><em class=gen>mus-xcoeff</em></td><td class="inner">x (input) coeff</td></tr>
<tr><td class="inner"><em class=gen>mus-xcoeffs</em></td><td class="inner">x (input) coeffs</td></tr>
<tr><td class="inner"><em class=gen>mus-ycoeff</em></td><td class="inner">y (output) coeff</td></tr>
<tr><td class="inner"><em class=gen>mus-ycoeffs</em></td><td class="inner">y (output) coeffs</td></tr>
<tr><td class="inner"><em class=gen>mus-data</em></td><td class="inner">current state (input values)</td></tr>
<tr><td class="inner"><em class=gen>mus-length</em></td><td class="inner">same as mus-order</td></tr>
</table>
<p>These are general FIR/IIR filters of arbitrary order.
The "order" argument is one greater than the nominal filter
order (it is the size of the coefficient array).
The filter generator might be defined:
</p>
<pre class="indented">
(let ((xout 0.0))
(set! (state 0) input)
(do ((j (- order 1) (- j 1)))
((= j 0))
(set! xout (+ xout (* (xcoeffs j) (state j))))
(set! (state 0) (- (state 0) (* (ycoeffs j) (state j))))
(set! (state j) (state (- j 1))))
(+ xout (* (state 0) (xcoeffs 0))))
</pre>
<table>
<tr>
<td>
<div class="scheme">
<pre class="indented">
(with-sound (:play #t)
(let ((flt (make-iir-filter 3 (float-vector 0.0 -1.978 0.998)))
(ran1 (make-rand 10000.0 0.002)))
(do ((i 0 (+ i 1)))
((= i 44100))
(outa i (* 0.5 (iir-filter flt (rand ran1)))))))
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="ruby">
<pre class="indented">
with_sound(:play, true) do
flt = make_iir_filter(3, vct(0.0, -1.978, 0.998));
ran1 = make_rand(10000.0, 0.002);
44100.times do |i|
outa(i, 0.5 * iir_filter(flt, rand(ran1)), $output);
end
end.output
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="forth">
<pre class="indented">
lambda: ( -- )
3 vct( 0.0 -1.978 0.998 ) make-iir-filter { flt }
10000.0 0.002 make-rand { ran1 }
44100 0 do
i flt ran1 0 rand iir-filter f2/ *output* outa drop
loop
; :play #t with-sound drop
</pre>
</div>
</td>
</tr>
</table>
<p><a href="sndscm.html#dspdoc">dsp.scm</a> has a number of filter design functions,
and various specializations of the filter generators, including such
perennial favorites as biquad, butterworth, hilbert transform, and
notch filters. Similarly, <a href="sndscm.html#analogfilterdoc">analog-filter.scm</a> has
the usual IIR suspects: Butterworth, Chebyshev, Bessel, and Elliptic filters.
A biquad section can be implemented as:
</p>
<pre class="indented">
(define (make-biquad a0 a1 a2 b1 b2)
(make-filter 3 (float-vector 0.0 b1 b2)))
</pre>
<p>
The Hilbert transform can be implemented with an fir-filter:
</p>
<pre class="indented">
(define* (make-hilbert-transform (len 30))
(let* ((arrlen (+ 1 (* 2 len)))
(arr (make-float-vector arrlen)))
(do ((lim (if (even? len) len (+ 1 len)))
(i (- len) (+ i 1)))
((= i lim))
(let ((k (+ i len))
(denom (* pi i))
(num (- 1.0 (cos (* pi i)))))
(set! (arr k) (if (or (= num 0.0) (= i 0))
0.0
(* (/ num denom)
(+ .54 (* .46 (cos (/ (* i pi) len)))))))))
(<em class=red>make-fir-filter</em> arrlen arr)))
(define hilbert-transform <em class=red>fir-filter</em>)
</pre>
<p>make-fir-coeffs translates a frequency response envelope (actually, evenly spaced points in a float-vector) into the corresponding FIR filter coefficients.
The order of the filter determines how close you
get to the envelope.
</p>
<table class="method">
<tr><td class="methodtitle">Filters</td></tr>
<tr><td>
<blockquote><small>
lowpass filter: <a href="sndscm.html#makelowpass">make-lowpass</a> in dsp.scm<br>
highpass filter: <a href="sndscm.html#makehighpass">make-highpass</a> in dsp.scm<br>
bandpass filter: <a href="sndscm.html#makebandpass">make-bandpass</a> in dsp.scm<br>
bandstop filter: <a href="sndscm.html#makebandstop">make-bandstop</a> in dsp.scm<br>
Butterworth, Chebyshev, Bessel, Elliptic filters: <a href="sndscm.html#analogfilterdoc">analog-filter.scm</a><br>
Hilbert transform: <a href="sndscm.html#makehilberttransform">make-hilbert-transform</a> in dsp.scm<br>
differentiator: <a href="sndscm.html#makedifferentiator">make-differentiator</a> in dsp.scm<br>
block DC: dc-block in prc95.scm or (make-filter 2 (float-vector 1 -1) (float-vector 0 -0.99))<br>
hum elimination: <a href="sndscm.html#IIRfilters">make-eliminate-hum</a> and <a href="sndscm.html#notchchannel">notch-channel</a> in dsp.scm<br>
hiss elimination: <a href="sndscm.html#notchoutrumbleandhiss">notch-out-rumble-and-hiss</a><br>
smoothing filters: <a href="#moving-average">moving-average</a>, <a href="#weighted-moving-average">weighted-moving-average</a>, exponentially-weighted-moving-average<br>
notch-filters: <a href="sndscm.html#notchchannel">notch-channel</a> and <a href="sndscm.html#notchselection">notch-selection</a><br>
arbitrary spectrum via FIR filter: <a href="sndscm.html#spectrumtocoeffs">spectrum->coeffs</a> in dsp.scm<br>
invert an FIR filter: <a href="sndscm.html#invertfilter">invert-filter</a> in dsp.scm<br>
filtered echo sound effect: <a href="sndscm.html#zecho">flecho</a> in examp.scm<br>
time varying filter: fltit in examp.scm<br>
draw frequency response: use the <a href="snd.html#editenvelope">envelope editor</a> or <a href="snd.html#filtercontrol">filter control</a> in control panel<br>
Moog filter: <a href="sndscm.html#moogdoc">moog.scm</a><br>
Savitzky-Golay filter: <a href="sndscm.html#sgfilter">savitzky-golay-filter</a><br>
click reduction: <a href="sndscm.html#removeclicks">remove-clicks</a>, <a href="sndscm.html#cleanchannel">clean-channel</a><br>
graphical equalizer filter bank: <a href="sndscm.html#clminsdoc">graphEq</a><br>
nonlinear (Volterra) filter: <a href="sndscm.html#volterrafilter">volterra-filter</a><br>
Kalman filter: <a href="sndscm.html#kalmanfilterchannel">kalman-filter-channel</a><br>
filter a sound: <a href="extsnd.html#filtersound">filter-sound</a>, <a href="extsnd.html#filterchannel">filter-channel</a><br>
see also convolution, physical modeling, reverb, and <a href="sndscm.html#ssffts">fft-based filtering</a><br>
</small></blockquote>
</td></tr></table>
<!-- DELAY -->
<div class="innerheader" id="delaydoc">delay, tap</div>
<pre class="indented">
<em class=def id="make-delay">make-delay</em>
size ; delay length
initial-contents ; delay line's initial values (a float-vector or a list)
(initial-element 0.0) ; delay line's initial element
max-size ; maximum delay size in case the delay changes
type ; interpolation type
<em class=def id="delay">delay</em> d input (pm 0.0)
<em class=def id="delay?">delay?</em> d
<em class=def id="tap">tap</em> d (offset 0)
<em class=def id="tap?">tap?</em> d
<em class=def id="delaytick">delay-tick</em> d input
</pre>
<table class="method">
<tr><td colspan=2 class="methodtitle">delay methods</td></tr>
<tr><td class="inner"><em class=gen>mus-length</em></td><td class="inner">length of delay</td></tr>
<tr><td class="inner"><em class=gen>mus-order</em></td><td class="inner">same as mus-length</td></tr>
<tr><td class="inner"><em class=gen>mus-data</em></td><td class="inner">delay line itself (no set!)</td></tr>
<tr><td class="inner"><em class=gen>mus-interp-type</em></td><td class="inner">interpolation choice (no set!)</td></tr>
<tr><td class="inner"><em class=gen>mus-scaler</em></td><td class="inner">available for delay specializations</td></tr>
<tr><td class="inner"><em class=gen>mus-location</em></td><td class="inner">current delay line write position</td></tr>
</table>
<p>The delay generator is a delay line.
The make-delay "size" argument sets the delay line length (in samples).
Input fed into a delay line reappears at the output size samples later.
If "max-size" is specified in make-delay,
and it is larger than "size", the delay line can provide varying-length delays (including fractional amounts).
The delay generator's "pm" argument determines how far from the original "size" we are; that is,
it is difference between the length set by make-delay
and the current actual delay length, size + pm. So, a positive "pm" corresponds to a longer
delay line. See <a href="sndscm.html#zecho">zecho</a> in examp.scm for an example.
The make-delay "type" argument sets the interpolation type in the case of fractional delays:
mus-interp-none, mus-interp-linear, mus-interp-all-pass,
mus-interp-lagrange, mus-interp-bezier, or mus-interp-hermite.
Delay could be defined:
</p>
<pre class="indented">
(let ((result (<a class=quiet href="#array-interp">array-interp</a> line (- loc pm))))
(set! (line loc) input)
(set! loc (+ 1 loc))
(if (<= size loc) (set! loc 0))
result)
</pre>
<table>
<tr>
<td>
<div class="scheme">
<pre class="indented">
(with-sound (:play #t)
(let ((dly (make-delay (seconds->samples 0.5)))
(osc1 (make-oscil 440.0))
(osc2 (make-oscil 660.0)))
(do ((i 0 (+ i 1)))
((= i 44100))
(outa i (* 0.5
(+ (oscil osc1)
(delay dly (oscil osc2))))))))
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="ruby">
<pre class="indented">
with_sound(:play, true) do
dly = make_delay(seconds2samples(0.5));
osc1 = make_oscil(440.0);
osc2 = make_oscil(660.0);
44100.times do |i|
outa(i,
0.5 * (oscil(osc1) +
delay(dly, oscil(osc2))),
$output);
end
end.output
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="forth">
<pre class="indented">
lambda: ( -- )
0.5 seconds->samples make-delay { dly }
440.0 make-oscil { osc1 }
660.0 make-oscil { osc2 }
44100 0 do
i
osc1 0 0 oscil
dly osc2 0 0 oscil 0 delay f+
f2/ *output* outa drop
loop
; :play #t with-sound drop
</pre>
</div>
</td>
</tr>
</table>
<p>
The tap function taps a delay line at a given offset from the output point.
delay-tick is a function that just puts a sample in the delay line, 'ticks' the delay forward, and
returns its "input" argument.
See prc95.scm for examples of both of these functions.
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#definstrument">definstrument</a> (echo beg dur scaler secs file)
(let ((del (<em class=red>make-delay</em> (<a class=quiet href="#secondstosamples">seconds->samples</a> secs)))
(rd (make-sampler 0 file)))
(do ((i beg (+ i 1)))
((= i (+ beg dur)))
(let ((inval (rd)))
(<a class=quiet href="#outa">outa</a> i (+ inval (<em class=red>delay</em> del (* scaler (+ (<em class=red>tap</em> del) inval)))))))))
(<a class=quiet href="sndscm.html#withsound">with-sound</a> () (echo 0 60000 .5 1.0 "pistol.snd"))
</pre>
<p>The <a href="#mus-scaler">mus-scaler</a> field is available for simple extensions of the delay. For example,
the <a href="#moving-max">moving-max</a> generator uses mus-scaler to track the current maximum sample value
in the delay line; the result is an envelope that tracks the peak amplitude in the
last "size" samples.
The <a href="#mus-location">mus-location</a> field returns the current delay line write position.
To access the delay line contents as a sliding window on the input data, use:
</p>
<pre class="indented">
(define (delay-ref dly loc)
(float-vector-ref (mus-data dly) (modulo (+ loc (<em class=red>mus-location</em> dly)) (mus-length dly))))
</pre>
<p>
The delay generator is used in some reverbs (<a href="sndscm.html#nrev">nrev</a>), many physical
models (<a href="sndscm.html#stereoflute">stereo-flute</a>), <a href="sndscm.html#dlocsigdoc">dlocsig</a>,
chorus effects (<a href="sndscm.html#chorus">chorus</a> in dsp.scm), and flanging (<a href="sndscm.html#neweffectsdoc">new-effects</a>),
and is the basis for about a dozen extensions (comb and friends below).
</p>
<!-- COMB, NOTCH -->
<div class="innerheader" id="combdoc">comb, notch</div>
<pre class="indented">
<em class=def id="make-comb">make-comb</em> (scaler 1.0) size initial-contents (initial-element 0.0) max-size
<em class=def id="comb">comb</em> cflt input (pm 0.0)
<em class=def id="comb?">comb?</em> cflt
<em class=def id="combbank">comb-bank</em> combs input
<em class=def id="combbankp">comb-bank?</em> object
<em class=def id="makecombbank">make-comb-bank</em> combs
<em class=def id="make-filtered-comb">make-filtered-comb</em> (scaler 1.0) size initial-contents (initial-element 0.0) max-size filter
<em class=def id="filtered-comb">filtered-comb</em> cflt input (pm 0.0)
<em class=def id="filtered-comb?">filtered-comb?</em> cflt
<em class=def id="filteredcombbank">filtered-comb-bank</em> fcombs input
<em class=def id="filteredcombbankp">filtered-comb-bank?</em> object
<em class=def id="makefilteredcombbank">make-filtered-comb-bank</em> fcombs
<em class=def id="make-notch">make-notch</em> (scaler 1.0) size initial-contents (initial-element 0.0) max-size
<em class=def id="notch">notch</em> cflt input (pm 0.0)
<em class=def id="notch?">notch?</em> cflt
</pre>
<table class="method">
<tr><td colspan=2 class="methodtitle">comb, filtered-comb, and notch methods</td></tr>
<tr><td class="inner"><em class=gen>mus-length</em></td><td class="inner">length of delay</td></tr>
<tr><td class="inner"><em class=gen>mus-order</em></td><td class="inner">same as mus-length</td></tr>
<tr><td class="inner"><em class=gen>mus-data</em></td><td class="inner">delay line itself (no set!)</td></tr>
<tr><td class="inner"><em class=gen>mus-feedback</em></td><td class="inner">scaler (comb only)</td></tr>
<tr><td class="inner"><em class=gen>mus-feedforward</em></td><td class="inner">scaler (notch only)</td></tr>
<tr><td class="inner"><em class=gen>mus-interp-type</em></td><td class="inner">interpolation choice (no set!)</td></tr>
</table>
<p>The comb generator is a delay line with a scaler on the feedback. notch
is a delay line with a scaler on the current input.
filtered-comb is a comb filter with a filter on the feedback.
Although normally this is a <a href="#one-zero">one-zero</a> filter, it can be any CLM generator.
The make-<gen> "size" argument sets the length
in samples of the delay line,
and the other arguments are also handled as in <a href="#delay">delay</a>.
</p>
<pre class="indented">
comb: y(n) = x(n - size) + scaler * y(n - size)
notch: y(n) = x(n) * scaler + x(n - size)
filtered-comb: y(n) = x(n - size) + scaler * filter(y(n - size))
</pre>
<img class="indented" src="pix/comb.png" alt="sonogram of comb">
<!-- 1024 blackman2 dB jet light=1 data-cutoff around .009 invert off using the local zc (not clm-ins!): (with-sound (:srate 44100) (zc 0 2 2000 .1 100 1000 .99))
-->
<table>
<tr>
<td>
<div class="scheme">
<pre class="indented">
(with-sound (:play #t)
(let ((cmb (make-comb 0.4 (seconds->samples 0.4)))
(osc (make-oscil 440.0))
(ampf (make-env '(0 0 1 1 2 1 3 0) :length 4410)))
(do ((i 0 (+ i 1)))
((= i 88200))
(outa i (* 0.5 (comb cmb (* (env ampf) (oscil osc))))))))
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="ruby">
<pre class="indented">
with_sound(:play, true) do
cmb = make_comb(0.4, seconds2samples(0.4));
osc = make_oscil(440.0);
ampf = make_env([0.0, 0.0, 1.0, 1.0, 2.0, 1.0, 3.0, 0.0], :length, 4410);
88200.times do |i|
outa(i, 0.5 * (comb(cmb, env(ampf) * oscil(osc))), $output);
end
end.output
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="forth">
<pre class="indented">
lambda: ( -- )
0.4 0.4 seconds->samples make-comb { cmb }
440.0 make-oscil { osc }
'( 0 0 1 1 2 1 3 0 ) :length 4410 make-env { ampf }
88200 0 do
i
cmb ( gen )
ampf env osc 0 0 oscil f* ( val )
0 ( pm )
comb f2/ *output* outa drop
loop
; :play #t with-sound drop
</pre>
</div>
</td>
</tr>
</table>
<p>As a rule of thumb, the decay time of the feedback is
7.0 * size / (1.0 - scaler) samples, so to get a decay of feedback-dur seconds,
</p>
<pre class="indented">
(make-comb :size size :scaler (- 1.0 (/ (* 7.0 size) feedback-dur *clm-srate*)))
</pre>
<p>The peak gain is 1.0 / (1.0 - (abs scaler)). The peaks (or valleys in notch's case) are evenly spaced
at *clm-srate* / size. The height (or depth) thereof is determined by scaler —
the closer to 1.0 it is, the more pronounced the dips or peaks.
See Julius Smith's "An Introduction to Digital Filter Theory" in
Strawn "Digital Audio Signal Processing", or Smith's "Music Applications of
Digital Waveguides".
The following instrument sweeps the comb filter using the pm argument:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#definstrument">definstrument</a> (zc time dur freq amp length1 length2 feedback)
(let* ((beg (<a class=quiet href="#secondstosamples">seconds->samples</a> time))
(end (+ beg (<a class=quiet href="#secondstosamples">seconds->samples</a> dur)))
(s (<a class=quiet href="#make-pulse-train">make-pulse-train</a> :frequency freq)) ; some raspy input so we can hear the effect easily
(d0 (<em class=red>make-comb</em> :size length1 :max-size (max length1 length2) :scaler feedback))
(aenv (<a class=quiet href="#make-env">make-env</a> '(0 0 .1 1 .9 1 1 0) :scaler amp :duration dur))
(zenv (<a class=quiet href="#make-env">make-env</a> '(0 0 1 1) :scaler (- length2 length1) :base 12.0 :duration dur)))
(do ((i beg (+ i 1))) ((= i end))
(<a class=quiet href="#outa">outa</a> i (* (<a class=quiet href="#env">env</a> aenv) (<em class=red>comb</em> d0 (<a class=quiet href="#pulse-train">pulse-train</a> s) (<a class=quiet href="#env">env</a> zenv)))))))
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> ()
(zc 0 3 100 .1 20 100 .5)
(zc 3.5 3 100 .1 90 100 .95))
</pre>
<p>Nearly every actual use of comb filters involves a bank of them, a vector of combs
summed in parallel. The comb-bank generator is intended for this kind of application.
make-comb-bank takes a vector of combs and returns the comb-bank generator which can
be called via comb-bank.
</p>
<pre class="indented">
(do ((sum 0.0)
(i 0 (+ i 1)))
((= i n) sum)
(set! sum (+ sum (comb (combs i) x))))
</pre>
<p>can be replaced with:
</p>
<pre class="indented">
(let ((cb (make-comb-bank combs)))
...
(comb-bank cb x))
</pre>
<p>The comb filter can produce some nice effects; here's one that treats the comb filter's
delay line as the coefficients for an FIR filter:
</p>
<pre class="indented">
(define (fir+comb beg dur freq amp size)
(let* ((start (<a class=quiet href="#secondstosamples">seconds->samples</a> beg))
(end (+ start (<a class=quiet href="#secondstosamples">seconds->samples</a> dur)))
(dly (<em class=red>make-comb</em> :scaler .9 :size size))
(flt (<a class=quiet href="#make-fir-filter">make-fir-filter</a> :order size :xcoeffs (<em class=red>mus-data</em> dly))) ; comb delay line as FIR coeffs
(r (<a class=quiet href="#make-rand">make-rand</a> freq))) ; feed comb with white noise
(do ((i start (+ i 1)))
((= i end))
(<a class=quiet href="#outa">outa</a> i (* amp (<a class=quiet href="#fir-filter">fir-filter</a> flt (<em class=red>comb</em> dly (<a class=quiet href="#rand">rand</a> r))))))))
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> ()
(fir+comb 0 2 10000 .001 200)
(fir+comb 2 2 1000 .0005 400)
(fir+comb 4 2 3000 .001 300)
(fir+comb 6 2 3000 .0005 1000))
</pre>
<p>Here's another that fluctuates between two sets of combs; it usually works best with voice sounds. We use comb-bank generators:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#definstrument">definstrument</a> (flux start-time file frequency combs0 combs1 (scaler 0.99) (comb-len 32))
(let* ((beg (<a class=quiet href="#secondstosamples">seconds->samples</a> start-time))
(end (+ beg (<a class=quiet href="extsnd.html#mussoundframples">mus-sound-framples</a> file)))
(num-combs0 (length combs0))
(num-combs1 (length combs1))
(cmbs0 (make-vector num-combs0))
(cmbs1 (make-vector num-combs1))
(osc (<a class=quiet href="#make-oscil">make-oscil</a> frequency))
(rd (<a class=quiet href="#make-readin">make-readin</a> file)))
(do ((k 0 (+ k 1)))
((= k num-combs0))
(set! (cmbs0 k)
(<em class=red>make-comb</em> scaler
(floor (* comb-len (combs0 k))))))
(do ((k 0 (+ k 1)))
((= k num-combs1))
(set! (cmbs1 k)
(<em class=red>make-comb</em> scaler
(floor (* comb-len (combs1 k))))))
(let ((nc0 (<em class=red>make-comb-bank</em> cmbs0))
(nc1 (<em class=red>make-comb-bank</em> cmbs1)))
(do ((i beg (+ i 1)))
((= i end))
(let ((interp (<a class=quiet href="#oscil">oscil</a> osc))
(x (<a class=quiet href="#readin">readin</a> rd)))
(<a class=quiet href="#outa">outa</a> i (+ (* interp (<em class=red>comb-bank</em> nc0 x))
(* (- 1.0 interp) (<em class=red>comb-bank</em> nc1 x)))))))))
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:scaled-to .5)
(flux 0 "oboe.snd" 10.0 '(1.0 1.25 1.5) '(1.0 1.333 1.6)) ; bowed oboe?
(flux 2 "now.snd" 4.0 '(1.0 1.25 1.5) '(1.0 1.333 1.6 2.0 3.0))
(flux 4 "now.snd" 1.0 '(1.0 1.25 1.5) '(1.0 1.333 1.6 2.0 3.0) 0.995 20)
(flux 6 "now.snd" 10.0 '(1.0 1.25 1.5) '(1.0 1.333 1.6 2.0 3.0) 0.99 10)
(flux 8 "now.snd" 10.0 '(2.0) '(1.0 1.333 1.6 2.0 3.0) 0.99 120)
(flux 10 "fyow.snd" .50 '(1.0 2.0 1.5) '(1.0 1.333 1.6 2.0 3.0) 0.99 120))
</pre>
<p>For more comb filter examples,
see examp.scm, <a href="sndscm.html#chordalize">chordalize</a> in dsp.scm, or
any of the standard reverbs such as <a href="sndscm.html#nrev">nrev</a>.
</p>
<br>
<p>filtered-comb is used in <a href="sndscm.html#freeverb">freeverb</a>
where a <a href="#one-zero">one-zero</a> filter is placed
in the feedback loop:
</p>
<pre class="indented">
(make-filtered-comb :size len :scaler room-decay-val :filter (make-one-zero :a0 (- 1.0 dmp) :a1 dmp))
</pre>
<p>As with the normal comb filter, the filtered-comb-bank generator sums a vector of filtered-comb
generators in parallel.
</p>
<!-- ALL-PASS -->
<div class="innerheader" id="all-passdoc">all-pass</div>
<pre class="indented">
<em class=def id="make-all-pass">make-all-pass</em>
(feedback 0.0)
(feedforward 0.0)
size
initial-contents
(initial-element 0.0)
max-size
<em class=def id="all-pass">all-pass</em> f input (pm 0.0)
<em class=def id="all-pass?">all-pass?</em> f
<em class=def id="allpassbank">all-pass-bank</em> all-passes input
<em class=def id="allpassbankp">all-pass-bank?</em> object
<em class=def id="makeallpassbank">make-all-pass-bank</em> all-passes
<em class=def id="make-one-pole-all-pass">make-one-pole-all-pass</em> size coeff
<em class=def id="one-pole-all-pass">one-pole-all-pass</em> f input
<em class=def id="one-pole-all-pass?">one-pole-all-pass?</em> f
</pre>
<table class="method">
<tr><td colspan=2 class="methodtitle">all-pass methods</td></tr>
<tr><td class="inner"><em class=gen>mus-length</em></td><td class="inner">length of delay</td></tr>
<tr><td class="inner"><em class=gen>mus-order</em></td><td class="inner">same as mus-length</td></tr>
<tr><td class="inner"><em class=gen>mus-data</em></td><td class="inner">delay line itself (no set!)</td></tr>
<tr><td class="inner"><em class=gen>mus-feedback</em></td><td class="inner">feedback scaler</td></tr>
<tr><td class="inner"><em class=gen>mus-feedforward</em></td><td class="inner">feedforward scaler</td></tr>
<tr><td class="inner"><em class=gen>mus-interp-type</em></td><td class="inner">interpolation choice (no set!)</td></tr>
</table>
<p>The all-pass or moving average comb generator is just like <a href="#comb">comb</a> but with
an added scaler on the input ("feedforward" is Julius Smith's suggested name for it). If feedforward is 0.0, we get a
comb filter. If both scale terms are 0.0, we get a pure delay line.
</p>
<pre class="indented">
y(n) = feedforward * x(n) + x(n - size) + feedback * y(n - size)
</pre>
<table>
<tr>
<td>
<div class="scheme">
<pre class="indented">
(with-sound (:play #t)
(let ((alp (make-all-pass -0.4 0.4 (seconds->samples 0.4)))
(osc (make-oscil 440.0))
(ampf (make-env '(0 0 1 1 2 1 3 0) :length 4410)))
(do ((i 0 (+ i 1)))
((= i 88200))
(outa i (* 0.5 (all-pass alp (* (env ampf) (oscil osc))))))))
</pre>
</div>
</td>
</tr>
<tr>
<td>
<div class="ruby">
<pre class="indented">
with_sound(:play, true) do
alp = make_all_pass(-0.4, 0.4, seconds2samples(0.4));
osc = make_oscil(440.0);
ampf = make_env([0.0, 0.0, 1.0, 1.0, 2.0, 1.0, 3.0, 0.0], :length, 4410);
88200.times do |i|
outa(i, 0.5 * (all_pass(alp, env(ampf) * oscil(osc))), $output);
end
end.output
</pre>
</div>
</td>
</tr>
<tr>
<td>
<div class="forth">
<pre class="indented">
lambda: ( -- )
-0.4 0.4 0.4 seconds->samples make-all-pass { alp }
440.0 make-oscil { osc }
'( 0 0 1 1 2 1 3 0 ) :length 4410 make-env { ampf }
88200 0 do
i
alp ( gen )
ampf env osc 0 0 oscil f* ( val )
0 ( pm )
all-pass f2/ *output* outa drop
loop
; :play #t with-sound drop
</pre>
</div>
</td>
</tr>
</table>
<p>all-pass filters are used extensively in reverberation;
see <a href="sndscm.html#jcrevdoc">jcrev</a> or <a href="sndscm.html#nrev">nrev</a>.
To get the "all-pass" behavior, set feedback equal to -feedforward. Here's an example
(based on John Chowning's ancient reverb) that was inspired by the bleed-through you get on
old analog tapes — the reverb slightly precedes the direct signal:
</p>
<pre class="indented">
(define (later file dly rev)
(let ((allpass1 (<em class=red>make-all-pass</em> -0.700 0.700 1051))
(allpass2 (<em class=red>make-all-pass</em> -0.700 0.700 337))
(allpass3 (<em class=red>make-all-pass</em> -0.700 0.700 113))
(comb1 (make-comb 0.742 4799))
(comb2 (make-comb 0.733 4999))
(comb3 (make-comb 0.715 5399))
(comb4 (make-comb 0.697 5801))
(len (floor (+ *clm-srate* (mus-sound-framples file))))
(rd (make-readin file)) ; the direct signal (via sound-let below)
(d (make-delay dly))) ; this delays the direct signal
(do ((backup (min 4799 dly))
(i 0 (+ i 1)))
((= i len))
(let* ((inval (readin rd))
(allpass-sum (<em class=red>all-pass</em> allpass3
(<em class=red>all-pass</em> allpass2
(<em class=red>all-pass</em> allpass1
(* rev inval)))))
(comb-sum
(+ (comb comb1 allpass-sum)
(comb comb2 allpass-sum)
(comb comb3 allpass-sum)
(comb comb4 allpass-sum)))
(orig (delay d inval)))
(if (>= i backup)
(outa (- i backup) (+ comb-sum orig)))))))
(with-sound ()
(sound-let ((tmp () (fm-violin 0 .1 440 .1)))
(later tmp 10000 .1)))
</pre>
<p>In all such applications, the all-pass filters are connected in series (each one's output is the
input to the next in the set). To package this up in one generator, use an all-pass-bank. An
all-pass-bank is slightly different from the other "bank" generators in that it connects the
vector of all-passes in series, rather than summing them in parallel.
Code of the form:
</p>
<pre class="indented">
(all-pass a1 (all-pass a2 input))
</pre>
<p>can be replaced with:
</p>
<pre class="indented">
(all-pass-bank (make-all-pass-bank (vector a1 a2)) input)
</pre>
<p>one-pole-all-pass is used by piano.scm:
</p>
<pre class="indented">
y(n) = x(n) + coeff * (y(n-1) - y(n))
x(n) = y(n-1)
</pre>
<p>This is repeated "size" times, with the generator input as the first y(n-1) value.
</p>
<!-- MOVING-AVERAGE. MOVING-MAX. MOVING-NORM. -->
<div class="innerheader" id="moving-averagedoc">moving-average, moving-max, moving-norm</div>
<pre class="indented">
<em class=def id="make-moving-average">make-moving-average</em> size initial-contents (initial-element 0.0)
<em class=def id="moving-average">moving-average</em> f input
<em class=def id="moving-average?">moving-average?</em> f
<em class=def id="make-moving-max">make-moving-max</em> size initial-contents (initial-element 0.0)
<em class=def id="moving-max">moving-max</em> f input
<em class=def id="moving-max?">moving-max?</em> f
<em class=def id="make-moving-norm">make-moving-norm</em> size (scaler 1.0)
<em class=def id="moving-norm">moving-norm</em> f input
<em class=def id="moving-norm?">moving-norm?</em> f
</pre>
<table class="method">
<tr><td colspan=2 class="methodtitle">moving-average methods</td></tr>
<tr><td class="inner"><em class=gen>mus-length</em></td><td class="inner">length of table</td></tr>
<tr><td class="inner"><em class=gen>mus-order</em></td><td class="inner">same as mus-length</td></tr>
<tr><td class="inner"><em class=gen>mus-data</em></td><td class="inner">table of last 'size' values</td></tr>
</table>
<p>The moving-average or moving window average generator returns the average of the last "size" values input to it.
</p>
<pre class="indented">
result = sum-of-last-n-inputs / n
</pre>
<table>
<tr>
<td>
<div class="scheme">
<pre class="indented">
(with-sound (:play #t)
(let ((avg (make-moving-average 4410))
(osc (make-oscil 440.0))
(stop (- 44100 4410)))
(do ((i 0 (+ i 1)))
((= i stop))
(let ((val (oscil osc)))
(outa i (* val (moving-average avg (abs val))))))
(do ((i stop (+ i 1)))
((= i 44100))
(outa i (* (oscil osc) (moving-average avg 0.0))))))
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="ruby">
<pre class="indented">
with_sound(:play, true) do
avg = make_moving_average(4410);
osc = make_oscil(440.0);
stop = 44100 - 4410;
stop.times do |i|
val = oscil(osc);
outa(i, val * moving_average(avg, val.abs), $output);
end
4410.times do |i|
outa(stop + i, oscil(osc) * moving_average(avg, 0.0), $output);
end
end.output</pre>
</div>
</td>
</tr><tr>
<td>
<div class="forth">
<pre class="indented">
lambda: ( -- )
4410 make-moving-average { avg }
440.0 make-oscil { osc }
44100 4410 - { stop }
0.0 { val }
stop 0 do
osc 0 0 oscil to val
i avg val fabs moving-average val f* *output* outa drop
loop
44100 stop do
i avg 0.0 moving-average osc 0 0 oscil f* *output* outa drop
loop
; :play #t with-sound drop
</pre>
</div>
</td>
</tr>
</table>
<p>
moving-average is used both to track rms values and to generate ramps between 0 and 1 in a "gate"
effect in new-effects.scm and in rms-envelope in env.scm. It can also be viewed as a low-pass filter.
And
in <a href="sndscm.html#soundstosegmentdata">sounds->segment-data</a> in examp.scm, it is used to segment a sound library.
Here is an example (from new-effects.scm) that implements a "squelch" effect,
throwing away any samples below a threshhold, and ramping between portions
that are squelched and those that are unchanged (to avoid clicks):
</p>
<pre class="indented">
(define (squelch-channel amount snd chn gate-size) ; gate-size = ramp length and rms window length
(let ((gate (<em class=red>make-moving-average</em> gate-size))
(ramp (<em class=red>make-moving-average</em> gate-size :initial-element 1.0)))
(<a class=quiet href="extsnd.html#mapchannel">map-channel</a> (lambda (y)
(* y (<em class=red>moving-average</em> ramp ; ramp between 0 and 1
(if (< (<em class=red>moving-average</em> gate (* y y)) amount) ; local (r)ms value
0.0 ; below "amount" so squelch
1.0))))
0 #f snd chn)))
</pre>
<p>
moving-max is a specialization
of the <a href="#delay">delay</a> generator; it produces an envelope that tracks the peak amplitude of the last 'n' samples.
<code>(make-moving-max 256)</code> returns the generator (this one's window size is 256),
and <code>(moving-max gen y)</code> then returns the envelope traced out by the signal 'y'.
The <a href="sndscm.html#harmonicizer">harmonicizer</a> uses this generator to normalize an in-coming signal to 1.0
so that the Chebyshev polynomials it is driving will produce a full spectrum at all times.
Here is a similar, but simpler, example; we use the moving-max generator to track the
current peak amplitude over a small window, use that value to drive a <a href="#contrast-enhancement">contrast-enhancement</a>
generator (so that its output is always fully modulated), and rescale by the same value
upon output (to track the original sound's amplitude envelope):
</p>
<pre class="indented">
(define (intensify index)
(let ((mx (<em class=red>make-moving-max</em>))
(flt (<a class=quiet href="sndscm.html#makelowpass">make-lowpass</a> (* pi .1) 8))) ; smooth the maxamp signal
(<a class=quiet href="extsnd.html#mapchannel">map-channel</a> (lambda (y)
(let ((amp (max .1 (<a class=quiet href="#fir-filter">fir-filter</a> flt (<em class=red>moving-max</em> mx y)))))
(* amp (<a class=quiet href="#contrast-enhancement">contrast-enhancement</a> (/ y amp) index)))))))
</pre>
<p>moving-norm specializes moving-max to provide automatic gain control. It is essentially a one-pole (low-pass)
filter on the output of moving-max, inverted and multiplied by a scaler. <code>(* input-signal (moving-norm g input-signal))</code>
is the normal usage.
</p>
<p>
See generators.scm for several related functions:
<a href="#moving-rms">moving-rms</a>, <a href="#moving-sum">moving-sum</a>,
<a href="#moving-length">moving-length</a>, <a href="#weighted-moving-average">weighted-moving-average</a>, and
<a href="#exponentially-weighted-moving-average">exponentially-weighted-moving-average</a>
(the latter being just a one-pole filter).
</p>
<!-- SRC -->
<div class="innerheader" id="srcdoc">src</div>
<pre class="indented">
<em class=def id="make-src">make-src</em> input (srate 1.0) (width 5)
<em class=def id="src">src</em> s (sr-change 0.0)
<em class=def id="src?">src?</em> s
</pre>
<table class="method">
<tr><td colspan=2 class="methodtitle">src methods</td></tr>
<tr><td class="inner"><em class=gen>mus-increment</em></td><td class="inner">srate arg to make-src</td></tr>
</table>
<p>The src generator performs sampling rate conversion
by convolving its input with a sinc
function.
make-src's "srate" argument is the
ratio between the old sampling rate and the new; an srate of 2 causes the sound to be half as long, transposed up an octave.
</p>
<table>
<tr>
<td>
<div class="scheme">
<pre class="indented">
(with-sound (:play #t :srate 22050)
(let* ((rd (make-readin "oboe.snd"))
(len (* 2 (mus-sound-framples "oboe.snd")))
(sr (make-src rd 0.5)))
(do ((i 0 (+ i 1)))
((= i len))
(outa i (src sr)))))
</pre>
</div>
</td>
</tr>
<tr>
<td>
<div class="ruby">
<pre class="indented">
with_sound(:play, true, :srate, 22050) do
rd = make_readin("oboe.snd");
len = 2 * mus_sound_framples("oboe.snd");
sr = make_src(lambda do |dir|
readin(rd) end, 0.5);
len.times do |i|
outa(i, src(sr), $output);
end
end.output
</pre>
</div>
</td>
</tr>
<tr>
<td>
<div class="forth">
<pre class="indented">
lambda: ( -- )
"oboe.snd" make-readin { rd }
rd 0.5 make-src { sr }
"oboe.snd" mus-sound-framples 2* ( len ) 0 do
i sr 0 #f src *output* outa drop
loop
; :play #t :srate 22050 with-sound drop
</pre>
</div>
</td>
</tr>
</table>
<p>
The "width" argument sets how many neighboring samples to convolve with the sinc function.
If you hear high-frequency artifacts in the conversion, try increasing this number;
Perry Cook's default value is 40, and I've seen cases where it needs to be 100.
It can also be set as low as 2 in some cases.
The greater the width, the slower the src generator runs.
</p>
<p>
The src generator's "sr-change"
argument is the amount to add to the current srate on a sample by sample
basis (if it's 0.0 and the original make-src srate argument was also 0.0, you get a constant output because the generator is not moving at all).
Here's
an instrument that provides time-varying sampling rate conversion:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#definstrument">definstrument</a> (simple-src start-time duration amp srt srt-env filename)
(let* ((senv (<a class=quiet href="#make-env">make-env</a> srt-env :duration duration))
(beg (<a class=quiet href="#secondstosamples">seconds->samples</a> start-time))
(end (+ beg (<a class=quiet href="#secondstosamples">seconds->samples</a> duration)))
(src-gen (<em class=red>make-src</em> :input (<a class=quiet href="#make-readin">make-readin</a> filename) :srate srt)))
(do ((i beg (+ i 1)))
((= i end))
(<a class=quiet href="#outa">outa</a> i (* amp (<em class=red>src</em> src-gen (<a class=quiet href="#env">env</a> senv)))))))
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> () (simple-src 0 4 1.0 0.5 '(0 1 1 2) "oboe.snd"))
</pre>
<p id="srcer">src can provide an all-purpose "Forbidden Planet" sound effect:</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#definstrument">definstrument</a> (srcer start-time duration amp srt fmamp fmfreq filename)
(let* ((os (<a class=quiet href="#make-oscil">make-oscil</a> fmfreq))
(beg (<a class=quiet href="#secondstosamples">seconds->samples</a> start-time))
(end (+ beg (<a class=quiet href="#secondstosamples">seconds->samples</a> duration)))
(src-gen (<em class=red>make-src</em> :input (<a class=quiet href="#make-readin">make-readin</a> filename) :srate srt)))
(do ((i beg (+ i 1)))
((= i end))
(<a class=quiet href="#outa">outa</a> i (* amp (<em class=red>src</em> src-gen (* fmamp (<a class=quiet href="#oscil">oscil</a> os))))))))
(<a class=quiet href="sndscm.html#withsound">with-sound</a> () (srcer 0 2 1.0 1 .3 20 "fyow.snd"))
(<a class=quiet href="sndscm.html#withsound">with-sound</a> () (srcer 0 25 10.0 .01 1 10 "fyow.snd"))
(<a class=quiet href="sndscm.html#withsound">with-sound</a> () (srcer 0 2 1.0 .9 .05 60 "oboe.snd"))
(<a class=quiet href="sndscm.html#withsound">with-sound</a> () (srcer 0 2 1.0 1.0 .5 124 "oboe.snd"))
(<a class=quiet href="sndscm.html#withsound">with-sound</a> () (srcer 0 2 10.0 .01 .2 8 "oboe.snd"))
(<a class=quiet href="sndscm.html#withsound">with-sound</a> () (srcer 0 2 1.0 1 3 20 "oboe.snd"))
</pre>
<p>The "input" argument to make-src and the "input-function" argument
to src provide the generator with input as it is needed.
The input function
is a function of one argument (the desired read direction, if the reader can support it), that is called each time src needs another
sample of input. Here's an example instrument that reads a file with an envelope on the src:
</p>
<pre class="indented">
(definstrument (src-change filename start-time duration file-start srcenv)
(let* ((beg (seconds->samples start-time))
(end (+ beg (seconds->samples duration)))
(loc (seconds->samples file-start))
(src-gen (make-src :srate 0.0))
(e (make-env srcenv :duration duration))
(inp (make-file->sample filename)))
(do ((i beg (+ i 1)))
((= i end))
(outa i (src src-gen (env e)
(<em class=red>lambda (dir)</em> ; our input function
(set! loc (+ loc dir))
(ina loc inp)))))))
;;; (with-sound () (src-change "pistol.snd" 0 2 0 '(0 0.5 1 -1.5)))
</pre>
<p>
If you jump around in the input (via mus-location for example), use
<a href="#mus-reset">mus-reset</a> to clear out any lingering state before starting to read at
the new position. (src, like many other generators, has an internal buffer
of recently read samples, so a sudden jump to a new location will otherwise cause
a click).
</p>
<p>There are several other ways to resample a sound. Some of the more interesting ones are in
dsp.scm (<a href="sndscm.html#downoct">down-oct</a>, <a href="sndscm.html#soundinterp">sound-interp</a>,
<a href="sndscm.html#linearsrcchannel">linear-src</a>, etc). To calculate a sound's new duration after
a time-varying src is applied, use <a href="sndscm.html#srcduration">src-duration</a>. To scale an src
envelope so that the result has a given duration, use <a href="sndscm.html#srcfitenvelope">scr-fit-envelope</a>.
</p>
<!-- CONVOLVE -->
<div class="innerheader" id="convolvedoc">convolve</div>
<pre class="indented">
<em class=def id="make-convolve">make-convolve</em> input filter fft-size filter-size
<em class=def id="convolve">convolve</em> gen
<em class=def id="convolve?">convolve?</em> gen
<em class=def id="convolvefiles">convolve-files</em> file1 file2 (maxamp 1.0) (output-file "tmp.snd")
</pre>
<table class="method">
<tr><td colspan=2 class="methodtitle">convolve methods</td></tr>
<tr><td class="inner"><em class=gen>mus-length</em></td><td class="inner">fft size used in the convolution</td></tr>
</table>
<p>The convolve generator convolves its input with the impulse response "filter" (a float-vector).
"input" is a function of one argument that is
called whenever convolve needs input.
</p>
<table>
<tr>
<td>
<div class="scheme">
<pre class="indented">
(with-sound (:play #t :statistics #t)
(let ((cnv (make-convolve
(make-readin "pistol.snd")
(samples 0 (framples "pistol.snd") "oboe.snd"))))
(do ((i 0 (+ i 1)))
((= i 88200))
(outa i (* 0.25 (convolve cnv))))))
</pre>
</div>
</td>
</tr>
<tr>
<td>
<div class="ruby">
<pre class="indented">
with_sound(:play, true, :statistics, true) do
rd = make_readin("oboe.snd");
flt = file2vct("pistol.snd"); # examp.rb
cnv = make_convolve(lambda { |dir| readin(rd)}, flt);
88200.times do |i|
outa(i, 0.25 * convolve(cnv), $output);
end
end.output
</pre>
</div>
</td>
</tr>
<tr>
<td>
<div class="forth">
<pre class="indented">
lambda: ( -- )
"pistol.snd" make-readin ( rd )
"oboe.snd" file->vct ( v ) make-convolve { cnv }
88200 0 do
i cnv #f convolve 0.25 f* *output* outa drop
loop
; :play #t :statistics #t with-sound drop
</pre>
</div>
</td>
</tr>
</table>
<table>
<tr>
<td>
<div class="scheme">
<pre class="indented">
(with-sound (:play #t)
(let* ((tempfile (convolve-files "oboe.snd"
"pistol.snd" 0.5
"convolved.snd"))
(len (mus-sound-framples tempfile))
(reader (make-readin tempfile)))
(do ((i 0 (+ i 1)))
((= i len))
(outa i (readin reader)))
(delete-file tempfile)))
</pre>
</div>
</td>
</tr>
<tr>
<td>
<div class="ruby">
<pre class="indented">
with_sound(:play, true) do
tempfile = convolve_files("oboe.snd",
"pistol.snd", 0.5,
"convolved.snd");
len = mus_sound_framples(tempfile);
reader = make_readin(tempfile);
len.times do |i|
outa(i, readin(reader), $output);
end
File.unlink(tempfile)
end.output
</pre>
</div>
</td>
</tr>
<tr>
<td>
<div class="forth">
<pre class="indented">
lambda: ( -- )
"oboe.snd" "pistol.snd" 0.5 "convolved.snd" convolve-files { tempfile }
tempfile make-readin { reader }
tempfile mus-sound-framples ( len ) 0 do
i reader readin *output* outa drop
loop
tempfile file-delete
; :play #t with-sound drop
</pre>
</div>
</td>
</tr>
</table>
<pre class="indented">
(<a class=quiet href="sndscm.html#definstrument">definstrument</a> (convins beg dur filter file (size 128))
(let* ((start (<a class=quiet href="#secondstosamples">seconds->samples</a> beg))
(end (+ start (<a class=quiet href="#secondstosamples">seconds->samples</a> dur)))
(ff (<em class=red>make-convolve</em> :input (<a class=quiet href="#make-readin">make-readin</a> file) :fft-size size :filter filter)))
(do ((i start (+ i 1)))
((= i end))
(<a class=quiet href="#outa">outa</a> i (<em class=red>convolve</em> ff)))))
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> ()
(convins 0 2 (float-vector 1.0 0.5 0.25 0.125) "oboe.snd")) ; same as fir-filter with those coeffs
</pre>
<p>convolve-files handles a very common special case: convolve
two files, then normalize the result to some maxamp. The convolve generator does not
know in advance what its maxamp will be, and when the two files are more or less
the same size, there's no real computational savings from using overlap-add (i.e.
the generator), so a one-time giant FFT saved as a temporary sound file is much
handier. If you're particular about the format of the convolved data:
</p>
<pre class="indented">
(define* (convolve-files->aifc file1 file2 (maxamp 1.0) (output-file "test.snd"))
(let ((outname (string-append "temp-" output-file)))
(<em class=red>convolve-files</em> file1 file2 maxamp outname)
(with-sound (:header-type mus-aifc :sample-type mus-bfloat)
(let ((len (seconds->samples (mus-sound-duration outname)))
(reader (make-readin outname)))
(do ((i 0 (+ i 1)))
((= i len))
(outa i (readin reader)))))
(delete-file outname)
output-file))
</pre>
<p>The convolve generator is the modern way to add reverb. There are impulse responses of various concert
halls floating around the web. convolve and <a href="#fir-filter">fir-filter</a> actually perform the same mathematical operation,
but convolve uses an FFT internally, rather than a laborious dot-product.
</p>
<!-- GRANULATE -->
<!-- INDEX grains:Granular synthesis --><em class=def id="grains"></em>
<div class="innerheader" id="granulatedoc">granulate</div>
<pre class="indented">
<em class=def id="make-granulate">make-granulate</em>
input
(expansion 1.0) ; how much to lengthen or compress the file
(length .15) ; length of file slices that are overlapped
(scaler .6) ; amplitude scaler on slices (to avoid overflows)
(hop .05) ; speed at which slices are repeated in output
(ramp .4) ; amount of slice-time spent ramping up/down
(jitter 1.0) ; affects spacing of successive grains
max-size ; internal buffer size
edit ; grain editing function
<em class=def id="granulate">granulate</em> e
<em class=def id="granulate?">granulate?</em> e
</pre>
<table class="method">
<tr><td colspan=2 class="methodtitle">granulate methods</td></tr>
<tr><td class="inner"><em class=gen>mus-frequency</em></td><td class="inner">time (seconds) between output grains (hop)</td></tr>
<tr><td class="inner"><em class=gen>mus-ramp</em></td><td class="inner">length (samples) of grain envelope ramp segment</td></tr>
<tr><td class="inner"><em class=gen>mus-hop</em></td><td class="inner">time (samples) between output grains (hop)</td></tr>
<tr><td class="inner"><em class=gen>mus-scaler</em></td><td class="inner">grain amp (scaler)</td></tr>
<tr><td class="inner"><em class=gen>mus-increment</em></td><td class="inner">expansion</td></tr>
<tr><td class="inner"><em class=gen>mus-length</em></td><td class="inner">grain length (samples)</td></tr>
<tr><td class="inner"><em class=gen>mus-data</em></td><td class="inner">grain samples (a float-vector)</td></tr>
<tr><td class="inner"><em class=gen>mus-location</em></td><td class="inner">granulate's local random number seed</td></tr>
</table>
<p>The granulate generator "granulates" its input (normally a sound file). It is the poor man's way
to change the speed at which things happen in a recorded sound without
changing the pitches. It works by slicing the input file into short
pieces, then overlapping these slices to lengthen (or shorten) the
result; this process is sometimes known as granular synthesis, and is
similar to the freeze function.
</p>
<pre class="indented">
result = overlap add many tiny slices from input
</pre>
<table>
<tr>
<td>
<div class="scheme">
<pre class="indented">
(with-sound (:play #t)
(let ((grn (make-granulate (make-readin "oboe.snd") 2.0)))
(do ((i 0 (+ i 1)))
((= i 44100))
(outa i (granulate grn)))))
</pre>
</div>
</td>
</tr>
<tr>
<td>
<div class="ruby">
<pre class="indented">
with_sound(:play, true) do
rd = make_readin("oboe.snd");
grn = make_granulate(lambda do |dir| readin(rd) end, 2.0);
88200.times do |i|
outa(i, granulate(grn), $output);
end
end.output
</pre>
</div>
</td>
</tr>
<tr>
<td>
<div class="forth">
<pre class="indented">
lambda: ( -- )
"oboe.snd" make-readin 2.0 make-granulate { grn }
44100 0 do
i grn #f #f granulate *output* outa drop
loop
; :play #t with-sound drop
</pre>
</div>
</td>
</tr>
</table>
<table>
<tr>
<td>
<div class="scheme">
<pre class="indented">
(with-sound (:play #t)
(let* ((osc (make-oscil 440.0))
(sweep (make-env '(0 0 1 1)
:scaler (hz->radians 440.0)
:length 44100))
(grn (make-granulate (lambda (dir)
(* 0.2 (oscil osc (env sweep))))
:expansion 2.0
:length .5)))
(do ((i 0 (+ i 1)))
((= i 88200))
(outa i (granulate grn)))))
</pre>
</div>
</td>
</tr>
<tr>
<td>
<div class="ruby">
<pre class="indented">
with_sound(:play, true) do
osc = make_oscil(440.0);
sweep = make_env([0.0, 0.0, 1.0, 1.0],
:scaler, hz2radians(440.0),
:length, 44100);
grn = make_granulate(lambda { |dir| 0.2 * oscil(osc, env(sweep))},
:expansion, 2.0,
:length, 0.5);
88200.times do |i|
outa(i, granulate(grn), $output);
end
end.output
</pre>
</div>
</td>
</tr>
<tr>
<td>
<div class="forth">
<pre class="indented">
: make-granulate-proc { osc sweep -- prc; dir self -- val }
1 proc-create osc , sweep , ( prc )
does> { dir self -- val }
self @ ( osc ) self cell+ @ ( sweep ) env 0 oscil 0.2 f*
;
lambda: ( -- )
440.0 make-oscil { osc }
'( 0 0 1 1 ) :scaler 440.0 hz->radians :length 44100 make-env { sweep }
osc sweep make-granulate-proc :expansion 2.0 :length 0.5 make-granulate { grn }
88200 0 do
i grn #f #f granulate *output* outa drop
loop
; :play #t with-sound drop
</pre>
</div>
</td>
</tr>
</table>
<p>The duration of each slice is
"length" — the longer the slice, the more the effect resembles reverb. The
portion of the length (on a scale from 0 to 1.0) spent on each
ramp (up or down) is set by the "ramp" argument. It can control the smoothness of
the result of the overlaps.
</p>
<p>The "jitter" argument sets
the accuracy with which granulate hops. If you set it to 0 (no randomness), you can get very strong
comb filter effects, or tremolo.
The more-or-less average time between
successive segments is "hop".
If jitter is 0.0, and hop is very small (say .01),
you're asking for trouble (a big comb filter).
If you're granulating more than one channel at a time, and want the channels to remain
in-sync, make each granulator use the same initial random number seed (via mus-location).
</p>
<p>The overall amplitude scaler on each segment is set by the
"scaler" argument; this is used to try to avoid overflows as we add
all these zillions of segments together. "expansion"
determines the input hop in relation to the output hop; an
expansion-amount of 2.0 should more or less double the length of the
original, whereas an expansion-amount of 1.0 should return something
close to the original tempo.
"input" and "input-function" are the same as in src and convolve (functions of
one argument that return a new input sample whenever they are called by granulate).
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#definstrument">definstrument</a> (granulate-sound file beg dur (orig-beg 0.0) (exp-amt 1.0))
(let* ((f-srate (srate file))
(f (<a class=quiet href="#make-readin">make-readin</a> file :start (round (* f-srate orig-beg))))
(st (<a class=quiet href="#secondstosamples">seconds->samples</a> beg))
(new-dur (or dur (- (<a class=quiet href="extsnd.html#mussoundduration">mus-sound-duration</a> file) orig-beg)))
(exA (<em class=red>make-granulate</em> :input f :expansion exp-amt))
(nd (+ st (<a class=quiet href="#secondstosamples">seconds->samples</a> new-dur))))
(do ((i st (+ i 1)))
((= i nd))
(<a class=quiet href="#outa">outa</a> i (<em class=red>granulate</em> exA)))))
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> () (granulate-sound "now.snd" 0 3.0 0 2.0))
</pre>
<p>See <a href="sndscm.html#expsrc">clm-expsrc</a> in clm-ins.scm. Here's an instrument that uses the input-function
argument to granulate. It cause the granulation to run backwards through the file:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#definstrument">definstrument</a> (grev beg dur exp-amt file file-beg)
(let ((exA (<em class=red>make-granulate</em> :expansion exp-amt))
(fil (<a class=quiet href="#make-filetosample">make-file->sample</a> file))
(ctr file-beg))
(do ((i beg (+ i 1)))
((= i (+ beg dur)))
(<a class=quiet href="#outa">outa</a> i (<em class=red>granulate</em> exA
(lambda (dir)
(let ((inval (<a class=quiet href="#filetosample">file->sample</a> fil ctr 0)))
(if (> ctr 0) (set! ctr (- ctr 1)))
inval)))))))
(<a class=quiet href="sndscm.html#withsound">with-sound</a> () (grev 0 100000 2.0 "pistol.snd" 40000))
</pre>
<p>But it's unnecessary to write clever input functions. It is just as fast, and much more perspicuous,
to use sound-let in cases like this. Here's an example that takes any set of notes and calls granulate
on the result:
</p>
<pre class="indented">
(define-macro (gran-any beg dur expansion . body)
`(<em class=red>sound-let</em> ((tmp () ,@body))
(let* ((start (floor (* *clm-srate* ,beg)))
(end (+ start (* *clm-srate* ,dur)))
(rd (make-readin tmp))
(gran (<em class=red>make-granulate</em> :input rd :expansion ,expansion)))
(do ((i start (+ i 1)))
((= i end))
(outa i (granulate gran))))))
(with-sound ()
(gran-any 0 2.5 4
(fm-violin 0 .1 440 .1)
(fm-violin .2 .1 660 .1)
(fm-violin .4 .1 880 .1)))
</pre>
<p>Any of the input-oriented generators (src, etc) can use this trick.
</p>
<p>
The "edit" argument can
be a function of one argument, the current granulate generator. It is called just before
a grain is added into the output buffer. The current grain is accessible via mus-data.
The edit function, if any, should return the length in samples of the grain, or 0.
In the following example, we use the edit function to reverse every other grain:
</p>
<pre class="indented">
(let ((forward #t))
(let ((grn (<em class=red>make-granulate</em> :expansion 2.0
:edit (lambda (g)
(let ((grain (<a class=quiet href="#mus-data">mus-data</a> g)) ; current grain
(len (<a class=quiet href="#mus-length">mus-length</a> g))) ; current grain length
(if forward ; no change to data
(set! forward #f)
(begin
(set! forward #t)
(reverse! grain)))
len))))
(rd (<a class=quiet href="extsnd.html#makesampler">make-sampler</a> 0)))
(<a class=quiet href="extsnd.html#mapchannel">map-channel</a> (lambda (y) (<em class=red>granulate</em> grn (lambda (dir) (rd)))))))
</pre>
<!-- PHASE-VOCODER -->
<div class="innerheader" id="phase-vocoderdoc">phase-vocoder</div>
<pre class="indented">
<em class=def id="make-phase-vocoder">make-phase-vocoder</em> input (fft-size 512) (overlap 4) (interp 128) (pitch 1.0) analyze edit synthesize
<em class=def id="phase-vocoder">phase-vocoder</em> pv
<em class=def id="phase-vocoder?">phase-vocoder?</em> pv
</pre>
<table class="method">
<tr><td colspan=2 class="methodtitle">phase-vocoder methods</td></tr>
<tr><td class="inner"><em class=gen>mus-frequency</em></td><td class="inner">pitch shift</td></tr>
<tr><td class="inner"><em class=gen>mus-length</em></td><td class="inner">fft-size</td></tr>
<tr><td class="inner"><em class=gen>mus-increment</em></td><td class="inner">interp</td></tr>
<tr><td class="inner"><em class=gen>mus-hop</em></td><td class="inner">fft-size / overlap</td></tr>
<tr><td class="inner"><em class=gen>mus-location</em></td><td class="inner">outctr (counter to next fft)</td></tr>
</table>
<p>The phase-vocoder generator performs phase-vocoder analysis and resynthesis. The process is
split into three pieces, the analysis stage, editing of the amplitudes and phases, then the resynthesis.
Each stage has a default that is invoked if the "analyze", "edit", or "synthesize"
arguments are omitted from make-phase-vocoder or the phase-vocoder generator. The edit and synthesize arguments are functions of one argument, the
phase-vocoder generator. The analyze argument is a function of two arguments, the generator and
the input function. The default is to read the current input,
take an fft, get the new amplitudes and phases (as the edit
function default), then resynthesize using sines; so, the
default case returns a resynthesis of the original input. The "interp" argument sets the time between
ffts (for time stretching, etc).
</p>
<table>
<tr>
<td>
<div class="scheme">
<pre class="indented">
(with-sound (:play #t) ; new pitch = 2 * old
(let ((pv (make-phase-vocoder
(make-readin "oboe.snd") :pitch 2.0)))
(do ((i 0 (+ i 1)))
((= i 44100))
(outa i (phase-vocoder pv)))))
</pre>
</div>
</td>
</tr>
<tr>
<td>
<div class="ruby">
<pre class="indented">
with_sound(:play, true) do
rd = make_readin("oboe.snd");
pv = make_phase_vocoder(
lambda do |dir|
readin(rd) end, :pitch, 2.0);
88200.times do |i|
outa(i, phase_vocoder(pv), $output);
end
end.output
</pre>
</div>
</td>
</tr>
<tr>
<td>
<div class="forth">
<pre class="indented">
lambda: ( -- )
"oboe.snd" make-readin :pitch 2.0 make-phase-vocoder { pv }
44100 0 do
i pv #f #f #f #f phase-vocoder *output* outa drop
loop
; :play #t with-sound drop
</pre>
</div>
</td>
</tr>
</table>
<table>
<tr>
<td>
<div class="scheme">
<pre class="indented">
(with-sound (:play #t :srate 22050) ; new dur = 2 * old
(let ((pv (make-phase-vocoder
(make-readin "oboe.snd")
:interp 256)) ; 2 * 512 / 4
;; 512: fft size, 4: overlap, new dur: 2 * old dur
(samps (* 2 (mus-sound-framples "oboe.snd"))))
(do ((i 0 (+ i 1)))
((= i samps))
(outa i (phase-vocoder pv)))))
</pre>
</div>
</td>
</tr>
<tr>
<td>
<div class="ruby">
<pre class="indented">
with_sound(:play, true, :srate, 22050) do
rd = make_readin("oboe.snd");
pv = make_phase_vocoder(
lambda do |dir| readin(rd) end,
:interp, 2 * 512 / 4);
samps = 2 * mus_sound_framples("oboe.snd");
samps.times do |i|
outa(i, phase_vocoder(pv), $output);
end
end.output
</pre>
</div>
</td>
</tr>
<tr>
<td>
<div class="forth">
<pre class="indented">
lambda: ( -- )
"oboe.snd" make-readin :interp 256 make-phase-vocoder { pv }
"oboe.snd" mus-sound-framples 2* ( samps ) 0 do
i pv #f #f #f #f phase-vocoder *output* outa drop
loop
; :play #t :srate 22050 with-sound drop
</pre>
</div>
</td>
</tr>
</table>
<p>There are several functions giving access to the phase-vocoder data:
</p>
<pre class="indented">
<em class=emdef>phase-vocoder-amps</em> gen
<em class=emdef>phase-vocoder-freqs</em> gen
<em class=emdef>phase-vocoder-phases</em> gen
<em class=emdef>phase-vocoder-amp-increments</em> gen
<em class=emdef>phase-vocoder-phase-increments</em> gen
</pre>
<p>These are arrays (float-vectors) containing the spectral data the phase-vocoder uses to
reconstruct the sound.
In the next example we use all these special functions to resynthesize down an octave:
</p>
<pre class="indented">
(with-sound (:srate 22050 :statistics #t)
(let ((pv (<em class=red>make-phase-vocoder</em>
(make-readin "oboe.snd")
512 4 128 1.0
#f ; no change to analysis method
#f ; no change to spectrum
(lambda (gen) ; resynthesis function
(float-vector-add! (phase-vocoder-amps gen) (phase-vocoder-amp-increments gen))
(float-vector-add! (phase-vocoder-phase-increments gen) (phase-vocoder-freqs gen))
(float-vector-add! (phase-vocoder-phases gen) (phase-vocoder-phase-increments gen))
(let ((sum 0.0)
(n (length (phase-vocoder-amps gen))))
(do ((k 0 (+ k 1)))
((= k n))
(set! sum (+ sum (* (float-vector-ref (phase-vocoder-amps gen) k)
(sin (* 0.5 (float-vector-ref (phase-vocoder-phases gen) k)))))))
sum)))))
(do ((i 0 (+ i 1)))
((= i 44100))
(outa i (<em class=red>phase-vocoder</em> pv)))))
</pre>
<p>but, sadly, this code crawls. It won't actually be useful until I optimize
handling of the caller's resynthesis function, but I am dragging my feet because I've never felt
that this phase-vocoder (as a generator) was the "right thing". The first step toward something less stupid is
moving-spectrum in generators.scm.
</p>
<!-- ASYMMETRIC-FM -->
<div class="innerheader" id="asymmetric-fmdoc">asymmetric-fm</div>
<pre class="indented">
<em class=def id="make-asymmetric-fm">make-asymmetric-fm</em>
(frequency 0.0)
(initial-phase 0.0)
(r 1.0) ; amplitude ratio between successive sidebands
(ratio 1.0) ; ratio between carrier and sideband spacing
<em class=def id="asymmetric-fm">asymmetric-fm</em> af index (fm 0.0)
<em class=def id="asymmetric-fm?">asymmetric-fm?</em> af
</pre>
<table class="method">
<tr><td colspan=2 class="methodtitle">asymmetric-fm methods</td></tr>
<tr><td class="inner"><em class=gen>mus-frequency</em></td><td class="inner">frequency in Hz</td></tr>
<tr><td class="inner"><em class=gen>mus-phase</em></td><td class="inner">phase in radians</td></tr>
<tr><td class="inner"><em class=gen>mus-scaler</em></td><td class="inner">"r" parameter; sideband scaler</td></tr>
<tr><td class="inner"><em class=gen>mus-offset</em></td><td class="inner">"ratio" parameter</td></tr>
<tr><td class="inner"><em class=gen>mus-increment</em></td><td class="inner">frequency in radians per sample</td></tr>
</table>
<p>The asymmetric-fm generator provides a way around the symmetric spectra normally produced by FM.
See Palamin and Palamin, "A Method of Generating and Controlling Asymmetrical
Spectra" JAES vol 36, no 9, Sept 88, p671-685. P&P use sin(sin), but I'm using cos(sin) so
that we get a sum of cosines, and can therefore easily normalize the peak amplitude to -1.0..1.0.
asymmetric-fm is based on:
</p>
<!-- LATEX:
original:
e^{(\frac{B}{2}(r-\frac{1}{r})\cos \omega_{m}t)}\sin(\omega_{c}t+\frac{B}{2}(r+\frac{1}{r})\sin \omega_{m}t)=\sum_{n=-\infty}^{\infty} r^{n}J_{n}(B)\sin(\omega_{c}t+n\omega_{m}t)
my version (for predicatable peak amp):
e^{(\frac{B}{2}(r-\frac{1}{r})\cos \omega_{m}t)}\cos(\omega_{c}t+\frac{B}{2}(r+\frac{1}{r})\sin \omega_{m}t)=\sum_{n=-\infty}^{\infty} r^{n}J_{n}(B)\cos(\omega_{c}t+n\omega_{m}t)
original:
e^{(\frac{B}{2}(r+\frac{1}{r})\cos \omega_{m}t)}\sin(\omega_{c}t+\frac{B}{2}(r-\frac{1}{r})\sin \omega_{m}t)=\sum_{n=-\infty}^{\infty} r^{n}I_{n}(B)\sin(\omega_{c}t+n\omega_{m}t)
my version:
e^{(\frac{B}{2}(r+\frac{1}{r})\cos \omega_{m}t)}\cos(\omega_{c}t+\frac{B}{2}(r-\frac{1}{r})\sin \omega_{m}t)=\sum_{n=-\infty}^{\infty} r^{n}I_{n}(B)\cos(\omega_{c}t+n\omega_{m}t)
here's the complicated case:
e^{\big( \big(\frac{B}{2}\big(r+\frac{1}{r}\big)\cos \omega_{m}t\big) - \frac{1}{2} \ln \big(I_{0}\big(B\big(r+\frac{1}{r}\big)\big)\big) \big)} \sin\big(\omega_{c}t+\frac{B}{2}\big(r-\frac{1}{r}\big)\sin \omega_{m}t\big)=\frac{1}{\sqrt{I_{0}(B(r+\frac{1}{r}))}} \sum r^{n}I_{n}(B)\sin(\omega_{c}t+n\omega_{m}t)
-->
<img class="indented" src="pix/sceq10.png" alt="e sin"><br>
<img class="indented" src="pix/sceq22.png" alt="I form">
<table>
<tr>
<td>
<div class="scheme">
<pre class="indented">
(with-sound (:play #t)
(let ((fm (make-asymmetric-fm 440.0 0.0 0.9 0.5)))
(do ((i 0 (+ i 1)))
((= i 44100))
(outa i (* 0.5 (asymmetric-fm fm 1.0))))))
</pre>
</div>
</td>
</tr>
<tr>
<td>
<div class="ruby">
<pre class="indented">
with_sound(:play, true) do
fm = make_asymmetric_fm(440.0, 0.0, 0.9, 0.5);
44100.times do |i|
outa(i, 0.5 * asymmetric_fm(fm, 1.0), $output);
end
end.output
</pre>
</div>
</td>
</tr>
<tr>
<td>
<div class="forth">
<pre class="indented">
lambda: ( -- )
440.0 0.0 0.9 0.5 make-asymmetric-fm { fm }
44100 0 do
i fm 1.0 0 asymmetric-fm f2/ *output* outa drop
loop
; :play #t with-sound drop
</pre>
</div>
</td>
</tr>
</table>
<p>"r" is the ratio between successive
sideband amplitudes, r < 0.0 or r > 1.0 pushes energy above the carrier, whereas r between 0.0 and 1.0 pushes it below. (r = 1.0
gives normal FM). The mirror image of r (around a given
carrier) is produced by -1/r.
"ratio" is the ratio between the carrier and modulator (i.e. sideband spacing). It's somewhat inconsistent
that asymmetric-fm takes "index" (the fm-index) as its second argument, but otherwise it
would be tricky to get time-varying indices. In this instrument we sweep "r" with an envelope:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#definstrument">definstrument</a> (asy beg dur freq amp index (ratio 1.0))
(let* ((st (<a class=quiet href="#secondstosamples">seconds->samples</a> beg))
(nd (+ st (<a class=quiet href="#secondstosamples">seconds->samples</a> dur)))
(r-env (<a class=quiet href="#make-env">make-env</a> '(0 -1 1 -20) :duration dur))
(asyf (<em class=red>make-asymmetric-fm</em> :ratio ratio :frequency freq)))
(do ((i st (+ i 1)))
((= i nd))
(set! (<a class=quiet href="#mus-scaler">mus-scaler</a> asyf) (<a class=quiet href="#env">env</a> r-env)) ; this sets "r"
(<a class=quiet href="#outa">outa</a> i (* amp (<em class=red>asymmetric-fm</em> asyf index))))))
</pre>
<p>For the other kind of asymmetric-fm see generators.scm, and for asymmetric spectra via "single sideband FM" see generators.scm.
</p>
<table class="method">
<tr><td class="inner"><em class=def id="frampletoframple">frample->frample</em><code> mf inf outf</code></td><td>pass frample through a matrix multiply, return outf</td></tr>
</table>
<!-- FILE->SAMPLE -->
<div class="innerheader" id="filetosampledoc">sound IO</div>
<p>Sound file IO is based on a set of file readers and writers that deal either in samples or float-vectors.
The six functions are file->sample, sample->file, file->frample, frample->file, array->file, and file->array.
The name "array" is used here, rather than "float-vector" for historical reasons (the CL version of CLM predates
Snd by many years).
These functions are then packaged up in more convenient forms as in-any, out-any, locsig, readin, etc.
Within with-sound, the variable *output* is bound to the with-sound output file via a sample->file
object.
</p>
<pre class="indented">
<em class=def id="make-filetosample">make-file->sample</em> name (buffer-size 8192)
<em class=def id="make-sampletofile">make-sample->file</em> name (chans 1) (format mus-lfloat) (type mus-next) comment
<em class=def id="filetosample?">file->sample?</em> obj
<em class=def id="sampletofile?">sample->file?</em> obj
<em class=def id="filetosample">file->sample</em> obj samp chan
<em class=def id="sampletofile">sample->file</em> obj samp chan val
<em class=def id="continue-sampletofile">continue-sample->file</em> file
<em class=def id="make-filetoframple">make-file->frample</em> name (buffer-size 8192)
<em class=def id="make-frampletofile">make-frample->file</em> name (chans 1) (format mus-lfloat) (type mus-next) comment
<em class=def id="frampletofile?">frample->file?</em> obj
<em class=def id="filetoframple?">file->frample?</em> obj
<em class=def id="filetoframple">file->frample</em> obj samp outf
<em class=def id="frampletofile">frample->file</em> obj samp val
<em class=def id="continue-frampletofile">continue-frample->file</em> file
<em class=def id="filetoarray">file->array</em> file channel beg dur array
<em class=def id="arraytofile">array->file</em> file data len srate channels
<em class=def id="mus-input?">mus-input?</em> obj
<em class=def id="mus-output?">mus-output?</em> obj
<em class=def id="mus-close">mus-close</em> obj
<em class=def id="*output*">*output*</em>
<em class=def id="*reverb*">*reverb*</em>
<em class=def id="musfilebuffersize">mus-file-buffer-size</em> (also known as *clm-file-buffer-size*)
</pre>
<table>
<tr>
<td>
<div class="scheme">
<pre class="indented">
(with-sound (:channels 2)
;; swap channels of stereo file
(let ((input (make-file->frample "stereo.snd"))
(len (mus-sound-framples "stereo.snd"))
(frample (make-float-vector 2)))
(do ((i 0 (+ i 1)))
((= i len))
(file->frample input i frample)
(let ((val (frample 0)))
(set! (frample 0) (frample 1))
(set! (frample 1) val))
(frample->file *output* i frample))))
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="ruby">
<pre class="indented">
with_sound(:channels, 2) do
input = make_file2frample("stereo.snd");
len = mus_sound_framples("stereo.snd");
frample = make_frample(2);
len.times do |i|
file2frample(input, i, frample);
val = frample_ref(frample, 0);
frample_set!(frample, 0, frample_ref(frample, 1));
frample_set!(frample, 1, val);
frample2file($output, i, frample);
end
end.output
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="forth">
<pre class="indented">
lambda: ( -- )
"stereo.snd" make-file->frample { input }
2 make-frample { frm }
"stereo.snd" mus-sound-framples ( len ) 0 do
input i frm file->frample ( frm ) 1 frample-ref ( val1 )
frm 0 frample-ref ( val0 ) frm 1 rot frample-set! drop
( val1 ) frm 0 rot frample-set! drop
*output* i frm frample->file drop
loop
; :channels 2 :play #t with-sound drop
</pre>
</div>
</td>
</tr>
</table>
<p>file->sample writes a sample to a file, frample->file writes a frample, file->sample reads a sample
from a file, and file->frample reads a frample.
continue-frample->file and continue-sample->file reopen an existing file to continue adding sound data to it.
mus-output? returns #t is its argument is some sort of file writing generator, and mus-input? returns #t if its
argument is a file reader.
In make-file->sample and make-file->frample, the buffer-size defaults to *clm-file-buffer-size*.
There are many examples of these functions in snd-test.scm, and clm-ins.scm.
Here is one that uses file->sample to mix in a sound file (there are a zillion other ways to do this):
</p>
<pre class="indented">
(define (simple-f2s beg dur amp file)
(let* ((start (<a class=quiet href="#secondstosamples">seconds->samples</a> beg))
(end (+ start (<a class=quiet href="#secondstosamples">seconds->samples</a> dur)))
(fil (<em class=red>make-file->sample</em> file)))
(do ((ctr 0)
(i start (+ i 1))) ((= i end))
(<a class=quiet href="#out-any">out-any</a> i (* amp (<em class=red>file->sample</em> fil ctr 0)) 0)
(set! ctr (+ 1 ctr)))))
</pre>
<p>mus-close flushes any pending output and closes the output stream 'obj'.
This is normally done for you by with-sound, but if you have your own
output streams,
and you forget to call mus-close, the GC will eventually do it for you.
</p>
<!-- READIN -->
<div class="innerheader" id="readindoc">readin</div>
<pre class="indented">
<em class=def id="make-readin">make-readin</em> file (channel 0) (start 0) (direction 1) size
<em class=def id="readin">readin</em> rd
<em class=def id="readin?">readin?</em> rd
</pre>
<table class="method">
<tr><td colspan=2 class="methodtitle">readin methods</td></tr>
<tr><td class="inner"><em class=gen>mus-channel</em></td><td class="inner">channel arg to make-readin (no set!)</td></tr>
<tr><td class="inner"><em class=gen>mus-location</em></td><td class="inner">current location in file</td></tr>
<tr><td class="inner"><em class=gen>mus-increment</em></td><td class="inner">sample increment (direction arg to make-readin)</td></tr>
<tr><td class="inner"><em class=gen>mus-file-name</em></td><td class="inner">name of file associated with gen</td></tr>
<tr><td class="inner"><em class=gen>mus-length</em></td><td class="inner">number of framples in file associated with gen</td></tr>
</table>
<p>readin returns successive samples from a file; it is an elaboration of file->sample that keeps track of the
current read location and channel number for you.
Its "file" argument is the input file's name.
"start" is the frample at which to start reading the input file.
"channel" is which channel to read (0-based).
"size" is the read buffer size in samples. It defaults to *clm-file-buffer-size*.
</p>
<table>
<tr>
<td>
<div class="scheme">
<pre class="indented">
(with-sound (:play #t)
(let ((reader (make-readin "oboe.snd")))
(do ((i 0 (+ i 1)))
((= i 44100))
(outa i (* 2.0 (readin reader))))))
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="ruby">
<pre class="indented">
with_sound(:play, true) do
reader = make_readin("oboe.snd");
44100.times do |i|
outa(i, 2.0 * readin(reader),
$output);
end
end.output
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="forth">
<pre class="indented">
lambda: ( -- )
"oboe.snd" make-readin { reader }
44100 0 do
i reader readin f2/ *output* outa drop
loop
; :play #t with-sound drop
</pre>
</div>
</td>
</tr>
</table>
<p>Here is an instrument that applies an envelope to a sound file using
readin and <a href="#env">env</a>:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#definstrument">definstrument</a> (env-sound file beg (amp 1.0) (amp-env '(0 1 100 1)))
(let* ((st (<a class=quiet href="#secondstosamples">seconds->samples</a> beg))
(dur (<a class=quiet href="extsnd.html#mussoundduration">mus-sound-duration</a> file))
(rev-amount .01)
(rdA (<em class=red>make-readin</em> file))
(ampf (<a class=quiet href="#make-env">make-env</a> amp-env amp dur))
(nd (+ st (<a class=quiet href="#secondstosamples">seconds->samples</a> dur))))
(do ((i st (+ i 1)))
((= i nd))
(let ((outval (* (<a class=quiet href="#env">env</a> ampf) (<em class=red>readin</em> rdA))))
(<a class=quiet href="#outa">outa</a> i outval)
(if <a class=quiet>*reverb*</a>
(<a class=quiet href="#outa">outa</a> i (* outval rev-amount) <a class=quiet>*reverb*</a>))))))
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> () (env-sound "oboe.snd" 0 1.0 '(0 0 1 1 2 1 3 0)))
</pre>
<!-- IN-ANY, OUT-ANY -->
<div class="innerheader" id="in-anydoc">in-any, out-any</div>
<pre class="indented">
<em class=def id="out-any">out-any</em> loc data channel (output *output*)
<em class=def id="outa">outa</em> loc data (output *output*)
<em class=emdef>outb</em> loc data (output *output*)
<em class=emdef>outc</em> loc data (output *output*)
<em class=emdef>outd</em> loc data (output *output*)
<em class=def id="outbank">out-bank</em> gens loc input
<em class=def id="in-any">in-any</em> loc channel input
<em class=def id="ina">ina</em> loc input
<em class=def id="inb">inb</em> loc input
</pre>
<p>These are the "generic" input and output functions.
out-any adds its "data" argument (a sound sample) into the "output" object at sample
position "loc".
The "output" argument can be a vector as well as the more usual frample->file object.
or any output-capable CLM generator.
In with-sound, the current output is *output* and the reverb output is *reverb*.
outa is the same as out-any with a channel of 0. It is not an error to try to write to a channel that doesn't exist;
the function just returns.
</p>
<p>in-any returns the sample at position "loc" in
"input". ina is the same as in-any with a channel of 0.
As in out-any and friends, the "input" argument can be a file->frample object, or a vector.
</p>
<table>
<tr>
<td>
<div class="scheme">
<pre class="indented">
(with-sound (:play #t)
(let ((infile (make-file->sample "oboe.snd")))
(do ((i 0 (+ i 1)))
((= i 44100))
(out-any i (in-any i 0 infile) 0))))
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="ruby">
<pre class="indented">
with_sound(:play, true) do
infile = make_file2sample("oboe.snd");
44100.times do |i|
out_any(i, in_any(i, 0, infile), 0, $output);
end
end.output
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="forth">
<pre class="indented">
lambda: ( -- )
"oboe.snd" make-file->sample { infile }
44100 0 do
i i 0 infile in-any 0 *output* out-any drop
loop
; :play #t with-sound drop
</pre>
</div>
</td>
</tr>
</table>
<pre class="indented">
(<a class=quiet href="sndscm.html#definstrument">definstrument</a> (simple-ina beg dur amp file)
(let* ((start (<a class=quiet href="#secondstosamples">seconds->samples</a> beg))
(end (+ start (<a class=quiet href="#secondstosamples">seconds->samples</a> dur)))
(fil (<em class=red>make-file->sample</em> file)))
(do ((i start (+ i 1)))
((= i end))
(<a class=quiet href="#outa">outa</a> i
(* amp (<em class=red>in-any</em> i 0 fil)))))) ; same as (<em class=red>ina</em> i fil)
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> () (simple-ina 0 1 .5 "oboe.snd"))
</pre>
<p>To write from <a href="sndscm.html#wsdoc">with-sound</a> to a vector, rather than a file,
use its :output argument:
</p>
<pre class="indented">
(with-sound (<em class=red>:output</em> (make-float-vector 44100)) ; this sets *output*, the default output location
(<a class=quiet href="sndscm.html#vdoc">fm-violin</a> 0 1 440 .1))
</pre>
<p>If *output* is a function, it should take 3 arguments, the sample number, current output value, and channel.
</p>
<pre class="indented">
(let ((avg 0.0)
(samps 0))
(with-sound (<em class=red>:output</em> (lambda (frample val chan) ; get the average of all the samples
(set! avg (+ avg val))
(set! samps (+ 1 samps))
val))
(do ((i 0 (+ i 1)))
((> i 10))
(<em class=red>outa</em> i (* i .1))))
(/ avg samps))
;; returns 0.5
</pre>
<p>Similarly, if in-any's "input" argument is a function, it takes the input location (sample number), and channel (0-based).
</p>
<pre class="indented">
(let ((input (<a class=quiet href="#make-readin">make-readin</a> "oboe.snd" :start 1000)))
(with-sound ((make-float-vector 10))
(do ((i 0 (+ i 1)))
((= i 10))
(<em class=red>outa</em> i (<em class=red>ina</em> i (lambda (loc chn)
(<a class=quiet href="#readin">readin</a> input)))))))
</pre>
<pre class="indented">
(let ((outv (make-float-vector 10)))
(with-sound ()
(do ((i 0 (+ i 1)))
((= i 10))
(outa i (* i .1) (lambda (loc val chan)
(set! (outv loc) val)))))
outv) ; this is equivalent to using :output (make-float-vector 10) as a with-sound argument
</pre>
<!-- LOCSIG -->
<!-- INDEX make-locsig:Sound placement -->
<div class="innerheader" id="locsigdoc">locsig</div>
<pre class="indented">
<em class=def id="make-locsig">make-locsig</em>
(degree 0.0)
(distance 1.0)
(reverb 0.0) ; reverb amount
(output *output*) ; output generator or location
(revout *reverb*) ; reverb output generator or location
(channels (channels output))
(type mus-interp-linear)
<em class=def id="locsig">locsig</em> loc i in-sig
<em class=def id="locsig?">locsig?</em> loc
<em class=def id="locsig-ref">locsig-ref</em> loc chan
<em class=def id="locsig-set!">locsig-set!</em> loc chan val
<em class=def id="locsig-reverb-ref">locsig-reverb-ref</em> loc chan
<em class=def id="locsig-reverb-set!">locsig-reverb-set!</em> loc chan val
<em class=def id="move-locsig">move-locsig</em> loc degree distance
<em class=def id="locsig-type">locsig-type</em> ()
</pre>
<table class="method">
<tr><td colspan=2 class="methodtitle">locsig methods</td></tr>
<tr><td class="inner"><em class=gen>mus-data</em></td><td class="inner">output scalers (a float-vector)</td></tr>
<tr><td class="inner"><em class=gen>mus-xcoeff</em></td><td class="inner">reverb scaler</td></tr>
<tr><td class="inner"><em class=gen>mus-xcoeffs</em></td><td class="inner">reverb scalers (a float-vector)</td></tr>
<tr><td class="inner"><em class=gen>mus-channels</em></td><td class="inner">output channels</td></tr>
<tr><td class="inner"><em class=gen>mus-length</em></td><td class="inner">output channels</td></tr>
</table>
<p>locsig places a sound in
an N-channel circle of speakers
by scaling the respective channel amplitudes
("that old trick <em>never</em> works"). It normally replaces <a href="#outa">out-any</a>.
"reverb" determines how much of
the direct signal gets sent to the reverberator. "distance" tries to
imitate a distance cue by fooling with the relative amounts of direct and
reverberated signal (independent of the "reverb" argument). The distance should
be greater than or equal to 1.0.
"type" (returned by the function locsig-type) can be <code>mus-interp-linear</code> (the default) or <code>mus-interp-sinusoidal</code>.
The mus-interp-sinusoidal
case uses sin and cos to set the respective channel amplitudes (this is reported to
help with the "hole-in-the-middle" problem).
The "output" argument can be a vector as well as a frample->file generator.
</p>
<table>
<tr>
<td>
<div class="scheme">
<pre class="indented">
(with-sound (:play #t :channels 2)
(let ((loc (make-locsig 60.0))
(osc (make-oscil 440.0)))
(do ((i 0 (+ i 1)))
((= i 44100))
(locsig loc i
(* 0.5 (oscil osc))))))
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="ruby">
<pre class="indented">
with_sound(:play, true, :channels, 2) do
loc = make_locsig(60.0, :output, $output);
osc = make_oscil(440.0);
44100.times do |i|
locsig(loc, i, 0.5 * oscil(osc));
end
end.output
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="forth">
<pre class="indented">
lambda: ( -- )
60.0 make-locsig { loc }
440.0 make-oscil { osc }
44100 0 do
loc i osc 0 0 oscil f2/ locsig drop
loop
; :play #t :channels 2 with-sound drop
</pre>
</div>
</td>
</tr>
</table>
<p>Locsig can send output to any number of channels.
If channels > 2, the speakers are assumed to be evenly spaced in
a circle.
You can use locsig-set! to override the placement decisions.
To have full output to both channels,</p>
<pre class="indented">
(locsig-set! loc 0 1.0)
(locsig-set! loc 1 1.0)
</pre>
<p>Here is an instrument that has envelopes on the distance and degrees, and optionally reverberates a file:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#definstrument">definstrument</a> (space file onset duration (distance-env '(0 1 100 10)) (amplitude-env '(0 1 100 1))
(degree-env '(0 45 50 0 100 90)) (reverb-amount .05))
(let* ((beg (<a class=quiet href="#secondstosamples">seconds->samples</a> onset))
(end (+ beg (<a class=quiet href="#secondstosamples">seconds->samples</a> duration)))
(loc (<em class=red>make-locsig</em> :degree 0 :distance 1 :reverb reverb-amount))
(rdA (<a class=quiet href="#make-readin">make-readin</a> :file file))
(dist-env (<a class=quiet href="#make-env">make-env</a> distance-env :duration duration))
(amp-env (<a class=quiet href="#make-env">make-env</a> amplitude-env :duration duration))
(deg-env (<a class=quiet href="#make-env">make-env</a> degree-env :scaler (/ 1.0 90.0) :duration duration))
(dist-scaler 0.0))
(do ((i beg (+ i 1)))
((= i end))
(let ((rdval (* (<a class=quiet href="#readin">readin</a> rdA) (<a class=quiet href="#env">env</a> amp-env)))
(degval (<a class=quiet href="#env">env</a> deg-env)))
(set! dist-scaler (/ (<a class=quiet href="#env">env</a> dist-env)))
(locsig-set! loc 0 (* (- 1.0 degval) dist-scaler))
(if (> (channels <a class=quiet>*output*</a>) 1)
(locsig-set! loc 1 (* degval dist-scaler)))
(if <a class=quiet>*reverb*</a>
(locsig-reverb-set! loc 0 (* reverb-amount (sqrt dist-scaler))))
(<em class=red>locsig</em> loc i rdval)))))
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:reverb jc-reverb :channels 2)
(space "pistol.snd" 0 3 :distance-env '(0 1 1 2) :degree-env '(0 0 1 90)))
</pre>
<p>For a moving sound
source, see either move-locsig, Fernando Lopez Lezcano's <a class=def href="http://ccrma.stanford.edu/~nando/clm/dlocsig/index.html">dlocsig</a>,
or <a href="#flocsig">flocsig</a> (flanged locsig) in generators.scm.
Here is an example of move-locsig:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:channels 4)
(let ((loc (<em class=red>make-locsig</em>))
(osc (<a class=quiet href="#make-oscil">make-oscil</a> 440.0))
(j 0))
(do ((i 0 (+ i 1)))
((= i 360))
(do ((k 0 (+ k 1)))
((= k 1000))
(let ((sig (* .5 (<a class=quiet href="#oscil">oscil</a> osc))))
(<em class=red>locsig</em> loc j sig)
(set! j (+ j 1))))
(<em class=red>move-locsig</em> loc (* 1.0 i) 1.0))))
</pre>
<table>
<tr>
<td><img class="indented" src="pix/circle.png" alt="move-locsig example"></td>
<td><img class="indented" src="pix/locsine.png" alt="move-locsig example"></td>
</tr><tr>
<td class="center">linear interp</td>
<td class="center">sinusoidal interp</td>
</tr></table>
<p>The interaction of outa, locsig, and *reverb* seems to be causing confusion, so here are some
simple examples:
</p>
<pre class="indented">
(load "nrev.scm")
(define (simp start end freq amp)
(let ((os (make-oscil freq)))
(do ((i start (+ i 1)))
((= i end))
(let ((output (* amp (oscil os))))
(outa i output)
(if *reverb* (outa i (* output .1) *reverb*))))))
; (with-sound () (simp 0 44100 440 .1)) ; no reverb
; (with-sound (:reverb nrev) (simp 0 44100 440 .1)); reverb
(define (locsimp start end freq amp)
(let ((os (make-oscil freq))
(loc (make-locsig :reverb .1)))
(do ((i start (+ i 1)))
((= i end))
(locsig loc i (* amp (oscil os))))))
; (with-sound () (locsimp 0 44100 440 .1)) ; no reverb
; (with-sound (:reverb nrev) (locsimp 0 44100 440 .1)); reverb
</pre>
<!-- MOVE-SOUND -->
<div class="innerheader" id="move-sounddoc">move-sound</div>
<pre class="indented">
<em class=def id="make-move-sound">make-move-sound</em> dlocs-list (output *output*) (revout *reverb*)
<em class=def id="move-sound">move-sound</em> dloc i in-sig
<em class=def id="move-sound?">move-sound?</em> dloc
</pre>
<p>move-sound is intended as the run-time portion of <a href="sndscm.html#dlocsigdoc">dlocsig</a>. make-dlocsig
creates a move-sound structure, passing it to the move-sound generator inside the
dlocsig macro. All the necessary data is packaged up in a list:
</p>
<pre class="indented">
(list
(start 0) ; absolute sample number at which samples first reach the listener
(end 0) ; absolute sample number of end of input samples
(out-channels 0) ; number of output channels in soundfile
(rev-channels 0) ; number of reverb channels in soundfile
path ; interpolated delay line for doppler
delay ; tap doppler env
rev ; reverberation amount
out-delays ; delay lines for output channels that have additional delays
gains ; gain envelopes, one for each output channel
rev-gains ; reverb gain envelopes, one for each reverb channel
out-map) ; mapping of speakers to output channels
</pre>
<p>Here's an instrument that uses this generator to pan a sound through four channels:
</p>
<pre class="indented">
(define (simple-dloc beg dur freq amp)
(let* ((os (<a class=quiet href="#make-oscil">make-oscil</a> freq))
(start (<a class=quiet href="#secondstosamples">seconds->samples</a> beg))
(end (+ start (<a class=quiet href="#secondstosamples">seconds->samples</a> dur)))
(loc (<em class=red>make-move-sound</em> (list start end 4 0
(<a class=quiet href="#make-delay">make-delay</a> 12)
(<a class=quiet href="#make-env">make-env</a> '(0 0 10 1) :length dur)
#f
(make-vector 4 #f)
(vector (<a class=quiet href="#make-env">make-env</a> '(0 0 1 1 2 0 3 0 4 0) :duration dur)
(<a class=quiet href="#make-env">make-env</a> '(0 0 1 0 2 1 3 0 4 0) :duration dur)
(<a class=quiet href="#make-env">make-env</a> '(0 0 1 0 2 0 3 1 4 0) :duration dur)
(<a class=quiet href="#make-env">make-env</a> '(0 0 1 0 2 0 3 0 4 1) :duration dur))
#f
(vector 0 1 2 3)))))
(do ((i start (+ i 1)))
((= i end))
(<em class=red>move-sound</em> loc i (* amp (<a class=quiet href="#oscil">oscil</a> os))))))
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:channels 4) (simple-dloc 0 2 440 .5))
</pre>
<div class="header" id="genericfunctions">Generic functions</div>
<!-- GENERIC FUNCTIONS -->
<p>Besides the 30 or so built-in generators, there are around 100 others defined in generators.scm. If we
required separate functions for each generator for access to the generator internal state (current phase, for example),
we'd end up with hundreds, or even thousands of accessors. Instead,
all the generators respond to a set of "generic" functions. mus-frequency, for example, tries to return (or set) a generator's
frequency, for any generator that has some sort of frequency field.
The generic functions are:
</p>
<table class="method">
<tr><td><em class=def id="mus-channel">mus-channel</em></td><td>channel being read/written</td></tr>
<tr><td><em class=def id="mus-channels">mus-channels</em></td><td>channels open</td></tr>
<tr><td><em class=def id="mus-copy">mus-copy</em></td><td>copy a generator</td></tr>
<tr><td><em class=def id="mus-data">mus-data</em></td><td>float-vector of data</td></tr>
<tr><td><em class=def id="mus-describe">mus-describe</em></td><td>description of current state</td></tr>
<tr><td><em class=def id="mus-feedback">mus-feedback</em></td><td>feedback coefficient</td></tr>
<tr><td><em class=def id="mus-feedforward">mus-feedforward</em></td><td>feedforward coefficient</td></tr>
<tr><td><em class=def id="mus-file-name">mus-file-name</em></td><td>file being read/written</td></tr>
<tr><td><em class=def id="mus-frequency">mus-frequency</em></td><td>frequency (Hz)</td></tr>
<tr><td><em class=def id="mus-hop">mus-hop</em></td><td>hop size for block processing</td></tr>
<tr><td><em class=def id="mus-increment">mus-increment</em></td><td>various increments</td></tr>
<tr><td><em class=def id="mus-interp-type">mus-interp-type</em></td><td>interpolation type (mus-interp-linear, etc)</td></tr>
<tr><td><em class=def id="mus-length">mus-length</em></td><td>data length</td></tr>
<tr><td><em class=def id="mus-location">mus-location</em></td><td>sample location for reads/writes</td></tr>
<tr><td><em class=def id="mus-name">mus-name</em></td><td>generator name ("oscil")</td></tr>
<tr><td><em class=def id="mus-offset">mus-offset</em></td><td>envelope offset</td></tr>
<tr><td><em class=def id="mus-order">mus-order</em></td><td>filter order</td></tr>
<tr><td><em class=def id="mus-phase">mus-phase</em></td><td>phase (radians)</td></tr>
<tr><td><em class=def id="mus-ramp">mus-ramp</em></td><td>granulate grain envelope ramp setting</td></tr>
<tr><td><em class=def id="mus-reset">mus-reset</em></td><td>set gen to default starting state</td></tr>
<tr><td><em class=def id="mus-run">mus-run</em></td><td>run any generator</td></tr>
<tr><td><em class=def id="mus-scaler">mus-scaler</em></td><td>scaler, normally on an amplitude</td></tr>
<tr><td><em class=def id="mus-width">mus-width</em></td><td>width of interpolation tables, etc</td></tr>
<tr><td><em class=def id="mus-xcoeff">mus-xcoeff</em></td><td>x (input) coefficient</td></tr>
<tr><td><em class=def id="mus-xcoeffs">mus-xcoeffs</em></td><td>float-vector of x (input) coefficients</td></tr>
<tr><td><em class=def id="mus-ycoeff">mus-ycoeff</em></td><td>y (output, feedback) coefficient</td></tr>
<tr><td><em class=def id="mus-ycoeffs">mus-ycoeffs</em></td><td>float-vector of y (feedback) coefficients</td></tr>
</table>
<p>Many of these are settable:
<code>(set! (mus-frequency osc1) 440.0)</code>
sets osc1's phase increment to (<a class=quiet href="#hztoradians">hz->radians</a> 440.0).
When I have a cold, I sometimes use the following function to see how high I can hear; count
the audible tones and multiply by 1000:
</p>
<pre class="indented">
(define (quick-check)
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> ()
(let ((gen (<a class=quiet href="#make-oscil">make-oscil</a> 1000)))
(do ((i 0 (+ i 1)))
((= i 400000))
(if (= (modulo i 20000) 0)
(set! (<em class=red>mus-frequency</em> gen) (+ 1000 (/ i 20))))
(<a class=quiet href="#outa">outa</a> i (* .5 (<a class=quiet href="#oscil">oscil</a> gen)))))))
</pre>
<p>Another example is run-with-fm-and-pm in generators.scm which applies phase modulation (as well as
the default frequency modulation) to any generator:
</p>
<pre class="indented">
(define (run-with-fm-and-pm gen fm pm)
(set! (<em class=red>mus-phase</em> gen) (+ (<em class=red>mus-phase</em> gen) pm))
(let ((result (<em class=red>mus-run</em> gen fm 0.0)))
(set! (<em class=red>mus-phase</em> gen) (- (<em class=red>mus-phase</em> gen) pm))
result))
</pre>
<p>
<em class=def id="musgeneratorp">mus-generator?</em> returns #t if its argument is
a generator.
A generator defined via <a href="#defgenerator">defgenerator</a> can also take part in these methods.
</p>
<div class="header" id="othergenerators">Other generators</div>
<!-- OTHER GENERATORS -->
<p>(this section is work in progress...)
</p>
<p>There are dozens of generators scattered around the *.scm files that come with Snd. Some that come to mind:
</p>
<pre class="indented">
analog-filter.scm:
filter: <a class=quiet href="sndscm.html#analogfilterdoc">butterworth-lowpass|highpass|bandpass|bandstop</a>,
<a class=quiet href="sndscm.html#analogfilterdoc">chebyshev-lowpass|highpass|bandpass|bandstop</a>,
<a class=quiet href="sndscm.html#analogfilterdoc">inverse-chebyshev-lowpass|highpass|bandpass|bandstop</a>,
<a class=quiet href="sndscm.html#analogfilterdoc">elliptic-lowpass|highpass|bandpass|bandstop</a>,
<a class=quiet href="sndscm.html#analogfilterdoc">bessel-lowpass|highpass|bandpass|bandstop</a>
clm-ins.scm:
<a class=quiet href="sndscm.html#rmsgain">rms gain balance</a>
dsp.scm:
fir-filter: <a class=quiet href="sndscm.html#hilberttransform">hilbert-transform</a>,
<a class=quiet href="sndscm.html#makehighpass">highpass, lowpass, bandpass, bandstop</a>,
<a class=quiet href="sndscm.html#makedifferentiator">differentiator</a>,
<a class=quiet href="sndscm.html#makespencerfilter">make-spencer-filter</a>,
<a class=quiet href="sndscm.html#sgfilter">savitzky-golay-filter</a>
filter: <a class=quiet href="sndscm.html#makebutter">butter-high-pass, butter-low-pass, butter-band-pass, butter-band-reject</a>,
<a class=quiet href="sndscm.html#makebiquad">biquad</a>,
<a class=quiet href="sndscm.html#IIRfilters">iir-low-pass, iir-high-pass, iir-band-pass, iir-band-stop, peaking</a>,
<a class=quiet href="sndscm.html#makebutter">butter-lp, butter-hp, butter-bp, butter-bs</a>
<a class=quiet href="sndscm.html#volterrafilter">volterra-filter</a>
env.scm:
<a class=quiet href="sndscm.html#powerenv">power-env</a> (and many env makers/modifiers)
extensions.scm:
<a class=quiet href="sndscm.html#envexptchannel">env-expt-channel</a> (and many related env modifiers)
examp.scm:
<a class=quiet href="sndscm.html#makeramp">ramp</a>,
<a class=quiet href="sndscm.html#soundinterp">sound-interp</a>
moog.scm:
<a class=quiet href="sndscm.html#moogfilter">moog-filter</a>
prc95.scm:
<a class=quiet href="sndscm.html#prc95doc">reed, bowtable, jettable, onep, lip, dc-block, delaya, delayl</a>
zip.scm:
<a class=quiet href="sndscm.html#zipper">zipper</a>
</pre>
<p>In this section, we concentrate on the generators defined in generators.scm. Nearly all of them respond to the
generic functions mus-name, mus-reset, mus-describe, mus-frequency, mus-scaler, mus-offset, mus-phase, and mus-order.
The parameters are generally "frequency", "n" (the number of sidebands), "r" (the ratio between successive sideband
amplitudes), and "ratio" (the ratio between the
frequency and the spacing between successive sidebands).
</p>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-polyoid">make-polyoid</em>
(frequency 0.0)
(partial-amps-and-phases '(1 1 0.0)) ; a list of harmonic numbers, their associated amplitudes, and their initial-phases
<em class=def id="polyoid">polyoid</em> w (fm 0.0)
<em class=def id="polyoid?">polyoid?</em> w
<em class=def id="polyoidenv">polyoid-env</em> w fm amps phases
<em class=def id="make-noid">make-noid</em> (frequency 0.0) (n 1) phases
<em class=def id="noid">noid</em> w (fm 0.0)
</pre>
<p>polyoid combines the first and second Chebyshev polynomials to provide
a sum of sinusoids each with arbitrary amplitude and initial-phase.
noid is a wrapper for polyoid that sets up n equal amplitude components, a generalization
of <a href="#ncosdoc">ncos and nsin</a>.
noid's phase argument can be a float-vector, <code>'min-peak</code>, <code>'max-peak</code>, or omitted (#f).
If omitted, the phases are set to random numbers between 0 and 2 pi; if
a float-vector, the float-vector's values are used as the phases; if 'max-peak, all phases are set
to pi/2 (ncos essentially — use <code>(make-float-vector n)</code> to get nsin);
and if 'min-peak, the minimum peak amplitude phases in <a href="sndscm.html#peakphasesdoc">peak-phases.scm</a> are used.
In the 'min-peak and 'max-peak cases, noid's output is normalized to fall between -1.0 and 1.0.
polyoid-env is an extension of polyoid that takes envelopes to control the amplitude and phase of each
harmonic.
</p>
<img src="pix/noidchoices.png" alt="noid choices">
<!--
run -horizontal
(do ((i 0 (+ i 1)))
((= i 4))
(with-sound (:clipped #f :output (string-append "test-noid-" (number->string i) ".snd"))
(let ((samps 44100)
(gen (make-noid 100.0 32 (if (= i 0) 'max-peak
(if (= i 1) (make-float-vector 32)
(if (= i 2) #f
'min-peak))))))
(do ((i 0 (+ i 1)))
((= i samps))
(outa i (noid gen 0.0))))))
-->
<p>We can use the <a href="sndscm.html#peakphasesdoc">peak-phases.scm</a> phases to reduce the "spikiness" of the waveform with any
set of components and component amplitudes. We could, for example, change noid to use
</p>
<pre class="indented">
(set! (amps (+ j 1)) (/ (expt r (- i 1)) norm))
</pre>
<p>where "r" is the ratio between successive component amplitude: "nroid"?
This is not as pointless as it might at first appear. Many of these waveforms actually
sound different, despite having the same (magnitude) spectrum; the minimum peak
version usually sounds raspier, and in the limit it can sound like white noise!
</p>
<!--
(with-sound (:clipped #f)
(let* ((dur 2.0)
(samps (seconds->samples dur))
(gen1 (make-noid 40.0 128 'max-peak))
(gen2 (make-noid 40.0 128 'min-peak))
(ampf (make-env '(0 0 .1 1 1 1 2 .25 3 .25) :scaler 0.5 :duration dur))
(indf (make-env '(0 0 1 0 2 1 3 1) :duration dur)))
(do ((i 0 (+ i 1)))
((= i samps))
(let ((ind (env indf)))
(outa i (* (env ampf)
(+ (* ind
(noid gen2 0.0))
(* (- 1.0 ind)
(noid gen1 0.0)))))))))
-->
<!--
<img class="indented" src="noid2.png">
-->
<p>Check out the n=1024 case:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> ()
(let ((samps 44100)
(gen (<em class=red>make-noid</em> 10.0 1024 'min-peak)))
(do ((i 0 (+ i 1)))
((= i samps))
(<a class=quiet href="#outa">outa</a> i (* 0.5 (<em class=red>noid</em> gen 0.0))))))
</pre>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-asyfm">make-asyfm</em> (frequency 0.0) (ratio 1.0) (r 1.0) (index 1.0)
<em class=def id="asyfmJ">asyfm-J</em> gen (fm 0.0)
<em class=def id="asyfmI">asyfm-I</em> gen (fm 0.0)
<em class=def id="asyfm?">asyfm?</em> gen
</pre>
<p>These two generators produce the two flavors of asymmetric-fm. asyfm-J is the same as the built-in asymmetric generator;
asyfm-I is the modified Bessel function version (the second formula in the <a href="#asymmetric-fm">asymmetric-fm</a> section).
</p>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-fmssb">make-fmssb</em> (frequency 0.0) (ratio 1.0) (index 1.0)
<em class=def id="fmssb">fmssb</em> gen (fm 0.0)
<em class=def id="fmssb?">fmssb?</em> gen
</pre>
<p>This generator produces the "gapped" spectra mentioned in <a href="fm.html">fm.html</a>. It is used extensively in the
various "imaginary machines". Also included in this section of generators.scm is fpmc, an instrument
that performs FM with a complex index (complex in the sense of complex numbers).
</p>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-blackman">make-blackman</em> frequency n ; 1 <= n <= 10
<em class=def id="blackman">blackman</em> gen (fm 0.0)
<em class=def id="blackman?">blackman?</em> gen
</pre>
<p>
This produces a Blackman-Harris sum of cosines of order 'n'. It could be viewed as a special case of pulsed-env, or
as yet another "kernel" along the lines of <a href="#ncos">ncos</a>.
</p>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-sinc-train">make-sinc-train</em> frequency (n 1)
<em class=def id="sinc-train">sinc-train</em> gen (fm 0.0)
<em class=def id="sinc-train?">sinc-train?</em> gen
</pre>
<p>
This produces a sinc-train ((sin x)/x) with n components. It is very similar to <a href="#ncos">ncos</a>.
</p>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-pink-noise">make-pink-noise</em> (n 1)
<em class=def id="pink-noise">pink-noise</em> gen
<em class=def id="pink-noise?">pink-noise?</em> gen
</pre>
<p>
This produces a reasonable approximation to 1/f noise, also known as pink-noise. 'n' sets the number of octaves used (starting at the high end);
12 is the recommended choice. (If n=1, you get white noise).
</p>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-brown-noise">make-brown-noise</em> frequency (amplitude 1.0)
<em class=def id="brown-noise">brown-noise</em> gen
<em class=def id="brown-noise?">brown-noise?</em> gen
</pre>
<p>
This produces (unbounded) brownian noise. 'amplitude' sets the maximum size of individual jumps.
</p>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-green-noise">make-green-noise</em> (frequency 0.0) (amplitude 1.0) (low -1.0) (high 1.0)
<em class=def id="green-noise">green-noise</em> gen (fm 0.0)
<em class=def id="green-noise?">green-noise?</em> gen
<em class=def id="make-green-noise-interp">make-green-noise-interp</em> (frequency 0.0) (amplitude 1.0) (low -1.0) (high 1.0)
<em class=def id="green-noise-interp">green-noise-interp</em> gen (fm 0.0)
<em class=def id="green-noise-interp?">green-noise-interp?</em> gen
</pre>
<p>These two generators produce bounded brownian noise; "green-noise" was Michael McNabb's name for it.
Unlike CLM's <a href="#rand">rand</a> or <a href="#rand-interp">rand-interp</a> which
produce white noise centered around 0.0, green-noise wanders around, bouncing off its bounds
every now and then. This produces a noise that can be similar to pink noise (see some graphs under <a href="#rand">rand</a>).
My informal explanation is that each time we bounce off an edge, we're transferring energy
from a low frequency into some higher frequency. It is still brownian noise however.
The 'amplitude' argument controls how large individual steps can be; 'low' and 'high' set
the overall output bounds; 'frequency' controls how often a new random number is chosen.
Here's an instrument that fuzzes up its amplitude envelope a bit using green noise:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#definstrument">definstrument</a> (green3 start dur freq amp amp-env noise-freq noise-width noise-max-step)
(let* ((grn (<em class=red>make-green-noise-interp</em> :frequency noise-freq
:amplitude noise-max-step
:high (* 0.5 noise-width) :low (* -0.5 noise-width)))
(osc (<a class=quiet href="#make-oscil">make-oscil</a> freq))
(e (<a class=quiet href="#make-env">make-env</a> amp-env :scaler amp :duration dur))
(beg (<a class=quiet href="#secondstosamples">seconds->samples</a> start))
(end (+ beg (<a class=quiet href="#secondstosamples">seconds->samples</a> dur))))
(do ((i beg (+ i 1)))
((= i end))
(<a class=quiet href="#outa">outa</a> i (* (<a class=quiet href="#env">env</a> e)
(+ 1.0 (<em class=red>green-noise-interp</em> grn))
(<a class=quiet href="#oscil">oscil</a> osc))))))
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> ()
(green3 0 2.0 440 .5 '(0 0 1 1 2 1 3 0) 100 .2 .02))
</pre>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-adjustable-square-wave">make-adjustable-square-wave</em> frequency (duty-factor 0.5) (amplitude 1.0)
<em class=def id="adjustable-square-wave">adjustable-square-wave</em> gen (fm 0.0)
<em class=def id="adjustable-square-wave?">adjustable-square-wave?</em> gen
<em class=def id="make-adjustable-triangle-wave">make-adjustable-triangle-wave</em> frequency (duty-factor 0.5) (amplitude 1.0)
<em class=def id="adjustable-triangle-wave">adjustable-triangle-wave</em> gen (fm 0.0)
<em class=def id="adjustable-triangle-wave?">adjustable-triangle-wave?</em> gen
<em class=def id="make-adjustable-sawtooth-wave">make-adjustable-sawtooth-wave</em> frequency (duty-factor 0.5) (amplitude 1.0)
<em class=def id="adjustable-sawtooth-wave">adjustable-sawtooth-wave</em> gen (fm 0.0)
<em class=def id="adjustable-sawtooth-wave?">adjustable-sawtooth-wave?</em> gen
</pre>
<p>
adjustable-square-wave produces a square-wave with optional "duty-factor" (ratio of pulse duration to pulse period).
The other two are similar, producing triangle and sawtooth waves. There is also an adjustable-oscil.
Use mus-scaler to set the duty-factor at run-time.
</p>
<img class="indented" src="pix/adjustable.png" alt="mus-scaler adjusts">
<!-- adjustable.png:
(with-sound (:channels 4)
(let* ((fsamps 500)
(freq (radians->hz (/ 1.0 fsamps)))
(sq (make-adjustable-square-wave freq 0.01))
(sw (make-adjustable-sawtooth-wave freq 0.01))
(tr (make-adjustable-triangle-wave freq 0.01))
(os (make-adjustable-oscil freq 0.01))
(pl (make-pulse-train freq))
(dur 1.0)
(samps (seconds->samples dur))
(ampf (make-env '(0 0 1 1 100 1 101 0) :duration dur))
(adjf (make-env '(0 .01 1 .99) :duration dur)))
(do ((i 0 (+ i 1)))
((= i samps))
(let ((amp (env ampf))
(adj (env adjf))
(trigger (pulse-train pl)))
(if (> trigger 0.1)
(begin
(set! (mus-scaler sq) adj)
(set! (mus-scaler sw) adj)
(set! (mus-scaler tr) adj)
(set! (mus-scaler os) adj)))
(outa i (* amp (adjustable-square-wave sq)))
(outb i (* amp (adjustable-sawtooth-wave sw)))
(outc i (* amp (adjustable-triangle-wave tr)))
(outd i (* amp (adjustable-oscil os)))
))
))
(set! *selected-graph-color* (make-color 1 1 1))
(set! *selected-data-color* (make-color 0 0 0))
(set! (x-axis-label 0 0) "adjustable-square-wave, duty-factor from .01 to .99")
(set! (x-axis-label 0 1) "adjustable-sawtooth-wave")
(set! (x-axis-label 0 2) "adjustable-triangle-wave")
(set! (x-axis-label 0 3) "adjustable-oscil")
(set! *axis-numbers-font* *tiny-font*)
(set! *axis-label-font* "10x20")
-->
<p>A similar trick can make, for example, a squared-off triangle-wave:
</p>
<pre class="indented">
(gen (<a class=quiet href="#make-triangle-wave">make-triangle-wave</a> 200.0 :amplitude 4)) ; amp sets slope
...
(outa i (max -1.0 (min 1.0 (<a class=quiet href="#triangle-wave">triangle-wave</a> gen))))
</pre>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-round-interp">make-round-interp</em> frequency n amplitude
<em class=def id="round-interp">round-interp</em> gen (fm 0.0)
<em class=def id="round-interp?">round-interp?</em> gen
</pre>
<p>
This is a <a href="#rand-interp">rand-interp</a> generator feeding a <a href="#moving-average">moving-average</a> generator. "n" is the length of the moving-average;
the higher "n", the more low-passed the output.
</p>
<img class="indented" src="pix/roundinterp.png" alt="round-interp">
<!--
(with-sound (:channels 5)
(let ((gen0 (make-round-interp 100 1))
(gen1 (make-round-interp 100 10))
(gen2 (make-round-interp 100 100))
(gen3 (make-round-interp 100 1000))
(gen4 (make-round-interp 100 10000)))
(do ((i 0 (+ i 1)))
((= i 100000))
(out-any i (round-interp gen0) 0)
(out-any i (round-interp gen1) 1)
(out-any i (round-interp gen2) 2)
(out-any i (round-interp gen3) 3)
(out-any i (round-interp gen4) 4))))
(set! (x-axis-label 0 0) "round-interp, n=1")
(set! (x-axis-label 0 1) "round-interp, n=10")
(set! (x-axis-label 0 2) "round-interp, n=100")
(set! (x-axis-label 0 3) "round-interp, n=1000")
(set! (x-axis-label 0 4) "round-interp, n=10000")
(set! *axis-label-font* "-*-times-medium-r-normal-*-20-*-*-*-*-*-*-*")
-->
<div class="separator"></div>
<pre class="indented">
<A class=def>make-moving-sum</A> (n 128)
<em class=def id="moving-sum">moving-sum</em> gen y
<A class=def>moving-sum?</A> gen
<A class=def>make-moving-rms</A> (n 128)
<em class=def id="moving-rms">moving-rms</em> gen y
<A class=def>moving-rms?</A> gen
<A class=def>make-moving-length</A> (n 128)
<em class=def id="moving-length">moving-length</em> gen y
<A class=def>moving-length?</A> gen
<A class=def>make-weighted-moving-average</A> n
<em class=def id="weighted-moving-average">weighted-moving-average</em> gen y
<A class=def>weighted-moving-average?</A> gen
<A class=def>make-exponentially-weighted-moving-average</A> n
<em class=def id="exponentially-weighted-moving-average">exponentially-weighted-moving-average</em> gen y
<A class=def>exponentially-weighted-moving-average?</A> gen
</pre>
<p>
The "moving" generators are specializations of the <a href="#moving-average">moving-average</a>
generator. moving-sum keeps the ongoing sum of absolute values, moving-length the square root of the sum
of squares, and moving-rms the square root of the sum of squares divided by the size.
moving-rms is used in <a href="sndscm.html#overlayrmsenv">overlay-rms-env</a> in draw.scm.
weighted-moving-average weights the
table entries by 1/n. Similarly exponentially-weighted-moving-average applies exponential weights (it is
actually just a one-pole filter — this generator wins the "longest-name-for-simplest-effect" award).
Also defined, but not tested, is moving-variance; in the same mold, but not defined, are things like moving-inner-product and moving-distance.
</p>
<!-- bessel functions -->
<div class="innerheader">Bessel functions</div>
<pre class="indented">
<em class=def id="make-bess">make-bess</em> (frequency 0.0) (n 0)
<em class=def id="bess">bess</em> gen (fm 0.0)
<em class=def id="bess?">bess?</em> gen
</pre>
<!-- LATEX: & J_{n}(x) = \frac{a}{\sqrt{x}}\sin(x+\delta) + \frac{r_{n}(x)}{x^{\frac{3}{2}}} \\ -->
<p>bess produces the nth Bessel function. The generator output is scaled to have a maximum of 1.0,
so bess's output is not the same as the raw bessel function value returned by <a href="extsnd.html#besj0">bes-jn</a>.
The "frequency" argument actually makes sense here because the Bessel functions
are close to damped sinusoids after their initial hesitation:
</p>
<img class="indented" src="pix/sceq24.png" alt="Jn">
<p>
where the variables other than x remain bounded as x increases. This explains, in a sketchy way, why Jn(cos) and Jn(Jn)
behave like FM. To see how close these are to FM, compare the expansion of J0(sin) with FM's cos(sin):
</p>
<!-- LATEX:
sceq35:
& J_{0}(B \sin x) = J_{0}^{2}\Big(\frac{B}{2}\Big)+2\sum_{k=1}^{\infty} J_{k}^{2}\Big(\frac{B}{2}\Big)\cos2kx \\
& \cos(B \sin x) = J_{0}(B) + 2 \sum_{k=1}^{\infty} J_{2k}(B) \cos 2kx \\
-->
<img class="indented" src="pix/sceq35.png" alt="J(sin) and cos(sin)">
<p>Except for jpcos, the rest of the generators in this section suffer a similar fate. From a waveshaping perspective,
we're using a sinusoid, or a modulated sinusoid, to index into the near-zero portion of a Bessel
function, and the result is sadly reminiscent of standard FM. But they're such pretty formulas;
I must be missing something.
</p>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-j0evencos">make-j0evencos</em> (frequency 0.0) (index 1.0)
<em class=def id="j0evencos">j0evencos</em> gen (fm 0.0)
<em class=def id="j0evencos?">j0evencos?</em> gen
</pre>
<p>j0evencos produces the J0(index * sin(x)) case mentioned above (with the DC component subtracted out).
If you sweep the index, the bandwidth is the same as in normal FM (J2k(B) is about 3log(k)*Jk(B/2)^2),
but the B/2 factor causes
the individual component amplitudes to follow the Bessel functions half as fast.
So j0evencos produces a spectral sweep that is like FM's but smoother.
</p>
<img class="indented" src="pix/j0fm.png" alt="compare FM and j0evencos">
<img class="indented" src="pix/j0jfm.png" alt="compare FM and j0evencos">
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:channels 2)
(let* ((dur 1.0)
(end (<a class=quiet href="#secondstosamples">seconds->samples</a> dur))
(jmd (<em class=red>make-j0evencos</em> 200.0))
(fmd (<a class=quiet href="#make-oscil">make-oscil</a> 200.0))
(ampf (<a class=quiet href="#make-env">make-env</a> '(0 0 1 1 20 1 21 0) :scaler 0.5 :duration dur))
(indf (<a class=quiet href="#make-env">make-env</a> '(0 0 1 20) :duration dur)))
(do ((i 0 (+ i 1)))
((= i end))
(let ((ind (<a class=quiet href="#env">env</a> indf))
(vol (<a class=quiet href="#env">env</a> ampf)))
(set! (<em class=red>jmd 'index</em>) ind)
(<a class=quiet href="#outa">outa</a> i (* vol (- (cos (* ind (<a class=quiet href="#oscil">oscil</a> fmd)))
(<a class=quiet href="extsnd.html#besj0">bes-j0</a> ind)))) ; subtract out DC (see cos(B sin x) above)
(<a class=quiet href="#out-any">outb</a> i (* vol (<em class=red>j0evencos</em> jmd)))))))
</pre>
<!--
snd -horizontal
(define* (fm beg dur freq amp mc-ratio index (index-env '(0 1 100 1)))
(let* ((start (seconds->samples beg))
(end (+ start (seconds->samples dur)))
(md (make-oscil (* freq mc-ratio)))
(ampf (make-env '(0 0 1 1 20 1 21 0) :scaler amp :duration dur))
(indf (make-env index-env :scaler index :duration dur)))
(do ((i start (+ i 1)))
((= i end))
(let ((ind (env indf)))
(outa i (* (env ampf)
(- (cos (* ind (oscil md)))
(bes-j0 ind))))))))
(with-sound ("test.snd") (fm 0 1.0 2000.0 0.5 .1 20.0 '(0 0 1 1)))
(define* (jfm beg dur freq amp mc-ratio index (index-env '(0 1 100 1)))
(let* ((start (seconds->samples beg))
(end (+ start (seconds->samples dur)))
(md (make-j0evencos (* freq mc-ratio)))
(ampf (make-env '(0 0 1 1 20 1 21 0) :scaler amp :duration dur))
(indf (make-env index-env :scaler index :duration dur)))
(do ((i start (+ i 1)))
((= i end))
(set! (md 'index) (env indf))
(outa i (* (env ampf)
(j0evencos md))))))
(with-sound ("test1.snd") (jfm 0 1.0 2000.0 0.5 .1 20.0 '(0 0 1 1)))
x 314 1.36
y 327 .4
z 0 1.9
4 0.24
blackman6 2048
(set! *selected-graph-color* (make-color 1 1 1))
-->
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-j0j1cos">make-j0j1cos</em> (frequency 0.0) (index 0.0)
<em class=def id="j0j1cos">j0j1cos</em> gen fm
<em class=def id="j0j1cos?">j0j1cos?</em> gen
</pre>
<img class="indented" src="pix/sceq36.png" alt="j0j1 formulsa">
<p>
This uses J0(index * cos(x)) + J1(index * cos(x)) to produce a full set of cosines. It is not yet normalized correctly,
and is very similar to normal FM.
</p>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-izcos">make-izcos</em> (frequency 0.0) (r 1.0)
<em class=def id="izcos">izcos</em> gen (fm 0.0)
<em class=def id="izcos?">izcos?</em> gen
</pre>
<img class="indented" src="pix/sceq39.png" alt="I(k) sum">
<p>This produces a sum of cosines scaled by In(r), again very similar to normal FM.
</p>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-jjcos">make-jjcos</em> (frequency 0.0) (r 0.5) (a 1.0) (k 1.0)
<em class=def id="jjcos">jjcos</em> gen (fm 0.0)
<em class=def id="jjcos?">jjcos?</em> gen
<em class=def id="make-j2cos">make-j2cos</em> (frequency 0.0) (r 0.5) (n 1)
<em class=def id="j2cos">j2cos</em> gen (fm 0.0)
<em class=def id="j2cos?">j2cos?</em> gen
<em class=def id="make-jpcos">make-jpcos</em> (frequency 0.0) (r 0.5) (a 0.0) (k 1.0)
<em class=def id="jpcos">jpcos</em> gen (fm 0.0)
<em class=def id="jpcos?">jpcos?</em> gen
<em class=def id="make-jncos">make-jncos</em> (frequency 0.0) (r 0.5) (a 1.0) (n 0)
<em class=def id="jncos">jncos</em> gen (fm 0.0)
<em class=def id="jncos?">jncos?</em> gen
</pre>
<p>These produce a sum of cosines scaled by a product of Bessel functions; in a sense, there are two, or maybe three "indices".
Normalization is handled correctly at least for jpcos. Of the four, jpcos seems the most interesting. "a" should not equal "r";
in general as a and r approach 1.0, the spectrum approaches "k" components, sometimes in a highly convoluted manner.
</p>
<table>
<tr><td>jjcos:</td><td><img class="indented" src="pix/sceq31.png" alt="more sums"></td></tr>
<tr><td>j2cos:</td><td><img class="indented" src="pix/sceq34.png" alt="more sums"></td></tr>
<tr><td>jpcos:</td><td><img class="indented" src="pix/sceq33.png" alt="more sums"></td></tr>
<tr><td>jncos:</td><td><img class="indented" src="pix/sceq32.png" alt="more sums"></td></tr>
</table>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-jycos">make-jycos</em> (frequency 0.0) (r 1.0) (a 1.0)
<em class=def id="jycos">jycos</em> gen (fm 0.0)
<em class=def id="jycos?">jycos?</em> gen
</pre>
<p>This uses bes-y0 to produce components scaled by Yn(r)*Jn(a). bes-y0(0) is -inf, so a^2 + r^2 should be greater than 2ar,
and r should be greater than 0.0. Tricky to use. (If you get an inf or a NaN from division by zero or whatever in Scheme,
both the time and frequency graphs will be unhappy).
</p>
<!-- finite sums -->
<div class="innerheader">finite sums</div>
<p>These generators produce a set of n sinusoids.
With a bit of bother,
they could be done with polywave. I don't
think there would be any difference, even taking FM into account.
</p>
<pre class="indented">
<em class=def id="make-nssb">make-nssb</em> (frequency 0.0) (ratio 1.0) (n 1)
<em class=def id="nssb">nssb</em> gen (fm 0.0)
<em class=def id="nssb?">nssb?</em> gen
</pre>
<p>nssb is the single side-band version of ncos and nsin. It is very similar to <a href="#nxysin">nxysin</a> and <a href="#nxycos">nxycos</a>.
</p>
<div class="separator"></div>
<pre class="indented">
<em class=emdef>make-ncos2</em> (frequency 0.0) (n 1)
<em class=emdef id="ncos2">ncos2</em> gen (fm 0.0)
<em class=def id="ncos2?">ncos2?</em> gen
</pre>
<p>This is the Fejer kernel. The i-th harmonic amplitude is (n-i)/(n+1).
</p>
<!-- LATEX: &\frac{\sin^{2} \frac{1}{2}nx}{\sin^{2} \frac{1}{2}x} = \frac{n}{2} + \sum_{k=1}^{n-1} (n-k)\cos kx -->
<img class="indented" src="pix/sceq23.png" alt="sum of cosines">
<div class="separator"></div>
<pre class="indented">
<em class=emdef>make-ncos4</em> (frequency 0.0) (n 1)
<em class=emdef>ncos4</em> gen (fm 0.0)
<em class=def id="ncos4?">ncos4?</em> gen
</pre>
<p>This is the Jackson kernel, the square of ncos2.
</p>
<!-- LATEX: &\Bigg(\frac{\sin(n+\frac{1}{2})x}{\sin \frac{x}{2}}\Bigg)^{4} -->
<!-- pointless...
<img class="indented" src="pix/sceq24.png" alt="sum of cosines">
-->
<pre class="indented">
<em class=emdef>make-npcos</em> (frequency 0.0) (n 1)
<em class=emdef>npcos</em> gen (fm 0.0)
<em class=def id="npcos?">npcos?</em> gen
</pre>
<p>This is the Poussin kernel, a combination of two ncos2 generators, one at "n" subtracted from twice another at 2n+1.
</p>
<pre class="indented">
<em class=def id="make-n1cos">make-n1cos</em> (frequency 0.0) (n 1)
<em class=def id="n1cos">n1cos</em> gen (fm 0.0)
<em class=def id="n1cos?">n1cos?</em> gen
</pre>
<p>Another spikey waveform, very similar to ncos2 above.
</p>
<img class="indented" src="pix/sceq45.png" alt="linear cosines">
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-nxycos">make-nxycos</em> (frequency 0.0) (ratio 1.0) (n 1)
<em class=def id="nxycos">nxycos</em> gen (fm 0.0)
<em class=def id="nxycos?">nxycos?</em> gen
<em class=def id="make-nxysin">make-nxysin</em> (frequency 0.0) (ratio 1.0) (n 1)
<em class=def id="nxysin">nxysin</em> gen (fm 0.0)
<em class=def id="nxysin?">nxysin?</em> gen
<em class=def id="make-nxy1cos">make-nxy1cos</em> (frequency 0.0) (ratio 1.0) (n 1)
<em class=def id="nxy1cos">nxy1cos</em> gen (fm 0.0)
<em class=def id="nxy1cos?">nxy1cos?</em> gen
<em class=def id="make-nxy1sin">make-nxy1sin</em> (frequency 0.0) (ratio 1.0) (n 1)
<em class=def id="nxy1sin">nxy1sin</em> gen (fm 0.0)
<em class=def id="nxy1sin?">nxy1sin?</em> gen
</pre>
<p>These produce a sum of "n" sinsoids starting at "frequency", spaced by "ratio". Since "frequency" can be treated
as the carrier, there's no point in an ssb version. nxy1cos is the same as nxycos, but every other
component is multiplied by -1, and "n" produces 2n components.
Normalization in the "sin" cases is tricky. If ratio is 1, we can use nsin's normalization, and if ratio = 2, noddsin's,
but otherwise nxysin currently uses 1/n. This ensures that the generator output is always between -1 and 1,
but in some cases (mainly involving low "n" and simple "ratio"), the output might not be full amplitude. nxy1sin is
even trickier, so it divides by "n".
</p>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-noddcos">make-noddcos</em> (frequency 0.0) (n 1)
<em class=def id="noddcos">noddcos</em> gen (fm 0.0)
<em class=def id="noddcos?">noddcos?</em> gen
<em class=def id="make-noddsin">make-noddsin</em> (frequency 0.0) (n 1)
<em class=def id="noddsin">noddsin</em> gen (fm 0.0)
<em class=def id="noddsin?">noddsin?</em> gen
<em class=def id="make-noddssb">make-noddssb</em> (frequency 0.0) (ratio 1.0) (n 1)
<em class=def id="noddssb">noddssb</em> gen (fm 0.0)
<em class=def id="noddssb?">noddssb?</em> gen
</pre>
<!-- LATEX: &\sum_{k=1}^{n}\cos(2k-1)x=\frac{1}{2}\frac{\sin 2nx}{\sin x} \\ -->
<p>These produce the sum of "n" equal amplitude odd-numbered sinusoids:
</p>
<img class="indented" src="pix/sceq29.png" alt="sum of cosines">
<!-- LATEX: &\sum_{k=1}^{n}\sin(2k-1)x=\frac{\sin^{2}nx}{\sin x} \\ -->
<img class="indented" src="pix/sceq28.png" alt="sum of sines">
<p>The corresponding "even" case is the same as ncos with twice the frequency. noddsin produces a somewhat clarinet-like tone:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:play #t)
(let ((gen (<em class=red>make-noddsin</em> 300 :n 3))
(ampf (<a class=quiet href="#make-env">make-env</a> '(0 0 1 1 2 1 3 0) :length 40000 :scaler .5)))
(do ((i 0 (+ i 1)))
((= i 40000))
(<a class=quiet href="#outa">outa</a> i (* (<a class=quiet href="#env">env</a> ampf) (<em class=red>noddsin</em> gen))))))
</pre>
<p>noddsin normalization is the same as nsin. The peak happens half as far from the 0 crossing as in nsin (3pi/(4n) for nsin,
and 3pi/(8n) for noddsin (assuming large n)), and its amplitude is 8n*sin^2(3pi/8)/(3pi), just as in nsin. The noddsin generator scales its output
by the inverse of this, so it is always between -1 and 1.
</p>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-nrcos">make-nrcos</em> (frequency 0.0) (n 1) (r 0.5) ; -1.0 < r < 1.0
<em class=def id="nrcos">nrcos</em> gen (fm 0.0)
<em class=def id="nrcos?">nrcos?</em> gen
<em class=def id="make-nrsin">make-nrsin</em> (frequency 0.0) (n 1) (r 0.5) ; -1.0 < r < 1.0
<em class=def id="nrsin">nrsin</em> gen (fm 0.0)
<em class=def id="nrsin?">nrsin?</em> gen
<em class=def id="make-nrssb">make-nrssb</em> (frequency 0.0) (ratio 1.0) (n 1) (r 0.5) ; 0.0 <= r < 1.0
<em class=def id="nrssb">nrssb</em> gen (fm 0.0)
<em class=def id="nrssbinterp">nrssb-interp</em> gen fm interp
<em class=def id="nrssb?">nrssb?</em> gen
</pre>
<p>These produce the sum of "n" sinusoids, with successive sinusoids scaled by "r"; the nth component has amplitude r^n.
nrsin is just a wrapper for <a href="#nrxysin">nrxysin</a>, and the other two are obviously variants of <a href="#nrxycos">nrxycos</a>.
In the nrssb-interp generator, the "interp" argument interpolates between the upper (interp=1.0) and lower (interp=-1.0) side bands.
</p>
<p>The instrument lutish uses nrcos: <code>lutish beg dur freq amp</code>:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:play #t)
(do ((i 0 (+ i 1)))
((= i 10))
(lutish (* i .1) 2 (* 100 (+ i 1)) .05)))
</pre>
<p>The instrument oboish uses nrssb: <code>oboish beg dur freq amp amp-env</code>:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:play #t)
(do ((i 0 (+ i 1)))
((= i 10))
(oboish (* i .3) .4 (+ 100 (* 50 i)) .05 '(0 0 1 1 2 1 3 0))))
</pre>
<p>organish also uses nrssb: <code>organish beg dur freq amp fm-index amp-env</code>:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:play #t)
(do ((i 0 (+ i 1)))
((= i 10))
(organish (* i .3) .4 (+ 100 (* 50 i)) .5 1.0 #f)))
</pre>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-nkssb">make-nkssb</em> (frequency 0.0) (ratio 1.0) (n 1)
<em class=def id="nkssb">nkssb</em> gen (fm 0.0)
<em class=def id="nkssbinterp">nkssb-interp</em> gen fm interp
<em class=def id="nkssb?">nkssb?</em> gen
</pre>
<p>This generator produces the single side-band version of the sum of "n" sinusoids, where the nth component has amplitude n.
In the nkssb-interp generator, the "interp" argument interpolates between the upper and lower side bands.
The instrument nkssber uses nkssb-interp:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:play #t)
(nkssber 0 1 1000 100 5 5 0.5)
(nkssber 1 2 600 100 4 1 0.5)
(nkssber 3 2 1000 540 3 3 0.5)
(nkssber 5 4 300 120 2 0.25 0.5)
(nkssber 9 1 30 4 40 0.5 0.5)
(nkssber 10 1 20 6 80 0.5 0.5))
</pre>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-nsincos">make-nsincos</em> (frequency 0.0) (n 1)
<em class=def id="nsincos">nsincos</em> gen (fm 0.0)
<em class=def id="nsincos?">nsincos?</em> gen
</pre>
<p>This generator produces a sum of n cosines scaled by sin(k*pi/(n+1))/sin(pi/(n+1)).
</p>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-nchoosekcos">make-nchoosekcos</em> (frequency 0.0) (ratio 1.0) (n 1)
<em class=def id="nchoosekcos">nchoosekcos</em> gen (fm 0.0)
<em class=def id="nchoosekcos?">nchoosekcos?</em> gen
</pre>
<p>This generator produces a sum of n cosines scaled by the binomial coefficients. If n is even, the last term is halved.
All these "finite sum" generators are a bit inflexible, and sound more or less the same. One (desperate) countermeasure
is amplitude modulation:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> ()
(let ((modulator (<em class=red>make-nchoosekcos</em> 100.0 1.0 4))
(carrier (<a class=quiet href="#make-nrcos">make-nrcos</a> 2000.0 :n 3 :r .5)))
(do ((i 0 (+ i 1))) ((= i 20000))
(<a class=quiet href="#outa">outa</a> i (* .5 (<a class=quiet href="#nrcos">nrcos</a> carrier)
(<em class=red>nchoosekcos</em> modulator))))))
</pre>
<img src="pix/nkcos.png" alt="am nchoosekcos">
<!-- infinite sums -->
<div class="innerheader">infinite sums</div>
<pre class="indented">
<em class=def id="make-rcos">make-rcos</em> (frequency 0.0) (r 0.5) ; -1.0 < r < 1.0
<em class=def id="rcos">rcos</em> gen (fm 0.0)
<em class=def id="rcos?">rcos?</em> gen
<em class=def id="make-rssb">make-rssb</em> (frequency 0.0) (ratio 1.0) (r 0.5) ; -1.0 < r < 1.0
<em class=def id="rssb">rssb</em> gen (fm 0.0)
<em class=def id="rssbinterp">rssb-interp</em> gen fm interp
<em class=def id="rssb?">rssb?</em> gen
<em class=def id="make-rxycos">make-rxycos</em> (frequency 0.0) (ratio 1.0) (r 0.5) ; -1.0 < r < 1.0
<em class=def id="rxycos">rxycos</em> gen (fm 0.0)
<em class=def id="rxycos?">rxycos?</em> gen
<em class=def id="make-rxysin">make-rxysin</em> (frequency 0.0) (ratio 1.0) (r 0.5) ; -1.0 < r < 1.0
<em class=def id="rxysin">rxysin</em> gen (fm 0.0)
<em class=def id="rxysin?">rxysin?</em> gen
</pre>
<p>These generators produce an infinite sum of sinusoids, each successive component scaled by "r" (so the nth component has amplitude r^n).
The bump instrument uses rssb-interp: <code>bump beg dur freq amp f0 f1 f2</code>:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:play #t)
(do ((k 0 (+ k 1)))
((= k 10))
(bump (* 0.4 k) 1 (* 16.3 (expt 2.0 (+ 3 (/ k 12)))) .5 520 1190 2390))
(do ((k 0 (+ k 1)))
((= k 10))
(let* ((freq (* 16.3 (expt 2.0 (+ 3 (/ k 12)))))
(scl (sqrt (/ freq 120))))
(bump (+ 4 (* 0.4 k)) 1 freq .5 (* scl 520) (* scl 1190) (* scl 2390)))))
</pre>
<p>As with all the "infinite sums" generators, aliasing is a major concern. We can use the following
relatively conservative function to find the highest safe "r" given the current fundamental and sampling rate:
</p>
<pre class="indented">
(define (safe-r-max freq srate) ; the safe-rxycos generator in generators.scm has this built-in
(expt .001 (/ 1.0 (floor (/ srate 3 freq)))))
</pre>
<p>
If you go over that value, be prepared for some very unintuitive behavior! For example,
at an srate of 44100:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:channels 2)
(let ((gen1 (<em class=red>make-rcos</em> 1050 0.99))
;; r=.6 or so is the safe max
(gen2 (<em class=red>make-rcos</em> 1048 0.99)))
(do ((i 0 (+ i 1)))
((= i 88200))
(<a class=quiet href="#outa">outa</a> i (<em class=red>rcos</em> gen1))
(<a class=quiet href="#outa">outb</a> i (<em class=red>rcos</em> gen2)))))
</pre>
<p>In the first case, all the aliased harmonics line up perfectly with the
unaliased ones because 21*1050 is 22050, but in the second case,
we get (for example) the strong 84 Hz component because the 42nd harmonic
which falls at 44100 - 42*1048 = 84 still has an amplitude of 0.99^42 = .66!
</p>
<img class="indented" src="pix/rcos.png" alt="rcos aliased">
<p>Another artifact of aliasing is that at some frequencies,
for example at 100 Hz, and a sampling rate of 44100, if r is -0.99 and the initial phase is 0.5*pi, or if r is 0.99 and the initial phase is 1.5*pi, the peak amp
is only 0.6639. Finally(?),
there's a sharp discontinuity (a click) as you sweep r through 0.0. As in nrxycos, the waveforms produced by r and -r
are the same, but there's an overall phase difference of pi.
</p>
<p>Other notes: the output of rssb is not normalized, nor is rxysin.
</p>
<!--
(set! (x-axis-label 0 0 0) "rcos freq=1050 r=.99")
(set! (x-axis-label 0 1 0) "rcos freq=1048 r=.99")
(set! *axis-label-font* "9x15")
-->
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-ercos">make-ercos</em> (frequency 0.0) (r 0.5) ; r > 0.0
<em class=def id="ercos">ercos</em> gen (fm 0.0)
<em class=def id="ercos?">ercos?</em> gen
<em class=def id="make-erssb">make-erssb</em> (frequency 0.0) (ratio 1.0) (r 0.5)
<em class=def id="erssb">erssb</em> gen (fm 0.0)
<em class=def id="erssb?">erssb?</em> gen
</pre>
<p>These produce a sum of sinusoids, each scaled by e^(-kr), a special case of rcos. Our safe (minimum) "r" here becomes <code>(/ (log 0.001) (floor (/ srate (* -3 freq))))</code>.
The ercoser instrument uses ercos:
<code>ercoser beg dur freq amp r</code>:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:play #t)
(ercoser 0 1 100 .5 0.1))
</pre>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-eoddcos">make-eoddcos</em> (frequency 0.0) (r 0.5)
<em class=def id="eoddcos">eoddcos</em> gen (fm 0.0)
<em class=def id="eoddcos?">eoddcos?</em> gen
</pre>
<p>This produces a sum of odd harmonics, each scaled by e^r(2k-1)/(2k-1). As "r" approches 0.0, this approaches a square wave.
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:play #t)
(let ((gen1 (<em class=red>make-eoddcos</em> 400.0 :r 0.0))
(gen2 (<a class=quiet href="#make-oscil">make-oscil</a> 400.0))
(a-env (<a class=quiet href="#make-env">make-env</a> '(0 0 1 1) :length 10000)))
(do ((i 0 (+ i 1)))
((= i 10000))
(set! (gen1 'r) (<a class=quiet href="#env">env</a> a-env))
(<a class=quiet href="#outa">outa</a> i (* .5 (<em class=red>eoddcos</em> gen1 (* .1 (<a class=quiet href="#oscil">oscil</a> gen2))))))))
</pre>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-rkcos">make-rkcos</em> (frequency 0.0) (r 0.5) ; -1.0 < r < 1.0
<em class=def id="rkcos">rkcos</em> gen (fm 0.0)
<em class=def id="rkcos?">rkcos?</em> gen
<em class=def id="make-rksin">make-rksin</em> (frequency 0.0) (r 0.5) ; -1.0 < r < 1.0
<em class=def id="rksin">rksin</em> gen (fm 0.0)
<em class=def id="rksin?">rksin?</em> gen
<em class=def id="make-rkssb">make-rkssb</em> (frequency 0.0) (ratio 1.0) (r 0.5) ; -1.0 < r < 1.0
<em class=def id="rkssb">rkssb</em> gen (fm 0.0)
<em class=def id="rkssb?">rkssb?</em> gen
</pre>
<!--
(with-sound ("r7.snd")
(let ((gen (make-rksin 100.0 :r .7)))
(do ((i 0 (+ i 1)))
((= i 44100))
(outa i (* 0.5 (rksin gen))))))
-->
<p>These produce a sum of sinusoids scaled by (r^k)/k. As r approaches 1.0 or -1.0, rksin approaches a sawtooth.
</p>
<img src="pix/rksin3.png" alt="sawtooths">
<p>As with rcos, we
can calculate the safe maximum r, given the current srate and frequency (this function is perhaps too cautious...):
</p>
<pre class="indented">
(define (safe-rk-max freq srate)
(let ((topk (floor (/ srate (* 3 freq)))))
(min 0.999999 (expt (* .001 topk) (/ 1.0 topk)))))
</pre>
<p>Similar to rkcos is (expt (asin (sqrt (oscil x))) 2).
rksin and rkcos provide a nice demonstration of how insensitive the ear is to phase. These two waveforms look different, but
have the same timbre. The sawtooth sounds louder to me, despite having the same peak amplitude.
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:channels 2)
(let ((gen1 (<em class=red>make-rkcos</em> 200.0 :r 0.9))
(gen2 (<em class=red>make-rksin</em> 200.0 :r 0.9)))
(do ((i 0 (+ i 1)))
((= i 100000))
(<a class=quiet href="#outa">outa</a> i (* .95 (<em class=red>rkcos</em> gen1)))
(<a class=quiet href="#outa">outb</a> i (* .95 (<em class=red>rksin</em> gen2))))))
> (channel-rms 0 0) ; from dsp.scm
0.305301097090353
> (channel-rms 0 1)
0.627769794744852
</pre>
<img class="indented" src="pix/rksin.png" alt="sin vs cos">
<p>We might conclude that the RMS value gives the perceived amplitude, but
in the next case, the RMS values are the same, and the peak amplitudes
differ by a factor of 3. I think the one with the higher peak amplitude sounds louder.
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:channels 2)
(let ((gen1 (<a class=quiet href="#make-adjustable-square-wave">make-adjustable-square-wave</a> 400
:duty-factor .75 :amplitude .25))
(gen2 (<a class=quiet href="#make-adjustable-square-wave">make-adjustable-square-wave</a> 400
:duty-factor .11 :amplitude .75))
(flt1 (<a class=quiet href="#make-moving-average">make-moving-average</a> 10))
(flt2 (<a class=quiet href="#make-moving-average">make-moving-average</a> 10)))
(do ((i 0 (+ i 1)))
((= i 50000))
(<a class=quiet href="#outa">outa</a> i (<a class=quiet href="#moving-average">moving-average</a> flt1
(<a class=quiet href="#adjustable-square-wave">adjustable-square-wave</a> gen1)))
(<a class=quiet href="#outa">outb</a> i (<a class=quiet href="#moving-average">moving-average</a> flt2
(<a class=quiet href="#adjustable-square-wave">adjustable-square-wave</a> gen2))))))
</pre>
<img class="indented" src="pix/rmspk.png" alt="rms vs peak">
<p>Since clipping is a disaster, we focus on peak amplitudes in the generators.
</p>
<!--
(set! *selected-graph-color* (make-color 1 1 1))
(set! *selected-data-color* (make-color 0 0 0))
(set! (x-axis-label 0 0) "rkcos, r=.9")
(set! (x-axis-label 0 1) "rksin, r=.9")
(set! *axis-label-font* "9x15")
(set! (x-axis-label 0 0) "rms: .21, peak amp: .25")
(set! (x-axis-label 0 1) "rms: .21, peak amp: .75")
-->
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-rk!cos">make-rk!cos</em> (frequency 0.0) (r 0.5) ; rk!cos is a special case of rxyk!cos
<em class=def id="rk!cos">rk!cos</em> gen (fm 0.0)
<em class=def id="rk!cos?">rk!cos?</em> gen
<em class=def id="make-rk!ssb">make-rk!ssb</em> (frequency 0.0) (ratio 1.0) (r 0.5)
<em class=def id="rk!ssb">rk!ssb</em> gen (fm 0.0)
<em class=def id="rk!ssb?">rk!ssb?</em> gen
<em class=def id="make-rxyk!cos">make-rxyk!cos</em> (frequency 0.0) (ratio 1.0) (r 0.5)
<em class=def id="rxyk!cos">rxyk!cos</em> gen (fm 0.0)
<em class=def id="rxyk!cos?">rxyk!cos?</em> gen
<em class=def id="make-rxyk!sin">make-rxyk!sin</em> (frequency 0.0) (ratio 1.0) (r 0.5)
<em class=def id="rxyk!sin">rxyk!sin</em> gen (fm 0.0)
<em class=def id="rxyk!sin?">rxyk!sin?</em> gen
</pre>
<!-- LATEX: for rk!cos
&\sum_{k=1}^{\infty}\frac{p^{k}\sin kx}{k!} = e^{p\cos x}\sin(p\sin x) \\
&\sum_{k=0}^{\infty}\frac{p^{k}\cos kx}{k!} = e^{p\cos x}\cos(p\sin x) \\
-->
<p>These produce a sum of sinusoids scaled by (r^k)/k!.
The k! denominator dominates eventually, so r * ratio * frequency is approximately the spectral center
(the ratio between successive harmonic amplitudes is (r^(k+1)/(k+1)!)/(r^k/k!) = r/(k+1), which
becomes less than 1.0 at k=r).
For example, in the graph on the right, the frequency is 100 and r is 30, so the center of the spectrum is around 3kHz.
Negative "r" gives the same spectrum as positive, but the waveform's initial-phase is shifted by pi.
The (very) safe maximum "r" is:
</p>
<pre class="indented">
(define (safe-rk!-max freq srate)
(let ((topk (floor (/ srate 3 freq))))
(expt (* .001 (factorial topk)) (/ 1.0 topk))))
;; factorial is in numerics.scm
</pre>
<img src="pix/rkbang.png" alt="rk!cos spectrum">
<p>As in other such cases, varying "r" gives changing spectra. You can sweep r through 0 smoothly except in rk!cos where you'll get a click.
Coupled with the fm argument, these generators provide an extension of multi-carrier FM, similar in effect to the "leap-frog" FM voice.
Here is a use of rk!cos to make a bird twitter:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:play #t :scaled-to .5)
(do ((k 0 (+ k 1)))
((= k 6))
(let ((gen (<em class=red>make-rk!cos</em> 3000.0 :r 0.6))
(ampf (<a class=quiet href="#make-env">make-env</a> '(0 0 1 1 2 1 3 0) :length 3000))
(frqf (<a class=quiet href="#make-env">make-env</a> '(0 0 1 1) :base .1 :scaler (<a class=quiet href="#hztoradians">hz->radians</a> 2000) :length 3000)))
(do ((i 0 (+ i 1)))
((= i 3000))
(<a class=quiet href="#outa">outa</a> (+ i (* k 4000))
(* (<a class=quiet href="#env">env</a> ampf)
(<em class=red>rk!cos</em> gen (<a class=quiet href="#env">env</a> frqf))))))))
</pre>
<p>The instrument bouncy uses rk!ssb: <code>bouncy beg dur freq amp (bounce-freq 5) (bounce-amp 20)</code>
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:play #t)
(bouncy 0 2 200 .5 3 2))
</pre>
<p>brassy (also in generators.scm) uses rxyk!cos, but it is more of an experiment with envelopes than spectra.
It takes a gliss envelope and turns it into a series of quick jumps between harmonics, handling both the
pitch and the index ("r") of the rxyk!cos generator. The effect is vaguely brass-like.
</p>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-r2k!cos">make-r2k!cos</em> (frequency 0.0) (r 0.5) (k 0.0)
<em class=def id="r2k!cos">r2k!cos</em> gen (fm 0.0)
<em class=def id="r2k!cos?">r2k!cos?</em> gen
</pre>
<p>
This generator produces a sum of cosines with a complicated-looking formula for
the component amplitudes. It's actually pretty simple, as this graph shows.
The "F" notation stands for a hypergeometric series, a generalization
of sinusoids and Bessel functions.
</p>
<!-- LATEX: sceq30:
&(1 - 2r \cos \theta + r^{2})^{-k} = \frac{1}{2} {}_{2}F_{1}(k, k; 1; r^{2}) + \sum_{n=1}^{\infty} \frac{(k)_{n}}{n!} r^{n} {}_{2}F_{1}(k, k+n; n+1; r^{2}) \cos n\theta
-->
<img class="indented" src="pix/sceq30.png" alt="sum of sines">
<br>
<img class="indented" src="pix/r2kfactcos.png" alt="r2k!cos spectra">
<!-- r2kfactcos.png:
(with-sound (:clipped #f :statistics #t :play #t :scaled-to .5)
(let* ((gen (make-r2k!cos 440.0 :r 0.65 :k 3.0))
(dur 2.0)
(samps (seconds->samples dur))
(indf (make-env '(0 0 1 1) :duration dur :scaler 10.0 :offset 1))
(ampf (make-env '(0 0 1 1 20 1 21 0) :duration dur))
)
(do ((i 0 (+ i 1)))
((= i samps))
(set! (r2k!cos-k gen) (env indf))
(outa i (* (env ampf)
(r2k!cos gen))))))
dark 3 rainbow .001 not inverted
2048 blackman2
x 311 1.67
y 281 0.94
z 350 1.30
hop 3
(spectrum-end 0 0) 0.544793281259146
-->
<p>Negative "r" gives the same output as the corresponding positive "r", and
there is sometimes a lot of DC. Despite appearances, as r increases beyond 1.0,
the spectrum collapses back towards the fundamental. (I think that r and 1/r produce the same spectrum).
Aliasing can be a problem,
especially when r is close to 1.
The instruments pianoy and pianoy1 use r2k!cos: <code>pianoy beg dur freq amp</code>, and
<code>pianoy1 beg dur freq amp (bounce-freq 5) (bounce-amp 20)</code>:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:play #t)
(pianoy 0 3 100 .5))
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:play #t)
(pianoy1 0 4 200 .5 1 .1))
</pre>
<p>pianoy2 combines r2k!cos with fmssb to try to get closer to the hammer sound:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:play #t)
(pianoy2 0 1 100 .5))
</pre>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-rkoddssb">make-rkoddssb</em> (frequency 0.0) (ratio 1.0) (r 0.5) ; -1.0 < r < 1.0
<em class=def id="rkoddssb">rkoddssb</em> gen (fm 0.0)
<em class=def id="rkoddssb?">rkoddssb?</em> gen
</pre>
<p>This produces a sum of odd-numbered harmonics scaled by (r^(2k-1))/(2k-1). This kind of spectrum is usually
called "clarinet-like".
Negative r gives the
same output as positive. The (not very)
safe maximum r is:
</p>
<pre class="indented">
(define (safe-rkodd-max-r freq srate)
(let ((k2-1 (- (* 2 (floor (/ srate 3 freq))) 1)))
(expt (* .001 k2-1) (/ 1.0 k2-1))))
</pre>
<p>The instrument stringy uses rkoddssb and rcos: <code>stringy beg dur freq amp</code>:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:play #t)
(do ((i 0 (+ i 1)))
((= i 10))
(stringy (* i .3) .3 (+ 200 (* 100 i)) .5)))
</pre>
<p>glassy also uses rkoddssb: <code>glassy beg dur freq amp</code>:
</p>
<pre class="indented">
(<a class=quiet href="sndscm.html#wsdoc">with-sound</a> (:play #t)
(do ((i 0 (+ i 1)))
((= i 10))
(glassy (* i .3) .1 (+ 400 (* 100 i)) .5)))
</pre>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-k2sin">make-k2sin</em> (frequency 0.0)
<em class=def id="k2sin">k2sin</em> gen (fm 0.0)
<em class=def id="k2sin?">k2sin?</em> gen
<em class=def id="make-k2cos">make-k2cos</em> (frequency 0.0)
<em class=def id="k2cos">k2cos</em> gen (fm 0.0)
<em class=def id="k2cos?">k2cos?</em> gen
<em class=def id="make-k2ssb">make-k2ssb</em> (frequency 0.0) (ratio 1.0)
<em class=def id="k2ssb">k2ssb</em> gen (fm 0.0)
<em class=def id="k2ssb?">k2ssb?</em> gen
</pre>
<p>These produce a sum of sinusoids scaled by 1/(2^k).
</p>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-k3sin">make-k3sin</em> (frequency 0.0)
<em class=def id="k3sin">k3sin</em> gen fm
<em class=def id="k3sin?">k3sin?</em> gen
</pre>
<p>This produces a sum of sines scaled by 1.0/(k^3).
</p>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-krksin">make-krksin</em> (frequency 0.0) (r 0.5)
<em class=def id="krksin">krksin</em> gen (fm 0.0)
<em class=def id="krksin?">krksin?</em> gen
</pre>
<img class="indented" src="pix/sceq25.png" alt="sum of sines">
<div class="separator"></div>
<p>This produces a sum of sinusoids scaled by kr^k. Its output is not normalized. I think the formula
given assumes that r is less than 1.0, and in that case, the safe maximum r is given by:
</p>
<pre class="indented">
(define (safe-krk-max-r freq srate)
(let ((topk (floor (/ srate 3 freq))))
(expt (/ .001 topk) (/ 1.0 topk))))
</pre>
<p>However, r can be greater than 1.0 without causing any trouble, and behaves in that case much like r2k!cos — as it increases, the spectrum collapses;
I think r in that case is equivalent to 1/r.
The only value to avoid is 1.0.
</p>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-abcos">make-abcos</em> (frequency 0.0) (a 0.5) (b 0.25)
<em class=def id="abcos">abcos</em> gen (fm 0.0)
<em class=def id="abcos?">abcos?</em> gen
<em class=def id="make-absin">make-absin</em> (frequency 0.0) (a 0.5) (b 0.25)
<em class=def id="absin">absin</em> gen (fm 0.0)
<em class=def id="absin?">absin?</em> gen
</pre>
<p>These produce a sum of sinusoids scaled as follows:
</p>
<img class="indented" src="pix/sceq27.png" alt="sum of sines">
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-r2k2cos">make-r2k2cos</em> (frequency 0.0) (r 0.5)
<em class=def id="r2k2cos">r2k2cos</em> gen (fm 0.0)
<em class=def id="r2k2cos?">r2k2cos?</em> gen
</pre>
<p>This produces a sum of cosines, each scaled by 1/(r^2+k^2). r shouldn't be 0, but otherwise it almost doesn't matter what it is —
this is not a very flexible generator!
</p>
<p>There are a dozen or so other generators defined in generators.scm, but most are close
variants of those given above.
</p>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-tanhsin">make-tanhsin</em> (frequency 0.0) (r 1.0) (initial-phase 0.0)
<em class=def id="tanhsin">tanhsin</em> gen (fm 0.0)
<em class=def id="tanhsin?">tanhsin?</em> gen
</pre>
<p>This produces tanh(r * sin(x)) which approaches a square wave as "r" increases.
</p>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-moving-fft">make-moving-fft</em> (input #f) (n 512) (hop 128)
<em class=def id="moving-fft">moving-fft</em> gen
<em class=def id="moving-fft?">moving-fft?</em> gen
</pre>
<p>moving-fft provides a sample-by-sample FFT (magnitudes and phases) of its
input (currently assumed to be a readin generator).
mus-xcoeffs returns the magnitudes, mus-ycoeffs returns the phases, and mus-data returns the current input block.
We could mimic the fft display window in the "lisp graph" via:
</p>
<pre class="indented">
(let ((ft (<em class=red>make-moving-fft</em> (make-readin "oboe.snd")))
(data (make-float-vector 256)))
(set! (lisp-graph?) #t)
(do ((i 0 (+ i 1)))
((= i 10000))
(<em class=red>moving-fft</em> ft)
(float-vector-subseq (mus-xcoeffs ft) 0 255 data)
(graph data "fft" 0.0 11025.0 0.0 0.1 0 0 #t)))
</pre>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-moving-spectrum">make-moving-spectrum</em> (input #f) (n 512) (hop 128)
<em class=def id="moving-spectrum">moving-spectrum</em> gen
<em class=def id="moving-spectrum?">moving-spectrum?</em> gen
</pre>
<p>moving-spectrum provides a sample-by-sample spectrum (amplitudes, frequencies, and current phases) of its
input (currently assumed to be a readin generator). It is identical to the first (analysis) portion of
the phase-vocoder generator (see test-sv in generators.scm for details). To access the current amps and so on,
use (gen 'amps), (gen 'phases), and (gen 'freqs).
</p>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-moving-autocorrelation">make-moving-autocorrelation</em> (input #f) (n 512) (hop 128)
<em class=def id="moving-autocorrelation">moving-autocorrelation</em> gen
<em class=def id="moving-autocorrelation?">moving-autocorrelation?</em> gen
</pre>
<p>moving-autocorrelation provides the autocorrelation of the last 'n' samples every 'hop' samples.
The samples come from 'input' (currently assumed to be a readin generator). The output is accessible
via mus-data.
</p>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-moving-pitch">make-moving-pitch</em> (input #f) (n 512) (hop 128)
<em class=def id="moving-pitch">moving-pitch</em> gen
<em class=def id="moving-pitch?">moving-pitch?</em> gen
</pre>
<p>moving-pitch provides the current pitch of its input, recalculated (via moving-autocorrelation) every 'hop' samples.
</p>
<pre class="indented">
(let ((rd (make-readin "oboe.snd"))
(cur-srate (srate "oboe.snd")))
(let-temporarily ((*clm-srate* cur-srate))
(let ((scn (<em class=red>make-moving-pitch</em> rd))
(last-pitch 0.0)
(pitch 0.0))
(do ((i 0 (+ i 1)))
((= i 22050))
(set! last-pitch pitch)
(set! pitch (<em class=red>moving-pitch</em> scn))
(if (not (= last-pitch pitch))
(format () "~A: ~A~%" (* 1.0 (/ i cur-srate)) pitch))))))
</pre>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-moving-scentroid">make-moving-scentroid</em> (dbfloor -40.0) (rfreq 100.0) (size 4096)
<em class=def id="moving-scentroid">moving-scentroid</em> gen
<em class=def id="moving-scentroid?">moving-scentroid?</em> gen
</pre>
<p>moving-scentroid provides a generator that mimics Bret Battey's scentroid instrument (in dsp.scm or scentroid.ins).
</p>
<div class="separator"></div>
<pre class="indented">
<em class=def id="make-flocsig">make-flocsig</em> (reverb-amount 0.0) (frequency 1.0) (amplitude 2.0) offset
<em class=def id="flocsig">flocsig</em> gen i val
<em class=def id="flocsig?">flocsig?</em> gen
</pre>
<p>flocsig is a version of locsig that adds changing delays between the channels (flanging).
The delay amount is set by a rand-interp centered around 'offset', moving as many as 'amplitude'
samples (this also affects signal placement), and moving at a speed set by 'frequency'.
Currently flocsig assumes stereo output and stereo reverb output.
This generator is trying to open up the space in the same manner that flanging does, but
hopefully unobtrusively. Here is an example, including a stereo reverb:
</p>
<pre class="indented">
(definstrument (jcrev2)
(let* ((allpass11 (make-all-pass -0.700 0.700 1051))
(allpass21 (make-all-pass -0.700 0.700 337))
(allpass31 (make-all-pass -0.700 0.700 113))
(comb11 (make-comb 0.742 4799))
(comb21 (make-comb 0.733 4999))
(comb31 (make-comb 0.715 5399))
(comb41 (make-comb 0.697 5801))
(outdel11 (make-delay (seconds->samples .01)))
(allpass12 (make-all-pass -0.700 0.700 1051))
(allpass22 (make-all-pass -0.700 0.700 337))
(allpass32 (make-all-pass -0.700 0.700 113))
(comb12 (make-comb 0.742 4799))
(comb22 (make-comb 0.733 4999))
(comb32 (make-comb 0.715 5399))
(comb42 (make-comb 0.697 5801))
(outdel12 (make-delay (seconds->samples .01)))
(len (floor (+ *clm-srate* (framples *reverb*)))))
(do ((i 0 (+ i 1)))
((= i len))
(let* ((allpass-sum (all-pass allpass31
(all-pass allpass21
(all-pass allpass11
(ina i *reverb*)))))
(comb-sum (+ (comb comb11 allpass-sum)
(comb comb21 allpass-sum)
(comb comb31 allpass-sum)
(comb comb41 allpass-sum))))
(outa i (delay outdel11 comb-sum)))
(let* ((allpass-sum (all-pass allpass32
(all-pass allpass22
(all-pass allpass12
(inb i *reverb*)))))
(comb-sum (+ (comb comb12 allpass-sum)
(comb comb22 allpass-sum)
(comb comb32 allpass-sum)
(comb comb42 allpass-sum))))
(outb i (delay outdel12 comb-sum))))))
(definstrument (simp beg dur (amp 0.5) (freq 440.0) (ramp 2.0) (rfreq 1.0) offset)
(let* ((os (make-pulse-train freq))
(floc (<em class=red>make-flocsig</em> :reverb-amount 0.1
:frequency rfreq
:amplitude ramp
:offset offset))
(start (seconds->samples beg))
(end (+ start (seconds->samples dur))))
(do ((i start (+ i 1)))
((= i end))
(<em class=red>flocsig</em> floc i (* amp (pulse-train os))))))
(with-sound (:channels 2 :reverb-channels 2 :reverb jcrev2)
(simp 0 1))
</pre>
<!-- defgenerator -->
<div class="innerheader">defgenerator</div>
<pre class="indented">
<em class=def id="defgenerator">defgenerator</em> name fields
</pre>
<p>defgenerator defines a generator. Its syntax is modelled after Common Lisp's defstruct.
It sets up
a structure, an environment with slots that you can get and set.
It also defines a "make"
function to create an instance of the environment, and a predicate for it.
Here is a way to define oscil using defgenerator:
</p>
<pre class="indented">
(<em class=red>defgenerator</em> osc freq phase)
;;; make-osc creates an osc, and osc? returns #t if passed an osc.
;;; Once we have an osc (an environment with "freq" and "phase" locals)
;;; we can either use with-let, or refer to the local variables
;;; directly via (gen 'freq) and (gen 'phase).
(define (osc gen fm) ; our new generator
(let ((result (sin (gen 'phase))))
(set! (gen 'phase) (+ (gen 'phase) (gen 'freq) fm))
result))
;;; now we can use the osc generator in an instrument:
(<a class=quiet href="sndscm.html#definstrument">definstrument</a> (osc-fm beg dur freq amp mc-ratio fm-index)
(let* ((start (<a class=quiet href="sndclm.html#secondstosamples">seconds->samples</a> beg))
(end (+ start (<a class=quiet href="sndclm.html#secondstosamples">seconds->samples</a> dur)))
(carrier (<em class=red>make-osc</em> (<a class=quiet href="sndclm.html#hztoradians">hz->radians</a> freq)))
(modulator (<em class=red>make-osc</em> (<a class=quiet href="sndclm.html#hztoradians">hz->radians</a> (* mc-ratio freq))))
(index (<a class=quiet href="sndclm.html#hztoradians">hz->radians</a> (* freq mc-ratio fm-index))))
(do ((i start (+ i 1)))
((= i end))
(<a class=quiet href="sndclm.html#outa">outa</a> i (* amp (<em class=red>osc</em> carrier (* index (<em class=red>osc</em> modulator 0.0))))))))
(with-sound () (osc-fm 0 1 440 .1 1 1))
</pre>
<p>
The first argument to defgenerator is the new object's name, and the rest are the fields of that object.
Each field has a name and an optional initial value which defaults to 0.0.
The "make" function (make-osc in our example) uses define* with
the field names and initial values as the optional keys. So make-osc above is declared (by the
defgenerator macro) as:
</p>
<pre class="indented">
(define* (make-osc (freq 0.0) (phase 0.0)) ...)
</pre>
<p>which we can invoke in various ways, e.g.:
</p>
<pre class="indented">
(make-osc 440)
(make-osc :phase (/ pi 2) :freq 440)
(make-osc 440 :phase 0.0)
</pre>
<p>The defgenerator "name" parameter can also be a list; in this case the first element is the actual generator name. The
next elements are <code>:make-wrapper</code> followed by a function of one argument
(the default object normally returned by defgenerator), and <code>:methods</code>, followed
by a list of the methods the generator responds to. The make wrapper function can
make any changes it pleases, then return the fixed-up generator. For example, in our
"osc" generator, we had to remember to change frequency in Hz to radians; we can use the
wrapper to handle that:
</p>
<pre class="indented">
(defgenerator
(osc <em class=red>:make-wrapper</em> (lambda (gen)
(set! (gen 'freq) (<a class=quiet href="sndclm.html#hztoradians">hz->radians</a> (gen 'freq)))
gen))
(freq 0.0) (phase 0.0))
</pre>
<p>and now the make process in the instrument can be simplified to:
</p>
<pre class="indented">
...
(carrier (make-osc freq))
(modulator (make-osc (* mc-ratio freq)))
...
</pre>
<p>If you want the struct to take part in the <a href="sndclm.html#genericfunctions">generic function</a> facility
in CLM, add the desired methods as an association list with the keyword :methods:
</p>
<pre class="indented">
(defgenerator (osc :make-wrapper
(lambda (gen)
(set! (gen 'freq) (<a class=quiet href="sndclm.html#hztoradians">hz->radians</a> (gen 'freq)))
gen)
<em class=red>:methods</em>
(list
(cons 'mus-frequency
(dilambda
(lambda (g) (<a class=quiet href="sndclm.html#hztoradians">radians->hz</a> (g 'freq)))
(lambda (g val) (set! (g 'freq) (<a class=quiet href="sndclm.html#hztoradians">hz->radians</a> val)))))
(cons 'mus-phase
(dilambda
(lambda (g) (g 'phase))
(lambda (g val) (set! (g 'phase) val))))
(cons 'mus-describe
(lambda (g) (<a class=quiet>format</a> #f "osc freq: ~A, phase: ~A"
(mus-frequency g)
(mus-phase g))))))
freq phase)
</pre>
<p>The make-wrapper might more accurately be called an after-method; it is
evaluated at the end of the automatically-created make function. All the
fields have been set at that point either by arguments to the make function,
or from the default values given in the defgenerator declaration. The make
function returns whatever
the make-wrapper function returns, so you almost always want to return the "gen" argument.
There are many examples in generators.scm.
</p>
<!-- FUNCTIONS -->
<div class="header" id="otherfunctions">Other functions</div>
<p>There are several functions closely tied to the generators and instruments.
</p>
<table class="method">
<tr><td><em class=def id="hztoradians">hz->radians</em><code> freq</code></td><td>convert freq to radians per sample (using *clm-srate*): (freq * 2 * pi) / srate</td></tr>
<tr><td><em class=def id="radianstohz">radians->hz</em><code> rads</code></td><td>convert rads to Hz (using *clm-srate*): (rads * srate) / (2 * pi)</td></tr>
<tr><td><em class=def id="dbtolinear">db->linear</em><code> dB</code></td><td>convert dB to linear value: 10^(dB/20)</td></tr>
<tr><td><em class=def id="lineartodb">linear->db</em><code> val</code></td><td>convert val to dB: 20 * log(x) / log(10)</td></tr>
<tr><td><em class=def id="timestosamples">times->samples</em><code> start duration</code></td><td>convert start and duration from seconds to samples (beg+dur in latter case)</td></tr>
<tr><td><em class=def id="samplestoseconds">samples->seconds</em><code> samps</code></td><td>convert samples to seconds (using *clm-srate*): samps / srate</td></tr>
<tr><td><em class=def id="secondstosamples">seconds->samples</em><code> secs</code></td><td>convert seconds to samples (using *clm-srate*): secs * srate</td></tr>
<tr><td><em class=def id="degreestoradians">degrees->radians</em><code> degs</code></td><td>convert degrees to radians: (degs * 2 * pi) / 360</td></tr>
<tr><td><em class=def id="radianstodegrees">radians->degrees</em><code> rads</code></td><td>convert radians to degrees: (rads * 360) / (2 * pi)</td></tr>
<tr><td><em class=def id="mussrate">mus-srate</em></td><td>sampling rate in with-sound (better known as *clm-srate*)</td></tr>
<tr><td><em class=def id="oddweight">odd-weight</em><code> x</code></td><td>return a number between 0.0 (x is even) and 1.0 (x is odd)</td></tr>
<tr><td><em class=def id="evenweight">even-weight</em><code> x</code></td><td>return a number between 0.0 (x is odd) and 1.0 (x is even)</td></tr>
<tr><td><em class=def id="oddmultiple">odd-multiple</em><code> x y</code></td><td>return y times the nearest odd integer to x</td></tr>
<tr><td><em class=def id="evenmultiple">even-multiple</em><code> x y</code></td><td>return y times the nearest even integer to x</td></tr>
</table>
<p>
hz->radians
converts its argument to radians/sample (for any situation where a
frequency is used as an amplitude — glissando or FM).
</p>
<blockquote>
<p>
<code>freq-in-hz * 2 * pi</code> gives us the number of radians traversed per
second; we then divide by the number of samples per second to get the
radians per sample; in dimensional terms: (radians/sec) /
(sample/sec) = radians/sample. We need this conversion whenever a
frequency-related value is being accessed on every sample, as
an increment of a phase variable.
</p></blockquote>
<pre class="indented">
> *clm-srate*
44100.0
> (hz->radians 440.0)
0.0626893772144902
> (/ (* 440.0 2 pi) 44100.0)
0.0626893772144902
> (linear->db .1)
-20.0
> (times->samples 1.0 2.0)
(44100 132300)
> (seconds->samples 2.0)
88200
> (samples->seconds 44100)
1.0
> (degrees->radians 45)
0.785398163397448
> (radians->degrees (/ pi 4))
45.0
</pre>
<div class="separator"></div>
<pre class="indented">
<em class=def id="musfloatequalfudgefactor">mus-float-equal-fudge-factor</em> (also known as *mus-float-equal-fudge-factor*)
</pre>
<p>This function sets how far apart generator float-vector elements can be and still be considered equal in equal?
</p>
<pre class="indented">
> *mus-float-equal-fudge-factor*
1.0e-7
> (define v1 (float-vector .1 .1 .101))
#<unspecified>
> (define v2 (float-vector .1 .1 .1))
#<unspecified>
> (equal? v1 v2)
#f
> (set! *mus-float-equal-fudge-factor* .01)
1.0e-7 ; set! returns the previous value
> (equal? v1 v2)
#t
</pre>
<div class="separator"></div>
<pre class="indented">
<em class=def id="musarrayprintlength">mus-array-print-length</em> (also known as *mus-array-print-length*)
</pre>
<p>
This function determines how many float-vector elements are printed by mus-describe.
</p>
<!-- POLYNOMIAL -->
<div class="innerheader">polynomial</div>
<pre class="indented">
<em class=def id="polynomial">polynomial</em> coeffs x
</pre>
<p>The polynomial function evaluates a polynomial, defined by giving its coefficients,
at the point "x".
"coeffs" is a vector of coefficients where
coeffs[0] is the constant term, and so on.
</p>
<pre class="indented">
> (polynomial (float-vector 0.0 1.0) 2.0) ; x
2.0
> (polynomial (float-vector 1.0 2.0 3.0) 2.0) ; 3x*x + 2x + 1
17.0
</pre>
<p>
<a href="sndscm.html#polydoc">poly.scm</a> has a variety of polynomial-related functions.
Abramowitz and Stegun, "A Handbook of Mathematical Functions" is a
treasure-trove of interesting polynomials.
</p>
<!-- ARRAY-INTERP -->
<div class="innerheader">array-interp, dot-product</div>
<pre class="indented">
<em class=def id="array-interp">array-interp</em> fn x size
<em class=def id="dot-product">dot-product</em> in1 in2
<em class=def id="edot-product">edot-product</em> freq data
<em class=def id="mus-interpolate">mus-interpolate</em> type x v size y1
</pre>
<p>array-interp interpolates in the array "fn" at the point "x". It underlies the <a href="#table-lookup">table-lookup</a>
generator, among others. Here's array-interp as a "compander":
</p>
<pre class="indented">
(define compand-table (float-vector -1.0 -0.96 -0.90 -0.82 -0.72 -0.60 -0.45 -0.25
0.0 0.25 0.45 0.60 0.72 0.82 0.90 0.96 1.0))
(<a class=quiet href="extsnd.html#mapchannel">map-channel</a>
(lambda (inval)
(let ((index (+ 8.0 (* 8.0 inval))))
(<em class=red>array-interp</em> compand-table index 17))))
</pre>
<p><a href="sndscm.html#soundinterp">sound-interp</a> in examp.scm fills an array with an entire sound,
then uses array-interp to read it.
</p>
<p>
dot-product is the usual "inner product" or "scalar product" (a name that should be banned from polite society).
We could define our own FIR filter using dot-product:
</p>
<pre class="indented">
(define (make-fr-filter coeffs)
(list coeffs (make-float-vector (length coeffs))))
(define (fr-filter flt x)
(let* ((coeffs (car flt))
(xs (cadr flt))
(xlen (length xs)))
(float-vector-move! xs (- xlen 1) (- xlen 2) #t)
(set! (xs 0) x)
(<em class=red>dot-product</em> coeffs xs xlen)))
</pre>
<p>
edot-product returns the complex dot-product of the "data" argument (a vector) with <code>(exp (* freq i))</code>.
Here, "i" goes from 0 to data's size - 1.
"freq" and the elements of "data" can be complex, as can the return value. See <a href="sndscm.html#stretchsoundviadft">stretch-sound-via-dft</a>
for an example.
</p>
<p>
mus-interpolate is the function used whenever table lookup interpolation is requested, as in
<a href="#delay">delay</a> or <a href="#wave-train">wave-train</a>.
The "type" argument is one of the interpolation types (<code>mus-interp-linear</code>, for example).
</p>
<!-- CONTRAST-ENHANCEMENT -->
<div class="innerheader">contrast-enhancement</div>
<pre class="indented">
<em class=def id="contrast-enhancement">contrast-enhancement</em> in-samp (fm-index 1.0)
</pre>
<p>contrast-enhancement passes its input to sin as a kind of phase modulation.
</p>
<pre class="indented">
(sin (+ (* input pi 0.5)
(* index (sin (* input pi 2)))))
</pre>
<p>
This brightens the
input, helping it cut through a huge mix.
A similar (slightly simpler) effect is:
</p>
<pre class="indented">
(let ((mx (maxamp)))
(<a class=quiet href="extsnd.html#mapchannel">map-channel</a>
(lambda (y)
(* mx (sin (/ (* pi y) mx))))))
</pre>
<p>This modulates the sound but keeps the output maxamp the same as the input.
See <a href="#moving-max">moving-max</a> for a similar function that does this kind of scaling throughout the sound,
resulting in a steady modulation, rather than an intensification of just the peaks.
And a sort of converse is <a href="sndscm.html#soundinterp">sound-interp</a>.
</p>
<!-- AMPLITUDE-MODULATE -->
<div class="innerheader">ring-modulate, amplitude-modulate</div>
<pre class="indented">
<em class=def id="ring-modulate">ring-modulate</em> in1 in2 ; returns <code>(* in1 in2)</code>
<em class=def id="amplitude-modulate">amplitude-modulate</em> am-carrier in1 in2 ; returns <code>(* in1 (+ am-carrier in2))</code>
</pre>
<table>
<tr>
<td>
<div class="scheme">
<pre class="indented">
(with-sound (:play #t)
(let ((osc1 (make-oscil 440.0))
(osc2 (make-oscil 220.0)))
(do ((i 0 (+ i 1)))
((= i 44100))
(outa i (* 0.5 (amplitude-modulate 0.3 (oscil osc1) (oscil osc2)))))))
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="ruby">
<pre class="indented">
with_sound(:play, true) do
osc1 = make_oscil(440.0);
osc2 = make_oscil(220.0);
44100.times do |i|
outa(i, 0.5 * amplitude_modulate(0.3, oscil(osc1), oscil(osc2)), $output);
end
end.output
</pre>
</div>
</td>
</tr><tr>
<td>
<div class="forth">
<pre class="indented">
lambda: ( -- )
440.0 make-oscil { osc1 }
220.0 make-oscil { osc2 }
44100 0 do
i
0.3 ( car )
osc1 0 0 oscil ( in1 )
osc2 0 0 oscil ( in2 ) amplitude-modulate f2/ *output* outa drop
loop
; :play #t with-sound drop
</pre>
</div>
</td>
</tr>
</table>
<p>ring-modulation is sometimes called "double-sideband-suppressed-carrier" modulation —
that is, amplitude modulation with the carrier omitted (set to 0.0 above).
The nomenclature here is a bit confusing — I can't remember now why I used
these names; think of "carrier" as "carrier amplitude" and "in1" as "carrier". Normal amplitude modulation using this function is:
</p>
<pre class="indented">
(define carrier (<a class=quiet href="#make-oscil">make-oscil</a> carrier-freq (* .5 pi)))
...
(amplitude-modulate 1.0 (<a class=quiet href="#oscil">oscil</a> carrier) signal)
</pre>
<p>Both of these functions take advantage of the "Modulation Theorem"; since
multiplying a signal by e^(iwt) translates its spectrum by w /
two pi Hz, multiplying by a sinusoid splits its spectrum into two equal parts
translated up and down by w/(two pi) Hz:
</p>
<img class="indented" src="pix/fmeq8.png" alt="coscos and sinsin">
<p>Waveshaping (via the Chebyshev polynomials) is an elaboration of AM. For example, cos^2x is amplitude modulation of cos x
with itself, splitting into cos2x and cos0x. T2 (that is, 2cos^2x - 1) then subtracts the cos0x term to return cos2x.
</p>
<p>
The upper sidebands may foldover (alias); if it's a problem, low-pass filter the inputs (surely no CLM user needs that silly reminder!).
</p>
<!-- FFT -->
<div class="innerheader">FFT (fourier transform)</div>
<pre class="indented">
<em class=def id="fft">mus-fft</em> rdat idat fftsize sign
<em class=def id="make-fft-window">make-fft-window</em> type size (beta 0.0) (alpha 0.0)
<em class=def id="rectangulartopolar">rectangular->polar</em> rdat idat
<em class=def id="rectangulartomagnitudes">rectangular->magnitudes</em> rdat idat
<em class=def id="polartorectangular">polar->rectangular</em> rdat idat
<em class=def id="spectrum">spectrum</em> rdat idat window norm-type
<em class=def id="convolution">convolution</em> rdat idat size
<em class=def id="autocorrelate">autocorrelate</em> data
<em class=def id="correlate">correlate</em> data1 data2
</pre>
<p>mus-fft, spectrum, and convolution are the standard functions used everywhere.
fft is the Fourier transform, convolution convolves its arguments, and spectrum
returns '(magnitude (rectangular->polar (fft))). The results are in dB (if "norm-type" is 0),
or linear and normalized to 1.0 ("norm-type" = 1), or linear unnormalized.
The name "mus-fft" is used to distuinguish clm's fft routine from Snd's; the
only difference is that mus-fft includes the fft length as an argument, whereas
<a href="extsnd.html#fft">fft</a> does not. Here we use mus-fft to low-pass filter a sound:
</p>
<pre class="indented">
(let* ((len (mus-sound-framples "oboe.snd"))
(fsize (expt 2 (ceiling (log len 2))))
(rdata (make-float-vector fsize))
(idata (make-float-vector fsize)))
(file->array "oboe.snd" 0 0 len rdata)
(<em class=red>mus-fft</em> rdata idata fsize 1)
(let ((fsize2 (/ fsize 2))
(cutoff (round (/ fsize 10))))
(do ((i cutoff (+ i 1))
(j (- fsize 1) (- j 1)))
((= i fsize2))
(set! (rdata i) 0.0)
(set! (idata i) 0.0)
(set! (rdata j) 0.0)
(set! (idata j) 0.0)))
(<em class=red>mus-fft</em> rdata idata fsize -1)
(array->file "test.snd"
(float-vector-scale! rdata (/ 1.0 fsize))
len
(srate "oboe.snd")
1)
(let ((previous-case (find-sound "test.snd")))
(if (sound? previous-case)
(close-sound previous-case)))
(open-sound "test.snd"))
</pre>
<p>make-fft-window can return many of the standard windows including:
</p>
<pre class="indented">
bartlett-hann-window bartlett-window blackman2-window blackman3-window
blackman4-window bohman-window cauchy-window connes-window
dolph-chebyshev-window exponential-window flat-top-window gaussian-window
hamming-window hann-poisson-window hann-window kaiser-window
parzen-window poisson-window rectangular-window riemann-window
samaraki-window tukey-window ultraspherical-window welch-window
blackman5-window blackman6-window blackman7-window blackman8-window
blackman9-window blackman10-window rv2-window rv3-window
rv4-window mlt-sine-window papoulis-window dpss-window
sinc-window
</pre>
<p>rectangular->polar and polar->rectangular change how we view the FFT data: in polar or rectangular coordinates.
rectangular->magnitudes is the same as rectangular->polar, but only calculates the magnitudes.
autocorrelate performs an (in place) autocorrelation of 'data' (a float-vector). See <a href="#moving-pitch">moving-pitch</a> in generators.scm,
or <a href="sndscm.html#rubberdoc">rubber.scm</a>.
correlate performs an in-place cross-correlation of data1 and data2 (see, for example, <a href="sndscm.html#snddiffdoc">snddiff</a>).
</p>
<table class="method">
<tr><td class="methodtitle">FFTs</td></tr><tr><td>
<blockquote><small>
Hartley transform in Scheme: <a href="sndscm.html#dht">dht</a><br>
Spectral edit dialog: <a href="snd.html#editenvelope">Envelope Editor</a><br>
fft-based filter: <a href="sndscm.html#fftedit">fft-edit</a>, <a href="sndscm.html#fftenvedit">fft-env-edit</a>, <a href="sndscm.html#fftenvinterp">fft-env-interp</a>, <a href="sndscm.html#fftsquelch">fft-squelch</a>, <a href="sndscm.html#fftcancel">fft-cancel</a><br>
phase-vocoder: <a href="#phase-vocoder">phase-vocoder</a>. <a href="sndscm.html#pvocdoc">pvoc</a><br>
transposition via fft: <a href="sndscm.html#downoct">down-oct</a><br>
phase rotation via fft: <a href="sndscm.html#zerophase">zero-phase, rotate-phase</a><br>
duration change via autocorrelation: <a href="sndscm.html#rubberdoc">rubber-sound</a><br>
smoothing via fft: <a href="sndscm.html#fftsmoother">fft-smoother</a><br>
cross-synthesis: <a href="sndscm.html#crosssynthesis">cross-synthesis</a><br>
voiced->unvoiced effect: <a href="sndscm.html#voicedtounvoiced">voiced->unvoiced</a><br>
noise reduction: <a href="sndscm.html#cleanchannel">clean-channel</a>, <a href="sndscm.html#clminsdoc">anoi</a><br>
spectral modeling: <a href="sndscm.html#clminsdoc">pins</a><br>
polynomial approach to spectral multiplies (convolution): <a href="sndscm.html#spectralpolynomial">spectral-polynomial</a><br>
More transforms: <a href="sndscm.html#fractionalfouriertransform">fractional-fourier-transform</a>, <a href="sndscm.html#ztransform">z-transform</a> in dsp.scm<br>
bark, mel, erb scale display: <a href="sndscm.html#displaybarkfft">display-bark-fft</a><br>
apply function to spectrum, inverse fft: <a href="sndscm.html#filterfft">filter-fft</a><br>
</small></blockquote>
</td></tr></table>
<div class="header" id="instruments">Instruments</div>
<p>It's hard to decide what's an "instrument" in this context, but I think I'll treat
it as something that can be called as a note in a notelist (in with-sound) and
generate its own sound.
There are hundreds of instruments scattered around the documentation, and most of the
<a href="extsnd.html#mapchannel">map-channel</a> functions can be recast as instruments.
There are also several that represent "classic" computer music instruments; they
are listed here, documented in sndscm.html, and tested (via sample runs) in
test 23 in snd-test.
</p>
<table class="borderspaced">
<tr>
<th class="beige">instrument</th>
<th class="beige">function</th>
<th class="beige">CL</th>
<th class="beige">Scheme</th>
<th class="beige">Ruby</th>
<th class="beige">Forth</th>
</tr>
<tr><td class="br">complete-add</td>
<td class="br">additive synthesis</td>
<td class="br">add.ins</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr><td class="br">addflts</td>
<td class="br">filters</td>
<td class="br">addflt.ins</td>
<td class="br"><a href="dsp.scm">dsp.scm</a></td>
<td class="br"><a href="dsp.rb">dsp.rb</a></td>
<td></td>
</tr>
<tr><td class="br">add-sound</td>
<td class="br">mix in a sound file</td>
<td class="br">addsnd.ins</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr><td class="br">bullfrog et al</td>
<td class="br">many animals (frogs, insects, birds)</td>
<td class="br"></td>
<td class="br"><a href="animals.scm">animals.scm</a></td>
<td></td>
<td></td>
</tr>
<tr><td class="br">anoi</td>
<td class="br">noise reduction</td>
<td class="br">anoi.ins</td>
<td class="br"><a href="clm-ins.scm">clm-ins.scm</a></td>
<td class="br"><a href="clm-ins.rb">clm-ins.rb</a></td>
<td class="br"><a href="clm-ins.fs">clm-ins.fs</a></td>
</tr>
<tr><td class="br">autoc</td>
<td class="br">pitch estimation (Bret Battey)</td>
<td class="br">autoc.ins</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr><td class="br">badd</td>
<td class="br">fancier additive synthesis (Doug Fulton)</td>
<td class="br">badd.ins</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr><td class="br">bandedwg</td>
<td class="br">Juan Reyes banded waveguide instrument</td>
<td class="br">bandedwg.ins</td>
<td class="br"><a href="bandedwg.cms">bandedwg.cms</a></td>
<td></td>
<td></td>
</tr>
<tr><td class="br">fm-bell</td>
<td class="br">fm bell sounds (Michael McNabb)</td>
<td class="br">bell.ins</td>
<td class="br"><a href="clm-ins.scm">clm-ins.scm</a></td>
<td class="br"><a href="clm-ins.rb">clm-ins.rb</a></td>
<td class="br"><a href="clm-ins.fs">clm-ins.fs</a></td>
</tr>
<tr><td class="br">bigbird</td>
<td class="br">waveshaping</td>
<td class="br">bigbird.ins</td>
<td class="br"><a href="bird.scm">bird.scm</a></td>
<td class="br"><a href="bird.rb">bird.rb</a></td>
<td class="br"><a href="clm-ins.fs">clm-ins.fs, bird.fs</a></td>
</tr>
<tr><td class="br">singbowl</td>
<td class="br">Juan Reyes Tibetan bowl instrument</td>
<td class="br">bowl.ins</td>
<td class="br"><a href="bowl.cms">bowl.cms</a></td>
<td></td>
<td></td>
</tr>
<tr><td class="br">canter</td>
<td class="br">fm bagpipes (Peter Commons)</td>
<td class="br">canter.ins</td>
<td class="br"><a href="clm-ins.scm">clm-ins.scm</a></td>
<td class="br"><a href="clm-ins.rb">clm-ins.rb</a></td>
<td class="br"><a href="clm-ins.fs">clm-ins.fs</a></td>
</tr>
<tr><td class="br">cellon</td>
<td class="br">feedback fm (Stanislaw Krupowicz)</td>
<td class="br">cellon.ins</td>
<td class="br"><a href="clm-ins.scm">clm-ins.scm</a></td>
<td class="br"><a href="clm-ins.rb">clm-ins.rb</a></td>
<td class="br"><a href="clm-ins.fs">clm-ins.fs</a></td>
</tr>
<tr><td class="br">cnvrev</td>
<td class="br">convolution (aimed at reverb)</td>
<td class="br">cnv.ins</td>
<td class="br"><a href="clm-ins.scm">clm-ins.scm</a></td>
<td></td>
<td></td>
</tr>
<tr><td class="br">moving sounds</td>
<td class="br">sound movement (Fernando Lopez-Lezcano)</td>
<td class="br">dlocsig.lisp</td>
<td class="br"><a href="dlocsig.scm">dlocsig.scm</a></td>
<td class="br"><a href="dlocsig.rb">dlocsig.rb</a></td>
<td></td>
</tr>
<tr><td class="br">drone</td>
<td class="br">additive synthesis (bag.clm) (Peter Commons)</td>
<td class="br">drone.ins</td>
<td class="br"><a href="clm-ins.scm">clm-ins.scm</a></td>
<td class="br"><a href="clm-ins.rb">clm-ins.rb</a></td>
<td class="br"><a href="clm-ins.fs">clm-ins.fs</a></td>
</tr>
<tr><td class="br">expandn</td>
<td class="br">granular synthesis (Michael Klingbeil)</td>
<td class="br">expandn.ins</td>
<td class="br"><a href="clm-ins.scm">clm-ins.scm</a></td>
<td></td>
<td></td>
</tr>
<tr><td class="br">granulate-sound</td>
<td class="br">examples granular synthesis</td>
<td class="br">expsrc.ins</td>
<td class="br"><a href="clm-ins.scm">clm-ins.scm</a></td>
<td class="br"><a href="clm-ins.rb">clm-ins.rb</a></td>
<td class="br"><a href="clm-ins.fs">clm-ins.fs</a></td>
</tr>
<tr><td class="br">cross-fade</td>
<td class="br">cross-fades in the frequency domain</td>
<td class="br">fade.ins</td>
<td class="br"><a href="fade.scm">fade.scm</a></td>
<td></td>
<td></td>
</tr>
<tr><td class="br">filter-sound</td>
<td class="br">filter a sound file</td>
<td class="br">fltsnd.ins</td>
<td class="br"><a href="dsp.scm">dsp.scm</a></td>
<td></td>
<td></td>
</tr>
<tr><td class="br">stereo-flute</td>
<td class="br">physical model of a flute (Nicky Hind)</td>
<td class="br">flute.ins</td>
<td class="br"><a href="clm-ins.scm">clm-ins.scm</a></td>
<td class="br"><a href="clm-ins.rb">clm-ins.rb</a></td>
<td class="br"><a href="clm-ins.fs">clm-ins.fs</a></td>
</tr>
<tr><td class="br">fm examples</td>
<td class="br">fm bell, gong, drum (Paul Weineke, Jan Mattox)</td>
<td class="br">fmex.ins</td>
<td class="br"><a href="clm-ins.scm">clm-ins.scm</a></td>
<td class="br"><a href="clm-ins.rb">clm-ins.rb</a></td>
<td class="br"><a href="clm-ins.fs">clm-ins.fs</a></td>
</tr>
<tr><td class="br">Jezar's reverb</td>
<td class="br">fancy reverb (Jezar Wakefield)</td>
<td class="br">freeverb.ins</td>
<td class="br"><a href="freeverb.scm">freeverb.scm</a></td>
<td class="br"><a href="freeverb.rb">freeverb.rb</a></td>
<td class="br"><a href="clm-ins.fs">clm-ins.fs</a></td>
</tr>
<tr><td class="br">fofins</td>
<td class="br">FOF synthesis</td>
<td class="br"><a href="#wave-train">sndclm.html</a></td>
<td class="br"><a href="clm-ins.scm">clm-ins.scm</a></td>
<td class="br"><a href="clm-ins.rb">clm-ins.rb</a></td>
<td class="br"><a href="clm-ins.fs">clm-ins.fs</a></td>
</tr>
<tr><td class="br">fullmix</td>
<td class="br">a mixer</td>
<td class="br">fullmix.ins</td>
<td class="br"><a href="clm-ins.scm">clm-ins.scm</a></td>
<td class="br"><a href="clm-ins.rb">clm-ins.rb</a></td>
<td class="br"><a href="clm-ins.fs">clm-ins.fs</a></td>
</tr>
<tr><td class="br">grani</td>
<td class="br">granular synthesis (Fernando Lopez-Lezcano)</td>
<td class="br">grani.ins</td>
<td class="br"><a href="grani.scm">grani.scm</a></td>
<td></td>
<td></td>
</tr>
<tr><td class="br">grapheq</td>
<td class="br">graphic equalizer (Marco Trevisani)</td>
<td class="br">grapheq.ins</td>
<td class="br"><a href="clm-ins.scm">clm-ins.scm</a></td>
<td class="br"><a href="clm-ins.rb">clm-ins.rb</a></td>
<td class="br"><a href="clm-ins.fs">clm-ins.fs</a></td>
</tr>
<tr><td class="br">fm-insect</td>
<td class="br">fm</td>
<td class="br">insect.ins</td>
<td class="br"><a href="clm-ins.scm">clm-ins.scm</a></td>
<td class="br"><a href="clm-ins.rb">clm-ins.rb</a></td>
<td></td>
</tr>
<tr><td class="br">jc-reverb</td>
<td class="br">a reverberator (see also jlrev)</td>
<td class="br">jcrev.ins</td>
<td class="br"><a href="jcrev.scm">jcrev.scm</a></td>
<td class="br"><a href="clm-ins.rb">clm-ins.rb</a></td>
<td class="br"><a href="clm-ins.fs">clm-ins.fs</a></td>
</tr>
<tr><td class="br">fm-voice</td>
<td class="br">fm voice (John Chowning)</td>
<td class="br">jcvoi.ins</td>
<td class="br"><a href="jcvoi.scm">jcvoi.scm </a></td>
<td></td>
<td></td>
</tr>
<tr><td class="br">kiprev</td>
<td class="br">a fancier reverberator (Kip Sheeline)</td>
<td class="br">kiprev.ins</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr><td class="br">lbj-piano</td>
<td class="br">additive synthesis piano (Doug Fulton)</td>
<td class="br">lbjPiano.ins</td>
<td class="br"><a href="clm-ins.scm">clm-ins.scm</a></td>
<td class="br"><a href="clm-ins.rb">clm-ins.rb</a></td>
<td class="br"><a href="clm-ins.fs">clm-ins.fs</a></td>
</tr>
<tr><td class="br">rotates</td>
<td class="br">Juan Reyes Leslie instrument</td>
<td class="br">leslie.ins</td>
<td class="br"><a href="leslie.cms">leslie.cms</a></td>
<td></td>
<td></td>
</tr>
<tr><td class="br">maraca</td>
<td class="br">Perry Cook's maraca physical models</td>
<td class="br">maraca.ins</td>
<td class="br"><a href="maraca.scm">maraca.scm</a></td>
<td class="br"><a href="maraca.rb">maraca.rb</a></td>
<td></td>
</tr>
<tr><td class="br">maxfilter</td>
<td class="br">Juan Reyes modular synthesis</td>
<td class="br">maxf.ins</td>
<td class="br"><a href="maxf.scm">maxf.scm</a></td>
<td class="br"><a href="maxf.rb">maxf.rb</a></td>
<td></td>
</tr>
<tr><td class="br">mlb-voice</td>
<td class="br">fm voice (Marc LeBrun)</td>
<td class="br">mlbvoi.ins</td>
<td class="br"><a href="clm-ins.scm">clm-ins.scm</a></td>
<td class="br"><a href="clm-ins.rb">clm-ins.rb</a></td>
<td class="br"><a href="clm-ins.fs">clm-ins.fs</a></td>
</tr>
<tr><td class="br">moog filters</td>
<td class="br">Moog filters (Fernando Lopez-Lezcano)</td>
<td class="br">moog.lisp</td>
<td class="br"><a href="moog.scm">moog.scm</a></td>
<td></td>
<td></td>
</tr>
<tr><td class="br">fm-noise</td>
<td class="br">noise maker</td>
<td class="br">noise.ins</td>
<td class="br"><a href="noise.scm">noise.scm</a></td>
<td class="br"><a href="noise.rb">noise.rb</a></td>
<td class="br"><a href="clm-ins.fs">clm-ins.fs</a></td>
</tr>
<tr><td class="br">nrev</td>
<td class="br">a popular reverberator (Michael McNabb)</td>
<td class="br">nrev.ins</td>
<td class="br"><a href="clm-ins.scm">clm-ins.scm</a></td>
<td class="br"><a href="clm-ins.rb">clm-ins.rb</a></td>
<td class="br"><a href="clm-ins.fs">clm-ins.fs</a></td>
</tr>
<tr><td class="br">one-cut</td>
<td class="br">"cut and paste" (Fernando Lopez-Lezcano)</td>
<td class="br">one-cut.ins</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr><td class="br">p</td>
<td class="br">Scott van Duyne's piano physical model</td>
<td class="br">piano.ins</td>
<td class="br"><a href="piano.scm">piano.scm</a></td>
<td class="br"><a href="piano.rb">piano.rb</a></td>
<td></td>
</tr>
<tr><td class="br">pluck</td>
<td class="br">Karplus-Strong synthesis (David Jaffe)</td>
<td class="br">pluck.ins</td>
<td class="br"><a href="clm-ins.scm">clm-ins.scm</a></td>
<td class="br"><a href="clm-ins.rb">clm-ins.rb</a></td>
<td class="br"><a href="clm-ins.fs">clm-ins.fs</a></td>
</tr>
<tr><td class="br">pqw</td>
<td class="br">waveshaping</td>
<td class="br">pqw.ins</td>
<td class="br"><a href="clm-ins.scm">clm-ins.scm</a></td>
<td class="br"><a href="clm-ins.rb">clm-ins.rb</a></td>
<td class="br"><a href="clm-ins.fs">clm-ins.fs</a></td>
</tr>
<tr><td class="br">pqw-vox</td>
<td class="br">waveshaping voice</td>
<td class="br">pqwvox.ins</td>
<td class="br"><a href="clm-ins.scm">clm-ins.scm</a></td>
<td class="br"><a href="clm-ins.rb">clm-ins.rb</a></td>
<td class="br"><a href="clm-ins.fs">clm-ins.fs</a></td>
</tr>
<tr><td class="br">physical models</td>
<td class="br">physical modelling (Perry Cook)</td>
<td class="br">prc-toolkit95.lisp</td>
<td class="br"><a href="prc95.scm">prc95.scm</a></td>
<td class="br"><a href="prc95.rb">prc95.rb</a></td>
<td class="br"><a href="clm-ins.fs">clm-ins.fs</a></td>
</tr>
<tr><td class="br">various ins</td>
<td class="br">from Perry Cook's Synthesis Toolkit</td>
<td class="br">prc96.ins</td>
<td class="br"><a href="clm-ins.scm">clm-ins.scm</a></td>
<td class="br"><a href="clm-ins.rb">clm-ins.rb</a></td>
<td class="br"><a href="clm-ins.fs">clm-ins.fs</a></td>
</tr>
<tr><td class="br">pvoc</td>
<td class="br">phase vocoder (Michael Klingbeil)</td>
<td class="br">pvoc.ins</td>
<td class="br"><a href="pvoc.scm">pvoc.scm</a></td>
<td class="br"><a href="pvoc.rb">pvoc.rb</a></td>
<td></td>
</tr>
<tr><td class="br">resflt</td>
<td class="br">filters (Xavier Serra, Richard Karpen)</td>
<td class="br">resflt.ins</td>
<td class="br"><a href="clm-ins.scm">clm-ins.scm</a></td>
<td class="br"><a href="clm-ins.rb">clm-ins.rb</a></td>
<td class="br"><a href="clm-ins.fs">clm-ins.fs</a></td>
</tr>
<tr><td class="br">reson</td>
<td class="br">fm formants (John Chowning)</td>
<td class="br">reson.ins</td>
<td class="br"><a href="clm-ins.scm">clm-ins.scm</a></td>
<td class="br"><a href="clm-ins.rb">clm-ins.rb</a></td>
<td class="br"><a href="clm-ins.fs">clm-ins.fs</a></td>
</tr>
<tr><td class="br">ring-modulate</td>
<td class="br">ring-modulation of sounds (Craig Sapp)</td>
<td class="br">ring-modulate.ins</td>
<td class="br"><a href="examp.scm">examp.scm</a></td>
<td class="br"><a href="examp.rb">examp.rb</a></td>
<td></td>
</tr>
<tr><td class="br">rmsenv</td>
<td class="br">rms envelope of sound (Bret Battey)</td>
<td class="br">rmsenv.ins</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr><td class="br">pins</td>
<td class="br">spectral modelling</td>
<td class="br">san.ins</td>
<td class="br"><a href="clm-ins.scm">clm-ins.scm</a></td>
<td class="br"><a href="clm-ins.rb">clm-ins.rb</a></td>
<td class="br"><a href="clm-ins.fs">clm-ins.fs</a></td>
</tr>
<tr><td class="br">scanned</td>
<td class="br">Juan Reyes scanned synthesis instrument</td>
<td class="br">scanned.ins</td>
<td class="br"><a href="dsp.scm">dsp.scm</a></td>
<td></td>
<td></td>
</tr>
<tr><td class="br">scentroid</td>
<td class="br">spectral scentroid envelope (Bret Battey)</td>
<td class="br">scentroid.ins</td>
<td class="br"><a href="dsp.scm">dsp.scm</a></td>
<td></td>
<td></td>
</tr>
<tr><td class="br">shepard</td>
<td class="br">Shepard tones (Juan Reyes)</td>
<td class="br">shepard.ins</td>
<td class="br"><a href="sndscm.html#wsdoc">sndscm.html</a></td>
<td></td>
<td></td>
</tr>
<tr><td class="br">singer</td>
<td class="br">Perry Cook's vocal tract physical model</td>
<td class="br">singer.ins</td>
<td class="br"><a href="singer.scm">singer.scm</a></td>
<td class="br"><a href="singer.rb">singer.rb</a></td>
<td></td>
</tr>
<tr><td class="br">sndwarp</td>
<td class="br">Csound-like sndwarp generator (Bret Battey)</td>
<td class="br">sndwarp.ins</td>
<td class="br"><a href="sndwarp.scm">sndwarp.scm</a></td>
<td></td>
<td></td>
</tr>
<tr><td class="br">stochastic</td>
<td class="br">Bill Sack's stochastic synthesis implementation</td>
<td class="br">stochastic.ins</td><td class="br"><a href="stochastic.scm">stochastic.scm</a></td>
<td></td>
<td></td>
</tr>
<tr><td class="br">bow</td>
<td class="br">Juan Reyes bowed string physical model</td>
<td class="br">strad.ins</td>
<td class="br"><a href="strad.scm">strad.scm</a></td>
<td class="br"><a href="strad.rb">strad.rb</a></td>
<td></td>
</tr>
<tr><td class="br">track-rms</td>
<td class="br">rms envelope of sound file (Michael Edwards)</td>
<td class="br">track-rms.ins</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr><td class="br">fm-trumpet</td>
<td class="br">fm trumpet (Dexter Morrill)</td>
<td class="br">trp.ins</td>
<td class="br"><a href="clm-ins.scm">clm-ins.scm</a></td>
<td class="br"><a href="clm-ins.rb">clm-ins.rb</a></td>
<td class="br"><a href="clm-ins.fs">clm-ins.fs</a></td>
</tr>
<tr><td class="br">various ins</td>
<td class="br">granular synthesis, formants, etc</td>
<td class="br">ugex.ins</td>
<td class="br"><a href="clm-ins.scm">clm-ins.scm</a></td>
<td class="br"><a href="clm-ins.rb">clm-ins.rb</a></td>
<td></td>
</tr>
<tr><td class="br">fm-violin</td>
<td class="br">fm violin (fmviolin.clm, popi.clm)</td>
<td class="br">v.ins</td>
<td class="br"><a href="v.scm">v.scm</a></td>
<td class="br"><a href="v.rb">v.rb</a></td>
<td class="br"><a href="clm-ins.fs">clm-ins.fs</a></td>
</tr>
<tr><td class="br">vowel</td>
<td class="br">vowels (Michelle Daniels)</td>
<td class="br">vowel.ins</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr><td class="br">vox</td>
<td class="br">fm voice (cream.clm)</td>
<td class="br">vox.ins</td>
<td class="br"><a href="clm-ins.scm">clm-ins.scm</a></td>
<td class="br"><a href="clm-ins.rb">clm-ins.rb</a></td>
<td class="br"><a href="clm-ins.fs">clm-ins.fs</a></td>
</tr>
<tr><td class="br">zc, zn</td>
<td class="br">interpolating delays</td>
<td class="br">zd.ins</td>
<td class="br"><a href="clm-ins.scm">clm-ins.scm</a></td>
<td class="br"><a href="clm-ins.rb">clm-ins.rb</a></td>
<td class="br"><a href="clm-ins.fs">clm-ins.fs</a></td>
</tr>
<tr><td class="br">zipper</td>
<td class="br">The 'digital zipper' effect.</td>
<td class="br">zipper.ins</td>
<td class="br"><a href="zip.scm">zip.scm</a></td>
<td class="br"><a href="zip.rb">zip.rb</a></td>
<td></td>
</tr>
</table>
<p>
If you develop
an interesting instrument that you're willing to share, please send it to me
(bil@ccrma.stanford.edu).
<a href="sndscm.html#definstrument">definstrument</a>, the individual instruments, and <a href="sndscm.html#wsdoc">with-sound</a> are documented in
<a href="sndscm.html">sndscm.html</a>.
</p>
<div class="related">
related documentation:
<a href="snd.html">snd.html </a>
<a href="extsnd.html">extsnd.html </a>
<a href="grfsnd.html">grfsnd.html </a>
<a href="sndscm.html">sndscm.html </a>
<a href="fm.html">fm.html </a>
<a href="sndlib.html">sndlib.html </a>
<a href="s7.html">s7.html </a>
<a href="index.html">index.html</a>
</div>
</body></html>
|