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.

2567 lines
91KB

  1. /*
  2. * Carla Native Plugin
  3. * Copyright (C) 2012-2017 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. // TODO, put into check below
  569. if ((pData->hints & PLUGIN_IS_SYNTH) != 0 && (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS))
  570. return CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  571. if (index >= 0)
  572. {
  573. const uint8_t channel = uint8_t((pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS) ? pData->ctrlChannel : 0);
  574. const uint32_t bank = pData->midiprog.data[index].bank;
  575. const uint32_t program = pData->midiprog.data[index].program;
  576. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  577. try {
  578. fDescriptor->set_midi_program(fHandle, channel, bank, program);
  579. } catch(...) {}
  580. if (fHandle2 != nullptr)
  581. {
  582. try {
  583. fDescriptor->set_midi_program(fHandle2, channel, bank, program);
  584. } catch(...) {}
  585. }
  586. fCurMidiProgs[channel] = index;
  587. }
  588. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  589. }
  590. // -------------------------------------------------------------------
  591. // Set ui stuff
  592. void showCustomUI(const bool yesNo) override
  593. {
  594. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  595. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  596. if (fDescriptor->ui_show == nullptr)
  597. return;
  598. fIsUiAvailable = true;
  599. fDescriptor->ui_show(fHandle, yesNo);
  600. // UI might not be available, see NATIVE_HOST_OPCODE_UI_UNAVAILABLE
  601. if (yesNo && ! fIsUiAvailable)
  602. return;
  603. fIsUiVisible = yesNo;
  604. if (! yesNo)
  605. {
  606. pData->transientTryCounter = 0;
  607. return;
  608. }
  609. #ifndef BUILD_BRIDGE
  610. if ((fDescriptor->hints & NATIVE_PLUGIN_USES_PARENT_ID) == 0 && std::strstr(fDescriptor->label, "file") == nullptr)
  611. pData->tryTransient();
  612. #endif
  613. if (fDescriptor->ui_set_custom_data != nullptr)
  614. {
  615. for (LinkedList<CustomData>::Itenerator it = pData->custom.begin2(); it.valid(); it.next())
  616. {
  617. const CustomData& cData(it.getValue(kCustomDataFallback));
  618. CARLA_SAFE_ASSERT_CONTINUE(cData.isValid());
  619. if (std::strcmp(cData.type, CUSTOM_DATA_TYPE_STRING) == 0 && std::strcmp(cData.key, "midiPrograms") != 0)
  620. fDescriptor->ui_set_custom_data(fHandle, cData.key, cData.value);
  621. }
  622. }
  623. if (fDescriptor->ui_set_midi_program != nullptr && pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  624. {
  625. const int32_t index = pData->midiprog.current;
  626. const uint8_t channel = uint8_t((pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS) ? pData->ctrlChannel : 0);
  627. const uint32_t bank = pData->midiprog.data[index].bank;
  628. const uint32_t program = pData->midiprog.data[index].program;
  629. fDescriptor->ui_set_midi_program(fHandle, channel, bank, program);
  630. }
  631. if (fDescriptor->ui_set_parameter_value != nullptr)
  632. {
  633. for (uint32_t i=0; i < pData->param.count; ++i)
  634. fDescriptor->ui_set_parameter_value(fHandle, i, fDescriptor->get_parameter_value(fHandle, i));
  635. }
  636. }
  637. void uiIdle() override
  638. {
  639. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  640. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  641. if (fIsUiVisible && fDescriptor->ui_idle != nullptr)
  642. fDescriptor->ui_idle(fHandle);
  643. CarlaPlugin::uiIdle();
  644. }
  645. // -------------------------------------------------------------------
  646. // Plugin state
  647. void reload() override
  648. {
  649. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  650. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  651. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  652. carla_debug("CarlaPluginNative::reload() - start");
  653. const EngineProcessMode processMode(pData->engine->getProccessMode());
  654. // Safely disable plugin for reload
  655. const ScopedDisabler sd(this);
  656. if (pData->active)
  657. deactivate();
  658. clearBuffers();
  659. const float sampleRate((float)pData->engine->getSampleRate());
  660. uint32_t aIns, aOuts, mIns, mOuts, params, j;
  661. bool forcedStereoIn, forcedStereoOut;
  662. forcedStereoIn = forcedStereoOut = false;
  663. bool needsCtrlIn, needsCtrlOut;
  664. needsCtrlIn = needsCtrlOut = false;
  665. aIns = fDescriptor->audioIns;
  666. aOuts = fDescriptor->audioOuts;
  667. mIns = fDescriptor->midiIns;
  668. mOuts = fDescriptor->midiOuts;
  669. params = (fDescriptor->get_parameter_count != nullptr && fDescriptor->get_parameter_info != nullptr) ? fDescriptor->get_parameter_count(fHandle) : 0;
  670. if ((pData->options & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1) && mIns <= 1 && mOuts <= 1)
  671. {
  672. if (fHandle2 == nullptr)
  673. fHandle2 = fDescriptor->instantiate(&fHost);
  674. if (fHandle2 != nullptr)
  675. {
  676. if (aIns == 1)
  677. {
  678. aIns = 2;
  679. forcedStereoIn = true;
  680. }
  681. if (aOuts == 1)
  682. {
  683. aOuts = 2;
  684. forcedStereoOut = true;
  685. }
  686. }
  687. }
  688. if (aIns > 0)
  689. {
  690. pData->audioIn.createNew(aIns);
  691. fAudioInBuffers = new float*[aIns];
  692. for (uint32_t i=0; i < aIns; ++i)
  693. fAudioInBuffers[i] = nullptr;
  694. }
  695. if (aOuts > 0)
  696. {
  697. pData->audioOut.createNew(aOuts);
  698. fAudioOutBuffers = new float*[aOuts];
  699. needsCtrlIn = true;
  700. for (uint32_t i=0; i < aOuts; ++i)
  701. fAudioOutBuffers[i] = nullptr;
  702. }
  703. if (mIns > 0)
  704. {
  705. fMidiIn.createNew(mIns);
  706. needsCtrlIn = (mIns == 1);
  707. }
  708. if (mOuts > 0)
  709. {
  710. fMidiOut.createNew(mOuts);
  711. needsCtrlOut = (mOuts == 1);
  712. }
  713. if (params > 0)
  714. {
  715. pData->param.createNew(params, true);
  716. }
  717. const uint portNameSize(pData->engine->getMaxPortNameSize());
  718. CarlaString portName;
  719. // Audio Ins
  720. for (j=0; j < aIns; ++j)
  721. {
  722. portName.clear();
  723. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  724. {
  725. portName = pData->name;
  726. portName += ":";
  727. }
  728. if (aIns > 1 && ! forcedStereoIn)
  729. {
  730. portName += "input_";
  731. portName += CarlaString(j+1);
  732. }
  733. else
  734. portName += "input";
  735. portName.truncate(portNameSize);
  736. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, j);
  737. pData->audioIn.ports[j].rindex = j;
  738. if (forcedStereoIn)
  739. {
  740. portName += "_2";
  741. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true, 1);
  742. pData->audioIn.ports[1].rindex = j;
  743. break;
  744. }
  745. }
  746. // Audio Outs
  747. for (j=0; j < aOuts; ++j)
  748. {
  749. portName.clear();
  750. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  751. {
  752. portName = pData->name;
  753. portName += ":";
  754. }
  755. if (aOuts > 1 && ! forcedStereoOut)
  756. {
  757. portName += "output_";
  758. portName += CarlaString(j+1);
  759. }
  760. else
  761. portName += "output";
  762. portName.truncate(portNameSize);
  763. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, j);
  764. pData->audioOut.ports[j].rindex = j;
  765. if (forcedStereoOut)
  766. {
  767. portName += "_2";
  768. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false, 1);
  769. pData->audioOut.ports[1].rindex = j;
  770. break;
  771. }
  772. }
  773. // MIDI Input (only if multiple)
  774. if (mIns > 1)
  775. {
  776. for (j=0; j < mIns; ++j)
  777. {
  778. portName.clear();
  779. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  780. {
  781. portName = pData->name;
  782. portName += ":";
  783. }
  784. portName += "midi-in_";
  785. portName += CarlaString(j+1);
  786. portName.truncate(portNameSize);
  787. fMidiIn.ports[j] = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, j);
  788. fMidiIn.indexes[j] = j;
  789. }
  790. pData->event.portIn = fMidiIn.ports[0];
  791. }
  792. // MIDI Output (only if multiple)
  793. if (mOuts > 1)
  794. {
  795. for (j=0; j < mOuts; ++j)
  796. {
  797. portName.clear();
  798. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  799. {
  800. portName = pData->name;
  801. portName += ":";
  802. }
  803. portName += "midi-out_";
  804. portName += CarlaString(j+1);
  805. portName.truncate(portNameSize);
  806. fMidiOut.ports[j] = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, j);
  807. fMidiOut.indexes[j] = j;
  808. }
  809. pData->event.portOut = fMidiOut.ports[0];
  810. }
  811. for (j=0; j < params; ++j)
  812. {
  813. const NativeParameter* const paramInfo(fDescriptor->get_parameter_info(fHandle, j));
  814. CARLA_SAFE_ASSERT_CONTINUE(paramInfo != nullptr);
  815. pData->param.data[j].type = PARAMETER_UNKNOWN;
  816. pData->param.data[j].index = static_cast<int32_t>(j);
  817. pData->param.data[j].rindex = static_cast<int32_t>(j);
  818. float min, max, def, step, stepSmall, stepLarge;
  819. // min value
  820. min = paramInfo->ranges.min;
  821. // max value
  822. max = paramInfo->ranges.max;
  823. if (min > max)
  824. max = min;
  825. else if (max < min)
  826. min = max;
  827. if (carla_isEqual(min, max))
  828. {
  829. carla_stderr2("WARNING - Broken plugin parameter '%s': max == min", paramInfo->name);
  830. max = min + 0.1f;
  831. }
  832. // default value
  833. def = paramInfo->ranges.def;
  834. if (def < min)
  835. def = min;
  836. else if (def > max)
  837. def = max;
  838. if (paramInfo->hints & NATIVE_PARAMETER_USES_SAMPLE_RATE)
  839. {
  840. min *= sampleRate;
  841. max *= sampleRate;
  842. def *= sampleRate;
  843. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  844. }
  845. if (paramInfo->hints & NATIVE_PARAMETER_IS_BOOLEAN)
  846. {
  847. step = max - min;
  848. stepSmall = step;
  849. stepLarge = step;
  850. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  851. }
  852. else if (paramInfo->hints & NATIVE_PARAMETER_IS_INTEGER)
  853. {
  854. step = 1.0f;
  855. stepSmall = 1.0f;
  856. stepLarge = 10.0f;
  857. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  858. }
  859. else
  860. {
  861. float range = max - min;
  862. step = range/100.0f;
  863. stepSmall = range/1000.0f;
  864. stepLarge = range/10.0f;
  865. }
  866. if (paramInfo->hints & NATIVE_PARAMETER_IS_OUTPUT)
  867. {
  868. pData->param.data[j].type = PARAMETER_OUTPUT;
  869. needsCtrlOut = true;
  870. }
  871. else
  872. {
  873. pData->param.data[j].type = PARAMETER_INPUT;
  874. needsCtrlIn = true;
  875. }
  876. // extra parameter hints
  877. if (paramInfo->hints & NATIVE_PARAMETER_IS_ENABLED)
  878. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  879. if (paramInfo->hints & NATIVE_PARAMETER_IS_AUTOMABLE)
  880. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  881. if (paramInfo->hints & NATIVE_PARAMETER_IS_LOGARITHMIC)
  882. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  883. if (paramInfo->hints & NATIVE_PARAMETER_USES_SCALEPOINTS)
  884. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  885. pData->param.ranges[j].min = min;
  886. pData->param.ranges[j].max = max;
  887. pData->param.ranges[j].def = def;
  888. pData->param.ranges[j].step = step;
  889. pData->param.ranges[j].stepSmall = stepSmall;
  890. pData->param.ranges[j].stepLarge = stepLarge;
  891. }
  892. if (needsCtrlIn && mIns <= 1)
  893. {
  894. portName.clear();
  895. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  896. {
  897. portName = pData->name;
  898. portName += ":";
  899. }
  900. portName += "events-in";
  901. portName.truncate(portNameSize);
  902. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true, 0);
  903. }
  904. if (needsCtrlOut && mOuts <= 1)
  905. {
  906. portName.clear();
  907. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  908. {
  909. portName = pData->name;
  910. portName += ":";
  911. }
  912. portName += "events-out";
  913. portName.truncate(portNameSize);
  914. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false, 0);
  915. }
  916. if (forcedStereoIn || forcedStereoOut)
  917. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  918. else
  919. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  920. // plugin hints
  921. pData->hints = 0x0;
  922. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  923. pData->hints |= PLUGIN_CAN_DRYWET;
  924. if (aOuts > 0)
  925. pData->hints |= PLUGIN_CAN_VOLUME;
  926. if (aOuts >= 2 && aOuts % 2 == 0)
  927. pData->hints |= PLUGIN_CAN_BALANCE;
  928. // native plugin hints
  929. if (fDescriptor->hints & NATIVE_PLUGIN_IS_RTSAFE)
  930. pData->hints |= PLUGIN_IS_RTSAFE;
  931. if (fDescriptor->hints & NATIVE_PLUGIN_IS_SYNTH)
  932. pData->hints |= PLUGIN_IS_SYNTH;
  933. if (fDescriptor->hints & NATIVE_PLUGIN_HAS_UI)
  934. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  935. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS)
  936. pData->hints |= PLUGIN_NEEDS_FIXED_BUFFERS;
  937. if (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_UI_MAIN_THREAD)
  938. pData->hints |= PLUGIN_NEEDS_UI_MAIN_THREAD;
  939. if (fDescriptor->hints & NATIVE_PLUGIN_USES_MULTI_PROGS)
  940. pData->hints |= PLUGIN_USES_MULTI_PROGS;
  941. // extra plugin hints
  942. pData->extraHints = 0x0;
  943. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && mIns <= 1 && mOuts <= 1)
  944. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  945. bufferSizeChanged(pData->engine->getBufferSize());
  946. reloadPrograms(true);
  947. if (pData->active)
  948. activate();
  949. carla_debug("CarlaPluginNative::reload() - end");
  950. }
  951. void reloadPrograms(const bool doInit) override
  952. {
  953. carla_debug("CarlaPluginNative::reloadPrograms(%s)", bool2str(doInit));
  954. uint32_t i, oldCount = pData->midiprog.count;
  955. const int32_t current = pData->midiprog.current;
  956. // Delete old programs
  957. pData->midiprog.clear();
  958. // Query new programs
  959. uint32_t count = 0;
  960. if (fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_info != nullptr && fDescriptor->set_midi_program != nullptr)
  961. count = fDescriptor->get_midi_program_count(fHandle);
  962. if (count > 0)
  963. {
  964. pData->midiprog.createNew(count);
  965. // Update data
  966. for (i=0; i < count; ++i)
  967. {
  968. const NativeMidiProgram* const mpDesc(fDescriptor->get_midi_program_info(fHandle, i));
  969. CARLA_SAFE_ASSERT_CONTINUE(mpDesc != nullptr);
  970. pData->midiprog.data[i].bank = mpDesc->bank;
  971. pData->midiprog.data[i].program = mpDesc->program;
  972. pData->midiprog.data[i].name = carla_strdup(mpDesc->name);
  973. }
  974. }
  975. #if defined(HAVE_LIBLO) && ! defined(BUILD_BRIDGE)
  976. // Update OSC Names
  977. if (pData->engine->isOscControlRegistered() && pData->id < pData->engine->getCurrentPluginCount())
  978. {
  979. pData->engine->oscSend_control_set_midi_program_count(pData->id, count);
  980. for (i=0; i < count; ++i)
  981. 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);
  982. }
  983. #endif
  984. if (doInit)
  985. {
  986. if (count > 0)
  987. setMidiProgram(0, false, false, false);
  988. }
  989. else
  990. {
  991. // Check if current program is invalid
  992. bool programChanged = false;
  993. if (count == oldCount+1)
  994. {
  995. // one midi program added, probably created by user
  996. pData->midiprog.current = static_cast<int32_t>(oldCount);
  997. programChanged = true;
  998. }
  999. else if (current < 0 && count > 0)
  1000. {
  1001. // programs exist now, but not before
  1002. pData->midiprog.current = 0;
  1003. programChanged = true;
  1004. }
  1005. else if (current >= 0 && count == 0)
  1006. {
  1007. // programs existed before, but not anymore
  1008. pData->midiprog.current = -1;
  1009. programChanged = true;
  1010. }
  1011. else if (current >= static_cast<int32_t>(count))
  1012. {
  1013. // current midi program > count
  1014. pData->midiprog.current = 0;
  1015. programChanged = true;
  1016. }
  1017. else
  1018. {
  1019. // no change
  1020. pData->midiprog.current = current;
  1021. }
  1022. if (programChanged)
  1023. setMidiProgram(pData->midiprog.current, true, true, true);
  1024. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  1025. }
  1026. }
  1027. // -------------------------------------------------------------------
  1028. // Plugin processing
  1029. void activate() noexcept override
  1030. {
  1031. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1032. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1033. if (fDescriptor->activate != nullptr)
  1034. {
  1035. try {
  1036. fDescriptor->activate(fHandle);
  1037. } catch(...) {}
  1038. if (fHandle2 != nullptr)
  1039. {
  1040. try {
  1041. fDescriptor->activate(fHandle2);
  1042. } catch(...) {}
  1043. }
  1044. }
  1045. }
  1046. void deactivate() noexcept override
  1047. {
  1048. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1049. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1050. if (fDescriptor->deactivate != nullptr)
  1051. {
  1052. try {
  1053. fDescriptor->deactivate(fHandle);
  1054. } catch(...) {}
  1055. if (fHandle2 != nullptr)
  1056. {
  1057. try {
  1058. fDescriptor->deactivate(fHandle2);
  1059. } catch(...) {}
  1060. }
  1061. }
  1062. }
  1063. void process(const float** const audioIn, float** const audioOut, const float** const cvIn, float** const cvOut, const uint32_t frames) override
  1064. {
  1065. // --------------------------------------------------------------------------------------------------------
  1066. // Check if active
  1067. if (! pData->active)
  1068. {
  1069. // disable any output sound
  1070. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1071. carla_zeroFloats(audioOut[i], frames);
  1072. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1073. carla_zeroFloats(cvOut[i], frames);
  1074. return;
  1075. }
  1076. fMidiEventCount = 0;
  1077. carla_zeroStructs(fMidiEvents, kPluginMaxMidiEvents*2);
  1078. // --------------------------------------------------------------------------------------------------------
  1079. // Check if needs reset
  1080. if (pData->needsReset)
  1081. {
  1082. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1083. {
  1084. fMidiEventCount = MAX_MIDI_CHANNELS*2;
  1085. for (uint8_t k=0, i=MAX_MIDI_CHANNELS; k < MAX_MIDI_CHANNELS; ++k)
  1086. {
  1087. fMidiEvents[k].data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
  1088. fMidiEvents[k].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1089. fMidiEvents[k].data[2] = 0;
  1090. fMidiEvents[k].size = 3;
  1091. fMidiEvents[k+i].data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (k & MIDI_CHANNEL_BIT));
  1092. fMidiEvents[k+i].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1093. fMidiEvents[k+i].data[2] = 0;
  1094. fMidiEvents[k+i].size = 3;
  1095. }
  1096. }
  1097. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  1098. {
  1099. fMidiEventCount = MAX_MIDI_NOTE;
  1100. for (uint8_t k=0; k < MAX_MIDI_NOTE; ++k)
  1101. {
  1102. fMidiEvents[k].data[0] = uint8_t(MIDI_STATUS_NOTE_OFF | (pData->ctrlChannel & MIDI_CHANNEL_BIT));
  1103. fMidiEvents[k].data[1] = k;
  1104. fMidiEvents[k].data[2] = 0;
  1105. fMidiEvents[k].size = 3;
  1106. }
  1107. }
  1108. pData->needsReset = false;
  1109. }
  1110. // --------------------------------------------------------------------------------------------------------
  1111. // Set TimeInfo
  1112. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  1113. fTimeInfo.playing = timeInfo.playing;
  1114. fTimeInfo.frame = timeInfo.frame;
  1115. fTimeInfo.usecs = timeInfo.usecs;
  1116. if (timeInfo.bbt.valid)
  1117. {
  1118. fTimeInfo.bbt.valid = true;
  1119. fTimeInfo.bbt.bar = timeInfo.bbt.bar;
  1120. fTimeInfo.bbt.beat = timeInfo.bbt.beat;
  1121. fTimeInfo.bbt.tick = timeInfo.bbt.tick;
  1122. fTimeInfo.bbt.barStartTick = timeInfo.bbt.barStartTick;
  1123. fTimeInfo.bbt.beatsPerBar = timeInfo.bbt.beatsPerBar;
  1124. fTimeInfo.bbt.beatType = timeInfo.bbt.beatType;
  1125. fTimeInfo.bbt.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
  1126. fTimeInfo.bbt.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
  1127. }
  1128. else
  1129. {
  1130. fTimeInfo.bbt.valid = false;
  1131. }
  1132. // --------------------------------------------------------------------------------------------------------
  1133. // Event Input and Processing
  1134. if (pData->event.portIn != nullptr)
  1135. {
  1136. // ----------------------------------------------------------------------------------------------------
  1137. // MIDI Input (External)
  1138. if (pData->extNotes.mutex.tryLock())
  1139. {
  1140. ExternalMidiNote note = { 0, 0, 0 };
  1141. for (; fMidiEventCount < kPluginMaxMidiEvents*2 && ! pData->extNotes.data.isEmpty();)
  1142. {
  1143. note = pData->extNotes.data.getFirst(note, true);
  1144. CARLA_SAFE_ASSERT_CONTINUE(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  1145. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1146. nativeEvent.data[0] = uint8_t((note.velo > 0 ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (note.channel & MIDI_CHANNEL_BIT));
  1147. nativeEvent.data[1] = note.note;
  1148. nativeEvent.data[2] = note.velo;
  1149. nativeEvent.size = 3;
  1150. }
  1151. pData->extNotes.mutex.unlock();
  1152. } // End of MIDI Input (External)
  1153. // ----------------------------------------------------------------------------------------------------
  1154. // Event Input (System)
  1155. #ifndef BUILD_BRIDGE
  1156. bool allNotesOffSent = false;
  1157. #endif
  1158. bool sampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  1159. uint32_t startTime = 0;
  1160. uint32_t timeOffset = 0;
  1161. uint32_t nextBankId;
  1162. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1163. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1164. else
  1165. nextBankId = 0;
  1166. for (uint32_t m=0, max=jmax(1U, fMidiIn.count); m < max; ++m)
  1167. {
  1168. CarlaEngineEventPort* const eventPort(m == 0 ? pData->event.portIn : fMidiIn.ports[m]);
  1169. for (uint32_t i=0, numEvents=eventPort->getEventCount(); i < numEvents; ++i)
  1170. {
  1171. const EngineEvent& event(eventPort->getEvent(i));
  1172. if (event.time >= frames)
  1173. continue;
  1174. CARLA_ASSERT_INT2(event.time >= timeOffset, event.time, timeOffset);
  1175. if (event.time > timeOffset && sampleAccurate)
  1176. {
  1177. if (processSingle(audioIn, audioOut, cvIn, cvOut, event.time - timeOffset, timeOffset))
  1178. {
  1179. startTime = 0;
  1180. timeOffset = event.time;
  1181. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1182. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1183. else
  1184. nextBankId = 0;
  1185. if (fMidiEventCount > 0)
  1186. {
  1187. carla_zeroStructs(fMidiEvents, fMidiEventCount);
  1188. fMidiEventCount = 0;
  1189. }
  1190. }
  1191. else
  1192. startTime += timeOffset;
  1193. }
  1194. // Control change
  1195. switch (event.type)
  1196. {
  1197. case kEngineEventTypeNull:
  1198. break;
  1199. case kEngineEventTypeControl: {
  1200. const EngineControlEvent& ctrlEvent = event.ctrl;
  1201. switch (ctrlEvent.type)
  1202. {
  1203. case kEngineControlEventTypeNull:
  1204. break;
  1205. case kEngineControlEventTypeParameter: {
  1206. #ifndef BUILD_BRIDGE
  1207. // Control backend stuff
  1208. if (event.channel == pData->ctrlChannel)
  1209. {
  1210. float value;
  1211. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) > 0)
  1212. {
  1213. value = ctrlEvent.value;
  1214. setDryWetRT(value);
  1215. }
  1216. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) > 0)
  1217. {
  1218. value = ctrlEvent.value*127.0f/100.0f;
  1219. setVolumeRT(value);
  1220. }
  1221. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) > 0)
  1222. {
  1223. float left, right;
  1224. value = ctrlEvent.value/0.5f - 1.0f;
  1225. if (value < 0.0f)
  1226. {
  1227. left = -1.0f;
  1228. right = (value*2.0f)+1.0f;
  1229. }
  1230. else if (value > 0.0f)
  1231. {
  1232. left = (value*2.0f)-1.0f;
  1233. right = 1.0f;
  1234. }
  1235. else
  1236. {
  1237. left = -1.0f;
  1238. right = 1.0f;
  1239. }
  1240. setBalanceLeftRT(left);
  1241. setBalanceRightRT(right);
  1242. }
  1243. }
  1244. #endif
  1245. // Control plugin parameters
  1246. for (uint32_t k=0; k < pData->param.count; ++k)
  1247. {
  1248. if (pData->param.data[k].midiChannel != event.channel)
  1249. continue;
  1250. if (pData->param.data[k].midiCC != ctrlEvent.param)
  1251. continue;
  1252. if (pData->param.data[k].type != PARAMETER_INPUT)
  1253. continue;
  1254. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1255. continue;
  1256. float value;
  1257. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1258. {
  1259. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  1260. }
  1261. else
  1262. {
  1263. if (pData->param.data[k].hints & PARAMETER_IS_LOGARITHMIC)
  1264. value = pData->param.ranges[k].getUnnormalizedLogValue(ctrlEvent.value);
  1265. else
  1266. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  1267. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1268. value = std::rint(value);
  1269. }
  1270. setParameterValueRT(k, value);
  1271. }
  1272. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param < MAX_MIDI_CONTROL)
  1273. {
  1274. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1275. continue;
  1276. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1277. carla_zeroStruct(nativeEvent);
  1278. nativeEvent.time = sampleAccurate ? startTime : event.time;
  1279. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1280. nativeEvent.data[1] = uint8_t(ctrlEvent.param);
  1281. nativeEvent.data[2] = uint8_t(ctrlEvent.value*127.0f);
  1282. nativeEvent.size = 3;
  1283. }
  1284. break;
  1285. } // case kEngineControlEventTypeParameter
  1286. case kEngineControlEventTypeMidiBank:
  1287. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  1288. {
  1289. if (event.channel == pData->ctrlChannel)
  1290. nextBankId = ctrlEvent.param;
  1291. }
  1292. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  1293. {
  1294. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1295. continue;
  1296. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1297. carla_zeroStruct(nativeEvent);
  1298. nativeEvent.time = sampleAccurate ? startTime : event.time;
  1299. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1300. nativeEvent.data[1] = MIDI_CONTROL_BANK_SELECT;
  1301. nativeEvent.data[2] = uint8_t(ctrlEvent.param);
  1302. nativeEvent.size = 3;
  1303. }
  1304. break;
  1305. case kEngineControlEventTypeMidiProgram:
  1306. if (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES)
  1307. {
  1308. if (event.channel < MAX_MIDI_CHANNELS)
  1309. {
  1310. const uint32_t nextProgramId(ctrlEvent.param);
  1311. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  1312. {
  1313. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  1314. {
  1315. fDescriptor->set_midi_program(fHandle, event.channel, nextBankId, nextProgramId);
  1316. if (fHandle2 != nullptr)
  1317. fDescriptor->set_midi_program(fHandle2, event.channel, nextBankId, nextProgramId);
  1318. fCurMidiProgs[event.channel] = static_cast<int32_t>(k);
  1319. if (event.channel == pData->ctrlChannel)
  1320. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, static_cast<int32_t>(k), 0, 0.0f);
  1321. break;
  1322. }
  1323. }
  1324. }
  1325. }
  1326. else if (pData->options & PLUGIN_OPTION_SEND_PROGRAM_CHANGES)
  1327. {
  1328. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1329. continue;
  1330. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1331. carla_zeroStruct(nativeEvent);
  1332. nativeEvent.time = sampleAccurate ? startTime : event.time;
  1333. nativeEvent.data[0] = uint8_t(MIDI_STATUS_PROGRAM_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1334. nativeEvent.data[1] = uint8_t(ctrlEvent.param);
  1335. nativeEvent.size = 2;
  1336. }
  1337. break;
  1338. case kEngineControlEventTypeAllSoundOff:
  1339. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1340. {
  1341. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1342. continue;
  1343. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1344. carla_zeroStruct(nativeEvent);
  1345. nativeEvent.time = sampleAccurate ? startTime : event.time;
  1346. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1347. nativeEvent.data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1348. nativeEvent.data[2] = 0;
  1349. nativeEvent.size = 3;
  1350. }
  1351. break;
  1352. case kEngineControlEventTypeAllNotesOff:
  1353. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1354. {
  1355. #ifndef BUILD_BRIDGE
  1356. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1357. {
  1358. allNotesOffSent = true;
  1359. sendMidiAllNotesOffToCallback();
  1360. }
  1361. #endif
  1362. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1363. continue;
  1364. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1365. carla_zeroStruct(nativeEvent);
  1366. nativeEvent.time = sampleAccurate ? startTime : event.time;
  1367. nativeEvent.data[0] = uint8_t(MIDI_STATUS_CONTROL_CHANGE | (event.channel & MIDI_CHANNEL_BIT));
  1368. nativeEvent.data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1369. nativeEvent.data[2] = 0;
  1370. nativeEvent.size = 3;
  1371. }
  1372. break;
  1373. }
  1374. break;
  1375. }
  1376. case kEngineEventTypeMidi: {
  1377. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1378. continue;
  1379. const EngineMidiEvent& midiEvent(event.midi);
  1380. if (midiEvent.size > 4)
  1381. continue;
  1382. #ifdef CARLA_PROPER_CPP11_SUPPORT
  1383. static_assert(4 <= EngineMidiEvent::kDataSize, "Incorrect data");
  1384. #endif
  1385. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  1386. if (status == MIDI_STATUS_CHANNEL_PRESSURE && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1387. continue;
  1388. if (status == MIDI_STATUS_CONTROL_CHANGE && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1389. continue;
  1390. if (status == MIDI_STATUS_POLYPHONIC_AFTERTOUCH && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1391. continue;
  1392. if (status == MIDI_STATUS_PITCH_WHEEL_CONTROL && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1393. continue;
  1394. // Fix bad note-off
  1395. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1396. status = MIDI_STATUS_NOTE_OFF;
  1397. NativeMidiEvent& nativeEvent(fMidiEvents[fMidiEventCount++]);
  1398. carla_zeroStruct(nativeEvent);
  1399. nativeEvent.port = midiEvent.port;
  1400. nativeEvent.time = sampleAccurate ? startTime : event.time;
  1401. nativeEvent.size = midiEvent.size;
  1402. nativeEvent.data[0] = uint8_t(status | (event.channel & MIDI_CHANNEL_BIT));
  1403. nativeEvent.data[1] = midiEvent.size >= 2 ? midiEvent.data[1] : 0;
  1404. nativeEvent.data[2] = midiEvent.size >= 3 ? midiEvent.data[2] : 0;
  1405. nativeEvent.data[3] = midiEvent.size == 4 ? midiEvent.data[3] : 0;
  1406. if (status == MIDI_STATUS_NOTE_ON)
  1407. pData->postponeRtEvent(kPluginPostRtEventNoteOn, event.channel, midiEvent.data[1], midiEvent.data[2]);
  1408. else if (status == MIDI_STATUS_NOTE_OFF)
  1409. pData->postponeRtEvent(kPluginPostRtEventNoteOff, event.channel, midiEvent.data[1], 0.0f);
  1410. } break;
  1411. } // switch (event.type)
  1412. }
  1413. pData->postRtEvents.trySplice();
  1414. if (frames > timeOffset)
  1415. processSingle(audioIn, audioOut, cvIn, cvOut, frames - timeOffset, timeOffset);
  1416. } // eventPort
  1417. } // End of Event Input and Processing
  1418. // --------------------------------------------------------------------------------------------------------
  1419. // Plugin processing (no events)
  1420. else
  1421. {
  1422. processSingle(audioIn, audioOut, cvIn, cvOut, frames, 0);
  1423. } // End of Plugin processing (no events)
  1424. // --------------------------------------------------------------------------------------------------------
  1425. // Control and MIDI Output
  1426. if (pData->event.portOut != nullptr)
  1427. {
  1428. #ifndef BUILD_BRIDGE
  1429. float value, curValue;
  1430. for (uint32_t k=0; k < pData->param.count; ++k)
  1431. {
  1432. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1433. continue;
  1434. curValue = fDescriptor->get_parameter_value(fHandle, k);
  1435. pData->param.ranges[k].fixValue(curValue);
  1436. if (pData->param.data[k].midiCC > 0)
  1437. {
  1438. value = pData->param.ranges[k].getNormalizedValue(curValue);
  1439. pData->event.portOut->writeControlEvent(0, pData->param.data[k].midiChannel, kEngineControlEventTypeParameter, static_cast<uint16_t>(pData->param.data[k].midiCC), value);
  1440. }
  1441. }
  1442. #endif
  1443. // reverse lookup MIDI events
  1444. for (uint32_t k = (kPluginMaxMidiEvents*2)-1; k >= fMidiEventCount; --k)
  1445. {
  1446. if (fMidiEvents[k].data[0] == 0)
  1447. break;
  1448. const uint8_t channel = uint8_t(MIDI_GET_CHANNEL_FROM_DATA(fMidiEvents[k].data));
  1449. const uint8_t port = fMidiEvents[k].port;
  1450. if (fMidiOut.count > 1 && port < fMidiOut.count)
  1451. fMidiOut.ports[port]->writeMidiEvent(fMidiEvents[k].time, channel, fMidiEvents[k].size, fMidiEvents[k].data);
  1452. else
  1453. pData->event.portOut->writeMidiEvent(fMidiEvents[k].time, channel, fMidiEvents[k].size, fMidiEvents[k].data);
  1454. }
  1455. } // End of Control and MIDI Output
  1456. }
  1457. bool processSingle(const float** const audioIn, float** const audioOut, const float** const cvIn, float** const cvOut, const uint32_t frames, const uint32_t timeOffset)
  1458. {
  1459. CARLA_SAFE_ASSERT_RETURN(frames > 0, false);
  1460. if (pData->audioIn.count > 0)
  1461. {
  1462. CARLA_SAFE_ASSERT_RETURN(audioIn != nullptr, false);
  1463. }
  1464. if (pData->audioOut.count > 0)
  1465. {
  1466. CARLA_SAFE_ASSERT_RETURN(audioOut != nullptr, false);
  1467. }
  1468. if (pData->cvIn.count > 0)
  1469. {
  1470. CARLA_SAFE_ASSERT_RETURN(cvIn != nullptr, false);
  1471. }
  1472. if (pData->cvOut.count > 0)
  1473. {
  1474. CARLA_SAFE_ASSERT_RETURN(cvOut != nullptr, false);
  1475. }
  1476. // --------------------------------------------------------------------------------------------------------
  1477. // Try lock, silence otherwise
  1478. if (fIsOffline)
  1479. {
  1480. pData->singleMutex.lock();
  1481. }
  1482. else if (! pData->singleMutex.tryLock())
  1483. {
  1484. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1485. {
  1486. for (uint32_t k=0; k < frames; ++k)
  1487. audioOut[i][k+timeOffset] = 0.0f;
  1488. }
  1489. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1490. {
  1491. for (uint32_t k=0; k < frames; ++k)
  1492. cvOut[i][k+timeOffset] = 0.0f;
  1493. }
  1494. return false;
  1495. }
  1496. // --------------------------------------------------------------------------------------------------------
  1497. // Set audio buffers
  1498. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1499. carla_copyFloats(fAudioInBuffers[i], audioIn[i]+timeOffset, frames);
  1500. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1501. carla_zeroFloats(fAudioOutBuffers[i], frames);
  1502. #if 0
  1503. // --------------------------------------------------------------------------------------------------------
  1504. // Set CV buffers
  1505. for (uint32_t i=0; i < pData->cvIn.count; ++i)
  1506. carla_copyFloats(fCvInBuffers[i], cvIn[i]+timeOffset, frames);
  1507. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1508. carla_zeroFloats(fCvOutBuffers[i], frames);
  1509. #endif
  1510. // --------------------------------------------------------------------------------------------------------
  1511. // Run plugin
  1512. fIsProcessing = true;
  1513. if (fHandle2 == nullptr)
  1514. {
  1515. fDescriptor->process(fHandle, fAudioInBuffers, fAudioOutBuffers, frames, fMidiEvents, fMidiEventCount);
  1516. }
  1517. else
  1518. {
  1519. fDescriptor->process(fHandle,
  1520. (pData->audioIn.count > 0) ? &fAudioInBuffers[0] : nullptr,
  1521. (pData->audioOut.count > 0) ? &fAudioOutBuffers[0] : nullptr,
  1522. frames, fMidiEvents, fMidiEventCount);
  1523. fDescriptor->process(fHandle2,
  1524. (pData->audioIn.count > 0) ? &fAudioInBuffers[1] : nullptr,
  1525. (pData->audioOut.count > 0) ? &fAudioOutBuffers[1] : nullptr,
  1526. frames, fMidiEvents, fMidiEventCount);
  1527. }
  1528. fIsProcessing = false;
  1529. fTimeInfo.frame += frames;
  1530. #ifndef BUILD_BRIDGE
  1531. // --------------------------------------------------------------------------------------------------------
  1532. // Post-processing (dry/wet, volume and balance)
  1533. {
  1534. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && carla_isNotEqual(pData->postProc.dryWet, 1.0f);
  1535. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && ! (carla_isEqual(pData->postProc.balanceLeft, -1.0f) && carla_isEqual(pData->postProc.balanceRight, 1.0f));
  1536. bool isPair;
  1537. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1538. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1539. {
  1540. // Dry/Wet
  1541. if (doDryWet)
  1542. {
  1543. for (uint32_t k=0; k < frames; ++k)
  1544. {
  1545. bufValue = fAudioInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  1546. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1547. }
  1548. }
  1549. // Balance
  1550. if (doBalance)
  1551. {
  1552. isPair = (i % 2 == 0);
  1553. if (isPair)
  1554. {
  1555. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1556. carla_copyFloats(oldBufLeft, fAudioOutBuffers[i], frames);
  1557. }
  1558. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1559. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1560. for (uint32_t k=0; k < frames; ++k)
  1561. {
  1562. if (isPair)
  1563. {
  1564. // left
  1565. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1566. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1567. }
  1568. else
  1569. {
  1570. // right
  1571. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1572. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1573. }
  1574. }
  1575. }
  1576. // Volume (and buffer copy)
  1577. {
  1578. for (uint32_t k=0; k < frames; ++k)
  1579. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  1580. }
  1581. }
  1582. } // End of Post-processing
  1583. #else
  1584. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1585. {
  1586. for (uint32_t k=0; k < frames; ++k)
  1587. audioOut[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1588. }
  1589. #endif
  1590. #if 0
  1591. for (uint32_t i=0; i < pData->cvOut.count; ++i)
  1592. {
  1593. for (uint32_t k=0; k < frames; ++k)
  1594. cvOut[i][k+timeOffset] = fCvOutBuffers[i][k];
  1595. }
  1596. #endif
  1597. // --------------------------------------------------------------------------------------------------------
  1598. pData->singleMutex.unlock();
  1599. return true;
  1600. }
  1601. void bufferSizeChanged(const uint32_t newBufferSize) override
  1602. {
  1603. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1604. carla_debug("CarlaPluginNative::bufferSizeChanged(%i)", newBufferSize);
  1605. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1606. {
  1607. if (fAudioInBuffers[i] != nullptr)
  1608. delete[] fAudioInBuffers[i];
  1609. fAudioInBuffers[i] = new float[newBufferSize];
  1610. }
  1611. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1612. {
  1613. if (fAudioOutBuffers[i] != nullptr)
  1614. delete[] fAudioOutBuffers[i];
  1615. fAudioOutBuffers[i] = new float[newBufferSize];
  1616. }
  1617. if (fCurBufferSize == newBufferSize)
  1618. return;
  1619. fCurBufferSize = newBufferSize;
  1620. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1621. {
  1622. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, static_cast<intptr_t>(newBufferSize), nullptr, 0.0f);
  1623. if (fHandle2 != nullptr)
  1624. fDescriptor->dispatcher(fHandle2, NATIVE_PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, static_cast<intptr_t>(newBufferSize), nullptr, 0.0f);
  1625. }
  1626. }
  1627. void sampleRateChanged(const double newSampleRate) override
  1628. {
  1629. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1630. carla_debug("CarlaPluginNative::sampleRateChanged(%g)", newSampleRate);
  1631. if (carla_isEqual(fCurSampleRate, newSampleRate))
  1632. return;
  1633. fCurSampleRate = newSampleRate;
  1634. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1635. {
  1636. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  1637. if (fHandle2 != nullptr)
  1638. fDescriptor->dispatcher(fHandle2, NATIVE_PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  1639. }
  1640. }
  1641. void offlineModeChanged(const bool isOffline) override
  1642. {
  1643. if (fIsOffline == isOffline)
  1644. return;
  1645. fIsOffline = isOffline;
  1646. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1647. {
  1648. fDescriptor->dispatcher(fHandle, NATIVE_PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  1649. if (fHandle2 != nullptr)
  1650. fDescriptor->dispatcher(fHandle2, NATIVE_PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  1651. }
  1652. }
  1653. // -------------------------------------------------------------------
  1654. // Plugin buffers
  1655. void initBuffers() const noexcept override
  1656. {
  1657. fMidiIn.initBuffers();
  1658. fMidiOut.initBuffers();
  1659. CarlaPlugin::initBuffers();
  1660. }
  1661. void clearBuffers() noexcept override
  1662. {
  1663. carla_debug("CarlaPluginNative::clearBuffers() - start");
  1664. if (fAudioInBuffers != nullptr)
  1665. {
  1666. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1667. {
  1668. if (fAudioInBuffers[i] != nullptr)
  1669. {
  1670. delete[] fAudioInBuffers[i];
  1671. fAudioInBuffers[i] = nullptr;
  1672. }
  1673. }
  1674. delete[] fAudioInBuffers;
  1675. fAudioInBuffers = nullptr;
  1676. }
  1677. if (fAudioOutBuffers != nullptr)
  1678. {
  1679. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1680. {
  1681. if (fAudioOutBuffers[i] != nullptr)
  1682. {
  1683. delete[] fAudioOutBuffers[i];
  1684. fAudioOutBuffers[i] = nullptr;
  1685. }
  1686. }
  1687. delete[] fAudioOutBuffers;
  1688. fAudioOutBuffers = nullptr;
  1689. }
  1690. if (fMidiIn.count > 1)
  1691. pData->event.portIn = nullptr;
  1692. if (fMidiOut.count > 1)
  1693. pData->event.portOut = nullptr;
  1694. fMidiIn.clear();
  1695. fMidiOut.clear();
  1696. CarlaPlugin::clearBuffers();
  1697. carla_debug("CarlaPluginNative::clearBuffers() - end");
  1698. }
  1699. // -------------------------------------------------------------------
  1700. // Post-poned UI Stuff
  1701. void uiParameterChange(const uint32_t index, const float value) noexcept override
  1702. {
  1703. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1704. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1705. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  1706. if (! fIsUiVisible)
  1707. return;
  1708. if (fDescriptor->ui_set_parameter_value != nullptr)
  1709. fDescriptor->ui_set_parameter_value(fHandle, index, value);
  1710. }
  1711. void uiMidiProgramChange(const uint32_t index) noexcept override
  1712. {
  1713. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1714. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1715. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  1716. if (! fIsUiVisible)
  1717. return;
  1718. if (index >= pData->midiprog.count)
  1719. return;
  1720. if (fDescriptor->ui_set_midi_program != nullptr)
  1721. fDescriptor->ui_set_midi_program(fHandle, 0, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  1722. }
  1723. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  1724. {
  1725. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1726. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1727. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1728. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1729. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1730. if (! fIsUiVisible)
  1731. return;
  1732. // TODO
  1733. }
  1734. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  1735. {
  1736. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1737. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1738. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1739. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1740. if (! fIsUiVisible)
  1741. return;
  1742. if (fDescriptor == nullptr || fHandle == nullptr)
  1743. return;
  1744. // TODO
  1745. }
  1746. // -------------------------------------------------------------------
  1747. protected:
  1748. const NativeTimeInfo* handleGetTimeInfo() const noexcept
  1749. {
  1750. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, nullptr);
  1751. return &fTimeInfo;
  1752. }
  1753. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  1754. {
  1755. CARLA_SAFE_ASSERT_RETURN(pData->enabled, false);
  1756. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, false);
  1757. CARLA_SAFE_ASSERT_RETURN(fMidiOut.count > 0 || pData->event.portOut != nullptr, false);
  1758. CARLA_SAFE_ASSERT_RETURN(event != nullptr, false);
  1759. CARLA_SAFE_ASSERT_RETURN(event->data[0] != 0, false);
  1760. // reverse-find first free event, and put it there
  1761. for (uint32_t i=(kPluginMaxMidiEvents*2)-1; i >= fMidiEventCount; --i)
  1762. {
  1763. if (fMidiEvents[i].data[0] != 0)
  1764. continue;
  1765. std::memcpy(&fMidiEvents[i], event, sizeof(NativeMidiEvent));
  1766. return true;
  1767. }
  1768. carla_stdout("CarlaPluginNative::handleWriteMidiEvent(%p) - buffer full", event);
  1769. return false;
  1770. }
  1771. void handleUiParameterChanged(const uint32_t index, const float value)
  1772. {
  1773. setParameterValue(index, value, false, true, true);
  1774. }
  1775. void handleUiCustomDataChanged(const char* const key, const char* const value)
  1776. {
  1777. setCustomData(CUSTOM_DATA_TYPE_STRING, key, value, false);
  1778. }
  1779. void handleUiClosed()
  1780. {
  1781. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1782. fIsUiVisible = false;
  1783. }
  1784. const char* handleUiOpenFile(const bool isDir, const char* const title, const char* const filter)
  1785. {
  1786. return pData->engine->runFileCallback(FILE_CALLBACK_OPEN, isDir, title, filter);
  1787. }
  1788. const char* handleUiSaveFile(const bool isDir, const char* const title, const char* const filter)
  1789. {
  1790. return pData->engine->runFileCallback(FILE_CALLBACK_SAVE, isDir, title, filter);
  1791. }
  1792. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  1793. {
  1794. carla_debug("CarlaPluginNative::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)", opcode, index, value, ptr, opt);
  1795. intptr_t ret = 0;
  1796. switch (opcode)
  1797. {
  1798. case NATIVE_HOST_OPCODE_NULL:
  1799. break;
  1800. case NATIVE_HOST_OPCODE_UPDATE_PARAMETER:
  1801. // TODO
  1802. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, -1, 0, 0.0f, nullptr);
  1803. break;
  1804. case NATIVE_HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  1805. // TODO
  1806. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, -1, 0, 0.0f, nullptr);
  1807. break;
  1808. case NATIVE_HOST_OPCODE_RELOAD_PARAMETERS:
  1809. reload(); // FIXME
  1810. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PARAMETERS, pData->id, -1, 0, 0.0f, nullptr);
  1811. break;
  1812. case NATIVE_HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  1813. reloadPrograms(false);
  1814. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, -1, 0, 0.0f, nullptr);
  1815. break;
  1816. case NATIVE_HOST_OPCODE_RELOAD_ALL:
  1817. reload();
  1818. pData->engine->callback(ENGINE_CALLBACK_RELOAD_ALL, pData->id, -1, 0, 0.0f, nullptr);
  1819. break;
  1820. case NATIVE_HOST_OPCODE_UI_UNAVAILABLE:
  1821. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, nullptr);
  1822. fIsUiAvailable = false;
  1823. break;
  1824. case NATIVE_HOST_OPCODE_HOST_IDLE:
  1825. pData->engine->callback(ENGINE_CALLBACK_IDLE, 0, 0, 0, 0.0f, nullptr);
  1826. break;
  1827. case NATIVE_HOST_OPCODE_INTERNAL_PLUGIN:
  1828. ret = 1;
  1829. break;
  1830. }
  1831. return ret;
  1832. // unused for now
  1833. (void)index;
  1834. (void)value;
  1835. (void)ptr;
  1836. (void)opt;
  1837. }
  1838. // -------------------------------------------------------------------
  1839. public:
  1840. void* getNativeHandle() const noexcept override
  1841. {
  1842. return fHandle;
  1843. }
  1844. const void* getNativeDescriptor() const noexcept override
  1845. {
  1846. return fDescriptor;
  1847. }
  1848. // -------------------------------------------------------------------
  1849. bool init(const char* const name, const char* const label, const uint options)
  1850. {
  1851. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1852. // ---------------------------------------------------------------
  1853. // first checks
  1854. if (pData->client != nullptr)
  1855. {
  1856. pData->engine->setLastError("Plugin client is already registered");
  1857. return false;
  1858. }
  1859. if (label == nullptr && label[0] != '\0')
  1860. {
  1861. pData->engine->setLastError("null label");
  1862. return false;
  1863. }
  1864. // ---------------------------------------------------------------
  1865. // get descriptor that matches label
  1866. sPluginInitializer.initIfNeeded();
  1867. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = gPluginDescriptors.begin2(); it.valid(); it.next())
  1868. {
  1869. fDescriptor = it.getValue(nullptr);
  1870. CARLA_SAFE_ASSERT_BREAK(fDescriptor != nullptr);
  1871. carla_debug("Check vs \"%s\"", fDescriptor->label);
  1872. if (fDescriptor->label != nullptr && std::strcmp(fDescriptor->label, label) == 0)
  1873. break;
  1874. fDescriptor = nullptr;
  1875. }
  1876. if (fDescriptor == nullptr)
  1877. {
  1878. pData->engine->setLastError("Invalid internal plugin");
  1879. return false;
  1880. }
  1881. // ---------------------------------------------------------------
  1882. // set icon
  1883. if (std::strcmp(fDescriptor->label, "audiofile") == 0)
  1884. pData->iconName = carla_strdup_safe("file");
  1885. else if (std::strcmp(fDescriptor->label, "midifile") == 0)
  1886. pData->iconName = carla_strdup_safe("file");
  1887. // ---------------------------------------------------------------
  1888. // get info
  1889. if (name != nullptr && name[0] != '\0')
  1890. pData->name = pData->engine->getUniquePluginName(name);
  1891. else if (fDescriptor->name != nullptr && fDescriptor->name[0] != '\0')
  1892. pData->name = pData->engine->getUniquePluginName(fDescriptor->name);
  1893. else
  1894. pData->name = pData->engine->getUniquePluginName(label);
  1895. {
  1896. CARLA_ASSERT(fHost.uiName == nullptr);
  1897. char uiName[std::strlen(pData->name)+6+1];
  1898. std::strcpy(uiName, pData->name);
  1899. std::strcat(uiName, " (GUI)");
  1900. fHost.uiName = carla_strdup(uiName);
  1901. }
  1902. // ---------------------------------------------------------------
  1903. // register client
  1904. pData->client = pData->engine->addClient(this);
  1905. if (pData->client == nullptr || ! pData->client->isOk())
  1906. {
  1907. pData->engine->setLastError("Failed to register plugin client");
  1908. return false;
  1909. }
  1910. // ---------------------------------------------------------------
  1911. // initialize plugin
  1912. fHandle = fDescriptor->instantiate(&fHost);
  1913. if (fHandle == nullptr)
  1914. {
  1915. pData->engine->setLastError("Plugin failed to initialize");
  1916. return false;
  1917. }
  1918. // ---------------------------------------------------------------
  1919. // set default options
  1920. bool hasMidiProgs = false;
  1921. if (fDescriptor->get_midi_program_count != nullptr)
  1922. {
  1923. try {
  1924. hasMidiProgs = fDescriptor->get_midi_program_count(fHandle) > 0;
  1925. } catch (...) {}
  1926. }
  1927. pData->options = 0x0;
  1928. if (fMidiOut.count != 0 || (fDescriptor->hints & NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS) != 0)
  1929. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1930. else if (options & PLUGIN_OPTION_FIXED_BUFFERS)
  1931. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1932. if (pData->engine->getOptions().forceStereo)
  1933. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1934. else if (options & PLUGIN_OPTION_FORCE_STEREO)
  1935. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1936. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CHANNEL_PRESSURE)
  1937. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1938. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_NOTE_AFTERTOUCH)
  1939. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1940. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PITCHBEND)
  1941. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1942. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_ALL_SOUND_OFF)
  1943. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1944. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_CONTROL_CHANGES)
  1945. {
  1946. if (options & PLUGIN_OPTION_SEND_CONTROL_CHANGES)
  1947. pData->options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  1948. }
  1949. if (fDescriptor->supports & NATIVE_PLUGIN_SUPPORTS_PROGRAM_CHANGES)
  1950. {
  1951. CARLA_SAFE_ASSERT(! hasMidiProgs);
  1952. pData->options |= PLUGIN_OPTION_SEND_PROGRAM_CHANGES;
  1953. }
  1954. else if (hasMidiProgs)
  1955. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1956. return true;
  1957. }
  1958. private:
  1959. NativePluginHandle fHandle;
  1960. NativePluginHandle fHandle2;
  1961. NativeHostDescriptor fHost;
  1962. const NativePluginDescriptor* fDescriptor;
  1963. bool fIsProcessing;
  1964. bool fIsOffline;
  1965. bool fIsUiAvailable;
  1966. bool fIsUiVisible;
  1967. float** fAudioInBuffers;
  1968. float** fAudioOutBuffers;
  1969. uint32_t fMidiEventCount;
  1970. NativeMidiEvent fMidiEvents[kPluginMaxMidiEvents*2];
  1971. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  1972. uint32_t fCurBufferSize;
  1973. double fCurSampleRate;
  1974. NativePluginMidiData fMidiIn;
  1975. NativePluginMidiData fMidiOut;
  1976. NativeTimeInfo fTimeInfo;
  1977. // -------------------------------------------------------------------
  1978. #define handlePtr ((CarlaPluginNative*)handle)
  1979. static uint32_t carla_host_get_buffer_size(NativeHostHandle handle) noexcept
  1980. {
  1981. return handlePtr->fCurBufferSize;
  1982. }
  1983. static double carla_host_get_sample_rate(NativeHostHandle handle) noexcept
  1984. {
  1985. return handlePtr->fCurSampleRate;
  1986. }
  1987. static bool carla_host_is_offline(NativeHostHandle handle) noexcept
  1988. {
  1989. return handlePtr->fIsOffline;
  1990. }
  1991. static const NativeTimeInfo* carla_host_get_time_info(NativeHostHandle handle) noexcept
  1992. {
  1993. return handlePtr->handleGetTimeInfo();
  1994. }
  1995. static bool carla_host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  1996. {
  1997. return handlePtr->handleWriteMidiEvent(event);
  1998. }
  1999. static void carla_host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  2000. {
  2001. handlePtr->handleUiParameterChanged(index, value);
  2002. }
  2003. static void carla_host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  2004. {
  2005. handlePtr->handleUiCustomDataChanged(key, value);
  2006. }
  2007. static void carla_host_ui_closed(NativeHostHandle handle)
  2008. {
  2009. handlePtr->handleUiClosed();
  2010. }
  2011. static const char* carla_host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  2012. {
  2013. return handlePtr->handleUiOpenFile(isDir, title, filter);
  2014. }
  2015. static const char* carla_host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  2016. {
  2017. return handlePtr->handleUiSaveFile(isDir, title, filter);
  2018. }
  2019. static intptr_t carla_host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  2020. {
  2021. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  2022. }
  2023. #undef handlePtr
  2024. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginNative)
  2025. };
  2026. // -----------------------------------------------------------------------
  2027. CarlaPlugin* CarlaPlugin::newNative(const Initializer& init)
  2028. {
  2029. carla_debug("CarlaPlugin::newNative({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "})", init.engine, init.filename, init.name, init.label, init.uniqueId);
  2030. CarlaPluginNative* const plugin(new CarlaPluginNative(init.engine, init.id));
  2031. if (! plugin->init(init.name, init.label, init.options))
  2032. {
  2033. delete plugin;
  2034. return nullptr;
  2035. }
  2036. return plugin;
  2037. }
  2038. // -----------------------------------------------------------------------
  2039. CARLA_BACKEND_END_NAMESPACE