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.

6003 lines
227KB

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