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.

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