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