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.

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