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.

2932 lines
104KB

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