Collection of tools useful for audio production
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.

1132 lines
38KB

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