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.

2435 lines
80KB

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