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. #ifndef BUILD_BRIDGE
  1106. bool allNotesOffSent = false;
  1107. #endif
  1108. bool sampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  1109. uint32_t time, nEvents = pData->event.portIn->getEventCount();
  1110. uint32_t startTime = 0;
  1111. uint32_t timeOffset = 0;
  1112. uint32_t nextBankId = 0;
  1113. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1114. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1115. for (uint32_t i=0; i < nEvents; ++i)
  1116. {
  1117. const EngineEvent& event(pData->event.portIn->getEvent(i));
  1118. time = event.time;
  1119. if (time >= frames)
  1120. continue;
  1121. CARLA_ASSERT_INT2(time >= timeOffset, time, timeOffset);
  1122. if (time > timeOffset && sampleAccurate)
  1123. {
  1124. if (processSingle(inBuffer, outBuffer, time - timeOffset, timeOffset))
  1125. {
  1126. startTime = 0;
  1127. timeOffset = time;
  1128. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1129. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1130. else
  1131. nextBankId = 0;
  1132. if (fMidiEventCount > 0)
  1133. {
  1134. carla_zeroStruct<NativeMidiEvent>(fMidiEvents, fMidiEventCount);
  1135. fMidiEventCount = 0;
  1136. }
  1137. }
  1138. else
  1139. startTime += timeOffset;
  1140. }
  1141. // Control change
  1142. switch (event.type)
  1143. {
  1144. case kEngineEventTypeNull:
  1145. break;
  1146. case kEngineEventTypeControl: {
  1147. const EngineControlEvent& ctrlEvent = event.ctrl;
  1148. switch (ctrlEvent.type)
  1149. {
  1150. case kEngineControlEventTypeNull:
  1151. break;
  1152. case kEngineControlEventTypeParameter: {
  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. #ifndef BUILD_BRIDGE
  1276. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1277. {
  1278. allNotesOffSent = true;
  1279. sendMidiAllNotesOffToCallback();
  1280. }
  1281. #endif
  1282. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1283. continue;
  1284. fMidiEvents[fMidiEventCount].port = 0;
  1285. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1286. fMidiEvents[fMidiEventCount].data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + event.channel);
  1287. fMidiEvents[fMidiEventCount].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1288. fMidiEvents[fMidiEventCount].data[2] = 0;
  1289. fMidiEvents[fMidiEventCount].size = 3;
  1290. fMidiEventCount += 1;
  1291. }
  1292. break;
  1293. }
  1294. break;
  1295. }
  1296. case kEngineEventTypeMidi: {
  1297. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1298. continue;
  1299. const EngineMidiEvent& midiEvent(event.midi);
  1300. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  1301. uint8_t channel = event.channel;
  1302. if (MIDI_IS_STATUS_CHANNEL_PRESSURE(status) && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1303. continue;
  1304. if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1305. continue;
  1306. if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1307. continue;
  1308. if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1309. continue;
  1310. // Fix bad note-off
  1311. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1312. status = MIDI_STATUS_NOTE_OFF;
  1313. fMidiEvents[fMidiEventCount].port = 0;
  1314. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1315. fMidiEvents[fMidiEventCount].size = midiEvent.size;
  1316. fMidiEvents[fMidiEventCount].data[0] = static_cast<uint8_t>(status + channel);
  1317. fMidiEvents[fMidiEventCount].data[1] = midiEvent.data[1];
  1318. fMidiEvents[fMidiEventCount].data[2] = midiEvent.data[2];
  1319. fMidiEvents[fMidiEventCount].data[3] = midiEvent.data[3];
  1320. fMidiEventCount += 1;
  1321. if (status == MIDI_STATUS_NOTE_ON)
  1322. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  1323. else if (status == MIDI_STATUS_NOTE_OFF)
  1324. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  1325. break;
  1326. }
  1327. }
  1328. }
  1329. pData->postRtEvents.trySplice();
  1330. if (frames > timeOffset)
  1331. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  1332. } // End of Event Input and Processing
  1333. // --------------------------------------------------------------------------------------------------------
  1334. // Plugin processing (no events)
  1335. else
  1336. {
  1337. processSingle(inBuffer, outBuffer, frames, 0);
  1338. } // End of Plugin processing (no events)
  1339. // --------------------------------------------------------------------------------------------------------
  1340. // Control and MIDI Output
  1341. if (fMidiOut.count > 0 || pData->event.portOut != nullptr)
  1342. {
  1343. #ifndef BUILD_BRIDGE
  1344. float value, curValue;
  1345. for (uint32_t k=0; k < pData->param.count; ++k)
  1346. {
  1347. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1348. continue;
  1349. curValue = fDescriptor->get_parameter_value(fHandle, k);
  1350. pData->param.ranges[k].fixValue(curValue);
  1351. if (pData->param.data[k].midiCC > 0)
  1352. {
  1353. value = pData->param.ranges[k].getNormalizedValue(curValue);
  1354. pData->event.portOut->writeControlEvent(0, pData->param.data[k].midiChannel, kEngineControlEventTypeParameter, static_cast<uint16_t>(pData->param.data[k].midiCC), value);
  1355. }
  1356. }
  1357. #endif
  1358. // reverse lookup MIDI events
  1359. for (uint32_t k = (kPluginMaxMidiEvents*2)-1; k >= fMidiEventCount; --k)
  1360. {
  1361. if (fMidiEvents[k].data[0] == 0)
  1362. break;
  1363. const uint8_t channel = uint8_t(MIDI_GET_CHANNEL_FROM_DATA(fMidiEvents[k].data));
  1364. const uint8_t port = fMidiEvents[k].port;
  1365. if (pData->event.portOut != nullptr)
  1366. pData->event.portOut->writeMidiEvent(fMidiEvents[k].time, channel, port, fMidiEvents[k].size, fMidiEvents[k].data);
  1367. else if (port < fMidiOut.count)
  1368. fMidiOut.ports[port]->writeMidiEvent(fMidiEvents[k].time, channel, port, fMidiEvents[k].size, fMidiEvents[k].data);
  1369. }
  1370. } // End of Control and MIDI Output
  1371. }
  1372. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  1373. {
  1374. CARLA_ASSERT(frames > 0);
  1375. if (frames == 0)
  1376. return false;
  1377. if (pData->audioIn.count > 0)
  1378. {
  1379. CARLA_ASSERT(inBuffer != nullptr);
  1380. if (inBuffer == nullptr)
  1381. return false;
  1382. }
  1383. if (pData->audioOut.count > 0)
  1384. {
  1385. CARLA_ASSERT(outBuffer != nullptr);
  1386. if (outBuffer == nullptr)
  1387. return false;
  1388. }
  1389. uint32_t i, k;
  1390. // --------------------------------------------------------------------------------------------------------
  1391. // Try lock, silence otherwise
  1392. if (pData->engine->isOffline())
  1393. {
  1394. pData->singleMutex.lock();
  1395. }
  1396. else if (! pData->singleMutex.tryLock())
  1397. {
  1398. for (i=0; i < pData->audioOut.count; ++i)
  1399. {
  1400. for (k=0; k < frames; ++k)
  1401. outBuffer[i][k+timeOffset] = 0.0f;
  1402. }
  1403. return false;
  1404. }
  1405. // --------------------------------------------------------------------------------------------------------
  1406. // Reset audio buffers
  1407. for (i=0; i < pData->audioIn.count; ++i)
  1408. FloatVectorOperations::copy(fAudioInBuffers[i], inBuffer[i]+timeOffset, static_cast<int>(frames));
  1409. for (i=0; i < pData->audioOut.count; ++i)
  1410. FloatVectorOperations::clear(fAudioOutBuffers[i], static_cast<int>(frames));
  1411. // --------------------------------------------------------------------------------------------------------
  1412. // Run plugin
  1413. fIsProcessing = true;
  1414. if (fHandle2 == nullptr)
  1415. {
  1416. fDescriptor->process(fHandle, fAudioInBuffers, fAudioOutBuffers, frames, fMidiEvents, fMidiEventCount);
  1417. }
  1418. else
  1419. {
  1420. fDescriptor->process(fHandle,
  1421. (pData->audioIn.count > 0) ? &fAudioInBuffers[0] : nullptr,
  1422. (pData->audioOut.count > 0) ? &fAudioOutBuffers[0] : nullptr,
  1423. frames, fMidiEvents, fMidiEventCount);
  1424. fDescriptor->process(fHandle2,
  1425. (pData->audioIn.count > 0) ? &fAudioInBuffers[1] : nullptr,
  1426. (pData->audioOut.count > 0) ? &fAudioOutBuffers[1] : nullptr,
  1427. frames, fMidiEvents, fMidiEventCount);
  1428. }
  1429. fIsProcessing = false;
  1430. fTimeInfo.frame += frames;
  1431. #ifndef BUILD_BRIDGE
  1432. // --------------------------------------------------------------------------------------------------------
  1433. // Post-processing (dry/wet, volume and balance)
  1434. {
  1435. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  1436. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  1437. bool isPair;
  1438. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1439. for (i=0; i < pData->audioOut.count; ++i)
  1440. {
  1441. // Dry/Wet
  1442. if (doDryWet)
  1443. {
  1444. for (k=0; k < frames; ++k)
  1445. {
  1446. bufValue = fAudioInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  1447. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1448. }
  1449. }
  1450. // Balance
  1451. if (doBalance)
  1452. {
  1453. isPair = (i % 2 == 0);
  1454. if (isPair)
  1455. {
  1456. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1457. FloatVectorOperations::copy(oldBufLeft, fAudioOutBuffers[i], static_cast<int>(frames));
  1458. }
  1459. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1460. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1461. for (k=0; k < frames; ++k)
  1462. {
  1463. if (isPair)
  1464. {
  1465. // left
  1466. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1467. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1468. }
  1469. else
  1470. {
  1471. // right
  1472. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1473. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1474. }
  1475. }
  1476. }
  1477. // Volume (and buffer copy)
  1478. {
  1479. for (k=0; k < frames; ++k)
  1480. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  1481. }
  1482. }
  1483. } // End of Post-processing
  1484. #else
  1485. for (i=0; i < pData->audioOut.count; ++i)
  1486. {
  1487. for (k=0; k < frames; ++k)
  1488. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1489. }
  1490. #endif
  1491. // --------------------------------------------------------------------------------------------------------
  1492. pData->singleMutex.unlock();
  1493. return true;
  1494. }
  1495. void bufferSizeChanged(const uint32_t newBufferSize) override
  1496. {
  1497. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1498. carla_debug("NativePlugin::bufferSizeChanged(%i)", newBufferSize);
  1499. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1500. {
  1501. if (fAudioInBuffers[i] != nullptr)
  1502. delete[] fAudioInBuffers[i];
  1503. fAudioInBuffers[i] = new float[newBufferSize];
  1504. }
  1505. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1506. {
  1507. if (fAudioOutBuffers[i] != nullptr)
  1508. delete[] fAudioOutBuffers[i];
  1509. fAudioOutBuffers[i] = new float[newBufferSize];
  1510. }
  1511. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1512. {
  1513. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, static_cast<intptr_t>(newBufferSize), nullptr, 0.0f);
  1514. if (fHandle2 != nullptr)
  1515. fDescriptor->dispatcher(fHandle2, PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, static_cast<intptr_t>(newBufferSize), nullptr, 0.0f);
  1516. }
  1517. }
  1518. void sampleRateChanged(const double newSampleRate) override
  1519. {
  1520. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1521. carla_debug("NativePlugin::sampleRateChanged(%g)", newSampleRate);
  1522. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1523. {
  1524. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  1525. if (fHandle2 != nullptr)
  1526. fDescriptor->dispatcher(fHandle2, PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  1527. }
  1528. }
  1529. void offlineModeChanged(const bool isOffline) override
  1530. {
  1531. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1532. {
  1533. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  1534. if (fHandle2 != nullptr)
  1535. fDescriptor->dispatcher(fHandle2, PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  1536. }
  1537. }
  1538. // -------------------------------------------------------------------
  1539. // Plugin buffers
  1540. void initBuffers() const noexcept override
  1541. {
  1542. fMidiIn.initBuffers();
  1543. fMidiOut.initBuffers();
  1544. CarlaPlugin::initBuffers();
  1545. }
  1546. void clearBuffers() noexcept override
  1547. {
  1548. carla_debug("NativePlugin::clearBuffers() - start");
  1549. if (fAudioInBuffers != nullptr)
  1550. {
  1551. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1552. {
  1553. if (fAudioInBuffers[i] != nullptr)
  1554. {
  1555. delete[] fAudioInBuffers[i];
  1556. fAudioInBuffers[i] = nullptr;
  1557. }
  1558. }
  1559. delete[] fAudioInBuffers;
  1560. fAudioInBuffers = nullptr;
  1561. }
  1562. if (fAudioOutBuffers != nullptr)
  1563. {
  1564. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1565. {
  1566. if (fAudioOutBuffers[i] != nullptr)
  1567. {
  1568. delete[] fAudioOutBuffers[i];
  1569. fAudioOutBuffers[i] = nullptr;
  1570. }
  1571. }
  1572. delete[] fAudioOutBuffers;
  1573. fAudioOutBuffers = nullptr;
  1574. }
  1575. fMidiIn.clear();
  1576. fMidiOut.clear();
  1577. CarlaPlugin::clearBuffers();
  1578. carla_debug("NativePlugin::clearBuffers() - end");
  1579. }
  1580. // -------------------------------------------------------------------
  1581. // Post-poned UI Stuff
  1582. void uiParameterChange(const uint32_t index, const float value) noexcept override
  1583. {
  1584. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1585. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1586. CARLA_SAFE_ASSERT_RETURN(index < pData->param.count,);
  1587. if (! fIsUiVisible)
  1588. return;
  1589. if (fDescriptor->ui_set_parameter_value != nullptr)
  1590. fDescriptor->ui_set_parameter_value(fHandle, index, value);
  1591. }
  1592. void uiMidiProgramChange(const uint32_t index) noexcept override
  1593. {
  1594. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1595. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1596. CARLA_SAFE_ASSERT_RETURN(index < pData->midiprog.count,);
  1597. if (! fIsUiVisible)
  1598. return;
  1599. if (index >= pData->midiprog.count)
  1600. return;
  1601. if (fDescriptor->ui_set_midi_program != nullptr)
  1602. fDescriptor->ui_set_midi_program(fHandle, 0, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  1603. }
  1604. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  1605. {
  1606. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1607. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1608. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1609. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1610. CARLA_SAFE_ASSERT_RETURN(velo > 0 && velo < MAX_MIDI_VALUE,);
  1611. if (! fIsUiVisible)
  1612. return;
  1613. // TODO
  1614. }
  1615. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  1616. {
  1617. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1618. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1619. CARLA_SAFE_ASSERT_RETURN(channel < MAX_MIDI_CHANNELS,);
  1620. CARLA_SAFE_ASSERT_RETURN(note < MAX_MIDI_NOTE,);
  1621. if (! fIsUiVisible)
  1622. return;
  1623. if (fDescriptor == nullptr || fHandle == nullptr)
  1624. return;
  1625. // TODO
  1626. }
  1627. // -------------------------------------------------------------------
  1628. protected:
  1629. uint32_t handleGetBufferSize() const noexcept
  1630. {
  1631. return pData->engine->getBufferSize();
  1632. }
  1633. double handleGetSampleRate() const noexcept
  1634. {
  1635. return pData->engine->getSampleRate();
  1636. }
  1637. bool handleIsOffline() const noexcept
  1638. {
  1639. return pData->engine->isOffline();
  1640. }
  1641. const NativeTimeInfo* handleGetTimeInfo() const noexcept
  1642. {
  1643. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, nullptr);
  1644. return &fTimeInfo;
  1645. }
  1646. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  1647. {
  1648. CARLA_ASSERT(pData->enabled);
  1649. CARLA_ASSERT(fIsProcessing);
  1650. CARLA_ASSERT(fMidiOut.count > 0 || pData->event.portOut != nullptr);
  1651. CARLA_ASSERT(event != nullptr);
  1652. CARLA_ASSERT(event->data[0] != 0);
  1653. if (! pData->enabled)
  1654. return false;
  1655. if (fMidiOut.count == 0)
  1656. return false;
  1657. if (event == nullptr)
  1658. return false;
  1659. if (event->data[0] == 0)
  1660. return false;
  1661. if (! fIsProcessing)
  1662. {
  1663. carla_stderr2("NativePlugin::handleWriteMidiEvent(%p) - received MIDI out event outside audio thread, ignoring", event);
  1664. return false;
  1665. }
  1666. // reverse-find first free event, and put it there
  1667. for (uint32_t i=(kPluginMaxMidiEvents*2)-1; i > fMidiEventCount; --i)
  1668. {
  1669. if (fMidiEvents[i].data[0] == 0)
  1670. {
  1671. std::memcpy(&fMidiEvents[i], event, sizeof(NativeMidiEvent));
  1672. return true;
  1673. }
  1674. }
  1675. return false;
  1676. }
  1677. void handleUiParameterChanged(const uint32_t index, const float value)
  1678. {
  1679. setParameterValue(index, value, false, true, true);
  1680. }
  1681. void handleUiCustomDataChanged(const char* const key, const char* const value)
  1682. {
  1683. setCustomData(CUSTOM_DATA_TYPE_STRING, key, value, false);
  1684. }
  1685. void handleUiClosed()
  1686. {
  1687. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1688. fIsUiVisible = false;
  1689. }
  1690. const char* handleUiOpenFile(const bool isDir, const char* const title, const char* const filter)
  1691. {
  1692. return pData->engine->runFileCallback(FILE_CALLBACK_OPEN, isDir, title, filter);
  1693. }
  1694. const char* handleUiSaveFile(const bool isDir, const char* const title, const char* const filter)
  1695. {
  1696. return pData->engine->runFileCallback(FILE_CALLBACK_SAVE, isDir, title, filter);
  1697. }
  1698. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  1699. {
  1700. carla_debug("NativePlugin::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)", opcode, index, value, ptr, opt);
  1701. intptr_t ret = 0;
  1702. switch (opcode)
  1703. {
  1704. case ::HOST_OPCODE_NULL:
  1705. break;
  1706. #ifdef BUILD_BRIDGE
  1707. case ::HOST_OPCODE_SET_VOLUME:
  1708. case ::HOST_OPCODE_SET_DRYWET:
  1709. case ::HOST_OPCODE_SET_BALANCE_LEFT:
  1710. case ::HOST_OPCODE_SET_BALANCE_RIGHT:
  1711. case ::HOST_OPCODE_SET_PANNING:
  1712. break;
  1713. #else
  1714. case ::HOST_OPCODE_SET_VOLUME:
  1715. setVolume(opt, true, true);
  1716. break;
  1717. case ::HOST_OPCODE_SET_DRYWET:
  1718. setDryWet(opt, true, true);
  1719. break;
  1720. case ::HOST_OPCODE_SET_BALANCE_LEFT:
  1721. setBalanceLeft(opt, true, true);
  1722. break;
  1723. case ::HOST_OPCODE_SET_BALANCE_RIGHT:
  1724. setBalanceRight(opt, true, true);
  1725. break;
  1726. case ::HOST_OPCODE_SET_PANNING:
  1727. setPanning(opt, true, true);
  1728. break;
  1729. #endif
  1730. case ::HOST_OPCODE_GET_PARAMETER_MIDI_CC:
  1731. case ::HOST_OPCODE_SET_PARAMETER_MIDI_CC:
  1732. // TODO
  1733. break;
  1734. case ::HOST_OPCODE_SET_PROCESS_PRECISION:
  1735. // TODO
  1736. break;
  1737. case ::HOST_OPCODE_UPDATE_PARAMETER:
  1738. // TODO
  1739. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, -1, 0, 0.0f, nullptr);
  1740. break;
  1741. case ::HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  1742. // TODO
  1743. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, -1, 0, 0.0f, nullptr);
  1744. break;
  1745. case ::HOST_OPCODE_RELOAD_PARAMETERS:
  1746. reload(); // FIXME
  1747. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PARAMETERS, pData->id, -1, 0, 0.0f, nullptr);
  1748. break;
  1749. case ::HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  1750. reloadPrograms(false);
  1751. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, -1, 0, 0.0f, nullptr);
  1752. break;
  1753. case ::HOST_OPCODE_RELOAD_ALL:
  1754. reload();
  1755. pData->engine->callback(ENGINE_CALLBACK_RELOAD_ALL, pData->id, -1, 0, 0.0f, nullptr);
  1756. break;
  1757. case ::HOST_OPCODE_UI_UNAVAILABLE:
  1758. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, nullptr);
  1759. break;
  1760. }
  1761. return ret;
  1762. // unused for now
  1763. (void)index;
  1764. (void)value;
  1765. (void)ptr;
  1766. (void)opt;
  1767. }
  1768. // -------------------------------------------------------------------
  1769. public:
  1770. static size_t getPluginCount() noexcept
  1771. {
  1772. return gPluginDescriptors.count();
  1773. }
  1774. static const NativePluginDescriptor* getPluginDescriptor(const size_t index) noexcept
  1775. {
  1776. CARLA_SAFE_ASSERT_RETURN(index < gPluginDescriptors.count(), nullptr);
  1777. return gPluginDescriptors.getAt(index, nullptr);
  1778. }
  1779. // -------------------------------------------------------------------
  1780. void* getNativeHandle() const noexcept override
  1781. {
  1782. return fHandle;
  1783. }
  1784. const void* getNativeDescriptor() const noexcept override
  1785. {
  1786. return fDescriptor;
  1787. }
  1788. // -------------------------------------------------------------------
  1789. bool init(const char* const name, const char* const label)
  1790. {
  1791. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1792. // ---------------------------------------------------------------
  1793. // first checks
  1794. if (pData->client != nullptr)
  1795. {
  1796. pData->engine->setLastError("Plugin client is already registered");
  1797. return false;
  1798. }
  1799. if (label == nullptr && label[0] != '\0')
  1800. {
  1801. pData->engine->setLastError("null label");
  1802. return false;
  1803. }
  1804. // ---------------------------------------------------------------
  1805. // get descriptor that matches label
  1806. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = gPluginDescriptors.begin(); it.valid(); it.next())
  1807. {
  1808. fDescriptor = it.getValue();
  1809. CARLA_SAFE_ASSERT_BREAK(fDescriptor != nullptr);
  1810. carla_debug("Check vs \"%s\"", fDescriptor->label);
  1811. if (fDescriptor->label != nullptr && std::strcmp(fDescriptor->label, label) == 0)
  1812. break;
  1813. fDescriptor = nullptr;
  1814. }
  1815. if (fDescriptor == nullptr)
  1816. {
  1817. pData->engine->setLastError("Invalid internal plugin");
  1818. return false;
  1819. }
  1820. // ---------------------------------------------------------------
  1821. // set icon
  1822. if (std::strcmp(fDescriptor->label, "audiofile") == 0)
  1823. pData->iconName = carla_strdup_safe("file");
  1824. else if (std::strcmp(fDescriptor->label, "midifile") == 0)
  1825. pData->iconName = carla_strdup_safe("file");
  1826. // ---------------------------------------------------------------
  1827. // get info
  1828. if (name != nullptr && name[0] != '\0')
  1829. pData->name = pData->engine->getUniquePluginName(name);
  1830. else if (fDescriptor->name != nullptr && fDescriptor->name[0] != '\0')
  1831. pData->name = pData->engine->getUniquePluginName(fDescriptor->name);
  1832. else
  1833. pData->name = pData->engine->getUniquePluginName(label);
  1834. {
  1835. CARLA_ASSERT(fHost.uiName == nullptr);
  1836. char uiName[std::strlen(pData->name)+6+1];
  1837. std::strcpy(uiName, pData->name);
  1838. std::strcat(uiName, " (GUI)");
  1839. fHost.uiName = carla_strdup(uiName);
  1840. }
  1841. // ---------------------------------------------------------------
  1842. // register client
  1843. pData->client = pData->engine->addClient(this);
  1844. if (pData->client == nullptr || ! pData->client->isOk())
  1845. {
  1846. pData->engine->setLastError("Failed to register plugin client");
  1847. return false;
  1848. }
  1849. // ---------------------------------------------------------------
  1850. // initialize plugin
  1851. fHandle = fDescriptor->instantiate(&fHost);
  1852. if (fHandle == nullptr)
  1853. {
  1854. pData->engine->setLastError("Plugin failed to initialize");
  1855. return false;
  1856. }
  1857. // ---------------------------------------------------------------
  1858. // set default options
  1859. const bool hasMidiProgs(fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_count(fHandle) > 0);
  1860. pData->options = 0x0;
  1861. if (hasMidiProgs && (fDescriptor->supports & ::PLUGIN_SUPPORTS_PROGRAM_CHANGES) == 0)
  1862. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1863. if (getMidiInCount() > 0 || (fDescriptor->hints & ::PLUGIN_NEEDS_FIXED_BUFFERS) != 0)
  1864. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1865. if (pData->engine->getOptions().forceStereo)
  1866. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1867. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_CHANNEL_PRESSURE)
  1868. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1869. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_NOTE_AFTERTOUCH)
  1870. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1871. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_PITCHBEND)
  1872. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1873. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_ALL_SOUND_OFF)
  1874. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1875. return true;
  1876. }
  1877. private:
  1878. NativePluginHandle fHandle;
  1879. NativePluginHandle fHandle2;
  1880. NativeHostDescriptor fHost;
  1881. const NativePluginDescriptor* fDescriptor;
  1882. bool fIsProcessing;
  1883. bool fIsUiVisible;
  1884. float** fAudioInBuffers;
  1885. float** fAudioOutBuffers;
  1886. uint32_t fMidiEventCount;
  1887. NativeMidiEvent fMidiEvents[kPluginMaxMidiEvents*2];
  1888. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  1889. NativePluginMidiData fMidiIn;
  1890. NativePluginMidiData fMidiOut;
  1891. NativeTimeInfo fTimeInfo;
  1892. // -------------------------------------------------------------------
  1893. #define handlePtr ((NativePlugin*)handle)
  1894. static uint32_t carla_host_get_buffer_size(NativeHostHandle handle) noexcept
  1895. {
  1896. return handlePtr->handleGetBufferSize();
  1897. }
  1898. static double carla_host_get_sample_rate(NativeHostHandle handle) noexcept
  1899. {
  1900. return handlePtr->handleGetSampleRate();
  1901. }
  1902. static bool carla_host_is_offline(NativeHostHandle handle) noexcept
  1903. {
  1904. return handlePtr->handleIsOffline();
  1905. }
  1906. static const NativeTimeInfo* carla_host_get_time_info(NativeHostHandle handle) noexcept
  1907. {
  1908. return handlePtr->handleGetTimeInfo();
  1909. }
  1910. static bool carla_host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  1911. {
  1912. return handlePtr->handleWriteMidiEvent(event);
  1913. }
  1914. static void carla_host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  1915. {
  1916. handlePtr->handleUiParameterChanged(index, value);
  1917. }
  1918. static void carla_host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  1919. {
  1920. handlePtr->handleUiCustomDataChanged(key, value);
  1921. }
  1922. static void carla_host_ui_closed(NativeHostHandle handle)
  1923. {
  1924. handlePtr->handleUiClosed();
  1925. }
  1926. static const char* carla_host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  1927. {
  1928. return handlePtr->handleUiOpenFile(isDir, title, filter);
  1929. }
  1930. static const char* carla_host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  1931. {
  1932. return handlePtr->handleUiSaveFile(isDir, title, filter);
  1933. }
  1934. static intptr_t carla_host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  1935. {
  1936. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  1937. }
  1938. #undef handlePtr
  1939. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  1940. };
  1941. // -----------------------------------------------------------------------
  1942. size_t CarlaPlugin::getNativePluginCount() noexcept
  1943. {
  1944. return NativePlugin::getPluginCount();
  1945. }
  1946. const NativePluginDescriptor* CarlaPlugin::getNativePluginDescriptor(const size_t index) noexcept
  1947. {
  1948. return NativePlugin::getPluginDescriptor(index);
  1949. }
  1950. // -----------------------------------------------------------------------
  1951. CarlaPlugin* CarlaPlugin::newNative(const Initializer& init)
  1952. {
  1953. carla_debug("CarlaPlugin::newNative({%p, \"%s\", \"%s\", \"%s\", " P_INT64 "})", init.engine, init.filename, init.name, init.label, init.uniqueId);
  1954. NativePlugin* const plugin(new NativePlugin(init.engine, init.id));
  1955. if (! plugin->init(init.name, init.label))
  1956. {
  1957. delete plugin;
  1958. return nullptr;
  1959. }
  1960. plugin->reload();
  1961. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  1962. {
  1963. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo Internal plugins, sorry!");
  1964. delete plugin;
  1965. return nullptr;
  1966. }
  1967. return plugin;
  1968. }
  1969. // -----------------------------------------------------------------------
  1970. CARLA_BACKEND_END_NAMESPACE