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.

3123 lines
112KB

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