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.

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