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.

2488 lines
88KB

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