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.

2476 lines
87KB

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