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
|
/*
* Copyright (C) 2021 Linux Studio Plugins Project <https://lsp-plug.in/>
* (C) 2021 Vladimir Sadovnikov <sadko4u@gmail.com>
*
* This file is part of lsp-plugin-fw
* Created on: 28 нояб. 2021 г.
*
* lsp-plugin-fw is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* lsp-plugin-fw 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with lsp-plugin-fw. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef LSP_PLUG_IN_PLUG_FW_WRAP_LV2_IMPL_WRAPPER_H_
#define LSP_PLUG_IN_PLUG_FW_WRAP_LV2_IMPL_WRAPPER_H_
#include <lsp-plug.in/plug-fw/version.h>
#include <lsp-plug.in/plug-fw/wrap/lv2/wrapper.h>
#include <lsp-plug.in/plug-fw/meta/manifest.h>
#include <lsp-plug.in/plug-fw/wrap/lv2/types.h>
#define LSP_LEGACY_KVT_URI LSP_LV2_BASE_URI "ui/lv2"
namespace lsp
{
namespace lv2
{
//---------------------------------------------------------------------
void Wrapper::LV2KVTListener::created(core::KVTStorage *storage, const char *id, const core::kvt_param_t *param, size_t pending)
{
pWrapper->state_changed();
}
void Wrapper::LV2KVTListener::changed(core::KVTStorage *storage, const char *id, const core::kvt_param_t *oval, const core::kvt_param_t *nval, size_t pending)
{
pWrapper->state_changed();
}
void Wrapper::LV2KVTListener::removed(core::KVTStorage *storage, const char *id, const core::kvt_param_t *param, size_t pending)
{
pWrapper->state_changed();
}
//---------------------------------------------------------------------
ssize_t Wrapper::compare_ports_by_urid(const lv2::Port *a, const lv2::Port *b)
{
return ssize_t(a->get_urid()) - ssize_t(b->get_urid());
}
Wrapper::Wrapper(plug::Module *plugin, resource::ILoader *loader, lv2::Extensions *ext):
plug::IWrapper(plugin, loader),
sKVTListener(this)
{
pPlugin = plugin;
pExt = ext;
pExecutor = NULL;
pAtomIn = NULL;
pAtomOut = NULL;
pLatency = NULL;
nPatchReqs = 0;
nStateReqs = 0;
nSyncTime = 0;
nSyncSamples = 0;
nClients = 0;
nDirectClients = 0;
sSurface.data = NULL;
sSurface.width = 0;
sSurface.height = 0;
sSurface.stride = 0;
bQueueDraw = false;
bUpdateSettings = true;
fSampleRate = DEFAULT_SAMPLE_RATE;
pOscPacket = reinterpret_cast<uint8_t *>(::malloc(OSC_PACKET_MAX));
nStateMode = SM_LOADING;
nDumpReq = 0;
nDumpResp = 0;
pPackage = NULL;
pKVTDispatcher = NULL;
pSamplePlayer = NULL;
nPlayPosition = 0;
nPlayLength = 0;
}
Wrapper::~Wrapper()
{
pPlugin = NULL;
pExt = NULL;
pExecutor = NULL;
pAtomIn = NULL;
pAtomOut = NULL;
pLatency = NULL;
nPatchReqs = 0;
nStateReqs = 0;
nSyncTime = 0;
nSyncSamples = 0;
nClients = 0;
nDirectClients = 0;
sSurface.data = NULL;
sSurface.width = 0;
sSurface.height = 0;
sSurface.stride = 0;
pPackage = NULL;
pKVTDispatcher = NULL;
pSamplePlayer = NULL;
}
lv2::Port *Wrapper::port_by_urid(LV2_URID urid)
{
// Try to find the corresponding port
ssize_t first = 0, last = vPluginPorts.size() - 1;
while (first <= last)
{
size_t center = size_t(first + last) >> 1;
lv2::Port *p = vPluginPorts.uget(center);
if (urid == p->get_urid())
return p;
else if (urid < p->get_urid())
last = center - 1;
else
first = center + 1;
}
return NULL;
}
lv2::Port *Wrapper::port(const char *id)
{
for (size_t i=0, n = vPluginPorts.size(); i<n; ++i)
{
lv2::Port *p = vPluginPorts.uget(i);
if (p == NULL)
continue;
const meta::port_t *meta = p->metadata();
if (meta == NULL)
continue;
if (!strcmp(meta->id, id))
return p;
}
return NULL;
}
status_t Wrapper::init(float srate)
{
// Update sample rate
fSampleRate = srate;
// Get plugin metadata
const meta::plugin_t *m = pPlugin->metadata();
status_t res;
// Load package information
io::IInStream *is = resources()->read_stream(LSP_BUILTIN_PREFIX "manifest.json");
if (is == NULL)
{
lsp_error("No manifest.json found in resources");
return STATUS_BAD_STATE;
}
res = meta::load_manifest(&pPackage, is);
is->close();
delete is;
if (res != STATUS_OK)
{
lsp_error("Error while reading manifest file");
return res;
}
// Create ports
lsp_trace("Creaing ports");
lltl::parray<plug::IPort> plugin_ports;
for (const meta::port_t *meta = m->ports ; meta->id != NULL; ++meta)
create_port(&plugin_ports, meta, NULL, false);
// Sort port lists
vPluginPorts.qsort(compare_ports_by_urid);
vMeshPorts.qsort(compare_ports_by_urid);
vStreamPorts.qsort(compare_ports_by_urid);
vFrameBufferPorts.qsort(compare_ports_by_urid);
// Need to create and start KVT dispatcher?
lsp_trace("Plugin extensions=0x%x", int(m->extensions));
if (m->extensions & meta::E_KVT_SYNC)
{
lsp_trace("Binding KVT listener");
sKVT.bind(&sKVTListener);
lsp_trace("Creating KVT dispatcher thread...");
pKVTDispatcher = new core::KVTDispatcher(&sKVT, &sKVTMutex);
lsp_trace("Starting KVT dispatcher thread...");
pKVTDispatcher->start();
}
// Initialize plugin
lsp_trace("Initializing plugin");
pPlugin->init(this, plugin_ports.array());
pPlugin->set_sample_rate(srate);
bUpdateSettings = true;
// Create sample player if required
if (m->extensions & meta::E_FILE_PREVIEW)
{
pSamplePlayer = new core::SamplePlayer(m);
if (pSamplePlayer == NULL)
return STATUS_NO_MEM;
pSamplePlayer->init(this, plugin_ports.array(), plugin_ports.size());
pSamplePlayer->set_sample_rate(srate);
}
// Update refresh rate
nSyncSamples = srate / pExt->ui_refresh_rate();
nClients = 0;
return STATUS_OK;
}
void Wrapper::destroy()
{
// Destroy sample player
if (pSamplePlayer != NULL)
{
pSamplePlayer->destroy();
delete pSamplePlayer;
pSamplePlayer = NULL;
}
// Stop KVT dispatcher
if (pKVTDispatcher != NULL)
{
lsp_trace("Stopping KVT dispatcher thread...");
pKVTDispatcher->cancel();
pKVTDispatcher->join();
delete pKVTDispatcher;
sKVT.unbind(&sKVTListener);
}
// Drop surface
sSurface.data = NULL;
sSurface.width = 0;
sSurface.height = 0;
sSurface.stride = 0;
// Shutdown and delete executor if exists
if (pExecutor != NULL)
{
pExecutor->shutdown();
delete pExecutor;
pExecutor = NULL;
}
// Drop plugin
if (pPlugin != NULL)
{
pPlugin->destroy();
delete pPlugin;
pPlugin = NULL;
}
// Cleanup ports
for (size_t i=0; i < vAllPorts.size(); ++i)
{
lsp_trace("destroy port id=%s", vAllPorts[i]->metadata()->id);
delete vAllPorts[i];
}
// Cleanup generated metadata
for (size_t i=0; i<vGenMetadata.size(); ++i)
{
lsp_trace("destroy generated port metadata %p", vGenMetadata[i]);
drop_port_metadata(vGenMetadata[i]);
}
// Destroy manifest
if (pPackage != NULL)
{
meta::free_manifest(pPackage);
pPackage = NULL;
}
vAllPorts.flush();
vExtPorts.flush();
vMeshPorts.flush();
vStreamPorts.flush();
vMidiPorts.flush();
vOscPorts.flush();
vFrameBufferPorts.flush();
vPluginPorts.flush();
vGenMetadata.flush();
// Delete temporary buffer for OSC serialization
if (pOscPacket != NULL)
{
::free(pOscPacket);
pOscPacket = NULL;
}
// Drop extensions
if (pExt != NULL)
{
delete pExt;
pExt = NULL;
}
// Drop loader
if (pLoader != NULL)
{
delete pLoader;
pLoader = NULL;
}
}
lv2::Port *Wrapper::create_port(lltl::parray<plug::IPort> *plugin_ports, const meta::port_t *p, const char *postfix, bool virt)
{
lv2::Port *result = NULL;
switch (p->role)
{
case meta::R_MESH:
if (pExt->atom_supported())
{
result = new lv2::MeshPort(p, pExt);
vMeshPorts.add(result);
}
else
result = new lv2::Port(p, pExt, false);
vPluginPorts.add(result);
plugin_ports->add(result);
break;
case meta::R_STREAM:
if (pExt->atom_supported())
{
result = new lv2::StreamPort(p, pExt);
vStreamPorts.add(result);
}
else
result = new lv2::Port(p, pExt, false);
vPluginPorts.add(result);
plugin_ports->add(result);
break;
case meta::R_FBUFFER:
if (pExt->atom_supported())
{
result = new lv2::FrameBufferPort(p, pExt);
vFrameBufferPorts.add(result);
}
else
result = new lv2::Port(p, pExt, false);
vPluginPorts.add(result);
plugin_ports->add(result);
break;
case meta::R_PATH:
if (pExt->atom_supported())
result = new lv2::PathPort(p, pExt);
else
result = new lv2::Port(p, pExt, false);
vPluginPorts.add(result);
plugin_ports->add(result);
break;
case meta::R_MIDI:
if (pExt->atom_supported())
{
result = new lv2::MidiPort(p, pExt);
vMidiPorts.add(result);
}
else
result = new lv2::Port(p, pExt, false);
plugin_ports->add(result);
break;
case meta::R_OSC:
if (pExt->atom_supported())
{
result = new lv2::OscPort(p, pExt);
vOscPorts.add(result);
}
else
result = new lv2::Port(p, pExt, false);
plugin_ports->add(result);
break;
case meta::R_AUDIO:
result = new lv2::AudioPort(p, pExt);
vPluginPorts.add(result);
vAudioPorts.add(static_cast<lv2::AudioPort *>(result));
plugin_ports->add(result);
if (postfix == NULL)
{
result->set_id(vExtPorts.size());
vExtPorts.add(result);
lsp_trace("Added external port id=%s, external_id=%d", result->metadata()->id, int(vExtPorts.size() - 1));
}
break;
case meta::R_CONTROL:
case meta::R_METER:
if (meta::is_out_port(p))
result = new lv2::OutputPort(p, pExt);
else
result = new lv2::InputPort(p, pExt, virt);
vPluginPorts.add(result);
plugin_ports->add(result);
if (postfix == NULL)
{
result->set_id(vExtPorts.size());
vExtPorts.add(result);
lsp_trace("Added external port id=%s, external_id=%d", result->metadata()->id, int(vExtPorts.size() - 1));
}
break;
case meta::R_BYPASS:
if (meta::is_out_port(p))
result = new lv2::Port(p, pExt, false);
else
result = new lv2::BypassPort(p, pExt);
vPluginPorts.add(result);
plugin_ports->add(result);
if (postfix == NULL)
{
result->set_id(vExtPorts.size());
vExtPorts.add(result);
lsp_trace("Added bypass port id=%s, external_id=%d", result->metadata()->id, int(vExtPorts.size() - 1));
}
break;
case meta::R_PORT_SET:
{
char postfix_buf[MAX_PARAM_ID_BYTES];
lv2::PortGroup *pg = new lv2::PortGroup(p, pExt, virt);
// Add Port Set immediately
vPluginPorts.add(pg);
vAllPorts.add(pg);
plugin_ports->add(pg);
// Generate nested ports
for (size_t row=0; row<pg->rows(); ++row)
{
// Generate postfix
snprintf(postfix_buf, sizeof(postfix_buf)-1, "%s_%d", (postfix != NULL) ? postfix : "", int(row));
// Clone port metadata
meta::port_t *cm = meta::clone_port_metadata(p->members, postfix_buf);
if (cm == NULL)
continue;
vGenMetadata.add(cm);
size_t col = 0;
for (; cm->id != NULL; ++cm, ++col)
{
if (meta::is_growing_port(cm))
cm->start = cm->min + ((cm->max - cm->min) * row) / float(pg->rows());
else if (meta::is_lowering_port(cm))
cm->start = cm->max - ((cm->max - cm->min) * row) / float(pg->rows());
// Recursively generate new ports associated with the port set
create_port(plugin_ports, cm, postfix_buf, true);
}
}
break;
}
default:
break;
}
// Register created port for garbage collection
if (result != NULL)
vAllPorts.add(result);
return result;
}
void Wrapper::connect(size_t id, void *data)
{
size_t ports_count = vExtPorts.size();
if (id < ports_count)
{
lv2::Port *p = vExtPorts.get(id);
if (p != NULL)
p->bind(data);
}
else
{
switch (id - ports_count)
{
case 0: pAtomIn = data; break;
case 1: pAtomOut = data; break;
case 2: pLatency = reinterpret_cast<float *>(data); break;
default:
lsp_warn("Unknown port number: %d", int(id));
break;
}
}
}
void Wrapper::run(size_t samples)
{
// Activate/deactivate the UI
ssize_t clients = nClients + nDirectClients;
if (clients > 0)
{
if (!pPlugin->ui_active())
pPlugin->activate_ui();
}
else if (pPlugin->ui_active())
pPlugin->deactivate_ui();
// First pre-process transport ports
clear_midi_ports();
receive_atoms(samples);
// Pre-rocess regular ports
size_t smode = nStateMode;
for (size_t i=0, n=vAllPorts.size(); i<n; ++i)
{
// Get port
lv2::Port *port = vAllPorts.uget(i);
if (port == NULL)
continue;
// Pre-process data in port
if (port->pre_process(samples))
{
lsp_trace("port changed: %s, value=%f", port->metadata()->id, port->value());
bUpdateSettings = true;
if ((smode != SM_LOADING) && (port->is_virtual()))
change_state_atomic(SM_SYNC, SM_CHANGED);
}
}
// Commit state
if (smode == SM_LOADING)
change_state_atomic(SM_LOADING, SM_SYNC);
// Check that input parameters have changed
if (bUpdateSettings)
{
pPlugin->update_settings();
bUpdateSettings = false;
}
// Need to dump state?
uatomic_t dump_req = nDumpReq;
if (dump_req != nDumpResp)
{
dump_plugin_state();
nDumpResp = dump_req;
}
// Call the main processing unit (split data buffers into chunks not greater than MaxBlockLength)
size_t n_audio_ports = vAudioPorts.size();
for (size_t off=0; off < samples; )
{
size_t to_process = lsp_min(samples - off, pExt->nMaxBlockLength);
// Sanitize input data
for (size_t i=0; i<n_audio_ports; ++i)
{
lv2::AudioPort *port = vAudioPorts.uget(i);
if (port != NULL)
port->sanitize_before(off, to_process);
}
// Process samples
pPlugin->process(to_process);
if (pSamplePlayer != NULL)
pSamplePlayer->process(to_process);
// Sanitize output data
for (size_t i=0; i<n_audio_ports; ++i)
{
lv2::AudioPort *port = vAudioPorts.uget(i);
if (port != NULL)
port->sanitize_after(off, to_process);
}
off += to_process;
}
// Transmit atoms (if possible)
transmit_atoms(samples);
clear_midi_ports();
// Post-process regular ports for changes
for (size_t i=0, n=vAllPorts.size(); i<n; ++i)
{
// Get port
lv2::Port *port = vAllPorts.uget(i);
if (port == NULL)
continue;
if (port != NULL)
port->post_process(samples);
}
// Transmit latency (if possible)
if (pLatency != NULL)
*pLatency = pPlugin->latency();
}
void Wrapper::clear_midi_ports()
{
// Clear all MIDI ports
for (size_t i=0, n=vMidiPorts.size(); i<n; ++i)
{
lv2::Port *p = vMidiPorts.uget(i);
if (!meta::is_midi_port(p->metadata()))
continue;
plug::midi_t *midi = p->buffer<plug::midi_t>();
if (midi != NULL)
midi->clear();
}
}
void Wrapper::receive_midi_event(const LV2_Atom_Event *ev)
{
// Are there any MIDI input ports in plugin?
if (vMidiPorts.size() <= 0)
return;
// Decode MIDI event
midi::event_t me;
const uint8_t *bytes = reinterpret_cast<const uint8_t*>(ev + 1);
// Debug
#ifdef LSP_TRACE
#define TRACE_KEY(x) case midi::MIDI_MSG_ ## x: evt_type = #x; break;
lsp_trace("midi dump: %02x %02x %02x", int(bytes[0]), int(bytes[1]), int(bytes[2]));
char tmp_evt_type[32];
const char *evt_type = NULL;
switch (me.type)
{
TRACE_KEY(NOTE_OFF)
TRACE_KEY(NOTE_ON)
TRACE_KEY(NOTE_PRESSURE)
TRACE_KEY(NOTE_CONTROLLER)
TRACE_KEY(PROGRAM_CHANGE)
TRACE_KEY(CHANNEL_PRESSURE)
TRACE_KEY(PITCH_BEND)
TRACE_KEY(SYSTEM_EXCLUSIVE)
TRACE_KEY(MTC_QUARTER)
TRACE_KEY(SONG_POS)
TRACE_KEY(SONG_SELECT)
TRACE_KEY(TUNE_REQUEST)
TRACE_KEY(END_EXCLUSIVE)
TRACE_KEY(CLOCK)
TRACE_KEY(START)
TRACE_KEY(CONTINUE)
TRACE_KEY(STOP)
TRACE_KEY(ACTIVE_SENSING)
TRACE_KEY(RESET)
default:
snprintf(tmp_evt_type, sizeof(tmp_evt_type), "UNKNOWN(0x%02x)", int(me.type));
evt_type = tmp_evt_type;
break;
}
lsp_trace("MIDI Event: type=%s, timestamp=%ld", evt_type, (long)(me.timestamp));
#undef TRACE_KEY
#endif /* LSP_TRACE */
if (midi::decode(&me, bytes) <= 0)
{
lsp_warn("Could not decode MIDI message");
return;
}
// Put the event to the queue
me.timestamp = uint32_t(ev->time.frames);
// For each MIDI port: add event to the queue
for (size_t i=0, n=vMidiPorts.size(); i<n; ++i)
{
lv2::Port *midi = vMidiPorts.uget(i);
if (!meta::is_midi_in_port(midi->metadata()))
continue;
plug::midi_t *buf = midi->buffer<plug::midi_t>();
if (buf == NULL)
continue;
if (!buf->push(me))
lsp_warn("MIDI event queue overflow");
}
}
void Wrapper::receive_raw_osc_event(osc::parse_frame_t *frame)
{
osc::parse_token_t token;
status_t res = osc::parse_token(frame, &token);
if (res != STATUS_OK)
return;
if (token == osc::PT_BUNDLE)
{
osc::parse_frame_t child;
uint64_t time_tag;
status_t res = osc::parse_begin_bundle(&child, frame, &time_tag);
if (res != STATUS_OK)
return;
receive_raw_osc_event(&child); // Perform recursive call
osc::parse_end(&child);
}
else if (token == osc::PT_MESSAGE)
{
const void *msg_start;
size_t msg_size;
const char *msg_addr;
// Perform address lookup and routing
status_t res = osc::parse_raw_message(frame, &msg_start, &msg_size, &msg_addr);
if (res != STATUS_OK)
return;
lsp_trace("Received OSC message of %d bytes, address=%s", int(msg_size), msg_addr);
osc::dump_packet(msg_start, msg_size);
if (::strstr(msg_addr, "/KVT/") == msg_addr)
pKVTDispatcher->submit(msg_start, msg_size);
else
{
for (size_t i=0, n=vOscPorts.size(); i<n; ++i)
{
lv2::Port *p = vOscPorts.uget(i);
if (!meta::is_osc_in_port(p->metadata()))
continue;
// Submit message to the buffer
plug::osc_buffer_t *buf = p->buffer<plug::osc_buffer_t>();
if (buf != NULL)
buf->submit(msg_start, msg_size);
}
}
}
}
void Wrapper::receive_atom_object(const LV2_Atom_Event *ev)
{
// Analyze object type
const LV2_Atom_Object *obj = reinterpret_cast<const LV2_Atom_Object*>(&ev->body);
// lsp_trace("obj->body.otype (%d) = %s", int(obj->body.otype), pExt->unmap_urid(obj->body.otype));
// lsp_trace("obj->body.id (%d) = %s", int(obj->body.id), pExt->unmap_urid(obj->body.id));
if (obj->body.otype == pExt->uridPatchGet) // PatchGet request
{
lsp_trace("triggered patch request");
#ifdef LSP_TRACE
for (
LV2_Atom_Property_Body *body = lv2_atom_object_begin(&obj->body) ;
!lv2_atom_object_is_end(&obj->body, obj->atom.size, body) ;
body = lv2_atom_object_next(body)
)
{
lsp_trace("body->key (%d) = %s", int(body->key), pExt->unmap_urid(body->key));
lsp_trace("body->value.type (%d) = %s", int(body->value.type), pExt->unmap_urid(body->value.type));
}
#endif /* LSP_TRACE */
// Increment the number of patch requests
nPatchReqs ++;
}
else if (obj->body.otype == pExt->uridPatchSet) // PatchSet request
{
// Parse atom body
const LV2_Atom_URID *key = NULL;
const LV2_Atom *value = NULL;
for (
LV2_Atom_Property_Body *body = lv2_atom_object_begin(&obj->body) ;
!lv2_atom_object_is_end(&obj->body, obj->atom.size, body) ;
body = lv2_atom_object_next(body)
)
{
lsp_trace("body->key (%d) = %s", int(body->key), pExt->unmap_urid(body->key));
lsp_trace("body->value.type (%d) = %s", int(body->value.type), pExt->unmap_urid(body->value.type));
if ((body->key == pExt->uridPatchProperty) && (body->value.type == pExt->uridAtomUrid))
{
key = reinterpret_cast<const LV2_Atom_URID *>(&body->value);
lsp_trace("body->value.body (%d) = %s", int(key->body), pExt->unmap_urid(key->body));
}
else if (body->key == pExt->uridPatchValue)
value = &body->value;
if ((key != NULL) && (value != NULL))
{
lv2::Port *p = port_by_urid(key->body);
if ((p != NULL) && (p->get_type_urid() == value->type))
{
lsp_trace("forwarding patch message to port %s", p->metadata()->id);
if (p->deserialize(value, 0)) // No flags for simple PATCH message
{
// Change state if it is a virtual port
if (p->is_virtual())
state_changed();
}
}
key = NULL;
value = NULL;
}
}
}
else if (obj->body.otype == pExt->uridTimePosition) // Time position notification
{
plug::position_t pos = sPosition;
pos.sampleRate = fSampleRate;
pos.ticksPerBeat = DEFAULT_TICKS_PER_BEAT;
for (
LV2_Atom_Property_Body *body = lv2_atom_object_begin(&obj->body) ;
!lv2_atom_object_is_end(&obj->body, obj->atom.size, body) ;
body = lv2_atom_object_next(body)
)
{
// lsp_trace("body->key (%d) = %s", int(body->key), pExt->unmap_urid(body->key));
// lsp_trace("body->value.type (%d) = %s", int(body->value.type), pExt->unmap_urid(body->value.type));
if ((body->key == pExt->uridTimeFrame) && (body->value.type == pExt->forge.Long))
pos.frame = (reinterpret_cast<LV2_Atom_Long *>(&body->value))->body;
else if ((body->key == pExt->uridTimeSpeed) && (body->value.type == pExt->forge.Float))
pos.speed = (reinterpret_cast<LV2_Atom_Float *>(&body->value))->body;
else if ((body->key == pExt->uridTimeBeatsPerMinute) && (body->value.type == pExt->forge.Float))
pos.beatsPerMinute = (reinterpret_cast<LV2_Atom_Float *>(&body->value))->body;
else if ((body->key == pExt->uridTimeBeatUnit) && (body->value.type == pExt->forge.Int))
pos.denominator = (reinterpret_cast<LV2_Atom_Int *>(&body->value))->body;
else if ((body->key == pExt->uridTimeBeatsPerBar) && (body->value.type == pExt->forge.Float))
pos.numerator = (reinterpret_cast<LV2_Atom_Float *>(&body->value))->body;
else if ((body->key == pExt->uridTimeBarBeat) && (body->value.type == pExt->forge.Float))
pos.tick = (reinterpret_cast<LV2_Atom_Float *>(&body->value))->body * pos.ticksPerBeat;
}
// lsp_trace("triggered timePosition event\n"
// " frame = %lld\n"
// " speed = %f\n"
// " bpm = %f\n"
// " numerator = %f\n"
// " denominator= %f\n"
// " tick = %f\n",
// (long long)(pos.frame), pos.speed, pos.beatsPerMinute, pos.denominator, pos.numerator, pos.tick
// );
// Call plugin callback and update position
bUpdateSettings = pPlugin->set_position(&pos);
sPosition = pos;
}
else if (obj->body.otype == pExt->uridUINotification)
{
if (obj->body.id == pExt->uridConnectUI)
{
nClients ++;
nStateReqs ++;
lsp_trace("UI has connected, current number of clients=%d", int(nClients));
if (pKVTDispatcher != NULL)
pKVTDispatcher->connect_client();
// Notify all ports that UI has connected to backend
for (size_t i=0, n = vAllPorts.size(); i<n; ++i)
{
lv2::Port *p = vAllPorts.get(i);
if (p != NULL)
p->ui_connected();
}
}
else if (obj->body.id == pExt->uridDisconnectUI)
{
nClients --;
if (pKVTDispatcher != NULL)
pKVTDispatcher->disconnect_client();
lsp_trace("UI has disconnected, current number of clients=%d", int(nClients));
}
else if (obj->body.id == pExt->uridDumpState)
{
lsp_trace("Received DUMP_STATE event");
atomic_add(&nDumpReq, 1);
}
}
else if (obj->body.otype == pExt->uridPlayRequestType)
{
if (pSamplePlayer != NULL)
{
char *file = pSamplePlayer->requested_file_name(); // Destination buffer to store the file name
wsize_t position = 0;
bool release = false;
file[0] = '\0';
for (
LV2_Atom_Property_Body *body = lv2_atom_object_begin(&obj->body) ;
!lv2_atom_object_is_end(&obj->body, obj->atom.size, body) ;
body = lv2_atom_object_next(body)
)
{
// lsp_trace("body->key (%d) = %s", int(body->key), pExt->unmap_urid(body->key));
// lsp_trace("body->value.type (%d) = %s", int(body->value.type), pExt->unmap_urid(body->value.type));
if ((body->key == pExt->uridPlayRequestFileName) && (body->value.type == pExt->forge.Path))
{
// Read the path string
const LV2_Atom *atom = &body->value;
lv2_set_string(file, PATH_MAX, reinterpret_cast<const char *>(atom + 1), atom->size);
}
else if ((body->key == pExt->uridPlayRequestPosition) && (body->value.type == pExt->forge.Long))
position = (reinterpret_cast<LV2_Atom_Long *>(&body->value))->body;
else if ((body->key == pExt->uridPlayRequestRelease) && (body->value.type == pExt->forge.Bool))
release = (reinterpret_cast<LV2_Atom_Long *>(&body->value))->body != 0;
}
// Submit the playback request and make the play position out-of-sync
pSamplePlayer->play_sample(position, release);
}
}
else
{
lsp_trace(
"Unknown object: \n"
" ev->body.type (%d) = %s\n"
" obj->body.otype (%d) = %s\n"
" obj->body.id (%d) = %s",
int(ev->body.type), pExt->unmap_urid(ev->body.type),
int(obj->body.otype), pExt->unmap_urid(obj->body.otype),
int(obj->body.id), pExt->unmap_urid(obj->body.id));
}
}
void Wrapper::receive_atoms(size_t samples)
{
// Update synchronization
if (nSyncTime <= 0)
{
size_t n_ports = vMeshPorts.size();
for (size_t i=0; i<n_ports; ++i)
{
plug::mesh_t *mesh = vMeshPorts[i]->buffer<plug::mesh_t>();
// lsp_trace("Mesh id=%s is waiting=%s", vMeshPorts[i]->metadata()->id, (mesh->isWaiting()) ? "true" : "false");
if ((mesh != NULL) && (mesh->isWaiting()))
mesh->cleanup();
}
}
// Get sequence
if (pAtomIn == NULL)
return;
const LV2_Atom_Sequence *seq = reinterpret_cast<const LV2_Atom_Sequence *>(pAtomIn);
// lsp_trace("pSequence->atom.type (%d) = %s", int(pSequence->atom.type), pExt->unmap_urid(pSequence->atom.type));
// lsp_trace("pSequence->atom.size = %d", int(pSequence->atom.size));
for (
const LV2_Atom_Event* ev = lv2_atom_sequence_begin(&seq->body);
!lv2_atom_sequence_is_end(&seq->body, seq->atom.size, ev);
ev = lv2_atom_sequence_next(ev)
)
{
// lsp_trace("ev->body.type (%d) = %s", int(ev->body.type), pExt->unmap_urid(ev->body.type));
if (ev->body.type == pExt->uridMidiEventType)
receive_midi_event(ev);
else if (ev->body.type == pExt->uridOscRawPacket)
{
osc::parser_t parser;
osc::parser_frame_t root;
status_t res = osc::parse_begin(&root, &parser, &ev[1], ev->body.size);
if (res == STATUS_OK)
{
receive_raw_osc_event(&root);
osc::parse_end(&root);
osc::parse_destroy(&parser);
}
}
else if ((ev->body.type == pExt->uridObject) || (ev->body.type == pExt->uridBlank))
receive_atom_object(ev);
}
}
void Wrapper::transmit_time_position_to_clients()
{
LV2_Atom_Forge_Frame frame;
pExt->forge_frame_time(0); // Event header
pExt->forge_object(&frame, 0, pExt->uridTimePosition);
pExt->forge_key(pExt->uridTimeFrame);
pExt->forge_long(int64_t(sPosition.frame));
pExt->forge_key(pExt->uridTimeFrameRate);
pExt->forge_float(fSampleRate);
pExt->forge_key(pExt->uridTimeSpeed);
pExt->forge_float(sPosition.speed);
pExt->forge_key(pExt->uridTimeBarBeat);
pExt->forge_float(sPosition.tick / sPosition.ticksPerBeat);
pExt->forge_key(pExt->uridTimeBar);
pExt->forge_long(0);
pExt->forge_key(pExt->uridTimeBeatUnit);
pExt->forge_int(int(sPosition.denominator));
pExt->forge_key(pExt->uridTimeBeatUnit);
pExt->forge_float(sPosition.numerator);
pExt->forge_key(pExt->uridTimeBeatsPerMinute);
pExt->forge_float(sPosition.beatsPerMinute);
pExt->forge_pop(&frame);
}
void Wrapper::transmit_play_position_to_clients()
{
if (pSamplePlayer == NULL)
return;
wssize_t position = pSamplePlayer->position();
wssize_t length = pSamplePlayer->sample_length();
if ((nPlayPosition == position) && (nPlayLength == length))
return;
lsp_trace("position = %lld, length=%lld", (long long)position, (long long)length);
LV2_Atom_Forge_Frame frame;
pExt->forge_frame_time(0); // Event header
pExt->forge_object(&frame, pExt->uridPlayPositionUpdate, pExt->uridPlayPositionType);
pExt->forge_key(pExt->uridPlayPositionPosition);
pExt->forge_long(position);
pExt->forge_key(pExt->uridPlayPositionLength);
pExt->forge_long(length);
pExt->forge_pop(&frame);
// Commit new position
nPlayPosition = position;
nPlayLength = length;
}
void Wrapper::transmit_port_data_to_clients(bool sync_req, bool patch_req, bool state_req)
{
// Serialize time/position of plugin
LV2_Atom_Forge_Frame frame;
// Serialize pending for transmission ports
for (size_t i=0, n = vPluginPorts.size(); i<n; ++i)
{
// Get port
lv2::Port *p = vPluginPorts[i];
if (p == NULL)
continue;
// Skip MESH, FBUFFER, PATH ports visible in global space
switch (p->metadata()->role)
{
case meta::R_AUDIO:
case meta::R_MIDI:
case meta::R_OSC:
case meta::R_UI_SYNC:
case meta::R_MESH:
case meta::R_STREAM:
case meta::R_FBUFFER:
continue;
case meta::R_PATH:
if (p->tx_pending()) // Tranmission request pending?
break;
if (state_req) // State request pending?
break;
if ((p->get_id() >= 0) && (patch_req)) // Global port and patch request pending?
break;
continue;
default:
if (p->tx_pending()) // Transmission request pending?
break;
if (state_req) // State request pending?
break;
continue;
}
// Check that we need to transmit the value
if ((!state_req) && (!p->tx_pending()))
continue;
// Create patch message containing valule of the port
lsp_trace("Serialize port id=%s, value=%f", p->metadata()->id, p->value());
pExt->forge_frame_time(0);
pExt->forge_object(&frame, pExt->uridPatchMessage, pExt->uridPatchSet);
pExt->forge_key(pExt->uridPatchProperty);
pExt->forge_urid(p->get_urid());
pExt->forge_key(pExt->uridPatchValue);
p->serialize();
pExt->forge_pop(&frame);
}
// Serialize meshes (it's own primitive MESH)
for (size_t i=0, n=vMeshPorts.size(); i<n; ++i)
{
lv2::Port *p = vMeshPorts[i];
if (p == NULL)
continue;
if ((!sync_req) && (!p->tx_pending()))
continue;
plug::mesh_t *mesh = p->buffer<plug::mesh_t>();
if ((mesh == NULL) || (!mesh->containsData()))
continue;
// lsp_trace("transmit mesh id=%s", p->metadata()->id);
pExt->forge_frame_time(0); // Event header
pExt->forge_object(&frame, p->get_urid(), pExt->uridMeshType);
p->serialize();
pExt->forge_pop(&frame);
// Cleanup data of the mesh for refill
mesh->markEmpty();
}
// Serialize streams (it's own primitive STREAM)
for (size_t i=0, n=vStreamPorts.size(); i<n; ++i)
{
lv2::Port *p = vStreamPorts[i];
if ((p == NULL) || (!p->tx_pending()))
continue;
plug::stream_t *s = p->buffer<plug::stream_t>();
if (s == NULL)
continue;
pExt->forge_frame_time(0); // Event header
pExt->forge_object(&frame, p->get_urid(), pExt->uridStreamType);
p->serialize();
pExt->forge_pop(&frame);
}
// Serialize frame buffers (it's own primitive FRAMEBUFFER)
for (size_t i=0, n=vFrameBufferPorts.size(); i<n; ++i)
{
lv2::Port *p = vFrameBufferPorts[i];
if ((p == NULL) || (!p->tx_pending()))
continue;
plug::frame_buffer_t *fb= p->buffer<plug::frame_buffer_t>();
if (fb == NULL)
continue;
pExt->forge_frame_time(0); // Event header
pExt->forge_object(&frame, p->get_urid(), pExt->uridFrameBufferType);
p->serialize();
pExt->forge_pop(&frame);
}
}
void Wrapper::transmit_midi_events(lv2::Port *p)
{
plug::midi_t *midi = p->buffer<plug::midi_t>();
if ((midi == NULL) || (midi->nEvents <= 0)) // There are no events ?
return;
midi->sort(); // Sort buffer chronologically
// Serialize MIDI events
LV2_Atom_Midi buf;
buf.atom.type = pExt->uridMidiEventType;
for (size_t i=0; i<midi->nEvents; ++i)
{
const midi::event_t *me = &midi->vEvents[i];
// Debug
#ifdef LSP_TRACE
#define TRACE_KEY(x) case midi::MIDI_MSG_ ## x: evt_type = #x; break;
char tmp_evt_type[32];
const char *evt_type = NULL;
switch (me->type)
{
TRACE_KEY(NOTE_OFF)
TRACE_KEY(NOTE_ON)
TRACE_KEY(NOTE_PRESSURE)
TRACE_KEY(NOTE_CONTROLLER)
TRACE_KEY(PROGRAM_CHANGE)
TRACE_KEY(CHANNEL_PRESSURE)
TRACE_KEY(PITCH_BEND)
TRACE_KEY(SYSTEM_EXCLUSIVE)
TRACE_KEY(MTC_QUARTER)
TRACE_KEY(SONG_POS)
TRACE_KEY(SONG_SELECT)
TRACE_KEY(TUNE_REQUEST)
TRACE_KEY(END_EXCLUSIVE)
TRACE_KEY(CLOCK)
TRACE_KEY(START)
TRACE_KEY(CONTINUE)
TRACE_KEY(STOP)
TRACE_KEY(ACTIVE_SENSING)
TRACE_KEY(RESET)
default:
snprintf(tmp_evt_type, sizeof(tmp_evt_type), "UNKNOWN(0x%02x)", int(me->type));
evt_type = tmp_evt_type;
break;
}
lsp_trace("MIDI Event: type=%s, timestamp=%ld", evt_type, (long)(me->timestamp));
#undef TRACE_KEY
#endif /* LSP_TRACE */
ssize_t size = midi::encode(buf.body, me);
if (size <= 0)
{
lsp_error("Tried to serialize invalid MIDI event, error=%d", int(-size));
continue;
}
buf.atom.size = size;
lsp_trace("midi dump: %02x %02x %02x (%d: %d)",
int(buf.body[0]), int(buf.body[1]), int(buf.body[2]), int(buf.atom.size), int(buf.atom.size + sizeof(LV2_Atom)));
// Serialize object
pExt->forge_frame_time(0);
pExt->forge_raw(&buf.atom, sizeof(LV2_Atom) + buf.atom.size);
pExt->forge_pad(sizeof(LV2_Atom) + buf.atom.size);
}
}
void Wrapper::transmit_kvt_events()
{
LV2_Atom atom;
size_t size;
while (true)
{
status_t res = pKVTDispatcher->fetch(pOscPacket, &size, OSC_PACKET_MAX);
switch (res)
{
case STATUS_OK:
{
lsp_trace("Transmitting OSC packet of %d bytes", int(size));
osc::dump_packet(pOscPacket, size);
atom.size = size;
atom.type = pExt->uridOscRawPacket;
pExt->forge_frame_time(0);
pExt->forge_raw(&atom, sizeof(LV2_Atom));
pExt->forge_raw(pOscPacket, size);
pExt->forge_pad(sizeof(LV2_Atom) + size);
break;
}
case STATUS_OVERFLOW:
lsp_warn("Received too big OSC packet, skipping");
pKVTDispatcher->skip();
break;
case STATUS_NO_DATA:
return;
default:
lsp_warn("Received error while deserializing KVT changes: %d", int(res));
return;
}
}
}
void Wrapper::transmit_atoms(size_t samples)
{
// Get sequence
if (pAtomOut == NULL)
return;
// Update synchronization time
nSyncTime -= samples;
bool sync_req = nSyncTime <= 0;
if (sync_req)
{
nSyncTime += nSyncSamples;
// Check that queue_draw() request for inline display is pending
if ((bQueueDraw) && (pExt->iDisplay != NULL))
{
pExt->iDisplay->queue_draw(pExt->iDisplay->handle);
bQueueDraw = false;
}
}
// Check that patch request is pending
bool patch_req = nPatchReqs > 0;
if (patch_req)
nPatchReqs --;
// Check that state request is pending
bool state_req = nStateReqs > 0;
if (state_req)
nStateReqs --;
// Initialize forge
LV2_Atom_Sequence *sequence = reinterpret_cast<LV2_Atom_Sequence *>(pAtomOut);
pExt->forge_set_buffer(sequence, sequence->atom.size);
// Forge sequence header
LV2_Atom_Forge_Frame seq;
pExt->forge_sequence_head(&seq, 0);
// Transmit state change atom if state has been changed
if (change_state_atomic(SM_CHANGED, SM_REPORTED))
{
LV2_Atom_Forge_Frame frame;
pExt->forge_frame_time(0); // Event header
pExt->forge_object(&frame, pExt->uridBlank, pExt->uridStateChanged);
pExt->forge_pop(&frame);
lsp_trace("#STATE MODE = %d", nStateMode);
}
// For each MIDI port, serialize it's data
for (size_t i=0, n_midi=vMidiPorts.size(); i<n_midi; ++i)
{
lv2::Port *p = vMidiPorts.uget(i);
if (!meta::is_midi_out_port(p->metadata()))
continue;
transmit_midi_events(p);
}
// For each OSC port, serialize it's data
for (size_t i=0, n_osc=vOscPorts.size(); i<n_osc; ++i)
{
lv2::Port *p = vOscPorts.uget(i);
if (!meta::is_osc_out_port(p->metadata()))
continue;
transmit_osc_events(p);
}
// Transmit different data to clients
if (nClients > 0)
{
if (pKVTDispatcher != NULL)
transmit_kvt_events();
transmit_time_position_to_clients();
transmit_port_data_to_clients(sync_req, patch_req, state_req);
}
transmit_play_position_to_clients();
// Complete sequence
pExt->forge_pop(&seq);
}
void Wrapper::transmit_osc_events(lv2::Port *p)
{
plug::osc_buffer_t *osc = p->buffer<plug::osc_buffer_t>();
if (osc == NULL) // There are no events ?
return;
size_t size;
LV2_Atom atom;
while (true)
{
// Try to fetch record from buffer
status_t res = osc->fetch(pOscPacket, &size, OSC_PACKET_MAX);
switch (res)
{
case STATUS_OK:
{
lsp_trace("Transmitting OSC packet of %d bytes", int(size));
osc::dump_packet(pOscPacket, size);
atom.size = size;
atom.type = pExt->uridOscRawPacket;
pExt->forge_frame_time(0);
pExt->forge_raw(&atom, sizeof(LV2_Atom));
pExt->forge_raw(pOscPacket, size);
pExt->forge_pad(sizeof(LV2_Atom) + size);
break;
}
case STATUS_NO_DATA: // No more data to transmit
return;
case STATUS_OVERFLOW:
{
lsp_warn("Too large OSC packet in the buffer, skipping");
osc->skip();
break;
}
default:
{
lsp_warn("OSC packet parsing error %d, skipping", int(res));
osc->skip();
break;
}
}
}
}
void Wrapper::save_kvt_parameters()
{
const core::kvt_param_t *p;
status_t res = STATUS_OK;
// We should use our own forge to prevent from race condition
LV2_Atom_Forge forge;
LV2_Atom_Forge_Frame frame;
lv2::lv2_sink sink(0x100);
// Initialize sink
lv2_atom_forge_init(&forge, pExt->map);
lv2_atom_forge_set_sink(&forge, lv2_sink::sink, lv2_sink::deref, &sink);
lv2_atom_forge_tuple(&forge, &frame);
// Read the whole KVT storage
core::KVTIterator *it = sKVT.enum_all();
while (it->next() == STATUS_OK)
{
res = it->get(&p);
if (res == STATUS_NOT_FOUND) // Not a parameter
continue;
else if (res != STATUS_OK)
{
lsp_warn("it->get() returned %d", int(res));
break;
}
else if (it->is_transient()) // Skip transient parameters
continue;
int flags = 0;
if (it->is_private())
flags |= LSP_LV2_PRIVATE;
const char *name = it->name();
if (name == NULL)
{
lsp_trace("it->name() returned NULL");
break;
}
kvt_dump_parameter("Saving state of KVT parameter: %s = ", p, name);
LV2_Atom_Forge_Frame prop;
lv2_atom_forge_object(&forge, &prop, 0, pExt->uridKvtEntryType);
// Key
lv2_atom_forge_key(&forge, pExt->uridKvtEntryKey);
lv2_atom_forge_string(&forge, name, strlen(name));
// Value
switch (p->type)
{
case core::KVT_INT32:
case core::KVT_UINT32:
{
LV2_Atom_Int v;
v.atom.type = (p->type == core::KVT_INT32) ? pExt->forge.Int : pExt->uridTypeUInt;
v.atom.size = sizeof(v) - sizeof(LV2_Atom);
v.body = p->i32;
lv2_atom_forge_key(&forge, pExt->uridKvtEntryValue);
lv2_atom_forge_primitive(&forge, &v.atom);
break;
}
case core::KVT_INT64:
case core::KVT_UINT64:
{
LV2_Atom_Long v;
v.atom.type = (p->type == core::KVT_INT64) ? pExt->forge.Long : pExt->uridTypeULong;
v.atom.size = sizeof(v) - sizeof(LV2_Atom);
v.body = p->i64;
lv2_atom_forge_key(&forge, pExt->uridKvtEntryValue);
lv2_atom_forge_primitive(&forge, &v.atom);
break;
}
case core::KVT_FLOAT32:
{
LV2_Atom_Float v;
v.atom.type = pExt->forge.Float;
v.atom.size = sizeof(v) - sizeof(LV2_Atom);
v.body = p->f32;
lv2_atom_forge_key(&forge, pExt->uridKvtEntryValue);
lv2_atom_forge_primitive(&forge, &v.atom);
break;
}
case core::KVT_FLOAT64:
{
LV2_Atom_Double v;
v.atom.type = pExt->forge.Double;
v.atom.size = sizeof(v) - sizeof(LV2_Atom);
v.body = p->f64;
lv2_atom_forge_key(&forge, pExt->uridKvtEntryValue);
lv2_atom_forge_primitive(&forge, &v.atom);
break;
}
case core::KVT_STRING:
{
lv2_atom_forge_key(&forge, pExt->uridKvtEntryValue);
const char *str = (p->str != NULL) ? p->str : "";
lv2_atom_forge_typed_string(&forge, forge.String, str, ::strlen(str));
break;
}
case core::KVT_BLOB:
{
LV2_Atom_Forge_Frame obj;
lv2_atom_forge_key(&forge, pExt->uridKvtEntryValue);
lv2_atom_forge_object(&forge, &obj, 0, pExt->uridBlobType);
{
if (p->blob.ctype != NULL)
{
lv2_atom_forge_key(&forge, pExt->uridContentType);
lv2_atom_forge_typed_string(&forge, forge.String, p->blob.ctype, ::strlen(p->blob.ctype));
}
uint32_t size = ((p->blob.size > 0) && (p->blob.data != NULL)) ? p->blob.size : 0;
lv2_atom_forge_key(&forge, pExt->uridContent);
lv2_atom_forge_atom(&forge, size, forge.Chunk);
if (size > 0)
lv2_atom_forge_write(&forge, p->blob.data, size);
}
lv2_atom_forge_pop(&forge, &obj);
break;
}
default:
lsp_warn("Invalid KVT property type: %d", int(p->type));
res = STATUS_BAD_TYPE;
break;
}
// Flags
lv2_atom_forge_key(&forge, pExt->uridKvtEntryFlags);
lv2_atom_forge_int(&forge, flags);
// End of object
lv2_atom_forge_pop(&forge, &prop);
// Successful status?
if (res != STATUS_OK)
break;
} // while
if ((res != STATUS_OK) ||(sink.res != STATUS_OK))
{
lsp_trace("Failed execution, result=%d, sink state=%d", int(res), int(sink.res));
return;
}
lsp_trace("Generated memory chunk of %d bytes, capacity is %d bytes", int(sink.size), int(sink.cap));
// TEST
/*{
kvt_param_t xp;
xp.blob.ctype = "text/plain";
xp.blob.data = "Test text";
xp.blob.size = strlen("Test text") + 1;
p = &xp;
LV2_Atom_Forge_Frame obj;
LV2_URID key = pExt->map_kvt("/TEST_BLOB");
lv2_atom_forge_key(&forge, key);
lv2_atom_forge_object(&forge, &obj, 0, pExt->uridBlobType);
{
if (p->blob.ctype != NULL)
{
lv2_atom_forge_key(&forge, pExt->uridContentType);
lv2_atom_forge_typed_string(&forge, forge.String, p->blob.ctype, ::strlen(p->blob.ctype));
}
uint32_t size = ((p->blob.size > 0) && (p->blob.data != NULL)) ? p->blob.size : 0;
lv2_atom_forge_key(&forge, pExt->uridContent);
lv2_atom_forge_atom(&forge, size, forge.Chunk);
if (size > 0)
lv2_atom_forge_write(&forge, p->blob.data, size);
}
lv2_atom_forge_pop(&forge, &obj);
}*/
lv2_atom_forge_pop(&forge, &frame);
LV2_Atom *msg = reinterpret_cast<LV2_Atom *>(sink.buf);
pExt->store_value(pExt->uridKvtObject, msg->type, &msg[1], msg->size);
}
void Wrapper::save_state(
LV2_State_Store_Function store,
LV2_State_Handle handle,
uint32_t flags,
const LV2_Feature *const * features)
{
pExt->init_state_context(store, NULL, handle, flags, features);
// Mark state as synchronized
nStateMode = SM_SYNC;
lsp_trace("#STATE MODE = %d", nStateMode);
// Save state of all ports
size_t ports_count = vAllPorts.size();
for (size_t i=0; i<ports_count; ++i)
{
// Get port
lv2::Port *lvp = vAllPorts[i];
if (lvp == NULL)
continue;
// Save state of port
lvp->save();
}
// Save state of all KVT parameters
if (sKVTMutex.lock())
{
save_kvt_parameters();
sKVT.gc();
sKVTMutex.unlock();
}
pExt->reset_state_context();
pPlugin->state_saved();
}
void Wrapper::restore_kvt_parameters()
{
uint32_t p_type = 0;
size_t p_size = 0;
const void *ptr = pExt->retrieve_value(pExt->uridKvtObject, &p_type, &p_size);
if (ptr == NULL)
return;
lsp_trace("p_type = %d (%s), p_size = %d", int(p_type), pExt->unmap_urid(p_type), int(p_size));
if ((p_type == pExt->forge.Object) || (p_type == pExt->uridBlank))
{
const LV2_Atom_Object_Body *obody = static_cast<const LV2_Atom_Object_Body *>(ptr);
if (obody->otype == pExt->uridKvtType)
parse_kvt_v1(obody, p_size);
else
lsp_warn("Unsupported KVT object type: %s", pExt->unmap_urid(obody->otype));
}
else if (p_type == pExt->forge.Tuple)
{
const LV2_Atom *body = static_cast<const LV2_Atom *>(ptr);
parse_kvt_v2(body, p_size);
}
else
lsp_warn("Unsupported KVT property type: %s", pExt->unmap_urid(p_type));
}
void Wrapper::parse_kvt_v2(const LV2_Atom *data, size_t size)
{
for (const LV2_Atom *item = data;
!lv2_atom_tuple_is_end(data, size, item);
item = lv2_atom_tuple_next(item))
{
if ((item->type != pExt->forge.Object) && (item->type != pExt->uridBlank))
{
lsp_warn("Unsupported KVT item type: %d (%s)", int(item->type), pExt->unmap_urid(item->type));
continue;
}
const LV2_Atom_Object *obj = reinterpret_cast<const LV2_Atom_Object *>(item);
if (obj->body.otype != pExt->uridKvtEntryType)
{
lsp_warn("Unsupported KVT item instance type: %s", pExt->unmap_urid(obj->body.otype));
continue;
}
core::kvt_param_t p;
char const *key = NULL;
size_t flags = core::KVT_TX;
p.type = core::KVT_ANY;
size_t mask = 0;
for (
LV2_Atom_Property_Body *xbody = lv2_atom_object_begin(&obj->body) ;
!lv2_atom_object_is_end(&obj->body, obj->atom.size, xbody) ;
xbody = lv2_atom_object_next(xbody)
)
{
lsp_trace("xbody->key (%d) = %s", int(xbody->key), pExt->unmap_urid(xbody->key));
lsp_trace("xbody->value.type (%d) = %s", int(xbody->value.type), pExt->unmap_urid(xbody->value.type));
// Analyze type of value
if (xbody->key == pExt->uridKvtEntryKey)
{
// Key
if (parse_kvt_key(&key, &xbody->value))
mask |= KP_KEY;
}
else if (xbody->key == pExt->uridKvtEntryValue)
{
// Value
if (parse_kvt_value(&p, &xbody->value))
mask |= KP_VALUE;
}
else if (xbody->key == pExt->uridKvtEntryFlags)
{
// Flags
if (parse_kvt_flags(&flags, &xbody->value))
mask |= KP_FLAGS;
}
else
lsp_warn("Unknown KVT Entry property: %d (%s)", xbody->key, pExt->unmap_urid(xbody->key));
} // for (item body)
// Store property
if ((key == NULL) || (!(mask & KP_KEY)))
lsp_warn("Failed to deserialize property missing key");
else if ((p.type == core::KVT_ANY) || (!(mask & KP_VALUE)))
lsp_warn("Failed to deserialize property %s: missing or invalid value", key);
else
{
kvt_dump_parameter("Fetched parameter %s = ", &p, key);
status_t res = sKVT.put(key, &p, flags);
if (res != STATUS_OK)
lsp_warn("Could not store parameter to KVT, error: %d", int(res));
}
} // for (tuple body)
}
void Wrapper::parse_kvt_v1(const LV2_Atom_Object_Body *data, size_t size)
{
const size_t prefix_len = ::strlen(LSP_LEGACY_KVT_URI);
const size_t prefix_len2 = ::strlen(pExt->uriKvt);
for (
LV2_Atom_Property_Body *item = lv2_atom_object_begin(data) ;
!lv2_atom_object_is_end(data, size, item) ;
item = lv2_atom_object_next(item)
)
{
// lsp_trace("body->key (%d) = %s", int(body->key), pExt->unmap_urid(body->key));
// lsp_trace("body->value.type (%d) = %s", int(body->value.type), pExt->unmap_urid(body->value.type));
const LV2_Atom_Object *pobject = reinterpret_cast<const LV2_Atom_Object *>(&item->value);
if ((pobject->atom.type != pExt->uridObject) && (pobject->atom.type != pExt->uridBlank))
{
lsp_warn("Unsupported value type (%d) = %s", int(item->value.type), pExt->unmap_urid(item->value.type));
continue;
}
// lsp_trace("pobject->body.otype (%d) = %s", int(pobject->body.otype), pExt->unmap_urid(pobject->body.otype));
if (pobject->body.otype != pExt->uridKvtPropertyType)
{
lsp_warn("Unsupported object type (%d) = %s", int(pobject->body.otype), pExt->unmap_urid(pobject->body.otype));
continue;
}
// Remove prefix for the KVT parameter
const char *uri = pExt->unmap_urid(item->key);
if (uri == NULL)
{
lsp_warn("Failed to unmap atom %d to URID value, skipping", int(item->key));
continue;
}
// Obtain the name of the property
const char *name = uri;
if (::strncmp(uri, LSP_LEGACY_KVT_URI, prefix_len) == 0)
name = (uri[prefix_len] == '/') ? &uri[prefix_len + 1] : NULL;
else if (::strncmp(uri, pExt->uriKvt, prefix_len2) == 0)
name = (uri[prefix_len2] == '/') ? &uri[prefix_len2 + 1] : NULL;
// Check that we have obtained the right name
if (name == NULL)
{
lsp_warn("Invalid property: urid=%d, uri=%s", item->key, uri);
continue;
}
core::kvt_param_t p;
size_t flags = core::KVT_TX;
p.type = core::KVT_ANY;
size_t mask = 0;
for (
LV2_Atom_Property_Body *xbody = lv2_atom_object_begin(&pobject->body) ;
!lv2_atom_object_is_end(&pobject->body, item->value.size, xbody) ;
xbody = lv2_atom_object_next(xbody)
)
{
// lsp_trace("xbody->key (%d) = %s", int(xbody->key), pExt->unmap_urid(xbody->key));
// lsp_trace("xbody->value.type (%d) = %s", int(xbody->value.type), pExt->unmap_urid(xbody->value.type));
// Analyze type of value
if (xbody->key == pExt->uridKvtPropertyValue)
{
// Value
if (parse_kvt_value(&p, &xbody->value))
mask |= KP_VALUE;
else
lsp_warn("KVT property %s has unsupported type or is invalid: 0x%x (%s)",
name, xbody->value.type, pExt->unmap_urid(xbody->value.type));
}
else if (xbody->key == pExt->uridKvtPropertyFlags)
{
// Flags
if (parse_kvt_flags(&flags, &xbody->value))
mask |= KP_FLAGS;
}
else
lsp_warn("Unknown KVT Entry property: %d (%s)", xbody->key, pExt->unmap_urid(xbody->key));
} // for (item body)
// Store property
if ((p.type != core::KVT_ANY) && (mask & KP_VALUE))
{
kvt_dump_parameter("Fetched parameter %s = ", &p, name);
status_t res = sKVT.put(name, &p, flags);
if (res != STATUS_OK)
lsp_warn("Could not store parameter to KVT, error: %d", int(res));
}
else
lsp_warn("Failed to deserialize property %s: missing value", name);
} // for (object body)
}
bool Wrapper::parse_kvt_key(char const **key, const LV2_Atom *value)
{
if (value->type != pExt->forge.String)
{
lsp_warn("Invalid type for key: %s", pExt->unmap_urid(value->type));
return false;
}
*key = reinterpret_cast<const char *>(&value[1]);
return true;
}
bool Wrapper::parse_kvt_flags(size_t *flags, const LV2_Atom *value)
{
if (value->type != pExt->forge.Int)
{
lsp_warn("Invalid type for flags");
return false;
}
size_t result = core::KVT_TX;
size_t pflags = (reinterpret_cast<const LV2_Atom_Int *>(value))->body;
if (pflags & LSP_LV2_PRIVATE)
result |= core::KVT_PRIVATE;
*flags = result;
return true;
}
bool Wrapper::parse_kvt_value(core::kvt_param_t *param, const LV2_Atom *value)
{
LV2_URID p_type = value->type;
core::kvt_param_t p;
if (p_type == pExt->forge.Int)
{
p.type = core::KVT_INT32;
p.i32 = (reinterpret_cast<const LV2_Atom_Int *>(value))->body;
}
else if (p_type == pExt->uridTypeUInt)
{
p.type = core::KVT_UINT32;
p.u32 = (reinterpret_cast<const LV2_Atom_Int *>(value))->body;
}
else if (p_type == pExt->forge.Long)
{
p.type = core::KVT_INT64;
p.i64 = (reinterpret_cast<const LV2_Atom_Long *>(value))->body;
}
else if (p_type == pExt->uridTypeULong)
{
p.type = core::KVT_UINT64;
p.u64 = (reinterpret_cast<const LV2_Atom_Long *>(value))->body;
}
else if (p_type == pExt->forge.Float)
{
p.type = core::KVT_FLOAT32;
p.f32 = (reinterpret_cast<const LV2_Atom_Float *>(value))->body;
}
else if (p_type == pExt->forge.Double)
{
p.type = core::KVT_FLOAT64;
p.f64 = (reinterpret_cast<const LV2_Atom_Double *>(value))->body;
}
else if (p_type == pExt->forge.String)
{
p.type = core::KVT_STRING;
p.str = reinterpret_cast<const char *>(&value[1]);
}
else if ((p_type == pExt->uridObject) || (p_type == pExt->uridBlank))
{
const LV2_Atom_Object *obj = reinterpret_cast<const LV2_Atom_Object *>(value);
lsp_trace("obj->atom.type = %d (%s), obj->body.id = %d (%s), obj->body.otype = %d (%s)",
obj->atom.type, pExt->unmap_urid(obj->atom.type),
obj->body.id, pExt->unmap_urid(obj->body.id),
obj->body.otype, pExt->unmap_urid(obj->body.otype)
);
if (obj->body.otype != pExt->uridBlobType)
{
lsp_warn("Expected object of BLOB type but get: %d (%s)",
obj->body.otype, pExt->unmap_urid(obj->body.otype));
return false;
}
// Read the value
p.blob.ctype = NULL;
p.blob.data = NULL;
p.blob.size = ~size_t(0);
for (
LV2_Atom_Property_Body *blob = lv2_atom_object_begin(&obj->body) ;
!lv2_atom_object_is_end(&obj->body, obj->atom.size, blob) ;
blob = lv2_atom_object_next(blob)
)
{
lsp_trace("blob->key (%d) = %s", int(blob->key), pExt->unmap_urid(blob->key));
lsp_trace("blob->value.type (%d) = %s", int(blob->value.type), pExt->unmap_urid(blob->value.type));
if ((blob->key == pExt->uridContentType) && (blob->value.type == pExt->forge.String))
p.blob.ctype = reinterpret_cast<const char *>(&blob[1]);
else if ((blob->key == pExt->uridContent) && (blob->value.type == pExt->forge.Chunk))
{
p.blob.size = blob->value.size;
if (p.blob.size > 0)
p.blob.data = &blob[1];
}
}
// Change type
if (p.blob.size != (~size_t(0)))
p.type = core::KVT_BLOB;
}
else
return false;
*param = p;
return true;
}
void Wrapper::restore_state(
LV2_State_Retrieve_Function retrieve,
LV2_State_Handle handle,
uint32_t flags,
const LV2_Feature *const * features
)
{
pExt->init_state_context(NULL, retrieve, handle, flags, features);
// Restore posts state
for (size_t i=0, n=vAllPorts.size(); i<n; ++i)
{
// Get port
lv2::Port *lvp = vAllPorts[i];
if (lvp == NULL)
continue;
// Restore state of port
lvp->restore();
}
// Restore KVT state
if (sKVTMutex.lock())
{
// Clear KVT
sKVT.clear();
restore_kvt_parameters();
sKVT.gc();
sKVTMutex.unlock();
}
pExt->reset_state_context();
pPlugin->state_loaded();
nStateMode = SM_LOADING;
lsp_trace("#STATE MODE = %d", nStateMode);
}
bool Wrapper::change_state_atomic(state_mode_t from, state_mode_t to)
{
// Perform atomic state change
while (true)
{
if (nStateMode != from)
return false;
if (atomic_cas(&nStateMode, from, to))
{
lsp_trace("#STATE state changed from %d to %d", int(from), int(to));
return true;
}
}
}
void Wrapper::connect_direct_ui()
{
// Increment number of clients
nDirectClients++;
if (pKVTDispatcher != NULL)
pKVTDispatcher->connect_client();
}
void Wrapper::disconnect_direct_ui()
{
if (nDirectClients <= 0)
return;
--nDirectClients;
if (pKVTDispatcher != NULL)
pKVTDispatcher->disconnect_client();
}
void Wrapper::job_run(
LV2_Worker_Respond_Handle handle,
LV2_Worker_Respond_Function respond,
uint32_t size,
const void* data
)
{
lv2::Executor *executor = static_cast<lv2::Executor *>(pExecutor);
executor->run_job(handle, respond, size, data);
}
ipc::IExecutor *Wrapper::executor()
{
lsp_trace("executor = %p", reinterpret_cast<void *>(pExecutor));
if (pExecutor != NULL)
return pExecutor;
// Create executor service
if (pExt->sched != NULL)
{
lsp_trace("Creating LV2 host-provided executor service");
pExecutor = new lv2::Executor(pExt->sched);
}
else
{
lsp_trace("Creating native executor service");
ipc::NativeExecutor *exec = new ipc::NativeExecutor();
if (exec == NULL)
return NULL;
status_t res = exec->start();
if (res != STATUS_OK)
{
delete exec;
return NULL;
}
pExecutor = exec;
}
return pExecutor;
}
LV2_Inline_Display_Image_Surface *Wrapper::render_inline_display(size_t width, size_t height)
{
// Allocate canvas for drawing
plug::ICanvas *canvas = create_canvas(width, height);
if (canvas == NULL)
return NULL;
// Call plugin for rendering and return canvas data
bool res = pPlugin->inline_display(canvas, width, height);
canvas->sync();
// Obtain canvas data
plug::canvas_data_t *data = canvas->data();
if ((!res) || (data == NULL) || (data->pData == NULL))
return NULL;
// Fill-in surface and return
sSurface.data = reinterpret_cast<unsigned char *>(data->pData);
sSurface.width = data->nWidth;
sSurface.height = data->nHeight;
sSurface.stride = data->nStride;
return &sSurface;
}
core::KVTStorage *Wrapper::kvt_lock()
{
return (sKVTMutex.lock()) ? &sKVT : NULL;
}
core::KVTStorage *Wrapper::kvt_trylock()
{
return (sKVTMutex.try_lock()) ? &sKVT : NULL;
}
bool Wrapper::kvt_release()
{
return sKVTMutex.unlock();
}
const meta::package_t *Wrapper::package() const
{
return pPackage;
}
void Wrapper::state_changed()
{
change_state_atomic(SM_SYNC, SM_CHANGED);
}
void Wrapper::query_display_draw()
{
bQueueDraw = true;
}
} /* namespace lv2 */
} /* namespace lsp */
#endif /* LSP_PLUG_IN_PLUG_FW_WRAP_LV2_IMPL_WRAPPER_H_ */
|