Collection of tools useful for audio production
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.

1737 lines
54KB

  1. /*
  2. * Carla Backend
  3. * Copyright (C) 2012 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * 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 COPYING file
  16. */
  17. #include "carla_plugin.hpp"
  18. #include "plugins/carla_native.h"
  19. // Internal C plugins
  20. extern "C" {
  21. extern void carla_register_native_plugin_bypass();
  22. }
  23. // Internal C++ plugins
  24. extern void carla_register_native_plugin_midiSplit();
  25. #ifdef WANT_ZYNADDSUBFX
  26. extern void carla_register_native_plugin_zynAddSubFx();
  27. #endif
  28. CARLA_BACKEND_START_NAMESPACE
  29. struct NativePluginMidiData {
  30. uint32_t count;
  31. uint32_t* rindexes;
  32. CarlaEngineMidiPort** ports;
  33. NativePluginMidiData()
  34. : count(0),
  35. rindexes(nullptr),
  36. ports(nullptr) {}
  37. };
  38. class NativePluginScopedInitiliazer
  39. {
  40. public:
  41. NativePluginScopedInitiliazer()
  42. {
  43. firstInit = true;
  44. }
  45. ~NativePluginScopedInitiliazer()
  46. {
  47. for (size_t i=0; i < descriptors.size(); i++)
  48. {
  49. const PluginDescriptor* const desc = descriptors[i];
  50. if (desc->_fini)
  51. desc->_fini((struct _PluginDescriptor*)desc);
  52. }
  53. descriptors.clear();
  54. }
  55. void maybeFirstInit()
  56. {
  57. if (firstInit)
  58. {
  59. firstInit = false;
  60. carla_register_native_plugin_bypass();
  61. carla_register_native_plugin_midiSplit();
  62. #ifdef WANT_ZYNADDSUBFX
  63. carla_register_native_plugin_zynAddSubFx();
  64. #endif
  65. }
  66. }
  67. void initializeIfNeeded(const PluginDescriptor* const desc)
  68. {
  69. if (descriptors.empty() || std::find(descriptors.begin(), descriptors.end(), desc) == descriptors.end())
  70. {
  71. if (desc->_init)
  72. desc->_init((struct _PluginDescriptor*)desc);
  73. descriptors.push_back(desc);
  74. }
  75. }
  76. private:
  77. bool firstInit;
  78. std::vector<const PluginDescriptor*> descriptors;
  79. };
  80. static NativePluginScopedInitiliazer scopedInitliazer;
  81. class NativePlugin : public CarlaPlugin
  82. {
  83. public:
  84. NativePlugin(CarlaEngine* const engine, const unsigned short id)
  85. : CarlaPlugin(engine, id)
  86. {
  87. qDebug("NativePlugin::NativePlugin()");
  88. m_type = PLUGIN_INTERNAL;
  89. descriptor = nullptr;
  90. handle = h2 = nullptr;
  91. host.handle = this;
  92. host.get_buffer_size = carla_host_get_buffer_size;
  93. host.get_sample_rate = carla_host_get_sample_rate;
  94. host.get_time_info = carla_host_get_time_info;
  95. host.write_midi_event = carla_host_write_midi_event;
  96. isProcessing = false;
  97. midiEventCount = 0;
  98. memset(midiEvents, 0, sizeof(::MidiEvent) * MAX_MIDI_EVENTS * 2);
  99. }
  100. ~NativePlugin()
  101. {
  102. qDebug("NativePlugin::~NativePlugin()");
  103. if (descriptor)
  104. {
  105. if (descriptor->deactivate && m_activeBefore)
  106. {
  107. if (handle)
  108. descriptor->deactivate(handle);
  109. if (h2)
  110. descriptor->deactivate(h2);
  111. }
  112. if (descriptor->cleanup)
  113. {
  114. if (handle)
  115. descriptor->cleanup(handle);
  116. if (h2)
  117. descriptor->cleanup(h2);
  118. }
  119. }
  120. }
  121. // -------------------------------------------------------------------
  122. // Information (base)
  123. PluginCategory category()
  124. {
  125. CARLA_ASSERT(descriptor);
  126. if (descriptor)
  127. return (PluginCategory)descriptor->category;
  128. return getPluginCategoryFromName(m_name);
  129. }
  130. // -------------------------------------------------------------------
  131. // Information (count)
  132. uint32_t midiInCount()
  133. {
  134. return mIn.count;
  135. }
  136. uint32_t midiOutCount()
  137. {
  138. return mOut.count;
  139. }
  140. uint32_t parameterScalePointCount(const uint32_t parameterId)
  141. {
  142. CARLA_ASSERT(descriptor);
  143. CARLA_ASSERT(parameterId < param.count);
  144. int32_t rindex = param.data[parameterId].rindex;
  145. if (descriptor && rindex < (int32_t)descriptor->portCount)
  146. {
  147. const PluginPort* const port = &descriptor->ports[rindex];
  148. if (port)
  149. return port->scalePointCount;
  150. }
  151. return 0;
  152. }
  153. // -------------------------------------------------------------------
  154. // Information (per-plugin data)
  155. double getParameterValue(const uint32_t parameterId)
  156. {
  157. CARLA_ASSERT(descriptor);
  158. CARLA_ASSERT(handle);
  159. CARLA_ASSERT(parameterId < param.count);
  160. if (descriptor && handle)
  161. return descriptor->get_parameter_value(handle, parameterId);
  162. return 0.0;
  163. }
  164. double getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId)
  165. {
  166. CARLA_ASSERT(descriptor);
  167. CARLA_ASSERT(parameterId < param.count);
  168. CARLA_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  169. const int32_t rindex = param.data[parameterId].rindex;
  170. if (descriptor && rindex < (int32_t)descriptor->portCount)
  171. {
  172. const PluginPort* const port = &descriptor->ports[rindex];
  173. if (port && scalePointId < port->scalePointCount)
  174. {
  175. const PluginPortScalePoint* const scalePoint = &port->scalePoints[scalePointId];
  176. if (scalePoint)
  177. return scalePoint->value;
  178. }
  179. }
  180. return 0.0;
  181. }
  182. void getLabel(char* const strBuf)
  183. {
  184. CARLA_ASSERT(descriptor);
  185. if (descriptor && descriptor->label)
  186. strncpy(strBuf, descriptor->label, STR_MAX);
  187. else
  188. CarlaPlugin::getLabel(strBuf);
  189. }
  190. void getMaker(char* const strBuf)
  191. {
  192. CARLA_ASSERT(descriptor);
  193. if (descriptor && descriptor->maker)
  194. strncpy(strBuf, descriptor->maker, STR_MAX);
  195. else
  196. CarlaPlugin::getMaker(strBuf);
  197. }
  198. void getCopyright(char* const strBuf)
  199. {
  200. CARLA_ASSERT(descriptor);
  201. if (descriptor && descriptor->copyright)
  202. strncpy(strBuf, descriptor->copyright, STR_MAX);
  203. else
  204. CarlaPlugin::getCopyright(strBuf);
  205. }
  206. void getRealName(char* const strBuf)
  207. {
  208. CARLA_ASSERT(descriptor);
  209. if (descriptor && descriptor->name)
  210. strncpy(strBuf, descriptor->name, STR_MAX);
  211. else
  212. CarlaPlugin::getRealName(strBuf);
  213. }
  214. void getParameterName(const uint32_t parameterId, char* const strBuf)
  215. {
  216. CARLA_ASSERT(descriptor);
  217. CARLA_ASSERT(parameterId < param.count);
  218. const int32_t rindex = param.data[parameterId].rindex;
  219. if (descriptor && rindex < (int32_t)descriptor->portCount)
  220. {
  221. const PluginPort* const port = &descriptor->ports[rindex];
  222. if (port && port->name)
  223. {
  224. strncpy(strBuf, port->name, STR_MAX);
  225. return;
  226. }
  227. }
  228. CarlaPlugin::getParameterName(parameterId, strBuf);
  229. }
  230. void getParameterText(const uint32_t parameterId, char* const strBuf)
  231. {
  232. CARLA_ASSERT(descriptor);
  233. CARLA_ASSERT(handle);
  234. CARLA_ASSERT(parameterId < param.count);
  235. if (descriptor && handle)
  236. {
  237. const int32_t rindex = param.data[parameterId].rindex;
  238. const char* const text = descriptor->get_parameter_text(handle, rindex);
  239. if (text)
  240. {
  241. strncpy(strBuf, text, STR_MAX);
  242. return;
  243. }
  244. }
  245. CarlaPlugin::getParameterText(parameterId, strBuf);
  246. }
  247. void getParameterUnit(const uint32_t parameterId, char* const strBuf)
  248. {
  249. CARLA_ASSERT(descriptor);
  250. CARLA_ASSERT(handle);
  251. CARLA_ASSERT(parameterId < param.count);
  252. if (descriptor && handle)
  253. {
  254. const int32_t rindex = param.data[parameterId].rindex;
  255. const char* const unit = descriptor->get_parameter_unit(handle, rindex);
  256. if (unit)
  257. {
  258. strncpy(strBuf, unit, STR_MAX);
  259. return;
  260. }
  261. }
  262. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  263. }
  264. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf)
  265. {
  266. CARLA_ASSERT(descriptor);
  267. CARLA_ASSERT(parameterId < param.count);
  268. CARLA_ASSERT(scalePointId < parameterScalePointCount(parameterId));
  269. int32_t rindex = param.data[parameterId].rindex;
  270. if (descriptor && rindex < (int32_t)descriptor->portCount)
  271. {
  272. const PluginPort* const port = &descriptor->ports[rindex];
  273. if (port && scalePointId < port->scalePointCount)
  274. {
  275. const PluginPortScalePoint* const scalePoint = &port->scalePoints[scalePointId];
  276. if (scalePoint && scalePoint->label)
  277. {
  278. strncpy(strBuf, scalePoint->label, STR_MAX);
  279. return;
  280. }
  281. }
  282. }
  283. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  284. }
  285. // -------------------------------------------------------------------
  286. // Set data (plugin-specific stuff)
  287. void setParameterValue(const uint32_t parameterId, double value, const bool sendGui, const bool sendOsc, const bool sendCallback)
  288. {
  289. CARLA_ASSERT(descriptor);
  290. CARLA_ASSERT(handle);
  291. CARLA_ASSERT(parameterId < param.count);
  292. if (descriptor)
  293. {
  294. fixParameterValue(value, param.ranges[parameterId]);
  295. descriptor->set_parameter_value(handle, param.data[parameterId].rindex, value);
  296. if (h2) descriptor->set_parameter_value(h2, param.data[parameterId].rindex, value);
  297. }
  298. CarlaPlugin::setParameterValue(parameterId, value, sendGui, sendOsc, sendCallback);
  299. }
  300. void setCustomData(const CustomDataType type, const char* const key, const char* const value, const bool sendGui)
  301. {
  302. CARLA_ASSERT(descriptor);
  303. CARLA_ASSERT(handle);
  304. CARLA_ASSERT(type == CUSTOM_DATA_STRING);
  305. CARLA_ASSERT(key);
  306. CARLA_ASSERT(value);
  307. if (type != CUSTOM_DATA_STRING)
  308. return qCritical("NativePlugin::setCustomData(%s, \"%s\", \"%s\", %s) - type is not string", CustomDataType2str(type), key, value, bool2str(sendGui));
  309. if (! key)
  310. return qCritical("NativePlugin::setCustomData(%s, \"%s\", \"%s\", %s) - key is null", CustomDataType2str(type), key, value, bool2str(sendGui));
  311. if (! value)
  312. return qCritical("Nativelugin::setCustomData(%s, \"%s\", \"%s\", %s) - value is null", CustomDataType2str(type), key, value, bool2str(sendGui));
  313. if (descriptor)
  314. {
  315. descriptor->set_custom_data(handle, key, value);
  316. if (h2) descriptor->set_custom_data(h2, key, value);
  317. }
  318. CarlaPlugin::setCustomData(type, key, value, sendGui);
  319. }
  320. void setMidiProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback, const bool block)
  321. {
  322. CARLA_ASSERT(descriptor);
  323. CARLA_ASSERT(handle);
  324. CARLA_ASSERT(index >= -1 && index < (int32_t)midiprog.count);
  325. if (index < -1)
  326. index = -1;
  327. else if (index > (int32_t)midiprog.count)
  328. return;
  329. if (descriptor && handle && index >= 0)
  330. {
  331. if (x_engine->isOffline())
  332. {
  333. const CarlaEngine::ScopedLocker m(x_engine, block);
  334. descriptor->set_midi_program(handle, midiprog.data[index].bank, midiprog.data[index].program);
  335. if (h2) descriptor->set_midi_program(h2, midiprog.data[index].bank, midiprog.data[index].program);
  336. }
  337. else
  338. {
  339. const ScopedDisabler m(this, block);
  340. descriptor->set_midi_program(handle, midiprog.data[index].bank, midiprog.data[index].program);
  341. if (h2) descriptor->set_midi_program(h2, midiprog.data[index].bank, midiprog.data[index].program);
  342. }
  343. }
  344. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback, block);
  345. }
  346. // -------------------------------------------------------------------
  347. // Set gui stuff
  348. void showGui(const bool yesNo)
  349. {
  350. CARLA_ASSERT(descriptor);
  351. if (descriptor && handle)
  352. descriptor->show_gui(handle, yesNo);
  353. }
  354. void idleGui()
  355. {
  356. // FIXME - this should not be called if there's no GUI!
  357. CARLA_ASSERT(descriptor);
  358. if (descriptor && descriptor->idle_gui && handle)
  359. descriptor->idle_gui(handle);
  360. }
  361. // -------------------------------------------------------------------
  362. // Plugin state
  363. void reload()
  364. {
  365. qDebug("NativePlugin::reload() - start");
  366. CARLA_ASSERT(descriptor);
  367. // Safely disable plugin for reload
  368. const ScopedDisabler m(this);
  369. if (x_client->isActive())
  370. x_client->deactivate();
  371. // Remove client ports
  372. removeClientPorts();
  373. // Delete old data
  374. deleteBuffers();
  375. uint32_t aIns, aOuts, mIns, mOuts, params, j;
  376. aIns = aOuts = mIns = mOuts = params = 0;
  377. const double sampleRate = x_engine->getSampleRate();
  378. const uint32_t portCount = descriptor->portCount;
  379. bool forcedStereoIn, forcedStereoOut;
  380. forcedStereoIn = forcedStereoOut = false;
  381. for (uint32_t i=0; i < portCount; i++)
  382. {
  383. const PortType portType = descriptor->ports[i].type;
  384. const uint32_t portHints = descriptor->ports[i].hints;
  385. if (portType == PORT_TYPE_AUDIO)
  386. {
  387. if (portHints & PORT_HINT_IS_OUTPUT)
  388. aOuts += 1;
  389. else
  390. aIns += 1;
  391. }
  392. else if (portType == PORT_TYPE_MIDI)
  393. {
  394. if (portHints & PORT_HINT_IS_OUTPUT)
  395. mOuts += 1;
  396. else
  397. mIns += 1;
  398. }
  399. else if (portType == PORT_TYPE_PARAMETER)
  400. params += 1;
  401. }
  402. if (x_engine->options.forceStereo && (aIns == 1 || aOuts == 1) && mIns <= 1 && mOuts <= 1 && ! h2)
  403. {
  404. h2 = descriptor->instantiate((struct _PluginDescriptor*)descriptor, &host);
  405. if (aIns == 1)
  406. {
  407. aIns = 2;
  408. forcedStereoIn = true;
  409. }
  410. if (aOuts == 1)
  411. {
  412. aOuts = 2;
  413. forcedStereoOut = true;
  414. }
  415. }
  416. if (aIns > 0)
  417. {
  418. aIn.ports = new CarlaEngineAudioPort*[aIns];
  419. aIn.rindexes = new uint32_t[aIns];
  420. }
  421. if (aOuts > 0)
  422. {
  423. aOut.ports = new CarlaEngineAudioPort*[aOuts];
  424. aOut.rindexes = new uint32_t[aOuts];
  425. }
  426. if (mIns > 0)
  427. {
  428. mIn.ports = new CarlaEngineMidiPort*[mIns];
  429. mIn.rindexes = new uint32_t[mIns];
  430. }
  431. if (mOuts > 0)
  432. {
  433. mOut.ports = new CarlaEngineMidiPort*[mOuts];
  434. mOut.rindexes = new uint32_t[mOuts];
  435. }
  436. if (params > 0)
  437. {
  438. param.data = new ParameterData[params];
  439. param.ranges = new ParameterRanges[params];
  440. }
  441. const int portNameSize = CarlaEngine::maxPortNameSize() - 2;
  442. char portName[portNameSize];
  443. bool needsCtrlIn = false;
  444. bool needsCtrlOut = false;
  445. for (uint32_t i=0; i < portCount; i++)
  446. {
  447. const PortType portType = descriptor->ports[i].type;
  448. const uint32_t portHints = descriptor->ports[i].hints;
  449. if (portType == PORT_TYPE_AUDIO || portType == PORT_TYPE_MIDI)
  450. {
  451. if (CarlaEngine::processMode != PROCESS_MODE_MULTIPLE_CLIENTS)
  452. {
  453. strcpy(portName, m_name);
  454. strcat(portName, ":");
  455. strncat(portName, descriptor->ports[i].name, portNameSize/2);
  456. }
  457. else
  458. strncpy(portName, descriptor->ports[i].name, portNameSize);
  459. }
  460. if (portType == PORT_TYPE_AUDIO)
  461. {
  462. if (portHints & PORT_HINT_IS_OUTPUT)
  463. {
  464. j = aOut.count++;
  465. aOut.ports[j] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, false);
  466. aOut.rindexes[j] = i;
  467. needsCtrlIn = true;
  468. if (forcedStereoOut)
  469. {
  470. strcat(portName, "_");
  471. aOut.ports[1] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, false);
  472. aOut.rindexes[1] = i;
  473. }
  474. }
  475. else
  476. {
  477. j = aIn.count++;
  478. aIn.ports[j] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, true);
  479. aIn.rindexes[j] = i;
  480. if (forcedStereoIn)
  481. {
  482. strcat(portName, "_");
  483. aIn.ports[1] = (CarlaEngineAudioPort*)x_client->addPort(CarlaEnginePortTypeAudio, portName, true);
  484. aIn.rindexes[1] = i;
  485. }
  486. }
  487. }
  488. else if (portType == PORT_TYPE_MIDI)
  489. {
  490. if (portHints & PORT_HINT_IS_OUTPUT)
  491. {
  492. j = mOut.count++;
  493. mOut.ports[j] = (CarlaEngineMidiPort*)x_client->addPort(CarlaEnginePortTypeMIDI, portName, false);
  494. mOut.rindexes[j] = i;
  495. }
  496. else
  497. {
  498. j = mIn.count++;
  499. mIn.ports[j] = (CarlaEngineMidiPort*)x_client->addPort(CarlaEnginePortTypeMIDI, portName, true);
  500. mIn.rindexes[j] = i;
  501. }
  502. }
  503. else if (portType == PORT_TYPE_PARAMETER)
  504. {
  505. j = param.count++;
  506. param.data[j].index = j;
  507. param.data[j].rindex = i;
  508. param.data[j].hints = 0;
  509. param.data[j].midiChannel = 0;
  510. param.data[j].midiCC = -1;
  511. double min, max, def, step, stepSmall, stepLarge;
  512. ::ParameterRanges ranges = { 0.0, 0.0, 1.0, 0.01, 0.0001, 0.1 };
  513. descriptor->get_parameter_ranges(handle, i, &ranges);
  514. // min value
  515. min = ranges.min;
  516. // max value
  517. max = ranges.max;
  518. if (min > max)
  519. max = min;
  520. else if (max < min)
  521. min = max;
  522. if (max - min == 0.0)
  523. {
  524. qWarning("Broken plugin parameter: max - min == 0");
  525. max = min + 0.1;
  526. }
  527. // default value
  528. def = ranges.def;
  529. if (def < min)
  530. def = min;
  531. else if (def > max)
  532. def = max;
  533. if (portHints & PORT_HINT_USES_SAMPLE_RATE)
  534. {
  535. min *= sampleRate;
  536. max *= sampleRate;
  537. def *= sampleRate;
  538. param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  539. }
  540. if (portHints & PORT_HINT_IS_BOOLEAN)
  541. {
  542. step = max - min;
  543. stepSmall = step;
  544. stepLarge = step;
  545. param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  546. }
  547. else if (portHints & PORT_HINT_IS_INTEGER)
  548. {
  549. step = 1.0;
  550. stepSmall = 1.0;
  551. stepLarge = 10.0;
  552. param.data[j].hints |= PARAMETER_IS_INTEGER;
  553. }
  554. else
  555. {
  556. double range = max - min;
  557. step = range/100.0;
  558. stepSmall = range/1000.0;
  559. stepLarge = range/10.0;
  560. }
  561. if (portHints & PORT_HINT_IS_OUTPUT)
  562. {
  563. param.data[j].type = PARAMETER_OUTPUT;
  564. needsCtrlOut = true;
  565. }
  566. else
  567. {
  568. param.data[j].type = PARAMETER_INPUT;
  569. needsCtrlIn = true;
  570. }
  571. // extra parameter hints
  572. if (portHints & PORT_HINT_IS_ENABLED)
  573. param.data[j].hints |= PARAMETER_IS_ENABLED;
  574. if (portHints & PORT_HINT_IS_AUTOMABLE)
  575. param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  576. if (portHints & PORT_HINT_IS_LOGARITHMIC)
  577. param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  578. if (portHints & PORT_HINT_USES_SCALEPOINTS)
  579. param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  580. if (portHints & PORT_HINT_USES_CUSTOM_TEXT)
  581. param.data[j].hints |= PARAMETER_USES_CUSTOM_TEXT;
  582. param.ranges[j].min = min;
  583. param.ranges[j].max = max;
  584. param.ranges[j].def = def;
  585. param.ranges[j].step = step;
  586. param.ranges[j].stepSmall = stepSmall;
  587. param.ranges[j].stepLarge = stepLarge;
  588. }
  589. }
  590. if (needsCtrlIn)
  591. {
  592. if (CarlaEngine::processMode != PROCESS_MODE_MULTIPLE_CLIENTS)
  593. {
  594. strcpy(portName, m_name);
  595. strcat(portName, ":control-in");
  596. }
  597. else
  598. strcpy(portName, "control-in");
  599. param.portCin = (CarlaEngineControlPort*)x_client->addPort(CarlaEnginePortTypeControl, portName, true);
  600. }
  601. if (needsCtrlOut)
  602. {
  603. if (CarlaEngine::processMode != PROCESS_MODE_MULTIPLE_CLIENTS)
  604. {
  605. strcpy(portName, m_name);
  606. strcat(portName, ":control-out");
  607. }
  608. else
  609. strcpy(portName, "control-out");
  610. param.portCout = (CarlaEngineControlPort*)x_client->addPort(CarlaEnginePortTypeControl, portName, false);
  611. }
  612. aIn.count = aIns;
  613. aOut.count = aOuts;
  614. mIn.count = mIns;
  615. mOut.count = mOuts;
  616. param.count = params;
  617. // plugin checks
  618. m_hints &= ~(PLUGIN_IS_SYNTH | PLUGIN_USES_CHUNKS | PLUGIN_CAN_DRYWET | PLUGIN_CAN_VOLUME | PLUGIN_CAN_BALANCE | PLUGIN_CAN_FORCE_STEREO);
  619. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  620. m_hints |= PLUGIN_CAN_DRYWET;
  621. if (aOuts > 0)
  622. m_hints |= PLUGIN_CAN_VOLUME;
  623. if (aOuts >= 2 && aOuts%2 == 0)
  624. m_hints |= PLUGIN_CAN_BALANCE;
  625. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && mIns <= 1 && mOuts <= 1)
  626. m_hints |= PLUGIN_CAN_FORCE_STEREO;
  627. m_hints |= getPluginHintsFromNative(descriptor->hints);
  628. reloadPrograms(true);
  629. x_client->activate();
  630. qDebug("NativePlugin::reload() - end");
  631. }
  632. void reloadPrograms(const bool init)
  633. {
  634. qDebug("NativePlugin::reloadPrograms(%s)", bool2str(init));
  635. uint32_t i, oldCount = midiprog.count;
  636. // Delete old programs
  637. if (midiprog.count > 0)
  638. {
  639. for (i=0; i < midiprog.count; i++)
  640. {
  641. if (midiprog.data[i].name)
  642. free((void*)midiprog.data[i].name);
  643. }
  644. delete[] midiprog.data;
  645. }
  646. midiprog.count = 0;
  647. midiprog.data = nullptr;
  648. // Query new programs
  649. if (descriptor->get_midi_program && descriptor->set_midi_program)
  650. {
  651. while (descriptor->get_midi_program(handle, midiprog.count))
  652. midiprog.count += 1;
  653. }
  654. if (midiprog.count > 0)
  655. midiprog.data = new MidiProgramData[midiprog.count];
  656. // Update data
  657. for (i=0; i < midiprog.count; i++)
  658. {
  659. const MidiProgram* const mpDesc = descriptor->get_midi_program(handle, i);
  660. CARLA_ASSERT(mpDesc);
  661. CARLA_ASSERT(mpDesc->name);
  662. midiprog.data[i].bank = mpDesc->bank;
  663. midiprog.data[i].program = mpDesc->program;
  664. midiprog.data[i].name = strdup(mpDesc->name);
  665. }
  666. #ifndef BUILD_BRIDGE
  667. // Update OSC Names
  668. if (x_engine->isOscControlRegisted())
  669. {
  670. x_engine->osc_send_control_set_midi_program_count(m_id, midiprog.count);
  671. for (i=0; i < midiprog.count; i++)
  672. x_engine->osc_send_control_set_midi_program_data(m_id, i, midiprog.data[i].bank, midiprog.data[i].program, midiprog.data[i].name);
  673. }
  674. #endif
  675. if (init)
  676. {
  677. if (midiprog.count > 0)
  678. setMidiProgram(0, false, false, false, true);
  679. }
  680. else
  681. {
  682. x_engine->callback(CALLBACK_RELOAD_PROGRAMS, m_id, 0, 0, 0.0);
  683. // Check if current program is invalid
  684. bool programChanged = false;
  685. if (midiprog.count == oldCount+1)
  686. {
  687. // one midi program added, probably created by user
  688. midiprog.current = oldCount;
  689. programChanged = true;
  690. }
  691. else if (midiprog.current >= (int32_t)midiprog.count)
  692. {
  693. // current midi program > count
  694. midiprog.current = 0;
  695. programChanged = true;
  696. }
  697. else if (midiprog.current < 0 && midiprog.count > 0)
  698. {
  699. // programs exist now, but not before
  700. midiprog.current = 0;
  701. programChanged = true;
  702. }
  703. else if (midiprog.current >= 0 && midiprog.count == 0)
  704. {
  705. // programs existed before, but not anymore
  706. midiprog.current = -1;
  707. programChanged = true;
  708. }
  709. if (programChanged)
  710. setMidiProgram(midiprog.current, true, true, true, true);
  711. }
  712. }
  713. // -------------------------------------------------------------------
  714. // Plugin processing
  715. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t framesOffset)
  716. {
  717. uint32_t i, k;
  718. double aInsPeak[2] = { 0.0 };
  719. double aOutsPeak[2] = { 0.0 };
  720. // reset MIDI
  721. midiEventCount = 0;
  722. memset(midiEvents, 0, sizeof(::MidiEvent) * MAX_MIDI_EVENTS * 2);
  723. CARLA_PROCESS_CONTINUE_CHECK;
  724. // --------------------------------------------------------------------------------------------------------
  725. // Input VU
  726. #ifndef BUILD_BRIDGE
  727. if (aIn.count > 0 && CarlaEngine::processMode != PROCESS_MODE_CONTINUOUS_RACK)
  728. #else
  729. if (aIn.count > 0)
  730. #endif
  731. {
  732. if (aIn.count == 1)
  733. {
  734. for (k=0; k < frames; k++)
  735. {
  736. if (abs(inBuffer[0][k]) > aInsPeak[0])
  737. aInsPeak[0] = abs(inBuffer[0][k]);
  738. }
  739. }
  740. else if (aIn.count > 1)
  741. {
  742. for (k=0; k < frames; k++)
  743. {
  744. if (abs(inBuffer[0][k]) > aInsPeak[0])
  745. aInsPeak[0] = abs(inBuffer[0][k]);
  746. if (abs(inBuffer[1][k]) > aInsPeak[1])
  747. aInsPeak[1] = abs(inBuffer[1][k]);
  748. }
  749. }
  750. }
  751. CARLA_PROCESS_CONTINUE_CHECK;
  752. // --------------------------------------------------------------------------------------------------------
  753. // Parameters Input [Automation]
  754. if (param.portCin && m_active && m_activeBefore)
  755. {
  756. bool allNotesOffSent = false;
  757. const CarlaEngineControlEvent* cinEvent;
  758. uint32_t time, nEvents = param.portCin->getEventCount();
  759. uint32_t nextBankId = 0;
  760. if (midiprog.current >= 0 && midiprog.count > 0)
  761. nextBankId = midiprog.data[midiprog.current].bank;
  762. for (i=0; i < nEvents; i++)
  763. {
  764. cinEvent = param.portCin->getEvent(i);
  765. if (! cinEvent)
  766. continue;
  767. time = cinEvent->time - framesOffset;
  768. if (time >= frames)
  769. continue;
  770. // Control change
  771. switch (cinEvent->type)
  772. {
  773. case CarlaEngineEventNull:
  774. break;
  775. case CarlaEngineEventControlChange:
  776. {
  777. double value;
  778. // Control backend stuff
  779. if (cinEvent->channel == m_ctrlInChannel)
  780. {
  781. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(cinEvent->controller) && (m_hints & PLUGIN_CAN_DRYWET) > 0)
  782. {
  783. value = cinEvent->value;
  784. setDryWet(value, false, false);
  785. postponeEvent(PluginPostEventParameterChange, PARAMETER_DRYWET, 0, value);
  786. continue;
  787. }
  788. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(cinEvent->controller) && (m_hints & PLUGIN_CAN_VOLUME) > 0)
  789. {
  790. value = cinEvent->value*127/100;
  791. setVolume(value, false, false);
  792. postponeEvent(PluginPostEventParameterChange, PARAMETER_VOLUME, 0, value);
  793. continue;
  794. }
  795. if (MIDI_IS_CONTROL_BALANCE(cinEvent->controller) && (m_hints & PLUGIN_CAN_BALANCE) > 0)
  796. {
  797. double left, right;
  798. value = cinEvent->value/0.5 - 1.0;
  799. if (value < 0.0)
  800. {
  801. left = -1.0;
  802. right = (value*2)+1.0;
  803. }
  804. else if (value > 0.0)
  805. {
  806. left = (value*2)-1.0;
  807. right = 1.0;
  808. }
  809. else
  810. {
  811. left = -1.0;
  812. right = 1.0;
  813. }
  814. setBalanceLeft(left, false, false);
  815. setBalanceRight(right, false, false);
  816. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  817. postponeEvent(PluginPostEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  818. continue;
  819. }
  820. }
  821. // Control plugin parameters
  822. for (k=0; k < param.count; k++)
  823. {
  824. if (param.data[k].midiChannel != cinEvent->channel)
  825. continue;
  826. if (param.data[k].midiCC != cinEvent->controller)
  827. continue;
  828. if (param.data[k].type != PARAMETER_INPUT)
  829. continue;
  830. if (param.data[k].hints & PARAMETER_IS_AUTOMABLE)
  831. {
  832. if (param.data[k].hints & PARAMETER_IS_BOOLEAN)
  833. {
  834. value = cinEvent->value < 0.5 ? param.ranges[k].min : param.ranges[k].max;
  835. }
  836. else
  837. {
  838. value = cinEvent->value * (param.ranges[k].max - param.ranges[k].min) + param.ranges[k].min;
  839. if (param.data[k].hints & PARAMETER_IS_INTEGER)
  840. value = rint(value);
  841. }
  842. setParameterValue(k, value, false, false, false);
  843. postponeEvent(PluginPostEventParameterChange, k, 0, value);
  844. }
  845. }
  846. break;
  847. }
  848. case CarlaEngineEventMidiBankChange:
  849. if (cinEvent->channel == m_ctrlInChannel)
  850. nextBankId = rint(cinEvent->value);
  851. break;
  852. case CarlaEngineEventMidiProgramChange:
  853. if (cinEvent->channel == m_ctrlInChannel)
  854. {
  855. uint32_t nextProgramId = rint(cinEvent->value);
  856. for (k=0; k < midiprog.count; k++)
  857. {
  858. if (midiprog.data[k].bank == nextBankId && midiprog.data[k].program == nextProgramId)
  859. {
  860. setMidiProgram(k, false, false, false, false);
  861. postponeEvent(PluginPostEventMidiProgramChange, k, 0, 0.0);
  862. break;
  863. }
  864. }
  865. }
  866. break;
  867. case CarlaEngineEventAllSoundOff:
  868. if (cinEvent->channel == m_ctrlInChannel)
  869. {
  870. if (mIn.count > 0 && ! allNotesOffSent)
  871. sendMidiAllNotesOff();
  872. if (descriptor->deactivate)
  873. {
  874. descriptor->deactivate(handle);
  875. if (h2) descriptor->deactivate(h2);
  876. }
  877. if (descriptor->activate)
  878. {
  879. descriptor->activate(handle);
  880. if (h2) descriptor->activate(h2);
  881. }
  882. postponeEvent(PluginPostEventParameterChange, PARAMETER_ACTIVE, 0, 0.0);
  883. postponeEvent(PluginPostEventParameterChange, PARAMETER_ACTIVE, 0, 1.0);
  884. allNotesOffSent = true;
  885. }
  886. break;
  887. case CarlaEngineEventAllNotesOff:
  888. if (cinEvent->channel == m_ctrlInChannel)
  889. {
  890. if (mIn.count > 0 && ! allNotesOffSent)
  891. sendMidiAllNotesOff();
  892. allNotesOffSent = true;
  893. }
  894. break;
  895. }
  896. }
  897. } // End of Parameters Input
  898. CARLA_PROCESS_CONTINUE_CHECK;
  899. // --------------------------------------------------------------------------------------------------------
  900. // MIDI Input
  901. if (mIn.count > 0 && m_active && m_activeBefore)
  902. {
  903. // ----------------------------------------------------------------------------------------------------
  904. // MIDI Input (External)
  905. {
  906. engineMidiLock();
  907. for (i=0; i < MAX_MIDI_EVENTS && midiEventCount < MAX_MIDI_EVENTS; i++)
  908. {
  909. if (extMidiNotes[i].channel < 0)
  910. break;
  911. ::MidiEvent* const midiEvent = &midiEvents[midiEventCount];
  912. memset(midiEvent, 0, sizeof(::MidiEvent));
  913. midiEvent->data[0] = uint8_t(extMidiNotes[i].velo ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) + extMidiNotes[i].channel;
  914. midiEvent->data[1] = extMidiNotes[i].note;
  915. midiEvent->data[2] = extMidiNotes[i].velo;
  916. midiEvent->size = 3;
  917. extMidiNotes[i].channel = -1; // mark as invalid
  918. midiEventCount += 1;
  919. }
  920. engineMidiUnlock();
  921. } // End of MIDI Input (External)
  922. CARLA_PROCESS_CONTINUE_CHECK;
  923. // ----------------------------------------------------------------------------------------------------
  924. // MIDI Input (System)
  925. for (i=0; i < mIn.count; i++)
  926. {
  927. if (! mIn.ports[i])
  928. continue;
  929. const CarlaEngineMidiEvent* minEvent;
  930. uint32_t time, nEvents = mIn.ports[i]->getEventCount();
  931. for (k=0; k < nEvents && midiEventCount < MAX_MIDI_EVENTS; k++)
  932. {
  933. minEvent = mIn.ports[i]->getEvent(k);
  934. if (! minEvent)
  935. continue;
  936. time = minEvent->time - framesOffset;
  937. if (time >= frames)
  938. continue;
  939. uint8_t status = minEvent->data[0];
  940. uint8_t channel = status & 0x0F;
  941. // Fix bad note-off
  942. if (MIDI_IS_STATUS_NOTE_ON(status) && minEvent->data[2] == 0)
  943. status -= 0x10;
  944. ::MidiEvent* const midiEvent = &midiEvents[midiEventCount];
  945. memset(midiEvent, 0, sizeof(::MidiEvent));
  946. midiEvent->portOffset = i;
  947. midiEvent->time = minEvent->time;
  948. midiEvent->size = minEvent->size;
  949. if (MIDI_IS_STATUS_NOTE_OFF(status))
  950. {
  951. uint8_t note = minEvent->data[1];
  952. midiEvent->data[0] = status;
  953. midiEvent->data[1] = note;
  954. postponeEvent(PluginPostEventNoteOff, channel, note, 0.0);
  955. }
  956. else if (MIDI_IS_STATUS_NOTE_ON(status))
  957. {
  958. uint8_t note = minEvent->data[1];
  959. uint8_t velo = minEvent->data[2];
  960. midiEvent->data[0] = status;
  961. midiEvent->data[1] = note;
  962. midiEvent->data[2] = velo;
  963. postponeEvent(PluginPostEventNoteOn, channel, note, velo);
  964. }
  965. else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status))
  966. {
  967. uint8_t note = minEvent->data[1];
  968. uint8_t pressure = minEvent->data[2];
  969. midiEvent->data[0] = status;
  970. midiEvent->data[1] = note;
  971. midiEvent->data[2] = pressure;
  972. }
  973. else if (MIDI_IS_STATUS_AFTERTOUCH(status))
  974. {
  975. uint8_t pressure = minEvent->data[1];
  976. midiEvent->data[0] = status;
  977. midiEvent->data[1] = pressure;
  978. }
  979. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status))
  980. {
  981. uint8_t lsb = minEvent->data[1];
  982. uint8_t msb = minEvent->data[2];
  983. midiEvent->data[0] = status;
  984. midiEvent->data[1] = lsb;
  985. midiEvent->data[2] = msb;
  986. }
  987. else
  988. continue;
  989. midiEventCount += 1;
  990. }
  991. } // End of MIDI Input (System)
  992. } // End of MIDI Input
  993. CARLA_PROCESS_CONTINUE_CHECK;
  994. // --------------------------------------------------------------------------------------------------------
  995. // Plugin processing
  996. uint32_t midiEventCountBefore = midiEventCount;
  997. if (m_active)
  998. {
  999. if (! m_activeBefore)
  1000. {
  1001. if (mIn.count > 0)
  1002. {
  1003. for (k=0; k < MAX_MIDI_CHANNELS; k++)
  1004. {
  1005. memset(&midiEvents[k], 0, sizeof(::MidiEvent));
  1006. midiEvents[k].data[0] = MIDI_STATUS_CONTROL_CHANGE + k;
  1007. midiEvents[k].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1008. midiEvents[k].size = 2;
  1009. memset(&midiEvents[k*2], 0, sizeof(::MidiEvent));
  1010. midiEvents[k*2].data[0] = MIDI_STATUS_CONTROL_CHANGE + k;
  1011. midiEvents[k*2].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1012. midiEvents[k*2].size = 2;
  1013. }
  1014. midiEventCount = MAX_MIDI_CHANNELS*2;
  1015. }
  1016. if (descriptor->activate)
  1017. {
  1018. descriptor->activate(handle);
  1019. if (h2) descriptor->activate(h2);
  1020. }
  1021. }
  1022. isProcessing = true;
  1023. if (h2)
  1024. {
  1025. descriptor->process(handle, inBuffer? &inBuffer[0] : nullptr, outBuffer? &outBuffer[0] : nullptr, frames, midiEventCountBefore, midiEvents);
  1026. descriptor->process(h2, inBuffer? &inBuffer[1] : nullptr, outBuffer? &outBuffer[1] : nullptr, frames, midiEventCountBefore, midiEvents);
  1027. }
  1028. else
  1029. descriptor->process(handle, inBuffer, outBuffer, frames, midiEventCountBefore, midiEvents);
  1030. isProcessing = false;
  1031. }
  1032. else
  1033. {
  1034. if (m_activeBefore)
  1035. {
  1036. if (descriptor->deactivate)
  1037. {
  1038. descriptor->deactivate(handle);
  1039. if (h2) descriptor->deactivate(h2);
  1040. }
  1041. }
  1042. }
  1043. CARLA_PROCESS_CONTINUE_CHECK;
  1044. // --------------------------------------------------------------------------------------------------------
  1045. // Post-processing (dry/wet, volume and balance)
  1046. if (m_active)
  1047. {
  1048. bool do_drywet = (m_hints & PLUGIN_CAN_DRYWET) > 0 && x_dryWet != 1.0;
  1049. bool do_volume = (m_hints & PLUGIN_CAN_VOLUME) > 0 && x_volume != 1.0;
  1050. bool do_balance = (m_hints & PLUGIN_CAN_BALANCE) > 0 && (x_balanceLeft != -1.0 || x_balanceRight != 1.0);
  1051. double bal_rangeL, bal_rangeR;
  1052. float bufValue, oldBufLeft[do_balance ? frames : 0];
  1053. for (i=0; i < aOut.count; i++)
  1054. {
  1055. // Dry/Wet
  1056. if (do_drywet)
  1057. {
  1058. for (k=0; k < frames; k++)
  1059. {
  1060. bufValue = (aIn.count == 1) ? inBuffer[0][k] : inBuffer[i][k];
  1061. outBuffer[i][k] = (outBuffer[i][k]*x_dryWet)+(bufValue*(1.0-x_dryWet));
  1062. }
  1063. }
  1064. // Balance
  1065. if (do_balance)
  1066. {
  1067. if (i%2 == 0)
  1068. memcpy(&oldBufLeft, outBuffer[i], sizeof(float)*frames);
  1069. bal_rangeL = (x_balanceLeft+1.0)/2;
  1070. bal_rangeR = (x_balanceRight+1.0)/2;
  1071. for (k=0; k < frames; k++)
  1072. {
  1073. if (i%2 == 0)
  1074. {
  1075. // left output
  1076. outBuffer[i][k] = oldBufLeft[k]*(1.0-bal_rangeL);
  1077. outBuffer[i][k] += outBuffer[i+1][k]*(1.0-bal_rangeR);
  1078. }
  1079. else
  1080. {
  1081. // right
  1082. outBuffer[i][k] = outBuffer[i][k]*bal_rangeR;
  1083. outBuffer[i][k] += oldBufLeft[k]*bal_rangeL;
  1084. }
  1085. }
  1086. }
  1087. // Volume
  1088. if (do_volume)
  1089. {
  1090. for (k=0; k < frames; k++)
  1091. outBuffer[i][k] *= x_volume;
  1092. }
  1093. // Output VU
  1094. #ifndef BUILD_BRIDGE
  1095. if (CarlaEngine::processMode != PROCESS_MODE_CONTINUOUS_RACK)
  1096. #endif
  1097. {
  1098. for (k=0; i < 2 && k < frames; k++)
  1099. {
  1100. if (abs(outBuffer[i][k]) > aOutsPeak[i])
  1101. aOutsPeak[i] = abs(outBuffer[i][k]);
  1102. }
  1103. }
  1104. }
  1105. }
  1106. else
  1107. {
  1108. // disable any output sound if not active
  1109. for (i=0; i < aOut.count; i++)
  1110. carla_zeroF(outBuffer[i], frames);
  1111. aOutsPeak[0] = 0.0;
  1112. aOutsPeak[1] = 0.0;
  1113. } // End of Post-processing
  1114. CARLA_PROCESS_CONTINUE_CHECK;
  1115. // --------------------------------------------------------------------------------------------------------
  1116. // MIDI Output
  1117. if (mOut.count > 0 && m_active)
  1118. {
  1119. uint8_t data[3] = { 0 };
  1120. for (uint32_t i = midiEventCountBefore; i < midiEventCount; i++)
  1121. {
  1122. data[0] = midiEvents[i].data[0];
  1123. data[1] = midiEvents[i].data[1];
  1124. data[2] = midiEvents[i].data[2];
  1125. // Fix bad note-off
  1126. if (MIDI_IS_STATUS_NOTE_ON(data[0]) && data[2] == 0)
  1127. data[0] -= 0x10;
  1128. const uint32_t portOffset = midiEvents[i].portOffset;
  1129. if (portOffset < mOut.count)
  1130. mOut.ports[portOffset]->writeEvent(midiEvents[i].time, data, 3);
  1131. }
  1132. } // End of MIDI Output
  1133. // --------------------------------------------------------------------------------------------------------
  1134. // Control Output
  1135. if (param.portCout && m_active)
  1136. {
  1137. double value, valueControl;
  1138. for (k=0; k < param.count; k++)
  1139. {
  1140. if (param.data[k].type == PARAMETER_OUTPUT)
  1141. {
  1142. value = descriptor->get_parameter_value(handle, param.data[k].rindex);
  1143. if (param.data[k].midiCC > 0)
  1144. {
  1145. valueControl = (value - param.ranges[k].min) / (param.ranges[k].max - param.ranges[k].min);
  1146. param.portCout->writeEvent(CarlaEngineEventControlChange, framesOffset, param.data[k].midiChannel, param.data[k].midiCC, valueControl);
  1147. }
  1148. }
  1149. }
  1150. } // End of Control Output
  1151. CARLA_PROCESS_CONTINUE_CHECK;
  1152. // --------------------------------------------------------------------------------------------------------
  1153. // Peak Values
  1154. x_engine->setInputPeak(m_id, 0, aInsPeak[0]);
  1155. x_engine->setInputPeak(m_id, 1, aInsPeak[1]);
  1156. x_engine->setOutputPeak(m_id, 0, aOutsPeak[0]);
  1157. x_engine->setOutputPeak(m_id, 1, aOutsPeak[1]);
  1158. m_activeBefore = m_active;
  1159. }
  1160. // -------------------------------------------------------------------
  1161. // Cleanup
  1162. void removeClientPorts()
  1163. {
  1164. qDebug("NativePlugin::removeClientPorts() - start");
  1165. for (uint32_t i=0; i < mIn.count; i++)
  1166. {
  1167. delete mIn.ports[i];
  1168. mIn.ports[i] = nullptr;
  1169. }
  1170. for (uint32_t i=0; i < mOut.count; i++)
  1171. {
  1172. delete mOut.ports[i];
  1173. mOut.ports[i] = nullptr;
  1174. }
  1175. CarlaPlugin::removeClientPorts();
  1176. qDebug("NativePlugin::removeClientPorts() - end");
  1177. }
  1178. void initBuffers()
  1179. {
  1180. uint32_t i;
  1181. for (i=0; i < mIn.count; i++)
  1182. {
  1183. if (mIn.ports[i])
  1184. mIn.ports[i]->initBuffer(x_engine);
  1185. }
  1186. for (i=0; i < mOut.count; i++)
  1187. {
  1188. if (mOut.ports[i])
  1189. mOut.ports[i]->initBuffer(x_engine);
  1190. }
  1191. CarlaPlugin::initBuffers();
  1192. }
  1193. void deleteBuffers()
  1194. {
  1195. qDebug("NativePlugin::deleteBuffers() - start");
  1196. if (mIn.count > 0)
  1197. {
  1198. delete[] mIn.ports;
  1199. delete[] mIn.rindexes;
  1200. }
  1201. if (mOut.count > 0)
  1202. {
  1203. delete[] mOut.ports;
  1204. delete[] mOut.rindexes;
  1205. }
  1206. mIn.count = 0;
  1207. mIn.ports = nullptr;
  1208. mIn.rindexes = nullptr;
  1209. mOut.count = 0;
  1210. mOut.ports = nullptr;
  1211. mOut.rindexes = nullptr;
  1212. CarlaPlugin::deleteBuffers();
  1213. qDebug("NativePlugin::deleteBuffers() - end");
  1214. }
  1215. // -------------------------------------------------------------------
  1216. uint32_t handleGetBufferSize()
  1217. {
  1218. return x_engine->getBufferSize();
  1219. }
  1220. double handleGetSampleRate()
  1221. {
  1222. return x_engine->getSampleRate();
  1223. }
  1224. const TimeInfo* handleGetTimeInfo()
  1225. {
  1226. // TODO
  1227. return nullptr;
  1228. }
  1229. bool handleWriteMidiEvent(MidiEvent* event)
  1230. {
  1231. CARLA_ASSERT(m_enabled);
  1232. CARLA_ASSERT(mOut.count > 0);
  1233. CARLA_ASSERT(isProcessing);
  1234. CARLA_ASSERT(event);
  1235. if (! m_enabled)
  1236. return false;
  1237. if (mOut.count == 0)
  1238. return false;
  1239. if (! isProcessing)
  1240. {
  1241. qCritical("NativePlugin::handleWriteMidiEvent(%p) - received MIDI out events outside audio thread, ignoring", event);
  1242. return false;
  1243. }
  1244. if (midiEventCount >= MAX_MIDI_EVENTS*2)
  1245. return false;
  1246. memcpy(&midiEvents[midiEventCount], event, sizeof(::MidiEvent));
  1247. midiEventCount += 1;
  1248. return true;
  1249. }
  1250. static uint32_t carla_host_get_buffer_size(HostHandle handle)
  1251. {
  1252. CARLA_ASSERT(handle);
  1253. return ((NativePlugin*)handle)->handleGetBufferSize();
  1254. }
  1255. static double carla_host_get_sample_rate(HostHandle handle)
  1256. {
  1257. CARLA_ASSERT(handle);
  1258. return ((NativePlugin*)handle)->handleGetSampleRate();
  1259. }
  1260. static const TimeInfo* carla_host_get_time_info(HostHandle handle)
  1261. {
  1262. CARLA_ASSERT(handle);
  1263. return ((NativePlugin*)handle)->handleGetTimeInfo();
  1264. }
  1265. static bool carla_host_write_midi_event(HostHandle handle, MidiEvent* event)
  1266. {
  1267. CARLA_ASSERT(handle);
  1268. return ((NativePlugin*)handle)->handleWriteMidiEvent(event);
  1269. }
  1270. // -------------------------------------------------------------------
  1271. static size_t getPluginCount()
  1272. {
  1273. scopedInitliazer.maybeFirstInit();
  1274. return pluginDescriptors.size();
  1275. }
  1276. static const PluginDescriptor* getPlugin(size_t index)
  1277. {
  1278. CARLA_ASSERT(index < pluginDescriptors.size());
  1279. if (index < pluginDescriptors.size())
  1280. return pluginDescriptors[index];
  1281. return nullptr;
  1282. }
  1283. static void registerPlugin(const PluginDescriptor* desc)
  1284. {
  1285. pluginDescriptors.push_back(desc);
  1286. }
  1287. // -------------------------------------------------------------------
  1288. bool init(const char* const name, const char* const label)
  1289. {
  1290. // ---------------------------------------------------------------
  1291. // initialize native-plugins descriptors
  1292. scopedInitliazer.maybeFirstInit();
  1293. // ---------------------------------------------------------------
  1294. // get descriptor that matches label
  1295. for (size_t i=0; i < pluginDescriptors.size(); i++)
  1296. {
  1297. descriptor = pluginDescriptors[i];
  1298. if (! descriptor)
  1299. break;
  1300. if (strcmp(descriptor->label, label) == 0)
  1301. break;
  1302. descriptor = nullptr;
  1303. }
  1304. if (! descriptor)
  1305. {
  1306. setLastError("Invalid internal plugin");
  1307. return false;
  1308. }
  1309. scopedInitliazer.initializeIfNeeded(descriptor);
  1310. // ---------------------------------------------------------------
  1311. // initialize plugin
  1312. handle = descriptor->instantiate((struct _PluginDescriptor*)descriptor, &host);
  1313. if (! handle)
  1314. {
  1315. setLastError("Plugin failed to initialize");
  1316. return false;
  1317. }
  1318. // ---------------------------------------------------------------
  1319. // get info
  1320. if (name)
  1321. m_name = x_engine->getUniquePluginName(name);
  1322. else
  1323. m_name = x_engine->getUniquePluginName(descriptor->name);
  1324. // ---------------------------------------------------------------
  1325. // register client
  1326. x_client = x_engine->addClient(this);
  1327. if (! x_client->isOk())
  1328. {
  1329. setLastError("Failed to register plugin client");
  1330. return false;
  1331. }
  1332. return true;
  1333. }
  1334. private:
  1335. const PluginDescriptor* descriptor;
  1336. PluginHandle handle, h2;
  1337. HostDescriptor host;
  1338. bool isProcessing;
  1339. NativePluginMidiData mIn;
  1340. NativePluginMidiData mOut;
  1341. uint32_t midiEventCount;
  1342. ::MidiEvent midiEvents[MAX_MIDI_EVENTS*2];
  1343. static std::vector<const PluginDescriptor*> pluginDescriptors;
  1344. };
  1345. std::vector<const PluginDescriptor*> NativePlugin::pluginDescriptors;
  1346. CarlaPlugin* CarlaPlugin::newNative(const initializer& init)
  1347. {
  1348. qDebug("CarlaPlugin::newNative(%p, \"%s\", \"%s\", \"%s\")", init.engine, init.filename, init.name, init.label);
  1349. short id = init.engine->getNewPluginId();
  1350. if (id < 0 || id > CarlaEngine::maxPluginNumber())
  1351. {
  1352. setLastError("Maximum number of plugins reached");
  1353. return nullptr;
  1354. }
  1355. NativePlugin* const plugin = new NativePlugin(init.engine, id);
  1356. if (! plugin->init(init.name, init.label))
  1357. {
  1358. delete plugin;
  1359. return nullptr;
  1360. }
  1361. plugin->reload();
  1362. if (CarlaEngine::processMode == PROCESS_MODE_CONTINUOUS_RACK)
  1363. {
  1364. if (! (plugin->hints() & PLUGIN_CAN_FORCE_STEREO))
  1365. {
  1366. setLastError("Carla's rack mode can only work with Mono or Stereo Internal plugins, sorry!");
  1367. delete plugin;
  1368. return nullptr;
  1369. }
  1370. }
  1371. plugin->registerToOscControl();
  1372. return plugin;
  1373. }
  1374. size_t CarlaPlugin::getNativePluginCount()
  1375. {
  1376. return NativePlugin::getPluginCount();
  1377. }
  1378. const PluginDescriptor* CarlaPlugin::getNativePlugin(size_t index)
  1379. {
  1380. return NativePlugin::getPlugin(index);
  1381. }
  1382. CARLA_BACKEND_END_NAMESPACE
  1383. void carla_register_native_plugin(const PluginDescriptor* desc)
  1384. {
  1385. CarlaBackend::NativePlugin::registerPlugin(desc);
  1386. }