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.

1890 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()
  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, const bool block)
  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. // FIXME
  343. if (fDescriptor != nullptr && fHandle != nullptr && index >= 0)
  344. {
  345. const uint32_t bank = kData->midiprog.data[index].bank;
  346. const uint32_t program = kData->midiprog.data[index].program;
  347. if (kData->engine->isOffline())
  348. {
  349. //const CarlaEngine::ScopedLocker m(x_engine, block);
  350. fDescriptor->set_midi_program(fHandle, bank, program);
  351. if (fHandle2 != nullptr)
  352. fDescriptor->set_midi_program(fHandle2, bank, program);
  353. }
  354. else
  355. {
  356. //const ScopedDisabler m(this, block);
  357. fDescriptor->set_midi_program(fHandle, bank, program);
  358. if (fHandle2 != nullptr)
  359. fDescriptor->set_midi_program(fHandle2, bank, program);
  360. }
  361. }
  362. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback, block);
  363. }
  364. // -------------------------------------------------------------------
  365. // Set gui stuff
  366. void showGui(const bool yesNo)
  367. {
  368. CARLA_ASSERT(fDescriptor != nullptr);
  369. CARLA_ASSERT(fHandle != nullptr);
  370. if (fDescriptor != nullptr && fHandle != nullptr)
  371. {
  372. if (fDescriptor->name != nullptr && std::strcmp(fDescriptor->label, "audiofile") == 0)
  373. {
  374. QString filenameTry = QFileDialog::getOpenFileName(nullptr, "Open Audio File");
  375. if (! filenameTry.isEmpty())
  376. fDescriptor->set_custom_data(fHandle, "file", filenameTry.toUtf8().constData());
  377. kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  378. }
  379. else if (fDescriptor->ui_show != nullptr)
  380. fDescriptor->ui_show(fHandle, yesNo);
  381. }
  382. }
  383. void idleGui()
  384. {
  385. CARLA_ASSERT(fDescriptor != nullptr);
  386. CARLA_ASSERT(fHandle != nullptr);
  387. if (fDescriptor != nullptr && fHandle != nullptr && fDescriptor->ui_idle != nullptr)
  388. fDescriptor->ui_idle(fHandle);
  389. }
  390. // -------------------------------------------------------------------
  391. // Plugin state
  392. void reload()
  393. {
  394. carla_debug("NativePlugin::reload() - start");
  395. CARLA_ASSERT(kData->engine != nullptr);
  396. CARLA_ASSERT(fDescriptor != nullptr);
  397. CARLA_ASSERT(fHandle != nullptr);
  398. const ProcessMode processMode(kData->engine->getProccessMode());
  399. // Safely disable plugin for reload
  400. const ScopedDisabler sd(this);
  401. if (kData->client->isActive())
  402. kData->client->deactivate();
  403. deleteBuffers();
  404. const double sampleRate = kData->engine->getSampleRate();
  405. uint32_t aIns, aOuts, mIns, mOuts, params, j;
  406. bool forcedStereoIn, forcedStereoOut;
  407. forcedStereoIn = forcedStereoOut = false;
  408. bool needsCtrlIn, needsCtrlOut;
  409. needsCtrlIn = needsCtrlOut = false;
  410. aIns = fDescriptor->audioIns;
  411. aOuts = fDescriptor->audioOuts;
  412. mIns = fDescriptor->midiIns;
  413. mOuts = fDescriptor->midiOuts;
  414. params = (fDescriptor->get_parameter_count != nullptr && fDescriptor->get_parameter_info != nullptr) ? fDescriptor->get_parameter_count(fHandle) : 0;
  415. if ((fOptions & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1) && mIns <= 1 && mOuts <= 1)
  416. {
  417. if (fHandle2 == nullptr)
  418. fHandle2 = fDescriptor->instantiate(fDescriptor, &fHost);
  419. if (aIns == 1)
  420. {
  421. aIns = 2;
  422. forcedStereoIn = true;
  423. }
  424. if (aOuts == 1)
  425. {
  426. aOuts = 2;
  427. forcedStereoOut = true;
  428. }
  429. }
  430. if (aIns > 0)
  431. {
  432. kData->audioIn.createNew(aIns);
  433. fAudioInBuffers = new float*[aIns];
  434. for (uint32_t i=0; i < aIns; i++)
  435. fAudioInBuffers[i] = nullptr;
  436. }
  437. if (aOuts > 0)
  438. {
  439. kData->audioOut.createNew(aOuts);
  440. fAudioOutBuffers = new float*[aOuts];
  441. needsCtrlIn = true;
  442. for (uint32_t i=0; i < aOuts; i++)
  443. fAudioOutBuffers[i] = nullptr;
  444. }
  445. if (mIns > 0)
  446. {
  447. fMidiIn.createNew(mIns);
  448. needsCtrlIn = (mIns == 1);
  449. }
  450. if (mOuts > 0)
  451. {
  452. fMidiOut.createNew(mOuts);
  453. needsCtrlOut = (mOuts == 1);
  454. }
  455. if (params > 0)
  456. {
  457. kData->param.createNew(params);
  458. }
  459. const int portNameSize = kData->engine->maxPortNameSize();
  460. CarlaString portName;
  461. // Audio Ins
  462. for (j=0; j < aIns; j++)
  463. {
  464. portName.clear();
  465. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  466. {
  467. portName = fName;
  468. portName += ":";
  469. }
  470. if (aIns > 1)
  471. {
  472. portName += "input_";
  473. portName += CarlaString(j+1);
  474. }
  475. else
  476. portName += "input";
  477. portName.truncate(portNameSize);
  478. kData->audioIn.ports[j].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, true);
  479. kData->audioIn.ports[j].rindex = j;
  480. if (forcedStereoIn)
  481. {
  482. portName += "_2";
  483. kData->audioIn.ports[1].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, true);
  484. kData->audioIn.ports[1].rindex = j;
  485. break;
  486. }
  487. }
  488. // Audio Outs
  489. for (j=0; j < aOuts; j++)
  490. {
  491. portName.clear();
  492. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  493. {
  494. portName = fName;
  495. portName += ":";
  496. }
  497. if (aOuts > 1)
  498. {
  499. portName += "output_";
  500. portName += CarlaString(j+1);
  501. }
  502. else
  503. portName += "output";
  504. portName.truncate(portNameSize);
  505. kData->audioOut.ports[j].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, false);
  506. kData->audioOut.ports[j].rindex = j;
  507. if (forcedStereoOut)
  508. {
  509. portName += "_2";
  510. kData->audioOut.ports[1].port = (CarlaEngineAudioPort*)kData->client->addPort(kEnginePortTypeAudio, portName, false);
  511. kData->audioOut.ports[1].rindex = j;
  512. break;
  513. }
  514. }
  515. // MIDI Input (only if multiple)
  516. if (mIns > 1)
  517. {
  518. for (j=0; j < mIns; j++)
  519. {
  520. portName.clear();
  521. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  522. {
  523. portName = fName;
  524. portName += ":";
  525. }
  526. portName += "midi-in_";
  527. portName += CarlaString(j+1);
  528. portName.truncate(portNameSize);
  529. fMidiIn.ports[j] = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  530. fMidiIn.indexes[j] = j;
  531. }
  532. }
  533. // MIDI Output (only if multiple)
  534. if (mOuts > 1)
  535. {
  536. for (j=0; j < mOuts; j++)
  537. {
  538. portName.clear();
  539. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  540. {
  541. portName = fName;
  542. portName += ":";
  543. }
  544. portName += "midi-out_";
  545. portName += CarlaString(j+1);
  546. portName.truncate(portNameSize);
  547. fMidiOut.ports[j] = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  548. fMidiOut.indexes[j] = j;
  549. }
  550. }
  551. for (j=0; j < params; j++)
  552. {
  553. const ::Parameter* const paramInfo = fDescriptor->get_parameter_info(fHandle, j);
  554. kData->param.data[j].index = j;
  555. kData->param.data[j].rindex = j;
  556. kData->param.data[j].hints = 0x0;
  557. kData->param.data[j].midiChannel = 0;
  558. kData->param.data[j].midiCC = -1;
  559. float min, max, def, step, stepSmall, stepLarge;
  560. // min value
  561. min = paramInfo->ranges.min;
  562. // max value
  563. max = paramInfo->ranges.max;
  564. if (min > max)
  565. max = min;
  566. else if (max < min)
  567. min = max;
  568. if (max - min == 0.0f)
  569. {
  570. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", paramInfo->name);
  571. max = min + 0.1f;
  572. }
  573. // default value
  574. def = paramInfo->ranges.def;
  575. if (def < min)
  576. def = min;
  577. else if (def > max)
  578. def = max;
  579. if (paramInfo->hints & ::PARAMETER_USES_SAMPLE_RATE)
  580. {
  581. min *= sampleRate;
  582. max *= sampleRate;
  583. def *= sampleRate;
  584. kData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  585. }
  586. if (paramInfo->hints & ::PARAMETER_IS_BOOLEAN)
  587. {
  588. step = max - min;
  589. stepSmall = step;
  590. stepLarge = step;
  591. kData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  592. }
  593. else if (paramInfo->hints & ::PARAMETER_IS_INTEGER)
  594. {
  595. step = 1.0f;
  596. stepSmall = 1.0f;
  597. stepLarge = 10.0f;
  598. kData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  599. }
  600. else
  601. {
  602. float range = max - min;
  603. step = range/100.0f;
  604. stepSmall = range/1000.0f;
  605. stepLarge = range/10.0f;
  606. }
  607. if (paramInfo->hints & ::PARAMETER_IS_OUTPUT)
  608. {
  609. kData->param.data[j].type = PARAMETER_OUTPUT;
  610. needsCtrlOut = true;
  611. }
  612. else
  613. {
  614. kData->param.data[j].type = PARAMETER_INPUT;
  615. needsCtrlIn = true;
  616. }
  617. // extra parameter hints
  618. if (paramInfo->hints & ::PARAMETER_IS_ENABLED)
  619. kData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  620. if (paramInfo->hints & ::PARAMETER_IS_AUTOMABLE)
  621. kData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  622. if (paramInfo->hints & ::PARAMETER_IS_LOGARITHMIC)
  623. kData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  624. if (paramInfo->hints & ::PARAMETER_USES_SCALEPOINTS)
  625. kData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  626. if (paramInfo->hints & ::PARAMETER_USES_CUSTOM_TEXT)
  627. kData->param.data[j].hints |= PARAMETER_USES_CUSTOM_TEXT;
  628. kData->param.ranges[j].min = min;
  629. kData->param.ranges[j].max = max;
  630. kData->param.ranges[j].def = def;
  631. kData->param.ranges[j].step = step;
  632. kData->param.ranges[j].stepSmall = stepSmall;
  633. kData->param.ranges[j].stepLarge = stepLarge;
  634. }
  635. if (needsCtrlIn)
  636. {
  637. portName.clear();
  638. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  639. {
  640. portName = fName;
  641. portName += ":";
  642. }
  643. portName += "event-in";
  644. portName.truncate(portNameSize);
  645. kData->event.portIn = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, true);
  646. }
  647. if (needsCtrlOut)
  648. {
  649. portName.clear();
  650. if (processMode == PROCESS_MODE_SINGLE_CLIENT)
  651. {
  652. portName = fName;
  653. portName += ":";
  654. }
  655. portName += "event-out";
  656. portName.truncate(portNameSize);
  657. kData->event.portOut = (CarlaEngineEventPort*)kData->client->addPort(kEnginePortTypeEvent, portName, false);
  658. }
  659. // plugin checks
  660. fHints &= ~(PLUGIN_IS_SYNTH | PLUGIN_USES_CHUNKS | PLUGIN_CAN_DRYWET | PLUGIN_CAN_VOLUME | PLUGIN_CAN_BALANCE | PLUGIN_CAN_FORCE_STEREO);
  661. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  662. fHints |= PLUGIN_CAN_DRYWET;
  663. if (aOuts > 0)
  664. fHints |= PLUGIN_CAN_VOLUME;
  665. if (aOuts >= 2 && aOuts%2 == 0)
  666. fHints |= PLUGIN_CAN_BALANCE;
  667. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && mIns <= 1 && mOuts <= 1)
  668. fHints |= PLUGIN_CAN_FORCE_STEREO;
  669. // native plugin hints
  670. if (fDescriptor->hints & ::PLUGIN_IS_RTSAFE)
  671. fHints |= PLUGIN_IS_RTSAFE;
  672. if (fDescriptor->hints & ::PLUGIN_IS_SYNTH)
  673. fHints |= PLUGIN_IS_SYNTH;
  674. if (fDescriptor->hints & ::PLUGIN_HAS_GUI)
  675. fHints |= PLUGIN_HAS_GUI;
  676. if (fDescriptor->hints & ::PLUGIN_USES_SINGLE_THREAD)
  677. fHints |= PLUGIN_USES_SINGLE_THREAD;
  678. // plugin options
  679. kData->availOptions &= ~(PLUGIN_OPTION_FIXED_BUFFER | PLUGIN_OPTION_SELF_AUTOMATION | PLUGIN_OPTION_SEND_ALL_SOUND_OFF | PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH | PLUGIN_OPTION_SEND_PITCHBEND);
  680. // always available if needed
  681. kData->availOptions |= PLUGIN_OPTION_FIXED_BUFFER;
  682. kData->availOptions |= PLUGIN_OPTION_SELF_AUTOMATION;
  683. // only for plugins with midi input
  684. if (mIns > 0)
  685. {
  686. kData->availOptions |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  687. kData->availOptions |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  688. kData->availOptions |= PLUGIN_OPTION_SEND_PITCHBEND;
  689. }
  690. bufferSizeChanged(kData->engine->getBufferSize());
  691. reloadPrograms(true);
  692. kData->client->activate();
  693. carla_debug("NativePlugin::reload() - end");
  694. }
  695. void reloadPrograms(const bool init)
  696. {
  697. carla_debug("NativePlugin::reloadPrograms(%s)", bool2str(init));
  698. uint32_t i, oldCount = kData->midiprog.count;
  699. // Delete old programs
  700. kData->midiprog.clear();
  701. // Query new programs
  702. uint32_t count = 0;
  703. if (fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_info != nullptr)
  704. count = fDescriptor->get_midi_program_count(fHandle);
  705. if (count > 0)
  706. kData->midiprog.createNew(count);
  707. // Update data
  708. for (i=0; i < kData->midiprog.count; i++)
  709. {
  710. const ::MidiProgram* const mpDesc = fDescriptor->get_midi_program_info(fHandle, i);
  711. CARLA_ASSERT(mpDesc != nullptr);
  712. CARLA_ASSERT(mpDesc->name != nullptr);
  713. kData->midiprog.data[i].bank = mpDesc->bank;
  714. kData->midiprog.data[i].program = mpDesc->program;
  715. kData->midiprog.data[i].name = carla_strdup(mpDesc->name);
  716. }
  717. #ifndef BUILD_BRIDGE
  718. // Update OSC Names
  719. if (kData->engine->isOscControlRegistered())
  720. {
  721. kData->engine->osc_send_control_set_midi_program_count(fId, kData->midiprog.count);
  722. for (i=0; i < kData->midiprog.count; i++)
  723. 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);
  724. }
  725. #endif
  726. if (init)
  727. {
  728. if (kData->midiprog.count > 0)
  729. setMidiProgram(0, false, false, false, true);
  730. }
  731. else
  732. {
  733. kData->engine->callback(CALLBACK_RELOAD_PROGRAMS, fId, 0, 0, 0.0f, nullptr);
  734. // Check if current program is invalid
  735. bool programChanged = false;
  736. if (kData->midiprog.count == oldCount+1)
  737. {
  738. // one midi program added, probably created by user
  739. kData->midiprog.current = oldCount;
  740. programChanged = true;
  741. }
  742. else if (kData->midiprog.current >= static_cast<int32_t>(kData->midiprog.count))
  743. {
  744. // current midi program > count
  745. kData->midiprog.current = 0;
  746. programChanged = true;
  747. }
  748. else if (kData->midiprog.current < 0 && kData->midiprog.count > 0)
  749. {
  750. // programs exist now, but not before
  751. kData->midiprog.current = 0;
  752. programChanged = true;
  753. }
  754. else if (kData->midiprog.current >= 0 && kData->midiprog.count == 0)
  755. {
  756. // programs existed before, but not anymore
  757. kData->midiprog.current = -1;
  758. programChanged = true;
  759. }
  760. if (programChanged)
  761. setMidiProgram(kData->midiprog.current, true, true, true, true);
  762. }
  763. }
  764. // -------------------------------------------------------------------
  765. // Plugin processing
  766. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames)
  767. {
  768. uint32_t i, k;
  769. // --------------------------------------------------------------------------------------------------------
  770. // Check if active
  771. if (! kData->active)
  772. {
  773. // disable any output sound
  774. for (i=0; i < kData->audioOut.count; i++)
  775. carla_zeroFloat(outBuffer[i], frames);
  776. if (kData->activeBefore)
  777. {
  778. if (fDescriptor->deactivate != nullptr)
  779. {
  780. fDescriptor->deactivate(fHandle);
  781. if (fHandle2 != nullptr)
  782. fDescriptor->deactivate(fHandle2);
  783. }
  784. }
  785. kData->activeBefore = kData->active;
  786. return;
  787. }
  788. fMidiEventCount = 0;
  789. carla_zeroMem(fMidiEvents, sizeof(::MidiEvent)*MAX_MIDI_EVENTS*2);
  790. // --------------------------------------------------------------------------------------------------------
  791. // Check if not active before
  792. if (! kData->activeBefore)
  793. {
  794. if (kData->event.portIn != nullptr)
  795. {
  796. for (k=0, i=MAX_MIDI_CHANNELS; k < MAX_MIDI_CHANNELS; k++)
  797. {
  798. fMidiEvents[k].data[0] = MIDI_STATUS_CONTROL_CHANGE + k;
  799. fMidiEvents[k].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  800. fMidiEvents[k+i].data[0] = MIDI_STATUS_CONTROL_CHANGE + k;
  801. fMidiEvents[k+i].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  802. }
  803. fMidiEventCount = MAX_MIDI_CHANNELS*2;
  804. }
  805. if (fDescriptor->activate != nullptr)
  806. {
  807. fDescriptor->activate(fHandle);
  808. if (fHandle2 != nullptr)
  809. fDescriptor->activate(fHandle2);
  810. }
  811. }
  812. CARLA_PROCESS_CONTINUE_CHECK;
  813. // --------------------------------------------------------------------------------------------------------
  814. // Set TimeInfo
  815. const EngineTimeInfo& timeInfo = kData->engine->getTimeInfo();
  816. fTimeInfo.playing = timeInfo.playing;
  817. fTimeInfo.frame = timeInfo.frame;
  818. fTimeInfo.time = timeInfo.time;
  819. if (timeInfo.valid & EngineTimeInfo::ValidBBT)
  820. {
  821. fTimeInfo.bbt.valid = true;
  822. fTimeInfo.bbt.bar = timeInfo.bbt.bar;
  823. fTimeInfo.bbt.beat = timeInfo.bbt.beat;
  824. fTimeInfo.bbt.tick = timeInfo.bbt.tick;
  825. fTimeInfo.bbt.barStartTick = timeInfo.bbt.barStartTick;
  826. fTimeInfo.bbt.beatsPerBar = timeInfo.bbt.beatsPerBar;
  827. fTimeInfo.bbt.beatType = timeInfo.bbt.beatType;
  828. fTimeInfo.bbt.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
  829. fTimeInfo.bbt.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
  830. }
  831. else
  832. fTimeInfo.bbt.valid = false;
  833. CARLA_PROCESS_CONTINUE_CHECK;
  834. // --------------------------------------------------------------------------------------------------------
  835. // Event Input and Processing
  836. if (kData->event.portIn != nullptr && kData->activeBefore)
  837. {
  838. // ----------------------------------------------------------------------------------------------------
  839. // MIDI Input (External)
  840. if (kData->extNotes.mutex.tryLock())
  841. {
  842. while (fMidiEventCount < MAX_MIDI_EVENTS*2 && ! kData->extNotes.data.isEmpty())
  843. {
  844. const ExternalMidiNote& note = kData->extNotes.data.getFirst(true);
  845. CARLA_ASSERT(note.channel >= 0);
  846. fMidiEvents[fMidiEventCount].port = 0;
  847. fMidiEvents[fMidiEventCount].time = 0;
  848. fMidiEvents[fMidiEventCount].data[0] = (note.velo > 0) ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF;
  849. fMidiEvents[fMidiEventCount].data[0] += note.channel;
  850. fMidiEvents[fMidiEventCount].data[1] = note.note;
  851. fMidiEvents[fMidiEventCount].data[2] = note.velo;
  852. fMidiEventCount += 1;
  853. }
  854. kData->extNotes.mutex.unlock();
  855. } // End of MIDI Input (External)
  856. // ----------------------------------------------------------------------------------------------------
  857. // Event Input (System)
  858. bool allNotesOffSent = false;
  859. bool sampleAccurate = (fOptions & PLUGIN_OPTION_FIXED_BUFFER) == 0;
  860. uint32_t time, nEvents = kData->event.portIn->getEventCount();
  861. uint32_t timeOffset = 0;
  862. uint32_t nextBankId = 0;
  863. if (kData->midiprog.current >= 0 && kData->midiprog.count > 0)
  864. nextBankId = kData->midiprog.data[kData->midiprog.current].bank;
  865. for (i=0; i < nEvents; i++)
  866. {
  867. const EngineEvent& event = kData->event.portIn->getEvent(i);
  868. time = event.time;
  869. if (time >= frames)
  870. continue;
  871. CARLA_ASSERT_INT2(time >= timeOffset, time, timeOffset);
  872. if (time > timeOffset && sampleAccurate)
  873. {
  874. processSingle(inBuffer, outBuffer, time - timeOffset, timeOffset);
  875. if (fMidiEventCount > 0)
  876. {
  877. carla_zeroMem(fMidiEvents, sizeof(::MidiEvent)*fMidiEventCount);
  878. fMidiEventCount = 0;
  879. }
  880. nextBankId = 0;
  881. timeOffset = time;
  882. }
  883. // Control change
  884. switch (event.type)
  885. {
  886. case kEngineEventTypeNull:
  887. break;
  888. case kEngineEventTypeControl:
  889. {
  890. const EngineControlEvent& ctrlEvent = event.ctrl;
  891. switch (ctrlEvent.type)
  892. {
  893. case kEngineControlEventTypeNull:
  894. break;
  895. case kEngineControlEventTypeParameter:
  896. {
  897. // Control backend stuff
  898. if (event.channel == kData->ctrlChannel)
  899. {
  900. double value;
  901. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (fHints & PLUGIN_CAN_DRYWET) > 0)
  902. {
  903. value = ctrlEvent.value;
  904. setDryWet(value, false, false);
  905. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  906. continue;
  907. }
  908. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (fHints & PLUGIN_CAN_VOLUME) > 0)
  909. {
  910. value = ctrlEvent.value*127/100;
  911. setVolume(value, false, false);
  912. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  913. continue;
  914. }
  915. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (fHints & PLUGIN_CAN_BALANCE) > 0)
  916. {
  917. double left, right;
  918. value = ctrlEvent.value/0.5 - 1.0;
  919. if (value < 0.0)
  920. {
  921. left = -1.0;
  922. right = (value*2)+1.0;
  923. }
  924. else if (value > 0.0)
  925. {
  926. left = (value*2)-1.0;
  927. right = 1.0;
  928. }
  929. else
  930. {
  931. left = -1.0;
  932. right = 1.0;
  933. }
  934. setBalanceLeft(left, false, false);
  935. setBalanceRight(right, false, false);
  936. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  937. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  938. continue;
  939. }
  940. }
  941. // Control plugin parameters
  942. for (k=0; k < kData->param.count; k++)
  943. {
  944. if (kData->param.data[k].midiChannel != event.channel)
  945. continue;
  946. if (kData->param.data[k].midiCC != ctrlEvent.param)
  947. continue;
  948. if (kData->param.data[k].type != PARAMETER_INPUT)
  949. continue;
  950. if ((kData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  951. continue;
  952. double value;
  953. if (kData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  954. {
  955. value = (ctrlEvent.value < 0.5) ? kData->param.ranges[k].min : kData->param.ranges[k].max;
  956. }
  957. else
  958. {
  959. // FIXME - ranges call for this
  960. value = ctrlEvent.value * (kData->param.ranges[k].max - kData->param.ranges[k].min) + kData->param.ranges[k].min;
  961. if (kData->param.data[k].hints & PARAMETER_IS_INTEGER)
  962. value = std::rint(value);
  963. }
  964. setParameterValue(k, value, false, false, false);
  965. postponeRtEvent(kPluginPostRtEventParameterChange, k, 0, value);
  966. }
  967. break;
  968. }
  969. case kEngineControlEventTypeMidiBank:
  970. if (event.channel == kData->ctrlChannel)
  971. nextBankId = ctrlEvent.param;
  972. break;
  973. case kEngineControlEventTypeMidiProgram:
  974. if (event.channel == kData->ctrlChannel)
  975. {
  976. const uint32_t nextProgramId = ctrlEvent.param;
  977. for (k=0; k < kData->midiprog.count; k++)
  978. {
  979. if (kData->midiprog.data[k].bank == nextBankId && kData->midiprog.data[k].program == nextProgramId)
  980. {
  981. setMidiProgram(k, false, false, false, false);
  982. postponeRtEvent(kPluginPostRtEventMidiProgramChange, k, 0, 0.0);
  983. break;
  984. }
  985. }
  986. }
  987. break;
  988. case kEngineControlEventTypeAllSoundOff:
  989. if (event.channel == kData->ctrlChannel)
  990. {
  991. if (! allNotesOffSent)
  992. sendMidiAllNotesOff();
  993. if (fDescriptor->deactivate != nullptr)
  994. {
  995. fDescriptor->deactivate(fHandle);
  996. if (fHandle2 != nullptr)
  997. fDescriptor->deactivate(fHandle2);
  998. }
  999. if (fDescriptor->activate != nullptr)
  1000. {
  1001. fDescriptor->activate(fHandle);
  1002. if (fHandle2 != nullptr)
  1003. fDescriptor->activate(fHandle2);
  1004. }
  1005. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 0.0);
  1006. postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_ACTIVE, 0, 1.0);
  1007. allNotesOffSent = true;
  1008. }
  1009. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1010. continue;
  1011. fMidiEvents[fMidiEventCount].port = 0;
  1012. fMidiEvents[fMidiEventCount].time = sampleAccurate ? 0 : time;
  1013. fMidiEvents[fMidiEventCount].data[0] = MIDI_STATUS_CONTROL_CHANGE + event.channel;
  1014. fMidiEvents[fMidiEventCount].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1015. fMidiEvents[fMidiEventCount].data[2] = 0;
  1016. fMidiEventCount += 1;
  1017. break;
  1018. case kEngineControlEventTypeAllNotesOff:
  1019. if (event.channel == kData->ctrlChannel)
  1020. {
  1021. if (! allNotesOffSent)
  1022. sendMidiAllNotesOff();
  1023. allNotesOffSent = true;
  1024. }
  1025. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1026. continue;
  1027. fMidiEvents[fMidiEventCount].port = 0;
  1028. fMidiEvents[fMidiEventCount].time = sampleAccurate ? 0: time;
  1029. fMidiEvents[fMidiEventCount].data[0] = MIDI_STATUS_CONTROL_CHANGE + event.channel;
  1030. fMidiEvents[fMidiEventCount].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1031. fMidiEvents[fMidiEventCount].data[2] = 0;
  1032. fMidiEventCount += 1;
  1033. break;
  1034. }
  1035. break;
  1036. }
  1037. case kEngineEventTypeMidi:
  1038. {
  1039. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1040. continue;
  1041. const EngineMidiEvent& midiEvent = event.midi;
  1042. uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent.data);
  1043. uint8_t channel = event.channel;
  1044. if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (fHints & PLUGIN_OPTION_SELF_AUTOMATION) == 0)
  1045. continue;
  1046. // Fix bad note-off (per DSSI spec)
  1047. if (MIDI_IS_STATUS_NOTE_ON(status) && midiEvent.data[2] == 0)
  1048. status -= 0x10;
  1049. fMidiEvents[fMidiEventCount].port = 0;
  1050. fMidiEvents[fMidiEventCount].time = sampleAccurate ? 0 : time - timeOffset;
  1051. fMidiEvents[fMidiEventCount].data[0] = status + channel;
  1052. fMidiEvents[fMidiEventCount].data[1] = midiEvent.data[1];
  1053. fMidiEvents[fMidiEventCount].data[2] = midiEvent.data[2];
  1054. fMidiEventCount += 1;
  1055. break;
  1056. }
  1057. }
  1058. }
  1059. kData->postRtEvents.trySplice();
  1060. if (frames > timeOffset)
  1061. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  1062. } // End of Event Input and Processing
  1063. // --------------------------------------------------------------------------------------------------------
  1064. // Plugin processing (no events)
  1065. else
  1066. {
  1067. processSingle(inBuffer, outBuffer, frames, 0);
  1068. } // End of Plugin processing (no events)
  1069. CARLA_PROCESS_CONTINUE_CHECK;
  1070. // --------------------------------------------------------------------------------------------------------
  1071. // Post-processing (dry/wet, volume and balance)
  1072. {
  1073. const bool doDryWet = (fHints & PLUGIN_CAN_DRYWET) > 0 && kData->postProc.dryWet != 1.0f;
  1074. const bool doVolume = (fHints & PLUGIN_CAN_VOLUME) > 0 && kData->postProc.volume != 1.0f;
  1075. const bool doBalance = (fHints & PLUGIN_CAN_BALANCE) > 0 && (kData->postProc.balanceLeft != -1.0f || kData->postProc.balanceRight != 1.0f);
  1076. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1077. for (i=0; i < kData->audioOut.count; i++)
  1078. {
  1079. // Dry/Wet
  1080. if (doDryWet)
  1081. {
  1082. for (k=0; k < frames; k++)
  1083. {
  1084. bufValue = inBuffer[(kData->audioIn.count == 1) ? 0 : i][k];
  1085. outBuffer[i][k] = (outBuffer[i][k] * kData->postProc.dryWet) + (bufValue * (1.0f - kData->postProc.dryWet));
  1086. }
  1087. }
  1088. // Balance
  1089. if (doBalance)
  1090. {
  1091. if (i % 2 == 0)
  1092. carla_copyFloat(oldBufLeft, outBuffer[i], frames);
  1093. float balRangeL = (kData->postProc.balanceLeft + 1.0f)/2.0f;
  1094. float balRangeR = (kData->postProc.balanceRight + 1.0f)/2.0f;
  1095. for (k=0; k < frames; k++)
  1096. {
  1097. if (i % 2 == 0)
  1098. {
  1099. // left
  1100. outBuffer[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1101. outBuffer[i][k] += outBuffer[i+1][k] * (1.0f - balRangeR);
  1102. }
  1103. else
  1104. {
  1105. // right
  1106. outBuffer[i][k] = outBuffer[i][k] * balRangeR;
  1107. outBuffer[i][k] += oldBufLeft[k] * balRangeL;
  1108. }
  1109. }
  1110. }
  1111. // Volume
  1112. if (doVolume)
  1113. {
  1114. for (k=0; k < frames; k++)
  1115. outBuffer[i][k] *= kData->postProc.volume;
  1116. }
  1117. }
  1118. } // End of Post-processing
  1119. CARLA_PROCESS_CONTINUE_CHECK;
  1120. // --------------------------------------------------------------------------------------------------------
  1121. // MIDI Output
  1122. if (fMidiOut.count > 0)
  1123. {
  1124. // reverse lookup
  1125. for (uint32_t i = (MAX_MIDI_EVENTS*2)-1; i >= fMidiEventCount; i--)
  1126. {
  1127. if (fMidiEvents[i].data[0] == 0)
  1128. break;
  1129. const uint8_t channel = MIDI_GET_CHANNEL_FROM_DATA(fMidiEvents[i].data);
  1130. const uint8_t port = fMidiEvents[i].port;
  1131. if (port < fMidiOut.count)
  1132. fMidiOut.ports[port]->writeMidiEvent(fMidiEvents[i].time, channel, port, fMidiEvents[i].data, 3);
  1133. }
  1134. } // End of MIDI Output
  1135. CARLA_PROCESS_CONTINUE_CHECK;
  1136. // --------------------------------------------------------------------------------------------------------
  1137. // Control Output
  1138. if (kData->event.portOut != nullptr)
  1139. {
  1140. float value, curValue;
  1141. for (k=0; k < kData->param.count; k++)
  1142. {
  1143. if (kData->param.data[k].type != PARAMETER_OUTPUT)
  1144. continue;
  1145. curValue = fDescriptor->get_parameter_value(fHandle, k);
  1146. kData->param.ranges[k].fixValue(curValue);
  1147. if (kData->param.data[k].midiCC > 0)
  1148. {
  1149. value = kData->param.ranges[k].normalizeValue(curValue);
  1150. kData->event.portOut->writeControlEvent(0, kData->param.data[k].midiChannel, kEngineControlEventTypeParameter, kData->param.data[k].midiCC, value);
  1151. }
  1152. }
  1153. } // End of Control Output
  1154. // --------------------------------------------------------------------------------------------------------
  1155. kData->activeBefore = kData->active;
  1156. }
  1157. void processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  1158. {
  1159. for (uint32_t i=0; i < kData->audioIn.count; i++)
  1160. carla_copyFloat(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  1161. for (uint32_t i=0; i < kData->audioOut.count; i++)
  1162. carla_zeroFloat(fAudioOutBuffers[i], frames);
  1163. fIsProcessing = true;
  1164. fDescriptor->process(fHandle, fAudioInBuffers, fAudioOutBuffers, frames, fMidiEventCount, fMidiEvents);
  1165. if (fHandle2 != nullptr)
  1166. fDescriptor->process(fHandle2, fAudioInBuffers, fAudioOutBuffers, frames, fMidiEventCount, fMidiEvents);
  1167. fIsProcessing = false;
  1168. fTimeInfo.frame += frames;
  1169. for (uint32_t i=0, k; i < kData->audioOut.count; i++)
  1170. {
  1171. for (k=0; k < frames; k++)
  1172. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1173. }
  1174. }
  1175. void bufferSizeChanged(const uint32_t newBufferSize)
  1176. {
  1177. for (uint32_t i=0; i < kData->audioIn.count; i++)
  1178. {
  1179. if (fAudioInBuffers[i] != nullptr)
  1180. delete[] fAudioInBuffers[i];
  1181. fAudioInBuffers[i] = new float[newBufferSize];
  1182. }
  1183. for (uint32_t i=0; i < kData->audioOut.count; i++)
  1184. {
  1185. if (fAudioOutBuffers[i] != nullptr)
  1186. delete[] fAudioOutBuffers[i];
  1187. fAudioOutBuffers[i] = new float[newBufferSize];
  1188. }
  1189. }
  1190. // -------------------------------------------------------------------
  1191. // Cleanup
  1192. void initBuffers()
  1193. {
  1194. fMidiIn.initBuffers(kData->engine);
  1195. fMidiOut.initBuffers(kData->engine);
  1196. CarlaPlugin::initBuffers();
  1197. }
  1198. void deleteBuffers()
  1199. {
  1200. carla_debug("NativePlugin::deleteBuffers() - start");
  1201. if (fAudioInBuffers != nullptr)
  1202. {
  1203. for (uint32_t i=0; i < kData->audioIn.count; i++)
  1204. {
  1205. if (fAudioInBuffers[i] != nullptr)
  1206. {
  1207. delete[] fAudioInBuffers[i];
  1208. fAudioInBuffers[i] = nullptr;
  1209. }
  1210. }
  1211. delete[] fAudioInBuffers;
  1212. fAudioInBuffers = nullptr;
  1213. }
  1214. if (fAudioOutBuffers != nullptr)
  1215. {
  1216. for (uint32_t i=0; i < kData->audioOut.count; i++)
  1217. {
  1218. if (fAudioOutBuffers[i] != nullptr)
  1219. {
  1220. delete[] fAudioOutBuffers[i];
  1221. fAudioOutBuffers[i] = nullptr;
  1222. }
  1223. }
  1224. delete[] fAudioOutBuffers;
  1225. fAudioOutBuffers = nullptr;
  1226. }
  1227. fMidiIn.clear();
  1228. fMidiOut.clear();
  1229. CarlaPlugin::deleteBuffers();
  1230. carla_debug("NativePlugin::deleteBuffers() - end");
  1231. }
  1232. // -------------------------------------------------------------------
  1233. protected:
  1234. uint32_t handleGetBufferSize()
  1235. {
  1236. return kData->engine->getBufferSize();
  1237. }
  1238. double handleGetSampleRate()
  1239. {
  1240. return kData->engine->getSampleRate();
  1241. }
  1242. const ::TimeInfo* handleGetTimeInfo()
  1243. {
  1244. CARLA_ASSERT(fIsProcessing);
  1245. return &fTimeInfo;
  1246. }
  1247. bool handleWriteMidiEvent(const ::MidiEvent* const event)
  1248. {
  1249. CARLA_ASSERT(fEnabled);
  1250. CARLA_ASSERT(fMidiOut.count > 0);
  1251. CARLA_ASSERT(event != nullptr);
  1252. CARLA_ASSERT(fIsProcessing);
  1253. if (! fEnabled)
  1254. return false;
  1255. if (fMidiOut.count == 0)
  1256. return false;
  1257. if (event == nullptr)
  1258. return false;
  1259. if (! fIsProcessing)
  1260. {
  1261. carla_stderr2("NativePlugin::handleWriteMidiEvent(%p) - received MIDI out events outside audio thread, ignoring", event);
  1262. return false;
  1263. }
  1264. if (fMidiEventCount >= MAX_MIDI_EVENTS*2)
  1265. return false;
  1266. // reverse-find first free event, and put it there
  1267. for (uint32_t i=fMidiEventCount-1; i >= fMidiEventCount; i--)
  1268. {
  1269. if (fMidiEvents[i].data[0] == 0)
  1270. {
  1271. std::memcpy(&fMidiEvents[i], event, sizeof(::MidiEvent));
  1272. break;
  1273. }
  1274. }
  1275. return true;
  1276. }
  1277. void handleUiParameterChanged(const uint32_t index, const float value)
  1278. {
  1279. setParameterValue(index, value, false, true, true);
  1280. }
  1281. void handleUiCustomDataChanged(const char* const key, const char* const value)
  1282. {
  1283. setCustomData(CUSTOM_DATA_STRING, key, value, false);
  1284. }
  1285. void handleUiClosed()
  1286. {
  1287. kData->engine->callback(CALLBACK_SHOW_GUI, fId, 0, 0, 0.0f, nullptr);
  1288. }
  1289. public:
  1290. static size_t getPluginCount()
  1291. {
  1292. return sPluginDescriptors.count();
  1293. }
  1294. static const PluginDescriptor* getPluginDescriptor(const size_t index)
  1295. {
  1296. CARLA_ASSERT(index < sPluginDescriptors.count());
  1297. if (index < sPluginDescriptors.count())
  1298. return sPluginDescriptors.getAt(index);
  1299. return nullptr;
  1300. }
  1301. static void registerPlugin(const PluginDescriptor* desc)
  1302. {
  1303. sPluginDescriptors.append(desc);
  1304. }
  1305. // -------------------------------------------------------------------
  1306. bool init(const char* const name, const char* const label)
  1307. {
  1308. CARLA_ASSERT(kData->engine != nullptr);
  1309. CARLA_ASSERT(kData->client == nullptr);
  1310. CARLA_ASSERT(label);
  1311. // ---------------------------------------------------------------
  1312. // get descriptor that matches label
  1313. // FIXME - use itenerator when available
  1314. for (size_t i=0; i < sPluginDescriptors.count(); i++)
  1315. {
  1316. fDescriptor = sPluginDescriptors.getAt(i);
  1317. if (fDescriptor == nullptr)
  1318. break;
  1319. if (fDescriptor->label != nullptr && std::strcmp(fDescriptor->label, label) == 0)
  1320. break;
  1321. fDescriptor = nullptr;
  1322. }
  1323. if (fDescriptor == nullptr)
  1324. {
  1325. kData->engine->setLastError("Invalid internal plugin");
  1326. return false;
  1327. }
  1328. // ---------------------------------------------------------------
  1329. // get info
  1330. if (name != nullptr)
  1331. fName = kData->engine->getNewUniquePluginName(name);
  1332. else if (fDescriptor->name != nullptr)
  1333. fName = kData->engine->getNewUniquePluginName(fDescriptor->name);
  1334. else
  1335. fName = kData->engine->getNewUniquePluginName(fDescriptor->name);
  1336. // ---------------------------------------------------------------
  1337. // register client
  1338. kData->client = kData->engine->addClient(this);
  1339. if (kData->client == nullptr || ! kData->client->isOk())
  1340. {
  1341. kData->engine->setLastError("Failed to register plugin client");
  1342. return false;
  1343. }
  1344. // ---------------------------------------------------------------
  1345. // initialize plugin
  1346. fHandle = fDescriptor->instantiate(fDescriptor, &fHost);
  1347. if (fHandle == nullptr)
  1348. {
  1349. kData->engine->setLastError("Plugin failed to initialize");
  1350. return false;
  1351. }
  1352. return true;
  1353. }
  1354. class ScopedInitializer
  1355. {
  1356. public:
  1357. ScopedInitializer()
  1358. {
  1359. initDescriptors();
  1360. }
  1361. ~ScopedInitializer()
  1362. {
  1363. clearDescriptors();
  1364. }
  1365. };
  1366. private:
  1367. PluginHandle fHandle;
  1368. PluginHandle fHandle2;
  1369. HostDescriptor fHost;
  1370. const PluginDescriptor* fDescriptor;
  1371. bool fIsProcessing;
  1372. float** fAudioInBuffers;
  1373. float** fAudioOutBuffers;
  1374. uint32_t fMidiEventCount;
  1375. ::MidiEvent fMidiEvents[MAX_MIDI_EVENTS*2];
  1376. NativePluginMidiData fMidiIn;
  1377. NativePluginMidiData fMidiOut;
  1378. ::TimeInfo fTimeInfo;
  1379. static NonRtList<const PluginDescriptor*> sPluginDescriptors;
  1380. // -------------------------------------------------------------------
  1381. static void initDescriptors()
  1382. {
  1383. #ifndef BUILD_BRIDGE
  1384. carla_register_native_plugin_bypass();
  1385. carla_register_native_plugin_midiSplit();
  1386. carla_register_native_plugin_midiThrough();
  1387. #if 0
  1388. carla_register_native_plugin_3BandEQ();
  1389. carla_register_native_plugin_3BandSplitter();
  1390. carla_register_native_plugin_PingPongPan();
  1391. #endif
  1392. # ifdef WANT_AUDIOFILE
  1393. carla_register_native_plugin_audiofile();
  1394. # endif
  1395. # ifdef WANT_ZYNADDSUBFX
  1396. carla_register_native_plugin_zynaddsubfx();
  1397. # endif
  1398. #endif
  1399. }
  1400. static void clearDescriptors()
  1401. {
  1402. sPluginDescriptors.clear();
  1403. }
  1404. // -------------------------------------------------------------------
  1405. #define handlePtr ((NativePlugin*)handle)
  1406. static uint32_t carla_host_get_buffer_size(HostHandle handle)
  1407. {
  1408. return handlePtr->handleGetBufferSize();
  1409. }
  1410. static double carla_host_get_sample_rate(HostHandle handle)
  1411. {
  1412. return handlePtr->handleGetSampleRate();
  1413. }
  1414. static const ::TimeInfo* carla_host_get_time_info(HostHandle handle)
  1415. {
  1416. return handlePtr->handleGetTimeInfo();
  1417. }
  1418. static bool carla_host_write_midi_event(HostHandle handle, const ::MidiEvent* event)
  1419. {
  1420. return handlePtr->handleWriteMidiEvent(event);
  1421. }
  1422. static void carla_host_ui_parameter_changed(HostHandle handle, uint32_t index, float value)
  1423. {
  1424. handlePtr->handleUiParameterChanged(index, value);
  1425. }
  1426. static void carla_host_ui_custom_data_changed(HostHandle handle, const char* key, const char* value)
  1427. {
  1428. handlePtr->handleUiCustomDataChanged(key, value);
  1429. }
  1430. static void carla_host_ui_closed(HostHandle handle)
  1431. {
  1432. handlePtr->handleUiClosed();
  1433. }
  1434. #undef handlePtr
  1435. };
  1436. NonRtList<const PluginDescriptor*> NativePlugin::sPluginDescriptors;
  1437. static const NativePlugin::ScopedInitializer _si;
  1438. CARLA_BACKEND_END_NAMESPACE
  1439. void carla_register_native_plugin(const PluginDescriptor* desc)
  1440. {
  1441. CARLA_BACKEND_USE_NAMESPACE
  1442. NativePlugin::registerPlugin(desc);
  1443. }
  1444. #else // WANT_NATIVE
  1445. # warning Building without Internal plugin support
  1446. #endif
  1447. CARLA_BACKEND_START_NAMESPACE
  1448. size_t CarlaPlugin::getNativePluginCount()
  1449. {
  1450. #ifdef WANT_NATIVE
  1451. return NativePlugin::getPluginCount();
  1452. #else
  1453. return 0;
  1454. #endif
  1455. }
  1456. const PluginDescriptor* CarlaPlugin::getNativePluginDescriptor(const size_t index)
  1457. {
  1458. #ifdef WANT_NATIVE
  1459. return NativePlugin::getPluginDescriptor(index);
  1460. #else
  1461. return nullptr;
  1462. // unused
  1463. (void)index;
  1464. #endif
  1465. }
  1466. // -----------------------------------------------------------------------
  1467. CarlaPlugin* CarlaPlugin::newNative(const Initializer& init)
  1468. {
  1469. carla_debug("CarlaPlugin::newNative(%p, \"%s\", \"%s\", \"%s\")", init.engine, init.filename, init.name, init.label);
  1470. #ifdef WANT_NATIVE
  1471. NativePlugin* const plugin = new NativePlugin(init.engine, init.id);
  1472. if (! plugin->init(init.name, init.label))
  1473. {
  1474. delete plugin;
  1475. return nullptr;
  1476. }
  1477. plugin->reload();
  1478. if (init.engine->getProccessMode() == PROCESS_MODE_CONTINUOUS_RACK && (plugin->hints() & PLUGIN_CAN_FORCE_STEREO) == 0)
  1479. {
  1480. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo Internal plugins, sorry!");
  1481. delete plugin;
  1482. return nullptr;
  1483. }
  1484. return plugin;
  1485. #else
  1486. init.engine->setLastError("Internal plugins not available");
  1487. return nullptr;
  1488. #endif
  1489. }
  1490. CARLA_BACKEND_END_NAMESPACE