A
download ws.rb
Language: Ruby
License: GPL
Copyright: (C) 2003--2005 Michael Scholz
LOC: 1284
Project Info
Snd
Server: SourceForge
Type: cvs
SourceForge\s\snd\snd\cvs‑snd\
   _new_snd-test.rb
   _sndlib.h
   aclocal.m4
   analog-filter.scm
   audinfo.c
   audio.c
   autosave.scm
   bess.rb
   bess.scm
   bess1.rb
   bess1.scm
   bird.rb
   bird.scm
   clm-ins.rb
   clm-ins.scm
   clm-strings.h
   clm.c
   clm.h
   clm23.scm
   clm2xen.c
   clm2xen.h
   cmn-glyphs.lisp
   config.guess
   config.h.in
   config.rpath
   config.sub
   configure.ac
   debug.scm
   dlocsig.rb
   draw.rb
   draw.scm
   dsp.rb
   dsp.scm
   edit-menu.scm
   edit123.scm
   effects-utils.scm
   effects.rb
   env.rb
   env.scm
   enved.scm
   eval-c.scm
   event.scm
   examp.rb
   examp.scm
   extensions.rb
   extensions.scm
   fade.scm
   fft-menu.scm
   fix-optargs.scm
   fmv.scm
   freeverb.rb
   freeverb.scm
   gettext.h
   gl.c
   grani.scm
   gtk-effects.scm
   gtk-popup.scm
   gui.scm
   headers.c
   help-snd-fm.pd
   help-snd.pd
   HISTORY.Snd
   hooks.rb
   hooks.scm
   index.rb
   index.scm
   inf-snd.el
   io.c
   jcrev.scm
   ladspa-help.scm
   ladspa.scm
   makefile.in
   makefile.no-gettext
   maraca.rb
   maraca.scm
   marks-menu.scm
   marks.rb
   marks.scm
   maxf.rb
   maxf.scm
   midi.c
   misc.scm
   mix-menu.scm
   mix.rb
   mix.scm
   mixer.scm
   moog.scm
   musglyphs.rb
   musglyphs.scm
   nb.rb
   nb.scm
   new-backgrounds.scm
   new-buttons.scm
   new-effects.scm
   new-icons.scm
   noise.rb
   noise.scm
   old-mac-audio.c
   old-snd.spec
   oo.scm
   oscope.scm
   panic.scm
   pd-add.scm
   pd-any.scm
   pd-fm.scm
   pd-global.scm
   pd-inout.scm
   pd-local.scm
   pd-mozilla.scm
   pd-send_receive.scm
   peak-env.scm
   piano.rb
   piano.scm
   play.rb
   play.scm
   poly.rb
   poly.scm
   popup.rb
   popup.scm
   prc95.rb
   prc95.scm
   pvoc.rb
   pvoc.scm
   README.Snd
   rgb.rb
   rgb.scm
   rmsgain.scm
   rt-compiler.scm
   rt-engine.scm
   rt-examples.scm
   rt.tex
   rtio.rb
   rtio.scm
   rubber.rb
   rubber.scm
   saw.c
   singer.rb
   singer.scm
   snd-0.h
   snd-1.h
   snd-axis.c
   snd-chn.c
   snd-completion.c
   snd-dac.c
   snd-data.c
   snd-draw.c
   snd-edits.c
   snd-env.c
   snd-error.c
   snd-fft.c
   snd-file.c
   snd-file.h
   snd-find.c
   snd-g0.h
   snd-g1.h
   snd-gchn.c
   snd-gdraw.c
   snd-gdrop.c
   snd-genv.c
   snd-gfft.c
   snd-gfile.c
   snd-gfind.c
   snd-ghelp.c
   snd-gl.scm
   snd-glistener.c
   snd-gmain.c
   snd-gmenu.c
   snd-gmix.c
   snd-gprefs.c
   snd-gprint.c
   snd-grec.c
   snd-gregion.c
   snd-gsnd.c
   snd-gtk.scm
   snd-gutils.c
   snd-gxbitmaps.c
   snd-gxcolormaps.c
   snd-gxen.c
   snd-gxutils.c
   snd-help.c
   snd-hobbit.scm
   snd-io.c
   snd-kbd.c
   snd-ladspa.c
   snd-listener.c
   snd-main.c
   snd-marks.c
   snd-menu.c
   snd-menu.h
   snd-mix.c
   snd-motif.scm
   snd-nogui.c
   snd-nogui0.h
   snd-nogui1.h
   snd-prefs.c
   snd-print.c
   snd-rec.c
   snd-rec.h
   snd-region.c
   snd-run.c
   snd-select.c
   snd-sig.c
   snd-snd.c
   snd-strings.h
   snd-trans.c
   snd-utils.c
   snd-x0.h
   snd-x1.h
   snd-xchn.c
   snd-xdraw.c
   snd-xdrop.c
   snd-xen.c
   snd-xenv.c
   snd-xfft.c
   snd-xfile.c
   snd-xfind.c
   snd-xhelp.c
   snd-xlistener.c
   snd-xm.rb
   snd-xmain.c
   snd-xmenu.c
   snd-xmix.c
   snd-xprefs.c
   snd-xprint.c
   snd-xrec.c
   snd-xref.c
   snd-xregion.c
   snd-xsnd.c
   snd-xutils.c
   snd-xxen.c
   snd.1
   Snd.ad
   snd.c
   Snd.gtkrc
   snd.h
   snd.spec
   snd_conffile.scm
   snd_pd_external.c
   snd_pd_external.h
   snd4.scm
   snd5.scm
   snd6.scm
   snd7.scm
   sndctrl.c
   sndinfo.c
   sndlib-strings.h
   sndlib.h.in
   sndlib2xen.c
   sndlib2xen.h
   sndplay.c
   sndrecord.c
   sndwarp.scm
   sound.c
   special-menu.scm
   spectr.rb
   spectr.scm
   strad.rb
   strad.scm
   TODO.Snd
   track-colors.scm
   v.rb
   v.scm
   vct.c
   vct.h
   ws.rb
   ws.scm
   xen.c
   xen.h
   xg-x11.h
   xm-enved.rb
   xm-enved.scm
   zip.rb
   zip.scm

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
# ws.rb -- with_sound and friends for Snd/Ruby -*- snd-ruby -*-

# Copyright (C) 2003--2005 Michael Scholz

# Author: Michael Scholz <scholz-micha@gmx.de>
# Created: Tue Apr 08 17:05:03 CEST 2003
# Last: Wed Aug 17 17:43:14 CEST 2005

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.

# This program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.

# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA

