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.

2257 lines
73KB

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