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.

3161 lines
113KB

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