# Commentary:
#
# module WS
#   ws_interrupt?
#   ws_break(*rest)
#   with_snd(*args) do ... end
#   with_reverb(reverb, reverb_data, reverb_amount, snd, *with_sound_args)
#   with_sound(*args) do ... end
#   with_dac(*args, &body)
#   snd_load(rbm_file, *args)
#   clm_load(rbm_file, *args)
#   with_temp_sound(snd) do |temp_snd_file_name| ... end
#   make_default_comment
#   remove_file(file)
#   each_sample(start, dur) do |samp| ... end
#   frame2sound_data!(data, samp, frm)
#   sound_data2frame!(data, samp, frm)
#   snd2frame!(readers, frm)
#   process_times
#
# class With_sound
#   initialize(*args, &body)
#
# properties:
#   output       # file name, String
#
# methods:
#   help   (or description, info)
#   inspect
#   to_s
#   describe   (show_local_variables)
#   with_sound(*args) do ... end
#   scaled_to(scale) do ... end
#   scaled_by(scale) do ... end
#   with_offset(secs) do ... end
#   with_current_sound(*args) do ... end
#   sound_let(*args) do |*sl_args| ... end
#   clm_load(rbm_file, *args)
#   with_mix(*args, file[, beg_time], body_string)
#   with_sound_info(instrument_name, start, dur)
#   run_instrument(start, dur, *locsig_args) do |samp| ... end
#   run_reverb(start, dur, chan = 0) do |in_val, samp| ... end
#   clm_mix(filename, *args)
#   run do ... end
#
# class Instrument
#   my_simp(start, dur, freq, amp, amp_env = [0, 1, 1, 1])
#   make_ws_reader(file, *args)
#   ws_readin(rd)
#   close_ws_reader(file, rd)
#   ws_location(rd)
#   set_ws_location(rd, val)
#   ws_increment(rd)
#   set_ws_increment(rd, val)
#   ws_srate(file)
#   ws_channels(file)
#   ws_duration(file)
#
# Instruments have access to @ws_output and @ws_reverb variables as
# well as @srate, @channels, @reverb_channels etc.  It is no longer
# necessary (but possible) to use the global variables $output and
# $reverb.
#
# The classes (Snd_|CLM_)Instrument are not really necessary, but the
# name may be more clear if special instruments are added.
# Instruments of class Snd_Instrument may be as well of class
# With_Snd, the same with CLM_Instrument <-> With_CLM and Instrument
# <-> With_sound.  See dlocsig.rb for additions.
# 
#                With_sound
#                    |
#                    v    
#            --- Instrument ---
#            |                |
#            v                v
#      Snd_Instrument  CLM_Instrument
#       |        |            |
#       v        v            v
#  With_Snd   With_DAC     With_CLM
#
# Instruments can use the generalized run-loop `run_instrument'.
#
# RUN_INSTRUMENT(start, dur, *locsig_args) do |samp| ... (return next sample) end
#
# The body (or block) of `run_instrument' should return the next sample, e.g.:
#
# class Instrument
#   def my_simp(start, dur, freq, amp, amp_env = [0, 1, 1, 1])
#     os = make_oscil(:frequency, freq)
#     en = make_env(:envelope, amp_env, :scaler, amp, :duration, dur)
#     run_instrument(start, dur) do
#       env(en) * oscil(os)
#     end
#   end
# end
# 
# in Snd as well as in a Ruby script:
#      with_sound do my_simp(0, 1, 440, 0.2) end
#   or with_sound(:clm, true) do my_simp(0, 1, 440, 0.2) end
#
# in Snd:
#      with_snd do my_simp(0, 1, 440, 0.2) end
#   or with_sound(:clm, false) do my_simp(0, 1, 440, 0.2) end
#
# In addition, `with_dac do my_simp(0, 1, 440, 0.2) end' can use the
# same instrument for dac-output.
#
# Reverbs can use the generalized run-loop `run_reverb'.
#
# RUN_REVERB(start, dur, chan = 0) do |value, samp| ... (return next frame) end
#
# VALUE is the next sample value on location SAMP of reverb file's
# CHANnel.  It replaces
#
#   (beg...len).each do |i|
#     ho = ina(i, $reverb)
#     ...
#   end
# by
#   run_reverb(start, dur) do |ho, i|
#     ...
#   end
#
# The body should return a frame object of out-channels length.  The
# body is called seconds2sample(dur) times, like run_instrument.
#
# def my_reverb(start, dur, *rest)
#   ...
#   out_frames = make_frame(@channels)
#   run_reverb(start, dur) do |ho, i|
#     ...
#     frame_set!(out_frames, 0, val0)
#     if @channels > 1
#       frame_set!(out_frames, 1, val1)
#     end
#     ...
#     out_frames
#   end
# end
#
# A special case occures in freeverb.rb.
#
#   run_reverb(start, dur, :frames) do |in_frame, i|
#     ...
#   end
#
# IN_FRAME is a frame object of the I-th location in the reverb file
# (or snd), the rest is like above.
#
# The classes Snd_Instrument and CLM_Instrument can be used to define
# special instruments, see FULLMIX in clm-ins.rb.
#
# Class Snd_Instrument instruments can only be used in Snd, that means
# the variables @ws_output and @ws_reverb are snd index numbers.
# `with_snd' calls instruments from this class with higher priority
# than from class Instrument or Object.
#
# Class CLM_Instrument instruments use sample2file output and can be
# used in Snd as well as in Ruby scripts, @ws_output and $output are
# the same, @ws_reverb and $reverb are the same too.  `with_sound'
# calls instruments from this class with higher priority than from
# class Instrument or Object.

# Usage:
#
# Global variables can be set in ~/.snd-ruby.rb or in other scripts
# before or after loading ws.rb.
#
# with_sound(:play, 3, :statistics, true, :reverb, :jc_reverb) do
#   fm_violin(0, 1, 440, 0.3)
# end
#
# clm_load("test.rbm", :play, 1, :statistics, true, :verbose, true)
#
# These functions can be called within with_sound or clm_load:
#   scaled_to(scale, &body)
#   scaled_by(scale, &body)
#   with_offset(secs, &body)
#   with_current_sound(*args, &body)
#   sound_let(*args, &body)
#   with_mix(*args) (does not use a block but a string as "body")
#
# SOUND_LET
#
# sound_let(*args) do |*sl_args| ... end
# args: [with_sound-args1, with_sound-body1],
#       [with_sound-args2, with_sound-body2],
#       with_sound_body3,
#       let_args1,
#       let_args2, ...
#
# sound_let works like let except for procedures which are handled by
# with_sound.  with_sound returns a filename which can be used in the
# sound_let body.
#
# sound_let(lambda do fm_violin(0, 1, 330, 0.5) end,
#           1024) do |tmp_file, val|
# end
#
# TMP_FILE is returned by with_sound and VAL is connected to 1024.
#
# If with_sound needs args, the args and the procedure must be in an
# array.
#
# sound_let([:scaled_to, 0.3, :output, "sl.snd", lambda do fm_violin(0, 1, 330, 0.5) end],
#           1024) do |tmp_file, val|
# end
# 
# Examples:
# 
# One with_sound-call and one temporary file name, arbitrary called TMP:
# 
# sound_let([:reverb, :jc_reverb, lambda do fm_violin(0, 1, 220, 0.2) end]) do |tmp|
#   mus_mix(@output, tmp)
# end
# 
# Two with_sound-calls and two temporary file names, arbitrary
# called TEMP_1 and TEMP_2:
# 
# sound_let([:reverb, :jc_reverb, lambda do fm_violin(0, 1, 220, 0.2) end],
#           lambda do fm_violin(0.5, 1, 440, 0.3) end) do |temp_1, temp_2|
#   mus_mix(temp_1, temp_2)
#   mus_mix(@output, temp_1)
# end
#
# WITH_MIX
#
# with_mix(file, body_string)
# with_mix(file, beg_time, body_string)
# with_mix(with_sound-args, file, beg_time, body_string)
#
# The FILE and BODY_STRING are necessary, BEG_TIME and
# WITH_SOUND-ARGS are optional, BEG_TIME defaults to 0.
#
# Creates a text file named FILE.rbm with contents BODY_STRING and a
# sound file named FILE.snd, the result of clm_load(FILE.rbm).  If
# BODY_STRING is changed, clm_load(FILE.rbm) is called before mixing
# FILE.snd in the underlying @output of with_sound.
#
# Example:
#
# with_sound() do
#   with_mix(:reverb, :jc_reverb, "foo", 0.2, %Q{
#     fm_violin(0, 1, 440, 0.1)
#   })
# end
#
# with_sound's NOTEHOOK and INFO options:
#
# Every time an instrument starts computing, @with_sound_note_hook is
# called with the instrument name, the start time and the duration.
#
# def my_notehook(name, start, dur)
#   if name =~ /violin/
#     Snd.message("%s: start %1.3f, dur %1.3f", name, start, dur)
#   end
# end
#
# with_sound(:notehook, :my_notehook) do
#   fm_violin(0, 1, 440, 0.1)
#   fbell = [0, 1, 2, 1.1, 25, 0.75, 75, 0.5, 100, 0.2]
#   abell = [0, 0, 0.1, 1, 10, 0.6, 25, 0.3, 50, 0.15, 90, 0.1, 100, 0]
#   fm_bell(0.5, 2.0, 220, 0.5, abell, fbell, 1)
# end
# 
# installs the @with_sound_note_hook and prints the line
# 
#   `# fm_violin: start 0.000, dur 1.000'
#
# If option :info is true, every instrument-call prints the line
# mentioned above.
#
# with_sound's SAVE_BODY option:
# 
# Works only if proc (the with_sound-body) is located in a source file
# and proc.to_s returns something like
# #<Proc:0x00000000@(/path/to/source/file):2> (older Ruby versions
# return only #<Proc:0x00000000>, so we can't find the source file and
# line number).
#
# Tries to locate the body code and saves it in the comment string of
# the sound file.  Scanning the source code is very simple and may not
# work in every case.

