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.

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