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.

2504 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 "CarlaHost.h"
  23. #include <QtCore/QStringList>
  24. CARLA_BACKEND_START_NAMESPACE
  25. #if 0
  26. }
  27. #endif
  28. struct NativePluginMidiData {
  29. uint32_t count;
  30. uint32_t* indexes;
  31. CarlaEngineEventPort** ports;
  32. NativePluginMidiData()
  33. : count(0),
  34. indexes(nullptr),
  35. ports(nullptr) {}
  36. ~NativePluginMidiData()
  37. {
  38. CARLA_ASSERT_INT(count == 0, count);
  39. CARLA_ASSERT(indexes == nullptr);
  40. CARLA_ASSERT(ports == nullptr);
  41. }
  42. void createNew(const uint32_t newCount)
  43. {
  44. CARLA_SAFE_ASSERT_INT(count == 0, count);
  45. CARLA_SAFE_ASSERT_RETURN(indexes == nullptr,);
  46. CARLA_SAFE_ASSERT_RETURN(ports == nullptr,);
  47. CARLA_SAFE_ASSERT_RETURN(newCount > 0,);
  48. ports = new CarlaEngineEventPort*[newCount];
  49. indexes = new uint32_t[newCount];
  50. count = newCount;
  51. for (uint32_t i=0; i < newCount; ++i)
  52. ports[i] = nullptr;
  53. for (uint32_t i=0; i < newCount; ++i)
  54. indexes[i] = 0;
  55. }
  56. void clear()
  57. {
  58. if (ports != nullptr)
  59. {
  60. for (uint32_t i=0; i < count; ++i)
  61. {
  62. if (ports[i] != nullptr)
  63. {
  64. delete ports[i];
  65. ports[i] = nullptr;
  66. }
  67. }
  68. delete[] ports;
  69. ports = nullptr;
  70. }
  71. if (indexes != nullptr)
  72. {
  73. delete[] indexes;
  74. indexes = nullptr;
  75. }
  76. count = 0;
  77. }
  78. void initBuffers()
  79. {
  80. for (uint32_t i=0; i < count; ++i)
  81. {
  82. if (ports[i] != nullptr)
  83. ports[i]->initBuffer();
  84. }
  85. }
  86. CARLA_DECLARE_NON_COPY_STRUCT(NativePluginMidiData)
  87. };
  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 int index(midiProg.toInt(&ok));
  461. if (ok && index >= 0 && index < static_cast<int>(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 int32_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 = static_cast<int32_t>(j);
  733. pData->param.data[j].rindex = static_cast<int32_t>(j);
  734. pData->param.data[j].midiCC = -1;
  735. pData->param.data[j].midiChannel = 0;
  736. pData->param.special[j] = PARAMETER_SPECIAL_NULL;
  737. float min, max, def, step, stepSmall, stepLarge;
  738. // min value
  739. min = paramInfo->ranges.min;
  740. // max value
  741. max = paramInfo->ranges.max;
  742. if (min > max)
  743. max = min;
  744. else if (max < min)
  745. min = max;
  746. if (max - min == 0.0f)
  747. {
  748. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", paramInfo->name);
  749. max = min + 0.1f;
  750. }
  751. // default value
  752. def = paramInfo->ranges.def;
  753. if (def < min)
  754. def = min;
  755. else if (def > max)
  756. def = max;
  757. if (paramInfo->hints & ::PARAMETER_USES_SAMPLE_RATE)
  758. {
  759. min *= sampleRate;
  760. max *= sampleRate;
  761. def *= sampleRate;
  762. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  763. }
  764. if (paramInfo->hints & ::PARAMETER_IS_BOOLEAN)
  765. {
  766. step = max - min;
  767. stepSmall = step;
  768. stepLarge = step;
  769. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  770. }
  771. else if (paramInfo->hints & ::PARAMETER_IS_INTEGER)
  772. {
  773. step = 1.0f;
  774. stepSmall = 1.0f;
  775. stepLarge = 10.0f;
  776. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  777. }
  778. else
  779. {
  780. float range = max - min;
  781. step = range/100.0f;
  782. stepSmall = range/1000.0f;
  783. stepLarge = range/10.0f;
  784. }
  785. if (paramInfo->hints & ::PARAMETER_IS_OUTPUT)
  786. {
  787. pData->param.data[j].type = PARAMETER_OUTPUT;
  788. needsCtrlOut = true;
  789. }
  790. else
  791. {
  792. pData->param.data[j].type = PARAMETER_INPUT;
  793. needsCtrlIn = true;
  794. }
  795. // extra parameter hints
  796. if (paramInfo->hints & ::PARAMETER_IS_ENABLED)
  797. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  798. if (paramInfo->hints & ::PARAMETER_IS_AUTOMABLE)
  799. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  800. if (paramInfo->hints & ::PARAMETER_IS_LOGARITHMIC)
  801. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  802. if (paramInfo->hints & ::PARAMETER_USES_SCALEPOINTS)
  803. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  804. if (paramInfo->hints & ::PARAMETER_USES_CUSTOM_TEXT)
  805. pData->param.data[j].hints |= PARAMETER_USES_CUSTOM_TEXT;
  806. pData->param.ranges[j].min = min;
  807. pData->param.ranges[j].max = max;
  808. pData->param.ranges[j].def = def;
  809. pData->param.ranges[j].step = step;
  810. pData->param.ranges[j].stepSmall = stepSmall;
  811. pData->param.ranges[j].stepLarge = stepLarge;
  812. }
  813. if (needsCtrlIn)
  814. {
  815. portName.clear();
  816. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  817. {
  818. portName = pData->name;
  819. portName += ":";
  820. }
  821. portName += "events-in";
  822. portName.truncate(portNameSize);
  823. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  824. }
  825. if (needsCtrlOut)
  826. {
  827. portName.clear();
  828. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  829. {
  830. portName = pData->name;
  831. portName += ":";
  832. }
  833. portName += "events-out";
  834. portName.truncate(portNameSize);
  835. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  836. }
  837. if (forcedStereoIn || forcedStereoOut)
  838. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  839. else
  840. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  841. // plugin hints
  842. pData->hints = 0x0;
  843. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  844. pData->hints |= PLUGIN_CAN_DRYWET;
  845. if (aOuts > 0)
  846. pData->hints |= PLUGIN_CAN_VOLUME;
  847. if (aOuts >= 2 && aOuts % 2 == 0)
  848. pData->hints |= PLUGIN_CAN_BALANCE;
  849. // native plugin hints
  850. if (fDescriptor->hints & ::PLUGIN_IS_RTSAFE)
  851. pData->hints |= PLUGIN_IS_RTSAFE;
  852. if (fDescriptor->hints & ::PLUGIN_IS_SYNTH)
  853. pData->hints |= PLUGIN_IS_SYNTH;
  854. if (fDescriptor->hints & ::PLUGIN_HAS_UI)
  855. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  856. if (fDescriptor->hints & ::PLUGIN_NEEDS_SINGLE_THREAD)
  857. pData->hints |= PLUGIN_NEEDS_SINGLE_THREAD;
  858. if (fDescriptor->hints & ::PLUGIN_NEEDS_FIXED_BUFFERS)
  859. pData->hints |= PLUGIN_NEEDS_FIXED_BUFFERS;
  860. // extra plugin hints
  861. pData->extraHints = 0x0;
  862. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && mIns <= 1 && mOuts <= 1)
  863. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  864. bufferSizeChanged(pData->engine->getBufferSize());
  865. reloadPrograms(true);
  866. if (pData->active)
  867. activate();
  868. carla_debug("NativePlugin::reload() - end");
  869. }
  870. void reloadPrograms(const bool doInit) override
  871. {
  872. carla_debug("NativePlugin::reloadPrograms(%s)", bool2str(doInit));
  873. uint32_t i, oldCount = pData->midiprog.count;
  874. const int32_t current = pData->midiprog.current;
  875. // Delete old programs
  876. pData->midiprog.clear();
  877. // Query new programs
  878. uint32_t count = 0;
  879. if (fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_info != nullptr && fDescriptor->set_midi_program != nullptr)
  880. count = fDescriptor->get_midi_program_count(fHandle);
  881. if (count > 0)
  882. {
  883. pData->midiprog.createNew(count);
  884. // Update data
  885. for (i=0; i < count; ++i)
  886. {
  887. const NativeMidiProgram* const mpDesc(fDescriptor->get_midi_program_info(fHandle, i));
  888. CARLA_ASSERT(mpDesc != nullptr);
  889. CARLA_ASSERT(mpDesc->name != nullptr);
  890. pData->midiprog.data[i].bank = mpDesc->bank;
  891. pData->midiprog.data[i].program = mpDesc->program;
  892. pData->midiprog.data[i].name = carla_strdup(mpDesc->name);
  893. }
  894. }
  895. #ifndef BUILD_BRIDGE
  896. // Update OSC Names
  897. if (pData->engine->isOscControlRegistered())
  898. {
  899. pData->engine->oscSend_control_set_midi_program_count(pData->id, count);
  900. for (i=0; i < count; ++i)
  901. 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);
  902. }
  903. #endif
  904. if (doInit)
  905. {
  906. if (count > 0)
  907. setMidiProgram(0, false, false, false);
  908. }
  909. else
  910. {
  911. // Check if current program is invalid
  912. bool programChanged = false;
  913. if (count == oldCount+1)
  914. {
  915. // one midi program added, probably created by user
  916. pData->midiprog.current = static_cast<int32_t>(oldCount);
  917. programChanged = true;
  918. }
  919. else if (current < 0 && count > 0)
  920. {
  921. // programs exist now, but not before
  922. pData->midiprog.current = 0;
  923. programChanged = true;
  924. }
  925. else if (current >= 0 && count == 0)
  926. {
  927. // programs existed before, but not anymore
  928. pData->midiprog.current = -1;
  929. programChanged = true;
  930. }
  931. else if (current >= static_cast<int32_t>(count))
  932. {
  933. // current midi program > count
  934. pData->midiprog.current = 0;
  935. programChanged = true;
  936. }
  937. else
  938. {
  939. // no change
  940. pData->midiprog.current = current;
  941. }
  942. if (programChanged)
  943. setMidiProgram(pData->midiprog.current, true, true, true);
  944. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  945. }
  946. }
  947. // -------------------------------------------------------------------
  948. // Plugin processing
  949. void activate() noexcept override
  950. {
  951. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  952. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  953. if (fDescriptor->activate != nullptr)
  954. {
  955. try {
  956. fDescriptor->activate(fHandle);
  957. } catch(...) {}
  958. if (fHandle2 != nullptr)
  959. {
  960. try {
  961. fDescriptor->activate(fHandle2);
  962. } catch(...) {}
  963. }
  964. }
  965. }
  966. void deactivate() noexcept override
  967. {
  968. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  969. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  970. if (fDescriptor->deactivate != nullptr)
  971. {
  972. try {
  973. fDescriptor->deactivate(fHandle);
  974. } catch(...) {}
  975. if (fHandle2 != nullptr)
  976. {
  977. try {
  978. fDescriptor->deactivate(fHandle2);
  979. } catch(...) {}
  980. }
  981. }
  982. }
  983. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  984. {
  985. // --------------------------------------------------------------------------------------------------------
  986. // Check if active
  987. if (! pData->active)
  988. {
  989. // disable any output sound
  990. for (uint32_t 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 (uint32_t 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] = static_cast<int32_t>(k);
  1218. if (event.channel == pData->ctrlChannel)
  1219. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, static_cast<int32_t>(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, static_cast<uint16_t>(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. FLOAT_COPY(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  1374. for (i=0; i < pData->audioOut.count; ++i)
  1375. FLOAT_CLEAR(fAudioOutBuffers[i], frames);
  1376. // --------------------------------------------------------------------------------------------------------
  1377. // Run plugin
  1378. fIsProcessing = true;
  1379. if (fHandle2 == nullptr)
  1380. {
  1381. fDescriptor->process(fHandle, fAudioInBuffers, fAudioOutBuffers, frames, fMidiEvents, fMidiEventCount);
  1382. }
  1383. else
  1384. {
  1385. fDescriptor->process(fHandle,
  1386. (pData->audioIn.count > 0) ? &fAudioInBuffers[0] : nullptr,
  1387. (pData->audioOut.count > 0) ? &fAudioOutBuffers[0] : nullptr,
  1388. frames, fMidiEvents, fMidiEventCount);
  1389. fDescriptor->process(fHandle2,
  1390. (pData->audioIn.count > 0) ? &fAudioInBuffers[1] : nullptr,
  1391. (pData->audioOut.count > 0) ? &fAudioOutBuffers[1] : nullptr,
  1392. frames, fMidiEvents, fMidiEventCount);
  1393. }
  1394. fIsProcessing = false;
  1395. fTimeInfo.frame += frames;
  1396. #ifndef BUILD_BRIDGE
  1397. // --------------------------------------------------------------------------------------------------------
  1398. // Post-processing (dry/wet, volume and balance)
  1399. {
  1400. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  1401. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  1402. bool isPair;
  1403. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1404. for (i=0; i < pData->audioOut.count; ++i)
  1405. {
  1406. // Dry/Wet
  1407. if (doDryWet)
  1408. {
  1409. for (k=0; k < frames; ++k)
  1410. {
  1411. bufValue = fAudioInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  1412. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1413. }
  1414. }
  1415. // Balance
  1416. if (doBalance)
  1417. {
  1418. isPair = (i % 2 == 0);
  1419. if (isPair)
  1420. {
  1421. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1422. FLOAT_COPY(oldBufLeft, fAudioOutBuffers[i], frames);
  1423. }
  1424. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1425. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1426. for (k=0; k < frames; ++k)
  1427. {
  1428. if (isPair)
  1429. {
  1430. // left
  1431. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1432. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1433. }
  1434. else
  1435. {
  1436. // right
  1437. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1438. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1439. }
  1440. }
  1441. }
  1442. // Volume (and buffer copy)
  1443. {
  1444. for (k=0; k < frames; ++k)
  1445. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  1446. }
  1447. }
  1448. } // End of Post-processing
  1449. #else
  1450. for (i=0; i < pData->audioOut.count; ++i)
  1451. {
  1452. for (k=0; k < frames; ++k)
  1453. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1454. }
  1455. #endif
  1456. // --------------------------------------------------------------------------------------------------------
  1457. pData->singleMutex.unlock();
  1458. return true;
  1459. }
  1460. void bufferSizeChanged(const uint32_t newBufferSize) override
  1461. {
  1462. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1463. carla_debug("NativePlugin::bufferSizeChanged(%i)", newBufferSize);
  1464. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1465. {
  1466. if (fAudioInBuffers[i] != nullptr)
  1467. delete[] fAudioInBuffers[i];
  1468. fAudioInBuffers[i] = new float[newBufferSize];
  1469. }
  1470. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1471. {
  1472. if (fAudioOutBuffers[i] != nullptr)
  1473. delete[] fAudioOutBuffers[i];
  1474. fAudioOutBuffers[i] = new float[newBufferSize];
  1475. }
  1476. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1477. {
  1478. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, newBufferSize, nullptr, 0.0f);
  1479. if (fHandle2 != nullptr)
  1480. fDescriptor->dispatcher(fHandle2, PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, newBufferSize, nullptr, 0.0f);
  1481. }
  1482. }
  1483. void sampleRateChanged(const double newSampleRate) override
  1484. {
  1485. CARLA_ASSERT_INT(newSampleRate > 0.0, (int)newSampleRate);
  1486. carla_debug("NativePlugin::sampleRateChanged(%g)", newSampleRate);
  1487. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1488. {
  1489. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  1490. if (fHandle2 != nullptr)
  1491. fDescriptor->dispatcher(fHandle2, PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  1492. }
  1493. }
  1494. void offlineModeChanged(const bool isOffline) override
  1495. {
  1496. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1497. {
  1498. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  1499. if (fHandle2 != nullptr)
  1500. fDescriptor->dispatcher(fHandle2, PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  1501. }
  1502. }
  1503. // -------------------------------------------------------------------
  1504. // Plugin buffers
  1505. void initBuffers() override
  1506. {
  1507. fMidiIn.initBuffers();
  1508. fMidiOut.initBuffers();
  1509. CarlaPlugin::initBuffers();
  1510. }
  1511. void clearBuffers() override
  1512. {
  1513. carla_debug("NativePlugin::clearBuffers() - start");
  1514. if (fAudioInBuffers != nullptr)
  1515. {
  1516. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1517. {
  1518. if (fAudioInBuffers[i] != nullptr)
  1519. {
  1520. delete[] fAudioInBuffers[i];
  1521. fAudioInBuffers[i] = nullptr;
  1522. }
  1523. }
  1524. delete[] fAudioInBuffers;
  1525. fAudioInBuffers = nullptr;
  1526. }
  1527. if (fAudioOutBuffers != nullptr)
  1528. {
  1529. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1530. {
  1531. if (fAudioOutBuffers[i] != nullptr)
  1532. {
  1533. delete[] fAudioOutBuffers[i];
  1534. fAudioOutBuffers[i] = nullptr;
  1535. }
  1536. }
  1537. delete[] fAudioOutBuffers;
  1538. fAudioOutBuffers = nullptr;
  1539. }
  1540. fMidiIn.clear();
  1541. fMidiOut.clear();
  1542. CarlaPlugin::clearBuffers();
  1543. carla_debug("NativePlugin::clearBuffers() - end");
  1544. }
  1545. // -------------------------------------------------------------------
  1546. // Post-poned UI Stuff
  1547. void uiParameterChange(const uint32_t index, const float /*value*/) noexcept override
  1548. {
  1549. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1550. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1551. CARLA_ASSERT(index < pData->param.count);
  1552. if (! fIsUiVisible)
  1553. return;
  1554. if (fDescriptor == nullptr || fHandle == nullptr)
  1555. return;
  1556. if (index >= pData->param.count)
  1557. return;
  1558. //if (fDescriptor->ui_set_parameter_value != nullptr)
  1559. // fDescriptor->ui_set_parameter_value(fHandle, index, value);
  1560. }
  1561. void uiMidiProgramChange(const uint32_t index) noexcept override
  1562. {
  1563. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1564. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1565. CARLA_ASSERT(index < pData->midiprog.count);
  1566. if (! fIsUiVisible)
  1567. return;
  1568. if (fDescriptor == nullptr || fHandle == nullptr)
  1569. return;
  1570. if (index >= pData->midiprog.count)
  1571. return;
  1572. //if (fDescriptor->ui_set_midi_program != nullptr) // TODO
  1573. // fDescriptor->ui_set_midi_program(fHandle, 0, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  1574. }
  1575. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  1576. {
  1577. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1578. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1579. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1580. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1581. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  1582. if (! fIsUiVisible)
  1583. return;
  1584. if (fDescriptor == nullptr || fHandle == nullptr)
  1585. return;
  1586. if (channel >= MAX_MIDI_CHANNELS)
  1587. return;
  1588. if (note >= MAX_MIDI_NOTE)
  1589. return;
  1590. if (velo >= MAX_MIDI_VALUE)
  1591. return;
  1592. // TODO
  1593. }
  1594. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  1595. {
  1596. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1597. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1598. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1599. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1600. if (! fIsUiVisible)
  1601. return;
  1602. if (fDescriptor == nullptr || fHandle == nullptr)
  1603. return;
  1604. if (channel >= MAX_MIDI_CHANNELS)
  1605. return;
  1606. if (note >= MAX_MIDI_NOTE)
  1607. return;
  1608. // TODO
  1609. }
  1610. // -------------------------------------------------------------------
  1611. protected:
  1612. uint32_t handleGetBufferSize() const
  1613. {
  1614. return pData->engine->getBufferSize();
  1615. }
  1616. double handleGetSampleRate() const
  1617. {
  1618. return pData->engine->getSampleRate();
  1619. }
  1620. bool handleIsOffline() const
  1621. {
  1622. return pData->engine->isOffline();
  1623. }
  1624. const NativeTimeInfo* handleGetTimeInfo() const
  1625. {
  1626. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, nullptr);
  1627. return &fTimeInfo;
  1628. }
  1629. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  1630. {
  1631. CARLA_ASSERT(pData->enabled);
  1632. CARLA_ASSERT(fIsProcessing);
  1633. CARLA_ASSERT(fMidiOut.count > 0 || pData->event.portOut != nullptr);
  1634. CARLA_ASSERT(event != nullptr);
  1635. CARLA_ASSERT(event->data[0] != 0);
  1636. if (! pData->enabled)
  1637. return false;
  1638. if (fMidiOut.count == 0)
  1639. return false;
  1640. if (event == nullptr)
  1641. return false;
  1642. if (event->data[0] == 0)
  1643. return false;
  1644. if (! fIsProcessing)
  1645. {
  1646. carla_stderr2("NativePlugin::handleWriteMidiEvent(%p) - received MIDI out event outside audio thread, ignoring", event);
  1647. return false;
  1648. }
  1649. // reverse-find first free event, and put it there
  1650. for (uint32_t i=(kPluginMaxMidiEvents*2)-1; i > fMidiEventCount; --i)
  1651. {
  1652. if (fMidiEvents[i].data[0] == 0)
  1653. {
  1654. std::memcpy(&fMidiEvents[i], event, sizeof(NativeMidiEvent));
  1655. return true;
  1656. }
  1657. }
  1658. return false;
  1659. }
  1660. void handleUiParameterChanged(const uint32_t index, const float value)
  1661. {
  1662. setParameterValue(index, value, false, true, true);
  1663. }
  1664. void handleUiCustomDataChanged(const char* const key, const char* const value)
  1665. {
  1666. setCustomData(CUSTOM_DATA_TYPE_STRING, key, value, false);
  1667. }
  1668. void handleUiClosed()
  1669. {
  1670. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1671. fIsUiVisible = false;
  1672. }
  1673. const char* handleUiOpenFile(const bool isDir, const char* const title, const char* const filter)
  1674. {
  1675. return carla_standalone_file_callback(FILE_CALLBACK_OPEN, isDir, title, filter);
  1676. }
  1677. const char* handleUiSaveFile(const bool isDir, const char* const title, const char* const filter)
  1678. {
  1679. return carla_standalone_file_callback(FILE_CALLBACK_SAVE, isDir, title, filter);
  1680. }
  1681. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  1682. {
  1683. carla_debug("NativePlugin::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)", opcode, index, value, ptr, opt);
  1684. intptr_t ret = 0;
  1685. switch (opcode)
  1686. {
  1687. case ::HOST_OPCODE_NULL:
  1688. break;
  1689. #ifdef BUILD_BRIDGE
  1690. case ::HOST_OPCODE_SET_VOLUME:
  1691. case ::HOST_OPCODE_SET_DRYWET:
  1692. case ::HOST_OPCODE_SET_BALANCE_LEFT:
  1693. case ::HOST_OPCODE_SET_BALANCE_RIGHT:
  1694. case ::HOST_OPCODE_SET_PANNING:
  1695. break;
  1696. #else
  1697. case ::HOST_OPCODE_SET_VOLUME:
  1698. setVolume(opt, true, true);
  1699. break;
  1700. case ::HOST_OPCODE_SET_DRYWET:
  1701. setDryWet(opt, true, true);
  1702. break;
  1703. case ::HOST_OPCODE_SET_BALANCE_LEFT:
  1704. setBalanceLeft(opt, true, true);
  1705. break;
  1706. case ::HOST_OPCODE_SET_BALANCE_RIGHT:
  1707. setBalanceRight(opt, true, true);
  1708. break;
  1709. case ::HOST_OPCODE_SET_PANNING:
  1710. setPanning(opt, true, true);
  1711. break;
  1712. #endif
  1713. case HOST_OPCODE_GET_PARAMETER_MIDI_CC:
  1714. case HOST_OPCODE_SET_PARAMETER_MIDI_CC:
  1715. // TODO
  1716. break;
  1717. case ::HOST_OPCODE_SET_PROCESS_PRECISION:
  1718. // TODO
  1719. break;
  1720. case ::HOST_OPCODE_UPDATE_PARAMETER:
  1721. // TODO
  1722. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, -1, 0, 0.0f, nullptr);
  1723. break;
  1724. case ::HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  1725. // TODO
  1726. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, -1, 0, 0.0f, nullptr);
  1727. break;
  1728. case ::HOST_OPCODE_RELOAD_PARAMETERS:
  1729. reload(); // FIXME
  1730. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PARAMETERS, pData->id, -1, 0, 0.0f, nullptr);
  1731. break;
  1732. case ::HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  1733. reloadPrograms(false);
  1734. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, -1, 0, 0.0f, nullptr);
  1735. break;
  1736. case ::HOST_OPCODE_RELOAD_ALL:
  1737. reload();
  1738. pData->engine->callback(ENGINE_CALLBACK_RELOAD_ALL, pData->id, -1, 0, 0.0f, nullptr);
  1739. break;
  1740. case HOST_OPCODE_UI_UNAVAILABLE:
  1741. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, nullptr);
  1742. break;
  1743. }
  1744. return ret;
  1745. // unused for now
  1746. (void)index;
  1747. (void)value;
  1748. (void)ptr;
  1749. }
  1750. // -------------------------------------------------------------------
  1751. public:
  1752. static size_t getPluginCount() noexcept
  1753. {
  1754. return sPluginDescriptors.count();
  1755. }
  1756. static const NativePluginDescriptor* getPluginDescriptor(const size_t index) noexcept
  1757. {
  1758. CARLA_SAFE_ASSERT_RETURN(index < sPluginDescriptors.count(), nullptr);
  1759. return sPluginDescriptors.getAt(index);
  1760. }
  1761. static void registerPlugin(const NativePluginDescriptor* desc)
  1762. {
  1763. sPluginDescriptors.append(desc);
  1764. }
  1765. // -------------------------------------------------------------------
  1766. bool init(const char* const name, const char* const label)
  1767. {
  1768. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1769. // ---------------------------------------------------------------
  1770. // first checks
  1771. if (pData->client != nullptr)
  1772. {
  1773. pData->engine->setLastError("Plugin client is already registered");
  1774. return false;
  1775. }
  1776. if (label == nullptr && label[0] != '\0')
  1777. {
  1778. pData->engine->setLastError("null label");
  1779. return false;
  1780. }
  1781. // ---------------------------------------------------------------
  1782. // get descriptor that matches label
  1783. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = sPluginDescriptors.begin(); it.valid(); it.next())
  1784. {
  1785. fDescriptor = it.getValue();
  1786. CARLA_SAFE_ASSERT_BREAK(fDescriptor != nullptr);
  1787. carla_debug("Check vs \"%s\"", fDescriptor->label);
  1788. if (fDescriptor->label != nullptr && std::strcmp(fDescriptor->label, label) == 0)
  1789. break;
  1790. fDescriptor = nullptr;
  1791. }
  1792. if (fDescriptor == nullptr)
  1793. {
  1794. pData->engine->setLastError("Invalid internal plugin");
  1795. return false;
  1796. }
  1797. // ---------------------------------------------------------------
  1798. // set icon
  1799. if (std::strcmp(fDescriptor->label, "audiofile") == 0)
  1800. pData->iconName = carla_strdup("file");
  1801. else if (std::strcmp(fDescriptor->label, "midifile") == 0)
  1802. pData->iconName = carla_strdup("file");
  1803. else if (std::strcmp(fDescriptor->label, "sunvoxfile") == 0)
  1804. pData->iconName = carla_strdup("file");
  1805. else if (std::strcmp(fDescriptor->label, "3BandEQ") == 0)
  1806. pData->iconName = carla_strdup("distrho");
  1807. else if (std::strcmp(fDescriptor->label, "3BandSplitter") == 0)
  1808. pData->iconName = carla_strdup("distrho");
  1809. else if (std::strcmp(fDescriptor->label, "Nekobi") == 0)
  1810. pData->iconName = carla_strdup("distrho");
  1811. else if (std::strcmp(fDescriptor->label, "Notes") == 0)
  1812. pData->iconName = carla_strdup("distrho");
  1813. else if (std::strcmp(fDescriptor->label, "PingPongPan") == 0)
  1814. pData->iconName = carla_strdup("distrho");
  1815. else if (std::strcmp(fDescriptor->label, "StereoEnhancer") == 0)
  1816. pData->iconName = carla_strdup("distrho");
  1817. // ---------------------------------------------------------------
  1818. // get info
  1819. if (name != nullptr && name[0] != '\0')
  1820. pData->name = pData->engine->getUniquePluginName(name);
  1821. else if (fDescriptor->name != nullptr && fDescriptor->name[0] != '\0')
  1822. pData->name = pData->engine->getUniquePluginName(fDescriptor->name);
  1823. else
  1824. pData->name = pData->engine->getUniquePluginName(label);
  1825. {
  1826. CARLA_ASSERT(fHost.uiName == nullptr);
  1827. char uiName[std::strlen(pData->name)+6+1];
  1828. std::strcpy(uiName, pData->name);
  1829. std::strcat(uiName, " (GUI)");
  1830. fHost.uiName = carla_strdup(uiName);
  1831. }
  1832. // ---------------------------------------------------------------
  1833. // register client
  1834. pData->client = pData->engine->addClient(this);
  1835. if (pData->client == nullptr || ! pData->client->isOk())
  1836. {
  1837. pData->engine->setLastError("Failed to register plugin client");
  1838. return false;
  1839. }
  1840. // ---------------------------------------------------------------
  1841. // initialize plugin
  1842. fHandle = fDescriptor->instantiate(&fHost);
  1843. if (fHandle == nullptr)
  1844. {
  1845. pData->engine->setLastError("Plugin failed to initialize");
  1846. return false;
  1847. }
  1848. // ---------------------------------------------------------------
  1849. // load plugin settings
  1850. {
  1851. const bool hasMidiProgs(fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_count(fHandle) > 0);
  1852. // set default options
  1853. pData->options = 0x0;
  1854. if (hasMidiProgs && (fDescriptor->supports & ::PLUGIN_SUPPORTS_PROGRAM_CHANGES) == 0)
  1855. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1856. if (getMidiInCount() > 0 || (fDescriptor->hints & ::PLUGIN_NEEDS_FIXED_BUFFERS) != 0)
  1857. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1858. if (pData->engine->getOptions().forceStereo)
  1859. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1860. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_CHANNEL_PRESSURE)
  1861. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1862. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_NOTE_AFTERTOUCH)
  1863. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1864. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_PITCHBEND)
  1865. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1866. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_ALL_SOUND_OFF)
  1867. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1868. // set identifier string
  1869. CarlaString identifier("Native/");
  1870. identifier += label;
  1871. pData->identifier = identifier.dup();
  1872. // load settings
  1873. pData->options = pData->loadSettings(pData->options, getOptionsAvailable());
  1874. // ignore settings, we need this anyway
  1875. if (getMidiInCount() > 0 || (fDescriptor->hints & ::PLUGIN_NEEDS_FIXED_BUFFERS) != 0)
  1876. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1877. }
  1878. return true;
  1879. }
  1880. class ScopedInitializer
  1881. {
  1882. public:
  1883. ScopedInitializer()
  1884. {
  1885. carla_register_all_plugins();
  1886. }
  1887. ~ScopedInitializer()
  1888. {
  1889. sPluginDescriptors.clear();
  1890. }
  1891. };
  1892. private:
  1893. NativePluginHandle fHandle;
  1894. NativePluginHandle fHandle2;
  1895. NativeHostDescriptor fHost;
  1896. const NativePluginDescriptor* fDescriptor;
  1897. bool fIsProcessing;
  1898. bool fIsUiVisible;
  1899. float** fAudioInBuffers;
  1900. float** fAudioOutBuffers;
  1901. uint32_t fMidiEventCount;
  1902. NativeMidiEvent fMidiEvents[kPluginMaxMidiEvents*2];
  1903. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  1904. NativePluginMidiData fMidiIn;
  1905. NativePluginMidiData fMidiOut;
  1906. NativeTimeInfo fTimeInfo;
  1907. static LinkedList<const NativePluginDescriptor*> sPluginDescriptors;
  1908. // -------------------------------------------------------------------
  1909. #define handlePtr ((NativePlugin*)handle)
  1910. static uint32_t carla_host_get_buffer_size(NativeHostHandle handle)
  1911. {
  1912. return handlePtr->handleGetBufferSize();
  1913. }
  1914. static double carla_host_get_sample_rate(NativeHostHandle handle)
  1915. {
  1916. return handlePtr->handleGetSampleRate();
  1917. }
  1918. static bool carla_host_is_offline(NativeHostHandle handle)
  1919. {
  1920. return handlePtr->handleIsOffline();
  1921. }
  1922. static const NativeTimeInfo* carla_host_get_time_info(NativeHostHandle handle)
  1923. {
  1924. return handlePtr->handleGetTimeInfo();
  1925. }
  1926. static bool carla_host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  1927. {
  1928. return handlePtr->handleWriteMidiEvent(event);
  1929. }
  1930. static void carla_host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  1931. {
  1932. handlePtr->handleUiParameterChanged(index, value);
  1933. }
  1934. static void carla_host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  1935. {
  1936. handlePtr->handleUiCustomDataChanged(key, value);
  1937. }
  1938. static void carla_host_ui_closed(NativeHostHandle handle)
  1939. {
  1940. handlePtr->handleUiClosed();
  1941. }
  1942. static const char* carla_host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  1943. {
  1944. return handlePtr->handleUiOpenFile(isDir, title, filter);
  1945. }
  1946. static const char* carla_host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  1947. {
  1948. return handlePtr->handleUiSaveFile(isDir, title, filter);
  1949. }
  1950. static intptr_t carla_host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  1951. {
  1952. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  1953. }
  1954. #undef handlePtr
  1955. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  1956. };
  1957. LinkedList<const NativePluginDescriptor*> NativePlugin::sPluginDescriptors;
  1958. static const NativePlugin::ScopedInitializer _si;
  1959. CARLA_BACKEND_END_NAMESPACE
  1960. void carla_register_native_plugin(const NativePluginDescriptor* desc)
  1961. {
  1962. CARLA_BACKEND_USE_NAMESPACE
  1963. NativePlugin::registerPlugin(desc);
  1964. }
  1965. #endif // WANT_NATIVE
  1966. // -----------------------------------------------------------------------
  1967. CARLA_BACKEND_START_NAMESPACE
  1968. #ifdef WANT_NATIVE
  1969. size_t CarlaPlugin::getNativePluginCount() noexcept
  1970. {
  1971. return NativePlugin::getPluginCount();
  1972. }
  1973. const NativePluginDescriptor* CarlaPlugin::getNativePluginDescriptor(const size_t index) noexcept
  1974. {
  1975. return NativePlugin::getPluginDescriptor(index);
  1976. }
  1977. #endif
  1978. // -----------------------------------------------------------------------
  1979. CarlaPlugin* CarlaPlugin::newNative(const Initializer& init)
  1980. {
  1981. carla_debug("CarlaPlugin::newNative({%p, \"%s\", \"%s\", \"%s\"})", init.engine, init.filename, init.name, init.label);
  1982. #ifdef WANT_NATIVE
  1983. NativePlugin* const plugin(new NativePlugin(init.engine, init.id));
  1984. if (! plugin->init(init.name, init.label))
  1985. {
  1986. delete plugin;
  1987. return nullptr;
  1988. }
  1989. plugin->reload();
  1990. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  1991. {
  1992. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo Internal plugins, sorry!");
  1993. delete plugin;
  1994. return nullptr;
  1995. }
  1996. return plugin;
  1997. #else
  1998. init.engine->setLastError("Internal plugins support not available");
  1999. return nullptr;
  2000. #endif
  2001. }
  2002. CARLA_BACKEND_END_NAMESPACE
  2003. // -----------------------------------------------------------------------