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.

3145 lines
112KB

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