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.

2503 lines
88KB

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