File: examp.fs

package info (click to toggle)
snd 25.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 44,016 kB
  • sloc: ansic: 291,818; lisp: 260,387; ruby: 71,134; sh: 3,293; fortran: 2,342; csh: 1,062; cpp: 294; makefile: 294; python: 87; xml: 27; javascript: 1
file content (2748 lines) | stat: -rw-r--r-- 74,857 bytes parent folder | download | duplicates (5)
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
\ examp.fs -- examples from examp.scm|rb

\ Translator/Author: Michael Scholz <mi-scholz@users.sourceforge.net>
\ Created: 05/07/05 13:09:37
\ Changed: 17/12/15 06:14:01
\
\ @(#)examp.fs	1.70 12/15/17

\ With original comments and doc strings from examp.scm.
\
\ all-chans		( -- array-of-lists )
\ close-sound-extend	( snd -- )
\ snd-snd		( :optional snd -- snd )
\ snd-chn		( :optional chn -- chn )
\ make-read-eval-loop	( -- )
\ remove-read-eval-loop	( -- )
\ toggle-read-eval-loop	( -- )
\
\ from frame.scm
\ insert-vct		( v :optional beg dur snd chn edpos -- samps )
\
\ examp.(scm|rb)
\ selection-rms		( -- val )
\ region-rms		( :optional n -- val )
\ window-samples	( :optional snd chn -- vct )
\ display-energy	( snd chn -- val )
\ display-db		( snd chn -- val )
\ window-rms		( -- val )
\ fft-peak		( snd chn scaler -- pk )
\ finfo			( file -- str )
\ display-correlate	( snd chn y0 y1 -- val )
\ zoom-spectrum		( snd chn y0 y1 -- val )
\ superimpose-ffts	( snd chn y0 y1 -- val )
\ locate-zero		( limit -- samp )
\ mpg			( mpgfile rawfile -- )
\ auto-dot		( snd chn y0 y1 -- val )
\ first-mark-in-window-at-left ( -- )
\ flash-selected-data	( millisecs -- )
\ mark-loops		( -- )
\
\ do-all-chans		( func :optional origin -- )
\ update-graphs		( -- )
\ do-chans		( func :optional origin -- )
\ do-sound-chans	( func :optional origin -- )
\ every-sample?		( func -- f )
\ sort-samples		( nbins -- ary )
\ place-sound		( mono stereo pan -- res )
\ fft-edit		( bottom top :optional snd chn -- vct )
\ fft-squelch		( squelch :optional snd chn -- scl )
\ fft-cancel		( lo-freq hi-freq :optional snd chn -- vct )
\ make-ramp		( :optional size -- gen )
\ ramp			( gen up -- val )
\ squelch-vowels	( :optional snd chn -- val )
\ fft-env-data		( fft-env :optional snd chn -- vct )
\ fft-env-edit		( fft-env :optional snd chn -- vct )
\ fft-env-interp	( env1 env2 interp :optional snd chn -- vct )
\ filter-fft		( flt :optional normalize snd chn -- val )
\ fft-smoother		( cutoff start samps :optional snd chn -- val )
\ comb-filter		( scaler size -- prc; x self -- res )
\ comb-chord		( scaler size amp -- prc; x self -- res )
\ zcomb			( scaler size pm -- prc; x self -- val )
\ notch-filter		( scaler size -- prc; x self -- val )
\ formant-filter	( radius frequency -- prc; x self -- val )
\ formants		( r1 f1 r2 f2 r3 f3 -- prc; x self -- val )
\ moving-formant	( radius move -- prc; x self -- val )
\ osc-formants		( radius bases amounts freqs -- prc; x self -- val )
\ 
\ echo			( scaler secs -- prc; y self -- val )
\ zecho			( scaler secs freq amp -- prc; y self -- val )
\ flecho		( scaler secs -- prc; y self -- val )
\ ring-mod		( freq gliss-env -- prc; y self -- val )
\ am			( freq -- prc; y self -- val )
\ vibro			( speed depth -- prc; y self -- val )
\ 
\ hello-dentist		( frq amp :optional snd chn -- vct )
\ fp			( sr osamp osfrq :optional snd chn -- vct )
\ compand		( -- prc; y self -- val )
\ expsrc		( rate snd chn -- val )
\ expsnd		( gr-env snd chn -- vct )
\ cross-synthesis	( cross-snd amp fftsize r -- prc; y self -- val )
\ voiced->unvoiced	( amp fftsize r tempo :optional snd chn -- vct )
\ pulse-voice		( cos :optional freq amp fftsize r snd chn -- vct )
\ cnvtest		( snd0 snd1 amp -- mx )
\ swap-selection-channels ( -- )
\ make-sound-interp	( start :optional snd chn -- prc; loc self -- val )
\ sound-interp		( func loc -- val )
\ env-sound-interp	( env :optional time-scale snd chn -- vct )
\ granulated-sound-interp ( e :optional tscl glen genv ohop snd chn -- vct )
\ filtered-env		( e :optional snd chn -- val )
\  
\ find-click		( loc -- pos )
\ remove-clicks		( -- )
\ search-for-click	( -- pos )
\ zero+			( -- prc; n self -- val )
\ next-peak		( -- prc; n self -- val )
\ find-pitch		( pitch -- prc; y self -- val )
\ file->vct		( file -- vct )
\ add-notes		( notes :optional snd chn -- #f )
\ region-play-list	( data -- )
\ region-play-sequence	( data -- )
\ replace-with-selection ( -- )
\ explode-sf2		( -- )
\ open-next-file-in-directory ( -- f )
\ click-middle-button-to-open-next-file-in-directory ( -- )
\ chain-dsps		( start dur :optional dsps -- )
\ 
\ scramble-channels	( new-order -- )
\ scramble-channel	( silence -- )
\ reverse-by-blocks	( block-len :optional snd chn -- val )
\ reverse-within-blocks	( block-len :optional snd chn -- val )
\ channel-clipped?	( :optional snd chn -- val )
\ sync-everything	( -- )
\ 
\ make-moog-filter	( freq Q -- gen )
\ moog-frequecy@	( gen -- frq )
\ moog-frequecy!	( frq gen -- )
\ moog-filter		( gen sig -- A )

'snd-nogui provided? [if]
	<'> noop alias show-widget ( a -- a )
	<'> noop alias hide-widget ( a -- a )
	<'> noop alias widget-size ( a -- a )
	: set-widget-size     ( a b -- a )    drop ;
	: window-property     ( a b -- a )    drop ;
	: set-window-property ( a b c -- a ) 2drop ;
[then]

\ #( #( snd0 chn0 ) #( snd0 chn1 ) ... )
: all-chans ( -- array-of-lists )
	nil { snd }
	#() ( ary )
	sounds each to snd
		snd channels 0 ?do
			( ary ) #( snd i ) array-push
		loop
	end-each
	( ary )
;

\ 5 == notebook widget
: close-sound-extend <{ snd -- }>
	main-widgets 5 object-ref if
		sounds snd object-index 0 max { idx }
		snd close-sound drop
		sounds empty? unless
			sounds
			    idx  sounds length < if
				    idx
			    else
				    -1
			    then object-ref set-selected-sound drop
		then
	else
		snd close-sound drop
	then
;

: snd-snd <{ :optional snd #f -- snd }>
	snd sound? if
		snd
	else
		selected-sound to snd
		snd sound? if
			snd
		else
			sounds car
		then
	then
;

: snd-chn <{ :optional chn #f -- chn }>
	chn integer? if
		chn
	else
		#f selected-channel to chn
		chn integer? if
			chn
		else
			0
		then
	then
;

\ === Traditional Forth Read-Eval-Loop for Snd's listener. ===

"ok " constant read-eval-loop-prompt

hide
#f value rel-ficl-stack
#f value rel-old-listener-prompt
#f value rel-old-hooks

: rel-cb <{ text -- flag }>
	rel-ficl-stack restore-stack
	$space snd-print drop
	text string-eval save-stack to rel-ficl-stack
	$cr snd-print drop
	read-eval-loop-prompt snd-print drop
	\ reset-listener-cursor returns #f
	reset-listener-cursor not
;

: rel-add ( -- )
	listener-prompt to rel-old-listener-prompt
	read-eval-loop-prompt set-listener-prompt drop
	read-hook hook->array to rel-old-hooks
	read-hook reset-hook!
	read-hook <'> rel-cb add-hook!
;

: rel-remove ( -- )
	read-hook <'> rel-cb remove-hook!
	nil { prc }
	rel-old-hooks empty? unless
		rel-old-hooks each to prc
			read-hook prc add-hook!
		end-each
	then
	rel-old-listener-prompt set-listener-prompt drop
;

: rel-toggle ( key -- )
	( key ) case
		'on of
			read-hook <'> rel-cb hook-member? unless
				rel-add
			then
		endof
		'off of
			rel-remove
		endof
		'toggle of
			read-hook <'> rel-cb hook-member? if
				rel-remove
			else
				rel-add
			then
		endof
	endcase
	#f to rel-ficl-stack
	stack-reset
	reset-listener-cursor drop
;
set-current

<'> noop alias ok

: make-read-eval-loop   ( -- )   'on rel-toggle ;
: remove-read-eval-loop ( -- )   'off rel-toggle ;
: toggle-read-eval-loop ( -- )   'toggle rel-toggle ;
previous

require clm
require env
require rgb

\ === from frame.scm
\
: insert-vct <{ v :optional beg 0 dur #f snd #f chn #f edpos #f -- samps }>
	doc" Insert vct V's data into sound SND at BEG."
	v vct? v 1 "a vct" assert-type
	dur v vct-length || { len }
	beg len v snd chn edpos #f "%S %s %s %s"
	    #( v beg dur get-func-name ) string-format insert-samples
;

\ === examp.scm|rb
\ 
\ this mainly involves keeping track of the current sound/channel
: selection-rms ( -- val )
	doc" Return rms of selection data using sample readers."
	undef selection? unless
		'no-active-selection #( get-func-name ) fth-throw
	then
	selection-position #f #f 1 #f make-sampler { rd }
	selection-framples { len }
	0.0 ( sum ) len 0 ?do
		rd next-sample dup f* f+ ( sum += ... )
	loop
	len f/ fsqrt
;

: region-rms <{ :optional reg 0 -- val }>
	doc" Return rms of region N's data (chan 0)."
	reg region? unless
		'no-such-region #( "%s: %s" get-func-name reg ) fth-throw
	then
	reg 0 0 region->vct { data }
	data dup dot-product data length f/ fsqrt
;

: window-samples <{ :optional snd #f chn #f -- vct }>
	doc" Sample in SND channel CHN in current graph window."
	snd chn left-sample { wl }
	snd chn right-sample { wr }
	wl  wr wl - 1+  snd chn #f channel->vct
;

\ graph data xlabel x0 x1 y0 y1 snd chn force-display show-axes-choice
: display-energy <{ snd chn -- v }>
	doc" A lisp-graph-hook function to display the time \
domain data as energy (squared).\n\
list-graph-hook <'> display-energy add-hook!."
	snd chn undef undef undef make-graph-data dup array? if
		1 array-ref
	then { data }
	data if
		snd chn left-sample { ls }
		snd chn right-sample { rs }
		snd srate { sr }
		snd chn y-zoom-slider { y-max }
		data data vct-multiply! "energy"
		    ls sr f/ rs sr f/ 0.0 y-max dup f*
		    snd chn #t show-all-axes graph
	else
		#f
	then
;
\ lisp-graph-hook <'> display-energy add-hook!

hide
: db-calc { val -- r }
	val 0.001 f< if
		-60.0
	else
		20.0 val flog10 f*
	then
;
set-current

: display-db <{ snd chn -- v }>
	doc" A lisp-graph-hook function to display \
the time domain data in dB.\n\
list-graph-hook <'> display-db add-hook!."
	snd chn undef undef undef make-graph-data dup array? if
		1 array-ref
	then { data }
	data if
		snd chn left-sample { ls }
		snd chn right-sample { rs }
		snd srate { sr }
		data map
			*key* fabs db-calc 60.0 f+
		end-map "dB" ls sr f/ rs sr f/ 0.0 60.0
		    snd chn #t show-all-axes graph
	else
		#f
	then
;
previous
\ lisp-graph-hook <'> display-db add-hook!

: window-rms ( -- val )
	doc" Return rms of data in currently selected graph window."
	#f #f left-sample  { ls }
	#f #f right-sample { rs }
	ls rs ls - 1+ #f #f #f channel->vct { data }
	data vct-length { len }
	data dup len dot-product len f/ fsqrt
;

: fft-peak <{ snd chn scaler -- pk }>
	doc" print peak spectral magnitude\n\
Returns the peak spectral magnitude.  \
It is intended for use with after-transform-hook."
	snd chn transform-graph?
	snd chn transform-graph-type graph-once = && if
		snd chn #f transform->vct vct-peak f2*
		    snd chn transform-size f/ { pk }
		pk number->string snd status-report drop
		pk
	else
		#f
	then
;
\ after-transform-hook <'> fft-peak add-hook!

\ ;;; -------- 'info' from extsnd.html using format --------

: finfo ( file -- str )
	doc" Return description (as a string) of file."
	find-file { file }
	file unless 
		'no-such-file #( "%s: %S" get-func-name file ) fth-throw
	then
	"%s: chans: %d, srate: %d, %s, %s, len: %1.3f"
	    #( file
	       file mus-sound-chans
	       file mus-sound-srate
	       file mus-sound-header-type mus-header-type-name
	       file mus-sound-sample-type mus-sample-type-name
	       file mus-sound-samples
	       file mus-sound-chans
	       file mus-sound-srate f* f/ ) string-format
;

\ ;;; -------- Correlation --------
\ ;;;
\ ;;; correlation of channels in a stereo sound

: display-correlate <{ snd chn y0 y1 -- val }>
	doc" Return the correlation of SND's 2 channels (intended \
for use with graph-hook).  \
y0 and y1 are ignored."
	snd channels 2 = if
		snd 0 #f framples 1 >
		snd 1 #f framples 1 > && if
			snd chn left-sample  { ls }
			snd chn right-sample { rs }
			rs ls - 1+ { ilen }
			ilen flog 2.0 flog f/ fround->s { pow2 }
			2.0 pow2 f** fround->s { fftlen }
			fftlen 2/ { fftlen2 }
			fftlen 1/f { fftscale }
			ls fftlen snd 0 #f channel->vct { rl1 }
			ls fftlen snd 1 #f channel->vct { rl2 }
			fftlen 0.0 make-vct { im1 }
			fftlen 0.0 make-vct { im2 }
			rl1 im1 1 fft drop
			rl2 im2 1 fft drop
			rl1 vct-copy { tmprl }
			im1 vct-copy { tmpim }
			fftlen2 0.0 make-vct { data3 }
			tmprl rl2 vct-multiply! drop
			tmpim im2 vct-multiply! drop
			im2   rl1 vct-multiply! drop
			rl2   im1 vct-multiply! drop
			tmprl tmpim 0 vct-add! drop
			im2 rl2 vct-subtract! drop
			tmprl im2 -1 fft drop
			data3 tmprl 0 vct-add! drop
			data3 fftscale vct-scale! drop
			data3 "lag time" 0 fftlen2
			    32768.0 -32768.0 snd chn #t show-all-axes graph
		then
	else
		get-func-name " wants stereo input" $+ snd status-report
	then
;
\ graph-hook <'> display-correlate add-hook!

\ ;;; -------- set transform-size based on current time domain window size
\ ;;;
\ ;;; also zoom spectrum based on y-axis zoom slider

: zoom-spectrum <{ snd chn y0 y1 -- val }>
	doc" Set the transform size to correspond to the \
time-domain window size (use with graph-hook)."
	snd chn transform-graph?
	snd chn transform-graph-type graph-once = && if
		2.0 snd chn right-sample snd chn left-sample f-
		    flog 2.0 flog f/ fceil f** fround->s { val }
		val 0> if
			val snd chn set-transform-size drop
		then
		snd chn y-zoom-slider snd chn set-spectrum-end drop
	then
	#f
;
\ graph-hook <'> zoom-spectrum add-hook!

\ ;;; -------- superimpose spectra of sycn'd sounds

: superimpose-ffts <{ snd chn y0 y1 -- val }>
	doc" Superimpose ffts of multiple (syncd) sounds (use with graph-hook)."
	0 ( maxsync )
	sounds each ( snd )
		sync max
	end-each { maxsync }
	0 sounds each { n }
		n sync snd sync = if
			n
		else
			maxsync 1+
		then min
	end-each { sndsync }
	snd chn left-sample  { ls }
	snd chn right-sample { rs }
	snd sync 0>
	rs ls > &&
	snd sndsync = && if
		rs ls f- 1.0 fmax 2.0 flog fceil->s { pow2 }
		2.0 pow2 f** floor->s { fftlen }
		pow2 2 > if
			#() { ffts }
			sounds each { n }
				n sync snd sync =
				n channels chn > && if
					ls fftlen n chn #f channel->vct { fdr }
					fftlen 0.0 make-vct { fdi }
					fftlen 2/ 0.0 make-vct ( spectr )
					    fdr fdi #f 2 spectrum
					    0 vct-add! { val }
					ffts #( val ) array-push drop
				then
			end-each
			ffts "spectra" 0.0 0.5 y0 y1
			    snd chn #t show-all-axes graph drop
		then
	then
	#f
;
\ graph-hook <'> superimpose-ffts add-hook!

\ ;;; -------- c-g? example (Anders Vinjar)

: locate-zero ( limit -- samp )
	doc" Look for successive samples that sum to less than LIMIT, \
moving the cursor if successful."
	{ limit }
	#f #f #f cursor { start }
	start #f #f 1 #f make-sampler { sf }
	sf next-sample fabs { val0 }
	sf next-sample fabs { val1 }
	begin
		sf sampler-at-end?
		val0 val1 f+ limit f< || not
	while
		start 1+ to start
		val1 to val0
		sf next-sample fabs to val1
	repeat
	sf free-sampler drop
	start #f #f #f set-cursor
;

\ ;;; -------- translate mpeg input to 16-bit linear and read into Snd
\ ;;;
\ ;;; mpg123 with the -s switch sends the 16-bit (mono or stereo)
\ ;;;   representation of an mpeg file to stdout.  There's also
\ ;;;   apparently a switch to write 'wave' output.

: mpg ( mpgfile rawfile -- )
	doc" Convert file from MPEG to raw 16-bit samples using mpg123."
	{ mpgfile rawfile }
	mpgfile io-open-read { io }
	io io-getc { b0 }
	io io-getc { b1 }
	io io-getc { b2 }
	io io-getc { b3 }
	io io-close
	b0 255 <>
	b1 0b11100000 and 0b11100000 <> || if
		"%s is not an MPEG file (first 11 bytes: %b %b)"
		    #( mpgfile b0 b1 0b11100000 and ) clm-print
		exit
	then
	b1 0b11000    and 3 rshift { id }
	b1 0b110      and 1 rshift { layer }
	b2 0b1100     and 2 rshift { srate-index }
	b3 0b11000000 and 6 rshift { channel-mode }
	id 1 = if
		"odd: %s is using a reserved Version ID"
		    #( mpgfile ) clm-print
	then
	layer 0= if
		"odd: %s is using a reserved layer description"
		    #( mpgfile ) clm-print
	then
	channel-mode 3 = if
		1
	else
		2
	then { chans }
	id 0= if
		4
	else
		id 2 = if
			2
		else
			1
		then
	then { mpegnum }
	layer 3 = if
		1
	else
		layer 2 = if
			2
		else
			3
		then
	then { mpeg-layer }
	#( 44100 48000 32000 0 ) srate-index array-ref mpegnum / { srate }
	"%s: %s Hz, %s, MPEG-%s"
	    #( mpgfile
	       srate
	       chans 1 = if
		       "mono"
	       else
		       "stereo"
	       then mpeg-layer ) clm-print
	"mpg123 -s %s > %s" #( mpgfile rawfile ) string-format file-system if
		rawfile chans srate little-endian? if
			mus-lshort
		else
			mus-bshort
		then open-raw-sound drop
	else
		"system in %s: %s" #( get-func-name exit-status ) fth-throw
	then
;
\ "mpeg.mpg" "mpeg.raw" mpg

\ ;;; -------- read ASCII files
\ ;;;
\ ;;; these are used by Octave (WaveLab) -- each line has one integer,
\ ;;;   apparently a signed short.

hide
: read-ascii-cb { fname snd -- prc; self -- }
	0 proc-create ( prc )
	fname , snd ,
  does> { self -- }
	self @ ( fname ) readlines { in-buffer }
	self cell+ @ { snd }
	512 { bufsize }
	bufsize 0.0 make-vct { data }
	0 { loc }
	0 { frame }
	32768.0 1/f { short->float }
	nil { val }
	in-buffer each ( line )
		nil string-split each ( str-val )
			string->number ( val ) short->float f*
			    data loc rot vct-set! drop
			loc 1+ to loc
			loc bufsize = if
				data frame bufsize snd 0 vct->channel drop
				frame bufsize d+ to frame
				0 to loc
			then
		end-each
	end-each
	loc d0> if
		data frame loc snd 0 vct->channel drop
	then
;
set-current

: read-ascii
    <{ fl :optional sf "test.snd" st mus-next ht mus-bshort sr 44100 -- snd }>
	doc" Try to read an ASCII sound file."
	"created by %s: %s" #( get-func-name fl ) string-format { com }
	sf 1 sr ht st com new-sound { snd }
	fl snd read-ascii-cb as-one-edit drop
	snd
;
previous

\ ;;; -------- make dot size dependent on number of samples being displayed
\ ;;; 
\ ;;; this could be extended to set time-graph-style to graph-lines
\ ;;; if many samples are displayed, etc

: auto-dot <{ snd chn y0 y1 -- val }>
	doc" Set the dot size depending on the number of \
samples being displayed (use with graph-hook)."
	snd chn right-sample snd chn left-sample - { dots }
	dots 100 > if
		1
	else
		dots 50 > if
			2
		else
			dots 25 > if
				3
			else
				5
			then
		then
	then snd chn set-dot-size
;
\ graph-hook <'> auto-dot add-hook!

\ ;;; -------- move window left edge to mark upon 'm'
\ ;;;
\ ;;; in large sounds, it can be pain to get the left edge of the window
\ ;;; aligned with a specific spot in the sound.  In this code, we assume
\ ;;; the desired left edge has a mark, and the 'm' key (without control)
\ ;;; will move the window left edge to that mark.

: first-mark-in-window-at-left <{ -- val }>
	doc" Move the graph so that the leftmost \
visible mark is at the left edge."
	#f snd-snd { keysnd }
	#f snd-chn { keychn }
	keysnd keychn left-sample { current-left-sample }
	keysnd keychn #f marks { chan-marks }
	chan-marks length 0= if
		"no marks" status-report
	else
		#f ( flag )
		chan-marks map
			*key* undef mark-sample
		end-map each { samp }
			samp current-left-sample > if
				drop 	\ drop #f
				samp
				leave
			then
		end-each { leftmost }
		leftmost if
			leftmost keysnd keychn set-left-sample
		else
			"no mark in window" status-report
		then
	then drop
	keyboard-no-action
;
\ "m" 0 <'> first-mark-in-window-at-left
\     #f "align window left edge with mark" dup bind-key drop

\ ;;; -------- flash selected data red and green

hide
: fsd-cb { ms xt -- prc; self -- val }
	0 proc-create ( prc )
	ms , xt ,
  does> { self -- val }
	self @ ( ms ) self cell+ @ ( xt ) execute
	#f
;
user data-red?
#t data-red? !
set-current

: flash-selected-data ( millisecs -- )
	doc" Cause the selected data to flash red and green."
	{ ms }
	selected-sound sound? if
		data-red? @ if
			green
		else
			red
		then set-selected-data-color drop
		data-red? @ not data-red? !
		ms ms running-word fsd-cb in drop
	then
;
previous

\ ;;; --------  use loop info (if any) to set marks at loop points

: mark-loops ( -- )
	doc" Place marks at loop points found in the selected sound's header."
	#f snd-snd { snd }
	#f snd-chn { chn }
	snd sound-loop-info
	snd file-name mus-sound-loop-info || { loops }
	loops empty? if
		"%s has no loop info"
		    #( snd short-file-name ) string-format
		    snd status-report drop
		exit
	then
	loops 0 array-ref 0<>
	loops 1 array-ref 0<> || if
		loops 0 array-ref  snd chn undef undef add-mark drop
		loops 1 array-ref snd chn undef undef add-mark drop
		loops 2 array-ref 0<>
		loops 3 array-ref 0<> || if
			loops 2 array-ref  snd chn undef undef add-mark drop
			loops 3 array-ref snd chn undef undef add-mark drop
		then
	then
;

\ ;;; -------- mapping extensions
\ ;;; (map arbitrary single-channel function over various channel collections)

: do-all-chans <{ func :optional origin #f -- }>
	doc" Apply FUNC to all active channels, \
using ORIGIN as the edit history indication:\n\
lambda: <{ val -- val*2 }> val f2* ; \"double all samples\" do-all-chans."
	all-chans each { lst }
		lst 0 array-ref { snd }
		lst 1 array-ref { chn }
		func 0 #f snd chn #f origin map-channel drop
	end-each
;

: update-graphs ( -- )
	doc" Update (redraw) all graphs."
	all-chans each { lst }
		lst 0 array-ref  { snd }
		lst 1 array-ref { chn }
		snd chn update-time-graph drop
	end-each
;

: do-chans <{ func :optional origin #f -- }>
	doc" Apply FUNC to all sync'd channels \
using ORIGIN as the edit history indication."
	#f snd-snd sync { snc }
	snc 0> if
		all-chans each { lst }
			lst 0 array-ref  { snd }
			lst 1 array-ref { chn }
			snd sync snc = if
				func 0 #f snd chn #f origin map-channel drop
			then
		end-each
	else
		"sync not set" snd status-report drop
	then
;

: do-sound-chans <{ func :optional origin #f -- }>
	doc" Apply FUNC to all selected channels \
using ORIGIN as the edit history indication."
	selected-sound { snd }
	snd sound? if
		snd channels 0 ?do
			func 0 #f snd i ( chn ) #f origin map-channel drop
		loop
	else
		"no selected sound" snd status-report drop
	then
;

hide
: everys-cb { func -- prc; y self -- f }
	1 proc-create ( prc )
	func ,
  does> { y self -- f }
	self @ ( func ) #( y ) run-proc not
;
set-current

: every-sample? ( func -- f )
	doc" Return #t if FUNC is not #f for all samples in the \
current channel, otherwise it moves the cursor to the first offending sample."
	{ func }
	func everys-cb 0 #f #f #f #f scan-channel { baddy }
	baddy if
		baddy 1 array-ref #f #f #f set-cursor drop
	then
	baddy not
;
previous

hide
: sorts-cb { bins -- prc; y self -- f }
	1 proc-create ( prc )
	bins ,
  does> { y self -- f }
	self @ { bins }
	y fabs  bins length f* fround->s { bin }
	bins bin 1 object-set+!
	#f
;
set-current

: sort-samples ( nbins -- ary )
	doc" Provide histogram in BINS bins."
	{ nbins }
	nbins :initial-element 0 make-array { bins }
	bins sorts-cb 0 #f #f #f #f scan-channel drop
	bins
;
previous

\ ;;; -------- mix mono sound into stereo sound panning according to env

hide
: places1-cb { rd pos -- prc; y self -- val }
	1 proc-create ( prc )
	rd , pos ,
  does> { y self -- val }
	self @ { rd }
	self cell+ @ { pos }
	rd read-sample pos f* y f+
;

: places0-cb { rd pos -- prc; y self -- val }
	1 proc-create ( prc )
	rd , pos ,
  does> { y self -- val }
	self @ { rd }
	self cell+ @ { pos }
	rd read-sample 1.0 pos f- f* y f+
;

: places3-cb { rd en -- prc; y self -- val }
	1 proc-create ( prc )
	rd , en ,
  does> { y self -- val }
	self @ { rd }
	self cell+ @ { en }
	rd read-sample en env f* y f+
;

: places2-cb { rd en -- prc; y self -- val }
	1 proc-create ( prc )
	rd , en ,
  does> { y self -- val }
	self @ { rd }
	self cell+ @ { en }
	rd read-sample 1.0 en env f- f* y f+
;
set-current

: place-sound ( mono stereo pan -- res )
	doc" Mix a mono sound into a stereo sound, \
splitting it into two copies whose amplitudes depend on the envelope PAN-ENV.  \
If PAN-ENV is a number, the sound is split such that 0 is all in channel 0 \
and 90 is all in channel 1."
	{ mono stereo pan }
	mono #f #f #f framples { len }
	pan number? if
		pan 90.0 f/ { pos }
		0 mono #f 1 #f make-sampler { reader0 }
		0 mono #f 1 #f make-sampler { reader1 }
		reader1 pos places1-cb 0 len stereo 1 #f #f map-channel drop
		reader0 pos places0-cb 0 len stereo 0 #f #f map-channel drop
	else
		:envelope pan :length len make-env { e0 }
		:envelope pan :length len make-env { e1 }
		0 mono #f 1 #f make-sampler { reader0 }
		0 mono #f 1 #f make-sampler { reader1 }
		reader1 e1 places3-cb 0 len stereo 1 #f #f map-channel drop
		reader0 e0 places2-cb 0 len stereo 0 #f #f map-channel drop
	then
;
previous

\ ;;; -------- FFT-based editing

: fft-edit <{ bottom top :optional snd #f chn #f -- vct }>
	doc" Fft an entire sound, remove all energy \
below BOTTOM and all above TOP, then inverse fft."
	snd srate { sr }
	snd chn #f framples { len }
	2.0  len flog 2.0 flog f/ fceil  f** fround->s { fsize }
	0 fsize snd chn #f channel->vct { rdata }
	fsize 0.0 make-vct { idata }
	bottom sr fsize f/ f/ fround->s { lo }
	top    sr fsize f/ f/ fround->s { hi }
	rdata idata 1 fft drop
	lo 0> if
		rdata 0 0.0 vct-set! drop
		idata 0 0.0 vct-set! drop
		fsize 1- { jj }
		lo 1 ?do
			rdata i  0.0 vct-set! drop
			rdata jj 0.0 vct-set! drop
			idata i  0.0 vct-set! drop
			idata jj 0.0 vct-set! drop
			jj 1- to jj
		loop
	then
	hi fsize 2/ < if
		fsize hi - { jj }
		fsize 2/ hi ?do
			rdata i  0.0 vct-set! drop
			rdata jj 0.0 vct-set! drop
			idata i  0.0 vct-set! drop
			idata jj 0.0 vct-set! drop
			jj 1- to jj
		loop
	then
	rdata idata -1 fft drop
	"%s %s %s" #( bottom top get-func-name ) string-format { origin }
	rdata fsize 1/f vct-scale! 0 len 1- snd chn #f origin vct->channel
;

: fft-squelch <{ squelch :optional snd #f chn #f -- scl }>
	doc" Fft an entire sound, set all bins \
to 0.0 whose energy is below squelch, then inverse fft."
	snd chn #f framples { len }
	2.0  len flog 2.0 flog f/ fceil  f** fround->s { fsize }
	0 fsize snd chn #f channel->vct { rdata }
	fsize 0.0 make-vct { idata }
	fsize 2/ { fsize2 }
	rdata idata 1 fft drop
	rdata vct-copy { vr }
	idata vct-copy { vi }
	vr vi rectangular->polar drop
	vr vct-peak { scaler }
	squelch scaler f* { scl-squelch }
	rdata 0 vct-ref dup f*
	    idata 0 vct-ref dup f* f+ fsqrt scl-squelch f< if
		rdata 0 0.0 vct-set! drop
		idata 0 0.0 vct-set! drop
	then
	fsize 1- { jj }
	fsize 1 ?do
		rdata i vct-ref dup f*
		    idata i vct-ref dup f* f+ fsqrt scl-squelch f< if
			rdata i  0.0 vct-set! drop
			rdata jj 0.0 vct-set! drop
			idata i  0.0 vct-set! drop
			idata jj 0.0 vct-set! drop
		then
		jj 1- to jj
	loop
	rdata idata -1 fft drop
	"%s %s" #( squelch get-func-name ) string-format { origin }
	rdata fsize 1/f vct-scale! 0 len 1- snd chn #f origin vct->channel drop
	scaler
;

: fft-cancel <{ lo-freq hi-freq :optional snd #f chn #f -- vct }>
	doc" Fft an entire sound, set the bin(s) \
representing LO-FREQ to HI-FREQ to 0, then inverse fft."
	snd srate { sr }
	snd chn #f framples { len }
	2.0  len flog 2.0 flog f/ fceil  f** fround->s { fsize }
	0 fsize snd chn #f channel->vct { rdata }
	fsize 0.0 make-vct { idata }
	rdata idata 1 fft drop
	sr fsize f/ { hz-bin }
	lo-freq hz-bin f/ fround->s { lo-bin }
	hi-freq hz-bin f/ fround->s { hi-bin }
	fsize lo-bin - { jj }
	hi-bin 1+ lo-bin ?do
		rdata i  0.0 vct-set! drop
		rdata jj 0.0 vct-set! drop
		idata i  0.0 vct-set! drop
		idata jj 0.0 vct-set! drop
		jj 1- to jj
	loop
	rdata idata -1 fft drop
	"%s %s %s" #( lo-freq hi-freq get-func-name ) string-format { origin }
	rdata fsize 1/f vct-scale! 0 len 1- snd chn #f origin vct->channel
;

: make-ramp <{ :optional size 128 -- gen }>
	doc" Return ramp generator."
	vct( 0.0 size )
;

: ramp <{ gen up -- val }>
	doc" Is a kind of CLM generator that produces a ramp of a \
given length, then sticks at 0.0 or 1.0 until the UP argument changes."
	gen 0 vct-ref { ctr }
	gen 1 vct-ref { size }
	ctr size f/ { val }
	gen 0  ctr up if
		1
	else
		-1
	then f+ 0 fmax size fmin  vct-set! drop
	val
;

hide
: squelch-vowels-cb { snd chn -- prc; y self -- val }
	32 { fft-size }
	0 snd chn 1 #f make-sampler { read-ahead }
	1 proc-create { prc }
	fft-size 0.0 make-vct map!
		read-ahead #() apply
	end-map ( rl ) ,
	fft-size 0.0 make-vct ( im ) ,
	256 make-ramp ( ramper ) ,
	snd chn #f maxamp fft-size f2/ f/ ( peak ) ,
	read-ahead ,
	#f ( in-vowel ) ,
	prc
  does> { y self -- val }
	self           @ { rl }
	self   cell+   @ { im }
	self 2 cells + @ { ramper }
	self 3 cells + @ { peak }
	self 4 cells + @ { read-ahead }
	self 5 cells + @ { in-vowel }
	rl read-ahead #() apply cycle-set!
	rl cycle-start@ 0= if
		rl im 1 fft ( rl ) dup vct-multiply! drop
		im im vct-multiply! drop
		rl im 0 vct-add! drop
		rl 0 vct-ref
		    rl 1 vct-ref f+
		    rl 2 vct-ref f+
		    rl 3 vct-ref f+ peak f> to in-vowel
		in-vowel self 5 cells + ! ( in-vowel )
		im 0.0 vct-fill! drop
	then
	1.0  ramper in-vowel ramp  f- ( rval ) y f*
;
set-current

: squelch-vowels <{ :optional snd #f chn #f -- val }>
	doc" Suppress portions of a sound that look like steady-state."
	snd chn squelch-vowels-cb 0 #f snd chn #f get-func-name map-channel
;
previous

: fft-env-data <{ fft-env :optional snd #f chn #f -- vct }>
	doc" Apply FFT-ENV as spectral env to current sound, \
returning vct of new data."
	snd chn #f framples { len }
	2.0  len flog 2.0 flog f/ fceil  f** fround->s { fsize }
	0 fsize snd chn #f channel->vct { rdata }
	fsize 0.0 make-vct { idata }
	fsize 2/ { fsize2 }
	:envelope fft-env :length fsize2 make-env { e }
	rdata idata 1 fft drop
	e env { val }
	rdata 0 val object-set*!
	idata 0 val object-set*!
	fsize 1- { jj }
	fsize2 1 ?do
		e env to val
		rdata i  val object-set*!
		rdata jj val object-set*!
		idata i  val object-set*!
		idata jj val object-set*!
		jj 1- to jj
	loop
	rdata idata -1 fft ( rdata ) fsize 1/f vct-scale!
;

: fft-env-edit <{ fft-env :optional snd #f chn #f -- vct }>
	doc" Edit (filter) current chan using FFT-ENV."
	"%s %s" #( fft-env get-func-name ) string-format { origin }
	fft-env snd chn fft-env-data 0 snd chn #f framples 1-
	    snd chn #f origin vct->channel
;

: fft-env-interp <{ env1 env2 interp :optional snd #f chn #f -- vct }>
	doc" Interpolate between two fft-filtered \
versions (ENV1 and ENV2 are the spectral envelopes) \
following interp (an env between 0 and 1)."
	env1 snd chn fft-env-data { data1 }
	env2 snd chn fft-env-data { data2 }
	snd chn #f framples { len }
	:envelope interp :length len make-env { e }
	"%s %s %s %s" #( env1 env2 interp get-func-name )
	    string-format { origin }
	len 0.0 make-vct map!
		e env { pan }
		1.0 pan f- data1 i vct-ref f* data2 i vct-ref pan f* f+
	end-map ( new-data ) 0 len 1- snd chn #f origin vct->channel
;

: filter-fft <{ flt :optional normalize #t snd #f chn #f -- val }>
	doc" Get the spectrum of all the data in the given channel, \
apply the function FLT to it, then inverse fft.  \
FLT should take one argument, the current spectrum value.\n\
lambda: <{ y -- val }> y 0.01 f< if 0.0 else y then ; filter-fft\n\
is like fft-squelch."
	snd chn #f framples { len }
	snd chn #f maxamp { mx }
	2.0  len flog 2.0 flog f/ fceil  f** fround->s { fsize }
	fsize 2/ { fsize2 }
	0 fsize snd chn #f channel->vct { rdata }
	fsize 0.0 make-vct { idata }
	rdata rectangular-window fsize #t 1.0
	    #f ( in-place == #f ) normalize snd-spectrum { spect }
	rdata idata 1 fft drop
	flt #( spect 0 vct-ref ) run-proc drop
	fsize 1- { jj }
	fsize2 1 ?do
		spect i vct-ref { orig }
		flt #( orig ) run-proc { cur }
		orig fabs 0.000001 f> if
			cur orig f/ { scl }
			rdata i  scl object-set*!
			idata i  scl object-set*!
			rdata jj scl object-set*!
			idata jj scl object-set*!
		else
			cur fabs 0.000001 f> if
				cur 2.0 fsqrt f/ { scl }
				rdata i  scl vct-set! drop
				idata i  scl vct-set! drop
				rdata jj scl vct-set! drop
				idata jj scl fnegate vct-set! drop
			then
		then
		jj 1- to jj
	loop
	rdata idata -1 fft drop
	"<'> %s %s" #( flt get-func-name ) string-format { origin }
	mx f0<> if
		rdata vct-peak { pk }
		rdata mx pk f/ vct-scale!
	else
		rdata
	then 0 len 1- snd chn #f origin vct->channel
;
\ 0.5  0.5  make-one-zero filter-fft
\ 0.05 0.05 make-one-pole filter-fft
\ lambda: <{ y -- val }> y 0.1 f< if 0.0 else y then ; filter-fft
\ 0 0 0 1 0 make-sampler filter-fft
\ <'> contrast-enhancement filter-fft
\ lambda: <{ y -- val }> y y f* y f* ; filter-fft

: fft-smoother <{ cutoff start samps :optional snd #f chn #f -- val }>
	doc" Use fft-filtering to smooth a section:\n\
#f #f #f cursor value curs
0.1 curs 400 #f #f fft-smoother  curs 400 #f #f #f undef vct->channel."
	2.0  samps 1+ flog 2.0 flog f/ fceil  f** fround->s { fftpts }
	start fftpts snd chn #f channel->vct { rl }
	fftpts 0.0 make-vct { im }
	fftpts cutoff f* fround->s { top }
	rl 0 vct-ref { old0 }
	rl samps 1- vct-ref { old1 }
	rl vct-peak { oldmax }
	rl im 1 fft drop
	fftpts top ?do
		rl i 0.0 vct-set! drop
		im i 0.0 vct-set! drop
	loop
	rl im -1 fft ( rl ) fftpts 1/f vct-scale! drop
	rl vct-peak { newmax }
	newmax f0<> if
		oldmax newmax f/ 1.5 f> if
			rl oldmax newmax f/ vct-scale! drop
		then
		rl 0 vct-ref { new0 }
		rl samps 1- vct-ref { new1 }
		old0 new0 f- { offset0 }
		old1 new1 f- { offset1 }
		offset1 offset0 f= if
			0.0
		else
			offset1 offset0 f- samps f/
		then { incr }
		offset0 { trend }
		samps 0 ?do
			rl i trend object-set+!
			trend incr f+ to trend
		loop
	then
	rl
;

\ ;;; -------- comb-filter

: comb-filter ( scaler size -- prc; x self -- val )
	doc" Return comb-filter ready for map-channel etc: \
0.8 32 comb-filter map-channel.  \
If you're in a hurry use: 0.8 32 make-comb clm-channel instead."
	{ scaler size }
	scaler size make-comb { gen }
	1 proc-create ( prc )
	gen ,
  does> { x self -- val }
	self @ ( cmb ) x 0.0 comb
;

\ ;;; by using filters at harmonically related sizes, we can get chords:

: comb-chord ( scaler size amp -- prc; x self -- val )
	doc" Return set of harmonically-related comb \
filters: 0.95 100 0.3 comb-chord map-channel."
	{ scaler size amp }
	scaler size make-comb { c1 }
	scaler size 0.75 f* f>s make-comb { c2 }
	scaler size 1.2  f* f>s make-comb { c3 }
	1 proc-create ( prc )
	amp , c1 , c2 , c3 ,
  does> ( x self -- val )
	{ x self }
	self @ { amp }
	self 1 cells + @ { c1 }
	self 2 cells + @ { c2 }
	self 3 cells + @ { c3 }
	c1 x 0.0 comb c2 x 0.0 comb c3 x 0.0 comb f+ f+ amp f*
;

\ ;;; or change the comb length via an envelope:

: zcomb ( scaler size pm -- prc; x self -- val )
	doc" Return comb filter whose length varies according to an envelope:\n\
0.8 32 #( 0 0 1 10 ) zcomb map-channel."
	{ scaler size pm }
	:size size
	    :max-size pm 0.0 max-envelope 1.0 f+ size f+ fround->s
	    make-comb { gen }
	:envelope pm :length #f #f #f framples make-env { penv }
	1 proc-create ( prc )
	gen , penv ,
  does> { x self -- val }
	self @ ( gen ) x self cell+ @ ( penv ) env comb
;

: notch-filter ( scaler size -- prc; x self -- val )
	doc" Return notch-filter: 0.8 32 notch-filter map-channel."
	make-notch { gen }
	1 proc-create ( prc )
	gen ,
  does> { x self -- val }
	self @ ( gen ) x 0.0 notch
;

: formant-filter ( radius frequency -- prc; x self -- val )
	doc" Return formant generator: 2400 0.99 formant-filter map-channel.  \
Faster is: 2400 0.99 make-formant filter-sound."
	make-formant { gen }
	1 proc-create ( prc )
	gen ,
  does> { x self -- val }
	self @ ( gen ) x formant
;

: formants ( r1 f1 r2 f2 r3 f3 -- prc; x self -- val )
	doc" Return 3 formant filters in \
parallel: 0.99 900 0.98 1800 0.99 2700 formants map-channel."
	{ r1 f1 r2 f2 r3 f3 }
	f1 r1 make-formant { fr1 }
	f2 r2 make-formant { fr2 }
	f3 r3 make-formant { fr3 }
	1 proc-create ( prc )
	fr1 , fr2 , fr3 ,
  does> { x self -- val }
	self           @ ( fr1 ) x formant
	self 1 cells + @ ( fr2 ) x formant f+
	self 2 cells + @ ( fr3 ) x formant f+
;

: moving-formant ( radius move -- prc; x self -- val )
	doc" Return time-varying (in frequency) formant filter:\n\
0.99 #( 0 1200 1 2400 ) moving-formant map-channel."
	{ radius move }
	move 1 array-ref radius make-formant { frm }
	:envelope move :length #f #f #f framples make-env { menv }
	1 proc-create ( prc )
	frm , menv ,
  does> { x self -- val }
	self @ ( frm ) x formant ( val )
	self @ ( frm ) self cell+ @ ( menv ) env set-mus-frequency drop
	( val )
;

: osc-formants ( radius bases amounts freqs -- prc; x self -- val )
	doc" Return time-varying (in frequency) formant filter:\n\
0.99 #( 0 1200 1 2400 ) moving-formant map-channel."
	{ radius bases amounts freqs }
	bases vct-length { len }
	len make-array map!
		bases i vct-ref radius make-formant
	end-map { frms }
	len make-array map!
		freqs i vct-ref make-oscil
	end-map { oscs }
	1 proc-create ( prc )
	frms , amounts , oscs , bases ,
  does> { x self -- val }
	self           @ { frms }
	self 1 cells + @ { amounts }
	self 2 cells + @ { oscs }
	self 3 cells + @ { bases }
	0.0 ( val )
	frms each { frm }
		frm x formant f+ ( val += ... )
		frm bases i vct-ref
		    amounts i vct-ref
		    oscs i array-ref 0.0 0.0 oscil f* f+ set-mus-frequency drop
	end-each
	( val )
;

\ ;;; -------- echo

: echo ( scaler secs -- prc; y self -- val )
	doc" Return echo maker: 0.5 0.5 echo 0 44100 map-channel."
	{ scaler secs }
	secs #f srate f* fround->s make-delay { del }
	1 proc-create ( prc )
	del , scaler ,
  does> { y self -- val }
	self @ { del }
	self cell+ @ { scaler }
	del  del 0.0 tap y f+ scaler f*  0.0  delay y f+
;

: zecho ( scaler secs freq amp -- prc; y self -- val )
	doc" Return modulated echo maker: \
0.5 0.75 6 10.0 zecho 0 65000 map-channel."
	{ scaler secs freq amp }
	freq make-oscil { os }
	secs #f srate f* fround->s { len }
	:size len :max-size len amp f+ fround->s 1+ make-delay { del }
	1 proc-create ( prc )
	del , scaler , os , amp ,
  does> { y self -- val }
	self @ { del }
	self cell+ @ { scaler }
	self 2 cells + @ { os }
	self 3 cells + @ { amp }
	del  del 0.0 tap y f+ scaler f*  os 0.0 0.0 oscil amp f*  delay y f+
;

: flecho ( scaler secs -- prc; y self -- val )
	doc" Return low-pass filtered echo maker: \
0.5 0.9 flecho 0 75000 map-channel."
	{ scaler secs }
	:order 4 :xcoeffs vct( 0.125 0.25 0.25 0.125 ) make-fir-filter { flt }
	secs #f srate f* fround->s make-delay { del }
	1 proc-create ( prc )
	del , scaler , flt ,
  does> { y self -- val }
	self @ { del }
	self cell+ @ { scaler }
	self 2 cells + @ { flt }
	del  flt  del 0.0 tap y f+ scaler f*  fir-filter  0.0  delay y f+
;

\ ;;; -------- ring-mod and am
\ ;;;
\ ;;; CLM instrument is ring-modulate.ins

: ring-mod ( freq gliss-env -- prc; y self -- val )
	doc" Return time-varying ring-modulation filter:\n\
10 #( 0 0 1 100 hz->radians ) ring-mod map-channel."
	{ freq gliss-env }
	:frequency freq make-oscil { os }
	:envelope gliss-env :length #f #f #f framples make-env { genv }
	1 proc-create ( prc )
	os , genv ,
  does> { y self -- val }
	self @ { os }
	self cell+ @ { genv }
	os  genv env 0.0  oscil y f*
;

: am ( freq -- prc; y self -- val )
	doc" Return amplitude-modulator: 440 am map-channel."
	make-oscil { os }
	1 proc-create ( prc )
	os ,
  does> { y self -- val }
	1.0  y  self @ ( os ) 0.0 0.0 oscil  amplitude-modulate
;

\ ;;; this taken from sox (vibro.c)
: vibro ( speed depth -- prc; y self -- val )
	{ speed depth }
	speed make-oscil { sine }
	depth f2/ { scl }
	1.0 scl f- { offset }
	1 proc-create ( prc )
	sine , scl , offset ,
  does> { y self -- val }
	self @ { sine }
	self cell+ @ { scl }
	self 2 cells + @ { offset }
	sine 0.0 0.0 oscil scl f* offset f+ y f*
;

\ ;;; -------- hello-dentist
\ ;;;
\ ;;; CLM instrument version is in sndclm.html

hide
: hd-input-cb { in-data -- prc; dir self -- val }
	1 proc-create ( prc )
	in-data  , 0 ( idx ) ,
  does> { dir self -- val }
	self       @ { in-data }
	self cell+ @ { idx }
	in-data idx object-range? if
		in-data idx vct-ref
	else
		0.0
	then ( val )
	idx dir + self cell+ ! ( idx )
	( val )
;
set-current

: hello-dentist <{ frq amp :optional snd #f chn #f -- vct }>
	doc" Vary the sampling rate randomly, making a voice sound quavery:\n\
40.0 0.1 #f #f hello-dentist drop."
	:frequency frq :amplitude amp make-rand-interp { rn }
	snd chn #f framples { len }
	0 len snd chn #f channel->vct { in-data }
	:srate 1.0 :input in-data hd-input-cb make-src { rd }
	"%s %s %s" #( frq amp get-func-name ) string-format { origin }
	amp f2* 1.0 f+ len f* fround->s 0.0 make-vct map!
		rd  rn 0.0 rand-interp  #f src
	end-map ( out-data ) 0 len snd chn #f origin vct->channel
;
previous

\ ;;; a very similar function uses oscil instead of rand-interp, giving
\ ;;; various "Forbidden Planet" sound effects:

hide
: fp-input-cb { sf -- prc; dir self -- val }
	1 proc-create ( prc )
	sf ,
  does> { dir self -- val }
	self @ ( sf ) dir 0> if
		next-sample
	else
		previous-sample
	then
;
set-current

: fp <{ sr osamp osfrq :optional snd #f chn #f -- vct }>
	doc" Vary the sampling rate via an oscil: 1.0 0.3 20 #f #f fp."
	osfrq make-oscil { os }
	0 snd chn 1 #f make-sampler { sf }
	:srate sr :input sf fp-input-cb make-src { s }
	snd chn #f framples { len }
	"%s %s %s %s" #( sr osamp osfrq get-func-name ) string-format { origin }
	len 0.0 make-vct map!
		s os 0.0 0.0 oscil osamp f* #f src
	end-map ( out-data ) 0 len snd chn #f origin vct->channel ( vct )
	sf free-sampler drop
;
previous

vct( -1.000 -0.960 -0.900 -0.820
     -0.720 -0.600 -0.450 -0.250 
      0.000  0.250  0.450  0.600
      0.720  0.820  0.900  0.960 1.000 ) constant compand-table

: compand ( -- prc; y self -- val )
	doc" Return compander: compand map-channel."
	1 proc-create ( prc )
  does> { y self -- val }
	compand-table  y 8.0 f* 8.0 f+ compand-table vct-length  array-interp
;

\ ;;; -------- shift pitch keeping duration constant
\ ;;;
\ ;;; both src and granulate take a function argument to get input
\ ;;;   whenever it is needed.
\ ;;;   In this case, src calls granulate which reads the currently
\ ;;;   selected file.  CLM version is in expsrc.ins

\ general reader for expsrc, expsnd, cnvtest
: sampler-cb { rd -- prc; dir self -- val }
	1 proc-create ( prc )
	rd ,
  does> { dir self -- val }
	self @ ( rd ) next-sample
;

hide
: expsr-cb { gr -- prc; dir self -- val }
	1 proc-create ( prc )
	gr ,
  does> { dir self -- val }
	self @ ( gr ) #f #f granulate
;
set-current

: expsrc { rate snd chn -- prc; y self -- val }
	doc" Use sampling-rate conversion and granular synthesis to \
produce a sound at a new pitch but at the original tempo.  \
It returns a function for map-channel."
	0 snd chn 1 #f make-sampler { rd }
	:input rd sampler-cb :expansion rate make-granulate { gr }
	:input gr expsr-cb :srate rate make-src { sr }
	1 proc-create ( prc )
	sr ,
  does> { y self -- val }
	self @ ( sr ) 0.0 #f src
;

\ 0.5       expsrc-test		- current sound and channel
\ 2.0 #f #t expsrc-test		- current sound and all channels
\ 2.0  0  0 expsrc-test		- sound 0 and channel 0
: expsrc-test <{ rate :optional snd #f chn #f start 0 dur #f -- }>
	"%s %s %s expsrc %s %s map-channel"
	'( rate snd chn start dur ) string-format { origin }
	chn true? if
		snd channels 0 do
			rate snd i expsrc start dur snd i #f origin
			    map-channel drop
		loop
	else
		rate snd chn expsrc start dur snd chn #f origin map-channel drop
	then
;
previous

\ ;;; the next (expsnd) changes the tempo according to an envelope; the new
\ ;;; duration will depend on the expansion envelope -- we integrate it to get
\ ;;; the overall expansion, then use that to decide the new length.

: expsnd { gr-env snd chn -- vct }
	doc" Use the granulate generator to change tempo \
according to an envelope:\n\
'( 0 0.5 2 2.0 ) #f #f expsnd."
	snd chn #f framples { len }
	len snd srate f/ gr-env integrate-envelope f*
	    gr-env envelope-last-x f/ { dur }
	0 snd chn 1 #f make-sampler { rd }
	:expansion gr-env cadr
	    :jitter 0
	    :input rd sampler-cb make-granulate { gr }
	:envelope gr-env :duration dur make-env { ge }
	snd srate dur f* fround->s { sound-len }
	sound-len len max to len
	"%s %s" #( gr-env get-func-name ) string-format { origin }
	len 0.0 make-vct map!
		gr #f granulate ( val )
		gr ge env set-mus-increment drop
	end-map ( out-data ) 0 len snd chn #f origin vct->channel ( vct )
	rd free-sampler drop
;

\ '( 0 0.5 2 2.0 )       expsnd-test	- current sound and channel
\ '( 0 0.5 2 2.0 ) #f #t expsnd-test	- current sound and all channels
\ '( 0 0.5 2 2.0 )  0  0 expsnd-test	- sound 0 and channel 0
: expsnd-test <{ gr-env :optional snd #f chn #f -- }>
	chn true? if
		snd channels 0 do
			gr-env snd i expsnd drop
		loop
	else
		gr-env snd chn expsnd drop
	then
;

\ ;;; -------- cross-synthesis
\ ;;;
\ ;;; CLM version is in sndclm.html

: cross-synthesis ( cross-snd amp fftsize r -- prc; y self -- val )
	doc" Do cross-synthesis between CROSS-SND (a sound index) \
and the currently selected sound:\n\
1 0.5 128 6.0 cross-synthesis map-channel."
	{ cross-snd amp fftsize r }
	fftsize 2/ { freq-inc }
	fftsize 0.0 make-vct { fdr }
	fftsize 0.0 make-vct { fdi }
	freq-inc 0.0 make-vct { spectr }
	1.0 r fftsize f/ f- { radius }
	mus-srate { osr }
	#f srate { csr }
	csr fftsize / { bin }
	\ The comment from examp.scm applies of course here as well:
	\ ;; if mus-srate is 44.1k and srate is 48k, make-formant
	\ ;;    thinks we're trying to go past srate/2
	\ ;;    and in any case it's setting its formants incorrectly
	\ ;;    for the actual output srate
	\ begin of temporary mus-srate
	csr set-mus-srate drop
	freq-inc make-array map!
		:radius radius :frequency bin i * make-formant
	end-map ( formants ) spectr make-formant-bank { formants }
	osr set-mus-srate drop
	\ end of temporary mus-srate
	1 proc-create ( prc )
	fdr , fdi , spectr , formants ,
	amp , freq-inc , cross-snd , fftsize , 0 ,
  does> { y self -- val }
	self           @ { fdr }
	self    cell+  @ { fdi }
	self 2 cells + @ { spectr }
	self 3 cells + @ { formants }
	self 4 cells + @ { amp }
	self 5 cells + @ { ctr }
	self 6 cells + @ { cross-snd }
	self 7 cells + @ { fftsize }
	self 8 cells + @ { inctr }
	fftsize 2/ { freq-inc }
	ctr freq-inc = if
		inctr fftsize cross-snd 0 #f channel->vct dup self ! to fdr
		inctr freq-inc + self 8 cells + ! ( inctr += freq-inc )
		fdr fdi #f 2 spectrum ( fdr )
		    spectr vct-subtract! ( fdr )
		    fftsize 2/ 1/f vct-scale! drop
		0 self 5 cells + ! ( ctr = 0 )
	then
	ctr 1+ self 5 cells + ! ( ctr++ )
	spectr fdr 0 vct-add! drop
	formants y formant-bank amp f* ( val )
;

: voiced->unvoiced <{ amp fftsize r tempo :optional snd #f chn #f -- vct }>
	doc" Turn a vocal sound into whispering: \
1.0 256 2.0 2.0 #f #f voiced->unvoiced."
	fftsize 2/ { freq-inc }
	snd chn #f framples { len }
	len tempo f/ fround->s { out-len }
	nil { fdr }
	fftsize 0.0 make-vct { fdi }
	freq-inc 0.0 make-vct { spectr }
	mus-srate { osr }
	snd srate { csr }
	\ See cross-synthesis above.
	\ begin of temporary mus-srate
	csr set-mus-srate drop
	csr fftsize / { bin }
	1.0 r fftsize f/ f- { radius }
	freq-inc make-array map!
		:radius radius :frequency bin i * make-formant
	end-map ( formants ) spectr make-formant-bank { formants }
	osr set-mus-srate drop
	\ end of temporary mus-srate
	csr 3.0 f/ make-rand { noi }
	0 0 { ctr inctr }
	freq-inc tempo f* fround->s { hop }
	0.0 { old-peak-amp }
	2.0 freq-inc f/ { 2/freq-inc }
 	len out-len max 0.0 make-vct { out-data }
	out-len 0 do
		out-len i - freq-inc min to ctr
		ctr odd? if ctr -1 to ctr then
		inctr fftsize snd chn #f channel->vct to fdr
		fdr vct-peak old-peak-amp fmax to old-peak-amp
		fdr fdi #f 2 spectrum ( fdr )
		    spectr vct-subtract! ( fdr )
		    2/freq-inc vct-scale! drop
		hop inctr + to inctr
		i { idx }
		ctr 0 ?do
			spectr fdr 0 vct-add! drop
			out-data idx
			    formants noi 0.0 rand formant-bank
			    vct-set! drop
			out-data idx 1+
			    formants noi 0.0 rand formant-bank
			    vct-set! drop
			idx 2 + to idx
		2 +loop
	freq-inc +loop
	"%s %s %s %s %s"
	    #( amp fftsize r tempo get-func-name ) string-format { origin }
	out-data old-peak-amp out-data vct-peak f/ amp f* vct-scale! ( odata )
 	    0 len out-len max snd chn #f origin vct->channel ( odata )
;

: pulse-voice
    <{ co :optional freq 440.0 amp 1.0 fftsize 256 r 2.0 snd #f chn #f -- vct }>
	doc" Use ncos to manipulate speech sounds."
	fftsize 2/ { freq-inc }
	freq-inc 0.0 make-vct { spectr }
	snd chn #f framples { len }
	mus-srate { osr }
	snd srate { csr }
	\ See cross-synthesis above.
	\ begin of temporary mus-srate
	csr set-mus-srate drop
	csr fftsize / { bin }
	1.0 r fftsize f/ f- { radius }
	freq-inc make-array map!
		:radius radius :frequency bin i * make-formant
	end-map ( formants ) spectr make-formant-bank { formants }
	osr set-mus-srate drop
	\ end of temporary mus-srate
	0.0 { old-peak-amp }
	freq co make-ncos { pulse }
	nil { fdr }
	0 { inctr }
	fftsize 0.0 make-vct { fdi }
	freq-inc 1/f { 1/freq-inc }
	len 0.0 make-vct map!
		i freq-inc mod 0= if
			inctr fftsize snd chn #f channel->vct to fdr
			fdr vct-peak old-peak-amp fmax to old-peak-amp
			fdr fdi #f 2 spectrum ( fdr )
			    spectr vct-subtract! ( fdr )
			    1/freq-inc vct-scale! drop
			freq-inc inctr + to inctr
		then
		spectr fdr 0 vct-add! drop
		formants pulse 0.0 ncos formant-bank ( outval )
	end-map { out-data }
	"%s %s %s %s %s %s"
	    #( co freq amp fftsize r get-func-name ) string-format { origin }
	out-data old-peak-amp out-data vct-peak f/ amp f* vct-scale! ( odata )
	    0 len snd chn #f origin vct->channel ( vct )
;
\ 80   20.0 1.0 1024 0.01 pulse-voice
\ 80  120.0 1.0 1024 0.2  pulse-voice
\ 30  240.0 1.0 1024 0.1  pulse-voice
\ 30  240.0 1.0 2048      pulse-voice
\  6 1000.0 1.0  512      pulse-voice

'snd-nogui provided? [unless]
	\ ;;; -------- convolution example

	: cnvtest ( snd0 snd1 amp -- mx )
		doc" Convolve SND0 and SND1, scaling by AMP, \
returns new max amp: 0 1 0.1 cnvtest."
		{ snd0 snd1 amp }
		snd0 #f #f framples { flt-len }
		snd1 #f #f framples flt-len + { total-len }
		0 snd1 0 1 #f make-sampler { rd }
		:input rd sampler-cb :filter 0 flt-len
		    snd0 #f #f channel->vct make-convolve { cnv }
		total-len 0.0 make-vct map!
			cnv #f convolve
		end-map amp vct-scale! ( out-data )
		0 total-len snd1 #f #f #f vct->channel vct-peak { max-samp }
		rd free-sampler drop
		max-samp 1.0 f> if
			#( max-samp fnegate max-samp ) snd1 #f set-y-bounds drop
		then
		max-samp
	;
[then]

\ ;;; -------- swap selection chans

: swap-selection-channels ( -- )
	doc" Swap the currently selected data's channels."
	undef selection? unless
		'no-active-selection #( get-func-name ) fth-throw
	then
	selection-chans 2 = if
		selection-position { beg }
		selection-framples { len }
		#f { snd-chn0 }
		#f { snd-chn1 }
		all-chans each { lst }
			lst car lst cadr selection-member? if
				snd-chn0 false? if
					lst to snd-chn0
				else
					snd-chn1 false? if
						lst to snd-chn1
						leave
					then
				then
			then
		end-each
		snd-chn1 if
			snd-chn0 car snd-chn0 cadr snd-chn1 car snd-chn1 cadr
			    beg len #f #f swap-channels drop
		else
			'wrong-number-of-channels
			    #( "%s: needs two channels to swap"
			       get-func-name ) fth-throw
		then
	else
		'wrong-number-of-channels
		    #( "%s: needs a stereo selection (not %s chans)"
		       get-func-name
		       selection-chans ) fth-throw
	then
;

\ ;;; -------- sound interp
\ ;;;
\ ;;; make-sound-interp sets up a sound reader that reads a channel at an
\ ;;;    arbitary location, interpolating between samples if necessary,
\ ;;;    the corresponding "generator" is sound-interp

: make-sound-interp <{ start :optional snd #f chn #f -- prc; loc self -- val }>
	doc" Return an interpolating reader for SND's channel CHN."
	0 #f snd chn #f channel->vct { data }
	1 proc-create ( prc )
	data , data vct-length ,
  does> { loc self -- val }
	self @ ( data ) loc self cell+ @ ( size ) array-interp
;

: sound-interp { func loc -- val }
	doc" Return sample at LOC (interpolated if necessary) \
from FUNC created by make-sound-interp."
	loc func execute
;

\ ;; env-sound-interp takes an envelope that goes between 0 and 1
\ ;;   (y-axis), and a time-scaler (1.0 = original length) and returns a
\ ;;   new version of the data in the specified channel that follows that
\ ;;   envelope (that is, when the envelope is 0 we get sample 0, when the
\ ;;   envelope is 1 we get the last sample, envelope = .5 we get the middle
\ ;;   sample of the sound and so on. (env-sound-interp #(0 0 1 1)) will
\ ;;   return a copy of the current sound; (env-sound-interp #(0 0 1 1 2 0)
\ ;;   2.0) will return a new sound with the sound copied first in normal
\ ;;   order, then reversed.  src-sound with an envelope could be used
\ ;;   for this effect, but it is much more direct to apply the envelope
\ ;;   to sound sample positions.

: env-sound-interp <{ en :optional time-scale 1.0 snd #f chn #f -- vct }>
	doc" Read SND's channel CHN according to EN and TIME-SCALE."
	snd chn #f framples { len }
	time-scale len f* fround->s { newlen }
	:envelope en :length newlen 1+ :scaler len make-env { read-env }
	0 #f snd chn #f channel->vct { data }
	newlen 0.0 make-vct map!
		data read-env env len array-interp
	end-map { new-snd }
	"%s %s %s" #( en time-scale get-func-name ) string-format { origin }
	0 newlen new-snd snd chn #t origin
	    0 current-edit-position #t set-samples ( vct )
;
\ #( 0 0 1 1 2 0 ) 2.0 env-sound-interp

: granulated-sound-interp
  <{ en :optional tscl 1.0 grlen 0.1 gren #f ohop 0.05 snd #f chn #f -- vct }>
	doc" Read the given channel following EN (as env-sound-interp), \
using grains to create the re-tempo'd read."
	gren empty? if
		#( 0 0 1 1 2 1 3 0 ) to gren
	then
	snd chn #f framples { len }
	tscl len f* fround->s { newlen }
	:envelope en :length newlen :scaler len make-env { read-env }
	snd srate { sr }
	grlen sr f* fround->s { grain-framples }
	ohop sr f* fround->s { hop-framples }
	grlen ohop f/ fceil->s { num-readers }
	0 0 { cur-readers next-reader }
	sr 0.005 f* { jitter }
	num-readers make-array { readers }
	num-readers make-array map!
		:envelope gren :length grain-framples make-env
	end-map { grain-envs }
	newlen 0.0 make-vct { new-snd }
	newlen 0 ?do
		read-env i set-mus-location drop
		read-env env { position-in-original }
		position-in-original jitter mus-random f+ fround->s 0 max { mx }
		mx snd chn 1 #f make-sampler { srd }
		readers srd cycle-set!
		grain-envs next-reader array-ref mus-reset drop
		readers cycle-start@ to next-reader
		cur-readers next-reader < if
			next-reader to cur-readers
		then
		cur-readers 0 ?do
			grain-envs i array-ref { e }
			readers i array-ref { rd }
			\ j is index from outer loop
			newlen hop-framples j + min j ?do
				new-snd i e env rd next-sample f* vct-set! drop
			loop
		loop
	hop-framples +loop
	"%s %s %s %s %s %s"
	    #( en tscl grlen gren ohop get-func-name ) string-format { origin }
	0 newlen new-snd snd chn #t origin
	    0 current-edit-position #t set-samples ( vct )
;

\ #( 0 0 1 .1 2 1 ) 1.0 0.2 #( 0 0 1 1 2 0 )      granulated-sound-interp
\ #( 0 0 1 1 ) 2.0                                granulated-sound-interp
\ #( 0 0 1 .1 2 1 ) 1.0 0.2 #( 0 0 1 1 2 0 ) 0.02 granulated-sound-interp

\ ;;; -------- filtered-env 

hide
: fe-cb { flt amp-env -- prc; y self -- val }
	1 proc-create ( prc )
	flt , amp-env ,
  does> { y self -- val }
	self @ { flt }
	self cell+ @ ( amp-env ) env { env-val }
	flt 0 env-val        set-mus-xcoeff drop
	flt 1 env-val 1.0 f- set-mus-xcoeff drop
	flt env-val y f* one-pole
;
set-current

: filtered-env <{ e :optional snd #f chn #f -- val }>
	doc" It's a time-varying one-pole filter: \
when env is at 1.0, no filtering, as env moves to 0.0, \
low-pass gets more intense; \
amplitude and low-pass amount move together."
	1.0 0.0 make-one-pole { flt }
	:envelope e :length snd chn #f framples make-env { amp-env }
	flt amp-env fe-cb  0 #f snd chn #f
	    "%s %s" #( e get-func-name ) string-format map-channel
;
previous

\ ;;; -------- C-x b support: hide all but one of the current sounds
\ ;;;  (more like Emacs)

hide
#f value xb-last-buffer
#f value xb-current-buffer
0  value xb-last-width
0  value xb-last-height
set-current

: open-current-buffer { width heigth -- }
	width  to xb-last-width
	heigth to xb-last-height
	xb-current-buffer 0 array-ref sound-widgets 0 array-ref { sound-pane }
	sound-pane if
		sound-pane show-widget drop
		sound-pane #( width heigth ) set-widget-size drop
		xb-current-buffer 0 array-ref select-sound drop
		xb-current-buffer 1 array-ref select-channel drop
	then
;

: close-all-buffers ( -- )
	sounds each ( s )
		sound-widgets 0 array-ref hide-widget drop
	end-each
;
previous

\ ;;; -------- remove-clicks 

: find-click ( loc -- pos )
	doc" Find the next click starting at LOC."
	{ loc }
	loc #f #f 1 #f make-sampler { rd }
	0.0 0.0 0.0 { samp0 samp1 samp2 }
	10 0.0 make-vct { samps }
	#f 					\ flag
	#f #f #f framples loc ?do
		samp1 to samp0
		samp2 to samp1
		rd next-sample to samp2
		samps samp0 cycle-set!
		samps vct-peak 0.1 fmax { local-max }
		samp0 samp1 f- fabs local-max f>
		samp1 samp2 f- fabs local-max f>     &&
		samp0 samp2 f- fabs local-max f2/ f< && if
			drop ( flag )
			i
			leave
		then
	loop
;

: remove-clicks ( -- )
	doc" Try to find and smooth-over clicks."
	-2 { click }
	begin
		click 2+ find-click to click
		click
	while
		click 2- 4 #f #f smooth-sound drop
	repeat
;

: search-for-click ( -- prc; val self -- pos )
	doc" Look for the next click (for use with C-s)."
	1 proc-create ( prc )
	10 0.0 make-vct , 0.0 , 0.0 , 0.0 ,
  does> { val self -- pos }
	self @ { samps }
	self cell+ @ { samp0 }
	self 2 cells + @ { samp1 }
	self 3 cells + @ { samp2 }
	samp1 to samp0
	samp2 to samp1
	val   to samp2
	samp0 self cell+ !
	samp1 self 2 cells + !
	samp2 self 3 cells + !
	samps samp0 cycle-set!
	samps vct-peak 0.1 fmax { local-max }
	samp0 samp1 f- fabs local-max f>=
	samp1 samp2 f- fabs local-max f>=     &&
	samp0 samp2 f- fabs local-max f2/ f<= && if
		-1
	else
		#f
	then
;

: zero+ ( -- prc; n self -- val )
	doc" Find the next positive-going zero crossing (if \
searching forward) (for use with C-s)."
	1 proc-create ( prc )
	0.0 ( lastn ) ,
  does> { n self -- val }
	self @ ( lastn ) f0< n f0>= && -1 && ( val )
	n self ! ( lastn = n )
	( val )
;

: next-peak ( -- prc; n self -- val )
	doc" Find the next max or min point in the \
time-domain waveform (for use with C-s)."
	1 proc-create ( prc )
	( last0 ) #f , ( last1 ) #f ,
  does> { n self -- val }
	self       @ { last0 }
	self cell+ @ { last1 }
	last0 number?
	last0 last1 f< last1 n f> &&
	last0 last1 f> last1 n f< && || &&
	-1 && ( val )
	last1 self !   ( last0 = last1 )
	n self cell+ ! ( last1 = n )
	( val )
;

: find-pitch ( pitch -- prc; y self -- val )
	doc" Find the point in the current sound \
where PITCH (in Hz) predominates:\n\
C-s 300 find-pitch\n\
In most cases, this will be slightly offset from the true \
beginning of the note."
	{ pitch }
	1 proc-create ( prc )
	#f #f transform-size 0.0 make-vct , pitch ,
  does> { y self -- val }
	self @ { data }
	self cell+ @ { pitch }
	data y cycle-set!
	data cycle-start@ 0= if
		data vct-peak 0.001 f> if
			data rectangular-window data length
			    #t 0.0 undef #t snd-spectrum { spectr }
			10.0 flog { log10 }
			0.0 0 { pk pkloc }
			data length 2/ 0 ?do
				spectr i vct-ref dup pk f> if
					( val ) to pk
					i to pkloc
				else
					( val ) drop
				then
			loop
			pkloc 0> if
				spectr pkloc 1- vct-ref { la }
				spectr pkloc    vct-ref { ca }
				spectr pkloc 1+ vct-ref { ra }
				la ca fmax ra fmax 0.001 f* { pk1 }
				la 0.0000001 fmax pk1 f/ flog log10 f/ { logla }
				ca 0.0000001 fmax pk1 f/ flog log10 f/ { logca }
				ra 0.0000001 fmax pk1 f/ flog log10 f/ { logra }
				logla logra f- f2/
				    logla logra f+  logca f2*  f- f/
			else
				0.0
			then pkloc f+ #f srate f* data length f/ { pit }
			pitch pit f- fabs #f srate data length f2* f/ f< if
				data length 2/ negate
			else
				#f
			then ( val )
		then
		data 0.0 vct-fill! drop
	else
		#f
	then
;

\ ;;; -------- file->vct and a sort of cue-list, I think

[ifundef] file->vct
	: file->vct ( file -- vct )
		doc" Return a vct with FILE's data."
		{ file }
		file find-file to file
		file unless
			'no-such-file #( "%s: %S" get-func-name file ) fth-throw
		then
		0 file undef 1 #f make-sampler { reader }
		file mus-sound-framples 0.0 make-vct map!
			reader next-sample
		end-map ( data )
		reader free-sampler drop
	;
[then]

hide
: an-cb ( notes snd chn -- prc; self -- #f )
	{ notes snd chn }
	0 proc-create ( prc )
	notes , snd , chn ,
  does> { self -- #f }
	self           @ { notes }
	self   cell+   @ { snd }
	self 2 cells + @ { chn }
	snd chn #f cursor { start }
	notes each { note }
		note 0 array-ref find-file { file }
		file unless
			'no-such-file #( "add-notes: %S" file ) fth-throw
		then
		note length 1 > if
			note 1 array-ref
		else
			0.0
		then { offset }
		note length 2 > if
			note 2 array-ref
		else
			1.0
		then { amp }
		snd srate offset f* fround->s start + { beg }
		amp 1.0 f<> if
			file file->vct amp vct-scale!
			    beg snd chn #f "add-notes" mix-vct
		else
			file beg 0 snd chn #f undef mix
		then drop
	end-each
	#f
;
set-current

: add-notes <{ notes :optional snd #f chn #f -- #f }>
	doc" Add (mix) NOTES which is a list of lists of the form:\n\
file :optional offset 0.0 amp 1.0\n\
starting at the cursor in the currently selected channel:\n\
#( #( \"oboe.snd\" ) #( \"pistol.snd\" 1.0 2.0 ) ) add-notes."
	notes snd chn an-cb
	    "%s %s" #( notes get-func-name ) string-format as-one-edit
;
previous

hide
: rpl-cb { reg -- prc; self -- val }
	0 proc-create ( prc )
	reg ,
  does> { self -- val }
	self @ ( reg ) play
;
set-current

: region-play-list ( data -- )
	doc" DATA is list of lists #( #( time reg ) ... ), TIME in secs, \
setting up a sort of play list:\n\
#( #( 0.0 0 ) #( 0.5 1 ) #( 1.0 2 ) #( 1.0 0 ) ) region-play-list."
	( data ) each { tone }
		tone 0 array-ref 1000.0 f* fround->s { time }
		tone 1 array-ref { region }
		region region? if
			time region rpl-cb in drop
		then
	end-each
;
previous

: region-play-sequence ( data -- )
	doc" DATA is list of region ids which will be played \
one after the other:\n\
#( 0 2 1 ) region-play-sequence."
	0.0 { time }
	( data ) map
		*key* { id }
		time { cur }
		id 0 region-framples id region-srate f/ time f+ to time
		#( cur id )
	end-map region-play-list
;

\ ;;; -------- replace-with-selection

: replace-with-selection ( -- )
	doc" Replace the samples from the cursor with the current selection."
	#f #f #f cursor { beg }
	#f #f selection-framples { len }
	beg #f #f insert-selection drop
	beg len + len #f #f #f delete-samples drop
;

\ ;;; -------- explode-sf2

: explode-sf2 ( -- )
	doc" Turn the currently selected soundfont file into \
a bunch of files of the form sample-name.aif."
	#f soundfont-info { lst }
	lst length 1- { last }
	lst each { vals }
		\ #( name start loop-start loop-end )
		vals 0 array-ref { name }
		vals 1 array-ref { start }
		i last < if
			lst i 1+ array-ref 1 array-ref
		else
			#f #f #f framples
		then { end }
		vals 2 array-ref start d- { loop-start }
		vals 3 array-ref start d- { loop-end }
		name ".aif" $+ { filename }
		undef selection? if
			#f #t #f set-selection-member? drop
		then
		#t #f #f set-selection-member? drop
		start #f #f set-selection-position drop
		end start d- #f #f set-selection-framples drop
		:file filename :header-type mus-aifc save-selection drop
		filename open-sound { temp }
		temp #( loop-start loop-end ) set-sound-loop-info drop
		temp close-sound drop
	end-each
;

\ ;;; -------- open-next-file-in-directory

hide
#f value nd-last-file-opened		\ string
#f value nd-current-directory		\ string
#f value nd-current-sorted-files	\ array

: gcf-sort-cb <{ a b -- n }>
	a b string< if
		-1
	else
		a b string> if
			1
		else
			0
		then
	then
;

: get-current-files { dir -- }
	dir to nd-current-directory
	dir sound-files-in-directory
	    <'> gcf-sort-cb sort to nd-current-sorted-files
;

: get-current-directory <{ filename -- filename }>
	filename to nd-last-file-opened
	filename mus-expand-filename file-dirname { new-path }
	nd-current-directory new-path string<> if
		new-path get-current-files
	then
	filename
;
set-current

: open-next-file-in-directory ( -- f )
	\ open-hook <'> get-current-directory hook-member? unless
	\     open-hook <'> get-current-directory add-hook!
	\ then
	nd-last-file-opened string? not
	sounds nil? not && if
		#f snd-snd file-name to nd-last-file-opened
	then
	nd-current-directory string? unless
		sounds nil? if
			file-pwd
		else
			nd-last-file-opened file-dirname
		then get-current-files
	then
	nd-current-sorted-files empty? if
		'no-such-file
		    #( "%s: %s" get-func-name nd-current-directory ) fth-throw
	else
		nd-current-sorted-files cycle-ref { next-file }
		next-file 0 find-sound if
			'file-already-open
			    #( "%s: %s" get-func-name next-file ) fth-throw
		else
			sounds nil? unless
				#f snd-snd close-sound drop
			then
			next-file find-file dup if
				open-sound
			then drop
		then
	then
	#t
;
previous

hide
: mouse-click-to-open-cb <{ snd chn button state x y axis -- f }>
	button 2 = if
		open-next-file-in-directory
	else
		#f
	then
;
set-current

: click-middle-button-to-open-next-file-in-directory ( -- )
	mouse-click-hook <'> mouse-click-to-open-cb add-hook!
	open-hook <'> get-current-directory add-hook!
;
previous

\ ;;; -------- chain-dsps

instrument: chain-dsps <{ start dur :optional dsps #() -- }>
	dsps map
		*key* array? if
			:envelope *key* :duration dur make-env
		else
			*key*
		then
	end-map { dsp-chain }
	start dur nil run-instrument
		0.0 { val }
		dsp-chain each { gen }
			gen env? if
				gen env val f*
			else
				gen readin? if
					gen readin val f+
				else
					gen mus-generator? if
						gen val 0.0 mus-apply
					else
						gen #( val ) run-proc
					then
				then
			then to val
		end-each
		val
	end-run
;instrument

hide
: cdsps-cb { os1 os2 -- prc; val self -- r }
	1 proc-create ( prc )
	os1 , os2 ,
  does> { val self -- r }
	self       @ ( osc1 ) val     0.0 oscil
	self cell+ @ ( osc2 ) val f2* 0.0 oscil f+
;
set-current

0 [if]
	lambda: ( -- )
		440.0 make-oscil { os1 }
		0 1.0  #( #( 0 0 1 1 2 0 ) os1 ) chain-dsps
		0.5 make-one-zero { oz }
		"oboe.snd" find-file make-readin { rd }
		0 1.0   #( #( 0 0 1 1 2 0 ) oz rd  ) chain-dsps
		220 make-oscil { osc1 }
		440 make-oscil { osc2 }
		osc1 osc2 cdsps-cb { cb }
		0 1.0 #( #( 0 0 1 1 2 0 ) cb ) chain-dsps
	; with-sound
[then]
previous

\ ;;; -------- re-order channels 

: scramble-channels ( new-order -- )
	\ ;; (scramble-channels 3 2 0 1) means chan 3 goes to 0, etc
	{ end-chans }
	end-chans length { len }
	len 1 > if
		end-chans map
			i
		end-map { cur-chans }
		end-chans each { end-chan }
			cur-chans i array-ref { cur-chan }
			end-chan cur-chan <> if
				#f cur-chans each { chn }
					chn end-chan = if
						drop ( #f )
						i
						leave
					then
				end-each { end-loc }
				#f end-loc #f i 0 len #f swap-channels drop
				cur-chans end-loc cur-chan array-set!
				cur-chans i end-chan array-set!
			then
		end-each
	then
;

hide
: sc-scan-cb { buffer silence in-silence edges samp -- prc; y self -- #f }
	1 proc-create ( prc )
	buffer , silence , in-silence , edges , samp ,
  does> { y self -- #f }
	self @ ( buffer ) y y f* moving-average { sum-of-squares }
	sum-of-squares self cell+ @ ( silence ) f< { now-silent }
	self 2 cells + @ ( in-silence ) now-silent equal? unless
		self 3 cells + @ ( edges )
		    self 4 cells + @ ( samp ) array-push drop
	then
	now-silent self 2 cells + ! ( in-silence = now-silent )
	1 self 4 cells + +! ( samp++ )
	#f
;

: sc-edit-cb { pieces -- prc; self -- val }
	0 proc-create ( prc )
	pieces , 0 ( start ) ,
  does> { self -- val }
	self @ { pieces }
	self cell+ @ { start }
	0.0 { scale-by }
	pieces length { len }
	len 0 ?do
		len random fround->s { this }
		pieces this array-ref { reg }
		pieces this #f array-set!
		reg unless
			len this 1+ ?do
				pieces i array-ref dup if
					to reg
					pieces i #f array-set!
					leave
				then
			loop
			reg unless
				0 this 1- ?do
					pieces i array-ref dup if
						to reg
						pieces i #f array-set!
						leave
					then
				-1 +loop
			then
		then
		reg start #f #f 0 mix-region drop
		reg 0 region-framples start + to start
		reg forget-region drop
	loop
	pieces
;
set-current

: scramble-channel ( silence -- )
	\ ;; (scramble-channel .01)
	{ silence }
	128 make-moving-average { buffer }
	silence 128 f/ to silence
	#() { edges }
	0 { samp }
	#t { in-silence }
	max-regions { old-max }
	with-mix-tags { old-tags }
	1024 set-max-regions drop
	#f set-with-mix-tags drop
	buffer silence in-silence edges samp sc-scan-cb
	    0 #f #f #f #f scan-channel drop
	edges #f #f #f framples array-push drop
	0 0 { start end }
	edges map
		start *key* #f #f make-region
		*key* to start
	end-map ( pieces ) sc-edit-cb get-func-name as-one-edit drop
	old-max set-max-regions drop
	old-tags set-with-mix-tags drop
;
previous

\ ;; -------- reorder blocks within channel

hide
: rbb-cb { rd beg ctr actual-block-len len snd chn -- prc; y self -- val }
	1 proc-create ( prc )
	rd , beg , ctr , actual-block-len , len , snd , chn ,
  does> { y self -- val }
	self @ { rd }
	self cell+ @ { beg }
	self 2 cells + @ { ctr }
	self 3 cells + @ { actual-block-len }
	self 4 cells + @ { len }
	self 5 cells + @ { snd }
	self 6 cells + @ { chn }
	rd read-sample { val }
	beg 10 < if
		val beg f* 0.1 f* to val
	else
		beg actual-block-len 10 - > if
			val actual-block-len beg - f* 0.1 f* to val
		then
	then
	beg 1+ to beg
	beg actual-block-len = if
		1 self 2 cells + +! ( ctr++ )
		0 self 1 cells + !  ( beg = 0 )
		len self 2 cells + @ ( ctr ) actual-block-len * - 0 max
		    snd chn 1 #f make-sampler self !
	then
	val
;
set-current

: reverse-by-blocks <{ block-len :optional snd #f chn #f -- val }>
	doc" Divide sound into block-len blocks, \
recombine blocks in reverse order."
	snd chn #f framples { len }
	len snd srate block-len f* f/ fround->s { num-blocks }
	num-blocks 1 > if
		len num-blocks f/ fceil f>s { actual-block-len }
		len actual-block-len - snd chn 1 #f make-sampler { rd }
		0 { beg }
		1 { ctr }
		"%s %s" #( block-len get-func-name ) string-format { origin }
		rd beg ctr actual-block-len len snd chn rbb-cb
		    0 #f snd chn #f origin map-channel
	else
		#f
	then
;
previous

hide
: rwb-cb { len actual-block-len no-clicks-env snd chn -- prc; self -- val }
	0 proc-create ( prc )
	len , actual-block-len , no-clicks-env , snd , chn ,
  does> { self -- val }
	self           @ { len }
	self   cell+   @ { actual-block-len }
	self 2 cells + @ { no-clicks-env }
	self 3 cells + @ { snd }
	self 4 cells + @ { chn }
	len 0 ?do
		i actual-block-len snd chn #f reverse-channel drop
		no-clicks-env i actual-block-len snd chn #f env-channel drop
	actual-block-len +loop
	#t
;
set-current

: reverse-within-blocks <{ block-len :optional snd #f chn #f -- val }>
	doc" Divide sound into blocks, recombine in order, \
but each block internally reversed."
	snd chn #f framples { len }
	len snd srate block-len f* f/ fround->s { num-blocks }
	num-blocks 1 > if
		len num-blocks f/ fceil f>s { actual-block-len }
		#( 0.0 0.0  0.01 1.0  0.99 1.0  1.0 0.0 ) { no-clicks-env }
		"%s %s" #( block-len get-func-name ) string-format { origin }
		len actual-block-len no-clicks-env snd chn rwb-cb
		    origin as-one-edit
	else
		0 #f snd chn #f reverse-channel
	then
;
previous

\ ;;; -------- channel-clipped?

hide
: cc-cb ( -- prc; y self -- f )
	1 proc-create ( prc )
	0.0 ( last-y ) ,
  does> { y self -- f }
	self @ { last-y }
	y      fabs 0.9999 f>=
	last-y fabs 0.9999 f>= && ( flag )
	y self ! ( last-y = y )
	( flag )
;
set-current

: channel-clipped? <{ :optional snd #f chn #f -- val }>
	doc" Return #t and a sample number if it finds clipping."
	cc-cb 0 #f snd chn #f scan-channel
;
previous

\ ;;; -------- sync-everything

: sync-everything ( -- )
	doc" Set the sync fields of all currently open sounds \
to the same, unique value."
	sync-max 1+ { new-sync }
	sounds each ( snd )
		new-sync swap set-sync drop
	end-each
;

\ === Moog Filter ===

hide
vct( 0.999969 0.990082 0.980347 0.970764 0.961304 0.951996 0.94281 0.933777
     0.924866 0.916077 0.90741 0.898865 0.890442 0.882141 0.873962 0.865906
     0.857941 0.850067 0.842346 0.834686 0.827148 0.819733 0.812378 0.805145
     0.798004 0.790955 0.783997 0.77713 0.770355 0.763672 0.75708 0.75058
     0.744141 0.737793 0.731537 0.725342 0.719238 0.713196 0.707245 0.701355
     0.695557 0.689819 0.684174 0.678558 0.673035 0.667572 0.66217 0.65686
     0.651581 0.646393 0.641235 0.636169 0.631134 0.62619 0.621277 0.616425
     0.611633 0.606903 0.602234 0.597626 0.593048 0.588531 0.584045 0.579651
     0.575287 0.570953 0.566681 0.562469 0.558289 0.554169 0.550079 0.546051
     0.542053 0.538116 0.53421 0.530334 0.52652 0.522736 0.518982 0.515289
     0.511627 0.507996 0.504425 0.500885 0.497375 0.493896 0.490448 0.487061
     0.483704 0.480377 0.477081 0.473816 0.470581 0.467377 0.464203 0.46109
     0.457977 0.454926 0.451874 0.448883 0.445892 0.442932 0.440033 0.437134
     0.434265 0.431427 0.428619 0.425842 0.423096 0.42038 0.417664 0.415009
     0.412354 0.409729 0.407135 0.404572 0.402008 0.399506 0.397003 0.394501
     0.392059 0.389618 0.387207 0.384827 0.382477 0.380127 0.377808 0.375488
     0.37323 0.370972 0.368713 0.366516 0.364319 0.362122 0.359985 0.357849
     0.355713 0.353607 0.351532 0.349457 0.347412 0.345398 0.343384 0.34137
     0.339417 0.337463 0.33551 0.333588 0.331665 0.329773 0.327911 0.32605
     0.324188 0.322357 0.320557 0.318756 0.316986 0.315216 0.313446 0.311707
     0.309998 0.308289 0.30658 0.304901 0.303223 0.301575 0.299927 0.298309
     0.296692 0.295074 0.293488 0.291931 0.290375 0.288818 0.287262 0.285736
     0.284241 0.282715 0.28125 0.279755 0.27829 0.276825 0.275391 0.273956
     0.272552 0.271118 0.269745 0.268341 0.266968 0.265594 0.264252 0.262909
     0.261566 0.260223 0.258911 0.257599 0.256317 0.255035
     0.25375 ) constant moog-gaintable

#( 0.0        -1.0
   0.03311111 -0.9
   0.06457143 -0.8
   0.0960272  -0.7
   0.127483   -0.6
   0.1605941  -0.5
   0.1920544  -0.4
   0.22682086 -0.3
   0.2615873  -0.2
   0.29801363 -0.1
   0.33278003 -0.0
   0.37086168  0.1
   0.40893877  0.2
   0.4536417   0.3
   0.5         0.4
   0.5463583   0.5
   0.5943719   0.6
   0.6556281   0.7
   0.72185487  0.8
   0.8096009   0.9
   0.87913835  0.95
   0.9933787   1.0
   1.0         1.0 ) constant moog-freqtable

#( "moog-freq"
   "moog-Q"
   "moog-s"
   "moog-y"
   "moog-fc" ) create-struct make-moog-filter-struct
set-current

<'> moog-freq@ alias moog-frequecy@ ( gen -- frq )
: moog-frequency! { gen frq -- }
	gen frq moog-freq!
	gen frq mus-srate f2/ f/ moog-freqtable 1.0 envelope-interp moog-fc!
;

: make-moog-filter ( freq Q -- gen )
	doc" Make a new moog-filter generator.  \
FREQ is the cutoff in Hz, Q sets the resonance: \
0 = no resonance, 1: oscillates at FREQUENCY."
	{ freq Q }
	make-moog-filter-struct { gen }
	gen freq moog-frequency!
	gen Q moog-Q!
	gen vct( 0.0 0.0 0.0 0.0 ) moog-s!
	gen 0.0 moog-y!
	gen
;

: moog-filter ( gen sig -- A )
	{ gen sig }
	0.25 sig gen moog-y@ f- f* { A }
	gen moog-s@ each { st }
		gen moog-fc@ A st f- f* A f+ -0.95 fmax 0.95 fmin to A
		gen moog-s@ i A vct-set! drop
		A st f+ -0.95 fmax 0.95 fmin to A
	end-each
	gen moog-fc@ 99.0 f* { ix }
	ix fround->s { ixint }
	ix ixint f- { ixfrac }
	A gen moog-Q@ f*
	    1.0 ixfrac f- moog-gaintable ixint 99 + vct-ref
	    f* ixfrac moog-gaintable ixint 100 + vct-ref f* f+ f*
	    gen swap moog-y!
	A
;
previous

\ 500.0 0.1 make-moog-filter value gen
\ lambda: <{ y -- val }> gen y moog-filter ; map-channel
\ gen 1.0 moog-filter

\ examp.fs ends here