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.

3194 lines
114KB

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