Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1883 lines
61KB

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