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.

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