# CLM examples (see clm.html) and their Snd/Ruby counterparts:
#
# ;; CLM examples
#  (with-sound () 
#    (mix (with-sound (:output "hiho.snd") 
#              (fm-violin 0 1 440 .1))
#            :amplitude .5))
# 
#  (with-sound ()
#    (with-mix () "s1" 0
#      (sound-let ((tmp ()
#                    (fm-violin 0 1 440 .1)))
#        (mix tmp))))
# 
#  (with-sound (:verbose t)
#    (with-mix () "s6" 0
#      (sound-let ((tmp ()
#                    (fm-violin 0 1 440 .1))
#                  (tmp1 (:reverb nrev)
#                    (mix "oboe.snd")))
#        (mix tmp1)
#        (mix tmp :amplitude .2 :output-frame *srate*))
#      (fm-violin .5 .1 330 .1)))
# 
#  (with-sound (:verbose t)
#    (sound-let ((tmp ()
#                  (with-mix () "s7" 0
#                    (sound-let ((tmp ()
#                                  (fm-violin 0 1 440 .1))
#                                (tmp1 ()
#                                  (mix "oboe.snd")))
#                     (mix tmp1)
#                     (mix tmp :output-frame *srate*))
#                   (fm-violin .5 .1 330 .1))))
#      (mix tmp :amplitude .5)))
#
=begin
# Snd/Ruby examples
with_sound() do
  clm_mix(with_sound(:output, "hiho.snd") do
            fm_violin(0, 1, 440, 0.1)
          end.output, :scale, 0.5)
end

with_sound() do
  with_mix "s1", %Q{
  sound_let(lambda do fm_violin(0, 1, 440, 0.1) end) do |tmp|
    clm_mix(tmp)
  end
  }
end

with_sound(:verbose, true) do
  with_mix "s6", %Q{
  sound_let(lambda do fm_violin(0, 1, 440, 0.1) end,
            [:reverb, :nrev, lambda do clm_mix("oboe.snd") end]) do |tmp, tmp1|
    clm_mix(tmp1)
    clm_mix(tmp, :scale, 0.2, :output_frame, seconds2samples(1))
  end
  fm_violin(0.5, 0.1, 330, 0.1)
  }
end

with_sound(:verbose, true) do
  sound_let(lambda do
                with_mix "s7", 0, %Q{
                  sound_let(lambda do fm_violin(0, 1, 440, 0.1) end,
                            lambda do clm_mix("oboe.snd") end) do |tmp, tmp1|
                    clm_mix(tmp1)
                    clm_mix(tmp, :output_frame, @srate)
                  end
                  fm_violin(0.5, 0.1, 330, 0.1)
                }
              end) do |tmp0|
    clm_mix(tmp0, :scale, 0.5)
  end
end
=end

# Code:

require "examp"
require "sndlib" unless provided? :sndlib
with_silence do
  unless defined? Etc.getlogin
    require "etc"
  end
  unless defined? Socket.gethostname
    require "socket"
  end
end
require "hooks"

$clm_version            = "17-08-2005"
$output                 = nil
$reverb                 = nil
$clm_file_name          = "test.snd" unless defined? $clm_file_name
$clm_comment            = nil        unless defined? $clm_comment
$clm_statistics         = false      unless defined? $clm_statistics
$clm_play               = 0          unless defined? $clm_play
$clm_player             = "sndplay"  unless defined? $clm_player
$clm_reverb_file_name   = nil        unless defined? $clm_reverb_file_name
$clm_reverb_channels    = 1          unless defined? $clm_reverb_channels
$clm_reverb             = nil        unless defined? $clm_reverb
$clm_reverb_data        = []         unless defined? $clm_reverb_data
$clm_delete_reverb      = false      unless defined? $clm_delete_reverb
$clm_verbose            = $VERBOSE   unless defined? $clm_verbose
$clm_info               = false      unless defined? $clm_info
$clm_clipped            = true       unless defined? $clm_clipped
$clm_notehook           = nil        unless defined? $clm_notehook
$clm_audio_format       = Mus_lshort unless defined? $clm_audio_format
$clm_file_buffer_size   = 65536      unless defined? $clm_file_buffer_size
$clm_table_size         = 512        unless defined? $clm_table_size
$clm_array_print_length = 8          unless defined? $clm_array_print_length

if provided? :snd
  $clm_srate         = default_output_srate         unless defined? $clm_srate
  $clm_channels      = default_output_chans         unless defined? $clm_channels
  $clm_header_type   = default_output_header_type   unless defined? $clm_header_type
  $clm_data_format   = default_output_data_format   unless defined? $clm_data_format
  $clm_locsig_type   = locsig_type                  unless defined? $clm_locsig_type
  $clm_rt_bufsize    = dac_size                     unless defined? $clm_rt_bufsize
  $clm_output_device = audio_output_device          unless defined? $clm_output_device
else
  $clm_srate         = 22050             unless defined? $clm_srate
  $clm_channels      = 1                 unless defined? $clm_channels
  $clm_header_type   = Mus_next          unless defined? $clm_header_type
  $clm_data_format   = Mus_lfloat        unless defined? $clm_data_format
  $clm_locsig_type   = Mus_interp_linear unless defined? $clm_locsig_type
  $clm_rt_bufsize    = 512               unless defined? $clm_rt_bufsize
  $clm_output_device = Mus_audio_default unless defined? $clm_output_device
end

module WS
  ws_doc = "\
 with_sound(*args) do ... end
 
   :output             $clm_file_name        test.snd
   :channels           $clm_channels         1
   :srate              $clm_srate            22050
   :header_type        $clm_header_type      Mus_next
   :data_format        $clm_data_format      Mus_lfloat
   :audio_format       $clm_audio_format     Mus_lshort
 
   :reverb             $clm_reverb           nil
   :reverb_data        $clm_reverb_data      []
   :reverb_channels    $clm_reverb_channels  1
   :revfile            $clm_reverb_file_name nil
   :delete_reverb      $clm_delete_reverb    false
 
   :decay_time         1.0
   :scaled_to          false
   :scaled_by          false
   :continue_old_file  false
   :notehook           $clm_notehook         nil
  
   :play               $clm_play             0
   :statistics         $clm_statistics       false
   :comment            $clm_comment          nil
   :locsig_type        $clm_locsig_type      Mus_interp_linear
   :verbose            $clm_verbose          false

# next option works only with newer Ruby versions
# save the with_sound-body in sound file's comment
   :save_body          false

# if an instrument uses run_instrument/run_reverb and INFO is true, a
# message will be printed: `instrument_name: start 0.000, dur 0.000'
   :info               $clm_info             false
   :clipped            $clm_clipped          true
   :player             $clm_player           sndplay
# save sound after computing
   :save               false
