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.

2496 lines
89KB

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