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.

2780 lines
99KB

  1. /*
  2. * Carla Native Plugin
  3. * Copyright (C) 2012-2019 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. #include "CarlaPluginInternal.hpp"
  18. #include "CarlaEngine.hpp"
  19. #include "CarlaMathUtils.hpp"
  20. #include "CarlaNative.h"
  21. #include "water/text/StringArray.h"
  22. using water::jmax;
  23. using water::String;
  24. using water::StringArray;
  25. // -----------------------------------------------------------------------
  26. // used in carla-base.cpp
  27. std::size_t carla_getNativePluginCount() noexcept;
  28. const NativePluginDescriptor* carla_getNativePluginDescriptor(const std::size_t index) noexcept;
  29. // -----------------------------------------------------------------------
  30. static LinkedList<const NativePluginDescriptor*> gPluginDescriptors;
  31. void carla_register_native_plugin(const NativePluginDescriptor* desc)
  32. {
  33. gPluginDescriptors.append(desc);
  34. }
  35. // -----------------------------------------------------------------------
  36. static
  37. class NativePluginInitializer
  38. {
  39. public:
  40. NativePluginInitializer() noexcept
  41. : fNeedsInit(true) {}
  42. ~NativePluginInitializer() noexcept
  43. {
  44. gPluginDescriptors.clear();
  45. }
  46. void initIfNeeded() noexcept
  47. {
  48. if (! fNeedsInit)
  49. return;
  50. fNeedsInit = false;
  51. try {
  52. carla_register_all_native_plugins();
  53. } CARLA_SAFE_EXCEPTION("carla_register_all_native_plugins")
  54. }
  55. private:
  56. bool fNeedsInit;
  57. } sPluginInitializer;
  58. // -----------------------------------------------------------------------
  59. std::size_t carla_getNativePluginCount() noexcept
  60. {
  61. sPluginInitializer.initIfNeeded();
  62. return gPluginDescriptors.count();
  63. }
  64. const NativePluginDescriptor* carla_getNativePluginDescriptor(const std::size_t index) noexcept
  65. {
  66. sPluginInitializer.initIfNeeded();
  67. return gPluginDescriptors.getAt(index, nullptr);
  68. }
  69. // -----------------------------------------------------------------------
  70. CARLA_BACKEND_START_NAMESPACE
  71. // -------------------------------------------------------------------
  72. // Fallback data
  73. static const CustomData kCustomDataFallback = { nullptr, nullptr, nullptr };
  74. static const EngineEvent kNullEngineEvent = { kEngineEventTypeNull, 0, 0, {} };
  75. // -----------------------------------------------------------------------
  76. struct NativePluginMidiOutData {
  77. uint32_t count;
  78. uint32_t* indexes;
  79. CarlaEngineEventPort** ports;
  80. NativePluginMidiOutData() noexcept
  81. : count(0),
  82. indexes(nullptr),
  83. ports(nullptr) {}
  84. ~NativePluginMidiOutData() noexcept
  85. {
  86. CARLA_SAFE_ASSERT_INT(count == 0, count);
  87. CARLA_SAFE_ASSERT(indexes == nullptr);
  88. CARLA_SAFE_ASSERT(ports == nullptr);
  89. }
  90. bool createNew(const uint32_t newCount)
  91. {
  92. CARLA_SAFE_ASSERT_INT(count == 0, count);
  93. CARLA_SAFE_ASSERT_RETURN(indexes == nullptr, false);
  94. CARLA_SAFE_ASSERT_RETURN(ports == nullptr, false);
  95. CARLA_SAFE_ASSERT_RETURN(newCount > 0, false);
  96. indexes = new uint32_t[newCount];
  97. ports = new CarlaEngineEventPort*[newCount];
  98. count = newCount;
  99. carla_zeroStructs(indexes, newCount);
  100. carla_zeroStructs(ports, newCount);
  101. return true;
  102. }
  103. void clear() noexcept
  104. {
  105. if (ports != nullptr)
  106. {
  107. for (uint32_t i=0; i < count; ++i)
  108. {
  109. if (ports[i] != nullptr)
  110. {
  111. delete ports[i];
  112. ports[i] = nullptr;
  113. }
  114. }
  115. delete[] ports;
  116. ports = nullptr;
  117. }
  118. if (indexes != nullptr)
  119. {
  120. delete[] indexes;
  121. indexes = nullptr;
  122. }
  123. count = 0;
  124. }
  125. void initBuffers() const noexcept
  126. {
  127. for (uint32_t i=0; i < count; ++i)
  128. {
  129. if (ports[i] != nullptr)
  130. ports[i]->initBuffer();
  131. }
  132. }
  133. CARLA_DECLARE_NON_COPY_STRUCT(NativePluginMidiOutData)
  134. };
  135. struct NativePluginMidiInData : NativePluginMidiOutData {
  136. struct MultiPortData {
  137. uint32_t cachedEventCount;
  138. uint32_t usedIndex;
  139. };
  140. MultiPortData* multiportData;
  141. NativePluginMidiInData() noexcept
  142. : NativePluginMidiOutData(),
  143. multiportData(nullptr) {}
  144. ~NativePluginMidiInData() noexcept
  145. {
  146. CARLA_SAFE_ASSERT(multiportData == nullptr);
  147. }
  148. bool createNew(const uint32_t newCount)
  149. {
  150. if (! NativePluginMidiOutData::createNew(newCount))
  151. return false;
  152. multiportData = new MultiPortData[newCount];
  153. carla_zeroStructs(multiportData, newCount);
  154. return true;
  155. }
  156. void clear() noexcept
  157. {
  158. if (multiportData != nullptr)
  159. {
  160. delete[] multiportData;
  161. multiportData = nullptr;
  162. }
  163. NativePluginMidiOutData::clear();
  164. }
  165. void initBuffers(CarlaEngineEventPort* const port) const noexcept
  166. {
  167. if (count == 1)
  168. {
  169. CARLA_SAFE_ASSERT_RETURN(port != nullptr,);
  170. carla_zeroStruct(multiportData[0]);
  171. multiportData[0].cachedEventCount = port->getEventCount();
  172. return;
  173. }
  174. for (uint32_t i=0; i < count; ++i)
  175. {
  176. carla_zeroStruct(multiportData[i]);
  177. if (ports[i] != nullptr)
  178. {
  179. ports[i]->initBuffer();
  180. multiportData[i].cachedEventCount = ports[i]->getEventCount();
  181. }
  182. }
  183. }
  184. CARLA_DECLARE_NON_COPY_STRUCT(NativePluginMidiInData)
  185. };
  186. // -----------------------------------------------------
  187. class CarlaPluginNative : public CarlaPlugin
  188. {
  189. public:
  190. CarlaPluginNative(CarlaEngine* const engine, const uint id)
  191. : CarlaPlugin(engine, id),
  192. fHandle(nullptr),
  193. fHandle2(nullptr),
  194. fHost(),
  195. fDescriptor(nullptr),
  196. fIsProcessing(false),
  197. fIsOffline(false),
  198. fIsUiAvailable(false),
  199. fIsUiVisible(false),
  200. fInlineDisplayNeedsRedraw(false),
  201. fAudioInBuffers(nullptr),
  202. fAudioOutBuffers(nullptr),
  203. fMidiEventInCount(0),
  204. fMidiEventOutCount(0),
  205. fCurBufferSize(engine->getBufferSize()),
  206. fCurSampleRate(engine->getSampleRate()),
  207. fMidiIn(),
  208. fMidiOut(),
  209. fTimeInfo()
  210. {
  211. carla_debug("CarlaPluginNative::CarlaPluginNative(%p, %i)", engine, id);
  212. carla_fill(fCurMidiProgs, 0, MAX_MIDI_CHANNELS);
  213. carla_zeroStructs(fMidiInEvents, kPluginMaxMidiEvents);
  214. carla_zeroStructs(fMidiOutEvents, kPluginMaxMidiEvents);
  215. carla_zeroStruct(fTimeInfo);
  216. fHost.handle = this;
  217. fHost.resourceDir = carla_strdup(engine->getOptions().resourceDir);
  218. fHost.uiName = nullptr;
  219. fHost.uiParentId = engine->getOptions().frontendWinId;
  220. fHost.get_buffer_size = carla_host_get_buffer_size;
  221. fHost.get_sample_rate = carla_host_get_sample_rate;
  222. fHost.is_offline = carla_host_is_offline;
  223. fHost.get_time_info = carla_host_get_time_info;
  224. fHost.write_midi_event = carla_host_write_midi_event;
  225. fHost.ui_parameter_changed = carla_host_ui_parameter_changed;
  226. fHost.ui_custom_data_changed = carla_host_ui_custom_data_changed;
  227. fHost.ui_closed = carla_host_ui_closed;
  228. fHost.ui_open_file = carla_host_ui_open_file;
  229. fHost.ui_save_file = carla_host_ui_save_file;
  230. fHost.dispatcher = carla_host_dispatcher;
  231. }
  232. ~CarlaPluginNative() override
  233. {
  234. carla_debug("CarlaPluginNative::~CarlaPluginNative()");
  235. // close UI
  236. if (pData->hints & PLUGIN_HAS_CUSTOM_UI)
  237. {
  238. if (fIsUiVisible && fDescriptor != nullptr && fDescriptor->ui_show != nullptr && fHandle != nullptr)
  239. fDescriptor->ui_show(fHandle, false);
  240. #ifndef BUILD_BRIDGE
  241. pData->transientTryCounter = 0;
  242. #endif
  243. }
  244. pData->singleMutex.lock();
  245. pData->masterMutex.lock();
  246. if (pData->client != nullptr && pData->client->isActive())
  247. pData->client->deactivate();
  248. CARLA_ASSERT(! fIsProcessing);
  249. if (pData->active)
  250. {
  251. deactivate();
  252. pData->active = false;
  253. }
  254. if (fDescriptor != nullptr)
  255. {
  256. if (fDescriptor->cleanup != nullptr)
  257. {
  258. if (fHandle != nullptr)
  259. fDescriptor->cleanup(fHandle);
  260. if (fHandle2 != nullptr)
  261. fDescriptor->cleanup(fHandle2);
  262. }
  263. fHandle = nullptr;
  264. fHandle2 = nullptr;
  265. fDescriptor = nullptr;
  266. }
  267. if (fHost.resourceDir != nullptr)
  268. {
  269. delete[] fHost.resourceDir;
  270. fHost.resourceDir = nullptr;
  271. }
  272. if (fHost.uiName != nullptr)
  273. {
  274. delete[] fHost.uiName;
  275. fHost.uiName = nullptr;
  276. }
  277. clearBuffers();
  278. }
  279. // -------------------------------------------------------------------
  280. // Information (base)
  281. PluginType getType() const noexcept override
  282. {
  283. return PLUGIN_INTERNAL;
  284. }
  285. PluginCategory getCategory() const noexcept override
  286. {
  287. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, PLUGIN_CATEGORY_NONE);
  288. return static_cast<PluginCategory>(fDescriptor->category);
  289. }
  290. // -------------------------------------------------------------------
  291. // Information (count)
  292. uint32_t getMidiInCount() const noexcept override
  293. {
  294. return fMidiIn.count;
  295. }
  296. uint32_t getMidiOutCount() const noexcept override
  297. {
  298. return fMidiOut.count;
  299. }
  300. uint32_t getParameterScalePointCount(const uint32_t parameterId) const noexcept override
  301. {
  302. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0);
  303. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr, 0);
  304. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0);
  305. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  306. // FIXME - try
  307. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  308. return param->scalePointCount;
  309. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  310. return 0;
  311. }
  312. // -------------------------------------------------------------------
  313. // Information (current data)
  314. // nothing
  315. // -------------------------------------------------------------------
  316. // Information (per-plugin data)
  317. uint getOptionsAvailable() const noexcept override
  318. {
  319. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0x0);
  320. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0x0);
  321. bool hasMidiProgs = false;
  322. if (fDescriptor->get_midi_program_count != nullptr)
  323. {
  324. try {
  325. hasMidiProgs = fDescriptor->get_midi_program_count(fHandle) > 0;
  326. } catch (...) {}
  327. }
  328. uint options = 0x0;
  329. // can't disable fixed buffers if required by the plugin
  330. if ((fDescriptor->hints & NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS) == 0x0)
  331. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  332. // can't disable forced stereo if enabled in the engine
  333. if (pData->engine->getOptions().forceStereo)
  334. pass();
  335. // if inputs or outputs are just 1, then yes we can force stereo
  336. else if (pData->audioIn.count == 1 || pData->audioOut.count == 1 || fHandle2 != nullptr)
  337. options |= PLUGIN_OPTION_FORCE_STEREO;
  338. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CONTROL_CHANGES)
  339. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  340. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CHANNEL_PRESSURE)
  341. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  342. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_NOTE_AFTERTOUCH)
  343. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  344. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PITCHBEND)
  345. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  346. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_ALL_SOUND_OFF)
  347. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  348. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PROGRAM_CHANGES)
  349. options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
  350. else if (hasMidiProgs)
  351. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  352. return options;
  353. }
  354. float getParameterValue(const uint32_t parameterId) const noexcept override
  355. {
  356. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0.0f);
  357. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_value != nullptr, 0.0f);
  358. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0.0f);
  359. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  360. // FIXME - try
  361. return fDescriptor->get_parameter_value(fHandle, parameterId);
  362. }
  363. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const noexcept override
  364. {
  365. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0.0f);
  366. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr, 0.0f);
  367. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0.0f);
  368. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  369. // FIXME - try
  370. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  371. {
  372. CARLA_SAFE_ASSERT_RETURN(scalePointId < param->scalePointCount, 0.0f);
  373. const NativeParameterScalePoint* scalePoint(&param->scalePoints[scalePointId]);
  374. return scalePoint->value;
  375. }
  376. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  377. return 0.0f;
  378. }
  379. void getLabel(char* const strBuf) const noexcept override
  380. {
  381. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  382. if (fDescriptor->label != nullptr)
  383. {
  384. std::strncpy(strBuf, fDescriptor->label, STR_MAX);
  385. return;
  386. }
  387. CarlaPlugin::getLabel(strBuf);
  388. }
  389. void getMaker(char* const strBuf) const noexcept override
  390. {
  391. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  392. if (fDescriptor->maker != nullptr)
  393. {
  394. std::strncpy(strBuf, fDescriptor->maker, STR_MAX);
  395. return;
  396. }
  397. CarlaPlugin::getMaker(strBuf);
  398. }
  399. void getCopyright(char* const strBuf) const noexcept override
  400. {
  401. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  402. if (fDescriptor->copyright != nullptr)
  403. {
  404. std::strncpy(strBuf, fDescriptor->copyright, STR_MAX);
  405. return;
  406. }
  407. CarlaPlugin::getCopyright(strBuf);
  408. }
  409. void getRealName(char* const strBuf) const noexcept override
  410. {
  411. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  412. if (fDescriptor->name != nullptr)
  413. {
  414. std::strncpy(strBuf, fDescriptor->name, STR_MAX);
  415. return;
  416. }
  417. CarlaPlugin::getRealName(strBuf);
  418. }
  419. void getParameterName(const uint32_t parameterId, char* const strBuf) const noexcept override
  420. {
  421. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  422. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr,);
  423. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  424. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  425. // FIXME - try
  426. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  427. {
  428. if (param->name != nullptr)
  429. {
  430. std::strncpy(strBuf, param->name, STR_MAX);
  431. return;
  432. }
  433. carla_safe_assert("param->name != nullptr", __FILE__, __LINE__);
  434. return CarlaPlugin::getParameterName(parameterId, strBuf);
  435. }
  436. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  437. CarlaPlugin::getParameterName(parameterId, strBuf);
  438. }
  439. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const noexcept override
  440. {
  441. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  442. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr,);
  443. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  444. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  445. // FIXME - try
  446. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  447. {
  448. if (param->unit != nullptr)
  449. {
  450. std::strncpy(strBuf, param->unit, STR_MAX);
  451. return;
  452. }
  453. return CarlaPlugin::getParameterUnit(parameterId, strBuf);
  454. }
  455. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  456. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  457. }
  458. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const noexcept override
  459. {
  460. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  461. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr,);
  462. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  463. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  464. // FIXME - try
  465. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  466. {
  467. CARLA_SAFE_ASSERT_RETURN(scalePointId < param->scalePointCount,);
  468. const NativeParameterScalePoint* scalePoint(&param->scalePoints[scalePointId]);
  469. if (scalePoint->label != nullptr)
  470. {
  471. std::strncpy(strBuf, scalePoint->label, STR_MAX);
  472. return;
  473. }
  474. carla_safe_assert("scalePoint->label != nullptr", __FILE__, __LINE__);
  475. return CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  476. }
  477. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  478. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  479. }
  480. // -------------------------------------------------------------------
  481. // Set data (state)
  482. void prepareForSave() override
  483. {
  484. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  485. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  486. if (pData->midiprog.count > 0 && fDescriptor->category == NATIVE_PLUGIN_CATEGORY_SYNTH)
  487. {
  488. char strBuf[STR_MAX+1];
  489. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i",
  490. fCurMidiProgs[0], fCurMidiProgs[1], fCurMidiProgs[2], fCurMidiProgs[3],
  491. fCurMidiProgs[4], fCurMidiProgs[5], fCurMidiProgs[6], fCurMidiProgs[7],
  492. fCurMidiProgs[8], fCurMidiProgs[9], fCurMidiProgs[10], fCurMidiProgs[11],
  493. fCurMidiProgs[12], fCurMidiProgs[13], fCurMidiProgs[14], fCurMidiProgs[15]);
  494. strBuf[STR_MAX] = '\0';
  495. CarlaPlugin::setCustomData(CUSTOM_DATA_TYPE_STRING, "midiPrograms", strBuf, false);
  496. }
  497. if (fDescriptor == nullptr || fDescriptor->get_state == nullptr || (fDescriptor->hints & NATIVE_PLUGIN_USES_STATE) == 0)
  498. return;
  499. if (char* data = fDescriptor->get_state(fHandle))
  500. {
  501. CarlaPlugin::setCustomData(CUSTOM_DATA_TYPE_CHUNK, "State", data, false);
  502. std::free(data);
  503. }
  504. }
  505. // -------------------------------------------------------------------
  506. // Set data (internal stuff)
  507. void setName(const char* const newName) override
  508. {
  509. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  510. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  511. CARLA_SAFE_ASSERT_RETURN(newName != nullptr && newName[0] != '\0',);
  512. char uiName[std::strlen(newName)+6+1];
  513. std::strcpy(uiName, newName);
  514. std::strcat(uiName, " (GUI)");
  515. if (fHost.uiName != nullptr)
  516. delete[] fHost.uiName;
  517. fHost.uiName = carla_strdup(uiName);
  518. if (fDescriptor->dispatcher != nullptr && fIsUiVisible)
  519. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_UI_NAME_CHANGED, 0, 0, uiName, 0.0f);
  520. CarlaPlugin::setName(newName);
  521. }
  522. void setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) noexcept override
  523. {
  524. if (channel >= 0 && channel < MAX_MIDI_CHANNELS && pData->midiprog.count > 0)
  525. pData->midiprog.current = fCurMidiProgs[channel];
  526. CarlaPlugin::setCtrlChannel(channel, sendOsc, sendCallback);
  527. }
  528. // -------------------------------------------------------------------
  529. // Set data (plugin-specific stuff)
  530. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) noexcept override
  531. {
  532. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  533. CARLA_SAFE_ASSERT_RETURN(fDescriptor->set_parameter_value != nullptr,);
  534. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  535. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  536. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  537. // FIXME - try
  538. fDescriptor->set_parameter_value(fHandle, parameterId, fixedValue);
  539. if (fHandle2 != nullptr)
  540. fDescriptor->set_parameter_value(fHandle2, parameterId, fixedValue);
  541. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  542. }
  543. void setParameterValueRT(const uint32_t parameterId, const float value) noexcept override
  544. {
  545. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  546. CARLA_SAFE_ASSERT_RETURN(fDescriptor->set_parameter_value != nullptr,);
  547. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  548. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  549. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  550. // FIXME - try
  551. fDescriptor->set_parameter_value(fHandle, parameterId, fixedValue);
  552. if (fHandle2 != nullptr)
  553. fDescriptor->set_parameter_value(fHandle2, parameterId, fixedValue);
  554. CarlaPlugin::setParameterValueRT(parameterId, fixedValue);
  555. }
  556. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  557. {
  558. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  559. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  560. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  561. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  562. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  563. carla_debug("CarlaPluginNative::setCustomData(%s, %s, ..., %s)", type, key, bool2str(sendGui));
  564. if (std::strcmp(type, CUSTOM_DATA_TYPE_PROPERTY) == 0)
  565. return CarlaPlugin::setCustomData(type, key, value, sendGui);
  566. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) != 0 && std::strcmp(type, CUSTOM_DATA_TYPE_CHUNK) != 0)
  567. return carla_stderr2("CarlaPluginNative::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is invalid", type, key, value, bool2str(sendGui));
  568. if (std::strcmp(type, CUSTOM_DATA_TYPE_CHUNK) == 0)
  569. {
  570. if (fDescriptor->set_state != nullptr && (fDescriptor->hints & NATIVE_PLUGIN_USES_STATE) != 0)
  571. {
  572. const ScopedSingleProcessLocker spl(this, true);
  573. fDescriptor->set_state(fHandle, value);
  574. if (fHandle2 != nullptr)
  575. fDescriptor->set_state(fHandle2, value);
  576. }
  577. }
  578. else if (std::strcmp(key, "midiPrograms") == 0 && fDescriptor->set_midi_program != nullptr)
  579. {
  580. StringArray midiProgramList(StringArray::fromTokens(value, ":", ""));
  581. if (midiProgramList.size() == MAX_MIDI_CHANNELS)
  582. {
  583. uint8_t channel = 0;
  584. for (String *it=midiProgramList.begin(), *end=midiProgramList.end(); it != end; ++it)
  585. {
  586. const int index(it->getIntValue());
  587. if (index >= 0 && index < static_cast<int>(pData->midiprog.count))
  588. {
  589. const uint32_t bank = pData->midiprog.data[index].bank;
  590. const uint32_t program = pData->midiprog.data[index].program;
  591. fDescriptor->set_midi_program(fHandle, channel, bank, program);
  592. if (fHandle2 != nullptr)
  593. fDescriptor->set_midi_program(fHandle2, channel, bank, program);
  594. fCurMidiProgs[channel] = index;
  595. if (pData->ctrlChannel == static_cast<int32_t>(channel))
  596. {
  597. pData->midiprog.current = index;
  598. pData->engine->callback(true, true,
  599. ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED,
  600. pData->id,
  601. index,
  602. 0, 0, 0.0f, nullptr);
  603. }
  604. }
  605. ++channel;
  606. }
  607. CARLA_SAFE_ASSERT(channel == MAX_MIDI_CHANNELS);
  608. }
  609. }
  610. else
  611. {
  612. if (fDescriptor->set_custom_data != nullptr)
  613. {
  614. fDescriptor->set_custom_data(fHandle, key, value);
  615. if (fHandle2 != nullptr)
  616. fDescriptor->set_custom_data(fHandle2, key, value);
  617. }
  618. if (sendGui && fIsUiVisible && fDescriptor->ui_set_custom_data != nullptr)
  619. fDescriptor->ui_set_custom_data(fHandle, key, value);
  620. }
  621. CarlaPlugin::setCustomData(type, key, value, sendGui);
  622. }
  623. void setMidiProgram(const int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool doingInit) noexcept override
  624. {
  625. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  626. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  627. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  628. CARLA_SAFE_ASSERT_RETURN(sendGui || sendOsc || sendCallback || doingInit,);
  629. // TODO, put into check below
  630. if ((pData->hints & PLUGIN_IS_SYNTH) != 0 && (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS))
  631. return CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback, doingInit);
  632. if (index >= 0)
  633. {
  634. const uint8_t channel = uint8_t((pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS) ? pData->ctrlChannel : 0);
  635. const uint32_t bank = pData->midiprog.data[index].bank;
  636. const uint32_t program = pData->midiprog.data[index].program;
  637. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  638. try {
  639. fDescriptor->set_midi_program(fHandle, channel, bank, program);
  640. } catch(...) {}
  641. if (fHandle2 != nullptr)
  642. {
  643. try {
  644. fDescriptor->set_midi_program(fHandle2, channel, bank, program);
  645. } catch(...) {}
  646. }
  647. fCurMidiProgs[channel] = index;
  648. }
  649. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback, doingInit);
  650. }
  651. // FIXME: this is never used
  652. void setMidiProgramRT(const uint32_t index) noexcept override
  653. {
  654. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  655. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  656. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  657. // TODO, put into check below
  658. if ((pData->hints & PLUGIN_IS_SYNTH) != 0 && (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS))
  659. return CarlaPlugin::setMidiProgramRT(index);
  660. const uint8_t channel = uint8_t((pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS) ? pData->ctrlChannel : 0);
  661. const uint32_t bank = pData->midiprog.data[index].bank;
  662. const uint32_t program = pData->midiprog.data[index].program;
  663. try {
  664. fDescriptor->set_midi_program(fHandle, channel, bank, program);
  665. } catch(...) {}
  666. if (fHandle2 != nullptr)
  667. {
  668. try {
  669. fDescriptor->set_midi_program(fHandle2, channel, bank, program);
  670. } catch(...) {}
  671. }
  672. fCurMidiProgs[channel] = static_cast<int32_t>(index);
  673. CarlaPlugin::setMidiProgramRT(index);
  674. }
  675. // -------------------------------------------------------------------
  676. // Set ui stuff
  677. void showCustomUI(const bool yesNo) override
  678. {
  679. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  680. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  681. if (fDescriptor->ui_show == nullptr)
  682. return;
  683. fIsUiAvailable = true;
  684. fDescriptor->ui_show(fHandle, yesNo);
  685. // UI might not be available, see NATIVE_HOST_OPCODE_UI_UNAVAILABLE
  686. if (yesNo && ! fIsUiAvailable)
  687. return;
  688. fIsUiVisible = yesNo;
  689. if (! yesNo)
  690. {
  691. #ifndef BUILD_BRIDGE
  692. pData->transientTryCounter = 0;
  693. #endif
  694. return;
  695. }
  696. #ifndef BUILD_BRIDGE
  697. if ((fDescriptor->hints & NATIVE_PLUGIN_USES_PARENT_ID) == 0 && std::strstr(fDescriptor->label, "file") == nullptr)
  698. pData->tryTransient();
  699. #endif
  700. if (fDescriptor->ui_set_custom_data != nullptr)
  701. {
  702. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  703. {
  704. const CustomData& cData(it.getValue(kCustomDataFallback));
  705. CARLA_SAFE_ASSERT_CONTINUE(cData.isValid());
  706. if (std::strcmp(cData.type, CUSTOM_DATA_TYPE_STRING) == 0 && std::strcmp(cData.key, "midiPrograms") != 0)
  707. fDescriptor->ui_set_custom_data(fHandle, cData.key, cData.value);
  708. }
  709. }
  710. if (fDescriptor->ui_set_midi_program != nullptr && pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  711. {
  712. const int32_t index = pData->midiprog.current;
  713. const uint8_t channel = uint8_t((pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS) ? pData->ctrlChannel : 0);
  714. const uint32_t bank = pData->midiprog.data[index].bank;
  715. const uint32_t program = pData->midiprog.data[index].program;
  716. fDescriptor->ui_set_midi_program(fHandle, channel, bank, program);
  717. }
  718. if (fDescriptor->ui_set_parameter_value != nullptr)
  719. {
  720. for (uint32_t i=0; i < pData->param.count; ++i)
  721. fDescriptor->ui_set_parameter_value(fHandle, i, fDescriptor->get_parameter_value(fHandle, i));
  722. }
  723. }
  724. void uiIdle() override
  725. {
  726. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  727. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  728. if (fIsUiVisible && fDescriptor->ui_idle != nullptr)
  729. fDescriptor->ui_idle(fHandle);
  730. CarlaPlugin::uiIdle();
  731. }
  732. // -------------------------------------------------------------------
  733. // Plugin state
  734. void reload() override
  735. {
  736. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  737. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  738. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  739. carla_debug("CarlaPluginNative::reload() - start");
  740. const EngineProcessMode processMode(pData->engine->getProccessMode());
  741. // Safely disable plugin for reload
  742. const ScopedDisabler sd(this);
  743. if (pData->active)
  744. deactivate();
  745. clearBuffers();
  746. const float sampleRate((float)pData->engine->getSampleRate());
  747. uint32_t aIns, aOuts, mIns, mOuts, params, j;
  748. bool forcedStereoIn, forcedStereoOut;
  749. forcedStereoIn = forcedStereoOut = false;
  750. bool needsCtrlIn, needsCtrlOut;
  751. needsCtrlIn = needsCtrlOut = false;
  752. aIns = fDescriptor->audioIns;
  753. aOuts = fDescriptor->audioOuts;
  754. mIns = fDescriptor->midiIns;
  755. mOuts = fDescriptor->midiOuts;
  756. params = (fDescriptor->get_parameter_count != nullptr && fDescriptor->get_parameter_info != nullptr) ? fDescriptor->get_parameter_count(fHandle) : 0;
  757. if ((pData->options & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1) && mIns <= 1 && mOuts <= 1)
  758. {
  759. if (fHandle2 == nullptr)
  760. fHandle2 = fDescriptor->instantiate(&fHost);
  761. if (fHandle2 != nullptr)
  762. {
  763. if (aIns == 1)
  764. {
  765. aIns = 2;
  766. forcedStereoIn = true;
  767. }
  768. if (aOuts == 1)
  769. {
  770. aOuts = 2;
  771. forcedStereoOut = true;
  772. }
  773. }
  774. }
  775. if (aIns > 0)
  776. {
  777. pData->audioIn.createNew(aIns);
  778. fAudioInBuffers = new float*[aIns];
  779. for (uint32_t i=0; i < aIns; ++i)
  780. fAudioInBuffers[i] = nullptr;
  781. }
  782. if (aOuts > 0)
  783. {
  784. pData->audioOut.createNew(aOuts);
  785. fAudioOutBuffers = new float*[aOuts];
  786. needsCtrlIn = true;
  787. for (uint32_t i=0; i < aOuts; ++i)
  788. fAudioOutBuffers[i] = nullptr;
  789. }
  790. if (mIns > 0)
  791. {
  792. fMidiIn.createNew(mIns);
  793. needsCtrlIn = (mIns == 1);
  794. }
  795. if (mOuts > 0)
  796. {
  797. fMidiOut.createNew(mOuts);
  798. needsCtrlOut = (mOuts == 1);
  799. }
  800. if (params > 0)
  801. {
  802. pData->param.createNew(params, true);
  803. }
  804. const uint portNameSize(pData->engine->getMaxPortNameSize());
  805. CarlaString portName;
  806. // Audio Ins
  807. for (j=0; j < aIns; ++j)
  808. {
  809. portName.clear();
  810. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  811. {
  812. portName = pData->name;
  813. portName += ":";
  814. }
  815. if (aIns > 1 && ! forcedStereoIn)
  816. {
  817. portName += "input_";
  818. portName += CarlaString(j+1);
  819. }
  820. else
  821. portName += "input";
  822. portName.truncate(portNameSize);
  823. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, j);
  824. pData->audioIn.ports[j].rindex = j;
  825. if (forcedStereoIn)
  826. {
  827. portName += "_2";
  828. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, 1);
  829. pData->audioIn.ports[1].rindex = j;
  830. break;
  831. }
  832. }
  833. // Audio Outs
  834. for (j=0; j < aOuts; ++j)
  835. {
  836. portName.clear();
  837. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  838. {
  839. portName = pData->name;
  840. portName += ":";
  841. }
  842. if (aOuts > 1 && ! forcedStereoOut)
  843. {
  844. portName += "output_";
  845. portName += CarlaString(j+1);
  846. }
  847. else
  848. portName += "output";
  849. portName.truncate(portNameSize);
  850. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, j);
  851. pData->audioOut.ports[j].rindex = j;
  852. if (forcedStereoOut)
  853. {
  854. portName += "_2";
  855. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, 1);
  856. pData->audioOut.ports[1].rindex = j;
  857. break;
  858. }
  859. }
  860. // MIDI Input (only if multiple)
  861. if (mIns > 1)
  862. {
  863. for (j=0; j < mIns; ++j)
  864. {
  865. portName.clear();
  866. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  867. {
  868. portName = pData->name;
  869. portName += ":";
  870. }
  871. portName += "midi-in_";
  872. portName += CarlaString(j+1);
  873. portName.truncate(portNameSize);
  874. fMidiIn.ports[j] = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, j);
  875. fMidiIn.indexes[j] = j;
  876. }
  877. pData->event.portIn = fMidiIn.ports[0];
  878. }
  879. // MIDI Output (only if multiple)
  880. if (mOuts > 1)
  881. {
  882. for (j=0; j < mOuts; ++j)
  883. {
  884. portName.clear();
  885. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  886. {
  887. portName = pData->name;
  888. portName += ":";
  889. }
  890. portName += "midi-out_";
  891. portName += CarlaString(j+1);
  892. portName.truncate(portNameSize);
  893. fMidiOut.ports[j] = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, j);
  894. fMidiOut.indexes[j] = j;
  895. }
  896. pData->event.portOut = fMidiOut.ports[0];
  897. }
  898. for (j=0; j < params; ++j)
  899. {
  900. const NativeParameter* const paramInfo(fDescriptor->get_parameter_info(fHandle, j));
  901. CARLA_SAFE_ASSERT_CONTINUE(paramInfo != nullptr);
  902. pData->param.data[j].type = PARAMETER_UNKNOWN;
  903. pData->param.data[j].index = static_cast<int32_t>(j);
  904. pData->param.data[j].rindex = static_cast<int32_t>(j);
  905. float min, max, def, step, stepSmall, stepLarge;
  906. // min value
  907. min = paramInfo->ranges.min;
  908. // max value
  909. max = paramInfo->ranges.max;
  910. if (min > max)
  911. max = min;
  912. if (carla_isEqual(min, max))
  913. {
  914. carla_stderr2("WARNING - Broken plugin parameter '%s': max == min", paramInfo->name);
  915. max = min + 0.1f;
  916. }
  917. // default value
  918. def = paramInfo->ranges.def;
  919. if (def < min)
  920. def = min;
  921. else if (def > max)
  922. def = max;
  923. if (paramInfo->hints & NATIVE_PARAMETER_USES_SAMPLE_RATE)
  924. {
  925. min *= sampleRate;
  926. max *= sampleRate;
  927. def *= sampleRate;
  928. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  929. }
  930. if (paramInfo->hints & NATIVE_PARAMETER_IS_BOOLEAN)
  931. {
  932. step = max - min;
  933. stepSmall = step;
  934. stepLarge = step;
  935. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  936. }
  937. else if (paramInfo->hints & NATIVE_PARAMETER_IS_INTEGER)
  938. {
  939. step = 1.0f;
  940. stepSmall = 1.0f;
  941. stepLarge = 10.0f;
  942. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  943. }
  944. else
  945. {
  946. float range = max - min;
  947. step = range/100.0f;
  948. stepSmall = range/1000.0f;
  949. stepLarge = range/10.0f;
  950. }
  951. if (paramInfo->hints & NATIVE_PARAMETER_IS_OUTPUT)
  952. {
  953. pData->param.data[j].type = PARAMETER_OUTPUT;
  954. needsCtrlOut = true;
  955. }
  956. else
  957. {
  958. pData->param.data[j].type = PARAMETER_INPUT;
  959. needsCtrlIn = true;
  960. }
  961. // extra parameter hints
  962. if (paramInfo->hints & NATIVE_PARAMETER_IS_ENABLED)
  963. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  964. if (paramInfo->hints & NATIVE_PARAMETER_IS_AUTOMABLE)
  965. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  966. if (paramInfo->hints & NATIVE_PARAMETER_IS_LOGARITHMIC)
  967. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  968. if (paramInfo->hints & NATIVE_PARAMETER_USES_SCALEPOINTS)
  969. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  970. pData->param.ranges[j].min = min;
  971. pData->param.ranges[j].max = max;
  972. pData->param.ranges[j].def = def;
  973. pData->param.ranges[j].step = step;
  974. pData->param.ranges[j].stepSmall = stepSmall;
  975. pData->param.ranges[j].stepLarge = stepLarge;
  976. }
  977. if (needsCtrlIn || mIns == 1)
  978. {
  979. portName.clear();
  980. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  981. {
  982. portName = pData->name;
  983. portName += ":";
  984. }
  985. portName += "events-in";
  986. portName.truncate(portNameSize);
  987. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, 0);
  988. }
  989. if (needsCtrlOut || mOuts == 1)
  990. {
  991. portName.clear();
  992. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  993. {
  994. portName = pData->name;
  995. portName += ":";
  996. }
  997. portName += "events-out";
  998. portName.truncate(portNameSize);
  999. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, 0);
  1000. }
  1001. if (forcedStereoIn || forcedStereoOut)
  1002. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1003. else
  1004. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  1005. // plugin hints
  1006. pData->hints = 0x0;
  1007. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  1008. pData->hints |= PLUGIN_CAN_DRYWET;
  1009. if (aOuts > 0)
  1010. pData->hints |= PLUGIN_CAN_VOLUME;
  1011. if (aOuts >= 2 && aOuts % 2 == 0)
  1012. pData->hints |= PLUGIN_CAN_BALANCE;
  1013. // native plugin hints
  1014. if (fDescriptor->hints & NATIVE_PLUGIN_IS_RTSAFE)
  1015. pData->hints |= PLUGIN_IS_RTSAFE;
  1016. if (fDescriptor->hints & NATIVE_PLUGIN_IS_SYNTH)
  1017. pData->hints |= PLUGIN_IS_SYNTH;
  1018. if (fDescriptor->hints & NATIVE_PLUGIN_HAS_UI)
  1019. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  1020. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS)
  1021. pData->hints |= PLUGIN_NEEDS_FIXED_BUFFERS;
  1022. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_UI_MAIN_THREAD)
  1023. pData->hints |= PLUGIN_NEEDS_UI_MAIN_THREAD;
  1024. if (fDescriptor->hints & NATIVE_PLUGIN_USES_MULTI_PROGS)
  1025. pData->hints |= PLUGIN_USES_MULTI_PROGS;
  1026. if (fDescriptor->hints & NATIVE_PLUGIN_HAS_INLINE_DISPLAY)
  1027. pData->hints |= PLUGIN_HAS_INLINE_DISPLAY;
  1028. // extra plugin hints
  1029. pData->extraHints = 0x0;
  1030. bufferSizeChanged(pData->engine->getBufferSize());
  1031. reloadPrograms(true);
  1032. if (pData->active)
  1033. activate();
  1034. carla_debug("CarlaPluginNative::reload() - end");
  1035. }
  1036. void reloadPrograms(const bool doInit) override
  1037. {
  1038. carla_debug("CarlaPluginNative::reloadPrograms(%s)", bool2str(doInit));
  1039. uint32_t i, oldCount = pData->midiprog.count;
  1040. const int32_t current = pData->midiprog.current;
  1041. // Delete old programs
  1042. pData->midiprog.clear();
  1043. // Query new programs
  1044. uint32_t count = 0;
  1045. if (fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_info != nullptr && fDescriptor->set_midi_program != nullptr)
  1046. count = fDescriptor->get_midi_program_count(fHandle);
  1047. if (count > 0)
  1048. {
  1049. pData->midiprog.createNew(count);
  1050. // Update data
  1051. for (i=0; i < count; ++i)
  1052. {
  1053. const NativeMidiProgram* const mpDesc(fDescriptor->get_midi_program_info(fHandle, i));
  1054. CARLA_SAFE_ASSERT_CONTINUE(mpDesc != nullptr);
  1055. pData->midiprog.data[i].bank = mpDesc->bank;
  1056. pData->midiprog.data[i].program = mpDesc->program;
  1057. pData->midiprog.data[i].name = carla_strdup(mpDesc->name);
  1058. }
  1059. }
  1060. if (doInit)
  1061. {
  1062. if (count > 0)
  1063. setMidiProgram(0, false, false, false, true);
  1064. }
  1065. else
  1066. {
  1067. // Check if current program is invalid
  1068. bool programChanged = false;
  1069. if (count == oldCount+1)
  1070. {
  1071. // one midi program added, probably created by user
  1072. pData->midiprog.current = static_cast<int32_t>(oldCount);
  1073. programChanged = true;
  1074. }
  1075. else if (current < 0 && count > 0)
  1076. {
  1077. // programs exist now, but not before
  1078. pData->midiprog.current = 0;
  1079. programChanged = true;
  1080. }
  1081. else if (current >= 0 && count == 0)
  1082. {
  1083. // programs existed before, but not anymore
  1084. pData->midiprog.current = -1;
  1085. programChanged = true;
  1086. }
  1087. else if (current >= static_cast<int32_t>(count))
  1088. {
  1089. // current midi program > count
  1090. pData->midiprog.current = 0;
  1091. programChanged = true;
  1092. }
  1093. else
  1094. {
  1095. // no change
  1096. pData->midiprog.current = current;
  1097. }
  1098. if (programChanged)
  1099. setMidiProgram(pData->midiprog.current, true, true, true, false);
  1100. pData->engine->callback(true, true,
  1101. ENGINE_CALLBACK_RELOAD_PROGRAMS,
  1102. pData->id,
  1103. 0, 0, 0, 0.0f, nullptr);
  1104. }
  1105. }
  1106. // -------------------------------------------------------------------
  1107. // Plugin processing
  1108. void activate() noexcept override
  1109. {
  1110. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1111. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1112. if (fDescriptor->activate != nullptr)
  1113. {
  1114. try {
  1115. fDescriptor->activate(fHandle);
  1116. } catch(...) {}
  1117. if (fHandle2 != nullptr)
  1118. {
  1119. try {
  1120. fDescriptor->activate(fHandle2);
  1121. } catch(...) {}
  1122. }
  1123. }
  1124. }
  1125. void deactivate() noexcept override
  1126. {
  1127. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1128. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1129. if (fDescriptor->deactivate != nullptr)
  1130. {
  1131. try {
  1132. fDescriptor->deactivate(fHandle);
  1133. } catch(...) {}
  1134. if (fHandle2 != nullptr)
  1135. {
  1136. try {
  1137. fDescriptor->deactivate(fHandle2);
  1138. } catch(...) {}
  1139. }
  1140. }
  1141. }
  1142. const EngineEvent& findNextEvent()
  1143. {
  1144. if (fMidiIn.count == 1)
  1145. {
  1146. NativePluginMidiInData::MultiPortData& multiportData(fMidiIn.multiportData[0]);
  1147. if (multiportData.usedIndex == multiportData.cachedEventCount)
  1148. {
  1149. const uint32_t eventCount = pData->event.portIn->getEventCount();
  1150. CARLA_SAFE_ASSERT_INT2(eventCount == multiportData.cachedEventCount,
  1151. eventCount, multiportData.cachedEventCount);
  1152. return kNullEngineEvent;
  1153. }
  1154. return pData->event.portIn->getEvent(multiportData.usedIndex++);
  1155. }
  1156. uint32_t lowestSampleTime = 9999999;
  1157. uint32_t portMatching = 0;
  1158. bool found = false;
  1159. // process events in order for multiple ports
  1160. for (uint32_t m=0; m < fMidiIn.count; ++m)
  1161. {
  1162. CarlaEngineEventPort* const eventPort(fMidiIn.ports[m]);
  1163. NativePluginMidiInData::MultiPortData& multiportData(fMidiIn.multiportData[m]);
  1164. if (multiportData.usedIndex == multiportData.cachedEventCount)
  1165. continue;
  1166. const EngineEvent& event(eventPort->getEventUnchecked(multiportData.usedIndex));
  1167. if (event.time < lowestSampleTime)
  1168. {
  1169. lowestSampleTime = event.time;
  1170. portMatching = m;
  1171. found = true;
  1172. }
  1173. }
  1174. if (found)
  1175. {
  1176. CarlaEngineEventPort* const eventPort(fMidiIn.ports[portMatching]);
  1177. NativePluginMidiInData::MultiPortData& multiportData(fMidiIn.multiportData[portMatching]);
  1178. return eventPort->getEvent(multiportData.usedIndex++);
  1179. }
  1180. return kNullEngineEvent;
  1181. }
  1182. void process(const float** const audioIn, float** const audioOut, const float** const, float** const, const uint32_t frames) override
  1183. {
  1184. // --------------------------------------------------------------------------------------------------------
  1185. // Check if active
  1186. if (! pData->active)
  1187. {
  1188. // disable any output sound
  1189. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1190. carla_zeroFloats(audioOut[i], frames);
  1191. return;
  1192. }
  1193. fMidiEventInCount = fMidiEventOutCount = 0;
  1194. carla_zeroStructs(fMidiInEvents, kPluginMaxMidiEvents);
  1195. carla_zeroStructs(fMidiOutEvents, kPluginMaxMidiEvents);
  1196. // --------------------------------------------------------------------------------------------------------
  1197. // Check if needs reset
  1198. if (pData->needsReset)
  1199. {
  1200. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1201. {
  1202. fMidiEventInCount = MAX_MIDI_CHANNELS*2;
  1203. for (uint8_t k=0, i=MAX_MIDI_CHANNELS; k < MAX_MIDI_CHANNELS; ++k)
  1204. {
  1205. fMidiInEvents[k].data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
  1206. fMidiInEvents[k].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1207. fMidiInEvents[k].data[2] = 0;
  1208. fMidiInEvents[k].size = 3;
  1209. fMidiInEvents[k+i].data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
  1210. fMidiInEvents[k+i].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1211. fMidiInEvents[k+i].data[2] = 0;
  1212. fMidiInEvents[k+i].size = 3;
  1213. }
  1214. }
  1215. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  1216. {
  1217. fMidiEventInCount = MAX_MIDI_NOTE;
  1218. for (uint8_t k=0; k < MAX_MIDI_NOTE; ++k)
  1219. {
  1220. fMidiInEvents[k].data[0] = uint8_t(MIDI_STATUS_NOTE_OFF | (pData->ctrlChannel & MIDI_CHANNEL_BIT));
  1221. fMidiInEvents[k].data[1] = k;
  1222. fMidiInEvents[k].data[2] = 0;
  1223. fMidiInEvents[k].size = 3;
  1224. }
  1225. }
  1226. pData->needsReset = false;
  1227. }
  1228. // --------------------------------------------------------------------------------------------------------
  1229. // Set TimeInfo
  1230. const EngineTimeInfo timeInfo(pData->engine->getTimeInfo());
  1231. fTimeInfo.playing = timeInfo.playing;
  1232. fTimeInfo.frame = timeInfo.frame;
  1233. fTimeInfo.usecs = timeInfo.usecs;
  1234. if (timeInfo.bbt.valid)
  1235. {
  1236. fTimeInfo.bbt.valid = true;
  1237. fTimeInfo.bbt.bar = timeInfo.bbt.bar;
  1238. fTimeInfo.bbt.beat = timeInfo.bbt.beat;
  1239. fTimeInfo.bbt.tick = timeInfo.bbt.tick;
  1240. fTimeInfo.bbt.barStartTick = timeInfo.bbt.barStartTick;
  1241. fTimeInfo.bbt.beatsPerBar = timeInfo.bbt.beatsPerBar;
  1242. fTimeInfo.bbt.beatType = timeInfo.bbt.beatType;
  1243. fTimeInfo.bbt.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
  1244. fTimeInfo.bbt.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
  1245. }
  1246. else
  1247. {
  1248. fTimeInfo.bbt.valid = false;
  1249. }
  1250. #if 0
  1251. // This test code has proven to be quite useful
  1252. // So I am leaving it behind, I might need it again..
  1253. if (pData->id == 1)
  1254. {
  1255. static int64_t last_frame = timeInfo.frame;
  1256. static int64_t last_dev_frame = 0;
  1257. static double last_val = timeInfo.bbt.barStartTick + ((timeInfo.bbt.beat-1) * timeInfo.bbt.ticksPerBeat) + timeInfo.bbt.tick;
  1258. static double last_dev_val = 0.0;
  1259. int64_t cur_frame = timeInfo.frame;
  1260. int64_t cur_dev_frame = cur_frame - last_frame;
  1261. double cur_val = timeInfo.bbt.barStartTick + ((timeInfo.bbt.beat-1) * timeInfo.bbt.ticksPerBeat) + timeInfo.bbt.tick;
  1262. double cur_dev_val = cur_val - last_val;
  1263. if (std::abs(last_dev_val - cur_dev_val) >= 0.0001 || last_dev_frame != cur_dev_frame)
  1264. {
  1265. carla_stdout("currently %u at %u => %f : DEV1: %li : DEV2: %f",
  1266. frames,
  1267. timeInfo.frame,
  1268. cur_val,
  1269. cur_dev_frame,
  1270. cur_dev_val);
  1271. }
  1272. last_val = cur_val;
  1273. last_dev_val = cur_dev_val;
  1274. last_frame = cur_frame;
  1275. last_dev_frame = cur_dev_frame;
  1276. }
  1277. #endif
  1278. // --------------------------------------------------------------------------------------------------------
  1279. // Event Input and Processing
  1280. if (pData->event.portIn != nullptr)
  1281. {
  1282. // ----------------------------------------------------------------------------------------------------
  1283. // MIDI Input (External)
  1284. if (pData->extNotes.mutex.tryLock())
  1285. {
  1286. ExternalMidiNote note = { 0, 0, 0 };
  1287. for (; fMidiEventInCount < kPluginMaxMidiEvents && ! pData->extNotes.data.isEmpty();)
  1288. {
  1289. note = pData->extNotes.data.getFirst(note, true);
  1290. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  1291. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1292. nativeEvent.data[0] = uint8_t((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  1293. nativeEvent.data[1] = note.note;
  1294. nativeEvent.data[2] = note.velo;
  1295. nativeEvent.size = 3;
  1296. }
  1297. pData->extNotes.mutex.unlock();
  1298. } // End of MIDI Input (External)
  1299. // ----------------------------------------------------------------------------------------------------
  1300. // Event Input (System)
  1301. #ifndef BUILD_BRIDGE
  1302. bool allNotesOffSent = false;
  1303. #endif
  1304. bool sampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  1305. uint32_t startTime = 0;
  1306. uint32_t timeOffset = 0;
  1307. uint32_t nextBankId;
  1308. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1309. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1310. else
  1311. nextBankId = 0;
  1312. for (;;)
  1313. {
  1314. const EngineEvent& event(findNextEvent());
  1315. if (event.type == kEngineEventTypeNull)
  1316. break;
  1317. uint32_t eventTime = event.time;
  1318. CARLA_SAFE_ASSERT_UINT2_CONTINUE(eventTime < frames, eventTime, frames);
  1319. if (eventTime < timeOffset)
  1320. {
  1321. carla_stderr2("Timing error, eventTime:%u < timeOffset:%u for '%s'",
  1322. eventTime, timeOffset, pData->name);
  1323. eventTime = timeOffset;
  1324. }
  1325. if (sampleAccurate && eventTime > timeOffset)
  1326. {
  1327. if (processSingle(audioIn, audioOut, eventTime - timeOffset, timeOffset))
  1328. {
  1329. startTime = 0;
  1330. timeOffset = eventTime;
  1331. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1332. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1333. else
  1334. nextBankId = 0;
  1335. if (fMidiEventInCount > 0)
  1336. {
  1337. carla_zeroStructs(fMidiInEvents, fMidiEventInCount);
  1338. fMidiEventInCount = 0;
  1339. }
  1340. if (fMidiEventOutCount > 0)
  1341. {
  1342. carla_zeroStructs(fMidiOutEvents, fMidiEventOutCount);
  1343. fMidiEventOutCount = 0;
  1344. }
  1345. }
  1346. else
  1347. startTime += timeOffset;
  1348. }
  1349. // Control change
  1350. switch (event.type)
  1351. {
  1352. case kEngineEventTypeNull:
  1353. break;
  1354. case kEngineEventTypeControl: {
  1355. const EngineControlEvent& ctrlEvent = event.ctrl;
  1356. switch (ctrlEvent.type)
  1357. {
  1358. case kEngineControlEventTypeNull:
  1359. break;
  1360. case kEngineControlEventTypeParameter: {
  1361. #ifndef BUILD_BRIDGE
  1362. // Control backend stuff
  1363. if (event.channel == pData->ctrlChannel)
  1364. {
  1365. float value;
  1366. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) > 0)
  1367. {
  1368. value = ctrlEvent.value;
  1369. setDryWetRT(value);
  1370. }
  1371. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) > 0)
  1372. {
  1373. value = ctrlEvent.value*127.0f/100.0f;
  1374. setVolumeRT(value);
  1375. }
  1376. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) > 0)
  1377. {
  1378. float left, right;
  1379. value = ctrlEvent.value/0.5f - 1.0f;
  1380. if (value < 0.0f)
  1381. {
  1382. left = -1.0f;
  1383. right = (value*2.0f)+1.0f;
  1384. }
  1385. else if (value > 0.0f)
  1386. {
  1387. left = (value*2.0f)-1.0f;
  1388. right = 1.0f;
  1389. }
  1390. else
  1391. {
  1392. left = -1.0f;
  1393. right = 1.0f;
  1394. }
  1395. setBalanceLeftRT(left);
  1396. setBalanceRightRT(right);
  1397. }
  1398. }
  1399. #endif
  1400. // Control plugin parameters
  1401. for (uint32_t k=0; k < pData->param.count; ++k)
  1402. {
  1403. if (pData->param.data[k].midiChannel != event.channel)
  1404. continue;
  1405. if (pData->param.data[k].midiCC != ctrlEvent.param)
  1406. continue;
  1407. if (pData->param.data[k].type != PARAMETER_INPUT)
  1408. continue;
  1409. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1410. continue;
  1411. float value;
  1412. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1413. {
  1414. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  1415. }
  1416. else
  1417. {
  1418. if (pData->param.data[k].hints & PARAMETER_IS_LOGARITHMIC)
  1419. value = pData->param.ranges[k].getUnnormalizedLogValue(ctrlEvent.value);
  1420. else
  1421. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  1422. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1423. value = std::rint(value);
  1424. }
  1425. setParameterValueRT(k, value);
  1426. }
  1427. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_CONTROL)
  1428. {
  1429. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1430. continue;
  1431. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1432. carla_zeroStruct(nativeEvent);
  1433. nativeEvent.time = sampleAccurate ? startTime : eventTime;
  1434. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1435. nativeEvent.data[1] = uint8_t(ctrlEvent.param);
  1436. nativeEvent.data[2] = uint8_t(ctrlEvent.value*127.0f);
  1437. nativeEvent.size = 3;
  1438. }
  1439. break;
  1440. } // case kEngineControlEventTypeParameter
  1441. case kEngineControlEventTypeMidiBank:
  1442. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  1443. {
  1444. if (event.channel == pData->ctrlChannel)
  1445. nextBankId = ctrlEvent.param;
  1446. }
  1447. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  1448. {
  1449. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1450. continue;
  1451. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1452. carla_zeroStruct(nativeEvent);
  1453. nativeEvent.time = sampleAccurate ? startTime : eventTime;
  1454. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1455. nativeEvent.data[1] = MIDI_CONTROL_BANK_SELECT;
  1456. nativeEvent.data[2] = uint8_t(ctrlEvent.param);
  1457. nativeEvent.size = 3;
  1458. }
  1459. break;
  1460. case kEngineControlEventTypeMidiProgram:
  1461. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  1462. {
  1463. if (event.channel < MAX_MIDI_CHANNELS)
  1464. {
  1465. const uint32_t nextProgramId(ctrlEvent.param);
  1466. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  1467. {
  1468. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  1469. {
  1470. fDescriptor->set_midi_program(fHandle, event.channel, nextBankId, nextProgramId);
  1471. if (fHandle2 != nullptr)
  1472. fDescriptor->set_midi_program(fHandle2, event.channel, nextBankId, nextProgramId);
  1473. fCurMidiProgs[event.channel] = static_cast<int32_t>(k);
  1474. if (event.channel == pData->ctrlChannel)
  1475. {
  1476. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange,
  1477. static_cast<int32_t>(k),
  1478. 0, 0, 0.0f);
  1479. }
  1480. break;
  1481. }
  1482. }
  1483. }
  1484. }
  1485. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  1486. {
  1487. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1488. continue;
  1489. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1490. carla_zeroStruct(nativeEvent);
  1491. nativeEvent.time = sampleAccurate ? startTime : eventTime;
  1492. nativeEvent.data[0] = uint8_t(MIDI_STATUS_PROGRAM_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1493. nativeEvent.data[1] = uint8_t(ctrlEvent.param);
  1494. nativeEvent.size = 2;
  1495. }
  1496. break;
  1497. case kEngineControlEventTypeAllSoundOff:
  1498. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1499. {
  1500. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1501. continue;
  1502. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1503. carla_zeroStruct(nativeEvent);
  1504. nativeEvent.time = sampleAccurate ? startTime : eventTime;
  1505. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1506. nativeEvent.data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1507. nativeEvent.data[2] = 0;
  1508. nativeEvent.size = 3;
  1509. }
  1510. break;
  1511. case kEngineControlEventTypeAllNotesOff:
  1512. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1513. {
  1514. #ifndef BUILD_BRIDGE
  1515. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1516. {
  1517. allNotesOffSent = true;
  1518. postponeRtAllNotesOff();
  1519. }
  1520. #endif
  1521. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1522. continue;
  1523. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1524. carla_zeroStruct(nativeEvent);
  1525. nativeEvent.time = sampleAccurate ? startTime : eventTime;
  1526. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1527. nativeEvent.data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1528. nativeEvent.data[2] = 0;
  1529. nativeEvent.size = 3;
  1530. }
  1531. break;
  1532. }
  1533. break;
  1534. }
  1535. case kEngineEventTypeMidi: {
  1536. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1537. continue;
  1538. const EngineMidiEvent& midiEvent(event.midi);
  1539. if (midiEvent.size > 4)
  1540. continue;
  1541. #ifdef CARLA_PROPER_CPP11_SUPPORT
  1542. static_assert(4 <= EngineMidiEvent::kDataSize, "Incorrect data");
  1543. #endif
  1544. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  1545. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1546. continue;
  1547. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1548. continue;
  1549. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1550. continue;
  1551. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1552. continue;
  1553. // Fix bad note-off
  1554. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1555. status = MIDI_STATUS_NOTE_OFF;
  1556. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1557. carla_zeroStruct(nativeEvent);
  1558. nativeEvent.port = midiEvent.port;
  1559. nativeEvent.time = sampleAccurate ? startTime : eventTime;
  1560. nativeEvent.size = midiEvent.size;
  1561. nativeEvent.data[0] = uint8_t(status | (event.channel & MIDI_CHANNEL_BIT));
  1562. nativeEvent.data[1] = midiEvent.size >= 2 ? midiEvent.data[1] : 0;
  1563. nativeEvent.data[2] = midiEvent.size >= 3 ? midiEvent.data[2] : 0;
  1564. nativeEvent.data[3] = midiEvent.size == 4 ? midiEvent.data[3] : 0;
  1565. if (status == MIDI_STATUS_NOTE_ON)
  1566. {
  1567. pData->postponeRtEvent(kPluginPostRtEventNoteOn,
  1568. event.channel,
  1569. midiEvent.data[1],
  1570. midiEvent.data[2],
  1571. 0.0f);
  1572. }
  1573. else if (status == MIDI_STATUS_NOTE_OFF)
  1574. {
  1575. pData->postponeRtEvent(kPluginPostRtEventNoteOff,
  1576. event.channel,
  1577. midiEvent.data[1],
  1578. 0, 0.0f);
  1579. }
  1580. } break;
  1581. } // switch (event.type)
  1582. }
  1583. pData->postRtEvents.trySplice();
  1584. if (frames > timeOffset)
  1585. processSingle(audioIn, audioOut, frames - timeOffset, timeOffset);
  1586. } // End of Event Input and Processing
  1587. // --------------------------------------------------------------------------------------------------------
  1588. // Plugin processing (no events)
  1589. else
  1590. {
  1591. processSingle(audioIn, audioOut, frames, 0);
  1592. } // End of Plugin processing (no events)
  1593. #ifndef BUILD_BRIDGE
  1594. // --------------------------------------------------------------------------------------------------------
  1595. // Control Output
  1596. if (pData->event.portOut != nullptr)
  1597. {
  1598. float value, curValue;
  1599. for (uint32_t k=0; k < pData->param.count; ++k)
  1600. {
  1601. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1602. continue;
  1603. curValue = fDescriptor->get_parameter_value(fHandle, k);
  1604. pData->param.ranges[k].fixValue(curValue);
  1605. if (pData->param.data[k].midiCC > 0)
  1606. {
  1607. value = pData->param.ranges[k].getNormalizedValue(curValue);
  1608. pData->event.portOut->writeControlEvent(0, pData->param.data[k].midiChannel, kEngineControlEventTypeParameter, static_cast<uint16_t>(pData->param.data[k].midiCC), value);
  1609. }
  1610. }
  1611. } // End of Control Output
  1612. #endif
  1613. }
  1614. bool processSingle(const float** const audioIn, float** const audioOut, const uint32_t frames, const uint32_t timeOffset)
  1615. {
  1616. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1617. if (pData->audioIn.count > 0)
  1618. {
  1619. CARLA_SAFE_ASSERT_RETURN(audioIn != nullptr, false);
  1620. }
  1621. if (pData->audioOut.count > 0)
  1622. {
  1623. CARLA_SAFE_ASSERT_RETURN(audioOut != nullptr, false);
  1624. }
  1625. // --------------------------------------------------------------------------------------------------------
  1626. // Try lock, silence otherwise
  1627. if (fIsOffline)
  1628. {
  1629. pData->singleMutex.lock();
  1630. }
  1631. else if (! pData->singleMutex.tryLock())
  1632. {
  1633. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1634. {
  1635. for (uint32_t k=0; k < frames; ++k)
  1636. audioOut[i][k+timeOffset] = 0.0f;
  1637. }
  1638. return false;
  1639. }
  1640. // --------------------------------------------------------------------------------------------------------
  1641. // Set audio buffers
  1642. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1643. carla_copyFloats(fAudioInBuffers[i], audioIn[i]+timeOffset, frames);
  1644. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1645. carla_zeroFloats(fAudioOutBuffers[i], frames);
  1646. // --------------------------------------------------------------------------------------------------------
  1647. // Run plugin
  1648. fIsProcessing = true;
  1649. if (fHandle2 == nullptr)
  1650. {
  1651. fDescriptor->process(fHandle,
  1652. const_cast<const float**>(fAudioInBuffers), fAudioOutBuffers, frames,
  1653. fMidiInEvents, fMidiEventInCount);
  1654. }
  1655. else
  1656. {
  1657. fDescriptor->process(fHandle,
  1658. (pData->audioIn.count > 0) ? const_cast<const float**>(&fAudioInBuffers[0]) : nullptr,
  1659. (pData->audioOut.count > 0) ? &fAudioOutBuffers[0] : nullptr,
  1660. frames, fMidiInEvents, fMidiEventInCount);
  1661. fDescriptor->process(fHandle2,
  1662. (pData->audioIn.count > 0) ? const_cast<const float**>(&fAudioInBuffers[1]) : nullptr,
  1663. (pData->audioOut.count > 0) ? &fAudioOutBuffers[1] : nullptr,
  1664. frames, fMidiInEvents, fMidiEventInCount);
  1665. }
  1666. fIsProcessing = false;
  1667. if (fTimeInfo.playing)
  1668. fTimeInfo.frame += frames;
  1669. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1670. // --------------------------------------------------------------------------------------------------------
  1671. // Post-processing (dry/wet, volume and balance)
  1672. {
  1673. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && carla_isNotEqual(pData->postProc.dryWet, 1.0f);
  1674. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
  1675. bool isPair;
  1676. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1677. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1678. {
  1679. // Dry/Wet
  1680. if (doDryWet)
  1681. {
  1682. for (uint32_t k=0; k < frames; ++k)
  1683. {
  1684. bufValue = fAudioInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  1685. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1686. }
  1687. }
  1688. // Balance
  1689. if (doBalance)
  1690. {
  1691. isPair = (i % 2 == 0);
  1692. if (isPair)
  1693. {
  1694. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1695. carla_copyFloats(oldBufLeft, fAudioOutBuffers[i], frames);
  1696. }
  1697. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1698. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1699. for (uint32_t k=0; k < frames; ++k)
  1700. {
  1701. if (isPair)
  1702. {
  1703. // left
  1704. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1705. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1706. }
  1707. else
  1708. {
  1709. // right
  1710. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1711. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1712. }
  1713. }
  1714. }
  1715. // Volume (and buffer copy)
  1716. {
  1717. for (uint32_t k=0; k < frames; ++k)
  1718. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  1719. }
  1720. }
  1721. } // End of Post-processing
  1722. #else
  1723. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1724. {
  1725. for (uint32_t k=0; k < frames; ++k)
  1726. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1727. }
  1728. #endif
  1729. // --------------------------------------------------------------------------------------------------------
  1730. // MIDI Output
  1731. if (pData->event.portOut != nullptr)
  1732. {
  1733. for (uint32_t k = 0; k < fMidiEventOutCount; ++k)
  1734. {
  1735. const uint8_t channel = uint8_t(MIDI_GET_CHANNEL_FROM_DATA(fMidiOutEvents[k].data));
  1736. const uint8_t port = fMidiOutEvents[k].port;
  1737. if (fMidiOut.count > 1 && port < fMidiOut.count)
  1738. fMidiOut.ports[port]->writeMidiEvent(fMidiOutEvents[k].time+timeOffset, channel, fMidiOutEvents[k].size, fMidiOutEvents[k].data);
  1739. else
  1740. pData->event.portOut->writeMidiEvent(fMidiOutEvents[k].time+timeOffset, channel, fMidiOutEvents[k].size, fMidiOutEvents[k].data);
  1741. }
  1742. }
  1743. // --------------------------------------------------------------------------------------------------------
  1744. pData->singleMutex.unlock();
  1745. return true;
  1746. }
  1747. void bufferSizeChanged(const uint32_t newBufferSize) override
  1748. {
  1749. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1750. carla_debug("CarlaPluginNative::bufferSizeChanged(%i)", newBufferSize);
  1751. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1752. {
  1753. if (fAudioInBuffers[i] != nullptr)
  1754. delete[] fAudioInBuffers[i];
  1755. fAudioInBuffers[i] = new float[newBufferSize];
  1756. }
  1757. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1758. {
  1759. if (fAudioOutBuffers[i] != nullptr)
  1760. delete[] fAudioOutBuffers[i];
  1761. fAudioOutBuffers[i] = new float[newBufferSize];
  1762. }
  1763. if (fCurBufferSize == newBufferSize)
  1764. return;
  1765. fCurBufferSize = newBufferSize;
  1766. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1767. {
  1768. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, static_cast<intptr_t>(newBufferSize), nullptr, 0.0f);
  1769. if (fHandle2 != nullptr)
  1770. fDescriptor->dispatcher(fHandle2, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, static_cast<intptr_t>(newBufferSize), nullptr, 0.0f);
  1771. }
  1772. }
  1773. void sampleRateChanged(const double newSampleRate) override
  1774. {
  1775. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1776. carla_debug("CarlaPluginNative::sampleRateChanged(%g)", newSampleRate);
  1777. if (carla_isEqual(fCurSampleRate, newSampleRate))
  1778. return;
  1779. fCurSampleRate = newSampleRate;
  1780. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1781. {
  1782. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  1783. if (fHandle2 != nullptr)
  1784. fDescriptor->dispatcher(fHandle2, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  1785. }
  1786. }
  1787. void offlineModeChanged(const bool isOffline) override
  1788. {
  1789. if (fIsOffline == isOffline)
  1790. return;
  1791. fIsOffline = isOffline;
  1792. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1793. {
  1794. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  1795. if (fHandle2 != nullptr)
  1796. fDescriptor->dispatcher(fHandle2, NATIVE_PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  1797. }
  1798. }
  1799. // -------------------------------------------------------------------
  1800. // Plugin buffers
  1801. void initBuffers() const noexcept override
  1802. {
  1803. CarlaPlugin::initBuffers();
  1804. fMidiIn.initBuffers(pData->event.portIn);
  1805. fMidiOut.initBuffers();
  1806. }
  1807. void clearBuffers() noexcept override
  1808. {
  1809. carla_debug("CarlaPluginNative::clearBuffers() - start");
  1810. if (fAudioInBuffers != nullptr)
  1811. {
  1812. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1813. {
  1814. if (fAudioInBuffers[i] != nullptr)
  1815. {
  1816. delete[] fAudioInBuffers[i];
  1817. fAudioInBuffers[i] = nullptr;
  1818. }
  1819. }
  1820. delete[] fAudioInBuffers;
  1821. fAudioInBuffers = nullptr;
  1822. }
  1823. if (fAudioOutBuffers != nullptr)
  1824. {
  1825. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1826. {
  1827. if (fAudioOutBuffers[i] != nullptr)
  1828. {
  1829. delete[] fAudioOutBuffers[i];
  1830. fAudioOutBuffers[i] = nullptr;
  1831. }
  1832. }
  1833. delete[] fAudioOutBuffers;
  1834. fAudioOutBuffers = nullptr;
  1835. }
  1836. if (fMidiIn.count > 1)
  1837. pData->event.portIn = nullptr;
  1838. if (fMidiOut.count > 1)
  1839. pData->event.portOut = nullptr;
  1840. fMidiIn.clear();
  1841. fMidiOut.clear();
  1842. CarlaPlugin::clearBuffers();
  1843. carla_debug("CarlaPluginNative::clearBuffers() - end");
  1844. }
  1845. // -------------------------------------------------------------------
  1846. // Post-poned UI Stuff
  1847. void uiParameterChange(const uint32_t index, const float value) noexcept override
  1848. {
  1849. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1850. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1851. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  1852. if (! fIsUiVisible)
  1853. return;
  1854. if (fDescriptor->ui_set_parameter_value != nullptr)
  1855. fDescriptor->ui_set_parameter_value(fHandle, index, value);
  1856. }
  1857. void uiMidiProgramChange(const uint32_t index) noexcept override
  1858. {
  1859. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1860. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1861. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  1862. if (! fIsUiVisible)
  1863. return;
  1864. if (index >= pData->midiprog.count)
  1865. return;
  1866. if (fDescriptor->ui_set_midi_program != nullptr)
  1867. fDescriptor->ui_set_midi_program(fHandle, 0, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  1868. }
  1869. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  1870. {
  1871. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1872. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1873. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1874. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1875. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1876. if (! fIsUiVisible)
  1877. return;
  1878. // TODO
  1879. }
  1880. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  1881. {
  1882. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1883. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1884. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1885. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1886. if (! fIsUiVisible)
  1887. return;
  1888. if (fDescriptor == nullptr || fHandle == nullptr)
  1889. return;
  1890. // TODO
  1891. }
  1892. // -------------------------------------------------------------------
  1893. const NativeInlineDisplayImageSurface* renderInlineDisplay(const uint32_t width, const uint32_t height)
  1894. {
  1895. CARLA_SAFE_ASSERT_RETURN(fDescriptor->hints & NATIVE_PLUGIN_HAS_INLINE_DISPLAY, nullptr);
  1896. CARLA_SAFE_ASSERT_RETURN(fDescriptor->render_inline_display, nullptr);
  1897. CARLA_SAFE_ASSERT_RETURN(width > 0, nullptr);
  1898. CARLA_SAFE_ASSERT_RETURN(height > 0, nullptr);
  1899. return fDescriptor->render_inline_display(fHandle, width, height);
  1900. }
  1901. // -------------------------------------------------------------------
  1902. protected:
  1903. const NativeTimeInfo* handleGetTimeInfo() const noexcept
  1904. {
  1905. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, nullptr);
  1906. return &fTimeInfo;
  1907. }
  1908. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  1909. {
  1910. CARLA_SAFE_ASSERT_RETURN(pData->enabled, false);
  1911. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, false);
  1912. CARLA_SAFE_ASSERT_RETURN(fMidiOut.count > 0 || pData->event.portOut != nullptr, false);
  1913. CARLA_SAFE_ASSERT_RETURN(event != nullptr, false);
  1914. CARLA_SAFE_ASSERT_RETURN(event->data[0] != 0, false);
  1915. if (fMidiEventOutCount == kPluginMaxMidiEvents)
  1916. {
  1917. carla_stdout("CarlaPluginNative::handleWriteMidiEvent(%p) - buffer full", event);
  1918. return false;
  1919. }
  1920. std::memcpy(&fMidiOutEvents[fMidiEventOutCount++], event, sizeof(NativeMidiEvent));
  1921. return true;
  1922. }
  1923. void handleUiParameterChanged(const uint32_t index, const float value)
  1924. {
  1925. setParameterValue(index, value, false, true, true);
  1926. }
  1927. void handleUiCustomDataChanged(const char* const key, const char* const value)
  1928. {
  1929. setCustomData(CUSTOM_DATA_TYPE_STRING, key, value, false);
  1930. }
  1931. void handleUiClosed()
  1932. {
  1933. pData->engine->callback(true, true, ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0, 0.0f, nullptr);
  1934. fIsUiVisible = false;
  1935. }
  1936. const char* handleUiOpenFile(const bool isDir, const char* const title, const char* const filter)
  1937. {
  1938. return pData->engine->runFileCallback(FILE_CALLBACK_OPEN, isDir, title, filter);
  1939. }
  1940. const char* handleUiSaveFile(const bool isDir, const char* const title, const char* const filter)
  1941. {
  1942. return pData->engine->runFileCallback(FILE_CALLBACK_SAVE, isDir, title, filter);
  1943. }
  1944. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  1945. {
  1946. carla_debug("CarlaPluginNative::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)", opcode, index, value, ptr, opt);
  1947. intptr_t ret = 0;
  1948. switch (opcode)
  1949. {
  1950. case NATIVE_HOST_OPCODE_NULL:
  1951. break;
  1952. case NATIVE_HOST_OPCODE_UPDATE_PARAMETER:
  1953. // TODO
  1954. pData->engine->callback(true, true, ENGINE_CALLBACK_UPDATE, pData->id, -1, 0, 0, 0.0f, nullptr);
  1955. break;
  1956. case NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  1957. // TODO
  1958. pData->engine->callback(true, true, ENGINE_CALLBACK_UPDATE, pData->id, -1, 0, 0, 0.0f, nullptr);
  1959. break;
  1960. case NATIVE_HOST_OPCODE_RELOAD_PARAMETERS:
  1961. reload(); // FIXME
  1962. pData->engine->callback(true, true, ENGINE_CALLBACK_RELOAD_PARAMETERS, pData->id, -1, 0, 0, 0.0f, nullptr);
  1963. break;
  1964. case NATIVE_HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  1965. reloadPrograms(false);
  1966. pData->engine->callback(true, true, ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, -1, 0, 0, 0.0f, nullptr);
  1967. break;
  1968. case NATIVE_HOST_OPCODE_RELOAD_ALL:
  1969. reload();
  1970. pData->engine->callback(true, true, ENGINE_CALLBACK_RELOAD_ALL, pData->id, -1, 0, 0, 0.0f, nullptr);
  1971. break;
  1972. case NATIVE_HOST_OPCODE_UI_UNAVAILABLE:
  1973. pData->engine->callback(true, true, ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0, 0.0f, nullptr);
  1974. fIsUiAvailable = false;
  1975. break;
  1976. case NATIVE_HOST_OPCODE_HOST_IDLE:
  1977. pData->engine->callback(true, false, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  1978. break;
  1979. case NATIVE_HOST_OPCODE_INTERNAL_PLUGIN:
  1980. ret = 1;
  1981. break;
  1982. case NATIVE_HOST_OPCODE_QUEUE_INLINE_DISPLAY:
  1983. fInlineDisplayNeedsRedraw = true;
  1984. break;
  1985. case NATIVE_HOST_OPCODE_UI_TOUCH_PARAMETER:
  1986. CARLA_SAFE_ASSERT_RETURN(index >= 0, 0);
  1987. pData->engine->touchPluginParameter(pData->id, static_cast<uint32_t>(index), value != 0);
  1988. break;
  1989. }
  1990. return ret;
  1991. // unused for now
  1992. (void)ptr;
  1993. (void)opt;
  1994. }
  1995. // -------------------------------------------------------------------
  1996. public:
  1997. void* getNativeHandle() const noexcept override
  1998. {
  1999. return fHandle;
  2000. }
  2001. const void* getNativeDescriptor() const noexcept override
  2002. {
  2003. return fDescriptor;
  2004. }
  2005. // -------------------------------------------------------------------
  2006. bool init(const char* const name, const char* const label, const uint options)
  2007. {
  2008. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  2009. // ---------------------------------------------------------------
  2010. // first checks
  2011. if (pData->client != nullptr)
  2012. {
  2013. pData->engine->setLastError("Plugin client is already registered");
  2014. return false;
  2015. }
  2016. if (label == nullptr || label[0] == '\0')
  2017. {
  2018. pData->engine->setLastError("null label");
  2019. return false;
  2020. }
  2021. // ---------------------------------------------------------------
  2022. // get descriptor that matches label
  2023. sPluginInitializer.initIfNeeded();
  2024. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = gPluginDescriptors.begin2(); it.valid(); it.next())
  2025. {
  2026. fDescriptor = it.getValue(nullptr);
  2027. CARLA_SAFE_ASSERT_BREAK(fDescriptor != nullptr);
  2028. carla_debug("Check vs \"%s\"", fDescriptor->label);
  2029. if (fDescriptor->label != nullptr && std::strcmp(fDescriptor->label, label) == 0)
  2030. break;
  2031. fDescriptor = nullptr;
  2032. }
  2033. if (fDescriptor == nullptr)
  2034. {
  2035. pData->engine->setLastError("Invalid internal plugin");
  2036. return false;
  2037. }
  2038. // ---------------------------------------------------------------
  2039. // set icon
  2040. /**/ if (std::strcmp(fDescriptor->label, "audiofile") == 0)
  2041. pData->iconName = carla_strdup_safe("file");
  2042. else if (std::strcmp(fDescriptor->label, "midifile") == 0)
  2043. pData->iconName = carla_strdup_safe("file");
  2044. else if (std::strcmp(fDescriptor->label, "3bandeq") == 0)
  2045. pData->iconName = carla_strdup_safe("distrho");
  2046. else if (std::strcmp(fDescriptor->label, "3bandsplitter") == 0)
  2047. pData->iconName = carla_strdup_safe("distrho");
  2048. else if (std::strcmp(fDescriptor->label, "kars") == 0)
  2049. pData->iconName = carla_strdup_safe("distrho");
  2050. else if (std::strcmp(fDescriptor->label, "nekobi") == 0)
  2051. pData->iconName = carla_strdup_safe("distrho");
  2052. else if (std::strcmp(fDescriptor->label, "pingpongpan") == 0)
  2053. pData->iconName = carla_strdup_safe("distrho");
  2054. // ---------------------------------------------------------------
  2055. // set info
  2056. if (name != nullptr && name[0] != '\0')
  2057. pData->name = pData->engine->getUniquePluginName(name);
  2058. else if (fDescriptor->name != nullptr && fDescriptor->name[0] != '\0')
  2059. pData->name = pData->engine->getUniquePluginName(fDescriptor->name);
  2060. else
  2061. pData->name = pData->engine->getUniquePluginName(label);
  2062. {
  2063. CARLA_ASSERT(fHost.uiName == nullptr);
  2064. char uiName[std::strlen(pData->name)+6+1];
  2065. std::strcpy(uiName, pData->name);
  2066. std::strcat(uiName, " (GUI)");
  2067. fHost.uiName = carla_strdup(uiName);
  2068. }
  2069. // ---------------------------------------------------------------
  2070. // register client
  2071. pData->client = pData->engine->addClient(this);
  2072. if (pData->client == nullptr || ! pData->client->isOk())
  2073. {
  2074. pData->engine->setLastError("Failed to register plugin client");
  2075. return false;
  2076. }
  2077. // ---------------------------------------------------------------
  2078. // initialize plugin
  2079. fHandle = fDescriptor->instantiate(&fHost);
  2080. if (fHandle == nullptr)
  2081. {
  2082. pData->engine->setLastError("Plugin failed to initialize");
  2083. return false;
  2084. }
  2085. // ---------------------------------------------------------------
  2086. // set default options
  2087. bool hasMidiProgs = false;
  2088. if (fDescriptor->get_midi_program_count != nullptr)
  2089. {
  2090. try {
  2091. hasMidiProgs = fDescriptor->get_midi_program_count(fHandle) > 0;
  2092. } catch (...) {}
  2093. }
  2094. pData->options = 0x0;
  2095. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS)
  2096. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  2097. else if (options & PLUGIN_OPTION_FIXED_BUFFERS)
  2098. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  2099. if (pData->engine->getOptions().forceStereo)
  2100. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  2101. else if (options & PLUGIN_OPTION_FORCE_STEREO)
  2102. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  2103. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CHANNEL_PRESSURE)
  2104. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  2105. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_NOTE_AFTERTOUCH)
  2106. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  2107. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PITCHBEND)
  2108. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  2109. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_ALL_SOUND_OFF)
  2110. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  2111. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CONTROL_CHANGES)
  2112. {
  2113. if (options & PLUGIN_OPTION_SEND_CONTROL_CHANGES)
  2114. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  2115. }
  2116. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PROGRAM_CHANGES)
  2117. {
  2118. CARLA_SAFE_ASSERT(! hasMidiProgs);
  2119. pData->options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
  2120. }
  2121. else if (hasMidiProgs)
  2122. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  2123. return true;
  2124. }
  2125. private:
  2126. NativePluginHandle fHandle;
  2127. NativePluginHandle fHandle2;
  2128. NativeHostDescriptor fHost;
  2129. const NativePluginDescriptor* fDescriptor;
  2130. bool fIsProcessing;
  2131. bool fIsOffline;
  2132. bool fIsUiAvailable;
  2133. bool fIsUiVisible;
  2134. bool fInlineDisplayNeedsRedraw;
  2135. float** fAudioInBuffers;
  2136. float** fAudioOutBuffers;
  2137. uint32_t fMidiEventInCount;
  2138. uint32_t fMidiEventOutCount;
  2139. NativeMidiEvent fMidiInEvents[kPluginMaxMidiEvents];
  2140. NativeMidiEvent fMidiOutEvents[kPluginMaxMidiEvents];
  2141. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  2142. uint32_t fCurBufferSize;
  2143. double fCurSampleRate;
  2144. NativePluginMidiInData fMidiIn;
  2145. NativePluginMidiOutData fMidiOut;
  2146. NativeTimeInfo fTimeInfo;
  2147. // -------------------------------------------------------------------
  2148. #define handlePtr ((CarlaPluginNative*)handle)
  2149. static uint32_t carla_host_get_buffer_size(NativeHostHandle handle) noexcept
  2150. {
  2151. return handlePtr->fCurBufferSize;
  2152. }
  2153. static double carla_host_get_sample_rate(NativeHostHandle handle) noexcept
  2154. {
  2155. return handlePtr->fCurSampleRate;
  2156. }
  2157. static bool carla_host_is_offline(NativeHostHandle handle) noexcept
  2158. {
  2159. return handlePtr->fIsOffline;
  2160. }
  2161. static const NativeTimeInfo* carla_host_get_time_info(NativeHostHandle handle) noexcept
  2162. {
  2163. return handlePtr->handleGetTimeInfo();
  2164. }
  2165. static bool carla_host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  2166. {
  2167. return handlePtr->handleWriteMidiEvent(event);
  2168. }
  2169. static void carla_host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  2170. {
  2171. handlePtr->handleUiParameterChanged(index, value);
  2172. }
  2173. static void carla_host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  2174. {
  2175. handlePtr->handleUiCustomDataChanged(key, value);
  2176. }
  2177. static void carla_host_ui_closed(NativeHostHandle handle)
  2178. {
  2179. handlePtr->handleUiClosed();
  2180. }
  2181. static const char* carla_host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  2182. {
  2183. return handlePtr->handleUiOpenFile(isDir, title, filter);
  2184. }
  2185. static const char* carla_host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  2186. {
  2187. return handlePtr->handleUiSaveFile(isDir, title, filter);
  2188. }
  2189. static intptr_t carla_host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  2190. {
  2191. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  2192. }
  2193. #undef handlePtr
  2194. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginNative)
  2195. };
  2196. // -----------------------------------------------------------------------
  2197. CarlaPlugin* CarlaPlugin::newNative(const Initializer& init)
  2198. {
  2199. carla_debug("CarlaPlugin::newNative({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "})", init.engine, init.filename, init.name, init.label, init.uniqueId);
  2200. CarlaPluginNative* const plugin(new CarlaPluginNative(init.engine, init.id));
  2201. if (! plugin->init(init.name, init.label, init.options))
  2202. {
  2203. delete plugin;
  2204. return nullptr;
  2205. }
  2206. return plugin;
  2207. }
  2208. // used in CarlaStandalone.cpp
  2209. const void* carla_render_inline_display_internal(CarlaPlugin* plugin, uint32_t width, uint32_t height);
  2210. const void* carla_render_inline_display_internal(CarlaPlugin* plugin, uint32_t width, uint32_t height)
  2211. {
  2212. CarlaPluginNative* const nativePlugin = (CarlaPluginNative*)plugin;
  2213. return nativePlugin->renderInlineDisplay(width, height);
  2214. }
  2215. // -----------------------------------------------------------------------
  2216. CARLA_BACKEND_END_NAMESPACE