1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542
|
//=============================================================================
// MusE
// Linux Music Editor
//
// lv2host.cpp
// Copyright (C) 2014 by Deryabin Andrew <andrewderyabin@gmail.com>
// 2017 - Implement LV2_STATE__StateChanged #565 (danvd)
//
// 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; version 2 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//=============================================================================
#include "config.h"
#ifdef LV2_SUPPORT
#define LV2_HOST_CPP
#include <string>
#include <string.h>
#include <signal.h>
#include <dlfcn.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <iostream>
#include <time.h>
#include <dlfcn.h>
#include <QMessageBox>
#include <QDirIterator>
#include <QInputDialog>
#include <QDir>
#include <QFileInfo>
#include <QUrl>
//#include <QX11EmbedWidget>
#include <QCoreApplication>
#include <QtGui/QWindow>
#include <QVBoxLayout>
#include "lv2host.h"
#include "synth.h"
#include "audio.h"
#include "jackaudio.h"
#include "midi.h"
#include "midiport.h"
#include "stringparam.h"
#include "plugin.h"
#include "controlfifo.h"
#include "xml.h"
#include "song.h"
#include "ctrl.h"
#include "minstrument.h"
#include "app.h"
#include "globals.h"
#include "globaldefs.h"
#include "gconfig.h"
#include "widgets/popupmenu.h"
#include "widgets/menutitleitem.h"
#include "icons.h"
#include <ladspa.h>
#include <cmath>
#include <assert.h>
#include <stdio.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sord/sord.h>
// Uncomment to print audio process info.
//#define LV2_DEBUG_PROCESS
// Uncomment to print general info.
// (There is also the CMake option LV2_DEBUG for more output.)
//#define LV2_DEBUG
#ifdef HAVE_GTK2
#include "lv2Gtk2Support/lv2Gtk2Support.h"
#endif
// Define to use GtkPlug instead of GtkWindow for a Gtk plugin gui container.
// This works better than GtkWindow for some plugins.
// For example with GtkWindow, AMSynth fails to embed into the container window
// resulting in two separate windows.
#define LV2_GUI_USE_GTKPLUG ;
namespace MusECore
{
#define NS_EXT "http://lv2plug.in/ns/ext/"
#define NS_LV2CORE "http://lv2plug.in/ns/lv2core"
#define LV2_INSTRUMENT_CLASS NS_LV2CORE "#InstrumentPlugin"
#define LV2_F_BOUNDED_BLOCK_LENGTH LV2_BUF_SIZE__boundedBlockLength
#define LV2_F_FIXED_BLOCK_LENGTH LV2_BUF_SIZE__fixedBlockLength
#define LV2_F_POWER_OF_2_BLOCK_LENGTH LV2_BUF_SIZE__powerOf2BlockLength
#define LV2_P_SEQ_SIZE LV2_BUF_SIZE__sequenceSize
#define LV2_P_MAX_BLKLEN LV2_BUF_SIZE__maxBlockLength
#define LV2_P_MIN_BLKLEN LV2_BUF_SIZE__minBlockLength
#define LV2_P_SAMPLE_RATE LV2_PARAMETERS__sampleRate
#define LV2_F_OPTIONS LV2_OPTIONS__options
#define LV2_F_URID_MAP LV2_URID__map
#define LV2_F_URID_UNMAP LV2_URID__unmap
#define LV2_F_URI_MAP LV2_URI_MAP_URI
#define LV2_F_UI_PARENT LV2_UI__parent
#define LV2_F_INSTANCE_ACCESS NS_EXT "instance-access"
#define LV2_F_DATA_ACCESS LV2_DATA_ACCESS_URI
#define LV2_F_UI_EXTERNAL_HOST LV2_EXTERNAL_UI__Host
#define LV2_F_WORKER_SCHEDULE LV2_WORKER__schedule
#define LV2_F_WORKER_INTERFACE LV2_WORKER__interface
#define LV2_F_UI_IDLE LV2_UI__idleInterface
#define LV2_F_UI_Qt5_UI LV2_UI_PREFIX "Qt5UI"
#define LV2_UI_HOST_URI LV2_F_UI_Qt5_UI
#define LV2_UI_EXTERNAL LV2_EXTERNAL_UI__Widget
#define LV2_UI_EXTERNAL_DEPRECATED LV2_EXTERNAL_UI_DEPRECATED_URI
#define LV2_F_DEFAULT_STATE LV2_STATE_PREFIX "loadDefaultState"
#define LV2_F_STATE_CHANGED LV2_STATE_PREFIX "StateChanged"
static LilvWorld *lilvWorld = 0;
static int uniqueID = 1;
//uri cache structure.
typedef struct
{
LilvNode *atom_AtomPort;
LilvNode *ev_EventPort;
LilvNode *lv2_AudioPort;
LilvNode *lv2_ControlPort;
LilvNode *lv2_InputPort;
LilvNode *lv2_OutputPort;
LilvNode *lv2_connectionOptional;
LilvNode *host_uiType;
LilvNode *ext_uiType;
LilvNode *ext_d_uiType;
LilvNode *lv2_portDiscrete;
LilvNode *lv2_portContinuous;
LilvNode *lv2_portLogarithmic;
LilvNode *lv2_portInteger;
LilvNode *lv2_portTrigger;
LilvNode *lv2_portToggled;
LilvNode *lv2_TimePosition;
LilvNode *lv2_FreeWheelPort;
LilvNode *lv2_SampleRate;
LilvNode *lv2_CVPort;
LilvNode *lv2_psetPreset;
LilvNode *lv2_rdfsLabel;
LilvNode *lv2_actionSavePreset;
LilvNode *lv2_actionUpdatePresets;
LilvNode *end; ///< NULL terminator for easy freeing of entire structure
} CacheNodes;
LV2_URID Synth_Urid_Map(LV2_URID_Unmap_Handle _host_data, const char *uri)
{
LV2Synth *_synth = reinterpret_cast<LV2Synth *>(_host_data);
if(_synth == NULL) //broken plugin
{
return 0;
}
return _synth->mapUrid(uri);
}
const char *Synth_Urid_Unmap(LV2_URID_Unmap_Handle _host_data, LV2_URID id)
{
LV2Synth *_synth = reinterpret_cast<LV2Synth *>(_host_data);
if(_synth == NULL) //broken plugin
{
return NULL;
}
return _synth->unmapUrid(id);
}
LV2_URID Synth_Uri_Map(LV2_URI_Map_Callback_Data _host_data, const char *, const char *uri)
{
LV2Synth *_synth = reinterpret_cast<LV2Synth *>(_host_data);
if(_synth == NULL) //broken plugin
{
return 0;
}
return _synth->mapUrid(uri);
}
static CacheNodes lv2CacheNodes;
LV2_Feature lv2Features [] =
{
{LV2_F_URID_MAP, NULL},
{LV2_F_URID_UNMAP, NULL},
{LV2_F_URI_MAP, NULL},
{LV2_F_BOUNDED_BLOCK_LENGTH, NULL},
{LV2_F_FIXED_BLOCK_LENGTH, NULL},
{LV2_F_POWER_OF_2_BLOCK_LENGTH, NULL},
{LV2_F_UI_PARENT, NULL},
{LV2_F_INSTANCE_ACCESS, NULL},
{LV2_F_UI_EXTERNAL_HOST, NULL},
{LV2_UI_EXTERNAL_DEPRECATED, NULL},
{LV2_F_WORKER_SCHEDULE, NULL},
{LV2_F_UI_IDLE, NULL},
{LV2_F_OPTIONS, NULL},
{LV2_UI__resize, NULL},
{LV2_PROGRAMS__Host, NULL},
{LV2_LOG__log, NULL},
{LV2_STATE__makePath, NULL},
{LV2_STATE__mapPath, NULL},
{LV2_F_STATE_CHANGED, NULL},
{LV2_F_DATA_ACCESS, NULL} //must be the last always!
};
std::vector<LV2Synth *> synthsToFree;
#define SIZEOF_ARRAY(x) sizeof(x)/sizeof(x[0])
void initLV2()
{
#ifdef HAVE_GTK2
//-----------------
// Initialize Gtk
//-----------------
MusEGui::lv2Gtk2Helper_init();
#endif
std::set<std::string> supportedFeatures;
uint32_t i = 0;
if(MusEGlobal::debugMsg)
std::cerr << "LV2: MusE supports these features:" << std::endl;
for(i = 0; i < SIZEOF_ARRAY(lv2Features); i++)
{
supportedFeatures.insert(lv2Features [i].URI);
if(MusEGlobal::debugMsg)
std::cerr << "\t" << lv2Features [i].URI << std::endl;
}
lilvWorld = lilv_world_new();
lv2CacheNodes.atom_AtomPort = lilv_new_uri(lilvWorld, LV2_ATOM__AtomPort);
lv2CacheNodes.ev_EventPort = lilv_new_uri(lilvWorld, LV2_EVENT__EventPort);
lv2CacheNodes.lv2_AudioPort = lilv_new_uri(lilvWorld, LV2_CORE__AudioPort);
lv2CacheNodes.lv2_ControlPort = lilv_new_uri(lilvWorld, LV2_CORE__ControlPort);
lv2CacheNodes.lv2_InputPort = lilv_new_uri(lilvWorld, LV2_CORE__InputPort);
lv2CacheNodes.lv2_OutputPort = lilv_new_uri(lilvWorld, LV2_CORE__OutputPort);
lv2CacheNodes.lv2_connectionOptional = lilv_new_uri(lilvWorld, LV2_CORE__connectionOptional);
lv2CacheNodes.host_uiType = lilv_new_uri(lilvWorld, LV2_UI_HOST_URI);
lv2CacheNodes.ext_uiType = lilv_new_uri(lilvWorld, LV2_UI_EXTERNAL);
lv2CacheNodes.ext_d_uiType = lilv_new_uri(lilvWorld, LV2_UI_EXTERNAL_DEPRECATED);
lv2CacheNodes.lv2_portContinuous = lilv_new_uri(lilvWorld, LV2_PORT_PROPS__continuousCV);
lv2CacheNodes.lv2_portDiscrete = lilv_new_uri(lilvWorld, LV2_PORT_PROPS__discreteCV);
lv2CacheNodes.lv2_portLogarithmic = lilv_new_uri(lilvWorld, LV2_PORT_PROPS__logarithmic);
lv2CacheNodes.lv2_portInteger = lilv_new_uri(lilvWorld, LV2_CORE__integer);
lv2CacheNodes.lv2_portTrigger = lilv_new_uri(lilvWorld, LV2_PORT_PROPS__trigger);
lv2CacheNodes.lv2_portToggled = lilv_new_uri(lilvWorld, LV2_CORE__toggled);
lv2CacheNodes.lv2_TimePosition = lilv_new_uri(lilvWorld, LV2_TIME__Position);
lv2CacheNodes.lv2_FreeWheelPort = lilv_new_uri(lilvWorld, LV2_CORE__freeWheeling);
lv2CacheNodes.lv2_SampleRate = lilv_new_uri(lilvWorld, LV2_CORE__sampleRate);
lv2CacheNodes.lv2_CVPort = lilv_new_uri(lilvWorld, LV2_CORE__CVPort);
lv2CacheNodes.lv2_psetPreset = lilv_new_uri(lilvWorld, LV2_PRESETS__Preset);
lv2CacheNodes.lv2_rdfsLabel = lilv_new_uri(lilvWorld, "http://www.w3.org/2000/01/rdf-schema#label");
lv2CacheNodes.lv2_actionSavePreset = lilv_new_uri(lilvWorld, "http://www.muse-sequencer.org/lv2host#lv2_actionSavePreset");
lv2CacheNodes.lv2_actionUpdatePresets= lilv_new_uri(lilvWorld, "http://www.muse-sequencer.org/lv2host#lv2_actionUpdatePresets");
lv2CacheNodes.end = NULL;
lilv_world_load_all(lilvWorld);
const LilvPlugins *plugins = lilv_world_get_all_plugins(lilvWorld);
LilvIter *pit = lilv_plugins_begin(plugins);
while(true)
{
if(lilv_plugins_is_end(plugins, pit))
{
break;
}
const LilvPlugin *plugin = lilv_plugins_get(plugins, pit);
if(lilv_plugin_is_replaced(plugin))
{
pit = lilv_plugins_next(plugins, pit);
continue;
}
LilvNode *nameNode = lilv_plugin_get_name(plugin);
//const LilvNode *uriNode = lilv_plugin_get_uri(plugin);
if(lilv_node_is_string(nameNode))
{
bool shouldLoad = true;
const char *pluginName = lilv_node_as_string(nameNode);
//const char *pluginUri = lilv_node_as_string(uriNode);
if(MusEGlobal::debugMsg)
std::cerr << "Found LV2 plugin: " << pluginName << std::endl;
// lilv_uri_to_path is deprecated. Use lilv_file_uri_parse instead. Return value must be freed with lilv_free.
const char *lfp = lilv_file_uri_parse(lilv_node_as_string(lilv_plugin_get_library_uri(plugin)), NULL);
if(MusEGlobal::debugMsg)
std::cerr << "Library path: " << lfp << std::endl;
if(MusEGlobal::debugMsg)
{
const LilvPluginClass *cls = lilv_plugin_get_class(plugin);
const LilvNode *ncuri = lilv_plugin_class_get_uri(cls);
const char *clsname = lilv_node_as_uri(ncuri);
std::cerr << "Plugin class: " << clsname << std::endl;
bool isSynth = false;
if(strcmp(clsname, LV2_INSTRUMENT_CLASS) == 0)
{
isSynth = true;
}
if(isSynth)
{
std::cerr << "Plugin is synth" << std::endl;
}
}
#ifdef DEBUG_LV2
std::cerr << "\tRequired features (by uri):" << std::endl;
#endif
LilvNodes *fts = lilv_plugin_get_required_features(plugin);
LilvIter *nit = lilv_nodes_begin(fts);
Plugin::PluginFeatures reqfeat = Plugin::NoFeatures;
while(true)
{
if(lilv_nodes_is_end(fts, nit))
{
break;
}
const LilvNode *fnode = lilv_nodes_get(fts, nit);
const char *uri = lilv_node_as_uri(fnode);
bool isSupported = (supportedFeatures.find(uri) != supportedFeatures.end());
#ifdef DEBUG_LV2
std::cerr << "\t - " << uri << " (" << (isSupported ? "supported" : "not supported") << ")" << std::endl;
#endif
if(isSupported)
{
if(strcmp(uri, LV2_F_FIXED_BLOCK_LENGTH) == 0)
reqfeat |= Plugin::FixedBlockSize;
else if(strcmp(uri, LV2_F_POWER_OF_2_BLOCK_LENGTH) == 0)
reqfeat |= Plugin::PowerOf2BlockSize;
}
else
{
shouldLoad = false;
std::cerr << "\t LV2: " << pluginName << ": Required feature: " << uri << ": not supported!" << std::endl;
}
nit = lilv_nodes_next(fts, nit);
}
lilv_nodes_free(fts);
//if (shouldLoad && isSynth)
if(shouldLoad) //load all plugins for now, not only synths
{
QFileInfo fi(lfp);
QString name = QString(pluginName) + QString(" LV2");
//QString label = QString(pluginUri) + QString("_LV2");
// Make sure it doesn't already exist.
std::vector<Synth *>::iterator is;
for(is = MusEGlobal::synthis.begin(); is != MusEGlobal::synthis.end(); ++is)
{
Synth *s = *is;
if(s->name() == name && s->baseName() == fi.completeBaseName())
{
break;
}
}
if(is == MusEGlobal::synthis.end())
{
LilvNode *nAuthor = lilv_plugin_get_author_name(plugin);
QString author;
if(nAuthor != NULL)
{
author = lilv_node_as_string(nAuthor);
lilv_node_free(nAuthor);
}
LV2Synth *s = new LV2Synth(fi, name, name, author, plugin, reqfeat);
if(s->isConstructed())
{
if((s->isSynth() && s->outPorts() > 0)
|| (s->inPorts() > 0 && s->outPorts() > 0))
//insert plugins with audio ins and outs to synths list too
{
MusEGlobal::synthis.push_back(s);
}
else
{
synthsToFree.push_back(s);
}
if(s->inPorts() > 0 && s->outPorts() > 0) // insert to plugin list
{
MusEGlobal::plugins.push_back(new LV2PluginWrapper(s, reqfeat));
}
}
else
{
delete s;
}
}
}
lilv_free((void*)lfp); // Must free.
}
if(nameNode != NULL)
{
lilv_node_free(nameNode);
}
pit = lilv_plugins_next(plugins, pit);
}
}
void deinitLV2()
{
for(size_t i = 0; i < synthsToFree.size(); i++)
{
delete synthsToFree [i];
}
synthsToFree.clear();
for(LilvNode **n = (LilvNode **)&lv2CacheNodes; *n; ++n)
{
lilv_node_free(*n);
}
#ifdef HAVE_GTK2
MusEGui::lv2Gtk2Helper_deinit();
#endif
lilv_world_free(lilvWorld);
lilvWorld = NULL;
}
void LV2Synth::lv2ui_ExtUi_Closed(LV2UI_Controller contr)
{
LV2PluginWrapper_State *state = (LV2PluginWrapper_State *)contr;
assert(state != NULL); //this shouldn't happen
assert(state->widget != NULL); // this too
assert(state->pluginWindow != NULL);
state->pluginWindow->setClosing(true);
//state->uiTimer->stopNextTime(false);
}
void LV2Synth::lv2ui_SendChangedControls(LV2PluginWrapper_State *state)
{
if(state != NULL && state->uiDesc != NULL && state->uiDesc->port_event != NULL && state->uiInst != NULL)
{
size_t numControls = 0;
MusECore::Port *controls = NULL;
size_t numControlsOut = 0;
MusECore::Port *controlsOut = NULL;
LV2Synth *synth = state->synth;
if(state->plugInst != NULL)
{
numControls = state->plugInst->controlPorts;
controls = state->plugInst->controls;
numControlsOut = state->plugInst->controlOutPorts;
controlsOut = state->plugInst->controlsOut;
}
else if(state->sif != NULL)
{
numControls = state->sif->_inportsControl;
controls = state->sif->_controls;
numControlsOut = state->sif->_outportsControl;
controlsOut = state->sif->_controlsOut;
}
if(numControls > 0)
{
assert(controls != NULL);
}
if(numControlsOut > 0)
{
assert(controlsOut != NULL);
}
for(uint32_t i = 0; i < numControls; ++i)
{
if(state->controlTimers [i] > 0)
{
--state->controlTimers [i];
continue;
}
if(state->controlsMask [i])
{
state->controlsMask [i] = false;
// Force send if re-opening.
if(state->uiIsOpening || state->lastControls [i] != controls [i].val)
{
state->lastControls [i] = controls [i].val;
state->uiDesc->port_event(state->uiInst,
controls [i].idx,
sizeof(float), 0,
&controls [i].val);
}
}
}
for(uint32_t i = 0; i < numControlsOut; ++i)
{
// Force send if re-opening.
if(state->uiIsOpening || state->lastControlsOut [i] != controlsOut [i].val)
{
state->lastControlsOut [i] = controlsOut [i].val;
state->uiDesc->port_event(state->uiInst,
controlsOut [i].idx,
sizeof(float), 0,
&controlsOut [i].val);
}
}
//process gui atom events (control events are already set by getData or apply.
size_t fifoItemSize = state->plugControlEvt.getItemSize();
size_t dataSize = 0;
uint32_t port_index = 0;
char evtBuffer [fifoItemSize];
while(state->plugControlEvt.get(&port_index, &dataSize, evtBuffer))
{
state->uiDesc->port_event(state->uiInst, port_index, dataSize, synth->_uAtom_EventTransfer, evtBuffer);
}
}
}
void LV2Synth::lv2ui_PortWrite(LV2UI_Controller controller, uint32_t port_index, uint32_t buffer_size, uint32_t protocol, void const *buffer)
{
LV2Synth::lv2state_PortWrite(controller, port_index, buffer_size, protocol, buffer, true);
}
void LV2Synth::lv2ui_Touch(LV2UI_Controller /*controller*/, uint32_t /*port_index*/, bool grabbed __attribute__ ((unused)))
{
#ifdef DEBUG_LV2
std::cerr << "LV2Synth::lv2ui_UiTouch: port: %u " << (grabbed ? "grabbed" : "released") << std::endl;
#endif
}
void LV2Synth::lv2state_FillFeatures(LV2PluginWrapper_State *state)
{
uint32_t i;
LV2Synth *synth = state->synth;
LV2_Feature *_ifeatures = state->_ifeatures;
LV2_Feature **_ppifeatures = state->_ppifeatures;
//state->uiTimer = new LV2PluginWrapper_Timer(state);
state->wrkSched.handle = (LV2_Worker_Schedule_Handle)state;
state->wrkSched.schedule_work = LV2Synth::lv2wrk_scheduleWork;
state->wrkIface = NULL;
state->wrkThread = new LV2PluginWrapper_Worker(state);
state->extHost.plugin_human_id = state->human_id = NULL;
state->extHost.ui_closed = LV2Synth::lv2ui_ExtUi_Closed;
state->extData.data_access = NULL;
for(i = 0; i < SIZEOF_ARRAY(lv2Features); i++)
{
_ifeatures [i] = synth->_features [i];
if(_ifeatures [i].URI == NULL)
{
break;
}
if(i == synth->_fInstanceAccess)
{
_ifeatures [i].data = NULL;
}
else if(i == synth->_fExtUiHost)
{
_ifeatures [i].data = &state->extHost;
}
else if(i == synth->_fExtUiHostD)
{
_ifeatures [i].data = &state->extHost;
}
else if(i == synth->_fDataAccess)
{
_ifeatures [i].data = &state->extData;
}
else if(i == synth->_fWrkSchedule)
{
_ifeatures [i].data = &state->wrkSched;
}
else if(i == synth->_fUiResize)
{
_ifeatures [i].data = &state->uiResize;
}
else if(i == synth->_fPrgHost)
{
_ifeatures [i].data = &state->prgHost;
}
else if(i == synth->_fMakePath)
{
_ifeatures [i].data = &state->makePath;
}
else if(i == synth->_fMapPath)
{
_ifeatures [i].data = &state->mapPath;
}
_ppifeatures [i] = &_ifeatures [i];
}
_ppifeatures [i] = NULL;
state->curBpm = 0.0;//double(60000000.0/MusEGlobal::tempomap.tempo(MusEGlobal::song->cpos()));
state->curIsPlaying = MusEGlobal::audio->isPlaying();
state->curFrame = MusEGlobal::audioDevice->getCurFrame();
lv2_atom_forge_init(&state->atomForge, &synth->_lv2_urid_map);
LV2Synth::lv2state_InitMidiPorts(state);
}
void LV2Synth::lv2state_PostInstantiate(LV2PluginWrapper_State *state)
{
LV2Synth *synth = state->synth;
const LV2_Descriptor *descr = lilv_instance_get_descriptor(state->handle);
state->_ifeatures [synth->_fInstanceAccess].data = lilv_instance_get_handle(state->handle);
if(descr->extension_data != NULL)
{
state->extData.data_access = descr->extension_data;
}
else
{
state->_ppifeatures [synth->_fDataAccess] = NULL;
}
state->controlsNameMap.clear();
size_t nCpIn = synth->_controlInPorts.size();
size_t nCpOut = synth->_controlOutPorts.size();
if(nCpIn > 0)
{
state->lastControls = new float [nCpIn];
state->controlsMask = new bool [nCpIn];
state->controlTimers = new int [nCpIn];
for(uint32_t i = 0; i < nCpIn; i++)
{
state->lastControls [i] = synth->_pluginControlsDefault [synth->_controlInPorts [i].index];
state->controlsMask [i] = false;
state->controlTimers [i] = 0;
state->controlsNameMap.insert(std::pair<QString, size_t>(QString(synth->_controlInPorts [i].cName).toLower(), i));
state->controlsSymMap.insert(std::pair<QString, size_t>(QString(synth->_controlInPorts [i].cSym).toLower(), i));
}
}
if(nCpOut > 0)
{
state->lastControlsOut = new float [nCpOut];
for(uint32_t i = 0; i < nCpOut; i++)
{
state->lastControlsOut [i] = synth->_pluginControlsDefault [synth->_controlOutPorts [i].index];
}
}
//fill pointers for CV port types;
uint32_t numAllPorts = lilv_plugin_get_num_ports(synth->_handle);
state->pluginCVPorts = new float *[numAllPorts];
int rv = posix_memalign((void **)&state->pluginCVPorts, 16, sizeof(float *) * numAllPorts);
if(rv != 0)
{
fprintf(stderr, "ERROR: LV2Synth::lv2state_PostInstantiate: posix_memalign returned error:%d. Aborting!\n", rv);
abort();
}
memset(state->pluginCVPorts, 0, sizeof(float *) * numAllPorts);
for(size_t i = 0; i < synth->_controlInPorts.size(); ++i)
{
if(synth->_controlInPorts [i].isCVPort)
{
size_t idx = synth->_controlInPorts [i].index;
rv = posix_memalign((void **)&state->pluginCVPorts [idx], 16, sizeof(float) * MusEGlobal::segmentSize);
if(rv != 0)
{
fprintf(stderr, "ERROR: LV2Synth::lv2state_PostInstantiate: posix_memalign returned error:%d. Aborting!\n", rv);
abort();
}
for(size_t k = 0; k < MusEGlobal::segmentSize; ++k)
{
state->pluginCVPorts [idx] [k] = synth->_controlInPorts [i].defVal;
}
lilv_instance_connect_port(state->handle, idx, state->pluginCVPorts [idx]);
}
}
for(size_t i = 0; i < synth->_controlOutPorts.size(); ++i)
{
if(synth->_controlOutPorts [i].isCVPort)
{
size_t idx = synth->_controlOutPorts [i].index;
rv = posix_memalign((void **)&state->pluginCVPorts [idx], 16, sizeof(float) * MusEGlobal::segmentSize);
if(rv != 0)
{
fprintf(stderr, "ERROR: LV2Synth::lv2state_PostInstantiate: posix_memalign returned error:%d. Aborting!\n", rv);
abort();
}
for(size_t k = 0; k < MusEGlobal::segmentSize; ++k)
{
state->pluginCVPorts [idx] [k] = synth->_controlOutPorts [i].defVal;
}
lilv_instance_connect_port(state->handle, idx, state->pluginCVPorts [idx]);
}
}
for(size_t i = 0; i < state->midiInPorts.size(); i++)
{
lilv_instance_connect_port(state->handle, state->midiInPorts [i].index, (void *)state->midiInPorts [i].buffer->getRawBuffer());
}
for(size_t i = 0; i < state->midiOutPorts.size(); i++)
{
lilv_instance_connect_port(state->handle, state->midiOutPorts [i].index, (void *)state->midiOutPorts [i].buffer->getRawBuffer());
}
//query for state interface
state->iState = (LV2_State_Interface *)lilv_instance_get_extension_data(state->handle, LV2_STATE__interface);
//query for LV2Worker interface
state->wrkIface = (LV2_Worker_Interface *)lilv_instance_get_extension_data(state->handle, LV2_F_WORKER_INTERFACE);
//query for programs interface
state->prgIface = (LV2_Programs_Interface *)lilv_instance_get_extension_data(state->handle, LV2_PROGRAMSNEW__Interface);
if(state->prgIface != NULL)
{
state->newPrgIface = true;
}
else
{
state->newPrgIface = false;
state->prgIface = (LV2_Programs_Interface *)lilv_instance_get_extension_data(state->handle, LV2_PROGRAMS__Interface);
}
LV2Synth::lv2prg_updatePrograms(state);
state->wrkThread->start(QThread::LowPriority);
}
void LV2Synth::lv2ui_FreeDescriptors(LV2PluginWrapper_State *state)
{
if(state->uiDesc != NULL && state->uiInst != NULL)
state->uiDesc->cleanup(state->uiInst);
state->uiInst = *(void **)(&state->uiDesc) = NULL;
#ifdef HAVE_GTK2
if(state->gtk2Plug != NULL)
MusEGui::lv2Gtk2Helper_gtk_widget_destroy(state->gtk2Plug);
#endif
state->gtk2Plug = NULL;
if(state->uiDlHandle != NULL)
{
dlclose(state->uiDlHandle);
state->uiDlHandle = NULL;
}
}
void LV2Synth::lv2state_FreeState(LV2PluginWrapper_State *state)
{
assert(state != NULL);
state->wrkThread->setClosing();
state->wrkThread->wait();
delete state->wrkThread;
if(state->human_id != NULL)
free(state->human_id);
if(state->lastControls)
{
delete [] state->lastControls;
state->lastControls = NULL;
}
if(state->controlsMask)
{
delete [] state->controlsMask;
state->controlsMask = NULL;
}
if(state->controlTimers)
{
delete [] state->controlTimers;
state->controlTimers = NULL;
}
if(state->lastControlsOut)
{
delete [] state->lastControlsOut;
state->lastControlsOut = NULL;
}
LV2Synth::lv2ui_FreeDescriptors(state);
if(state->handle != NULL)
{
lilv_instance_free(state->handle);
state->handle = NULL;
}
delete state;
}
void LV2Synth::lv2audio_SendTransport(LV2PluginWrapper_State *state, LV2EvBuf *buffer, unsigned long nsamp)
{
//send transport events if any
LV2Synth *synth = state->synth;
unsigned int cur_frame = MusEGlobal::audio->pos().frame();
Pos p(MusEGlobal::extSyncFlag.value() ? MusEGlobal::audio->tickPos() : cur_frame, MusEGlobal::extSyncFlag.value() ? true : false);
double curBpm = (60000000.0 / MusEGlobal::tempomap.tempo(p.tick())) * double(MusEGlobal::tempomap.globalTempo())/100.0;
bool curIsPlaying = MusEGlobal::audio->isPlaying();
unsigned int curFrame = MusEGlobal::audioDevice->getCurFrame();
// if(state->curFrame != curFrame
// || state->curIsPlaying != curIsPlaying
// || state->curBpm != curBpm)
// {
//send transport/tempo changes always
//as some plugins revert to default settings when not playing
state->curFrame = curFrame;
state->curIsPlaying = curIsPlaying;
state->curBpm = curBpm;
uint8_t pos_buf[1024];
memset(pos_buf, 0, sizeof(pos_buf));
LV2_Atom* lv2_pos = (LV2_Atom*)pos_buf;
/* Build an LV2 position object to report change to plugin */
LV2_Atom_Forge* atomForge = &state->atomForge;
lv2_atom_forge_set_buffer(atomForge, pos_buf, sizeof(pos_buf));
LV2_Atom_Forge_Frame frame;
lv2_atom_forge_object(atomForge, &frame, 1, synth->_uTime_Position);
lv2_atom_forge_key(atomForge, synth->_uTime_frame);
lv2_atom_forge_long(atomForge, curFrame);
lv2_atom_forge_key(atomForge, synth->_uTime_speed);
lv2_atom_forge_float(atomForge, curIsPlaying ? 1.0 : 0.0);
lv2_atom_forge_key(atomForge, synth->_uTime_beatsPerMinute);
lv2_atom_forge_float(atomForge, (float)curBpm);
buffer->write(nsamp, 0, lv2_pos->type, lv2_pos->size, (const uint8_t *)LV2_ATOM_BODY(lv2_pos));
}
void LV2Synth::lv2state_InitMidiPorts(LV2PluginWrapper_State *state)
{
LV2Synth *synth = state->synth;
state->midiInPorts = synth->_midiInPorts;
state->midiOutPorts = synth->_midiOutPorts;
state->inPortsMidi= state->midiInPorts.size();
state->outPortsMidi = state->midiOutPorts.size();
//connect midi and control ports
for(size_t i = 0; i < state->midiInPorts.size(); i++)
{
LV2EvBuf *newEvBuffer = new LV2EvBuf(true, state->midiInPorts [i].old_api, synth->_uAtom_Sequence, synth->_uAtom_Chunk);
if(!newEvBuffer)
{
abort();
}
state->midiInPorts [i].buffer = newEvBuffer;
state->idx2EvtPorts.insert(std::make_pair(state->midiInPorts [i].index, newEvBuffer));
}
for(size_t i = 0; i < state->midiOutPorts.size(); i++)
{
LV2EvBuf *newEvBuffer = new LV2EvBuf(false, state->midiOutPorts [i].old_api, synth->_uAtom_Sequence, synth->_uAtom_Chunk);
if(!newEvBuffer)
{
abort();
}
state->midiOutPorts [i].buffer = newEvBuffer;
state->idx2EvtPorts.insert(std::make_pair(state->midiOutPorts [i].index, newEvBuffer));
}
}
void LV2Synth::lv2audio_preProcessMidiPorts(LV2PluginWrapper_State *state, unsigned long nsamp)
{
for(size_t j = 0; j < state->inPortsMidi; j++)
{
state->midiInPorts [j].buffer->resetBuffer();
}
for(size_t j = 0; j < state->outPortsMidi; j++)
{
state->midiOutPorts [j].buffer->resetBuffer();
}
if(state->inPortsMidi > 0)
{
LV2EvBuf *rawMidiBuffer = state->midiInPorts [0].buffer;
if(state->midiInPorts [0].supportsTimePos)
{
//send transport events if any
LV2Synth::lv2audio_SendTransport(state, rawMidiBuffer, nsamp);
}
}
//process gui atom events (control events are already set by getData or apply call).
size_t fifoItemSize = state->uiControlEvt.getItemSize();
size_t dataSize = 0;
uint32_t port_index = 0;
char evtBuffer [fifoItemSize];
while(state->uiControlEvt.get(&port_index, &dataSize, evtBuffer))
{
std::map<uint32_t, LV2EvBuf *>::iterator it = state->idx2EvtPorts.find(port_index);
if(it != state->idx2EvtPorts.end())
{
LV2EvBuf *buffer = it->second;
const LV2_Atom* const atom = (const LV2_Atom*)evtBuffer;
buffer->write(nsamp, 0, atom->type, atom->size, static_cast<const uint8_t *>(LV2_ATOM_BODY_CONST(atom)));
}
}
}
void LV2Synth::lv2audio_postProcessMidiPorts(LV2PluginWrapper_State *state, unsigned long)
{
//send Atom events to gui.
//Synchronize send rate with gui update rate
size_t fifoItemSize = state->plugControlEvt.getItemSize();
size_t outp = state->midiOutPorts.size();
for(size_t j = 0; j < outp; j++)
{
if(!state->midiOutPorts [j].old_api)
{
do
{
uint32_t frames, subframes, type, size;
uint8_t *data = NULL;
if(!state->midiOutPorts [j].buffer->read(&frames, &subframes, &type, &size, &data))
{
break;
}
if(type == state->synth->_uAtom_Object)
{
const LV2_Atom_Object_Body *aObjBody = reinterpret_cast<LV2_Atom_Object_Body *>(data);
if(aObjBody->otype == state->synth->_uAtom_StateChanged)
{
//Just make song status dirty (pending event) - something had changed in the plugin controls
state->songDirtyPending = true;
}
}
if(state->uiInst == NULL)
{
continue;
}
unsigned char atom_data [fifoItemSize];
LV2_Atom *atom_evt = reinterpret_cast<LV2_Atom *>(atom_data);
atom_evt->type = type;
atom_evt->size = size;
if(fifoItemSize - sizeof(LV2_Atom) < size)
{
#ifdef DEBUG_LV2
std::cerr << "LV2Synth::lv2audio_postProcessMidiPorts(): Plugin event data is bigger than rt fifo item size. Skipping." << std::endl;
#endif
continue;
}
unsigned char *evt = static_cast<unsigned char *>(LV2_ATOM_BODY(atom_evt));
memcpy(evt, data, size);
state->plugControlEvt.put(state->midiOutPorts [j].index, sizeof(LV2_Atom) + size, atom_evt);
}
while(true);
}
}
}
void LV2Synth::lv2ui_PostShow(LV2PluginWrapper_State *state)
{
assert(state->pluginWindow != NULL);
assert(state->uiDesc != NULL);
assert(state->uiInst != NULL);
if(state->uiDesc->port_event != NULL)
{
uint32_t numControls = 0;
Port *controls = NULL;
if(state->plugInst != NULL)
{
numControls = state->plugInst->controlPorts;
controls = state->plugInst->controls;
}
else if(state->sif != NULL)
{
numControls = state->sif->_inportsControl;
controls = state->sif->_controls;
}
if(numControls > 0)
{
assert(controls != NULL);
}
for(uint32_t i = 0; i < numControls; ++i)
{
state->uiDesc->port_event(state->uiInst,
controls [i].idx,
sizeof(float), 0,
&controls [i].val);
}
}
// Set the flag to tell the update timer to force sending all controls and program.
state->uiIsOpening = true;
state->pluginWindow->startNextTime();
}
int LV2Synth::lv2ui_Resize(LV2UI_Feature_Handle handle, int width, int height)
{
LV2PluginWrapper_State *state = (LV2PluginWrapper_State *)handle;
if(state->widget != NULL && state->hasGui)
{
((LV2PluginWrapper_Window *)state->widget)->resize(width, height);
QWidget *ewWin = ((LV2PluginWrapper_Window *)state->widget)->findChild<QWidget *>();
if(ewWin != NULL)
{
ewWin->resize(width, height);
}
else
{
#ifdef LV2_GUI_USE_QWIDGET
// TODO Check this, maybe wrong widget, maybe need the one contained by it?
QWidget *ewCent= ((LV2PluginWrapper_Window *)state->widget);
#else
QWidget *ewCent= ((LV2PluginWrapper_Window *)state->widget)->centralWidget();
#endif
if(ewCent != NULL)
{
ewCent->resize(width, height);
}
}
state->uiX11Size.setWidth(width);
state->uiX11Size.setHeight(height);
return 0;
}
return 1;
}
void LV2Synth::lv2ui_Gtk2AllocateCb(int width, int height, void *arg)
{
LV2PluginWrapper_State *state = (LV2PluginWrapper_State *)arg;
if(state == NULL)
return;
if(!state->gtk2AllocateCompleted && state->widget != NULL && state->hasGui && state->gtk2Plug != NULL)
{
state->gtk2AllocateCompleted = true;
((LV2PluginWrapper_Window *)state->widget)->setMinimumSize(width, height);
}
}
void LV2Synth::lv2ui_Gtk2ResizeCb(int width, int height, void *arg)
{
LV2PluginWrapper_State *state = (LV2PluginWrapper_State *)arg;
if(state == NULL)
return;
if(!state->gtk2ResizeCompleted && state->widget != NULL && state->hasGui && state->gtk2Plug != NULL)
{
state->gtk2ResizeCompleted = true;
((LV2PluginWrapper_Window *)state->widget)->resize(width, height);
}
}
void LV2Synth::lv2ui_ShowNativeGui(LV2PluginWrapper_State *state, bool bShow)
{
LV2Synth* synth = state->synth;
LV2PluginWrapper_Window *win = NULL;
if(synth->_pluginUiTypes.size() == 0)
return;
//state->uiTimer->stopNextTime();
if(state->pluginWindow != NULL)
state->pluginWindow->stopNextTime();
if(!bShow)
return;
LV2_PLUGIN_UI_TYPES::iterator itUi;
if((state->uiCurrent == NULL) || MusEGlobal::config.lv2UiBehavior == MusEGlobal::CONF_LV2_UI_ASK_ALWAYS)
{
state->uiCurrent = NULL;
state->gtk2ResizeCompleted = false;
state->gtk2AllocateCompleted = false;
QAction *aUiTypeSelected = NULL;
if((synth->_pluginUiTypes.size() == 1) || MusEGlobal::config.lv2UiBehavior == MusEGlobal::CONF_LV2_UI_USE_FIRST)
{
state->uiCurrent = synth->_pluginUiTypes.begin()->first;
}
else
{
QMenu mGuisPopup;
MusEGui::MenuTitleItem *aUiTypeHeader = new MusEGui::MenuTitleItem(QObject::tr("Select gui type"), NULL);
aUiTypeHeader->setEnabled(false);
QFont fHeader;
fHeader.setBold(true);
fHeader.setUnderline(true);
aUiTypeHeader->setFont(fHeader);
mGuisPopup.addAction(aUiTypeHeader);
for(itUi = synth->_pluginUiTypes.begin(); itUi != synth->_pluginUiTypes.end(); itUi++)
{
const LilvUI *selectedUi = itUi->first;
const LilvNode *pluginUiType = itUi->second.second;
QAction *aUiType = new QAction(QString(lilv_node_as_string(pluginUiType)), NULL);
aUiType->setData(QVariant(reinterpret_cast<qlonglong>(selectedUi)));
mGuisPopup.addAction(aUiType);
}
aUiTypeSelected = mGuisPopup.exec(QCursor::pos());
if(aUiTypeSelected == NULL)
{
return;
}
state->uiCurrent = reinterpret_cast<const LilvUI *>(aUiTypeSelected->data().toLongLong());
}
}
itUi = synth->_pluginUiTypes.find(state->uiCurrent);
assert(itUi != synth->_pluginUiTypes.end());
const LilvUI *selectedUi = itUi->first;
bool bExtUi = itUi->second.first;
const LilvNode *pluginUiType = itUi->second.second;
state->uiIdleIface = NULL;
if(bExtUi)
{
state->hasGui = false;
state->hasExternalGui = true;
}
else
{
state->hasGui = true;
state->hasExternalGui = false;
}
#ifdef LV2_GUI_USE_QWIDGET
win = new LV2PluginWrapper_Window(state, Q_NULLPTR, Qt::Window);
#else
win = new LV2PluginWrapper_Window(state);
#endif
state->uiX11Size.setWidth(0);
state->uiX11Size.setHeight(0);
if(win != NULL)
{
state->widget = win;
state->pluginWindow = win;
const char *cUiUri = lilv_node_as_uri(pluginUiType);
const char *cUiTypeUri = lilv_node_as_uri(lilv_ui_get_uri(selectedUi));
bool bEmbed = false;
bool bGtk = false;
QWidget *ewWin = NULL;
QWindow *x11QtWindow = NULL;
state->gtk2Plug = NULL;
state->_ifeatures [synth->_fUiParent].data = NULL;
if(strcmp(LV2_UI__X11UI, cUiUri) == 0)
{
bEmbed = true;
ewWin = new QWidget();
#ifdef LV2_GUI_USE_QWIDGET
QVBoxLayout* layout = new QVBoxLayout();
layout->setMargin(0);
layout->setSpacing(0);
layout->addWidget(ewWin);
win->setLayout(layout);
#else
win->setCentralWidget(ewWin);
#endif
state->_ifeatures [synth->_fUiParent].data = (void*)ewWin->winId();
}
else if(strcmp(LV2_UI__GtkUI, cUiUri) == 0)
{
#ifndef HAVE_GTK2
win->stopNextTime();
return;
#else
bEmbed = true;
bGtk = true;
#ifdef LV2_GUI_USE_GTKPLUG
state->gtk2Plug = MusEGui::lv2Gtk2Helper_gtk_plug_new(0, state);
#else
state->gtk2Plug = MusEGui::lv2Gtk2Helper_gtk_window_new(state);
#endif
MusEGui::lv2Gtk2Helper_register_allocate_cb(static_cast<void *>(state->gtk2Plug), lv2ui_Gtk2AllocateCb);
MusEGui::lv2Gtk2Helper_register_resize_cb(static_cast<void *>(state->gtk2Plug), lv2ui_Gtk2ResizeCb);
#endif
}
else if(strcmp(LV2_F_UI_Qt5_UI, cUiUri) == 0) //Qt5 uis are handled natively
{
state->_ifeatures [synth->_fUiParent].data = win;
}
else //external uis
{
state->_ifeatures [synth->_fUiParent].data = NULL;
}
//now open ui library file
// lilv_uri_to_path is deprecated. Use lilv_file_uri_parse instead. Return value must be freed with lilv_free.
const char *uiPath = lilv_file_uri_parse(lilv_node_as_uri(lilv_ui_get_binary_uri(selectedUi)), NULL);
// REMOVE Tim. LV2. Changed. TESTING. RESTORE. Qt4 versions of synthv1,drumk,? crashes on Qt5.
// TESTED: On my system it gets much farther into the call now, dozens of Qt4 calls into it,
// but ultimately still ends up crashing on a call to dlopen libkdecore.5 for some reason.
// state->uiDlHandle = dlopen(uiPath, RTLD_NOW);
//state->uiDlHandle = dlmopen(LM_ID_NEWLM, uiPath, RTLD_LAZY | RTLD_DEEPBIND); // Just a test
state->uiDlHandle = dlopen(uiPath, RTLD_NOW | RTLD_DEEPBIND);
lilv_free((void*)uiPath); // Must free.
if(state->uiDlHandle == NULL)
{
win->stopNextTime();
return;
}
//find lv2 ui descriptor function and call it to get ui descriptor struct
LV2UI_DescriptorFunction lv2fUiDesc;
*(void **)(&lv2fUiDesc) = dlsym(state->uiDlHandle, "lv2ui_descriptor");
if(lv2fUiDesc == NULL)
{
win->stopNextTime();
return;
}
state->uiDesc = NULL;
for(uint32_t i = 0; ;++i)
{
state->uiDesc = lv2fUiDesc(i);
if(state->uiDesc == NULL)
break;
if(strcmp(state->uiDesc->URI, cUiTypeUri) == 0) //found selected ui
break;
}
if(state->uiDesc == NULL)
{
win->stopNextTime();
return;
}
void *uiW = NULL;
// lilv_uri_to_path is deprecated. Use lilv_file_uri_parse instead. Return value must be freed with lilv_free.
const char* bundle_path = lilv_file_uri_parse(lilv_node_as_uri(lilv_ui_get_bundle_uri(selectedUi)), NULL);
state->uiInst = state->uiDesc->instantiate(state->uiDesc,
lilv_node_as_uri(lilv_plugin_get_uri(synth->_handle)),
bundle_path,
LV2Synth::lv2ui_PortWrite,
state,
&uiW,
state->_ppifeatures);
lilv_free((void*)bundle_path); // Must free.
if(state->uiInst != NULL)
{
state->uiIdleIface = NULL;
state->uiPrgIface = NULL;
if(state->uiDesc->extension_data != NULL)
{
state->uiIdleIface = (LV2UI_Idle_Interface *)state->uiDesc->extension_data(LV2_F_UI_IDLE);
state->uiPrgIface = (LV2_Programs_UI_Interface *)state->uiDesc->extension_data(LV2_PROGRAMSNEW__UIInterface);
if(state->uiPrgIface != NULL)
{
state->newPrgIface = true;
}
else
{
state->newPrgIface = false;
state->uiPrgIface = (LV2_Programs_UI_Interface *)state->uiDesc->extension_data(LV2_PROGRAMS__UIInterface);
}
}
if(state->hasGui)
{
if(!bEmbed)
{
#ifdef LV2_GUI_USE_QWIDGET
QVBoxLayout* layout = new QVBoxLayout();
layout->setMargin(0);
layout->setSpacing(0);
layout->addWidget(static_cast<QWidget *>(uiW));
win->setLayout(layout);
#else
win->setCentralWidget(static_cast<QWidget *>(uiW));
#endif
}
else
{
if(bGtk)
{
#ifdef HAVE_GTK2
MusEGui::lv2Gtk2Helper_gtk_container_add(state->gtk2Plug, uiW);
MusEGui::lv2Gtk2Helper_gtk_widget_show_all(state->gtk2Plug);
#ifdef LV2_GUI_USE_GTKPLUG
unsigned long plugX11Id = MusEGui::lv2Gtk2Helper_gdk_x11_drawable_get_xid(state->gtk2Plug);
#else
unsigned long plugX11Id = MusEGui::lv2Gtk2Helper_gtk_window_get_xid(state->gtk2Plug);
#endif
x11QtWindow = QWindow::fromWinId(plugX11Id);
ewWin = QWidget::createWindowContainer(x11QtWindow, win);
state->pluginQWindow = x11QtWindow;
#ifdef LV2_GUI_USE_QWIDGET
QVBoxLayout* layout = new QVBoxLayout();
layout->setMargin(0);
layout->setSpacing(0);
layout->addWidget(ewWin);
win->setLayout(layout);
#else
win->setCentralWidget(ewWin);
#endif
if(state->uiX11Size.width() == 0 || state->uiX11Size.height() == 0)
{
int w = 0;
int h = 0;
MusEGui::lv2Gtk2Helper_gtk_widget_get_allocation(uiW, &w, &h);
win->setMinimumSize(w, h);
win->resize(w, h);
}
#endif
}
else
{
if(state->uiX11Size.width() == 0 || state->uiX11Size.height() == 0)
win->resize(ewWin->size());
}
}
win->show();
win->setWindowTitle(state->extHost.plugin_human_id);
}
else if(state->hasExternalGui)
{
state->widget = uiW;
LV2_EXTERNAL_UI_SHOW((LV2_External_UI_Widget *)state->widget);
}
LV2Synth::lv2ui_PostShow(state);
return;
}
}
if(win != NULL)
{
win->stopNextTime();
}
state->pluginWindow = NULL;
state->widget = NULL;
state->uiCurrent = NULL;
//no ui is shown
state->hasExternalGui = state->hasGui = false;
}
const void *LV2Synth::lv2state_stateRetreive(LV2_State_Handle handle, uint32_t key, size_t *size, uint32_t *type, uint32_t *flags)
{
QMap<QString, QPair<QString, QVariant> >::const_iterator it;
LV2PluginWrapper_State *state = (LV2PluginWrapper_State *)handle;
LV2Synth *synth = state->synth;
const char *cKey = synth->unmapUrid(key);
assert(cKey != NULL); //should'n happen
QString strKey = QString(cKey);
it = state->iStateValues.find(strKey);
if(it != state->iStateValues.end())
{
if(it.value().second.type() == QVariant::ByteArray)
{
QString sType = it.value().first;
QByteArray arrType = sType.toUtf8();
*type = synth->mapUrid(arrType.constData());
*flags = LV2_STATE_IS_POD;
QByteArray valArr = it.value().second.toByteArray();
if(sType.compare(QString(LV2_ATOM__Path)) == 0) //prepend project folder to abstract path
{
QString plugFolder = ((state->sif != NULL) ? state->sif->name() : state->plugInst->name()) + QString("/");
QString strPath = QString::fromUtf8(valArr.data());
QFile ff(strPath);
QFileInfo fiPath(ff);
if(fiPath.isRelative())
{
if(strPath.indexOf(plugFolder) < 0)
{
strPath = plugFolder + strPath;
}
strPath = MusEGlobal::museProject + QString("/") + strPath;
valArr = strPath.toUtf8();
int len = strPath.length();
valArr.setRawData(strPath.toUtf8().constData(), len + 1);
valArr [len] = 0;
}
}
size_t i;
size_t numValues = state->numStateValues;
for(i = 0; i < numValues; ++i)
{
if(state->tmpValues [i] == NULL)
break;
}
assert(i < numValues); //sanity check
size_t sz = valArr.size();
state->iStateValues.remove(strKey);
if(sz > 0)
{
state->tmpValues [i] = new char [sz];
memset(state->tmpValues [i], 0, sz);
memcpy(state->tmpValues [i], valArr.constData(), sz);
*size = sz;
return state->tmpValues [i];
}
}
}
return NULL;
}
LV2_State_Status LV2Synth::lv2state_stateStore(LV2_State_Handle handle, uint32_t key, const void *value, size_t size, uint32_t type, uint32_t flags)
{
if(flags & (LV2_STATE_IS_POD | LV2_STATE_IS_PORTABLE))
{
LV2PluginWrapper_State *state = (LV2PluginWrapper_State *)handle;
LV2Synth *synth = state->synth;
const char *uriKey = synth->unmapUrid(key);
const char *uriType = synth->unmapUrid(type);
assert(uriType != NULL && uriKey != NULL); //FIXME: buggy plugin or uridBiMap realization?
QString strKey = QString(uriKey);
QMap<QString, QPair<QString, QVariant> >::const_iterator it = state->iStateValues.find(strKey);
if(it == state->iStateValues.end())
{
QString strUriType = uriType;
QVariant varVal = QByteArray((const char *)value, size);
state->iStateValues.insert(strKey, QPair<QString, QVariant>(strUriType, varVal));
}
return LV2_STATE_SUCCESS;
}
return LV2_STATE_ERR_BAD_FLAGS;
}
LV2_Worker_Status LV2Synth::lv2wrk_scheduleWork(LV2_Worker_Schedule_Handle handle, uint32_t size, const void *data)
{
#ifdef DEBUG_LV2
std::cerr << "LV2Synth::lv2wrk_scheduleWork" << std::endl;
#endif
LV2PluginWrapper_State *state = (LV2PluginWrapper_State *)handle;
//assert(state->wrkEndWork != true);
if(state->wrkEndWork != false)
return LV2_WORKER_ERR_UNKNOWN;
state->wrkDataSize = size;
state->wrkDataBuffer = data;
if(MusEGlobal::audio->freewheel()) //don't wait for a thread. Do it now
state->wrkThread->makeWork();
else
return state->wrkThread->scheduleWork();
return LV2_WORKER_SUCCESS;
}
LV2_Worker_Status LV2Synth::lv2wrk_respond(LV2_Worker_Respond_Handle handle, uint32_t size, const void *data)
{
LV2PluginWrapper_State *state = (LV2PluginWrapper_State *)handle;
state->wrkDataSize = size;
state->wrkDataBuffer = data;
state->wrkEndWork = true;
return LV2_WORKER_SUCCESS;
}
void LV2Synth::lv2conf_write(LV2PluginWrapper_State *state, int level, Xml &xml)
{
state->iStateValues.clear();
state->numStateValues = 0;
if(state->iState != NULL)
{
state->iState->save(lilv_instance_get_handle(state->handle), LV2Synth::lv2state_stateStore, state, LV2_STATE_IS_POD, state->_ppifeatures);
}
if(state->sif != NULL) // write control ports values only for synths
{
for(size_t c = 0; c < state->sif->_inportsControl; c++)
{
state->iStateValues.insert(QString(state->sif->_controlInPorts [c].cName), QPair<QString, QVariant>(QString(""), QVariant((double)state->sif->_controls[c].val)));
}
}
if(state->uiCurrent != NULL)
{
const char *cUiUri = lilv_node_as_uri(lilv_ui_get_uri(state->uiCurrent));
state->iStateValues.insert(QString(cUiUri), QPair<QString, QVariant>(QString(""), QVariant(QString(cUiUri))));
}
QByteArray arrOut;
QDataStream streamOut(&arrOut, QIODevice::WriteOnly);
streamOut << state->iStateValues;
QByteArray outEnc64 = arrOut.toBase64();
QString customData(outEnc64);
for (int pos=0; pos < customData.size(); pos+=150)
{
customData.insert(pos++,'\n'); // add newlines for readability
}
xml.strTag(level, "customData", customData);
}
void LV2Synth::lv2conf_set(LV2PluginWrapper_State *state, const std::vector<QString> &customParams)
{
if(customParams.size() == 0)
return;
state->iStateValues.clear();
for(size_t i = 0; i < customParams.size(); i++)
{
QString param = customParams [i];
param.remove('\n'); // remove all linebreaks that may have been added to prettyprint the songs file
QByteArray paramIn;
paramIn.append(param);
QByteArray dec64 = QByteArray::fromBase64(paramIn);
QDataStream streamIn(&dec64, QIODevice::ReadOnly);
streamIn >> state->iStateValues;
break; //one customData tag includes all data in base64
}
size_t numValues = state->iStateValues.size();
state->numStateValues = numValues;
if(state->iState != NULL && numValues > 0)
{
state->tmpValues = new char*[numValues];;
memset(state->tmpValues, 0, numValues * sizeof(char *));
state->iState->restore(lilv_instance_get_handle(state->handle), LV2Synth::lv2state_stateRetreive, state, 0, state->_ppifeatures);
for(size_t i = 0; i < numValues; ++i)
{
if(state->tmpValues [i] != NULL)
delete [] state->tmpValues [i];
}
delete [] state->tmpValues;
state->tmpValues = NULL;
}
QMap<QString, QPair<QString, QVariant> >::const_iterator it;
for(it = state->iStateValues.begin(); it != state->iStateValues.end(); ++it)
{
QString name = it.key();
QVariant qVal = it.value().second;
if(!name.isEmpty() && qVal.isValid())
{
if(qVal.type() == QVariant::String) // plugin ui uri
{
QString sUiUri = qVal.toString();
LV2_PLUGIN_UI_TYPES::iterator it;
for(it = state->synth->_pluginUiTypes.begin(); it != state->synth->_pluginUiTypes.end(); ++it)
{
if(sUiUri == QString(lilv_node_as_uri(lilv_ui_get_uri(it->first))))
{
state->uiCurrent = it->first;
break;
}
}
}
else
{
if(state->sif != NULL) //setting control value only for synths
{
bool ok = false;
float val = (float)qVal.toDouble(&ok);
if(ok)
{
std::map<QString, size_t>::iterator it = state->controlsNameMap.find(name.toLower());
if(it != state->controlsNameMap.end())
{
size_t ctrlNum = it->second;
state->sif->_controls [ctrlNum].val = state->sif->_controls [ctrlNum].tmpVal = val;
}
}
}
}
}
}
}
unsigned LV2Synth::lv2ui_IsSupported(const char *, const char *ui_type_uri)
{
if(strcmp(LV2_F_UI_Qt5_UI, ui_type_uri) == 0
|| (strcmp(LV2_UI__GtkUI, ui_type_uri) == 0)
|| strcmp(LV2_UI__X11UI, ui_type_uri) == 0)
{
return 1;
}
return 0;
}
void LV2Synth::lv2prg_updatePrograms(LV2PluginWrapper_State *state)
{
assert(state != NULL);
state->index2prg.clear();
state->prg2index.clear();
if(state->prgIface != NULL)
{
uint32_t iPrg = 0;
const LV2_Program_Descriptor *pDescr = NULL;
while((pDescr = state->prgIface->get_program(
lilv_instance_get_handle(state->handle), iPrg)) != NULL)
{
// 16384 banks arranged as 128 hi and lo banks each with up to the first 128 programs supported.
uint32_t hb = pDescr->bank >> 8;
uint32_t lb = pDescr->bank & 0xff;
if(hb < 128 && lb < 128 && pDescr->program < 128)
{
lv2ExtProgram extPrg;
extPrg.index = iPrg;
extPrg.bank = pDescr->bank;
extPrg.prog = pDescr->program;
extPrg.useIndex = true;
extPrg.name = QString(pDescr->name);
state->index2prg.insert(std::make_pair(iPrg, extPrg));
hb &= 0x7f;
lb &= 0x7f;
uint32_t midiprg = (hb << 16) + (lb << 8) + extPrg.prog;
state->prg2index.insert(std::make_pair(midiprg, iPrg));
}
++iPrg;
}
}
}
int LV2Synth::lv2_printf(LV2_Log_Handle handle, LV2_URID type, const char *fmt, ...)
{
va_list argptr;
va_start(argptr, fmt);
int ret = LV2Synth::lv2_vprintf(handle, type, fmt, argptr);
va_end(argptr);
return ret;
}
int LV2Synth::lv2_vprintf(LV2_Log_Handle, LV2_URID, const char *fmt, va_list ap)
{
return vprintf(fmt, ap);
}
char *LV2Synth::lv2state_makePath(LV2_State_Make_Path_Handle handle, const char *path)
{
LV2PluginWrapper_State *state = (LV2PluginWrapper_State *)handle;
assert(state != NULL);
QFile ff(path);
QFileInfo fiPath(ff);
if(fiPath.isAbsolute())
{
return strdup(path);
}
QString plugName = (state->sif != NULL) ? state->sif->name() : state->plugInst->name();
QString dirName = MusEGlobal::museProject + QString("/") + plugName;
QDir dir;
dir.mkpath(dirName);
QString resPath = dirName + QString("/") + QString(path);
return strdup(resPath.toUtf8().constData());
}
char *LV2Synth::lv2state_abstractPath(LV2_State_Map_Path_Handle handle, const char *absolute_path)
{
LV2PluginWrapper_State *state = (LV2PluginWrapper_State *)handle;
assert(state != NULL);
//some plugins do not support abstract paths properly,
//so return duplicate without modification for now
//return strdup(absolute_path);
QString resPath = QString(absolute_path);
int rIdx = resPath.lastIndexOf('/');
if(rIdx > -1)
{
resPath = resPath.mid(rIdx + 1);
}
QString plugName = (state->sif != NULL) ? state->sif->name() : state->plugInst->name();
QDir dir;
QString prjPath = MusEGlobal::museProject + QString("/") + plugName;
dir.mkpath(prjPath);
QFile ff(absolute_path);
QFileInfo fiPath(ff);
if(resPath.length() && fiPath.isAbsolute() && resPath != QString(absolute_path))
{
QFile::link(QString(absolute_path), prjPath + QString("/") + resPath);
}
if(strlen(absolute_path) == 0)
{
resPath = prjPath + QString("/") + resPath;
}
return strdup(resPath.toUtf8().constData());
}
char *LV2Synth::lv2state_absolutePath(LV2_State_Map_Path_Handle handle, const char *abstract_path)
{
return LV2Synth::lv2state_makePath((LV2_State_Make_Path_Handle)handle, abstract_path);
}
void LV2Synth::lv2state_populatePresetsMenu(LV2PluginWrapper_State *state, MusEGui::PopupMenu *menu)
{
menu->clear();
menu->setIcon(QIcon(*MusEGui::presetsNewIcon));
LV2Synth *synth = state->synth;
//this is good by slow down menu population.
//So it's called only on changes (preset save/manual update)
//LV2Synth::lv2state_UnloadLoadPresets(synth, true);
MusEGui::MenuTitleItem *actPresetActionsHeader = new MusEGui::MenuTitleItem(QObject::tr("Preset actions"), menu);
menu->addAction(actPresetActionsHeader);
QAction *actSave = menu->addAction(QObject::tr("Save preset..."));
actSave->setObjectName("lv2state_presets_save_action");
actSave->setData(QVariant::fromValue<void *>(static_cast<void *>(lv2CacheNodes.lv2_actionSavePreset)));
QAction *actUpdate = menu->addAction(QObject::tr("Update list"));
actUpdate->setObjectName("lv2state_presets_update_action");
actUpdate->setData(QVariant::fromValue<void *>(static_cast<void *>(lv2CacheNodes.lv2_actionUpdatePresets)));
std::map<QString, LilvNode *>::iterator it;
MusEGui::MenuTitleItem *actSavedPresetsHeader = new MusEGui::MenuTitleItem(QObject::tr("Saved presets"), menu);
menu->addAction(actSavedPresetsHeader);
for(it = synth->_presets.begin(); it != synth->_presets.end(); ++it)
{
QAction *act = menu->addAction(it->first);
act->setData(QVariant::fromValue<void *>(static_cast<void *>((it->second))));
}
if(menu->actions().size() == 0)
{
QAction *act = menu->addAction(QObject::tr("No presets found"));
act->setDisabled(true);
act->setData(QVariant::fromValue<void *>(NULL));
}
}
void LV2Synth::lv2state_PortWrite(LV2UI_Controller controller, uint32_t port_index, uint32_t buffer_size, uint32_t protocol, const void *buffer, bool fromUi)
{
LV2PluginWrapper_State *state = (LV2PluginWrapper_State *)controller;
assert(state != NULL); //this shouldn't happen
assert(state->inst != NULL || state->sif != NULL); // this too
if(protocol != 0 && protocol != state->synth->_uAtom_EventTransfer)
{
#ifdef DEBUG_LV2
std::cerr << "LV2Synth::lv2state_PortWrite: unsupported protocol (" << protocol << ")" << std::endl;
#endif
return;
}
if(protocol == state->synth->_uAtom_EventTransfer) //put atom transfers to dedicated ring buffer
{
#ifdef DEBUG_LV2
std::cerr << "LV2Synth::lv2state_PortWrite: atom_EventTransfer, port = " << port_index << ", size =" << buffer_size << std::endl;
#endif
state->uiControlEvt.put(port_index, buffer_size, buffer);
return;
}
std::map<uint32_t, uint32_t>::iterator it = state->synth->_idxToControlMap.find(port_index);
if(it == state->synth->_idxToControlMap.end())
{
#ifdef DEBUG_LV2
std::cerr << "LV2Synth::lv2state_PortWrite: wrong port index (" << port_index << ")" << std::endl;
#endif
return;
}
uint32_t cport = it->second;
float value = *(float *)buffer;
// Schedules a timed control change:
ControlEvent ce;
ce.unique = false;
ce.fromGui = fromUi; // It came from the plugin's own GUI (or not).
ce.idx = cport;
ce.value = value;
// Don't use timestamp(), because it's circular, which is making it impossible to deal
// with 'modulo' events which slip in 'under the wire' before processing the ring buffers.
ce.frame = MusEGlobal::audio->curFrame();
ControlFifo *_controlFifo = NULL;
if(state->inst != NULL)
{
_controlFifo = &state->plugInst->_controlFifo;
if(fromUi)
{
// Record automation:
// Take care of this immediately, because we don't want the silly delay associated with
// processing the fifo one-at-a-time in the apply().
// NOTE: With some vsts we don't receive control events until the user RELEASES a control.
// So the events all arrive at once when the user releases a control.
// That makes this pretty useless... But what the heck...
//AutomationType at = AUTO_OFF;
if(state->plugInst->_track && state->plugInst->_id != -1)
{
unsigned long id = genACnum(state->plugInst->_id, cport);
state->plugInst->_track->recordAutomation(id, value);
//at = state->plugInst->_track->automationType();
}
//state->plugInst->enableController(cport, false);
}
}
else if(state->sif != NULL)
{
_controlFifo = &state->sif->_controlFifo;
if(fromUi)
{
// Record automation:
// Take care of this immediately, because we don't want the silly delay associated with
// processing the fifo one-at-a-time in the apply().
// NOTE: With some vsts we don't receive control events until the user RELEASES a control.
// So the events all arrive at once when the user releases a control.
// That makes this pretty useless... But what the heck...
if(state->sif->id() != -1)
{
unsigned long pid = genACnum(state->sif->id(), cport);
state->sif->synti->recordAutomation(pid, value);
}
//state->sif->enableController(cport, false);
}
}
if(fromUi)
{
state->controlTimers [cport] = 1000 / 30; // 1 sec controllers will not be send to guis
}
assert(_controlFifo != NULL);
if(_controlFifo->put(ce))
std::cerr << "LV2Synth::lv2state_PortWrite: fifo overflow: in control number:" << cport << std::endl;
#ifdef DEBUG_LV2
std::cerr << "LV2Synth::lv2state_PortWrite: port=" << cport << "(" << port_index << ")" << ", val=" << value << std::endl;
#endif
}
void LV2Synth::lv2state_setPortValue(const char *port_symbol, void *user_data, const void *value, uint32_t size, uint32_t type)
{
LV2PluginWrapper_State *state = (LV2PluginWrapper_State *)user_data;
assert(state != NULL);
std::map<QString, size_t>::iterator it = state->controlsSymMap.find(QString::fromUtf8(port_symbol).toLower());
if(it != state->controlsSymMap.end())
{
size_t ctrlNum = it->second;
uint32_t ctrlIdx = state->synth->_controlInPorts [ctrlNum].index;
float fvalue;
if (type == state->atomForge.Float)
{
fvalue = *(const float*)value;
}
else if (type == state->atomForge.Double)
{
fvalue = *(const double*)value;
}
else if (type == state->atomForge.Int)
{
fvalue = *(const int32_t*)value;
}
else if (type == state->atomForge.Long)
{
fvalue = *(const int64_t*)value;
}
else
{
fprintf(stderr, "error: Preset `%s' value has bad type <%s>\n",
port_symbol, state->synth->uridBiMap.unmap(type));
return;
}
LV2Synth::lv2state_PortWrite((LV2UI_Controller)user_data, ctrlIdx, size, 0, &fvalue, false);
}
}
const void *LV2Synth::lv2state_getPortValue(const char *port_symbol, void *user_data, uint32_t *size, uint32_t *type)
{
LV2PluginWrapper_State *state = (LV2PluginWrapper_State *)user_data;
assert(state != NULL);
std::map<QString, size_t>::iterator it = state->controlsSymMap.find(QString::fromUtf8(port_symbol).toLower());
*size = *type = 0;
if(it != state->controlsSymMap.end())
{
size_t ctrlNum = it->second;
MusECore::Port *controls = NULL;
if(state->plugInst != NULL)
{
controls = state->plugInst->controls;
}
else if(state->sif != NULL)
{
controls = state->sif->_controls;
}
if(controls != NULL)
{
*size = sizeof(float);
*type = state->atomForge.Float;
return &controls [ctrlNum].val;
}
}
return NULL;
}
void LV2Synth::lv2state_applyPreset(LV2PluginWrapper_State *state, LilvNode *preset)
{
//handle special actions first
if(preset == lv2CacheNodes.lv2_actionSavePreset)
{
bool isOk = false;
QString presetName = QInputDialog::getText(MusEGlobal::muse, QObject::tr("Enter new preset name"),
QObject::tr(("Preset name:")), QLineEdit::Normal,
QString(""), &isOk);
if(isOk && !presetName.isEmpty())
{
presetName = presetName.trimmed();
QString synthName = state->synth->name().replace(' ', '_');
QString presetDir = MusEGlobal::museUser + QString("/.lv2/")
+ synthName + QString("_")
+ presetName + QString(".lv2/");
QString presetFile = synthName + QString("_") + presetName
+ QString(".ttl");
QString plugName = (state->sif != NULL) ? state->sif->name() : state->plugInst->name();
QString plugFileDirName = MusEGlobal::museProject + QString("/") + plugName;
char *cPresetName = strdup(presetName.toUtf8().constData());
char *cPresetDir = strdup(presetDir.toUtf8().constData());
char *cPresetFile = strdup(presetFile.toUtf8().constData());
char *cPlugFileDirName = strdup(plugFileDirName.toUtf8().constData());
LilvState* const lilvState = lilv_state_new_from_instance(state->synth->_handle, state->handle, &state->synth->_lv2_urid_map,
cPlugFileDirName, cPresetDir, cPresetDir, cPresetDir,
LV2Synth::lv2state_getPortValue, state,
LV2_STATE_IS_POD | LV2_STATE_IS_PORTABLE, NULL);
lilv_state_set_label(lilvState, cPresetName);
lilv_state_save(lilvWorld, &state->synth->_lv2_urid_map,
&state->synth->_lv2_urid_unmap,
lilvState, NULL, cPresetDir,
cPresetFile);
lilv_state_free(lilvState);
free(cPresetName);
free(cPresetDir);
free(cPresetFile);
free(cPlugFileDirName);
LV2Synth::lv2state_UnloadLoadPresets(state->synth, true, true);
}
return;
}
else if(preset == lv2CacheNodes.lv2_actionUpdatePresets)
{
LV2Synth::lv2state_UnloadLoadPresets(state->synth, true, true);
return;
}
LilvState* lilvState = lilv_state_new_from_world(lilvWorld, &state->synth->_lv2_urid_map, preset);
if(lilvState)
{
lilv_state_restore(lilvState, state->handle, LV2Synth::lv2state_setPortValue, state, 0, NULL);
lilv_state_free(lilvState);
}
}
void LV2Synth::lv2state_UnloadLoadPresets(LV2Synth *synth, bool load, bool update)
{
assert(synth != NULL);
//std::cerr << "LV2Synth::lv2state_UnloadLoadPresets: handling <" << synth->_name.toStdString() << ">" << std::endl;
std::map<QString, LilvNode *>::iterator it;
for(it = synth->_presets.begin(); it != synth->_presets.end(); ++it)
{
lilv_world_unload_resource(lilvWorld, it->second);
lilv_node_free(it->second);
}
synth->_presets.clear();
if(load)
{
if(update)
{
//rescan and refresh user-defined presets first
QDirIterator dir_it(MusEGlobal::museUser + QString("/.lv2"), QStringList() << "*.lv2", QDir::Dirs, QDirIterator::NoIteratorFlags);
while (dir_it.hasNext())
{
QString nextDir = dir_it.next() + QString("/");
std::cerr << nextDir.toStdString() << std::endl;
SerdNode sdir = serd_node_new_file_uri((const uint8_t*)nextDir.toUtf8().constData(), 0, 0, 0);
LilvNode* ldir = lilv_new_uri(lilvWorld, (const char*)sdir.buf);
lilv_world_unload_bundle(lilvWorld, ldir);
lilv_world_load_bundle(lilvWorld, ldir);
serd_node_free(&sdir);
lilv_node_free(ldir);
}
}
//scan for preserts
LilvNodes* presets = lilv_plugin_get_related(synth->_handle, lv2CacheNodes.lv2_psetPreset);
LILV_FOREACH(nodes, i, presets)
{
const LilvNode* preset = lilv_nodes_get(presets, i);
#ifdef DEBUG_LV2
std::cerr << "\tPreset: " << lilv_node_as_uri(preset) << std::endl;
#endif
lilv_world_load_resource(lilvWorld, preset);
LilvNodes* pLabels = lilv_world_find_nodes(lilvWorld, preset, lv2CacheNodes.lv2_rdfsLabel, NULL);
if (pLabels != NULL)
{
const LilvNode* pLabel = lilv_nodes_get_first(pLabels);
synth->_presets.insert(std::make_pair(lilv_node_as_string(pLabel), lilv_node_duplicate(preset)));
lilv_nodes_free(pLabels);
}
else
{
#ifdef DEBUG_LV2
std::cerr << "\t\tPreset <%s> has no rdfs:label" << lilv_node_as_string(lilv_nodes_get(presets, i)) << std::endl;
#endif
}
}
lilv_nodes_free(presets);
}
}
void LV2SynthIF::lv2prg_Changed(LV2_Programs_Handle handle, int32_t index)
{
LV2PluginWrapper_State *state = (LV2PluginWrapper_State *)handle;
#ifdef DEBUG_LV2
std::cerr << "LV2Synth::lv2prg_Changed: index: " << index << std::endl;
#endif
if(state->sif && state->sif->synti)
{
std::map<uint32_t, lv2ExtProgram>::iterator itIndex = state->index2prg.find(index);
if(itIndex == state->index2prg.end())
return;
int ch = 0;
int port = state->sif->synti->midiPort();
const lv2ExtProgram &extPrg = itIndex->second;
uint32_t hb = extPrg.bank >> 8;
uint32_t lb = extPrg.bank & 0xff;
if(hb > 127 || lb > 127 || extPrg.prog > 127)
return;
hb &= 0x7f;
lb &= 0x7f;
state->sif->synti->setCurrentProg(ch, extPrg.prog, lb, hb);
const int rv = (hb << 16) | (lb << 8) | extPrg.prog;
if(port != -1)
{
MidiPlayEvent event(0, port, ch, MusECore::ME_CONTROLLER, MusECore::CTRL_PROGRAM, rv);
//MusEGlobal::midiPorts[port].sendEvent(event);
MusEGlobal::midiPorts[port].sendHwCtrlState(event, false);
if(state->sif->id() != -1 && state->sif->_controls != NULL)
{
for(unsigned long k = 0; k < state->sif->_inportsControl; ++k)
{
state->sif->synti->setPluginCtrlVal(genACnum(state->sif->id(), k), state->sif->_controls[k].val);
}
}
}
}
}
LV2Synth::LV2Synth(const QFileInfo &fi, QString label, QString name, QString author, const LilvPlugin *_plugin, Plugin::PluginFeatures reqFeatures)
: Synth(fi, label, name, author, QString(""), reqFeatures),
_handle(_plugin),
_features(NULL),
_ppfeatures(NULL),
_options(NULL),
_isSynth(false),
_uis(NULL),
_hasFreeWheelPort(false),
_isConstructed(false),
_pluginControlsDefault(NULL),
_pluginControlsMin(NULL),
_pluginControlsMax(NULL)
{
//fake id for LV2PluginWrapper functionality
_uniqueID = uniqueID++;
_midi_event_id = mapUrid(LV2_MIDI__MidiEvent);
_uTime_Position = mapUrid(LV2_TIME__Position);
_uTime_frame = mapUrid(LV2_TIME__frame);
_uTime_speed = mapUrid(LV2_TIME__speed);
_uTime_beatsPerMinute = mapUrid(LV2_TIME__beatsPerMinute);
_uTime_barBeat = mapUrid(LV2_TIME__barBeat);
_uAtom_EventTransfer = mapUrid(LV2_ATOM__eventTransfer);
_uAtom_Chunk = mapUrid(LV2_ATOM__Chunk);
_uAtom_Sequence = mapUrid(LV2_ATOM__Sequence);
_uAtom_StateChanged = mapUrid(LV2_F_STATE_CHANGED);
_uAtom_Object = mapUrid(LV2_ATOM__Object);
_sampleRate = (double)MusEGlobal::sampleRate;
_fSampleRate = (float)MusEGlobal::sampleRate;
//prepare features and options arrays
LV2_Options_Option _tmpl_options [] =
{
{LV2_OPTIONS_INSTANCE, 0, uridBiMap.map(LV2_P_SAMPLE_RATE), sizeof(float), uridBiMap.map(LV2_ATOM__Float), &_fSampleRate},
{LV2_OPTIONS_INSTANCE, 0, uridBiMap.map(LV2_P_MIN_BLKLEN), sizeof(int32_t), uridBiMap.map(LV2_ATOM__Int), &MusEGlobal::segmentSize},
{LV2_OPTIONS_INSTANCE, 0, uridBiMap.map(LV2_P_MAX_BLKLEN), sizeof(int32_t), uridBiMap.map(LV2_ATOM__Int), &MusEGlobal::segmentSize},
{LV2_OPTIONS_INSTANCE, 0, uridBiMap.map(LV2_P_SEQ_SIZE), sizeof(int32_t), uridBiMap.map(LV2_ATOM__Int), &MusEGlobal::segmentSize},
{LV2_OPTIONS_INSTANCE, 0, uridBiMap.map(LV2_CORE__sampleRate), sizeof(double), uridBiMap.map(LV2_ATOM__Double), &_sampleRate},
{LV2_OPTIONS_INSTANCE, 0, 0, 0, 0, NULL}
};
_options = new LV2_Options_Option[SIZEOF_ARRAY(_tmpl_options)]; // last option is NULLs
for(uint32_t i = 0; i < SIZEOF_ARRAY(_tmpl_options); i++)
{
_options [i] = _tmpl_options [i];
}
_features = new LV2_Feature[SIZEOF_ARRAY(lv2Features)];
_ppfeatures = new LV2_Feature *[SIZEOF_ARRAY(lv2Features) + 1];
_lv2_urid_map.map = Synth_Urid_Map;
_lv2_urid_map.handle = this;
_lv2_urid_unmap.unmap = Synth_Urid_Unmap;
_lv2_urid_unmap.handle = this;
_lv2_uri_map.uri_to_id = Synth_Uri_Map;
_lv2_uri_map.callback_data = this;
_lv2_log_log.handle = this;
_lv2_log_log.printf = LV2Synth::lv2_printf;
_lv2_log_log.vprintf = LV2Synth::lv2_vprintf;
uint32_t i;
for(i = 0; i < SIZEOF_ARRAY(lv2Features); i++)
{
_features [i] = lv2Features [i];
if(_features [i].URI == NULL)
{
break;
}
if(std::string(LV2_F_URID_MAP) == _features [i].URI)
{
_features [i].data = &_lv2_urid_map;
}
else if(std::string(LV2_F_URID_UNMAP) == _features [i].URI)
{
_features [i].data = &_lv2_urid_unmap;
}
else if(std::string(LV2_F_URI_MAP) == _features [i].URI)
{
_features [i].data = &_lv2_uri_map;
}
else if(std::string(LV2_F_OPTIONS) == _features [i].URI)
{
_features [i].data = _options;
}
else if(std::string(LV2_F_INSTANCE_ACCESS) == _features [i].URI)
{
_fInstanceAccess = i;
}
else if(std::string(LV2_F_UI_PARENT) == _features [i].URI)
{
_fUiParent = i;
}
else if((std::string(LV2_F_UI_EXTERNAL_HOST) == _features [i].URI))
{
_fExtUiHost = i;
}
else if((std::string(LV2_UI_EXTERNAL_DEPRECATED) == _features [i].URI))
{
_fExtUiHostD = i;
}
else if((std::string(LV2_F_WORKER_SCHEDULE) == _features [i].URI))
{
_fWrkSchedule = i;
}
else if((std::string(LV2_UI__resize) == _features [i].URI))
{
_fUiResize = i;
}
else if((std::string(LV2_PROGRAMS__Host) == _features [i].URI))
{
_fPrgHost = i;
}
else if((std::string(LV2_LOG__log) == _features [i].URI))
{
_features [i].data = &_lv2_log_log;
}
else if((std::string(LV2_STATE__makePath) == _features [i].URI))
{
_fMakePath = i;
}
else if((std::string(LV2_STATE__mapPath) == _features [i].URI))
{
_fMapPath = i;
}
else if(std::string(LV2_F_DATA_ACCESS) == _features [i].URI)
{
_fDataAccess = i; //must be the last!
}
_ppfeatures [i] = &_features [i];
}
_ppfeatures [i] = 0;
//enum plugin ports;
uint32_t numPorts = lilv_plugin_get_num_ports(_handle);
const LilvPort *lilvFreeWheelPort = lilv_plugin_get_port_by_designation(_handle, lv2CacheNodes.lv2_InputPort, lv2CacheNodes.lv2_FreeWheelPort);
_pluginControlsDefault = new float [numPorts];
_pluginControlsMin = new float [numPorts];
_pluginControlsMax = new float [numPorts];
memset(_pluginControlsDefault, 0, sizeof(float) * numPorts);
memset(_pluginControlsMin, 0, sizeof(float) * numPorts);
memset(_pluginControlsMax, 0, sizeof(float) * numPorts);
lilv_plugin_get_port_ranges_float(_handle, _pluginControlsMin, _pluginControlsMax, _pluginControlsDefault);
for(uint32_t i = 0; i < numPorts; i++)
{
const LilvPort *_port = lilv_plugin_get_port_by_index(_handle, i);
LilvNode *_nPname = lilv_port_get_name(_handle, _port);
const LilvNode *_nPsym = lilv_port_get_symbol(_handle, _port);
char cAutoGenPortName [1024];
char cAutoGenPortSym [1024];
memset(cAutoGenPortName, 0, sizeof(cAutoGenPortName));
memset(cAutoGenPortSym, 0, sizeof(cAutoGenPortSym));
snprintf(cAutoGenPortName, sizeof(cAutoGenPortName) - 1, "autoport #%u", i);
snprintf(cAutoGenPortSym, sizeof(cAutoGenPortSym) - 1, "autoport#%u", i);
const char *_portName = cAutoGenPortName;
const char *_portSym = cAutoGenPortSym;
if(_nPname != 0)
{
_portName = lilv_node_as_string(_nPname);
}
if(_nPsym != 0)
{
_portSym = lilv_node_as_string(_nPsym);
}
const bool optional = lilv_port_has_property(_handle, _port, lv2CacheNodes.lv2_connectionOptional);
LV2_MIDI_PORTS *mPorts = &_midiOutPorts;
LV2_CONTROL_PORTS *cPorts = &_controlOutPorts;
LV2_AUDIO_PORTS *aPorts = &_audioOutPorts;
if(lilv_port_is_a(_handle, _port, lv2CacheNodes.lv2_InputPort))
{
mPorts = &_midiInPorts;
cPorts = &_controlInPorts;
aPorts = &_audioInPorts;
}
else if(!lilv_port_is_a(_handle, _port, lv2CacheNodes.lv2_OutputPort))
{
#ifdef DEBUG_LV2
std::cerr << "plugin has port with unknown direction - ignoring" << std::endl;
#endif
if(_nPname != 0)
lilv_node_free(_nPname);
continue;
}
bool isCVPort = lilv_port_is_a(_handle, _port, lv2CacheNodes.lv2_CVPort);
if(lilv_port_is_a(_handle, _port, lv2CacheNodes.lv2_ControlPort) || isCVPort)
{
LV2ControlPortType _cType = LV2_PORT_CONTINUOUS;
if(lilv_port_has_property(_handle, _port, lv2CacheNodes.lv2_portDiscrete))
_cType = LV2_PORT_DISCRETE;
else if(lilv_port_has_property(_handle, _port, lv2CacheNodes.lv2_portInteger))
_cType = LV2_PORT_INTEGER;
else if(lilv_port_has_property(_handle, _port, lv2CacheNodes.lv2_portTrigger)
|| lilv_port_has_property(_handle, _port, lv2CacheNodes.lv2_portToggled))
_cType = LV2_PORT_TRIGGER;
else if(lilv_port_has_property(_handle, _port, lv2CacheNodes.lv2_portLogarithmic))
_cType = LV2_PORT_LOGARITHMIC;
cPorts->push_back(LV2ControlPort(_port, i, 0.0f, _portName, _portSym, _cType, isCVPort));
if(std::isnan(_pluginControlsDefault [i]))
_pluginControlsDefault [i] = 0;
if(std::isnan(_pluginControlsMin [i]))
_pluginControlsMin [i] = 0;
if(std::isnan(_pluginControlsMax [i]))
_pluginControlsMax [i] = 0;
if(isCVPort)
{
_pluginControlsDefault [i] = 1;
_pluginControlsMin [i] = 0;
_pluginControlsMax [i] = 1;
}
else if (lilv_port_has_property (_handle, _port, lv2CacheNodes.lv2_SampleRate))
{
_pluginControlsDefault [i] *= MusEGlobal::sampleRate;
_pluginControlsMin [i] *= MusEGlobal::sampleRate;
_pluginControlsMax [i] *= MusEGlobal::sampleRate;
}
}
else if(lilv_port_is_a(_handle, _port, lv2CacheNodes.lv2_AudioPort))
{
aPorts->push_back(LV2AudioPort(_port, i, NULL, _portName));
}
else if(lilv_port_is_a(_handle, _port, lv2CacheNodes.ev_EventPort))
{
bool portSupportsTimePos = lilv_port_supports_event(_handle, _port, lv2CacheNodes.lv2_TimePosition);
mPorts->push_back(LV2MidiPort(_port, i, _portName, true /* old api is on */,portSupportsTimePos));
}
else if(lilv_port_is_a(_handle, _port, lv2CacheNodes.atom_AtomPort))
{
bool portSupportsTimePos = lilv_port_supports_event(_handle, _port, lv2CacheNodes.lv2_TimePosition);
mPorts->push_back(LV2MidiPort(_port, i, _portName, false /* old api is off */, portSupportsTimePos));
}
else if(!optional)
{
//#ifdef DEBUG_LV2
std::cerr << "plugin has port with unknown type - ignoring plugin " << label.toStdString() << "!" << std::endl;
//#endif
if(_nPname != 0)
lilv_node_free(_nPname);
return;
}
if(_nPname != 0)
lilv_node_free(_nPname);
}
for(uint32_t i = 0; i < _controlInPorts.size(); ++i)
{
_idxToControlMap.insert(std::pair<uint32_t, uint32_t>(_controlInPorts [i].index, i));
if(lilvFreeWheelPort != NULL)
{
if(lilv_port_get_index(_handle, _controlInPorts [i].port) == lilv_port_get_index(_handle, lilvFreeWheelPort))
{
_hasFreeWheelPort = true;
_freeWheelPortIndex = i;
}
}
}
const LilvPluginClass *cls = lilv_plugin_get_class(_plugin);
const LilvNode *ncuri = lilv_plugin_class_get_uri(cls);
const char *clsname = lilv_node_as_uri(ncuri);
if((strcmp(clsname, LV2_INSTRUMENT_CLASS) == 0) && (_midiInPorts.size() > 0))
{
_isSynth = true;
}
const LilvNode *pluginUIType = NULL;
_uis = NULL;
_uis = lilv_plugin_get_uis(_handle);
if(_uis)
{
LilvIter *it = lilv_uis_begin(_uis);
#ifdef DEBUG_LV2
std::cerr << "Plugin support uis of type:" << std::endl;
#endif
while(!lilv_uis_is_end(_uis, it))
{
const LilvUI *ui = lilv_uis_get(_uis, it);
if(lilv_ui_is_supported(ui,
LV2Synth::lv2ui_IsSupported,
lv2CacheNodes.host_uiType,
&pluginUIType))
{
#ifdef DEBUG_LV2
const char *strUiType = lilv_node_as_string(pluginUIType); //internal uis are preferred
std::cerr << "Plugin " << label.toStdString() << " supports ui of type " << strUiType << std::endl;
#endif
#ifndef HAVE_GTK2
const char *cUiUri = lilv_node_as_uri(pluginUIType);
if(strcmp(LV2_UI__GtkUI, cUiUri) != 0)
#endif
_pluginUiTypes.insert(std::make_pair(ui, std::make_pair(false, pluginUIType)));
}
else
{
const LilvNodes *nUiClss = lilv_ui_get_classes(ui);
LilvIter *nit = lilv_nodes_begin(nUiClss);
while(!lilv_nodes_is_end(_uis, nit))
{
const LilvNode *nUiCls = lilv_nodes_get(nUiClss, nit);
#ifdef DEBUG_LV2
const char *ClsStr = lilv_node_as_string(nUiCls);
std::cerr << ClsStr << std::endl;
#endif
bool extUi = lilv_node_equals(nUiCls, lv2CacheNodes.ext_uiType);
bool extdUi = lilv_node_equals(nUiCls, lv2CacheNodes.ext_d_uiType);
if(extUi || extdUi)
{
pluginUIType = extUi ? lv2CacheNodes.ext_uiType : lv2CacheNodes.ext_d_uiType;
#ifdef DEBUG_LV2
std::cerr << "Plugin " << label.toStdString() << " supports ui of type " << LV2_UI_EXTERNAL << std::endl;
#endif
#ifndef HAVE_GTK2
const char *cUiUri = lilv_node_as_uri(pluginUIType);
if(strcmp(LV2_UI__GtkUI, cUiUri) != 0)
#endif
_pluginUiTypes.insert(std::make_pair(ui, std::make_pair(true, pluginUIType)));
}
nit = lilv_nodes_next(nUiClss, nit);
}
}
it = lilv_uis_next(_uis, it);
};
}
_presets.clear();
LV2Synth::lv2state_UnloadLoadPresets(this, true);
_isConstructed = true;
}
LV2Synth::~LV2Synth()
{
LV2Synth::lv2state_UnloadLoadPresets(this);
if(_ppfeatures)
{
delete [] _ppfeatures;
_ppfeatures = NULL;
}
if(_features)
{
delete [] _features;
_features = NULL;
}
if(_options)
{
delete [] _options;
_options = NULL;
}
if(_uis != NULL)
{
lilv_uis_free(_uis);
_uis = NULL;
}
}
SynthIF *LV2Synth::createSIF(SynthI *synthi)
{
++_instances;
LV2SynthIF *sif = new LV2SynthIF(synthi);
if(!sif->init(this))
{
delete sif;
sif = NULL;
}
return sif;
}
LV2_URID LV2Synth::mapUrid(const char *uri)
{
return uridBiMap.map(uri);
}
const char *LV2Synth::unmapUrid(LV2_URID id)
{
return uridBiMap.unmap(id);
}
LV2SynthIF::~LV2SynthIF()
{
if(_state != NULL)
{
_state->deleteLater = true;
//_uiState->uiTimer->stopNextTime(false);
if(_state->pluginWindow != NULL)
_state->pluginWindow->stopNextTime();
else
LV2Synth::lv2state_FreeState(_state);
_state = NULL;
}
LV2_AUDIO_PORTS::iterator _itA = _audioInPorts.begin();
for(; _itA != _audioInPorts.end(); ++_itA)
{
free((*_itA).buffer);
}
_itA = _audioOutPorts.begin();
for(; _itA != _audioOutPorts.end(); ++_itA)
{
free((*_itA).buffer);
}
if(_audioInSilenceBuf)
free(_audioInSilenceBuf);
if(_audioInBuffers)
{
delete [] _audioInBuffers;
_audioInBuffers = NULL;
}
if(_audioOutBuffers)
{
delete [] _audioOutBuffers;
_audioOutBuffers = NULL;
}
if(_controls)
{
delete [] _controls;
}
if(_controlsOut)
{
delete [] _controlsOut;
}
if(_ppifeatures)
{
delete [] _ppifeatures;
_ppifeatures = NULL;
}
if(_ifeatures)
{
delete [] _ifeatures;
_ifeatures = NULL;
}
}
LV2SynthIF::LV2SynthIF(SynthI *s): SynthIF(s)
{
_synth = NULL;
_handle = NULL;
_audioInBuffers = NULL;
_audioOutBuffers = NULL;
_inports = 0;
_outports = 0;
_controls = NULL;
_controlsOut = NULL;
_inportsControl = 0;
_outportsControl = 0;
_inportsMidi = 0,
_outportsMidi = 0,
_audioInSilenceBuf = NULL;
_ifeatures = NULL;
_ppifeatures = NULL;
_state = NULL;
}
bool LV2SynthIF::init(LV2Synth *s)
{
_synth = s;
//use LV2Synth features as template
_state = new LV2PluginWrapper_State;
_state->inst = NULL;
_state->widget = NULL;
_state->uiInst = NULL;
_state->plugInst = NULL;
_state->_ifeatures = new LV2_Feature[SIZEOF_ARRAY(lv2Features)];
_state->_ppifeatures = new LV2_Feature *[SIZEOF_ARRAY(lv2Features) + 1];
_state->sif = this;
_state->synth = _synth;
LV2Synth::lv2state_FillFeatures(_state);
_state->handle = _handle = lilv_plugin_instantiate(_synth->_handle, (double)MusEGlobal::sampleRate, _state->_ppifeatures);
if(_handle == NULL)
{
delete [] _state->_ppifeatures;
_state->_ppifeatures = NULL;
delete [] _state->_ifeatures;
_state->_ifeatures = NULL;
return false;
}
_audioInPorts = s->_audioInPorts;
_audioOutPorts = s->_audioOutPorts;
_controlInPorts = s->_controlInPorts;
_controlOutPorts = s->_controlOutPorts;
_inportsMidi = _state->midiInPorts.size();
_outportsMidi = _state->midiOutPorts.size();
_inportsControl = _controlInPorts.size();
_outportsControl = _controlOutPorts.size();
if(_inportsControl != 0)
{
_controls = new Port[_inportsControl];
}
else
{
_controls = NULL;
}
if(_outportsControl != 0)
{
_controlsOut = new Port[_outportsControl];
}
else
{
_controlsOut = NULL;
}
_synth->midiCtl2PortMap.clear();
_synth->port2MidiCtlMap.clear();
for(size_t i = 0; i < _inportsControl; i++)
{
uint32_t idx = _controlInPorts [i].index;
_controls [i].idx = idx;
_controls [i].val = _controls [i].tmpVal = _controlInPorts [i].defVal = _synth->_pluginControlsDefault [idx];
if(_synth->_hasFreeWheelPort && _synth->_freeWheelPortIndex == i)
_controls [i].enCtrl = false;
else
_controls [i].enCtrl = true;
_controlInPorts [i].minVal = _synth->_pluginControlsMin [idx];
_controlInPorts [i].maxVal = _synth->_pluginControlsMax [idx];
int ctlnum = CTRL_NRPN14_OFFSET + 0x2000 + i;
// We have a controller number! Insert it and the lv2 port number into both maps.
_synth->midiCtl2PortMap.insert(std::pair<int, int>(ctlnum, i));
_synth->port2MidiCtlMap.insert(std::pair<int, int>(i, ctlnum));
int id = genACnum(MAX_PLUGINS, i);
CtrlList *cl;
CtrlListList *cll = track()->controller();
iCtrlList icl = cll->find(id);
if(icl == cll->end())
{
cl = new CtrlList(id);
cll->add(cl);
cl->setCurVal(_controls[i].val);
}
else
{
cl = icl->second;
_controls[i].val = cl->curVal();
}
cl->setRange(_synth->_pluginControlsMin [idx], _synth->_pluginControlsMax [idx]);
cl->setName(QString(_controlInPorts [i].cName));
CtrlValueType vt = VAL_LINEAR;
switch(_controlInPorts [i].cType)
{
case LV2_PORT_CONTINUOUS:
vt = VAL_LINEAR;
break;
case LV2_PORT_DISCRETE:
case LV2_PORT_INTEGER:
vt = VAL_INT;
break;
case LV2_PORT_LOGARITHMIC:
vt = VAL_LOG;
break;
case LV2_PORT_TRIGGER:
vt = VAL_BOOL;
break;
default:
break;
}
cl->setValueType(vt);
cl->setMode(((_controlInPorts [i].cType == LV2_PORT_CONTINUOUS)
||(_controlInPorts [i].cType == LV2_PORT_LOGARITHMIC))? CtrlList::INTERPOLATE : CtrlList::DISCRETE);
if(!_controlInPorts [i].isCVPort)
lilv_instance_connect_port(_handle, idx, &_controls [i].val);
}
for(size_t i = 0; i < _outportsControl; i++)
{
uint32_t idx = _controlOutPorts [i].index;
_controlsOut[i].idx = idx;
_controlsOut[i].val = 0.0;
_controlsOut[i].tmpVal = 0.0;
_controlsOut[i].enCtrl = false;
_controlOutPorts [i].defVal = _controlOutPorts [i].minVal = _controlOutPorts [i].maxVal = 0.0;
if(!_controlOutPorts [i].isCVPort)
lilv_instance_connect_port(_handle, idx, &_controlsOut[i].val);
}
int rv = posix_memalign((void **)&_audioInSilenceBuf, 16, sizeof(float) * MusEGlobal::segmentSize);
if(rv != 0)
{
fprintf(stderr, "ERROR: LV2SynthIF::init: posix_memalign returned error:%d. Aborting!\n", rv);
abort();
}
if(MusEGlobal::config.useDenormalBias)
{
for(unsigned q = 0; q < MusEGlobal::segmentSize; ++q)
{
_audioInSilenceBuf[q] = MusEGlobal::denormalBias;
}
}
else
{
memset(_audioInSilenceBuf, 0, sizeof(float) * MusEGlobal::segmentSize);
}
//cache number of ports
_inports = _audioInPorts.size();
_outports = _audioOutPorts.size();
if(_inports > 0)
{
_audioInBuffers = new float*[_inports];
for(size_t i = 0; i < _inports; i++)
{
int rv = posix_memalign((void **)&_audioInBuffers [i], 16, sizeof(float) * MusEGlobal::segmentSize);
if(rv != 0)
{
fprintf(stderr, "ERROR: LV2SynthIF::init: posix_memalign returned error:%d. Aborting!\n", rv);
abort();
}
if(MusEGlobal::config.useDenormalBias)
{
for(unsigned q = 0; q < MusEGlobal::segmentSize; ++q)
{
_audioInBuffers [i][q] = MusEGlobal::denormalBias;
}
}
else
{
memset(_audioInBuffers [i], 0, sizeof(float) * MusEGlobal::segmentSize);
}
_audioInPorts [i].buffer = _audioInBuffers [i];
lilv_instance_connect_port(_handle, _audioInPorts [i].index, _audioInBuffers [i]);
}
}
if(_outports > 0)
{
_audioOutBuffers = new float*[_outports];
for(size_t i = 0; i < _outports; i++)
{
int rv = posix_memalign((void **)&_audioOutBuffers [i], 16, sizeof(float) * MusEGlobal::segmentSize);
if(rv != 0)
{
fprintf(stderr, "ERROR: LV2SynthIF::init: posix_memalign returned error:%d. Aborting!\n", rv);
abort();
}
if(MusEGlobal::config.useDenormalBias)
{
for(unsigned q = 0; q < MusEGlobal::segmentSize; ++q)
{
_audioOutBuffers [i][q] = MusEGlobal::denormalBias;
}
}
else
{
memset(_audioOutBuffers [i], 0, sizeof(float) * MusEGlobal::segmentSize);
}
_audioOutPorts [i].buffer = _audioOutBuffers [i];
lilv_instance_connect_port(_handle, _audioOutPorts [i].index, _audioOutBuffers [i]);
}
}
LV2Synth::lv2state_PostInstantiate(_state);
activate();
return true;
}
void LV2SynthIF::doSelectProgram(unsigned char channel, int bankH, int bankL, int prog)
{
// // Only if there's something to change...
// if(bankH >= 128 && bankL >= 128 && prog >= 128)
// return;
if(bankH > 127) // Map "dont care" to 0
bankH = 0;
if(bankL > 127)
bankL = 0;
if(prog > 127)
prog = 0;
const int bank = (bankH << 8) | bankL;
if(_state && _state->prgIface && (_state->prgIface->select_program || _state->prgIface->select_program_for_channel))
{
if(_state->newPrgIface)
_state->prgIface->select_program_for_channel(lilv_instance_get_handle(_state->handle), channel, (uint32_t)bank, (uint32_t)prog);
else
_state->prgIface->select_program(lilv_instance_get_handle(_state->handle), (uint32_t)bank, (uint32_t)prog);
/*
* A plugin is permitted to re-write the values of its input
* control ports when select_program is called. The host should
* re-read the input control port values and update its own
* records appropriately. (This is the only circumstance in which
* a LV2 plugin is allowed to modify its own control-input ports.)
*/
if(id() != -1)
{
for(unsigned long k = 0; k < _inportsControl; ++k)
{
// We're in the audio thread context: no need to send a message, just modify directly.
synti->setPluginCtrlVal(genACnum(id(), k), _controls[k].val);
}
}
//set update ui program flag
_state->uiChannel = channel;
_state->uiBank = bank;
_state->uiProg = prog;
_state->uiDoSelectPrg = true;
}
}
void LV2SynthIF::sendLv2MidiEvent(LV2EvBuf *evBuf, long frame, int paramCount, uint8_t a, uint8_t b, uint8_t c)
{
if(paramCount < 1 || paramCount > 3)
return;
if(evBuf)
{
uint8_t midiEv [paramCount];
midiEv [0] = a;
if(paramCount >= 2)
midiEv [1] = b;
if(paramCount == 3)
midiEv [2] = c;
evBuf->write(frame, 0, _synth->_midi_event_id, paramCount, midiEv);
}
}
int LV2SynthIF::channels() const
{
return (_outports) > MAX_CHANNELS ? MAX_CHANNELS : (_outports) ;
}
int LV2SynthIF::totalInChannels() const
{
return _inports;
}
int LV2SynthIF::totalOutChannels() const
{
return _outports;
}
void LV2SynthIF::activate()
{
if(_handle)
{
lilv_instance_activate(_handle);
}
}
void LV2SynthIF::deactivate()
{
if(_handle)
{
lilv_instance_deactivate(_handle);
}
}
void LV2SynthIF::deactivate3()
{
deactivate();
}
int LV2SynthIF::eventsPending() const
{
//TODO: what's this?
return 0;
}
bool LV2SynthIF::lv2MidiControlValues(size_t port, int ctlnum, int *min, int *max, int *def)
{
float fmin, fmax, fdef;
int imin;
float frng;
fdef = _controlInPorts [port].defVal;
fmin = _controlInPorts [port].minVal;
fmax = _controlInPorts [port].maxVal;
bool hasdef = (fdef == fdef);
if(fmin != fmin)
{
fmin = 0.0;
}
if(fmax != fmax)
{
fmax = 0.0;
}
MidiController::ControllerType t = midiControllerType(ctlnum);
#ifdef PLUGIN_DEBUGIN
printf("lv2MidiControlValues: ctlnum:%d ladspa port:%lu has default?:%d default:%f\n", ctlnum, port, hasdef, fdef);
#endif
frng = fmax - fmin;
imin = lrintf(fmin);
//imax = lrintf(fmax);
int ctlmn = 0;
int ctlmx = 127;// Avoid divide-by-zero error below.
#ifdef PLUGIN_DEBUGIN
printf("lv2MidiControlValues: port min:%f max:%f \n", fmin, fmax);
#endif
bool isneg = (imin < 0);
int bias = 0;
switch(t)
{
case MidiController::RPN:
case MidiController::NRPN:
case MidiController::Controller7:
if(isneg)
{
ctlmn = -64;
ctlmx = 63;
bias = -64;
}
else
{
ctlmn = 0;
ctlmx = 127;
}
break;
case MidiController::Controller14:
case MidiController::RPN14:
case MidiController::NRPN14:
if(isneg)
{
ctlmn = -8192;
ctlmx = 8191;
bias = -8192;
}
else
{
ctlmn = 0;
ctlmx = 16383;
}
break;
case MidiController::Program:
ctlmn = 0;
ctlmx = 0x3fff; // FIXME: Really should not happen or be allowed. What to do here...
break;
case MidiController::Pitch:
ctlmn = -8192;
ctlmx = 8191;
break;
case MidiController::Velo: // cannot happen
default:
break;
}
float fctlrng = float(ctlmx - ctlmn);
// It's a floating point control, just use wide open maximum range.
*min = ctlmn;
*max = ctlmx;
float normdef = (frng == 0.0) ? 0.0 : fdef / frng;
fdef = normdef * fctlrng;
// FIXME: TODO: Incorrect... Fix this somewhat more trivial stuff later....
*def = (int)lrintf(fdef) + bias;
#ifdef PLUGIN_DEBUGIN
printf("lv2MidiControlValues: setting default:%d\n", *def);
#endif
return hasdef;
}
//---------------------------------------------------------
// midi2LadspaValue
//---------------------------------------------------------
float LV2SynthIF::midi2Lv2Value(unsigned long port, int ctlnum, int val)
{
float fmin, fmax;
int imin;
float frng;
MidiController::ControllerType t = midiControllerType(ctlnum);
#ifdef PLUGIN_DEBUGIN
printf("midi2Lv2Value: ctlnum:%d port:%lu val:%d\n", ctlnum, port, val);
#endif
fmin = _controlInPorts [port].minVal;
fmax = _controlInPorts [port].maxVal;
if(fmin != fmin)
{
fmin = 0.0;
}
if(fmax != fmax)
{
fmax = 0.0;
}
frng = fmax - fmin;
imin = lrintf(fmin);
int ctlmn = 0;
int ctlmx = 127;
#ifdef PLUGIN_DEBUGIN
printf("midi2Lv2Value: port min:%f max:%f \n", fmin, fmax);
#endif
bool isneg = (imin < 0);
int bval = val;
//int cval = val;
switch(t)
{
case MidiController::RPN:
case MidiController::NRPN:
case MidiController::Controller7:
if(isneg)
{
ctlmn = -64;
ctlmx = 63;
bval -= 64;
//cval -= 64;
}
else
{
ctlmn = 0;
ctlmx = 127;
//cval -= 64;
}
break;
case MidiController::Controller14:
case MidiController::RPN14:
case MidiController::NRPN14:
if(isneg)
{
ctlmn = -8192;
ctlmx = 8191;
bval -= 8192;
//cval -= 8192;
}
else
{
ctlmn = 0;
ctlmx = 16383;
//cval -= 8192;
}
break;
case MidiController::Program:
ctlmn = 0;
ctlmx = 0xffffff;
break;
case MidiController::Pitch:
ctlmn = -8192;
ctlmx = 8191;
break;
case MidiController::Velo: // cannot happen
default:
break;
}
int ctlrng = ctlmx - ctlmn;
float fctlrng = float(ctlmx - ctlmn);
// Avoid divide-by-zero error below.
if(ctlrng == 0)
{
return 0.0;
}
// It's a floating point control, just use wide open maximum range.
float normval = float(bval) / fctlrng;
float ret = normval * frng + fmin;
#ifdef PLUGIN_DEBUGIN
printf("midi2Lv2Value: float returning:%f\n", ret);
#endif
return ret;
}
int LV2SynthIF::getControllerInfo(int id, QString* name, int *ctrl, int *min, int *max, int *initval)
{
size_t _id = (size_t)id;
if(_id == _inportsControl || _id == _inportsControl + 1)
{
//
// It is unknown at this point whether or not a synth recognizes aftertouch and poly aftertouch
// (channel and key pressure) midi messages, so add support for them now (as controllers).
//
if(_id == _inportsControl)
{
*ctrl = CTRL_POLYAFTER;
}
else if(_id == _inportsControl + 1)
{
*ctrl = CTRL_AFTERTOUCH;
}
*min = 0;
*max = 127;
*initval = CTRL_VAL_UNKNOWN;
*name = midiCtrlName(*ctrl);
return ++_id;
}
else if(_id >= _inportsControl + 2)
{
return 0;
}
int ctlnum; // = DSSI_NONE;
// No controller number? Give it one.
//if(ctlnum == DSSI_NONE)
//{
// Simple but flawed solution: Start them at 0x60000 + 0x2000 = 0x62000. Max NRPN number is 0x3fff.
ctlnum = CTRL_NRPN14_OFFSET + 0x2000 + _id;
//}
int def = CTRL_VAL_UNKNOWN;
if(lv2MidiControlValues(_id, ctlnum, min, max, &def))
{
*initval = def;
}
else
{
*initval = CTRL_VAL_UNKNOWN;
}
#ifdef DEBUG_LV2
printf("LV2SynthIF::getControllerInfo passed ctlnum:%d min:%d max:%d initval:%d\n", ctlnum, *min, *max, *initval);
#endif
*ctrl = ctlnum;
*name = QString(_controlInPorts[_id].cName);
return ++_id;
}
bool LV2SynthIF::processEvent(const MidiPlayEvent &e, LV2EvBuf *evBuf, long frame)
{
int chn = e.channel();
int a = e.dataA();
int b = e.dataB();
int type = e.type();
#ifdef LV2_DEBUG
fprintf(stderr, "LV2SynthIF::processEvent midi event type:%d chn:%d a:%d b:%d\n", e.type(), chn, a, b);
#endif
// REMOVE Tim. Noteoff. Added.
const MidiInstrument::NoteOffMode nom = synti->noteOffMode();
switch(type)
{
case ME_NOTEON:
#ifdef LV2_DEBUG
fprintf(stderr, "LV2SynthIF::processEvent midi event is ME_NOTEON\n");
#endif
// REMOVE Tim. Noteoff. Changed.
// if(b)
// {
// sendLv2MidiEvent(evBuf, frame, (type | chn) & 0xff, a & 0x7f, b & 0x7f);
// }
// else
// {
// sendLv2MidiEvent(evBuf, frame, (ME_NOTEOFF | chn) & 0xff, a & 0x7f, 0);
// }
if(b == 0)
{
// Handle zero-velocity note ons. Technically this is an error because internal midi paths
// are now all 'note-off' without zero-vel note ons - they're converted to note offs.
// Nothing should be setting a Note type Event's on velocity to zero.
// But just in case... If we get this warning, it means there is still code to change.
fprintf(stderr, "LV2SynthIF::processEvent: Warning: Zero-vel note on: time:%d type:%d (ME_NOTEON) ch:%d A:%d B:%d\n", e.time(), e.type(), chn, a, b);
switch(nom)
{
// Instrument uses note offs. Convert to zero-vel note off.
case MidiInstrument::NoteOffAll:
//if(MusEGlobal::midiOutputTrace)
// fprintf(stderr, "MidiOut: LV2: Following event will be converted to zero-velocity note off:\n");
sendLv2MidiEvent(evBuf, frame, 3, (ME_NOTEOFF | chn) & 0xff, a & 0x7f, 0);
break;
// Instrument uses no note offs at all. Send as-is.
case MidiInstrument::NoteOffNone:
// Instrument converts all note offs to zero-vel note ons. Send as-is.
case MidiInstrument::NoteOffConvertToZVNoteOn:
sendLv2MidiEvent(evBuf, frame, 3, (type | chn) & 0xff, a & 0x7f, b & 0x7f);
break;
}
}
else
sendLv2MidiEvent(evBuf, frame, 3, (type | chn) & 0xff, a & 0x7f, b & 0x7f);
break;
case ME_NOTEOFF:
#ifdef LV2_DEBUG
fprintf(stderr, "LV2SynthIF::processEvent midi event is ME_NOTEOFF\n");
#endif
// REMOVE Tim. Noteoff. Changed.
// sendLv2MidiEvent(evBuf, frame, (type | chn) & 0xff, a & 0x7f, b & 0x7f);
switch(nom)
{
// Instrument uses note offs. Send as-is.
case MidiInstrument::NoteOffAll:
sendLv2MidiEvent(evBuf, frame, 3, (type | chn) & 0xff, a & 0x7f, b & 0x7f);
break;
// Instrument uses no note offs at all. Send nothing. Eat up the event - return false.
case MidiInstrument::NoteOffNone:
return false;
// Instrument converts all note offs to zero-vel note ons. Convert to zero-vel note on.
case MidiInstrument::NoteOffConvertToZVNoteOn:
//if(MusEGlobal::midiOutputTrace)
// fprintf(stderr, "MidiOut: LV2: Following event will be converted to zero-velocity note on:\n");
sendLv2MidiEvent(evBuf, frame, 3, (ME_NOTEON | chn) & 0xff, a & 0x7f, 0);
break;
}
break;
case ME_PROGRAM:
{
#ifdef LV2_DEBUG
fprintf(stderr, "LV2SynthIF::processEvent midi event is ME_PROGRAM\n");
#endif
int hb, lb;
synti->currentProg(chn, NULL, &lb, &hb);
synti->setCurrentProg(chn, a & 0xff, lb, hb);
doSelectProgram(chn, hb, lb, a);
// Event pointer not filled. Return false.
return false;
}
break;
case ME_CONTROLLER:
{
#ifdef LV2_DEBUG
fprintf(stderr, "LV2SynthIF::processEvent midi event is ME_CONTROLLER\n");
#endif
// Our internal hwCtrl controllers support the 'unknown' value.
// Don't send 'unknown' values to the driver. Ignore and return no error.
if(b == CTRL_VAL_UNKNOWN)
return false;
if(a == CTRL_PROGRAM)
{
#ifdef LV2_DEBUG
fprintf(stderr, "LV2SynthIF::processEvent midi event is ME_CONTROLLER, dataA is CTRL_PROGRAM\n");
#endif
int hb = (b >> 16) & 0xff;
int lb = (b >> 8) & 0xff;
int pr = b & 0xff;
synti->setCurrentProg(chn, pr, lb, hb);
doSelectProgram(chn, hb, lb, pr);
// Event pointer not filled. Return false.
return false;
}
if(a == CTRL_HBANK)
{
int lb, pr;
synti->currentProg(chn, &pr, &lb, NULL);
synti->setCurrentProg(chn, pr, lb, b & 0xff);
doSelectProgram(chn, b, lb, pr);
// Event pointer not filled. Return false.
return false;
}
if(a == CTRL_LBANK)
{
int hb, pr;
synti->currentProg(chn, &pr, NULL, &hb);
synti->setCurrentProg(chn, pr, b & 0xff, hb);
doSelectProgram(chn, hb, b, pr);
// Event pointer not filled. Return false.
return false;
}
if(a == CTRL_PITCH)
{
#ifdef LV2_DEBUG
fprintf(stderr, "LV2SynthIF::processEvent midi event is ME_CONTROLLER, dataA is CTRL_PITCH\n");
#endif
b += 8192;
sendLv2MidiEvent(evBuf, frame, 3, (ME_PITCHBEND | chn) & 0xff, b & 0x7f, (b >> 7) & 0x7f);
// Event pointer filled. Return true.
return true;
}
if(a == CTRL_AFTERTOUCH)
{
#ifdef LV2_DEBUG
fprintf(stderr, "LV2SynthIF::processEvent midi event is ME_CONTROLLER, dataA is CTRL_AFTERTOUCH\n");
#endif
sendLv2MidiEvent(evBuf, frame, 2, (ME_AFTERTOUCH | chn) & 0xff, b & 0x7f, 0);
// Event pointer filled. Return true.
return true;
}
if((a | 0xff) == CTRL_POLYAFTER)
{
#ifdef LV2_DEBUG
fprintf(stderr, "LV2SynthIF::processEvent midi event is ME_CONTROLLER, dataA is CTRL_POLYAFTER\n");
#endif
sendLv2MidiEvent(evBuf, frame, 3, (ME_POLYAFTER | chn) & 0xff, a & 0x7f, b & 0x7f);
// Event pointer filled. Return true.
return true;
}
ciMidiCtl2LadspaPort ip = _synth->midiCtl2PortMap.find(a);
// Is it just a regular midi controller, not mapped to a LADSPA port (either by the plugin or by us)?
// NOTE: There's no way to tell which of these controllers is supported by the plugin.
// For example sustain footpedal or pitch bend may be supported, but not mapped to any LADSPA port.
if(ip == _synth->midiCtl2PortMap.end())
{
if(midiControllerType(a) == MidiController::NRPN14
|| midiControllerType(a) == MidiController::NRPN)
{
//send full nrpn control sequence (4 values):
// 99 + ((a & 0xff00) >> 8) - first byte
// 98 + (a & 0xff) - second byte
// 6 + ((b & 0x3f80) >> 7) - third byte
// 38 + (b & 0x7f) - fourth byte
sendLv2MidiEvent(evBuf, frame, 3, (type | chn) & 0xff, 99, ((a & 0xff00) >> 8));
sendLv2MidiEvent(evBuf, frame, 3, (type | chn) & 0xff, 98, (a & 0xff));
sendLv2MidiEvent(evBuf, frame, 3, (type | chn) & 0xff, 6, ((b & 0x3f80) >> 7));
sendLv2MidiEvent(evBuf, frame, 3, (type | chn) & 0xff, 38, (b & 0x7f));
return true;
}
else if(midiControllerType(a) != MidiController::Controller7)
{
return false; // Event pointer not filled. Return false.
}
// Fill the event.
#ifdef LV2_DEBUG
printf("LV2SynthIF::processEvent non-ladspa filling midi event chn:%d dataA:%d dataB:%d\n", chn, a, b);
#endif
sendLv2MidiEvent(evBuf, frame, 3, (type | chn) & 0xff, a & 0x7f, b & 0x7f);
return true;
}
unsigned long k = ip->second;
int ctlnum; // = DSSI_NONE;
// No midi controller for the ladspa port? Send to ladspa control.
//if(ctlnum == DSSI_NONE)
//{
// Sanity check.
if(k > _inportsControl)
{
return false;
}
// Simple but flawed solution: Start them at 0x60000 + 0x2000 = 0x62000. Max NRPN number is 0x3fff.
ctlnum = k + (CTRL_NRPN14_OFFSET + 0x2000);
//}
float val = midi2Lv2Value(k, ctlnum, b);
#ifdef LV2_DEBUG
//fprintf(stderr, "LV2SynthIF::processEvent control port:%lu port:%lu dataA:%d Converting val from:%d to lv2:%f\n", i, k, a, b, val);
fprintf(stderr, "LV2SynthIF::processEvent port:%lu dataA:%d Converting val from:%d to lv2:%f\n", k, a, b, val);
#endif
// Set the lv2 port value.
_controls[k].val = val;
// Need to update the automation value, otherwise it overwrites later with the last automation value.
if(id() != -1)
// We're in the audio thread context: no need to send a message, just modify directly.
{
synti->setPluginCtrlVal(genACnum(id(), k), val);
}
// Since we absorbed the message as a lv2 control change, return false - the event is not filled.
return false;
}
break;
case ME_PITCHBEND:
a += 8192;
sendLv2MidiEvent(evBuf, frame, 3, (type | chn) & 0xff, a & 0x7f, (a >> 7) & 0x7f);
break;
case ME_AFTERTOUCH:
sendLv2MidiEvent(evBuf, frame, 2, (type | chn) & 0xff, a & 0x7f, 0);
break;
case ME_POLYAFTER:
sendLv2MidiEvent(evBuf, frame, 3, (type | chn) & 0xff, a & 0x7f, b & 0x7f);
break;
default:
if(MusEGlobal::debugMsg)
{
fprintf(stderr, "LV2SynthIF::processEvent midi event unknown type:%d\n", e.type());
}
// Event not filled.
return false;
break;
}
return true;
}
bool LV2SynthIF::getData(MidiPort *, unsigned int pos, int ports, unsigned int nframes, float **buffer)
{
const unsigned int syncFrame = MusEGlobal::audio->curSyncFrame();
// All ports must be connected to something!
const unsigned long nop = ((unsigned long) ports) > _outports ? _outports : ((unsigned long) ports);
const bool usefixedrate = (requiredFeatures() & Plugin::FixedBlockSize);;
const unsigned long min_per = (usefixedrate || MusEGlobal::config.minControlProcessPeriod > nframes) ? nframes : MusEGlobal::config.minControlProcessPeriod;
const unsigned long min_per_mask = min_per - 1; // min_per must be power of 2
unsigned long sample = 0;
AudioTrack *atrack = track();
const AutomationType at = atrack->automationType();
const bool no_auto = !MusEGlobal::automation || at == AUTO_OFF;
CtrlListList *cll = atrack->controller();
ciCtrlList icl_first;
const int plug_id = id();
LV2EvBuf *evBuf = (_inportsMidi > 0) ? _state->midiInPorts [0].buffer : NULL;
//set freewheeling property if plugin supports it
if(_synth->_hasFreeWheelPort)
{
_controls [_synth->_freeWheelPortIndex].val = MusEGlobal::audio->freewheel() ? 1.0f : 0.0f;
}
if(plug_id != -1 && ports != 0) // Don't bother if not 'running'.
{
icl_first = cll->lower_bound(genACnum(plug_id, 0));
}
bool used_in_chan_array[_inports]; // Don't bother initializing if not 'running'.
// Don't bother if not 'running'.
if(ports != 0)
{
// Initialize the array.
for(size_t i = 0; i < _inports; ++i)
used_in_chan_array[i] = false;
if(!atrack->noInRoute())
{
RouteList *irl = atrack->inRoutes();
for(ciRoute i = irl->begin(); i != irl->end(); ++i)
{
if(i->track->isMidiTrack())
continue;
// Only this synth knows how many destination channels there are,
// while only the track knows how many source channels there are.
// So take care of the destination channels here, and let the track handle the source channels.
const int dst_ch = i->channel <= -1 ? 0 : i->channel;
if((unsigned long)dst_ch >= _inports)
continue;
const int dst_chs = i->channels <= -1 ? _inports : i->channels;
//const int total_ins = atrack->totalRoutableInputs(Route::TRACK_ROUTE);
const int src_ch = i->remoteChannel <= -1 ? 0 : i->remoteChannel;
const int src_chs = i->channels;
int fin_dst_chs = dst_chs;
if((unsigned long)(dst_ch + fin_dst_chs) > _inports)
fin_dst_chs = _inports - dst_ch;
static_cast<AudioTrack*>(i->track)->copyData(pos,
dst_ch, dst_chs, fin_dst_chs,
src_ch, src_chs,
nframes, &_audioInBuffers[0],
false, used_in_chan_array);
const int nxt_ch = dst_ch + fin_dst_chs;
for(int ch = dst_ch; ch < nxt_ch; ++ch)
used_in_chan_array[ch] = true;
}
}
}
int cur_slice = 0;
while(sample < nframes)
{
unsigned long nsamp = nframes - sample;
const unsigned long slice_frame = pos + sample;
//
// Process automation control values, while also determining the maximum acceptable
// size of this run. Further processing, from FIFOs for example, can lower the size
// from there, but this section determines where the next highest maximum frame
// absolutely needs to be for smooth playback of the controller value stream...
//
if(ports != 0) // Don't bother if not 'running'.
{
ciCtrlList icl = icl_first;
for(unsigned long k = 0; k < _inportsControl; ++k)
{
//don't process freewheel port
if(_synth->_hasFreeWheelPort && _synth->_freeWheelPortIndex == k)
continue;
CtrlList *cl = (cll && plug_id != -1 && icl != cll->end()) ? icl->second : NULL;
CtrlInterpolate &ci = _controls[k].interp;
// Always refresh the interpolate struct at first, since things may have changed.
// Or if the frame is outside of the interpolate range - and eStop is not true. // FIXME TODO: Be sure these comparisons are correct.
if(cur_slice == 0 || (!ci.eStop && MusEGlobal::audio->isPlaying() &&
(slice_frame < (unsigned long)ci.sFrame || (ci.eFrame != -1 && slice_frame >= (unsigned long)ci.eFrame))))
{
if(cl && plug_id != -1 && (unsigned long)cl->id() == genACnum(plug_id, k))
{
cl->getInterpolation(slice_frame, no_auto || !_controls[k].enCtrl, &ci);
if(icl != cll->end())
{
++icl;
}
}
else
{
// No matching controller, or end. Just copy the current value into the interpolator.
// Keep the current icl iterator, because since they are sorted by frames,
// if the IDs didn't match it means we can just let k catch up with icl.
ci.sFrame = 0;
ci.eFrame = -1;
ci.sVal = _controls[k].val;
ci.eVal = ci.sVal;
ci.doInterp = false;
ci.eStop = false;
}
}
else
{
if(ci.eStop && ci.eFrame != -1 && slice_frame >= (unsigned long)ci.eFrame) // FIXME TODO: Get that comparison right.
{
// Clear the stop condition and set up the interp struct appropriately as an endless value.
ci.sFrame = 0; //ci->eFrame;
ci.eFrame = -1;
ci.sVal = ci.eVal;
ci.doInterp = false;
ci.eStop = false;
}
if(cl && cll && icl != cll->end())
{
++icl;
}
}
if(!usefixedrate && MusEGlobal::audio->isPlaying())
{
unsigned long samps = nsamp;
if(ci.eFrame != -1)
{
samps = (unsigned long)ci.eFrame - slice_frame;
}
if(!ci.doInterp && samps > min_per)
{
samps &= ~min_per_mask;
if((samps & min_per_mask) != 0)
{
samps += min_per;
}
}
else
{
samps = min_per;
}
if(samps < nsamp)
{
nsamp = samps;
}
}
if(ci.doInterp && cl)
{
_controls[k].val = cl->interpolate(MusEGlobal::audio->isPlaying() ? slice_frame : pos, ci);
}
else
{
_controls[k].val = ci.sVal;
}
_state->controlsMask [k] = true;
#ifdef LV2_DEBUG_PROCESS
fprintf(stderr, "LV2SynthIF::getData k:%lu val:%f sample:%lu ci.eFrame:%d nsamp:%lu \n", k, _controls[k].val, sample, ci.eFrame, nsamp);
#endif
}
}
bool found = false;
unsigned long frame = 0;
unsigned long index = 0;
// Get all control ring buffer items valid for this time period...
while(!_controlFifo.isEmpty())
{
unsigned long evframe;
const ControlEvent& v = _controlFifo.peek();
// The events happened in the last period or even before that. Shift into this period with + n. This will sync with audio.
// If the events happened even before current frame - n, make sure they are counted immediately as zero-frame.
evframe = (syncFrame > v.frame + nframes) ? 0 : v.frame - syncFrame + nframes;
#ifdef DEBUG_LV2
fprintf(stderr, "LV2SynthIF::getData found:%d evframe:%lu frame:%lu event frame:%lu idx:%lu val:%f unique:%d\n",
found, evframe, frame, v.frame, v.idx, v.value, v.unique);
#endif
// Protection. Observed this condition. Why? Supposed to be linear timestamps.
if(found && evframe < frame)
{
fprintf(stderr, "LV2SynthIF::getData *** Error: evframe:%lu < frame:%lu event: frame:%lu idx:%lu val:%f unique:%d\n",
evframe, frame, v.frame, v.idx, v.value, v.unique);
// No choice but to ignore it.
_controlFifo.remove(); // Done with the ring buffer's item. Remove it.
continue;
}
if(evframe >= nframes // Next events are for a later period.
|| (!usefixedrate && !found && !v.unique && (evframe - sample >= nsamp)) // Next events are for a later run in this period. (Autom took prio.)
|| (found && !v.unique && (evframe - sample >= min_per)) // Eat up events within minimum slice - they're too close.
|| (usefixedrate && found && v.unique && v.idx == index)) // Fixed rate and must reply to all.
{
break;
}
// _controlFifo.remove(); // Done with the ring buffer's item. Remove it.
found = true;
frame = evframe;
index = v.idx;
if(index >= _inportsControl) // Sanity check.
{
_controlFifo.remove(); // Done with the ring buffer's item. Remove it.
break;
}
//don't process freewheel port
if(_synth->_hasFreeWheelPort && _synth->_freeWheelPortIndex == index)
{
_controlFifo.remove(); // Done with the ring buffer's item. Remove it.
continue;
}
if(ports == 0) // Don't bother if not 'running'.
{
_controls[index].val = v.value; // Might as well at least update these.
}
else
{
CtrlInterpolate *ci = &_controls[index].interp;
// Tell it to stop the current ramp at this frame, when it does stop, set this value:
ci->eFrame = frame;
ci->eVal = v.value;
ci->eStop = true;
}
// Need to update the automation value, otherwise it overwrites later with the last automation value.
if(plug_id != -1)
{
synti->setPluginCtrlVal(genACnum(plug_id, index), v.value);
}
if(v.fromGui) //don't send gui control changes back
{
_state->lastControls [index] = v.value;
_state->controlsMask [index] = false;
}
_controlFifo.remove(); // Done with the ring buffer's item. Remove it.
}
if(found && !usefixedrate) // If a control FIFO item was found, takes priority over automation controller stream.
{
nsamp = frame - sample;
}
if(sample + nsamp > nframes) // Safety check.
{
nsamp = nframes - sample;
}
// TODO: Don't allow zero-length runs. This could/should be checked in the control loop instead.
// Note this means it is still possible to get stuck in the top loop (at least for a while).
if(nsamp != 0)
{
LV2Synth::lv2audio_preProcessMidiPorts(_state, nsamp);
// Get the state of the stop flag.
const bool do_stop = synti->stopFlag();
MidiPlayEvent buf_ev;
// Transfer the user lock-free buffer events to the user sorted multi-set.
// False = don't use the size snapshot, but update it.
const unsigned int usr_buf_sz = synti->eventBuffers(MidiDevice::UserBuffer)->getSize(false);
for(unsigned int i = 0; i < usr_buf_sz; ++i)
{
if(synti->eventBuffers(MidiDevice::UserBuffer)->get(buf_ev))
synti->_outUserEvents.insert(buf_ev);
}
// Transfer the playback lock-free buffer events to the playback sorted multi-set.
const unsigned int pb_buf_sz = synti->eventBuffers(MidiDevice::PlaybackBuffer)->getSize(false);
for(unsigned int i = 0; i < pb_buf_sz; ++i)
{
// Are we stopping? Just remove the item.
if(do_stop)
synti->eventBuffers(MidiDevice::PlaybackBuffer)->remove();
// Otherwise get the item.
else if(synti->eventBuffers(MidiDevice::PlaybackBuffer)->get(buf_ev))
synti->_outPlaybackEvents.insert(buf_ev);
}
// Are we stopping?
if(do_stop)
{
// Transport has stopped, purge ALL further scheduled playback events now.
synti->_outPlaybackEvents.clear();
// Reset the flag.
synti->setStopFlag(false);
}
iMPEvent impe_pb = synti->_outPlaybackEvents.begin();
iMPEvent impe_us = synti->_outUserEvents.begin();
bool using_pb;
while(1)
{
if(impe_pb != synti->_outPlaybackEvents.end() && impe_us != synti->_outUserEvents.end())
using_pb = *impe_pb < *impe_us;
else if(impe_pb != synti->_outPlaybackEvents.end())
using_pb = true;
else if(impe_us != synti->_outUserEvents.end())
using_pb = false;
else break;
const MidiPlayEvent& e = using_pb ? *impe_pb : *impe_us;
#ifdef LV2_DEBUG
fprintf(stderr, "LV2SynthIF::getData eventFifos event time:%d\n", e.time());
#endif
if(e.time() >= (sample + nsamp + syncFrame))
break;
if(ports != 0) // Don't bother if not 'running'.
{
// Time-stamp the event.
unsigned int ft = (e.time() < syncFrame) ? 0 : e.time() - syncFrame;
ft = (ft < sample) ? 0 : ft - sample;
if(ft >= nsamp)
{
fprintf(stderr, "LV2SynthIF::getData: eventFifos event time:%d out of range. pos:%d syncFrame:%u ft:%u sample:%lu nsamp:%lu\n",
e.time(), pos, syncFrame, ft, sample, nsamp);
ft = nsamp - 1;
}
if(processEvent(e, evBuf, ft))
{
}
}
// Done with ring buffer's event. Remove it.
// C++11.
if(using_pb)
impe_pb = synti->_outPlaybackEvents.erase(impe_pb);
else
impe_us = synti->_outUserEvents.erase(impe_us);
}
if(ports != 0) // Don't bother if not 'running'.
{
//connect ports
for(size_t j = 0; j < _inports; ++j)
{
if(used_in_chan_array [j])
{
lilv_instance_connect_port(_handle, _audioInPorts [j].index, _audioInBuffers [j] + sample);
}
else
{
lilv_instance_connect_port(_handle, _audioInPorts [j].index, _audioInSilenceBuf + sample);
}
}
for(size_t j = 0; j < nop; ++j)
{
lilv_instance_connect_port(_handle, _audioOutPorts [j].index, buffer [j] + sample);
}
for(size_t j = nop; j < _outports; j++)
{
lilv_instance_connect_port(_handle, _audioOutPorts [j].index, _audioOutBuffers [j] + sample);
}
for(size_t j = 0; j < _inportsControl; ++j)
{
uint32_t idx = _controlInPorts [j].index;
if(_state->pluginCVPorts [idx] != NULL)
{
float cvVal = _controls [j].val;
for(size_t jj = 0; jj < nsamp; ++jj)
{
_state->pluginCVPorts [idx] [jj + sample] = cvVal;
}
lilv_instance_connect_port(_handle, idx, _state->pluginCVPorts [idx] + sample);
}
}
#ifdef LV2_DEBUG_PROCESS
//dump atom sequence events to stderr
if(evBuf)
{
evBuf->dump();
}
#endif
lilv_instance_run(_handle, nsamp);
//notify worker that this run() finished
if(_state->wrkIface && _state->wrkIface->end_run)
_state->wrkIface->end_run(lilv_instance_get_handle(_handle));
//notify worker about processed data (if any)
if(_state->wrkIface && _state->wrkIface->work_response && _state->wrkEndWork)
{
_state->wrkIface->work_response(lilv_instance_get_handle(_handle), _state->wrkDataSize, _state->wrkDataBuffer);
_state->wrkDataSize = 0;
_state->wrkDataBuffer = NULL;
_state->wrkEndWork = false;
}
LV2Synth::lv2audio_postProcessMidiPorts(_state, nsamp);
}
sample += nsamp;
}
++cur_slice; // Slice is done. Moving on to any next slice now...
}
return true;
}
void LV2SynthIF::getNativeGeometry(int *x, int *y, int *w, int *h) const
{
if(_state->pluginWindow != NULL && !_state->hasExternalGui)
{
QRect g = _state->pluginWindow->geometry();
if(x) *x = g.x();
if(y) *y = g.y();
if(w) *w = g.width();
if(h) *h = g.height();
return;
}
// Fall back to blank geometry.
SynthIF::getNativeGeometry(x, y, w, h);
}
double LV2SynthIF::getParameter(long unsigned int n) const
{
if(n >= _inportsControl)
{
std::cout << "LV2SynthIF::getParameter param number " << n << " out of range of ports: " << _inportsControl << std::endl;
return 0.0;
}
if(!_controls)
{
return 0.0;
}
return _controls[n].val;
}
double LV2SynthIF::getParameterOut(long unsigned int n) const
{
if(n >= _outportsControl)
{
std::cout << "LV2SynthIF::getParameterOut param number " << n << " out of range of ports: " << _outportsControl << std::endl;
return 0.0;
}
if(!_controlsOut)
{
return 0.0;
}
return _controlsOut[n].val;
}
QString LV2SynthIF::getPatchName(int /* ch */, int prog, bool) const
{
uint32_t program = prog & 0xff;
uint32_t lbank = (prog >> 8) & 0xff;
uint32_t hbank = (prog >> 16) & 0xff;
if (program > 127) // Map "dont care" to 0
program = 0;
if (lbank > 127)
lbank = 0;
if (hbank > 127)
hbank = 0;
const uint32_t patch = (hbank << 16) | (lbank << 8) | program;
std::map<uint32_t, uint32_t>::iterator itPrg = _state->prg2index.find(patch);
if(itPrg == _state->prg2index.end())
return QString("?");
uint32_t index = itPrg->second;
std::map<uint32_t, lv2ExtProgram>::iterator itIndex = _state->index2prg.find(index);
if(itIndex == _state->index2prg.end())
return QString("?");
return QString(itIndex->second.name);
}
void LV2SynthIF::guiHeartBeat()
{
//check for pending song dirty status
if(_state->songDirtyPending){
MusEGlobal::song->setDirty();
_state->songDirtyPending = false;
}
}
bool LV2SynthIF::hasGui() const
{
return true;
}
bool LV2SynthIF::hasNativeGui() const
{
return (_synth->_pluginUiTypes.size() > 0);
}
bool LV2SynthIF::nativeGuiVisible() const
{
if(_state != NULL)
{
if(_state->hasExternalGui)
{
return (_state->widget != NULL);
}
else if(_state->hasGui && _state->widget != NULL)
{
return ((QWidget *)_state->widget)->isVisible();
}
}
return false;
}
void LV2SynthIF::populatePatchPopup(MusEGui::PopupMenu *menu, int, bool)
{
LV2Synth::lv2prg_updatePrograms(_state);
menu->clear();
MusEGui::PopupMenu *subMenuPrograms = new MusEGui::PopupMenu(menu->parent());
subMenuPrograms->setTitle(QObject::tr("Midi programs"));
subMenuPrograms->setIcon(QIcon(*MusEGui::pianoNewIcon));
menu->addMenu(subMenuPrograms);
MusEGui::PopupMenu *subMenuPresets = new MusEGui::PopupMenu(menu->parent());
subMenuPresets->setTitle(QObject::tr("Presets"));
menu->addMenu(subMenuPresets);
//First: fill programs submenu
std::map<int, MusEGui::PopupMenu *> submenus;
std::map<uint32_t, lv2ExtProgram>::iterator itIndex;
for(itIndex = _state->index2prg.begin(); itIndex != _state->index2prg.end(); ++itIndex)
{
const lv2ExtProgram &extPrg = itIndex->second;
uint32_t hb = extPrg.bank >> 8;
uint32_t lb = extPrg.bank & 0xff;
// 16384 banks arranged as 128 hi and lo banks each with up to the first 128 programs supported.
if(hb > 127 || lb > 127 || extPrg.prog > 127)
continue;
hb &= 0x7f;
lb &= 0x7f;
const uint32_t patch_bank = (hb << 8) | lb;
const uint32_t patch = (patch_bank << 8) | extPrg.prog;
std::map<int, MusEGui::PopupMenu *>::iterator itS = submenus.find(patch_bank);
MusEGui::PopupMenu *submenu= NULL;
if(itS != submenus.end())
{
submenu = itS->second;
}
else
{
submenu = new MusEGui::PopupMenu(menu->parent());
submenu->setTitle(QString("Bank #") + QString::number(extPrg.bank + 1));
subMenuPrograms->addMenu(submenu);
submenus.insert(std::make_pair(patch_bank, submenu));
}
QAction *act = submenu->addAction(extPrg.name);
act->setData(patch);
}
//Second:: Fill presets submenu
LV2Synth::lv2state_populatePresetsMenu(_state, subMenuPresets);
}
void LV2SynthIF::preProcessAlways()
{
}
MidiPlayEvent LV2SynthIF::receiveEvent()
{
return MidiPlayEvent();
}
void LV2SynthIF::setNativeGeometry(int x, int y, int w, int h)
{
// Store the native geometry.
SynthIF::setNativeGeometry(x, y, w, h);
if(_state->pluginWindow && !_state->hasExternalGui)
{
//_state->pluginWindow->move(x, y);
//don't resize lv2 uis - this is handles at plugin level
//_uiState->pluginWindow->resize(w, h);
#ifdef QT_SHOW_POS_BUG_WORKAROUND
// Because of the bug, no matter what we must supply a position,
// even upon first showing...
// Check sane size.
if(w == 0)
w = _state->pluginWindow->sizeHint().width();
if(h == 0)
h = _state->pluginWindow->sizeHint().height();
// No size hint? Try minimum size.
if(w == 0)
w = _state->pluginWindow->minimumSize().width();
if(h == 0)
h = _state->pluginWindow->minimumSize().height();
// Fallback.
if(w == 0)
w = 400;
if(h == 0)
h = 300;
_state->pluginWindow->setGeometry(x, y, w, h);
#else
// If the saved geometry is valid, use it.
// Otherwise this is probably the first time showing,
// so do not set a geometry - let Qt pick one
// (using auto-placement and sizeHint).
if(!(x == 0 && y == 0 && w == 0 && h == 0))
{
// Check sane size.
if(w == 0)
w = _state->pluginWindow->sizeHint().width();
if(h == 0)
h = _state->pluginWindow->sizeHint().height();
// No size hint? Try minimum size.
if(w == 0)
w = _state->pluginWindow->minimumSize().width();
if(h == 0)
h = _state->pluginWindow->minimumSize().height();
// Fallback.
if(w == 0)
w = 400;
if(h == 0)
h = 300;
_state->pluginWindow->setGeometry(x, y, w, h);
}
#endif
}
}
void LV2SynthIF::setParameter(long unsigned int idx, double value)
{
addScheduledControlEvent(idx, value, MusEGlobal::audio->curFrame());
}
void LV2SynthIF::showNativeGui(bool bShow)
{
if(track() != NULL)
{
if(_state->human_id != NULL)
{
free(_state->human_id);
}
_state->extHost.plugin_human_id = _state->human_id = strdup((track()->name() + QString(": ") + name()).toUtf8().constData());
}
LV2Synth::lv2ui_ShowNativeGui(_state, bShow);
}
void LV2SynthIF::write(int level, Xml &xml) const
{
LV2Synth::lv2conf_write(_state, level, xml);
}
void LV2SynthIF::setCustomData(const std::vector< QString > &customParams)
{
LV2Synth::lv2conf_set(_state, customParams);
}
double LV2SynthIF::param(long unsigned int i) const
{
return getParameter(i);
}
long unsigned int LV2SynthIF::parameters() const
{
return _inportsControl;
}
long unsigned int LV2SynthIF::parametersOut() const
{
return _outportsControl;
}
const char *LV2SynthIF::paramName(long unsigned int i)
{
return _controlInPorts [i].cName;
}
const char *LV2SynthIF::paramOutName(long unsigned int i)
{
return _controlOutPorts [i].cName;
}
CtrlValueType LV2SynthIF::ctrlValueType(unsigned long i) const
{
CtrlValueType vt = VAL_LINEAR;
std::map<uint32_t, uint32_t>::iterator it = _synth->_idxToControlMap.find(i);
assert(it != _synth->_idxToControlMap.end());
i = it->second;
assert(i < _inportsControl);
switch(_synth->_controlInPorts [i].cType)
{
case LV2_PORT_CONTINUOUS:
vt = VAL_LINEAR;
break;
case LV2_PORT_DISCRETE:
case LV2_PORT_INTEGER:
vt = VAL_INT;
break;
case LV2_PORT_LOGARITHMIC:
vt = VAL_LOG;
break;
case LV2_PORT_TRIGGER:
vt = VAL_BOOL;
break;
default:
break;
}
return vt;
}
CtrlList::Mode LV2SynthIF::ctrlMode(unsigned long i) const
{
std::map<uint32_t, uint32_t>::iterator it = _synth->_idxToControlMap.find(i);
assert(it != _synth->_idxToControlMap.end());
i = it->second;
assert(i < _inportsControl);
return ((_synth->_controlInPorts [i].cType == LV2_PORT_CONTINUOUS)
||(_synth->_controlInPorts [i].cType == LV2_PORT_LOGARITHMIC)) ? CtrlList::INTERPOLATE : CtrlList::DISCRETE;
}
LADSPA_PortRangeHint LV2SynthIF::range(unsigned long i)
{
assert(i < _inportsControl);
LADSPA_PortRangeHint hint;
hint.HintDescriptor = 0;
hint.LowerBound = _controlInPorts [i].minVal;
hint.UpperBound = _controlInPorts [i].maxVal;
if(hint.LowerBound == hint.LowerBound)
{
hint.HintDescriptor |= LADSPA_HINT_BOUNDED_BELOW;
}
if(hint.UpperBound == hint.UpperBound)
{
hint.HintDescriptor |= LADSPA_HINT_BOUNDED_ABOVE;
}
return hint;
}
LADSPA_PortRangeHint LV2SynthIF::rangeOut(unsigned long i)
{
assert(i < _outportsControl);
LADSPA_PortRangeHint hint;
hint.HintDescriptor = 0;
hint.LowerBound = _controlOutPorts [i].minVal;
hint.UpperBound = _controlOutPorts [i].maxVal;
if(hint.LowerBound == hint.LowerBound)
{
hint.HintDescriptor |= LADSPA_HINT_BOUNDED_BELOW;
}
if(hint.UpperBound == hint.UpperBound)
{
hint.HintDescriptor |= LADSPA_HINT_BOUNDED_ABOVE;
}
return hint;
}
double LV2SynthIF::paramOut(long unsigned int i) const
{
return getParameterOut(i);
}
void LV2SynthIF::setParam(long unsigned int i, double val)
{
setParameter(i, val);
}
void LV2SynthIF::enableController(unsigned long i, bool v) { _controls[i].enCtrl = v; }
bool LV2SynthIF::controllerEnabled(unsigned long i) const { return _controls[i].enCtrl; }
void LV2SynthIF::enableAllControllers(bool v)
{
if(!_synth)
return;
for(unsigned long i = 0; i < _inportsControl; ++i)
_controls[i].enCtrl = v;
}
void LV2SynthIF::updateControllers() { }
void LV2SynthIF::populatePresetsMenu(MusEGui::PopupMenu *menu)
{
LV2Synth::lv2state_populatePresetsMenu(_state, menu);
}
void LV2SynthIF::applyPreset(void *preset)
{
LV2Synth::lv2state_applyPreset(_state, static_cast<LilvNode *>(preset));
}
void LV2SynthIF::writeConfiguration(int level, Xml &xml)
{
MusECore::SynthIF::writeConfiguration(level, xml);
}
bool LV2SynthIF::readConfiguration(Xml &xml, bool readPreset)
{
return MusECore::SynthIF::readConfiguration(xml, readPreset);
}
void LV2PluginWrapper_Window::hideEvent(QHideEvent *e)
{
if(_state->plugInst != NULL)
_state->plugInst->saveNativeGeometry(geometry().x(), geometry().y(), geometry().width(), geometry().height());
else if(_state->sif != NULL)
_state->sif->saveNativeGeometry(geometry().x(), geometry().y(), geometry().width(), geometry().height());
e->ignore();
QMainWindow::hideEvent(e);
}
void LV2PluginWrapper_Window::showEvent(QShowEvent *e)
{
int x = 0, y = 0, w = 0, h = 0;
if(_state->plugInst != NULL)
_state->plugInst->savedNativeGeometry(&x, &y, &w, &h);
else if(_state->sif != NULL)
_state->sif->savedNativeGeometry(&x, &y, &w, &h);
#ifdef QT_SHOW_POS_BUG_WORKAROUND
// Because of the bug, no matter what we must supply a position,
// even upon first showing...
// Check sane size.
if(w == 0)
w = sizeHint().width();
if(h == 0)
h = sizeHint().height();
// No size hint? Try minimum size.
if(w == 0)
w = minimumSize().width();
if(h == 0)
h = minimumSize().height();
// Fallback.
if(w == 0)
w = 400;
if(h == 0)
h = 300;
setGeometry(x, y, w, h);
#else
// If the saved geometry is valid, use it.
// Otherwise this is probably the first time showing,
// so do not set a geometry - let Qt pick one
// (using auto-placement and sizeHint).
if(!(x == 0 && y == 0 && w == 0 && h == 0))
{
// Check sane size.
if(w == 0)
w = sizeHint().width();
if(h == 0)
h = sizeHint().height();
// No size hint? Try minimum size.
if(w == 0)
w = minimumSize().width();
if(h == 0)
h = minimumSize().height();
// Fallback.
if(w == 0)
w = 400;
if(h == 0)
h = 300;
setGeometry(x, y, w, h);
}
#endif
// Convenience: If the window was minimized, restore it.
if(isMinimized())
setWindowState((windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
e->ignore();
QMainWindow::showEvent(e);
}
void LV2PluginWrapper_Window::closeEvent(QCloseEvent *event)
{
assert(_state != NULL);
event->accept();
stopUpdateTimer();
//if(_state->gtk2Plug != NULL)
//{
if(_state->pluginQWindow != NULL)
{
_state->pluginQWindow->setParent(NULL);
delete _state->pluginQWindow;
_state->pluginQWindow = NULL;
}
//}
if(_state->deleteLater)
{
LV2Synth::lv2state_FreeState(_state);
}
else
{
//_state->uiTimer->stopNextTime(false);
_state->widget = NULL;
_state->pluginWindow = NULL;
_state->uiDoSelectPrg = false;
_state->uiPrgIface = NULL;
LV2Synth::lv2ui_FreeDescriptors(_state);
}
// Reset the flag, just to be sure.
_state->uiIsOpening = false;
// The widget is automatically deleted by use of the
// WA_DeleteOnClose attribute in the constructor.
}
void LV2PluginWrapper_Window::stopUpdateTimer()
{
if(updateTimer.isActive())
updateTimer.stop();
while(updateTimer.isActive())
{
QCoreApplication::processEvents();
}
}
#ifdef LV2_GUI_USE_QWIDGET
LV2PluginWrapper_Window::LV2PluginWrapper_Window(LV2PluginWrapper_State *state,
QWidget *parent,
Qt::WindowFlags flags)
: QWidget(parent, flags), _state ( state ), _closing(false)
#else
LV2PluginWrapper_Window::LV2PluginWrapper_Window(LV2PluginWrapper_State *state,
QWidget *parent,
Qt::WindowFlags flags)
: QMainWindow(parent, flags), _state ( state ), _closing(false)
#endif
{
// Automatically delete the wiget when it closes.
setAttribute(Qt::WA_DeleteOnClose, true);
connect(&updateTimer, SIGNAL(timeout()), this, SLOT(updateGui()));
connect(this, SIGNAL(makeStopFromGuiThread()), this, SLOT(stopFromGuiThread()));
connect(this, SIGNAL(makeStartFromGuiThread()), this, SLOT(startFromGuiThread()));
}
LV2PluginWrapper_Window::~LV2PluginWrapper_Window()
{
#ifdef DEBUG_LV2
std::cout << "LV2PluginWrapper_Window::~LV2PluginWrapper_Window()" << std::endl;
#endif
}
void LV2PluginWrapper_Window::startNextTime()
{
emit makeStartFromGuiThread();
}
void LV2PluginWrapper_Window::stopNextTime()
{
setClosing(true);
emit makeStopFromGuiThread();
}
void LV2PluginWrapper_Window::updateGui()
{
if(_state->deleteLater || _closing)
{
stopNextTime();
return;
}
LV2Synth::lv2ui_SendChangedControls(_state);
//send program change if any
// Force send if re-opening.
if(_state->uiIsOpening || _state->uiDoSelectPrg)
{
_state->uiDoSelectPrg = false;
if(_state->uiPrgIface != NULL && (_state->uiPrgIface->select_program != NULL || _state->uiPrgIface->select_program_for_channel != NULL))
{
if(_state->newPrgIface)
_state->uiPrgIface->select_program_for_channel(lilv_instance_get_handle(_state->handle), _state->uiChannel, (uint32_t)_state->uiBank, (uint32_t)_state->uiProg);
else
_state->uiPrgIface->select_program(lilv_instance_get_handle(_state->handle), (uint32_t)_state->uiBank, (uint32_t)_state->uiProg);
}
}
// Reset the flag.
_state->uiIsOpening = false;
//call ui idle callback if any
if(_state->uiIdleIface != NULL)
{
int iRet = _state->uiIdleIface->idle(_state->uiInst);
if(iRet != 0) // ui don't want us to call it's idle callback any more
_state->uiIdleIface = NULL;
}
if(_state->hasExternalGui)
{
LV2_EXTERNAL_UI_RUN((LV2_External_UI_Widget *)_state->widget);
}
//if(_closing)
//stopNextTime();
}
void LV2PluginWrapper_Window::stopFromGuiThread()
{
stopUpdateTimer();
emit close();
}
void LV2PluginWrapper_Window::startFromGuiThread()
{
stopUpdateTimer();
updateTimer.start(1000/30);
}
LV2PluginWrapper::LV2PluginWrapper(LV2Synth *s, PluginFeatures reqFeatures)
{
_synth = s;
_requiredFeatures = reqFeatures;
_fakeLd.Label = strdup(_synth->name().toUtf8().constData());
_fakeLd.Name = strdup(_synth->name().toUtf8().constData());
_fakeLd.UniqueID = _synth->_uniqueID;
_fakeLd.Maker = strdup(_synth->maker().toUtf8().constData());
_fakeLd.Copyright = strdup(_synth->version().toUtf8().constData());
_isLV2Plugin = true;
_isLV2Synth = s->_isSynth;
int numPorts = _synth->_audioInPorts.size()
+ _synth->_audioOutPorts.size()
+ _synth->_controlInPorts.size()
+ _synth->_controlOutPorts.size()
+ _synth->_midiInPorts.size()
+ _synth->_midiOutPorts.size();
_fakeLd.PortCount = numPorts;
_fakePds = new LADSPA_PortDescriptor [numPorts];
memset(_fakePds, 0, sizeof(int) * numPorts);
for(size_t i = 0; i < _synth->_audioInPorts.size(); i++)
{
_fakePds [_synth->_audioInPorts [i].index] = LADSPA_PORT_INPUT | LADSPA_PORT_AUDIO;
}
for(size_t i = 0; i < _synth->_audioOutPorts.size(); i++)
{
_fakePds [_synth->_audioOutPorts [i].index] = LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO;
}
for(size_t i = 0; i < _synth->_controlInPorts.size(); i++)
{
_fakePds [_synth->_controlInPorts [i].index] = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
}
for(size_t i = 0; i < _synth->_controlOutPorts.size(); i++)
{
_fakePds [_synth->_controlOutPorts [i].index] = LADSPA_PORT_OUTPUT | LADSPA_PORT_CONTROL;
}
_fakeLd.PortNames = NULL;
_fakeLd.PortRangeHints = NULL;
_fakeLd.PortDescriptors = _fakePds;
_fakeLd.Properties = 0;
plugin = &_fakeLd;
_isDssi = false;
_isDssiSynth = false;
_isVstNativePlugin = false;
_isVstNativeSynth = false;
#ifdef DSSI_SUPPORT
dssi_descr = NULL;
#endif
fi = _synth->info;
ladspa = NULL;
_handle = 0;
_references = 0;
_instNo = 0;
_label = _synth->name();
_name = _synth->description();
_uniqueID = plugin->UniqueID;
_maker = _synth->maker();
_copyright = _synth->version();
_portCount = plugin->PortCount;
_inports = 0;
_outports = 0;
_controlInPorts = 0;
_controlOutPorts = 0;
for(unsigned long k = 0; k < _portCount; ++k)
{
LADSPA_PortDescriptor pd = plugin->PortDescriptors[k];
if(pd & LADSPA_PORT_AUDIO)
{
if(pd & LADSPA_PORT_INPUT)
{
++_inports;
}
else if(pd & LADSPA_PORT_OUTPUT)
{
++_outports;
}
}
else if(pd & LADSPA_PORT_CONTROL)
{
if(pd & LADSPA_PORT_INPUT)
{
++_controlInPorts;
}
else if(pd & LADSPA_PORT_OUTPUT)
{
++_controlOutPorts;
}
}
}
}
LV2PluginWrapper::~LV2PluginWrapper()
{
free((void*)_fakeLd.Label);
free((void*)_fakeLd.Name);
free((void*)_fakeLd.Maker);
free((void*)_fakeLd.Copyright);
delete [] _fakePds;
}
LADSPA_Handle LV2PluginWrapper::instantiate(PluginI *plugi)
{
LV2PluginWrapper_State *state = new LV2PluginWrapper_State;
state->inst = this;
state->widget = NULL;
state->uiInst = NULL;
state->plugInst = plugi;
state->_ifeatures = new LV2_Feature[SIZEOF_ARRAY(lv2Features)];
state->_ppifeatures = new LV2_Feature *[SIZEOF_ARRAY(lv2Features) + 1];
state->sif = NULL;
state->synth = _synth;
LV2Synth::lv2state_FillFeatures(state);
state->handle = lilv_plugin_instantiate(_synth->_handle, (double)MusEGlobal::sampleRate, state->_ppifeatures);
if(state->handle == NULL)
{
delete [] state->_ppifeatures;
delete [] state->_ifeatures;
return NULL;
}
//_states.insert(std::pair<void *, LV2PluginWrapper_State *>(state->handle, state));
LV2Synth::lv2state_PostInstantiate(state);
return (LADSPA_Handle)state;
}
void LV2PluginWrapper::connectPort(LADSPA_Handle handle, long unsigned int port, float *value)
{
LV2PluginWrapper_State *state = (LV2PluginWrapper_State *)handle;
lilv_instance_connect_port((LilvInstance *)state->handle, port, (void *)value);
}
int LV2PluginWrapper::incReferences(int ref)
{
_synth->incInstances(ref);
return _synth->instances();
}
void LV2PluginWrapper::activate(LADSPA_Handle handle)
{
LV2PluginWrapper_State *state = (LV2PluginWrapper_State *)handle;
lilv_instance_activate((LilvInstance *) state->handle);
}
void LV2PluginWrapper::deactivate(LADSPA_Handle handle)
{
if (handle)
{
LV2PluginWrapper_State *state = (LV2PluginWrapper_State *)handle;
lilv_instance_deactivate((LilvInstance *) state->handle);
}
}
void LV2PluginWrapper::cleanup(LADSPA_Handle handle)
{
if(handle != NULL)
{
LV2PluginWrapper_State *state = (LV2PluginWrapper_State *)handle;
state->deleteLater = true;
if(state->pluginWindow != NULL)
state->pluginWindow->stopNextTime();
else
LV2Synth::lv2state_FreeState(state);
}
}
void LV2PluginWrapper::apply(LADSPA_Handle handle, unsigned long n)
{
LV2PluginWrapper_State *state = (LV2PluginWrapper_State *)handle;
LV2Synth::lv2audio_preProcessMidiPorts(state, n);
//set freewheeling property if plugin supports it
if(state->synth->_hasFreeWheelPort)
{
state->plugInst->controls[_synth->_freeWheelPortIndex].val = MusEGlobal::audio->freewheel() ? 1.0f : 0.0f;
}
for(size_t j = 0; j < state->plugInst->controlPorts; ++j)
{
uint32_t idx = state->synth->_controlInPorts [j].index;
if(state->pluginCVPorts [idx] != NULL)
{
float cvVal = state->plugInst->controls [j].val;
for(size_t jj = 0; jj < n; ++jj)
{
state->pluginCVPorts [idx] [jj] = cvVal;
}
lilv_instance_connect_port(state->handle, idx, state->pluginCVPorts [idx]);
}
}
for(size_t j = 0; j < state->plugInst->controlOutPorts; ++j)
{
uint32_t idx = state->synth->_controlOutPorts [j].index;
if(state->pluginCVPorts [idx] != NULL)
{
float cvVal = state->plugInst->controlsOut [j].val;
for(size_t jj = 0; jj < n; ++jj)
{
state->pluginCVPorts [idx] [jj] = cvVal;
}
lilv_instance_connect_port(state->handle, idx, state->pluginCVPorts [idx]);
}
}
lilv_instance_run(state->handle, n);
//notify worker that this run() finished
if(state->wrkIface && state->wrkIface->end_run)
state->wrkIface->end_run(lilv_instance_get_handle(state->handle));
//notify worker about processes data (if any)
if(state->wrkIface && state->wrkIface->work_response && state->wrkEndWork)
{
state->wrkEndWork = false;
state->wrkIface->work_response(lilv_instance_get_handle(state->handle), state->wrkDataSize, state->wrkDataBuffer);
state->wrkDataSize = 0;
state->wrkDataBuffer = NULL;
}
LV2Synth::lv2audio_postProcessMidiPorts(state, n);
}
LADSPA_PortDescriptor LV2PluginWrapper::portd(unsigned long k) const
{
return _fakeLd.PortDescriptors[k];
}
LADSPA_PortRangeHint LV2PluginWrapper::range(unsigned long i)
{
LADSPA_PortRangeHint hint;
hint.HintDescriptor = 0;
hint.LowerBound = _synth->_pluginControlsMin [i];
hint.UpperBound = _synth->_pluginControlsMax [i];
if(hint.LowerBound == hint.LowerBound)
{
hint.HintDescriptor |= LADSPA_HINT_BOUNDED_BELOW;
}
if(hint.UpperBound == hint.UpperBound)
{
hint.HintDescriptor |= LADSPA_HINT_BOUNDED_ABOVE;
}
return hint;
}
void LV2PluginWrapper::range(unsigned long i, float *min, float *max) const
{
*min = _synth->_pluginControlsMin [i];
*max = _synth->_pluginControlsMax [i];
}
double LV2PluginWrapper::defaultValue(unsigned long port) const
{
return _synth->_pluginControlsDefault [port];
}
const char *LV2PluginWrapper::portName(unsigned long i)
{
return lilv_node_as_string(lilv_port_get_name(_synth->_handle, lilv_plugin_get_port_by_index(_synth->_handle, i)));
}
CtrlValueType LV2PluginWrapper::ctrlValueType(unsigned long i) const
{
CtrlValueType vt = VAL_LINEAR;
std::map<uint32_t, uint32_t>::iterator it = _synth->_idxToControlMap.find(i);
assert(it != _synth->_idxToControlMap.end());
i = it->second;
assert(i < _controlInPorts);
switch(_synth->_controlInPorts [i].cType)
{
case LV2_PORT_CONTINUOUS:
vt = VAL_LINEAR;
break;
case LV2_PORT_DISCRETE:
case LV2_PORT_INTEGER:
vt = VAL_INT;
break;
case LV2_PORT_LOGARITHMIC:
vt = VAL_LOG;
break;
case LV2_PORT_TRIGGER:
vt = VAL_BOOL;
break;
default:
break;
}
return vt;
}
CtrlList::Mode LV2PluginWrapper::ctrlMode(unsigned long i) const
{
std::map<uint32_t, uint32_t>::iterator it = _synth->_idxToControlMap.find(i);
assert(it != _synth->_idxToControlMap.end());
i = it->second;
assert(i < _controlInPorts);
return ((_synth->_controlInPorts [i].cType == LV2_PORT_CONTINUOUS)
||(_synth->_controlInPorts [i].cType == LV2_PORT_LOGARITHMIC)) ? CtrlList::INTERPOLATE : CtrlList::DISCRETE;
}
bool LV2PluginWrapper::hasNativeGui() const
{
return (_synth->_pluginUiTypes.size() > 0);
}
void LV2PluginWrapper::showNativeGui(PluginI *p, bool bShow)
{
assert(p->instances > 0);
LV2PluginWrapper_State *state = (LV2PluginWrapper_State *)p->handle [0];
if(p->track() != NULL)
{
if(state->human_id != NULL)
{
free(state->human_id);
}
state->extHost.plugin_human_id = state->human_id = strdup((p->track()->name() + QString(": ") + label()).toUtf8().constData());
}
LV2Synth::lv2ui_ShowNativeGui(state, bShow);
}
bool LV2PluginWrapper::nativeGuiVisible(const PluginI *p) const
{
assert(p->instances > 0);
LV2PluginWrapper_State *state = (LV2PluginWrapper_State *)p->handle [0];
return (state->widget != NULL);
}
void LV2PluginWrapper::setLastStateControls(LADSPA_Handle handle, size_t index, bool bSetMask, bool bSetVal, bool bMask, float fVal)
{
LV2PluginWrapper_State *state = (LV2PluginWrapper_State *)handle;
assert(state != NULL);
if(_controlInPorts == 0)
return;
if(bSetMask)
state->controlsMask [index] = bMask;
if(bSetVal)
state->lastControls [index] = fVal;
}
void LV2PluginWrapper::writeConfiguration(LADSPA_Handle handle, int level, Xml &xml)
{
LV2PluginWrapper_State *state = (LV2PluginWrapper_State *)handle;
assert(state != NULL);
LV2Synth::lv2conf_write(state, level, xml);
}
void LV2PluginWrapper::setCustomData(LADSPA_Handle handle, const std::vector<QString> &customParams)
{
LV2PluginWrapper_State *state = (LV2PluginWrapper_State *)handle;
assert(state != NULL);
LV2Synth::lv2conf_set(state, customParams);
}
void LV2PluginWrapper::populatePresetsMenu(PluginI *p, MusEGui::PopupMenu *menu)
{
assert(p->instances > 0);
LV2PluginWrapper_State *state = (LV2PluginWrapper_State *)p->handle [0];
assert(state != NULL);
LV2Synth::lv2state_populatePresetsMenu(state, menu);
}
void LV2PluginWrapper::applyPreset(PluginI *p, void *preset)
{
assert(p->instances > 0);
LV2PluginWrapper_State *state = (LV2PluginWrapper_State *)p->handle [0];
assert(state != NULL);
LV2Synth::lv2state_applyPreset(state, static_cast<LilvNode *>(preset));
}
void LV2PluginWrapper_Worker::run()
{
while(true)
{
_mSem.acquire(1);
if(_closing)
break;
makeWork();
}
}
LV2_Worker_Status LV2PluginWrapper_Worker::scheduleWork()
{
if(_mSem.available() != 0)
return LV2_WORKER_ERR_NO_SPACE;
_mSem.release(1);
return LV2_WORKER_SUCCESS;
}
void LV2PluginWrapper_Worker::makeWork()
{
#ifdef DEBUG_LV2
std::cerr << "LV2PluginWrapper_Worker::makeWork" << std::endl;
#endif
if(_state->wrkIface && _state->wrkIface->work)
{
const void *dataBuffer = _state->wrkDataBuffer;
uint32_t dataSize = _state->wrkDataSize;
_state->wrkDataBuffer = NULL;
_state->wrkDataSize = 0;
if(_state->wrkIface->work(lilv_instance_get_handle(_state->handle),
LV2Synth::lv2wrk_respond,
_state,
dataSize,
dataBuffer) != LV2_WORKER_SUCCESS)
{
_state->wrkEndWork = false;
_state->wrkDataBuffer = NULL;
_state->wrkDataSize = 0;
}
}
}
LV2EvBuf::LV2EvBuf(bool isInput, bool oldApi, LV2_URID atomTypeSequence, LV2_URID atomTypeChunk)
:_isInput(isInput), _oldApi(oldApi), _uAtomTypeSequence(atomTypeSequence), _uAtomTypeChunk(atomTypeChunk)
{
if(_isInput)
{
// Resize and fill with initial value.
_buffer.resize(LV2_EVBUF_SIZE, 0);
}
else
{
// Reserve the space.
_buffer.reserve(LV2_EVBUF_SIZE);
// Add one item, the first item.
_buffer.assign(sizeof(LV2_Atom_Sequence), 0);
}
#ifdef LV2_DEBUG
std::cerr << "LV2EvBuf ctor: _buffer size:" << _buffer.size() << " capacity:" << _buffer.capacity() << std::endl;
#endif
resetBuffer();
}
size_t LV2EvBuf::mkPadSize(size_t size)
{
return (size + 7U) & (~7U);
}
void LV2EvBuf::resetPointers(bool r, bool w)
{
if(!r && !w)
return;
size_t ptr = 0;
if(_oldApi)
{
ptr = sizeof(LV2_Event_Buffer);
}
else
{
ptr = sizeof(LV2_Atom_Sequence);
}
if(r)
{
curRPointer = ptr;
}
if(w)
{
curWPointer = ptr;
}
}
void LV2EvBuf::resetBuffer()
{
if(_oldApi)
{
_evbuf = reinterpret_cast<LV2_Event_Buffer *>(&_buffer [0]);
_evbuf->capacity = _buffer.size() - sizeof(LV2_Event_Buffer);
_evbuf->data = reinterpret_cast<uint8_t *>(_evbuf + 1);
_evbuf->event_count = 0;
_evbuf->header_size = sizeof(LV2_Event_Buffer);
_evbuf->stamp_type = LV2_EVENT_AUDIO_STAMP;
_evbuf->size = 0;
}
else
{
_seqbuf = reinterpret_cast<LV2_Atom_Sequence *>(&_buffer [0]);
if(!_isInput)
{
_seqbuf->atom.type = _uAtomTypeChunk;
_seqbuf->atom.size = _buffer.size() - sizeof(LV2_Atom_Sequence);
}
else
{
_seqbuf->atom.type = _uAtomTypeSequence;
_seqbuf->atom.size = sizeof(LV2_Atom_Sequence_Body);
}
_seqbuf->body.unit = 0;
_seqbuf->body.pad = 0;
}
resetPointers(true, true);
}
bool LV2EvBuf::write(uint32_t frames, uint32_t subframes, uint32_t type, uint32_t size, const uint8_t *data)
{
if(!_isInput)
return false;
if(_oldApi)
{
size_t paddedSize = mkPadSize(sizeof(LV2_Event) + size);
size_t resSize = curWPointer + paddedSize;
if(resSize > _buffer.size())
{
std::cerr << "LV2 Event buffer overflow! frames=" << frames << ", size=" << size << std::endl;
return false;
}
LV2_Event *ev = reinterpret_cast<LV2_Event *>(&_buffer [curWPointer]);
ev->frames = frames;
ev->subframes = subframes;
ev->type = type;
ev->size = size;
memcpy((void *)(ev + 1), data, size);
curWPointer += paddedSize;
_evbuf->size += paddedSize;
_evbuf->event_count++;
}
else
{
size_t paddedSize = mkPadSize(sizeof(LV2_Atom_Event) + size);
size_t resSize = curWPointer + paddedSize;
if(resSize > _buffer.size())
{
std::cerr << "LV2 Atom_Event buffer overflow! frames=" << frames << ", size=" << size << std::endl;
return false;
}
LV2_Atom_Event *ev = reinterpret_cast<LV2_Atom_Event *>(&_buffer [curWPointer]);
ev->time.frames = frames;
ev->body.size = size;
ev->body.type = type;
memcpy(LV2_ATOM_BODY(&ev->body), data, size);
_seqbuf->atom.size += paddedSize;
curWPointer += paddedSize;
}
return true;
}
bool LV2EvBuf::read(uint32_t *frames, uint32_t *subframes, uint32_t *type, uint32_t *size, uint8_t **data)
{
*frames = *subframes = *type = *size = 0;
*data = NULL;
if(_isInput)
return false;
if(_oldApi)
{
LV2_Event *ev = reinterpret_cast<LV2_Event *>(&_buffer [curRPointer]);
if((_evbuf->size + sizeof(LV2_Event_Buffer) - curRPointer) < sizeof(LV2_Event))
{
return false;
}
*frames = ev->frames;
*subframes = ev->subframes;
*type = ev->type;
*size = ev->size;
*data = reinterpret_cast<uint8_t *>(ev + 1);
size_t padSize = mkPadSize(sizeof(LV2_Event) + ev->size);
curRPointer += padSize;
}
else
{
LV2_Atom_Event *ev = reinterpret_cast<LV2_Atom_Event *>(&_buffer [curRPointer]);
if((_seqbuf->atom.size + sizeof(LV2_Atom_Sequence) - curRPointer) < sizeof(LV2_Atom_Event))
{
return false;
}
*frames = ev->time.frames;
*type = ev->body.type;
*size = ev->body.size;
*data = reinterpret_cast<uint8_t *>(LV2_ATOM_BODY(&ev->body));
size_t padSize = mkPadSize(sizeof(LV2_Atom_Event) + ev->body.size);
curRPointer += padSize;
}
return true;
}
uint8_t *LV2EvBuf::getRawBuffer()
{
return &_buffer [0];
}
void LV2EvBuf::dump()
{
if(_oldApi){
return;
}
int n = 1;
LV2_Atom_Sequence *b = (LV2_Atom_Sequence *)&_buffer [0];
LV2_ATOM_SEQUENCE_FOREACH(b, s)
{
if(n == 1)
{
fprintf(stderr, "-------------- Atom seq dump START---------------\n");
}
fprintf(stderr, "\tSeq. no.: %d\n", n);
fprintf(stderr, "\t\tFrames: %ld\n", (long)s->time.frames);
fprintf(stderr, "\t\tSize: %d\n", s->body.size);
fprintf(stderr, "\t\tType: %d\n", s->body.type);
fprintf(stderr, "\t\tData (hex):\n");
uint8_t *d = (uint8_t *)LV2_ATOM_BODY(&s->body);
for(uint32_t i = 0; i < s->body.size; i++)
{
if((i % 10) == 0)
{
fprintf(stderr, "\n\t\t");
}
else
{
fprintf(stderr, " ");
}
fprintf(stderr, "0x%02X", d [i]);
}
fprintf(stderr, "\n");
n++;
}
if(n > 1)
{
fprintf(stderr, "-------------- Atom seq dump END---------------\n\n");
}
}
LV2SimpleRTFifo::LV2SimpleRTFifo(size_t size):
fifoSize(size),
itemSize(LV2_RT_FIFO_ITEM_SIZE)
{
eventsBuffer.resize(fifoSize);
assert(eventsBuffer.size() == fifoSize);
readIndex = writeIndex = 0;
for(size_t i = 0; i < fifoSize; ++i)
{
eventsBuffer [i].port_index = 0;
eventsBuffer [i].buffer_size = 0;
eventsBuffer [i].data = new char [itemSize];
}
}
LV2SimpleRTFifo::~LV2SimpleRTFifo()
{
for(size_t i = 0; i < fifoSize; ++i)
{
delete [] eventsBuffer [i].data;
}
}
bool LV2SimpleRTFifo::put(uint32_t port_index, uint32_t size, const void *data)
{
if(size > itemSize)
{
#ifdef DEBUG_LV2
std::cerr << "LV2SimpleRTFifo:put(): size("<<size<<") is too big" << std::endl;
#endif
return false;
}
size_t i = writeIndex;
bool found = false;
do
{
if(eventsBuffer.at(i).buffer_size == 0)
{
found = true;
break;
}
i++;
i %= fifoSize;
}
while(i != writeIndex);
if(!found)
{
#ifdef DEBUG_LV2
std::cerr << "LV2SimpleRTFifo:put(): fifo is full" << std::endl;
#endif
return false;
}
#ifdef DEBUG_LV2
// std::cerr << "LV2SimpleRTFifo:put(): used index = " << i << std::endl;
#endif
memcpy(eventsBuffer.at(i).data, data, size);
eventsBuffer.at(i).port_index = port_index;
__sync_fetch_and_add(&eventsBuffer.at(i).buffer_size, size);
writeIndex = (i + 1) % fifoSize;
return true;
}
bool LV2SimpleRTFifo::get(uint32_t *port_index, size_t *szOut, char *data_out)
{
size_t i = readIndex;
bool found = false;
if(eventsBuffer.at(i).buffer_size != 0)
{
found = true;
}
if(!found)
{
#ifdef DEBUG_LV2
//std::cerr << "LV2SimpleRTFifo:get(): fifo is empty" << std::endl;
#endif
return false;
}
#ifdef DEBUG_LV2
//std::cerr << "LV2SimpleRTFifo:get(): used index = " << i << std::endl;
#endif
*szOut = eventsBuffer.at(i).buffer_size;
*port_index = eventsBuffer [i].port_index;
memcpy(data_out, eventsBuffer [i].data, *szOut);
__sync_fetch_and_and(&eventsBuffer.at(i).buffer_size, 0);
readIndex = (i + 1) % fifoSize;
return true;
}
LV2UridBiMap::LV2UridBiMap() : nextId ( 1 ) {_map.clear(); _rmap.clear();}
LV2UridBiMap::~LV2UridBiMap()
{
LV2_SYNTH_URID_MAP::iterator it = _map.begin();
for(;it != _map.end(); ++it)
{
free((void*)(*it).first);
}
}
LV2_URID LV2UridBiMap::map(const char *uri)
{
std::pair<LV2_SYNTH_URID_MAP::iterator, bool> p;
uint32_t id;
idLock.lock();
LV2_SYNTH_URID_MAP::iterator it = _map.find(uri);
if(it == _map.end())
{
const char *mUri = strdup(uri);
p = _map.insert ( std::make_pair ( mUri, nextId ) );
_rmap.insert ( std::make_pair ( nextId, mUri ) );
nextId++;
id = p.first->second;
}
else
id = it->second;
idLock.unlock();
return id;
}
const char *LV2UridBiMap::unmap(uint32_t id)
{
LV2_SYNTH_URID_RMAP::iterator it = _rmap.find ( id );
if ( it != _rmap.end() ) {
return it->second;
}
return NULL;
}
}
#else //LV2_SUPPORT
namespace MusECore
{
void initLV2() {}
}
#endif
|