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.

2466 lines
81KB

  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(); // kinda unfinished
  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(); // unfinished
  56. #endif
  57. // DISTRHO plugins (Qt)
  58. //carla_register_native_plugin_Notes(); // unfinished
  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 (fHandle2 != nullptr)
  627. {
  628. if (aIns == 1)
  629. {
  630. aIns = 2;
  631. forcedStereoIn = true;
  632. }
  633. if (aOuts == 1)
  634. {
  635. aOuts = 2;
  636. forcedStereoOut = true;
  637. }
  638. }
  639. }
  640. if (aIns > 0)
  641. {
  642. kData->audioIn.createNew(aIns);
  643. fAudioInBuffers = new float*[aIns];
  644. for (uint32_t i=0; i < aIns; ++i)
  645. fAudioInBuffers[i] = nullptr;
  646. }
  647. if (aOuts > 0)
  648. {
  649. kData->audioOut.createNew(aOuts);
  650. fAudioOutBuffers = new float*[aOuts];
  651. needsCtrlIn = true;
  652. for (uint32_t i=0; i < aOuts; ++i)
  653. fAudioOutBuffers[i] = nullptr;
  654. }
  655. if (mIns > 0)
  656. {
  657. fMidiIn.createNew(mIns);
  658. needsCtrlIn = (mIns == 1);
  659. }
  660. if (mOuts > 0)
  661. {
  662. fMidiOut.createNew(mOuts);
  663. needsCtrlOut = (mOuts == 1);
  664. }
  665. if (params > 0)
  666. {
  667. kData->param.createNew(params);
  668. }
  669. const uint portNameSize(kData->engine->maxPortNameSize());
  670. CarlaString portName;
  671. // Audio Ins
  672. for (j=0; j < aIns; ++j)
  673. {
  674. portName.clear();
  675. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  676. {
  677. portName = fName;
  678. portName += ":";
  679. }
  680. if (aIns > 1 && ! forcedStereoIn)
  681. {
  682. portName += "input_";
  683. portName += CarlaString(j+1);
  684. }
  685. else
  686. portName += "input";
  687. portName.truncate(portNameSize);
  688. kData->audioIn.ports[j].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, true);
  689. kData->audioIn.ports[j].rindex = j;
  690. if (forcedStereoIn)
  691. {
  692. portName += "_2";
  693. kData->audioIn.ports[1].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, true);
  694. kData->audioIn.ports[1].rindex = j;
  695. break;
  696. }
  697. }
  698. // Audio Outs
  699. for (j=0; j < aOuts; ++j)
  700. {
  701. portName.clear();
  702. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  703. {
  704. portName = fName;
  705. portName += ":";
  706. }
  707. if (aOuts > 1 && ! forcedStereoOut)
  708. {
  709. portName += "output_";
  710. portName += CarlaString(j+1);
  711. }
  712. else
  713. portName += "output";
  714. portName.truncate(portNameSize);
  715. kData->audioOut.ports[j].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, false);
  716. kData->audioOut.ports[j].rindex = j;
  717. if (forcedStereoOut)
  718. {
  719. portName += "_2";
  720. kData->audioOut.ports[1].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, false);
  721. kData->audioOut.ports[1].rindex = j;
  722. break;
  723. }
  724. }
  725. // MIDI Input (only if multiple)
  726. if (mIns > 1)
  727. {
  728. for (j=0; j < mIns; ++j)
  729. {
  730. portName.clear();
  731. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  732. {
  733. portName = fName;
  734. portName += ":";
  735. }
  736. portName += "midi-in_";
  737. portName += CarlaString(j+1);
  738. portName.truncate(portNameSize);
  739. fMidiIn.ports[j] = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  740. fMidiIn.indexes[j] = j;
  741. }
  742. }
  743. // MIDI Output (only if multiple)
  744. if (mOuts > 1)
  745. {
  746. for (j=0; j < mOuts; ++j)
  747. {
  748. portName.clear();
  749. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  750. {
  751. portName = fName;
  752. portName += ":";
  753. }
  754. portName += "midi-out_";
  755. portName += CarlaString(j+1);
  756. portName.truncate(portNameSize);
  757. fMidiOut.ports[j] = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  758. fMidiOut.indexes[j] = j;
  759. }
  760. }
  761. for (j=0; j < params; ++j)
  762. {
  763. const ::Parameter* const paramInfo(fDescriptor->get_parameter_info(fHandle, j));
  764. CARLA_ASSERT(paramInfo != nullptr);
  765. if (paramInfo == nullptr)
  766. continue;
  767. kData->param.data[j].index = j;
  768. kData->param.data[j].rindex = j;
  769. kData->param.data[j].hints = 0x0;
  770. kData->param.data[j].midiChannel = 0;
  771. kData->param.data[j].midiCC = -1;
  772. float min, max, def, step, stepSmall, stepLarge;
  773. // min value
  774. min = paramInfo->ranges.min;
  775. // max value
  776. max = paramInfo->ranges.max;
  777. if (min > max)
  778. max = min;
  779. else if (max < min)
  780. min = max;
  781. if (max - min == 0.0f)
  782. {
  783. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", paramInfo->name);
  784. max = min + 0.1f;
  785. }
  786. // default value
  787. def = paramInfo->ranges.def;
  788. if (def < min)
  789. def = min;
  790. else if (def > max)
  791. def = max;
  792. if (paramInfo->hints & ::PARAMETER_USES_SAMPLE_RATE)
  793. {
  794. min *= sampleRate;
  795. max *= sampleRate;
  796. def *= sampleRate;
  797. kData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  798. }
  799. if (paramInfo->hints & ::PARAMETER_IS_BOOLEAN)
  800. {
  801. step = max - min;
  802. stepSmall = step;
  803. stepLarge = step;
  804. kData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  805. }
  806. else if (paramInfo->hints & ::PARAMETER_IS_INTEGER)
  807. {
  808. step = 1.0f;
  809. stepSmall = 1.0f;
  810. stepLarge = 10.0f;
  811. kData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  812. }
  813. else
  814. {
  815. float range = max - min;
  816. step = range/100.0f;
  817. stepSmall = range/1000.0f;
  818. stepLarge = range/10.0f;
  819. }
  820. if (paramInfo->hints & ::PARAMETER_IS_OUTPUT)
  821. {
  822. kData->param.data[j].type = PARAMETER_OUTPUT;
  823. needsCtrlOut = true;
  824. }
  825. else
  826. {
  827. kData->param.data[j].type = PARAMETER_INPUT;
  828. needsCtrlIn = true;
  829. }
  830. // extra parameter hints
  831. if (paramInfo->hints & ::PARAMETER_IS_ENABLED)
  832. kData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  833. if (paramInfo->hints & ::PARAMETER_IS_AUTOMABLE)
  834. kData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  835. if (paramInfo->hints & ::PARAMETER_IS_LOGARITHMIC)
  836. kData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  837. if (paramInfo->hints & ::PARAMETER_USES_SCALEPOINTS)
  838. kData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  839. if (paramInfo->hints & ::PARAMETER_USES_CUSTOM_TEXT)
  840. kData->param.data[j].hints |= PARAMETER_USES_CUSTOM_TEXT;
  841. kData->param.ranges[j].min = min;
  842. kData->param.ranges[j].max = max;
  843. kData->param.ranges[j].def = def;
  844. kData->param.ranges[j].step = step;
  845. kData->param.ranges[j].stepSmall = stepSmall;
  846. kData->param.ranges[j].stepLarge = stepLarge;
  847. }
  848. if (needsCtrlIn)
  849. {
  850. portName.clear();
  851. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  852. {
  853. portName = fName;
  854. portName += ":";
  855. }
  856. portName += "events-in";
  857. portName.truncate(portNameSize);
  858. kData->event.portIn = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  859. }
  860. if (needsCtrlOut)
  861. {
  862. portName.clear();
  863. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  864. {
  865. portName = fName;
  866. portName += ":";
  867. }
  868. portName += "events-out";
  869. portName.truncate(portNameSize);
  870. kData->event.portOut = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  871. }
  872. if (forcedStereoIn || forcedStereoOut)
  873. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  874. else
  875. fOptions &= ~PLUGIN_OPTION_FORCE_STEREO;
  876. // plugin hints
  877. fHints = 0x0;
  878. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  879. fHints |= PLUGIN_CAN_DRYWET;
  880. if (aOuts > 0)
  881. fHints |= PLUGIN_CAN_VOLUME;
  882. if (aOuts >= 2 && aOuts % 2 == 0)
  883. fHints |= PLUGIN_CAN_BALANCE;
  884. // native plugin hints
  885. if (fDescriptor->hints & ::PLUGIN_IS_RTSAFE)
  886. fHints |= PLUGIN_IS_RTSAFE;
  887. if (fDescriptor->hints & ::PLUGIN_IS_SYNTH)
  888. fHints |= PLUGIN_IS_SYNTH;
  889. if (fDescriptor->hints & ::PLUGIN_HAS_GUI)
  890. fHints |= PLUGIN_HAS_GUI;
  891. if (fDescriptor->hints & ::PLUGIN_USES_GUI_AS_FILE)
  892. fHints |= PLUGIN_HAS_GUI_AS_FILE;
  893. if (fDescriptor->hints & ::PLUGIN_USES_SINGLE_THREAD)
  894. fHints |= PLUGIN_HAS_SINGLE_THREAD;
  895. // extra plugin hints
  896. kData->extraHints = 0x0;
  897. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && mIns <= 1 && mOuts <= 1)
  898. kData->extraHints |= PLUGIN_HINT_CAN_RUN_RACK;
  899. bufferSizeChanged(kData->engine->getBufferSize());
  900. reloadPrograms(true);
  901. if (kData->active)
  902. activate();
  903. carla_debug("NativePlugin::reload() - end");
  904. }
  905. void reloadPrograms(const bool init) override
  906. {
  907. carla_debug("NativePlugin::reloadPrograms(%s)", bool2str(init));
  908. uint32_t i, oldCount = kData->midiprog.count;
  909. const int32_t current = kData->midiprog.current;
  910. // Delete old programs
  911. kData->midiprog.clear();
  912. // Query new programs
  913. uint32_t count = 0;
  914. if (fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_info != nullptr && fDescriptor->set_midi_program != nullptr)
  915. count = fDescriptor->get_midi_program_count(fHandle);
  916. if (count > 0)
  917. {
  918. kData->midiprog.createNew(count);
  919. // Update data
  920. for (i=0; i < count; ++i)
  921. {
  922. const ::MidiProgram* const mpDesc = fDescriptor->get_midi_program_info(fHandle, i);
  923. CARLA_ASSERT(mpDesc != nullptr);
  924. CARLA_ASSERT(mpDesc->name != nullptr);
  925. kData->midiprog.data[i].bank = mpDesc->bank;
  926. kData->midiprog.data[i].program = mpDesc->program;
  927. kData->midiprog.data[i].name = carla_strdup(mpDesc->name);
  928. }
  929. }
  930. #ifndef BUILD_BRIDGE
  931. // Update OSC Names
  932. if (kData->engine->isOscControlRegistered())
  933. {
  934. kData->engine->osc_send_control_set_midi_program_count(fId, count);
  935. for (i=0; i < count; ++i)
  936. 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);
  937. }
  938. #endif
  939. if (init)
  940. {
  941. if (count > 0)
  942. setMidiProgram(0, false, false, false);
  943. }
  944. else
  945. {
  946. // Check if current program is invalid
  947. bool programChanged = false;
  948. if (count == oldCount+1)
  949. {
  950. // one midi program added, probably created by user
  951. kData->midiprog.current = oldCount;
  952. programChanged = true;
  953. }
  954. else if (current < 0 && count > 0)
  955. {
  956. // programs exist now, but not before
  957. kData->midiprog.current = 0;
  958. programChanged = true;
  959. }
  960. else if (current >= 0 && count == 0)
  961. {
  962. // programs existed before, but not anymore
  963. kData->midiprog.current = -1;
  964. programChanged = true;
  965. }
  966. else if (current >= static_cast<int32_t>(count))
  967. {
  968. // current midi program > count
  969. kData->midiprog.current = 0;
  970. programChanged = true;
  971. }
  972. else
  973. {
  974. // no change
  975. kData->midiprog.current = current;
  976. }
  977. if (programChanged)
  978. setMidiProgram(kData->midiprog.current, true, true, true);
  979. kData->engine->callback(CALLBACK_RELOAD_PROGRAMS, fId, 0, 0, 0.0f, nullptr);
  980. }
  981. }
  982. // -------------------------------------------------------------------
  983. // Plugin processing
  984. void activate() override
  985. {
  986. CARLA_ASSERT(fDescriptor != nullptr);
  987. CARLA_ASSERT(fHandle != nullptr);
  988. if (fDescriptor->activate != nullptr)
  989. {
  990. fDescriptor->activate(fHandle);
  991. if (fHandle2 != nullptr)
  992. fDescriptor->activate(fHandle2);
  993. }
  994. }
  995. void deactivate() override
  996. {
  997. CARLA_ASSERT(fDescriptor != nullptr);
  998. CARLA_ASSERT(fHandle != nullptr);
  999. if (fDescriptor->deactivate != nullptr)
  1000. {
  1001. fDescriptor->deactivate(fHandle);
  1002. if (fHandle2 != nullptr)
  1003. fDescriptor->deactivate(fHandle2);
  1004. }
  1005. }
  1006. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  1007. {
  1008. uint32_t i, k;
  1009. // --------------------------------------------------------------------------------------------------------
  1010. // Check if active
  1011. if (! kData->active)
  1012. {
  1013. // disable any output sound
  1014. for (i=0; i < kData->audioOut.count; ++i)
  1015. carla_zeroFloat(outBuffer[i], frames);
  1016. return;
  1017. }
  1018. fMidiEventCount = 0;
  1019. carla_zeroStruct< ::MidiEvent>(fMidiEvents, MAX_MIDI_EVENTS*2);
  1020. // --------------------------------------------------------------------------------------------------------
  1021. // Check if needs reset
  1022. if (kData->needsReset)
  1023. {
  1024. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1025. {
  1026. for (k=0, i=MAX_MIDI_CHANNELS; k < MAX_MIDI_CHANNELS; ++k)
  1027. {
  1028. fMidiEvents[k].data[0] = MIDI_STATUS_CONTROL_CHANGE + k;
  1029. fMidiEvents[k].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1030. fMidiEvents[k].data[2] = 0;
  1031. fMidiEvents[k].size = 3;
  1032. fMidiEvents[k+i].data[0] = MIDI_STATUS_CONTROL_CHANGE + k;
  1033. fMidiEvents[k+i].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1034. fMidiEvents[k+i].data[2] = 0;
  1035. fMidiEvents[k+i].size = 3;
  1036. }
  1037. fMidiEventCount = MAX_MIDI_CHANNELS*2;
  1038. }
  1039. else if (kData->ctrlChannel >= 0 && kData->ctrlChannel < MAX_MIDI_CHANNELS)
  1040. {
  1041. for (k=0; k < MAX_MIDI_NOTE; ++k)
  1042. {
  1043. fMidiEvents[k].data[0] = MIDI_STATUS_NOTE_OFF + kData->ctrlChannel;
  1044. fMidiEvents[k].data[1] = k;
  1045. fMidiEvents[k].data[2] = 0;
  1046. fMidiEvents[k].size = 3;
  1047. }
  1048. fMidiEventCount = MAX_MIDI_NOTE;
  1049. }
  1050. kData->needsReset = false;
  1051. }
  1052. CARLA_PROCESS_CONTINUE_CHECK;
  1053. // --------------------------------------------------------------------------------------------------------
  1054. // Set TimeInfo
  1055. const EngineTimeInfo& timeInfo(kData->engine->getTimeInfo());
  1056. fTimeInfo.playing = timeInfo.playing;
  1057. fTimeInfo.frame = timeInfo.frame;
  1058. fTimeInfo.usecs = timeInfo.usecs;
  1059. if (timeInfo.valid & EngineTimeInfo::ValidBBT)
  1060. {
  1061. fTimeInfo.bbt.valid = true;
  1062. fTimeInfo.bbt.bar = timeInfo.bbt.bar;
  1063. fTimeInfo.bbt.beat = timeInfo.bbt.beat;
  1064. fTimeInfo.bbt.tick = timeInfo.bbt.tick;
  1065. fTimeInfo.bbt.barStartTick = timeInfo.bbt.barStartTick;
  1066. fTimeInfo.bbt.beatsPerBar = timeInfo.bbt.beatsPerBar;
  1067. fTimeInfo.bbt.beatType = timeInfo.bbt.beatType;
  1068. fTimeInfo.bbt.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
  1069. fTimeInfo.bbt.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
  1070. }
  1071. else
  1072. fTimeInfo.bbt.valid = false;
  1073. CARLA_PROCESS_CONTINUE_CHECK;
  1074. // --------------------------------------------------------------------------------------------------------
  1075. // Event Input and Processing
  1076. if (kData->event.portIn != nullptr)
  1077. {
  1078. // ----------------------------------------------------------------------------------------------------
  1079. // MIDI Input (External)
  1080. if (kData->extNotes.mutex.tryLock())
  1081. {
  1082. while (fMidiEventCount < MAX_MIDI_EVENTS*2 && ! kData->extNotes.data.isEmpty())
  1083. {
  1084. const ExternalMidiNote& note(kData->extNotes.data.getFirst(true));
  1085. CARLA_ASSERT(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  1086. fMidiEvents[fMidiEventCount].data[0] = (note.velo > 0) ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF;
  1087. fMidiEvents[fMidiEventCount].data[0] += note.channel;
  1088. fMidiEvents[fMidiEventCount].data[1] = note.note;
  1089. fMidiEvents[fMidiEventCount].data[2] = note.velo;
  1090. fMidiEvents[fMidiEventCount].size = 3;
  1091. fMidiEventCount += 1;
  1092. }
  1093. kData->extNotes.mutex.unlock();
  1094. } // End of MIDI Input (External)
  1095. // ----------------------------------------------------------------------------------------------------
  1096. // Event Input (System)
  1097. bool allNotesOffSent = false;
  1098. bool sampleAccurate = (fOptions & PLUGIN_OPTION_FIXED_BUFFER) == 0;
  1099. uint32_t time, nEvents = kData->event.portIn->getEventCount();
  1100. uint32_t startTime = 0;
  1101. uint32_t timeOffset = 0;
  1102. uint32_t nextBankId = 0;
  1103. if (kData->midiprog.current >= 0 && kData->midiprog.count > 0)
  1104. nextBankId = kData->midiprog.data[kData->midiprog.current].bank;
  1105. for (i=0; i < nEvents; ++i)
  1106. {
  1107. const EngineEvent& event(kData->event.portIn->getEvent(i));
  1108. time = event.time;
  1109. if (time >= frames)
  1110. continue;
  1111. CARLA_ASSERT_INT2(time >= timeOffset, time, timeOffset);
  1112. if (time > timeOffset && sampleAccurate)
  1113. {
  1114. if (processSingle(inBuffer, outBuffer, time - timeOffset, timeOffset))
  1115. {
  1116. startTime = 0;
  1117. timeOffset = time;
  1118. if (kData->midiprog.current >= 0 && kData->midiprog.count > 0)
  1119. nextBankId = kData->midiprog.data[kData->midiprog.current].bank;
  1120. else
  1121. nextBankId = 0;
  1122. if (fMidiEventCount > 0)
  1123. {
  1124. carla_zeroStruct< ::MidiEvent>(fMidiEvents, fMidiEventCount);
  1125. fMidiEventCount = 0;
  1126. }
  1127. }
  1128. else
  1129. startTime += timeOffset;
  1130. }
  1131. // Control change
  1132. switch (event.type)
  1133. {
  1134. case kEngineEventTypeNull:
  1135. break;
  1136. case kEngineEventTypeControl:
  1137. {
  1138. const EngineControlEvent& ctrlEvent = event.ctrl;
  1139. switch (ctrlEvent.type)
  1140. {
  1141. case kEngineControlEventTypeNull:
  1142. break;
  1143. case kEngineControlEventTypeParameter:
  1144. {
  1145. #ifndef BUILD_BRIDGE
  1146. // Control backend stuff
  1147. if (event.channel == kData->ctrlChannel)
  1148. {
  1149. float value;
  1150. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (fHints & PLUGIN_CAN_DRYWET) > 0)
  1151. {
  1152. value = ctrlEvent.value;
  1153. setDryWet(value, false, false);
  1154. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  1155. }
  1156. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (fHints & PLUGIN_CAN_VOLUME) > 0)
  1157. {
  1158. value = ctrlEvent.value*127.0f/100.0f;
  1159. setVolume(value, false, false);
  1160. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  1161. }
  1162. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (fHints & PLUGIN_CAN_BALANCE) > 0)
  1163. {
  1164. float left, right;
  1165. value = ctrlEvent.value/0.5f - 1.0f;
  1166. if (value < 0.0f)
  1167. {
  1168. left = -1.0f;
  1169. right = (value*2.0f)+1.0f;
  1170. }
  1171. else if (value > 0.0f)
  1172. {
  1173. left = (value*2.0f)-1.0f;
  1174. right = 1.0f;
  1175. }
  1176. else
  1177. {
  1178. left = -1.0f;
  1179. right = 1.0f;
  1180. }
  1181. setBalanceLeft(left, false, false);
  1182. setBalanceRight(right, false, false);
  1183. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  1184. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  1185. }
  1186. }
  1187. #endif
  1188. // Control plugin parameters
  1189. for (k=0; k < kData->param.count; ++k)
  1190. {
  1191. if (kData->param.data[k].midiChannel != event.channel)
  1192. continue;
  1193. if (kData->param.data[k].midiCC != ctrlEvent.param)
  1194. continue;
  1195. if (kData->param.data[k].type != PARAMETER_INPUT)
  1196. continue;
  1197. if ((kData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1198. continue;
  1199. float value;
  1200. if (kData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1201. {
  1202. value = (ctrlEvent.value < 0.5f) ? kData->param.ranges[k].min : kData->param.ranges[k].max;
  1203. }
  1204. else
  1205. {
  1206. value = kData->param.ranges[k].unnormalizeValue(ctrlEvent.value);
  1207. if (kData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1208. value = std::rint(value);
  1209. }
  1210. setParameterValue(k, value, false, false, false);
  1211. postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  1212. }
  1213. if ((fOptions & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  1214. {
  1215. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1216. continue;
  1217. fMidiEvents[fMidiEventCount].port = 0;
  1218. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1219. fMidiEvents[fMidiEventCount].data[0] = MIDI_STATUS_CONTROL_CHANGE + event.channel;
  1220. fMidiEvents[fMidiEventCount].data[1] = ctrlEvent.param;
  1221. fMidiEvents[fMidiEventCount].data[2] = ctrlEvent.value*127.0f;
  1222. fMidiEvents[fMidiEventCount].size = 3;
  1223. fMidiEventCount += 1;
  1224. }
  1225. break;
  1226. }
  1227. case kEngineControlEventTypeMidiBank:
  1228. if (event.channel == kData->ctrlChannel && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1229. nextBankId = ctrlEvent.param;
  1230. break;
  1231. case kEngineControlEventTypeMidiProgram:
  1232. if (event.channel < MAX_MIDI_CHANNELS && (fOptions & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1233. {
  1234. const uint32_t nextProgramId(ctrlEvent.param);
  1235. for (k=0; k < kData->midiprog.count; ++k)
  1236. {
  1237. if (kData->midiprog.data[k].bank == nextBankId && kData->midiprog.data[k].program == nextProgramId)
  1238. {
  1239. fDescriptor->set_midi_program(fHandle, event.channel, nextBankId, nextProgramId);
  1240. if (fHandle2 != nullptr)
  1241. fDescriptor->set_midi_program(fHandle2, event.channel, nextBankId, nextProgramId);
  1242. fCurMidiProgs[event.channel] = k;
  1243. if (event.channel == kData->ctrlChannel)
  1244. postponeRtEvent(kPluginPostRtEventMidiProgramChange, k, 0, 0.0f);
  1245. break;
  1246. }
  1247. }
  1248. }
  1249. break;
  1250. case kEngineControlEventTypeAllSoundOff:
  1251. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1252. {
  1253. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1254. continue;
  1255. fMidiEvents[fMidiEventCount].port = 0;
  1256. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1257. fMidiEvents[fMidiEventCount].data[0] = MIDI_STATUS_CONTROL_CHANGE + event.channel;
  1258. fMidiEvents[fMidiEventCount].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1259. fMidiEvents[fMidiEventCount].data[2] = 0;
  1260. fMidiEvents[fMidiEventCount].size = 3;
  1261. fMidiEventCount += 1;
  1262. }
  1263. break;
  1264. case kEngineControlEventTypeAllNotesOff:
  1265. if (fOptions & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1266. {
  1267. if (event.channel == kData->ctrlChannel && ! allNotesOffSent)
  1268. {
  1269. allNotesOffSent = true;
  1270. sendMidiAllNotesOffToCallback();
  1271. }
  1272. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1273. continue;
  1274. fMidiEvents[fMidiEventCount].port = 0;
  1275. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1276. fMidiEvents[fMidiEventCount].data[0] = MIDI_STATUS_CONTROL_CHANGE + event.channel;
  1277. fMidiEvents[fMidiEventCount].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1278. fMidiEvents[fMidiEventCount].data[2] = 0;
  1279. fMidiEvents[fMidiEventCount].size = 3;
  1280. fMidiEventCount += 1;
  1281. }
  1282. break;
  1283. }
  1284. break;
  1285. }
  1286. case kEngineEventTypeMidi:
  1287. {
  1288. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1289. continue;
  1290. const EngineMidiEvent& midiEvent(event.midi);
  1291. uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent.data);
  1292. uint8_t channel = event.channel;
  1293. if (MIDI_IS_STATUS_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1294. continue;
  1295. if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (fOptions & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1296. continue;
  1297. if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (fOptions & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1298. continue;
  1299. if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (fOptions & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1300. continue;
  1301. // Fix bad note-off
  1302. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1303. status -= 0x10;
  1304. fMidiEvents[fMidiEventCount].port = 0;
  1305. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1306. fMidiEvents[fMidiEventCount].size = midiEvent.size;
  1307. fMidiEvents[fMidiEventCount].data[0] = status + channel;
  1308. fMidiEvents[fMidiEventCount].data[1] = midiEvent.data[1];
  1309. fMidiEvents[fMidiEventCount].data[2] = midiEvent.data[2];
  1310. fMidiEvents[fMidiEventCount].data[3] = midiEvent.data[3];
  1311. fMidiEventCount += 1;
  1312. if (status == MIDI_STATUS_NOTE_ON)
  1313. postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  1314. else if (status == MIDI_STATUS_NOTE_OFF)
  1315. postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  1316. break;
  1317. }
  1318. }
  1319. }
  1320. kData->postRtEvents.trySplice();
  1321. if (frames > timeOffset)
  1322. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  1323. } // End of Event Input and Processing
  1324. // --------------------------------------------------------------------------------------------------------
  1325. // Plugin processing (no events)
  1326. else
  1327. {
  1328. processSingle(inBuffer, outBuffer, frames, 0);
  1329. } // End of Plugin processing (no events)
  1330. CARLA_PROCESS_CONTINUE_CHECK;
  1331. // --------------------------------------------------------------------------------------------------------
  1332. // Control and MIDI Output
  1333. if (fMidiOut.count > 0 || kData->event.portOut != nullptr)
  1334. {
  1335. float value, curValue;
  1336. for (k=0; k < kData->param.count; ++k)
  1337. {
  1338. if (kData->param.data[k].type != PARAMETER_OUTPUT)
  1339. continue;
  1340. curValue = fDescriptor->get_parameter_value(fHandle, k);
  1341. kData->param.ranges[k].fixValue(curValue);
  1342. if (kData->param.data[k].midiCC > 0)
  1343. {
  1344. value = kData->param.ranges[k].normalizeValue(curValue);
  1345. kData->event.portOut->writeControlEvent(0, kData->param.data[k].midiChannel, kEngineControlEventTypeParameter, kData->param.data[k].midiCC, value);
  1346. }
  1347. }
  1348. // reverse lookup MIDI events
  1349. for (k = (MAX_MIDI_EVENTS*2)-1; k >= fMidiEventCount; --k)
  1350. {
  1351. if (fMidiEvents[k].data[0] == 0)
  1352. break;
  1353. const uint8_t channel = MIDI_GET_CHANNEL_FROM_DATA(fMidiEvents[k].data);
  1354. const uint8_t port = fMidiEvents[k].port;
  1355. if (kData->event.portOut != nullptr)
  1356. kData->event.portOut->writeMidiEvent(fMidiEvents[k].time, channel, port, fMidiEvents[k].data, fMidiEvents[k].size);
  1357. else if (port < fMidiOut.count)
  1358. fMidiOut.ports[port]->writeMidiEvent(fMidiEvents[k].time, channel, port, fMidiEvents[k].data, fMidiEvents[k].size);
  1359. }
  1360. } // End of Control and MIDI Output
  1361. }
  1362. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  1363. {
  1364. CARLA_ASSERT(frames > 0);
  1365. if (frames == 0)
  1366. return false;
  1367. if (kData->audioIn.count > 0)
  1368. {
  1369. CARLA_ASSERT(inBuffer != nullptr);
  1370. if (inBuffer == nullptr)
  1371. return false;
  1372. }
  1373. if (kData->audioOut.count > 0)
  1374. {
  1375. CARLA_ASSERT(outBuffer != nullptr);
  1376. if (outBuffer == nullptr)
  1377. return false;
  1378. }
  1379. uint32_t i, k;
  1380. // --------------------------------------------------------------------------------------------------------
  1381. // Try lock, silence otherwise
  1382. if (kData->engine->isOffline())
  1383. {
  1384. kData->singleMutex.lock();
  1385. }
  1386. else if (! kData->singleMutex.tryLock())
  1387. {
  1388. for (i=0; i < kData->audioOut.count; ++i)
  1389. {
  1390. for (k=0; k < frames; ++k)
  1391. outBuffer[i][k+timeOffset] = 0.0f;
  1392. }
  1393. return false;
  1394. }
  1395. // --------------------------------------------------------------------------------------------------------
  1396. // Reset audio buffers
  1397. for (i=0; i < kData->audioIn.count; ++i)
  1398. carla_copyFloat(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  1399. for (i=0; i < kData->audioOut.count; ++i)
  1400. carla_zeroFloat(fAudioOutBuffers[i], frames);
  1401. // --------------------------------------------------------------------------------------------------------
  1402. // Run plugin
  1403. fIsProcessing = true;
  1404. if (fHandle2 == nullptr)
  1405. {
  1406. fDescriptor->process(fHandle, fAudioInBuffers, fAudioOutBuffers, frames, fMidiEventCount, fMidiEvents);
  1407. }
  1408. else
  1409. {
  1410. fDescriptor->process(fHandle,
  1411. (kData->audioIn.count > 0) ? &fAudioInBuffers[0] : nullptr,
  1412. (kData->audioOut.count > 0) ? &fAudioOutBuffers[0] : nullptr,
  1413. frames, fMidiEventCount, fMidiEvents);
  1414. fDescriptor->process(fHandle2,
  1415. (kData->audioIn.count > 0) ? &fAudioInBuffers[1] : nullptr,
  1416. (kData->audioOut.count > 0) ? &fAudioOutBuffers[1] : nullptr,
  1417. frames, fMidiEventCount, fMidiEvents);
  1418. }
  1419. fIsProcessing = false;
  1420. fTimeInfo.frame += frames;
  1421. #ifndef BUILD_BRIDGE
  1422. // --------------------------------------------------------------------------------------------------------
  1423. // Post-processing (dry/wet, volume and balance)
  1424. {
  1425. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) != 0 && kData->postProc.dryWet != 1.0f;
  1426. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) != 0 && (kData->postProc.balanceLeft != -1.0f || kData->postProc.balanceRight != 1.0f);
  1427. bool isPair;
  1428. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1429. for (i=0; i < kData->audioOut.count; ++i)
  1430. {
  1431. // Dry/Wet
  1432. if (doDryWet)
  1433. {
  1434. for (k=0; k < frames; ++k)
  1435. {
  1436. bufValue = fAudioInBuffers[(kData->audioIn.count == 1) ? 0 : i][k];
  1437. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * kData->postProc.dryWet) + (bufValue * (1.0f - kData->postProc.dryWet));
  1438. }
  1439. }
  1440. // Balance
  1441. if (doBalance)
  1442. {
  1443. isPair = (i % 2 == 0);
  1444. if (isPair)
  1445. {
  1446. CARLA_ASSERT(i+1 < kData->audioOut.count);
  1447. carla_copyFloat(oldBufLeft, fAudioOutBuffers[i], frames);
  1448. }
  1449. float balRangeL = (kData->postProc.balanceLeft + 1.0f)/2.0f;
  1450. float balRangeR = (kData->postProc.balanceRight + 1.0f)/2.0f;
  1451. for (k=0; k < frames; ++k)
  1452. {
  1453. if (isPair)
  1454. {
  1455. // left
  1456. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1457. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1458. }
  1459. else
  1460. {
  1461. // right
  1462. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1463. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1464. }
  1465. }
  1466. }
  1467. // Volume (and buffer copy)
  1468. {
  1469. for (k=0; k < frames; ++k)
  1470. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * kData->postProc.volume;
  1471. }
  1472. }
  1473. } // End of Post-processing
  1474. #else
  1475. for (i=0; i < kData->audioOut.count; ++i)
  1476. {
  1477. for (k=0; k < frames; ++k)
  1478. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1479. }
  1480. #endif
  1481. // --------------------------------------------------------------------------------------------------------
  1482. kData->singleMutex.unlock();
  1483. return true;
  1484. }
  1485. void bufferSizeChanged(const uint32_t newBufferSize) override
  1486. {
  1487. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1488. carla_debug("NativePlugin::bufferSizeChanged(%i)", newBufferSize);
  1489. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  1490. {
  1491. if (fAudioInBuffers[i] != nullptr)
  1492. delete[] fAudioInBuffers[i];
  1493. fAudioInBuffers[i] = new float[newBufferSize];
  1494. }
  1495. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  1496. {
  1497. if (fAudioOutBuffers[i] != nullptr)
  1498. delete[] fAudioOutBuffers[i];
  1499. fAudioOutBuffers[i] = new float[newBufferSize];
  1500. }
  1501. }
  1502. // -------------------------------------------------------------------
  1503. // Plugin buffers
  1504. void initBuffers() override
  1505. {
  1506. fMidiIn.initBuffers(kData->engine);
  1507. fMidiOut.initBuffers(kData->engine);
  1508. CarlaPlugin::initBuffers();
  1509. }
  1510. void clearBuffers() override
  1511. {
  1512. carla_debug("NativePlugin::clearBuffers() - start");
  1513. if (fAudioInBuffers != nullptr)
  1514. {
  1515. for (uint32_t i=0; i < kData->audioIn.count; ++i)
  1516. {
  1517. if (fAudioInBuffers[i] != nullptr)
  1518. {
  1519. delete[] fAudioInBuffers[i];
  1520. fAudioInBuffers[i] = nullptr;
  1521. }
  1522. }
  1523. delete[] fAudioInBuffers;
  1524. fAudioInBuffers = nullptr;
  1525. }
  1526. if (fAudioOutBuffers != nullptr)
  1527. {
  1528. for (uint32_t i=0; i < kData->audioOut.count; ++i)
  1529. {
  1530. if (fAudioOutBuffers[i] != nullptr)
  1531. {
  1532. delete[] fAudioOutBuffers[i];
  1533. fAudioOutBuffers[i] = nullptr;
  1534. }
  1535. }
  1536. delete[] fAudioOutBuffers;
  1537. fAudioOutBuffers = nullptr;
  1538. }
  1539. fMidiIn.clear();
  1540. fMidiOut.clear();
  1541. CarlaPlugin::clearBuffers();
  1542. carla_debug("NativePlugin::clearBuffers() - end");
  1543. }
  1544. // -------------------------------------------------------------------
  1545. // Post-poned UI Stuff
  1546. void uiParameterChange(const uint32_t index, const float value) override
  1547. {
  1548. CARLA_ASSERT(fDescriptor != nullptr);
  1549. CARLA_ASSERT(fHandle != nullptr);
  1550. CARLA_ASSERT(index < kData->param.count);
  1551. if (! fIsUiVisible)
  1552. return;
  1553. if (fDescriptor == nullptr || fHandle == nullptr)
  1554. return;
  1555. if (index >= kData->param.count)
  1556. return;
  1557. if (fDescriptor->ui_set_parameter_value != nullptr)
  1558. fDescriptor->ui_set_parameter_value(fHandle, index, value);
  1559. }
  1560. void uiMidiProgramChange(const uint32_t index) override
  1561. {
  1562. CARLA_ASSERT(fDescriptor != nullptr);
  1563. CARLA_ASSERT(fHandle != nullptr);
  1564. CARLA_ASSERT(index < kData->midiprog.count);
  1565. if (! fIsUiVisible)
  1566. return;
  1567. if (fDescriptor == nullptr || fHandle == nullptr)
  1568. return;
  1569. if (index >= kData->midiprog.count)
  1570. return;
  1571. if (fDescriptor->ui_set_midi_program != nullptr) // TODO
  1572. fDescriptor->ui_set_midi_program(fHandle, 0, kData->midiprog.data[index].bank, kData->midiprog.data[index].program);
  1573. }
  1574. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) override
  1575. {
  1576. CARLA_ASSERT(fDescriptor != nullptr);
  1577. CARLA_ASSERT(fHandle != nullptr);
  1578. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1579. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1580. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  1581. if (! fIsUiVisible)
  1582. return;
  1583. if (fDescriptor == nullptr || fHandle == nullptr)
  1584. return;
  1585. if (channel >= MAX_MIDI_CHANNELS)
  1586. return;
  1587. if (note >= MAX_MIDI_NOTE)
  1588. return;
  1589. if (velo >= MAX_MIDI_VALUE)
  1590. return;
  1591. // TODO
  1592. }
  1593. void uiNoteOff(const uint8_t channel, const uint8_t note) override
  1594. {
  1595. CARLA_ASSERT(fDescriptor != nullptr);
  1596. CARLA_ASSERT(fHandle != nullptr);
  1597. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1598. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1599. if (! fIsUiVisible)
  1600. return;
  1601. if (fDescriptor == nullptr || fHandle == nullptr)
  1602. return;
  1603. if (channel >= MAX_MIDI_CHANNELS)
  1604. return;
  1605. if (note >= MAX_MIDI_NOTE)
  1606. return;
  1607. // TODO
  1608. }
  1609. // -------------------------------------------------------------------
  1610. protected:
  1611. uint32_t handleGetBufferSize()
  1612. {
  1613. return kData->engine->getBufferSize();
  1614. }
  1615. double handleGetSampleRate()
  1616. {
  1617. return kData->engine->getSampleRate();
  1618. }
  1619. const ::TimeInfo* handleGetTimeInfo()
  1620. {
  1621. CARLA_ASSERT(fIsProcessing);
  1622. return &fTimeInfo;
  1623. }
  1624. bool handleWriteMidiEvent(const ::MidiEvent* const event)
  1625. {
  1626. CARLA_ASSERT(fEnabled);
  1627. CARLA_ASSERT(fIsProcessing);
  1628. CARLA_ASSERT(fMidiOut.count > 0 || kData->event.portOut != nullptr);
  1629. CARLA_ASSERT(event != nullptr);
  1630. CARLA_ASSERT(event->data[0] != 0);
  1631. if (! fEnabled)
  1632. return false;
  1633. if (fMidiOut.count == 0)
  1634. return false;
  1635. if (event == nullptr)
  1636. return false;
  1637. if (event->data[0] == 0)
  1638. return false;
  1639. if (! fIsProcessing)
  1640. {
  1641. carla_stderr2("NativePlugin::handleWriteMidiEvent(%p) - received MIDI out event outside audio thread, ignoring", event);
  1642. return false;
  1643. }
  1644. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1645. return false;
  1646. // reverse-find first free event, and put it there
  1647. for (uint32_t i=(MAX_MIDI_EVENTS*2)-1; i >= fMidiEventCount; --i)
  1648. {
  1649. if (fMidiEvents[i].data[0] == 0)
  1650. {
  1651. std::memcpy(&fMidiEvents[i], event, sizeof(::MidiEvent));
  1652. break;
  1653. }
  1654. }
  1655. return true;
  1656. }
  1657. void handleUiParameterChanged(const uint32_t index, const float value)
  1658. {
  1659. setParameterValue(index, value, false, true, true);
  1660. }
  1661. void handleUiCustomDataChanged(const char* const key, const char* const value)
  1662. {
  1663. setCustomData(CUSTOM_DATA_STRING, key, value, false);
  1664. }
  1665. void handleUiClosed()
  1666. {
  1667. kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  1668. fIsUiVisible = false;
  1669. }
  1670. const char* handleUiOpenFile(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::getOpenFileName(nullptr, title, "", filter, nullptr, options).toUtf8().constData();
  1675. return retStr.isNotEmpty() ? (const char*)retStr : nullptr;
  1676. }
  1677. const char* handleUiSaveFile(const bool isDir, const char* const title, const char* const filter)
  1678. {
  1679. static CarlaString retStr;
  1680. QFileDialog::Options options(isDir ? QFileDialog::ShowDirsOnly : 0x0);
  1681. retStr = QFileDialog::getSaveFileName(nullptr, title, "", filter, nullptr, options).toUtf8().constData();
  1682. return retStr.isNotEmpty() ? (const char*)retStr : nullptr;
  1683. }
  1684. intptr_t handleDispatcher(HostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr)
  1685. {
  1686. intptr_t ret = 0;
  1687. switch (opcode)
  1688. {
  1689. case HOST_OPCODE_NULL:
  1690. break;
  1691. case HOST_OPCODE_SET_PROCESS_PRECISION:
  1692. // TODO
  1693. break;
  1694. case HOST_OPCODE_UI_UNAVAILABLE:
  1695. kData->engine->callback(CALLBACK_SHOW_GUI, fId, -1, 0, 0.0f, nullptr);
  1696. break;
  1697. }
  1698. return ret;
  1699. // unused for now
  1700. (void)index;
  1701. (void)value;
  1702. (void)ptr;
  1703. }
  1704. // -------------------------------------------------------------------
  1705. public:
  1706. static size_t getPluginCount()
  1707. {
  1708. return sPluginDescriptors.count();
  1709. }
  1710. static const PluginDescriptor* getPluginDescriptor(const size_t index)
  1711. {
  1712. CARLA_ASSERT(index < sPluginDescriptors.count());
  1713. if (index < sPluginDescriptors.count())
  1714. return sPluginDescriptors.getAt(index);
  1715. return nullptr;
  1716. }
  1717. static void registerPlugin(const PluginDescriptor* desc)
  1718. {
  1719. sPluginDescriptors.append(desc);
  1720. }
  1721. // -------------------------------------------------------------------
  1722. bool init(const char* const name, const char* const label)
  1723. {
  1724. CARLA_ASSERT(kData->engine != nullptr);
  1725. CARLA_ASSERT(kData->client == nullptr);
  1726. CARLA_ASSERT(label != nullptr);
  1727. // ---------------------------------------------------------------
  1728. // first checks
  1729. if (kData->engine == nullptr)
  1730. {
  1731. return false;
  1732. }
  1733. if (kData->client != nullptr)
  1734. {
  1735. kData->engine->setLastError("Plugin client is already registered");
  1736. return false;
  1737. }
  1738. if (label == nullptr)
  1739. {
  1740. kData->engine->setLastError("null label");
  1741. return false;
  1742. }
  1743. // ---------------------------------------------------------------
  1744. // get descriptor that matches label
  1745. for (NonRtList<const PluginDescriptor*>::Itenerator it = sPluginDescriptors.begin(); it.valid(); it.next())
  1746. {
  1747. fDescriptor = *it;
  1748. CARLA_ASSERT(fDescriptor != nullptr);
  1749. if (fDescriptor == nullptr)
  1750. break;
  1751. if (fDescriptor->label != nullptr && std::strcmp(fDescriptor->label, label) == 0)
  1752. break;
  1753. fDescriptor = nullptr;
  1754. }
  1755. if (fDescriptor == nullptr)
  1756. {
  1757. kData->engine->setLastError("Invalid internal plugin");
  1758. return false;
  1759. }
  1760. // ---------------------------------------------------------------
  1761. // set icon
  1762. if (std::strcmp(fDescriptor->label, "audiofile") == 0)
  1763. fIconName = "file";
  1764. else if (std::strcmp(fDescriptor->label, "midifile") == 0)
  1765. fIconName = "file";
  1766. else if (std::strcmp(fDescriptor->label, "sunvoxfile") == 0)
  1767. fIconName = "file";
  1768. else if (std::strcmp(fDescriptor->label, "3BandEQ") == 0)
  1769. fIconName = "distrho";
  1770. else if (std::strcmp(fDescriptor->label, "3BandSplitter") == 0)
  1771. fIconName = "distrho";
  1772. else if (std::strcmp(fDescriptor->label, "Nekobi") == 0)
  1773. fIconName = "distrho";
  1774. else if (std::strcmp(fDescriptor->label, "Notes") == 0)
  1775. fIconName = "distrho";
  1776. else if (std::strcmp(fDescriptor->label, "PingPongPan") == 0)
  1777. fIconName = "distrho";
  1778. else if (std::strcmp(fDescriptor->label, "StereoEnhancer") == 0)
  1779. fIconName = "distrho";
  1780. // ---------------------------------------------------------------
  1781. // get info
  1782. if (name != nullptr)
  1783. fName = kData->engine->getUniquePluginName(name);
  1784. else if (fDescriptor->name != nullptr)
  1785. fName = kData->engine->getUniquePluginName(fDescriptor->name);
  1786. else
  1787. fName = kData->engine->getUniquePluginName(label);
  1788. {
  1789. CARLA_ASSERT(fHost.ui_name == nullptr);
  1790. char uiName[fName.length()+6+1];
  1791. std::strcpy(uiName, (const char*)fName);
  1792. std::strcat(uiName, " (GUI)");
  1793. fHost.ui_name = carla_strdup(uiName);
  1794. }
  1795. // ---------------------------------------------------------------
  1796. // register client
  1797. kData->client = kData->engine->addClient(this);
  1798. if (kData->client == nullptr || ! kData->client->isOk())
  1799. {
  1800. kData->engine->setLastError("Failed to register plugin client");
  1801. return false;
  1802. }
  1803. // ---------------------------------------------------------------
  1804. // initialize plugin
  1805. fHandle = fDescriptor->instantiate(fDescriptor, &fHost);
  1806. if (fHandle == nullptr)
  1807. {
  1808. kData->engine->setLastError("Plugin failed to initialize");
  1809. return false;
  1810. }
  1811. // ---------------------------------------------------------------
  1812. // load plugin settings
  1813. {
  1814. // set default options
  1815. fOptions = 0x0;
  1816. fOptions |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1817. if (midiInCount() > 0)
  1818. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  1819. if (kData->engine->getOptions().forceStereo)
  1820. fOptions |= PLUGIN_OPTION_FORCE_STEREO;
  1821. if (fDescriptor->midiIns > 0)
  1822. {
  1823. fOptions |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1824. fOptions |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1825. fOptions |= PLUGIN_OPTION_SEND_PITCHBEND;
  1826. fOptions |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1827. }
  1828. // load settings
  1829. kData->idStr = "Native/";
  1830. kData->idStr += label;
  1831. fOptions = kData->loadSettings(fOptions, availableOptions());
  1832. // ignore settings, we need this anyway
  1833. if (midiInCount() > 0)
  1834. fOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  1835. }
  1836. return true;
  1837. }
  1838. class ScopedInitializer
  1839. {
  1840. public:
  1841. ScopedInitializer()
  1842. {
  1843. carla_register_all_plugins();
  1844. }
  1845. ~ScopedInitializer()
  1846. {
  1847. sPluginDescriptors.clear();
  1848. }
  1849. };
  1850. private:
  1851. PluginHandle fHandle;
  1852. PluginHandle fHandle2;
  1853. HostDescriptor fHost;
  1854. const PluginDescriptor* fDescriptor;
  1855. bool fIsProcessing;
  1856. bool fIsUiVisible;
  1857. float** fAudioInBuffers;
  1858. float** fAudioOutBuffers;
  1859. uint32_t fMidiEventCount;
  1860. ::MidiEvent fMidiEvents[MAX_MIDI_EVENTS*2];
  1861. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  1862. NativePluginMidiData fMidiIn;
  1863. NativePluginMidiData fMidiOut;
  1864. ::TimeInfo fTimeInfo;
  1865. static NonRtList<const PluginDescriptor*> sPluginDescriptors;
  1866. // -------------------------------------------------------------------
  1867. #define handlePtr ((NativePlugin*)handle)
  1868. static uint32_t carla_host_get_buffer_size(HostHandle handle)
  1869. {
  1870. return handlePtr->handleGetBufferSize();
  1871. }
  1872. static double carla_host_get_sample_rate(HostHandle handle)
  1873. {
  1874. return handlePtr->handleGetSampleRate();
  1875. }
  1876. static const ::TimeInfo* carla_host_get_time_info(HostHandle handle)
  1877. {
  1878. return handlePtr->handleGetTimeInfo();
  1879. }
  1880. static bool carla_host_write_midi_event(HostHandle handle, const ::MidiEvent* event)
  1881. {
  1882. return handlePtr->handleWriteMidiEvent(event);
  1883. }
  1884. static void carla_host_ui_parameter_changed(HostHandle handle, uint32_t index, float value)
  1885. {
  1886. handlePtr->handleUiParameterChanged(index, value);
  1887. }
  1888. static void carla_host_ui_custom_data_changed(HostHandle handle, const char* key, const char* value)
  1889. {
  1890. handlePtr->handleUiCustomDataChanged(key, value);
  1891. }
  1892. static void carla_host_ui_closed(HostHandle handle)
  1893. {
  1894. handlePtr->handleUiClosed();
  1895. }
  1896. static const char* carla_host_ui_open_file(HostHandle handle, bool isDir, const char* title, const char* filter)
  1897. {
  1898. return handlePtr->handleUiOpenFile(isDir, title, filter);
  1899. }
  1900. static const char* carla_host_ui_save_file(HostHandle handle, bool isDir, const char* title, const char* filter)
  1901. {
  1902. return handlePtr->handleUiSaveFile(isDir, title, filter);
  1903. }
  1904. static intptr_t carla_host_dispatcher(HostHandle handle, HostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr)
  1905. {
  1906. return handlePtr->handleDispatcher(opcode, index, value, ptr);
  1907. }
  1908. #undef handlePtr
  1909. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  1910. };
  1911. NonRtList<const PluginDescriptor*> NativePlugin::sPluginDescriptors;
  1912. static const NativePlugin::ScopedInitializer _si;
  1913. CARLA_BACKEND_END_NAMESPACE
  1914. void carla_register_native_plugin(const PluginDescriptor* desc)
  1915. {
  1916. CARLA_BACKEND_USE_NAMESPACE
  1917. NativePlugin::registerPlugin(desc);
  1918. }
  1919. #else // WANT_NATIVE
  1920. # warning Building without Internal plugin support
  1921. #endif
  1922. CARLA_BACKEND_START_NAMESPACE
  1923. // -----------------------------------------------------------------------
  1924. #ifdef WANT_NATIVE
  1925. size_t CarlaPlugin::getNativePluginCount()
  1926. {
  1927. return NativePlugin::getPluginCount();
  1928. }
  1929. const PluginDescriptor* CarlaPlugin::getNativePluginDescriptor(const size_t index)
  1930. {
  1931. return NativePlugin::getPluginDescriptor(index);
  1932. }
  1933. #endif
  1934. // -----------------------------------------------------------------------
  1935. CarlaPlugin* CarlaPlugin::newNative(const Initializer& init)
  1936. {
  1937. carla_debug("CarlaPlugin::newNative({%p, \"%s\", \"%s\", \"%s\"})", init.engine, init.filename, init.name, init.label);
  1938. #ifdef WANT_NATIVE
  1939. NativePlugin* const plugin(new NativePlugin(init.engine, init.id));
  1940. if (! plugin->init(init.name, init.label))
  1941. {
  1942. delete plugin;
  1943. return nullptr;
  1944. }
  1945. plugin->reload();
  1946. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && ! CarlaPluginProtectedData::canRunInRack(plugin))
  1947. {
  1948. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo Internal plugins, sorry!");
  1949. delete plugin;
  1950. return nullptr;
  1951. }
  1952. return plugin;
  1953. #else
  1954. init.engine->setLastError("Internal plugins support not available");
  1955. return nullptr;
  1956. #endif
  1957. }
  1958. CARLA_BACKEND_END_NAMESPACE