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.

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