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.

2761 lines
98KB

  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. fAudioInBuffers(nullptr),
  201. fAudioOutBuffers(nullptr),
  202. fMidiEventInCount(0),
  203. fMidiEventOutCount(0),
  204. fCurBufferSize(engine->getBufferSize()),
  205. fCurSampleRate(engine->getSampleRate()),
  206. fMidiIn(),
  207. fMidiOut(),
  208. fTimeInfo()
  209. {
  210. carla_debug("CarlaPluginNative::CarlaPluginNative(%p, %i)", engine, id);
  211. carla_fill(fCurMidiProgs, 0, MAX_MIDI_CHANNELS);
  212. carla_zeroStructs(fMidiInEvents, kPluginMaxMidiEvents);
  213. carla_zeroStructs(fMidiOutEvents, kPluginMaxMidiEvents);
  214. carla_zeroStruct(fTimeInfo);
  215. fHost.handle = this;
  216. fHost.resourceDir = carla_strdup(engine->getOptions().resourceDir);
  217. fHost.uiName = nullptr;
  218. fHost.uiParentId = engine->getOptions().frontendWinId;
  219. fHost.get_buffer_size = carla_host_get_buffer_size;
  220. fHost.get_sample_rate = carla_host_get_sample_rate;
  221. fHost.is_offline = carla_host_is_offline;
  222. fHost.get_time_info = carla_host_get_time_info;
  223. fHost.write_midi_event = carla_host_write_midi_event;
  224. fHost.ui_parameter_changed = carla_host_ui_parameter_changed;
  225. fHost.ui_parameter_touch = carla_host_ui_parameter_touch;
  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. // extra plugin hints
  1027. pData->extraHints = 0x0;
  1028. bufferSizeChanged(pData->engine->getBufferSize());
  1029. reloadPrograms(true);
  1030. if (pData->active)
  1031. activate();
  1032. carla_debug("CarlaPluginNative::reload() - end");
  1033. }
  1034. void reloadPrograms(const bool doInit) override
  1035. {
  1036. carla_debug("CarlaPluginNative::reloadPrograms(%s)", bool2str(doInit));
  1037. uint32_t i, oldCount = pData->midiprog.count;
  1038. const int32_t current = pData->midiprog.current;
  1039. // Delete old programs
  1040. pData->midiprog.clear();
  1041. // Query new programs
  1042. uint32_t count = 0;
  1043. if (fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_info != nullptr && fDescriptor->set_midi_program != nullptr)
  1044. count = fDescriptor->get_midi_program_count(fHandle);
  1045. if (count > 0)
  1046. {
  1047. pData->midiprog.createNew(count);
  1048. // Update data
  1049. for (i=0; i < count; ++i)
  1050. {
  1051. const NativeMidiProgram* const mpDesc(fDescriptor->get_midi_program_info(fHandle, i));
  1052. CARLA_SAFE_ASSERT_CONTINUE(mpDesc != nullptr);
  1053. pData->midiprog.data[i].bank = mpDesc->bank;
  1054. pData->midiprog.data[i].program = mpDesc->program;
  1055. pData->midiprog.data[i].name = carla_strdup(mpDesc->name);
  1056. }
  1057. }
  1058. if (doInit)
  1059. {
  1060. if (count > 0)
  1061. setMidiProgram(0, false, false, false, true);
  1062. }
  1063. else
  1064. {
  1065. // Check if current program is invalid
  1066. bool programChanged = false;
  1067. if (count == oldCount+1)
  1068. {
  1069. // one midi program added, probably created by user
  1070. pData->midiprog.current = static_cast<int32_t>(oldCount);
  1071. programChanged = true;
  1072. }
  1073. else if (current < 0 && count > 0)
  1074. {
  1075. // programs exist now, but not before
  1076. pData->midiprog.current = 0;
  1077. programChanged = true;
  1078. }
  1079. else if (current >= 0 && count == 0)
  1080. {
  1081. // programs existed before, but not anymore
  1082. pData->midiprog.current = -1;
  1083. programChanged = true;
  1084. }
  1085. else if (current >= static_cast<int32_t>(count))
  1086. {
  1087. // current midi program > count
  1088. pData->midiprog.current = 0;
  1089. programChanged = true;
  1090. }
  1091. else
  1092. {
  1093. // no change
  1094. pData->midiprog.current = current;
  1095. }
  1096. if (programChanged)
  1097. setMidiProgram(pData->midiprog.current, true, true, true, false);
  1098. pData->engine->callback(true, true,
  1099. ENGINE_CALLBACK_RELOAD_PROGRAMS,
  1100. pData->id,
  1101. 0, 0, 0, 0.0f, nullptr);
  1102. }
  1103. }
  1104. // -------------------------------------------------------------------
  1105. // Plugin processing
  1106. void activate() noexcept override
  1107. {
  1108. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1109. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1110. if (fDescriptor->activate != nullptr)
  1111. {
  1112. try {
  1113. fDescriptor->activate(fHandle);
  1114. } catch(...) {}
  1115. if (fHandle2 != nullptr)
  1116. {
  1117. try {
  1118. fDescriptor->activate(fHandle2);
  1119. } catch(...) {}
  1120. }
  1121. }
  1122. }
  1123. void deactivate() noexcept override
  1124. {
  1125. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1126. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1127. if (fDescriptor->deactivate != nullptr)
  1128. {
  1129. try {
  1130. fDescriptor->deactivate(fHandle);
  1131. } catch(...) {}
  1132. if (fHandle2 != nullptr)
  1133. {
  1134. try {
  1135. fDescriptor->deactivate(fHandle2);
  1136. } catch(...) {}
  1137. }
  1138. }
  1139. }
  1140. const EngineEvent& findNextEvent()
  1141. {
  1142. if (fMidiIn.count == 1)
  1143. {
  1144. NativePluginMidiInData::MultiPortData& multiportData(fMidiIn.multiportData[0]);
  1145. if (multiportData.usedIndex == multiportData.cachedEventCount)
  1146. {
  1147. const uint32_t eventCount = pData->event.portIn->getEventCount();
  1148. CARLA_SAFE_ASSERT_INT2(eventCount == multiportData.cachedEventCount,
  1149. eventCount, multiportData.cachedEventCount);
  1150. return kNullEngineEvent;
  1151. }
  1152. return pData->event.portIn->getEvent(multiportData.usedIndex++);
  1153. }
  1154. uint32_t lowestSampleTime = 9999999;
  1155. uint32_t portMatching = 0;
  1156. bool found = false;
  1157. // process events in order for multiple ports
  1158. for (uint32_t m=0; m < fMidiIn.count; ++m)
  1159. {
  1160. CarlaEngineEventPort* const eventPort(fMidiIn.ports[m]);
  1161. NativePluginMidiInData::MultiPortData& multiportData(fMidiIn.multiportData[m]);
  1162. if (multiportData.usedIndex == multiportData.cachedEventCount)
  1163. continue;
  1164. const EngineEvent& event(eventPort->getEventUnchecked(multiportData.usedIndex));
  1165. if (event.time < lowestSampleTime)
  1166. {
  1167. lowestSampleTime = event.time;
  1168. portMatching = m;
  1169. found = true;
  1170. }
  1171. }
  1172. if (found)
  1173. {
  1174. CarlaEngineEventPort* const eventPort(fMidiIn.ports[portMatching]);
  1175. NativePluginMidiInData::MultiPortData& multiportData(fMidiIn.multiportData[portMatching]);
  1176. return eventPort->getEvent(multiportData.usedIndex++);
  1177. }
  1178. return kNullEngineEvent;
  1179. }
  1180. void process(const float** const audioIn, float** const audioOut, const float** const, float** const, const uint32_t frames) override
  1181. {
  1182. // --------------------------------------------------------------------------------------------------------
  1183. // Check if active
  1184. if (! pData->active)
  1185. {
  1186. // disable any output sound
  1187. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1188. carla_zeroFloats(audioOut[i], frames);
  1189. return;
  1190. }
  1191. fMidiEventInCount = fMidiEventOutCount = 0;
  1192. carla_zeroStructs(fMidiInEvents, kPluginMaxMidiEvents);
  1193. carla_zeroStructs(fMidiOutEvents, kPluginMaxMidiEvents);
  1194. // --------------------------------------------------------------------------------------------------------
  1195. // Check if needs reset
  1196. if (pData->needsReset)
  1197. {
  1198. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1199. {
  1200. fMidiEventInCount = MAX_MIDI_CHANNELS*2;
  1201. for (uint8_t k=0, i=MAX_MIDI_CHANNELS; k < MAX_MIDI_CHANNELS; ++k)
  1202. {
  1203. fMidiInEvents[k].data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
  1204. fMidiInEvents[k].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1205. fMidiInEvents[k].data[2] = 0;
  1206. fMidiInEvents[k].size = 3;
  1207. fMidiInEvents[k+i].data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
  1208. fMidiInEvents[k+i].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1209. fMidiInEvents[k+i].data[2] = 0;
  1210. fMidiInEvents[k+i].size = 3;
  1211. }
  1212. }
  1213. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  1214. {
  1215. fMidiEventInCount = MAX_MIDI_NOTE;
  1216. for (uint8_t k=0; k < MAX_MIDI_NOTE; ++k)
  1217. {
  1218. fMidiInEvents[k].data[0] = uint8_t(MIDI_STATUS_NOTE_OFF | (pData->ctrlChannel & MIDI_CHANNEL_BIT));
  1219. fMidiInEvents[k].data[1] = k;
  1220. fMidiInEvents[k].data[2] = 0;
  1221. fMidiInEvents[k].size = 3;
  1222. }
  1223. }
  1224. pData->needsReset = false;
  1225. }
  1226. // --------------------------------------------------------------------------------------------------------
  1227. // Set TimeInfo
  1228. const EngineTimeInfo timeInfo(pData->engine->getTimeInfo());
  1229. fTimeInfo.playing = timeInfo.playing;
  1230. fTimeInfo.frame = timeInfo.frame;
  1231. fTimeInfo.usecs = timeInfo.usecs;
  1232. if (timeInfo.bbt.valid)
  1233. {
  1234. fTimeInfo.bbt.valid = true;
  1235. fTimeInfo.bbt.bar = timeInfo.bbt.bar;
  1236. fTimeInfo.bbt.beat = timeInfo.bbt.beat;
  1237. fTimeInfo.bbt.tick = timeInfo.bbt.tick;
  1238. fTimeInfo.bbt.barStartTick = timeInfo.bbt.barStartTick;
  1239. fTimeInfo.bbt.beatsPerBar = timeInfo.bbt.beatsPerBar;
  1240. fTimeInfo.bbt.beatType = timeInfo.bbt.beatType;
  1241. fTimeInfo.bbt.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
  1242. fTimeInfo.bbt.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
  1243. }
  1244. else
  1245. {
  1246. fTimeInfo.bbt.valid = false;
  1247. }
  1248. #if 0
  1249. // This test code has proven to be quite useful
  1250. // So I am leaving it behind, I might need it again..
  1251. if (pData->id == 1)
  1252. {
  1253. static int64_t last_frame = timeInfo.frame;
  1254. static int64_t last_dev_frame = 0;
  1255. static double last_val = timeInfo.bbt.barStartTick + ((timeInfo.bbt.beat-1) * timeInfo.bbt.ticksPerBeat) + timeInfo.bbt.tick;
  1256. static double last_dev_val = 0.0;
  1257. int64_t cur_frame = timeInfo.frame;
  1258. int64_t cur_dev_frame = cur_frame - last_frame;
  1259. double cur_val = timeInfo.bbt.barStartTick + ((timeInfo.bbt.beat-1) * timeInfo.bbt.ticksPerBeat) + timeInfo.bbt.tick;
  1260. double cur_dev_val = cur_val - last_val;
  1261. if (std::abs(last_dev_val - cur_dev_val) >= 0.0001 || last_dev_frame != cur_dev_frame)
  1262. {
  1263. carla_stdout("currently %u at %u => %f : DEV1: %li : DEV2: %f",
  1264. frames,
  1265. timeInfo.frame,
  1266. cur_val,
  1267. cur_dev_frame,
  1268. cur_dev_val);
  1269. }
  1270. last_val = cur_val;
  1271. last_dev_val = cur_dev_val;
  1272. last_frame = cur_frame;
  1273. last_dev_frame = cur_dev_frame;
  1274. }
  1275. #endif
  1276. // --------------------------------------------------------------------------------------------------------
  1277. // Event Input and Processing
  1278. if (pData->event.portIn != nullptr)
  1279. {
  1280. // ----------------------------------------------------------------------------------------------------
  1281. // MIDI Input (External)
  1282. if (pData->extNotes.mutex.tryLock())
  1283. {
  1284. ExternalMidiNote note = { 0, 0, 0 };
  1285. for (; fMidiEventInCount < kPluginMaxMidiEvents && ! pData->extNotes.data.isEmpty();)
  1286. {
  1287. note = pData->extNotes.data.getFirst(note, true);
  1288. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  1289. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1290. nativeEvent.data[0] = uint8_t((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  1291. nativeEvent.data[1] = note.note;
  1292. nativeEvent.data[2] = note.velo;
  1293. nativeEvent.size = 3;
  1294. }
  1295. pData->extNotes.mutex.unlock();
  1296. } // End of MIDI Input (External)
  1297. // ----------------------------------------------------------------------------------------------------
  1298. // Event Input (System)
  1299. #ifndef BUILD_BRIDGE
  1300. bool allNotesOffSent = false;
  1301. #endif
  1302. bool sampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  1303. uint32_t startTime = 0;
  1304. uint32_t timeOffset = 0;
  1305. uint32_t nextBankId;
  1306. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1307. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1308. else
  1309. nextBankId = 0;
  1310. for (;;)
  1311. {
  1312. const EngineEvent& event(findNextEvent());
  1313. if (event.type == kEngineEventTypeNull)
  1314. break;
  1315. uint32_t eventTime = event.time;
  1316. CARLA_SAFE_ASSERT_UINT2_CONTINUE(eventTime < frames, eventTime, frames);
  1317. if (eventTime < timeOffset)
  1318. {
  1319. carla_stderr2("Timing error, eventTime:%u < timeOffset:%u for '%s'",
  1320. eventTime, timeOffset, pData->name);
  1321. eventTime = timeOffset;
  1322. }
  1323. if (sampleAccurate && eventTime > timeOffset)
  1324. {
  1325. if (processSingle(audioIn, audioOut, eventTime - timeOffset, timeOffset))
  1326. {
  1327. startTime = 0;
  1328. timeOffset = eventTime;
  1329. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1330. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1331. else
  1332. nextBankId = 0;
  1333. if (fMidiEventInCount > 0)
  1334. {
  1335. carla_zeroStructs(fMidiInEvents, fMidiEventInCount);
  1336. fMidiEventInCount = 0;
  1337. }
  1338. if (fMidiEventOutCount > 0)
  1339. {
  1340. carla_zeroStructs(fMidiOutEvents, fMidiEventOutCount);
  1341. fMidiEventOutCount = 0;
  1342. }
  1343. }
  1344. else
  1345. startTime += timeOffset;
  1346. }
  1347. // Control change
  1348. switch (event.type)
  1349. {
  1350. case kEngineEventTypeNull:
  1351. break;
  1352. case kEngineEventTypeControl: {
  1353. const EngineControlEvent& ctrlEvent = event.ctrl;
  1354. switch (ctrlEvent.type)
  1355. {
  1356. case kEngineControlEventTypeNull:
  1357. break;
  1358. case kEngineControlEventTypeParameter: {
  1359. #ifndef BUILD_BRIDGE
  1360. // Control backend stuff
  1361. if (event.channel == pData->ctrlChannel)
  1362. {
  1363. float value;
  1364. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) > 0)
  1365. {
  1366. value = ctrlEvent.value;
  1367. setDryWetRT(value);
  1368. }
  1369. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) > 0)
  1370. {
  1371. value = ctrlEvent.value*127.0f/100.0f;
  1372. setVolumeRT(value);
  1373. }
  1374. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) > 0)
  1375. {
  1376. float left, right;
  1377. value = ctrlEvent.value/0.5f - 1.0f;
  1378. if (value < 0.0f)
  1379. {
  1380. left = -1.0f;
  1381. right = (value*2.0f)+1.0f;
  1382. }
  1383. else if (value > 0.0f)
  1384. {
  1385. left = (value*2.0f)-1.0f;
  1386. right = 1.0f;
  1387. }
  1388. else
  1389. {
  1390. left = -1.0f;
  1391. right = 1.0f;
  1392. }
  1393. setBalanceLeftRT(left);
  1394. setBalanceRightRT(right);
  1395. }
  1396. }
  1397. #endif
  1398. // Control plugin parameters
  1399. for (uint32_t k=0; k < pData->param.count; ++k)
  1400. {
  1401. if (pData->param.data[k].midiChannel != event.channel)
  1402. continue;
  1403. if (pData->param.data[k].midiCC != ctrlEvent.param)
  1404. continue;
  1405. if (pData->param.data[k].type != PARAMETER_INPUT)
  1406. continue;
  1407. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1408. continue;
  1409. float value;
  1410. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1411. {
  1412. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  1413. }
  1414. else
  1415. {
  1416. if (pData->param.data[k].hints & PARAMETER_IS_LOGARITHMIC)
  1417. value = pData->param.ranges[k].getUnnormalizedLogValue(ctrlEvent.value);
  1418. else
  1419. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  1420. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1421. value = std::rint(value);
  1422. }
  1423. setParameterValueRT(k, value);
  1424. }
  1425. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_CONTROL)
  1426. {
  1427. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1428. continue;
  1429. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1430. carla_zeroStruct(nativeEvent);
  1431. nativeEvent.time = sampleAccurate ? startTime : eventTime;
  1432. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1433. nativeEvent.data[1] = uint8_t(ctrlEvent.param);
  1434. nativeEvent.data[2] = uint8_t(ctrlEvent.value*127.0f);
  1435. nativeEvent.size = 3;
  1436. }
  1437. break;
  1438. } // case kEngineControlEventTypeParameter
  1439. case kEngineControlEventTypeMidiBank:
  1440. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  1441. {
  1442. if (event.channel == pData->ctrlChannel)
  1443. nextBankId = ctrlEvent.param;
  1444. }
  1445. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  1446. {
  1447. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1448. continue;
  1449. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1450. carla_zeroStruct(nativeEvent);
  1451. nativeEvent.time = sampleAccurate ? startTime : eventTime;
  1452. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1453. nativeEvent.data[1] = MIDI_CONTROL_BANK_SELECT;
  1454. nativeEvent.data[2] = uint8_t(ctrlEvent.param);
  1455. nativeEvent.size = 3;
  1456. }
  1457. break;
  1458. case kEngineControlEventTypeMidiProgram:
  1459. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  1460. {
  1461. if (event.channel < MAX_MIDI_CHANNELS)
  1462. {
  1463. const uint32_t nextProgramId(ctrlEvent.param);
  1464. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  1465. {
  1466. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  1467. {
  1468. fDescriptor->set_midi_program(fHandle, event.channel, nextBankId, nextProgramId);
  1469. if (fHandle2 != nullptr)
  1470. fDescriptor->set_midi_program(fHandle2, event.channel, nextBankId, nextProgramId);
  1471. fCurMidiProgs[event.channel] = static_cast<int32_t>(k);
  1472. if (event.channel == pData->ctrlChannel)
  1473. {
  1474. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange,
  1475. static_cast<int32_t>(k),
  1476. 0, 0, 0.0f);
  1477. }
  1478. break;
  1479. }
  1480. }
  1481. }
  1482. }
  1483. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  1484. {
  1485. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1486. continue;
  1487. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1488. carla_zeroStruct(nativeEvent);
  1489. nativeEvent.time = sampleAccurate ? startTime : eventTime;
  1490. nativeEvent.data[0] = uint8_t(MIDI_STATUS_PROGRAM_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1491. nativeEvent.data[1] = uint8_t(ctrlEvent.param);
  1492. nativeEvent.size = 2;
  1493. }
  1494. break;
  1495. case kEngineControlEventTypeAllSoundOff:
  1496. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1497. {
  1498. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1499. continue;
  1500. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1501. carla_zeroStruct(nativeEvent);
  1502. nativeEvent.time = sampleAccurate ? startTime : eventTime;
  1503. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1504. nativeEvent.data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1505. nativeEvent.data[2] = 0;
  1506. nativeEvent.size = 3;
  1507. }
  1508. break;
  1509. case kEngineControlEventTypeAllNotesOff:
  1510. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1511. {
  1512. #ifndef BUILD_BRIDGE
  1513. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1514. {
  1515. allNotesOffSent = true;
  1516. postponeRtAllNotesOff();
  1517. }
  1518. #endif
  1519. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1520. continue;
  1521. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1522. carla_zeroStruct(nativeEvent);
  1523. nativeEvent.time = sampleAccurate ? startTime : eventTime;
  1524. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1525. nativeEvent.data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1526. nativeEvent.data[2] = 0;
  1527. nativeEvent.size = 3;
  1528. }
  1529. break;
  1530. }
  1531. break;
  1532. }
  1533. case kEngineEventTypeMidi: {
  1534. if (fMidiEventInCount >= kPluginMaxMidiEvents)
  1535. continue;
  1536. const EngineMidiEvent& midiEvent(event.midi);
  1537. if (midiEvent.size > 4)
  1538. continue;
  1539. #ifdef CARLA_PROPER_CPP11_SUPPORT
  1540. static_assert(4 <= EngineMidiEvent::kDataSize, "Incorrect data");
  1541. #endif
  1542. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  1543. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1544. continue;
  1545. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1546. continue;
  1547. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1548. continue;
  1549. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1550. continue;
  1551. // Fix bad note-off
  1552. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1553. status = MIDI_STATUS_NOTE_OFF;
  1554. NativeMidiEvent& nativeEvent(fMidiInEvents[fMidiEventInCount++]);
  1555. carla_zeroStruct(nativeEvent);
  1556. nativeEvent.port = midiEvent.port;
  1557. nativeEvent.time = sampleAccurate ? startTime : eventTime;
  1558. nativeEvent.size = midiEvent.size;
  1559. nativeEvent.data[0] = uint8_t(status | (event.channel & MIDI_CHANNEL_BIT));
  1560. nativeEvent.data[1] = midiEvent.size >= 2 ? midiEvent.data[1] : 0;
  1561. nativeEvent.data[2] = midiEvent.size >= 3 ? midiEvent.data[2] : 0;
  1562. nativeEvent.data[3] = midiEvent.size == 4 ? midiEvent.data[3] : 0;
  1563. if (status == MIDI_STATUS_NOTE_ON)
  1564. {
  1565. pData->postponeRtEvent(kPluginPostRtEventNoteOn,
  1566. event.channel,
  1567. midiEvent.data[1],
  1568. midiEvent.data[2],
  1569. 0.0f);
  1570. }
  1571. else if (status == MIDI_STATUS_NOTE_OFF)
  1572. {
  1573. pData->postponeRtEvent(kPluginPostRtEventNoteOff,
  1574. event.channel,
  1575. midiEvent.data[1],
  1576. 0, 0.0f);
  1577. }
  1578. } break;
  1579. } // switch (event.type)
  1580. }
  1581. pData->postRtEvents.trySplice();
  1582. if (frames > timeOffset)
  1583. processSingle(audioIn, audioOut, frames - timeOffset, timeOffset);
  1584. } // End of Event Input and Processing
  1585. // --------------------------------------------------------------------------------------------------------
  1586. // Plugin processing (no events)
  1587. else
  1588. {
  1589. processSingle(audioIn, audioOut, frames, 0);
  1590. } // End of Plugin processing (no events)
  1591. #ifndef BUILD_BRIDGE
  1592. // --------------------------------------------------------------------------------------------------------
  1593. // Control Output
  1594. if (pData->event.portOut != nullptr)
  1595. {
  1596. float value, curValue;
  1597. for (uint32_t k=0; k < pData->param.count; ++k)
  1598. {
  1599. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1600. continue;
  1601. curValue = fDescriptor->get_parameter_value(fHandle, k);
  1602. pData->param.ranges[k].fixValue(curValue);
  1603. if (pData->param.data[k].midiCC > 0)
  1604. {
  1605. value = pData->param.ranges[k].getNormalizedValue(curValue);
  1606. pData->event.portOut->writeControlEvent(0, pData->param.data[k].midiChannel, kEngineControlEventTypeParameter, static_cast<uint16_t>(pData->param.data[k].midiCC), value);
  1607. }
  1608. }
  1609. } // End of Control Output
  1610. #endif
  1611. }
  1612. bool processSingle(const float** const audioIn, float** const audioOut, const uint32_t frames, const uint32_t timeOffset)
  1613. {
  1614. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1615. if (pData->audioIn.count > 0)
  1616. {
  1617. CARLA_SAFE_ASSERT_RETURN(audioIn != nullptr, false);
  1618. }
  1619. if (pData->audioOut.count > 0)
  1620. {
  1621. CARLA_SAFE_ASSERT_RETURN(audioOut != nullptr, false);
  1622. }
  1623. // --------------------------------------------------------------------------------------------------------
  1624. // Try lock, silence otherwise
  1625. if (fIsOffline)
  1626. {
  1627. pData->singleMutex.lock();
  1628. }
  1629. else if (! pData->singleMutex.tryLock())
  1630. {
  1631. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1632. {
  1633. for (uint32_t k=0; k < frames; ++k)
  1634. audioOut[i][k+timeOffset] = 0.0f;
  1635. }
  1636. return false;
  1637. }
  1638. // --------------------------------------------------------------------------------------------------------
  1639. // Set audio buffers
  1640. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1641. carla_copyFloats(fAudioInBuffers[i], audioIn[i]+timeOffset, frames);
  1642. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1643. carla_zeroFloats(fAudioOutBuffers[i], frames);
  1644. // --------------------------------------------------------------------------------------------------------
  1645. // Run plugin
  1646. fIsProcessing = true;
  1647. if (fHandle2 == nullptr)
  1648. {
  1649. fDescriptor->process(fHandle,
  1650. const_cast<const float**>(fAudioInBuffers), fAudioOutBuffers, frames,
  1651. fMidiInEvents, fMidiEventInCount);
  1652. }
  1653. else
  1654. {
  1655. fDescriptor->process(fHandle,
  1656. (pData->audioIn.count > 0) ? const_cast<const float**>(&fAudioInBuffers[0]) : nullptr,
  1657. (pData->audioOut.count > 0) ? &fAudioOutBuffers[0] : nullptr,
  1658. frames, fMidiInEvents, fMidiEventInCount);
  1659. fDescriptor->process(fHandle2,
  1660. (pData->audioIn.count > 0) ? const_cast<const float**>(&fAudioInBuffers[1]) : nullptr,
  1661. (pData->audioOut.count > 0) ? &fAudioOutBuffers[1] : nullptr,
  1662. frames, fMidiInEvents, fMidiEventInCount);
  1663. }
  1664. fIsProcessing = false;
  1665. if (fTimeInfo.playing)
  1666. fTimeInfo.frame += frames;
  1667. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  1668. // --------------------------------------------------------------------------------------------------------
  1669. // Post-processing (dry/wet, volume and balance)
  1670. {
  1671. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && carla_isNotEqual(pData->postProc.dryWet, 1.0f);
  1672. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
  1673. bool isPair;
  1674. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1675. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1676. {
  1677. // Dry/Wet
  1678. if (doDryWet)
  1679. {
  1680. for (uint32_t k=0; k < frames; ++k)
  1681. {
  1682. bufValue = fAudioInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  1683. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1684. }
  1685. }
  1686. // Balance
  1687. if (doBalance)
  1688. {
  1689. isPair = (i % 2 == 0);
  1690. if (isPair)
  1691. {
  1692. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1693. carla_copyFloats(oldBufLeft, fAudioOutBuffers[i], frames);
  1694. }
  1695. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1696. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1697. for (uint32_t k=0; k < frames; ++k)
  1698. {
  1699. if (isPair)
  1700. {
  1701. // left
  1702. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1703. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1704. }
  1705. else
  1706. {
  1707. // right
  1708. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1709. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1710. }
  1711. }
  1712. }
  1713. // Volume (and buffer copy)
  1714. {
  1715. for (uint32_t k=0; k < frames; ++k)
  1716. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  1717. }
  1718. }
  1719. } // End of Post-processing
  1720. #else
  1721. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1722. {
  1723. for (uint32_t k=0; k < frames; ++k)
  1724. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1725. }
  1726. #endif
  1727. // --------------------------------------------------------------------------------------------------------
  1728. // MIDI Output
  1729. if (pData->event.portOut != nullptr)
  1730. {
  1731. for (uint32_t k = 0; k < fMidiEventOutCount; ++k)
  1732. {
  1733. const uint8_t channel = uint8_t(MIDI_GET_CHANNEL_FROM_DATA(fMidiOutEvents[k].data));
  1734. const uint8_t port = fMidiOutEvents[k].port;
  1735. if (fMidiOut.count > 1 && port < fMidiOut.count)
  1736. fMidiOut.ports[port]->writeMidiEvent(fMidiOutEvents[k].time+timeOffset, channel, fMidiOutEvents[k].size, fMidiOutEvents[k].data);
  1737. else
  1738. pData->event.portOut->writeMidiEvent(fMidiOutEvents[k].time+timeOffset, channel, fMidiOutEvents[k].size, fMidiOutEvents[k].data);
  1739. }
  1740. }
  1741. // --------------------------------------------------------------------------------------------------------
  1742. pData->singleMutex.unlock();
  1743. return true;
  1744. }
  1745. void bufferSizeChanged(const uint32_t newBufferSize) override
  1746. {
  1747. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1748. carla_debug("CarlaPluginNative::bufferSizeChanged(%i)", newBufferSize);
  1749. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1750. {
  1751. if (fAudioInBuffers[i] != nullptr)
  1752. delete[] fAudioInBuffers[i];
  1753. fAudioInBuffers[i] = new float[newBufferSize];
  1754. }
  1755. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1756. {
  1757. if (fAudioOutBuffers[i] != nullptr)
  1758. delete[] fAudioOutBuffers[i];
  1759. fAudioOutBuffers[i] = new float[newBufferSize];
  1760. }
  1761. if (fCurBufferSize == newBufferSize)
  1762. return;
  1763. fCurBufferSize = newBufferSize;
  1764. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1765. {
  1766. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, static_cast<intptr_t>(newBufferSize), nullptr, 0.0f);
  1767. if (fHandle2 != nullptr)
  1768. fDescriptor->dispatcher(fHandle2, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, static_cast<intptr_t>(newBufferSize), nullptr, 0.0f);
  1769. }
  1770. }
  1771. void sampleRateChanged(const double newSampleRate) override
  1772. {
  1773. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1774. carla_debug("CarlaPluginNative::sampleRateChanged(%g)", newSampleRate);
  1775. if (carla_isEqual(fCurSampleRate, newSampleRate))
  1776. return;
  1777. fCurSampleRate = newSampleRate;
  1778. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1779. {
  1780. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  1781. if (fHandle2 != nullptr)
  1782. fDescriptor->dispatcher(fHandle2, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  1783. }
  1784. }
  1785. void offlineModeChanged(const bool isOffline) override
  1786. {
  1787. if (fIsOffline == isOffline)
  1788. return;
  1789. fIsOffline = isOffline;
  1790. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1791. {
  1792. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  1793. if (fHandle2 != nullptr)
  1794. fDescriptor->dispatcher(fHandle2, NATIVE_PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  1795. }
  1796. }
  1797. // -------------------------------------------------------------------
  1798. // Plugin buffers
  1799. void initBuffers() const noexcept override
  1800. {
  1801. CarlaPlugin::initBuffers();
  1802. fMidiIn.initBuffers(pData->event.portIn);
  1803. fMidiOut.initBuffers();
  1804. }
  1805. void clearBuffers() noexcept override
  1806. {
  1807. carla_debug("CarlaPluginNative::clearBuffers() - start");
  1808. if (fAudioInBuffers != nullptr)
  1809. {
  1810. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1811. {
  1812. if (fAudioInBuffers[i] != nullptr)
  1813. {
  1814. delete[] fAudioInBuffers[i];
  1815. fAudioInBuffers[i] = nullptr;
  1816. }
  1817. }
  1818. delete[] fAudioInBuffers;
  1819. fAudioInBuffers = nullptr;
  1820. }
  1821. if (fAudioOutBuffers != nullptr)
  1822. {
  1823. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1824. {
  1825. if (fAudioOutBuffers[i] != nullptr)
  1826. {
  1827. delete[] fAudioOutBuffers[i];
  1828. fAudioOutBuffers[i] = nullptr;
  1829. }
  1830. }
  1831. delete[] fAudioOutBuffers;
  1832. fAudioOutBuffers = nullptr;
  1833. }
  1834. if (fMidiIn.count > 1)
  1835. pData->event.portIn = nullptr;
  1836. if (fMidiOut.count > 1)
  1837. pData->event.portOut = nullptr;
  1838. fMidiIn.clear();
  1839. fMidiOut.clear();
  1840. CarlaPlugin::clearBuffers();
  1841. carla_debug("CarlaPluginNative::clearBuffers() - end");
  1842. }
  1843. // -------------------------------------------------------------------
  1844. // Post-poned UI Stuff
  1845. void uiParameterChange(const uint32_t index, const float value) noexcept override
  1846. {
  1847. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1848. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1849. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  1850. if (! fIsUiVisible)
  1851. return;
  1852. if (fDescriptor->ui_set_parameter_value != nullptr)
  1853. fDescriptor->ui_set_parameter_value(fHandle, index, value);
  1854. }
  1855. void uiMidiProgramChange(const uint32_t index) noexcept override
  1856. {
  1857. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1858. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1859. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  1860. if (! fIsUiVisible)
  1861. return;
  1862. if (index >= pData->midiprog.count)
  1863. return;
  1864. if (fDescriptor->ui_set_midi_program != nullptr)
  1865. fDescriptor->ui_set_midi_program(fHandle, 0, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  1866. }
  1867. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  1868. {
  1869. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1870. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1871. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1872. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1873. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1874. if (! fIsUiVisible)
  1875. return;
  1876. // TODO
  1877. }
  1878. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  1879. {
  1880. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1881. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1882. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1883. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1884. if (! fIsUiVisible)
  1885. return;
  1886. if (fDescriptor == nullptr || fHandle == nullptr)
  1887. return;
  1888. // TODO
  1889. }
  1890. // -------------------------------------------------------------------
  1891. protected:
  1892. const NativeTimeInfo* handleGetTimeInfo() const noexcept
  1893. {
  1894. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, nullptr);
  1895. return &fTimeInfo;
  1896. }
  1897. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  1898. {
  1899. CARLA_SAFE_ASSERT_RETURN(pData->enabled, false);
  1900. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, false);
  1901. CARLA_SAFE_ASSERT_RETURN(fMidiOut.count > 0 || pData->event.portOut != nullptr, false);
  1902. CARLA_SAFE_ASSERT_RETURN(event != nullptr, false);
  1903. CARLA_SAFE_ASSERT_RETURN(event->data[0] != 0, false);
  1904. if (fMidiEventOutCount == kPluginMaxMidiEvents)
  1905. {
  1906. carla_stdout("CarlaPluginNative::handleWriteMidiEvent(%p) - buffer full", event);
  1907. return false;
  1908. }
  1909. std::memcpy(&fMidiOutEvents[fMidiEventOutCount++], event, sizeof(NativeMidiEvent));
  1910. return true;
  1911. }
  1912. void handleUiParameterChanged(const uint32_t index, const float value)
  1913. {
  1914. setParameterValue(index, value, false, true, true);
  1915. }
  1916. void handleUiParameterTouch(const uint32_t index, const bool touch) const
  1917. {
  1918. carla_stdout("pluginNative handleUiParameterTouch %u %s", index, bool2str(touch));
  1919. pData->engine->touchPluginParameter(pData->id, index, touch);
  1920. }
  1921. void handleUiCustomDataChanged(const char* const key, const char* const value)
  1922. {
  1923. setCustomData(CUSTOM_DATA_TYPE_STRING, key, value, false);
  1924. }
  1925. void handleUiClosed()
  1926. {
  1927. pData->engine->callback(true, true, ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0, 0.0f, nullptr);
  1928. fIsUiVisible = false;
  1929. }
  1930. const char* handleUiOpenFile(const bool isDir, const char* const title, const char* const filter)
  1931. {
  1932. return pData->engine->runFileCallback(FILE_CALLBACK_OPEN, isDir, title, filter);
  1933. }
  1934. const char* handleUiSaveFile(const bool isDir, const char* const title, const char* const filter)
  1935. {
  1936. return pData->engine->runFileCallback(FILE_CALLBACK_SAVE, isDir, title, filter);
  1937. }
  1938. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  1939. {
  1940. carla_debug("CarlaPluginNative::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)", opcode, index, value, ptr, opt);
  1941. intptr_t ret = 0;
  1942. switch (opcode)
  1943. {
  1944. case NATIVE_HOST_OPCODE_NULL:
  1945. break;
  1946. case NATIVE_HOST_OPCODE_UPDATE_PARAMETER:
  1947. // TODO
  1948. pData->engine->callback(true, true, ENGINE_CALLBACK_UPDATE, pData->id, -1, 0, 0, 0.0f, nullptr);
  1949. break;
  1950. case NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  1951. // TODO
  1952. pData->engine->callback(true, true, ENGINE_CALLBACK_UPDATE, pData->id, -1, 0, 0, 0.0f, nullptr);
  1953. break;
  1954. case NATIVE_HOST_OPCODE_RELOAD_PARAMETERS:
  1955. reload(); // FIXME
  1956. pData->engine->callback(true, true, ENGINE_CALLBACK_RELOAD_PARAMETERS, pData->id, -1, 0, 0, 0.0f, nullptr);
  1957. break;
  1958. case NATIVE_HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  1959. reloadPrograms(false);
  1960. pData->engine->callback(true, true, ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, -1, 0, 0, 0.0f, nullptr);
  1961. break;
  1962. case NATIVE_HOST_OPCODE_RELOAD_ALL:
  1963. reload();
  1964. pData->engine->callback(true, true, ENGINE_CALLBACK_RELOAD_ALL, pData->id, -1, 0, 0, 0.0f, nullptr);
  1965. break;
  1966. case NATIVE_HOST_OPCODE_UI_UNAVAILABLE:
  1967. pData->engine->callback(true, true, ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0, 0.0f, nullptr);
  1968. fIsUiAvailable = false;
  1969. break;
  1970. case NATIVE_HOST_OPCODE_HOST_IDLE:
  1971. pData->engine->callback(true, false, ENGINE_CALLBACK_IDLE, 0, 0, 0, 0, 0.0f, nullptr);
  1972. break;
  1973. case NATIVE_HOST_OPCODE_INTERNAL_PLUGIN:
  1974. ret = 1;
  1975. break;
  1976. }
  1977. return ret;
  1978. // unused for now
  1979. (void)index;
  1980. (void)value;
  1981. (void)ptr;
  1982. (void)opt;
  1983. }
  1984. // -------------------------------------------------------------------
  1985. public:
  1986. void* getNativeHandle() const noexcept override
  1987. {
  1988. return fHandle;
  1989. }
  1990. const void* getNativeDescriptor() const noexcept override
  1991. {
  1992. return fDescriptor;
  1993. }
  1994. // -------------------------------------------------------------------
  1995. bool init(const char* const name, const char* const label, const uint options)
  1996. {
  1997. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1998. // ---------------------------------------------------------------
  1999. // first checks
  2000. if (pData->client != nullptr)
  2001. {
  2002. pData->engine->setLastError("Plugin client is already registered");
  2003. return false;
  2004. }
  2005. if (label == nullptr || label[0] == '\0')
  2006. {
  2007. pData->engine->setLastError("null label");
  2008. return false;
  2009. }
  2010. // ---------------------------------------------------------------
  2011. // get descriptor that matches label
  2012. sPluginInitializer.initIfNeeded();
  2013. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = gPluginDescriptors.begin2(); it.valid(); it.next())
  2014. {
  2015. fDescriptor = it.getValue(nullptr);
  2016. CARLA_SAFE_ASSERT_BREAK(fDescriptor != nullptr);
  2017. carla_debug("Check vs \"%s\"", fDescriptor->label);
  2018. if (fDescriptor->label != nullptr && std::strcmp(fDescriptor->label, label) == 0)
  2019. break;
  2020. fDescriptor = nullptr;
  2021. }
  2022. if (fDescriptor == nullptr)
  2023. {
  2024. pData->engine->setLastError("Invalid internal plugin");
  2025. return false;
  2026. }
  2027. // ---------------------------------------------------------------
  2028. // set icon
  2029. /**/ if (std::strcmp(fDescriptor->label, "audiofile") == 0)
  2030. pData->iconName = carla_strdup_safe("file");
  2031. else if (std::strcmp(fDescriptor->label, "midifile") == 0)
  2032. pData->iconName = carla_strdup_safe("file");
  2033. else if (std::strcmp(fDescriptor->label, "3bandeq") == 0)
  2034. pData->iconName = carla_strdup_safe("distrho");
  2035. else if (std::strcmp(fDescriptor->label, "3bandsplitter") == 0)
  2036. pData->iconName = carla_strdup_safe("distrho");
  2037. else if (std::strcmp(fDescriptor->label, "kars") == 0)
  2038. pData->iconName = carla_strdup_safe("distrho");
  2039. else if (std::strcmp(fDescriptor->label, "nekobi") == 0)
  2040. pData->iconName = carla_strdup_safe("distrho");
  2041. else if (std::strcmp(fDescriptor->label, "pingpongpan") == 0)
  2042. pData->iconName = carla_strdup_safe("distrho");
  2043. // ---------------------------------------------------------------
  2044. // set info
  2045. if (name != nullptr && name[0] != '\0')
  2046. pData->name = pData->engine->getUniquePluginName(name);
  2047. else if (fDescriptor->name != nullptr && fDescriptor->name[0] != '\0')
  2048. pData->name = pData->engine->getUniquePluginName(fDescriptor->name);
  2049. else
  2050. pData->name = pData->engine->getUniquePluginName(label);
  2051. {
  2052. CARLA_ASSERT(fHost.uiName == nullptr);
  2053. char uiName[std::strlen(pData->name)+6+1];
  2054. std::strcpy(uiName, pData->name);
  2055. std::strcat(uiName, " (GUI)");
  2056. fHost.uiName = carla_strdup(uiName);
  2057. }
  2058. // ---------------------------------------------------------------
  2059. // register client
  2060. pData->client = pData->engine->addClient(this);
  2061. if (pData->client == nullptr || ! pData->client->isOk())
  2062. {
  2063. pData->engine->setLastError("Failed to register plugin client");
  2064. return false;
  2065. }
  2066. // ---------------------------------------------------------------
  2067. // initialize plugin
  2068. fHandle = fDescriptor->instantiate(&fHost);
  2069. if (fHandle == nullptr)
  2070. {
  2071. pData->engine->setLastError("Plugin failed to initialize");
  2072. return false;
  2073. }
  2074. // ---------------------------------------------------------------
  2075. // set default options
  2076. bool hasMidiProgs = false;
  2077. if (fDescriptor->get_midi_program_count != nullptr)
  2078. {
  2079. try {
  2080. hasMidiProgs = fDescriptor->get_midi_program_count(fHandle) > 0;
  2081. } catch (...) {}
  2082. }
  2083. pData->options = 0x0;
  2084. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS)
  2085. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  2086. else if (options & PLUGIN_OPTION_FIXED_BUFFERS)
  2087. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  2088. if (pData->engine->getOptions().forceStereo)
  2089. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  2090. else if (options & PLUGIN_OPTION_FORCE_STEREO)
  2091. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  2092. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CHANNEL_PRESSURE)
  2093. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  2094. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_NOTE_AFTERTOUCH)
  2095. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  2096. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PITCHBEND)
  2097. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  2098. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_ALL_SOUND_OFF)
  2099. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  2100. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CONTROL_CHANGES)
  2101. {
  2102. if (options & PLUGIN_OPTION_SEND_CONTROL_CHANGES)
  2103. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  2104. }
  2105. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PROGRAM_CHANGES)
  2106. {
  2107. CARLA_SAFE_ASSERT(! hasMidiProgs);
  2108. pData->options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
  2109. }
  2110. else if (hasMidiProgs)
  2111. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  2112. return true;
  2113. }
  2114. private:
  2115. NativePluginHandle fHandle;
  2116. NativePluginHandle fHandle2;
  2117. NativeHostDescriptor fHost;
  2118. const NativePluginDescriptor* fDescriptor;
  2119. bool fIsProcessing;
  2120. bool fIsOffline;
  2121. bool fIsUiAvailable;
  2122. bool fIsUiVisible;
  2123. float** fAudioInBuffers;
  2124. float** fAudioOutBuffers;
  2125. uint32_t fMidiEventInCount;
  2126. uint32_t fMidiEventOutCount;
  2127. NativeMidiEvent fMidiInEvents[kPluginMaxMidiEvents];
  2128. NativeMidiEvent fMidiOutEvents[kPluginMaxMidiEvents];
  2129. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  2130. uint32_t fCurBufferSize;
  2131. double fCurSampleRate;
  2132. NativePluginMidiInData fMidiIn;
  2133. NativePluginMidiOutData fMidiOut;
  2134. NativeTimeInfo fTimeInfo;
  2135. // -------------------------------------------------------------------
  2136. #define handlePtr ((CarlaPluginNative*)handle)
  2137. static uint32_t carla_host_get_buffer_size(NativeHostHandle handle) noexcept
  2138. {
  2139. return handlePtr->fCurBufferSize;
  2140. }
  2141. static double carla_host_get_sample_rate(NativeHostHandle handle) noexcept
  2142. {
  2143. return handlePtr->fCurSampleRate;
  2144. }
  2145. static bool carla_host_is_offline(NativeHostHandle handle) noexcept
  2146. {
  2147. return handlePtr->fIsOffline;
  2148. }
  2149. static const NativeTimeInfo* carla_host_get_time_info(NativeHostHandle handle) noexcept
  2150. {
  2151. return handlePtr->handleGetTimeInfo();
  2152. }
  2153. static bool carla_host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  2154. {
  2155. return handlePtr->handleWriteMidiEvent(event);
  2156. }
  2157. static void carla_host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  2158. {
  2159. handlePtr->handleUiParameterChanged(index, value);
  2160. }
  2161. static void carla_host_ui_parameter_touch(NativeHostHandle handle, uint32_t index, bool touch)
  2162. {
  2163. handlePtr->handleUiParameterTouch(index, touch);
  2164. }
  2165. static void carla_host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  2166. {
  2167. handlePtr->handleUiCustomDataChanged(key, value);
  2168. }
  2169. static void carla_host_ui_closed(NativeHostHandle handle)
  2170. {
  2171. handlePtr->handleUiClosed();
  2172. }
  2173. static const char* carla_host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  2174. {
  2175. return handlePtr->handleUiOpenFile(isDir, title, filter);
  2176. }
  2177. static const char* carla_host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  2178. {
  2179. return handlePtr->handleUiSaveFile(isDir, title, filter);
  2180. }
  2181. static intptr_t carla_host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  2182. {
  2183. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  2184. }
  2185. #undef handlePtr
  2186. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginNative)
  2187. };
  2188. // -----------------------------------------------------------------------
  2189. CarlaPlugin* CarlaPlugin::newNative(const Initializer& init)
  2190. {
  2191. carla_debug("CarlaPlugin::newNative({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "})", init.engine, init.filename, init.name, init.label, init.uniqueId);
  2192. CarlaPluginNative* const plugin(new CarlaPluginNative(init.engine, init.id));
  2193. if (! plugin->init(init.name, init.label, init.options))
  2194. {
  2195. delete plugin;
  2196. return nullptr;
  2197. }
  2198. return plugin;
  2199. }
  2200. // -----------------------------------------------------------------------
  2201. CARLA_BACKEND_END_NAMESPACE