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.

2516 lines
88KB

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