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.

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