# special with_dac options:
   :bufsize            $clm_rt_bufsize       512
   :device             $clm_output_device    Mus_audio_default
 
 Usage: with_sound(:play, 1, :statistics, true) do fm_violin end"
  
  def ws_interrupt?
    if c_g?
      raise(Interrupt, format("%s interrupted by user (C-g)", get_func_name(3)), caller(1))
    end
  end

  def ws_break(*rest)
    msg = format("%s received Break", get_func_name(2))
    unless rest.null?
      msg += ":"
      rest.each do |s| msg += format(" %s,", s) end
    end
    raise(Break, msg.chomp(","), caller(1))
  end

  add_help(:with_snd, ws_doc)
  def with_snd(*args, &body)
    ws = if provided? :snd
           With_Snd.new(*args, &body)
         else
           With_CLM.new(*args, &body)
         end
    if get_args(args, :help, false)
      Snd.message(ws.help)
    else
      ws.run
    end
    ws
  end

  add_help(:with_reverb,
           "with_reverb(reverb,[reverb_data=[],[reverb_amount=0.05,[snd=false]]], *with_sound_args)
with_reverb(:jc_reverb)
require 'clm-ins'
with_reverb(:jl_reverb, [], 0.2)")
  def with_reverb(reverb, reverb_data = [], reverb_amount = 0.05, snd = false, *args)
    ws = With_Snd.new(:reverb, reverb,
                      :reverb_data, reverb_data,
                      :reverb_channels, 1,
                      :output, file_name(snd),
                      :save, false,
                      *args)
    ws.with_reverb(Snd.snd(snd), reverb_amount)
    ws
  end
  
  add_help(:with_sound, ws_doc)
  def with_sound(*args, &body)
    ws = if get_args(args, :clm, provided?(:snd) ? false : true)
           With_CLM.new(*args, &body)
         else
           With_Snd.new(*args, &body)
         end
    if get_args(args, :help, false)
      Snd.message(ws.help)
    else
      ws.run
    end
    ws
  end
  
  add_help(:with_dac, ws_doc)
  def with_dac(*args, &body)
    ws = With_DAC.new(*args, &body)
    if get_args(args, :help, false)
      Snd.message(ws.help)
    else
      ws.run
    end
    ws
  end
  
  add_help(:snd_load, "snd_load(rbm_file, *with_sound_args)\n" + ws_doc)
  def snd_load(rbm_file, *args)
    ws = With_Snd.new(*args)
    ws.clm_load(rbm_file)
    ws
  end
  
  add_help(:clm_load, "clm_load(rbm_file, *with_sound_args)\n" + ws_doc)
  def clm_load(rbm_file, *args)
    ws = With_CLM.new(*args)
    ws.clm_load(rbm_file)
    ws
  end

  add_help(:with_temp_sound,
           "with_temp_sound(snd = false) do |temp_snd_file_name| ... end \
Saves SND in a temporary file, which name can be accessed in the body code.  \
After finishing the body, the file will be removed.")
  def with_temp_sound(snd = false, &body)
    t = tempnam
    save_sound_as(t, snd)
    ret = body.call(t)
    remove_file(t)
    ret
  end

  def make_default_comment
    date = Time.new.localtime.strftime("%a %d-%b-%y %H:%M %Z")
    version = format("by %s at %s using ruby %s (%s) [%s]",
                     Etc.getlogin,
                     Socket.gethostname,
                     RUBY_VERSION,
                     RUBY_RELEASE_DATE,
                     RUBY_PLATFORM)
    format("Written %s %s, clm (Ruby) of %s", date, version, $clm_version)
  end
  
  def remove_file(file)
    if provided? :snd and (snd = find_sound(file))
      revert_sound(snd)
      close_sound_extend(snd)
    end
     File.owned?(file) and File.unlink(file)
  end

  def each_sample(start, dur)
    beg, ends = times2samples(start, dur)
    (beg...ends).each do |samp| yield(samp) end
  end

  def frame2sound_data!(data, samp, frm)
    sound_data_chans(data).times do |chn|
      sound_data_set!(data, chn, samp, frame_ref(frm, chn))
    end
  end

  def sound_data2frame!(data, samp, frm)
    sound_data_chans(data).times do |chn|
      frame_set!(frm, chn, sound_data_ref(data, chn, samp))
    end
    frm
  end

  # READERS is an array of sample_reader objects
  def snd2frame!(readers, frm)
    readers.each_with_index do |rd, chn|
      frame_set!(frm, chn, read_sample(rd))
    end
    frm
  end

  def process_times
    if defined? Process.times
      Process.times
    else
      Time.times
    end
  end
end

class With_sound
  include Info
  include WS
  @@file_number = 0

  def initialize(*args, &body)
    @output          = get_args(args, :output, $clm_file_name)
    @channels        = get_args(args, :channels, $clm_channels)
    @srate           = get_args(args, :srate, $clm_srate).to_f
    @header_type     = get_args(args, :header_type, $clm_header_type)
    @data_format     = get_args(args, :data_format, $clm_data_format)
    @audio_format    = get_args(args, :audio_format, $clm_audio_format)

    @reverb          = get_args(args, :reverb, $clm_reverb)
    @reverb_data     = get_args(args, :reverb_data, $clm_reverb_data)
    @reverb_channels = get_args(args, :reverb_channels, $clm_reverb_channels)
    @revfile         = get_args(args, :reverb_file_name, nil)
    @delete_reverb   = get_args(args, :delete_reverb, $clm_delete_reverb)

    @decay_time      = get_args(args, :decay_time, 1.0)
    @scaled_to       = get_args(args, :scaled_to, false)
    @scaled_by       = get_args(args, :scaled_by, false)

    @continue        = get_args(args, :continue_old_file, false)
    @notehook        = get_args(args, :notehook, $clm_notehook)
    @save_body       = get_args(args, :save_body, false)
    @save            = get_args(args, :save, false)
    @player          = get_args(args, :player, $clm_player)

    @play            = get_args(args, :play, $clm_play)
    @statistics      = get_args(args, :statistics, $clm_statistics)
    @verbose         = get_args(args, :verbose, $clm_verbose)
    @info            = get_args(args, :info, $clm_info)
    @comment         = get_args(args, :comment, $clm_comment)
    @locsig_type     = get_args(args, :locsig_type, $clm_locsig_type)
    @clipped         = get_args(args, :clipped, :undefined)
    @offset          = get_args(args, :offset, 0.0)

    @rtime = @utime = @stime = 0.0
    @body = body
    @revfile = make_reverb_file_name() unless @revfile
    @reverb_amount = 0.05
    # play: either :play, true
    #       or     :play, false
    #       or     :play, nil
    #       or     :play, integer (integer times playing)
    @play = case @play
            when TrueClass
              1
            when FalseClass, NilClass
              0
            when Numeric
              Integer(@play).abs
            else
              0
            end
    if @continue
      @play = 0
      @statistics = false
    end
    # without reverb: either :reverb_channels, 0
    #                 or     :reverb, false
    if @reverb_channels.zero?
      @reverb = $reverb = false
    end
    unless string?(@comment) then @comment = make_default_comment end
    if @save_body and proc?(@body) then @comment << "\n" << @body.to_body end
    @old_srate = mus_srate
    @old_update_interval = if defined? auto_update_interval
                             auto_update_interval
                           else
                             false
                           end
    @start = @dur = 0.0
    @locsig = nil
    $output = @ws_output = false
    $reverb = @ws_reverb = false
    # recursive call: :reverb, [:old_reverb, @ws_reverb]
    if (not @reverb)
      @reverb_channels = 0
    elsif array?(@reverb)
      $reverb = @ws_reverb = @reverb.last
      @reverb = false
    end
    @clm_instruments = Hash.new
    @with_sound_note_hook = Hook.new("@with_sound_note_hook", 3, "\
lambda do |inst_name, start, dur| ... end: called if an instrument has
the run_instrument/run_reverb loop included.

Every time an instrument starts computing, @with_sound_note_hook is
called with the instrument name INST_NAME, the start time START, and
the duration DUR.

def my_notehook(name, start, dur)
  if name =~ /violin/
    Snd.message(\"%s: start %1.3f, dur %1.3f\", name, start, dur)
  end
end

with_sound(:notehook, :my_notehook) do
  fm_violin(0, 1, 440, 0.1)
  fbell = [0, 1, 2, 1.1, 25, 0.75, 75, 0.5, 100, 0.2]
  abell = [0, 0, 0.1, 1, 10, 0.6, 25, 0.3, 50, 0.15, 90, 0.1, 100, 0]
  fm_bell(0.5, 2.0, 220, 0.5, abell, fbell, 1)
end

installs the @with_sound_note_hook and prints the line

  `# fm_violin: start 0.000, dur 1.000'.")
    if @notehook
      assert_type((proc?(@notehook) or func?(@notehook)), @notehook, 0, "a proc or a function name")
      prc = case @notehook
            when Proc
              @notehook
            when Symbol, String
              # older Ruby versions return -1 on method(:func).to_proc.arity
              if method(@notehook).to_proc.arity == 3
                method(@notehook).to_proc
              else
                lambda do |inst_name, start, dur| @notehook.call(inst_name, start, dur) end
              end
            end
      @with_sound_note_hook.add_hook!("with-sound-note-hook", &prc)
    end
    set_help
  end
  attr_reader :output, :with_sound_note_hook
  alias help description
  
  def to_s
    format("#<%s: output: %s, channels: %d, srate: %d%s>",
           self.class, @output.inspect, @channels, @srate.to_i,
           @reverb ? format(", reverb: %s, reverb-channels: %d", @reverb, @reverb_channels) : "")
  end
  alias inspect to_s

  def describe
    show_local_variables
  end
  
  def with_sound(*args, &body)
    com = format("%s#%s: temporary sound, args %s", self.class, get_func_name, args.inspect)
    ws = self.class.new(:output,            get_args(args, :output, @output),
                        :comment,           get_args(args, :comment, com),
                        :play,              get_args(args, :play, false),
                        :statistics,        get_args(args, :statistics, false),
                        :save,              get_args(args, :save, true),
                        :reverb,            get_args(args, :reverb, [:old_reverb, @ws_reverb]),
                        :reverb_data,       get_args(args, :reverb_data, @reverb_data),
                        :reverb_file_name,  get_args(args, :reverb_file_name, @revfile),
                        :reverb_channels,   get_args(args, :reverb_channels, @reverb_channels),
                        :delete_reverb,     get_args(args, :delete_reverb, @delete_reverb),
                        :decay_time,        get_args(args, :decay_time, @decay_time),
                        :continue_old_file, get_args(args, :continue_old_file, @continue),
                        :scaled_to,         get_args(args, :scaled_to, @scaled_to),
                        :scaled_by,         get_args(args, :scaled_by, @scaled_by),
                        :notehook,          get_args(args, :notehook, @notehook),
                        :save_body,         get_args(args, :save_body, @save_body),
                        :channels,          get_args(args, :channels, @channels),
                        :srate,             get_args(args, :srate, @srate).to_f,
                        :header_type,       get_args(args, :header_type, @header_type),
                        :data_format,       get_args(args, :data_format, @data_format),
                        :audio_format,      get_args(args, :audio_format, @audio_format),
                        :verbose,           get_args(args, :verbose, @verbose),
                        :info,              get_args(args, :info, @info),
                        :clipped,           get_args(args, :clipped, @clipped),
                        :offset,            get_args(args, :offset, 0.0),
                        &body)
    ws.run
    ws
  end

  def scaled_to(scale, &body)
    with_current_sound(:scaled_to, scale, &body)
  end

  def scaled_by(scale, &body)
    with_current_sound(:scaled_by, scale, &body)
  end
  
  def with_offset(secs, &body)
    with_current_sound(:offset, secs, &body)
  end
  
  def with_current_sound(*args, &body)
    output, offset, scaled_to, scaled_by = nil
    optkey(args, binding,
           [:output, tempnam],
           [:offset, 0.0],
           [:scaled_to, false],
           [:scaled_by, false])
    ws = with_sound(:output, outfile,
                    :scaled_to, scaled_to,
                    :scaled_by, scaled_by,
                    :offset, offset,
                    *args, &body)
    clm_mix(ws.output)
    remove_file(ws.output)
    ws
  end

  def sound_let(*args, &body)
    outfile_list = []
    arg_list = []
    args.each do |sl_args|
      if proc?(sl_args) or (array?(sl_args) and proc?(sl_args.last))
        if proc?(sl_args)
          ws_body = sl_args
          ws_args = []
        else
          ws_body = sl_args.pop
          ws_args = sl_args.dup
        end
        ws_args.push(:output, tempnam())
        outfile_list.push(with_sound(*ws_args, &ws_body).output)
        arg_list.push(outfile_list.last)
      else
        arg_list.push(sl_args)
      end
    end
    body.call(*arg_list)
  rescue
    raise
  ensure
    outfile_list.apply(:remove_file)
  end

  def clm_load(rbm_file, *args)
    assert_type(File.exists?(rbm_file), rbm_file, 0, "an existing file")
    with_sound(*args) do
      Snd.message("Loading %s", rbm_file.inspect) if @verbose
      eval(File.open(rbm_file).read, nil, format("(clm_load %s)", rbm_file), 1)
    end
  end

  def with_mix(*args)
    body_str = args.pop
    assert_type(string?(body_str), body_str, 0, "a string (body string)")
    beg = if number?(args[-1])
            args.pop
          else
            0
          end
    fname = args.pop
    assert_type(string?(fname), fname, 0, "a string (filename)")
    out_file = fname + ".snd"
    rbm_file = fname + ".rbm"
    snd_time = if File.exists?(out_file)
                 File.mtime(out_file)
               else
                 :load
               end
    old_body = if File.exists?(rbm_file)
                 File.open(rbm_file).read
               else
                 ""
               end
    rbm_time = if old_body == body_str
                 File.mtime(rbm_file)
               else
                 File.open(rbm_file, "w") do |f| f << body_str end
                 :load
               end
    if snd_time == :load or rbm_time == :load or snd_time < rbm_time
      clm_load(rbm_file,
               :output, out_file,
               :comment, format("[%s, %s]", args.to_s.inspect, body_str.inspect),
               *args)
    end
    clm_mix(out_file, :output_frame, seconds2samples(beg))
  end

  def with_sound_info(name, start, dur)
    Snd.message("%s: start %1.3f, dur %1.3f", name, start, dur) if @info
    @with_sound_note_hook.call(name, start, dur) if @notehook
  end
  
  def run_instrument(start, dur, *locsig_args, &environ)
    name = get_func_name(3)
    with_sound_info(name, start, dur)
    @clm_instruments.store(environ, [name, start, dur])
    degree, distance, reverb_amount = nil
    optkey(locsig_args, binding,
           [:degree, random(90.0)],
           [:distance, 1.0],
           [:reverb_amount, 0.05])
    @reverb_amount = reverb_amount
    @locsig = make_locsig(:degree, degree,
                          :distance, distance,
                          :reverb, reverb_amount,
                          :output, (@clm ? @ws_output : false),
                          :revout, (@clm ? @ws_reverb : false),
                          :channels, @channels,
                          :type, @locsig_type)
  end

  # case chan
  # when Integer
  #   # jc_reverb_rb, jl_reverb_rb, nrev_rb
  #   body.call(sample_of_CHAN, current_sample_number)
  # when :frames
  #   # freeverb_rb
  #   body.call(frame_of_reverb_file, current_sample_number)
  # end
  def run_reverb(start, dur, chan, &environ)
    name = get_func_name(3)
    with_sound_info(name, start, dur)
    @clm_instruments.store(environ, [name, start, dur])
    if @verbose
      Snd.message("%s on %d in and %d out channel%s",
                  name,
                  @reverb_channels,
                  @channels,
                  (@channels > 1 ? "s" : ""))
    end
    unless chan.between?(0, @reverb_channels - 1)
      Snd.raise(:out_of_range, chan, @reverb_channels, "reverb channel number is out of range")
    end
  end
  
  def run(&body)
    @body ||= body
    set_clm_table_size($clm_table_size)
    set_mus_file_buffer_size($clm_file_buffer_size)
    set_mus_array_print_length($clm_array_print_length)
    if @clipped == :undefined
      if (@scaled_by or @scaled_to) and
          [Mus_bfloat, Mus_lfloat, Mus_bdouble, Mus_ldouble].member?(@data_format)
        set_mus_file_data_clipped(false)
      else
        set_mus_file_data_clipped($clm_clipped)
      end
    else
      set_mus_file_data_clipped(@clipped)
    end
    if defined? set_auto_update_interval then set_auto_update_interval(0.0) end
    set_mus_srate(@srate)
    before_output
    init_process_time
    run_body
    after_output
    if @reverb
      mus_close(@ws_reverb) if mus_output?(@ws_reverb)
      old_reverb = @ws_reverb
      # non-RUN_REVERB...END functions need it here
      $reverb = make_file2sample(@revfile)
      @dur += @decay_time
      run_reverb_body
      mus_close(@ws_reverb) if mus_input?(@ws_reverb)
      $reverb = @ws_reverb = old_reverb
      after_reverb
    end
    if @statistics
      set_process_time
      statistics
    end
    finish_sound
    1.upto(@play) do play_it end
    set_mus_srate(@old_srate)
  end

  protected
  def run_body
    frm1 = ws_frame_location
    instance_eval(&@body)
  rescue Interrupt, ScriptError, StandardError
    set_mus_srate(@old_srate)
    finish_sound
    err, $! = $!, nil
    show_local_variables
    case err
    when Interrupt, Break
      # C-g, ws_break
      Snd.message("with_sound body: %s", err.message)
    else
      raise err
    end
  else
    frm2 = ws_frame_location
    scaled_to_sound(frm1, frm2 - frm1) if @scaled_to
    scaled_by_sound(frm1, frm2 - frm1) if @scaled_by
  end
  
  def run_reverb_body
    assert_type((proc?(@reverb) or func?(@reverb)), @reverb, 0, "a proc object or a function name")
    case @reverb
    when Proc
      @reverb.call(@start, @dur, *@reverb_data)
    when String, Symbol
      snd_func(@reverb, @start, @dur, *@reverb_data)
    end
  rescue Interrupt, ScriptError, StandardError
    set_mus_srate(@old_srate)
    finish_sound
    case $!
    when Interrupt, Break
      # C-g, ws_break
      err, $! = $!, nil
      show_local_variables
      Snd.message("with_sound body (reverb): %s", err.message)
    else
      raise
    end
  end

  def show_local_variables
    Snd.message()
    # {run_instrument|reverb-proc => [instrument-name, start, dur]}
    # sorted by value[start]
    # environ == proc
    @clm_instruments.sort do |a, b| a.cadr.cadr <=> b.cadr.cadr end.each do |environ, vals|
      Snd.message("=== %s [%1.3f-%1.3f] ===", *vals)
      eval("local_variables", environ).each do |var|
        Snd.message("%s = %s", var, eval(var, environ).inspect)
      end
      Snd.message()
    end
    @clm_instruments.values
  end
  
  def tempnam
    if provided? :snd
      snd_tempnam()
    else
      @@file_number += 1
      dir = (ENV.map do |k, v| v if /TMP/ =~ k end.compact.first or "/tmp")
      format("%s/snd_%d_%d.snd", dir, $$, @@file_number)
    end
  end

  def make_reverb_file_name
    path = File.split(@output).first
    file = File.basename(@output, ".*") + ".reverb"
    file = path + "/" + file unless path == "."
    file
  end

  def init_process_time
    @rtime = Time.now
    tms = process_times
    @utime = tms.utime
    @stime = tms.stime
  end
  
  def set_process_time
    tms = process_times
    @rtime = Time.now - @rtime
    @utime = tms.utime - @utime
    @stime = tms.stime - @stime
  end
  
  def before_output
  end

  def after_output
  end

  def after_reverb
    if provided? :snd
      if snd = find_sound(@output)
        update_sound(snd)
      else
        open_sound(@output)
      end
    end
  end
  
  def ws_frame_location
  end

  def scaled_to_sound(from, to)
  end

  def scaled_by_sound(from, to)
  end
  
  def statistics(frms, data_fmt, type)
    Snd.message("filename: %s", @output.inspect)
    Snd.message("   chans: %d, srate: %d", @channels, @srate.round)
    if frms > 0
      Snd.message("  length: %1.3f (%d frames)", frms / @srate, frms)
    end
    if data_fmt and type
      Snd.message("  format: %s [%s]", mus_data_format_name(data_fmt), mus_header_type_name(type))
    end
    Snd.message("    real: %1.3f  (utime %1.3f, stime %1.3f)", @rtime, @utime, @stime) # 
    if frms > 1
      rt = (@srate / frms)
      Snd.message("   ratio: %1.2f  (uratio %1.2f)", @rtime * rt, @utime * rt)
    end
  end
  
  def finish_sound
    if defined? set_auto_update_interval then set_auto_update_interval(@old_update_interval) end
    @reverb and @delete_reverb and (not @continue) and remove_file(@revfile)
  end

  def play_it
  end
  
  def set_help
    self.description = format("\
# with_sound(*args) do ... end
# 
#   :output             $clm_file_name (#{$clm_file_name.inspect})
#   :channels           $clm_channels (#$clm_channels)
#   :srate              $clm_srate (#$clm_srate)
#   :header_type        $clm_header_type (#$clm_header_type)
#   :data_format        $clm_data_format (#$clm_data_format)
#   :audio_format       $clm_audio_format (#$clm_audio_format)
# 
#   :reverb             $clm_reverb (#{$clm_reverb.inspect})
#   :reverb_data        $clm_reverb_data (#{$clm_reverb_data.inspect})
#   :reverb_channels    $clm_reverb_channels (#$clm_reverb_channels)
#   :revfile            $clm_reverb_file_name (#{$clm_reverb_file_name.inspect})
#   :delete_reverb      $clm_delete_reverb (#$clm_delete_reverb)
# 
#   :decay_time         1.0
#   :scaled_to          false
#   :scaled_by          false
#   :continue_old_file  false
#   :notehook           $clm_notehook (#{$clm_notehook.inspect})
#  
#   :play               $clm_play (#$clm_play)
#   :statistics         $clm_statistics (#$clm_statistics)
#   :comment            $clm_comment (#{$clm_comment.inspect})
#   :locsig_type        $clm_locsig_type (#{$clm_locsig_type})
#   :verbose            $clm_verbose (#$clm_verbose)
#
## next option works only with newer Ruby versions
## save the with_sound-body in sound file's comment
#   :save_body          false
#
## if an instrument uses run_instrument/run_reverb and INFO is true, a
## message will be printed: `instrument_name: start 0.000, dur 0.000'
#   :info               $clm_info (#$clm_info)
#   :clipped            $clm_clipped (#$clm_clipped)
#
%s
#
## special with_dac options:
#   :bufsize            $clm_rt_bufsize (#$clm_rt_bufsize)
#   :device             $clm_output_device (#$clm_output_device)
# 
# Usage: with_sound(:play, 1, :statistics, true) do fm_violin end",
                              @clm ? "\
#   :player             $clm_player (#{$clm_player.inspect})
" : "\
## save sound after computing
#   :save               false")
  end
end

class Instrument < With_sound
  # Actually it isn't necessary to define instruments as methods of
  # class Instrument.
  def my_simp(start = 0, dur = 1, freq = 440, amp = 0.5, amp_env = [0, 1, 1, 1])
    os = make_sum_of_cosines(:frequency, freq, :cosines, 3)
    en = make_env(:envelope, amp_env, :scaler, amp, :duration, dur)
    fs = hz2radians(freq)
    pv = make_triangle_wave(:frequency, 6.0, :amplitude, 0.0025 * fs)
    rv = make_rand_interp(:frequency, 8.0, :amplitude, 0.005 * fs)
    run_instrument(start, dur) do
      sum_of_cosines(os, triangle_wave(pv) + rand_interp(rv)) * env(en)
    end
  end

  # simple violin, see snd-7/fm.html
  def fm_v(start = 0, dur = 1, freq = 440, amp = 0.5, fm_index = 1, amp_env = [0, 1, 1, 1])
    frq_scl = hz2radians(freq)
    maxdev = frq_scl * fm_index
    index1 = maxdev * (5.0 / log(freq))
    index2 = maxdev * 3.0 * ((8.5 - log(freq)) / (3.0 + freq / 1000.0))
    index3 = maxdev * (4.0 / sqrt(freq))
    carrier = make_oscil(:frequency, freq)
    fmosc1 = make_oscil(:frequency, freq)
    fmosc2 = make_oscil(:frequency, freq * 3.0)
    fmosc3 = make_oscil(:frequency, freq * 4.0)
    ampf = make_env(:envelope, amp_env, :scaler, amp, :duration, dur)
    indf1 = make_env(:envelope, [0, 1, 25, 0.4, 75, 0.6, 100, 0], :scaler, index1, :duration, dur)
    indf2 = make_env(:envelope, [0, 1, 25, 0.4, 75, 0.6, 100, 0], :scaler, index2, :duration, dur)
    indf3 = make_env(:envelope, [0, 1, 25, 0.4, 75, 0.6, 100, 0], :scaler, index3, :duration, dur)
    pervib = make_triangle_wave(:frequency, 0.5, :amplitude, 0.0025 *  frq_scl)
    ranvib = make_rand_interp(:frequency, 16.0, :amplitude, 0.005 * frq_scl)
    run_instrument(start, dur) do
      vib = triangle_wave(pervib) + rand_interp(ranvib)
      env(ampf) * oscil(carrier,
                        vib + \
                        env(indf1) * oscil(fmosc1, vib) + \
                        env(indf2) * oscil(fmosc2, 3.0 * vib) + \
                        env(indf3) * oscil(fmosc3, 4.0 * vib))
    end
  end
end

# Channel2Vct and Sample_Reader are helper classes used by class
# Snd_Instrument.  sample_reader has no possibility to set location
# (and direction) which is needed by instrument grani (clm-ins.rb).
class Channel2Vct
  # (channel->vct (beg 0) (dur len) (snd #f) (chn #f) (edpos #f))
  def initialize(snd = false, chn = false, start = 0, dir = 1)
    @vct = channel2vct(0, frames(snd, chn), snd, chn, false)
    @location = ((dir > 0) ? (start - 1) : (start + 1))
    @start = start
    @direction = dir
    @snd = snd
    @chn = chn
    @last_location = @vct.length - 1
  end
  attr_accessor :direction
  
  def inspect
    format("%s.new(%s, %s, %s, %s)", self.class, @snd.inspect, @chn.inspect, @start, @direction)
  end
  
  def to_s
    format("#<%s snd: %s, chn: %s, location: %d, direction: %d, vct: %s>",
           self.class, @snd.inspect, @chn.inspect, location, @direction, @vct.to_str)
  end

  def next
    if @direction > 0
      if @location < @last_location
        @location += 1
        @vct[@location]
      else
        0.0
      end
    else
      if @location > 0
        @location -= 1
        @vct[@location]
      else
        0.0
      end
    end
  end
  
  def close
    # not needed
  end
  
  def location
    if @direction > 0
      @location + 1
    else
      @location - 1
    end
  end

  def location=(val)
    if @direction > 0
      @location = val - 1
    else
      @location = val + 1
    end
  end
end

class Sample_Reader
  # (make-sample-reader (start-samp 0) (snd #f) (chn #f) (dir 1) (edpos #f))
  def initialize(snd = false, chn = false, start = 0, dir = 1)
    @reader = make_sample_reader(start, snd, chn, dir, false)
    @direction = dir
    @start = start
    @direction = dir
    @snd = snd
    @chn = chn
  end
  attr_accessor :direction
  
  def inspect
    format("%s.new(%s, %s, %s, %s)", self.class, @snd.inspect, @chn.inspect, @start, @direction)
  end
  
  def to_s
    @reader.inspect
  end

  def next
    read_sample(@reader)
  end

  def close
    free_sample_reader(@reader)
  end
  
  def location
    sample_reader_position(@reader)
  end

  def location=(val)
    # sample_reader_position isn't settable
  end
end

class Snd_Instrument < Instrument
  # place holder for special Snd instruments, see FULLMIX in
  # clm-ins.rb.

  def get_snd(file)
    snd = if integer?(file)
            file
          elsif string?(file)
            if s = find_sound(file)
              s
            else
              open_sound(file)
            end
          end
    if integer?(snd) and sound?(snd)
      snd
    else
      Snd.raise(:no_such_sound, snd)
    end
  end
  private :get_snd
  
  def make_ws_reader(file, *args)
    start, channel, direction = nil
    optkey(args, binding,
           [:start, 0],
           [:channel, 0],
           [:direction, 1])
    if get_args(args, :vct?, false)
      Channel2Vct.new(get_snd(file), channel, start, direction)
    else
      Sample_Reader.new(get_snd(file), channel, start, direction)
    end
  end

  # (read-sample reader)
  def ws_readin(rd)
    rd.next
  end

  def close_ws_reader(file, rd)
    rd.close
    if snd = (integer?(file) ? file : find_sound(file))
      close_sound_extend(snd)
    end
  end

  def ws_location(rd)
    rd.location
  end

  def set_ws_location(rd, val)
    rd.location = val.to_i
  end

  def ws_increment(rd)
    rd.direction
  end

  def set_ws_increment(rd, val)
    rd.direction = val.to_i
  end
  
  def ws_srate(file)
    srate(get_snd(file)).to_f
  end

  def ws_channels(file)
    channels(get_snd(file))
  end
  
  def ws_duration(file)
    snd = get_snd(file)
    frames(snd) / srate(snd).to_f
  end
end

class CLM_Instrument < Instrument
  # place holder for special Snd instruments, see FULLMIX in
  # clm-ins.rb.

  # (make-readin (:file) (:channel 0) (:start 0) (:direction 1)
  def make_ws_reader(file, *args)
    start, channel, direction = nil
    optkey(args, binding,
           [:start, 0],
           [:channel, 0],
           [:direction, 1])
    make_readin(:file, file, :channel, channel, :start, start, :direction, direction)
  end

  # (readin gen)
  def ws_readin(rd)
    readin(rd)
  end

  def close_ws_reader(file, rd)
    mus_close(rd)
  end

  def ws_location(rd)
    mus_location(rd)
  end

  def set_ws_location(rd, val)
    set_mus_location(rd, val)
  end

  def ws_increment(rd)
    mus_increment(rd)
  end

  def set_ws_increment(rd, val)
    set_mus_increment(rd, val)
  end

  def ws_srate(file)
    mus_sound_srate(file)
  end

  def ws_channels(file)
    mus_sound_chans(file)
  end
  
  def ws_duration(file)
    mus_sound_duration(file)
  end
end

class With_Snd < Snd_Instrument
  def initialize(*args, &body)
    @clm = false
    super(*args, &body)
  end
  
  def run_instrument(start, dur, *locsig_args, &body)
    super(start, dur, *locsig_args, &body)
    beg = seconds2samples(start + @offset)
    len = seconds2samples(dur)
    ws_interrupt?
    out_data = Vct.new(len) do |i| body.call(beg + i) end
    @channels.times do |chn|
      mix_vct(out_data.scale(locsig_ref(@locsig, chn)), beg, @ws_output, chn, false)
    end
    @reverb_channels.times do |chn|
      scl = @reverb_amount * locsig_ref(@locsig, [chn, @channels - 1].min)
      mix_vct(out_data.scale(scl), beg, @ws_reverb, chn, false)
    end
    ws_interrupt?
  end
  
  def run_reverb(start, dur, chan = 0, &body)
    beg = seconds2samples(start)
    len = seconds2samples(dur)
    out_data = make_sound_data(@channels, len)
    ws_interrupt?
    case chan
    when Integer
      super(start, dur, chan, &body)
      let(make_sample_reader(beg, @ws_reverb, chan)) do |rd|
        len.times do |i|
          frame2sound_data!(out_data, i, body.call(read_sample(rd), i + beg))
        end
        free_sample_reader(rd)
      end
    when :frames
      super(start, dur, 0, &body)
      frm = make_frame(@reverb_channels)
      readers = make_array(@reverb_channels) do |chn|
        make_sample_reader(beg, @ws_reverb, chn)
      end
      len.times do |i|
        frame2sound_data!(out_data, i, body.call(snd2frame!(readers, frm), i + beg))
      end
      readers.each do |rd|
        free_sample_reader(rd)
      end
    end
    v = Vct.new(len)
    @channels.times do |chn|
      mix_vct(sound_data2vct(out_data, chn, v), beg, @ws_output, chn, false)
    end
    ws_interrupt?
  end
  
  add_help(:clm_mix, "clm_mix(filename, *args)
        :input_frame  = 0
        :output_frame = 0
        :frames       = mus_sound_frames(filename)
        :scale        = 1.0
Example: clm_mix(\"tmp\")")
  def clm_mix(filename, *args)
    input_frame, output_frame, frames, scale = nil
    optkey(args, binding,
           [:input_frame, 0],
           [:output_frame, 0],
           [:frames, mus_sound_frames(filename)],
           [:scale, 1.0])
    unless snd = find_sound(filename)
      unless snd = open_sound(filename)
        Snd.raise(:no_such_file, filename, "file name required")
      end
    end
    [channels(snd), @channels].min.times do |chn|
      scale_channel(scale, input_frame, frames, snd, chn) if scale.nonzero?
      mix_vct(channel2vct(input_frame, frames, snd, chn), output_frame, @ws_output, chn, false)
    end
    revert_sound(snd)
    close_sound_extend(snd)
  end

  def with_reverb(snd, reverb_amount)
    @channels = channels(snd)
    set_mus_srate(@srate)
    len = frames(snd)
    @start = 0
    @dur = len / @srate + @decay_time
    init_process_time
    if rsnd = find_sound(@revfile)
      if channels(rsnd) == @reverb_channels and srate(rsnd) == @srate.to_i
        delete_samples(0, len, rsnd, 0)
      else
        remove_file(@revfile)
        rsnd = new_sound(@revfile, @header_type, @data_format, @srate.to_i, @reverb_channels)
      end
    else
      rsnd = new_sound(@revfile, @header_type, @data_format, @srate.to_i, @reverb_channels)
    end
    vct2channel(channel2vct(0, len, snd, 0).scale(reverb_amount), 0, len, rsnd, 0)
    @ws_output = snd
    @ws_reverb = rsnd
    save_sound(rsnd)
    run_reverb_body
    if @statistics
      set_process_time
      statistics
    end
    finish_sound
    1.upto(@play) do play_it end
    set_mus_srate(@old_srate)
  end

  protected
  def before_output
    if @output
      if snd = find_sound(@output)
        unless @continue
          if channels(snd) == @channels and srate(snd) == @srate.round
            set_comment(snd, @comment)
            @channels.times do |chn| set_frames(0, snd, chn) end
          else
            revert_sound(snd)
            close_sound_extend(snd)
            snd = new_sound(@output, @header_type, @data_format, @srate.to_i, @channels, @comment)
          end
        end
      else
        snd = new_sound(@output, @header_type, @data_format, @srate.to_i, @channels, @comment)
      end
    else
      snd = false
    end
    $output = @ws_output = snd
    # recursive call: :reverb, [:old_reverb, @ws_reverb]
    unless @ws_reverb
      if @reverb_channels.nonzero?
        if rsnd = find_sound(@revfile)
          unless @continue
            if channels(rsnd) == @reverb_channels and srate(rsnd) == @srate.round
              @reverb_channels.times do |chn| set_frames(0, rsnd, chn) end
            else
              remove_file(@revfile)
              rsnd = new_sound(@revfile, @header_type, @data_format, @srate.round, @reverb_channels)
            end
          end
        else
          rsnd = new_sound(@revfile, @header_type, @data_format, @srate.round, @reverb_channels)
        end
      else
        rsnd = false
      end
      $reverb = @ws_reverb = rsnd
    end
    @start = frames(@ws_output) / @srate
  end

  def after_output
    @dur = frames(@ws_output) / @srate
  end

  def ws_frame_location
    frames(@ws_output)
  end

  def scaled_to_sound(beg, len)
    @channels.times do |chn| scale_to(@scaled_to, @ws_output, chn) end
  end

  def scaled_by_sound(beg, len)
    @channels.times do |chn| scale_channel(@scaled_by, beg, len, @ws_output, chn) end
  end

  def statistics
    super(frames(@ws_output), data_format(@ws_output), header_type(@ws_output))
    Snd.message(" max out: %s", maxamp(@ws_output, true).to_string)
    if @reverb
      Snd.message(" max rev: %s", maxamp(@ws_reverb, true).to_string)
    end
  end

  def finish_sound
    super
    save_sound(@ws_output) if @save
    select_sound(@ws_output)
  end

  def play_it
    play(0, @ws_output)
  end

  # with_closed_sound(snd) do |snd_name| ... end
  # returns new snd index
  def with_closed_sound(snd, &body)
    snd_name = file_name(snd)
    save_sound(snd)
    close_sound(snd)
    body.call(snd_name)
    open_sound(snd_name)
  end
end

class With_CLM < CLM_Instrument
  def initialize(*args, &body)
    @clm = true
    super(*args, &body)
  end

  def run_instrument(start, dur, *locsig_args, &body)
    super(start, dur, *locsig_args, &body)
    ws_interrupt?
    each_sample(start + @offset, dur) do |samp|
      locsig(@locsig, samp, body.call(samp))
    end
    ws_interrupt?
  end

  def run_reverb(start, dur, chan = 0, &body)
    @ws_reverb = $reverb
    ws_interrupt?
    case chan
    when Integer
      super(start, dur, chan, &body)
      each_sample(start, dur) do |samp|
        frame2file(@ws_output, samp, body.call(in_any(samp, chan, @ws_reverb), samp))
      end
    when :frames
      super(start, dur, 0, &body)
      frm = make_frame(@reverb_channels)
      each_sample(start, dur) do |samp|
        frame2file(@ws_output, samp, body.call(file2frame(@ws_reverb, samp, frm), samp))
      end
    end
    ws_interrupt?
  end

  add_help(:clm_mix, "clm_mix(filename, *args)
        :input_frame  = 0
        :output_frame = 0
        :frames       = mus_sound_frames(filename)
        :scale        = 1.0
Example: clm_mix(\"tmp\")")
  def clm_mix(filename, *args)
    input_frame, output_frame, frames, scale = nil
    optkey(args, binding,
           [:input_frame, 0],
           [:output_frame, 0],
           [:frames, mus_sound_frames(filename)],
           [:scale, 1.0])
    with_closed_output do
      if provided? :snd
        if scale.nonzero?
          unless (snd = find_sound(filename))
            snd = open_sound(filename)
          end
          channels(snd).times do |chn| scale_channel(scale, input_frame, frames, snd, chn) end
          save_sound(snd)
          close_sound_extend(snd)
        end
        mus_mix(@output, filename, output_frame, frames, input_frame)
      else
        mus_mix(@output, filename, output_frame, frames, input_frame,
                make_mixer(@channels, *(1..@channels * @channels).map do scale end))
      end
    end
  end
  
  protected
  def before_output
    if @continue
      @ws_output = continue_sample2file(@output)
      @ws_reverb = continue_sample2file(@revfile) if @reverb_channels.nonzero?
    else
      remove_file(@output)
      @ws_output = make_sample2file(@output, @channels, @data_format, @header_type, @comment)
      # recursive call: :reverb, [:old_reverb, @ws_reverb]
      unless @ws_reverb
        if @reverb_channels.nonzero?
          remove_file(@revfile)
          @ws_reverb = make_sample2file(@revfile, @reverb_channels, @data_format, @header_type)
        end
      end
    end
    with_closed_output do
      @start = mus_sound_duration(@output)
    end
    $output = @ws_output
    $reverb = @ws_reverb
  end

  def after_output
    if provided? :snd and (snd = find_sound(@output))
      update_sound(snd)
    end
    with_closed_output do
      @dur = mus_sound_duration(@output)
    end
  end

  def ws_frame_location
    with_closed_output do
      mus_sound_frames(@output)
    end
  end

  def scaled_to_sound(beg, len)
    if provided? :snd
      unless snd = find_sound(@output)
        snd = open_sound(@output)
      end
      @channels.times do |chn| scale_to(@scaled_to, snd, chn) end
      save_sound(snd)
    else
      amax = mus_sound_maxamp(@output)
      tmpa = []
      1.step(amax.length - 1, 2) do |i| tmpa << amax[i] end
      scale = ([0.0, [1.0, @scaled_to].min].max / tmpa.max) - 1
      mus_mix(@output, @output, beg, len, beg,
              make_mixer(@channels, *(1..@channels * @channels).map do scale end))
    end
  end

  def scaled_by_sound(beg, len)
    if provided? :snd
      unless (snd = find_sound(@output))
        snd = open_sound(@output)
      end
      @channels.times do |chn| scale_channel(@scaled_by, beg, len, snd, chn) end
      save_sound(snd)
    else
      scale = @scaled_by - 1
      mus_mix(@output, @output, beg, len, beg,
              make_mixer(@channels, *(1..@channels * @channels).map do scale end))
    end
  end
  
  def statistics
    super(mus_sound_frames(@output), mus_sound_data_format(@output), mus_sound_header_type(@output))
    ch = "@"
    mus_sound_maxamp(@output).each_pair do |s, v|
      Snd.message("maxamp %s: %1.3f (near %1.3f secs)", ch.next!, v, s / @srate)
    end
    if @reverb
      ch = "@"
      mus_sound_maxamp(@revfile).each_pair do |s, v|
        Snd.message("revamp %s: %1.3f (near %1.3f secs)", ch.next!, v, s / @srate)
      end
    end
  end
  
  def finish_sound
    super
    mus_close(@ws_output) if mus_output?(@ws_output)
    if provided? :snd
      if @ws_output = find_sound(@output)
        select_sound(@ws_output)
        update_sound(@ws_output)
      else
        @ws_output = open_sound(@output)
      end
    end
  end

  def play_it
    if provided? :snd
      play(0, @ws_output)
    else
      system(format("%s %s", @player, @output))
    end
  end

  def with_closed_output(&body)
    mus_close(@ws_output) if mus_output?(@ws_output)
    ret = body.call
    $output = @ws_output = continue_sample2file(@output)
    ret
  end
end

class With_DAC < Snd_Instrument
  # no reverb
  # handles instruments parallel if computing is fast enough
  def initialize(*args, &body)
    @clm = false
    super(*args, &body)
    @bufsize = get_args(args, :bufsize, $clm_rt_bufsize)
    @device  = get_args(args, :device, $clm_output_device)
    @ws_reverb = @ws_output = @reverb = false
    @output = "dac"
    # @instruments = [[first_samp, last_samp, body], ...]
    @instruments = []
    @current_sample = 0
  end

  def run_instrument(start, dur, *args, &body)
    # A bad idea; it scales all current parallel instruments in method
    # REAL_RUN with the last called instrument-locsig-gen.
    super(start, dur, *args, &body)
    beg, ends = times2samples(start, dur)
    @instruments.push([beg, ends, body])
    ws_interrupt?
    real_run(beg)
    ws_interrupt?
  end

  def real_run(sample)
    while @current_sample < sample
      idxs = []
      dac_vct = Vct.new(@bufsize)
      # calls bufsize times all instruments in current sample-window
      # and accumulates the result in dac_vct
      @instruments.each_with_index do |arg, idx|
        beg, ends, body = arg
        if @current_sample.between?(beg, ends)
          dac_vct.add!(Vct.new(@bufsize) do |i| body.call(@current_sample + i) end)
        elsif @current_sample >= ends
          idxs.push(idx)
        end
      end
      idxs.each do |idx| @instruments.delete_at(idx) end
      dac_data = make_sound_data(@channels, @bufsize)
      @channels.times do |chn|
        vct2sound_data(dac_vct.scale(locsig_ref(@locsig, chn)), dac_data, chn)
      end
      mus_audio_write(@ws_output, dac_data, @bufsize)
      @current_sample += @bufsize
    end
  end

  protected
  def before_output
    @ws_output = mus_audio_open_output(@device, @srate.round, @channels, @audio_format,
                                       @bufsize * @channels * 2)
    if @ws_output < 0
      Snd.raise(:mus_error, @ws_output, "can't open DAC")
    end
  end

  def after_output
    # flush contents of instrument array
    real_run(@current_sample + 1) until @instruments.empty?
  end

  def finish_sound
    mus_audio_close(@ws_output) if number?(@ws_output)
    @ws_output = false
  end
  
  def statistics
    super(0, false, false)
  end
end

include WS
require "v"

# ws.rb ends here

About Koders | Resources | Downloads | Support | Black Duck | Terms of Service | DMCA | Privacy Policy | Contact Us