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.

1906 lines
62KB

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