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.

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