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.

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