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.

2392 lines
78KB

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