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.

2427 lines
79KB

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