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
|
// -*- c-basic-offset: 4 -*-
/*
Rosegarden
A sequencer and musical notation editor.
This program is Copyright 2000-2008
Guillaume Laurent <glaurent@telegraph-road.org>,
Chris Cannam <cannam@all-day-breakfast.com>,
Richard Bown <bownie@bownie.com>
The moral right of the authors to claim authorship of this work
has been asserted.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version. See the file
COPYING included with this distribution for more information.
*/
#include "JackDriver.h"
#include "AlsaDriver.h"
#include "MappedStudio.h"
#include "AudioProcess.h"
#include "Profiler.h"
#include "AudioLevel.h"
#include "Audit.h"
#include "PluginFactory.h"
#ifdef HAVE_ALSA
#ifdef HAVE_LIBJACK
//#define DEBUG_JACK_DRIVER 1
//#define DEBUG_JACK_TRANSPORT 1
//#define DEBUG_JACK_PROCESS 1
//#define DEBUG_JACK_XRUN 1
namespace Rosegarden
{
#if (defined(DEBUG_JACK_DRIVER) || defined(DEBUG_JACK_PROCESS) || defined(DEBUG_JACK_TRANSPORT))
static unsigned long framesThisPlay = 0;
static RealTime startTime;
#endif
JackDriver::JackDriver(AlsaDriver *alsaDriver) :
m_client(0),
m_bufferSize(0),
m_sampleRate(0),
m_tempOutBuffer(0),
m_jackTransportEnabled(false),
m_jackTransportMaster(false),
m_waiting(false),
m_waitingState(JackTransportStopped),
m_waitingToken(0),
m_ignoreProcessTransportCount(0),
m_bussMixer(0),
m_instrumentMixer(0),
m_fileReader(0),
m_fileWriter(0),
m_alsaDriver(alsaDriver),
m_masterLevel(1.0),
m_directMasterAudioInstruments(0L),
m_directMasterSynthInstruments(0L),
m_haveAsyncAudioEvent(false),
m_kickedOutAt(0),
m_framesProcessed(0),
m_ok(false)
{
assert(sizeof(sample_t) == sizeof(float));
initialise();
}
JackDriver::~JackDriver()
{
#ifdef DEBUG_JACK_DRIVER
std::cerr << "JackDriver::~JackDriver" << std::endl;
#endif
m_ok = false; // prevent any more work in process()
if (m_client) {
#ifdef DEBUG_JACK_DRIVER
std::cerr << "JackDriver::shutdown - deactivating JACK client"
<< std::endl;
#endif
if (jack_deactivate(m_client)) {
std::cerr << "JackDriver::shutdown - deactivation failed"
<< std::endl;
}
}
#ifdef DEBUG_JACK_DRIVER
std::cerr << "JackDriver::~JackDriver: terminating buss mixer" << std::endl;
#endif
AudioBussMixer *bussMixer = m_bussMixer;
m_bussMixer = 0;
if (bussMixer)
bussMixer->terminate();
#ifdef DEBUG_JACK_DRIVER
std::cerr << "JackDriver::~JackDriver: terminating instrument mixer" << std::endl;
#endif
AudioInstrumentMixer *instrumentMixer = m_instrumentMixer;
m_instrumentMixer = 0;
if (instrumentMixer) {
instrumentMixer->terminate();
instrumentMixer->destroyAllPlugins();
}
#ifdef DEBUG_JACK_DRIVER
std::cerr << "JackDriver::~JackDriver: terminating file reader" << std::endl;
#endif
AudioFileReader *reader = m_fileReader;
m_fileReader = 0;
if (reader)
reader->terminate();
#ifdef DEBUG_JACK_DRIVER
std::cerr << "JackDriver::~JackDriver: terminating file writer" << std::endl;
#endif
AudioFileWriter *writer = m_fileWriter;
m_fileWriter = 0;
if (writer)
writer->terminate();
if (m_client) {
#ifdef DEBUG_JACK_DRIVER
std::cerr << "JackDriver::shutdown - tearing down JACK client"
<< std::endl;
#endif
for (unsigned int i = 0; i < m_inputPorts.size(); ++i) {
#ifdef DEBUG_JACK_DRIVER
std::cerr << "unregistering input " << i << std::endl;
#endif
if (jack_port_unregister(m_client, m_inputPorts[i])) {
std::cerr << "JackDriver::shutdown - "
<< "can't unregister input port " << i + 1
<< std::endl;
}
}
for (unsigned int i = 0; i < m_outputSubmasters.size(); ++i) {
#ifdef DEBUG_JACK_DRIVER
std::cerr << "unregistering output sub " << i << std::endl;
#endif
if (jack_port_unregister(m_client, m_outputSubmasters[i])) {
std::cerr << "JackDriver::shutdown - "
<< "can't unregister output submaster " << i + 1 << std::endl;
}
}
for (unsigned int i = 0; i < m_outputMonitors.size(); ++i) {
#ifdef DEBUG_JACK_DRIVER
std::cerr << "unregistering output mon " << i << std::endl;
#endif
if (jack_port_unregister(m_client, m_outputMonitors[i])) {
std::cerr << "JackDriver::shutdown - "
<< "can't unregister output monitor " << i + 1 << std::endl;
}
}
for (unsigned int i = 0; i < m_outputMasters.size(); ++i) {
#ifdef DEBUG_JACK_DRIVER
std::cerr << "unregistering output master " << i << std::endl;
#endif
if (jack_port_unregister(m_client, m_outputMasters[i])) {
std::cerr << "JackDriver::shutdown - "
<< "can't unregister output master " << i + 1 << std::endl;
}
}
#ifdef DEBUG_JACK_DRIVER
std::cerr << "closing client" << std::endl;
#endif
jack_client_close(m_client);
std::cerr << "done" << std::endl;
m_client = 0;
}
#ifdef DEBUG_JACK_DRIVER
std::cerr << "JackDriver: deleting mixers etc" << std::endl;
#endif
delete bussMixer;
delete instrumentMixer;
delete reader;
delete writer;
#ifdef DEBUG_JACK_DRIVER
std::cerr << "JackDriver::~JackDriver exiting" << std::endl;
#endif
}
void
JackDriver::initialise(bool reinitialise)
{
m_ok = false;
Audit audit;
audit << std::endl;
std::string jackClientName = "rosegarden";
// attempt connection to JACK server
//
if ((m_client = jack_client_new(jackClientName.c_str())) == 0) {
audit << "JackDriver::initialiseAudio - "
<< "JACK server not running"
<< std::endl;
return ;
}
InstrumentId instrumentBase;
int instrumentCount;
m_alsaDriver->getAudioInstrumentNumbers(instrumentBase, instrumentCount);
for (InstrumentId id = instrumentBase;
id < instrumentBase + instrumentCount; ++id) {
// prefill so that we can refer to the map without a lock (as
// the number of instruments won't change)
m_recordInputs[id] = RecordInputDesc(1000, -1, 0.0);
}
// set callbacks
//
jack_set_process_callback(m_client, jackProcessStatic, this);
jack_set_buffer_size_callback(m_client, jackBufferSize, this);
jack_set_sample_rate_callback(m_client, jackSampleRate, this);
jack_on_shutdown(m_client, jackShutdown, this);
jack_set_xrun_callback(m_client, jackXRun, this);
jack_set_sync_callback(m_client, jackSyncCallback, this);
// get and report the sample rate and buffer size
//
m_sampleRate = jack_get_sample_rate(m_client);
m_bufferSize = jack_get_buffer_size(m_client);
audit << "JackDriver::initialiseAudio - JACK sample rate = "
<< m_sampleRate << "Hz, buffer size = " << m_bufferSize
<< std::endl;
PluginFactory::setSampleRate(m_sampleRate);
// Get the initial buffer size before we activate the client
//
if (!reinitialise) {
// create processing buffer(s)
//
m_tempOutBuffer = new sample_t[m_bufferSize];
audit << "JackDriver::initialiseAudio - "
<< "creating disk thread" << std::endl;
m_fileReader = new AudioFileReader(m_alsaDriver, m_sampleRate);
m_fileWriter = new AudioFileWriter(m_alsaDriver, m_sampleRate);
m_instrumentMixer = new AudioInstrumentMixer
(m_alsaDriver, m_fileReader, m_sampleRate, m_bufferSize);
m_bussMixer = new AudioBussMixer
(m_alsaDriver, m_instrumentMixer, m_sampleRate, m_bufferSize);
m_instrumentMixer->setBussMixer(m_bussMixer);
// We run the file reader whatever, but we only run the other
// threads (instrument mixer, buss mixer, file writer) when we
// actually need them. (See updateAudioData and createRecordFile.)
m_fileReader->run();
}
// Create and connect the default numbers of ports. We always create
// one stereo pair each of master and monitor outs, and then we create
// record ins, fader outs and submaster outs according to the user's
// preferences. Since we don't know the user's preferences yet, we'll
// start by creating one pair of record ins and no fader or submaster
// outs.
//
m_outputMasters.clear();
m_outputMonitors.clear();
m_outputSubmasters.clear();
m_outputInstruments.clear();
m_inputPorts.clear();
if (!createMainOutputs()) { // one stereo pair master, one pair monitor
audit << "JackDriver::initialise - "
<< "failed to create main outputs!" << std::endl;
return ;
}
if (!createRecordInputs(1)) {
audit << "JackDriver::initialise - "
<< "failed to create record inputs!" << std::endl;
return ;
}
if (jack_activate(m_client)) {
audit << "JackDriver::initialise - "
<< "client activation failed" << std::endl;
return ;
}
// Now set up the default connections.
std::string playback_1, playback_2;
const char **ports =
jack_get_ports(m_client, NULL, NULL,
JackPortIsPhysical | JackPortIsInput);
if (ports) {
if (ports[0])
playback_1 = std::string(ports[0]);
if (ports[1])
playback_2 = std::string(ports[1]);
// count ports
unsigned int i = 0;
for (i = 0; ports[i]; i++)
;
audit << "JackDriver::initialiseAudio - "
<< "found " << i << " JACK physical outputs"
<< std::endl;
} else
audit << "JackDriver::initialiseAudio - "
<< "no JACK physical outputs found"
<< std::endl;
free(ports);
if (playback_1 != "") {
audit << "JackDriver::initialiseAudio - "
<< "connecting from "
<< "\"" << jack_port_name(m_outputMasters[0])
<< "\" to \"" << playback_1.c_str() << "\""
<< std::endl;
// connect our client up to the ALSA ports - first left output
//
if (jack_connect(m_client, jack_port_name(m_outputMasters[0]),
playback_1.c_str())) {
audit << "JackDriver::initialiseAudio - "
<< "cannot connect to JACK output port" << std::endl;
return ;
}
/*
if (jack_connect(m_client, jack_port_name(m_outputMonitors[0]),
playback_1.c_str()))
{
audit << "JackDriver::initialiseAudio - "
<< "cannot connect to JACK output port" << std::endl;
return;
}
*/
}
if (playback_2 != "") {
audit << "JackDriver::initialiseAudio - "
<< "connecting from "
<< "\"" << jack_port_name(m_outputMasters[1])
<< "\" to \"" << playback_2.c_str() << "\""
<< std::endl;
if (jack_connect(m_client, jack_port_name(m_outputMasters[1]),
playback_2.c_str())) {
audit << "JackDriver::initialiseAudio - "
<< "cannot connect to JACK output port" << std::endl;
}
/*
if (jack_connect(m_client, jack_port_name(m_outputMonitors[1]),
playback_2.c_str()))
{
audit << "JackDriver::initialiseAudio - "
<< "cannot connect to JACK output port" << std::endl;
}
*/
}
std::string capture_1, capture_2;
ports =
jack_get_ports(m_client, NULL, NULL,
JackPortIsPhysical | JackPortIsOutput);
if (ports) {
if (ports[0])
capture_1 = std::string(ports[0]);
if (ports[1])
capture_2 = std::string(ports[1]);
// count ports
unsigned int i = 0;
for (i = 0; ports[i]; i++)
;
audit << "JackDriver::initialiseAudio - "
<< "found " << i << " JACK physical inputs"
<< std::endl;
} else
audit << "JackDriver::initialiseAudio - "
<< "no JACK physical inputs found"
<< std::endl;
free(ports);
if (capture_1 != "") {
audit << "JackDriver::initialiseAudio - "
<< "connecting from "
<< "\"" << capture_1.c_str()
<< "\" to \"" << jack_port_name(m_inputPorts[0]) << "\""
<< std::endl;
if (jack_connect(m_client, capture_1.c_str(),
jack_port_name(m_inputPorts[0]))) {
audit << "JackDriver::initialiseAudio - "
<< "cannot connect to JACK input port" << std::endl;
}
}
if (capture_2 != "") {
audit << "JackDriver::initialiseAudio - "
<< "connecting from "
<< "\"" << capture_2.c_str()
<< "\" to \"" << jack_port_name(m_inputPorts[1]) << "\""
<< std::endl;
if (jack_connect(m_client, capture_2.c_str(),
jack_port_name(m_inputPorts[1]))) {
audit << "JackDriver::initialiseAudio - "
<< "cannot connect to JACK input port" << std::endl;
}
}
audit << "JackDriver::initialiseAudio - "
<< "initialised JACK audio subsystem"
<< std::endl;
m_ok = true;
}
bool
JackDriver::createMainOutputs()
{
if (!m_client)
return false;
jack_port_t *port = jack_port_register
(m_client, "master out L",
JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
if (!port)
return false;
m_outputMasters.push_back(port);
port = jack_port_register
(m_client, "master out R",
JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
if (!port)
return false;
m_outputMasters.push_back(port);
port = jack_port_register
(m_client, "record monitor out L",
JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
if (!port)
return false;
m_outputMonitors.push_back(port);
port = jack_port_register
(m_client, "record monitor out R",
JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
if (!port)
return false;
m_outputMonitors.push_back(port);
return true;
}
bool
JackDriver::createFaderOutputs(int audioPairs, int synthPairs)
{
if (!m_client)
return false;
int pairs = audioPairs + synthPairs;
int pairsNow = m_outputInstruments.size() / 2;
if (pairs == pairsNow)
return true;
for (int i = pairsNow; i < pairs; ++i) {
char namebuffer[22];
jack_port_t *port;
if (i < audioPairs) {
snprintf(namebuffer, 21, "audio fader %d out L", i + 1);
} else {
snprintf(namebuffer, 21, "synth fader %d out L", i - audioPairs + 1);
}
port = jack_port_register(m_client,
namebuffer,
JACK_DEFAULT_AUDIO_TYPE,
JackPortIsOutput,
0);
if (!port)
return false;
m_outputInstruments.push_back(port);
if (i < audioPairs) {
snprintf(namebuffer, 21, "audio fader %d out R", i + 1);
} else {
snprintf(namebuffer, 21, "synth fader %d out R", i - audioPairs + 1);
}
port = jack_port_register(m_client,
namebuffer,
JACK_DEFAULT_AUDIO_TYPE,
JackPortIsOutput,
0);
if (!port)
return false;
m_outputInstruments.push_back(port);
}
while ((int)m_outputInstruments.size() > pairs * 2) {
std::vector<jack_port_t *>::iterator itr = m_outputInstruments.end();
--itr;
jack_port_unregister(m_client, *itr);
m_outputInstruments.erase(itr);
}
return true;
}
bool
JackDriver::createSubmasterOutputs(int pairs)
{
if (!m_client)
return false;
int pairsNow = m_outputSubmasters.size() / 2;
if (pairs == pairsNow)
return true;
for (int i = pairsNow; i < pairs; ++i) {
char namebuffer[22];
jack_port_t *port;
snprintf(namebuffer, 21, "submaster %d out L", i + 1);
port = jack_port_register(m_client,
namebuffer,
JACK_DEFAULT_AUDIO_TYPE,
JackPortIsOutput,
0);
if (!port)
return false;
m_outputSubmasters.push_back(port);
snprintf(namebuffer, 21, "submaster %d out R", i + 1);
port = jack_port_register(m_client,
namebuffer,
JACK_DEFAULT_AUDIO_TYPE,
JackPortIsOutput,
0);
if (!port)
return false;
m_outputSubmasters.push_back(port);
}
while ((int)m_outputSubmasters.size() > pairs * 2) {
std::vector<jack_port_t *>::iterator itr = m_outputSubmasters.end();
--itr;
jack_port_unregister(m_client, *itr);
m_outputSubmasters.erase(itr);
}
return true;
}
bool
JackDriver::createRecordInputs(int pairs)
{
if (!m_client)
return false;
int pairsNow = m_inputPorts.size() / 2;
if (pairs == pairsNow)
return true;
for (int i = pairsNow; i < pairs; ++i) {
char namebuffer[22];
jack_port_t *port;
snprintf(namebuffer, 21, "record in %d L", i + 1);
port = jack_port_register(m_client,
namebuffer,
JACK_DEFAULT_AUDIO_TYPE,
JackPortIsInput,
0);
if (!port)
return false;
m_inputPorts.push_back(port);
snprintf(namebuffer, 21, "record in %d R", i + 1);
port = jack_port_register(m_client,
namebuffer,
JACK_DEFAULT_AUDIO_TYPE,
JackPortIsInput,
0);
if (!port)
return false;
m_inputPorts.push_back(port);
}
while ((int)m_outputSubmasters.size() > pairs * 2) {
std::vector<jack_port_t *>::iterator itr = m_outputSubmasters.end();
--itr;
jack_port_unregister(m_client, *itr);
m_outputSubmasters.erase(itr);
}
return true;
}
void
JackDriver::setAudioPorts(bool faderOuts, bool submasterOuts)
{
if (!m_client)
return ;
Audit audit;
#ifdef DEBUG_JACK_DRIVER
std::cerr << "JackDriver::setAudioPorts(" << faderOuts << "," << submasterOuts << ")" << std::endl;
#endif
if (!m_client) {
std::cerr << "JackDriver::setAudioPorts(" << faderOuts << "," << submasterOuts << "): no client yet" << std::endl;
return ;
}
if (faderOuts) {
InstrumentId instrumentBase;
int audioInstruments;
int synthInstruments;
m_alsaDriver->getAudioInstrumentNumbers(instrumentBase, audioInstruments);
m_alsaDriver->getSoftSynthInstrumentNumbers(instrumentBase, synthInstruments);
if (!createFaderOutputs(audioInstruments, synthInstruments)) {
m_ok = false;
audit << "Failed to create fader outs!" << std::endl;
return ;
}
} else {
createFaderOutputs(0, 0);
}
if (submasterOuts) {
// one fewer than returned here, because the master has a buss object too
if (!createSubmasterOutputs
(m_alsaDriver->getMappedStudio()->getObjectCount
(MappedObject::AudioBuss) - 1)) {
m_ok = false;
audit << "Failed to create submaster outs!" << std::endl;
return ;
}
} else {
createSubmasterOutputs(0);
}
}
RealTime
JackDriver::getAudioPlayLatency() const
{
if (!m_client)
return RealTime::zeroTime;
jack_nframes_t latency =
jack_port_get_total_latency(m_client, m_outputMasters[0]);
return RealTime::frame2RealTime(latency, m_sampleRate);
}
RealTime
JackDriver::getAudioRecordLatency() const
{
if (!m_client)
return RealTime::zeroTime;
jack_nframes_t latency =
jack_port_get_total_latency(m_client, m_inputPorts[0]);
return RealTime::frame2RealTime(latency, m_sampleRate);
}
RealTime
JackDriver::getInstrumentPlayLatency(InstrumentId id) const
{
if (m_instrumentLatencies.find(id) == m_instrumentLatencies.end()) {
return RealTime::zeroTime;
} else {
return m_instrumentLatencies.find(id)->second;
}
}
RealTime
JackDriver::getMaximumPlayLatency() const
{
return m_maxInstrumentLatency;
}
int
JackDriver::jackProcessStatic(jack_nframes_t nframes, void *arg)
{
JackDriver *inst = static_cast<JackDriver*>(arg);
if (inst)
return inst->jackProcess(nframes);
else
return 0;
}
int
JackDriver::jackProcess(jack_nframes_t nframes)
{
if (!m_ok || !m_client) {
#ifdef DEBUG_JACK_PROCESS
std::cerr << "JackDriver::jackProcess: not OK" << std::endl;
#endif
return 0;
}
if (!m_bussMixer) {
#ifdef DEBUG_JACK_PROCESS
std::cerr << "JackDriver::jackProcess: no buss mixer" << std::endl;
#endif
return jackProcessEmpty(nframes);
}
if (m_alsaDriver->areClocksRunning()) {
m_alsaDriver->checkTimerSync(m_framesProcessed);
} else {
m_alsaDriver->checkTimerSync(0);
}
bool lowLatencyMode = m_alsaDriver->getLowLatencyMode();
bool clocksRunning = m_alsaDriver->areClocksRunning();
bool playing = m_alsaDriver->isPlaying();
bool asyncAudio = m_haveAsyncAudioEvent;
#ifdef DEBUG_JACK_PROCESS
Profiler profiler("jackProcess", true);
#else
#ifdef DEBUG_JACK_XRUN
Profiler profiler("jackProcess", false);
#endif
#endif
if (lowLatencyMode) {
if (clocksRunning) {
if (playing || asyncAudio) {
if (m_instrumentMixer->tryLock() == 0) {
m_instrumentMixer->kick(false);
m_instrumentMixer->releaseLock();
//#ifdef DEBUG_JACK_PROCESS
} else {
std::cerr << "JackDriver::jackProcess: no instrument mixer lock available" << std::endl;
//#endif
}
if (m_bussMixer->getBussCount() > 0) {
if (m_bussMixer->tryLock() == 0) {
m_bussMixer->kick(false, false);
m_bussMixer->releaseLock();
//#ifdef DEBUG_JACK_PROCESS
} else {
std::cerr << "JackDriver::jackProcess: no buss mixer lock available" << std::endl;
//#endif
}
}
}
}
}
if (jack_cpu_load(m_client) > 97.0) {
reportFailure(MappedEvent::FailureCPUOverload);
return jackProcessEmpty(nframes);
}
#ifdef DEBUG_JACK_PROCESS
Profiler profiler2("jackProcess post mix", true);
#else
#ifdef DEBUG_JACK_XRUN
Profiler profiler2("jackProcess post mix", false);
#endif
#endif
SequencerDataBlock *sdb = m_alsaDriver->getSequencerDataBlock();
jack_position_t position;
jack_transport_state_t state = JackTransportRolling;
bool doneRecord = false;
int ignoreCount = m_ignoreProcessTransportCount;
if (ignoreCount > 0)
--m_ignoreProcessTransportCount;
InstrumentId audioInstrumentBase;
int audioInstruments;
m_alsaDriver->getAudioInstrumentNumbers(audioInstrumentBase, audioInstruments);
if (m_jackTransportEnabled) {
state = jack_transport_query(m_client, &position);
#ifdef DEBUG_JACK_PROCESS
std::cerr << "JackDriver::jackProcess: JACK transport state is " << state << std::endl;
#endif
if (state == JackTransportStopped) {
if (playing && clocksRunning && !m_waiting) {
ExternalTransport *transport =
m_alsaDriver->getExternalTransportControl();
if (transport) {
#ifdef DEBUG_JACK_TRANSPORT
std::cerr << "JackDriver::jackProcess: JACK transport stopped externally at " << position.frame << std::endl;
#endif
m_waitingToken =
transport->transportJump
(ExternalTransport::TransportStopAtTime,
RealTime::frame2RealTime(position.frame,
position.frame_rate));
}
} else if (clocksRunning) {
if (!asyncAudio) {
#ifdef DEBUG_JACK_PROCESS
std::cerr << "JackDriver::jackProcess: no interesting async events" << std::endl;
#endif
// do this before record monitor, otherwise we lose monitor out
jackProcessEmpty(nframes);
}
// for monitoring:
int rv = 0;
for (InstrumentId id = audioInstrumentBase;
id < audioInstrumentBase + audioInstruments; ++id) {
int irv = jackProcessRecord(id, nframes, 0, 0, clocksRunning);
if (irv != 0)
rv = irv;
}
doneRecord = true;
if (!asyncAudio) {
return rv;
}
} else {
return jackProcessEmpty(nframes);
}
} else if (state == JackTransportStarting) {
return jackProcessEmpty(nframes);
} else if (state != JackTransportRolling) {
std::cerr << "JackDriver::jackProcess: unexpected JACK transport state " << state << std::endl;
}
}
if (state == JackTransportRolling) { // also covers not-on-transport case
if (m_waiting) {
if (ignoreCount > 0) {
#ifdef DEBUG_JACK_TRANSPORT
std::cerr << "JackDriver::jackProcess: transport rolling, but we're ignoring it (count = " << ignoreCount << ")" << std::endl;
#endif
} else {
#ifdef DEBUG_JACK_TRANSPORT
std::cerr << "JackDriver::jackProcess: transport rolling, telling ALSA driver to go!" << std::endl;
#endif
m_alsaDriver->startClocksApproved();
m_waiting = false;
}
}
#ifdef DEBUG_JACK_PROCESS
std::cerr << "JackDriver::jackProcess (rolling or not on JACK transport)" << std::endl;
#endif
if (!clocksRunning) {
#ifdef DEBUG_JACK_PROCESS
std::cerr << "JackDriver::jackProcess: clocks stopped" << std::endl;
#endif
return jackProcessEmpty(nframes);
} else if (!playing) {
#ifdef DEBUG_JACK_PROCESS
std::cerr << "JackDriver::jackProcess: not playing" << std::endl;
#endif
if (!asyncAudio) {
#ifdef DEBUG_JACK_PROCESS
std::cerr << "JackDriver::jackProcess: no interesting async events" << std::endl;
#endif
// do this before record monitor, otherwise we lose monitor out
jackProcessEmpty(nframes);
}
// for monitoring:
int rv = 0;
for (InstrumentId id = audioInstrumentBase;
id < audioInstrumentBase + audioInstruments; ++id) {
int irv = jackProcessRecord(id, nframes, 0, 0, clocksRunning);
if (irv != 0)
rv = irv;
}
doneRecord = true;
if (!asyncAudio) {
return rv;
}
}
}
#ifdef DEBUG_JACK_PROCESS
Profiler profiler3("jackProcess post transport", true);
#else
#ifdef DEBUG_JACK_XRUN
Profiler profiler3("jackProcess post transport", false);
#endif
#endif
InstrumentId synthInstrumentBase;
int synthInstruments;
m_alsaDriver->getSoftSynthInstrumentNumbers(synthInstrumentBase, synthInstruments);
// We always have the master out
sample_t *master[2] = {
static_cast<sample_t *>
(jack_port_get_buffer(m_outputMasters[0], nframes)),
static_cast<sample_t *>
(jack_port_get_buffer(m_outputMasters[1], nframes))
};
memset(master[0], 0, nframes * sizeof(sample_t));
memset(master[1], 0, nframes * sizeof(sample_t));
// Reset monitor outs (if present) here prior to mixing
if (m_outputMonitors.size() > 0) {
sample_t *buffer = static_cast<sample_t *>
(jack_port_get_buffer(m_outputMonitors[0], nframes));
if (buffer)
memset(buffer, 0, nframes * sizeof(sample_t));
}
if (m_outputMonitors.size() > 1) {
sample_t *buffer = static_cast<sample_t *>
(jack_port_get_buffer(m_outputMonitors[1], nframes));
if (buffer)
memset(buffer, 0, nframes * sizeof(sample_t));
}
int bussCount = m_bussMixer->getBussCount();
// If we have any busses, then we just mix from them (but we still
// need to keep ourselves up to date by reading and monitoring the
// instruments). If we have no busses, mix direct from instruments.
for (int buss = 0; buss < bussCount; ++buss) {
sample_t *submaster[2] = { 0, 0 };
sample_t peak[2] = { 0.0, 0.0 };
if ((int)m_outputSubmasters.size() > buss * 2 + 1) {
submaster[0] = static_cast<sample_t *>
(jack_port_get_buffer(m_outputSubmasters[buss * 2], nframes));
submaster[1] = static_cast<sample_t *>
(jack_port_get_buffer(m_outputSubmasters[buss * 2 + 1], nframes));
}
if (!submaster[0])
submaster[0] = m_tempOutBuffer;
if (!submaster[1])
submaster[1] = m_tempOutBuffer;
for (int ch = 0; ch < 2; ++ch) {
RingBuffer<AudioBussMixer::sample_t> *rb =
m_bussMixer->getRingBuffer(buss, ch);
if (!rb || m_bussMixer->isBussDormant(buss)) {
if (rb)
rb->skip(nframes);
if (submaster[ch])
memset(submaster[ch], 0, nframes * sizeof(sample_t));
} else {
size_t actual = rb->read(submaster[ch], nframes);
if (actual < nframes) {
reportFailure(MappedEvent::FailureBussMixUnderrun);
}
for (size_t i = 0; i < nframes; ++i) {
sample_t sample = submaster[ch][i];
if (sample > peak[ch])
peak[ch] = sample;
master[ch][i] += sample;
}
}
}
if (sdb) {
LevelInfo info;
info.level = AudioLevel::multiplier_to_fader
(peak[0], 127, AudioLevel::LongFader);
info.levelRight = AudioLevel::multiplier_to_fader
(peak[1], 127, AudioLevel::LongFader);
sdb->setSubmasterLevel(buss, info);
}
for (InstrumentId id = audioInstrumentBase;
id < audioInstrumentBase + audioInstruments; ++id) {
if (buss + 1 == m_recordInputs[id].input) {
jackProcessRecord(id, nframes, submaster[0], submaster[1], clocksRunning);
}
}
}
#ifdef DEBUG_JACK_PROCESS
std::cerr << "JackDriver::jackProcess: have " << audioInstruments << " audio and " << synthInstruments << " synth instruments and " << bussCount << " busses" << std::endl;
#endif
bool allInstrumentsDormant = true;
static RealTime dormantTime = RealTime::zeroTime;
for (int i = 0; i < audioInstruments + synthInstruments; ++i) {
InstrumentId id;
if (i < audioInstruments)
id = audioInstrumentBase + i;
else
id = synthInstrumentBase + (i - audioInstruments);
if (m_instrumentMixer->isInstrumentEmpty(id))
continue;
sample_t *instrument[2] = { 0, 0 };
sample_t peak[2] = { 0.0, 0.0 };
if (int(m_outputInstruments.size()) > i * 2 + 1) {
instrument[0] = static_cast<sample_t *>
(jack_port_get_buffer(m_outputInstruments[i * 2], nframes));
instrument[1] = static_cast<sample_t *>
(jack_port_get_buffer(m_outputInstruments[i * 2 + 1], nframes));
}
if (!instrument[0])
instrument[0] = m_tempOutBuffer;
if (!instrument[1])
instrument[1] = m_tempOutBuffer;
for (int ch = 0; ch < 2; ++ch) {
// We always need to read from an instrument's ring buffer
// to keep the instrument moving along, as well as for
// monitoring. If the instrument is connected straight to
// the master, then we also need to mix from it. (We have
// that information cached courtesy of updateAudioData.)
bool directToMaster = false;
if (i < audioInstruments) {
directToMaster = (m_directMasterAudioInstruments & (1 << i));
} else {
directToMaster = (m_directMasterSynthInstruments &
(1 << (i - audioInstruments)));
}
#ifdef DEBUG_JACK_PROCESS
if (id == 1000 || id == 10000) {
std::cerr << "JackDriver::jackProcess: instrument id " << id << ", base " << audioInstrumentBase << ", direct masters " << m_directMasterAudioInstruments << ": " << directToMaster << std::endl;
}
#endif
RingBuffer<AudioInstrumentMixer::sample_t, 2> *rb =
m_instrumentMixer->getRingBuffer(id, ch);
if (!rb || m_instrumentMixer->isInstrumentDormant(id)) {
#ifdef DEBUG_JACK_PROCESS
if (id == 1000 || id == 10000) {
if (rb) {
std::cerr << "JackDriver::jackProcess: instrument " << id << " dormant" << std::endl;
} else {
std::cerr << "JackDriver::jackProcess: instrument " << id << " has no ring buffer for channel " << ch << std::endl;
}
}
#endif
if (rb)
rb->skip(nframes);
if (instrument[ch])
memset(instrument[ch], 0, nframes * sizeof(sample_t));
} else {
allInstrumentsDormant = false;
size_t actual = rb->read(instrument[ch], nframes);
#ifdef DEBUG_JACK_PROCESS
if (id == 1000) {
std::cerr << "JackDriver::jackProcess: read " << actual << " of " << nframes << " frames for instrument " << id << " channel " << ch << std::endl;
}
#endif
if (actual < nframes) {
std::cerr << "JackDriver::jackProcess: read " << actual << " of " << nframes << " frames for " << id << " ch " << ch << " (pl " << playing << ", cl " << clocksRunning << ", aa " << asyncAudio << ")" << std::endl;
reportFailure(MappedEvent::FailureMixUnderrun);
}
for (size_t f = 0; f < nframes; ++f) {
sample_t sample = instrument[ch][f];
if (sample > peak[ch])
peak[ch] = sample;
if (directToMaster)
master[ch][f] += sample;
}
}
// If the instrument is connected straight to master we
// also need to skip() on the buss mixer's reader for it,
// otherwise it'll block because the buss mixer isn't
// needing to read it.
if (rb && directToMaster) {
rb->skip(nframes, 1); // 1 is the buss mixer's reader (magic)
}
}
if (sdb) {
LevelInfo info;
info.level = AudioLevel::multiplier_to_fader
(peak[0], 127, AudioLevel::LongFader);
info.levelRight = AudioLevel::multiplier_to_fader
(peak[1], 127, AudioLevel::LongFader);
sdb->setInstrumentLevel(id, info);
}
}
if (asyncAudio) {
if (!allInstrumentsDormant) {
dormantTime = RealTime::zeroTime;
} else {
dormantTime = dormantTime +
RealTime::frame2RealTime(m_bufferSize, m_sampleRate);
if (dormantTime > RealTime(10, 0)) {
std::cerr << "JackDriver: dormantTime = " << dormantTime << ", resetting m_haveAsyncAudioEvent" << std::endl;
m_haveAsyncAudioEvent = false;
}
}
}
// Get master fader levels. There's no pan on the master.
float gain = AudioLevel::dB_to_multiplier(m_masterLevel);
float masterPeak[2] = { 0.0, 0.0 };
for (int ch = 0; ch < 2; ++ch) {
for (size_t i = 0; i < nframes; ++i) {
sample_t sample = master[ch][i] * gain;
if (sample > masterPeak[ch])
masterPeak[ch] = sample;
master[ch][i] = sample;
}
}
if (sdb) {
LevelInfo info;
info.level = AudioLevel::multiplier_to_fader
(masterPeak[0], 127, AudioLevel::LongFader);
info.levelRight = AudioLevel::multiplier_to_fader
(masterPeak[1], 127, AudioLevel::LongFader);
sdb->setMasterLevel(info);
}
for (InstrumentId id = audioInstrumentBase;
id < audioInstrumentBase + audioInstruments; ++id) {
if (m_recordInputs[id].input == 0) {
jackProcessRecord(id, nframes, master[0], master[1], clocksRunning);
} else if (m_recordInputs[id].input < 1000) { // buss, already done
// nothing
} else if (!doneRecord) {
jackProcessRecord(id, nframes, 0, 0, clocksRunning);
}
}
if (playing) {
if (!lowLatencyMode) {
if (m_bussMixer->getBussCount() == 0) {
m_instrumentMixer->signal();
} else {
m_bussMixer->signal();
}
}
}
m_framesProcessed += nframes;
#if (defined(DEBUG_JACK_DRIVER) || defined(DEBUG_JACK_PROCESS) || defined(DEBUG_JACK_TRANSPORT))
framesThisPlay += nframes; //!!!
#endif
#ifdef DEBUG_JACK_PROCESS
std::cerr << "JackDriver::jackProcess: " << nframes << " frames, " << framesThisPlay << " this play, " << m_framesProcessed << " total" << std::endl;
#endif
return 0;
}
int
JackDriver::jackProcessEmpty(jack_nframes_t nframes)
{
sample_t *buffer;
#ifdef DEBUG_JACK_PROCESS
std::cerr << "JackDriver::jackProcessEmpty" << std::endl;
#endif
buffer = static_cast<sample_t *>
(jack_port_get_buffer(m_outputMasters[0], nframes));
if (buffer)
memset(buffer, 0, nframes * sizeof(sample_t));
buffer = static_cast<sample_t *>
(jack_port_get_buffer(m_outputMasters[1], nframes));
if (buffer)
memset(buffer, 0, nframes * sizeof(sample_t));
buffer = static_cast<sample_t *>
(jack_port_get_buffer(m_outputMonitors[0], nframes));
if (buffer)
memset(buffer, 0, nframes * sizeof(sample_t));
buffer = static_cast<sample_t *>
(jack_port_get_buffer(m_outputMonitors[1], nframes));
if (buffer)
memset(buffer, 0, nframes * sizeof(sample_t));
for (unsigned int i = 0; i < m_outputSubmasters.size(); ++i) {
buffer = static_cast<sample_t *>
(jack_port_get_buffer(m_outputSubmasters[i], nframes));
if (buffer)
memset(buffer, 0, nframes * sizeof(sample_t));
}
for (unsigned int i = 0; i < m_outputInstruments.size(); ++i) {
buffer = static_cast<sample_t *>
(jack_port_get_buffer(m_outputInstruments[i], nframes));
if (buffer)
memset(buffer, 0, nframes * sizeof(sample_t));
}
m_framesProcessed += nframes;
#if (defined(DEBUG_JACK_DRIVER) || defined(DEBUG_JACK_PROCESS) || defined(DEBUG_JACK_TRANSPORT))
framesThisPlay += nframes;
#endif
#ifdef DEBUG_JACK_PROCESS
std::cerr << "JackDriver::jackProcess: " << nframes << " frames, " << framesThisPlay << " this play, " << m_framesProcessed << " total" << std::endl;
#endif
return 0;
}
int
JackDriver::jackProcessRecord(InstrumentId id,
jack_nframes_t nframes,
sample_t *sourceBufferLeft,
sample_t *sourceBufferRight,
bool clocksRunning)
{
#ifdef DEBUG_JACK_PROCESS
Profiler profiler("jackProcessRecord", true);
#else
#ifdef DEBUG_JACK_XRUN
Profiler profiler("jackProcessRecord", false);
#endif
#endif
SequencerDataBlock *sdb = m_alsaDriver->getSequencerDataBlock();
bool wroteSomething = false;
sample_t peakLeft = 0.0, peakRight = 0.0;
#ifdef DEBUG_JACK_PROCESS
std::cerr << "JackDriver::jackProcessRecord(" << id << "): clocksRunning " << clocksRunning << std::endl;
#endif
// Get input buffers
//
sample_t *inputBufferLeft = 0, *inputBufferRight = 0;
int recInput = m_recordInputs[id].input;
int channel = m_recordInputs[id].channel;
int channels = (channel == -1 ? 2 : 1);
if (channels == 2)
channel = 0;
float level = m_recordInputs[id].level;
if (sourceBufferLeft) {
#ifdef DEBUG_JACK_PROCESS
std::cerr << "JackDriver::jackProcessRecord(" << id << "): buss input provided" << std::endl;
#endif
inputBufferLeft = sourceBufferLeft;
if (sourceBufferRight)
inputBufferRight = sourceBufferRight;
} else if (recInput < 1000) {
#ifdef DEBUG_JACK_PROCESS
std::cerr << "JackDriver::jackProcessRecord(" << id << "): no known input" << std::endl;
#endif
return 0;
} else {
#ifdef DEBUG_JACK_PROCESS
std::cerr << "JackDriver::jackProcessRecord(" << id << "): record input " << recInput << std::endl;
#endif
int input = recInput - 1000;
int port = input * channels + channel;
int portRight = input * channels + 1;
if (port < int(m_inputPorts.size())) {
inputBufferLeft = static_cast<sample_t*>
(jack_port_get_buffer(m_inputPorts[port], nframes));
}
if (channels == 2 && portRight < int(m_inputPorts.size())) {
inputBufferRight = static_cast<sample_t*>
(jack_port_get_buffer(m_inputPorts[portRight], nframes));
}
}
float gain = AudioLevel::dB_to_multiplier(level);
if (m_alsaDriver->getRecordStatus() == RECORD_ON &&
clocksRunning &&
m_fileWriter->haveRecordFileOpen(id)) {
#ifdef DEBUG_JACK_PROCESS
std::cerr << "JackDriver::jackProcessRecord(" << id << "): recording" << std::endl;
#endif
memset(m_tempOutBuffer, 0, nframes * sizeof(sample_t));
if (inputBufferLeft) {
for (size_t i = 0; i < nframes; ++i) {
sample_t sample = inputBufferLeft[i] * gain;
if (sample > peakLeft)
peakLeft = sample;
m_tempOutBuffer[i] = sample;
}
if (m_outputMonitors.size() > 0) {
sample_t *buf =
static_cast<sample_t *>
(jack_port_get_buffer(m_outputMonitors[0], nframes));
if (buf) {
for (size_t i = 0; i < nframes; ++i) {
buf[i] += m_tempOutBuffer[i];
}
}
}
m_fileWriter->write(id, m_tempOutBuffer, 0, nframes);
}
if (channels == 2) {
if (inputBufferRight) {
for (size_t i = 0; i < nframes; ++i) {
sample_t sample = inputBufferRight[i] * gain;
if (sample > peakRight)
peakRight = sample;
m_tempOutBuffer[i] = sample;
}
if (m_outputMonitors.size() > 1) {
sample_t *buf =
static_cast<sample_t *>
(jack_port_get_buffer(m_outputMonitors[1], nframes));
if (buf) {
for (size_t i = 0; i < nframes; ++i) {
buf[i] += m_tempOutBuffer[i];
}
}
}
}
m_fileWriter->write(id, m_tempOutBuffer, 1, nframes);
}
wroteSomething = true;
} else {
// want peak levels and monitors anyway, even if not recording
#ifdef DEBUG_JACK_PROCESS
std::cerr << "JackDriver::jackProcessRecord(" << id << "): monitoring only" << std::endl;
#endif
if (inputBufferLeft) {
sample_t *buf = 0;
if (m_outputMonitors.size() > 0) {
buf = static_cast<sample_t *>
(jack_port_get_buffer(m_outputMonitors[0], nframes));
}
for (size_t i = 0; i < nframes; ++i) {
sample_t sample = inputBufferLeft[i] * gain;
if (sample > peakLeft)
peakLeft = sample;
if (buf)
buf[i] = sample;
}
if (channels == 2 && inputBufferRight) {
buf = 0;
if (m_outputMonitors.size() > 1) {
buf = static_cast<sample_t *>
(jack_port_get_buffer(m_outputMonitors[1], nframes));
}
for (size_t i = 0; i < nframes; ++i) {
sample_t sample = inputBufferRight[i] * gain;
if (sample > peakRight)
peakRight = sample;
if (buf)
buf[i] = sample;
}
}
}
}
if (channels < 2)
peakRight = peakLeft;
if (sdb) {
LevelInfo info;
info.level = AudioLevel::multiplier_to_fader
(peakLeft, 127, AudioLevel::LongFader);
info.levelRight = AudioLevel::multiplier_to_fader
(peakRight, 127, AudioLevel::LongFader);
sdb->setInstrumentRecordLevel(id, info);
}
if (wroteSomething) {
m_fileWriter->signal();
}
return 0;
}
int
JackDriver::jackSyncCallback(jack_transport_state_t state,
jack_position_t *position,
void *arg)
{
JackDriver *inst = (JackDriver *)arg;
if (!inst)
return true; // or rather, return "huh?"
inst->m_alsaDriver->checkTimerSync(0); // reset, as not processing
if (!inst->m_jackTransportEnabled)
return true; // ignore
ExternalTransport *transport =
inst->m_alsaDriver->getExternalTransportControl();
if (!transport)
return true;
#ifdef DEBUG_JACK_TRANSPORT
std::cerr << "JackDriver::jackSyncCallback: state " << state << " [" << (state == 0 ? "stopped" : state == 1 ? "rolling" : state == 2 ? "looping" : state == 3 ? "starting" : "unknown") << "], frame " << position->frame << ", waiting " << inst->m_waiting << ", playing " << inst->m_alsaDriver->isPlaying() << std::endl;
std::cerr << "JackDriver::jackSyncCallback: m_waitingState " << inst->m_waitingState << ", unique_1 " << position->unique_1 << ", unique_2 " << position->unique_2 << std::endl;
std::cerr << "JackDriver::jackSyncCallback: rate " << position->frame_rate << ", bar " << position->bar << ", beat " << position->beat << ", tick " << position->tick << ", bpm " << position->beats_per_minute << std::endl;
#endif
ExternalTransport::TransportRequest request =
ExternalTransport::TransportNoChange;
if (inst->m_alsaDriver->isPlaying()) {
if (state == JackTransportStarting) {
request = ExternalTransport::TransportJumpToTime;
} else if (state == JackTransportStopped) {
request = ExternalTransport::TransportStop;
}
} else {
if (state == JackTransportStarting) {
request = ExternalTransport::TransportStartAtTime;
} else if (state == JackTransportStopped) {
request = ExternalTransport::TransportNoChange;
}
}
if (!inst->m_waiting || inst->m_waitingState != state) {
if (request == ExternalTransport::TransportJumpToTime ||
request == ExternalTransport::TransportStartAtTime) {
RealTime rt = RealTime::frame2RealTime(position->frame,
position->frame_rate);
#ifdef DEBUG_JACK_TRANSPORT
std::cerr << "JackDriver::jackSyncCallback: Requesting jump to " << rt << std::endl;
#endif
inst->m_waitingToken = transport->transportJump(request, rt);
#ifdef DEBUG_JACK_TRANSPORT
std::cerr << "JackDriver::jackSyncCallback: My token is " << inst->m_waitingToken << std::endl;
#endif
} else if (request == ExternalTransport::TransportStop) {
#ifdef DEBUG_JACK_TRANSPORT
std::cerr << "JackDriver::jackSyncCallback: Requesting state change to " << request << std::endl;
#endif
inst->m_waitingToken = transport->transportChange(request);
#ifdef DEBUG_JACK_TRANSPORT
std::cerr << "JackDriver::jackSyncCallback: My token is " << inst->m_waitingToken << std::endl;
#endif
} else if (request == ExternalTransport::TransportNoChange) {
#ifdef DEBUG_JACK_TRANSPORT
std::cerr << "JackDriver::jackSyncCallback: Requesting no state change!" << std::endl;
#endif
inst->m_waitingToken = transport->transportChange(request);
#ifdef DEBUG_JACK_TRANSPORT
std::cerr << "JackDriver::jackSyncCallback: My token is " << inst->m_waitingToken << std::endl;
#endif
}
inst->m_waiting = true;
inst->m_waitingState = state;
#ifdef DEBUG_JACK_TRANSPORT
std::cerr << "JackDriver::jackSyncCallback: Setting waiting to " << inst->m_waiting << " and waiting state to " << inst->m_waitingState << " (request was " << request << ")" << std::endl;
#endif
return 0;
} else {
if (transport->isTransportSyncComplete(inst->m_waitingToken)) {
#ifdef DEBUG_JACK_TRANSPORT
std::cerr << "JackDriver::jackSyncCallback: Sync complete" << std::endl;
#endif
return 1;
} else {
#ifdef DEBUG_JACK_TRANSPORT
std::cerr << "JackDriver::jackSyncCallback: Sync not complete" << std::endl;
#endif
return 0;
}
}
}
bool
JackDriver::relocateTransportInternal(bool alsoStart)
{
if (!m_client)
return true;
#ifdef DEBUG_JACK_TRANSPORT
const char *fn = (alsoStart ?
"JackDriver::startTransport" :
"JackDriver::relocateTransport");
#endif
#ifdef DEBUG_JACK_TRANSPORT
std::cerr << fn << std::endl;
#else
#ifdef DEBUG_JACK_DRIVER
std::cerr << "JackDriver::relocateTransportInternal" << std::endl;
#endif
#endif
// m_waiting is true if we are waiting for the JACK transport
// to finish a change of state.
if (m_jackTransportEnabled) {
// If on the transport, we never return true here -- instead
// the JACK process calls startClocksApproved() to signal to
// the ALSA driver that it's time to go. But we do use this
// to manage our JACK transport state requests.
// Where did this request come from? Are we just responding
// to an external sync?
ExternalTransport *transport =
m_alsaDriver->getExternalTransportControl();
if (transport) {
if (transport->isTransportSyncComplete(m_waitingToken)) {
// Nope, this came from Rosegarden
#ifdef DEBUG_JACK_TRANSPORT
std::cerr << fn << ": asking JACK transport to start, setting wait state" << std::endl;
#endif
m_waiting = true;
m_waitingState = JackTransportStarting;
long frame = RealTime::realTime2Frame
(m_alsaDriver->getSequencerTime(), m_sampleRate);
if (frame < 0) {
// JACK Transport doesn't support preroll and
// can't set transport position to before zero
// (frame count is unsigned), so there's no very
// satisfactory fix for what to do for count-in
// bars. Let's just start at zero instead.
jack_transport_locate(m_client, 0);
} else {
jack_transport_locate(m_client, frame);
}
if (alsoStart) {
jack_transport_start(m_client);
m_ignoreProcessTransportCount = 1;
} else {
m_ignoreProcessTransportCount = 2;
}
} else {
#ifdef DEBUG_JACK_TRANSPORT
std::cerr << fn << ": waiting already" << std::endl;
#endif
}
}
return false;
}
#if (defined(DEBUG_JACK_DRIVER) || defined(DEBUG_JACK_PROCESS) || defined(DEBUG_JACK_TRANSPORT))
framesThisPlay = 0; //!!!
struct timeval tv;
(void)gettimeofday(&tv, 0);
startTime = RealTime(tv.tv_sec, tv.tv_usec * 1000); //!!!
#endif
#ifdef DEBUG_JACK_TRANSPORT
std::cerr << fn << ": not on JACK transport, accepting right away" << std::endl;
#endif
return true;
}
bool
JackDriver::startTransport()
{
return relocateTransportInternal(true);
}
bool
JackDriver::relocateTransport()
{
return relocateTransportInternal(false);
}
void
JackDriver::stopTransport()
{
if (!m_client)
return ;
std::cerr << "JackDriver::stopTransport: resetting m_haveAsyncAudioEvent" << std::endl;
m_haveAsyncAudioEvent = false;
#ifdef DEBUG_JACK_TRANSPORT
struct timeval tv;
(void)gettimeofday(&tv, 0);
RealTime endTime = RealTime(tv.tv_sec, tv.tv_usec * 1000); //!!!
std::cerr << "\nJackDriver::stop: frames this play: " << framesThisPlay << ", elapsed " << (endTime - startTime) << std::endl;
#endif
if (m_jackTransportEnabled) {
// Where did this request come from? Is this a result of our
// sync to a transport that has in fact already stopped?
ExternalTransport *transport =
m_alsaDriver->getExternalTransportControl();
if (transport) {
if (transport->isTransportSyncComplete(m_waitingToken)) {
// No, we have no outstanding external requests; this
// must have genuinely been requested from within
// Rosegarden, so:
#ifdef DEBUG_JACK_TRANSPORT
std::cerr << "JackDriver::stop: internal request, asking JACK transport to stop" << std::endl;
#endif
jack_transport_stop(m_client);
} else {
// Nothing to do
#ifdef DEBUG_JACK_TRANSPORT
std::cerr << "JackDriver::stop: external request, JACK transport is already stopped" << std::endl;
#endif
}
}
}
if (m_instrumentMixer)
m_instrumentMixer->resetAllPlugins(true); // discard events too
}
// Pick up any change of buffer size
//
int
JackDriver::jackBufferSize(jack_nframes_t nframes, void *arg)
{
JackDriver *inst = static_cast<JackDriver*>(arg);
#ifdef DEBUG_JACK_DRIVER
std::cerr << "JackDriver::jackBufferSize - buffer size changed to "
<< nframes << std::endl;
#endif
inst->m_bufferSize = nframes;
// Recreate our temporary mix buffers to the new size
//
//!!! need buffer size change callbacks on plugins (so long as they
// have internal buffers) and the mix manager, with locks acquired
// appropriately
delete [] inst->m_tempOutBuffer;
inst->m_tempOutBuffer = new sample_t[inst->m_bufferSize];
return 0;
}
// Sample rate change
//
int
JackDriver::jackSampleRate(jack_nframes_t nframes, void *arg)
{
JackDriver *inst = static_cast<JackDriver*>(arg);
#ifdef DEBUG_JACK_DRIVER
std::cerr << "JackDriver::jackSampleRate - sample rate changed to "
<< nframes << std::endl;
#endif
inst->m_sampleRate = nframes;
return 0;
}
void
JackDriver::jackShutdown(void *arg)
{
#ifdef DEBUG_JACK_DRIVER
std::cerr << "JackDriver::jackShutdown() - callback received - "
<< "informing GUI" << std::endl;
#endif
#ifdef DEBUG_JACK_XRUN
std::cerr << "JackDriver::jackShutdown" << std::endl;
Profiles::getInstance()->dump();
#endif
JackDriver *inst = static_cast<JackDriver*>(arg);
inst->m_ok = false;
inst->m_kickedOutAt = time(0);
inst->reportFailure(MappedEvent::FailureJackDied);
}
int
JackDriver::jackXRun(void *arg)
{
#ifdef DEBUG_JACK_DRIVER
std::cerr << "JackDriver::jackXRun" << std::endl;
#endif
#ifdef DEBUG_JACK_XRUN
std::cerr << "JackDriver::jackXRun" << std::endl;
Profiles::getInstance()->dump();
#endif
// Report to GUI
//
JackDriver *inst = static_cast<JackDriver*>(arg);
inst->reportFailure(MappedEvent::FailureXRuns);
return 0;
}
void
JackDriver::restoreIfRestorable()
{
if (m_kickedOutAt == 0)
return ;
if (m_client) {
jack_client_close(m_client);
std::cerr << "closed client" << std::endl;
m_client = 0;
}
time_t now = time(0);
if (now < m_kickedOutAt || now >= m_kickedOutAt + 3) {
if (m_instrumentMixer)
m_instrumentMixer->resetAllPlugins(true);
std::cerr << "reset plugins" << std::endl;
initialise(true);
if (m_ok) {
reportFailure(MappedEvent::FailureJackRestart);
} else {
reportFailure(MappedEvent::FailureJackRestartFailed);
}
m_kickedOutAt = 0;
}
}
void
JackDriver::prepareAudio()
{
if (!m_instrumentMixer)
return ;
// This is used when restarting clocks after repositioning, but
// when not actually playing (yet). We need to do things like
// regenerating the processing buffers here. prebufferAudio()
// also does all of this, but rather more besides.
m_instrumentMixer->allocateBuffers();
m_instrumentMixer->resetAllPlugins(false);
}
void
JackDriver::prebufferAudio()
{
if (!m_instrumentMixer)
return ;
// We want this to happen when repositioning during playback, and
// stopTransport no longer happens then, so we call it from here.
// NB. Don't want to discard events here as this is called after
// pushing events to the soft synth queues at startup
m_instrumentMixer->resetAllPlugins(false);
#ifdef DEBUG_JACK_DRIVER
std::cerr << "JackDriver::prebufferAudio: sequencer time is "
<< m_alsaDriver->getSequencerTime() << std::endl;
#endif
RealTime sliceStart = getNextSliceStart(m_alsaDriver->getSequencerTime());
m_fileReader->fillBuffers(sliceStart);
if (m_bussMixer->getBussCount() > 0) {
m_bussMixer->fillBuffers(sliceStart); // also calls on m_instrumentMixer
} else {
m_instrumentMixer->fillBuffers(sliceStart);
}
}
void
JackDriver::kickAudio()
{
#ifdef DEBUG_JACK_PROCESS
std::cerr << "JackDriver::kickAudio" << std::endl;
#endif
if (m_fileReader)
m_fileReader->kick();
if (m_instrumentMixer)
m_instrumentMixer->kick();
if (m_bussMixer)
m_bussMixer->kick();
if (m_fileWriter)
m_fileWriter->kick();
}
void
JackDriver::updateAudioData()
{
if (!m_ok || !m_client)
return ;
#ifdef DEBUG_JACK_DRIVER
// std::cerr << "JackDriver::updateAudioData starting" << std::endl;
#endif
MappedAudioBuss *mbuss =
m_alsaDriver->getMappedStudio()->getAudioBuss(0);
if (mbuss) {
float level = 0.0;
(void)mbuss->getProperty(MappedAudioBuss::Level, level);
m_masterLevel = level;
}
unsigned long directMasterAudioInstruments = 0L;
unsigned long directMasterSynthInstruments = 0L;
InstrumentId audioInstrumentBase;
int audioInstruments;
m_alsaDriver->getAudioInstrumentNumbers(audioInstrumentBase, audioInstruments);
InstrumentId synthInstrumentBase;
int synthInstruments;
m_alsaDriver->getSoftSynthInstrumentNumbers(synthInstrumentBase, synthInstruments);
RealTime jackLatency = getAudioPlayLatency();
RealTime maxLatency = RealTime::zeroTime;
for (int i = 0; i < audioInstruments + synthInstruments; ++i) {
InstrumentId id;
if (i < audioInstruments)
id = audioInstrumentBase + i;
else
id = synthInstrumentBase + (i - audioInstruments);
MappedAudioFader *fader = m_alsaDriver->getMappedStudio()->getAudioFader(id);
if (!fader)
continue;
float f = 2;
(void)fader->getProperty(MappedAudioFader::Channels, f);
int channels = (int)f;
int inputChannel = -1;
if (channels == 1) {
float f = 0;
(void)fader->getProperty(MappedAudioFader::InputChannel, f);
inputChannel = (int)f;
}
float level = 0.0;
(void)fader->getProperty(MappedAudioFader::FaderRecordLevel, level);
// Like in base/Instrument.h, we use numbers < 1000 to
// mean buss numbers and >= 1000 to mean record ins
// when recording the record input number.
MappedObjectValueList connections = fader->getConnections
(MappedConnectableObject::In);
int input = 1000;
if (connections.empty()) {
std::cerr << "No connections in for record instrument "
<< (id) << " (mapped id " << fader->getId() << ")" << std::endl;
// oh dear.
input = 1000;
} else if (*connections.begin() == mbuss->getId()) {
input = 0;
} else {
MappedObject *obj = m_alsaDriver->getMappedStudio()->
getObjectById(MappedObjectId(*connections.begin()));
if (!obj) {
std::cerr << "No such object as " << *connections.begin() << std::endl;
input = 1000;
} else if (obj->getType() == MappedObject::AudioBuss) {
input = (int)((MappedAudioBuss *)obj)->getBussId();
} else if (obj->getType() == MappedObject::AudioInput) {
input = (int)((MappedAudioInput *)obj)->getInputNumber()
+ 1000;
} else {
std::cerr << "Object " << *connections.begin() << " is not buss or input" << std::endl;
input = 1000;
}
}
if (m_recordInputs[id].input != input) {
std::cerr << "Changing record input for instrument "
<< id << " to " << input << std::endl;
}
m_recordInputs[id] = RecordInputDesc(input, inputChannel, level);
size_t pluginLatency = 0;
bool empty = m_instrumentMixer->isInstrumentEmpty(id);
if (!empty) {
pluginLatency = m_instrumentMixer->getPluginLatency(id);
}
// If we find the object is connected to no output, or to buss
// number 0 (the master), then we set the bit appropriately.
connections = fader->getConnections(MappedConnectableObject::Out);
if (connections.empty() || (*connections.begin() == mbuss->getId())) {
if (i < audioInstruments) {
directMasterAudioInstruments |= (1 << i);
} else {
directMasterSynthInstruments |= (1 << (i - audioInstruments));
}
} else if (!empty) {
pluginLatency +=
m_instrumentMixer->getPluginLatency((unsigned int) * connections.begin());
}
if (empty) {
m_instrumentLatencies[id] = RealTime::zeroTime;
} else {
m_instrumentLatencies[id] = jackLatency +
RealTime::frame2RealTime(pluginLatency, m_sampleRate);
if (m_instrumentLatencies[id] > maxLatency) {
maxLatency = m_instrumentLatencies[id];
}
}
}
m_maxInstrumentLatency = maxLatency;
m_directMasterAudioInstruments = directMasterAudioInstruments;
m_directMasterSynthInstruments = directMasterSynthInstruments;
m_maxInstrumentLatency = maxLatency;
int inputs = m_alsaDriver->getMappedStudio()->
getObjectCount(MappedObject::AudioInput);
if (m_client) {
// this will return with no work if the inputs are already correct:
createRecordInputs(inputs);
}
m_bussMixer->updateInstrumentConnections();
m_instrumentMixer->updateInstrumentMuteStates();
if (m_bussMixer->getBussCount() == 0 || m_alsaDriver->getLowLatencyMode()) {
if (m_bussMixer->running()) {
m_bussMixer->terminate();
}
} else {
if (!m_bussMixer->running()) {
m_bussMixer->run();
}
}
if (m_alsaDriver->getLowLatencyMode()) {
if (m_instrumentMixer->running()) {
m_instrumentMixer->terminate();
}
} else {
if (!m_instrumentMixer->running()) {
m_instrumentMixer->run();
}
}
#ifdef DEBUG_JACK_DRIVER
// std::cerr << "JackDriver::updateAudioData exiting" << std::endl;
#endif
}
void
JackDriver::setAudioBussLevels(int bussNo, float dB, float pan)
{
if (m_bussMixer) {
m_bussMixer->setBussLevels(bussNo, dB, pan);
}
}
void
JackDriver::setAudioInstrumentLevels(InstrumentId instrument, float dB, float pan)
{
if (m_instrumentMixer) {
m_instrumentMixer->setInstrumentLevels(instrument, dB, pan);
}
}
RealTime
JackDriver::getNextSliceStart(const RealTime &now) const
{
jack_nframes_t frame;
bool neg = false;
if (now < RealTime::zeroTime) {
neg = true;
frame = RealTime::realTime2Frame(RealTime::zeroTime - now, m_sampleRate);
} else {
frame = RealTime::realTime2Frame(now, m_sampleRate);
}
jack_nframes_t rounded = frame;
rounded /= m_bufferSize;
rounded *= m_bufferSize;
RealTime roundrt;
if (rounded == frame)
roundrt = RealTime::frame2RealTime(rounded, m_sampleRate);
else if (neg)
roundrt = RealTime::frame2RealTime(rounded - m_bufferSize, m_sampleRate);
else
roundrt = RealTime::frame2RealTime(rounded + m_bufferSize, m_sampleRate);
if (neg)
roundrt = RealTime::zeroTime - roundrt;
return roundrt;
}
int
JackDriver::getAudioQueueLocks()
{
// We have to lock the mixers first, because the mixers can try to
// lock the disk manager from within a locked section -- so if we
// locked the disk manager first we would risk deadlock when
// trying to acquire the instrument mixer lock
int rv = 0;
if (m_bussMixer) {
#ifdef DEBUG_JACK_DRIVER
std::cerr << "JackDriver::getAudioQueueLocks: trying to lock buss mixer" << std::endl;
#endif
rv = m_bussMixer->getLock();
if (rv)
return rv;
}
if (m_instrumentMixer) {
#ifdef DEBUG_JACK_DRIVER
std::cerr << "JackDriver::getAudioQueueLocks: ok, now trying for instrument mixer" << std::endl;
#endif
rv = m_instrumentMixer->getLock();
if (rv)
return rv;
}
if (m_fileReader) {
#ifdef DEBUG_JACK_DRIVER
std::cerr << "JackDriver::getAudioQueueLocks: ok, now trying for disk reader" << std::endl;
#endif
rv = m_fileReader->getLock();
if (rv)
return rv;
}
if (m_fileWriter) {
#ifdef DEBUG_JACK_DRIVER
std::cerr << "JackDriver::getAudioQueueLocks: ok, now trying for disk writer" << std::endl;
#endif
rv = m_fileWriter->getLock();
}
#ifdef DEBUG_JACK_DRIVER
std::cerr << "JackDriver::getAudioQueueLocks: ok" << std::endl;
#endif
return rv;
}
int
JackDriver::tryAudioQueueLocks()
{
int rv = 0;
if (m_bussMixer) {
rv = m_bussMixer->tryLock();
if (rv)
return rv;
}
if (m_instrumentMixer) {
rv = m_instrumentMixer->tryLock();
if (rv) {
if (m_bussMixer) {
m_bussMixer->releaseLock();
}
}
}
if (m_fileReader) {
rv = m_fileReader->tryLock();
if (rv) {
if (m_instrumentMixer) {
m_instrumentMixer->releaseLock();
}
if (m_bussMixer) {
m_bussMixer->releaseLock();
}
}
}
if (m_fileWriter) {
rv = m_fileWriter->tryLock();
if (rv) {
if (m_fileReader) {
m_fileReader->releaseLock();
}
if (m_instrumentMixer) {
m_instrumentMixer->releaseLock();
}
if (m_bussMixer) {
m_bussMixer->releaseLock();
}
}
}
return rv;
}
int
JackDriver::releaseAudioQueueLocks()
{
int rv = 0;
#ifdef DEBUG_JACK_DRIVER
std::cerr << "JackDriver::releaseAudioQueueLocks" << std::endl;
#endif
if (m_fileWriter)
rv = m_fileWriter->releaseLock();
if (m_fileReader)
rv = m_fileReader->releaseLock();
if (m_instrumentMixer)
rv = m_instrumentMixer->releaseLock();
if (m_bussMixer)
rv = m_bussMixer->releaseLock();
return rv;
}
void
JackDriver::setPluginInstance(InstrumentId id, TQString identifier,
int position)
{
if (m_instrumentMixer) {
m_instrumentMixer->setPlugin(id, position, identifier);
}
if (!m_alsaDriver->isPlaying()) {
prebufferAudio(); // to ensure the plugin's ringbuffers are generated
}
}
void
JackDriver::removePluginInstance(InstrumentId id, int position)
{
if (m_instrumentMixer)
m_instrumentMixer->removePlugin(id, position);
}
void
JackDriver::removePluginInstances()
{
if (m_instrumentMixer)
m_instrumentMixer->removeAllPlugins();
}
void
JackDriver::setPluginInstancePortValue(InstrumentId id, int position,
unsigned long portNumber,
float value)
{
if (m_instrumentMixer)
m_instrumentMixer->setPluginPortValue(id, position, portNumber, value);
}
float
JackDriver::getPluginInstancePortValue(InstrumentId id, int position,
unsigned long portNumber)
{
if (m_instrumentMixer)
return m_instrumentMixer->getPluginPortValue(id, position, portNumber);
return 0;
}
void
JackDriver::setPluginInstanceBypass(InstrumentId id, int position, bool value)
{
if (m_instrumentMixer)
m_instrumentMixer->setPluginBypass(id, position, value);
}
TQStringList
JackDriver::getPluginInstancePrograms(InstrumentId id, int position)
{
if (m_instrumentMixer)
return m_instrumentMixer->getPluginPrograms(id, position);
return TQStringList();
}
TQString
JackDriver::getPluginInstanceProgram(InstrumentId id, int position)
{
if (m_instrumentMixer)
return m_instrumentMixer->getPluginProgram(id, position);
return TQString();
}
TQString
JackDriver::getPluginInstanceProgram(InstrumentId id, int position,
int bank, int program)
{
if (m_instrumentMixer)
return m_instrumentMixer->getPluginProgram(id, position, bank, program);
return TQString();
}
unsigned long
JackDriver::getPluginInstanceProgram(InstrumentId id, int position, TQString name)
{
if (m_instrumentMixer)
return m_instrumentMixer->getPluginProgram(id, position, name);
return 0;
}
void
JackDriver::setPluginInstanceProgram(InstrumentId id, int position, TQString program)
{
if (m_instrumentMixer)
m_instrumentMixer->setPluginProgram(id, position, program);
}
TQString
JackDriver::configurePlugin(InstrumentId id, int position, TQString key, TQString value)
{
if (m_instrumentMixer)
return m_instrumentMixer->configurePlugin(id, position, key, value);
return TQString();
}
RunnablePluginInstance *
JackDriver::getSynthPlugin(InstrumentId id)
{
if (m_instrumentMixer)
return m_instrumentMixer->getSynthPlugin(id);
else
return 0;
}
void
JackDriver::clearSynthPluginEvents()
{
if (!m_instrumentMixer) return;
#ifdef DEBUG_JACK_DRIVER
std::cerr << "JackDriver::clearSynthPluginEvents" << std::endl;
#endif
m_instrumentMixer->discardPluginEvents();
}
bool
JackDriver::openRecordFile(InstrumentId id,
const std::string &filename)
{
if (m_fileWriter) {
if (!m_fileWriter->running()) {
m_fileWriter->run();
}
return m_fileWriter->openRecordFile(id, filename);
} else {
std::cerr << "JackDriver::openRecordFile: No file writer available!" << std::endl;
return false;
}
}
bool
JackDriver::closeRecordFile(InstrumentId id,
AudioFileId &returnedId)
{
if (m_fileWriter) {
return m_fileWriter->closeRecordFile(id, returnedId);
if (m_fileWriter->running() && !m_fileWriter->haveRecordFilesOpen()) {
m_fileWriter->terminate();
}
} else
return false;
}
void
JackDriver::reportFailure(MappedEvent::FailureCode code)
{
if (m_alsaDriver)
m_alsaDriver->reportFailure(code);
}
}
#endif // HAVE_LIBJACK
#endif // HAVE_ALSA
|