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.

1116 lines
38KB

  1. /*
  2. * Carla Bridge UI, LV2 version
  3. * Copyright (C) 2011-2013 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 GPL.txt file
  16. */
  17. #include "CarlaBridgeClient.hpp"
  18. #include "CarlaLv2Utils.hpp"
  19. #include "CarlaMIDI.h"
  20. #include "RtList.hpp"
  21. #include <QtCore/QDir>
  22. extern "C" {
  23. #include "rtmempool/rtmempool-lv2.h"
  24. }
  25. // -----------------------------------------------------
  26. // Our LV2 World class object
  27. Lv2WorldClass gLv2World;
  28. CARLA_BRIDGE_START_NAMESPACE
  29. // -------------------------------------------------------------------------
  30. // fake values
  31. static uint32_t gBufferSize = 1024;
  32. static double gSampleRate = 44100.0;
  33. // static max values
  34. const unsigned int MAX_EVENT_BUFFER = 8192; // 0x2000
  35. // LV2 URI Map Ids
  36. const uint32_t CARLA_URI_MAP_ID_NULL = 0;
  37. const uint32_t CARLA_URI_MAP_ID_ATOM_BLANK = 1;
  38. const uint32_t CARLA_URI_MAP_ID_ATOM_BOOL = 2;
  39. const uint32_t CARLA_URI_MAP_ID_ATOM_CHUNK = 3;
  40. const uint32_t CARLA_URI_MAP_ID_ATOM_DOUBLE = 4;
  41. const uint32_t CARLA_URI_MAP_ID_ATOM_FLOAT = 5;
  42. const uint32_t CARLA_URI_MAP_ID_ATOM_INT = 6;
  43. const uint32_t CARLA_URI_MAP_ID_ATOM_LITERAL = 7;
  44. const uint32_t CARLA_URI_MAP_ID_ATOM_LONG = 8;
  45. const uint32_t CARLA_URI_MAP_ID_ATOM_PATH = 9;
  46. const uint32_t CARLA_URI_MAP_ID_ATOM_PROPERTY = 10;
  47. const uint32_t CARLA_URI_MAP_ID_ATOM_RESOURCE = 11;
  48. const uint32_t CARLA_URI_MAP_ID_ATOM_SEQUENCE = 12;
  49. const uint32_t CARLA_URI_MAP_ID_ATOM_STRING = 13;
  50. const uint32_t CARLA_URI_MAP_ID_ATOM_TUPLE = 14;
  51. const uint32_t CARLA_URI_MAP_ID_ATOM_URI = 15;
  52. const uint32_t CARLA_URI_MAP_ID_ATOM_URID = 16;
  53. const uint32_t CARLA_URI_MAP_ID_ATOM_VECTOR = 17;
  54. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM = 18;
  55. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT = 19;
  56. const uint32_t CARLA_URI_MAP_ID_BUF_MAX_LENGTH = 20;
  57. const uint32_t CARLA_URI_MAP_ID_BUF_MIN_LENGTH = 21;
  58. const uint32_t CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE = 22;
  59. const uint32_t CARLA_URI_MAP_ID_LOG_ERROR = 23;
  60. const uint32_t CARLA_URI_MAP_ID_LOG_NOTE = 24;
  61. const uint32_t CARLA_URI_MAP_ID_LOG_TRACE = 25;
  62. const uint32_t CARLA_URI_MAP_ID_LOG_WARNING = 26;
  63. const uint32_t CARLA_URI_MAP_ID_TIME_POSITION = 27; // base type
  64. const uint32_t CARLA_URI_MAP_ID_TIME_BAR = 28; // values
  65. const uint32_t CARLA_URI_MAP_ID_TIME_BAR_BEAT = 29;
  66. const uint32_t CARLA_URI_MAP_ID_TIME_BEAT = 30;
  67. const uint32_t CARLA_URI_MAP_ID_TIME_BEAT_UNIT = 31;
  68. const uint32_t CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR = 32;
  69. const uint32_t CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE = 33;
  70. const uint32_t CARLA_URI_MAP_ID_TIME_FRAME = 34;
  71. const uint32_t CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND = 35;
  72. const uint32_t CARLA_URI_MAP_ID_TIME_SPEED = 36;
  73. const uint32_t CARLA_URI_MAP_ID_MIDI_EVENT = 37;
  74. const uint32_t CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE = 38;
  75. const uint32_t CARLA_URI_MAP_ID_COUNT = 39;
  76. // LV2 Feature Ids
  77. const uint32_t kFeatureIdBufSizeBounded = 0;
  78. const uint32_t kFeatureIdBufSizeFixed = 1;
  79. const uint32_t kFeatureIdBufSizePowerOf2 = 2;
  80. const uint32_t kFeatureIdEvent = 3;
  81. const uint32_t kFeatureIdHardRtCapable = 4;
  82. const uint32_t kFeatureIdInPlaceBroken = 5;
  83. const uint32_t kFeatureIdIsLive = 6;
  84. const uint32_t kFeatureIdLogs = 7;
  85. const uint32_t kFeatureIdOptions = 8;
  86. const uint32_t kFeatureIdPrograms = 9;
  87. const uint32_t kFeatureIdRtMemPool = 10;
  88. const uint32_t kFeatureIdStateMakePath = 11;
  89. const uint32_t kFeatureIdStateMapPath = 12;
  90. const uint32_t kFeatureIdStrictBounds = 13;
  91. const uint32_t kFeatureIdUriMap = 14;
  92. const uint32_t kFeatureIdUridMap = 15;
  93. const uint32_t kFeatureIdUridUnmap = 16;
  94. const uint32_t kFeatureIdWorker = 17;
  95. const uint32_t kFeatureIdUiDataAccess = 18;
  96. const uint32_t kFeatureIdUiInstanceAccess = 19;
  97. const uint32_t kFeatureIdUiIdle = 20;
  98. const uint32_t kFeatureIdUiFixedSize = 21;
  99. const uint32_t kFeatureIdUiMakeResident = 22;
  100. const uint32_t kFeatureIdUiNoUserResize = 23;
  101. const uint32_t kFeatureIdUiParent = 24;
  102. const uint32_t kFeatureIdUiPortMap = 25;
  103. const uint32_t kFeatureIdUiPortSubscribe = 26;
  104. const uint32_t kFeatureIdUiResize = 27;
  105. const uint32_t kFeatureIdUiTouch = 28;
  106. const uint32_t kFeatureIdExternalUi = 29;
  107. const uint32_t kFeatureIdExternalUiOld = 30;
  108. const uint32_t kFeatureCount = 31;
  109. // -------------------------------------------------------------------------
  110. struct Lv2PluginOptions {
  111. int maxBufferSize;
  112. int minBufferSize;
  113. int sequenceSize;
  114. double sampleRate;
  115. LV2_Options_Option optMaxBlockLenth;
  116. LV2_Options_Option optMinBlockLenth;
  117. LV2_Options_Option optSequenceSize;
  118. LV2_Options_Option optSampleRate;
  119. LV2_Options_Option optNull;
  120. LV2_Options_Option* opts[5];
  121. Lv2PluginOptions()
  122. : maxBufferSize(0),
  123. minBufferSize(0),
  124. sequenceSize(MAX_EVENT_BUFFER),
  125. sampleRate(0.0)
  126. {
  127. optMaxBlockLenth.context = LV2_OPTIONS_INSTANCE;
  128. optMaxBlockLenth.subject = 0;
  129. optMaxBlockLenth.key = CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  130. optMaxBlockLenth.size = sizeof(int);
  131. optMaxBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  132. optMaxBlockLenth.value = &maxBufferSize;
  133. optMinBlockLenth.context = LV2_OPTIONS_INSTANCE;
  134. optMinBlockLenth.subject = 0;
  135. optMinBlockLenth.key = CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  136. optMinBlockLenth.size = sizeof(int);
  137. optMinBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  138. optMinBlockLenth.value = &minBufferSize;
  139. optSequenceSize.context = LV2_OPTIONS_INSTANCE;
  140. optSequenceSize.subject = 0;
  141. optSequenceSize.key = CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  142. optSequenceSize.size = sizeof(int);
  143. optSequenceSize.type = CARLA_URI_MAP_ID_ATOM_INT;
  144. optSequenceSize.value = &sequenceSize;
  145. optSampleRate.context = LV2_OPTIONS_INSTANCE;
  146. optSampleRate.subject = 0;
  147. optSampleRate.key = CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  148. optSampleRate.size = sizeof(double);
  149. optSampleRate.type = CARLA_URI_MAP_ID_ATOM_DOUBLE;
  150. optSampleRate.value = &sampleRate;
  151. optNull.context = LV2_OPTIONS_INSTANCE;
  152. optNull.subject = 0;
  153. optNull.key = CARLA_URI_MAP_ID_NULL;
  154. optNull.size = 0;
  155. optNull.type = CARLA_URI_MAP_ID_NULL;
  156. optNull.value = nullptr;
  157. opts[0] = &optMinBlockLenth;
  158. opts[1] = &optMaxBlockLenth;
  159. opts[2] = &optSequenceSize;
  160. opts[3] = &optSampleRate;
  161. opts[4] = &optNull;
  162. }
  163. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(Lv2PluginOptions)
  164. };
  165. // -------------------------------------------------------------------------
  166. class CarlaLv2Client : public CarlaBridgeClient
  167. {
  168. public:
  169. CarlaLv2Client(const char* const uiTitle)
  170. : CarlaBridgeClient(uiTitle)
  171. {
  172. handle = nullptr;
  173. widget = nullptr;
  174. descriptor = nullptr;
  175. rdf_descriptor = nullptr;
  176. rdf_ui_descriptor = nullptr;
  177. programs = nullptr;
  178. #ifdef BRIDGE_LV2_X11
  179. m_resizable = false;
  180. #else
  181. m_resizable = true;
  182. #endif
  183. for (uint32_t i=0; i < CARLA_URI_MAP_ID_COUNT; ++i)
  184. customURIDs.push_back(nullptr);
  185. for (uint32_t i=0; i < kFeatureCount+1; ++i)
  186. features[i] = nullptr;
  187. // -----------------------------------------------------------------
  188. // initialize features
  189. LV2_Event_Feature* const eventFt = new LV2_Event_Feature;
  190. eventFt->callback_data = this;
  191. eventFt->lv2_event_ref = carla_lv2_event_ref;
  192. eventFt->lv2_event_unref = carla_lv2_event_unref;
  193. LV2_Log_Log* const logFt = new LV2_Log_Log;
  194. logFt->handle = this;
  195. logFt->printf = carla_lv2_log_printf;
  196. logFt->vprintf = carla_lv2_log_vprintf;
  197. LV2_Programs_Host* const programsFt = new LV2_Programs_Host;
  198. programsFt->handle = this;
  199. programsFt->program_changed = carla_lv2_program_changed;
  200. LV2_RtMemPool_Pool* const rtMemPoolFt = new LV2_RtMemPool_Pool;
  201. lv2_rtmempool_init(rtMemPoolFt);
  202. LV2_State_Make_Path* const stateMakePathFt = new LV2_State_Make_Path;
  203. stateMakePathFt->handle = this;
  204. stateMakePathFt->path = carla_lv2_state_make_path;
  205. LV2_State_Map_Path* const stateMapPathFt = new LV2_State_Map_Path;
  206. stateMapPathFt->handle = this;
  207. stateMapPathFt->abstract_path = carla_lv2_state_map_abstract_path;
  208. stateMapPathFt->absolute_path = carla_lv2_state_map_absolute_path;
  209. LV2_URI_Map_Feature* const uriMapFt = new LV2_URI_Map_Feature;
  210. uriMapFt->callback_data = this;
  211. uriMapFt->uri_to_id = carla_lv2_uri_to_id;
  212. LV2_URID_Map* const uridMapFt = new LV2_URID_Map;
  213. uridMapFt->handle = this;
  214. uridMapFt->map = carla_lv2_urid_map;
  215. LV2_URID_Unmap* const uridUnmapFt = new LV2_URID_Unmap;
  216. uridUnmapFt->handle = this;
  217. uridUnmapFt->unmap = carla_lv2_urid_unmap;
  218. LV2UI_Port_Map* const uiPortMapFt = new LV2UI_Port_Map;
  219. uiPortMapFt->handle = this;
  220. uiPortMapFt->port_index = carla_lv2_ui_port_map;
  221. LV2UI_Resize* const uiResizeFt = new LV2UI_Resize;
  222. uiResizeFt->handle = this;
  223. uiResizeFt->ui_resize = carla_lv2_ui_resize;
  224. #if 0
  225. features[lv2_feature_id_bufsize_bounded] = new LV2_Feature;
  226. features[lv2_feature_id_bufsize_bounded]->URI = LV2_BUF_SIZE__boundedBlockLength;
  227. features[lv2_feature_id_bufsize_bounded]->data = nullptr;
  228. features[lv2_feature_id_bufsize_fixed] = new LV2_Feature;
  229. features[lv2_feature_id_bufsize_fixed]->URI = LV2_BUF_SIZE__fixedBlockLength;
  230. features[lv2_feature_id_bufsize_fixed]->data = nullptr;
  231. features[lv2_feature_id_bufsize_powerof2] = new LV2_Feature;
  232. features[lv2_feature_id_bufsize_powerof2]->URI = LV2_BUF_SIZE__powerOf2BlockLength;
  233. features[lv2_feature_id_bufsize_powerof2]->data = nullptr;
  234. features[lv2_feature_id_event] = new LV2_Feature;
  235. features[lv2_feature_id_event]->URI = LV2_EVENT_URI;
  236. features[lv2_feature_id_event]->data = eventFt;
  237. features[lv2_feature_id_logs] = new LV2_Feature;
  238. features[lv2_feature_id_logs]->URI = LV2_LOG__log;
  239. features[lv2_feature_id_logs]->data = logFt;
  240. features[lv2_feature_id_options] = new LV2_Feature;
  241. features[lv2_feature_id_options]->URI = LV2_OPTIONS__options;
  242. features[lv2_feature_id_options]->data = optionsFt;
  243. features[lv2_feature_id_programs] = new LV2_Feature;
  244. features[lv2_feature_id_programs]->URI = LV2_PROGRAMS__Host;
  245. features[lv2_feature_id_programs]->data = programsFt;
  246. features[lv2_feature_id_rtmempool] = new LV2_Feature;
  247. features[lv2_feature_id_rtmempool]->URI = LV2_RTSAFE_MEMORY_POOL__Pool;
  248. features[lv2_feature_id_rtmempool]->data = rtMemPoolFt;
  249. features[lv2_feature_id_state_make_path] = new LV2_Feature;
  250. features[lv2_feature_id_state_make_path]->URI = LV2_STATE__makePath;
  251. features[lv2_feature_id_state_make_path]->data = stateMakePathFt;
  252. features[lv2_feature_id_state_map_path] = new LV2_Feature;
  253. features[lv2_feature_id_state_map_path]->URI = LV2_STATE__mapPath;
  254. features[lv2_feature_id_state_map_path]->data = stateMapPathFt;
  255. features[lv2_feature_id_strict_bounds] = new LV2_Feature;
  256. features[lv2_feature_id_strict_bounds]->URI = LV2_PORT_PROPS__supportsStrictBounds;
  257. features[lv2_feature_id_strict_bounds]->data = nullptr;
  258. features[lv2_feature_id_uri_map] = new LV2_Feature;
  259. features[lv2_feature_id_uri_map]->URI = LV2_URI_MAP_URI;
  260. features[lv2_feature_id_uri_map]->data = uriMapFt;
  261. features[lv2_feature_id_urid_map] = new LV2_Feature;
  262. features[lv2_feature_id_urid_map]->URI = LV2_URID__map;
  263. features[lv2_feature_id_urid_map]->data = uridMapFt;
  264. features[lv2_feature_id_urid_unmap] = new LV2_Feature;
  265. features[lv2_feature_id_urid_unmap]->URI = LV2_URID__unmap;
  266. features[lv2_feature_id_urid_unmap]->data = uridUnmapFt;
  267. features[lv2_feature_id_ui_parent] = new LV2_Feature;
  268. features[lv2_feature_id_ui_parent]->URI = LV2_UI__parent;
  269. features[lv2_feature_id_ui_parent]->data = nullptr;
  270. features[lv2_feature_id_ui_port_map] = new LV2_Feature;
  271. features[lv2_feature_id_ui_port_map]->URI = LV2_UI__portMap;
  272. features[lv2_feature_id_ui_port_map]->data = uiPortMapFt;
  273. features[lv2_feature_id_ui_resize] = new LV2_Feature;
  274. features[lv2_feature_id_ui_resize]->URI = LV2_UI__resize;
  275. features[lv2_feature_id_ui_resize]->data = uiResizeFt;
  276. #endif
  277. }
  278. ~CarlaLv2Client()
  279. {
  280. if (rdf_descriptor)
  281. delete rdf_descriptor;
  282. #if 0
  283. const LV2_Options_Option* const options = (const LV2_Options_Option*)features[lv2_feature_id_options]->data;
  284. delete[] options;
  285. delete (LV2_Event_Feature*)features[lv2_feature_id_event]->data;
  286. delete (LV2_Log_Log*)features[lv2_feature_id_logs]->data;
  287. delete (LV2_Programs_Host*)features[lv2_feature_id_programs]->data;
  288. delete (LV2_RtMemPool_Pool*)features[lv2_feature_id_rtmempool]->data;
  289. delete (LV2_State_Make_Path*)features[lv2_feature_id_state_make_path]->data;
  290. delete (LV2_State_Map_Path*)features[lv2_feature_id_state_map_path]->data;
  291. delete (LV2_URI_Map_Feature*)features[lv2_feature_id_uri_map]->data;
  292. delete (LV2_URID_Map*)features[lv2_feature_id_urid_map]->data;
  293. delete (LV2_URID_Unmap*)features[lv2_feature_id_urid_unmap]->data;
  294. delete (LV2UI_Port_Map*)features[lv2_feature_id_ui_port_map]->data;
  295. delete (LV2UI_Resize*)features[lv2_feature_id_ui_resize]->data;
  296. #endif
  297. for (uint32_t i=0; i < kFeatureCount; ++i)
  298. {
  299. if (features[i] != nullptr)
  300. delete features[i];
  301. }
  302. for (size_t i=0; i < customURIDs.size(); ++i)
  303. {
  304. if (customURIDs[i])
  305. free((void*)customURIDs[i]);
  306. }
  307. customURIDs.clear();
  308. }
  309. // ---------------------------------------------------------------------
  310. // ui initialization
  311. bool uiInit(const char* pluginURI, const char* uiURI)
  312. {
  313. // -----------------------------------------------------------------
  314. // init
  315. CarlaBridgeClient::uiInit(pluginURI, uiURI);
  316. // -----------------------------------------------------------------
  317. // get plugin from lv2_rdf (lilv)
  318. gLv2World.init();
  319. rdf_descriptor = lv2_rdf_new(pluginURI);
  320. if (! rdf_descriptor)
  321. return false;
  322. // -----------------------------------------------------------------
  323. // find requested UI
  324. for (uint32_t i=0; i < rdf_descriptor->UICount; ++i)
  325. {
  326. if (std::strcmp(rdf_descriptor->UIs[i].URI, uiURI) == 0)
  327. {
  328. rdf_ui_descriptor = &rdf_descriptor->UIs[i];
  329. break;
  330. }
  331. }
  332. if (! rdf_ui_descriptor)
  333. return false;
  334. // -----------------------------------------------------------------
  335. // open DLL
  336. if (! uiLibOpen(rdf_ui_descriptor->Binary))
  337. return false;
  338. // -----------------------------------------------------------------
  339. // get DLL main entry
  340. const LV2UI_DescriptorFunction ui_descFn = (LV2UI_DescriptorFunction)uiLibSymbol("lv2ui_descriptor");
  341. if (! ui_descFn)
  342. return false;
  343. // -----------------------------------------------------------
  344. // get descriptor that matches URI
  345. uint32_t i = 0;
  346. while ((descriptor = ui_descFn(i++)))
  347. {
  348. if (std::strcmp(descriptor->URI, uiURI) == 0)
  349. break;
  350. }
  351. if (! descriptor)
  352. return false;
  353. // -----------------------------------------------------------
  354. // initialize UI
  355. #ifdef BRIDGE_LV2_X11
  356. //features[lv2_feature_id_ui_parent]->data = getContainerId();
  357. #endif
  358. handle = descriptor->instantiate(descriptor, pluginURI, rdf_ui_descriptor->Bundle, carla_lv2_ui_write_function, this, &widget, features);
  359. if (! handle)
  360. return false;
  361. // -----------------------------------------------------------
  362. // check if not resizable
  363. #ifndef BRIDGE_LV2_X11
  364. for (uint32_t i=0; i < rdf_ui_descriptor->FeatureCount; ++i)
  365. {
  366. if (std::strcmp(rdf_ui_descriptor->Features[i].URI, LV2_UI__fixedSize) == 0 || std::strcmp(rdf_ui_descriptor->Features[i].URI, LV2_UI__noUserResize) == 0)
  367. {
  368. m_resizable = false;
  369. break;
  370. }
  371. }
  372. #endif
  373. // -----------------------------------------------------------
  374. // check for known extensions
  375. for (uint32_t i=0; descriptor->extension_data && i < rdf_ui_descriptor->ExtensionCount; ++i)
  376. {
  377. if (std::strcmp(rdf_ui_descriptor->Extensions[i], LV2_PROGRAMS__UIInterface) == 0)
  378. {
  379. programs = (LV2_Programs_UI_Interface*)descriptor->extension_data(LV2_PROGRAMS__UIInterface);
  380. if (programs && ! programs->select_program)
  381. // invalid
  382. programs = nullptr;
  383. break;
  384. }
  385. }
  386. return true;
  387. }
  388. void uiClose()
  389. {
  390. CarlaBridgeClient::uiClose();
  391. if (handle && descriptor && descriptor->cleanup)
  392. descriptor->cleanup(handle);
  393. uiLibClose();
  394. }
  395. // ---------------------------------------------------------------------
  396. // ui management
  397. void* getWidget() const
  398. {
  399. return widget;
  400. }
  401. bool isResizable() const
  402. {
  403. return m_resizable;
  404. }
  405. bool needsReparent() const
  406. {
  407. #ifdef BRIDGE_LV2_X11
  408. return true;
  409. #else
  410. return false;
  411. #endif
  412. }
  413. // ---------------------------------------------------------------------
  414. // processing
  415. void setParameter(const int32_t rindex, const float value)
  416. {
  417. CARLA_ASSERT(handle && descriptor);
  418. if (handle && descriptor && descriptor->port_event)
  419. {
  420. float fvalue = value;
  421. descriptor->port_event(handle, rindex, sizeof(float), 0, &fvalue);
  422. }
  423. }
  424. void setProgram(const uint32_t)
  425. {
  426. }
  427. void setMidiProgram(const uint32_t bank, const uint32_t program)
  428. {
  429. CARLA_ASSERT(handle);
  430. if (handle && programs)
  431. programs->select_program(handle, bank, program);
  432. }
  433. void noteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  434. {
  435. CARLA_ASSERT(handle && descriptor);
  436. if (handle && descriptor && descriptor->port_event)
  437. {
  438. LV2_Atom_MidiEvent midiEv;
  439. midiEv.event.time.frames = 0;
  440. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  441. midiEv.event.body.size = 3;
  442. midiEv.data[0] = MIDI_STATUS_NOTE_ON + channel;
  443. midiEv.data[1] = note;
  444. midiEv.data[2] = velo;
  445. descriptor->port_event(handle, 0, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  446. }
  447. }
  448. void noteOff(const uint8_t channel, const uint8_t note)
  449. {
  450. CARLA_ASSERT(handle && descriptor);
  451. if (handle && descriptor && descriptor->port_event)
  452. {
  453. LV2_Atom_MidiEvent midiEv;
  454. midiEv.event.time.frames = 0;
  455. midiEv.event.body.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  456. midiEv.event.body.size = 3;
  457. midiEv.data[0] = MIDI_STATUS_NOTE_OFF + channel;
  458. midiEv.data[1] = note;
  459. midiEv.data[2] = 0;
  460. descriptor->port_event(handle, 0, 3, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  461. }
  462. }
  463. // ---------------------------------------------------------------------
  464. uint32_t getCustomURID(const char* const uri)
  465. {
  466. carla_debug("CarlaLv2Client::getCustomURID(%s)", uri);
  467. CARLA_ASSERT(uri);
  468. if (! uri)
  469. return CARLA_URI_MAP_ID_NULL;
  470. for (size_t i=0; i < customURIDs.size(); ++i)
  471. {
  472. if (customURIDs[i] && std::strcmp(customURIDs[i], uri) == 0)
  473. return i;
  474. }
  475. customURIDs.push_back(strdup(uri));
  476. return customURIDs.size()-1;
  477. }
  478. const char* getCustomURIString(const LV2_URID urid) const
  479. {
  480. carla_debug("CarlaLv2Client::getCustomURIString(%i)", urid);
  481. CARLA_ASSERT(urid > CARLA_URI_MAP_ID_NULL);
  482. if (urid == CARLA_URI_MAP_ID_NULL)
  483. return nullptr;
  484. if (urid < customURIDs.size())
  485. return customURIDs[urid];
  486. return nullptr;
  487. }
  488. // ---------------------------------------------------------------------
  489. void handleAtomTransfer(const uint32_t portIndex, const LV2_Atom* const atom)
  490. {
  491. CARLA_ASSERT(atom != nullptr);
  492. carla_debug("CarlaLv2Client::handleTransferEvent(%i, %p)", portIndex, atom);
  493. if (atom != nullptr && handle != nullptr && descriptor != nullptr && descriptor->port_event != nullptr)
  494. descriptor->port_event(handle, portIndex, lv2_atom_total_size(atom), CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, atom);
  495. }
  496. void handleUridMap(const LV2_URID urid, const char* const uri)
  497. {
  498. CARLA_ASSERT(urid != CARLA_URI_MAP_ID_NULL);
  499. CARLA_ASSERT(uri != nullptr);
  500. carla_debug("CarlaLv2Client::handleUridMap(%i, \"%s\")", urid, uri);
  501. // TODO
  502. }
  503. // ---------------------------------------------------------------------
  504. void handleProgramChanged(int32_t /*index*/)
  505. {
  506. if (isOscControlRegistered())
  507. sendOscConfigure("reloadprograms", "");
  508. }
  509. uint32_t handleUiPortMap(const char* const symbol)
  510. {
  511. CARLA_ASSERT(symbol);
  512. if (! symbol)
  513. return LV2UI_INVALID_PORT_INDEX;
  514. for (uint32_t i=0; i < rdf_descriptor->PortCount; ++i)
  515. {
  516. if (std::strcmp(rdf_descriptor->Ports[i].Symbol, symbol) == 0)
  517. return i;
  518. }
  519. return LV2UI_INVALID_PORT_INDEX;
  520. }
  521. int handleUiResize(int width, int height)
  522. {
  523. CARLA_ASSERT(width > 0);
  524. CARLA_ASSERT(height > 0);
  525. if (width <= 0 || height <= 0)
  526. return 1;
  527. toolkitResize(width, height);
  528. return 0;
  529. }
  530. void handleUiWrite(uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer)
  531. {
  532. if (buffer == nullptr || ! isOscControlRegistered())
  533. return;
  534. if (format == 0)
  535. {
  536. CARLA_ASSERT(bufferSize == sizeof(float));
  537. if (bufferSize != sizeof(float))
  538. return;
  539. const float value(*(const float*)buffer);
  540. sendOscControl(portIndex, value);
  541. }
  542. else if (format == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM || CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  543. {
  544. CARLA_ASSERT(bufferSize != 0);
  545. if (bufferSize == 0)
  546. return;
  547. QByteArray chunk((const char*)buffer, bufferSize);
  548. sendOscLv2AtomTransfer(portIndex, chunk.toBase64().constData());
  549. }
  550. }
  551. // ----------------- Event Feature ---------------------------------------------------
  552. static uint32_t carla_lv2_event_ref(const LV2_Event_Callback_Data callback_data, LV2_Event* const event)
  553. {
  554. carla_debug("CarlaLv2Client::carla_lv2_event_ref(%p, %p)", callback_data, event);
  555. CARLA_ASSERT(callback_data);
  556. CARLA_ASSERT(event);
  557. return 0;
  558. }
  559. static uint32_t carla_lv2_event_unref(const LV2_Event_Callback_Data callback_data, LV2_Event* const event)
  560. {
  561. carla_debug("CarlaLv2Client::carla_lv2_event_unref(%p, %p)", callback_data, event);
  562. CARLA_ASSERT(callback_data);
  563. CARLA_ASSERT(event);
  564. return 0;
  565. }
  566. // ----------------- Logs Feature ----------------------------------------------------
  567. static int carla_lv2_log_printf(const LV2_Log_Handle handle, const LV2_URID type, const char* const fmt, ...)
  568. {
  569. carla_debug("CarlaLv2Client::carla_lv2_log_printf(%p, %i, %s, ...)", handle, type, fmt);
  570. CARLA_ASSERT(handle);
  571. CARLA_ASSERT(type > 0);
  572. #ifndef DEBUG
  573. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  574. return 0;
  575. #endif
  576. va_list args;
  577. va_start(args, fmt);
  578. const int ret = carla_lv2_log_vprintf(handle, type, fmt, args);
  579. va_end(args);
  580. return ret;
  581. }
  582. static int carla_lv2_log_vprintf(const LV2_Log_Handle handle, const LV2_URID type, const char* const fmt, va_list ap)
  583. {
  584. carla_debug("CarlaLv2Client::carla_lv2_log_vprintf(%p, %i, %s, ...)", handle, type, fmt);
  585. CARLA_ASSERT(handle);
  586. CARLA_ASSERT(type > 0);
  587. #ifndef DEBUG
  588. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  589. return 0;
  590. #endif
  591. char buf[8196];
  592. vsprintf(buf, fmt, ap);
  593. if (*buf == 0)
  594. return 0;
  595. switch (type)
  596. {
  597. case CARLA_URI_MAP_ID_LOG_ERROR:
  598. qCritical("%s", buf);
  599. break;
  600. case CARLA_URI_MAP_ID_LOG_NOTE:
  601. printf("%s\n", buf);
  602. break;
  603. case CARLA_URI_MAP_ID_LOG_TRACE:
  604. carla_debug("%s", buf);
  605. break;
  606. case CARLA_URI_MAP_ID_LOG_WARNING:
  607. carla_stderr("%s", buf);
  608. break;
  609. default:
  610. break;
  611. }
  612. return std::strlen(buf);
  613. }
  614. // ----------------- Programs Feature ------------------------------------------------
  615. static void carla_lv2_program_changed(const LV2_Programs_Handle handle, const int32_t index)
  616. {
  617. carla_debug("CarlaLv2Client::carla_lv2_program_changed(%p, %i)", handle, index);
  618. CARLA_ASSERT(handle);
  619. if (! handle)
  620. return;
  621. CarlaLv2Client* const client = (CarlaLv2Client*)handle;
  622. client->handleProgramChanged(index);
  623. }
  624. // ----------------- State Feature ---------------------------------------------------
  625. static char* carla_lv2_state_make_path(const LV2_State_Make_Path_Handle handle, const char* const path)
  626. {
  627. carla_debug("CarlaLv2Client::carla_lv2_state_make_path(%p, %p)", handle, path);
  628. CARLA_ASSERT(handle);
  629. CARLA_ASSERT(path);
  630. if (! path)
  631. return nullptr;
  632. QDir dir;
  633. dir.mkpath(path);
  634. return strdup(path);
  635. }
  636. static char* carla_lv2_state_map_abstract_path(const LV2_State_Map_Path_Handle handle, const char* const absolute_path)
  637. {
  638. carla_debug("CarlaLv2Client::carla_lv2_state_map_abstract_path(%p, %p)", handle, absolute_path);
  639. CARLA_ASSERT(handle);
  640. CARLA_ASSERT(absolute_path);
  641. if (! absolute_path)
  642. return nullptr;
  643. QDir dir(absolute_path);
  644. return strdup(dir.canonicalPath().toUtf8().constData());
  645. }
  646. static char* carla_lv2_state_map_absolute_path(const LV2_State_Map_Path_Handle handle, const char* const abstract_path)
  647. {
  648. carla_debug("CarlaLv2Client::carla_lv2_state_map_absolute_path(%p, %p)", handle, abstract_path);
  649. CARLA_ASSERT(handle);
  650. CARLA_ASSERT(abstract_path);
  651. if (! abstract_path)
  652. return nullptr;
  653. QDir dir(abstract_path);
  654. return strdup(dir.absolutePath().toUtf8().constData());
  655. }
  656. // ----------------- URI-Map Feature ---------------------------------------
  657. static uint32_t carla_lv2_uri_to_id(const LV2_URI_Map_Callback_Data data, const char* const map, const char* const uri)
  658. {
  659. carla_debug("CarlaLv2Client::carla_lv2_uri_to_id(%p, %s, %s)", data, map, uri);
  660. return carla_lv2_urid_map((LV2_URID_Map_Handle*)data, uri);
  661. }
  662. // ----------------- URID Feature ------------------------------------------
  663. static LV2_URID carla_lv2_urid_map(const LV2_URID_Map_Handle handle, const char* const uri)
  664. {
  665. carla_debug("CarlaLv2Client::carla_lv2_urid_map(%p, %s)", handle, uri);
  666. CARLA_ASSERT(handle);
  667. CARLA_ASSERT(uri);
  668. if (! uri)
  669. return CARLA_URI_MAP_ID_NULL;
  670. // Atom types
  671. if (std::strcmp(uri, LV2_ATOM__Blank) == 0)
  672. return CARLA_URI_MAP_ID_ATOM_BLANK;
  673. if (std::strcmp(uri, LV2_ATOM__Chunk) == 0)
  674. return CARLA_URI_MAP_ID_ATOM_CHUNK;
  675. if (std::strcmp(uri, LV2_ATOM__Double) == 0)
  676. return CARLA_URI_MAP_ID_ATOM_DOUBLE;
  677. if (std::strcmp(uri, LV2_ATOM__Int) == 0)
  678. return CARLA_URI_MAP_ID_ATOM_INT;
  679. if (std::strcmp(uri, LV2_ATOM__Path) == 0)
  680. return CARLA_URI_MAP_ID_ATOM_PATH;
  681. if (std::strcmp(uri, LV2_ATOM__Sequence) == 0)
  682. return CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  683. if (std::strcmp(uri, LV2_ATOM__String) == 0)
  684. return CARLA_URI_MAP_ID_ATOM_STRING;
  685. if (std::strcmp(uri, LV2_ATOM__atomTransfer) == 0)
  686. return CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM;
  687. if (std::strcmp(uri, LV2_ATOM__eventTransfer) == 0)
  688. return CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT;
  689. // BufSize types
  690. if (std::strcmp(uri, LV2_BUF_SIZE__maxBlockLength) == 0)
  691. return CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  692. if (std::strcmp(uri, LV2_BUF_SIZE__minBlockLength) == 0)
  693. return CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  694. if (std::strcmp(uri, LV2_BUF_SIZE__sequenceSize) == 0)
  695. return CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  696. // Log types
  697. if (std::strcmp(uri, LV2_LOG__Error) == 0)
  698. return CARLA_URI_MAP_ID_LOG_ERROR;
  699. if (std::strcmp(uri, LV2_LOG__Note) == 0)
  700. return CARLA_URI_MAP_ID_LOG_NOTE;
  701. if (std::strcmp(uri, LV2_LOG__Trace) == 0)
  702. return CARLA_URI_MAP_ID_LOG_TRACE;
  703. if (std::strcmp(uri, LV2_LOG__Warning) == 0)
  704. return CARLA_URI_MAP_ID_LOG_WARNING;
  705. // Others
  706. if (std::strcmp(uri, LV2_MIDI__MidiEvent) == 0)
  707. return CARLA_URI_MAP_ID_MIDI_EVENT;
  708. if (std::strcmp(uri, LV2_PARAMETERS__sampleRate) == 0)
  709. return CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  710. if (! handle)
  711. return CARLA_URI_MAP_ID_NULL;
  712. // Custom types
  713. CarlaLv2Client* const client = (CarlaLv2Client*)handle;
  714. return client->getCustomURID(uri);
  715. }
  716. static const char* carla_lv2_urid_unmap(const LV2_URID_Map_Handle handle, const LV2_URID urid)
  717. {
  718. carla_debug("CarlaLv2Client::carla_lv2_urid_unmap(%p, %i)", handle, urid);
  719. CARLA_ASSERT(handle);
  720. CARLA_ASSERT(urid > CARLA_URI_MAP_ID_NULL);
  721. if (urid == CARLA_URI_MAP_ID_NULL)
  722. return nullptr;
  723. // Atom types
  724. if (urid == CARLA_URI_MAP_ID_ATOM_BLANK)
  725. return LV2_ATOM__Blank;
  726. if (urid == CARLA_URI_MAP_ID_ATOM_CHUNK)
  727. return LV2_ATOM__Chunk;
  728. if (urid == CARLA_URI_MAP_ID_ATOM_DOUBLE)
  729. return LV2_ATOM__Double;
  730. if (urid == CARLA_URI_MAP_ID_ATOM_INT)
  731. return LV2_ATOM__Int;
  732. if (urid == CARLA_URI_MAP_ID_ATOM_PATH)
  733. return LV2_ATOM__Path;
  734. if (urid == CARLA_URI_MAP_ID_ATOM_SEQUENCE)
  735. return LV2_ATOM__Sequence;
  736. if (urid == CARLA_URI_MAP_ID_ATOM_STRING)
  737. return LV2_ATOM__String;
  738. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  739. return LV2_ATOM__atomTransfer;
  740. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  741. return LV2_ATOM__eventTransfer;
  742. // BufSize types
  743. if (urid == CARLA_URI_MAP_ID_BUF_MAX_LENGTH)
  744. return LV2_BUF_SIZE__maxBlockLength;
  745. if (urid == CARLA_URI_MAP_ID_BUF_MIN_LENGTH)
  746. return LV2_BUF_SIZE__minBlockLength;
  747. if (urid == CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE)
  748. return LV2_BUF_SIZE__sequenceSize;
  749. // Log types
  750. if (urid == CARLA_URI_MAP_ID_LOG_ERROR)
  751. return LV2_LOG__Error;
  752. if (urid == CARLA_URI_MAP_ID_LOG_NOTE)
  753. return LV2_LOG__Note;
  754. if (urid == CARLA_URI_MAP_ID_LOG_TRACE)
  755. return LV2_LOG__Trace;
  756. if (urid == CARLA_URI_MAP_ID_LOG_WARNING)
  757. return LV2_LOG__Warning;
  758. // Others
  759. if (urid == CARLA_URI_MAP_ID_MIDI_EVENT)
  760. return LV2_MIDI__MidiEvent;
  761. if (urid == CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE)
  762. return LV2_PARAMETERS__sampleRate;
  763. if (! handle)
  764. return nullptr;
  765. // Custom types
  766. CarlaLv2Client* const client = (CarlaLv2Client*)handle;
  767. return client->getCustomURIString(urid);
  768. }
  769. // ----------------- UI Port-Map Feature ---------------------------------------------
  770. static uint32_t carla_lv2_ui_port_map(const LV2UI_Feature_Handle handle, const char* const symbol)
  771. {
  772. carla_debug("CarlaLv2Client::carla_lv2_ui_port_map(%p, %s)", handle, symbol);
  773. CARLA_ASSERT(handle);
  774. if (! handle)
  775. return LV2UI_INVALID_PORT_INDEX;
  776. CarlaLv2Client* const client = (CarlaLv2Client*)handle;
  777. return client->handleUiPortMap(symbol);
  778. }
  779. // ----------------- UI Resize Feature -------------------------------------
  780. static int carla_lv2_ui_resize(const LV2UI_Feature_Handle handle, const int width, const int height)
  781. {
  782. carla_debug("CarlaLv2Client::carla_lv2_ui_resize(%p, %i, %i)", handle, width, height);
  783. CARLA_ASSERT(handle);
  784. if (! handle)
  785. return 1;
  786. CarlaLv2Client* const client = (CarlaLv2Client*)handle;
  787. return client->handleUiResize(width, height);
  788. }
  789. // ----------------- UI Extension ------------------------------------------
  790. static void carla_lv2_ui_write_function(const LV2UI_Controller controller, const uint32_t port_index, const uint32_t buffer_size, const uint32_t format, const void* const buffer)
  791. {
  792. carla_debug("CarlaLv2Client::carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller, port_index, buffer_size, format, buffer);
  793. CARLA_ASSERT(controller);
  794. if (! controller)
  795. return;
  796. CarlaLv2Client* const client = (CarlaLv2Client*)controller;
  797. client->handleUiWrite(port_index, buffer_size, format, buffer);
  798. }
  799. private:
  800. LV2UI_Handle handle;
  801. LV2UI_Widget widget;
  802. const LV2UI_Descriptor* descriptor;
  803. LV2_Feature* features[kFeatureCount+1];
  804. const LV2_RDF_Descriptor* rdf_descriptor;
  805. const LV2_RDF_UI* rdf_ui_descriptor;
  806. const LV2_Programs_UI_Interface* programs;
  807. bool m_resizable;
  808. std::vector<const char*> customURIDs;
  809. };
  810. #define lv2ClientPtr ((CarlaLv2Client*)kClient)
  811. int CarlaBridgeOsc::handleMsgLv2AtomTransfer(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  812. {
  813. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "is");
  814. carla_debug("CarlaBridgeOsc::handleMsgLv2AtomTransfer()");
  815. if (kClient == nullptr)
  816. return 1;
  817. const int32_t portIndex = argv[0]->i;
  818. const char* const atomBuf = (const char*)&argv[1]->s;
  819. if (portIndex < 0)
  820. return 0;
  821. QByteArray chunk;
  822. chunk = QByteArray::fromBase64(atomBuf);
  823. LV2_Atom* const atom = (LV2_Atom*)chunk.constData();
  824. lv2ClientPtr->handleAtomTransfer(portIndex, atom);
  825. return 0;
  826. }
  827. int CarlaBridgeOsc::handleMsgLv2UridMap(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  828. {
  829. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "is");
  830. carla_debug("CarlaBridgeOsc::handleMsgLv2UridMap()");
  831. if (kClient == nullptr)
  832. return 1;
  833. const int32_t urid = argv[0]->i;
  834. const char* const uri = (const char*)&argv[1]->s;
  835. if (urid <= 0)
  836. return 0;
  837. lv2ClientPtr->handleUridMap(urid, uri);
  838. return 0;
  839. }
  840. #undef lv2ClientPtr
  841. CARLA_BRIDGE_END_NAMESPACE
  842. int main(int argc, char* argv[])
  843. {
  844. CARLA_BRIDGE_USE_NAMESPACE
  845. if (argc != 5)
  846. {
  847. carla_stderr("usage: %s <osc-url|\"null\"> <plugin-uri> <ui-uri> <ui-title>", argv[0]);
  848. return 1;
  849. }
  850. const char* oscUrl = argv[1];
  851. const char* pluginURI = argv[2];
  852. const char* uiURI = argv[3];
  853. const char* uiTitle = argv[4];
  854. const bool useOsc = std::strcmp(oscUrl, "null");
  855. // try to get sampleRate value
  856. if (const char* const sampleRateStr = getenv("CARLA_SAMPLE_RATE"))
  857. gSampleRate = atof(sampleRateStr);
  858. // Init LV2 client
  859. CarlaLv2Client client(uiTitle);
  860. // Init OSC
  861. if (useOsc)
  862. client.oscInit(oscUrl);
  863. // Load UI
  864. int ret;
  865. if (client.uiInit(pluginURI, uiURI))
  866. {
  867. client.toolkitExec(!useOsc);
  868. ret = 0;
  869. }
  870. else
  871. {
  872. qCritical("Failed to load LV2 UI");
  873. ret = 1;
  874. }
  875. // Close OSC
  876. if (useOsc)
  877. client.oscClose();
  878. // Close LV2 client
  879. client.uiClose();
  880. return ret;
  881. }