Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5719 lines
215KB

  1. /*
  2. * Carla LV2 Plugin
  3. * Copyright (C) 2011-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. // testing macros
  18. // #define LV2_UIS_ONLY_BRIDGES
  19. // #define LV2_UIS_ONLY_INPROCESS
  20. #include "CarlaPluginInternal.hpp"
  21. #include "CarlaEngine.hpp"
  22. #ifdef WANT_LV2
  23. #include "CarlaLv2Utils.hpp"
  24. #include "CarlaMathUtils.hpp"
  25. #include "CarlaPluginUi.hpp"
  26. #include "Lv2AtomQueue.hpp"
  27. #include "../engine/CarlaEngineOsc.hpp"
  28. extern "C" {
  29. #include "rtmempool/rtmempool-lv2.h"
  30. }
  31. #include <QtCore/QDir>
  32. #include <QtCore/QFile>
  33. #include <QtCore/QUrl>
  34. #define URI_CARLA_ATOM_WORKER "http://kxstudio.sf.net/ns/carla/atomWorker"
  35. #define URI_CARLA_FRONTEND_WIN_ID "http://kxstudio.sf.net/ns/carla/frontendWinId"
  36. // -----------------------------------------------------
  37. CARLA_BACKEND_START_NAMESPACE
  38. #if 0
  39. }
  40. #endif
  41. // Maximum default buffer size
  42. const unsigned int MAX_DEFAULT_BUFFER_SIZE = 8192; // 0x2000
  43. // Extra Plugin Hints
  44. const unsigned int PLUGIN_HAS_EXTENSION_OPTIONS = 0x1000;
  45. const unsigned int PLUGIN_HAS_EXTENSION_PROGRAMS = 0x2000;
  46. const unsigned int PLUGIN_HAS_EXTENSION_STATE = 0x4000;
  47. const unsigned int PLUGIN_HAS_EXTENSION_WORKER = 0x8000;
  48. // Extra Parameter Hints
  49. const unsigned int PARAMETER_IS_STRICT_BOUNDS = 0x1000;
  50. const unsigned int PARAMETER_IS_TRIGGER = 0x2000;
  51. // LV2 Event Data/Types
  52. const unsigned int CARLA_EVENT_DATA_ATOM = 0x01;
  53. const unsigned int CARLA_EVENT_DATA_EVENT = 0x02;
  54. const unsigned int CARLA_EVENT_DATA_MIDI_LL = 0x04;
  55. const unsigned int CARLA_EVENT_TYPE_MESSAGE = 0x10; // unused
  56. const unsigned int CARLA_EVENT_TYPE_MIDI = 0x20;
  57. const unsigned int CARLA_EVENT_TYPE_TIME = 0x40;
  58. // LV2 URI Map Ids
  59. const uint32_t CARLA_URI_MAP_ID_NULL = 0;
  60. const uint32_t CARLA_URI_MAP_ID_ATOM_BLANK = 1;
  61. const uint32_t CARLA_URI_MAP_ID_ATOM_BOOL = 2;
  62. const uint32_t CARLA_URI_MAP_ID_ATOM_CHUNK = 3;
  63. const uint32_t CARLA_URI_MAP_ID_ATOM_DOUBLE = 4;
  64. const uint32_t CARLA_URI_MAP_ID_ATOM_EVENT = 5;
  65. const uint32_t CARLA_URI_MAP_ID_ATOM_FLOAT = 6;
  66. const uint32_t CARLA_URI_MAP_ID_ATOM_INT = 7;
  67. const uint32_t CARLA_URI_MAP_ID_ATOM_LITERAL = 8;
  68. const uint32_t CARLA_URI_MAP_ID_ATOM_LONG = 9;
  69. const uint32_t CARLA_URI_MAP_ID_ATOM_NUMBER = 10;
  70. const uint32_t CARLA_URI_MAP_ID_ATOM_OBJECT = 11;
  71. const uint32_t CARLA_URI_MAP_ID_ATOM_PATH = 12;
  72. const uint32_t CARLA_URI_MAP_ID_ATOM_PROPERTY = 13;
  73. const uint32_t CARLA_URI_MAP_ID_ATOM_RESOURCE = 14;
  74. const uint32_t CARLA_URI_MAP_ID_ATOM_SEQUENCE = 15;
  75. const uint32_t CARLA_URI_MAP_ID_ATOM_SOUND = 16;
  76. const uint32_t CARLA_URI_MAP_ID_ATOM_STRING = 17;
  77. const uint32_t CARLA_URI_MAP_ID_ATOM_TUPLE = 18;
  78. const uint32_t CARLA_URI_MAP_ID_ATOM_URI = 19;
  79. const uint32_t CARLA_URI_MAP_ID_ATOM_URID = 20;
  80. const uint32_t CARLA_URI_MAP_ID_ATOM_VECTOR = 21;
  81. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM = 22;
  82. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT = 23;
  83. const uint32_t CARLA_URI_MAP_ID_BUF_MAX_LENGTH = 24;
  84. const uint32_t CARLA_URI_MAP_ID_BUF_MIN_LENGTH = 25;
  85. const uint32_t CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE = 26;
  86. const uint32_t CARLA_URI_MAP_ID_LOG_ERROR = 27;
  87. const uint32_t CARLA_URI_MAP_ID_LOG_NOTE = 28;
  88. const uint32_t CARLA_URI_MAP_ID_LOG_TRACE = 29;
  89. const uint32_t CARLA_URI_MAP_ID_LOG_WARNING = 30;
  90. const uint32_t CARLA_URI_MAP_ID_TIME_POSITION = 31; // base type
  91. const uint32_t CARLA_URI_MAP_ID_TIME_BAR = 32; // values
  92. const uint32_t CARLA_URI_MAP_ID_TIME_BAR_BEAT = 33;
  93. const uint32_t CARLA_URI_MAP_ID_TIME_BEAT = 34;
  94. const uint32_t CARLA_URI_MAP_ID_TIME_BEAT_UNIT = 35;
  95. const uint32_t CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR = 36;
  96. const uint32_t CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE = 37;
  97. const uint32_t CARLA_URI_MAP_ID_TIME_FRAME = 38;
  98. const uint32_t CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND = 39;
  99. const uint32_t CARLA_URI_MAP_ID_TIME_SPEED = 40;
  100. const uint32_t CARLA_URI_MAP_ID_MIDI_EVENT = 41;
  101. const uint32_t CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE = 42;
  102. const uint32_t CARLA_URI_MAP_ID_UI_WINDOW_TITLE = 43;
  103. const uint32_t CARLA_URI_MAP_ID_CARLA_ATOM_WORKER = 44;
  104. const uint32_t CARLA_URI_MAP_ID_CARLA_FRONTEND_WIN_ID = 45;
  105. const uint32_t CARLA_URI_MAP_ID_COUNT = 46;
  106. // LV2 Feature Ids
  107. const uint32_t kFeatureIdBufSizeBounded = 0;
  108. const uint32_t kFeatureIdBufSizeFixed = 1;
  109. const uint32_t kFeatureIdBufSizePowerOf2 = 2;
  110. const uint32_t kFeatureIdEvent = 3;
  111. const uint32_t kFeatureIdHardRtCapable = 4;
  112. const uint32_t kFeatureIdInPlaceBroken = 5;
  113. const uint32_t kFeatureIdIsLive = 6;
  114. const uint32_t kFeatureIdLogs = 7;
  115. const uint32_t kFeatureIdOptions = 8;
  116. const uint32_t kFeatureIdPrograms = 9;
  117. const uint32_t kFeatureIdResizePort = 10;
  118. const uint32_t kFeatureIdRtMemPool = 11;
  119. const uint32_t kFeatureIdRtMemPoolOld = 12;
  120. const uint32_t kFeatureIdStateMakePath = 13;
  121. const uint32_t kFeatureIdStateMapPath = 14;
  122. const uint32_t kFeatureIdStrictBounds = 15;
  123. const uint32_t kFeatureIdUriMap = 16;
  124. const uint32_t kFeatureIdUridMap = 17;
  125. const uint32_t kFeatureIdUridUnmap = 18;
  126. const uint32_t kFeatureIdWorker = 19;
  127. const uint32_t kFeatureCountPlugin = 20;
  128. const uint32_t kFeatureIdUiDataAccess = 20;
  129. const uint32_t kFeatureIdUiInstanceAccess = 21;
  130. const uint32_t kFeatureIdUiIdleInterface = 22;
  131. const uint32_t kFeatureIdUiFixedSize = 23;
  132. const uint32_t kFeatureIdUiMakeResident = 24;
  133. const uint32_t kFeatureIdUiNoUserResize = 25;
  134. const uint32_t kFeatureIdUiParent = 26;
  135. const uint32_t kFeatureIdUiPortMap = 27;
  136. const uint32_t kFeatureIdUiPortSubscribe = 28;
  137. const uint32_t kFeatureIdUiResize = 29;
  138. const uint32_t kFeatureIdUiTouch = 30;
  139. const uint32_t kFeatureIdExternalUi = 31;
  140. const uint32_t kFeatureIdExternalUiOld = 32;
  141. const uint32_t kFeatureCountAll = 33;
  142. // -----------------------------------------------------
  143. struct Lv2EventData {
  144. uint32_t type;
  145. uint32_t rindex;
  146. CarlaEngineEventPort* port;
  147. union {
  148. LV2_Atom_Buffer* atom;
  149. LV2_Event_Buffer* event;
  150. LV2_MIDI midi;
  151. };
  152. Lv2EventData()
  153. : type(0x0),
  154. rindex(0),
  155. port(nullptr) {}
  156. ~Lv2EventData()
  157. {
  158. if (port != nullptr)
  159. {
  160. delete port;
  161. port = nullptr;
  162. }
  163. const uint32_t rtype(type);
  164. type = 0x0;
  165. if (rtype & CARLA_EVENT_DATA_ATOM)
  166. {
  167. CARLA_SAFE_ASSERT_RETURN(atom != nullptr,);
  168. std::free(atom);
  169. atom = nullptr;
  170. }
  171. else if (rtype & CARLA_EVENT_DATA_EVENT)
  172. {
  173. CARLA_SAFE_ASSERT_RETURN(event != nullptr,);
  174. std::free(event);
  175. event = nullptr;
  176. }
  177. else if (rtype & CARLA_EVENT_DATA_MIDI_LL)
  178. {
  179. CARLA_SAFE_ASSERT_RETURN(midi.data != nullptr,);
  180. delete[] midi.data;
  181. midi.data = nullptr;
  182. }
  183. }
  184. CARLA_DECLARE_NON_COPY_STRUCT(Lv2EventData)
  185. };
  186. struct Lv2PluginEventData {
  187. uint32_t count;
  188. Lv2EventData* data;
  189. Lv2EventData* ctrl; // default port, either this->data[x] or pData->portIn/Out
  190. uint32_t ctrlIndex;
  191. Lv2PluginEventData()
  192. : count(0),
  193. data(nullptr),
  194. ctrl(nullptr),
  195. ctrlIndex(0) {}
  196. ~Lv2PluginEventData()
  197. {
  198. CARLA_ASSERT_INT(count == 0, count);
  199. CARLA_ASSERT(data == nullptr);
  200. CARLA_ASSERT(ctrl == nullptr);
  201. CARLA_ASSERT_INT(ctrlIndex == 0, ctrlIndex);
  202. }
  203. void createNew(const uint32_t newCount)
  204. {
  205. CARLA_SAFE_ASSERT_INT(count == 0, count);
  206. CARLA_SAFE_ASSERT_INT(ctrlIndex == 0, ctrlIndex);
  207. CARLA_SAFE_ASSERT_RETURN(data == nullptr,);
  208. CARLA_SAFE_ASSERT_RETURN(ctrl == nullptr,);
  209. CARLA_SAFE_ASSERT_RETURN(newCount > 0,);
  210. data = new Lv2EventData[newCount];
  211. count = newCount;
  212. }
  213. void clear()
  214. {
  215. if (data != nullptr)
  216. {
  217. for (uint32_t i=0; i < count; ++i)
  218. {
  219. if (data[i].port != nullptr && ctrl != nullptr && data[i].port == ctrl->port)
  220. data[i].port = nullptr;
  221. }
  222. delete[] data;
  223. data = nullptr;
  224. }
  225. count = 0;
  226. ctrl = nullptr;
  227. ctrlIndex = 0;
  228. }
  229. void initBuffers() const noexcept
  230. {
  231. for (uint32_t i=0; i < count; ++i)
  232. {
  233. if (data[i].port != nullptr && (ctrl == nullptr || data[i].port != ctrl->port))
  234. data[i].port->initBuffer();
  235. }
  236. }
  237. CARLA_DECLARE_NON_COPY_STRUCT(Lv2PluginEventData)
  238. };
  239. // -----------------------------------------------------
  240. struct Lv2PluginOptions {
  241. enum OptIndex {
  242. MaxBlockLenth = 0,
  243. MinBlockLenth,
  244. SequenceSize,
  245. SampleRate,
  246. FrontendWinId,
  247. WindowTitle,
  248. Null,
  249. Count
  250. };
  251. int maxBufferSize;
  252. int minBufferSize;
  253. int sequenceSize;
  254. double sampleRate;
  255. int64_t frontendWinId;
  256. const char* windowTitle;
  257. LV2_Options_Option opts[Count];
  258. Lv2PluginOptions()
  259. : maxBufferSize(0),
  260. minBufferSize(0),
  261. sequenceSize(MAX_DEFAULT_BUFFER_SIZE),
  262. sampleRate(0.0),
  263. frontendWinId(0),
  264. windowTitle(nullptr)
  265. {
  266. LV2_Options_Option& optMaxBlockLenth(opts[MaxBlockLenth]);
  267. optMaxBlockLenth.context = LV2_OPTIONS_INSTANCE;
  268. optMaxBlockLenth.subject = 0;
  269. optMaxBlockLenth.key = CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  270. optMaxBlockLenth.size = sizeof(int);
  271. optMaxBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  272. optMaxBlockLenth.value = &maxBufferSize;
  273. LV2_Options_Option& optMinBlockLenth(opts[MinBlockLenth]);
  274. optMinBlockLenth.context = LV2_OPTIONS_INSTANCE;
  275. optMinBlockLenth.subject = 0;
  276. optMinBlockLenth.key = CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  277. optMinBlockLenth.size = sizeof(int);
  278. optMinBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  279. optMinBlockLenth.value = &minBufferSize;
  280. LV2_Options_Option& optSequenceSize(opts[SequenceSize]);
  281. optSequenceSize.context = LV2_OPTIONS_INSTANCE;
  282. optSequenceSize.subject = 0;
  283. optSequenceSize.key = CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  284. optSequenceSize.size = sizeof(int);
  285. optSequenceSize.type = CARLA_URI_MAP_ID_ATOM_INT;
  286. optSequenceSize.value = &sequenceSize;
  287. LV2_Options_Option& optSampleRate(opts[SampleRate]);
  288. optSampleRate.context = LV2_OPTIONS_INSTANCE;
  289. optSampleRate.subject = 0;
  290. optSampleRate.key = CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  291. optSampleRate.size = sizeof(double);
  292. optSampleRate.type = CARLA_URI_MAP_ID_ATOM_DOUBLE;
  293. optSampleRate.value = &sampleRate;
  294. LV2_Options_Option& optFrontendWinId(opts[FrontendWinId]);
  295. optFrontendWinId.context = LV2_OPTIONS_INSTANCE;
  296. optFrontendWinId.subject = 0;
  297. optFrontendWinId.key = CARLA_URI_MAP_ID_CARLA_FRONTEND_WIN_ID;
  298. optFrontendWinId.size = sizeof(int64_t);
  299. optFrontendWinId.type = CARLA_URI_MAP_ID_ATOM_LONG;
  300. optFrontendWinId.value = &frontendWinId;
  301. LV2_Options_Option& optWindowTitle(opts[WindowTitle]);
  302. optWindowTitle.context = LV2_OPTIONS_INSTANCE;
  303. optWindowTitle.subject = 0;
  304. optWindowTitle.key = CARLA_URI_MAP_ID_UI_WINDOW_TITLE;
  305. optWindowTitle.size = 0;
  306. optWindowTitle.type = CARLA_URI_MAP_ID_ATOM_STRING;
  307. optWindowTitle.value = nullptr;
  308. LV2_Options_Option& optNull(opts[Null]);
  309. optNull.context = LV2_OPTIONS_INSTANCE;
  310. optNull.subject = 0;
  311. optNull.key = CARLA_URI_MAP_ID_NULL;
  312. optNull.size = 0;
  313. optNull.type = CARLA_URI_MAP_ID_NULL;
  314. optNull.value = nullptr;
  315. }
  316. ~Lv2PluginOptions()
  317. {
  318. LV2_Options_Option& optWindowTitle(opts[WindowTitle]);
  319. optWindowTitle.size = 0;
  320. optWindowTitle.value = nullptr;
  321. if (windowTitle != nullptr)
  322. {
  323. delete[] windowTitle;
  324. windowTitle = nullptr;
  325. }
  326. }
  327. };
  328. // -----------------------------------------------------
  329. class Lv2Plugin : public CarlaPlugin,
  330. CarlaPluginUi::CloseCallback
  331. {
  332. public:
  333. Lv2Plugin(CarlaEngine* const engine, const unsigned int id)
  334. : CarlaPlugin(engine, id),
  335. fHandle(nullptr),
  336. fHandle2(nullptr),
  337. fDescriptor(nullptr),
  338. fRdfDescriptor(nullptr),
  339. fAudioInBuffers(nullptr),
  340. fAudioOutBuffers(nullptr),
  341. fCvInBuffers(nullptr),
  342. fCvOutBuffers(nullptr),
  343. fParamBuffers(nullptr),
  344. fFirstActive(true)
  345. {
  346. carla_debug("Lv2Plugin::Lv2Plugin(%p, %i)", engine, id);
  347. carla_fill<LV2_Feature*>(fFeatures, kFeatureCountAll+1, nullptr);
  348. for (uint32_t i=0; i < CARLA_URI_MAP_ID_COUNT; ++i)
  349. fCustomURIDs.append(nullptr);
  350. #if defined(__clang__)
  351. # pragma clang diagnostic push
  352. # pragma clang diagnostic ignored "-Wdeprecated-declarations"
  353. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  354. # pragma GCC diagnostic push
  355. # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  356. #endif
  357. fAtomForge.Blank = CARLA_URI_MAP_ID_ATOM_BLANK;
  358. fAtomForge.Bool = CARLA_URI_MAP_ID_ATOM_BOOL;
  359. fAtomForge.Chunk = CARLA_URI_MAP_ID_ATOM_CHUNK;
  360. fAtomForge.Double = CARLA_URI_MAP_ID_ATOM_DOUBLE;
  361. fAtomForge.Float = CARLA_URI_MAP_ID_ATOM_FLOAT;
  362. fAtomForge.Int = CARLA_URI_MAP_ID_ATOM_INT;
  363. fAtomForge.Literal = CARLA_URI_MAP_ID_ATOM_LITERAL;
  364. fAtomForge.Long = CARLA_URI_MAP_ID_ATOM_LONG;
  365. fAtomForge.Object = CARLA_URI_MAP_ID_ATOM_OBJECT;
  366. fAtomForge.Path = CARLA_URI_MAP_ID_ATOM_PATH;
  367. fAtomForge.Property = CARLA_URI_MAP_ID_ATOM_PROPERTY;
  368. fAtomForge.Resource = CARLA_URI_MAP_ID_ATOM_RESOURCE;
  369. fAtomForge.Sequence = CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  370. fAtomForge.String = CARLA_URI_MAP_ID_ATOM_STRING;
  371. fAtomForge.Tuple = CARLA_URI_MAP_ID_ATOM_TUPLE;
  372. fAtomForge.URI = CARLA_URI_MAP_ID_ATOM_URI;
  373. fAtomForge.URID = CARLA_URI_MAP_ID_ATOM_URID;
  374. fAtomForge.Vector = CARLA_URI_MAP_ID_ATOM_VECTOR;
  375. #if defined(__clang__)
  376. # pragma clang diagnostic pop
  377. #elif defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  378. # pragma GCC diagnostic pop
  379. #endif
  380. pData->osc.thread.setMode(CarlaPluginThread::PLUGIN_THREAD_LV2_GUI);
  381. }
  382. ~Lv2Plugin() override
  383. {
  384. carla_debug("Lv2Plugin::~Lv2Plugin()");
  385. // close UI
  386. if (fUi.type != UI::TYPE_NULL)
  387. {
  388. showCustomUI(false);
  389. if (fUi.type == UI::TYPE_OSC)
  390. {
  391. pData->osc.thread.stopThread(static_cast<int>(pData->engine->getOptions().uiBridgesTimeout * 2));
  392. }
  393. else
  394. {
  395. if (fFeatures[kFeatureIdUiDataAccess] != nullptr && fFeatures[kFeatureIdUiDataAccess]->data != nullptr)
  396. delete (LV2_Extension_Data_Feature*)fFeatures[kFeatureIdUiDataAccess]->data;
  397. if (fFeatures[kFeatureIdUiPortMap] != nullptr && fFeatures[kFeatureIdUiPortMap]->data != nullptr)
  398. delete (LV2UI_Port_Map*)fFeatures[kFeatureIdUiPortMap]->data;
  399. if (fFeatures[kFeatureIdUiResize] != nullptr && fFeatures[kFeatureIdUiResize]->data != nullptr)
  400. delete (LV2UI_Resize*)fFeatures[kFeatureIdUiResize]->data;
  401. if (fFeatures[kFeatureIdExternalUi] != nullptr && fFeatures[kFeatureIdExternalUi]->data != nullptr)
  402. delete (LV2_External_UI_Host*)fFeatures[kFeatureIdExternalUi]->data;
  403. fUi.descriptor = nullptr;
  404. pData->uiLibClose();
  405. }
  406. #ifndef LV2_UIS_ONLY_BRIDGES
  407. if (fUi.window != nullptr)
  408. {
  409. delete fUi.window;
  410. fUi.window = nullptr;
  411. }
  412. #endif
  413. fUi.rdfDescriptor = nullptr;
  414. }
  415. pData->singleMutex.lock();
  416. pData->masterMutex.lock();
  417. if (pData->client != nullptr && pData->client->isActive())
  418. pData->client->deactivate();
  419. if (pData->active)
  420. {
  421. deactivate();
  422. pData->active = false;
  423. }
  424. if (fDescriptor != nullptr)
  425. {
  426. if (fDescriptor->cleanup != nullptr)
  427. {
  428. if (fHandle != nullptr)
  429. fDescriptor->cleanup(fHandle);
  430. if (fHandle2 != nullptr)
  431. fDescriptor->cleanup(fHandle2);
  432. }
  433. fHandle = nullptr;
  434. fHandle2 = nullptr;
  435. fDescriptor = nullptr;
  436. }
  437. if (fRdfDescriptor != nullptr)
  438. {
  439. delete fRdfDescriptor;
  440. fRdfDescriptor = nullptr;
  441. }
  442. if (fFeatures[kFeatureIdEvent] != nullptr && fFeatures[kFeatureIdEvent]->data != nullptr)
  443. delete (LV2_Event_Feature*)fFeatures[kFeatureIdEvent]->data;
  444. if (fFeatures[kFeatureIdLogs] != nullptr && fFeatures[kFeatureIdLogs]->data != nullptr)
  445. delete (LV2_Log_Log*)fFeatures[kFeatureIdLogs]->data;
  446. if (fFeatures[kFeatureIdStateMakePath] != nullptr && fFeatures[kFeatureIdStateMakePath]->data != nullptr)
  447. delete (LV2_State_Make_Path*)fFeatures[kFeatureIdStateMakePath]->data;
  448. if (fFeatures[kFeatureIdStateMapPath] != nullptr && fFeatures[kFeatureIdStateMapPath]->data != nullptr)
  449. delete (LV2_State_Map_Path*)fFeatures[kFeatureIdStateMapPath]->data;
  450. if (fFeatures[kFeatureIdPrograms] != nullptr && fFeatures[kFeatureIdPrograms]->data != nullptr)
  451. delete (LV2_Programs_Host*)fFeatures[kFeatureIdPrograms]->data;
  452. if (fFeatures[kFeatureIdResizePort] != nullptr && fFeatures[kFeatureIdResizePort]->data != nullptr)
  453. delete (LV2_Resize_Port_Resize*)fFeatures[kFeatureIdResizePort]->data;
  454. if (fFeatures[kFeatureIdRtMemPool] != nullptr && fFeatures[kFeatureIdRtMemPool]->data != nullptr)
  455. delete (LV2_RtMemPool_Pool*)fFeatures[kFeatureIdRtMemPool]->data;
  456. if (fFeatures[kFeatureIdRtMemPoolOld] != nullptr && fFeatures[kFeatureIdRtMemPoolOld]->data != nullptr)
  457. delete (LV2_RtMemPool_Pool_Deprecated*)fFeatures[kFeatureIdRtMemPoolOld]->data;
  458. if (fFeatures[kFeatureIdUriMap] != nullptr && fFeatures[kFeatureIdUriMap]->data != nullptr)
  459. delete (LV2_URI_Map_Feature*)fFeatures[kFeatureIdUriMap]->data;
  460. if (fFeatures[kFeatureIdUridMap] != nullptr && fFeatures[kFeatureIdUridMap]->data != nullptr)
  461. delete (LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data;
  462. if (fFeatures[kFeatureIdUridUnmap] != nullptr && fFeatures[kFeatureIdUridUnmap]->data != nullptr)
  463. delete (LV2_URID_Unmap*)fFeatures[kFeatureIdUridUnmap]->data;
  464. if (fFeatures[kFeatureIdWorker] != nullptr && fFeatures[kFeatureIdWorker]->data != nullptr)
  465. delete (LV2_Worker_Schedule*)fFeatures[kFeatureIdWorker]->data;
  466. for (uint32_t i=0; i < kFeatureCountAll; ++i)
  467. {
  468. if (fFeatures[i] != nullptr)
  469. {
  470. delete fFeatures[i];
  471. fFeatures[i] = nullptr;
  472. }
  473. }
  474. for (LinkedList<const char*>::Itenerator it = fCustomURIDs.begin(); it.valid(); it.next())
  475. {
  476. const char* const uri(it.getValue());
  477. if (uri != nullptr)
  478. delete[] uri;
  479. }
  480. fCustomURIDs.clear();
  481. clearBuffers();
  482. }
  483. // -------------------------------------------------------------------
  484. // Information (base)
  485. PluginType getType() const noexcept override
  486. {
  487. return PLUGIN_LV2;
  488. }
  489. PluginCategory getCategory() const noexcept override
  490. {
  491. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, CarlaPlugin::getCategory());
  492. const LV2_Property cat1(fRdfDescriptor->Type[0]);
  493. const LV2_Property cat2(fRdfDescriptor->Type[1]);
  494. if (LV2_IS_DELAY(cat1, cat2))
  495. return PLUGIN_CATEGORY_DELAY;
  496. if (LV2_IS_DISTORTION(cat1, cat2))
  497. return PLUGIN_CATEGORY_OTHER;
  498. if (LV2_IS_DYNAMICS(cat1, cat2))
  499. return PLUGIN_CATEGORY_DYNAMICS;
  500. if (LV2_IS_EQ(cat1, cat2))
  501. return PLUGIN_CATEGORY_EQ;
  502. if (LV2_IS_FILTER(cat1, cat2))
  503. return PLUGIN_CATEGORY_FILTER;
  504. if (LV2_IS_GENERATOR(cat1, cat2))
  505. return PLUGIN_CATEGORY_SYNTH;
  506. if (LV2_IS_MODULATOR(cat1, cat2))
  507. return PLUGIN_CATEGORY_MODULATOR;
  508. if (LV2_IS_REVERB(cat1, cat2))
  509. return PLUGIN_CATEGORY_DELAY;
  510. if (LV2_IS_SIMULATOR(cat1, cat2))
  511. return PLUGIN_CATEGORY_OTHER;
  512. if (LV2_IS_SPATIAL(cat1, cat2))
  513. return PLUGIN_CATEGORY_OTHER;
  514. if (LV2_IS_SPECTRAL(cat1, cat2))
  515. return PLUGIN_CATEGORY_UTILITY;
  516. if (LV2_IS_UTILITY(cat1, cat2))
  517. return PLUGIN_CATEGORY_UTILITY;
  518. return CarlaPlugin::getCategory();
  519. }
  520. int64_t getUniqueId() const noexcept override
  521. {
  522. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  523. return static_cast<int64_t>(fRdfDescriptor->UniqueID);
  524. }
  525. // -------------------------------------------------------------------
  526. // Information (count)
  527. uint32_t getMidiInCount() const noexcept override
  528. {
  529. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  530. uint32_t count = 0;
  531. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  532. {
  533. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  534. if (LV2_IS_PORT_INPUT(portTypes) && LV2_PORT_SUPPORTS_MIDI_EVENT(portTypes))
  535. ++count;
  536. }
  537. return count;
  538. }
  539. uint32_t getMidiOutCount() const noexcept override
  540. {
  541. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  542. uint32_t count = 0;
  543. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  544. {
  545. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  546. if (LV2_IS_PORT_OUTPUT(portTypes) && LV2_PORT_SUPPORTS_MIDI_EVENT(portTypes))
  547. ++count;
  548. }
  549. return count;
  550. }
  551. uint32_t getParameterScalePointCount(const uint32_t parameterId) const noexcept override
  552. {
  553. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0);
  554. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  555. const int32_t rindex(pData->param.data[parameterId].rindex);
  556. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  557. {
  558. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  559. return port->ScalePointCount;
  560. }
  561. return 0;
  562. }
  563. // -------------------------------------------------------------------
  564. // Information (current data)
  565. // nothing
  566. // -------------------------------------------------------------------
  567. // Information (per-plugin data)
  568. unsigned int getOptionsAvailable() const noexcept override
  569. {
  570. const bool hasMidiIn(getMidiInCount() > 0);
  571. unsigned int options = 0x0;
  572. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  573. if (! (hasMidiIn || needsFixedBuffer()))
  574. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  575. if (pData->engine->getProccessMode() != ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  576. {
  577. if (pData->options & PLUGIN_OPTION_FORCE_STEREO)
  578. options |= PLUGIN_OPTION_FORCE_STEREO;
  579. else if (pData->audioIn.count <= 1 && pData->audioOut.count <= 1 && (pData->audioIn.count != 0 || pData->audioOut.count != 0))
  580. options |= PLUGIN_OPTION_FORCE_STEREO;
  581. }
  582. if (hasMidiIn)
  583. {
  584. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  585. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  586. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  587. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  588. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  589. }
  590. return options;
  591. }
  592. float getParameterValue(const uint32_t parameterId) const noexcept override
  593. {
  594. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr, 0.0f);
  595. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  596. if (pData->param.data[parameterId].hints & PARAMETER_IS_STRICT_BOUNDS)
  597. pData->param.ranges[parameterId].fixValue(fParamBuffers[parameterId]);
  598. return fParamBuffers[parameterId];
  599. }
  600. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept override
  601. {
  602. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, 0.0f);
  603. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  604. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId), 0.0f);
  605. const int32_t rindex(pData->param.data[parameterId].rindex);
  606. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  607. {
  608. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  609. if (scalePointId < port->ScalePointCount)
  610. {
  611. const LV2_RDF_PortScalePoint* const portScalePoint(&port->ScalePoints[scalePointId]);
  612. return portScalePoint->Value;
  613. }
  614. }
  615. return 0.0f;
  616. }
  617. void getLabel(char* const strBuf) const noexcept override
  618. {
  619. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  620. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor->URI != nullptr,);
  621. std::strncpy(strBuf, fRdfDescriptor->URI, STR_MAX);
  622. }
  623. void getMaker(char* const strBuf) const noexcept override
  624. {
  625. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  626. if (fRdfDescriptor->Author != nullptr)
  627. std::strncpy(strBuf, fRdfDescriptor->Author, STR_MAX);
  628. else
  629. CarlaPlugin::getMaker(strBuf);
  630. }
  631. void getCopyright(char* const strBuf) const noexcept override
  632. {
  633. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  634. if (fRdfDescriptor->License != nullptr)
  635. std::strncpy(strBuf, fRdfDescriptor->License, STR_MAX);
  636. else
  637. CarlaPlugin::getCopyright(strBuf);
  638. }
  639. void getRealName(char* const strBuf) const noexcept override
  640. {
  641. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  642. if (fRdfDescriptor->Name != nullptr)
  643. std::strncpy(strBuf, fRdfDescriptor->Name, STR_MAX);
  644. else
  645. CarlaPlugin::getRealName(strBuf);
  646. }
  647. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  648. {
  649. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  650. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  651. const int32_t rindex(pData->param.data[parameterId].rindex);
  652. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  653. std::strncpy(strBuf, fRdfDescriptor->Ports[rindex].Name, STR_MAX);
  654. else
  655. CarlaPlugin::getParameterName(parameterId, strBuf);
  656. }
  657. void getParameterSymbol(const uint32_t parameterId, char* const strBuf) const noexcept override
  658. {
  659. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  660. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  661. const int32_t rindex(pData->param.data[parameterId].rindex);
  662. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  663. std::strncpy(strBuf, fRdfDescriptor->Ports[rindex].Symbol, STR_MAX);
  664. else
  665. CarlaPlugin::getParameterSymbol(parameterId, strBuf);
  666. }
  667. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  668. {
  669. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  670. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  671. const int32_t rindex(pData->param.data[parameterId].rindex);
  672. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  673. {
  674. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  675. if (LV2_HAVE_PORT_UNIT_SYMBOL(port->Unit.Hints) && port->Unit.Symbol != nullptr)
  676. {
  677. std::strncpy(strBuf, port->Unit.Symbol, STR_MAX);
  678. return;
  679. }
  680. if (LV2_HAVE_PORT_UNIT_UNIT(port->Unit.Hints))
  681. {
  682. switch (port->Unit.Unit)
  683. {
  684. case LV2_PORT_UNIT_BAR:
  685. std::strncpy(strBuf, "bars", STR_MAX);
  686. return;
  687. case LV2_PORT_UNIT_BEAT:
  688. std::strncpy(strBuf, "beats", STR_MAX);
  689. return;
  690. case LV2_PORT_UNIT_BPM:
  691. std::strncpy(strBuf, "BPM", STR_MAX);
  692. return;
  693. case LV2_PORT_UNIT_CENT:
  694. std::strncpy(strBuf, "ct", STR_MAX);
  695. return;
  696. case LV2_PORT_UNIT_CM:
  697. std::strncpy(strBuf, "cm", STR_MAX);
  698. return;
  699. case LV2_PORT_UNIT_COEF:
  700. std::strncpy(strBuf, "(coef)", STR_MAX);
  701. return;
  702. case LV2_PORT_UNIT_DB:
  703. std::strncpy(strBuf, "dB", STR_MAX);
  704. return;
  705. case LV2_PORT_UNIT_DEGREE:
  706. std::strncpy(strBuf, "deg", STR_MAX);
  707. return;
  708. case LV2_PORT_UNIT_FRAME:
  709. std::strncpy(strBuf, "frames", STR_MAX);
  710. return;
  711. case LV2_PORT_UNIT_HZ:
  712. std::strncpy(strBuf, "Hz", STR_MAX);
  713. return;
  714. case LV2_PORT_UNIT_INCH:
  715. std::strncpy(strBuf, "in", STR_MAX);
  716. return;
  717. case LV2_PORT_UNIT_KHZ:
  718. std::strncpy(strBuf, "kHz", STR_MAX);
  719. return;
  720. case LV2_PORT_UNIT_KM:
  721. std::strncpy(strBuf, "km", STR_MAX);
  722. return;
  723. case LV2_PORT_UNIT_M:
  724. std::strncpy(strBuf, "m", STR_MAX);
  725. return;
  726. case LV2_PORT_UNIT_MHZ:
  727. std::strncpy(strBuf, "MHz", STR_MAX);
  728. return;
  729. case LV2_PORT_UNIT_MIDINOTE:
  730. std::strncpy(strBuf, "note", STR_MAX);
  731. return;
  732. case LV2_PORT_UNIT_MILE:
  733. std::strncpy(strBuf, "mi", STR_MAX);
  734. return;
  735. case LV2_PORT_UNIT_MIN:
  736. std::strncpy(strBuf, "min", STR_MAX);
  737. return;
  738. case LV2_PORT_UNIT_MM:
  739. std::strncpy(strBuf, "mm", STR_MAX);
  740. return;
  741. case LV2_PORT_UNIT_MS:
  742. std::strncpy(strBuf, "ms", STR_MAX);
  743. return;
  744. case LV2_PORT_UNIT_OCT:
  745. std::strncpy(strBuf, "oct", STR_MAX);
  746. return;
  747. case LV2_PORT_UNIT_PC:
  748. std::strncpy(strBuf, "%", STR_MAX);
  749. return;
  750. case LV2_PORT_UNIT_S:
  751. std::strncpy(strBuf, "s", STR_MAX);
  752. return;
  753. case LV2_PORT_UNIT_SEMITONE:
  754. std::strncpy(strBuf, "semi", STR_MAX);
  755. return;
  756. }
  757. }
  758. }
  759. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  760. }
  761. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept override
  762. {
  763. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  764. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  765. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId),);
  766. const int32_t rindex(pData->param.data[parameterId].rindex);
  767. if (rindex < static_cast<int32_t>(fRdfDescriptor->PortCount))
  768. {
  769. const LV2_RDF_Port* const port(&fRdfDescriptor->Ports[rindex]);
  770. if (scalePointId < port->ScalePointCount)
  771. {
  772. const LV2_RDF_PortScalePoint* const portScalePoint(&port->ScalePoints[scalePointId]);
  773. if (portScalePoint->Label != nullptr)
  774. {
  775. std::strncpy(strBuf, portScalePoint->Label, STR_MAX);
  776. return;
  777. }
  778. }
  779. }
  780. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  781. }
  782. // -------------------------------------------------------------------
  783. // Set data (state)
  784. void prepareForSave() override
  785. {
  786. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  787. if (fExt.state != nullptr && fExt.state->save != nullptr)
  788. {
  789. fExt.state->save(fHandle, carla_lv2_state_store, this, LV2_STATE_IS_POD, fFeatures);
  790. if (fHandle2 != nullptr)
  791. fExt.state->save(fHandle2, carla_lv2_state_store, this, LV2_STATE_IS_POD, fFeatures);
  792. }
  793. }
  794. // -------------------------------------------------------------------
  795. // Set data (internal stuff)
  796. void setName(const char* const newName) override
  797. {
  798. CarlaPlugin::setName(newName);
  799. if (fLv2Options.windowTitle == nullptr)
  800. return;
  801. QString guiTitle(QString("%1 (GUI)").arg(pData->name));
  802. delete[] fLv2Options.windowTitle;
  803. fLv2Options.windowTitle = carla_strdup(guiTitle.toUtf8().constData());
  804. fLv2Options.opts[Lv2PluginOptions::WindowTitle].size = (uint32_t)std::strlen(fLv2Options.windowTitle);
  805. fLv2Options.opts[Lv2PluginOptions::WindowTitle].value = fLv2Options.windowTitle;
  806. if (fFeatures[kFeatureIdExternalUi] != nullptr && fFeatures[kFeatureIdExternalUi]->data != nullptr)
  807. ((LV2_External_UI_Host*)fFeatures[kFeatureIdExternalUi]->data)->plugin_human_id = fLv2Options.windowTitle;
  808. #ifndef LV2_UIS_ONLY_BRIDGES
  809. if (fUi.window != nullptr)
  810. fUi.window->setTitle(fLv2Options.windowTitle);
  811. #endif
  812. }
  813. // -------------------------------------------------------------------
  814. // Set data (plugin-specific stuff)
  815. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  816. {
  817. CARLA_SAFE_ASSERT_RETURN(fParamBuffers != nullptr,);
  818. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  819. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  820. fParamBuffers[parameterId] = fixedValue;
  821. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  822. }
  823. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  824. {
  825. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  826. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  827. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  828. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  829. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  830. carla_debug("Lv2Plugin::setCustomData(%s, %s, %s, %s)", type, key, value, bool2str(sendGui));
  831. // we should only call state restore once
  832. // so inject this in CarlaPlugin::loadSaveState
  833. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) == 0 && std::strcmp(key, "CarlaLoadLv2StateNow") == 0 && std::strcmp(value, "true") == 0)
  834. {
  835. if (fExt.state == nullptr)
  836. return;
  837. LV2_State_Status status = LV2_STATE_ERR_UNKNOWN;
  838. {
  839. const ScopedSingleProcessLocker spl(this, true);
  840. try {
  841. status = fExt.state->restore(fHandle, carla_lv2_state_retrieve, this, 0, fFeatures);
  842. } catch(...) {}
  843. if (fHandle2 != nullptr)
  844. {
  845. try {
  846. fExt.state->restore(fHandle, carla_lv2_state_retrieve, this, 0, fFeatures);
  847. } catch(...) {}
  848. }
  849. }
  850. switch (status)
  851. {
  852. case LV2_STATE_SUCCESS:
  853. carla_debug("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - success", type, key, bool2str(sendGui));
  854. break;
  855. case LV2_STATE_ERR_UNKNOWN:
  856. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - unknown error", type, key, bool2str(sendGui));
  857. break;
  858. case LV2_STATE_ERR_BAD_TYPE:
  859. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, bad type", type, key, bool2str(sendGui));
  860. break;
  861. case LV2_STATE_ERR_BAD_FLAGS:
  862. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, bad flags", type, key, bool2str(sendGui));
  863. break;
  864. case LV2_STATE_ERR_NO_FEATURE:
  865. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, missing feature", type, key, bool2str(sendGui));
  866. break;
  867. case LV2_STATE_ERR_NO_PROPERTY:
  868. carla_stderr("Lv2Plugin::setCustomData(\"%s\", \"%s\", <value>, %s) - error, missing property", type, key, bool2str(sendGui));
  869. break;
  870. }
  871. return;
  872. }
  873. CarlaPlugin::setCustomData(type, key, value, sendGui);
  874. }
  875. void setProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  876. {
  877. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  878. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->prog.count),);
  879. if (index >= 0 && index < static_cast<int32_t>(fRdfDescriptor->PresetCount))
  880. {
  881. if (const LilvState* state = Lv2WorldClass::getInstance().getState(fRdfDescriptor->Presets[index].URI, (const LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data))
  882. {
  883. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  884. lilv_state_restore(state, fExt.state, fHandle, carla_lilv_set_port_value, this, 0, fFeatures);
  885. if (fHandle2 != nullptr)
  886. lilv_state_restore(state, fExt.state, fHandle2, carla_lilv_set_port_value, this, 0, fFeatures);
  887. }
  888. }
  889. CarlaPlugin::setProgram(index, sendGui, sendOsc, sendCallback);
  890. }
  891. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  892. {
  893. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  894. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  895. if (index >= 0 && fExt.programs != nullptr && fExt.programs->select_program != nullptr)
  896. {
  897. const uint32_t bank(pData->midiprog.data[index].bank);
  898. const uint32_t program(pData->midiprog.data[index].program);
  899. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  900. try {
  901. fExt.programs->select_program(fHandle, bank, program);
  902. } catch(...) {}
  903. if (fHandle2 != nullptr)
  904. {
  905. try {
  906. fExt.programs->select_program(fHandle2, bank, program);
  907. } catch(...) {}
  908. }
  909. }
  910. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  911. }
  912. // -------------------------------------------------------------------
  913. // Set ui stuff
  914. void showCustomUI(const bool yesNo) override
  915. {
  916. CARLA_SAFE_ASSERT_RETURN(fUi.type != UI::TYPE_NULL,);
  917. if (! yesNo)
  918. pData->transientTryCounter = 0;
  919. if (fUi.type == UI::TYPE_OSC)
  920. {
  921. if (yesNo)
  922. {
  923. pData->osc.data.free();
  924. pData->osc.thread.startThread();
  925. }
  926. else
  927. {
  928. if (pData->osc.data.target != nullptr)
  929. {
  930. osc_send_hide(pData->osc.data);
  931. osc_send_quit(pData->osc.data);
  932. pData->osc.data.free();
  933. }
  934. pData->osc.thread.stopThread(static_cast<int>(pData->engine->getOptions().uiBridgesTimeout * 2));
  935. }
  936. return;
  937. }
  938. // take some precautions
  939. CARLA_SAFE_ASSERT_RETURN(fUi.descriptor != nullptr,);
  940. CARLA_SAFE_ASSERT_RETURN(fUi.rdfDescriptor != nullptr,);
  941. if (yesNo)
  942. {
  943. CARLA_SAFE_ASSERT_RETURN(fUi.descriptor->instantiate != nullptr,);
  944. CARLA_SAFE_ASSERT_RETURN(fUi.descriptor->cleanup != nullptr,);
  945. }
  946. else
  947. {
  948. if (fUi.handle == nullptr)
  949. return;
  950. }
  951. if (yesNo)
  952. {
  953. if (fUi.handle == nullptr)
  954. {
  955. #ifndef LV2_UIS_ONLY_BRIDGES
  956. if (fUi.type == UI::TYPE_EMBED)
  957. {
  958. const char* msg = nullptr;
  959. const uintptr_t frontendWinId(pData->engine->getOptions().frontendWinId);
  960. switch (fUi.rdfDescriptor->Type)
  961. {
  962. case LV2_UI_GTK2:
  963. case LV2_UI_GTK3:
  964. case LV2_UI_QT4:
  965. case LV2_UI_QT5:
  966. case LV2_UI_EXTERNAL:
  967. case LV2_UI_OLD_EXTERNAL:
  968. msg = "Invalid UI type";
  969. break;
  970. case LV2_UI_COCOA:
  971. # ifdef CARLA_OS_MAC
  972. fUi.window = CarlaPluginUi::newCocoa(this, frontendWinId);
  973. # else
  974. msg = "UI is for MacOS only";
  975. # endif
  976. break;
  977. case LV2_UI_WINDOWS:
  978. # ifdef CARLA_OS_WIN
  979. fUi.window = CarlaPluginUi::newWindows(this, frontendWinId);
  980. # else
  981. msg = "UI is for Windows only";
  982. # endif
  983. break;
  984. case LV2_UI_X11:
  985. # ifdef HAVE_X11
  986. fUi.window = CarlaPluginUi::newX11(this, frontendWinId);
  987. # else
  988. msg = "UI is only for systems with X11";
  989. # endif
  990. break;
  991. default:
  992. msg = "Unknown UI type";
  993. break;
  994. }
  995. if (fUi.window == nullptr && fExt.uishow == nullptr)
  996. {
  997. return pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, msg);
  998. // unused
  999. (void)frontendWinId;
  1000. }
  1001. if (fUi.window != nullptr)
  1002. {
  1003. fUi.window->setTitle(fLv2Options.windowTitle);
  1004. fFeatures[kFeatureIdUiParent]->data = fUi.window->getPtr();
  1005. }
  1006. }
  1007. #endif
  1008. fUi.widget = nullptr;
  1009. fUi.handle = fUi.descriptor->instantiate(fUi.descriptor, fRdfDescriptor->URI, fUi.rdfDescriptor->Bundle,
  1010. carla_lv2_ui_write_function, this, &fUi.widget, fFeatures);
  1011. }
  1012. CARLA_SAFE_ASSERT(fUi.handle != nullptr);
  1013. CARLA_SAFE_ASSERT(fUi.type != UI::TYPE_EXTERNAL || fUi.widget != nullptr);
  1014. if (fUi.handle == nullptr || (fUi.type == UI::TYPE_EXTERNAL && fUi.widget == nullptr))
  1015. {
  1016. fUi.widget = nullptr;
  1017. if (fUi.handle != nullptr)
  1018. {
  1019. fUi.descriptor->cleanup(fUi.handle);
  1020. fUi.handle = nullptr;
  1021. }
  1022. return pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, "Plugin refused to open its own UI");
  1023. }
  1024. updateUi();
  1025. #ifndef LV2_UIS_ONLY_BRIDGES
  1026. if (fUi.type == UI::TYPE_EMBED)
  1027. {
  1028. if (fUi.window != nullptr)
  1029. {
  1030. fUi.window->show();
  1031. }
  1032. else if (fExt.uishow != nullptr)
  1033. {
  1034. fExt.uishow->show(fUi.handle);
  1035. pData->tryTransient();
  1036. }
  1037. }
  1038. else
  1039. #endif
  1040. {
  1041. LV2_EXTERNAL_UI_SHOW((LV2_External_UI_Widget*)fUi.widget);
  1042. pData->tryTransient();
  1043. }
  1044. }
  1045. else
  1046. {
  1047. #ifndef LV2_UIS_ONLY_BRIDGES
  1048. if (fUi.type == UI::TYPE_EMBED)
  1049. {
  1050. if (fUi.window != nullptr)
  1051. fUi.window->hide();
  1052. else if (fExt.uishow != nullptr)
  1053. fExt.uishow->hide(fUi.handle);
  1054. }
  1055. else
  1056. #endif
  1057. {
  1058. CARLA_SAFE_ASSERT(fUi.widget != nullptr);
  1059. if (fUi.widget != nullptr)
  1060. LV2_EXTERNAL_UI_HIDE((LV2_External_UI_Widget*)fUi.widget);
  1061. }
  1062. fUi.descriptor->cleanup(fUi.handle);
  1063. fUi.handle = nullptr;
  1064. fUi.widget = nullptr;
  1065. }
  1066. }
  1067. void idle() override
  1068. {
  1069. if (! fAtomQueueOut.isEmpty())
  1070. {
  1071. char dumpBuf[fAtomQueueOut.getSize()];
  1072. Lv2AtomQueue tmpQueue;
  1073. tmpQueue.copyAndDumpDataFromQueue(fAtomQueueOut, dumpBuf);
  1074. CARLA_SAFE_ASSERT(! tmpQueue.isEmpty());
  1075. uint32_t portIndex;
  1076. const LV2_Atom* atom;
  1077. const bool hasPortEvent(fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr);
  1078. for (; tmpQueue.get(&atom, &portIndex);)
  1079. {
  1080. if (atom->type == CARLA_URI_MAP_ID_CARLA_ATOM_WORKER)
  1081. {
  1082. CARLA_SAFE_ASSERT_CONTINUE(fExt.worker != nullptr && fExt.worker->work != nullptr);
  1083. fExt.worker->work(fHandle, carla_lv2_worker_respond, this, atom->size, LV2_ATOM_BODY_CONST(atom));
  1084. }
  1085. else if (fUi.type == UI::TYPE_OSC)
  1086. {
  1087. if (pData->osc.data.target != nullptr)
  1088. {
  1089. QByteArray chunk((const char*)atom, static_cast<int>(lv2_atom_total_size(atom)));
  1090. osc_send_lv2_atom_transfer(pData->osc.data, portIndex, chunk.toBase64().constData());
  1091. }
  1092. }
  1093. else
  1094. {
  1095. if (hasPortEvent)
  1096. fUi.descriptor->port_event(fUi.handle, portIndex, lv2_atom_total_size(atom), CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT, atom);
  1097. }
  1098. }
  1099. }
  1100. if (fUi.handle != nullptr && fUi.descriptor != nullptr)
  1101. {
  1102. if (fUi.type == UI::TYPE_EXTERNAL && fUi.widget != nullptr)
  1103. LV2_EXTERNAL_UI_RUN((LV2_External_UI_Widget*)fUi.widget);
  1104. #ifndef LV2_UIS_ONLY_BRIDGES
  1105. else if (fUi.type == UI::TYPE_EMBED && fUi.window != nullptr)
  1106. fUi.window->idle();
  1107. if (fExt.uiidle != nullptr && fExt.uiidle->idle(fUi.handle) != 0)
  1108. {
  1109. showCustomUI(false);
  1110. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1111. }
  1112. #endif
  1113. }
  1114. CarlaPlugin::idle();
  1115. }
  1116. // -------------------------------------------------------------------
  1117. // Plugin state
  1118. void reload() override
  1119. {
  1120. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  1121. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1122. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1123. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  1124. carla_debug("Lv2Plugin::reload() - start");
  1125. const EngineProcessMode processMode(pData->engine->getProccessMode());
  1126. // Safely disable plugin for reload
  1127. const ScopedDisabler sd(this);
  1128. if (pData->active)
  1129. deactivate();
  1130. clearBuffers();
  1131. const float sampleRate(static_cast<float>(pData->engine->getSampleRate()));
  1132. const uint32_t portCount(fRdfDescriptor->PortCount);
  1133. uint32_t aIns, aOuts, cvIns, cvOuts, params;
  1134. aIns = aOuts = cvIns = cvOuts = params = 0;
  1135. LinkedList<unsigned int> evIns, evOuts;
  1136. const uint32_t eventBufferSize(static_cast<uint32_t>(fLv2Options.sequenceSize));
  1137. bool forcedStereoIn, forcedStereoOut;
  1138. forcedStereoIn = forcedStereoOut = false;
  1139. bool needsCtrlIn, needsCtrlOut;
  1140. needsCtrlIn = needsCtrlOut = false;
  1141. for (uint32_t i=0; i < portCount; ++i)
  1142. {
  1143. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  1144. if (LV2_IS_PORT_AUDIO(portTypes))
  1145. {
  1146. if (LV2_IS_PORT_INPUT(portTypes))
  1147. aIns += 1;
  1148. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1149. aOuts += 1;
  1150. }
  1151. else if (LV2_IS_PORT_CV(portTypes))
  1152. {
  1153. if (LV2_IS_PORT_INPUT(portTypes))
  1154. cvIns += 1;
  1155. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1156. cvOuts += 1;
  1157. }
  1158. else if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes))
  1159. {
  1160. if (LV2_IS_PORT_INPUT(portTypes))
  1161. evIns.append(CARLA_EVENT_DATA_ATOM);
  1162. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1163. evOuts.append(CARLA_EVENT_DATA_ATOM);
  1164. }
  1165. else if (LV2_IS_PORT_EVENT(portTypes))
  1166. {
  1167. if (LV2_IS_PORT_INPUT(portTypes))
  1168. evIns.append(CARLA_EVENT_DATA_EVENT);
  1169. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1170. evOuts.append(CARLA_EVENT_DATA_EVENT);
  1171. }
  1172. else if (LV2_IS_PORT_MIDI_LL(portTypes))
  1173. {
  1174. if (LV2_IS_PORT_INPUT(portTypes))
  1175. evIns.append(CARLA_EVENT_DATA_MIDI_LL);
  1176. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1177. evOuts.append(CARLA_EVENT_DATA_MIDI_LL);
  1178. }
  1179. else if (LV2_IS_PORT_CONTROL(portTypes))
  1180. params += 1;
  1181. }
  1182. recheckExtensions();
  1183. if ((pData->options & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1) && fExt.state == nullptr && fExt.worker == nullptr)
  1184. {
  1185. if (fHandle2 == nullptr)
  1186. {
  1187. try {
  1188. fHandle2 = fDescriptor->instantiate(fDescriptor, sampleRate, fRdfDescriptor->Bundle, fFeatures);
  1189. } catch(...) {}
  1190. }
  1191. if (fHandle2 != nullptr)
  1192. {
  1193. if (aIns == 1)
  1194. {
  1195. aIns = 2;
  1196. forcedStereoIn = true;
  1197. }
  1198. if (aOuts == 1)
  1199. {
  1200. aOuts = 2;
  1201. forcedStereoOut = true;
  1202. }
  1203. }
  1204. }
  1205. if (aIns > 0)
  1206. {
  1207. pData->audioIn.createNew(aIns);
  1208. fAudioInBuffers = new float*[aIns];
  1209. for (uint32_t i=0; i < aIns; ++i)
  1210. fAudioInBuffers[i] = nullptr;
  1211. }
  1212. if (aOuts > 0)
  1213. {
  1214. pData->audioOut.createNew(aOuts);
  1215. fAudioOutBuffers = new float*[aOuts];
  1216. needsCtrlIn = true;
  1217. for (uint32_t i=0; i < aOuts; ++i)
  1218. fAudioOutBuffers[i] = nullptr;
  1219. }
  1220. if (cvIns > 0)
  1221. {
  1222. fCvIn.createNew(cvIns);
  1223. fCvInBuffers = new float*[cvIns];
  1224. for (uint32_t i=0; i < cvIns; ++i)
  1225. fCvInBuffers[i] = nullptr;
  1226. }
  1227. if (cvOuts > 0)
  1228. {
  1229. fCvOut.createNew(cvOuts);
  1230. fCvOutBuffers = new float*[cvOuts];
  1231. for (uint32_t i=0; i < cvOuts; ++i)
  1232. fCvOutBuffers[i] = nullptr;
  1233. }
  1234. if (params > 0)
  1235. {
  1236. pData->param.createNew(params, true, false);
  1237. fParamBuffers = new float[params];
  1238. FLOAT_CLEAR(fParamBuffers, params);
  1239. }
  1240. if (const uint32_t count = static_cast<uint32_t>(evIns.count()))
  1241. {
  1242. fEventsIn.createNew(count);
  1243. for (uint32_t i=0; i < count; ++i)
  1244. {
  1245. const uint32_t& type(evIns.getAt(i));
  1246. if (type == CARLA_EVENT_DATA_ATOM)
  1247. {
  1248. fEventsIn.data[i].type = CARLA_EVENT_DATA_ATOM;
  1249. fEventsIn.data[i].atom = lv2_atom_buffer_new(eventBufferSize, CARLA_URI_MAP_ID_NULL, CARLA_URI_MAP_ID_ATOM_SEQUENCE, true);
  1250. }
  1251. else if (type == CARLA_EVENT_DATA_EVENT)
  1252. {
  1253. fEventsIn.data[i].type = CARLA_EVENT_DATA_EVENT;
  1254. fEventsIn.data[i].event = lv2_event_buffer_new(eventBufferSize, LV2_EVENT_AUDIO_STAMP);
  1255. }
  1256. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  1257. {
  1258. fEventsIn.data[i].type = CARLA_EVENT_DATA_MIDI_LL;
  1259. fEventsIn.data[i].midi.capacity = eventBufferSize;
  1260. fEventsIn.data[i].midi.data = new unsigned char[eventBufferSize];
  1261. }
  1262. }
  1263. }
  1264. else
  1265. {
  1266. fEventsIn.createNew(1);
  1267. fEventsIn.ctrl = &fEventsIn.data[0];
  1268. }
  1269. if (const uint32_t count = static_cast<uint32_t>(evOuts.count()))
  1270. {
  1271. fEventsOut.createNew(count);
  1272. for (uint32_t i=0; i < count; ++i)
  1273. {
  1274. const uint32_t& type(evOuts.getAt(i));
  1275. if (type == CARLA_EVENT_DATA_ATOM)
  1276. {
  1277. fEventsOut.data[i].type = CARLA_EVENT_DATA_ATOM;
  1278. fEventsOut.data[i].atom = lv2_atom_buffer_new(eventBufferSize, CARLA_URI_MAP_ID_NULL, CARLA_URI_MAP_ID_ATOM_SEQUENCE, false);
  1279. }
  1280. else if (type == CARLA_EVENT_DATA_EVENT)
  1281. {
  1282. fEventsOut.data[i].type = CARLA_EVENT_DATA_EVENT;
  1283. fEventsOut.data[i].event = lv2_event_buffer_new(eventBufferSize, LV2_EVENT_AUDIO_STAMP);
  1284. }
  1285. else if (type == CARLA_EVENT_DATA_MIDI_LL)
  1286. {
  1287. fEventsOut.data[i].type = CARLA_EVENT_DATA_MIDI_LL;
  1288. fEventsOut.data[i].midi.capacity = eventBufferSize;
  1289. fEventsOut.data[i].midi.data = new unsigned char[eventBufferSize];
  1290. }
  1291. }
  1292. }
  1293. const uint portNameSize(pData->engine->getMaxPortNameSize());
  1294. CarlaString portName;
  1295. for (uint32_t i=0, iAudioIn=0, iAudioOut=0, iCvIn=0, iCvOut=0, iEvIn=0, iEvOut=0, iCtrl=0; i < portCount; ++i)
  1296. {
  1297. const LV2_Property portTypes(fRdfDescriptor->Ports[i].Types);
  1298. portName.clear();
  1299. if (LV2_IS_PORT_AUDIO(portTypes) || LV2_IS_PORT_CV(portTypes) || LV2_IS_PORT_ATOM_SEQUENCE(portTypes) || LV2_IS_PORT_EVENT(portTypes) || LV2_IS_PORT_MIDI_LL(portTypes))
  1300. {
  1301. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1302. {
  1303. portName = pData->name;
  1304. portName += ":";
  1305. }
  1306. portName += fRdfDescriptor->Ports[i].Name;
  1307. portName.truncate(portNameSize);
  1308. }
  1309. if (LV2_IS_PORT_AUDIO(portTypes))
  1310. {
  1311. if (LV2_IS_PORT_INPUT(portTypes))
  1312. {
  1313. uint32_t j = iAudioIn++;
  1314. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  1315. pData->audioIn.ports[j].rindex = i;
  1316. if (forcedStereoIn)
  1317. {
  1318. portName += "_2";
  1319. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  1320. pData->audioIn.ports[1].rindex = i;
  1321. }
  1322. }
  1323. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1324. {
  1325. uint32_t j = iAudioOut++;
  1326. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  1327. pData->audioOut.ports[j].rindex = i;
  1328. if (forcedStereoOut)
  1329. {
  1330. portName += "_2";
  1331. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  1332. pData->audioOut.ports[1].rindex = i;
  1333. }
  1334. }
  1335. else
  1336. carla_stderr2("WARNING - Got a broken Port (Audio, but not input or output)");
  1337. }
  1338. else if (LV2_IS_PORT_CV(portTypes))
  1339. {
  1340. if (LV2_IS_PORT_INPUT(portTypes))
  1341. {
  1342. uint32_t j = iCvIn++;
  1343. fCvIn.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, true);
  1344. fCvIn.ports[j].rindex = i;
  1345. }
  1346. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1347. {
  1348. uint32_t j = iCvOut++;
  1349. fCvOut.ports[j].port = (CarlaEngineCVPort*)pData->client->addPort(kEnginePortTypeCV, portName, false);
  1350. fCvOut.ports[j].rindex = i;
  1351. }
  1352. else
  1353. carla_stderr("WARNING - Got a broken Port (CV, but not input or output)");
  1354. }
  1355. else if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes))
  1356. {
  1357. if (LV2_IS_PORT_INPUT(portTypes))
  1358. {
  1359. uint32_t j = iEvIn++;
  1360. fDescriptor->connect_port(fHandle, i, &fEventsIn.data[j].atom->atoms);
  1361. if (fHandle2 != nullptr)
  1362. fDescriptor->connect_port(fHandle2, i, &fEventsIn.data[j].atom->atoms);
  1363. fEventsIn.data[j].rindex = i;
  1364. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1365. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1366. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1367. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1368. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1369. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1370. if (evIns.count() == 1)
  1371. {
  1372. fEventsIn.ctrl = &fEventsIn.data[j];
  1373. fEventsIn.ctrlIndex = j;
  1374. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1375. needsCtrlIn = true;
  1376. }
  1377. else
  1378. {
  1379. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1380. fEventsIn.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1381. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1382. {
  1383. fEventsIn.ctrl = &fEventsIn.data[j];
  1384. fEventsIn.ctrlIndex = j;
  1385. }
  1386. }
  1387. }
  1388. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1389. {
  1390. uint32_t j = iEvOut++;
  1391. fDescriptor->connect_port(fHandle, i, &fEventsOut.data[j].atom->atoms);
  1392. if (fHandle2 != nullptr)
  1393. fDescriptor->connect_port(fHandle2, i, &fEventsOut.data[j].atom->atoms);
  1394. fEventsOut.data[j].rindex = i;
  1395. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1396. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1397. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1398. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1399. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1400. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1401. if (evOuts.count() == 1)
  1402. {
  1403. fEventsOut.ctrl = &fEventsOut.data[j];
  1404. fEventsOut.ctrlIndex = j;
  1405. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1406. needsCtrlOut = true;
  1407. }
  1408. else
  1409. {
  1410. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1411. fEventsOut.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1412. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1413. {
  1414. fEventsOut.ctrl = &fEventsOut.data[j];
  1415. fEventsOut.ctrlIndex = j;
  1416. }
  1417. }
  1418. }
  1419. else
  1420. carla_stderr2("WARNING - Got a broken Port (Atom-Sequence, but not input or output)");
  1421. }
  1422. else if (LV2_IS_PORT_EVENT(portTypes))
  1423. {
  1424. if (LV2_IS_PORT_INPUT(portTypes))
  1425. {
  1426. uint32_t j = iEvIn++;
  1427. fDescriptor->connect_port(fHandle, i, fEventsIn.data[j].event);
  1428. if (fHandle2 != nullptr)
  1429. fDescriptor->connect_port(fHandle2, i, fEventsIn.data[j].event);
  1430. fEventsIn.data[j].rindex = i;
  1431. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1432. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1433. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1434. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1435. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1436. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1437. if (evIns.count() == 1)
  1438. {
  1439. fEventsIn.ctrl = &fEventsIn.data[j];
  1440. fEventsIn.ctrlIndex = j;
  1441. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1442. needsCtrlIn = true;
  1443. }
  1444. else
  1445. {
  1446. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1447. fEventsIn.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1448. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1449. {
  1450. fEventsIn.ctrl = &fEventsIn.data[j];
  1451. fEventsIn.ctrlIndex = j;
  1452. }
  1453. }
  1454. }
  1455. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1456. {
  1457. uint32_t j = iEvOut++;
  1458. fDescriptor->connect_port(fHandle, i, fEventsOut.data[j].event);
  1459. if (fHandle2 != nullptr)
  1460. fDescriptor->connect_port(fHandle2, i, fEventsOut.data[j].event);
  1461. fEventsOut.data[j].rindex = i;
  1462. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1463. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1464. if (portTypes & LV2_PORT_DATA_PATCH_MESSAGE)
  1465. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MESSAGE;
  1466. if (portTypes & LV2_PORT_DATA_TIME_POSITION)
  1467. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_TIME;
  1468. if (evOuts.count() == 1)
  1469. {
  1470. fEventsOut.ctrl = &fEventsOut.data[j];
  1471. fEventsOut.ctrlIndex = j;
  1472. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1473. needsCtrlOut = true;
  1474. }
  1475. else
  1476. {
  1477. if (portTypes & LV2_PORT_DATA_MIDI_EVENT)
  1478. fEventsOut.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1479. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1480. {
  1481. fEventsOut.ctrl = &fEventsOut.data[j];
  1482. fEventsOut.ctrlIndex = j;
  1483. }
  1484. }
  1485. }
  1486. else
  1487. carla_stderr2("WARNING - Got a broken Port (Event, but not input or output)");
  1488. }
  1489. else if (LV2_IS_PORT_MIDI_LL(portTypes))
  1490. {
  1491. if (LV2_IS_PORT_INPUT(portTypes))
  1492. {
  1493. uint32_t j = iEvIn++;
  1494. fDescriptor->connect_port(fHandle, i, &fEventsIn.data[j].midi);
  1495. if (fHandle2 != nullptr)
  1496. fDescriptor->connect_port(fHandle2, i, &fEventsIn.data[j].midi);
  1497. fEventsIn.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1498. fEventsIn.data[j].rindex = i;
  1499. if (evIns.count() == 1)
  1500. {
  1501. needsCtrlIn = true;
  1502. fEventsIn.ctrl = &fEventsIn.data[j];
  1503. fEventsIn.ctrlIndex = j;
  1504. }
  1505. else
  1506. {
  1507. fEventsIn.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1508. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1509. {
  1510. fEventsIn.ctrl = &fEventsIn.data[j];
  1511. fEventsIn.ctrlIndex = j;
  1512. }
  1513. }
  1514. }
  1515. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1516. {
  1517. uint32_t j = iEvOut++;
  1518. fDescriptor->connect_port(fHandle, i, &fEventsOut.data[j].midi);
  1519. if (fHandle2 != nullptr)
  1520. fDescriptor->connect_port(fHandle2, i, &fEventsOut.data[j].midi);
  1521. fEventsOut.data[j].type |= CARLA_EVENT_TYPE_MIDI;
  1522. fEventsOut.data[j].rindex = i;
  1523. if (evOuts.count() == 1)
  1524. {
  1525. needsCtrlOut = true;
  1526. fEventsOut.ctrl = &fEventsOut.data[j];
  1527. fEventsOut.ctrlIndex = j;
  1528. }
  1529. else
  1530. {
  1531. fEventsOut.data[j].port = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1532. if (LV2_IS_PORT_DESIGNATION_CONTROL(fRdfDescriptor->Ports[i].Designation))
  1533. {
  1534. fEventsOut.ctrl = &fEventsOut.data[j];
  1535. fEventsOut.ctrlIndex = j;
  1536. }
  1537. }
  1538. }
  1539. else
  1540. carla_stderr2("WARNING - Got a broken Port (MIDI, but not input or output)");
  1541. }
  1542. else if (LV2_IS_PORT_CONTROL(portTypes))
  1543. {
  1544. const LV2_Property portProps(fRdfDescriptor->Ports[i].Properties);
  1545. const LV2_Property portDesignation(fRdfDescriptor->Ports[i].Designation);
  1546. const LV2_RDF_PortPoints portPoints(fRdfDescriptor->Ports[i].Points);
  1547. uint32_t j = iCtrl++;
  1548. pData->param.data[j].hints = 0x0;
  1549. pData->param.data[j].index = static_cast<int32_t>(j);
  1550. pData->param.data[j].rindex = static_cast<int32_t>(i);
  1551. pData->param.data[j].midiCC = -1;
  1552. pData->param.data[j].midiChannel = 0;
  1553. pData->param.special[j] = PARAMETER_SPECIAL_NULL;
  1554. float min, max, def, step, stepSmall, stepLarge;
  1555. // min value
  1556. if (LV2_HAVE_MINIMUM_PORT_POINT(portPoints.Hints))
  1557. min = portPoints.Minimum;
  1558. else
  1559. min = 0.0f;
  1560. // max value
  1561. if (LV2_HAVE_MAXIMUM_PORT_POINT(portPoints.Hints))
  1562. max = portPoints.Maximum;
  1563. else
  1564. max = 1.0f;
  1565. if (min > max)
  1566. max = min;
  1567. // stupid hack for ir.lv2 (broken plugin)
  1568. if (std::strcmp(fRdfDescriptor->URI, "http://factorial.hu/plugins/lv2/ir") == 0 && std::strncmp(fRdfDescriptor->Ports[i].Name, "FileHash", 8) == 0)
  1569. {
  1570. min = 0.0f;
  1571. max = (float)0xffffff;
  1572. }
  1573. if (max - min == 0.0f)
  1574. {
  1575. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", fRdfDescriptor->Ports[i].Name);
  1576. max = min + 0.1f;
  1577. }
  1578. // default value
  1579. if (LV2_HAVE_DEFAULT_PORT_POINT(portPoints.Hints))
  1580. {
  1581. def = portPoints.Default;
  1582. }
  1583. else
  1584. {
  1585. // no default value
  1586. if (min < 0.0f && max > 0.0f)
  1587. def = 0.0f;
  1588. else
  1589. def = min;
  1590. }
  1591. if (def < min)
  1592. def = min;
  1593. else if (def > max)
  1594. def = max;
  1595. if (LV2_IS_PORT_SAMPLE_RATE(portProps))
  1596. {
  1597. min *= sampleRate;
  1598. max *= sampleRate;
  1599. def *= sampleRate;
  1600. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  1601. }
  1602. if (LV2_IS_PORT_TOGGLED(portProps))
  1603. {
  1604. step = max - min;
  1605. stepSmall = step;
  1606. stepLarge = step;
  1607. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  1608. }
  1609. else if (LV2_IS_PORT_INTEGER(portProps))
  1610. {
  1611. step = 1.0f;
  1612. stepSmall = 1.0f;
  1613. stepLarge = 10.0f;
  1614. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  1615. }
  1616. else
  1617. {
  1618. float range = max - min;
  1619. step = range/100.0f;
  1620. stepSmall = range/1000.0f;
  1621. stepLarge = range/10.0f;
  1622. }
  1623. if (LV2_IS_PORT_INPUT(portTypes))
  1624. {
  1625. pData->param.data[j].type = PARAMETER_INPUT;
  1626. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  1627. {
  1628. carla_stderr("Plugin has latency input port, this should not happen!");
  1629. }
  1630. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  1631. {
  1632. def = sampleRate;
  1633. step = 1.0f;
  1634. stepSmall = 1.0f;
  1635. stepLarge = 1.0f;
  1636. pData->param.special[j] = PARAMETER_SPECIAL_SAMPLE_RATE;
  1637. }
  1638. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  1639. {
  1640. pData->param.special[j] = PARAMETER_SPECIAL_LV2_FREEWHEEL;
  1641. }
  1642. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  1643. {
  1644. pData->param.special[j] = PARAMETER_SPECIAL_LV2_TIME;
  1645. }
  1646. else
  1647. {
  1648. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  1649. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1650. needsCtrlIn = true;
  1651. }
  1652. // MIDI CC value
  1653. const LV2_RDF_PortMidiMap& portMidiMap(fRdfDescriptor->Ports[i].MidiMap);
  1654. if (LV2_IS_PORT_MIDI_MAP_CC(portMidiMap.Type))
  1655. {
  1656. if (portMidiMap.Number < 0x5F && ! MIDI_IS_CONTROL_BANK_SELECT(portMidiMap.Number))
  1657. pData->param.data[j].midiCC = int16_t(portMidiMap.Number);
  1658. }
  1659. }
  1660. else if (LV2_IS_PORT_OUTPUT(portTypes))
  1661. {
  1662. pData->param.data[j].type = PARAMETER_OUTPUT;
  1663. if (LV2_IS_PORT_DESIGNATION_LATENCY(portDesignation))
  1664. {
  1665. min = 0.0f;
  1666. max = sampleRate;
  1667. def = 0.0f;
  1668. step = 1.0f;
  1669. stepSmall = 1.0f;
  1670. stepLarge = 1.0f;
  1671. pData->param.special[j] = PARAMETER_SPECIAL_LATENCY;
  1672. }
  1673. else if (LV2_IS_PORT_DESIGNATION_SAMPLE_RATE(portDesignation))
  1674. {
  1675. def = sampleRate;
  1676. step = 1.0f;
  1677. stepSmall = 1.0f;
  1678. stepLarge = 1.0f;
  1679. pData->param.special[j] = PARAMETER_SPECIAL_SAMPLE_RATE;
  1680. }
  1681. else if (LV2_IS_PORT_DESIGNATION_FREEWHEELING(portDesignation))
  1682. {
  1683. carla_stderr("Plugin has freewheeling output port, this should not happen!");
  1684. }
  1685. else if (LV2_IS_PORT_DESIGNATION_TIME(portDesignation))
  1686. {
  1687. pData->param.special[j] = PARAMETER_SPECIAL_LV2_TIME;
  1688. }
  1689. else
  1690. {
  1691. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  1692. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  1693. needsCtrlOut = true;
  1694. }
  1695. }
  1696. else
  1697. {
  1698. pData->param.data[j].type = PARAMETER_UNKNOWN;
  1699. carla_stderr2("WARNING - Got a broken Port (Control, but not input or output)");
  1700. }
  1701. // extra parameter hints
  1702. if (LV2_IS_PORT_LOGARITHMIC(portProps))
  1703. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  1704. if (LV2_IS_PORT_TRIGGER(portProps))
  1705. pData->param.data[j].hints |= PARAMETER_IS_TRIGGER;
  1706. if (LV2_IS_PORT_STRICT_BOUNDS(portProps))
  1707. pData->param.data[j].hints |= PARAMETER_IS_STRICT_BOUNDS;
  1708. if (LV2_IS_PORT_ENUMERATION(portProps))
  1709. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  1710. // check if parameter is not enabled or automable
  1711. if (LV2_IS_PORT_NOT_ON_GUI(portProps))
  1712. pData->param.data[j].hints &= ~(PARAMETER_IS_ENABLED|PARAMETER_IS_AUTOMABLE);
  1713. else if (LV2_IS_PORT_CAUSES_ARTIFACTS(portProps) || LV2_IS_PORT_EXPENSIVE(portProps) || LV2_IS_PORT_NOT_AUTOMATIC(portProps))
  1714. pData->param.data[j].hints &= ~PARAMETER_IS_AUTOMABLE;
  1715. pData->param.ranges[j].min = min;
  1716. pData->param.ranges[j].max = max;
  1717. pData->param.ranges[j].def = def;
  1718. pData->param.ranges[j].step = step;
  1719. pData->param.ranges[j].stepSmall = stepSmall;
  1720. pData->param.ranges[j].stepLarge = stepLarge;
  1721. // Start parameters in their default values (except freewheel, which is off by default)
  1722. if (pData->param.data[j].type == PARAMETER_INPUT && pData->param.special[j] == PARAMETER_SPECIAL_LV2_FREEWHEEL)
  1723. fParamBuffers[j] = min;
  1724. else
  1725. fParamBuffers[j] = def;
  1726. fDescriptor->connect_port(fHandle, i, &fParamBuffers[j]);
  1727. if (fHandle2 != nullptr)
  1728. fDescriptor->connect_port(fHandle2, i, &fParamBuffers[j]);
  1729. }
  1730. else
  1731. {
  1732. // Port Type not supported, but it's optional anyway
  1733. fDescriptor->connect_port(fHandle, i, nullptr);
  1734. if (fHandle2 != nullptr)
  1735. fDescriptor->connect_port(fHandle2, i, nullptr);
  1736. }
  1737. }
  1738. if (needsCtrlIn)
  1739. {
  1740. portName.clear();
  1741. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1742. {
  1743. portName = pData->name;
  1744. portName += ":";
  1745. }
  1746. portName += "events-in";
  1747. portName.truncate(portNameSize);
  1748. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  1749. }
  1750. if (needsCtrlOut)
  1751. {
  1752. portName.clear();
  1753. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  1754. {
  1755. portName = pData->name;
  1756. portName += ":";
  1757. }
  1758. portName += "events-out";
  1759. portName.truncate(portNameSize);
  1760. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  1761. }
  1762. if (fExt.worker != nullptr || (fUi.type != UI::TYPE_NULL && fEventsIn.count > 0 && (fEventsIn.data[0].type & CARLA_EVENT_DATA_ATOM) != 0))
  1763. fAtomQueueIn.createBuffer(eventBufferSize);
  1764. if (fExt.worker != nullptr || (fUi.type != UI::TYPE_NULL && fEventsOut.count > 0 && (fEventsOut.data[0].type & CARLA_EVENT_DATA_ATOM) != 0))
  1765. fAtomQueueOut.createBuffer(eventBufferSize);
  1766. if (fEventsIn.ctrl != nullptr && fEventsIn.ctrl->port == nullptr)
  1767. fEventsIn.ctrl->port = pData->event.portIn;
  1768. if (fEventsOut.ctrl != nullptr && fEventsOut.ctrl->port == nullptr)
  1769. fEventsOut.ctrl->port = pData->event.portOut;
  1770. if (forcedStereoIn || forcedStereoOut)
  1771. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1772. else
  1773. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  1774. // plugin hints
  1775. pData->hints = 0x0;
  1776. if (isRealtimeSafe())
  1777. pData->hints |= PLUGIN_IS_RTSAFE;
  1778. if (fUi.type != UI::TYPE_NULL)
  1779. {
  1780. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  1781. if (fUi.type == UI::TYPE_EMBED)
  1782. pData->hints |= PLUGIN_NEEDS_SINGLE_THREAD;
  1783. }
  1784. if (LV2_IS_GENERATOR(fRdfDescriptor->Type[0], fRdfDescriptor->Type[1]))
  1785. pData->hints |= PLUGIN_IS_SYNTH;
  1786. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  1787. pData->hints |= PLUGIN_CAN_DRYWET;
  1788. if (aOuts > 0)
  1789. pData->hints |= PLUGIN_CAN_VOLUME;
  1790. if (aOuts >= 2 && aOuts % 2 == 0)
  1791. pData->hints |= PLUGIN_CAN_BALANCE;
  1792. // extra plugin hints
  1793. pData->extraHints = 0x0;
  1794. if (fExt.state != nullptr || fExt.worker != nullptr)
  1795. {
  1796. if ((aIns == 0 || aIns == 2) && (aOuts == 0 || aOuts == 2) && evIns.count() <= 1 && evOuts.count() <= 1)
  1797. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  1798. }
  1799. else
  1800. {
  1801. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && evIns.count() <= 1 && evOuts.count() <= 1)
  1802. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  1803. }
  1804. bufferSizeChanged(pData->engine->getBufferSize());
  1805. reloadPrograms(true);
  1806. // check latency
  1807. // TODO
  1808. evIns.clear();
  1809. evOuts.clear();
  1810. if (pData->active)
  1811. activate();
  1812. carla_debug("Lv2Plugin::reload() - end");
  1813. }
  1814. void reloadPrograms(const bool doInit) override
  1815. {
  1816. carla_debug("DssiPlugin::reloadPrograms(%s)", bool2str(doInit));
  1817. const uint32_t oldCount = pData->midiprog.count;
  1818. const int32_t current = pData->midiprog.current;
  1819. // special LV2 programs handling
  1820. if (doInit)
  1821. {
  1822. pData->prog.clear();
  1823. const uint32_t presetCount(fRdfDescriptor->PresetCount);
  1824. if (presetCount > 0)
  1825. {
  1826. pData->prog.createNew(presetCount);
  1827. for (uint32_t i=0; i < presetCount; ++i)
  1828. pData->prog.names[i] = carla_strdup(fRdfDescriptor->Presets[i].Label);
  1829. }
  1830. }
  1831. // Delete old programs
  1832. pData->midiprog.clear();
  1833. // Query new programs
  1834. uint32_t newCount = 0;
  1835. if (fExt.programs != nullptr && fExt.programs->get_program != nullptr && fExt.programs->select_program != nullptr)
  1836. {
  1837. for (; fExt.programs->get_program(fHandle, newCount);)
  1838. ++newCount;
  1839. }
  1840. if (newCount > 0)
  1841. {
  1842. pData->midiprog.createNew(newCount);
  1843. // Update data
  1844. for (uint32_t i=0; i < newCount; ++i)
  1845. {
  1846. const LV2_Program_Descriptor* const pdesc(fExt.programs->get_program(fHandle, i));
  1847. CARLA_SAFE_ASSERT_CONTINUE(pdesc != nullptr);
  1848. CARLA_SAFE_ASSERT(pdesc->name != nullptr);
  1849. pData->midiprog.data[i].bank = pdesc->bank;
  1850. pData->midiprog.data[i].program = pdesc->program;
  1851. pData->midiprog.data[i].name = carla_strdup(pdesc->name);
  1852. }
  1853. }
  1854. #ifndef BUILD_BRIDGE
  1855. // Update OSC Names
  1856. if (pData->engine->isOscControlRegistered())
  1857. {
  1858. pData->engine->oscSend_control_set_midi_program_count(pData->id, newCount);
  1859. for (uint32_t i=0; i < newCount; ++i)
  1860. pData->engine->oscSend_control_set_midi_program_data(pData->id, i, pData->midiprog.data[i].bank, pData->midiprog.data[i].program, pData->midiprog.data[i].name);
  1861. }
  1862. #endif
  1863. if (doInit)
  1864. {
  1865. if (newCount > 0)
  1866. {
  1867. setMidiProgram(0, false, false, false);
  1868. }
  1869. else
  1870. {
  1871. // load default state
  1872. if (const LilvState* state = Lv2WorldClass::getInstance().getState(fDescriptor->URI, (const LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data))
  1873. {
  1874. lilv_state_restore(state, fExt.state, fHandle, carla_lilv_set_port_value, this, 0, fFeatures);
  1875. if (fHandle2 != nullptr)
  1876. lilv_state_restore(state, fExt.state, fHandle2, carla_lilv_set_port_value, this, 0, fFeatures);
  1877. }
  1878. }
  1879. }
  1880. else
  1881. {
  1882. // Check if current program is invalid
  1883. bool programChanged = false;
  1884. if (newCount == oldCount+1)
  1885. {
  1886. // one midi program added, probably created by user
  1887. pData->midiprog.current = static_cast<int32_t>(oldCount);
  1888. programChanged = true;
  1889. }
  1890. else if (current < 0 && newCount > 0)
  1891. {
  1892. // programs exist now, but not before
  1893. pData->midiprog.current = 0;
  1894. programChanged = true;
  1895. }
  1896. else if (current >= 0 && newCount == 0)
  1897. {
  1898. // programs existed before, but not anymore
  1899. pData->midiprog.current = -1;
  1900. programChanged = true;
  1901. }
  1902. else if (current >= static_cast<int32_t>(newCount))
  1903. {
  1904. // current midi program > count
  1905. pData->midiprog.current = 0;
  1906. programChanged = true;
  1907. }
  1908. else
  1909. {
  1910. // no change
  1911. pData->midiprog.current = current;
  1912. }
  1913. if (programChanged)
  1914. setMidiProgram(pData->midiprog.current, true, true, true);
  1915. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  1916. }
  1917. }
  1918. // -------------------------------------------------------------------
  1919. // Plugin processing
  1920. void activate() noexcept override
  1921. {
  1922. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1923. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1924. if (fDescriptor->activate != nullptr)
  1925. {
  1926. try {
  1927. fDescriptor->activate(fHandle);
  1928. } catch(...) {}
  1929. if (fHandle2 != nullptr)
  1930. {
  1931. try {
  1932. fDescriptor->activate(fHandle2);
  1933. } catch(...) {}
  1934. }
  1935. }
  1936. fFirstActive = true;
  1937. }
  1938. void deactivate() noexcept override
  1939. {
  1940. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1941. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1942. if (fDescriptor->deactivate != nullptr)
  1943. {
  1944. try {
  1945. fDescriptor->deactivate(fHandle);
  1946. } catch(...) {}
  1947. if (fHandle2 != nullptr)
  1948. {
  1949. try {
  1950. fDescriptor->deactivate(fHandle2);
  1951. } catch(...) {}
  1952. }
  1953. }
  1954. }
  1955. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  1956. {
  1957. // --------------------------------------------------------------------------------------------------------
  1958. // Check if active
  1959. if (! pData->active)
  1960. {
  1961. // disable any output sound
  1962. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1963. FLOAT_CLEAR(outBuffer[i], frames);
  1964. return;
  1965. }
  1966. // --------------------------------------------------------------------------------------------------------
  1967. // Event itenerators from different APIs (input)
  1968. LV2_Atom_Buffer_Iterator evInAtomIters[fEventsIn.count];
  1969. LV2_Event_Iterator evInEventIters[fEventsIn.count];
  1970. LV2_MIDIState evInMidiStates[fEventsIn.count];
  1971. for (uint32_t i=0; i < fEventsIn.count; ++i)
  1972. {
  1973. if (fEventsIn.data[i].type & CARLA_EVENT_DATA_ATOM)
  1974. {
  1975. lv2_atom_buffer_reset(fEventsIn.data[i].atom, true);
  1976. lv2_atom_buffer_begin(&evInAtomIters[i], fEventsIn.data[i].atom);
  1977. }
  1978. else if (fEventsIn.data[i].type & CARLA_EVENT_DATA_EVENT)
  1979. {
  1980. lv2_event_buffer_reset(fEventsIn.data[i].event, LV2_EVENT_AUDIO_STAMP, fEventsIn.data[i].event->data);
  1981. lv2_event_begin(&evInEventIters[i], fEventsIn.data[i].event);
  1982. }
  1983. else if (fEventsIn.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  1984. {
  1985. fEventsIn.data[i].midi.event_count = 0;
  1986. fEventsIn.data[i].midi.size = 0;
  1987. evInMidiStates[i].midi = &fEventsIn.data[i].midi;
  1988. evInMidiStates[i].frame_count = frames;
  1989. evInMidiStates[i].position = 0;
  1990. }
  1991. }
  1992. for (uint32_t i=0; i < fEventsOut.count; ++i)
  1993. {
  1994. if (fEventsOut.data[i].type & CARLA_EVENT_DATA_ATOM)
  1995. {
  1996. lv2_atom_buffer_reset(fEventsOut.data[i].atom, false);
  1997. }
  1998. else if (fEventsOut.data[i].type & CARLA_EVENT_DATA_EVENT)
  1999. {
  2000. lv2_event_buffer_reset(fEventsOut.data[i].event, LV2_EVENT_AUDIO_STAMP, fEventsOut.data[i].event->data);
  2001. }
  2002. else if (fEventsOut.data[i].type & CARLA_EVENT_DATA_MIDI_LL)
  2003. {
  2004. // not needed
  2005. }
  2006. }
  2007. CARLA_PROCESS_CONTINUE_CHECK;
  2008. // --------------------------------------------------------------------------------------------------------
  2009. // Check if needs reset
  2010. if (pData->needsReset)
  2011. {
  2012. uint8_t midiData[3] = { 0 };
  2013. if (fEventsIn.ctrl != nullptr && (fEventsIn.ctrl->type & CARLA_EVENT_TYPE_MIDI) != 0)
  2014. {
  2015. const uint32_t j = fEventsIn.ctrlIndex;
  2016. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2017. {
  2018. for (uint8_t i=0; i < MAX_MIDI_CHANNELS; ++i)
  2019. {
  2020. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + i);
  2021. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  2022. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2023. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2024. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2025. lv2_event_write(&evInEventIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2026. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2027. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiData);
  2028. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + i);
  2029. midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  2030. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2031. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2032. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2033. lv2_event_write(&evInEventIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2034. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2035. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiData);
  2036. }
  2037. }
  2038. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  2039. {
  2040. for (uint8_t k=0; k < MAX_MIDI_NOTE; ++k)
  2041. {
  2042. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_NOTE_OFF + pData->ctrlChannel);
  2043. midiData[1] = k;
  2044. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2045. lv2_atom_buffer_write(&evInAtomIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2046. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2047. lv2_event_write(&evInEventIters[k], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2048. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2049. lv2midi_put_event(&evInMidiStates[k], 0.0, 3, midiData);
  2050. }
  2051. }
  2052. }
  2053. if (pData->latency > 0)
  2054. {
  2055. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2056. FLOAT_CLEAR(pData->latencyBuffers[i], pData->latency);
  2057. }
  2058. pData->needsReset = false;
  2059. }
  2060. // --------------------------------------------------------------------------------------------------------
  2061. // TimeInfo
  2062. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  2063. if (fFirstActive || fLastTimeInfo != timeInfo)
  2064. {
  2065. bool doPostRt;
  2066. int32_t rindex;
  2067. // update input ports
  2068. for (uint32_t k=0; k < pData->param.count; ++k)
  2069. {
  2070. if (pData->param.data[k].type != PARAMETER_INPUT)
  2071. continue;
  2072. if (pData->param.special[k] != PARAMETER_SPECIAL_LV2_TIME)
  2073. continue;
  2074. doPostRt = false;
  2075. rindex = pData->param.data[k].rindex;
  2076. CARLA_SAFE_ASSERT_CONTINUE(rindex >= 0 && rindex < static_cast<int32_t>(fRdfDescriptor->PortCount));
  2077. switch (fRdfDescriptor->Ports[rindex].Designation)
  2078. {
  2079. // Non-BBT
  2080. case LV2_PORT_DESIGNATION_TIME_SPEED:
  2081. if (fLastTimeInfo.playing != timeInfo.playing)
  2082. {
  2083. fParamBuffers[k] = timeInfo.playing ? 1.0f : 0.0f;
  2084. doPostRt = true;
  2085. }
  2086. break;
  2087. case LV2_PORT_DESIGNATION_TIME_FRAME:
  2088. if (fLastTimeInfo.frame != timeInfo.frame)
  2089. {
  2090. fParamBuffers[k] = static_cast<float>(timeInfo.frame);
  2091. doPostRt = true;
  2092. }
  2093. break;
  2094. case LV2_PORT_DESIGNATION_TIME_FRAMES_PER_SECOND:
  2095. break;
  2096. // BBT
  2097. case LV2_PORT_DESIGNATION_TIME_BAR:
  2098. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.bar != timeInfo.bbt.bar)
  2099. {
  2100. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.bar - 1);
  2101. doPostRt = true;
  2102. }
  2103. break;
  2104. case LV2_PORT_DESIGNATION_TIME_BAR_BEAT:
  2105. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && (fLastTimeInfo.bbt.tick != timeInfo.bbt.tick ||
  2106. fLastTimeInfo.bbt.ticksPerBeat != timeInfo.bbt.ticksPerBeat))
  2107. {
  2108. fParamBuffers[k] = static_cast<float>(static_cast<double>(timeInfo.bbt.beat) - 1.0 + (static_cast<double>(timeInfo.bbt.tick) / timeInfo.bbt.ticksPerBeat));
  2109. doPostRt = true;
  2110. }
  2111. break;
  2112. case LV2_PORT_DESIGNATION_TIME_BEAT:
  2113. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beat != timeInfo.bbt.beat)
  2114. {
  2115. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.beat - 1);
  2116. doPostRt = true;
  2117. }
  2118. break;
  2119. case LV2_PORT_DESIGNATION_TIME_BEAT_UNIT:
  2120. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beatType != timeInfo.bbt.beatType)
  2121. {
  2122. fParamBuffers[k] = timeInfo.bbt.beatType;
  2123. doPostRt = true;
  2124. }
  2125. break;
  2126. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_BAR:
  2127. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beatsPerBar != timeInfo.bbt.beatsPerBar)
  2128. {
  2129. fParamBuffers[k] = timeInfo.bbt.beatsPerBar;
  2130. doPostRt = true;
  2131. }
  2132. break;
  2133. case LV2_PORT_DESIGNATION_TIME_BEATS_PER_MINUTE:
  2134. if ((timeInfo.valid & EngineTimeInfo::kValidBBT) != 0 && fLastTimeInfo.bbt.beatsPerMinute != timeInfo.bbt.beatsPerMinute)
  2135. {
  2136. fParamBuffers[k] = static_cast<float>(timeInfo.bbt.beatsPerMinute);
  2137. doPostRt = true;
  2138. }
  2139. break;
  2140. }
  2141. if (doPostRt)
  2142. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  2143. }
  2144. for (uint32_t i=0; i < fEventsIn.count; ++i)
  2145. {
  2146. if ((fEventsIn.data[i].type & CARLA_EVENT_DATA_ATOM) == 0 || (fEventsIn.data[i].type & CARLA_EVENT_TYPE_TIME) == 0)
  2147. continue;
  2148. uint8_t timeInfoBuf[256];
  2149. lv2_atom_forge_set_buffer(&fAtomForge, timeInfoBuf, sizeof(timeInfoBuf));
  2150. LV2_Atom_Forge_Frame forgeFrame;
  2151. lv2_atom_forge_object(&fAtomForge, &forgeFrame, CARLA_URI_MAP_ID_NULL, CARLA_URI_MAP_ID_TIME_POSITION);
  2152. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_SPEED);
  2153. lv2_atom_forge_float(&fAtomForge, timeInfo.playing ? 1.0f : 0.0f);
  2154. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_FRAME);
  2155. lv2_atom_forge_long(&fAtomForge, static_cast<int64_t>(timeInfo.frame));
  2156. if (timeInfo.valid & EngineTimeInfo::kValidBBT)
  2157. {
  2158. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BAR);
  2159. lv2_atom_forge_long(&fAtomForge, timeInfo.bbt.bar - 1);
  2160. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BAR_BEAT);
  2161. lv2_atom_forge_float(&fAtomForge, static_cast<float>(static_cast<double>(timeInfo.bbt.beat) - 1.0 + (static_cast<double>(timeInfo.bbt.tick) / timeInfo.bbt.ticksPerBeat)));
  2162. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEAT);
  2163. lv2_atom_forge_double(&fAtomForge, timeInfo.bbt.beat -1);
  2164. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEAT_UNIT);
  2165. lv2_atom_forge_int(&fAtomForge, static_cast<int32_t>(timeInfo.bbt.beatType));
  2166. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR);
  2167. lv2_atom_forge_float(&fAtomForge, timeInfo.bbt.beatsPerBar);
  2168. lv2_atom_forge_key(&fAtomForge, CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE);
  2169. lv2_atom_forge_float(&fAtomForge, static_cast<float>(timeInfo.bbt.beatsPerMinute));
  2170. }
  2171. lv2_atom_forge_pop(&fAtomForge, &forgeFrame);
  2172. LV2_Atom* const atom((LV2_Atom*)timeInfoBuf);
  2173. CARLA_SAFE_ASSERT_BREAK(atom->size < 256);
  2174. // send only deprecated blank object for now
  2175. lv2_atom_buffer_write(&evInAtomIters[i], 0, 0, CARLA_URI_MAP_ID_ATOM_BLANK, atom->size, LV2_ATOM_BODY_CONST(atom));
  2176. // for atom:object
  2177. //lv2_atom_buffer_write(&evInAtomIters[i], 0, 0, atom->type, atom->size, LV2_ATOM_BODY_CONST(atom));
  2178. }
  2179. pData->postRtEvents.trySplice();
  2180. carla_copyStruct<EngineTimeInfo>(fLastTimeInfo, timeInfo);
  2181. CARLA_PROCESS_CONTINUE_CHECK;
  2182. }
  2183. // --------------------------------------------------------------------------------------------------------
  2184. // CV ports
  2185. float* cvInBuf[fCvIn.count /*> 0 ? fCvIn.count : 1*/];
  2186. float* cvOutBuf[fCvOut.count /*> 0 ? fCvOut.count : 1*/];
  2187. for (uint32_t i=0; i < fCvIn.count; ++i)
  2188. {
  2189. CARLA_SAFE_ASSERT_CONTINUE(fCvIn.ports[i].port != nullptr);
  2190. cvInBuf[i] = fCvIn.ports[i].port->getBuffer();
  2191. }
  2192. for (uint32_t i=0; i < fCvOut.count; ++i)
  2193. {
  2194. CARLA_SAFE_ASSERT_CONTINUE(fCvOut.ports[i].port != nullptr);
  2195. cvOutBuf[i] = fCvOut.ports[i].port->getBuffer();
  2196. }
  2197. // --------------------------------------------------------------------------------------------------------
  2198. // Event Input and Processing
  2199. if (fEventsIn.ctrl != nullptr)
  2200. {
  2201. // ----------------------------------------------------------------------------------------------------
  2202. // Message Input
  2203. if (fAtomQueueIn.tryLock())
  2204. {
  2205. if (! fAtomQueueIn.isEmpty())
  2206. {
  2207. const LV2_Atom* atom;
  2208. uint32_t j, portIndex;
  2209. for (; fAtomQueueIn.get(&atom, &portIndex);)
  2210. {
  2211. j = (portIndex < fEventsIn.count) ? portIndex : fEventsIn.ctrlIndex;
  2212. if (atom->type == CARLA_URI_MAP_ID_CARLA_ATOM_WORKER)
  2213. {
  2214. CARLA_SAFE_ASSERT_CONTINUE(fExt.worker != nullptr && fExt.worker->work_response != nullptr);
  2215. fExt.worker->work_response(fHandle, atom->size, LV2_ATOM_BODY_CONST(atom));
  2216. }
  2217. else if (! lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, atom->type, atom->size, LV2_ATOM_BODY_CONST(atom)))
  2218. {
  2219. carla_stdout("Event input buffer full, at least 1 message lost");
  2220. continue;
  2221. }
  2222. }
  2223. }
  2224. fAtomQueueIn.unlock();
  2225. }
  2226. // ----------------------------------------------------------------------------------------------------
  2227. // MIDI Input (External)
  2228. if (pData->extNotes.mutex.tryLock())
  2229. {
  2230. if ((fEventsIn.ctrl->type & CARLA_EVENT_TYPE_MIDI) == 0)
  2231. {
  2232. // does not handle MIDI
  2233. pData->extNotes.data.clear();
  2234. }
  2235. else
  2236. {
  2237. const uint32_t j = fEventsIn.ctrlIndex;
  2238. for (; ! pData->extNotes.data.isEmpty();)
  2239. {
  2240. const ExternalMidiNote& note(pData->extNotes.data.getFirst(true));
  2241. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  2242. uint8_t midiEvent[3];
  2243. midiEvent[0] = static_cast<uint8_t>((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  2244. midiEvent[1] = note.note;
  2245. midiEvent[2] = note.velo;
  2246. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2247. lv2_atom_buffer_write(&evInAtomIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiEvent);
  2248. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2249. lv2_event_write(&evInEventIters[j], 0, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiEvent);
  2250. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2251. lv2midi_put_event(&evInMidiStates[j], 0.0, 3, midiEvent);
  2252. }
  2253. }
  2254. pData->extNotes.mutex.unlock();
  2255. } // End of MIDI Input (External)
  2256. // ----------------------------------------------------------------------------------------------------
  2257. // Event Input (System)
  2258. bool allNotesOffSent = false;
  2259. bool isSampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  2260. uint32_t numEvents = (fEventsIn.ctrl->port != nullptr) ? fEventsIn.ctrl->port->getEventCount() : 0;
  2261. uint32_t startTime = 0;
  2262. uint32_t timeOffset = 0;
  2263. uint32_t nextBankId;
  2264. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  2265. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  2266. else
  2267. nextBankId = 0;
  2268. for (uint32_t i=0; i < numEvents; ++i)
  2269. {
  2270. const EngineEvent& event(fEventsIn.ctrl->port->getEvent(i));
  2271. if (event.time >= frames)
  2272. continue;
  2273. CARLA_ASSERT_INT2(event.time >= timeOffset, event.time, timeOffset);
  2274. if (isSampleAccurate && event.time > timeOffset)
  2275. {
  2276. if (processSingle(inBuffer, outBuffer, cvInBuf, cvOutBuf, event.time - timeOffset, timeOffset))
  2277. {
  2278. startTime = 0;
  2279. timeOffset = event.time;
  2280. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  2281. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  2282. else
  2283. nextBankId = 0;
  2284. // reset iters
  2285. const uint32_t j = fEventsIn.ctrlIndex;
  2286. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2287. {
  2288. lv2_atom_buffer_reset(fEventsIn.data[j].atom, true);
  2289. lv2_atom_buffer_begin(&evInAtomIters[j], fEventsIn.data[j].atom);
  2290. }
  2291. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2292. {
  2293. lv2_event_buffer_reset(fEventsIn.data[j].event, LV2_EVENT_AUDIO_STAMP, fEventsIn.data[j].event->data);
  2294. lv2_event_begin(&evInEventIters[j], fEventsIn.data[j].event);
  2295. }
  2296. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2297. {
  2298. fEventsIn.data[j].midi.event_count = 0;
  2299. fEventsIn.data[j].midi.size = 0;
  2300. evInMidiStates[j].position = event.time;
  2301. }
  2302. }
  2303. else
  2304. startTime += timeOffset;
  2305. }
  2306. switch (event.type)
  2307. {
  2308. case kEngineEventTypeNull:
  2309. break;
  2310. case kEngineEventTypeControl: {
  2311. const EngineControlEvent& ctrlEvent(event.ctrl);
  2312. switch (ctrlEvent.type)
  2313. {
  2314. case kEngineControlEventTypeNull:
  2315. break;
  2316. case kEngineControlEventTypeParameter: {
  2317. #ifndef BUILD_BRIDGE
  2318. // Control backend stuff
  2319. if (event.channel == pData->ctrlChannel)
  2320. {
  2321. float value;
  2322. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) != 0)
  2323. {
  2324. value = ctrlEvent.value;
  2325. setDryWet(value, false, false);
  2326. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  2327. break;
  2328. }
  2329. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) != 0)
  2330. {
  2331. value = ctrlEvent.value*127.0f/100.0f;
  2332. setVolume(value, false, false);
  2333. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  2334. break;
  2335. }
  2336. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) != 0)
  2337. {
  2338. float left, right;
  2339. value = ctrlEvent.value/0.5f - 1.0f;
  2340. if (value < 0.0f)
  2341. {
  2342. left = -1.0f;
  2343. right = (value*2.0f)+1.0f;
  2344. }
  2345. else if (value > 0.0f)
  2346. {
  2347. left = (value*2.0f)-1.0f;
  2348. right = 1.0f;
  2349. }
  2350. else
  2351. {
  2352. left = -1.0f;
  2353. right = 1.0f;
  2354. }
  2355. setBalanceLeft(left, false, false);
  2356. setBalanceRight(right, false, false);
  2357. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  2358. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  2359. break;
  2360. }
  2361. }
  2362. #endif
  2363. // Control plugin parameters
  2364. uint32_t k;
  2365. for (k=0; k < pData->param.count; ++k)
  2366. {
  2367. if (pData->param.data[k].midiChannel != event.channel)
  2368. continue;
  2369. if (pData->param.data[k].midiCC != ctrlEvent.param)
  2370. continue;
  2371. if (pData->param.data[k].type != PARAMETER_INPUT)
  2372. continue;
  2373. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  2374. continue;
  2375. float value;
  2376. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  2377. {
  2378. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  2379. }
  2380. else
  2381. {
  2382. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  2383. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  2384. value = std::rint(value);
  2385. }
  2386. setParameterValue(k, value, false, false, false);
  2387. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  2388. break;
  2389. }
  2390. // check if event is already handled
  2391. if (k != pData->param.count)
  2392. break;
  2393. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  2394. {
  2395. uint8_t midiData[3];
  2396. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + i);
  2397. midiData[1] = static_cast<uint8_t>(ctrlEvent.param);
  2398. midiData[2] = uint8_t(ctrlEvent.value*127.0f);
  2399. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2400. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2401. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2402. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2403. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2404. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2405. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  2406. }
  2407. break;
  2408. } // case kEngineControlEventTypeParameter
  2409. case kEngineControlEventTypeMidiBank:
  2410. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  2411. nextBankId = ctrlEvent.param;
  2412. break;
  2413. case kEngineControlEventTypeMidiProgram:
  2414. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  2415. {
  2416. const uint32_t nextProgramId = ctrlEvent.param;
  2417. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  2418. {
  2419. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  2420. {
  2421. const int32_t index(static_cast<int32_t>(k));
  2422. setMidiProgram(index, false, false, false);
  2423. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, index, 0, 0.0f);
  2424. break;
  2425. }
  2426. }
  2427. }
  2428. break;
  2429. case kEngineControlEventTypeAllSoundOff:
  2430. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2431. {
  2432. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2433. uint8_t midiData[3];
  2434. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + i);
  2435. midiData[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  2436. midiData[2] = 0;
  2437. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2438. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2439. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2440. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2441. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2442. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  2443. }
  2444. break;
  2445. case kEngineControlEventTypeAllNotesOff:
  2446. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  2447. {
  2448. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  2449. {
  2450. allNotesOffSent = true;
  2451. sendMidiAllNotesOffToCallback();
  2452. }
  2453. const uint32_t mtime(isSampleAccurate ? startTime : event.time);
  2454. uint8_t midiData[3];
  2455. midiData[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + i);
  2456. midiData[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  2457. midiData[2] = 0;
  2458. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2459. lv2_atom_buffer_write(&evInAtomIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2460. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2461. lv2_event_write(&evInEventIters[fEventsIn.ctrlIndex], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, 3, midiData);
  2462. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2463. lv2midi_put_event(&evInMidiStates[fEventsIn.ctrlIndex], mtime, 3, midiData);
  2464. }
  2465. break;
  2466. } // switch (ctrlEvent.type)
  2467. break;
  2468. } // case kEngineEventTypeControl
  2469. case kEngineEventTypeMidi:
  2470. {
  2471. const EngineMidiEvent& midiEvent(event.midi);
  2472. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  2473. uint8_t channel = event.channel;
  2474. uint32_t mtime = isSampleAccurate ? startTime : event.time;
  2475. // Fix bad note-off (per LV2 spec)
  2476. if (MIDI_IS_STATUS_NOTE_ON(status) && midiEvent.data[2] == 0)
  2477. status = MIDI_STATUS_NOTE_OFF;
  2478. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  2479. continue;
  2480. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  2481. continue;
  2482. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  2483. continue;
  2484. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  2485. continue;
  2486. const uint32_t j = fEventsIn.ctrlIndex;
  2487. if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2488. lv2_atom_buffer_write(&evInAtomIters[j], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, midiEvent.size, midiEvent.data);
  2489. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_EVENT)
  2490. lv2_event_write(&evInEventIters[j], mtime, 0, CARLA_URI_MAP_ID_MIDI_EVENT, midiEvent.size, midiEvent.data);
  2491. else if (fEventsIn.ctrl->type & CARLA_EVENT_DATA_MIDI_LL)
  2492. lv2midi_put_event(&evInMidiStates[j], mtime, midiEvent.size, midiEvent.data);
  2493. if (status == MIDI_STATUS_NOTE_ON)
  2494. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  2495. else if (status == MIDI_STATUS_NOTE_OFF)
  2496. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  2497. break;
  2498. } // case kEngineEventTypeMidi
  2499. } // switch (event.type)
  2500. }
  2501. pData->postRtEvents.trySplice();
  2502. if (frames > timeOffset)
  2503. processSingle(inBuffer, outBuffer, cvInBuf, cvOutBuf, frames - timeOffset, timeOffset);
  2504. } // End of Event Input and Processing
  2505. // --------------------------------------------------------------------------------------------------------
  2506. // Plugin processing (no events)
  2507. else
  2508. {
  2509. processSingle(inBuffer, outBuffer, cvInBuf, cvOutBuf, frames, 0);
  2510. } // End of Plugin processing (no events)
  2511. CARLA_PROCESS_CONTINUE_CHECK;
  2512. // --------------------------------------------------------------------------------------------------------
  2513. // MIDI Output
  2514. if (fEventsOut.ctrl != nullptr)
  2515. {
  2516. if (fEventsOut.ctrl->type & CARLA_EVENT_DATA_ATOM)
  2517. {
  2518. const LV2_Atom_Event* ev;
  2519. LV2_Atom_Buffer_Iterator iter;
  2520. uint8_t* data;
  2521. lv2_atom_buffer_begin(&iter, fEventsOut.ctrl->atom);
  2522. for (;;)
  2523. {
  2524. data = nullptr;
  2525. ev = lv2_atom_buffer_get(&iter, &data);
  2526. if (ev == nullptr || ev->body.size == 0 || data == nullptr)
  2527. break;
  2528. if (ev->body.type == CARLA_URI_MAP_ID_MIDI_EVENT)
  2529. {
  2530. if (fEventsOut.ctrl->port != nullptr)
  2531. {
  2532. CARLA_SAFE_ASSERT_CONTINUE(ev->time.frames >= 0);
  2533. CARLA_SAFE_ASSERT_CONTINUE(ev->body.size < 0xFF);
  2534. fEventsOut.ctrl->port->writeMidiEvent(static_cast<uint32_t>(ev->time.frames), static_cast<uint8_t>(ev->body.size), data);
  2535. }
  2536. }
  2537. else //if (ev->body.type == CARLA_URI_MAP_ID_ATOM_BLANK)
  2538. {
  2539. //carla_stdout("Got out event, %s", carla_lv2_urid_unmap(this, ev->body.type));
  2540. fAtomQueueOut.put(&ev->body, fEventsOut.ctrl->rindex);
  2541. }
  2542. lv2_atom_buffer_increment(&iter);
  2543. }
  2544. }
  2545. else if ((fEventsOut.ctrl->type & CARLA_EVENT_DATA_EVENT) != 0 && fEventsOut.ctrl->port != nullptr)
  2546. {
  2547. const LV2_Event* ev;
  2548. LV2_Event_Iterator iter;
  2549. uint8_t* data;
  2550. lv2_event_begin(&iter, fEventsOut.ctrl->event);
  2551. for (;;)
  2552. {
  2553. data = nullptr;
  2554. ev = lv2_event_get(&iter, &data);
  2555. if (ev == nullptr || data == nullptr)
  2556. break;
  2557. if (ev->type == CARLA_URI_MAP_ID_MIDI_EVENT)
  2558. {
  2559. CARLA_SAFE_ASSERT_CONTINUE(ev->size < 0xFF);
  2560. fEventsOut.ctrl->port->writeMidiEvent(ev->frames, static_cast<uint8_t>(ev->size), data);
  2561. }
  2562. lv2_event_increment(&iter);
  2563. }
  2564. }
  2565. else if ((fEventsOut.ctrl->type & CARLA_EVENT_DATA_MIDI_LL) != 0 && fEventsOut.ctrl->port != nullptr)
  2566. {
  2567. LV2_MIDIState state = { &fEventsOut.ctrl->midi, frames, 0 };
  2568. uint32_t eventSize;
  2569. double eventTime;
  2570. unsigned char* eventData;
  2571. for (;;)
  2572. {
  2573. eventSize = 0;
  2574. eventTime = 0.0;
  2575. eventData = nullptr;
  2576. lv2midi_get_event(&state, &eventTime, &eventSize, &eventData);
  2577. if (eventData == nullptr || eventSize == 0)
  2578. break;
  2579. CARLA_SAFE_ASSERT_CONTINUE(eventSize < 0xFF);
  2580. CARLA_SAFE_ASSERT_CONTINUE(eventTime >= 0.0);
  2581. fEventsOut.ctrl->port->writeMidiEvent(static_cast<uint32_t>(eventTime), static_cast<uint8_t>(eventSize), eventData);
  2582. lv2midi_step(&state);
  2583. }
  2584. }
  2585. }
  2586. // --------------------------------------------------------------------------------------------------------
  2587. // Control Output
  2588. if (pData->event.portOut != nullptr)
  2589. {
  2590. uint8_t channel;
  2591. uint16_t param;
  2592. float value;
  2593. for (uint32_t k=0; k < pData->param.count; ++k)
  2594. {
  2595. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  2596. continue;
  2597. pData->param.ranges[k].fixValue(fParamBuffers[k]);
  2598. if (pData->param.data[k].midiCC > 0)
  2599. {
  2600. channel = pData->param.data[k].midiChannel;
  2601. param = static_cast<uint16_t>(pData->param.data[k].midiCC);
  2602. value = pData->param.ranges[k].getNormalizedValue(fParamBuffers[k]);
  2603. pData->event.portOut->writeControlEvent(0, channel, kEngineControlEventTypeParameter, param, value);
  2604. }
  2605. }
  2606. } // End of Control Output
  2607. CARLA_PROCESS_CONTINUE_CHECK;
  2608. // --------------------------------------------------------------------------------------------------------
  2609. // Final work
  2610. if (fExt.worker != nullptr && fExt.worker->end_run != nullptr)
  2611. {
  2612. fExt.worker->end_run(fHandle);
  2613. if (fHandle2 != nullptr)
  2614. fExt.worker->end_run(fHandle2);
  2615. }
  2616. fFirstActive = false;
  2617. // --------------------------------------------------------------------------------------------------------
  2618. }
  2619. bool processSingle(float** const audioInBuf, float** const audioOutBuf, float** const cvInBuf, float** const cvOutBuf, const uint32_t frames, const uint32_t timeOffset)
  2620. {
  2621. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  2622. if (pData->audioIn.count > 0)
  2623. {
  2624. CARLA_SAFE_ASSERT_RETURN(audioInBuf != nullptr, false);
  2625. }
  2626. if (pData->audioOut.count > 0)
  2627. {
  2628. CARLA_SAFE_ASSERT_RETURN(audioOutBuf != nullptr, false);
  2629. }
  2630. if (fCvIn.count > 0)
  2631. {
  2632. CARLA_SAFE_ASSERT_RETURN(cvInBuf != nullptr, false);
  2633. }
  2634. if (fCvOut.count > 0)
  2635. {
  2636. CARLA_SAFE_ASSERT_RETURN(cvOutBuf != nullptr, false);
  2637. }
  2638. // --------------------------------------------------------------------------------------------------------
  2639. // Try lock, silence otherwise
  2640. if (pData->engine->isOffline())
  2641. {
  2642. pData->singleMutex.lock();
  2643. }
  2644. else if (! pData->singleMutex.tryLock())
  2645. {
  2646. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2647. {
  2648. for (uint32_t k=0; k < frames; ++k)
  2649. audioOutBuf[i][k+timeOffset] = 0.0f;
  2650. }
  2651. return false;
  2652. }
  2653. // --------------------------------------------------------------------------------------------------------
  2654. // Set audio buffers
  2655. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2656. FLOAT_COPY(fAudioInBuffers[i], audioInBuf[i]+timeOffset, frames);
  2657. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2658. FLOAT_CLEAR(fAudioOutBuffers[i], frames);
  2659. // --------------------------------------------------------------------------------------------------------
  2660. // Set CV buffers
  2661. for (uint32_t i=0; i < fCvIn.count; ++i)
  2662. FLOAT_COPY(fCvInBuffers[i], cvInBuf[i]+timeOffset, frames);
  2663. for (uint32_t i=0; i < fCvOut.count; ++i)
  2664. FLOAT_CLEAR(fCvOutBuffers[i], frames);
  2665. // --------------------------------------------------------------------------------------------------------
  2666. // Run plugin
  2667. fDescriptor->run(fHandle, frames);
  2668. if (fHandle2 != nullptr)
  2669. fDescriptor->run(fHandle2, frames);
  2670. // --------------------------------------------------------------------------------------------------------
  2671. // Handle trigger parameters
  2672. for (uint32_t k=0; k < pData->param.count; ++k)
  2673. {
  2674. if (pData->param.data[k].type != PARAMETER_INPUT)
  2675. continue;
  2676. if (pData->param.data[k].hints & PARAMETER_IS_TRIGGER)
  2677. {
  2678. if (fParamBuffers[k] != pData->param.ranges[k].def)
  2679. {
  2680. fParamBuffers[k] = pData->param.ranges[k].def;
  2681. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, fParamBuffers[k]);
  2682. }
  2683. }
  2684. }
  2685. pData->postRtEvents.trySplice();
  2686. #ifndef BUILD_BRIDGE
  2687. // --------------------------------------------------------------------------------------------------------
  2688. // Post-processing (dry/wet, volume and balance)
  2689. {
  2690. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  2691. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  2692. bool isPair;
  2693. float bufValue, oldBufLeft[doBalance ? frames : 1];
  2694. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2695. {
  2696. // Dry/Wet
  2697. if (doDryWet)
  2698. {
  2699. for (uint32_t k=0; k < frames; ++k)
  2700. {
  2701. // TODO
  2702. //if (k < pData->latency && pData->latency < frames)
  2703. // bufValue = (pData->audioIn.count == 1) ? pData->latencyBuffers[0][k] : pData->latencyBuffers[i][k];
  2704. //else
  2705. // bufValue = (pData->audioIn.count == 1) ? inBuffer[0][k-m_latency] : inBuffer[i][k-m_latency];
  2706. bufValue = fAudioInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  2707. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  2708. }
  2709. }
  2710. // Balance
  2711. if (doBalance)
  2712. {
  2713. isPair = (i % 2 == 0);
  2714. if (isPair)
  2715. {
  2716. CARLA_ASSERT(i+1 < pData->audioOut.count);
  2717. FLOAT_COPY(oldBufLeft, fAudioOutBuffers[i], frames);
  2718. }
  2719. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  2720. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  2721. for (uint32_t k=0; k < frames; ++k)
  2722. {
  2723. if (isPair)
  2724. {
  2725. // left
  2726. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  2727. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  2728. }
  2729. else
  2730. {
  2731. // right
  2732. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  2733. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  2734. }
  2735. }
  2736. }
  2737. // Volume (and buffer copy)
  2738. {
  2739. for (uint32_t k=0; k < frames; ++k)
  2740. audioOutBuf[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  2741. }
  2742. }
  2743. } // End of Post-processing
  2744. #else // BUILD_BRIDGE
  2745. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2746. {
  2747. for (uint32_t k=0; k < frames; ++k)
  2748. audioOutBuf[i][k+timeOffset] = fAudioOutBuffers[i][k];
  2749. }
  2750. #endif
  2751. // --------------------------------------------------------------------------------------------------------
  2752. pData->singleMutex.unlock();
  2753. return true;
  2754. }
  2755. void bufferSizeChanged(const uint32_t newBufferSize) override
  2756. {
  2757. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  2758. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - start", newBufferSize);
  2759. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2760. {
  2761. if (fAudioInBuffers[i] != nullptr)
  2762. delete[] fAudioInBuffers[i];
  2763. fAudioInBuffers[i] = new float[newBufferSize];
  2764. }
  2765. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2766. {
  2767. if (fAudioOutBuffers[i] != nullptr)
  2768. delete[] fAudioOutBuffers[i];
  2769. fAudioOutBuffers[i] = new float[newBufferSize];
  2770. }
  2771. if (fHandle2 == nullptr)
  2772. {
  2773. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2774. {
  2775. CARLA_ASSERT(fAudioInBuffers[i] != nullptr);
  2776. fDescriptor->connect_port(fHandle, pData->audioIn.ports[i].rindex, fAudioInBuffers[i]);
  2777. }
  2778. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2779. {
  2780. CARLA_ASSERT(fAudioOutBuffers[i] != nullptr);
  2781. fDescriptor->connect_port(fHandle, pData->audioOut.ports[i].rindex, fAudioOutBuffers[i]);
  2782. }
  2783. }
  2784. else
  2785. {
  2786. if (pData->audioIn.count > 0)
  2787. {
  2788. CARLA_ASSERT(pData->audioIn.count == 2);
  2789. CARLA_ASSERT(fAudioInBuffers[0] != nullptr);
  2790. CARLA_ASSERT(fAudioInBuffers[1] != nullptr);
  2791. fDescriptor->connect_port(fHandle, pData->audioIn.ports[0].rindex, fAudioInBuffers[0]);
  2792. fDescriptor->connect_port(fHandle2, pData->audioIn.ports[1].rindex, fAudioInBuffers[1]);
  2793. }
  2794. if (pData->audioOut.count > 0)
  2795. {
  2796. CARLA_ASSERT(pData->audioOut.count == 2);
  2797. CARLA_ASSERT(fAudioOutBuffers[0] != nullptr);
  2798. CARLA_ASSERT(fAudioOutBuffers[1] != nullptr);
  2799. fDescriptor->connect_port(fHandle, pData->audioOut.ports[0].rindex, fAudioOutBuffers[0]);
  2800. fDescriptor->connect_port(fHandle2, pData->audioOut.ports[1].rindex, fAudioOutBuffers[1]);
  2801. }
  2802. }
  2803. for (uint32_t i=0; i < fCvIn.count; ++i)
  2804. {
  2805. if (fCvInBuffers[i] != nullptr)
  2806. delete[] fCvInBuffers[i];
  2807. fCvInBuffers[i] = new float[newBufferSize];
  2808. fDescriptor->connect_port(fHandle, fCvIn.ports[i].rindex, fCvInBuffers[i]);
  2809. if (fHandle2 != nullptr)
  2810. fDescriptor->connect_port(fHandle2, fCvIn.ports[i].rindex, fCvInBuffers[i]);
  2811. }
  2812. for (uint32_t i=0; i < fCvOut.count; ++i)
  2813. {
  2814. if (fCvOutBuffers[i] != nullptr)
  2815. delete[] fCvOutBuffers[i];
  2816. fCvOutBuffers[i] = new float[newBufferSize];
  2817. fDescriptor->connect_port(fHandle, fCvOut.ports[i].rindex, fCvOutBuffers[i]);
  2818. if (fHandle2 != nullptr)
  2819. fDescriptor->connect_port(fHandle2, fCvOut.ports[i].rindex, fCvOutBuffers[i]);
  2820. }
  2821. const int newBufferSizeInt(static_cast<int>(newBufferSize));
  2822. if (fLv2Options.maxBufferSize != newBufferSizeInt || (fLv2Options.minBufferSize != 1 && fLv2Options.minBufferSize != newBufferSizeInt))
  2823. {
  2824. fLv2Options.maxBufferSize = newBufferSizeInt;
  2825. if (fLv2Options.minBufferSize != 1)
  2826. fLv2Options.minBufferSize = newBufferSizeInt;
  2827. if (fExt.options != nullptr && fExt.options->set != nullptr)
  2828. {
  2829. fExt.options->set(fHandle, &fLv2Options.opts[Lv2PluginOptions::MaxBlockLenth]);
  2830. fExt.options->set(fHandle, &fLv2Options.opts[Lv2PluginOptions::MinBlockLenth]);
  2831. }
  2832. }
  2833. carla_debug("Lv2Plugin::bufferSizeChanged(%i) - end", newBufferSize);
  2834. }
  2835. void sampleRateChanged(const double newSampleRate) override
  2836. {
  2837. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  2838. carla_debug("Lv2Plugin::sampleRateChanged(%g) - start", newSampleRate);
  2839. if (fLv2Options.sampleRate != newSampleRate)
  2840. {
  2841. fLv2Options.sampleRate = newSampleRate;
  2842. if (fExt.options != nullptr && fExt.options->set != nullptr)
  2843. fExt.options->set(fHandle, &fLv2Options.opts[Lv2PluginOptions::SampleRate]);
  2844. }
  2845. for (uint32_t k=0; k < pData->param.count; ++k)
  2846. {
  2847. if (pData->param.data[k].type == PARAMETER_INPUT && pData->param.special[k] == PARAMETER_SPECIAL_SAMPLE_RATE)
  2848. {
  2849. fParamBuffers[k] = static_cast<float>(newSampleRate);
  2850. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  2851. break;
  2852. }
  2853. }
  2854. carla_debug("Lv2Plugin::sampleRateChanged(%g) - end", newSampleRate);
  2855. }
  2856. void offlineModeChanged(const bool isOffline) override
  2857. {
  2858. for (uint32_t k=0; k < pData->param.count; ++k)
  2859. {
  2860. if (pData->param.data[k].type == PARAMETER_INPUT && pData->param.special[k] == PARAMETER_SPECIAL_LV2_FREEWHEEL)
  2861. {
  2862. fParamBuffers[k] = isOffline ? pData->param.ranges[k].max : pData->param.ranges[k].min;
  2863. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 1, fParamBuffers[k]);
  2864. break;
  2865. }
  2866. }
  2867. }
  2868. // -------------------------------------------------------------------
  2869. // Plugin buffers
  2870. void initBuffers() override
  2871. {
  2872. fCvIn.initBuffers();
  2873. fCvOut.initBuffers();
  2874. fEventsIn.initBuffers();
  2875. fEventsOut.initBuffers();
  2876. CarlaPlugin::initBuffers();
  2877. }
  2878. void clearBuffers() override
  2879. {
  2880. carla_debug("Lv2Plugin::clearBuffers() - start");
  2881. if (fAudioInBuffers != nullptr)
  2882. {
  2883. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  2884. {
  2885. if (fAudioInBuffers[i] != nullptr)
  2886. {
  2887. delete[] fAudioInBuffers[i];
  2888. fAudioInBuffers[i] = nullptr;
  2889. }
  2890. }
  2891. delete[] fAudioInBuffers;
  2892. fAudioInBuffers = nullptr;
  2893. }
  2894. if (fAudioOutBuffers != nullptr)
  2895. {
  2896. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  2897. {
  2898. if (fAudioOutBuffers[i] != nullptr)
  2899. {
  2900. delete[] fAudioOutBuffers[i];
  2901. fAudioOutBuffers[i] = nullptr;
  2902. }
  2903. }
  2904. delete[] fAudioOutBuffers;
  2905. fAudioOutBuffers = nullptr;
  2906. }
  2907. if (fCvInBuffers != nullptr)
  2908. {
  2909. for (uint32_t i=0; i < fCvIn.count; ++i)
  2910. {
  2911. if (fCvInBuffers[i] != nullptr)
  2912. {
  2913. delete[] fCvInBuffers[i];
  2914. fCvInBuffers[i] = nullptr;
  2915. }
  2916. }
  2917. delete[] fCvInBuffers;
  2918. fCvInBuffers = nullptr;
  2919. }
  2920. if (fCvOutBuffers != nullptr)
  2921. {
  2922. for (uint32_t i=0; i < fCvOut.count; ++i)
  2923. {
  2924. if (fCvOutBuffers[i] != nullptr)
  2925. {
  2926. delete[] fCvOutBuffers[i];
  2927. fCvOutBuffers[i] = nullptr;
  2928. }
  2929. }
  2930. delete[] fCvOutBuffers;
  2931. fCvOutBuffers = nullptr;
  2932. }
  2933. if (fParamBuffers != nullptr)
  2934. {
  2935. delete[] fParamBuffers;
  2936. fParamBuffers = nullptr;
  2937. }
  2938. fCvIn.clear();
  2939. fCvOut.clear();
  2940. fEventsIn.clear();
  2941. fEventsOut.clear();
  2942. CarlaPlugin::clearBuffers();
  2943. carla_debug("Lv2Plugin::clearBuffers() - end");
  2944. }
  2945. // -------------------------------------------------------------------
  2946. // OSC stuff
  2947. bool updateOscDataExtra() override
  2948. {
  2949. for (size_t i=CARLA_URI_MAP_ID_COUNT, count=fCustomURIDs.count(); i < count; ++i)
  2950. osc_send_lv2_urid_map(pData->osc.data, static_cast<uint32_t>(i), fCustomURIDs.getAt(i));
  2951. osc_send_lv2_urid_map(pData->osc.data, CARLA_URI_MAP_ID_NULL, "Complete");
  2952. return true;
  2953. }
  2954. // -------------------------------------------------------------------
  2955. // Post-poned UI Stuff
  2956. void uiParameterChange(const uint32_t index, const float value) noexcept override
  2957. {
  2958. CARLA_SAFE_ASSERT_RETURN(fUi.type != UI::TYPE_NULL,);
  2959. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  2960. if (fUi.type == UI::TYPE_OSC)
  2961. {
  2962. if (pData->osc.data.target != nullptr)
  2963. osc_send_control(pData->osc.data, pData->param.data[index].rindex, value);
  2964. }
  2965. else
  2966. {
  2967. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr)
  2968. {
  2969. CARLA_SAFE_ASSERT_RETURN(pData->param.data[index].rindex >= 0,);
  2970. fUi.descriptor->port_event(fUi.handle, static_cast<uint32_t>(pData->param.data[index].rindex), sizeof(float), 0, &value);
  2971. }
  2972. }
  2973. }
  2974. void uiMidiProgramChange(const uint32_t index) noexcept override
  2975. {
  2976. CARLA_SAFE_ASSERT_RETURN(fUi.type != UI::TYPE_NULL,);
  2977. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  2978. if (fUi.type == UI::TYPE_OSC)
  2979. {
  2980. if (pData->osc.data.target != nullptr)
  2981. osc_send_midi_program(pData->osc.data, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  2982. }
  2983. else
  2984. {
  2985. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program != nullptr)
  2986. fExt.uiprograms->select_program(fUi.handle, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  2987. }
  2988. }
  2989. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  2990. {
  2991. CARLA_SAFE_ASSERT_RETURN(fUi.type != UI::TYPE_NULL,);
  2992. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  2993. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  2994. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  2995. if (fUi.type == UI::TYPE_OSC)
  2996. {
  2997. if (pData->osc.data.target != nullptr)
  2998. {
  2999. uint8_t midiData[4] = { 0 };
  3000. midiData[1] = static_cast<uint8_t>(MIDI_STATUS_NOTE_ON + channel);
  3001. midiData[2] = note;
  3002. midiData[3] = velo;
  3003. osc_send_midi(pData->osc.data, midiData);
  3004. }
  3005. }
  3006. else
  3007. {
  3008. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr)
  3009. {
  3010. LV2_Atom_MidiEvent midiEv;
  3011. midiEv.event.time.frames = 0;
  3012. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  3013. midiEv.event.body.size = 3;
  3014. midiEv.data[0] = static_cast<uint8_t>(MIDI_STATUS_NOTE_ON + channel);
  3015. midiEv.data[1] = note;
  3016. midiEv.data[2] = velo;
  3017. fUi.descriptor->port_event(fUi.handle, fEventsIn.ctrl->rindex, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  3018. }
  3019. }
  3020. }
  3021. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  3022. {
  3023. CARLA_SAFE_ASSERT_RETURN(fUi.type != UI::TYPE_NULL,);
  3024. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  3025. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  3026. if (fUi.type == UI::TYPE_OSC)
  3027. {
  3028. if (pData->osc.data.target != nullptr)
  3029. {
  3030. uint8_t midiData[4] = { 0 };
  3031. midiData[1] = static_cast<uint8_t>(MIDI_STATUS_NOTE_OFF + channel);
  3032. midiData[2] = note;
  3033. osc_send_midi(pData->osc.data, midiData);
  3034. }
  3035. }
  3036. else
  3037. {
  3038. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->port_event != nullptr && fEventsIn.ctrl != nullptr)
  3039. {
  3040. LV2_Atom_MidiEvent midiEv;
  3041. midiEv.event.time.frames = 0;
  3042. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  3043. midiEv.event.body.size = 3;
  3044. midiEv.data[0] = static_cast<uint8_t>(MIDI_STATUS_NOTE_OFF + channel);
  3045. midiEv.data[1] = note;
  3046. midiEv.data[2] = 0;
  3047. fUi.descriptor->port_event(fUi.handle, fEventsIn.ctrl->rindex, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  3048. }
  3049. }
  3050. }
  3051. // -------------------------------------------------------------------
  3052. bool isRealtimeSafe() const noexcept
  3053. {
  3054. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  3055. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  3056. {
  3057. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_CORE__hardRTCapable) == 0)
  3058. return true;
  3059. }
  3060. return false;
  3061. }
  3062. bool needsFixedBuffer() const noexcept
  3063. {
  3064. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  3065. for (uint32_t i=0; i < fRdfDescriptor->FeatureCount; ++i)
  3066. {
  3067. if (std::strcmp(fRdfDescriptor->Features[i].URI, LV2_BUF_SIZE__fixedBlockLength) == 0)
  3068. return true;
  3069. }
  3070. return false;
  3071. }
  3072. // -------------------------------------------------------------------
  3073. bool isUiBridgeable(const uint32_t uiId) const noexcept
  3074. {
  3075. CARLA_SAFE_ASSERT_RETURN(uiId < fRdfDescriptor->UICount, false);
  3076. #ifndef LV2_UIS_ONLY_INPROCESS
  3077. const LV2_RDF_UI* const rdfUi(&fRdfDescriptor->UIs[uiId]);
  3078. if (std::strstr(rdfUi->URI, "http://calf.sourceforge.net/plugins/gui/") != nullptr)
  3079. return false;
  3080. for (uint32_t i=0; i < rdfUi->FeatureCount; ++i)
  3081. {
  3082. if (std::strcmp(rdfUi->Features[i].URI, LV2_INSTANCE_ACCESS_URI) == 0)
  3083. return false;
  3084. if (std::strcmp(rdfUi->Features[i].URI, LV2_DATA_ACCESS_URI) == 0)
  3085. return false;
  3086. }
  3087. return true;
  3088. #else
  3089. return false;
  3090. #endif
  3091. }
  3092. bool isUiResizable() const noexcept
  3093. {
  3094. CARLA_SAFE_ASSERT_RETURN(fUi.rdfDescriptor != nullptr, false);
  3095. for (uint32_t i=0; i < fUi.rdfDescriptor->FeatureCount; ++i)
  3096. {
  3097. if (std::strcmp(fUi.rdfDescriptor->Features[i].URI, LV2_UI__fixedSize) == 0)
  3098. return false;
  3099. if (std::strcmp(fUi.rdfDescriptor->Features[i].URI, LV2_UI__noUserResize) == 0)
  3100. return false;
  3101. }
  3102. return true;
  3103. }
  3104. const char* getUiBridgeBinary(const LV2_Property type) const
  3105. {
  3106. CarlaString bridgeBinary(pData->engine->getOptions().binaryDir);
  3107. if (bridgeBinary.isEmpty())
  3108. return nullptr;
  3109. #ifdef CARLA_OS_LINUX
  3110. // test for local build
  3111. if (bridgeBinary.endsWith("/source/backend/"))
  3112. bridgeBinary += "../bridges/";
  3113. #endif
  3114. switch (type)
  3115. {
  3116. case LV2_UI_GTK2:
  3117. bridgeBinary += "carla-bridge-lv2-gtk2";
  3118. break;
  3119. case LV2_UI_GTK3:
  3120. bridgeBinary += "carla-bridge-lv2-gtk3";
  3121. break;
  3122. case LV2_UI_QT4:
  3123. bridgeBinary += "carla-bridge-lv2-qt4";
  3124. break;
  3125. case LV2_UI_QT5:
  3126. bridgeBinary += "carla-bridge-lv2-qt5";
  3127. break;
  3128. case LV2_UI_COCOA:
  3129. bridgeBinary += "carla-bridge-lv2-cocoa";
  3130. break;
  3131. case LV2_UI_WINDOWS:
  3132. bridgeBinary += "carla-bridge-lv2-windows";
  3133. break;
  3134. case LV2_UI_X11:
  3135. bridgeBinary += "carla-bridge-lv2-x11";
  3136. break;
  3137. case LV2_UI_EXTERNAL:
  3138. case LV2_UI_OLD_EXTERNAL:
  3139. bridgeBinary += "carla-bridge-lv2-external";
  3140. break;
  3141. default:
  3142. return nullptr;
  3143. }
  3144. #ifdef CARLA_OS_WIN
  3145. bridgeBinary += ".exe";
  3146. #endif
  3147. QFile file(bridgeBinary.buffer());
  3148. if (! file.exists())
  3149. return nullptr;
  3150. return bridgeBinary.dup();
  3151. }
  3152. // -------------------------------------------------------------------
  3153. void recheckExtensions()
  3154. {
  3155. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr,);
  3156. carla_debug("Lv2Plugin::recheckExtensions()");
  3157. fExt.options = nullptr;
  3158. fExt.programs = nullptr;
  3159. fExt.state = nullptr;
  3160. fExt.worker = nullptr;
  3161. for (uint32_t i=0; i < fRdfDescriptor->ExtensionCount; ++i)
  3162. {
  3163. CARLA_SAFE_ASSERT_CONTINUE(fRdfDescriptor->Extensions[i] != nullptr);
  3164. if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_OPTIONS__interface) == 0)
  3165. pData->hints |= PLUGIN_HAS_EXTENSION_OPTIONS;
  3166. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_PROGRAMS__Interface) == 0)
  3167. pData->hints |= PLUGIN_HAS_EXTENSION_PROGRAMS;
  3168. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_STATE__interface) == 0)
  3169. pData->hints |= PLUGIN_HAS_EXTENSION_STATE;
  3170. else if (std::strcmp(fRdfDescriptor->Extensions[i], LV2_WORKER__interface) == 0)
  3171. pData->hints |= PLUGIN_HAS_EXTENSION_WORKER;
  3172. else
  3173. carla_stdout("Plugin has non-supported extension: '%s'", fRdfDescriptor->Extensions[i]);
  3174. }
  3175. if (fDescriptor->extension_data != nullptr)
  3176. {
  3177. if (pData->hints & PLUGIN_HAS_EXTENSION_OPTIONS)
  3178. fExt.options = (const LV2_Options_Interface*)fDescriptor->extension_data(LV2_OPTIONS__interface);
  3179. if (pData->hints & PLUGIN_HAS_EXTENSION_PROGRAMS)
  3180. fExt.programs = (const LV2_Programs_Interface*)fDescriptor->extension_data(LV2_PROGRAMS__Interface);
  3181. if (pData->hints & PLUGIN_HAS_EXTENSION_STATE)
  3182. fExt.state = (const LV2_State_Interface*)fDescriptor->extension_data(LV2_STATE__interface);
  3183. if (pData->hints & PLUGIN_HAS_EXTENSION_WORKER)
  3184. fExt.worker = (const LV2_Worker_Interface*)fDescriptor->extension_data(LV2_WORKER__interface);
  3185. // check if invalid
  3186. if (fExt.options != nullptr && fExt.options->get == nullptr && fExt.options->set == nullptr)
  3187. fExt.options = nullptr;
  3188. if (fExt.programs != nullptr && (fExt.programs->get_program == nullptr || fExt.programs->select_program == nullptr))
  3189. fExt.programs = nullptr;
  3190. if (fExt.state != nullptr && (fExt.state->save == nullptr || fExt.state->restore == nullptr))
  3191. fExt.state = nullptr;
  3192. if (fExt.worker != nullptr && fExt.worker->work == nullptr)
  3193. fExt.worker = nullptr;
  3194. }
  3195. }
  3196. // -------------------------------------------------------------------
  3197. void updateUi()
  3198. {
  3199. CARLA_SAFE_ASSERT_RETURN(fUi.handle != nullptr,);
  3200. CARLA_SAFE_ASSERT_RETURN(fUi.descriptor != nullptr,);
  3201. carla_debug("Lv2Plugin::updateUi()");
  3202. // update midi program
  3203. if (fExt.uiprograms != nullptr && pData->midiprog.count > 0 && pData->midiprog.current >= 0)
  3204. {
  3205. const MidiProgramData& curData(pData->midiprog.getCurrent());
  3206. fExt.uiprograms->select_program(fUi.handle, curData.bank, curData.program);
  3207. }
  3208. // update control ports
  3209. if (fUi.descriptor->port_event != nullptr)
  3210. {
  3211. float value;
  3212. for (uint32_t i=0; i < pData->param.count; ++i)
  3213. {
  3214. value = getParameterValue(i);
  3215. fUi.descriptor->port_event(fUi.handle, static_cast<uint32_t>(pData->param.data[i].rindex), sizeof(float), CARLA_URI_MAP_ID_NULL, &value);
  3216. }
  3217. }
  3218. }
  3219. // -------------------------------------------------------------------
  3220. LV2_URID getCustomURID(const char* const uri)
  3221. {
  3222. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', CARLA_URI_MAP_ID_NULL);
  3223. carla_debug("Lv2Plugin::getCustomURID(\"%s\")", uri);
  3224. for (size_t i=0; i < fCustomURIDs.count(); ++i)
  3225. {
  3226. const char* const thisUri(fCustomURIDs.getAt(i));
  3227. if (thisUri != nullptr && std::strcmp(thisUri, uri) == 0)
  3228. return static_cast<LV2_URID>(i);
  3229. }
  3230. const LV2_URID urid(static_cast<LV2_URID>(fCustomURIDs.count()));
  3231. fCustomURIDs.append(carla_strdup(uri));
  3232. if (fUi.type == UI::TYPE_OSC && pData->osc.data.target != nullptr)
  3233. osc_send_lv2_urid_map(pData->osc.data, urid, uri);
  3234. return urid;
  3235. }
  3236. const char* getCustomURIDString(const LV2_URID urid) const noexcept
  3237. {
  3238. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL, nullptr);
  3239. CARLA_SAFE_ASSERT_RETURN(urid < fCustomURIDs.count(), nullptr);
  3240. carla_debug("Lv2Plugin::getCustomURIString(%i)", urid);
  3241. return fCustomURIDs.getAt(urid);
  3242. }
  3243. // -------------------------------------------------------------------
  3244. void handleProgramChanged(const int32_t index)
  3245. {
  3246. CARLA_SAFE_ASSERT_RETURN(index >= -1,);
  3247. carla_debug("Lv2Plugin::handleProgramChanged(%i)", index);
  3248. if (index == -1)
  3249. {
  3250. const ScopedSingleProcessLocker spl(this, true);
  3251. return reloadPrograms(false);
  3252. }
  3253. if (index < static_cast<int32_t>(pData->midiprog.count) && fExt.programs != nullptr && fExt.programs->get_program != nullptr)
  3254. {
  3255. if (const LV2_Program_Descriptor* const progDesc = fExt.programs->get_program(fHandle, static_cast<uint32_t>(index)))
  3256. {
  3257. CARLA_SAFE_ASSERT_RETURN(progDesc->name != nullptr,);
  3258. if (pData->midiprog.data[index].name != nullptr)
  3259. delete[] pData->midiprog.data[index].name;
  3260. pData->midiprog.data[index].name = carla_strdup(progDesc->name);
  3261. if (index == pData->midiprog.current)
  3262. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, 0, 0, 0.0, nullptr);
  3263. else
  3264. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0, nullptr);
  3265. }
  3266. }
  3267. }
  3268. // -------------------------------------------------------------------
  3269. LV2_Resize_Port_Status handleResizePort(const uint32_t index, const size_t size)
  3270. {
  3271. CARLA_SAFE_ASSERT_RETURN(size > 0, LV2_RESIZE_PORT_ERR_UNKNOWN);
  3272. carla_debug("Lv2Plugin::handleResizePort(%i, " P_SIZE ")", index, size);
  3273. // TODO
  3274. return LV2_RESIZE_PORT_ERR_NO_SPACE;
  3275. (void)index;
  3276. }
  3277. // -------------------------------------------------------------------
  3278. LV2_State_Status handleStateStore(const uint32_t key, const void* const value, const size_t size, const uint32_t type, const uint32_t flags)
  3279. {
  3280. CARLA_SAFE_ASSERT_RETURN(key != CARLA_URI_MAP_ID_NULL, LV2_STATE_ERR_NO_PROPERTY);
  3281. CARLA_SAFE_ASSERT_RETURN(value != nullptr, LV2_STATE_ERR_NO_PROPERTY);
  3282. CARLA_SAFE_ASSERT_RETURN(size > 0, LV2_STATE_ERR_NO_PROPERTY);
  3283. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, LV2_STATE_ERR_BAD_TYPE);
  3284. CARLA_SAFE_ASSERT_RETURN(flags & LV2_STATE_IS_POD, LV2_STATE_ERR_BAD_FLAGS);
  3285. carla_debug("Lv2Plugin::handleStateStore(%i:\"%s\", %p, " P_SIZE ", %i:\"%s\", %i)", key, carla_lv2_urid_unmap(this, key), value, size, type, carla_lv2_urid_unmap(this, type), flags);
  3286. const char* const skey(carla_lv2_urid_unmap(this, key));
  3287. const char* const stype(carla_lv2_urid_unmap(this, type));
  3288. CARLA_SAFE_ASSERT_RETURN(skey != nullptr, LV2_STATE_ERR_BAD_TYPE);
  3289. CARLA_SAFE_ASSERT_RETURN(stype != nullptr, LV2_STATE_ERR_BAD_TYPE);
  3290. // Check if we already have this key
  3291. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  3292. {
  3293. CustomData& data(it.getValue());
  3294. if (std::strcmp(data.key, skey) == 0)
  3295. {
  3296. // found it
  3297. if (data.value != nullptr)
  3298. delete[] data.value;
  3299. if (type == CARLA_URI_MAP_ID_ATOM_STRING || type == CARLA_URI_MAP_ID_ATOM_PATH)
  3300. data.value = carla_strdup((const char*)value);
  3301. else
  3302. data.value = carla_strdup(QByteArray((const char*)value, static_cast<int>(size)).toBase64().constData());
  3303. return LV2_STATE_SUCCESS;
  3304. }
  3305. }
  3306. // Otherwise store it
  3307. CustomData newData;
  3308. newData.type = carla_strdup(stype);
  3309. newData.key = carla_strdup(skey);
  3310. if (type == CARLA_URI_MAP_ID_ATOM_STRING || type == CARLA_URI_MAP_ID_ATOM_PATH)
  3311. newData.value = carla_strdup((const char*)value);
  3312. else
  3313. newData.value = carla_strdup(QByteArray((const char*)value, static_cast<int>(size)).toBase64().constData());
  3314. pData->custom.append(newData);
  3315. return LV2_STATE_SUCCESS;
  3316. }
  3317. const void* handleStateRetrieve(const uint32_t key, size_t* const size, uint32_t* const type, uint32_t* const flags)
  3318. {
  3319. CARLA_SAFE_ASSERT_RETURN(key != CARLA_URI_MAP_ID_NULL, nullptr);
  3320. CARLA_SAFE_ASSERT_RETURN(size != nullptr, nullptr);
  3321. CARLA_SAFE_ASSERT_RETURN(type != nullptr, nullptr);
  3322. CARLA_SAFE_ASSERT_RETURN(flags != nullptr, nullptr);
  3323. carla_debug("Lv2Plugin::handleStateRetrieve(%i, %p, %p, %p)", key, size, type, flags);
  3324. const char* const skey(carla_lv2_urid_unmap(this, key));
  3325. CARLA_SAFE_ASSERT_RETURN(skey != nullptr, nullptr);
  3326. const char* stype = nullptr;
  3327. const char* stringData = nullptr;
  3328. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  3329. {
  3330. const CustomData& data(it.getValue());
  3331. if (std::strcmp(data.key, skey) == 0)
  3332. {
  3333. stype = data.type;
  3334. stringData = data.value;
  3335. break;
  3336. }
  3337. }
  3338. CARLA_SAFE_ASSERT_RETURN(stype != nullptr, nullptr);
  3339. CARLA_SAFE_ASSERT_RETURN(stringData != nullptr, nullptr);
  3340. *type = carla_lv2_urid_map(this, stype);
  3341. *flags = LV2_STATE_IS_POD;
  3342. if (*type == CARLA_URI_MAP_ID_ATOM_STRING || *type == CARLA_URI_MAP_ID_ATOM_PATH)
  3343. {
  3344. *size = std::strlen(stringData);
  3345. return stringData;
  3346. }
  3347. else
  3348. {
  3349. static QByteArray chunk; // FIXME
  3350. chunk = QByteArray::fromBase64(stringData);
  3351. CARLA_SAFE_ASSERT_RETURN(chunk.size() > 0, nullptr);
  3352. *size = static_cast<size_t>(chunk.size());
  3353. return chunk.constData();
  3354. }
  3355. }
  3356. // -------------------------------------------------------------------
  3357. LV2_Worker_Status handleWorkerSchedule(const uint32_t size, const void* const data)
  3358. {
  3359. CARLA_SAFE_ASSERT_RETURN(fExt.worker != nullptr && fExt.worker->work != nullptr, LV2_WORKER_ERR_UNKNOWN);
  3360. CARLA_SAFE_ASSERT_RETURN(fEventsIn.ctrl != nullptr, LV2_WORKER_ERR_UNKNOWN);
  3361. carla_debug("Lv2Plugin::handleWorkerSchedule(%i, %p)", size, data);
  3362. if (pData->engine->isOffline())
  3363. {
  3364. fExt.worker->work(fHandle, carla_lv2_worker_respond, this, size, data);
  3365. return LV2_WORKER_SUCCESS;
  3366. }
  3367. LV2_Atom atom;
  3368. atom.size = size;
  3369. atom.type = CARLA_URI_MAP_ID_CARLA_ATOM_WORKER;
  3370. return fAtomQueueOut.putChunk(&atom, data, fEventsOut.ctrlIndex) ? LV2_WORKER_SUCCESS : LV2_WORKER_ERR_NO_SPACE;
  3371. }
  3372. LV2_Worker_Status handleWorkerRespond(const uint32_t size, const void* const data)
  3373. {
  3374. carla_debug("Lv2Plugin::handleWorkerRespond(%i, %p)", size, data);
  3375. LV2_Atom atom;
  3376. atom.size = size;
  3377. atom.type = CARLA_URI_MAP_ID_CARLA_ATOM_WORKER;
  3378. return fAtomQueueIn.putChunk(&atom, data, fEventsIn.ctrlIndex) ? LV2_WORKER_SUCCESS : LV2_WORKER_ERR_NO_SPACE;
  3379. }
  3380. // -------------------------------------------------------------------
  3381. void handleExternalUiClosed()
  3382. {
  3383. CARLA_SAFE_ASSERT_RETURN(fUi.type == UI::TYPE_EXTERNAL,);
  3384. carla_debug("Lv2Plugin::handleExternalUiClosed()");
  3385. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->cleanup != nullptr)
  3386. fUi.descriptor->cleanup(fUi.handle);
  3387. fUi.handle = nullptr;
  3388. fUi.widget = nullptr;
  3389. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  3390. }
  3391. void handlePluginUiClosed() override
  3392. {
  3393. CARLA_SAFE_ASSERT_RETURN(fUi.type == UI::TYPE_EMBED,);
  3394. CARLA_SAFE_ASSERT_RETURN(fUi.window != nullptr,);
  3395. carla_debug("Lv2Plugin::handleExternalUiClosed()");
  3396. fUi.window->hide();
  3397. if (fUi.handle != nullptr && fUi.descriptor != nullptr && fUi.descriptor->cleanup != nullptr)
  3398. fUi.descriptor->cleanup(fUi.handle);
  3399. fUi.handle = nullptr;
  3400. fUi.widget = nullptr;
  3401. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  3402. }
  3403. // -------------------------------------------------------------------
  3404. uint32_t handleUiPortMap(const char* const symbol) const noexcept
  3405. {
  3406. CARLA_SAFE_ASSERT_RETURN(symbol != nullptr && symbol[0] != '\0', LV2UI_INVALID_PORT_INDEX);
  3407. carla_debug("Lv2Plugin::handleUiPortMap(\"%s\")", symbol);
  3408. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  3409. {
  3410. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, symbol) == 0)
  3411. return i;
  3412. }
  3413. return LV2UI_INVALID_PORT_INDEX;
  3414. }
  3415. int handleUiResize(const int width, const int height)
  3416. {
  3417. CARLA_SAFE_ASSERT_RETURN(fUi.window != nullptr, 1);
  3418. CARLA_SAFE_ASSERT_RETURN(width > 0, 1);
  3419. CARLA_SAFE_ASSERT_RETURN(height > 0, 1);
  3420. carla_debug("Lv2Plugin::handleUiResize(%i, %i)", width, height);
  3421. fUi.window->setSize(static_cast<uint>(width), static_cast<uint>(height), true);
  3422. return 0;
  3423. }
  3424. void handleUiWrite(const uint32_t rindex, const uint32_t bufferSize, const uint32_t format, const void* const buffer)
  3425. {
  3426. CARLA_SAFE_ASSERT_RETURN(buffer != nullptr,);
  3427. CARLA_SAFE_ASSERT_RETURN(bufferSize > 0,);
  3428. carla_debug("Lv2Plugin::handleUiWrite(%i, %i, %i, %p)", rindex, bufferSize, format, buffer);
  3429. uint32_t index = LV2UI_INVALID_PORT_INDEX;
  3430. switch (format)
  3431. {
  3432. case CARLA_URI_MAP_ID_NULL: {
  3433. CARLA_SAFE_ASSERT_RETURN(bufferSize == sizeof(float),);
  3434. for (uint32_t i=0; i < pData->param.count; ++i)
  3435. {
  3436. if (pData->param.data[i].rindex != static_cast<int32_t>(rindex))
  3437. continue;
  3438. index = i;
  3439. break;
  3440. }
  3441. CARLA_SAFE_ASSERT_RETURN(index != LV2UI_INVALID_PORT_INDEX,);
  3442. const float value(*(const float*)buffer);
  3443. if (fParamBuffers[index] != value)
  3444. setParameterValue(index, value, false, true, true);
  3445. } break;
  3446. case CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM:
  3447. case CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT: {
  3448. CARLA_SAFE_ASSERT_RETURN(bufferSize >= sizeof(LV2_Atom),);
  3449. const LV2_Atom* const atom((const LV2_Atom*)buffer);
  3450. // plugins sometimes fail on this, not good...
  3451. CARLA_SAFE_ASSERT_INT2(bufferSize == lv2_atom_pad_size(static_cast<uint32_t>(sizeof(LV2_Atom)+atom->size)), bufferSize, atom->size);
  3452. for (uint32_t i=0; i < fEventsIn.count; ++i)
  3453. {
  3454. if (fEventsIn.data[i].rindex != rindex)
  3455. continue;
  3456. index = i;
  3457. break;
  3458. }
  3459. // for bad plugins
  3460. if (index == LV2UI_INVALID_PORT_INDEX)
  3461. {
  3462. CARLA_SAFE_ASSERT(index != LV2UI_INVALID_PORT_INDEX); // FIXME
  3463. index = fEventsIn.ctrlIndex;
  3464. }
  3465. fAtomQueueIn.put(atom, index);
  3466. } break;
  3467. default:
  3468. carla_stdout("Lv2Plugin::handleUiWrite(%i, %i, %i:\"%s\", %p) - unknown format", rindex, bufferSize, format, carla_lv2_urid_unmap(this, format), buffer);
  3469. break;
  3470. }
  3471. }
  3472. // -------------------------------------------------------------------
  3473. void handleLilvSetPortValue(const char* const portSymbol, const void* const value, const uint32_t size, const uint32_t type)
  3474. {
  3475. CARLA_SAFE_ASSERT_RETURN(portSymbol != nullptr && portSymbol[0] != '\0',);
  3476. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  3477. CARLA_SAFE_ASSERT_RETURN(size > 0,);
  3478. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL,);
  3479. carla_debug("Lv2Plugin::handleLilvSetPortValue(\"%s\", %p, %i, %i)", portSymbol, value, size, type);
  3480. int32_t rindex = -1;
  3481. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  3482. {
  3483. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, portSymbol) == 0)
  3484. {
  3485. rindex = static_cast<int32_t>(i);
  3486. break;
  3487. }
  3488. }
  3489. CARLA_SAFE_ASSERT_RETURN(rindex >= 0,);
  3490. float paramValue;
  3491. switch (type)
  3492. {
  3493. case CARLA_URI_MAP_ID_ATOM_BOOL:
  3494. CARLA_SAFE_ASSERT_RETURN(size == sizeof(bool),);
  3495. paramValue = (*(const bool*)value) ? 1.0f : 0.0f;
  3496. break;
  3497. case CARLA_URI_MAP_ID_ATOM_DOUBLE:
  3498. CARLA_SAFE_ASSERT_RETURN(size == sizeof(double),);
  3499. paramValue = static_cast<float>((*(const double*)value));
  3500. break;
  3501. case CARLA_URI_MAP_ID_ATOM_FLOAT:
  3502. CARLA_SAFE_ASSERT_RETURN(size == sizeof(float),);
  3503. paramValue = (*(const float*)value);
  3504. break;
  3505. case CARLA_URI_MAP_ID_ATOM_INT:
  3506. CARLA_SAFE_ASSERT_RETURN(size == sizeof(int32_t),);
  3507. paramValue = static_cast<float>((*(const int32_t*)value));
  3508. break;
  3509. case CARLA_URI_MAP_ID_ATOM_LONG:
  3510. CARLA_SAFE_ASSERT_RETURN(size == sizeof(int64_t),);
  3511. paramValue = static_cast<float>((*(const int64_t*)value));
  3512. break;
  3513. default:
  3514. carla_stdout("Lv2Plugin::handleLilvSetPortValue(\"%s\", %p, %i, %i:\"%s\") - unknown type", portSymbol, value, size, type, carla_lv2_urid_unmap(this, type));
  3515. return;
  3516. }
  3517. for (uint32_t i=0; i < pData->param.count; ++i)
  3518. {
  3519. if (pData->param.data[i].rindex == rindex)
  3520. {
  3521. setParameterValue(i, paramValue, true, true, true);
  3522. break;
  3523. }
  3524. }
  3525. }
  3526. // -------------------------------------------------------------------
  3527. public:
  3528. bool init(const char* const bundle, const char* const name, const char* const uri)
  3529. {
  3530. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  3531. // ---------------------------------------------------------------
  3532. // first checks
  3533. if (pData->client != nullptr)
  3534. {
  3535. pData->engine->setLastError("Plugin client is already registered");
  3536. return false;
  3537. }
  3538. if (bundle == nullptr || bundle[0] == '\0')
  3539. {
  3540. pData->engine->setLastError("null bundle");
  3541. return false;
  3542. }
  3543. if (uri == nullptr || uri[0] == '\0')
  3544. {
  3545. pData->engine->setLastError("null uri");
  3546. return false;
  3547. }
  3548. // ---------------------------------------------------------------
  3549. // get plugin from lv2_rdf (lilv)
  3550. fRdfDescriptor = lv2_rdf_new(uri, true);
  3551. if (fRdfDescriptor == nullptr)
  3552. {
  3553. pData->engine->setLastError("Failed to find the requested plugin in the LV2 Bundle");
  3554. return false;
  3555. }
  3556. // ---------------------------------------------------------------
  3557. // open DLL
  3558. if (! pData->libOpen(fRdfDescriptor->Binary))
  3559. {
  3560. pData->engine->setLastError(pData->libError(fRdfDescriptor->Binary));
  3561. return false;
  3562. }
  3563. // ---------------------------------------------------------------
  3564. // try to get DLL main entry via new mode
  3565. if (const LV2_Lib_Descriptor_Function libDescFn = (LV2_Lib_Descriptor_Function)pData->libSymbol("lv2_lib_descriptor"))
  3566. {
  3567. // -----------------------------------------------------------
  3568. // all ok, get lib descriptor
  3569. const LV2_Lib_Descriptor* const libDesc = libDescFn(bundle, nullptr);
  3570. if (libDesc == nullptr)
  3571. {
  3572. pData->engine->setLastError("Could not find the LV2 Descriptor");
  3573. return false;
  3574. }
  3575. // -----------------------------------------------------------
  3576. // get descriptor that matches URI (new mode)
  3577. uint32_t i = 0;
  3578. while ((fDescriptor = libDesc->get_plugin(libDesc->handle, i++)))
  3579. {
  3580. if (std::strcmp(fDescriptor->URI, uri) == 0)
  3581. break;
  3582. }
  3583. }
  3584. else
  3585. {
  3586. // -----------------------------------------------------------
  3587. // get DLL main entry (old mode)
  3588. const LV2_Descriptor_Function descFn = (LV2_Descriptor_Function)pData->libSymbol("lv2_descriptor");
  3589. if (descFn == nullptr)
  3590. {
  3591. pData->engine->setLastError("Could not find the LV2 Descriptor in the plugin library");
  3592. return false;
  3593. }
  3594. // -----------------------------------------------------------
  3595. // get descriptor that matches URI (old mode)
  3596. uint32_t i = 0;
  3597. while ((fDescriptor = descFn(i++)))
  3598. {
  3599. if (std::strcmp(fDescriptor->URI, uri) == 0)
  3600. break;
  3601. }
  3602. }
  3603. if (fDescriptor == nullptr)
  3604. {
  3605. pData->engine->setLastError("Could not find the requested plugin URI in the plugin library");
  3606. return false;
  3607. }
  3608. // ---------------------------------------------------------------
  3609. // check supported port-types and features
  3610. bool canContinue = true;
  3611. // Check supported ports
  3612. for (uint32_t j=0; j < fRdfDescriptor->PortCount; ++j)
  3613. {
  3614. const LV2_Property portTypes(fRdfDescriptor->Ports[j].Types);
  3615. if (! is_lv2_port_supported(portTypes))
  3616. {
  3617. if (! LV2_IS_PORT_OPTIONAL(fRdfDescriptor->Ports[j].Properties))
  3618. {
  3619. pData->engine->setLastError("Plugin requires a port type that is not currently supported");
  3620. canContinue = false;
  3621. break;
  3622. }
  3623. }
  3624. }
  3625. // Check supported features
  3626. for (uint32_t j=0; j < fRdfDescriptor->FeatureCount && canContinue; ++j)
  3627. {
  3628. if (! is_lv2_feature_supported(fRdfDescriptor->Features[j].URI))
  3629. {
  3630. QString msg(QString("Plugin wants a feature that is not supported:\n%1").arg(fRdfDescriptor->Features[j].URI));
  3631. if (LV2_IS_FEATURE_REQUIRED(fRdfDescriptor->Features[j].Type))
  3632. {
  3633. canContinue = false;
  3634. pData->engine->setLastError(msg.toUtf8().constData());
  3635. break;
  3636. }
  3637. else
  3638. carla_stderr("%s", msg.toUtf8().constData());
  3639. }
  3640. }
  3641. if (! canContinue)
  3642. {
  3643. // error already set
  3644. return false;
  3645. }
  3646. // ---------------------------------------------------------------
  3647. // get info
  3648. if (name != nullptr && name[0] != '\0')
  3649. pData->name = pData->engine->getUniquePluginName(name);
  3650. else
  3651. pData->name = pData->engine->getUniquePluginName(fRdfDescriptor->Name);
  3652. pData->filename = carla_strdup(bundle);
  3653. // ---------------------------------------------------------------
  3654. // register client
  3655. pData->client = pData->engine->addClient(this);
  3656. if (pData->client == nullptr || ! pData->client->isOk())
  3657. {
  3658. pData->engine->setLastError("Failed to register plugin client");
  3659. return false;
  3660. }
  3661. // ---------------------------------------------------------------
  3662. // initialize options
  3663. fLv2Options.minBufferSize = 1;
  3664. fLv2Options.maxBufferSize = static_cast<int>(pData->engine->getBufferSize());
  3665. fLv2Options.sampleRate = pData->engine->getSampleRate();
  3666. fLv2Options.frontendWinId = static_cast<int64_t>(pData->engine->getOptions().frontendWinId);
  3667. uint32_t eventBufferSize = MAX_DEFAULT_BUFFER_SIZE;
  3668. for (uint32_t j=0; j < fRdfDescriptor->PortCount; ++j)
  3669. {
  3670. const LV2_Property portTypes(fRdfDescriptor->Ports[j].Types);
  3671. if (LV2_IS_PORT_ATOM_SEQUENCE(portTypes) || LV2_IS_PORT_EVENT(portTypes) || LV2_IS_PORT_MIDI_LL(portTypes))
  3672. {
  3673. if (fRdfDescriptor->Ports[j].MinimumSize > eventBufferSize)
  3674. eventBufferSize = fRdfDescriptor->Ports[j].MinimumSize;
  3675. }
  3676. }
  3677. fLv2Options.sequenceSize = static_cast<int>(eventBufferSize);
  3678. // ---------------------------------------------------------------
  3679. // initialize features (part 1)
  3680. LV2_Event_Feature* const eventFt = new LV2_Event_Feature;
  3681. eventFt->callback_data = this;
  3682. eventFt->lv2_event_ref = carla_lv2_event_ref;
  3683. eventFt->lv2_event_unref = carla_lv2_event_unref;
  3684. LV2_Log_Log* const logFt = new LV2_Log_Log;
  3685. logFt->handle = this;
  3686. logFt->printf = carla_lv2_log_printf;
  3687. logFt->vprintf = carla_lv2_log_vprintf;
  3688. LV2_State_Make_Path* const stateMakePathFt = new LV2_State_Make_Path;
  3689. stateMakePathFt->handle = this;
  3690. stateMakePathFt->path = carla_lv2_state_make_path;
  3691. LV2_State_Map_Path* const stateMapPathFt = new LV2_State_Map_Path;
  3692. stateMapPathFt->handle = this;
  3693. stateMapPathFt->abstract_path = carla_lv2_state_map_abstract_path;
  3694. stateMapPathFt->absolute_path = carla_lv2_state_map_absolute_path;
  3695. LV2_Programs_Host* const programsFt = new LV2_Programs_Host;
  3696. programsFt->handle = this;
  3697. programsFt->program_changed = carla_lv2_program_changed;
  3698. LV2_Resize_Port_Resize* const rsPortFt = new LV2_Resize_Port_Resize;
  3699. rsPortFt->data = this;
  3700. rsPortFt->resize = carla_lv2_resize_port;
  3701. LV2_RtMemPool_Pool* const rtMemPoolFt = new LV2_RtMemPool_Pool;
  3702. lv2_rtmempool_init(rtMemPoolFt);
  3703. LV2_RtMemPool_Pool_Deprecated* const rtMemPoolOldFt = new LV2_RtMemPool_Pool_Deprecated;
  3704. lv2_rtmempool_init_deprecated(rtMemPoolOldFt);
  3705. LV2_URI_Map_Feature* const uriMapFt = new LV2_URI_Map_Feature;
  3706. uriMapFt->callback_data = this;
  3707. uriMapFt->uri_to_id = carla_lv2_uri_to_id;
  3708. LV2_URID_Map* const uridMapFt = new LV2_URID_Map;
  3709. uridMapFt->handle = this;
  3710. uridMapFt->map = carla_lv2_urid_map;
  3711. LV2_URID_Unmap* const uridUnmapFt = new LV2_URID_Unmap;
  3712. uridUnmapFt->handle = this;
  3713. uridUnmapFt->unmap = carla_lv2_urid_unmap;
  3714. LV2_Worker_Schedule* const workerFt = new LV2_Worker_Schedule;
  3715. workerFt->handle = this;
  3716. workerFt->schedule_work = carla_lv2_worker_schedule;
  3717. // ---------------------------------------------------------------
  3718. // initialize features (part 2)
  3719. for (uint32_t j=0; j < kFeatureCountPlugin; ++j)
  3720. fFeatures[j] = new LV2_Feature;
  3721. fFeatures[kFeatureIdBufSizeBounded]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3722. fFeatures[kFeatureIdBufSizeBounded]->data = nullptr;
  3723. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__fixedBlockLength;
  3724. fFeatures[kFeatureIdBufSizeFixed]->data = nullptr;
  3725. fFeatures[kFeatureIdBufSizePowerOf2]->URI = LV2_BUF_SIZE__powerOf2BlockLength;
  3726. fFeatures[kFeatureIdBufSizePowerOf2]->data = nullptr;
  3727. fFeatures[kFeatureIdEvent]->URI = LV2_EVENT_URI;
  3728. fFeatures[kFeatureIdEvent]->data = eventFt;
  3729. fFeatures[kFeatureIdHardRtCapable]->URI = LV2_CORE__hardRTCapable;
  3730. fFeatures[kFeatureIdHardRtCapable]->data = nullptr;
  3731. fFeatures[kFeatureIdInPlaceBroken]->URI = LV2_CORE__inPlaceBroken;
  3732. fFeatures[kFeatureIdInPlaceBroken]->data = nullptr;
  3733. fFeatures[kFeatureIdIsLive]->URI = LV2_CORE__isLive;
  3734. fFeatures[kFeatureIdIsLive]->data = nullptr;
  3735. fFeatures[kFeatureIdLogs]->URI = LV2_LOG__log;
  3736. fFeatures[kFeatureIdLogs]->data = logFt;
  3737. fFeatures[kFeatureIdOptions]->URI = LV2_OPTIONS__options;
  3738. fFeatures[kFeatureIdOptions]->data = fLv2Options.opts;
  3739. fFeatures[kFeatureIdPrograms]->URI = LV2_PROGRAMS__Host;
  3740. fFeatures[kFeatureIdPrograms]->data = programsFt;
  3741. fFeatures[kFeatureIdResizePort]->URI = LV2_RESIZE_PORT__resize;
  3742. fFeatures[kFeatureIdResizePort]->data = rsPortFt;
  3743. fFeatures[kFeatureIdRtMemPool]->URI = LV2_RTSAFE_MEMORY_POOL__Pool;
  3744. fFeatures[kFeatureIdRtMemPool]->data = rtMemPoolFt;
  3745. fFeatures[kFeatureIdRtMemPoolOld]->URI = LV2_RTSAFE_MEMORY_POOL_DEPRECATED_URI;
  3746. fFeatures[kFeatureIdRtMemPoolOld]->data = rtMemPoolOldFt;
  3747. fFeatures[kFeatureIdStateMakePath]->URI = LV2_STATE__makePath;
  3748. fFeatures[kFeatureIdStateMakePath]->data = stateMakePathFt;
  3749. fFeatures[kFeatureIdStateMapPath]->URI = LV2_STATE__mapPath;
  3750. fFeatures[kFeatureIdStateMapPath]->data = stateMapPathFt;
  3751. fFeatures[kFeatureIdStrictBounds]->URI = LV2_PORT_PROPS__supportsStrictBounds;
  3752. fFeatures[kFeatureIdStrictBounds]->data = nullptr;
  3753. fFeatures[kFeatureIdUriMap]->URI = LV2_URI_MAP_URI;
  3754. fFeatures[kFeatureIdUriMap]->data = uriMapFt;
  3755. fFeatures[kFeatureIdUridMap]->URI = LV2_URID__map;
  3756. fFeatures[kFeatureIdUridMap]->data = uridMapFt;
  3757. fFeatures[kFeatureIdUridUnmap]->URI = LV2_URID__unmap;
  3758. fFeatures[kFeatureIdUridUnmap]->data = uridUnmapFt;
  3759. fFeatures[kFeatureIdWorker]->URI = LV2_WORKER__schedule;
  3760. fFeatures[kFeatureIdWorker]->data = workerFt;
  3761. // if a fixed buffer is not needed, skip its feature
  3762. if (! needsFixedBuffer())
  3763. fFeatures[kFeatureIdBufSizeFixed]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3764. // if power of 2 is not possible, skip its feature FIXME
  3765. //if (fLv2Options.maxBufferSize)
  3766. // fFeatures[kFeatureIdBufSizePowerOf2]->URI = LV2_BUF_SIZE__boundedBlockLength;
  3767. // ---------------------------------------------------------------
  3768. // initialize plugin
  3769. try {
  3770. fHandle = fDescriptor->instantiate(fDescriptor, pData->engine->getSampleRate(), fRdfDescriptor->Bundle, fFeatures);
  3771. } catch(...) {}
  3772. if (fHandle == nullptr)
  3773. {
  3774. pData->engine->setLastError("Plugin failed to initialize");
  3775. return false;
  3776. }
  3777. // ---------------------------------------------------------------
  3778. // load plugin settings
  3779. {
  3780. // set default options
  3781. pData->options = 0x0;
  3782. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  3783. if (getMidiInCount() > 0 || needsFixedBuffer())
  3784. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  3785. if (pData->engine->getOptions().forceStereo)
  3786. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  3787. if (getMidiInCount() > 0)
  3788. {
  3789. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  3790. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  3791. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  3792. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  3793. }
  3794. // set identifier string
  3795. CarlaString identifier("LV2/");
  3796. identifier += uri;
  3797. pData->identifier = identifier.dup();
  3798. // load settings
  3799. pData->options = pData->loadSettings(pData->options, getOptionsAvailable());
  3800. // ignore settings, we need this anyway
  3801. if (getMidiInCount() > 0 || needsFixedBuffer())
  3802. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  3803. }
  3804. // ---------------------------------------------------------------
  3805. // gui stuff
  3806. if (fRdfDescriptor->UICount != 0)
  3807. initUi();
  3808. return true;
  3809. }
  3810. // -------------------------------------------------------------------
  3811. void initUi()
  3812. {
  3813. // ---------------------------------------------------------------
  3814. // find the most appropriate ui
  3815. int eQt4, eQt5, eGtk2, eGtk3, eCocoa, eWindows, eX11, eExt, iCocoa, iWindows, iX11, iExt, iFinal;
  3816. eQt4 = eQt5 = eGtk2 = eGtk3 = eCocoa = eWindows = eX11 = eExt = iCocoa = iWindows = iX11 = iExt = iFinal = -1;
  3817. #if defined(BUILD_BRIDGE)
  3818. const bool preferUiBridges(false);
  3819. #elif defined(LV2_UIS_ONLY_BRIDGES)
  3820. const bool preferUiBridges(true);
  3821. #else
  3822. const bool preferUiBridges(pData->engine->getOptions().preferUiBridges && (pData->hints & PLUGIN_IS_BRIDGE) == 0);
  3823. #endif
  3824. for (uint32_t i=0; i < fRdfDescriptor->UICount; ++i)
  3825. {
  3826. CARLA_SAFE_ASSERT_CONTINUE(fRdfDescriptor->UIs[i].URI != nullptr);
  3827. const int ii(static_cast<int>(i));
  3828. switch (fRdfDescriptor->UIs[i].Type)
  3829. {
  3830. case LV2_UI_QT4:
  3831. if (isUiBridgeable(i))
  3832. eQt4 = ii;
  3833. break;
  3834. case LV2_UI_QT5:
  3835. if (isUiBridgeable(i))
  3836. eQt5 = ii;
  3837. break;
  3838. case LV2_UI_GTK2:
  3839. if (isUiBridgeable(i))
  3840. eGtk2 = ii;
  3841. break;
  3842. case LV2_UI_GTK3:
  3843. if (isUiBridgeable(i))
  3844. eGtk3 = ii;
  3845. break;
  3846. case LV2_UI_COCOA:
  3847. if (isUiBridgeable(i) && preferUiBridges)
  3848. eCocoa = ii;
  3849. iCocoa = ii;
  3850. break;
  3851. case LV2_UI_WINDOWS:
  3852. if (isUiBridgeable(i) && preferUiBridges)
  3853. eWindows = ii;
  3854. iWindows = ii;
  3855. break;
  3856. case LV2_UI_X11:
  3857. if (isUiBridgeable(i) && preferUiBridges)
  3858. eX11 = ii;
  3859. iX11 = ii;
  3860. break;
  3861. case LV2_UI_EXTERNAL:
  3862. case LV2_UI_OLD_EXTERNAL:
  3863. if (isUiBridgeable(i))
  3864. eExt = ii;
  3865. iExt = ii;
  3866. break;
  3867. default:
  3868. break;
  3869. }
  3870. }
  3871. if (eQt4 >= 0)
  3872. iFinal = eQt4;
  3873. else if (eQt5 >= 0)
  3874. iFinal = eQt5;
  3875. else if (eGtk2 >= 0)
  3876. iFinal = eGtk2;
  3877. else if (eGtk3 >= 0)
  3878. iFinal = eGtk3;
  3879. #ifdef CARLA_OS_MAC
  3880. else if (eCocoa >= 0)
  3881. iFinal = eCocoa;
  3882. #endif
  3883. #ifdef CARLA_OS_WIN
  3884. else if (eWindows >= 0)
  3885. iFinal = eWindows;
  3886. #endif
  3887. #ifdef HAVE_X11
  3888. else if (eX11 >= 0)
  3889. iFinal = eX11;
  3890. #endif
  3891. //else if (eExt >= 0) // TODO
  3892. // iFinal = eExt;
  3893. #ifndef LV2_UIS_ONLY_BRIDGES
  3894. # ifdef CARLA_OS_MAC
  3895. else if (iCocoa >= 0)
  3896. iFinal = iCocoa;
  3897. # endif
  3898. # ifdef CARLA_OS_WIN
  3899. else if (iWindows >= 0)
  3900. iFinal = iWindows;
  3901. # endif
  3902. # ifdef HAVE_X11
  3903. else if (iX11 >= 0)
  3904. iFinal = iX11;
  3905. # endif
  3906. #endif
  3907. else if (iExt >= 0)
  3908. iFinal = iExt;
  3909. if (iFinal < 0)
  3910. {
  3911. // no suitable UI found, see if there's one which supports ui:showInterface
  3912. bool hasShowInterface = false;
  3913. for (uint32_t i=0; i < fRdfDescriptor->UICount && ! hasShowInterface; ++i)
  3914. {
  3915. LV2_RDF_UI* const ui(&fRdfDescriptor->UIs[i]);
  3916. for (uint32_t j=0; j < ui->ExtensionCount; ++j)
  3917. {
  3918. CARLA_SAFE_ASSERT_CONTINUE(ui->Extensions[j] != nullptr);
  3919. if (std::strcmp(ui->Extensions[j], LV2_UI__showInterface) != 0)
  3920. continue;
  3921. iFinal = static_cast<int>(i);
  3922. hasShowInterface = true;
  3923. break;
  3924. }
  3925. }
  3926. if (! hasShowInterface)
  3927. {
  3928. carla_stderr("Failed to find an appropriate LV2 UI for this plugin");
  3929. return;
  3930. }
  3931. }
  3932. fUi.rdfDescriptor = &fRdfDescriptor->UIs[iFinal];
  3933. // ---------------------------------------------------------------
  3934. // check supported ui features
  3935. bool canContinue = true;
  3936. bool canDelete = true;
  3937. for (uint32_t i=0; i < fUi.rdfDescriptor->FeatureCount; ++i)
  3938. {
  3939. if (! is_lv2_ui_feature_supported(fUi.rdfDescriptor->Features[i].URI))
  3940. {
  3941. carla_stderr("Plugin UI requires a feature that is not supported:\n%s", fUi.rdfDescriptor->Features[i].URI);
  3942. if (LV2_IS_FEATURE_REQUIRED(fUi.rdfDescriptor->Features[i].Type))
  3943. {
  3944. canContinue = false;
  3945. break;
  3946. }
  3947. }
  3948. if (std::strcmp(fUi.rdfDescriptor->Features[i].URI, LV2_UI__makeResident) == 0)
  3949. canDelete = false;
  3950. }
  3951. if (! canContinue)
  3952. {
  3953. fUi.rdfDescriptor = nullptr;
  3954. return;
  3955. }
  3956. // ---------------------------------------------------------------
  3957. // initialize ui according to type
  3958. const LV2_Property uiType(fUi.rdfDescriptor->Type);
  3959. if (iFinal == eQt4 || iFinal == eQt5 || iFinal == eGtk2 || iFinal == eGtk3 || iFinal == eCocoa || iFinal == eWindows || iFinal == eX11 || iFinal == eExt)
  3960. {
  3961. // -----------------------------------------------------------
  3962. // initialize osc-bridge
  3963. if (const char* const bridgeBinary = getUiBridgeBinary(uiType))
  3964. {
  3965. carla_stdout("Will use OSC-Bridge UI, binary: \"%s\"", bridgeBinary);
  3966. fUi.type = UI::TYPE_OSC;
  3967. pData->osc.thread.setOscData(bridgeBinary, fDescriptor->URI, fUi.rdfDescriptor->URI);
  3968. delete[] bridgeBinary;
  3969. return;
  3970. }
  3971. if (iFinal == eQt4 || iFinal == eQt5 || iFinal == eGtk2 || iFinal == eGtk3)
  3972. {
  3973. carla_stderr2("Failed to find UI bridge binary, cannot use UI");
  3974. fUi.rdfDescriptor = nullptr;
  3975. return;
  3976. }
  3977. }
  3978. #ifdef LV2_UIS_ONLY_BRIDGES
  3979. carla_stderr2("Failed to get an UI working, canBridge:%s", bool2str(isUiBridgeable(static_cast<uint32_t>(iFinal))));
  3980. fUi.rdfDescriptor = nullptr;
  3981. return;
  3982. #endif
  3983. // ---------------------------------------------------------------
  3984. // open UI DLL
  3985. if (! pData->uiLibOpen(fUi.rdfDescriptor->Binary, canDelete))
  3986. {
  3987. carla_stderr2("Could not load UI library, error was:\n%s", pData->libError(fUi.rdfDescriptor->Binary));
  3988. fUi.rdfDescriptor = nullptr;
  3989. return;
  3990. }
  3991. // ---------------------------------------------------------------
  3992. // get UI DLL main entry
  3993. LV2UI_DescriptorFunction uiDescFn = (LV2UI_DescriptorFunction)pData->uiLibSymbol("lv2ui_descriptor");
  3994. if (uiDescFn == nullptr)
  3995. {
  3996. carla_stderr2("Could not find the LV2UI Descriptor in the UI library");
  3997. pData->uiLibClose();
  3998. fUi.rdfDescriptor = nullptr;
  3999. return;
  4000. }
  4001. // ---------------------------------------------------------------
  4002. // get UI descriptor that matches UI URI
  4003. uint32_t i = 0;
  4004. while ((fUi.descriptor = uiDescFn(i++)))
  4005. {
  4006. if (std::strcmp(fUi.descriptor->URI, fUi.rdfDescriptor->URI) == 0)
  4007. break;
  4008. }
  4009. if (fUi.descriptor == nullptr)
  4010. {
  4011. carla_stderr2("Could not find the requested GUI in the plugin UI library");
  4012. pData->uiLibClose();
  4013. fUi.rdfDescriptor = nullptr;
  4014. return;
  4015. }
  4016. // ---------------------------------------------------------------
  4017. // check if ui is usable
  4018. switch (uiType)
  4019. {
  4020. case LV2_UI_QT4:
  4021. carla_stdout("Will use LV2 Qt4 UI, NOT!");
  4022. fUi.type = UI::TYPE_EMBED;
  4023. break;
  4024. case LV2_UI_QT5:
  4025. carla_stdout("Will use LV2 Qt5 UI, NOT!");
  4026. fUi.type = UI::TYPE_EMBED;
  4027. break;
  4028. case LV2_UI_GTK2:
  4029. carla_stdout("Will use LV2 Gtk2 UI, NOT!");
  4030. fUi.type = UI::TYPE_EMBED;
  4031. break;
  4032. case LV2_UI_GTK3:
  4033. carla_stdout("Will use LV2 Gtk3 UI, NOT!");
  4034. fUi.type = UI::TYPE_EMBED;
  4035. break;
  4036. #ifdef CARLA_OS_MAC
  4037. case LV2_UI_COCOA:
  4038. carla_stdout("Will use LV2 Cocoa UI");
  4039. fUi.type = UI::TYPE_EMBED;
  4040. break;
  4041. #endif
  4042. #ifdef CARLA_OS_WIN
  4043. case LV2_UI_WINDOWS:
  4044. carla_stdout("Will use LV2 Windows UI");
  4045. fUi.type = UI::TYPE_EMBED;
  4046. break;
  4047. #endif
  4048. case LV2_UI_X11:
  4049. #ifdef HAVE_X11
  4050. carla_stdout("Will use LV2 X11 UI");
  4051. #else
  4052. carla_stdout("Will use LV2 X11 UI, NOT!");
  4053. #endif
  4054. fUi.type = UI::TYPE_EMBED;
  4055. break;
  4056. case LV2_UI_EXTERNAL:
  4057. case LV2_UI_OLD_EXTERNAL:
  4058. carla_stdout("Will use LV2 External UI");
  4059. fUi.type = UI::TYPE_EXTERNAL;
  4060. break;
  4061. }
  4062. if (fUi.type == UI::TYPE_NULL)
  4063. {
  4064. pData->uiLibClose();
  4065. fUi.descriptor = nullptr;
  4066. fUi.rdfDescriptor = nullptr;
  4067. return;
  4068. }
  4069. // ---------------------------------------------------------------
  4070. // initialize ui data
  4071. QString guiTitle(QString("%1 (GUI)").arg(pData->name));
  4072. fLv2Options.windowTitle = carla_strdup(guiTitle.toUtf8().constData());
  4073. fLv2Options.opts[Lv2PluginOptions::WindowTitle].size = (uint32_t)std::strlen(fLv2Options.windowTitle);
  4074. fLv2Options.opts[Lv2PluginOptions::WindowTitle].value = fLv2Options.windowTitle;
  4075. // ---------------------------------------------------------------
  4076. // initialize ui features (part 1)
  4077. LV2_Extension_Data_Feature* const uiDataFt = new LV2_Extension_Data_Feature;
  4078. uiDataFt->data_access = fDescriptor->extension_data;
  4079. LV2UI_Port_Map* const uiPortMapFt = new LV2UI_Port_Map;
  4080. uiPortMapFt->handle = this;
  4081. uiPortMapFt->port_index = carla_lv2_ui_port_map;
  4082. LV2UI_Resize* const uiResizeFt = new LV2UI_Resize;
  4083. uiResizeFt->handle = this;
  4084. uiResizeFt->ui_resize = carla_lv2_ui_resize;
  4085. LV2_External_UI_Host* const uiExternalHostFt = new LV2_External_UI_Host;
  4086. uiExternalHostFt->ui_closed = carla_lv2_external_ui_closed;
  4087. uiExternalHostFt->plugin_human_id = fLv2Options.windowTitle;
  4088. // ---------------------------------------------------------------
  4089. // initialize ui features (part 2)
  4090. for (uint32_t j=kFeatureCountPlugin; j < kFeatureCountAll; ++j)
  4091. fFeatures[j] = new LV2_Feature;
  4092. fFeatures[kFeatureIdUiDataAccess]->URI = LV2_DATA_ACCESS_URI;
  4093. fFeatures[kFeatureIdUiDataAccess]->data = uiDataFt;
  4094. fFeatures[kFeatureIdUiInstanceAccess]->URI = LV2_INSTANCE_ACCESS_URI;
  4095. fFeatures[kFeatureIdUiInstanceAccess]->data = fHandle;
  4096. fFeatures[kFeatureIdUiIdleInterface]->URI = LV2_UI__idleInterface;
  4097. fFeatures[kFeatureIdUiIdleInterface]->data = nullptr;
  4098. fFeatures[kFeatureIdUiFixedSize]->URI = LV2_UI__fixedSize;
  4099. fFeatures[kFeatureIdUiFixedSize]->data = nullptr;
  4100. fFeatures[kFeatureIdUiMakeResident]->URI = LV2_UI__makeResident;
  4101. fFeatures[kFeatureIdUiMakeResident]->data = nullptr;
  4102. fFeatures[kFeatureIdUiNoUserResize]->URI = LV2_UI__noUserResize;
  4103. fFeatures[kFeatureIdUiNoUserResize]->data = nullptr;
  4104. fFeatures[kFeatureIdUiParent]->URI = LV2_UI__parent;
  4105. fFeatures[kFeatureIdUiParent]->data = nullptr;
  4106. fFeatures[kFeatureIdUiPortMap]->URI = LV2_UI__portMap;
  4107. fFeatures[kFeatureIdUiPortMap]->data = uiPortMapFt;
  4108. fFeatures[kFeatureIdUiPortSubscribe]->URI = LV2_UI__portSubscribe;
  4109. fFeatures[kFeatureIdUiPortSubscribe]->data = nullptr;
  4110. fFeatures[kFeatureIdUiResize]->URI = LV2_UI__resize;
  4111. fFeatures[kFeatureIdUiResize]->data = uiResizeFt;
  4112. fFeatures[kFeatureIdUiTouch]->URI = LV2_UI__touch;
  4113. fFeatures[kFeatureIdUiTouch]->data = nullptr;
  4114. fFeatures[kFeatureIdExternalUi]->URI = LV2_EXTERNAL_UI__Host;
  4115. fFeatures[kFeatureIdExternalUi]->data = uiExternalHostFt;
  4116. fFeatures[kFeatureIdExternalUiOld]->URI = LV2_EXTERNAL_UI_DEPRECATED_URI;
  4117. fFeatures[kFeatureIdExternalUiOld]->data = uiExternalHostFt;
  4118. // ---------------------------------------------------------------
  4119. // initialize ui extensions
  4120. if (fUi.descriptor->extension_data == nullptr)
  4121. return;
  4122. fExt.uiidle = (const LV2UI_Idle_Interface*)fUi.descriptor->extension_data(LV2_UI__idleInterface);
  4123. fExt.uishow = (const LV2UI_Show_Interface*)fUi.descriptor->extension_data(LV2_UI__showInterface);
  4124. fExt.uiprograms = (const LV2_Programs_UI_Interface*)fUi.descriptor->extension_data(LV2_PROGRAMS__UIInterface);
  4125. // check if invalid
  4126. if (fExt.uiidle != nullptr && fExt.uiidle->idle == nullptr)
  4127. fExt.uiidle = nullptr;
  4128. if (fExt.uishow != nullptr && (fExt.uishow->show == nullptr || fExt.uishow->hide == nullptr))
  4129. fExt.uishow = nullptr;
  4130. if (fExt.uiprograms != nullptr && fExt.uiprograms->select_program == nullptr)
  4131. fExt.uiprograms = nullptr;
  4132. }
  4133. // -------------------------------------------------------------------
  4134. void handleTransferAtom(const uint32_t portIndex, const LV2_Atom* const atom)
  4135. {
  4136. CARLA_SAFE_ASSERT_RETURN(atom != nullptr,);
  4137. carla_stdout("Lv2Plugin::handleTransferAtom(%i, %p)", portIndex, atom);
  4138. fAtomQueueIn.put(atom, portIndex);
  4139. }
  4140. void handleUridMap(const LV2_URID urid, const char* const uri)
  4141. {
  4142. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL,);
  4143. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0',);
  4144. carla_stdout("Lv2Plugin::handleUridMap(%i v " P_SIZE ", \"%s\")", urid, fCustomURIDs.count()-1, uri);
  4145. if (urid < fCustomURIDs.count())
  4146. {
  4147. const char* const ourURI(carla_lv2_urid_unmap(this, urid));
  4148. CARLA_SAFE_ASSERT_RETURN(ourURI != nullptr,);
  4149. if (std::strcmp(ourURI, uri) != 0)
  4150. {
  4151. carla_stderr2("PLUGIN :: wrong URI '%s' vs '%s'", ourURI, uri);
  4152. }
  4153. }
  4154. else
  4155. {
  4156. CARLA_SAFE_ASSERT_RETURN(urid == fCustomURIDs.count(),);
  4157. fCustomURIDs.append(carla_strdup(uri));
  4158. }
  4159. }
  4160. // -------------------------------------------------------------------
  4161. private:
  4162. LV2_Handle fHandle;
  4163. LV2_Handle fHandle2;
  4164. LV2_Feature* fFeatures[kFeatureCountAll+1];
  4165. const LV2_Descriptor* fDescriptor;
  4166. const LV2_RDF_Descriptor* fRdfDescriptor;
  4167. float** fAudioInBuffers;
  4168. float** fAudioOutBuffers;
  4169. float** fCvInBuffers;
  4170. float** fCvOutBuffers;
  4171. float* fParamBuffers;
  4172. Lv2AtomQueue fAtomQueueIn;
  4173. Lv2AtomQueue fAtomQueueOut;
  4174. LV2_Atom_Forge fAtomForge;
  4175. PluginCVData fCvIn;
  4176. PluginCVData fCvOut;
  4177. Lv2PluginEventData fEventsIn;
  4178. Lv2PluginEventData fEventsOut;
  4179. Lv2PluginOptions fLv2Options;
  4180. LinkedList<const char*> fCustomURIDs;
  4181. bool fFirstActive; // first process() call after activate()
  4182. EngineTimeInfo fLastTimeInfo;
  4183. struct Extensions {
  4184. const LV2_Options_Interface* options;
  4185. const LV2_State_Interface* state;
  4186. const LV2_Worker_Interface* worker;
  4187. const LV2_Programs_Interface* programs;
  4188. const LV2UI_Idle_Interface* uiidle;
  4189. const LV2UI_Show_Interface* uishow;
  4190. const LV2_Programs_UI_Interface* uiprograms;
  4191. Extensions()
  4192. : options(nullptr),
  4193. state(nullptr),
  4194. worker(nullptr),
  4195. programs(nullptr),
  4196. uiidle(nullptr),
  4197. uishow(nullptr),
  4198. uiprograms(nullptr) {}
  4199. } fExt;
  4200. struct UI {
  4201. enum Type {
  4202. TYPE_NULL,
  4203. TYPE_EMBED,
  4204. TYPE_EXTERNAL,
  4205. TYPE_OSC
  4206. };
  4207. Type type;
  4208. LV2UI_Handle handle;
  4209. LV2UI_Widget widget;
  4210. const LV2UI_Descriptor* descriptor;
  4211. const LV2_RDF_UI* rdfDescriptor;
  4212. CarlaPluginUi* window;
  4213. UI()
  4214. : type(TYPE_NULL),
  4215. handle(nullptr),
  4216. widget(nullptr),
  4217. descriptor(nullptr),
  4218. rdfDescriptor(nullptr),
  4219. window(nullptr) {}
  4220. ~UI()
  4221. {
  4222. CARLA_ASSERT(handle == nullptr);
  4223. CARLA_ASSERT(widget == nullptr);
  4224. CARLA_ASSERT(descriptor == nullptr);
  4225. CARLA_ASSERT(rdfDescriptor == nullptr);
  4226. CARLA_ASSERT(window == nullptr);
  4227. }
  4228. } fUi;
  4229. // -------------------------------------------------------------------
  4230. // Event Feature
  4231. static uint32_t carla_lv2_event_ref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  4232. {
  4233. CARLA_SAFE_ASSERT_RETURN(callback_data != nullptr, 0);
  4234. CARLA_SAFE_ASSERT_RETURN(event != nullptr, 0);
  4235. carla_debug("carla_lv2_event_ref(%p, %p)", callback_data, event);
  4236. return 0;
  4237. }
  4238. static uint32_t carla_lv2_event_unref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  4239. {
  4240. CARLA_SAFE_ASSERT_RETURN(callback_data != nullptr, 0);
  4241. CARLA_SAFE_ASSERT_RETURN(event != nullptr, 0);
  4242. carla_debug("carla_lv2_event_unref(%p, %p)", callback_data, event);
  4243. return 0;
  4244. }
  4245. // -------------------------------------------------------------------
  4246. // Logs Feature
  4247. static int carla_lv2_log_printf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, ...)
  4248. {
  4249. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  4250. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, 0);
  4251. CARLA_SAFE_ASSERT_RETURN(fmt != nullptr, 0);
  4252. #ifndef DEBUG
  4253. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  4254. return 0;
  4255. #endif
  4256. va_list args;
  4257. va_start(args, fmt);
  4258. const int ret(carla_lv2_log_vprintf(handle, type, fmt, args));
  4259. va_end(args);
  4260. return ret;
  4261. }
  4262. static int carla_lv2_log_vprintf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, va_list ap)
  4263. {
  4264. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  4265. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, 0);
  4266. CARLA_SAFE_ASSERT_RETURN(fmt != nullptr, 0);
  4267. #ifndef DEBUG
  4268. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  4269. return 0;
  4270. #endif
  4271. int ret = 0;
  4272. switch (type)
  4273. {
  4274. case CARLA_URI_MAP_ID_LOG_ERROR:
  4275. std::fprintf(stderr, "\x1b[31m");
  4276. ret = std::vfprintf(stderr, fmt, ap);
  4277. std::fprintf(stderr, "\x1b[0m");
  4278. break;
  4279. case CARLA_URI_MAP_ID_LOG_NOTE:
  4280. ret = std::vfprintf(stdout, fmt, ap);
  4281. break;
  4282. case CARLA_URI_MAP_ID_LOG_TRACE:
  4283. #ifdef DEBUG
  4284. std::fprintf(stdout, "\x1b[30;1m");
  4285. ret = std::vfprintf(stdout, fmt, ap);
  4286. std::fprintf(stdout, "\x1b[0m");
  4287. #endif
  4288. break;
  4289. case CARLA_URI_MAP_ID_LOG_WARNING:
  4290. ret = std::vfprintf(stderr, fmt, ap);
  4291. break;
  4292. default:
  4293. break;
  4294. }
  4295. return ret;
  4296. }
  4297. // -------------------------------------------------------------------
  4298. // Programs Feature
  4299. static void carla_lv2_program_changed(LV2_Programs_Handle handle, int32_t index)
  4300. {
  4301. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  4302. carla_debug("carla_lv2_program_changed(%p, %i)", handle, index);
  4303. ((Lv2Plugin*)handle)->handleProgramChanged(index);
  4304. }
  4305. // -------------------------------------------------------------------
  4306. // Resize Port Feature
  4307. static LV2_Resize_Port_Status carla_lv2_resize_port(LV2_Resize_Port_Feature_Data data, uint32_t index, size_t size)
  4308. {
  4309. CARLA_SAFE_ASSERT_RETURN(data != nullptr, LV2_RESIZE_PORT_ERR_UNKNOWN);
  4310. carla_debug("carla_lv2_program_changed(%p, %i, " P_SIZE ")", data, index, size);
  4311. return ((Lv2Plugin*)data)->handleResizePort(index, size);
  4312. }
  4313. // -------------------------------------------------------------------
  4314. // State Feature
  4315. static char* carla_lv2_state_make_path(LV2_State_Make_Path_Handle handle, const char* path)
  4316. {
  4317. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4318. CARLA_SAFE_ASSERT_RETURN(path != nullptr && path[0] != '\0', nullptr);
  4319. carla_debug("carla_lv2_state_make_path(%p, \"%s\")", handle, path);
  4320. QDir dir;
  4321. dir.mkpath(path);
  4322. return strdup(path);
  4323. }
  4324. static char* carla_lv2_state_map_abstract_path(LV2_State_Map_Path_Handle handle, const char* absolute_path)
  4325. {
  4326. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4327. CARLA_SAFE_ASSERT_RETURN(absolute_path != nullptr && absolute_path[0] != '\0', nullptr);
  4328. carla_debug("carla_lv2_state_map_abstract_path(%p, \"%s\")", handle, absolute_path);
  4329. QDir dir(absolute_path);
  4330. return strdup(dir.canonicalPath().toUtf8().constData());
  4331. }
  4332. static char* carla_lv2_state_map_absolute_path(LV2_State_Map_Path_Handle handle, const char* abstract_path)
  4333. {
  4334. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4335. CARLA_SAFE_ASSERT_RETURN(abstract_path != nullptr && abstract_path[0] != '\0', nullptr);
  4336. carla_debug("carla_lv2_state_map_absolute_path(%p, \"%s\")", handle, abstract_path);
  4337. QDir dir(abstract_path);
  4338. return strdup(dir.absolutePath().toUtf8().constData());
  4339. }
  4340. static LV2_State_Status carla_lv2_state_store(LV2_State_Handle handle, uint32_t key, const void* value, size_t size, uint32_t type, uint32_t flags)
  4341. {
  4342. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_STATE_ERR_UNKNOWN);
  4343. carla_debug("carla_lv2_state_store(%p, %i, %p, " P_SIZE ", %i, %i)", handle, key, value, size, type, flags);
  4344. return ((Lv2Plugin*)handle)->handleStateStore(key, value, size, type, flags);
  4345. }
  4346. static const void* carla_lv2_state_retrieve(LV2_State_Handle handle, uint32_t key, size_t* size, uint32_t* type, uint32_t* flags)
  4347. {
  4348. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4349. carla_debug("carla_lv2_state_retrieve(%p, %i, %p, %p, %p)", handle, key, size, type, flags);
  4350. return ((Lv2Plugin*)handle)->handleStateRetrieve(key, size, type, flags);
  4351. }
  4352. // -------------------------------------------------------------------
  4353. // URI-Map Feature
  4354. static uint32_t carla_lv2_uri_to_id(LV2_URI_Map_Callback_Data data, const char* map, const char* uri)
  4355. {
  4356. carla_debug("carla_lv2_uri_to_id(%p, \"%s\", \"%s\")", data, map, uri);
  4357. return carla_lv2_urid_map((LV2_URID_Map_Handle*)data, uri);
  4358. // unused
  4359. (void)map;
  4360. }
  4361. // -------------------------------------------------------------------
  4362. // URID Feature
  4363. static LV2_URID carla_lv2_urid_map(LV2_URID_Map_Handle handle, const char* uri)
  4364. {
  4365. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, CARLA_URI_MAP_ID_NULL);
  4366. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', CARLA_URI_MAP_ID_NULL);
  4367. carla_debug("carla_lv2_urid_map(%p, \"%s\")", handle, uri);
  4368. // Atom types
  4369. if (std::strcmp(uri, LV2_ATOM__Blank) == 0)
  4370. return CARLA_URI_MAP_ID_ATOM_BLANK;
  4371. if (std::strcmp(uri, LV2_ATOM__Bool) == 0)
  4372. return CARLA_URI_MAP_ID_ATOM_BOOL;
  4373. if (std::strcmp(uri, LV2_ATOM__Chunk) == 0)
  4374. return CARLA_URI_MAP_ID_ATOM_CHUNK;
  4375. if (std::strcmp(uri, LV2_ATOM__Double) == 0)
  4376. return CARLA_URI_MAP_ID_ATOM_DOUBLE;
  4377. if (std::strcmp(uri, LV2_ATOM__Event) == 0)
  4378. return CARLA_URI_MAP_ID_ATOM_EVENT;
  4379. if (std::strcmp(uri, LV2_ATOM__Float) == 0)
  4380. return CARLA_URI_MAP_ID_ATOM_FLOAT;
  4381. if (std::strcmp(uri, LV2_ATOM__Int) == 0)
  4382. return CARLA_URI_MAP_ID_ATOM_INT;
  4383. if (std::strcmp(uri, LV2_ATOM__Literal) == 0)
  4384. return CARLA_URI_MAP_ID_ATOM_LITERAL;
  4385. if (std::strcmp(uri, LV2_ATOM__Long) == 0)
  4386. return CARLA_URI_MAP_ID_ATOM_LONG;
  4387. if (std::strcmp(uri, LV2_ATOM__Number) == 0)
  4388. return CARLA_URI_MAP_ID_ATOM_NUMBER;
  4389. if (std::strcmp(uri, LV2_ATOM__Object) == 0)
  4390. return CARLA_URI_MAP_ID_ATOM_OBJECT;
  4391. if (std::strcmp(uri, LV2_ATOM__Path) == 0)
  4392. return CARLA_URI_MAP_ID_ATOM_PATH;
  4393. if (std::strcmp(uri, LV2_ATOM__Property) == 0)
  4394. return CARLA_URI_MAP_ID_ATOM_PROPERTY;
  4395. if (std::strcmp(uri, LV2_ATOM__Resource) == 0)
  4396. return CARLA_URI_MAP_ID_ATOM_RESOURCE;
  4397. if (std::strcmp(uri, LV2_ATOM__Sequence) == 0)
  4398. return CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  4399. if (std::strcmp(uri, LV2_ATOM__Sound) == 0)
  4400. return CARLA_URI_MAP_ID_ATOM_SOUND;
  4401. if (std::strcmp(uri, LV2_ATOM__String) == 0)
  4402. return CARLA_URI_MAP_ID_ATOM_STRING;
  4403. if (std::strcmp(uri, LV2_ATOM__Tuple) == 0)
  4404. return CARLA_URI_MAP_ID_ATOM_TUPLE;
  4405. if (std::strcmp(uri, LV2_ATOM__URI) == 0)
  4406. return CARLA_URI_MAP_ID_ATOM_URI;
  4407. if (std::strcmp(uri, LV2_ATOM__URID) == 0)
  4408. return CARLA_URI_MAP_ID_ATOM_URID;
  4409. if (std::strcmp(uri, LV2_ATOM__Vector) == 0)
  4410. return CARLA_URI_MAP_ID_ATOM_VECTOR;
  4411. if (std::strcmp(uri, LV2_ATOM__atomTransfer) == 0)
  4412. return CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM;
  4413. if (std::strcmp(uri, LV2_ATOM__eventTransfer) == 0)
  4414. return CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT;
  4415. // BufSize types
  4416. if (std::strcmp(uri, LV2_BUF_SIZE__maxBlockLength) == 0)
  4417. return CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  4418. if (std::strcmp(uri, LV2_BUF_SIZE__minBlockLength) == 0)
  4419. return CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  4420. if (std::strcmp(uri, LV2_BUF_SIZE__sequenceSize) == 0)
  4421. return CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  4422. // Log types
  4423. if (std::strcmp(uri, LV2_LOG__Error) == 0)
  4424. return CARLA_URI_MAP_ID_LOG_ERROR;
  4425. if (std::strcmp(uri, LV2_LOG__Note) == 0)
  4426. return CARLA_URI_MAP_ID_LOG_NOTE;
  4427. if (std::strcmp(uri, LV2_LOG__Trace) == 0)
  4428. return CARLA_URI_MAP_ID_LOG_TRACE;
  4429. if (std::strcmp(uri, LV2_LOG__Warning) == 0)
  4430. return CARLA_URI_MAP_ID_LOG_WARNING;
  4431. // Time types
  4432. if (std::strcmp(uri, LV2_TIME__Position) == 0)
  4433. return CARLA_URI_MAP_ID_TIME_POSITION;
  4434. if (std::strcmp(uri, LV2_TIME__bar) == 0)
  4435. return CARLA_URI_MAP_ID_TIME_BAR;
  4436. if (std::strcmp(uri, LV2_TIME__barBeat) == 0)
  4437. return CARLA_URI_MAP_ID_TIME_BAR_BEAT;
  4438. if (std::strcmp(uri, LV2_TIME__beat) == 0)
  4439. return CARLA_URI_MAP_ID_TIME_BEAT;
  4440. if (std::strcmp(uri, LV2_TIME__beatUnit) == 0)
  4441. return CARLA_URI_MAP_ID_TIME_BEAT_UNIT;
  4442. if (std::strcmp(uri, LV2_TIME__beatsPerBar) == 0)
  4443. return CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR;
  4444. if (std::strcmp(uri, LV2_TIME__beatsPerMinute) == 0)
  4445. return CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE;
  4446. if (std::strcmp(uri, LV2_TIME__frame) == 0)
  4447. return CARLA_URI_MAP_ID_TIME_FRAME;
  4448. if (std::strcmp(uri, LV2_TIME__framesPerSecond) == 0)
  4449. return CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND;
  4450. if (std::strcmp(uri, LV2_TIME__speed) == 0)
  4451. return CARLA_URI_MAP_ID_TIME_SPEED;
  4452. // Others
  4453. if (std::strcmp(uri, LV2_MIDI__MidiEvent) == 0)
  4454. return CARLA_URI_MAP_ID_MIDI_EVENT;
  4455. if (std::strcmp(uri, LV2_PARAMETERS__sampleRate) == 0)
  4456. return CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  4457. if (std::strcmp(uri, LV2_UI__windowTitle) == 0)
  4458. return CARLA_URI_MAP_ID_UI_WINDOW_TITLE;
  4459. // Custom
  4460. if (std::strcmp(uri, URI_CARLA_ATOM_WORKER) == 0)
  4461. return CARLA_URI_MAP_ID_CARLA_ATOM_WORKER;
  4462. if (std::strcmp(uri, URI_CARLA_FRONTEND_WIN_ID) == 0)
  4463. return CARLA_URI_MAP_ID_CARLA_FRONTEND_WIN_ID;
  4464. // Custom types
  4465. return ((Lv2Plugin*)handle)->getCustomURID(uri);
  4466. }
  4467. static const char* carla_lv2_urid_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
  4468. {
  4469. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  4470. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL, nullptr);
  4471. carla_debug("carla_lv2_urid_unmap(%p, %i)", handle, urid);
  4472. // Atom types
  4473. if (urid == CARLA_URI_MAP_ID_ATOM_BLANK)
  4474. return LV2_ATOM__Blank;
  4475. if (urid == CARLA_URI_MAP_ID_ATOM_BOOL)
  4476. return LV2_ATOM__Bool;
  4477. if (urid == CARLA_URI_MAP_ID_ATOM_CHUNK)
  4478. return LV2_ATOM__Chunk;
  4479. if (urid == CARLA_URI_MAP_ID_ATOM_DOUBLE)
  4480. return LV2_ATOM__Double;
  4481. if (urid == CARLA_URI_MAP_ID_ATOM_EVENT)
  4482. return LV2_ATOM__Event;
  4483. if (urid == CARLA_URI_MAP_ID_ATOM_FLOAT)
  4484. return LV2_ATOM__Float;
  4485. if (urid == CARLA_URI_MAP_ID_ATOM_INT)
  4486. return LV2_ATOM__Int;
  4487. if (urid == CARLA_URI_MAP_ID_ATOM_LITERAL)
  4488. return LV2_ATOM__Literal;
  4489. if (urid == CARLA_URI_MAP_ID_ATOM_LONG)
  4490. return LV2_ATOM__Long;
  4491. if (urid == CARLA_URI_MAP_ID_ATOM_NUMBER)
  4492. return LV2_ATOM__Number;
  4493. if (urid == CARLA_URI_MAP_ID_ATOM_OBJECT)
  4494. return LV2_ATOM__Object;
  4495. if (urid == CARLA_URI_MAP_ID_ATOM_PATH)
  4496. return LV2_ATOM__Path;
  4497. if (urid == CARLA_URI_MAP_ID_ATOM_PROPERTY)
  4498. return LV2_ATOM__Property;
  4499. if (urid == CARLA_URI_MAP_ID_ATOM_RESOURCE)
  4500. return LV2_ATOM__Resource;
  4501. if (urid == CARLA_URI_MAP_ID_ATOM_SEQUENCE)
  4502. return LV2_ATOM__Sequence;
  4503. if (urid == CARLA_URI_MAP_ID_ATOM_SOUND)
  4504. return LV2_ATOM__Sound;
  4505. if (urid == CARLA_URI_MAP_ID_ATOM_STRING)
  4506. return LV2_ATOM__String;
  4507. if (urid == CARLA_URI_MAP_ID_ATOM_TUPLE)
  4508. return LV2_ATOM__Tuple;
  4509. if (urid == CARLA_URI_MAP_ID_ATOM_URI)
  4510. return LV2_ATOM__URI;
  4511. if (urid == CARLA_URI_MAP_ID_ATOM_URID)
  4512. return LV2_ATOM__URID;
  4513. if (urid == CARLA_URI_MAP_ID_ATOM_VECTOR)
  4514. return LV2_ATOM__Vector;
  4515. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  4516. return LV2_ATOM__atomTransfer;
  4517. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  4518. return LV2_ATOM__eventTransfer;
  4519. // BufSize types
  4520. if (urid == CARLA_URI_MAP_ID_BUF_MAX_LENGTH)
  4521. return LV2_BUF_SIZE__maxBlockLength;
  4522. if (urid == CARLA_URI_MAP_ID_BUF_MIN_LENGTH)
  4523. return LV2_BUF_SIZE__minBlockLength;
  4524. if (urid == CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE)
  4525. return LV2_BUF_SIZE__sequenceSize;
  4526. // Log types
  4527. if (urid == CARLA_URI_MAP_ID_LOG_ERROR)
  4528. return LV2_LOG__Error;
  4529. if (urid == CARLA_URI_MAP_ID_LOG_NOTE)
  4530. return LV2_LOG__Note;
  4531. if (urid == CARLA_URI_MAP_ID_LOG_TRACE)
  4532. return LV2_LOG__Trace;
  4533. if (urid == CARLA_URI_MAP_ID_LOG_WARNING)
  4534. return LV2_LOG__Warning;
  4535. // Time types
  4536. if (urid == CARLA_URI_MAP_ID_TIME_POSITION)
  4537. return LV2_TIME__Position;
  4538. if (urid == CARLA_URI_MAP_ID_TIME_BAR)
  4539. return LV2_TIME__bar;
  4540. if (urid == CARLA_URI_MAP_ID_TIME_BAR_BEAT)
  4541. return LV2_TIME__barBeat;
  4542. if (urid == CARLA_URI_MAP_ID_TIME_BEAT)
  4543. return LV2_TIME__beat;
  4544. if (urid == CARLA_URI_MAP_ID_TIME_BEAT_UNIT)
  4545. return LV2_TIME__beatUnit;
  4546. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR)
  4547. return LV2_TIME__beatsPerBar;
  4548. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE)
  4549. return LV2_TIME__beatsPerMinute;
  4550. if (urid == CARLA_URI_MAP_ID_TIME_FRAME)
  4551. return LV2_TIME__frame;
  4552. if (urid == CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND)
  4553. return LV2_TIME__framesPerSecond;
  4554. if (urid == CARLA_URI_MAP_ID_TIME_SPEED)
  4555. return LV2_TIME__speed;
  4556. // Others
  4557. if (urid == CARLA_URI_MAP_ID_MIDI_EVENT)
  4558. return LV2_MIDI__MidiEvent;
  4559. if (urid == CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE)
  4560. return LV2_PARAMETERS__sampleRate;
  4561. if (urid == CARLA_URI_MAP_ID_UI_WINDOW_TITLE)
  4562. return LV2_UI__windowTitle;
  4563. // Custom
  4564. if (urid == CARLA_URI_MAP_ID_CARLA_ATOM_WORKER)
  4565. return URI_CARLA_ATOM_WORKER;
  4566. if (urid == CARLA_URI_MAP_ID_CARLA_FRONTEND_WIN_ID)
  4567. return URI_CARLA_FRONTEND_WIN_ID;
  4568. // Custom types
  4569. return ((Lv2Plugin*)handle)->getCustomURIDString(urid);
  4570. }
  4571. // -------------------------------------------------------------------
  4572. // Worker Feature
  4573. static LV2_Worker_Status carla_lv2_worker_schedule(LV2_Worker_Schedule_Handle handle, uint32_t size, const void* data)
  4574. {
  4575. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_WORKER_ERR_UNKNOWN);
  4576. carla_debug("carla_lv2_worker_schedule(%p, %i, %p)", handle, size, data);
  4577. return ((Lv2Plugin*)handle)->handleWorkerSchedule(size, data);
  4578. }
  4579. static LV2_Worker_Status carla_lv2_worker_respond(LV2_Worker_Respond_Handle handle, uint32_t size, const void* data)
  4580. {
  4581. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2_WORKER_ERR_UNKNOWN);
  4582. carla_debug("carla_lv2_worker_respond(%p, %i, %p)", handle, size, data);
  4583. return ((Lv2Plugin*)handle)->handleWorkerRespond(size, data);
  4584. }
  4585. // -------------------------------------------------------------------
  4586. // External UI Feature
  4587. static void carla_lv2_external_ui_closed(LV2UI_Controller controller)
  4588. {
  4589. CARLA_SAFE_ASSERT_RETURN(controller != nullptr,);
  4590. carla_debug("carla_lv2_external_ui_closed(%p)", controller);
  4591. ((Lv2Plugin*)controller)->handleExternalUiClosed();
  4592. }
  4593. // -------------------------------------------------------------------
  4594. // UI Port-Map Feature
  4595. static uint32_t carla_lv2_ui_port_map(LV2UI_Feature_Handle handle, const char* symbol)
  4596. {
  4597. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2UI_INVALID_PORT_INDEX);
  4598. carla_debug("carla_lv2_ui_port_map(%p, \"%s\")", handle, symbol);
  4599. return ((Lv2Plugin*)handle)->handleUiPortMap(symbol);
  4600. }
  4601. // -------------------------------------------------------------------
  4602. // UI Resize Feature
  4603. static int carla_lv2_ui_resize(LV2UI_Feature_Handle handle, int width, int height)
  4604. {
  4605. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 1);
  4606. carla_debug("carla_lv2_ui_resize(%p, %i, %i)", handle, width, height);
  4607. return ((Lv2Plugin*)handle)->handleUiResize(width, height);
  4608. }
  4609. // -------------------------------------------------------------------
  4610. // UI Extension
  4611. static void carla_lv2_ui_write_function(LV2UI_Controller controller, uint32_t port_index, uint32_t buffer_size, uint32_t format, const void* buffer)
  4612. {
  4613. CARLA_SAFE_ASSERT_RETURN(controller != nullptr,);
  4614. carla_debug("carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller, port_index, buffer_size, format, buffer);
  4615. ((Lv2Plugin*)controller)->handleUiWrite(port_index, buffer_size, format, buffer);
  4616. }
  4617. // -------------------------------------------------------------------
  4618. // Lilv State
  4619. static void carla_lilv_set_port_value(const char* port_symbol, void* user_data, const void* value, uint32_t size, uint32_t type)
  4620. {
  4621. CARLA_SAFE_ASSERT_RETURN(user_data != nullptr,);
  4622. carla_debug("carla_lilv_set_port_value(\"%s\", %p, %p, %i, %i", port_symbol, user_data, value, size, type);
  4623. ((Lv2Plugin*)user_data)->handleLilvSetPortValue(port_symbol, value, size, type);
  4624. }
  4625. // -------------------------------------------------------------------
  4626. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Lv2Plugin)
  4627. };
  4628. // -------------------------------------------------------------------------------------------------------------------
  4629. #define lv2PluginPtr ((Lv2Plugin*)plugin)
  4630. int CarlaEngineOsc::handleMsgLv2AtomTransfer(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  4631. {
  4632. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "is");
  4633. carla_debug("CarlaOsc::handleMsgLv2AtomTransfer()");
  4634. const int32_t portIndex = argv[0]->i;
  4635. const char* const atomBuf = (const char*)&argv[1]->s;
  4636. CARLA_SAFE_ASSERT_RETURN(portIndex >= 0, 0);
  4637. QByteArray chunk(QByteArray::fromBase64(atomBuf));
  4638. CARLA_SAFE_ASSERT_RETURN(chunk.size() > 0, 0);
  4639. const LV2_Atom* const atom((const LV2_Atom*)chunk.constData());
  4640. lv2PluginPtr->handleTransferAtom(static_cast<uint32_t>(portIndex), atom);
  4641. return 0;
  4642. }
  4643. int CarlaEngineOsc::handleMsgLv2UridMap(CARLA_ENGINE_OSC_HANDLE_ARGS2)
  4644. {
  4645. CARLA_ENGINE_OSC_CHECK_OSC_TYPES(2, "is");
  4646. carla_debug("CarlaOsc::handleMsgLv2UridMap()");
  4647. const int32_t urid = argv[0]->i;
  4648. const char* const uri = (const char*)&argv[1]->s;
  4649. CARLA_SAFE_ASSERT_RETURN(urid > 0, 0);
  4650. lv2PluginPtr->handleUridMap(static_cast<LV2_URID>(urid), uri);
  4651. return 0;
  4652. }
  4653. #undef lv2PluginPtr
  4654. CARLA_BACKEND_END_NAMESPACE
  4655. #endif // WANT_LV2
  4656. // -------------------------------------------------------------------------------------------------------------------
  4657. CARLA_BACKEND_START_NAMESPACE
  4658. CarlaPlugin* CarlaPlugin::newLV2(const Initializer& init)
  4659. {
  4660. carla_debug("CarlaPlugin::newLV2({%p, \"%s\", \"%s\", " P_INT64 "})", init.engine, init.name, init.label, init.uniqueId);
  4661. #ifdef WANT_LV2
  4662. Lv2Plugin* const plugin(new Lv2Plugin(init.engine, init.id));
  4663. if (! plugin->init(init.filename, init.name, init.label))
  4664. {
  4665. delete plugin;
  4666. return nullptr;
  4667. }
  4668. plugin->reload();
  4669. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  4670. {
  4671. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo LV2 plugins, sorry!");
  4672. delete plugin;
  4673. return nullptr;
  4674. }
  4675. return plugin;
  4676. #else
  4677. init.engine->setLastError("LV2 support not available");
  4678. return nullptr;
  4679. #endif
  4680. }
  4681. CARLA_BACKEND_END_NAMESPACE
  4682. // -------------------------------------------------------------------------------------------------------------------