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.

2811 lines
100KB

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