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.

2013 lines
65KB

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