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.

2501 lines
88KB

  1. /*
  2. * Carla Native Plugin
  3. * Copyright (C) 2012-2013 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaPluginInternal.hpp"
  18. #include "CarlaEngine.hpp"
  19. #ifdef WANT_NATIVE
  20. #include "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. 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 uint index(midiProg.toUInt(&ok));
  460. if (ok && index < 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 uint32_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 = j;
  732. pData->param.data[j].rindex = j;
  733. pData->param.data[j].midiCC = -1;
  734. pData->param.data[j].midiChannel = 0;
  735. float min, max, def, step, stepSmall, stepLarge;
  736. // min value
  737. min = paramInfo->ranges.min;
  738. // max value
  739. max = paramInfo->ranges.max;
  740. if (min > max)
  741. max = min;
  742. else if (max < min)
  743. min = max;
  744. if (max - min == 0.0f)
  745. {
  746. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", paramInfo->name);
  747. max = min + 0.1f;
  748. }
  749. // default value
  750. def = paramInfo->ranges.def;
  751. if (def < min)
  752. def = min;
  753. else if (def > max)
  754. def = max;
  755. if (paramInfo->hints & ::PARAMETER_USES_SAMPLE_RATE)
  756. {
  757. min *= sampleRate;
  758. max *= sampleRate;
  759. def *= sampleRate;
  760. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  761. }
  762. if (paramInfo->hints & ::PARAMETER_IS_BOOLEAN)
  763. {
  764. step = max - min;
  765. stepSmall = step;
  766. stepLarge = step;
  767. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  768. }
  769. else if (paramInfo->hints & ::PARAMETER_IS_INTEGER)
  770. {
  771. step = 1.0f;
  772. stepSmall = 1.0f;
  773. stepLarge = 10.0f;
  774. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  775. }
  776. else
  777. {
  778. float range = max - min;
  779. step = range/100.0f;
  780. stepSmall = range/1000.0f;
  781. stepLarge = range/10.0f;
  782. }
  783. if (paramInfo->hints & ::PARAMETER_IS_OUTPUT)
  784. {
  785. pData->param.data[j].type = PARAMETER_OUTPUT;
  786. needsCtrlOut = true;
  787. }
  788. else
  789. {
  790. pData->param.data[j].type = PARAMETER_INPUT;
  791. needsCtrlIn = true;
  792. }
  793. // extra parameter hints
  794. if (paramInfo->hints & ::PARAMETER_IS_ENABLED)
  795. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  796. if (paramInfo->hints & ::PARAMETER_IS_AUTOMABLE)
  797. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  798. if (paramInfo->hints & ::PARAMETER_IS_LOGARITHMIC)
  799. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  800. if (paramInfo->hints & ::PARAMETER_USES_SCALEPOINTS)
  801. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  802. if (paramInfo->hints & ::PARAMETER_USES_CUSTOM_TEXT)
  803. pData->param.data[j].hints |= PARAMETER_USES_CUSTOM_TEXT;
  804. pData->param.ranges[j].min = min;
  805. pData->param.ranges[j].max = max;
  806. pData->param.ranges[j].def = def;
  807. pData->param.ranges[j].step = step;
  808. pData->param.ranges[j].stepSmall = stepSmall;
  809. pData->param.ranges[j].stepLarge = stepLarge;
  810. }
  811. if (needsCtrlIn)
  812. {
  813. portName.clear();
  814. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  815. {
  816. portName = pData->name;
  817. portName += ":";
  818. }
  819. portName += "events-in";
  820. portName.truncate(portNameSize);
  821. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  822. }
  823. if (needsCtrlOut)
  824. {
  825. portName.clear();
  826. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  827. {
  828. portName = pData->name;
  829. portName += ":";
  830. }
  831. portName += "events-out";
  832. portName.truncate(portNameSize);
  833. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  834. }
  835. if (forcedStereoIn || forcedStereoOut)
  836. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  837. else
  838. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  839. // plugin hints
  840. pData->hints = 0x0;
  841. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  842. pData->hints |= PLUGIN_CAN_DRYWET;
  843. if (aOuts > 0)
  844. pData->hints |= PLUGIN_CAN_VOLUME;
  845. if (aOuts >= 2 && aOuts % 2 == 0)
  846. pData->hints |= PLUGIN_CAN_BALANCE;
  847. // native plugin hints
  848. if (fDescriptor->hints & ::PLUGIN_IS_RTSAFE)
  849. pData->hints |= PLUGIN_IS_RTSAFE;
  850. if (fDescriptor->hints & ::PLUGIN_IS_SYNTH)
  851. pData->hints |= PLUGIN_IS_SYNTH;
  852. if (fDescriptor->hints & ::PLUGIN_HAS_UI)
  853. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  854. if (fDescriptor->hints & ::PLUGIN_NEEDS_SINGLE_THREAD)
  855. pData->hints |= PLUGIN_NEEDS_SINGLE_THREAD;
  856. if (fDescriptor->hints & ::PLUGIN_NEEDS_FIXED_BUFFERS)
  857. pData->hints |= PLUGIN_NEEDS_FIXED_BUFFERS;
  858. // extra plugin hints
  859. pData->extraHints = 0x0;
  860. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && mIns <= 1 && mOuts <= 1)
  861. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  862. bufferSizeChanged(pData->engine->getBufferSize());
  863. reloadPrograms(true);
  864. if (pData->active)
  865. activate();
  866. carla_debug("NativePlugin::reload() - end");
  867. }
  868. void reloadPrograms(const bool doInit) override
  869. {
  870. carla_debug("NativePlugin::reloadPrograms(%s)", bool2str(doInit));
  871. uint32_t i, oldCount = pData->midiprog.count;
  872. const int32_t current = pData->midiprog.current;
  873. // Delete old programs
  874. pData->midiprog.clear();
  875. // Query new programs
  876. uint32_t count = 0;
  877. if (fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_info != nullptr && fDescriptor->set_midi_program != nullptr)
  878. count = fDescriptor->get_midi_program_count(fHandle);
  879. if (count > 0)
  880. {
  881. pData->midiprog.createNew(count);
  882. // Update data
  883. for (i=0; i < count; ++i)
  884. {
  885. const NativeMidiProgram* const mpDesc(fDescriptor->get_midi_program_info(fHandle, i));
  886. CARLA_ASSERT(mpDesc != nullptr);
  887. CARLA_ASSERT(mpDesc->name != nullptr);
  888. pData->midiprog.data[i].bank = mpDesc->bank;
  889. pData->midiprog.data[i].program = mpDesc->program;
  890. pData->midiprog.data[i].name = carla_strdup(mpDesc->name);
  891. }
  892. }
  893. #ifndef BUILD_BRIDGE
  894. // Update OSC Names
  895. if (pData->engine->isOscControlRegistered())
  896. {
  897. pData->engine->oscSend_control_set_midi_program_count(pData->id, count);
  898. for (i=0; i < count; ++i)
  899. 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);
  900. }
  901. #endif
  902. if (doInit)
  903. {
  904. if (count > 0)
  905. setMidiProgram(0, false, false, false);
  906. }
  907. else
  908. {
  909. // Check if current program is invalid
  910. bool programChanged = false;
  911. if (count == oldCount+1)
  912. {
  913. // one midi program added, probably created by user
  914. pData->midiprog.current = oldCount;
  915. programChanged = true;
  916. }
  917. else if (current < 0 && count > 0)
  918. {
  919. // programs exist now, but not before
  920. pData->midiprog.current = 0;
  921. programChanged = true;
  922. }
  923. else if (current >= 0 && count == 0)
  924. {
  925. // programs existed before, but not anymore
  926. pData->midiprog.current = -1;
  927. programChanged = true;
  928. }
  929. else if (current >= static_cast<int32_t>(count))
  930. {
  931. // current midi program > count
  932. pData->midiprog.current = 0;
  933. programChanged = true;
  934. }
  935. else
  936. {
  937. // no change
  938. pData->midiprog.current = current;
  939. }
  940. if (programChanged)
  941. setMidiProgram(pData->midiprog.current, true, true, true);
  942. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  943. }
  944. }
  945. // -------------------------------------------------------------------
  946. // Plugin processing
  947. void activate() noexcept override
  948. {
  949. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  950. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  951. if (fDescriptor->activate != nullptr)
  952. {
  953. try {
  954. fDescriptor->activate(fHandle);
  955. } catch(...) {}
  956. if (fHandle2 != nullptr)
  957. {
  958. try {
  959. fDescriptor->activate(fHandle2);
  960. } catch(...) {}
  961. }
  962. }
  963. }
  964. void deactivate() noexcept override
  965. {
  966. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  967. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  968. if (fDescriptor->deactivate != nullptr)
  969. {
  970. try {
  971. fDescriptor->deactivate(fHandle);
  972. } catch(...) {}
  973. if (fHandle2 != nullptr)
  974. {
  975. try {
  976. fDescriptor->deactivate(fHandle2);
  977. } catch(...) {}
  978. }
  979. }
  980. }
  981. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  982. {
  983. // --------------------------------------------------------------------------------------------------------
  984. // Check if active
  985. if (! pData->active)
  986. {
  987. // disable any output sound
  988. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  989. FLOAT_CLEAR(outBuffer[i], frames);
  990. return;
  991. }
  992. fMidiEventCount = 0;
  993. carla_zeroStruct<NativeMidiEvent>(fMidiEvents, kPluginMaxMidiEvents*2);
  994. // --------------------------------------------------------------------------------------------------------
  995. // Check if needs reset
  996. if (pData->needsReset)
  997. {
  998. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  999. {
  1000. for (uint8_t k=0, i=MAX_MIDI_CHANNELS; k < MAX_MIDI_CHANNELS; ++k)
  1001. {
  1002. fMidiEvents[k].data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + k);
  1003. fMidiEvents[k].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1004. fMidiEvents[k].data[2] = 0;
  1005. fMidiEvents[k].size = 3;
  1006. fMidiEvents[k+i].data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + k);
  1007. fMidiEvents[k+i].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1008. fMidiEvents[k+i].data[2] = 0;
  1009. fMidiEvents[k+i].size = 3;
  1010. }
  1011. fMidiEventCount = MAX_MIDI_CHANNELS*2;
  1012. }
  1013. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  1014. {
  1015. for (uint8_t k=0; k < MAX_MIDI_NOTE; ++k)
  1016. {
  1017. fMidiEvents[k].data[0] = static_cast<uint8_t>(MIDI_STATUS_NOTE_OFF + pData->ctrlChannel);
  1018. fMidiEvents[k].data[1] = k;
  1019. fMidiEvents[k].data[2] = 0;
  1020. fMidiEvents[k].size = 3;
  1021. }
  1022. fMidiEventCount = MAX_MIDI_NOTE;
  1023. }
  1024. pData->needsReset = false;
  1025. }
  1026. CARLA_PROCESS_CONTINUE_CHECK;
  1027. // --------------------------------------------------------------------------------------------------------
  1028. // Set TimeInfo
  1029. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  1030. fTimeInfo.playing = timeInfo.playing;
  1031. fTimeInfo.frame = timeInfo.frame;
  1032. fTimeInfo.usecs = timeInfo.usecs;
  1033. if (timeInfo.valid & EngineTimeInfo::kValidBBT)
  1034. {
  1035. fTimeInfo.bbt.valid = true;
  1036. fTimeInfo.bbt.bar = timeInfo.bbt.bar;
  1037. fTimeInfo.bbt.beat = timeInfo.bbt.beat;
  1038. fTimeInfo.bbt.tick = timeInfo.bbt.tick;
  1039. fTimeInfo.bbt.barStartTick = timeInfo.bbt.barStartTick;
  1040. fTimeInfo.bbt.beatsPerBar = timeInfo.bbt.beatsPerBar;
  1041. fTimeInfo.bbt.beatType = timeInfo.bbt.beatType;
  1042. fTimeInfo.bbt.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
  1043. fTimeInfo.bbt.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
  1044. }
  1045. else
  1046. fTimeInfo.bbt.valid = false;
  1047. CARLA_PROCESS_CONTINUE_CHECK;
  1048. // --------------------------------------------------------------------------------------------------------
  1049. // Event Input and Processing
  1050. if (pData->event.portIn != nullptr)
  1051. {
  1052. // ----------------------------------------------------------------------------------------------------
  1053. // MIDI Input (External)
  1054. if (pData->extNotes.mutex.tryLock())
  1055. {
  1056. while (fMidiEventCount < kPluginMaxMidiEvents*2 && ! pData->extNotes.data.isEmpty())
  1057. {
  1058. const ExternalMidiNote& note(pData->extNotes.data.getFirst(true));
  1059. CARLA_ASSERT(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  1060. fMidiEvents[fMidiEventCount].data[0] = note.channel + (note.velo > 0) ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF;
  1061. fMidiEvents[fMidiEventCount].data[1] = note.note;
  1062. fMidiEvents[fMidiEventCount].data[2] = note.velo;
  1063. fMidiEvents[fMidiEventCount].size = 3;
  1064. fMidiEventCount += 1;
  1065. }
  1066. pData->extNotes.mutex.unlock();
  1067. } // End of MIDI Input (External)
  1068. // ----------------------------------------------------------------------------------------------------
  1069. // Event Input (System)
  1070. bool allNotesOffSent = false;
  1071. bool sampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  1072. uint32_t time, nEvents = pData->event.portIn->getEventCount();
  1073. uint32_t startTime = 0;
  1074. uint32_t timeOffset = 0;
  1075. uint32_t nextBankId = 0;
  1076. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1077. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1078. for (uint32_t i=0; i < nEvents; ++i)
  1079. {
  1080. const EngineEvent& event(pData->event.portIn->getEvent(i));
  1081. time = event.time;
  1082. if (time >= frames)
  1083. continue;
  1084. CARLA_ASSERT_INT2(time >= timeOffset, time, timeOffset);
  1085. if (time > timeOffset && sampleAccurate)
  1086. {
  1087. if (processSingle(inBuffer, outBuffer, time - timeOffset, timeOffset))
  1088. {
  1089. startTime = 0;
  1090. timeOffset = time;
  1091. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1092. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1093. else
  1094. nextBankId = 0;
  1095. if (fMidiEventCount > 0)
  1096. {
  1097. carla_zeroStruct<NativeMidiEvent>(fMidiEvents, fMidiEventCount);
  1098. fMidiEventCount = 0;
  1099. }
  1100. }
  1101. else
  1102. startTime += timeOffset;
  1103. }
  1104. // Control change
  1105. switch (event.type)
  1106. {
  1107. case kEngineEventTypeNull:
  1108. break;
  1109. case kEngineEventTypeControl:
  1110. {
  1111. const EngineControlEvent& ctrlEvent = event.ctrl;
  1112. switch (ctrlEvent.type)
  1113. {
  1114. case kEngineControlEventTypeNull:
  1115. break;
  1116. case kEngineControlEventTypeParameter:
  1117. {
  1118. #ifndef BUILD_BRIDGE
  1119. // Control backend stuff
  1120. if (event.channel == pData->ctrlChannel)
  1121. {
  1122. float value;
  1123. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) > 0)
  1124. {
  1125. value = ctrlEvent.value;
  1126. setDryWet(value, false, false);
  1127. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  1128. }
  1129. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) > 0)
  1130. {
  1131. value = ctrlEvent.value*127.0f/100.0f;
  1132. setVolume(value, false, false);
  1133. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  1134. }
  1135. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) > 0)
  1136. {
  1137. float left, right;
  1138. value = ctrlEvent.value/0.5f - 1.0f;
  1139. if (value < 0.0f)
  1140. {
  1141. left = -1.0f;
  1142. right = (value*2.0f)+1.0f;
  1143. }
  1144. else if (value > 0.0f)
  1145. {
  1146. left = (value*2.0f)-1.0f;
  1147. right = 1.0f;
  1148. }
  1149. else
  1150. {
  1151. left = -1.0f;
  1152. right = 1.0f;
  1153. }
  1154. setBalanceLeft(left, false, false);
  1155. setBalanceRight(right, false, false);
  1156. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  1157. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  1158. }
  1159. }
  1160. #endif
  1161. // Control plugin parameters
  1162. for (uint32_t k=0; k < pData->param.count; ++k)
  1163. {
  1164. if (pData->param.data[k].midiChannel != event.channel)
  1165. continue;
  1166. if (pData->param.data[k].midiCC != ctrlEvent.param)
  1167. continue;
  1168. if (pData->param.data[k].type != PARAMETER_INPUT)
  1169. continue;
  1170. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1171. continue;
  1172. float value;
  1173. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1174. {
  1175. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  1176. }
  1177. else
  1178. {
  1179. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  1180. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1181. value = std::rint(value);
  1182. }
  1183. setParameterValue(k, value, false, false, false);
  1184. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  1185. }
  1186. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  1187. {
  1188. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1189. continue;
  1190. fMidiEvents[fMidiEventCount].port = 0;
  1191. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1192. fMidiEvents[fMidiEventCount].data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + event.channel);
  1193. fMidiEvents[fMidiEventCount].data[1] = static_cast<uint8_t>(ctrlEvent.param);
  1194. fMidiEvents[fMidiEventCount].data[2] = uint8_t(ctrlEvent.value*127.0f);
  1195. fMidiEvents[fMidiEventCount].size = 3;
  1196. fMidiEventCount += 1;
  1197. }
  1198. break;
  1199. }
  1200. case kEngineControlEventTypeMidiBank:
  1201. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1202. nextBankId = ctrlEvent.param;
  1203. break;
  1204. case kEngineControlEventTypeMidiProgram:
  1205. if (event.channel < MAX_MIDI_CHANNELS && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1206. {
  1207. const uint32_t nextProgramId(ctrlEvent.param);
  1208. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  1209. {
  1210. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  1211. {
  1212. fDescriptor->set_midi_program(fHandle, event.channel, nextBankId, nextProgramId);
  1213. if (fHandle2 != nullptr)
  1214. fDescriptor->set_midi_program(fHandle2, event.channel, nextBankId, nextProgramId);
  1215. fCurMidiProgs[event.channel] = k;
  1216. if (event.channel == pData->ctrlChannel)
  1217. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, k, 0, 0.0f);
  1218. break;
  1219. }
  1220. }
  1221. }
  1222. break;
  1223. case kEngineControlEventTypeAllSoundOff:
  1224. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1225. {
  1226. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1227. continue;
  1228. fMidiEvents[fMidiEventCount].port = 0;
  1229. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1230. fMidiEvents[fMidiEventCount].data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + event.channel);
  1231. fMidiEvents[fMidiEventCount].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1232. fMidiEvents[fMidiEventCount].data[2] = 0;
  1233. fMidiEvents[fMidiEventCount].size = 3;
  1234. fMidiEventCount += 1;
  1235. }
  1236. break;
  1237. case kEngineControlEventTypeAllNotesOff:
  1238. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1239. {
  1240. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1241. {
  1242. allNotesOffSent = true;
  1243. sendMidiAllNotesOffToCallback();
  1244. }
  1245. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1246. continue;
  1247. fMidiEvents[fMidiEventCount].port = 0;
  1248. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1249. fMidiEvents[fMidiEventCount].data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + event.channel);
  1250. fMidiEvents[fMidiEventCount].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1251. fMidiEvents[fMidiEventCount].data[2] = 0;
  1252. fMidiEvents[fMidiEventCount].size = 3;
  1253. fMidiEventCount += 1;
  1254. }
  1255. break;
  1256. }
  1257. break;
  1258. }
  1259. case kEngineEventTypeMidi:
  1260. {
  1261. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1262. continue;
  1263. const EngineMidiEvent& midiEvent(event.midi);
  1264. uint8_t status = uint8_t(MIDI_GET_STATUS_FROM_DATA(midiEvent.data));
  1265. uint8_t channel = event.channel;
  1266. if (MIDI_IS_STATUS_CHANNEL_PRESSURE(status) && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1267. continue;
  1268. if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1269. continue;
  1270. if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1271. continue;
  1272. if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1273. continue;
  1274. // Fix bad note-off
  1275. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1276. status = MIDI_STATUS_NOTE_OFF;
  1277. fMidiEvents[fMidiEventCount].port = 0;
  1278. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1279. fMidiEvents[fMidiEventCount].size = midiEvent.size;
  1280. fMidiEvents[fMidiEventCount].data[0] = static_cast<uint8_t>(status + channel);
  1281. fMidiEvents[fMidiEventCount].data[1] = midiEvent.data[1];
  1282. fMidiEvents[fMidiEventCount].data[2] = midiEvent.data[2];
  1283. fMidiEvents[fMidiEventCount].data[3] = midiEvent.data[3];
  1284. fMidiEventCount += 1;
  1285. if (status == MIDI_STATUS_NOTE_ON)
  1286. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  1287. else if (status == MIDI_STATUS_NOTE_OFF)
  1288. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  1289. break;
  1290. }
  1291. }
  1292. }
  1293. pData->postRtEvents.trySplice();
  1294. if (frames > timeOffset)
  1295. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  1296. } // End of Event Input and Processing
  1297. // --------------------------------------------------------------------------------------------------------
  1298. // Plugin processing (no events)
  1299. else
  1300. {
  1301. processSingle(inBuffer, outBuffer, frames, 0);
  1302. } // End of Plugin processing (no events)
  1303. CARLA_PROCESS_CONTINUE_CHECK;
  1304. // --------------------------------------------------------------------------------------------------------
  1305. // Control and MIDI Output
  1306. if (fMidiOut.count > 0 || pData->event.portOut != nullptr)
  1307. {
  1308. float value, curValue;
  1309. for (uint32_t k=0; k < pData->param.count; ++k)
  1310. {
  1311. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1312. continue;
  1313. curValue = fDescriptor->get_parameter_value(fHandle, k);
  1314. pData->param.ranges[k].fixValue(curValue);
  1315. if (pData->param.data[k].midiCC > 0)
  1316. {
  1317. value = pData->param.ranges[k].getNormalizedValue(curValue);
  1318. pData->event.portOut->writeControlEvent(0, pData->param.data[k].midiChannel, kEngineControlEventTypeParameter, pData->param.data[k].midiCC, value);
  1319. }
  1320. }
  1321. // reverse lookup MIDI events
  1322. for (uint32_t k = (kPluginMaxMidiEvents*2)-1; k >= fMidiEventCount; --k)
  1323. {
  1324. if (fMidiEvents[k].data[0] == 0)
  1325. break;
  1326. const uint8_t channel = uint8_t(MIDI_GET_CHANNEL_FROM_DATA(fMidiEvents[k].data));
  1327. const uint8_t port = fMidiEvents[k].port;
  1328. if (pData->event.portOut != nullptr)
  1329. pData->event.portOut->writeMidiEvent(fMidiEvents[k].time, channel, port, fMidiEvents[k].size, fMidiEvents[k].data);
  1330. else if (port < fMidiOut.count)
  1331. fMidiOut.ports[port]->writeMidiEvent(fMidiEvents[k].time, channel, port, fMidiEvents[k].size, fMidiEvents[k].data);
  1332. }
  1333. } // End of Control and MIDI Output
  1334. }
  1335. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  1336. {
  1337. CARLA_ASSERT(frames > 0);
  1338. if (frames == 0)
  1339. return false;
  1340. if (pData->audioIn.count > 0)
  1341. {
  1342. CARLA_ASSERT(inBuffer != nullptr);
  1343. if (inBuffer == nullptr)
  1344. return false;
  1345. }
  1346. if (pData->audioOut.count > 0)
  1347. {
  1348. CARLA_ASSERT(outBuffer != nullptr);
  1349. if (outBuffer == nullptr)
  1350. return false;
  1351. }
  1352. uint32_t i, k;
  1353. // --------------------------------------------------------------------------------------------------------
  1354. // Try lock, silence otherwise
  1355. if (pData->engine->isOffline())
  1356. {
  1357. pData->singleMutex.lock();
  1358. }
  1359. else if (! pData->singleMutex.tryLock())
  1360. {
  1361. for (i=0; i < pData->audioOut.count; ++i)
  1362. {
  1363. for (k=0; k < frames; ++k)
  1364. outBuffer[i][k+timeOffset] = 0.0f;
  1365. }
  1366. return false;
  1367. }
  1368. // --------------------------------------------------------------------------------------------------------
  1369. // Reset audio buffers
  1370. for (i=0; i < pData->audioIn.count; ++i)
  1371. FLOAT_COPY(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  1372. for (i=0; i < pData->audioOut.count; ++i)
  1373. FLOAT_CLEAR(fAudioOutBuffers[i], frames);
  1374. // --------------------------------------------------------------------------------------------------------
  1375. // Run plugin
  1376. fIsProcessing = true;
  1377. if (fHandle2 == nullptr)
  1378. {
  1379. fDescriptor->process(fHandle, fAudioInBuffers, fAudioOutBuffers, frames, fMidiEvents, fMidiEventCount);
  1380. }
  1381. else
  1382. {
  1383. fDescriptor->process(fHandle,
  1384. (pData->audioIn.count > 0) ? &fAudioInBuffers[0] : nullptr,
  1385. (pData->audioOut.count > 0) ? &fAudioOutBuffers[0] : nullptr,
  1386. frames, fMidiEvents, fMidiEventCount);
  1387. fDescriptor->process(fHandle2,
  1388. (pData->audioIn.count > 0) ? &fAudioInBuffers[1] : nullptr,
  1389. (pData->audioOut.count > 0) ? &fAudioOutBuffers[1] : nullptr,
  1390. frames, fMidiEvents, fMidiEventCount);
  1391. }
  1392. fIsProcessing = false;
  1393. fTimeInfo.frame += frames;
  1394. #ifndef BUILD_BRIDGE
  1395. // --------------------------------------------------------------------------------------------------------
  1396. // Post-processing (dry/wet, volume and balance)
  1397. {
  1398. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  1399. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  1400. bool isPair;
  1401. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1402. for (i=0; i < pData->audioOut.count; ++i)
  1403. {
  1404. // Dry/Wet
  1405. if (doDryWet)
  1406. {
  1407. for (k=0; k < frames; ++k)
  1408. {
  1409. bufValue = fAudioInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  1410. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1411. }
  1412. }
  1413. // Balance
  1414. if (doBalance)
  1415. {
  1416. isPair = (i % 2 == 0);
  1417. if (isPair)
  1418. {
  1419. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1420. FLOAT_COPY(oldBufLeft, fAudioOutBuffers[i], frames);
  1421. }
  1422. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1423. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1424. for (k=0; k < frames; ++k)
  1425. {
  1426. if (isPair)
  1427. {
  1428. // left
  1429. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1430. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1431. }
  1432. else
  1433. {
  1434. // right
  1435. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1436. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1437. }
  1438. }
  1439. }
  1440. // Volume (and buffer copy)
  1441. {
  1442. for (k=0; k < frames; ++k)
  1443. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  1444. }
  1445. }
  1446. } // End of Post-processing
  1447. #else
  1448. for (i=0; i < pData->audioOut.count; ++i)
  1449. {
  1450. for (k=0; k < frames; ++k)
  1451. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1452. }
  1453. #endif
  1454. // --------------------------------------------------------------------------------------------------------
  1455. pData->singleMutex.unlock();
  1456. return true;
  1457. }
  1458. void bufferSizeChanged(const uint32_t newBufferSize) override
  1459. {
  1460. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1461. carla_debug("NativePlugin::bufferSizeChanged(%i)", newBufferSize);
  1462. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1463. {
  1464. if (fAudioInBuffers[i] != nullptr)
  1465. delete[] fAudioInBuffers[i];
  1466. fAudioInBuffers[i] = new float[newBufferSize];
  1467. }
  1468. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1469. {
  1470. if (fAudioOutBuffers[i] != nullptr)
  1471. delete[] fAudioOutBuffers[i];
  1472. fAudioOutBuffers[i] = new float[newBufferSize];
  1473. }
  1474. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1475. {
  1476. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, newBufferSize, nullptr, 0.0f);
  1477. if (fHandle2 != nullptr)
  1478. fDescriptor->dispatcher(fHandle2, PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, newBufferSize, nullptr, 0.0f);
  1479. }
  1480. }
  1481. void sampleRateChanged(const double newSampleRate) override
  1482. {
  1483. CARLA_ASSERT_INT(newSampleRate > 0.0, (int)newSampleRate);
  1484. carla_debug("NativePlugin::sampleRateChanged(%g)", newSampleRate);
  1485. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1486. {
  1487. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  1488. if (fHandle2 != nullptr)
  1489. fDescriptor->dispatcher(fHandle2, PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  1490. }
  1491. }
  1492. void offlineModeChanged(const bool isOffline) override
  1493. {
  1494. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1495. {
  1496. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  1497. if (fHandle2 != nullptr)
  1498. fDescriptor->dispatcher(fHandle2, PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  1499. }
  1500. }
  1501. // -------------------------------------------------------------------
  1502. // Plugin buffers
  1503. void initBuffers() override
  1504. {
  1505. fMidiIn.initBuffers();
  1506. fMidiOut.initBuffers();
  1507. CarlaPlugin::initBuffers();
  1508. }
  1509. void clearBuffers() override
  1510. {
  1511. carla_debug("NativePlugin::clearBuffers() - start");
  1512. if (fAudioInBuffers != nullptr)
  1513. {
  1514. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1515. {
  1516. if (fAudioInBuffers[i] != nullptr)
  1517. {
  1518. delete[] fAudioInBuffers[i];
  1519. fAudioInBuffers[i] = nullptr;
  1520. }
  1521. }
  1522. delete[] fAudioInBuffers;
  1523. fAudioInBuffers = nullptr;
  1524. }
  1525. if (fAudioOutBuffers != nullptr)
  1526. {
  1527. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1528. {
  1529. if (fAudioOutBuffers[i] != nullptr)
  1530. {
  1531. delete[] fAudioOutBuffers[i];
  1532. fAudioOutBuffers[i] = nullptr;
  1533. }
  1534. }
  1535. delete[] fAudioOutBuffers;
  1536. fAudioOutBuffers = nullptr;
  1537. }
  1538. fMidiIn.clear();
  1539. fMidiOut.clear();
  1540. CarlaPlugin::clearBuffers();
  1541. carla_debug("NativePlugin::clearBuffers() - end");
  1542. }
  1543. // -------------------------------------------------------------------
  1544. // Post-poned UI Stuff
  1545. void uiParameterChange(const uint32_t index, const float /*value*/) noexcept override
  1546. {
  1547. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1548. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1549. CARLA_ASSERT(index < pData->param.count);
  1550. if (! fIsUiVisible)
  1551. return;
  1552. if (fDescriptor == nullptr || fHandle == nullptr)
  1553. return;
  1554. if (index >= pData->param.count)
  1555. return;
  1556. //if (fDescriptor->ui_set_parameter_value != nullptr)
  1557. // fDescriptor->ui_set_parameter_value(fHandle, index, value);
  1558. }
  1559. void uiMidiProgramChange(const uint32_t index) noexcept override
  1560. {
  1561. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1562. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1563. CARLA_ASSERT(index < pData->midiprog.count);
  1564. if (! fIsUiVisible)
  1565. return;
  1566. if (fDescriptor == nullptr || fHandle == nullptr)
  1567. return;
  1568. if (index >= pData->midiprog.count)
  1569. return;
  1570. //if (fDescriptor->ui_set_midi_program != nullptr) // TODO
  1571. // fDescriptor->ui_set_midi_program(fHandle, 0, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  1572. }
  1573. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) noexcept override
  1574. {
  1575. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1576. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1577. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1578. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1579. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  1580. if (! fIsUiVisible)
  1581. return;
  1582. if (fDescriptor == nullptr || fHandle == nullptr)
  1583. return;
  1584. if (channel >= MAX_MIDI_CHANNELS)
  1585. return;
  1586. if (note >= MAX_MIDI_NOTE)
  1587. return;
  1588. if (velo >= MAX_MIDI_VALUE)
  1589. return;
  1590. // TODO
  1591. }
  1592. void uiNoteOff(const uint8_t channel, const uint8_t note) noexcept override
  1593. {
  1594. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1595. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1596. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1597. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1598. if (! fIsUiVisible)
  1599. return;
  1600. if (fDescriptor == nullptr || fHandle == nullptr)
  1601. return;
  1602. if (channel >= MAX_MIDI_CHANNELS)
  1603. return;
  1604. if (note >= MAX_MIDI_NOTE)
  1605. return;
  1606. // TODO
  1607. }
  1608. // -------------------------------------------------------------------
  1609. protected:
  1610. uint32_t handleGetBufferSize() const
  1611. {
  1612. return pData->engine->getBufferSize();
  1613. }
  1614. double handleGetSampleRate() const
  1615. {
  1616. return pData->engine->getSampleRate();
  1617. }
  1618. bool handleIsOffline() const
  1619. {
  1620. return pData->engine->isOffline();
  1621. }
  1622. const NativeTimeInfo* handleGetTimeInfo() const
  1623. {
  1624. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, nullptr);
  1625. return &fTimeInfo;
  1626. }
  1627. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  1628. {
  1629. CARLA_ASSERT(pData->enabled);
  1630. CARLA_ASSERT(fIsProcessing);
  1631. CARLA_ASSERT(fMidiOut.count > 0 || pData->event.portOut != nullptr);
  1632. CARLA_ASSERT(event != nullptr);
  1633. CARLA_ASSERT(event->data[0] != 0);
  1634. if (! pData->enabled)
  1635. return false;
  1636. if (fMidiOut.count == 0)
  1637. return false;
  1638. if (event == nullptr)
  1639. return false;
  1640. if (event->data[0] == 0)
  1641. return false;
  1642. if (! fIsProcessing)
  1643. {
  1644. carla_stderr2("NativePlugin::handleWriteMidiEvent(%p) - received MIDI out event outside audio thread, ignoring", event);
  1645. return false;
  1646. }
  1647. // reverse-find first free event, and put it there
  1648. for (uint32_t i=(kPluginMaxMidiEvents*2)-1; i > fMidiEventCount; --i)
  1649. {
  1650. if (fMidiEvents[i].data[0] == 0)
  1651. {
  1652. std::memcpy(&fMidiEvents[i], event, sizeof(NativeMidiEvent));
  1653. return true;
  1654. }
  1655. }
  1656. return false;
  1657. }
  1658. void handleUiParameterChanged(const uint32_t index, const float value)
  1659. {
  1660. setParameterValue(index, value, false, true, true);
  1661. }
  1662. void handleUiCustomDataChanged(const char* const key, const char* const value)
  1663. {
  1664. setCustomData(CUSTOM_DATA_TYPE_STRING, key, value, false);
  1665. }
  1666. void handleUiClosed()
  1667. {
  1668. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1669. fIsUiVisible = false;
  1670. }
  1671. const char* handleUiOpenFile(const bool isDir, const char* const title, const char* const filter)
  1672. {
  1673. return carla_standalone_file_callback(FILE_CALLBACK_OPEN, isDir, title, filter);
  1674. }
  1675. const char* handleUiSaveFile(const bool isDir, const char* const title, const char* const filter)
  1676. {
  1677. return carla_standalone_file_callback(FILE_CALLBACK_SAVE, isDir, title, filter);
  1678. }
  1679. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  1680. {
  1681. carla_debug("NativePlugin::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)", opcode, index, value, ptr, opt);
  1682. intptr_t ret = 0;
  1683. switch (opcode)
  1684. {
  1685. case ::HOST_OPCODE_NULL:
  1686. break;
  1687. #ifdef BUILD_BRIDGE
  1688. case ::HOST_OPCODE_SET_VOLUME:
  1689. case ::HOST_OPCODE_SET_DRYWET:
  1690. case ::HOST_OPCODE_SET_BALANCE_LEFT:
  1691. case ::HOST_OPCODE_SET_BALANCE_RIGHT:
  1692. case ::HOST_OPCODE_SET_PANNING:
  1693. break;
  1694. #else
  1695. case ::HOST_OPCODE_SET_VOLUME:
  1696. setVolume(opt, true, true);
  1697. break;
  1698. case ::HOST_OPCODE_SET_DRYWET:
  1699. setDryWet(opt, true, true);
  1700. break;
  1701. case ::HOST_OPCODE_SET_BALANCE_LEFT:
  1702. setBalanceLeft(opt, true, true);
  1703. break;
  1704. case ::HOST_OPCODE_SET_BALANCE_RIGHT:
  1705. setBalanceRight(opt, true, true);
  1706. break;
  1707. case ::HOST_OPCODE_SET_PANNING:
  1708. setPanning(opt, true, true);
  1709. break;
  1710. #endif
  1711. case HOST_OPCODE_GET_PARAMETER_MIDI_CC:
  1712. case HOST_OPCODE_SET_PARAMETER_MIDI_CC:
  1713. // TODO
  1714. break;
  1715. case ::HOST_OPCODE_SET_PROCESS_PRECISION:
  1716. // TODO
  1717. break;
  1718. case ::HOST_OPCODE_UPDATE_PARAMETER:
  1719. // TODO
  1720. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, -1, 0, 0.0f, nullptr);
  1721. break;
  1722. case ::HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  1723. // TODO
  1724. pData->engine->callback(ENGINE_CALLBACK_UPDATE, pData->id, -1, 0, 0.0f, nullptr);
  1725. break;
  1726. case ::HOST_OPCODE_RELOAD_PARAMETERS:
  1727. reload(); // FIXME
  1728. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PARAMETERS, pData->id, -1, 0, 0.0f, nullptr);
  1729. break;
  1730. case ::HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  1731. reloadPrograms(false);
  1732. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, -1, 0, 0.0f, nullptr);
  1733. break;
  1734. case ::HOST_OPCODE_RELOAD_ALL:
  1735. reload();
  1736. pData->engine->callback(ENGINE_CALLBACK_RELOAD_ALL, pData->id, -1, 0, 0.0f, nullptr);
  1737. break;
  1738. case HOST_OPCODE_UI_UNAVAILABLE:
  1739. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, nullptr);
  1740. break;
  1741. }
  1742. return ret;
  1743. // unused for now
  1744. (void)index;
  1745. (void)value;
  1746. (void)ptr;
  1747. }
  1748. // -------------------------------------------------------------------
  1749. public:
  1750. static size_t getPluginCount() noexcept
  1751. {
  1752. return sPluginDescriptors.count();
  1753. }
  1754. static const NativePluginDescriptor* getPluginDescriptor(const size_t index) noexcept
  1755. {
  1756. CARLA_SAFE_ASSERT_RETURN(index < sPluginDescriptors.count(), nullptr);
  1757. return sPluginDescriptors.getAt(index);
  1758. }
  1759. static void registerPlugin(const NativePluginDescriptor* desc)
  1760. {
  1761. sPluginDescriptors.append(desc);
  1762. }
  1763. // -------------------------------------------------------------------
  1764. bool init(const char* const name, const char* const label)
  1765. {
  1766. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr, false);
  1767. // ---------------------------------------------------------------
  1768. // first checks
  1769. if (pData->client != nullptr)
  1770. {
  1771. pData->engine->setLastError("Plugin client is already registered");
  1772. return false;
  1773. }
  1774. if (label == nullptr && label[0] != '\0')
  1775. {
  1776. pData->engine->setLastError("null label");
  1777. return false;
  1778. }
  1779. // ---------------------------------------------------------------
  1780. // get descriptor that matches label
  1781. for (LinkedList<const NativePluginDescriptor*>::Itenerator it = sPluginDescriptors.begin(); it.valid(); it.next())
  1782. {
  1783. fDescriptor = it.getValue();
  1784. CARLA_SAFE_ASSERT_BREAK(fDescriptor != nullptr);
  1785. carla_debug("Check vs \"%s\"", fDescriptor->label);
  1786. if (fDescriptor->label != nullptr && std::strcmp(fDescriptor->label, label) == 0)
  1787. break;
  1788. fDescriptor = nullptr;
  1789. }
  1790. if (fDescriptor == nullptr)
  1791. {
  1792. pData->engine->setLastError("Invalid internal plugin");
  1793. return false;
  1794. }
  1795. // ---------------------------------------------------------------
  1796. // set icon
  1797. if (std::strcmp(fDescriptor->label, "audiofile") == 0)
  1798. pData->iconName = carla_strdup("file");
  1799. else if (std::strcmp(fDescriptor->label, "midifile") == 0)
  1800. pData->iconName = carla_strdup("file");
  1801. else if (std::strcmp(fDescriptor->label, "sunvoxfile") == 0)
  1802. pData->iconName = carla_strdup("file");
  1803. else if (std::strcmp(fDescriptor->label, "3BandEQ") == 0)
  1804. pData->iconName = carla_strdup("distrho");
  1805. else if (std::strcmp(fDescriptor->label, "3BandSplitter") == 0)
  1806. pData->iconName = carla_strdup("distrho");
  1807. else if (std::strcmp(fDescriptor->label, "Nekobi") == 0)
  1808. pData->iconName = carla_strdup("distrho");
  1809. else if (std::strcmp(fDescriptor->label, "Notes") == 0)
  1810. pData->iconName = carla_strdup("distrho");
  1811. else if (std::strcmp(fDescriptor->label, "PingPongPan") == 0)
  1812. pData->iconName = carla_strdup("distrho");
  1813. else if (std::strcmp(fDescriptor->label, "StereoEnhancer") == 0)
  1814. pData->iconName = carla_strdup("distrho");
  1815. // ---------------------------------------------------------------
  1816. // get info
  1817. if (name != nullptr && name[0] != '\0')
  1818. pData->name = pData->engine->getUniquePluginName(name);
  1819. else if (fDescriptor->name != nullptr && fDescriptor->name[0] != '\0')
  1820. pData->name = pData->engine->getUniquePluginName(fDescriptor->name);
  1821. else
  1822. pData->name = pData->engine->getUniquePluginName(label);
  1823. {
  1824. CARLA_ASSERT(fHost.uiName == nullptr);
  1825. char uiName[std::strlen(pData->name)+6+1];
  1826. std::strcpy(uiName, pData->name);
  1827. std::strcat(uiName, " (GUI)");
  1828. fHost.uiName = carla_strdup(uiName);
  1829. }
  1830. // ---------------------------------------------------------------
  1831. // register client
  1832. pData->client = pData->engine->addClient(this);
  1833. if (pData->client == nullptr || ! pData->client->isOk())
  1834. {
  1835. pData->engine->setLastError("Failed to register plugin client");
  1836. return false;
  1837. }
  1838. // ---------------------------------------------------------------
  1839. // initialize plugin
  1840. fHandle = fDescriptor->instantiate(&fHost);
  1841. if (fHandle == nullptr)
  1842. {
  1843. pData->engine->setLastError("Plugin failed to initialize");
  1844. return false;
  1845. }
  1846. // ---------------------------------------------------------------
  1847. // load plugin settings
  1848. {
  1849. const bool hasMidiProgs(fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_count(fHandle) > 0);
  1850. // set default options
  1851. pData->options = 0x0;
  1852. if (hasMidiProgs && (fDescriptor->supports & ::PLUGIN_SUPPORTS_PROGRAM_CHANGES) == 0)
  1853. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1854. if (getMidiInCount() > 0 || (fDescriptor->hints & ::PLUGIN_NEEDS_FIXED_BUFFERS) != 0)
  1855. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1856. if (pData->engine->getOptions().forceStereo)
  1857. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1858. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_CHANNEL_PRESSURE)
  1859. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1860. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_NOTE_AFTERTOUCH)
  1861. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1862. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_PITCHBEND)
  1863. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1864. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_ALL_SOUND_OFF)
  1865. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1866. // set identifier string
  1867. CarlaString identifier("Native/");
  1868. identifier += label;
  1869. pData->identifier = identifier.dup();
  1870. // load settings
  1871. pData->options = pData->loadSettings(pData->options, getOptionsAvailable());
  1872. // ignore settings, we need this anyway
  1873. if (getMidiInCount() > 0 || (fDescriptor->hints & ::PLUGIN_NEEDS_FIXED_BUFFERS) != 0)
  1874. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1875. }
  1876. return true;
  1877. }
  1878. class ScopedInitializer
  1879. {
  1880. public:
  1881. ScopedInitializer()
  1882. {
  1883. carla_register_all_plugins();
  1884. }
  1885. ~ScopedInitializer()
  1886. {
  1887. sPluginDescriptors.clear();
  1888. }
  1889. };
  1890. private:
  1891. NativePluginHandle fHandle;
  1892. NativePluginHandle fHandle2;
  1893. NativeHostDescriptor fHost;
  1894. const NativePluginDescriptor* fDescriptor;
  1895. bool fIsProcessing;
  1896. bool fIsUiVisible;
  1897. float** fAudioInBuffers;
  1898. float** fAudioOutBuffers;
  1899. uint32_t fMidiEventCount;
  1900. NativeMidiEvent fMidiEvents[kPluginMaxMidiEvents*2];
  1901. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  1902. NativePluginMidiData fMidiIn;
  1903. NativePluginMidiData fMidiOut;
  1904. NativeTimeInfo fTimeInfo;
  1905. static LinkedList<const NativePluginDescriptor*> sPluginDescriptors;
  1906. // -------------------------------------------------------------------
  1907. #define handlePtr ((NativePlugin*)handle)
  1908. static uint32_t carla_host_get_buffer_size(NativeHostHandle handle)
  1909. {
  1910. return handlePtr->handleGetBufferSize();
  1911. }
  1912. static double carla_host_get_sample_rate(NativeHostHandle handle)
  1913. {
  1914. return handlePtr->handleGetSampleRate();
  1915. }
  1916. static bool carla_host_is_offline(NativeHostHandle handle)
  1917. {
  1918. return handlePtr->handleIsOffline();
  1919. }
  1920. static const NativeTimeInfo* carla_host_get_time_info(NativeHostHandle handle)
  1921. {
  1922. return handlePtr->handleGetTimeInfo();
  1923. }
  1924. static bool carla_host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  1925. {
  1926. return handlePtr->handleWriteMidiEvent(event);
  1927. }
  1928. static void carla_host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  1929. {
  1930. handlePtr->handleUiParameterChanged(index, value);
  1931. }
  1932. static void carla_host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  1933. {
  1934. handlePtr->handleUiCustomDataChanged(key, value);
  1935. }
  1936. static void carla_host_ui_closed(NativeHostHandle handle)
  1937. {
  1938. handlePtr->handleUiClosed();
  1939. }
  1940. static const char* carla_host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  1941. {
  1942. return handlePtr->handleUiOpenFile(isDir, title, filter);
  1943. }
  1944. static const char* carla_host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  1945. {
  1946. return handlePtr->handleUiSaveFile(isDir, title, filter);
  1947. }
  1948. static intptr_t carla_host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  1949. {
  1950. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  1951. }
  1952. #undef handlePtr
  1953. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  1954. };
  1955. LinkedList<const NativePluginDescriptor*> NativePlugin::sPluginDescriptors;
  1956. static const NativePlugin::ScopedInitializer _si;
  1957. CARLA_BACKEND_END_NAMESPACE
  1958. void carla_register_native_plugin(const NativePluginDescriptor* desc)
  1959. {
  1960. CARLA_BACKEND_USE_NAMESPACE
  1961. NativePlugin::registerPlugin(desc);
  1962. }
  1963. #endif // WANT_NATIVE
  1964. // -----------------------------------------------------------------------
  1965. CARLA_BACKEND_START_NAMESPACE
  1966. #ifdef WANT_NATIVE
  1967. size_t CarlaPlugin::getNativePluginCount() noexcept
  1968. {
  1969. return NativePlugin::getPluginCount();
  1970. }
  1971. const NativePluginDescriptor* CarlaPlugin::getNativePluginDescriptor(const size_t index) noexcept
  1972. {
  1973. return NativePlugin::getPluginDescriptor(index);
  1974. }
  1975. #endif
  1976. // -----------------------------------------------------------------------
  1977. CarlaPlugin* CarlaPlugin::newNative(const Initializer& init)
  1978. {
  1979. carla_debug("CarlaPlugin::newNative({%p, \"%s\", \"%s\", \"%s\"})", init.engine, init.filename, init.name, init.label);
  1980. #ifdef WANT_NATIVE
  1981. NativePlugin* const plugin(new NativePlugin(init.engine, init.id));
  1982. if (! plugin->init(init.name, init.label))
  1983. {
  1984. delete plugin;
  1985. return nullptr;
  1986. }
  1987. plugin->reload();
  1988. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  1989. {
  1990. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo Internal plugins, sorry!");
  1991. delete plugin;
  1992. return nullptr;
  1993. }
  1994. return plugin;
  1995. #else
  1996. init.engine->setLastError("Internal plugins support not available");
  1997. return nullptr;
  1998. #endif
  1999. }
  2000. CARLA_BACKEND_END_NAMESPACE
  2001. // -----------------------------------------------------------------------