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.

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