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
#![allow(non_camel_case_types, non_upper_case_globals, non_snake_case)]
use std::marker::PhantomData;
use std::mem;
use std::io;
use csound_sys;
use csound_sys::RTCLOCK;
use rtaudio::{CS_AudioDevice, CS_MidiDevice, RT_AudioParams};
use callbacks::*;
use channels::{ChannelBehavior, ChannelInfo, ChannelHints, pvs_DataExt};
use enums::{Status, ChannelData, MessageType, ControlChannelType, Language};
use handler::{Handler, Callbacks};
use std::panic;
use std::fmt;
use std::ffi::{CStr, CString, NulError};
use std::str::Utf8Error;
use std::str;
use libc::{c_int, c_double, c_char, c_void, c_long /*,memcpy ,fopen, fclose*/};
// the length in bytes of the output type name in csound
const OUTPUT_TYPE_LENGTH:usize = 6;
// The length in bytes of the output format name in csound
const OUTPUT_FORMAT_LENGTH:usize = 8;
//pub type controlChannelType = u32;
/// Struct with information about a csound opcode.
///
/// Used to get the complete csound opcodes list, so the
/// [`Csound::get_opcode_list_entry`](struct.Csound.html#method.get_opcode_list_entry) method will return
/// a list of OpcodeListEntry, where each of this struct contain information relative
/// a specific csound opcode.
#[derive(Default, Debug)]
pub struct OpcodeListEntry{
/// The opcode name.
pub opname: String,
/// The opcode ouput type.
pub outypes: String,
/// The opcode input type.
pub intypes: String,
/// specific opcode flags.
pub flags: i32,
}
pub struct CallbackHandler{
pub callbacks: Callbacks<'static>,
}
unsafe impl Send for CallbackHandler {}
impl fmt::Debug for CallbackHandler {
fn fmt(&self, f: & mut fmt::Formatter) -> fmt::Result {
"callbacks...".fmt(f)
}
}
/// Opaque struct representing an csound object
///
/// This is the main struct used to access the libcsound API functions.
/// The Engine element is the inner representation of the CSOUND opaque pointer and is
/// the object wich talk directly with the libcsound c library.
///
#[derive(Debug)]
pub struct Csound{
/// Inner representation of the CSOUN opaque pointer
engine: Engine<CallbackHandler>,
}
// H is the hndler wich is defined in the lib.rs file
pub struct Engine<H>{
pub inner: Box<Inner<H>>,
}
/// Opaque struct representing a csound object
#[derive(Debug)]
pub struct Inner<H>{
pub csound: *mut csound_sys::CSOUND,
// base params used to defined the csound's internal buffers
myflt: usize,
pub handler: H,
}
unsafe impl<H: Send> Send for Inner<H> {}
impl<H: Handler> Engine<H> {
/// Create a new csound object
pub fn new(handler: H) -> Engine<H> {
unsafe {
// Csound must not handle signals
csound_sys::csoundInitialize(csound_sys::CSOUNDINIT_NO_SIGNAL_HANDLER as c_int);
csound_sys::csoundInitialize(csound_sys::CSOUNDINIT_NO_ATEXIT as c_int);
// For now we will assue there isn't host Data
let csound_sys = csound_sys::csoundCreate(::std::ptr::null_mut());
assert!(!csound_sys.is_null());
let ret = Engine {
inner: Box::new(Inner{
csound: csound_sys,
handler: handler,
myflt: csound_sys::csoundGetSizeOfMYFLT() as usize,
}),
};
ret.default_config();
return ret;
}
}
fn default_config(& self){
unsafe{
let host_data_ptr = &*self.inner as *const _ as *const _;
csound_sys::csoundSetHostData(self.inner.csound, host_data_ptr as *mut c_void);
}
}
}
impl Csound {
/// Create a new csound object.
///
/// This is the core of almost all operations in the csound library.
/// A new instance of csound will created by this function, a custom callback handler will be used,
/// This custom callback handler will be active only if the user calls some of the
/// callbacks setting functions which receive a closure for a specific callback.
///
/// # Example
///
/// ```
/// // Creates a Csound instance and use a custom callback handler
/// let csound = Csound::new();
/// // configure a callback passing a closure for the specific callback, in this
/// // case the message_string callback:
/// let func = |mtype:u32, message:&str| {
/// println!("message type: {} message content: {}", mtype, message);
/// };
/// // enable the callback passign the closuro to the custom callback handler
/// csound..message_string_callback(func);
/// csound.compile_csd(csd_filename).unwrap();
/// csound.start();
/// ...
/// ```
pub fn new() -> Csound {
Csound {
engine: Engine::new( CallbackHandler {
callbacks: Callbacks::default(),
}),
}
}
/// Initialise Csound library with specific flags.
/// This function is called internally by Csound::new(), so there is generally no need to use it explicitly unless
/// you need to avoid default initilization that sets signal handlers and atexit() callbacks.
/// Return value is Ok() on success or an error message in case of failure
pub fn initialize(flags: i32) -> Result<(), &'static str>{
unsafe {
match csound_sys::csoundInitialize(flags as c_int) as i32{
csound_sys::CSOUND_ERROR => Err("Can't to initialize csound "),
csound_sys::CSOUND_SUCCESS => Ok(()),
value => {
if value > 0 {
Err("Initialization was done already")
}else{
Err("Unknown error - can to initialize")
}
},
}
}
}
/// Sets a single csound option(flag).
///
/// NB: blank spaces are not allowed.
/// # Returns
/// returns Ok on success or a error message in case the option is invalid.
pub fn set_option(&self, options:&str) -> Result<(), &'static str>{
let op = CString::new(options).map_err(|_| "Error parsing the string")?;
unsafe{
match csound_sys::csoundSetOption(self.engine.inner.csound, op.as_ptr()){
csound_sys::CSOUND_SUCCESS => Ok(()),
_ => Err("Options not valid"),
}
}
}
/// Prepares Csound for performance.
///
/// Normally called after compiling a csd file or an orc file, in which case score preprocessing is performed and
/// performance terminates when the score terminates.
/// However, if called before compiling a csd file or an orc file,
/// score preprocessing is not performed and "i" statements are dispatched as real-time events,
/// the <CsOptions> tag is ignored, and performance continues indefinitely or until ended using the API.
/// # Example
///
/// ```
/// let csound = Csound::new();
/// csound.compile_csd(csd_filename).unwrap();
/// csound.start();
/// ...
/// ```
///
pub fn start(&mut self) -> Result<(), &'static str>{
unsafe {
let result: c_int = csound_sys::csoundStart(self.engine.inner.csound);
if result == csound_sys::CSOUND_SUCCESS {
Ok(())
}
else{
Err("Csound is already started, call csoundReset() before starting again.")
}
}
}
/// Returns the version number times 1000
/// for example, if the current csound version is 6.12.0
/// this function will return 6120.
pub fn version(& self) -> u32{
unsafe {
csound_sys::csoundGetVersion() as u32
}
}
/// Returns the API version number times 100
pub fn api_version(&self) -> u32{
unsafe {
csound_sys::csoundGetAPIVersion() as u32
}
}
/* Engine performance functions implementations ********************************************************* */
/// Stops a perform() all of the running instance of Csound.
/// *Note*: that it is not guaranteed that [`Csound::perform`](struct.Csound.html#method.perform) has already stopped when this function returns.
pub fn stop(&self){
unsafe{
csound_sys::csoundStop(self.engine.inner.csound);
}
}
/// Resets all internal memory and state in preparation for a new performance.
///
/// Enables external software to run successive Csound performances without reloading Csound.
pub fn reset(&self){
unsafe{
csound_sys::csoundReset(self.engine.inner.csound);
}
}
/// Compiles Csound input files (such as an orchestra and score, or CSD) as directed by the supplied command-line arguments , but does not perform them.
///
/// This function cannot be called during performance, and before a repeated call, csoundReset() needs to be called.
/// # Arguments
/// * `args` A slice containing the arguments to be passed to csound
/// # Returns
/// A error message in case of failure
pub fn compile(&self, args:&[&str]) -> Result<(), &'static str> {
if args.is_empty(){
return Err("Not enough arguments");
}
let arguments:Vec<CString> = args.iter().map(|&arg| CString::new(arg).unwrap()).collect();
let args_raw:Vec<*const c_char> = arguments.iter().map(|arg| arg.as_ptr()).collect();
let argv: *const *const c_char = args_raw.as_ptr();
unsafe{
match csound_sys::csoundCompile(self.engine.inner.csound, args_raw.len() as c_int, argv){
csound_sys::CSOUND_SUCCESS => Ok(()),
_ => Err("Can't compile carguments"),
}
}
}
/// Read arguments, parse and compile an orchestra, read, process and load a score.
pub fn compile_args(&self, args:&[&str]) -> Result<(), &'static str> {
if args.is_empty(){
return Err("Not enough arguments");
}
let arguments:Vec<CString> = args.iter().map(|&arg| CString::new(arg).unwrap()).collect();
let args_raw:Vec<*const c_char> = arguments.iter().map(|arg| arg.as_ptr()).collect();
let argv: *const *const c_char = args_raw.as_ptr();
unsafe{
match csound_sys::csoundCompileArgs(self.engine.inner.csound, args_raw.len() as c_int, argv){
csound_sys::CSOUND_SUCCESS => Ok(()),
_ => Err("Can't compile arguments"),
}
}
}
/// Compiles a Csound input file (CSD, .csd file), but does not perform it.
/// If [`Csound::start`](struct.Csound.html#method.start) is called before `compile_csd`, the <CsOptions> element is ignored
/// (but se_option can be called any number of times),
/// the <CsScore> element is not pre-processed, but dispatched as real-time events;
/// and performance continues indefinitely, or until ended by calling [`Csound::stop`](struct.Csound.html#method.stop) or some other logic.
/// In this "real-time" mode, the sequence of calls should be:
///
///```
/// let csound = Csound::new();
/// csound.set_option("-an_option");
/// csound.set_option("-another_option");
/// csound.start();
/// csound.compile_csd(csd_filename);
/// while true{
/// // Send realtime events
/// csound.send_score_event("i 1 0 5 4.5 6.2");
/// //...
/// // some logic to break the loop after a performance of realtime events
/// }
/// ```
///
/// *Note*: this function can be called repeatedly during performance to replace or add new instruments and events.
/// But if csoundCompileCsd is called before csoundStart, the <CsOptions> element is used,the <CsScore> section is pre-processed and dispatched normally,
/// and performance terminates when the score terminates, or [`Csound::stop`](struct.Csound.html#method.stop) is called.
/// In this "non-real-time" mode (which can still output real-time audio and handle real-time events), the sequence of calls should be:
///
/// ```
/// let csound = Csound::new();
/// csound.compile_csd(csd_filename);
/// csound.start();
/// while true {
/// let result = csound.perform_ksmps();
/// if result == true{
/// break;
/// }
/// }
/// ```
///
/// # Arguments
/// * `csd` A reference to .csd file name
///
pub fn compile_csd(&self, csd: &str) -> Result<(), &'static str> {
if csd.is_empty(){
return Err("Empty file name");
}
let path = CString::new(csd).map_err(|_| "Bad file name")?;
unsafe {
match csound_sys::csoundCompileCsd(self.engine.inner.csound, path.as_ptr()){
csound_sys::CSOUND_SUCCESS => Ok(()),
_ => Err("Can't compile the csd file"),
}
}
}
/// Behaves the same way as [`Csound::compile_csd`](struct.Csound.html#method.compile_csd),
/// except that the content of the CSD is read from the csd_text string rather than from a file.
/// This is convenient when it is desirable to package the csd as part of an application or a multi-language piece.
///
/// # Arguments
/// * `csd_text` A reference to the text to be compiled by csound
///
pub fn compile_csd_text(&self, csdText: &str) -> Result<(), &'static str>{
if csdText.is_empty(){
return Err("Empty file name");
}
let path = CString::new(csdText).map_err(|_e| "Bad file name")?;
unsafe {
match csound_sys::csoundCompileCsdText(self.engine.inner.csound, path.as_ptr()){
csound_sys::CSOUND_SUCCESS => Ok(()),
_ => Err("Can't compile the csd file"),
}
}
}
/// Parse, and compile the given orchestra from an ASCII string, also evaluating any global space code (i-time only)
/// this can be called during performance to compile a new orchestra.
///
/// ```
/// let csound = Csound::new();
/// let orc_code = "instr 1 \n
/// a1 rand 0dbfs/4 \n
/// out a1 \n";
/// csound.compile_orc(orc_code);
/// ```
///
/// # Arguments
/// * `orcPath` A reference to .orc file name
///
pub fn compile_orc(&self, orcPath: &str) -> Result<(), &'static str> {
if orcPath.is_empty(){
return Err("Empty file name")
}
let path = CString::new(orcPath).map_err(|_e| "Bad file name")?;
unsafe {
match csound_sys::csoundCompileOrc(self.engine.inner.csound, path.as_ptr()){
csound_sys::CSOUND_SUCCESS => Ok(()),
_ => Err("Can't to compile orc"),
}
}
}
/// Async version of [`Csound::compile_orc`](struct.Csound.html#method.compile_orc). The code is parsed and compiled,
/// then placed on a queue for asynchronous merge into the running engine, and evaluation.
/// The function returns following parsing and compilation.
///
/// # Arguments
/// * `orcPath` A reference to .orc file name
///
pub fn compile_orc_async(&self, orcPath: &str) -> Result<(), &'static str>{
if orcPath.is_empty(){
return Err("Empty file name")
}
let path = CString::new(orcPath).map_err(|_e| "Bad file name")?;
unsafe {
match csound_sys::csoundCompileOrcAsync(self.engine.inner.csound, path.as_ptr()){
csound_sys::CSOUND_SUCCESS => Ok(()),
_ => Err("Can't to compile orc"),
}
}
}
/// Parse and compile an orchestra given on a string,
/// evaluating any global space code (i-time only).
/// # Returns
/// On SUCCESS it returns a value passed to the
/// 'return' opcode in global space.
/// code = "i1 = 2 + 2 \n return i1 \n"
/// retval = csound.eval_code(code)
pub fn eval_code(&self, code: &str) -> f64{
let cd = CString::new(code).unwrap();
unsafe{
let retval = csound_sys::csoundEvalCode(self.engine.inner.csound, cd.as_ptr());
retval as f64
}
}
/// TODO Imlement csoundCompileTree functions
/// Senses input events and performs audio output
///
/// perform until: 1. the end of score is reached (positive return value), 2. an error occurs (negative return value),
/// or 3. performance is stopped by calling *stop()* from another thread (zero return value).
/// Note that some csf file, text or score have to be compiled first and then *start()* must be called.
/// In the case of zero return value, *perform()* can be called again to continue the stopped performance.
/// Otherwise, [`Csound::reset`](struct.Csound.html#method.reset) should be called to clean up after the finished or failed performance.
pub fn perform(& self) -> i32{
unsafe {
csound_sys::csoundPerform(self.engine.inner.csound) as i32
}
}
/// Senses input events, and performs one control sample worth ```ksmps * number of channels * size_off::<f64> bytes``` of audio output.
///
/// Note that some csd file, text or score have to be compiled first and then [`Csound::start`](struct.Csound.html#method.start).
/// Enables external software to control the execution of Csound, and to synchronize
/// performance with audio input and output(see: [`Csound::read_spin_buffer`](struct.Csound.html#method.read_spin_buffer), [`Csound::read_spout_buffer`](struct.Csound.html#method.read_spout_buffer))
/// # Returns
/// *false* during performance, and true when performance is finished. If called until it returns *true*, will perform an entire score.
pub fn perform_ksmps(& self) -> bool {
unsafe {
csound_sys::csoundPerformKsmps(self.engine.inner.csound) != 0
}
}
/// Performs Csound, sensing real-time and score events and processing one buffer's worth (-b frames) of interleaved audio.
/// Note that some csf file, text or score have to be compiled first and then [`Csound::start`](struct.Csound.html#method.start),
/// you could call [`Csound::read_output_buffer`](struct.Csound.html#method.start) or
/// [`Csound::write_input_buffer`](struct.Csound.html#method.write_input_buffer) to write/read the csound's I/O buffers content.
/// #Returns
/// *false* during performance or *true* when performance is finished.
pub fn perform_buffer(& self) -> bool {
unsafe {
csound_sys::csoundPerformBuffer(self.engine.inner.csound) != 0
}
}
/*********************************** UDP ****************************************************/
/// Starts the UDP server on a
/// # Arguments
/// * `port` The server port number.
///
/// # Returns
/// *Ok* on success or an error code on failure.
pub fn udp_server_start(&self, port: u32) -> Result<(), Status>{
unsafe{
match Status::from(csound_sys::csoundUDPServerStart(self.engine.inner.csound, port as c_int) as i32){
Status::CS_SUCCESS => Ok(()),
status => Err(status),
}
}
}
/// # Returns
/// The port number on which the server is running, or None if the server is not running.
pub fn udp_server_status(&self) -> Option<u32>{
unsafe{
let status = csound_sys::csoundUDPServerStatus(self.engine.inner.csound);
if status == csound_sys::CSOUND_ERROR{
None
}else{
Some(status as u32)
}
}
}
/// Closes the UDP server
///
/// # Returns
/// *Ok* if the running server was successfully closed, Status code otherwise.
pub fn udp_server_close(&self) -> Result<(), Status>{
unsafe{
match Status::from(csound_sys::csoundUDPServerClose(self.engine.inner.csound) as i32){
Status::CS_SUCCESS => Ok(()),
status => Err(status),
}
}
}
/// Turns on the transmission of console messages
/// # Arguments
/// * `addr` The UDP server destination address.
/// * `port` The UDP server port number.
/// * `mirror` If it is true, the messages will continue to be sent to the usual destination
/// (see [`Csound::message_string_callback`](struct.Csound.html#method.message_string_callback) ) as well as to UDP.
///
/// # Returns
/// *Ok* on success or an Status code if the UDP transmission could not be set up.
pub fn udp_console(&self, addr: &str, port:u32, mirror:bool) -> Result<(), Status>{
unsafe{
let ip = CString::new(addr).map_err(|_e| Status::CS_ERROR)?;
if csound_sys::csoundUDPConsole(self.engine.inner.csound, ip.as_ptr(),
port as c_int, mirror as c_int) == csound_sys::CSOUND_SUCCESS{
Ok(())
}else{
Err(Status::CS_ERROR)
}
}
}
/// Stop transmitting console messages via UDP
pub fn udp_stop_console(&self){
unsafe{
csound_sys::csoundStopUDPConsole(self.engine.inner.csound);
}
}
/* Engine Attributes functions implmentations ********************************************************* */
/// # Returns
/// The number of audio sample frames per second.
pub fn get_sample_rate(& self) -> f64 {
unsafe {
csound_sys::csoundGetSr(self.engine.inner.csound) as f64
}
}
/// # Returns
/// The number of control samples per second.
pub fn get_control_rate(& self) -> f64 {
unsafe {
csound_sys::csoundGetKr(self.engine.inner.csound) as f64
}
}
/// # Returns
/// The number of audio sample frames per control sample.
pub fn get_ksmps(& self) -> u32 {
unsafe {
csound_sys::csoundGetKsmps(self.engine.inner.csound)
}
}
/// # Returns
/// The number of audio output channels. Set through the nchnls header variable in the csd file.
pub fn output_channels(& self) -> u32 {
unsafe {
csound_sys::csoundGetNchnls(self.engine.inner.csound) as u32
}
}
/// # Returns
/// The number of audio input channels.
/// Set through the **nchnls_i** header variable in the csd file.
/// If this variable is not set, the value is taken from nchnls.
pub fn input_channels(& self) -> u32 {
unsafe {
csound_sys::csoundGetNchnlsInput(self.engine.inner.csound) as u32
}
}
/// # Returns
/// The 0dBFS level of the spin/spout buffers.
pub fn get_0dBFS(& self) -> f64 {
unsafe {
csound_sys::csoundGet0dBFS(self.engine.inner.csound) as f64
}
}
/// # Returns
/// The A4 frequency reference
pub fn get_freq(& self) -> f64 {
unsafe {
csound_sys::csoundGetA4(self.engine.inner.csound) as f64
}
}
/// #Returns
/// The current performance time in samples
pub fn get_current_sample_time(& self) -> usize {
unsafe {
csound_sys::csoundGetCurrentTimeSamples(self.engine.inner.csound) as usize
}
}
/// # Returns
/// The size of MYFLT in bytes.
pub fn get_size_myflt(& self) -> u32 {
unsafe {
csound_sys::csoundGetSizeOfMYFLT() as u32
}
}
/// # Returns
/// Whether Csound is set to print debug messages.
///
/// sents through the *DebugMsg()* csouns's internal API function.
/// Anything different to 0 means true.
pub fn get_debug_level(& self) -> u32 {
unsafe {
csound_sys::csoundGetDebug(self.engine.inner.csound) as u32
}
}
/// Sets whether Csound prints debug messages from the *DebugMsg()* csouns's internal API function.
///
/// # Arguments
/// * `level` The debug level to assign, anything different to 0 means true.
///
pub fn set_debug_level(&self, level: i32){
unsafe {
csound_sys::csoundSetDebug(self.engine.inner.csound, level as c_int);
}
}
/* Engine general InputOutput functions implmentations ********************************************************* */
/// Gets input source name
pub fn get_input_name(&self) -> Result<String, &'static str>{
unsafe {
let ptr = csound_sys::csoundGetInputName(self.engine.inner.csound);
if !ptr.is_null(){
let name = CStr::from_ptr(ptr).to_str().map_err(|_| "Some Utf8 error have occurred while parsing the device name")?;
Ok(name.to_owned())
}else{
Err("Real time audio input is not configured in csound, you have to add the -iadc option into you csd file")
}
}
}
/// Gets output device name
pub fn get_output_name(&self) -> Result<String, &'static str> {
unsafe {
let ptr = csound_sys::csoundGetOutputName(self.engine.inner.csound);
if !ptr.is_null(){
let name = CStr::from_ptr(ptr).to_str().map_err(|_| "Some Utf8 error have occurred while parsing the device name")?;
Ok(name.to_owned())
}else{
Err("Real time audio output is not configured in csound, you have to add the -odac option into you csd file")
}
}
}
/// Set output destination, type and format
///
/// # Arguments
/// * `name` The destination/device name, for RT audio use the field [`CS_AudioDevice::device_id`](struct.CS_AudioDevice.html#field.device_id).
/// (see: [`Csound::get_audio_devices`](struct.Csound.html#method.get_audio_devices))
/// * `out_type` can be one of "wav","aiff", "au","raw", "paf", "svx", "nist", "voc", "ircam","w64","mat4", "mat5", "pvf","xi", "htk","sds","avr",
/// "wavex","sd2", "flac", "caf","wve","ogg","mpc2k","rf64", or NULL (use default or realtime IO).
/// * `format` can be one of "alaw", "schar", "uchar", "float", "double", "long", "short", "ulaw", "24bit", "vorbis", or NULL (use default or realtime IO).
pub fn set_output(& self, name: &str, out_type: &str, format: &str) -> Result<(), NulError> {
unsafe{
let devName = CString::new(name)?;
let devType = CString::new(out_type)?;
let devFormat = CString::new(format)?;
csound_sys::csoundSetOutput(self.engine.inner.csound, devName.as_ptr(),
devType.as_ptr(), devFormat.as_ptr() );
Ok(())
}
}
/// Get output type and format.
/// # Example
///
/// ```
///
/// let csound = Csound::new();
/// let result = csound.get_output_format();
/// let (output_type, output_format) = (result.0.unwrap(), result.1.unwrap());
///
/// ```
pub fn get_output_format(& self) -> Result<(String, String), Utf8Error>{
let otype = vec!['\0' as u8; OUTPUT_TYPE_LENGTH];
let format = vec!['\0' as u8; OUTPUT_FORMAT_LENGTH];
unsafe{
let otype = CString::from_vec_unchecked(otype).into_raw();
let format = CString::from_vec_unchecked(format).into_raw();
csound_sys::csoundGetOutputFormat(self.engine.inner.csound, otype, format);
let otype = CString::from_raw(otype);
let otype = otype.to_str()?;
let format = CString::from_raw(format);
let format = format.to_str()?;
Ok( (otype.to_string(), format.to_string()) )
}
}
/// Sets input source
/// # Arguments
/// * `name` The source device name.
pub fn set_input(& self, name: &str) -> Result<(), NulError> {
unsafe{
let devName = CString::new(name)?;
csound_sys::csoundSetInput(self.engine.inner.csound, devName.as_ptr() );
Ok(())
}
}
/// Set MIDI file input name
pub fn set_midi_file_input(& self, name: &str) -> Result<(), NulError>{
unsafe {
let devName = CString::new(name)?;
csound_sys::csoundSetMIDIFileInput(self.engine.inner.csound, devName.as_ptr());
Ok(())
}
}
/// Set MIDI file output name
pub fn set_midi_file_output(& self, name: &str) -> Result<(), NulError>{
unsafe {
let devName = CString::new(name)?;
csound_sys::csoundSetMIDIFileOutput(self.engine.inner.csound, devName.as_ptr());
Ok(())
}
}
/// Set MIDI input device name/number
pub fn set_midi_input(& self, name: &str) -> Result<(), NulError>{
unsafe {
let devName = CString::new(name)?;
csound_sys::csoundSetMIDIInput(self.engine.inner.csound, devName.as_ptr());
Ok(())
}
}
/// Set MIDI output device name
pub fn set_midi_output(& self, name: &str) -> Result<(), NulError>{
unsafe {
let devName = CString::new(name)?;
csound_sys::csoundSetMIDIOutput(self.engine.inner.csound, devName.as_ptr());
Ok(())
}
}
/* Engine general Realtime Audio I/O functions implmentations ********************************************************* */
/// Sets the current RT audio module
pub fn set_rt_audio_module(& self, name: &str) -> Result<(), NulError>{
unsafe {
let devName = CString::new(name)?;
csound_sys::csoundSetRTAudioModule(self.engine.inner.csound, devName.as_ptr());
Ok(())
}
}
/// # Returns
/// The number of samples in Csound's input buffer.
pub fn get_input_buffer_size(& self) -> usize{
unsafe {
csound_sys::csoundGetInputBufferSize(self.engine.inner.csound) as usize
}
}
/// # Returns
/// The number of samples in Csound's input buffer.
pub fn get_output_buffer_size(& self) -> usize{
unsafe {
csound_sys::csoundGetOutputBufferSize(self.engine.inner.csound) as usize
}
}
/// Method used when you want to copy audio samples from the csound's output buffer.
/// # Arguments
/// * `out` a reference to a mutable slice where the Csound's output buffer content
/// will be copied. This buffer have to has enough memory for at least
/// [`Csound::get_output_buffer_size`](struct.Csound.html#method.get_output_buffer_size), samples.
///
/// # Returns
/// The number of samples copied into the slice on success, or an
/// error message if the internal csound's buffer has not been initialized.
/// # Example
///
/// ```
///
/// let csound = Csound::new();
/// csound.compile_csd("some_file_path");
/// csound.start();
/// let output_buffer_length = csound.get_output_buffer_size();
/// let mut output_buffer = vec![0f64; output_buffer_length];
/// while !csound.perform_buffer() {
/// csound.read_output_buffer(&mut output_buffer).unwrap();
/// // ... do some stuff with the buffer
/// }
/// ```
pub fn read_output_buffer(& self, output: &mut[f64] ) -> Result<usize, &'static str>{
let size = self.get_output_buffer_size();
let obuffer = unsafe {
csound_sys::csoundGetOutputBuffer(self.engine.inner.csound) as *const f64
};
let mut len = output.len();
if size < len{
len = size;
}
if !obuffer.is_null() {
unsafe {
std::ptr::copy(obuffer, output.as_ptr() as *mut f64, len);
return Ok(len);
}
}
Err("The output buffer is not initialized, call the 'compile()' and 'start()' methods.")
}
/// Method used when you want to copy custom audio samples into the csound buffer to be processed.
/// # Arguments
/// * `input` a reference to a slice with samples which will be copied to
/// the Csound's input buffer.
///
/// # Returns
/// The number of samples copied into the csound's input buffer or an
/// error message if the internal csound's buffer has not been initialized.
/// # Example
///
/// ```
///
/// let csound = Csound::new();
/// csound.compile_csd("some_file_path");
/// csound.start();
/// let input_buffer_length = csound.get_input_buffer_size();
/// let mut input_buffer = vec![0f64; output_buffer_length];
/// while !csound.perform_buffer() {
/// // fills your buffer with audio samples you want to pass into csound
/// foo_fill_buffer(&mut input_buffer);
/// csound.write_input_buffer(&input_buffer);
/// // ...
/// }
/// ```
///
pub fn write_input_buffer(& self, input: &[f64] ) -> Result<usize, &'static str> {
let size = self.get_input_buffer_size();
let ibuffer = unsafe {
csound_sys::csoundGetInputBuffer(self.engine.inner.csound) as *mut f64
};
let mut len = input.len();
if size < len{
len = size;
}
if !ibuffer.is_null() {
unsafe {
std::ptr::copy(input.as_ptr(), ibuffer, len);
return Ok(len);
}
}
Err("The input buffer is not initialized, call the 'compile()' and 'start()' methods.")
}
/// Enables external software to read audio from Csound after calling csoundPerformKsmps. [`Csound::perform_ksmps`](struct.Csound.html#method.perform_ksmps)
///
///# Arguments
///
///* `out` a reference to a slice, this method will copy **out.len()** samples from the spout buffer so, the out length shouldn be at least
///[`Csound::get_ksmps`](struct.Csound.html#method.get_ksmps) * [`Csound::input_channels`](struct.Csound.html#method.input_channels) samples.
///
/// # Returns
/// The number of samples copied or an
/// error message if the internal csound's buffer has not been initialized.
/// # Example
///
/// ```
/// let csound = Csound::new();
/// csound.compile_csd("some_file_path");
/// csound.start();
/// let spout_length = csound.get_ksmps() * csound.output_channels();
/// let mut spout_buffer = vec![0f64; spout_length as usize];
/// while !csound.perform_ksmps() {
/// // fills your buffer with audio samples you want to pass into csound
/// foo_fill_buffer(&mut spout_buffer);
/// csound.read_spout_buffer(&spout_buffer);
/// // ...
/// }
/// ```
///
pub fn read_spout_buffer(& self, output: &mut [f64] ) -> Result<usize, &'static str> {
let size = self.get_ksmps() as usize * self.output_channels() as usize;
let spout = unsafe {
csound_sys::csoundGetSpout(self.engine.inner.csound) as *const f64
};
let mut len = output.len();
if size < len{
len = size;
}
if !spout.is_null() {
unsafe {
std::ptr::copy(spout, output.as_mut_ptr(), len);
return Ok(len);
}
}
Err("The spout buffer is not initialized, call the 'compile()' and 'start()' methods.")
}
/// Enables external software to write audio into Csound before calling [`Csound::perform_ksmps`](struct.Csound.html#method.perform_ksmps)
///
///# Arguments
///
///* `input` a slice whose length would be at least
/// [`Csound::get_ksmps`](struct.Csound.html#method.get_ksmps) * [`Csound::input_channels`](struct.Csound.html#method.input_channels).
/// # Returns
/// The number of samples copied or an
/// error message if the internal csound's buffer has not been initialized.
///
/// # Example
///
/// ```
/// let csound = Csound::new();
/// csound.compile_csd("some_file_path");
/// csound.start();
/// let spin_length = csound.get_ksmps() * csound.input_channels();
/// let mut spin_buffer = vec![0f64; spin_length as usize];
/// while !csound.perform_ksmps() {
/// // fills your buffer with audio samples you want to pass into csound
/// foo_fill_buffer(&mut spin_buffer);
/// csound.write_spin_buffer(&spin_buffer);
/// // ...
/// }
/// ```
///
pub fn write_spin_buffer(& self, input: &[f64] ) -> Result<usize, &'static str> {
let size = self.get_ksmps() as usize * self.input_channels() as usize;
let spin = unsafe {
csound_sys::csoundGetSpin(self.engine.inner.csound) as *mut f64
};
let mut len = input.len();
if size < len{
len = size;
}
if !spin.is_null() {
unsafe {
std::ptr::copy(input.as_ptr(), spin, len);
return Ok(len);
}
}
Err("The spin buffer is not initialized, call the 'compile()' and 'start()' methods.")
}
/// Clears the spin buffer.
pub fn clear_spin(& self){
unsafe{
csound_sys::csoundClearSpin(self.engine.inner.csound);
}
}
/// Adds the indicated sample into the audio input working buffer (spin);
/// this only ever makes sense before calling [`Csound::perform_ksmps`](struct.Csound.html#method.perform_ksmps).
/// The frame and channel must be in bounds relative to ksmps and nchnls.
/// *Note*: the spin buffer needs to be cleared at every k-cycle by calling [`Csound::clear_spin`](struct.Csound.html#method.clear_spin).
pub fn add_spin_sample(& self, frame:u32, channel:u32, sample: f64){
unsafe{
csound_sys::csoundAddSpinSample(self.engine.inner.csound, frame as i32, channel as i32, sample as c_double);
}
}
/// Sets the audio input working buffer (spin) to the indicated sample.
///
/// this only ever makes sense before calling [`Csound::perform_ksmps`](struct.Csound.html#method.perform_ksmps).
/// The frame and channel must be in bounds relative to ksmps and nchnls.
pub fn set_spin_sample(& self, frame:u32, channel:u32, sample: f64){
unsafe{
csound_sys::csoundSetSpinSample(self.engine.inner.csound, frame as i32, channel as i32, sample as c_double);
}
}
/// Gets an audio sample from the spout buffer.
///
/// only ever makes sense before calling [`Csound::perform_ksmps`](struct.Csound.html#method.perform_ksmps).
/// The frame and channel must be in bounds relative to ksmps and nchnls.
/// #Returns
/// The indicated sample from the Csound audio output working buffer (spout).
///
pub fn get_spout_sample(& self, frame: u32, channel: u32) -> f64 {
unsafe{
csound_sys::csoundGetSpoutSample(self.engine.inner.csound, frame as i32, channel as i32) as f64
}
}
/// Enable to host to handle the audio implementation.
///
/// Calling this function with a non-zero 'state' value between [`Csound::create`](struct.Csound.html#method.create) and the start of performance will disable
/// all default handling of sound I/O by the Csound library,
/// allowing the host application to use the *spin*,*spout*,*input*, *output* buffers directly.
/// # Arguments
///
/// * `state` An no zero value will diseable all default handling of sound I/O in csound.
///
/// * `bufSize` For applications using *spin* / *spout*, this argument should be set to 0 but if *bufSize* is greater than zero, the buffer size (-b) in frames will be set to the integer
/// multiple of ksmps that is nearest to the value specified.
pub fn set_host_implemented_audioIO(& self, state:u32, bufSize: u32){
unsafe{
csound_sys::csoundSetHostImplementedAudioIO(self.engine.inner.csound, state as c_int, bufSize as c_int);
}
}
/// This function can be called to obtain a list of available input and output audio devices.
///
/// # Returns
/// A tuple, being input devices first element in the returned tuple, and output devices the
/// second one.
pub fn get_audio_devices(& self) -> (Vec<CS_AudioDevice>, Vec<CS_AudioDevice>){
let mut input_devices = Vec::new();
let mut output_devices = Vec::new();
unsafe {
let num_of_idevices = csound_sys::csoundGetAudioDevList(self.engine.inner.csound, 0 as *mut _, 0);
let num_of_odevices = csound_sys::csoundGetAudioDevList(self.engine.inner.csound, 0 as *mut _, 0);
let mut in_vec = vec![csound_sys::CS_AUDIODEVICE::default(); num_of_idevices as usize];
let mut out_vec = vec![csound_sys::CS_AUDIODEVICE::default(); num_of_odevices as usize];
csound_sys::csoundGetAudioDevList(self.engine.inner.csound, in_vec.as_mut_ptr(), 0);
csound_sys::csoundGetAudioDevList(self.engine.inner.csound, out_vec.as_mut_ptr(), 1);
for dev in &in_vec{
let name = (CStr::from_ptr(dev.device_name.as_ptr())).to_owned();
let id = (CStr::from_ptr(dev.device_id.as_ptr())).to_owned();
let module = (CStr::from_ptr(dev.rt_module.as_ptr())).to_owned();
input_devices.push( CS_AudioDevice {
device_name: name.into_string().unwrap(),
device_id: id.into_string().unwrap(),
rt_module: module.into_string().unwrap(),
max_nchnls: dev.max_nchnls as u32,
isOutput: 0,
});
}
for dev in &out_vec{
let name = (CStr::from_ptr(dev.device_name.as_ptr())).to_owned();
let id = (CStr::from_ptr(dev.device_id.as_ptr())).to_owned();
let module = (CStr::from_ptr(dev.rt_module.as_ptr())).to_owned();
output_devices.push( CS_AudioDevice{
device_name: name.into_string().unwrap(),
device_id: id.into_string().unwrap(),
rt_module: module.into_string().unwrap(),
max_nchnls: dev.max_nchnls as u32,
isOutput: 1,
});
}
}
(output_devices, input_devices)
}
/* Real time MIDI IO functions implmentations *************************************************************** */
/// Sets the current MIDI IO module
pub fn set_midi_module(& self, name: &str){
unsafe {
let devName = CString::new(name);
if devName.is_ok() {
csound_sys::csoundSetMIDIModule(self.engine.inner.csound, devName.unwrap().as_ptr());
}
}
}
/// call this function with state 1 if the host is implementing MIDI via the callbacks
pub fn set_host_implemented_midiIO(& self, state:u32){
unsafe{
csound_sys::csoundSetHostImplementedMIDIIO(self.engine.inner.csound, state as c_int);
}
}
/// This function can be called to obtain a list of available input or output midi devices.
///
/// This function will return a tuple with two vectors, beign the first one for input MIDI
/// devices and the second one for output MIDI devices
pub fn get_midi_devices(& self) -> (Vec<CS_MidiDevice>, Vec<CS_MidiDevice>) {
let mut input_devices = Vec::new();
let mut output_devices = Vec::new();
unsafe {
let num_of_idevices = csound_sys::csoundGetMIDIDevList(self.engine.inner.csound, 0 as *mut _, 0);
let num_of_odevices = csound_sys::csoundGetMIDIDevList(self.engine.inner.csound, 0 as *mut _, 0);
let mut in_vec = vec![csound_sys::CS_MIDIDEVICE::default(); num_of_idevices as usize];
let mut out_vec = vec![csound_sys::CS_MIDIDEVICE::default(); num_of_odevices as usize];
csound_sys::csoundGetMIDIDevList(self.engine.inner.csound, in_vec.as_mut_ptr(), 0);
csound_sys::csoundGetMIDIDevList(self.engine.inner.csound, out_vec.as_mut_ptr(), 1);
for dev in &in_vec{
let name = (CStr::from_ptr(dev.device_name.as_ptr())).to_owned();
let id = (CStr::from_ptr(dev.device_id.as_ptr())).to_owned();
let module = (CStr::from_ptr(dev.midi_module.as_ptr())).to_owned();
let interface = (CStr::from_ptr(dev.interface_name.as_ptr())).to_owned();
input_devices.push( CS_MidiDevice {
device_name: name.into_string().unwrap(),
device_id: id.into_string().unwrap(),
midi_module: module.into_string().unwrap(),
interface_name: interface.into_string().unwrap(),
isOutput: 0,
});
}
for dev in &out_vec{
let name = (CStr::from_ptr(dev.device_name.as_ptr())).to_owned();
let id = (CStr::from_ptr(dev.device_id.as_ptr())).to_owned();
let module = (CStr::from_ptr(dev.midi_module.as_ptr())).to_owned();
let interface = (CStr::from_ptr(dev.interface_name.as_ptr())).to_owned();
output_devices.push( CS_MidiDevice {
device_name: name.into_string().unwrap(),
device_id: id.into_string().unwrap(),
midi_module: module.into_string().unwrap(),
interface_name: interface.into_string().unwrap(),
isOutput: 1,
});
}
}
(output_devices, input_devices)
}
/* Score Handling functions implmentations ********************************************************* */
/// Read, preprocess, and load a score from an ASCII string.
///
/// It can be called repeatedly with the new score events being added to the currently scheduled ones.
///
///# Arguments
///* `score` The name of the score file
pub fn read_score(&mut self, score: &str) -> Result<(), &'static str>{
unsafe {
match CString::new(score){
Ok(s) => {
if csound_sys::csoundReadScore(self.engine.inner.csound, s.as_ptr()) == csound_sys::CSOUND_SUCCESS {
Ok(())
}
else {
Err("Can't to read the score")
}
},
_ => Err("Invalid score")
}
}
}
/// Asynchronous version of [`Csound::read_score`](struct.Csound.html#method.read_score)
pub fn read_score_async(&mut self, score: &str) -> Result<(), &'static str>{
unsafe {
match CString::new(score){
Ok(s) => {
csound_sys::csoundReadScoreAsync(self.engine.inner.csound, s.as_ptr());
Ok(())
},
_ => Err("Invalid score")
}
}
}
/// # Returns
/// The current score time in seconds since the beginning of performance.
pub fn get_score_time(& self) -> f64 {
unsafe {
csound_sys::csoundGetScoreTime(self.engine.inner.csound) as f64
}
}
/// Sets whether Csound score events are performed or not.
///
/// Independently of real-time MIDI events (see [`Csound::set_score_pending`](struct.Csound.html#method.set_score_pending)).
pub fn is_score_pending(& self) -> i32{
unsafe {
csound_sys::csoundIsScorePending(self.engine.inner.csound) as i32
}
}
/// Sets whether Csound score events are performed or not (real-time events will continue to be performed).
///
/// Can be used by external software, such as a VST host, to turn off performance of score events (while continuing to perform real-time events),
/// for example to mute a Csound score while working on other tracks of a piece, or to play the Csound instruments live.
pub fn set_score_pending (& self, pending: i32) {
unsafe {
csound_sys::csoundSetScorePending(self.engine.inner.csound, pending as c_int);
}
}
/// Gets the current score's time.
///
/// # Returns
/// The score time beginning at which score events will actually immediately be performed
/// (see [`Csound::set_score_offset_seconds`](struct.Csound.html#method.set_score_offset_seconds)).
pub fn get_score_offset_seconds(& self) -> f64 {
unsafe {
csound_sys::csoundGetScoreOffsetSeconds(self.engine.inner.csound) as f64
}
}
/// Csound score events prior to the specified time are not performed.
///
/// And performance begins immediately at the specified time
/// (real-time events will continue to be performed as they are received).
/// Can be used by external software, such as a VST host, to begin score performance midway through a Csound score,
/// for example to repeat a loop in a sequencer, or to synchronize other events with the Csound score.
pub fn set_score_offset_seconds(& self, offset: f64) {
unsafe {
csound_sys::csoundSetScoreOffsetSeconds(self.engine.inner.csound, offset as c_double);
}
}
/// Rewinds a compiled Csound score to the time specified with [`Csound::set_score_offset_seconds`](struct.Csound.html#method.set_score_offset_seconds)
pub fn rewindScore(& self){
unsafe {
csound_sys::csoundRewindScore(self.engine.inner.csound);
}
}
// TODO SCORE SORT FUNCTIONS
/* Engine general messages functions implmentations ********************************************************* */
/// # Returns
/// The Csound message level (from 0 to 231).
pub fn get_message_level(& self) -> u32 {
unsafe {
csound_sys::csoundGetMessageLevel(self.engine.inner.csound) as u32
}
}
/// Sets the Csound message level (from 0 to 231).
pub fn set_message_level(& self, level: u32) {
unsafe {
csound_sys::csoundSetMessageLevel(self.engine.inner.csound, level as c_int);
}
}
/// Creates a buffer for storing messages printed by Csound. Should be called after creating a Csound instance and the buffer can be freed by
/// calling [`Csound::destroy_message_buffer`](struct.Csound.html#method.destroy_message_buffer), this buffer will be destroyed when the csound instance is dropped.
/// You will generally want to call [`Csound::cleanup`](struct.Csound.html#method.cleanup) to make sure the last messages are flushed to the message buffer before destroying Csound.
/// # Arguments
///
/// * `toStdOut` If is non-zero, the messages are also printed to stdout and stderr (depending on the type of the message), in addition to being stored in the buffer.
///
/// *Note*: Using the message buffer ties up the internal message callback,
/// so [`Csound::message_string_callback`](struct.Csound.html#method.message_string_callback) should not be called after creating the message buffer.
pub fn create_message_buffer(& self, stdout: i32){
unsafe {
csound_sys::csoundCreateMessageBuffer(self.engine.inner.csound, stdout as c_int);
}
}
/// Releases all memory used by the message buffer.
///
/// If this buffer is created, the Drop method
/// will call this function when the Csound instance were dropped.
pub fn destroy_message_buffer( & self ){
unsafe {
csound_sys::csoundDestroyMessageBuffer(self.engine.inner.csound );
}
}
/// # Returns
/// The first message from the buffer.
pub fn get_first_message( & self ) -> Option<String> {
unsafe {
match CStr::from_ptr(csound_sys::csoundGetFirstMessage(self.engine.inner.csound)).to_str(){
Ok(m) => {
Some(m.to_owned())
},
_ => None,
}
}
}
/// # Returns
/// The attribute parameter ([`MessageType`](enum.MessageType.html)) of the first message in the buffer.
pub fn get_first_message_attr( & self) -> MessageType {
unsafe {
MessageType::from_u32(csound_sys::csoundGetFirstMessageAttr(self.engine.inner.csound) as u32)
}
}
/// Removes the first message from the buffer.
pub fn pop_first_message( & self) {
unsafe {
csound_sys::csoundPopFirstMessage( self.engine.inner.csound );
}
}
/// # Returns
/// The number of pending messages in the buffer.
pub fn get_message_count(& self) -> u32 {
unsafe {
csound_sys::csoundGetMessageCnt(self.engine.inner.csound) as u32
}
}
/* Engine general Channels, Control and Events implementations ********************************************** */
/// Requests a list of all control channels.
/// # Returns
/// A vector with all control channels info or None if there are not control channels. see: ([`ChannelInfo`](struct.ChannelInfo.html))
pub fn list_channels( & self) -> Option< Vec<ChannelInfo> > {
let mut ptr = ::std::ptr::null_mut() as *mut csound_sys::controlChannelInfo_t;
let ptr2: *mut *mut csound_sys::controlChannelInfo_t = &mut ptr as *mut *mut _;
unsafe {
let count = csound_sys::csoundListChannels(self.engine.inner.csound, ptr2) as i32;
let mut ptr = *ptr2;
if count > 0 {
let mut list = Vec::new();
for _ in 0..count{
let name = (CStr::from_ptr((*ptr).name).to_str().unwrap()).to_owned();
let ctype = (*ptr).type_ as i32;
let hints = (*ptr).hints;
let mut attributes = String::new();
if !(hints.attributes).is_null(){
attributes = (CStr::from_ptr(hints.attributes).to_str().unwrap()).to_owned();
}
list.push(ChannelInfo{
name : name,
type_ : ctype,
hints: ChannelHints {
behav: ChannelBehavior::from_u32(hints.behav as u32),
dflt: hints.dflt as f64,
min: hints.min as f64,
max: hints.max as f64,
x: hints.x as i32,
y: hints.y as i32,
width: hints.width as i32,
height: hints.height as i32,
attributes: attributes,
}
});
ptr = ptr.add(1);
}
csound_sys::csoundDeleteChannelList(self.engine.inner.csound, *ptr2);
Some(list)
}else{
None
}
}
}
/// Return a [`ControlChannelPtr`](struct.ControlChannelPtr.html) which represent a csound's channel ptr.
/// creating the channel first if it does not exist yet.
/// # Arguments
///
/// * `name` The channel name.
/// * `channel_type` must be the bitwise OR of exactly one of the following values:
/// - CSOUND_CONTROL_CHANNEL
/// control data (one MYFLT value)
/// - CSOUND_AUDIO_CHANNEL
/// audio data (get_ksmps() f64 values)
/// - CSOUND_STRING_CHANNEL
/// string data (f64 values with enough space to store
/// get_channel_data_size() characters, including the
/// NULL character at the end of the string)
/// and at least one of these:
/// - CSOUND_INPUT_CHANNEL
/// - CSOUND_OUTPUT_CHANNEL
///
/// If the channel already exists, it must match the data type
/// (control, audio, or string), however, the input/output bits are
/// OR'd with the new value. Note that audio and string channels
/// can only be created after calling Compile(), because the
/// storage size is not known until then.
///
/// # Returns
/// The ControlChannelPtr on success or a Status code,
/// "Not enough memory for allocating the channel" (CS_MEMORY)
/// "The specified name or type is invalid" (CS_ERROR)
/// or, if a channel with the same name but incompatible type
/// already exists, the type of the existing channel.
///
/// * Note:* to find out the type of a channel without actually
/// creating or changing it, set 'channel_type' argument to CSOUND_UNKNOWN_CHANNEL, so that the error
/// value will be either the type of the channel, or CSOUND_ERROR
/// if it does not exist.
///
/// Operations on the channel pointer are not thread-safe by default. The host is
/// required to take care of threadsafety by
/// 1) with control channels use __sync_fetch_and_add() or
/// __sync_fetch_and_or() gcc atomic builtins to get or set a channel,
/// if available.
/// 2) For string and audio channels (and controls if option 1 is not
/// available), retrieve the channel lock with ChannelLock()
/// and use SpinLock() and SpinUnLock() to protect access
/// to the channel.
/// See Top/threadsafe.c in the Csound library sources for
/// examples. Optionally, use the channel get/set functions
/// which are threadsafe by default.
pub fn get_channel_ptr<'a>(&'a self, name: &str, channel_type: ControlChannelType) -> Result<ControlChannelPtr<'a>, Status >{
let cname = CString::new(name).map_err(|_| Status::CS_ERROR)?;
let mut ptr = ::std::ptr::null_mut() as *mut f64;
let ptr = &mut ptr as *mut *mut _;
let channel = ControlChannelType::from_bits(channel_type.bits() & ControlChannelType::CSOUND_CHANNEL_TYPE_MASK.bits()).unwrap();
let len:usize = match channel{
ControlChannelType::CSOUND_CONTROL_CHANNEL => {
std::mem::size_of::<f64>()
},
ControlChannelType::CSOUND_AUDIO_CHANNEL =>{
self.get_ksmps() as usize
},
ControlChannelType::CSOUND_STRING_CHANNEL => {
self.get_channel_data_size(name)/std::mem::size_of::<f64>()
},
_ => return Err(Status::CS_ERROR),
};
unsafe{
let result = Status::from(csound_sys::csoundGetChannelPtr(self.engine.inner.csound, ptr, cname.as_ptr(),
channel_type.bits() as c_int));
match result{
Status::CS_SUCCESS => {
Ok(ControlChannelPtr{
ptr: *ptr,
channel_type: channel,
len: len,
phantom: PhantomData,
})
},
Status::CS_OK(channel) => Err(Status::CS_OK(channel)),
result => Err(result),
}
}
}
/// Set parameters hints for a control channel.
///
/// These hints have no internal function but can be used by front ends to construct GUIs or to constrain values.
/// # Returns
/// CS_SUCCESS on success, or CS_ERROR on failure: the channel does not exist, is not a control channel,
/// or the specified parameters are invalid or CS_MEMORY: could not allocate memory for the
/// channel. see: ([`Status`](enum.Status.html))
pub fn set_channel_hints( & self, name: &str, hint: &ChannelHints) -> Result<(), Status> {
let attr = &hint.attributes[..];
let attr = CString::new(attr).map_err(|_| Status::CS_ERROR)?;
let cname = CString::new(name).map_err(|_| Status::CS_ERROR)?;
let channel_hint = csound_sys::controlChannelHints_t {
behav: ChannelBehavior::to_u32(&hint.behav),
dflt: hint.dflt,
min: hint.min,
max: hint.max,
x: hint.x,
y: hint.y,
width: hint.width as c_int,
height: hint.height as c_int,
attributes: attr.as_ptr() as *mut c_char,
};
unsafe {
match Status::from(csound_sys::csoundSetControlChannelHints(self.engine.inner.csound, cname.as_ptr(),
channel_hint) as i32){
Status::CS_SUCCESS => Ok(()),
status => Err(status),
}
}
}
/// Returns special parameters (or None if there are not any) of a control channel.
///
/// Previously set with csoundSetControlChannelHints() or the
/// [chnparams](http://www.csounds.com/manualOLPC/chnparams.html) opcode.
pub fn get_channel_hints(& self, name: &str) -> Result<ChannelHints, Status> {
let cname = CString::new(name).map_err(|_| Status::CS_ERROR)?;
let hint = Box::new(csound_sys::controlChannelHints_t::default());
unsafe {
let hint = Box::into_raw(hint);
match csound_sys::csoundGetControlChannelHints( self.engine.inner.csound,
cname.as_ptr() as *mut c_char, hint) {
csound_sys::CSOUND_SUCCESS =>{
let hint = Box::from_raw(hint);
let mut attr = String::new();
if !(*hint).attributes.is_null(){
attr = (CStr::from_ptr(hint.attributes).to_str().unwrap()).to_owned();
}
let hints = ChannelHints {
behav: ChannelBehavior::from_u32(hint.behav as u32),
dflt: hint.dflt,
min: hint.min,
max: hint.max,
x: hint.x as i32,
y: hint.y as i32,
width: hint.width as i32,
height: hint.height as i32,
attributes: attr
};
Ok(hints)
},
status => Err(Status::from(status)),
}
}
}
/// Retrieves the value of control channel.
/// # Arguments
///
/// * `name` The channel name.
///
/// An error message will be returned if the channel is not a control channel,
/// the channel not exist or if the name is invalid.
pub fn get_control_channel(& self, name: &str) -> Result<f64, &'static str> {
let cname = CString::new(name).map_err(|_| "invalid channel name")?;
let err = Box::new(csound_sys::CSOUND_ERROR);
unsafe {
let err = Box::into_raw(err);
let ret = csound_sys::csoundGetControlChannel(self.engine.inner.csound, cname.as_ptr(), err) as f64;
if (*err) == csound_sys::CSOUND_SUCCESS {
Ok(ret)
} else {
Err("channel not exist or is not a control channel")
}
}
}
/// Sets the value of control channel.
/// # Arguments
///
/// * `name` The channel name.
pub fn set_control_channel(& self, name: &str, value: f64) {
let cname = CString::new(name).unwrap();
unsafe {
csound_sys::csoundSetControlChannel(self.engine.inner.csound, cname.as_ptr(), value);
}
}
/// Copies samples from an audio channel.
/// # Arguments
/// * `name` The channel name.
/// * `out` The slice where the date contained in the internal audio channel buffer
/// will be copied. Should contain enough memory for ksmps f64 samples.
/// # Panic
/// If the buffer passed to this function has not enough memory.
///
pub fn read_audio_channel(& self, name: &str,output: &mut [f64]) {
let size = self.get_ksmps() as usize;
let bytes = output.len();
let cname = CString::new(name).unwrap();
assert!(size <= bytes, "The audio channel's capacity is {} so, it isn't possible to copy {} samples", size, bytes);
unsafe {
csound_sys::csoundGetAudioChannel(self.engine.inner.csound, cname.as_ptr(), output.as_ptr() as *mut c_double );
}
}
/// Writes data into an audio channel buffer. audio channel identified by *name* with data from slice *input* which should
/// contain at least ksmps f64 samples, if not, this method will panic.
/// # Arguments
/// * `input` The slice with data to be copied into the audio channel buffer. Could contain up to ksmps samples.
/// # panic
/// This method will panic if input.len() > ksmps.
///
pub fn write_audio_channel(& self, name: &str,input: &[f64]) {
let size = self.get_ksmps() as usize * self.input_channels() as usize;
let bytes = input.len();
let cname = CString::new(name).unwrap();
assert!(size <= bytes, "The audio channel's capacity is {} so, it isn't possible to copy {} bytes", size, bytes);
unsafe {
csound_sys::csoundSetAudioChannel(self.engine.inner.csound, cname.as_ptr(), input.as_ptr() as *mut c_double );
}
}
/// Returns the content of the string channel identified by *name*
pub fn get_string_channel(& self, name: &str) -> String {
let cname = CString::new(name).unwrap();
let mut data = String::with_capacity(self.get_channel_data_size(name));
unsafe {
let ptr = data.as_mut_vec();
csound_sys::csoundGetStringChannel(self.engine.inner.csound, cname.as_ptr(), ptr.as_ptr() as *mut _);
}
data
}
/// Sets the string channel identified by *name* with *content*
pub fn set_string_channel(& self, name: &str, content: &str) {
let cname = CString::new(name).unwrap();
let content = CString::new(content).unwrap();
unsafe {
csound_sys::csoundSetStringChannel(self.engine.inner.csound, cname.as_ptr(), content.as_ptr() as *mut _);
}
}
/// returns the size of data stored in the channel identified by *name*
pub fn get_channel_data_size(&self, name: &str) -> usize{
let cname = CString::new(name).unwrap();
unsafe {
csound_sys::csoundGetChannelDatasize(self.engine.inner.csound, cname.as_ptr()) as usize
}
}
/// Receives a PVSDAT fout from the [*pvsout*](http://www.csounds.com/manual/html/pvsout.html) opcode.
///
/// This method will return Ok on success,
/// [`Status::CS_ERROR`](enum.Status.html#member.CS_ERROR) if the channel name is not valid or the channel doesn't
/// exist or [`Status::CS_MEMORY`](enum.Status.html#member.CS_MEMORY) if the frame buffer lengths haven't the same size
/// as the requested table
///
/// # Arguments
/// * `name` The channel identifier.
/// * `pvs_data` Reference to tha struct which will be filled with the pvs data.
///
/// # Example
/// ```
/// let mut pvs = pvs_DataExt::new(512);
/// cs.get_pvs_channel("1", &mut pvs);
/// ```
///
pub fn get_pvs_channel(&self, name:&str, pvs_data: &mut pvs_DataExt) -> Result<(), Status> {
let cname = CString::new(name).map_err(|_| Status::CS_ERROR)?;
let mut ptr = ::std::ptr::null_mut() as *mut f64;
unsafe{
if csound_sys::csoundGetChannelPtr(self.engine.inner.csound, &mut ptr as *mut *mut _, cname.as_ptr(),
(csound_sys::CSOUND_PVS_CHANNEL | csound_sys::CSOUND_INPUT_CHANNEL) as c_int) == csound_sys::CSOUND_SUCCESS{
if(*(ptr as *mut csound_sys::PVSDATEXT)).N == pvs_data.N as c_int{
let data = &mut csound_sys::PVSDATEXT::default();
data.frame = pvs_data.frame.as_mut_slice().as_ptr() as *mut f32;
let result = csound_sys::csoundGetPvsChannel(self.engine.inner.csound, &mut *data, cname.as_ptr());
match result {
csound_sys::CSOUND_SUCCESS =>{
pvs_data.N = data.N as u32;
pvs_data.sliding = data.sliding as u32;
pvs_data.NB = data.NB as i32;
pvs_data.overlap = data.overlap as u32;
pvs_data.winsize = data.winsize as u32;
pvs_data.wintype = data.wintype as u32;
pvs_data.format = data.format as u32;
pvs_data.framecount = data.framecount as u32;
Ok(())
},
err => Err(Status::from(err)),
}
}else{
Err(Status::CS_MEMORY)
}
}else{
Err(Status::CS_ERROR)
}
}
}
pub fn set_pvs_channel(&self, name:&str, pvs_data: &pvs_DataExt){
unsafe{
let cname = CString::new(name);
if cname.is_ok(){
let data = &mut csound_sys::PVSDATEXT{
N: pvs_data.N as _,
sliding: pvs_data.sliding as _,
NB: pvs_data.NB as _,
overlap: pvs_data.overlap as _,
winsize: pvs_data.winsize as _,
wintype: pvs_data.wintype as _,
format: pvs_data.format as _,
framecount: pvs_data.framecount as _,
frame: pvs_data.frame.as_slice().as_ptr() as *mut f32,
};
csound_sys::csoundSetPvsChannel(self.engine.inner.csound, &mut *data, cname.unwrap().as_ptr());
}
}
}
/// Send a new score event.
/// # Arguments
///
/// * `event_type` is the score event type ('a', 'i', 'q', 'f', or 'e').
/// * `pfields` is a slice of f64 values with all the pfields for this event.
///
/// # Example
/// ```
/// let cs = Csound::new();
///
/// let pFields = [1.0, 1.0, 5.0];
/// while cs.perform_ksmps() == false {
/// cs.send_score_event('i', &pFields);
/// }
/// ```
///
pub fn send_score_event(& self, event_type: char, pfields: &[f64]) -> Status {
unsafe {
Status::from(csound_sys::csoundScoreEvent(self.engine.inner.csound, event_type as c_char, pfields.as_ptr()
as *const c_double, pfields.len() as i64) as i32)
}
}
/// Like [`Csound::send_score_event`](struct.Csound.html#method.send_score_event).
///
/// This function inserts a score event,
/// but at absolute time with respect to the start of performance,
/// or from an offset set with *time_offset*
pub fn send_score_event_absolute(& self, event_type: char, pfields: &[f64], time_offset: f64) -> Status {
unsafe {
Status::from(csound_sys::csoundScoreEventAbsolute(self.engine.inner.csound, event_type as c_char, pfields.as_ptr()
as *const c_double, pfields.len() as i64, time_offset as c_double) as i32)
}
}
/// Asynchronous version of [`Csound::send_score_event`](struct.Csound.html#method.send_score_event)
pub fn send_score_event_async(& self, event_type: char, pfields: &[f64]) -> Status {
unsafe {
Status::from(csound_sys::csoundScoreEventAsync(self.engine.inner.csound, event_type as c_char, pfields.as_ptr()
as *const c_double, pfields.len() as i64) as i32)
}
}
/// Asynchronous version of [`Csound::send_score_event_absolute`](struct.Csound.html#method.send_score_event_absolute)
pub fn send_score_event_absolute_async(& self, event_type: char, pfields: &[f64], time_offset: f64) -> Status {
unsafe {
Status::from(csound_sys::csoundScoreEventAbsoluteAsync(self.engine.inner.csound, event_type as c_char, pfields.as_ptr()
as *const c_double, pfields.len() as i64, time_offset as c_double) as i32)
}
}
/// Input a string (as if from a console), used for line events.
/// # Example
/// ```
/// let cs = Csound::new();
///
/// let pFields = [1.0, 1.0, 5.0];
/// while cs.perform_ksmps() == false {
/// cs.send_input_message("i 2 0 0.75 1");
/// }
/// ```
pub fn send_input_message(& self, message: &str) -> Result<(), NulError>{
let cmessage = CString::new(message)?;
unsafe {
csound_sys::csoundInputMessage(self.engine.inner.csound, cmessage.as_ptr() as *const c_char);
Ok(())
}
}
/// Asynchronous version of [`Csound::send_input_message`](struct.Csound.html#method.send_input_message)
pub fn send_input_message_async(& self, message: &str) -> Result<(), NulError>{
let cmessage = CString::new(message)?;
unsafe {
csound_sys::csoundInputMessageAsync(self.engine.inner.csound, cmessage.as_ptr() as *const c_char);
Ok(())
}
}
/// Kills off one or more running instances of an instrument.
///
/// # Arguments
///
/// * `instr` The numeric identifier of the instrument.
/// * `name` The string identifier of the instrument or name. If it is None, the instrument
/// numeric identifier is used.
/// * `mode` is a sum of the following values: 0,1,2: kill all instances (1), oldest only (1), or newest (2)
/// 4: only turnoff notes with exactly matching (fractional) instr number
/// 8: only turnoff notes with indefinite duration (p3 < 0 or MIDI).
/// * `allow_release` if true, the killed instances are allowed to release.
///
pub fn kill_instrument(& self, instr: f64, name: Option<&str>, mode:u32, allow_release: bool) -> Status {
let cname = CString::new(name.unwrap_or_else(|| "")).unwrap();
unsafe {
Status::from(csound_sys::csoundKillInstance(self.engine.inner.csound, instr as c_double, cname.as_ptr()
as *const c_char, mode as c_int, allow_release as c_int) as i32)
}
}
/// Set the ASCII code of the most recent key pressed.
/// # Arguments
///
/// * `key` The ASCII identifier for the key pressed.
pub fn key_press(& self, key: char){
unsafe {
csound_sys::csoundKeyPress(self.engine.inner.csound, key as c_char);
}
}
/* Engine general Table function implementations **************************************************************************************** */
/// Returns the length of a function table (not including the guard point), or None if the table does not exist.
/// # Arguments
/// * `table` The function table identifier.
pub fn table_length(& self, table: u32) -> Result< usize, &'static str > {
unsafe {
let value = csound_sys::csoundTableLength(self.engine.inner.csound, table as c_int) as i32;
if value > 0{
Ok(value as usize)
}else{
Err("Table doesn't exist")
}
}
}
/// Returns the value of a slot in a function table.
///
/// If the Table or index are not valid, None will be returned.
///
/// # Arguments
/// * `table` The function table identifier.
/// * `index` The value at table[index] which will be read.
pub fn table_get(& self, table: u32, index:u32) -> Result< f64, &'static str > {
unsafe {
let size = self.table_length(table)?;
if index < size as u32 {
Ok(csound_sys::csoundTableGet(self.engine.inner.csound, table as c_int, index as c_int) as f64)
}else{
Err("index out of range")
}
}
}
/// Sets the value of a slot in a function table.
/// # Arguments
/// * `table` The function table identifier.
/// * `index` The slot at table[index] where value will be added.
pub fn table_set(& self, table: u32, index:u32, value: f64) -> Result< (), &'static str >{
unsafe {
let size = self.table_length(table)?;
if index < size as u32 {
csound_sys::csoundTableSet(self.engine.inner.csound, table as c_int, index as c_int, value);
Ok(())
}
else{
Err("index out of range")
}
}
}
/// Returns the contents of a function table if it exist.
///
/// # Arguments
/// * `table` The function table identifier.
///
pub fn table_copy_out(& self, table: u32, output: &mut[f64]) -> Result< (), &'static str > {
unsafe {
let size = self.table_length(table)?;
if output.len() < size {
Err("Not enough memory to copy the table")
}else{
csound_sys::csoundTableCopyOut(self.engine.inner.csound,
table as c_int, output.as_ptr() as *mut c_double);
Ok(())
}
}
}
/// Asynchronous version of [`Csound:: table_copy_out`](struct.Csound.html#method.table_copy_out)
pub fn table_copy_out_async(& self, table: u32, output: &mut[f64]) -> Result< (), &'static str > {
unsafe {
let size = self.table_length(table)?;
if output.len() < size {
Err("Not enough memory to copy the table")
}else{
csound_sys::csoundTableCopyOutAsync(self.engine.inner.csound,
table as c_int, output.as_ptr() as *mut c_double);
Ok(())
}
}
}
/// Copy the contents of an array into a given function table. Error messages will be returned
/// if the function table doesn't exist or has not enough capacity.
///
/// # Arguments
/// * `table` The function table identifier.
/// * `src` Slice with the values to be copied into the function table
/// # Panic
/// This method will panic if the table has not enough memory.
pub fn table_copy_in(& self, table: u32, src: &[f64]) -> Result< (), &'static str > {
let size = self.table_length(table)?;
if size < src.len(){
Err("Table doesn't have enough capacity")
}else{
unsafe {
csound_sys::csoundTableCopyIn(self.engine.inner.csound, table as c_int, src.as_ptr() as *const c_double);
Ok(())
}
}
}
/// Asynchronous version of [`Csound:: table_copy_in`](struct.Csound.html#method.table_copy_in)
pub fn table_copy_in_async(& self, table: u32, src: &[f64]) -> Result< (), &'static str > {
let size = self.table_length(table)?;
if size < src.len(){
Err("Table doesn't have enough capacity")
}else{
unsafe {
csound_sys::csoundTableCopyInAsync(self.engine.inner.csound, table as c_int, src.as_ptr() as *const c_double);
Ok(())
}
}
}
/// Returns a [`Csound::Table`](struct.Table.html).
///
/// which could be used to read/write the table content
/// directly( not using [`Csound:: table_copy_in`](struct.Csound.html#method.table_copy_in) or [`Csound::table_copy_out`](struct.Csound.html#method.table_copy_out)).
/// this table will be valid along the csound instance. Returns None if the table doesn't
/// exist.
/// # Arguments
/// * `table` The function table identifier.
///
/// # Example
/// ```
/// let cs = Csound::new();
///
/// cs.compile_csd("some.csd");
/// cs.start().unwrap();
/// while cs.perform_ksmps() == false {
/// let mut table_buff = vec![0f64; cs.table_length(1).unwrap() as usize];
/// let mut table = cs.get_table(1).unwrap();
/// table.read( table_buff.as_mut_slice() ).unwrap();
/// // Do some stuffs
/// table.write(&table_buff.into_iter().map(|x| x*2.5).collect::<Vec<f64>>().as_mut_slice());
/// // Do some stuffs
/// }
/// ```
/// see [`Table::read`](struct.Table.html#method.read) or [`Table::write`](struct.Table.html#method.write).
pub fn get_table<'a>(&'a self, table: u32) -> Option<Table> {
let mut ptr = ::std::ptr::null_mut() as *mut c_double;
let length;
unsafe{
length = csound_sys::csoundGetTable(self.engine.inner.csound, &mut ptr as *mut *mut c_double, table as c_int) as i32;
}
match length {
-1 => None,
_ => Some(Table{
ptr: ptr,
length: length as usize,
phantom: PhantomData,
}),
}
}
/// Returns a vector with the arguments which was used to generate the table content.
/// # Arguments
/// * `table` The function table identifier.
///
/// * Note:* the argument list starts with the GEN number and is followed by its parameters.
/// eg. f 1 0 1024 10 1 0.5 yields the list {10.0,1.0,0.5}.
pub fn get_table_args(& self, table: u32) -> Option< Vec<f64> > {
let mut ptr = ::std::ptr::null_mut() as *mut c_double;
let length;
unsafe{
length = csound_sys::csoundGetTableArgs(self.engine.inner.csound, &mut ptr as *mut *mut c_double, table as c_int);
if length < 0 {
None
}else{
let mut result = Vec::with_capacity(length as usize);
for pos in 0..length as isize{
result.push(*ptr.offset(pos));
}
Some(result)
}
}
}
/// Checks if a given *gen* number is a named GEN if so,
/// it returns the string length, else, returns None
///
/// # Arguments
/// * `gen` The GEN number identifier.
pub fn is_named_gen(& self, gen: u32) -> usize{
unsafe {
csound_sys::csoundIsNamedGEN(self.engine.inner.csound, gen as c_int) as usize
}
}
/// Returns the GEN name if it exist ans is named, else, returns None
///
/// # Arguments
/// * `gen` The GEN number identifier.
pub fn get_gen_name(& self, gen: u32) -> Option<String>{
unsafe{
let len = self.is_named_gen(gen);
if len > 0 {
let name = vec!['\0' as u8; len];
let name_raw = CString::from_vec_unchecked(name).into_raw();
csound_sys::csoundGetNamedGEN(self.engine.inner.csound, gen as c_int, name_raw, len as c_int);
let name = CString::from_raw(name_raw);
let name = name.to_str().unwrap().to_owned();
Some(name)
}else{
None
}
}
}
/* Engine general Opcode function implementations **************************************************************************************** */
/// Gets an alphabetically sorted list of all opcodes.
///
/// Should be called after externals are loaded by csoundCompile().
/// The opcode information is contained in [`Csound::OpcodeListEntry`](struct.Csound.html#struct.OpcodeListEntry)
pub fn get_opcode_list_entry(& self) -> Option< Vec<OpcodeListEntry> > {
let mut ptr = ::std::ptr::null_mut() as *mut csound_sys::opcodeListEntry;
let length;
unsafe{
length = csound_sys::csoundNewOpcodeList(self.engine.inner.csound, &mut ptr as *mut *mut csound_sys::opcodeListEntry);
}
if length < 0 {
None
}else{
let mut result: Vec<OpcodeListEntry> = Vec::with_capacity(length as usize);
for pos in 0..length as isize{
unsafe {
let opname = (CStr::from_ptr( (*ptr.offset(pos)).opname)).to_owned();
let opname = opname.into_string().unwrap();
let outypes = (CStr::from_ptr( (*ptr.offset(pos)).outypes)).to_owned();
let outypes = outypes.into_string().unwrap();
let intypes = (CStr::from_ptr( (*ptr.offset(pos)).intypes)).to_owned();
let intypes = intypes.into_string().unwrap();
let flags = (*ptr.offset(pos)).flags as i32;
result.push( OpcodeListEntry{
opname: opname,
outypes: outypes,
intypes:intypes,
flags: flags
});
}
}
unsafe {
csound_sys::csoundDisposeOpcodeList(self.engine.inner.csound, ptr);
Some(result)
}
}
}
/**
TODO genName and appendOpcode functions
*****/
/* Engine miscellaneous functions **************************************************************************************** */
///Set language to 'lang_code'.
///
/// # Argument
/// * `lang_code` can be for example any of [`Language`](enum.Language.html) variants.
/// This affects all Csound instances running in the address
/// space of the current process. The special language code
/// *Language::CSLANGUAGE_DEFAULT* can be used to disable translation of messages and
/// free all memory allocated by a previous call to this function.
/// set_language() loads all files for the selected language from the directory specified by the **CSSTRNGS** environment
/// variable.
pub fn set_language(lang_code: Language){
unsafe {
csound_sys::csoundSetLanguage(lang_code as u32);
}
}
/// Return a 32-bit unsigned integer to be used as seed from current time.
pub fn get_random_seed_from_time() -> u32 {
unsafe {
csound_sys::csoundGetRandomSeedFromTime() as u32
}
}
/// Simple linear congruential random number generator: seed = seed * 742938285 % 2147483647
/// Returns the next number from the pseudo-random sequence, in the range 1 to 2147483646.
/// if the value of seed is not in the range 1 to 2147483646 an error message will
/// be returned.
pub fn get_rand31(seed: &mut u32) -> Result<u32, &'static str> {
unsafe {
match seed {
1...2147483646 =>{
let ptr: *mut u32 = &mut *seed;
let res = csound_sys::csoundRand31( ptr as *mut c_int) as u32;
Ok(res)
},
_ => Err("invalid seed value"),
}
}
}
/// Returns an initialised timer structure.
pub fn init_timer() -> RTCLOCK {
let mut timer = RTCLOCK::default();
unsafe{
let ptr: *mut RTCLOCK = &mut timer as *mut RTCLOCK;
csound_sys::csoundInitTimerStruct(ptr);
}
timer
}
/// Return the elapsed real time (in seconds) since the specified timer
///
/// # Arguments
/// * `timer` time struct since the elapsed time will be calculated.
pub fn get_real_time(timer: &RTCLOCK) -> f64 {
unsafe {
let ptr: *mut csound_sys::RTCLOCK = &mut csound_sys::RTCLOCK{
starttime_real: timer.starttime_real as c_long,
starttime_CPU: timer.starttime_CPU as c_long,
};
csound_sys::csoundGetRealTime(ptr) as f64
}
}
/// Return the elapsed CPU time (in seconds) since the specified *timer* structure was initialised.
/// # Arguments
/// * `gen` The GEN number identifier.
pub fn get_cpu_time(timer: &mut RTCLOCK) -> f64 {
unsafe {
csound_sys::csoundGetCPUTime(timer as *mut RTCLOCK) as f64
}
}
/// Create circular buffer.
///
/// # Arguments
/// * `num_elem` The buffer length.
/// # Example
///
/// ```
/// let csound = Csound::new();
/// let circular_buffer = csound.create_circular_buffer::<f64>(1024);
/// ```
pub fn create_circular_buffer<'a, T: 'a + Copy>(&'a self, num_elem: u32) -> CircularBuffer<T>{
unsafe{
let ptr: *mut T = csound_sys::csoundCreateCircularBuffer(self.engine.inner.csound, num_elem as c_int, mem::size_of::<T>() as c_int) as *mut T;
CircularBuffer{
csound: self.engine.inner.csound,
ptr: ptr,
phantom: PhantomData,
}
}
}
// TODO global variables functions
/********************************** Callback settings using the custom callback Handler implementation******/
/// Sets a function that is called to obtain a list of audio devices.
///
/// This should be set by rtaudio modules and should not be set by hosts.
pub fn audio_device_list_callback<F>(&mut self, f:F)
where F: FnMut(CS_AudioDevice) + Send + 'static
{
self.engine.inner.handler.callbacks.audio_dev_list_cb = Some(Box::new(f));
self.engine.enable_callback(AUDIO_DEV_LIST);
}
/// Sets a function to be called by Csound for opening real-time audio playback.
///
/// This callback is used to inform to the user about the current audio device Which
/// Csound will use to play the audio samples.
/// # Arguments
/// * `user_func` A function/closure which will receive a reference
/// to a RT_AudioParams struct with information about the csound audio params.
pub fn play_open_audio_callback<F>(&mut self, user_func:F)
where F: FnMut(&RT_AudioParams) -> Status + Send + 'static
{
self.engine.inner.handler.callbacks.play_open_cb = Some(Box::new(user_func));
self.engine.enable_callback(PLAY_OPEN);
}
/// Sets a function to be called by Csound for opening real-time audio recording.
///
/// This callback is used to inform to the user about the current audio device Which
/// Csound will use for opening realtime audio recording. You have to return Status::CS_SUCCESS
pub fn rec_open_audio_callback<F>(&mut self, f:F)
where F: FnMut(&RT_AudioParams)->Status + Send + 'static
{
self.engine.inner.handler.callbacks.rec_open_cb = Some(Box::new(f));
self.engine.enable_callback(REC_OPEN);
}
/// Sets a function to be called by Csound for performing real-time audio playback.
///
/// A reference to a buffer with audio samples is passed
/// to the user function in the callback. These samples have to be processed and sent
/// to a proper audio device.
pub fn rt_audio_play_callback<F>(&mut self, f:F)
where F: FnMut(&[f64]) + Send + 'static
{
self.engine.inner.handler.callbacks.rt_play_cb = Some(Box::new(f));
self.engine.enable_callback(REAL_TIME_PLAY);
}
/// Sets a function to be called by Csound for performing real-time audio recording.
///
/// With this callback the user can fill a buffer with samples from a custom
/// audio module, and pass it into csound.
pub fn rt_audio_rec_callback<F>(&mut self, f:F)
where F: FnMut(&mut[f64])->usize + Send + 'static
{
self.engine.inner.handler.callbacks.rt_rec_cb = Some(Box::new(f));
self.engine.enable_callback(REAL_TIME_REC);
}
/// Indicates to the user when csound has closed the rtaudio device.
pub fn rt_close_callback<F>(&mut self, f:F)
where F: FnMut() + Send + 'static
{
self.engine.inner.handler.callbacks.rt_close_cb = Some(Box::new(f));
self.engine.enable_callback(RT_CLOSE_CB);
}
/// Sets callback to be called once in every control period.
///
/// This facility can be used to ensure a function is called synchronously
/// before every csound control buffer processing.
/// It is important to make sure no blocking operations are performed in the callback.
pub fn sense_event_callback<F>(&mut self, f:F)
where F: FnMut() + Send + 'static
{
self.engine.inner.handler.callbacks.sense_event_cb = Some(Box::new(f));
self.engine.enable_callback(SENSE_EVENT);
}
/*fn cscore_callback<F>(&mut self, f:F)
where F: FnMut() + Send + 'static
{
self.engine.inner.handler.callbacks.cscore_cb = Some(Box::new(f));
self.engine.enable_callback(CSCORE_CB);
}*/
/// Sets a callback which will be called by csound to print an informational message.
///
/// # Arguments
/// * ´f´ Function which implement the FnMut trait.
/// The callback arguments are *u32* which indicates the message atributte,
/// and a reference to the message content.
/// # Example
///
/// ```
/// let mut cs = Csound::new();
/// cs.message_string_callback(|att: MessageType, message: &str| print!("{}", message));
/// ```
pub fn message_string_callback<F>(&mut self, f:F)
where F: FnMut(MessageType, &str) + Send + 'static
{
self.engine.inner.handler.callbacks.message_cb = Some(Box::new(f));
self.engine.enable_callback(MESSAGE_CB);
}
/*
*pub fn keyboard_callback<F>(&mut self, f:F)
* where F: FnMut(i32) + Send + 'static
*{
* self.engine.inner.handler.callbacks.keyboard_cb = Some(Box::new(f));
* self.engine.enable_callback(KEYBOARD_CB);
*}
*/
/// Sets the function which will be called whenever the [*invalue*](http://www.csounds.com/manual/html/invalue.html) opcode is used.
///
/// # Arguments
/// * ´f´ Function which implement the FnMut trait. The invalue opcode will trigger this callback passing
/// the channel name which requiere the data. This function/closure have to return the data which will be
/// passed to that specific channel if not only return ChannelData::CS_UNKNOWN_CHANNEL. Only *String* and *control* Channels
/// are supported.
/// # Example
/// ```
/// let input_channel = |name: &str|->ChannelData {
/// if name == "myStringChannel"{
/// let myString = "my data".to_owned();
/// ChannelData::CS_STRING_CHANNEL(myString)
/// }
/// ChannelData::CS_UNKNOWN_CHANNEL
/// };
/// let mut cs = Csound::new();
/// cs.input_channel_callback(input_channel);
/// ```
pub fn input_channel_callback<F>(&mut self, f:F)
where F: FnMut(&str) -> ChannelData + Send + 'static
{
self.engine.inner.handler.callbacks.input_channel_cb = Some(Box::new(f));
self.engine.enable_callback(CHANNEL_INPUT_CB);
}
/// Sets the function which will be called whenever the [*outvalue*](http://www.csounds.com/manual/html/outvalue.html) opcode is used.
///
/// # Arguments
/// * ´f´ Function which implement the FnMut trait. The outvalue opcode will trigger this callback passing
/// the channel ##name and the channel's output data encoded in the ChannelData. Only *String* and *control* Channels
/// are supported.
/// # Example
/// ```
/// let output_channel = |name: &str, data:ChannelData|{
/// print!("channel name:{} data: {:?}", name, data);
/// };
/// let mut cs = Csound::new();
/// cs.output_channel_callback(output_channel);
/// ```
pub fn output_channel_callback<F>(&mut self, f:F)
where F: FnMut(&str, ChannelData) + Send + 'static
{
self.engine.inner.handler.callbacks.output_channel_cb = Some(Box::new(f));
self.engine.enable_callback(CHANNEL_OUTPUT_CB);
}
/// Sets an external callback for receiving notices whenever Csound opens a file.
///
/// The callback is made after the file is successfully opened.
/// The following information is passed to the callback:
/// ## `file_info`
/// A [`FileInfo`](struct.FileInfo.html) struct containing the relevant file info.
pub fn file_open_callback<F>(&mut self, f:F)
where F: FnMut(&FileInfo) + Send + 'static
{
self.engine.inner.handler.callbacks.file_open_cb = Some(Box::new(f));
self.engine.enable_callback(FILE_OPEN_CB);
}
/// Sets a function to be called by Csound for opening real-time MIDI input.
///
/// This callback is used to inform to the user about the current MIDI input device.
/// # Arguments
/// * `user_func` A function/closure which will receive a reference
/// to a str with the device name.
pub fn midi_in_open_callback<F>(&mut self, f:F)
where F: FnMut(&str) + Send + 'static
{
self.engine.inner.handler.callbacks.midi_in_open_cb = Some(Box::new(f));
self.engine.enable_callback(MIDI_IN_OPEN_CB);
}
/// Sets a function to be called by Csound for opening real-time MIDI output.
///
/// This callback is used to inform to the user about the current MIDI output device.
/// # Arguments
/// * `user_func` A function/closure which will receive a reference
/// to a str with the device name.
pub fn midi_out_open_callback<F>(&mut self, f:F)
where F: FnMut(&str) + Send + 'static
{
self.engine.inner.handler.callbacks.midi_out_open_cb = Some(Box::new(f));
self.engine.enable_callback(MIDI_OUT_OPEN_CB);
}
/// Sets a function to be called by Csound for reading from real time MIDI input.
///
/// A reference to a buffer with audio samples is passed
/// to the user function in the callback. The callback have to return the number of elements red from the buffer.
pub fn midi_read_callback<F>(&mut self, f:F)
where F: FnMut(&[u8])->usize + Send + 'static
{
self.engine.inner.handler.callbacks.midi_read_cb = Some(Box::new(f));
self.engine.enable_callback(MIDI_READ_CB);
}
/// Sets a function to be called by Csound for Writing to real time MIDI input.
///
/// A reference to the device buffer is passed
/// to the user function in the callback. The passed buffer have the max length that
/// the user is able to use, and the callback have to return the number of element written into the buffer.
pub fn midi_write_callback<F>(&mut self, f:F)
where F: FnMut(&mut[u8])->usize + Send + 'static
{
self.engine.inner.handler.callbacks.midi_write_cb = Some(Box::new(f));
self.engine.enable_callback(MIDI_WRITE_CB);
}
/// Indicates to the user when csound has closed the midi input device.
pub fn midi_in_close_callback<F>(&mut self, f:F)
where F: FnMut() + Send + 'static
{
self.engine.inner.handler.callbacks.midi_in_close_cb = Some(Box::new(f));
self.engine.enable_callback(MIDI_IN_CLOSE);
}
/// Indicates to the user when csound has closed the midi output device.
pub fn midi_out_close_callback<F>(&mut self, f:F)
where F: FnMut() + Send + 'static
{
self.engine.inner.handler.callbacks.midi_out_close_cb = Some(Box::new(f));
self.engine.enable_callback(MIDI_OUT_CLOSE);
}
}//End impl block
// Drop method to free the memory using during the csound performance and instantiation
impl<H> Drop for Engine<H> {
fn drop(&mut self){
unsafe {
csound_sys::csoundStop(self.inner.csound);
csound_sys::csoundCleanup(self.inner.csound);
csound_sys::csoundDestroy(self.inner.csound);
}
}
}
impl<H: fmt::Debug> fmt::Debug for Engine<H> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result{
f.debug_struct("Engine")
.field("handler", &self.inner.handler)
.finish()
}
}
/// Csound's Circular Buffer refresentation.
///
/// This struct wraps a *mut T pointer to a circular buffer
/// allocated by csound. This Circular buffer won't outlive
/// the csound instance that allocated the buffer.
///
pub struct CircularBuffer<'a, T: 'a + Copy>{
csound: *mut csound_sys::CSOUND,
ptr: *mut T,
//pub num_elem: u32,
phantom: PhantomData<&'a T>,
}
impl<'a,T> CircularBuffer<'a,T> where T: Copy{
/// Read from circular buffer. This method will panic if the number of items
/// to read/write exceed the buffer capacity which passed as argument to this function.
///
/// # Arguments
/// * `out` A mutable slice where the items will be copied.
/// * `items` The number of elements to read and remove from the buffer.
///
/// Returns the actual number of items read **(0 <= n <= items)**.
pub fn read(&self, out:&mut[T], items:u32) -> Result<usize, &'static str>{
if items as usize <= out.len(){
return Err("your buffer has not enough capacity");
}
unsafe{
Ok(csound_sys::csoundReadCircularBuffer(self.csound, self.ptr as *mut c_void,
out.as_mut_ptr() as *mut c_void, items as c_int) as usize)
}
}
/// Read from circular buffer without removing them from the buffer.
/// This method will panic if the number of items to read/write
/// exceed the buffer capacity which passed as argument to this function.
///
/// # Arguments
/// * `out` A mutable slice where the items will be copied.
/// * `items` The number of elements to peek from the buffer.
///
/// # Returns
/// The actual number of items read **(0 <= n <= items)**.
pub fn peek(&self, out:&mut[T], items:u32) -> Result<usize, &'static str>{
if items as usize <= out.len(){
return Err("your buffer has not enough capacity");
}
unsafe{
Ok(csound_sys::csoundPeekCircularBuffer(self.csound, self.ptr as *mut c_void,
out.as_mut_ptr() as *mut c_void, items as c_int) as usize)
}
}
/// Write to circular buffer.
/// This method will panic if the number of items to read/write
/// exceed the buffer capacity which passed as argument to this function.
///
/// # Arguments
/// * `input` A slice with the date which will be copied into the buffer.
/// * `items` The number of elements to wrtie into the buffer.
///
/// # Returns
/// The actual number of items written **(0 <= n <= items)**.
pub fn write(&self, input:&[T], items:u32) -> Result<usize, &'static str>{
if items as usize <= input.len(){
return Err("your buffer has not enough capacity");
}
unsafe{
Ok(csound_sys::csoundWriteCircularBuffer(self.csound, self.ptr as *mut c_void,
input.as_ptr() as *const c_void, items as c_int) as usize)
}
}
/// Empty circular buffer of any remaining data.
///
/// This function should only be used if there is no reader actively getting data from the buffer.
pub fn flush(&self){
unsafe{
csound_sys::csoundFlushCircularBuffer(self.csound, self.ptr as *mut c_void);
}
}
}
impl<'a, T> Drop for CircularBuffer<'a, T> where T: Copy {
fn drop(&mut self){
unsafe {
csound_sys::csoundDestroyCircularBuffer(self.csound, self.ptr as *mut c_void);
}
}
}
/// Csound table representation.
///
/// This struct is build up to manipulate directly a csound's table.
#[derive(Debug)]
pub struct Table<'a> {
ptr: *mut f64 ,
/// The table length.
pub length: usize,
phantom: PhantomData<&'a f64>,
}
impl<'a> Table<'a>{
/// method used to copy data from the table internal buffer
/// into an user buffer. A error message is returned if the Table is not longer valid.
/// # Arguments
/// * `out` A slice where out.len() elements from the table will be copied.
/// # Returns
/// The number of elements copied into the output buffer or an error message
/// # Example
/// ```
/// let cs = Csound::new();
///
/// cs.compile_csd("some.csd");
/// cs.start().unwrap();
/// while cs.perform_ksmps() == false {
/// let mut table = cs.get_table(1).unwrap();
/// let mut table_buff = vec![0f64; table.length];
/// // copy Table::length elements from the table's internal buffer
/// table.read( table_buff.as_mut_slice() ).unwrap();
/// // Do some stuffs
/// }
/// ```
pub fn read(&self, out: &mut [f64]) -> Result<usize, &'static str>{
unsafe{
if !self.ptr.is_null(){
let mut len = out.len();
if self.length < len{
len = self.length;
}
std::ptr::copy(self.ptr, out.as_ptr() as *mut f64, len);
Ok(len)
}else{
Err("This table is not valid")
}
}
}
/// method used to copy data into the table internal buffer
/// from an user buffer. A error message is returned if the Table is not longer valid.
/// # Arguments
/// * `input` A slice where input.len() elements will be copied.
/// # Returns
/// The number of elements copied into the table or an error message
///
/// # Example
/// ```
/// let cs = Csound::new();
///
/// cs.compile_csd("some.csd");
/// cs.start().unwrap();
/// while cs.perform_ksmps() == false {
/// let mut table = cs.get_table(1).unwrap();
/// let mut table_buff = vec![0f64; table.length];
/// // copy Table::length elements from the table's internal buffer
/// table.read( table_buff.as_mut_slice() ).unwrap();
/// // Do some stuffs
/// table.write(&table_buff.into_iter().map(|x| x*2.5).collect::<Vec<f64>>().as_mut_slice());
/// // Do some stuffs
/// }
/// ```
pub fn write(&mut self, input: &[f64]) -> Result<usize, &'static str> {
unsafe{
if !self.ptr.is_null(){
let mut len = input.len();
if self.length < len{
len = self.length;
}
std::ptr::copy(input.as_ptr(), self.ptr, len);
Ok(len)
}else{
Err("This table is not valid")
}
}
}
}
/// Rust representation for an raw csound channel pointer
#[derive(Debug)]
pub struct ControlChannelPtr<'a>{
ptr: *mut f64,
pub len: usize,
channel_type:ControlChannelType,
phantom: PhantomData<&'a f64>,
}
impl<'a> ControlChannelPtr<'a>{
pub fn read(&self, dest: &mut [f64]) -> Result<usize, io::Error> {
let mut len: usize = dest.len();
if self.len < len{
len = self.len;
}
if self.len == 0 {
return Err(io::Error::new(
io::ErrorKind::Other,
format!(
"Missing data: requesting {} but only got {}.",
len,
self.len
),
));
}
unsafe{
std::ptr::copy(self.ptr as *const _, dest.as_mut_ptr(), len);
}
Ok(len * std::mem::size_of::<f64>())
}
pub fn write(&self, src: &[f64]) -> Result<usize, io::Error> {
let mut len: usize = src.len();
if self.len < len{
len = self.len;
}
if self.len == 0 {
return Err(io::Error::new(
io::ErrorKind::Other,
format!(
"Not memory for data: writing {} but only got {}.",
len,
self.len
),
));
}
unsafe{
std::ptr::copy(src.as_ptr(), self.ptr as *mut _,len);
}
Ok(len * std::mem::size_of::<f64>())
}
}