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.

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