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.

1693 lines
53KB

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