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.

2492 lines
88KB

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