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.

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