File: dlocsig.scm

package info (click to toggle)
snd 26.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 44,064 kB
  • sloc: ansic: 292,212; lisp: 260,692; ruby: 71,134; sh: 3,293; fortran: 2,342; csh: 1,067; makefile: 294; cpp: 294; python: 87; xml: 27; javascript: 1
file content (3098 lines) | stat: -rw-r--r-- 110,093 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
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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Suggestions, comments and bug reports are welcome. Please 
;;; address email to: nando@ccrma.stanford.edu
;;;
;;; see the file COPYING for license info.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Dynamic multichannel three-dimentional signal locator
;;; (wow that sound good! :-)
;;;
;;; by Fernando Lopez Lezcano
;;;    CCRMA, Stanford University
;;;    nando@ccrma.stanford.edu
;;;
;;; Thanks to Juan Pampin for help in the initial coding of the new version
;;; and for prodding me to finish it. To Joseph L. Anderson and Marcelo Perticone
;;; for insights into the Ambisonics coding and decoding process. 
;;; http://www.york.ac.uk/inst/mustech/3d_audio/ambison.htm for more details...

;;; CHANGES:
;;; 01/28/2012: third order ambisonics support (Nando).
;;; 04/18/2011: various small changes from lint.scm.
;;; 04/26/2010: add delay hack to remove artifacts in delay output, fix other bugs (Nando)
;;;             added proper doppler src conversion thanks to Bill's code in dsp.scm
;;;             merged in code for higher order ambisonics (up to 2nd order h/v)
;;; 06/28/2009: remove class/method stuff for s7 (Bill)
;;; 01/08/2007: make a few functions local etc (Bill)
;;; 07/05/2006: translate to scheme, use move-sound generator (Bill)
;;; 04/29/2002: fixed reverb envelopes for no reverb under clisp
;;; 01/14/2001: added multichannel reverb output with local and global control
;;;             in the reverberator (the hrtf code is currently not merged into
;;;             this version). Reverb-amount can now be an envelope. Ambisonics
;;;             now outputs signal to the reverb stream. 
;;; 02/05/2000: . don't compile as part of the clm package, import symbols
;;;             . switched over to clm-2, otherwise convolve HAS to operate
;;;               on a file, we want convolve to process the output of the 
;;;               doppler delay line (for the hrtf code)
;;; 02/03/2000: started working on hrtf's
;;; 01/15/2000: rewrote transform-path code to account for 3d paths
;;; 01/13/2000: . changed order of b-format output file to W:X:Y:Z from X:Y:Z:W
;;;             . plot method would bomb with paths that had constant velocity,
;;;               fixed norm function
;;;             . added make-literal-path and friends to enable to create
;;;               paths with user supplied coordinates
;;;             . decoded-ambisonics was rotating sounds in the wrong direction
;;;             . in change-direction: only check for change if both points are
;;;               different (intersect-inside-radius can create a redundant point)
;;; 11/28/1999: decoded-ambisonics is now working for N channels
;;;             includes reverberation send
;;; 11/27/1999: set minimum segment distance for rendering, otherwise for long
;;;             lines the amplitude envelope does not reflect power curve. 
;;; 11/26/1999: added code to check for intersection with inner radius
;;;             fixed nearest-point to handle trivial cases

;;; 01/21/2001: fix envelope generated for mono reverberation stream.
;;;             change input and output to use frames and mixers
;;;             fix supersonic movement warning code
;;; > add warnings when object goes outside of area covered by speakers
;;; > fix one common vertice case of 3 speaker group transitions
;;; > redo the old code for multiple images (reflections in a rectangular room)
;;;     a bit of a pain, would have to add z (ceiling and floor reflections)
;;;     would be better to find general purpose code for non-rectangular rooms
;;; > we really need a good N-channel reverb [fixed with freeverb]
;;; > change reverb to be multichannel, add local and global reverb
;;;   11/24/1999: should use a waveguide reverb like pph@ccrma project
;;;               would be a good idea to inject the signal through a center
;;;               injection point that moves inside the virtual cube, more like
;;;               a physical model of what actually happens in a room
;;; | add ambisonics back-end
;;;   11/24/1999: code to b-format sort of working
;;;                 how to deal with the inner space and 0:0:0?
;;;               decoded format not working if we incorporate distance att
;;;                 formulas are wrong...
;;; > add hrtf back-end
;;; > extract a supath from an existing path
;;; > recode the transformation functions
;;; > add arcs of circles and other basic geometric paths
;;;     make it so that you can concatenate them...
;;; | 11/25/1999 fix the "diagonal case" (sounds go through the head of the listener)

