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.

2963 lines
106KB

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