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.

3109 lines
111KB

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