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.

2747 lines
97KB

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