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.

2598 lines
92KB

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