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.

2488 lines
87KB

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