(provide 'snd-dlocsig.scm)

#|
(define* (envelope-interp x e base)   ;e is list of x y breakpoint pairs, interpolate at x returning y
 ;; "(envelope-interp x e (base 1.0)) -> value of e at x; base controls connecting segment type: (envelope-interp .3 '(0 0 .5 1 1 0) -> .6"
  (cond ((null? e) 0.0)		        ;no data -- return 0.0
	((or (<= x (car e))	        ;we're sitting on x val (or if < we blew it)
	     (null? (cddr e)))	        ;or we're at the end of the list
	 (cadr e))		        ;so return current y value
	((> (caddr e) x)		;x <= next env x axis value
	 (if (or (= (cadr e) (cadddr e))
		 (and base (= base 0.0)))
	     (cadr e)		        ;y1=y0, so just return y0 (avoid endless calculations below)
	     (if (or (not base) (= base 1.0))
		 (+ (cadr e)	        ;y0+(x-x0)*(y1-y0)/(x1-x0)
		    (* (- x (car e))
		       (/ (- (cadddr e) (cadr e))
			  (- (caddr e) (car e)))))
		 (+ (cadr e) ; this does not exactly match xramp-channel
		    (* (/ (- (cadddr e) (cadr e))
			  (- base 1.0))
		       (- (expt base (/ (- x (car e))
					(- (caddr e) (car e))))
			  1.0))))))
	(else (envelope-interp x (cddr e) base)))) ;go on looking for x segment
|#

(define x-norm 
  (let ((+documentation+ "(x-norm e xmax) changes 'e' x axis values so that they run to 'xmax'"))
    (lambda (e xmax)
      (let x-norm-1 ((e e)
                     (scl (/ xmax (e (- (length e) 2))))
                     (new-e ()))
        (if (null? e)
            (reverse! new-e)
            (x-norm-1 (cddr e) scl (cons (cadr e) (cons (* scl (car e)) new-e))))))))


;;;;;;;;;;;;;;;;;;;;;
;;; Global Parameters

;;; Define the base in which all angles are expressed
(define dlocsig-one-turn 360)

(define one-turn-is 
  (let ((+documentation+ "(one-turn-is unit) sets dlocsig's angle unit (degrees=360, the default or radians=2*pi)"))
    (lambda (unit)
      (set! dlocsig-one-turn unit))))

(define angles-in-degree
  (let ((+documentation+ "(angles-in-degree) sets dlocsig's unit to degrees (the default)"))
    (lambda ()
      (one-turn-is 360))))

(define angles-in-radians
  (let ((+documentation+ "(angles-in-radians) sets dlocsig's unit to radians (default is degrees)"))
    (lambda ()
      (one-turn-is (* 2 pi)))))

(define angles-in-turns
  (let ((+documentation+ "(angles-in-turns) sets dlocsig's angle unit to turns"))
    (lambda ()
      (one-turn-is 1))))

;; speed of sound in air, in meters per second under normal conditions
(define dlocsig-speed-of-sound 344)

(define distances-in-meters
  (let ((+documentation+ "(distances-in-meters) sets dlocsig's distances in meters (the default)"))
    (lambda ()
      (set! dlocsig-speed-of-sound 344))))

(define distances-in-feet
  (let ((+documentation+ "(distances-in-feet) sets dlocsig's distances in feet (default is meters)"))
    (lambda ()
      (set! dlocsig-speed-of-sound 1128))))

;; default for whether to use two or three-dimensional speaker configurations
(define dlocsig-3d #f)

;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Speaker Configuration

(define* (make-group (id 0) (size 0) vertices speakers matrix)
  (list 'group id size vertices speakers matrix))

(define group-id (dilambda (lambda (a) (a 1)) (lambda (a b) (set! (a 1) b))))
(define group-size (dilambda (lambda (a) (a 2)) (lambda (a b) (set! (a 2) b))))
(define group-vertices (dilambda (lambda (a) (a 3)) (lambda (a b) (set! (a 3) b))))
(define group-speakers (dilambda (lambda (a) (a 4)) (lambda (a b) (set! (a 4) b))))
(define group-matrix (dilambda (lambda (a) (a 5)) (lambda (a b) (set! (a 5) b))))


(define* (make-speaker-config number dimension coords groups delays omap)
  (list 'speaker-config number dimension coords groups delays omap))

(define speaker-config-number (dilambda (lambda (a) (a 1)) (lambda (a b) (set! (a 1) b))))
(define speaker-config-dimension (dilambda (lambda (a) (a 2)) (lambda (a b) (set! (a 2) b))))
(define speaker-config-coords (dilambda (lambda (a) (a 3)) (lambda (a b) (set! (a 3) b))))
(define speaker-config-groups (dilambda (lambda (a) (a 4)) (lambda (a b) (set! (a 4) b))))
(define speaker-config-delays (dilambda (lambda (a) (a 5)) (lambda (a b) (set! (a 5) b))))
(define speaker-config-map (dilambda (lambda (a) (a 6)) (lambda (a b) (set! (a 6) b))))


;;; Create a speaker configuration structure based on a list of speakers
;;;
;;; speakers:  list of angles of speakers with respect to 0
;;; delays:    list of delays for each speaker, zero if nil
;;; distances: list relative speaker distances, 
;;;            (instead of delays)
;;; omap:      mapping of speakers to output channels
;;;            content should be output channel number, zero based

(define cis 
  (let ((+documentation+ "(cis a) returns e^(ia)"))
    (lambda (a)
      (exp (* 0.0+1.0i a)))))

(define third 
  (let ((+documentation+ "(third lst) returns the 3rd element of 'lst'"))
    (lambda (a) 
      (and (>= (length a) 3) (a 2)))))

(define fourth 
  (let ((+documentation+ "(fourth lst) returns the 4th element of 'lst'"))
    (lambda (a) 
      (and (>= (length a) 4) (a 3)))))

(define* (last lst (n 1))
  (and (pair? lst) 
       (list-tail lst (- (length lst) n))))

(define arrange-speakers
  ;; sanity checking of configuration
  (letrec ((has-duplicates? 
	    (lambda (lst)
	      (and (not (null? lst))
		   (or (member (car lst) (cdr lst))
		       (has-duplicates? (cdr lst))))))
	   (invert3x3 
	    (lambda (mat) ; invert a 3x3 matrix using cofactors
	      (let ((m (make-float-vector '(3 3))))
		(do ((i 0 (+ i 1)))
		    ((= i 3))
		  (do ((j 0 (+ j 1)))
		      ((= j 3))
		    (set! (m i j) (mat i j))))
		(set! (mat 0 0) (- (* (m 1 1) (m 2 2)) (* (m 1 2) (m 2 1))))
		(set! (mat 0 1) (- (* (m 0 2) (m 2 1)) (* (m 0 1) (m 2 2))))
		(set! (mat 0 2) (- (* (m 0 1) (m 1 2)) (* (m 0 2) (m 1 1))))
		(set! (mat 1 0) (- (* (m 1 2) (m 2 0)) (* (m 1 0) (m 2 2))))
		(set! (mat 1 1) (- (* (m 0 0) (m 2 2)) (* (m 0 2) (m 2 0))))
		(set! (mat 1 2) (- (* (m 0 2) (m 1 0)) (* (m 0 0) (m 1 2))))
		(set! (mat 2 0) (- (* (m 1 0) (m 2 1)) (* (m 1 1) (m 2 0))))
		(set! (mat 2 1) (- (* (m 0 1) (m 2 0)) (* (m 0 0) (m 2 1))))
		(set! (mat 2 2) (- (* (m 0 0) (m 1 1)) (* (m 0 1) (m 1 0))))
		
		(let ((det (+ (* (m 0 0) (mat 0 0))
			      (* (m 0 1) (mat 1 0))
			      (* (m 0 2) (mat 2 0)))))
		  (and (> (abs det) 1e-06)
		       (do ((invdet (/ 1.0 det))
			    (row 0 (+ 1 row)))
			   ((= row 3) mat)
			 (do ((col 0 (+ 1 col)))
			     ((= col 3))
			   (set! (mat row col) (* (mat row col) invdet)))))))))
	   (invert2x2 
	    (lambda (mat) ; invert a 2x2 matrix
	      (let ((m (make-float-vector '(2 2)))
		    (det (- (* (mat 0 0) (mat 1 1))
			    (* (mat 1 0) (mat 0 1)))))
		(and (> (abs det) 1e-06)
		     (begin
		       (set! (m 0 0) (/ (mat 1 1) det))
		       (set! (m 1 1) (/ (mat 0 0) det))
		       (set! (m 0 1) (- (/ (mat 0 1) det)))
		       (set! (m 1 0) (- (/ (mat 1 0) det)))
		       m))))))
    
    ;; arrange-speakers
    (lambda* ((speakers ())
	      (groups ())
	      (delays ())
	      (distances ())
	      (channel-map ()))
      (if (null? speakers)
	  (error 'mus-error "a speaker configuration must have at least one speaker!~%"))
      
      (if (pair? groups)
	  (let ((first-len (length (car groups))))
	    (for-each
	     (lambda (group)
	       (if (not (= (length group) first-len))
		   (error 'mus-error "all groups must be of the same length! (~A)~%" first-len)))
	     groups))
	  
	  ;; if the speakers are defined with only azimuth angles (no elevation)
	  (if (not (pair? (car speakers)))
	      ;; then create the groups ourselves because it is a 2d configuration;
	      ;; we could create the 3d configuration groups but the algorithm for
	      ;; doing that in the generic case is not trivial
	      
	      (let ((len (length speakers)))
		(if (= len 1)
		    (set! groups (list (list 0)))
		    (do ((i 0 (+ i 1))
			 (j 1 (+ j 1)))
			((= i len)
			 (set! groups (reverse groups)))
		      (set! groups (cons (list i (modulo j len)) groups)))))))
      
      (if (null? groups)
	  (error 'mus-error "no groups specified, speakers must be arranged in groups~%"))
      
      (when (pair? delays)
	(if (pair? distances)
	    (error 'mus-error "please specify delays or distances but not both~%"))
	
	(if (> (length speakers) (length delays))
	    (error 'mus-error "all speaker delays have to be specified, only ~A supplied [~A]~%" (length delays) delays)
	    (if (< (length speakers) (length delays))
		(error 'mus-error "more speaker delays than speakers, ~A supplied instead of ~A [~A]~%" (length delays) (length speakers) delays)))
	
	(for-each
	 (lambda (dly)
	   (if (< dly 0.0) (error 'mus-error "delays must be all positive, ~A is negative~%" dly)))
	 delays))
      
      (when (pair? distances)
	(if (> (length speakers) (length distances))
	    (error 'mus-error "all speaker distances have to be specified, only ~A supplied [~A]~%" (length distances) distances)
	    (if (< (length speakers) (length distances))
		(error 'mus-error "more speaker distances than speakers, ~A supplied instead of ~A [~A]~%" (length distances) (length speakers) distances)))
	
	(for-each
	 (lambda (dly)
	   (if (< dly 0.0) (error 'mus-error "distances must be all positive, ~A is negative~%" dly)))
	 distances))
      
      (if (pair? channel-map)
	  (if (> (length speakers) (length channel-map))
	      (error 'mus-error "must map all speakers to output channels, only ~A mapped [~A]~%" (length channel-map) channel-map)
	      (if (< (length speakers) (length channel-map))
		  (error 'mus-error "trying to map more channels than there are speakers, ~A supplied instead of ~A [~A]~%" 
			 (length channel-map) (length speakers) channel-map))))
      
      ;; collect unit vectors describing the speaker positions
      (let* ((coords
	      (let ((val ()))
		(for-each
		 (lambda (s) ; speakers
		   (let* ((evec (let ((e (if (pair? s) (or (cadr s) 0.0) 0.0)))
				  (cis (/ (* e 2 pi) dlocsig-one-turn))))
			  (avec (let ((a (if (pair? s) (car s) s)))
				  (cis (/ (* a 2 pi) dlocsig-one-turn))))
			  (dxy (real-part evec))
			  (z (imag-part evec))
			  (x (* dxy (imag-part avec)))
			  (y (* dxy (real-part avec)))
			  (mag (sqrt (+ (* x x) (* y y) (* z z)))))
		     (set! val (cons (list (/ x mag) (/ y mag) (/ z mag)) val))))
		 speakers)
		(reverse val)))
	     
	     ;; find delay times from specified distances or delays
	     (times (let ((min-dist (if (pair? distances)  ; minimum distance
					(apply min distances)
					0.0))
			  (v (make-float-vector (length speakers))))
		      (do ((i 0 (+ i 1)))
			  ((= i (length speakers)))
			(set! (v i) (let ((distance (and (pair? distances) (distances i)))
					  (dly (and (pair? delays) (delays i))))
				      (or dly
					  (and (number? distance)
					       (/ (- distance min-dist) dlocsig-speed-of-sound))
					  0.0))))
		      v))
	     
	     ;; create the group structures
	     (groups (let ((vals ())
			   (id 0))
		       (for-each 
			(lambda (group)
			  (let* ((size (length group))
				 (vertices (map coords group))
				 (matrix (if (= size 3)
					     (do ((m (make-float-vector '(3 3)))
						  (i 0 (+ i 1)))
						 ((= i 3)
						  (invert3x3 m))   
					       (do ((j 0 (+ j 1)))
						   ((= j 3))
						 (set! (m i j) ((vertices i) j))))
					     (and (= size 2)
						  (do ((m (make-float-vector '(2 2)))
						       (i 0 (+ i 1)))
						      ((= i 2)
						       (invert2x2 m))   
						    (do ((j 0 (+ j 1)))
							((= j 2))
						      (set! (m i j) ((vertices i) j))))))))
			    (set! vals (cons (make-group :id id
							 :size size
							 :speakers group
							 :vertices vertices
							 :matrix matrix)
					     vals))
			    (set! id (+ 1 id))))
			groups)
		       (reverse vals))))
	
	;; check validity of map entries
	(if channel-map
	    (let ((entries (length channel-map)))
	      (for-each
	       (lambda (entry)
		 (if (>= entry entries)
		     (error 'mus-error "channel ~A in map ~A is out of range (max=~A)~%" entry channel-map entries)))
	       channel-map)
	      (if (has-duplicates? channel-map)
		  (error 'mus-error "there are duplicate channels in channel-map ~A~%" channel-map))))
	
	;; create the speaker configuration structure
	
	(make-speaker-config :number (length speakers)
			     :dimension (group-size (car groups))
			     :coords coords
			     :groups groups
			     :delays times
			     :omap (do ((v (make-vector (length speakers)))
					(chan 0 (+ 1 chan)))
				       ((= chan (length speakers)) v)
				     (set! (v chan) (or (and (pair? channel-map) (channel-map chan))
							chan))))))))
    
;;; Default speaker configurations

(define dlocsig-speaker-configs
  ;; by default up to eight channels, 2-d and 3-d configurations
  (list 
   (list
    ;;
    ;; 2-D speaker configurations
    ;; no channels: impossible
    #f
    ;; mono
    (arrange-speakers :speakers '(0))
    ;; stereo
    (arrange-speakers :speakers '(-60 60))
    ;; three channels
    (arrange-speakers :speakers '(-45 45 180))
    ;; four channels
    (arrange-speakers :speakers '(-45 45 135 225))
    ;; five channels (5.1 arrangement)
    (arrange-speakers :speakers '(-45 0 45 135 -135))
    ;; six channels
    (arrange-speakers :speakers '(-60 0 60 120 180 240))
    ;; seven channels
    (arrange-speakers :speakers '(-45 0 45 100 140 -140 -100))
    ;; eight speakers
    (arrange-speakers :speakers '(-22.5 22.5 67.5 112.5 157.5 202.5 247.5 292.5)))
   ;;
   ;; 3-D speaker configurations
   ;;
   (list
    ;; no channels: impossible
    #f
    ;; mono
    #f
    ;; stereo
    #f
    ;; three channels
    #f
    ;; four channels 3d
    (arrange-speakers :speakers '((-60 0) (60 0) (180 0)
				  (0 90))
		      :groups '((0 1 3) (1 2 3) (2 0 3)
				;; floor
				(0 1 2)))
    ;; five channels 3d
    (arrange-speakers :speakers '((-45 0) (45 0) (135 0) (-135 0)
				  (0 90))
		      :groups '((0 1 4) (1 2 4) (2 3 4) (3 0 4)
				;; floor
				(0 1 2) (2 3 0)))
    ;; six channels 3d
    (arrange-speakers :speakers '((-45 0) (45 0) (135 0) (-135 0)
				  (-90 60) (90 60))
		      :groups '((0 1 4) (1 4 5) (1 2 5) (2 3 5) (3 4 5) (3 0 4)
				;; floor
				(0 1 2) (2 3 0)))
    ;; seven channels 3d
    (arrange-speakers :speakers '((-45 0) (45 0) (135 0) (-135 0)
				  (-60 60) (60 60) (180 60))
		      :groups '((0 1 4) (1 4 5) (1 2 5) (2 6 5) (2 3 6) (3 4 6) (3 0 4) (4 5 6)
				;; floor
				(0 1 2) (2 3 0)))
    ;; eight speakers 3d
    (arrange-speakers :speakers '((-45 -10) (45 -10) (135 -10) (225 -10)
				  (-45 45) (45 45) (135 45) (225 45))
		      :groups '((0 4 5) (0 5 1) (5 1 2) (2 6 5) (6 7 2) (2 3 7)
				(3 7 4) (3 0 4)
				;; ceiling
				(4 7 6) (6 5 4)
				;; floor
				(0 1 2) (2 3 0))))))


;;; Set a particular speaker configuration

(define set-speaker-configuration 
  (let ((+documentation+ "(set-speaker-configuration config (configs dlocsig-speaker-configs)) sets a dlocsig speaker configuration"))
    (lambda* (config (configs dlocsig-speaker-configs))
      (let ((lst ((if (< (speaker-config-dimension config) 3) car cadr) configs))
	    (num (speaker-config-number config)))
	(set! (lst num) config)))))


;;; Get the speaker configuration for a given number of output channels

(define get-speaker-configuration 
  (let ((+documentation+ "(get-speaker-configuration channels (3d dlocsig-3d) (configs dlocsig-speaker-configs)) returns a dlocsig speaker configuration"))
    (lambda* (channels (3d dlocsig-3d) (configs dlocsig-speaker-configs))
      (let ((config (((if 3d cadr car) configs) channels)))
	(if (null? config)
	    (error 'mus-error "no speaker configuration exists for ~A ~A output channel~A~%~%" 
		   (if 3d "tridimensional" "bidimensional")
		   channels (if (= channels 1) "s" "")))
	config))))


;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Dlocsig unit generator
;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; global dlocsig parameters

(define dlocsig-path ())
(define dlocsig-scaler 1.0)
(define dlocsig-direct-power 1.5)
(define dlocsig-inside-direct-power 1.5)
(define dlocsig-reverb-power 0.5)
(define dlocsig-inside-reverb-power 0.5)
(define dlocsig-initial-delay #f)
(define dlocsig-unity-gain-distance #f)
(define dlocsig-reverb-amount 0.04)
(define dlocsig-inside-radius 1.0)
(define dlocsig-minimum-segment-length 1.0)

;; render using:

(define amplitude-panning 1)
(define ambisonics 2)
(define decoded-ambisonics 3)
					;(define stereo-hrtf 4)

					; for backwards compatibility
(define b-format-ambisonics ambisonics)

					; a reasonable default

(define dlocsig-render-using amplitude-panning)

;; ambisonics horizontal and vertical order for encoding
;; the default is first order b-format WXYZ

(define dlocsig-ambisonics-h-order 1)
(define dlocsig-ambisonics-v-order 1)

;; globals for ambisonics

(define point707 (cos (/ (* pi 2) 8.0)))
(define dlocsig-ambisonics-scaler point707)
(define dlocsig-ambisonics-ho-rev-scaler 0.05)
;; for 3rd order FuMa

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Get number of channels needed by ambisonics

(define* (ambisonics-channels 
	  (h-order dlocsig-ambisonics-h-order)
	  (v-order dlocsig-ambisonics-v-order))
  (if (< h-order 0)
      0              ;; error: we need at least horizontal order 1!
      (let ((count 0))
	(if (>= h-order 1)
	    ;; W X Y
	    (set! count (+ count 3)))
	(if (>= v-order 1)
	    ;; Z
	    (set! count (+ count 1)))
	(if (>= v-order 2)
	    ;; R S T
	    (set! count (+ count 3)))
	(if (>= h-order 2)
	    ;; U V
	    (set! count (+ count 2)))
	(if (>= v-order 3)
	    ;; K L M N O
	    (set! count (+ count 5)))
	(if (>= h-order 3)
	    ;; P Q
	    (set! count (+ count 2)))
	count)))
	

;;;;;;;;;
;;; Paths
;;;;;;;;;

;;; Generic path class

;;; path is a list (type rx ry rz rv rt tx ty tz tt ...)

(define path-rx (dilambda (lambda (p) (p 1)) (lambda (p val) (set! (p 1) val))))
(define path-ry (dilambda (lambda (p) (p 2)) (lambda (p val) (set! (p 2) val))))
(define path-rz (dilambda (lambda (p) (p 3)) (lambda (p val) (set! (p 3) val))))
(define path-rv (dilambda (lambda (p) (p 4)) (lambda (p val) (set! (p 4) val))))
(define path-rt (dilambda (lambda (p) (p 5)) (lambda (p val) (set! (p 5) val))))
(define path-tx (dilambda (lambda (p) (p 6)) (lambda (p val) (set! (p 6) val))))
(define path-ty (dilambda (lambda (p) (p 7)) (lambda (p val) (set! (p 7) val))))
(define path-tz (dilambda (lambda (p) (p 8)) (lambda (p val) (set! (p 8) val))))
(define path-tt (dilambda (lambda (p) (p 9)) (lambda (p val) (set! (p 9) val))))

					;(define (make-path) (list 'path () () () () () () () () ()))
#|
(define (describe path)
  (format #f
	  (if (memq (car path) '(bezier-path open-bezier-path))
	      (values
	       "<bezier-path>:~%  rx: ~A~%  ry: ~A~%  rz: ~A~%  rv: ~A~%  rt: ~A~%  tx: ~A~%  ty: ~A~%  tz: ~A~%  tt: ~A~%  ~
                         x: ~A~%  y: ~A~%  z: ~A~%  v: ~A~%  bx: ~A~%  by: ~A~%  bz: ~A~%  error: ~A~%  curvature: ~A~%"
	       (path-rx path) (path-ry path) (path-rz path) (path-rv path) (path-rt path) (path-tx path)
	       (path-ty path) (path-tz path) (path-tt path) (bezier-x path) (bezier-y path)
	       (bezier-z path) (bezier-v path) (bezier-bx path) (bezier-by path) (bezier-bz path)
	       (bezier-error path) (bezier-curvature path))
	      (values
	       "<path>:~%  rx: ~A~%  ry: ~A~%  rz: ~A~%  rv: ~A~%  rt: ~A~%  tx: ~A~%  ty: ~A~%  tz: ~A~%  tt: ~A~%"
	       (path-rx path) (path-ry path) (path-rz path) (path-rv path) (path-rt path) (path-tx path)
	       (path-ty path) (path-tz path) (path-tt path)))))
|#

;;; Inquiries into the state of the path

(define (not-rendered path)
  (null? (path-rx path)))

(define (not-transformed path)
  (null? (path-tx path)))

;;; Reset any transformations on the originally rendered path

(define (reset-transformation path)
  (set! (path-tt path) ())
  (set! (path-tx path) ())
  (set! (path-ty path) ())
  (set! (path-tz path) ())
  path)

;;; Reset the rendered path (and any transformations)

(define (reset-rendering path)
  (set! (path-rt path) ())
  (set! (path-rv path) ())
  (set! (path-rx path) ())
  (set! (path-ry path) ())
  (set! (path-rz path) ())
  (reset-transformation path))

;;; Return the best possible set of coordinates

(define list?? 
  (let ((+documentation+ "list?? returns a if it is a list"))
    (lambda (a) 
      (and (pair? a) a))))

(define (path-x path)
  (or (list?? (path-tx path))
      (list?? (path-rx path))
      (path-rx (render-path path))))

(define (path-y path)
  (or (list?? (path-ty path))
      (list?? (path-ry path))
      (path-ry (render-path path))))

(define (path-z path)
  (or (list?? (path-tz path))
      (list?? (path-rz path))
      (path-rz (render-path path))))

(define (path-time path)
  (or (list?? (path-tt path))
      (list?? (path-rt path))
      (path-rt (render-path path))))


;;;;;;;;;;;;;;;;
;;; Bezier paths
;;;;;;;;;;;;;;;;

;;; Parse a path as two or three-dimensional paths

(define path-3d #f)

;;; Path class for bezier rendered paths

;;; bezier-path is path + path 3d polar x y z v bx by bz error curvature


(define bezier-path      (dilambda (lambda (p) (p 10)) (lambda (p val) (set! (p 10) val))))
(define bezier-3d        (dilambda (lambda (p) (p 11)) (lambda (p val) (set! (p 11) val))))
(define bezier-polar     (dilambda (lambda (p) (p 12)) (lambda (p val) (set! (p 12) val))))
(define bezier-x         (dilambda (lambda (p) (p 13)) (lambda (p val) (set! (p 13) val))))
(define bezier-y         (dilambda (lambda (p) (p 14)) (lambda (p val) (set! (p 14) val))))
(define bezier-z         (dilambda (lambda (p) (p 15)) (lambda (p val) (set! (p 15) val))))
(define bezier-v         (dilambda (lambda (p) (p 16)) (lambda (p val) (set! (p 16) val))))
(define bezier-bx        (dilambda (lambda (p) (p 17)) (lambda (p val) (set! (p 17) val))))
(define bezier-by        (dilambda (lambda (p) (p 18)) (lambda (p val) (set! (p 18) val))))
(define bezier-bz        (dilambda (lambda (p) (p 19)) (lambda (p val) (set! (p 19) val))))
(define bezier-error     (dilambda (lambda (p) (p 20)) (lambda (p val) (set! (p 20) val))))
(define bezier-curvature (dilambda (lambda (p) (p 21)) (lambda (p val) (set! (p 21) val))))

(define* (make-bezier-path (path ()) (3d #t) polar (error 0.01) curvature)
  (list 'bezier-path () () () () () () () () () path 3d polar () () () () () () () error curvature))


;;; Path class for open bezier paths

(define initial-direction (dilambda (lambda (p) (p 22)) (lambda (p val) (set! (p 22) val))))
(define final-direction   (dilambda (lambda (p) (p 23)) (lambda (p val) (set! (p 23) val))))

(define* (make-open-bezier-path (path ()) (3d #t) polar (error 0.01) curvature 
				(initial-direction '(0.0 0.0 0.0)) (final-direction '(0.0 0.0 0.0)))
  (list 'open-bezier-path () () () () () () () () () path 3d polar () () () () () () () error curvature initial-direction final-direction))



;;;
;;; Generic defining function (for open, closed, polar and cartesian paths)
;;;

(define make-path
  (let ()

    (define (make-path-error-checks path closed initial-direction final-direction)
      ;; some sanity checks
      (if (null? path)
	  (error 'mus-error "Can't define a path with no points in it~%"))
      (when closed 
	(if initial-direction
	    (error 'mus-error "Can't specify initial direction ~A for a closed path ~A~%" initial-direction path))
	(if final-direction
	    (error 'mus-error "Can't specify final direction ~A for a closed path ~A~%" final-direction path))
	(let ((closed? (if (pair? (car path))
			   (let ((start (car path))
				 (end (car (last path))))
			     (and (= (car start) (car end))
				  (= (cadr start) (cadr end))
				  (or (not path-3d)
				      (= (third start) (third end)))))
			   (let ((end (last path (if path-3d 3 2))))
			     (and (= (car path) (car end))
				  (= (cadr path) (cadr end))
				  (or (not path-3d)
				      (= (third path) (third end))))))))
	  (if (not closed?)
	      (error 'mus-error "Closed path ~A is not closed~%" path)))))

    (lambda* (path
	      (3d path-3d)
	      polar
	      closed
	      curvature
	      (error 0.01) ; this name ("error") is a bad idea -- it means we can't call the real error function (this is Scheme, not CL)
	      ;; only for open paths
	      initial-direction
	      final-direction)
  
      (make-path-error-checks path closed initial-direction final-direction) ; the error check uses path-3d -- was 3d intended?
      
      ;; create the path structure
      (if closed
	  (make-bezier-path
	   :path path
	   :3d 3d
	   :polar polar
	   :curvature curvature
	   :error error)
	  (make-open-bezier-path
	   :path path
	   :3d 3d
	   :polar polar
	   :curvature curvature
	   :error error
	   :initial-direction initial-direction
	   :final-direction final-direction)))))


;;; Some convenient abbreviations

(define* (make-polar-path path
			  (3d path-3d)
			  closed
			  curvature
			  (error 0.01)
			  ;; only for open paths
			  initial-direction
			  final-direction)
  (if closed
      (make-path :path path
		 :3d 3d
		 :polar #t
		 :closed closed
		 :curvature curvature
		 :error error)
      (make-path :path path
		 :3d 3d
		 :polar #t
		 :closed closed
		 :curvature curvature
		 :error error
		 :initial-direction initial-direction
		 :final-direction final-direction)))

(define* (make-closed-path path
			   (3d path-3d)
			   polar
			   curvature
			   (error 0.01))
  (make-path :path path
	     :3d 3d
	     :polar polar
	     :closed #t
	     :curvature curvature
	     :error error))



;;;
;;; Parse a path and transform it into cartesian coordinates
;;;

(define (not-parsed path)
  (null? (bezier-x path)))


;;; Parse a set of 2d or 3d points into the separate coordinates

(define parse-cartesian-coordinates 
  (let ((+documentation+ "(parse-cartesian-coordinates points 3d) parses a set of 2d or 3d points into the separate coordinates"))
    (lambda (points 3d)
      (if (pair? (car points))
	  ;; decode a list of lists into x:y:z:v components
	  ;; 3d -> t [default]
	  ;;   '((x0 y0 z0 v0) (x1 y1 z1 v1)...(xn yn zn vn))
	  ;;   '((x0 y0 z0) (x1 y1 z1)...(xn yn zn))
	  ;;   '((x0 y0) (x1 y1)...(xn yn)) 
	  ;;      v: relative velocity
	  ;;      x, y, z: coordinates of source [missing z's assumed 0.0]
	  ;; 3d -> nil
	  ;;   '((x0 y0 v0) (x1 y1 v1)...(xn yn vn))
	  ;;   '((x0 y0) (x1 y1)...(xn yn))
	  ;;      v: relative velocity
	  ;;      x, y, z: coordinates of source [missing z's assumed 0.0]
	  (let ((v ())
		(x ())
		(y ())
		(z ()))
	    (for-each
	     (lambda (p)
	       (set! x (cons (car p) x))
	       (set! y (cons (cadr p) y))
	       (set! z (cons (if 3d (or (third p) 0.0) 0.0) z))
	       (set! v (cons ((if 3d fourth third) p) v)))
	     points)
	    (map reverse (list x y z v)))
	  
	  ;; decode a plain list
	  (let ((px ())
		(py ())
		(len (length points)))
	    (if 3d
		;; it's a three dimensional list
		;; '(x0 y0 z0 x1 y1 z1 ... xn yn zn)
		;;     x, y, z: coordinates of source
		(do ((pz ())
		     (i 0 (+ i 3)))
		    ((>= i len)
		     (list (reverse px) (reverse py) (reverse pz) (make-list (length px) #f)))   
		  (set! px (cons (points i) px))
		  (set! py (cons (points (+ i 1)) py))
		  (set! pz (cons (points (+ i 2)) pz)))
		;; it's a two dimensional list
		;; '(x0 y0 x1 y1 ... xn yn)
		;;     x, y, z: coordinates of source [missing z's assumed 0.0]
		(do ((i 0 (+ i 2)))
		    ((>= i len)
		     (list (reverse px) (reverse py) (make-list (length px) 0.0) (make-list (length px) #f)))   
		  (set! px (cons (points i) px))
		  (set! py (cons (points (+ i 1)) py)))))))))

;;; Parse a set of 2d or 3d polar points into the separate coordinates

(define parse-polar-coordinates 
  (let ((+documentation+ "(parse-polar-coordinates points 3d) parses a polar path"))
    (lambda (points 3d)
      (let ((x ())
	    (y ()))
	(if (pair? (car points))
	    ;; decode a list of lists of d:a:e:v into x:y:z:v components
	    ;; 3d --> t [default]
	    ;;   '((d0 a0 e0 v0) (d1 a1 e1 v1)...(dn an en vn))
	    ;;   '((d0 a0 e0) (d1 a1 e1)...(dn an en))
	    ;;   '((d0 a0) (d1 a1)...(dn an))  
	    ;; 3d --> nil
	    ;;   '((d0 a0 v0) (d1 a1 v1)...(dn an vn))
	    ;;   '((d0 a0) (d1 a1)...(dn an))
	    ;;      v: velocity
	    ;;      d: distance
	    ;;      a: azimut angle
	    ;;      e: elevarion angle [missing elevations assumed 0.0]
	    (let ((z ())
		  (v ()))
	      (for-each
	       (lambda (p)
		 (let ((d (car p))
			(evec (let ((e (if (and 3d (pair? (cddr p))) (caddr p) 0.0)))
				(cis (/ (* e 2 pi) dlocsig-one-turn)))))
		   (let ((dxy (* d (real-part evec)))
			 (avec (let ((a (cadr p)))
				 (cis (/ (* a 2 pi) dlocsig-one-turn)))))
		     (set! x (cons (* dxy (imag-part avec)) x))
		     (set! y (cons (* dxy (real-part avec)) y)))
		   (set! z (cons (* d (imag-part evec)) z))
		   (set! v (cons ((if 3d fourth third) p) v))))
	       points)
	      (map reverse (list x y z v)))
	    
	    ;; decode a list of d:a:e components
	    (let ((len (length points)))
	      (if 3d
		  ;; decode a three dimensional list
		  ;;   '(d0 a0 e0 d1 a1 e1 ... dn an en)
		  ;;      d: distance
		  ;;      a: azimut angle
		  ;;      e: elevarion angle [missing elevations assumed 0.0]
		  (do ((z ())
		       (i 0 (+ i 3)))
		      ((>= i len)
		       (list (reverse x) (reverse y) (reverse z) (make-list (length x) #f)))
		    (let* ((d (points i))
			   (evec (let ((e (points (+ i 2))))
				   (cis (/ (* e 2 pi) dlocsig-one-turn))))
			   (dxy (* d (real-part evec)))
			   (avec (let ((a (points (+ i 1))))
				   (cis (/ (* a 2 pi) dlocsig-one-turn)))))
		      (set! x (cons (* dxy (imag-part avec)) x))
		      (set! y (cons (* dxy (real-part avec)) y))
		      (set! z (cons (* d (imag-part evec)) z))))
		  
		  ;; decode a two dimensional list
		  ;;   '(d0 a0 d1 a1 ... dn an)
		  ;;      d: distance
		  ;;      a: azimut angle
		  ;;      e: elevarion angle [missing elevations assumed 0.0]
		  (do ((i 0 (+ i 2)))
		      ((>= i len)
		       (list (reverse x) (reverse y) (make-list (length x) 0.0) (make-list (length x) #f)))     
		    (let ((d (points i))
			  (avec (let ((a (points (+ i 1))))
				  (cis (/ (* a 2 pi) dlocsig-one-turn)))))
		      (set! x (cons (* d (imag-part avec)) x))
		      (set! y (cons (* d (real-part avec)) y)))))))))))
		

(define (xparse-path xpath)
  (let ((polar (bezier-polar xpath))
	(points (bezier-path xpath))
	(3d (bezier-3d xpath)))
    (let ((vals ((if polar parse-polar-coordinates parse-cartesian-coordinates) points 3d)))
      (set! (bezier-x xpath) (car vals))
      (set! (bezier-y xpath) (cadr vals))
      (set! (bezier-z xpath) (caddr vals))
      (set! (bezier-v xpath) (cadddr vals))))
  (for-each
   (lambda (v)
     (if (and (real? v) 
	      (< v 0))
	 (error 'mus-error "velocities for path ~A must be all positive~%" (bezier-path xpath))))
   (bezier-v xpath))
  (reset-fit xpath))


;;;
;;; Bezier curve fitting auxiliary functions
;;;

;;; Pythagoras

(define distance 
  (let ((+documentation+ "(distance x y z) returns the euclidean distance of (x y z) from the origin"))
    (lambda (x y z)
      (sqrt (+ (* x x) (* y y) (* z z))))))

;;; Nearest point in a line

(define nearest-point 
  (let ((same (lambda (a0 b0 c0 a1 b1 c1)
		(and (= a0 a1) 
		     (= b0 b1)
		     (= c0 c1)))))
    (lambda (x0 y0 z0 x1 y1 z1 px py pz)
      (cond ((same x0 y0 z0 px py pz) (list x0 y0 z0))
	    ((same x1 y1 z1 px py pz) (list x1 y1 z1))
	    ((same x0 y0 z0 x1 y1 z1) (list x0 y0 z0))
	    (else (let ((xm0 (- x1 x0))
			(ym0 (- y1 y0))
			(zm0 (- z1 z0)))
		    (let ((ratio (let ((p (let ((xm1 (- px x0))
						(ym1 (- py y0))
						(zm1 (- pz z0))
						(vcos (lambda (a0 b0 c0 a1 b1 c1)
							(/ (+ (* a0 a1) (* b0 b1) (* c0 c1))
							   (* (distance a0 b0 c0) (distance a1 b1 c1))))))
					    (* (distance xm1 ym1 zm1)
					       (vcos xm0 ym0 zm0 xm1 ym1 zm1)))))
				    (/ p (distance xm0 ym0 zm0)))))
		      (list (+ x0 (* xm0 ratio))
			    (+ y0 (* ym0 ratio))
			    (+ z0 (* zm0 ratio))))))))))

;;; Bezier curve fitting auxilliary functions

(define path-ak-even #f)
(define path-ak-odd #f)
(define path-maxcoeff 8)

(define (make-a-even)
  (let ((g (let ((path-gtab #f))
	     (lambda (m)
	       (if (not path-gtab)
		   (begin
		     (set! path-gtab (make-vector path-maxcoeff))
		     (set! (path-gtab 0) 1)
		     (set! (path-gtab 1) -4)
		     (do ((i 2 (+ i 1)))
			 ((= i path-maxcoeff))
		       (set! (path-gtab i) (- (* -4 (path-gtab (- i 1)))
					      (path-gtab (- i 2)))))))
	       (path-gtab m)))))
  
  (set! path-ak-even (make-vector (- path-maxcoeff 1)))
  (do ((m 1 (+ 1 m)))
      ((= m path-maxcoeff))
    (set! (path-ak-even (- m 1)) (make-vector m))
    (do ((k 1 (+ k 1)))
	((> k m))
      (set! ((path-ak-even (- m 1)) (- k 1)) (* 1.0 (/ (- (g (- m k))) (g m))))))))

(define (make-a-odd)
  (let ((f (let ((path-ftab #f))
	     (lambda (m)
	       (if (not path-ftab)
		   (begin
		     (set! path-ftab (make-vector path-maxcoeff))
		     (set! (path-ftab 0) 1)
		     (set! (path-ftab 1) -3)
		     (do ((i 2 (+ i 1)))
			 ((= i path-maxcoeff))
		       (set! (path-ftab i) (- (* -4 (path-ftab (- i 1)))
					      (path-ftab (- i 2)))))))
	       (path-ftab m)))))
  
  (set! path-ak-odd (make-vector (- path-maxcoeff 1)))
  (do ((m 1 (+ 1 m)))
      ((= m path-maxcoeff))
    (set! (path-ak-odd (- m 1)) (make-vector m))
    (do ((k 1 (+ k 1)))
	((> k m))
      (set! ((path-ak-odd (- m 1)) (- k 1)) (* 1.0 (/ (- (f (- m k))) (f m))))))))

;;; Calculate bezier difference vectors for the given path
;;; (path-x (make-path '((-10 10)(0 5)(10 10))))

(define (fit path)
  (let ((n (- (length (bezier-x path )) 1)))
    (cond ((not (eq? (car path) 'open-bezier-path))
	   (let ((m (/ (- n (if (odd? n) 3 4)) 2))
		 ;; data points P(i)
		 (p (vector (apply vector (bezier-x path))
			    (apply vector (bezier-y path))
			    (apply vector (bezier-z path))))
		 ;; control points D(i)
		 (d (let ((maker (lambda () (make-vector n 0.0))))
		      (vector (maker) (maker) (maker))))
	     
		 (a-1 (lambda (k n)
			(if (odd? (min (+ (* path-maxcoeff 2) 1) n))
			    (begin
			      (if (not path-ak-odd) (make-a-odd))
			      ((path-ak-odd (/ (- n 3) 2)) (- k 1)))
			    (begin
			      (if (not path-ak-even) (make-a-even))
			      ((path-ak-even (/ (- n 4) 2)) (- k 1))))))
		 
		 (xvector-ref (lambda (z j i)
				(z j (if (> i (- n 1))
					 (- i n)
					 (if (< i 0) 
					     (+ i n) i))))))
	     (do ((i 0 (+ i 1)))
		 ((= i n))
	       (do ((k 1 (+ k 1)))
		   ((> k m))
		 (do ((a 0 (+ 1 a)))
		     ((> a 2))
		   (set! (d a i)
			 (+ (d a i)
			    (* (a-1 k n)
			       (- (xvector-ref p a (+ i k))
				  (xvector-ref p a (- i k)))))))))
	     (if (bezier-curvature path)
		 (do ((i 0 (+ i 1)))
		     ((= i n))
		   (set! (d 0 i) (* (d 0 i) (bezier-curvature path)))
		   (set! (d 1 i) (* (d 1 i) (bezier-curvature path)))
		   (set! (d 2 i) (* (d 2 i) (bezier-curvature path)))))
	     (list (- n 1) p d)))
	  (else
	   (let ((m (- n 1))
		 ;; data points P(i)
		 (p (vector (apply vector (bezier-x path))
			    (apply vector (bezier-y path))
			    (apply vector (bezier-z path))))
		 ;; control points D(i)
		 (d (vector (make-vector (+ n 1) 0.0) 
			    (make-vector (+ n 1) 0.0) 
			    (make-vector (+ n 1) 0.0)))
	     
		 (ac (lambda (k n)
		       (let ((un (min n path-maxcoeff)))
			 (if (not path-ak-even) (make-a-even))
			 ((path-ak-even (- un 2)) (- k 1))))))
		 
	     (define (ref z j i) ; refers to 'd
	       (cond ((> i n) (z j (- i n)))
		     ((< i 0) (z j (+ i n)))
		     ((= i n) (- (z j n) (d j n)))
		     ((= i 0) (+ (z j 0) (d j 0)))
		     (else    (z j i))))
	     
	     ;; forced initial direction
	     (if (initial-direction path)
		 (begin
		   (set! (d 0 0) (car (initial-direction path)))
		   (set! (d 1 0) (cadr (initial-direction path)))
		   (set! (d 2 0) (third (initial-direction path))))
		 (begin
		   (set! (d 0 0) 0.0)
		   (set! (d 1 0) 0.0)
		   (set! (d 2 0) 0.0)))
	     
	     ;; forced final direction
	     (if (final-direction path)
		 (begin
		   (set! (d 0 n) (car (final-direction path)))
		   (set! (d 1 n) (cadr (final-direction path)))
		   (set! (d 2 n) (caddr (final-direction path))))
		 (begin
		   (set! (d 0 n) 0.0)
		   (set! (d 1 n) 0.0)
		   (set! (d 2 n) 0.0)))
	     
	     ;; calculate fit
	     (do ((i 1 (+ i 1)))
		 ((= i n))
	       (do ((k 1 (+ k 1)))
		   ((> k (min m (- path-maxcoeff 1))))
		 (let ((d0 (d 0 i))
		       (d1 (d 1 i))
		       (d2 (d 2 i)))
		   (set! (d 0 i) (+ d0 (* (ac k n)
					  (- (ref p 0 (+ i k))
					     (ref p 0 (- i k))))))
		   (set! (d 1 i) (+ d1 (* (ac k n)
					  (- (ref p 1 (+ i k))
					     (ref p 1 (- i k))))))
		   (set! (d 2 i) (+ d2 (* (ac k n)
					  (- (ref p 2 (+ i k))
					     (ref p 2 (- i k)))))))))
	     (list n p d))))))

;;; Calculate bezier control points for the given open path

(define (not-fitted path)
  (null? (bezier-bx path)))

(define (reset-fit path)
  (set! (bezier-bx path) ())
  (set! (bezier-by path) ())
  (set! (bezier-bz path) ())
  (reset-rendering path))

(define (fit-path path)
  (cond ((eq? (car path) 'open-bezier-path)
	 (if (not-parsed path)
	     (xparse-path path))
	 
	 (let ((points (length (bezier-x path))))
	   (cond ((> points 2)
		  (let ((vals (fit path)))
		    (let ((n (car vals))
			  (p (cadr vals))
			  (d (caddr vals)))
		      (let ((c (bezier-curvature path))
			    (cs (make-vector n)))
			;; setup the curvatures array
			(cond ((memq c '(#f ()))                          ; no curvature specified, default is 1.0
			       (do ((i 0 (+ i 1)))
				   ((= i n))
				 (set! (cs i) (list 1.0 1.0))))
			      ((number? c)                    ; same curvature for all segments
			       (do ((i 0 (+ i 1)))
				   ((= i n))
				 (set! (cs i) (list c c))))
			      ((and (pair? c) (= n (length c)))   ; list of curvatures
			       (let ((i 0))
				 (for-each
				  (lambda (ci)
				    (set! (cs i) (if (pair? ci) 
						     (if (= (length ci) 2)
							 ci
							 (error 'mus-error "curvature sublist must have two elements ~A~%" ci))
						     (list ci ci)))
				    (set! i (+ i 1)))
				  c)))
			      (else (error 'mus-error "bad curvature argument ~A to path, need ~A elements~%" c n)))
			
			;; calculate control points
			(do ((xc ())
			     (yc ())
			     (zc ())
			     (i 0 (+ i 1)))
			    ((= i n)
			     (set! (bezier-bx path) (reverse xc))
			     (set! (bezier-by path) (reverse yc))
			     (set! (bezier-bz path) (reverse zc)))
			     
			  (set! xc (cons (list (p 0 i)
					       (+ (p 0 i) (* (d 0 i) (car (cs i))))
					       (- (p 0 (+ i 1)) (* (d 0 (+ i 1)) (cadr (cs i))))
					       (p 0 (+ i 1))) xc))
			  (set! yc (cons (list (p 1 i)
					       (+ (p 1 i) (* (d 1 i) (car (cs i))))
					       (- (p 1 (+ i 1)) (* (d 1 (+ i 1)) (cadr (cs i))))
					       (p 1 (+ i 1))) yc))
			  (set! zc (cons (list (p 2 i)
					       (+ (p 2 i) (* (d 2 i) (car (cs i))))
					       (- (p 2 (+ i 1)) (* (d 2 (+ i 1)) (cadr (cs i))))
					       (p 2 (+ i 1))) zc)))))))
		 
		 ((= points 2)
		  ;; just a line, stays a line
		  (let ((x1 (car (bezier-x path)))
			(x2 (cadr (bezier-x path)))
			(y1 (car (bezier-y path)))
			(y2 (cadr (bezier-y path)))
			(z1 (car (bezier-z path)))
			(z2 (cadr (bezier-z path))))
		    (set! (bezier-bx path) (list (list x1 x1 x2 x2)))
		    (set! (bezier-by path) (list (list y1 y1 y2 y2)))
		    (set! (bezier-bz path) (list (list z1 z1 z2 z2)))))
		 ((= points 1)
		  ;; just one point, bezier won't do much here
		  (set! (bezier-bx path) ())
		  (set! (bezier-by path) ())
		  (set! (bezier-bz path) ())))

	   (reset-rendering path)))
	
	(else
	 (if (not-parsed path)
	     (xparse-path path))
	 
	 (if (> (length (bezier-x path)) 4)
	     (let ((vals (fit path)))
	       (do ((n (car vals))
		    (p (cadr vals))
		    (d (caddr vals))
		    ;; enough points, fit path
		    (xc ())
		    (yc ())
		    (zc ())
		    (i 0 (+ i 1)))
		   ((= i n)
		    (set! (bezier-bx path) (append (reverse xc) (list (list (p 0 n)
									    (+ (p 0 n) (d 0 n))
									    (- (p 0 0) (d 0 0))
									    (p 0 0)))))
		    (set! (bezier-by path) (append (reverse yc) (list (list (p 1 n)
									    (+ (p 1 n) (d 1 n))
									    (- (p 1 0) (d 1 0))
									    (p 1 0)))))
		    (set! (bezier-bz path) (append (reverse zc) (list (list (p 2 n)
									    (+ (p 2 n) (d 2 n))
									    (- (p 2 0) (d 2 0))
									    (p 2 0))))))
		 (set! xc (cons (list (p 0 i)
				      (+ (p 0 i) (d 0 i))
				      (- (p 0 (+ i 1)) (d 0 (+ i 1)))
				      (p 0 (+ i 1))) xc))
		 (set! yc (cons (list (p 1 i)
				      (+ (p 1 i) (d 1 i))
				      (- (p 1 (+ i 1)) (d 1 (+ i 1)))
				      (p 1 (+ i 1))) yc))
		 (set! zc (cons (list (p 2 i)
				      (+ (p 2 i) (d 2 i))
				      (- (p 2 (+ i 1)) (d 2 (+ i 1)))
				      (p 2 (+ i 1))) zc))))
	     
	     ;; not enough points to fit a closed path
	     (do ((xc ())
		  (yc ())
		  (zc ())
		  (len (min (length (bezier-x path)) (length (bezier-y path)) (length (bezier-z path))))
		  (i 0 (+ i 1)))
		 ((>= i len)
		  (format *stderr* "[fit-path:closed-path] not enough points to do bezier fit (~A points)" len)
		  (set! (bezier-bx path) (reverse xc))
		  (set! (bezier-by path) (reverse yc))
		  (set! (bezier-bz path) (reverse zc)))
	       (let ((x1 ((bezier-x path) i))
		     (x2 ((bezier-x path) (+ i 1)))
		     (y1 ((bezier-y path) i))
		     (y2 ((bezier-y path) (+ i 1)))
		     (z1 ((bezier-z path) i))
		     (z2 ((bezier-z path) (+ i 1))))
		 (set! xc (cons (list x1 x1 x2 x2) xc))
		 (set! yc (cons (list y1 y1 y2 y2) yc))
		 (set! zc (cons (list z1 z1 z2 z2) zc)))))
	 (reset-rendering path))))



;;;;;;;;;;;;;;;;;
;;; Literal paths
;;;;;;;;;;;;;;;;;


(define literal-points (dilambda (lambda (p) (p 10)) (lambda (p val) (set! (p 10) val))))
(define literal-3d     (dilambda (lambda (p) (p 11)) (lambda (p val) (set! (p 11) val))))
(define literal-polar  (dilambda (lambda (p) (p 12)) (lambda (p val) (set! (p 12) val))))

;;; Generic literal path creation function
(define* (make-literal-path (points ()) (3d path-3d) polar)
  (list 'literal-path () () () () () () () () () points 3d polar))

;;; Specific polar literal path creation function
(define* (make-literal-polar-path (points ()) (3d path-3d))
  (make-literal-path points 3d #t))


;;;;;;;;;;;
;;; Spirals
;;;;;;;;;;;

(define spiral-start-angle (dilambda (lambda (p) (p 13)) (lambda (p val) (set! (p 13) val))))
(define spiral-total-angle (dilambda (lambda (p) (p 14)) (lambda (p val) (set! (p 14) val))))
(define spiral-step-angle  (dilambda (lambda (p) (p 15)) (lambda (p val) (set! (p 15) val))))
(define spiral-turns       (dilambda (lambda (p) (p 16)) (lambda (p val) (set! (p 16) val))))
(define spiral-distance    (dilambda (lambda (p) (p 17)) (lambda (p val) (set! (p 17) val))))
(define spiral-height      (dilambda (lambda (p) (p 18)) (lambda (p val) (set! (p 18) val))))
(define spiral-velocity    (dilambda (lambda (p) (p 19)) (lambda (p val) (set! (p 19) val))))

(define* (make-spiral-path (start-angle 0.0)
			   total-angle
			   step-angle
			   (turns ())
			   (distance '(0 10 1 10))
			   (height '(0 0 1 0))
			   (velocity '(0 1 1 1)))
  (if (and total-angle (pair? turns))
      (error 'mus-error "can't specify total-angle [~A] and turns [~A] at the same time for the spiral path~%" total-angle turns))
  
  (list 'spiral-path () () () () () () () () () () path-3d #f 
	start-angle total-angle 
	(or step-angle (/ dlocsig-one-turn 100))
	turns distance height velocity))



;;; Transform a Bezier control point fit to a linear segment approximation

(define (bezier-render path)
  (if (not-fitted path)
      (fit-path path))
  (let ((xrx ()) (xry ()) (xrz ()) (xrv ()))
    
    ;; Create linear segment approximations of the bezier segments
    ;; make sure there are initial and final velocity values
    (if (not (pair? (bezier-v path)))
	(set! (bezier-v path) (list 1 1))
	(if (not (car (bezier-v path)))
	    (begin
	      (set! ((bezier-v path) 0) 1)
	      (set! ((bezier-v path) (- (length (bezier-v path)) 1)) 1))))
    
    ;; only one point means no movement, static source
    (if (= (length (bezier-x path)) 1)
	(begin
	  (set! (path-rx path) (bezier-x path))
	  (set! (path-ry path) (bezier-y path))
	  (set! (path-rz path) (bezier-z path))
	  (set! (path-rt path) (list 0.0))
	  (reset-transformation path)) ; after?
	(begin
	  (let ((len (length (bezier-bx path))))
					;(path-x (make-path '((-10 10)(0 5)(10 10))))
	    ;; render the path only if it has at least two points
	    (do ((i 0 (+ i 1)))
		((= i len))
	      (let ((x-bz ((bezier-bx path) i))
		    (y-bz ((bezier-by path) i))
		    (z-bz ((bezier-bz path) i)))
		(let ((vi-bz ((bezier-v path) i))
		      (vf-bz ((bezier-v path) (+ i 1)))
		      (xi-bz (car x-bz))
		      (xf-bz (x-bz (- (length x-bz) 1)))
		      (yi-bz (car y-bz))
		      (yf-bz (y-bz (- (length y-bz) 1)))
		      (zi-bz (car z-bz))
		      (zf-bz (z-bz (- (length z-bz) 1))))

		  (define berny 
		    ;; Create a linear segment rendering of a bezier segment
		    (let ((bezier-point 
			   (lambda (u c)
			     ;; Evaluate a point at parameter u in bezier segment
			     (let ((u1 (- 1 u))
				   (cr (vector (make-vector 3 0.0) (make-vector 3 0.0) (make-vector 3 0.0))))
			       (do ((j 0 (+ j 1)))
				   ((= j 3))
				 (set! (cr 0 j) (+ (* u1 (c 0 j)) (* u (c 0 (+ j 1)))))
				 (set! (cr 1 j) (+ (* u1 (c 1 j)) (* u (c 1 (+ j 1)))))
				 (set! (cr 2 j) (+ (* u1 (c 2 j)) (* u (c 2 (+ j 1))))))
			       (do ((i 1 (- i 1)))
				   ((< i 0))
				 (do ((j 0 (+ j 1)))
				     ((> j i))
				   (set! (cr 0 j) (+ (* u1 (cr 0 j)) (* u (cr 0 (+ j 1)))))
				   (set! (cr 1 j) (+ (* u1 (cr 1 j)) (* u (cr 1 (+ j 1)))))
				   (set! (cr 2 j) (+ (* u1 (cr 2 j)) (* u (cr 2 (+ j 1)))))))
			       (list (cr 0 0)
				     (cr 1 0)
				     (cr 2 0))))))
			   (lambda (xl yl zl xh yh zh ul u uh c err)
			     (let ((vals (bezier-point u c)))
			       (let ((x (car vals))
				     (y (cadr vals))
				     (z (caddr vals)))
				 (let ((val1 (nearest-point xl yl zl xh yh zh x y z)))
				   (let ((xn (car val1))
					 (yn (cadr val1))
					 (zn (caddr val1)))
				     (if (<= (distance (- xn x) (- yn y) (- zn z)) err)
					 (list () () ())
					 (let ((val2 (berny xl yl zl x y z ul (/ (+ ul u) 2) u c err))
					       (val3 (berny x y z xh yh zh u (/ (+ u uh) 2) uh c err)))
					   (let ((xi (car val2))
						 (yi (cadr val2))
						 (zi (caddr val2))
						 (xj (car val3))
						 (yj (cadr val3))
						 (zj (caddr val3)))
					     (list (append xi (cons x xj))
						   (append yi (cons y yj))
						   (append zi (cons z zj)))))))))))))
		  
		  (let ((vals (berny xi-bz yi-bz zi-bz xf-bz yf-bz zf-bz 0.0 0.5 1.0 
				     (vector (apply vector x-bz)
					     (apply vector y-bz)
					     (apply vector z-bz))
				     (bezier-error path))))
		      ;; approximate the bezier curve with linear segments
		    (set! xry (append xry (cons yi-bz (cadr vals))))
		    (set! xrz (append xrz (cons zi-bz (caddr vals))))
		    (let ((xs (car vals)))
		      (set! xrx (append xrx (cons xi-bz xs)))
		      ;; accumulate intermediate unknown velocities as nils
		      (set! xrv (append xrv (cons vi-bz (make-list (length xs) #f))))
		      (if (= i (- len 1))
			  (begin
			    ;; add the last point
			    (set! xrx (append xrx (list xf-bz)))
			    (set! xry (append xry (list yf-bz)))
			    (set! xrz (append xrz (list zf-bz)))
			    (set! xrv (append xrv (list vf-bz)))))))))))
	  
	  ;; calculate times for each velocity segment
	  (let ((ti 0)
		(times (list 0)))
	    (let ((len (- (length xrx) 1))
		  (xseg (list (xrx 0)))
		  (yseg (list (xry 0)))
		  (zseg (list (xrz 0)))
		  (vseg (list (xrv 0)))
		  (vi (xrv 0)))
	      (do ((i 0 (+ i 1)))
		  ((= i len))
		(let ((x (xrx (+ i 1)))
		      (y (xry (+ i 1)))
		      (z (xrz (+ i 1)))
		      (v (xrv (+ i 1))))
		  (set! xseg (append xseg (list x)))
		  (set! yseg (append yseg (list y)))
		  (set! zseg (append zseg (list z)))
		  (set! vseg (append vseg (list v)))
		  
		  (when v
		    (let ((dseg ())
			  (sum 0.0)
			  (len (- (length xseg) 1)))
		      (do ((i 0 (+ i 1)))
			  ((= i len))
			(let ((xsi (xseg i))
			      (ysi (yseg i))
			      (zsi (zseg i))
			      (xsf (xseg (+ i 1)))
			      (ysf (yseg (+ i 1)))
			      (zsf (zseg (+ i 1))))
			  
			  (set! sum (+ sum (distance (- xsf xsi) (- ysf ysi) (- zsf zsi))))
			  (set! dseg (cons sum dseg))))
		      
		      (let ((df (car dseg)))
			(set! dseg (reverse dseg))
			(let ((tseg ()))
			  (let ((a (/ (* (- v vi) (+ v vi)) df 4)))
			    (if (= vi 0.0) (set! vi 1))
			    (for-each
			     (lambda (d)
			       (let ((seg (+ ti (if (= v vi)
						    (/ d vi)
						    (/ (- (sqrt (+ (* vi vi) (* 4 a d))) vi) (* 2 a))))))
				 (set! tseg (cons seg tseg))))
			     dseg))
			  (set! ti (car tseg))
			  (set! times (append times (reverse tseg))))))
		    (set! xseg (list x))
		    (set! yseg (list y))
		    (set! zseg (list z))
		    (set! vseg (list v))
		    (set! vi v)))))
	    
	    (set! (path-rx path) xrx)
	    (set! (path-ry path) xry)
	    (set! (path-rz path) xrz)
	    (set! (path-rt path) 
		  (let ((tf (times (- (length times) 1)))
			(val ()))
		    (for-each
		     (lambda (ti)
		       (set! val (cons (/ ti tf) val)))
		     times)
		    (reverse val)))
	    (reset-transformation path))))))

;; (set! p (make-path '((-10 10 0 0) (0 5 0 1) (10 10 0 0)) :error 0.01))
;; (set! p (make-path '((-10 10 0 1) (-7 7 0 0.9) (0 5 0 0) (7 7 0 0.2) (10 10 0 1)) :error 0.001))
;; (with-sound(:channels 4 :play #f) (sinewave 0 2 880 0.5 :path p))


(define (literal-render path)
  
  ;; Render a user-defined literal path from the data points
  
  ;; decode the points into coordinates
  (let ((points (literal-points path))
	(3d (literal-3d path))
	(polar (literal-polar path)))
    (let ((vals ((if polar parse-polar-coordinates parse-cartesian-coordinates) points 3d)))
      (set! (path-rx path) (car vals))
      (set! (path-ry path) (cadr vals))
      (set! (path-rz path) (caddr vals))
      (set! (path-rv path) (cadddr vals)))
    
    ;; make sure there are initial and final velocity values
    (if (not (car (path-rv path)))
	(begin
	  (set! ((path-rv path) 0) 1)
	  (set! ((path-rv path) (- (length (path-rv path)) 1)) 1)))
    
    ;; only one point means no movement, static source
    (if (= (length (path-rx path)) 1)
	(begin
	  (set! (path-rt path) (list 0.0))
	  (reset-transformation path))
	(let ((rx (path-rx path))
	      (ry (path-ry path))
	      (rz (path-rz path))
	      (rv (path-rv path)))
	  (let ((xseg (list (car rx)))
		(yseg (list (car ry)))
		(zseg (list (car rz)))
		(vseg (list (car rv)))
		(vi (car rv))
		(len (length rx))
		(ti 0))
	    (let ((times (list ti)))
	      (do ((i 1 (+ i 1)))
		  ((= i len))
		(let ((x (rx i))
		      (y (ry i))
		      (z (rz i))
		      (v (rv i)))
		  (set! xseg (append xseg (list x)))
		  (set! yseg (append yseg (list y)))
		  (set! zseg (append zseg (list z)))
		  (set! vseg (append vseg (list v)))
		  
		  (when (number? v)
		    (let ((sofar 0.0)
			  (dseg ())
			  (len (- (length xseg) 1)))
		      (do ((i 0 (+ i 1)))
			  ((= i len))
			(let ((xsi (xseg i))
			      (ysi (yseg i))
			      (zsi (zseg i))
			      (xsf (xseg (+ i 1)))
			      (ysf (yseg (+ i 1)))
			      (zsf (zseg (+ i 1))))
			  (set! sofar (+ sofar (distance (- xsf xsi) (- ysf ysi) (- zsf zsi))))
			  (set! dseg (cons sofar dseg))))
		      (let ((df (car dseg)))
			(set! dseg (reverse dseg))
			(let ((tseg ()))
			  (let ((a (/ (* (- v vi) (+ v vi)) df 4)))
			    (for-each
			     (lambda (d)
			       (let ((seg (+ ti (if (= v vi)
						    (/ d vi)
						    (/ (- (sqrt (+ (* vi vi) (* 4 a d))) vi) (* 2 a))))))
				 (set! tseg (cons seg tseg))))
			     dseg))
			  (set! ti (car tseg))
			  (set! times (append times (reverse tseg))))))
		    (set! xseg (list x))
		    (set! yseg (list y))
		    (set! zseg (list z))
		    (set! vseg (list v))
		    (set! vi v))))
	      
	      (set! (path-rt path) (let ((val ())
					 (tf (times (- (length times) 1))))
				     (for-each
				      (lambda (ti)
					(set! val (cons (/ ti tf) val)))
				      times)
				     (reverse val)))
	      (reset-transformation path)))))))

(define (spiral-render path)
  ;; Render a spiral path from the object data
  
  (let* ((start (/ (* (spiral-start-angle path) 2 pi) dlocsig-one-turn))
	 (total (if (spiral-total-angle path)
		    (/ (* (spiral-total-angle path) 2 pi) dlocsig-one-turn)
		    (if (spiral-turns path)
			(* (spiral-turns path) 2 pi)
			(error 'mus-error "a spiral-path needs either a total-angle or turns, none specified~%"))))
	 (step (let ((steps (abs (/ (* total dlocsig-one-turn) (* (spiral-step-angle path) 2 pi)))))
		 (/ total (ceiling steps)
		  (if (< (spiral-step-angle path) 0) -1 1))))
	 (xdistance (x-norm (spiral-distance path) total))
	 (height (x-norm (spiral-height path) total)))
    (let ((x ())
	  (y ())
	  (z ())
	  (len (+ 1 (round (abs (/ total step))))))
      (do ((i 0 (+ i 1))
	   (angle start (+ angle step)))
	  ((>= i len))
	(let ((xy (cis angle))
	      (d (envelope-interp angle xdistance)))
	  (set! x (cons (* d (imag-part xy)) x))
	  (set! y (cons (* d (real-part xy)) y))
	  (set! z (cons (envelope-interp angle height) z))))
      
      (set! x (reverse x))
      (set! y (reverse y))
      (set! z (reverse z))
      
      (let ((dp ())
	    (len (- (length x) 1))
	    (sofar 0.0))
	(do ((i 0 (+ i 1)))
	    ((>= i len))
	  (let ((xi (x i))
		(xf (x (+ i 1)))
		(yi (y i))
		(yf (y (+ i 1)))
		(zi (z i))
		(zf (z (+ i 1))))
	    (set! sofar (+ sofar (distance (- xf xi) (- yf yi) (- zf zi))))
	    (set! dp (cons sofar dp))))

	(set! dp (reverse dp))
	(let ((tp ())
	      (td 0)
	      (len (- (length dp) 1)))
	  (do ((i 0 (+ i 1)))
	      ((>= i len))
	    (let* ((di (dp i))
		   (df (dp (+ i 1)))
		   (vp (x-norm (spiral-velocity path) df)))
	      (let ((vi (envelope-interp di vp))
		    (vf (envelope-interp df vp)))
		(set! tp (cons td tp))
		(set! td (+ td (/ (- df di) (+ vi vf) 2))))))
	  (let ((tf (car tp)))
	    (set! tp (reverse tp))
	    (set! (path-rx path) x)
	    (set! (path-ry path) y)
	    (set! (path-rz path) z)
	    (let ((val ()))
	      (for-each
	       (lambda (ti)
		 (set! val (cons (/ ti tf) val)))
	       tp)
	      (set! (path-rt path) (reverse val)))))))
    
    (reset-transformation path)))


(define (render-path path)
  ((case (car path) 
    ((bezier-path open-bezier-path) bezier-render)
    ((literal-path)   	            literal-render)
    (else                           spiral-render))
   path))




;;;;;;;;;;;;;;;;;;;
;;; Transformations
;;;;;;;;;;;;;;;;;;;

;;; Transform a rendered path using scaling, translation and rotation 

;;; Transform a path (scaling + translation + rotation)

(define* (transform-path path
			 scaling
			 translation
			 rotation
			 rotation-center
			 (rotation-axis '(0.0 0.0 1.0)))
  
  ;; Derive a rotation matrix from an axis vector and an angle
  
  (define rotation-matrix 
    ;; translated from C routine by David Eberly
    ;; (http://www.magic-software.com/)

    (let ((normalize (lambda (a b c)
		       (let ((mag (distance a b c)))
			 (list (/ a mag) (/ b mag) (/ c mag))))))
    
      (lambda (x y z angle)
	(let ((rotate (vector (vector 0.0 0.0 0.0) (vector 0.0 0.0 0.0) (vector 0.0 0.0 0.0)))
	      (I (vector (vector 1.0 0.0 0.0) (vector 0.0 1.0 0.0) (vector 0.0 0.0 1.0)))
	      (A (let ((vals (normalize x y z)))
		   (let ((dx (car vals))
			 (dy (cadr vals))
			 (dz (caddr vals)))
		     (vector (vector 0.0 dz (- dy)) (vector (- dz) 0.0 dx) (vector dy (- dx) 0.0)))))
	      (AA (vector (vector 0.0 0.0 0.0) (vector 0.0 0.0 0.0) (vector 0.0 0.0 0.0)))
	      (sn (sin (- angle)))
	      (omcs (- 1 (cos angle)))) ; (cos (- angle)) == (cos angle)
	  
	  (do ((row 0 (+ 1 row)))
	      ((= row 3))
	    (do ((col 0 (+ 1 col)))
		((= col 3))
	      (set! (AA row col) 0.0)
	      (do ((mid 0 (+ 1 mid)))
		  ((= mid 3))
		(set! (AA row col)
		      (+ (AA row col)
			 (* (A row mid) 
			    (A mid col)))))))
	  
	  ;; rotation matrix is I+sin(angle)*A+[1-cos(angle)]*A*A 
	  (do ((row 0 (+ 1 row)))
	      ((= row 3))
	    (do ((col 0 (+ 1 col)))
		((= col 3))
	      (set! (rotate row col)
		    (+ (I row col)
		       (* sn (A row col))
		       (* omcs (AA row col))))))
	  rotate))))
  
  (if (not-rendered path)
      (render-path path))
  (if (or scaling translation rotation)
      ;; there's at least one transformation to execute
      (let* ((rotation (and (number? rotation)
			    (/ (* 2 pi rotation) dlocsig-one-turn)))
	     (matrix (and rotation (rotation-matrix (car rotation-axis)
						   (cadr rotation-axis)
						   (third rotation-axis)
						   rotation)))
	     (xc (path-x path))
	     (yc (path-y path))
	     (zc (path-z path)))
	(if (and rotation-center (not (= (length rotation-center) 3)))
	    (error 'mus-error "rotation center has to have all three coordinates~%"))
	(if (and rotation-axis (not (= (length rotation-axis) 3)))
	    (error 'mus-error "rotation axis has to have all three coordinates~%"))
	(do ((xtr ())
	      (ytr ())
	      (ztr ())
	      (len (length xc))
	      (i 0 (+ i 1)))
	    ((= i len)
	     (set! (path-tx path) (reverse xtr))
	     (set! (path-ty path) (reverse ytr))
	     (set! (path-tz path) (reverse ztr)))

	  (let ((x (xc i))
		(y (yc i))
		(z (zc i)))
	    (let ((xw x)
		  (yw y)
		  (zw z))
	      (when rotation
		;; rotating around non-triple zero? translate first
		(when rotation-center
		  (set! xw (- xw (car rotation-center)))
		  (set! yw (- yw (cadr rotation-center)))
		  (set! zw (- zw (third rotation-center))))
		;; rotation
		(let ((xr (+ (* (matrix 0 0) xw)
			     (* (matrix 1 0) yw)
			     (* (matrix 2 0) zw)))
		      (yr (+ (* (matrix 0 1) xw)
			     (* (matrix 1 1) yw)
			     (* (matrix 2 1) zw)))
		      (zr (+ (* (matrix 0 2) xw)
			     (* (matrix 1 2) yw)
			     (* (matrix 2 2) zw))))
		  (set! xw xr)
		  (set! yw yr)
		  (set! zw zr))
		;; rotating around non-triple zero? untranslate
		(when rotation-center
		  (set! xw (+ xw (car rotation-center)))
		  (set! yw (+ yw (cadr rotation-center)))
		  (set! zw (+ zw (third rotation-center)))))
	      
	      ;; scaling
	      (when scaling
		(set! xw (* xw (car scaling)))
		(if (cadr scaling)
		    (set! yw (* yw (cadr scaling))))
		(if (third scaling)
		    (set! zw (* zw (third scaling)))))
	      
	      ;; translating
	      (when translation
		(set! xw (+ xw (car translation)))
		(if (cadr translation)
		    (set! yw (+ yw (cadr translation))))
		(if (third translation)
		    (set! zw (+ zw (third translation)))))
	      
	      ;; collect the points
	      (set! xtr (cons xw xtr))
	      (set! ytr (cons yw ytr))
	      (set! ztr (cons zw ztr))))))
      
      (begin
	;; if there's no transformation just copy the rendered path
	(set! (path-tt path) (copy (path-rt path)))
	(set! (path-tx path) (copy (path-rx path)))
	(set! (path-ty path) (copy (path-ry path)))
	(set! (path-tz path) (copy (path-rz path)))))
  path)

;;; Scale a path

(define (scale-path path scaling)
  (transform-path path :scaling scaling))

;;; Translate a path

(define (translate-path path translation)
  (transform-path path :translation translation))

;;; Rotate a path

(define rotate-path 
  (let ((+documentation+ "rotate-path is a dlocsig function that rotates a dlocsig path"))
    (lambda* (path rotation rotation-center (rotation-axis '(0.0 0.0 1.0)))
      (transform-path path 
		      :rotation rotation 
		      :rotation-center rotation-center
		      :rotation-axis rotation-axis))))

;;; Mirror a path around an axis

(define* (mirror-path path (axis 'y) (around 0))
  (if (not-transformed path)
      (transform-path path))
  (let ((val ()))
    (if (eq? axis 'y)
	(begin
	  (for-each
	   (lambda (x)
	     (set! val (cons (- around x) val)))
	   (path-tx path))
	  (set! (path-tx path) (reverse val)))
	(begin
	  (for-each
	   (lambda (y)
	     (set! val (cons (- around y) val)))
	   (path-ty path))
	  (set! (path-ty path) (reverse val)))))
  path)

;;; Change the times of the rendered envelope so that the velocity is constant

(define constant-velocity 
  (let ((+documentation+ "constant-velocity is a dlocsig function that changes the times of the rendered envelope so that the velocity is constant"))
    (lambda (path)
      (if (not (path-rx path))
	  (render-path path))
      (reset-transformation path)
      (let* ((xcoords (path-x path))
	     (ycoords (path-y path))
	     (zcoords (path-z path))
	     (tcoords (path-time path))
	     (total-distance 
	      (do ((sum 0.0)
		   (len (length xcoords))
		   (i 0 (+ i 1)))
		  ((= i len) sum)
		(let ((x1 (xcoords i))
		      (x2 (xcoords (+ i 1)))
		      (y1 (ycoords i))
		      (y2 (ycoords (+ i 1)))
		      (z1 (zcoords i))
		      (z2 (zcoords (+ i 1))))
		  (set! sum (+ sum (distance (- x2 x1) (- y2 y1) (- z2 z1)))))))
	     (start-time (car tcoords))
	     (velocity (/ total-distance (- (tcoords (- (length tcoords) 1)) start-time)))
	     (now ()))
	(do ((dist 0.0)
	     (len (length xcoords))
	     (i 0 (+ i 1)))
	    ((= i len))
	  (let ((xp (xcoords i))
		(x (xcoords (+ i 1)))
		(yp (ycoords i))
		(y (ycoords (+ i 1)))
		(zp (zcoords i))
		(z (zcoords (+ i 1))))
	    (set! dist (+ dist (distance (- x xp) (- y yp) (- z zp))))
	    (set! now (cons (/ dist velocity) now))))
	(set! now (reverse now))
	(set! (path-rt path) (cons start-time now))
	(set! (path-tx path) (copy (path-rx path)))
	(set! (path-ty path) (copy (path-ry path)))
	(set! (path-tz path) (copy (path-rz path))))
      path)))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Create a new dlocsig structure



(define* (make-dlocsig start-time
		       duration
		       (path dlocsig-path)
		       (scaler dlocsig-scaler)
		       (direct-power dlocsig-direct-power)
		       (inside-direct-power dlocsig-inside-direct-power)
		       (reverb-power dlocsig-reverb-power)
		       (inside-reverb-power dlocsig-inside-reverb-power)
		       (reverb-amount dlocsig-reverb-amount)
		       (initial-delay dlocsig-initial-delay)
		       (unity-gain-dist dlocsig-unity-gain-distance)
		       (inside-radius dlocsig-inside-radius)
		       (minimum-segment-length dlocsig-minimum-segment-length)
		       (render-using dlocsig-render-using)
		       (ambisonics-h-order dlocsig-ambisonics-h-order)
		       (ambisonics-v-order dlocsig-ambisonics-v-order)
		       out-channels
		       rev-channels)
  
  (if (null? start-time)
      (error 'mus-error "a start time is required in make-dlocsig~%"))
  (if (null? duration)
      (error 'mus-error "a duration has to be specified in make-dlocsig~%"))
  
  ;; check to see if we have the right number of channels for b-format ambisonics
  (if (= render-using ambisonics)
      (begin
	(if (or (> ambisonics-h-order 3)
		(> ambisonics-v-order 3))
	    (error 'mus-error "ambisonics encoding is currently limited to third order components~%"))
	(let ((channels (ambisonics-channels ambisonics-h-order ambisonics-v-order)))
	  (if (< (or out-channels (mus-channels *output*)) channels)
	      (error 'mus-error "ambisonics number of channels is wrong, dlocsig needs ~A output channels for h:~A, v:~A order (current number is ~A)~%"
		     channels ambisonics-h-order ambisonics-v-order (or out-channels (mus-channels *output*)))))))
  
  (if (not out-channels)
      (if *output*
	  (set! out-channels (channels *output*))
	  (begin
	    (format () "warning: no *output*?  Will set out-channels to 2~%")
	    (set! out-channels 2))))
  (if (not rev-channels)
      (set! rev-channels (if *reverb* (channels *reverb*) 0)))
  
  (let (;; speaker configuration for current number of channels
	(speakers (and (not (= render-using ambisonics))
		       (get-speaker-configuration out-channels)))
	;; coordinates of rendered path
	(xpoints (path-x path))
	(ypoints (path-y path))
	(zpoints (path-z path))
	(tpoints (path-time path))
	
	;; array of gains -- envelopes
	(channel-gains (make-vector out-channels ()))
	(channel-rev-gains (make-vector out-channels ()))
	
	;; speaker output delays
	(max-out-delay 0.0)
	(out-delays (make-vector out-channels))
	
	;; speed of sound expressed in terms of path time coordinates
	(dly ())
	(doppler ())
	(prev-time #f)
	(prev-dist #f)
	(first-dist #f)
	(last-dist #f)
	(min-dist #f)
	(max-dist #f)
	(delay-hack 1)
	;; channel offsets in output stream for ambisonics
	;; (depends on horizontal and vertical order, default is h=1,v=1)
	(z-offset #f)
	(r-offset #f)
	(s-offset #f)
	(t-offset #f)
	(u-offset #f)
	(v-offset #f)
	(k-offset #f)
	(l-offset #f)
	(m-offset #f)
	(n-offset #f)
	(o-offset #f)
	(p-offset #f)
	(q-offset #f))
    
    (when (= render-using ambisonics)
      ;; calculate output channel offsets for ambisonics rendering
      (let ((offset 3))
	;; the default is at least a horizontal order of 1
	(if (>= ambisonics-v-order 1)
	    (begin
	      ;; add Z
	      (set! z-offset offset)
	      (set! offset (+ offset 1))))
	(if (>= ambisonics-v-order 2)
	    (begin
	      ;; add R S T
	      (set! r-offset offset)
	      (set! s-offset (+ offset 1))
	      (set! t-offset (+ offset 2))
	      (set! offset (+ offset 3))))
	(if (>= ambisonics-h-order 2)
	    (begin
	      ;; add U V
	      (set! u-offset offset)
	      (set! v-offset (+ offset 1))
	      (set! offset (+ offset 2))))
	(if (>= ambisonics-v-order 3)
	    (begin
	      ;; add K L M N O 
	      (set! k-offset offset)
	      (set! l-offset (+ offset 1))
	      (set! m-offset (+ offset 2))
	      (set! n-offset (+ offset 3))
	      (set! o-offset (+ offset 4))
	      (set! offset (+ offset 5))))
	(if (>= ambisonics-h-order 3)
	    (begin
	      ;; add P Q
	      (set! p-offset offset)
	      (set! q-offset (+ offset 1)))
	    (set! offset (+ offset 2)))))
    
    (define (dist->samples d) (round (* d (/ *clm-srate* dlocsig-speed-of-sound))))
    ;; (define (dist->seconds d) (/ d dlocsig-speed-of-sound))
    (define (time->samples time) (round (* time *clm-srate*)))
    
    ;; calculate speaker gains for group
    (define (find-gains x y z group)
      (let ((zero-coord 1.0e-10)
	    (zero-gain 1.0e-10)
	    (size (group-size group))
	    (mat (group-matrix group))) ; returns float-vector
	(if (and (< (abs x) zero-coord)
		 (< (abs y) zero-coord)
		 (< (abs z) zero-coord))
	    (list #t (list 1.0 1.0 1.0))
	    (case size
	      ((3)
	       (let* ((gain-a (+ (* (mat 0 0) x)
				 (* (mat 1 0) y)
				 (* (mat 2 0) z)))
		      (gain-b (+ (* (mat 0 1) x)
				 (* (mat 1 1) y)
				 (* (mat 2 1) z)))
		      (gain-c (+ (* (mat 0 2) x)
				 (* (mat 1 2) y)
				 (* (mat 2 2) z)))
		      (mag (distance gain-a gain-b gain-c)))
		 ;; truncate to zero roundoff errors
		 (if (< (abs gain-a) zero-gain)
		     (set! gain-a 0.0))
		 (if (< (abs gain-b) zero-gain)
		     (set! gain-b 0.0))
		 (if (< (abs gain-c) zero-gain)
		     (set! gain-c 0.0))
		 (list (and (>= gain-a 0) (>= gain-b 0) (>= gain-c 0))
		       (list (/ gain-a mag) (/ gain-b mag) (/ gain-c mag)))))
	      
	      ((2)
	       (let* ((gain-a (+ (* (mat 0 0) x)
				 (* (mat 1 0) y)))
		      (gain-b (+ (* (mat 0 1) x)
				 (* (mat 1 1) y)))
		      (mag (sqrt (+ (* gain-a gain-a)
				    (* gain-b gain-b)))))
		 ;; truncate to zero roundoff errors
		 (if (< (abs gain-a) zero-gain)
		     (set! gain-a 0.0))
		 (if (< (abs gain-b) zero-gain)
		     (set! gain-b 0.0))
		 (list (and (>= gain-a 0) (>= gain-b 0))
		       (list (/ gain-a mag) (/ gain-b mag)))))
	      
	      ((1)
	       (list #t (list 1.0)))))))
    

    ;; Render a trajectory breakpoint through amplitude panning
    (define famplitude-panning 
      (let ((speed-limit (/ (* dlocsig-speed-of-sound (- (car (last tpoints)) (car tpoints))) duration))
	    (prev-group #f)
	    (prev-x #f)
	    (prev-y #f)
	    (prev-z #f))
	
	(define (equalp-intersection L1 L2)
	  (if (null? L2) 
	      L2
	      (let loop1 ((L1 L1) 
			  (result ()))
		(cond ((null? L1) 
		       (reverse! result))
		      ((member (car L1) L2) 
		       (loop1 (cdr L1) 
			      (cons (car L1) 
				    result)))
		      (else (loop1 (cdr L1) 
				   result))))))
	
	(define (transition-point-3 vert-a vert-b xa ya za xb yb zb) 
	  (define (cross v1 v2)
	    (list (- (* (cadr v1) (third v2))
		     (* (third v1) (cadr v2)))
		  (- (* (third v1) (car v2))
		     (* (car v1) (third v2)))
		  (- (* (car v1) (cadr v2))
		     (* (cadr v1) (car v2)))))
	  (define (dot v1 v2)
	    (+ (* (car v1) (car v2))
	       (* (cadr v1) (cadr v2))
	       (* (third v1) (third v2))))
	  (define (sub v1 v2)
	    (list (- (car v1) (car v2))
		  (- (cadr v1) (cadr v2))
		  (- (third v1) (third v2))))
	  (define (add v1 v2)
	    (list (+ (car v1) (car v2))
		  (+ (cadr v1) (cadr v2))
		  (+ (third v1) (third v2))))
	  (define (scale v1 c)
	    (list (* (car v1) c)
		  (* (cadr v1) c)
		  (* (third v1) c)))
	  
	  (let* ((tolerance 1.0e-6)
		 (line-b (list xa ya za))
		 (line-m (sub (list xb yb zb) line-b))
		 (normal (cross vert-a vert-b))
		 (denominator (dot normal line-m)))
	    (and (> (abs denominator) tolerance)
		 (add line-b (scale line-m (/ (- (dot normal line-b)) denominator))))))
	
	;; calculate transition point between two adjacent two-speaker groups
	;; original line intersection code from Graphic Gems III
	(define (transition-point-2 vert xa ya xb yb)
	  (let ((Ax (car vert))
		(Bx (- xa xb))
		(Ay (cadr vert))
		(By (- ya yb)))
	    (let ((d (- (* By (- xa)) (* Bx (- ya))))
		  (f (- (* Ay Bx) (* Ax By))))
	      (and (not (= f 0))
		   (list (/ (* d Ax) f)
			 (/ (* d Ay) f))))))
	
	;; find the speaker group that contains a point
	(define (find-group x y z)
	  (call-with-exit
	   (lambda (return)
	     (for-each
	      (lambda (group)
		(let ((vals (find-gains x y z group)))
		  (if (car vals)                           ; inside
		      (return (list group (cadr vals)))))) ; gains
	      (speaker-config-groups speakers))
	     (list #f #f))))
	
	;; push zero gains on all channels
	(define (push-zero-gains time)
	  (do ((len (speaker-config-number speakers))
	       (i 0 (+ i 1)))
	      ((= i len))
	    (set! (channel-gains i) (cons time (channel-gains i)))
	    (set! (channel-gains i) (cons 0.0 (channel-gains i))))
	  (do ((len rev-channels)
	       (i 0 (+ i 1)))
	      ((= i len))
	    (set! (channel-rev-gains i) (cons time (channel-rev-gains i)))
	    (set! (channel-rev-gains i) (cons 0.0 (channel-rev-gains i)))))
	
	;; push gain and time into envelopes
	(define (push-gains group gains dist time)
	  
	  (define* (position val lst (pos 0))
	    (call-with-exit
	     (lambda (return)
	       (and (not (null? lst))
		    (if (= val (car lst))
			(return pos)
			(position val (cdr lst) (+ 1 pos)))))))
	  
	  (let ((outputs (make-vector out-channels 0.0))
		(rev-outputs (make-vector rev-channels 0.0))
		;; attenuation with distance of reverberated signal
		(ratt (if (>= dist inside-radius)
			  (expt dist (- reverb-power))
			  (- 1.0 (expt (/ dist inside-radius) (/ inside-reverb-power))))))
	    (let (;; attenuation with distance of direct signal
		  (att (if (>= dist inside-radius)
			   (expt dist (- direct-power))
			   (- 1.0 (expt (/ dist inside-radius) (/ inside-direct-power))))))
	      (if (>= dist inside-radius)
		  ;; outside the inner sphere, signal is sent to group
		  (do ((len (length gains))
		       (i 0 (+ i 1)))
		      ((= i len))
		    (let ((speaker ((group-speakers group) i))
			  (gain (gains i)))
		      (set! (outputs speaker) (* gain att))
		      (if (> rev-channels (max 1 speaker))
			  (set! (rev-outputs speaker) (* gain ratt)))))
		  
		  (let ((gain 0.0)
			(len (speaker-config-number speakers)))
		    (do ((speaker 0 (+ 1 speaker)))
			((= speaker len))
		      ;; inside the inner sphere, signal is sent to all speakers
		      (let ((found (position speaker (group-speakers group))))
			(if found
			    ;; speaker belongs to group, add to existing gain
			    (begin
			      (set! gain (gains found))
			      (set! (outputs speaker) (+ gain (* (- 1.0 gain) att)))
			      (if (> rev-channels (max 1 speaker))
				  (set! (rev-outputs speaker) (+ gain (* (- 1.0 gain) ratt)))))
			    ;; speaker outside of group
			    (begin
			      (set! (outputs speaker) att)
			      (if (> rev-channels (max 1 speaker))
				  (set! (rev-outputs speaker) ratt)))))))))
	    
	    ;; push all channel gains into envelopes
	    (let ((len (speaker-config-number speakers)))
	      (do ((i 0 (+ i 1)))
		  ((= i len))
		(if (or (null? (channel-gains i))
			(> time (cadr (channel-gains i))))
		    (begin
		      (set! (channel-gains i) (cons time (channel-gains i)))
		      (set! (channel-gains i) (cons (outputs i) (channel-gains i)))))))
	    
	    (if (> rev-channels 1)
		(do ((i 0 (+ i 1)))
		    ((= i rev-channels))
		  (if (or (null? (channel-rev-gains i))
			  (> time (cadr (channel-rev-gains i))))
		      (begin
			(set! (channel-rev-gains i) (cons time (channel-rev-gains i)))
			(set! (channel-rev-gains i) (cons (rev-outputs i) (channel-rev-gains i)))))))
	    
	    ;; push reverb gain into envelope for mono reverb
	    (if (and (= rev-channels 1)
		     (or (null? (channel-rev-gains 0))
			 (> time (cadr (channel-rev-gains 0)))))
		(begin
		  (set! (channel-rev-gains 0) (cons time (channel-rev-gains 0)))
		  (set! (channel-rev-gains 0) (cons ratt (channel-rev-gains 0)))))))
	
	(lambda (x y z dist time)
	  
	  ;; output gains for current point
	  (if prev-group
	      (let ((vals (find-gains x y z prev-group)))
		(let ((inside (car vals))
		      (gains (cadr vals)))
		  ;; check that the source is not moving faster than sound
		  (if (not (= time prev-time))
		      (let ((speed (/ (- dist prev-dist) (- time prev-time))))
			(if (> speed speed-limit)
			    (format () "warning: supersonic radial movement at [~F,~F,~F, ~F], speed=~F~%" x y z time speed))))
		  (if inside
		      ;; still in the same group
		      (begin
			(push-gains prev-group gains dist time)
			(set! prev-x x)
			(set! prev-y y)
			(set! prev-z z))
		      ;; left the group
		      (let ((vals (find-group x y z)))
			(let ((group (car vals))
			      (gains (cadr vals)))
			  (if (not group)
			      (begin
				;; current point is outside all defined groups
				;; we should send a warning at this point...
				(push-zero-gains time)
				(set! prev-group #f))
			      (begin
				;; we have to interpolate a new point that lies on the shared
				;; edge of the adjacent groups so that the speakers opposite
				;; the edge have zero gain when the trajectory switches groups
				(let ((edge (equalp-intersection (group-vertices group)
								 (group-vertices prev-group))))
				  (cond ((= (length edge) 2)
					 ;; the groups have two shared points (ie: share an edge)
					 ;; this must be a three speaker groups transition
					 (let ((pint (transition-point-3 (car edge) (cadr edge) x y z prev-x prev-y prev-z)))
					   (when pint
					     (let* ((xi (car pint))
						    (yi (cadr pint))
						    (zi (third pint))
						    (vals (find-gains xi yi zi prev-group)))
					       (let ((di (distance xi yi zi))
						     (ti (+ prev-time (max .00001 (* (/ (distance (- xi prev-x)
												  (- yi prev-y)
												  (- zi prev-z))
											(distance (- x prev-x)
												  (- y prev-y)
												  (- z prev-z)))
										     (- time prev-time)))))
						     ;; see if we are inside the previous group
						     ;; we can be on either side due to roundoff errors
						     (inside (car vals))
						     (gains (cadr vals)))
						 (if inside
						     (push-gains prev-group gains di ti)
						     (let ((val1 (find-gains xi yi zi group)))
						       (let ((inside (car val1))
							     (gains (cadr val1)))
							 (if inside
							     (push-gains group gains di ti)
							     ;; how did we get here?
							     (error 'mus-error "Outside of both adjacent groups [~A:~A:~A @~A]~%~%" xi yi zi ti))))))))))
					
					((and (pair? edge) 
					      (null? (cdr edge))
					      (= (group-size group) 2))
					 ;; two two-speaker groups share one point
					 ;; z coordinates are silently ignored
					 (let ((pint (transition-point-2 (car edge) x y prev-x prev-y)))
					   (when pint
					     (let* ((xi (car pint))
						    (yi (cadr pint))
						    (vals (find-gains xi yi 0.0 prev-group)))
					       (let ((di (distance xi yi 0.0))
						     (ti (+ prev-time (max .00001 (* (/ (distance (- xi prev-x)
												  (- yi prev-y)
												  0.0)
											(distance (- x prev-x)
												  (- y prev-y)
												  0.0))
										     (- time prev-time)))))
						     ;; see if we are inside the previous group
						     ;; we can be on either side due to roundoff errors
						     (inside (car vals))
						     (gains (cadr vals)))
						 (if inside 
						     (push-gains prev-group gains di ti)
						     (let ((val1 (find-gains xi yi 0.0 group)))
						       (let ((inside (car val1))
							     (gains (cadr val1)))
							 (if inside
							     (push-gains group gains di ti)
							     ;; how did we get here?
							     (format () "Outside of both adjacent groups [~A:~A @~A]~%~%" xi yi ti))))))))))
					
					((and (pair? edge) 
					      (null? (cdr edge)))
					 ;; groups share only one point... for now a warning
					 ;; we should calculate two additional interpolated
					 ;; points as the trajectory must be crossing a third
					 ;; group
					 (for-each
					  (lambda (int-group)
					    (if (and (member (car edge) (group-vertices int-group))
						     (not (equal? int-group group))
						     (not (equal? int-group prev-group)))
						(format () "e1=~A; e2=~A~%~%"
							(equalp-intersection (group-vertices int-group)
									     (group-vertices prev-group))
							(equalp-intersection (group-vertices int-group)
									     (group-vertices group)))))
					  
					  (speaker-config-groups speakers))
					 (format () "warning: crossing between groups with only one point in common~%  prev=~A~%  curr=~A~%" prev-group group))
					
					;; groups don't share points... how did we get here?
					((null? edge)
					 (format () "warning: crossing between groups with no common points, ~A~A to ~A~A~%"
						 (group-id prev-group) (group-speakers prev-group)
						 (group-id group) (group-speakers group)))))
				
				;; finally push gains for current group
				(push-gains group gains dist time)
				(set! prev-group group)
				(set! prev-x x)
				(set! prev-y y)
				(set! prev-z z))))))))
	      ;; first time around
	      (let ((vals (find-group x y z)))
		(let ((group (car vals))
		      (gains (cadr vals)))
		  (if group
		      (begin
			(push-gains group gains dist time)
			(set! prev-group group)
			(set! prev-x x)
			(set! prev-y y)
			(set! prev-z z))
		      (begin
			(push-zero-gains time)
			(set! prev-group #f)))))))))
    
    ;; Render a trajectory breakpoint for ambisonics b-format coding
    ;; http://www.york.ac.uk/inst/mustech/3d_audio/ambis2.htm
    ;;
    ;; Ambisonics b-format has four discrete channels encoded as follows:
    ;; W     0.707107             0.707107
    ;; X     cos(A)cos(E)         x
    ;; Y     sin(A)cos(E)         y
    ;; R     1.5sin(E)sin(E)-0.5  1.5zz-0.5
    ;; S     cos(A)sin(2E)        2zx
    ;; T     sin(A)sin(2E)        2yz
    ;; U     cos(2A)cos(E)cos(E)  xx-yy
    ;; V     sin(2A)cos(E)cos(E)  2xy
    ;; K                          z(2.5zz-1.5)
    ;; L                          K1(x(5zz-1)
    ;;       K1=sqrt(21*45/(224*8))
    ;; M                          K1(y(5zz-1))
    ;; N                          K2(Uz)
    ;;       K2=sqrt(3)*3/2
    ;; O                          K2(Vz)
    ;; P                          x(xx-3yy)
    ;; Q                          y(3xx-yy)
    ;;
    ;; where:
    ;; A: counter-clockwise angle of rotation from the front center
    ;; E: the angle of elevation above the horizontal plane
    ;; 
    ;; in our coordinate system (normalizing the vectors):
    ;; xy: (* dist (cos E))
    ;; (cos A): (/ y xy)
    ;; (sin A): (/ -x xy)
    ;; (cos E): (/ xy dist)
    ;; (sin E): (/ z dist)
    ;; so:
    ;; W: (* signal 0.707)
    ;; X: (* signal (/ y dist))
    ;; Y: (* signal (/ -x dist))
    ;; Z: (* signal (/ z dist))
    ;;
    ;; R: (* signal (- (* 1.5 z z 1/dist 1/dist) 0.5))
    ;; S: (* signal 2 z (- x) 1/dist 1/dist)
    ;; T: (* signal 2 z y 1/dist 1/dist)
    ;; U: (* signal (- (* x x 1/dist 1/dist) (* y y 1/dist 1/dist)))
    ;; V: (* signal 2 (- x) y 1/dist 1/dist)
    ;;
    ;; K: (* signal z (- (* 2.5 z z 1/dist 1/dist) 1.5))
    ;; L: (* signal K1 x 1/dist (- (* 5 z z 1/dist 1/dist) 1))
    ;; M: (* signal K1 y 1/dist (- (* 5 z z 1/dist 1/dist) 1))
    ;; N: (* signal K2 U z 1/dist)
    ;; O: (* signal K2 V z 1/dist)
    ;; P: (* signal x 1/dist (- (* x x 1/dist 1/dist) (* 3 y y 1/dist 1/dist)))
    ;; Q: (* signal y 1/dist (- (* 3 x x 1/dist 1/dist) (* y y 1/dist 1/dist)))
    ;;
    ;; see also: http://wiki.xiph.org/index.php/Ambisonics
    ;; for mixed order systems
    ;;
    (define render-ambisonics
      (let ((w-offset 0)
	    (x-offset 1)
	    (y-offset 2)
	    (ambisonics-k1 (sqrt 135/256)) ;(/ (* 21 45) 224 8)
	    (ambisonics-k2 (* (sqrt 3) 3 0.5)))

	(lambda (x y z dist time)
	  (let ((att (if (> dist inside-radius)
			 (expt (/ inside-radius dist) direct-power)
			 (expt (/ dist inside-radius) (/ inside-direct-power))))
		(ratt (if (> dist inside-radius)
			  (expt (/ inside-radius dist) reverb-power)
			  (expt (/ dist inside-radius) (/ inside-reverb-power))))
		(u 0)
		(v 0)
		(lm 0)
		(no 0))
	    ;; output encoding gains for point
	    ;; W: 0.707
	    (set! (channel-gains w-offset) (cons time (channel-gains w-offset)))
	    (let ((attW (if (> dist inside-radius)
			    (* point707 att)
			    (- 1 (* (- 1 point707) (expt (/ dist inside-radius) direct-power))))))
	      (set! (channel-gains w-offset) (cons attW (channel-gains w-offset))))
	    ;; X: (* (cos A) (cos E))
	    (set! (channel-gains x-offset) (cons time (channel-gains x-offset)))
	    (set! (channel-gains x-offset) (cons (if (zero? dist) 0 (/ (* att y) dist)) (channel-gains x-offset)))
	    ;; Y: (* (sin A) (cos E))
	    (set! (channel-gains y-offset) (cons time (channel-gains y-offset)))
	    (set! (channel-gains y-offset) (cons (if (zero? dist) 0 (/ (* att (- x)) dist)) (channel-gains y-offset)))
	    (when (>= ambisonics-v-order 1)
	      ;; Z: (sin E)
	      (set! (channel-gains z-offset) (cons time (channel-gains z-offset)))
	      (set! (channel-gains z-offset) (cons (if (zero? dist) 0 (/ (* att z) dist)) (channel-gains z-offset))))
	    (when (>= ambisonics-v-order 2)
	      ;; R
	      (set! (channel-gains r-offset) (cons time (channel-gains r-offset)))
	      (set! (channel-gains r-offset) (cons (if (zero? dist) 0 (* (- (/ (* 1.5 z z) dist dist) 0.5) att))
						   (channel-gains r-offset)))
	      ;; S
	      (set! (channel-gains s-offset) (cons time (channel-gains s-offset)))
	      (set! (channel-gains s-offset) (cons (if (zero? dist) 0 (/ (* -2 z x att) dist dist))
						   (channel-gains s-offset)))
	      ;; T
	      (set! (channel-gains t-offset) (cons time (channel-gains t-offset)))
	      (set! (channel-gains t-offset) (cons (if (zero? dist) 0 (/ (* 2 z y att) dist dist))
						   (channel-gains t-offset))))
	    (when (>= ambisonics-h-order 2)
	      (set! u (if (zero? dist) 0 (* (/ (- (* x x) (* y y)) dist dist) att)))
	      (set! v (if (zero? dist) 0 (/ (* -2 x y att) dist dist)))
		    ;; U
	      (set! (channel-gains u-offset) (cons time (channel-gains u-offset)))
	      (set! (channel-gains u-offset) (cons u (channel-gains u-offset)))
	      ;; V
	      (set! (channel-gains v-offset) (cons time (channel-gains v-offset)))
	      (set! (channel-gains v-offset) (cons v (channel-gains v-offset))))
	    
	    (when (>= ambisonics-v-order 3)
	      (set! lm (* ambisonics-k1 att (- (* 5 z z (if (zero? dist) 1 (/ 1.0 dist dist))) 1)))
	      (set! no (* ambisonics-k2 z att (if (zero? dist) 1 (/ dist))))
	      ;; K
	      (set! (channel-gains k-offset) (cons time (channel-gains k-offset)))
	      (set! (channel-gains k-offset) (cons (if (zero? dist) 0
						       (* (- (* 2.5 z z (if (zero? dist) 1 (/ 1.0 dist dist))) 1.5) att))
						   (channel-gains k-offset)))
	      ;; L
	      (set! (channel-gains l-offset) (cons time (channel-gains l-offset)))
	      (set! (channel-gains l-offset) (cons (if (zero? dist) 0 (/ (* lm x) dist)) (channel-gains l-offset)))
	      ;; M
	      (set! (channel-gains m-offset) (cons time (channel-gains m-offset)))
	      (set! (channel-gains m-offset) (cons (if (zero? dist) 0 (/ (* lm y) dist)) (channel-gains m-offset)))
	      ;; N
	      (set! (channel-gains n-offset) (cons time (channel-gains n-offset)))
	      (set! (channel-gains n-offset) (cons (if (zero? dist) 0 (* no u)) (channel-gains n-offset)))
	      ;; O
	      (set! (channel-gains o-offset) (cons time (channel-gains o-offset)))
	      (set! (channel-gains o-offset) (cons (if (zero? dist) 0 (* no v)) (channel-gains o-offset))))
	    
	    (when (>= ambisonics-h-order 3)
	      ;; P
	      (set! (channel-gains p-offset) (cons time (channel-gains p-offset)))
	      (set! (channel-gains p-offset) (let ((dist-p (- (* x x (if (zero? dist) 1 (/ 1.0 dist dist)))
							      (* 3 y y (if (zero? dist) 1 (/ 1.0 dist dist))))))
					       (cons (if (zero? dist) 0 (/ (* att x dist-p) dist))
						     (channel-gains p-offset))))
	      ;; Q
	      (set! (channel-gains q-offset) (cons time (channel-gains q-offset)))
	      (set! (channel-gains q-offset) (let ((dist-q (- (* 3 x x (if (zero? dist) 1 (/ 1.0 dist dist)))
							      (* y y (if (zero? dist) 1 (/ 1.0 dist dist))))))
					       (cons (if (zero? dist) 0 (/ (* att y dist-q) dist))
						     (channel-gains q-offset)))))
	    ;; push reverb gain into envelope
	    (when (= rev-channels 1)
	      ;; mono reverb output
	      (set! (channel-rev-gains 0) (cons time (channel-rev-gains 0)))
	      (set! (channel-rev-gains 0) (cons (if (>= dist inside-radius)
						    (expt dist (- reverb-power))
						    (- 1.0 (expt (/ dist inside-radius) (/ inside-reverb-power))))
						(channel-rev-gains 0))))
	    (when (> rev-channels 1)
	      ;; multichannel reverb, send ambisonics components
	      ;; W: 0.707
	      (set! (channel-rev-gains w-offset) (cons time (channel-rev-gains w-offset)))
	      (let ((rattW (if (> dist inside-radius)
			       (* point707 ratt)
			       (- 1 (* (- 1 point707) (expt (/ dist inside-radius) reverb-power))))))
		(set! (channel-rev-gains w-offset) (cons rattW (channel-rev-gains w-offset))))
	      ;; X: (* (cos A)(cos E))
	      (set! (channel-rev-gains x-offset) (cons time (channel-rev-gains x-offset)))
	      (set! (channel-rev-gains x-offset) (cons (if (zero? dist) 0 (* y (if (zero? dist) 1 (/ dist)) ratt))
						       (channel-rev-gains x-offset)))
	      ;; Y: (* (sin A)(cos E))
	      (set! (channel-rev-gains y-offset) (cons time (channel-rev-gains y-offset)))
	      (set! (channel-rev-gains y-offset) (cons (if (zero? dist) 0 (* (- x) (if (zero? dist) 1 (/ dist)) ratt))
						       (channel-rev-gains y-offset)))
	      (when (>= ambisonics-v-order 1)
		;; Z: (sin E)
		(set! (channel-rev-gains z-offset) (cons time (channel-rev-gains z-offset)))
		(set! (channel-rev-gains z-offset) (cons (if (zero? dist) 0 (* z (if (zero? dist) 1 (/ dist)) ratt))
							 (channel-rev-gains z-offset))))
	      (when (>= ambisonics-v-order 2)
		;; R
		(set! (channel-rev-gains r-offset) (cons time (channel-rev-gains r-offset)))
		(set! (channel-rev-gains r-offset) (cons (if (zero? dist) 0 (* (- (/ (* 1.5 z z) dist dist) 0.5)
									       dlocsig-ambisonics-ho-rev-scaler ratt))
							 (channel-rev-gains r-offset)))
		;; S
		(set! (channel-rev-gains s-offset) (cons time (channel-rev-gains s-offset)))
		(set! (channel-rev-gains s-offset) (cons (if (zero? dist) 0 (/ (* -2 z x dlocsig-ambisonics-ho-rev-scaler ratt)
									       dist dist))
							 (channel-rev-gains s-offset)))
		;; T
		(set! (channel-rev-gains t-offset) (cons time (channel-rev-gains t-offset)))
		(set! (channel-rev-gains t-offset) (cons (if (zero? dist) 0 (/ (* 2 z y dlocsig-ambisonics-ho-rev-scaler ratt)
									       dist dist))
							 (channel-rev-gains t-offset))))
	      (when (>= ambisonics-h-order 2)
		;; U
		(set! (channel-rev-gains u-offset) (cons time (channel-rev-gains u-offset)))
		(set! (channel-rev-gains u-offset) (let ((dist-u (if (zero? dist) 0 (/ (* (- (* x x) (* y y)) dlocsig-ambisonics-ho-rev-scaler ratt)
										       dist dist))))
						     (cons dist-u (channel-rev-gains u-offset))))
		;; V
		(set! (channel-rev-gains v-offset) (cons time (channel-rev-gains v-offset)))
		(set! (channel-rev-gains v-offset) (let ((dist-v (if (zero? dist) 0 (/ (* -2 x y dlocsig-ambisonics-ho-rev-scaler ratt)
										       dist dist))))
						     (cons dist-v (channel-rev-gains v-offset)))))
	      
	      (when (>= ambisonics-v-order 3)
		(set! lm (* ambisonics-k1 (- (* 5 z z (if (zero? dist) 1 (/ 1.0 dist dist))) 1) 
			    dlocsig-ambisonics-ho-rev-scaler ratt))
		(set! no (* ambisonics-k2 z (if (zero? dist) 1 (/ dist)) ratt))
		;; K
		(set! (channel-rev-gains k-offset) (cons time (channel-rev-gains k-offset)))
		(set! (channel-rev-gains k-offset) (let ((dist-k (if (zero? dist) 0 (* (- (/ (* 2.5 z z) dist dist) 1.5) dlocsig-ambisonics-ho-rev-scaler ratt))))
						     (cons dist-k (channel-rev-gains k-offset))))
		;; L
		(set! (channel-rev-gains l-offset) (cons time (channel-rev-gains l-offset)))
		(set! (channel-rev-gains l-offset) (cons (if (zero? dist) 0 (/ (* lm x) dist))
							 (channel-rev-gains l-offset)))
		;; M
		(set! (channel-rev-gains m-offset) (cons time (channel-rev-gains m-offset)))
		(set! (channel-rev-gains m-offset) (cons (if (zero? dist) 0 (/ (* lm y) dist))
							 (channel-rev-gains m-offset)))
		;; N
		(set! (channel-rev-gains n-offset) (cons time (channel-rev-gains n-offset)))
		(set! (channel-rev-gains n-offset) (cons (if (zero? dist) 0 (* no u))
							 (channel-rev-gains n-offset)))
		;; O
		(set! (channel-rev-gains o-offset) (cons time (channel-rev-gains o-offset)))
		(set! (channel-rev-gains o-offset) (cons (if (zero? dist) 0 (* no v))
							 (channel-rev-gains o-offset))))
	      (when (>= ambisonics-h-order 3)
		;; P
		(set! (channel-rev-gains p-offset) (cons time (channel-rev-gains p-offset)))
		(set! (channel-rev-gains p-offset) (let ((dist-p (if (zero? dist) 
								     0 
								     (/ (* ratt dlocsig-ambisonics-ho-rev-scaler x (- (* x x) (* 3 y y)))
									dist dist dist))))
						     (cons dist-p (channel-rev-gains p-offset))))
		;; Q
		(set! (channel-rev-gains q-offset) (cons time (channel-rev-gains q-offset)))
		(set! (channel-rev-gains q-offset) (let ((dist-q (if (zero? dist) 
								     0 
								     (/ (* ratt dlocsig-ambisonics-ho-rev-scaler y (- (* 3 x x) (* y y))) 
									dist dist dist))))
						     (cons dist-q (channel-rev-gains q-offset)))))
	      )))))
    
    ;; Render a trajectory breakpoint to a room for decoded ambisonics
    ;;
    ;; for a given speaker located in 3d space in polar coordinates:
    ;; az: azimut angle, increments clockwise
    ;; el: elevation angle
    ;;
    ;; S: (+ W (* X (cos az) (cos el))
    ;;         (* Y (sin az) (cos el))
    ;;         (* Z (sin el)))
    ;; 
    (define (fdecoded-ambisonics x y z dist time)
      (let ((att (if (> dist inside-radius)
		      (expt (/ inside-radius dist) direct-power)
		      (expt (/ dist inside-radius) (/ inside-direct-power))))
	     (ratt (if (> dist inside-radius)
		       (expt (/ inside-radius dist) reverb-power)
		       (expt (/ dist inside-radius) (/ inside-reverb-power)))))
	(let ((attW (if (> dist inside-radius)
			(* point707 att)
			(- 1 (* (- 1 point707) (expt (/ dist inside-radius) direct-power)))))
	      (rattW (if (> dist inside-radius)
			 (* point707 ratt)
			 (- 1 (* (- 1 point707) (expt (/ dist inside-radius) reverb-power))))))
	  ;; output decoded gains for point
	  (let ((len (speaker-config-number speakers))
		(spkrs (speaker-config-coords speakers)))
	    (do ((i 0 (+ i 1)))
		((= i len))
	      (let ((signal (let ((s (spkrs i)))
			      (* dlocsig-ambisonics-scaler
				 (if (zero? dist)
				     (* attW point707)
				     (+ (* attW point707)
					(/ (* att (+ (* x (car s))
						     (* y (cadr s))
						     (* z (caddr s))))
					   dist)))))))
		(set! (channel-gains i) (cons time (channel-gains i)))
		(set! (channel-gains i) (cons signal (channel-gains i))))))
	  
	  ;; push reverb gain into envelope
	  (if (= rev-channels 1)
	      (begin
		;; mono reverberation
		(set! (channel-rev-gains 0) (cons time (channel-rev-gains 0)))
		(set! (channel-rev-gains 0) (cons (if (>= dist inside-radius)
						      (expt dist (- reverb-power))
						      (- 1.0 (expt (/ dist inside-radius) (/ inside-reverb-power))))
						  (channel-rev-gains 0))))
	      ;; multichannel reverb
	      (do ((i 0 (+ i 1)))
		  ((= i rev-channels))
		(let ((signal (let ((s ((speaker-config-coords speakers) i)))
				(* dlocsig-ambisonics-scaler
				   (if (zero? dist)
				       (* rattW point707)
				       (+ (* rattW point707)
					  (/ (* ratt (+ (* x (car s)) 
							(* y (cadr s))
							(* z (caddr s))))
					     dist)))))))
		  (set! (channel-rev-gains i) (cons time (channel-rev-gains i)))
		  (set! (channel-rev-gains i) (cons signal (channel-rev-gains i)))))))))
    
    ;; Loop through all virtual rooms for one breakpoint in the trajectory
    (define (walk-all-rooms x y z time)
      (let ((dist (distance x y z)))
	;; remember first and last distances
	(if (not first-dist) ; set to #f (far) above
	    (set! first-dist dist))
	(set! last-dist dist)
	;; remember maximum and minimum distances
	(if (or (not (real? min-dist)) (< dist min-dist))
	    (set! min-dist dist))
	(if (or (not (real? max-dist)) (> dist max-dist))
	    (set! max-dist dist))
	;; push delay for current point (for doppler)
	(if (or (null? dly)
		(> time (cadr dly)))
	    (begin
	      (set! dly (cons (dist->samples dist) 
			      (cons time dly)))
	      ;; doppler should be easy, yeah right. We use "relativistic" correction
	      ;; as the sound object can be travelling close to the speed of sound. 
	      ;; http://www.mathpages.com/rr/s2-04/2-04.htm, 
	      ;; va = 0 (stationary listener)
	      ;; ve = moving object
	      ;; va = (* ve (/ 1 (+ 1 (/ ve c))) (sqrt (- 1 (* (/ ve c) (/ ve c)))))
	      (if prev-time
		  (let ((ratio (/ (- dist prev-dist)
				   (* duration (- time prev-time) dlocsig-speed-of-sound))))
		    (set! doppler (cons (* (/ 1.0 (+ 1 ratio)) (sqrt (- 1 (* ratio ratio)))) 
					(cons (/ (+ prev-time time) 2) doppler)))))))

	;; do the rendering of the point
	(cond ((= render-using amplitude-panning)
	       ;; amplitude panning
	       (famplitude-panning x y z dist time))
	      ((= render-using ambisonics)
	       ;; ambisonics b format
	       (render-ambisonics x y z dist time))
	      ((= render-using decoded-ambisonics)
	       ;; ambisonics decoded
	       (fdecoded-ambisonics x y z dist time)))
	
	(let ((room 1))
	  ;; remember current time and distance for next point
	  (set! prev-time time)
	  (set! prev-dist dist)
	  ;; return number of rooms processed (?)
	  room)))
    
    ;; Check to see if a segment changes radial direction:
    ;;   a change in radial direction implies a change in 
    ;;   doppler shift that has to be reflected as a new
    ;;   point in the rendered envelopes
    (define (change-direction xa ya za ta xb yb zb tb)
      (walk-all-rooms xa ya za ta)
      (unless (and (= xa xb)
		   (= ya yb)
		   (= za zb)
		   (= ta tb))
	(let ((vals (nearest-point xa ya za xb yb zb 0 0 0)))
	  (let ((xi (car vals))
		(yi (cadr vals))
		(zi (caddr vals)))
	    (if (and (if (< xa xb) (<= xa xi xb) (<= xb xi xa))
		     (if (< ya yb) (<= ya yi yb) (<= yb yi ya))
		     (if (< za zb) (<= za zi zb) (<= zb zi za)))
		(walk-all-rooms xi yi zi
				(+ tb (* (- ta tb)
					 (/ (distance (- xb xi) (- yb yi) (- zb zi))
					    (distance (- xb xa) (- yb ya) (- zb za)))))))))))
    
    ;; Check to see if a segment intersects the inner sphere:
    ;;   points inside are rendered differently so we need to
    ;;   create additional envelope points in the boundaries
    (define (intersects-inside-radius xa ya za ta xb yb zb tb)
      (let* ((mag (distance (- xb xa) (- yb ya) (- zb za)))
	     (vx (/ (- xb xa) mag))
	     (vy (/ (- yb ya) mag))
	     (vz (/ (- zb za) mag))
	     (bsq (+ (* xa vx) (* ya vy) (* za vz)))
	     (disc (let ((u (- (+ (* xa xa) (* ya ya) (* za za))
			       (* inside-radius inside-radius))))
		     (- (* bsq bsq) u)))
	     (hit (>= disc 0.0)))
	(if (not hit)
	    (change-direction xa ya za ta xb yb zb tb)
	    ;; ray defined by two points hits sphere
	    (let ((root (sqrt disc)))
	      (let ((rin  (- (+ bsq root)))
		    (rout (- root bsq))
		    (xi #f) (yi #f) (zi #f) (ti #f) (xo #f) (yo #f) (zo #f) (to #f))
		(if (> mag rin 0) ;(and (> rin 0) (< rin mag))
		    ;; intersects entering sphere
		    (begin
		      (set! xi (+ xa (* vx rin)))
		      (set! yi (+ ya (* vy rin)))
		      (set! zi (+ za (* vz rin)))
		      (set! ti (+ tb (* (- ta tb)
					(/ (distance (- xb xi) (- yb yi) (- zb zi))
					   (distance (- xb xa) (- yb ya) (- zb za))))))))
		(if (and (> rout 0) (< (abs rout) mag))
		    ;; intersects leaving sphere
		    (begin
		      (set! xo (+ xa (* vx rout)))
		      (set! yo (+ ya (* vy rout)))
		      (set! zo (+ za (* vz rout)))
		      (set! to (+ tb (* (- ta tb)
					(/ (distance (- xb xo) (- yb yo) (- zb zo))
					   (distance (- xb xa) (- yb ya) (- zb za))))))))
		(if xi
		    (begin
		      (change-direction xa ya za ta xi yi zi ti)
		      (if xo
			  (begin
			    (change-direction xi yi zi ti xo yo zo to)
			    (change-direction xo yo zo to xb yb zb tb))
			  (change-direction xi yi zi ti xb yb zb tb)))
		    (if xo
			(begin
			  (change-direction xa ya za ta xo yo zo to)
			  (change-direction xo yo zo to xb yb zb tb))
			(change-direction xa ya za ta xb yb zb tb))))))))
    
    ;; Recursively split segment if longer than minimum rendering distance:
    ;;   otherwise long line segments that have changes in distance render 
    ;;   the amplitude envelope as a linear function that does not reflect
    ;;   the chosen power function (1/d^n)
    (define (fminimum-segment-length xa ya za ta xb yb zb tb)
      (let ((dist (distance (- xb xa) (- yb ya) (- zb za))))
	(if (< dist minimum-segment-length)
	    (intersects-inside-radius xa ya za ta xb yb zb tb)
	    ;; interpolate a new point half way thorugh the segment
	    (let* ((xi (/ (+ xa xb) 2))
		   (yi (/ (+ ya yb) 2))
		   (zi (/ (+ za zb) 2))
		   (ti (+ tb (* (- ta tb)
				(/ (distance (- xb xi) (- yb yi) (- zb zi))
				   (distance (- xb xa) (- yb ya) (- zb za)))))))
	      (fminimum-segment-length xa ya za ta xi yi zi ti)
	      (fminimum-segment-length xi yi zi ti xb yb zb tb)))))
    
    ;; returns the new duration of a sound after using an envelope for time-varying sampling-rate conversion
    ;; (from Bill's dsp.scm)
    (define (src-duration e)
      (let ((len (- (length e) 2)))
	(do ((all-x (- (e len) (e 0))) ; last x - first x
	     (dur 0.0)
	     (i 0 (+ i 2)))
	    ((>= i len) dur)
	  (let ((area (let ((x0 (e i))
			    (x1 (e (+ i 2)))
			    (y0 (e (+ i 1))) ; 1/x x points
			    (y1 (e (+ i 3))))
			(if (< (abs (real-part (- y0 y1))) .0001)
			    (/ (- x1 x0) (* y0 all-x))
			    (/ (* (- (log y1) (log y0)) 
				  (- x1 x0)) 
			       (* (- y1 y0) all-x))))))
	    (set! dur (+ dur (abs (real-part area))))))))
    
    ;; Loop for each pair of points in the position envelope and render them
    (if (and (pair? xpoints) 
	     (null? (cdr xpoints)))
	;; static source (we should check if this is inside the inner radius?)
	(walk-all-rooms (car xpoints) (car ypoints) (car zpoints) (car tpoints))
	
	;; moving source
	(do ((len (- (min (length xpoints) (length ypoints) (length zpoints) (length tpoints)) 1))
	     (i 0 (+ i 1)))
	    ((>= i len))
	  (let ((xa (xpoints i))
		(ya (ypoints i))
		(za (zpoints i))
		(ta (tpoints i))
		(xb (xpoints (+ i 1)))
		(yb (ypoints (+ i 1)))
		(zb (zpoints (+ i 1)))
		(tb (tpoints (+ i 1))))
	    (fminimum-segment-length xa ya za ta xb yb zb tb)
	    (if (= i len)
		(walk-all-rooms xb yb zb tb)))))
    
    ;; create delay lines for output channels that need them
    (if speakers
	(let ((delays (speaker-config-delays speakers)))
	  (do ((len (length delays))
	       (channel 0 (+ 1 channel)))
	      ((= channel len))
	    (let ((delayo (delays channel)))
	      (set! (out-delays channel) (and (not (= delayo 0.0))
					      (make-delay (time->samples delayo))))
	      (set! max-out-delay (max max-out-delay delayo))))))
    
    ;; end of the run according to the duration of the note
    ;; (set! end (time->samples duration))
    ;; start and end of the loop in samples
    (let (;; delay from the minimum distance to the listener
	  (min-delay (dist->samples min-dist))
	  ;; duration of sound at listener's position after doppler src
	  ;; this does not work quite right but the error leads to a longer
	  ;; run with zeroed samples at the end so it should be fine
	  (real-dur (* duration (if (null? doppler) 1.0 (src-duration (reverse doppler)))))
	  ;; minimum distance for unity gain calculation
	  (min-dist-unity (max min-dist inside-radius))
	  (run-beg (time->samples start-time)))
      (let ((run-end (floor (- (+ (time->samples (+ start-time (max duration real-dur)))
				  (dist->samples last-dist)
				  (time->samples max-out-delay))
			       (if initial-delay 0.0 min-delay))))
	    ;; sample at which signal first arrives to the listener
	    (start (+ run-beg (dist->samples (- first-dist (if initial-delay 0.0 min-dist)))))
	    (direct-gain (* scaler
			    (if (number? unity-gain-dist)
				(expt unity-gain-dist direct-power)
				(if unity-gain-dist
				    1.0
				    (expt min-dist-unity direct-power)))))
	    (reverb-gain (* scaler 
			    (if (number? unity-gain-dist)
				(expt unity-gain-dist reverb-power)
				(if unity-gain-dist ; defaults to #f above
				    1.0
				    (expt min-dist-unity reverb-power))))))
      
	;; XXX hack!! this should be intercepted in the calling code, no 0 duration please...
	(if (<= real-dur 0.0)
	    (begin
	      (format () ";;; error: resetting real duration to 0.1 (was ~A)~%" real-dur)
	      (set! real-dur 0.1)))
	
	(let ((gen (make-move-sound
		    (list
		     ;; :start 
		     start
		     ;; :end 
		     (time->samples (+ start-time (max duration real-dur)))
		     ;; :out-channels 
		     (if speakers (speaker-config-number speakers) out-channels)
		     ;; :rev-channels 
		     rev-channels
		     ;; :path 
		     (make-delay delay-hack :max-size (max 1 (+ (ceiling (dist->samples max-dist)) delay-hack)))
		     ;; :delay 
		     (make-env (reverse dly)
			       :offset (if initial-delay 0.0 (- min-delay))
			       :duration real-dur)
		     ;; :rev 
		     (make-env (if (number? reverb-amount) ; as opposed to an envelope I guess
				   (list 0 reverb-amount 1 reverb-amount)
				   reverb-amount)
			       :duration real-dur)
		     ;; :out-delays 
		     out-delays
		     ;; :gains 
		     (do ((v (make-vector out-channels))
			  (i 0 (+ i 1)))
			 ((= i out-channels) v)
		       (set! (v i) (make-env (reverse (channel-gains i))
					     :scaler direct-gain
					     :duration real-dur)))
		     ;; :rev-gains 
		     (and (> rev-channels 0)
			  (do ((v (make-vector rev-channels))
			       (i 0 (+ i 1)))
			      ((= i rev-channels) v)
			    (set! (v i) (make-env (reverse (channel-rev-gains i))
						  :scaler reverb-gain
						  :duration real-dur))))
		     ;; :out-map 
		     (if speakers 
			 (speaker-config-map speakers) 
			 (do ((v (make-vector out-channels))
			      (i 0 (+ i 1)))
			     ((= i out-channels) v)
			   (set! (v i) i))))
		    *output*
		    *reverb*)))
	  (list gen
		;; return start and end samples for the run loop
		run-beg
		run-end))))))

;; (with-sound(:channels 6 :play #f :statistics #t) (sinewave 0 10 440 0.5 :path (make-path '((-10 10) (0.5 0.5) (10 10)) :error 0.001)))
;;
;; (with-sound(:statistics #t :channels 4 :reverb-channels 4 :reverb freeverb :decay-time 3)
;;  (move 0 "/usr/ccrma/snd/nando/sounds/kitchen/bowl/small-medium-large-1.snd"
;;	:paths (list (make-spiral-path :start-angle 0 :turns 2.5)
;;		     (make-spiral-path :start-angle 180 :turns 3.5))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Run macro to localize samples
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(define dlocsig move-sound)


#|

;; (define hi (make-path '((-10 10) (0.5 0.5) (10 10)) :3d #f :error 0.001))
;; (make-dlocsig 0 1.0 :out-channels 2 :rev-channels 0 :path (make-path '((-10 10) (0.5 0.5) (10 10)) :3d #f))

(if (provided? 'snd)
    (require snd-ws.scm)
    (require sndlib-ws.scm))

(define* (sinewave start-time duration freq amp 
		   (amp-env '(0 1 1 1))
		   (path (make-path :path '(-10 10 0 5 10 10))))
  (let* ((vals (make-dlocsig :start-time start-time
			     :duration duration
			     :path path))
	 (dloc (car vals))
	 (beg (cadr vals))
	 (end (caddr vals)))
    (let ((osc (make-oscil :frequency freq))
	  (aenv (make-env :envelope amp-env :scaler amp :duration duration)))
       (do ((i beg (+ i 1)))
	   ((= i end))
	 (dlocsig dloc i (* (env aenv) (oscil osc)))))))

(with-sound (:channels 2) (sinewave 0 1.0 440 .5 :path (make-path '((-10 10) (0.5 0.5) (10 10)) :3d #f)))

|#