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.

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