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.

2478 lines
86KB

  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 doc/GPL.txt file.
  16. */
  17. #include "CarlaPluginInternal.hpp"
  18. #ifdef WANT_NATIVE
  19. #include "CarlaNative.h"
  20. #include "CarlaHost.h"
  21. extern const char* carla_file_callback(FileCallbackOpcode action, bool isDir, const char* title, const char* filter);
  22. CARLA_BACKEND_START_NAMESPACE
  23. #if 0
  24. }
  25. #endif
  26. struct NativePluginMidiData {
  27. uint32_t count;
  28. uint32_t* indexes;
  29. CarlaEngineEventPort** ports;
  30. NativePluginMidiData()
  31. : count(0),
  32. indexes(nullptr),
  33. ports(nullptr) {}
  34. ~NativePluginMidiData()
  35. {
  36. CARLA_ASSERT_INT(count == 0, count);
  37. CARLA_ASSERT(ports == nullptr);
  38. CARLA_ASSERT(indexes == nullptr);
  39. }
  40. void createNew(const uint32_t newCount)
  41. {
  42. CARLA_ASSERT_INT(count == 0, count);
  43. CARLA_ASSERT(ports == nullptr);
  44. CARLA_ASSERT(indexes == nullptr);
  45. CARLA_ASSERT_INT(newCount > 0, newCount);
  46. if (ports != nullptr || indexes != nullptr || newCount == 0)
  47. return;
  48. ports = new CarlaEngineEventPort*[newCount];
  49. indexes = new uint32_t[newCount];
  50. count = newCount;
  51. for (uint32_t i=0; i < newCount; ++i)
  52. ports[i] = nullptr;
  53. for (uint32_t i=0; i < newCount; ++i)
  54. indexes[i] = 0;
  55. }
  56. void clear()
  57. {
  58. if (ports != nullptr)
  59. {
  60. for (uint32_t i=0; i < count; ++i)
  61. {
  62. if (ports[i] != nullptr)
  63. {
  64. delete ports[i];
  65. ports[i] = nullptr;
  66. }
  67. }
  68. delete[] ports;
  69. ports = nullptr;
  70. }
  71. if (indexes != nullptr)
  72. {
  73. delete[] indexes;
  74. indexes = nullptr;
  75. }
  76. count = 0;
  77. }
  78. void initBuffers()
  79. {
  80. for (uint32_t i=0; i < count; ++i)
  81. {
  82. if (ports[i] != nullptr)
  83. ports[i]->initBuffer();
  84. }
  85. }
  86. CARLA_DECLARE_NON_COPY_STRUCT(NativePluginMidiData)
  87. };
  88. class NativePlugin : public CarlaPlugin
  89. {
  90. public:
  91. NativePlugin(CarlaEngine* const engine, const unsigned int id)
  92. : CarlaPlugin(engine, id),
  93. fHandle(nullptr),
  94. fHandle2(nullptr),
  95. fDescriptor(nullptr),
  96. fIsProcessing(false),
  97. fIsUiVisible(false),
  98. fAudioInBuffers(nullptr),
  99. fAudioOutBuffers(nullptr),
  100. fMidiEventCount(0)
  101. {
  102. carla_debug("NativePlugin::NativePlugin(%p, %i)", engine, id);
  103. carla_fill<int32_t>(fCurMidiProgs, MAX_MIDI_CHANNELS, 0);
  104. carla_zeroStruct<NativeMidiEvent>(fMidiEvents, kPluginMaxMidiEvents*2);
  105. carla_zeroStruct<NativeTimeInfo>(fTimeInfo);
  106. fHost.handle = this;
  107. fHost.resourceDir = carla_strdup((const char*)engine->getOptions().resourceDir);
  108. fHost.uiName = nullptr;
  109. fHost.get_buffer_size = carla_host_get_buffer_size;
  110. fHost.get_sample_rate = carla_host_get_sample_rate;
  111. fHost.is_offline = carla_host_is_offline;
  112. fHost.get_time_info = carla_host_get_time_info;
  113. fHost.write_midi_event = carla_host_write_midi_event;
  114. fHost.ui_parameter_changed = carla_host_ui_parameter_changed;
  115. fHost.ui_custom_data_changed = carla_host_ui_custom_data_changed;
  116. fHost.ui_closed = carla_host_ui_closed;
  117. fHost.ui_open_file = carla_host_ui_open_file;
  118. fHost.ui_save_file = carla_host_ui_save_file;
  119. fHost.dispatcher = carla_host_dispatcher;
  120. }
  121. ~NativePlugin() override
  122. {
  123. carla_debug("NativePlugin::~NativePlugin()");
  124. // close UI
  125. if (pData->hints & PLUGIN_HAS_UI)
  126. {
  127. if (fIsUiVisible && fDescriptor != nullptr && fDescriptor->ui_show != nullptr && fHandle != nullptr)
  128. fDescriptor->ui_show(fHandle, false);
  129. }
  130. pData->singleMutex.lock();
  131. pData->masterMutex.lock();
  132. if (pData->client != nullptr && pData->client->isActive())
  133. pData->client->deactivate();
  134. CARLA_ASSERT(! fIsProcessing);
  135. if (pData->active)
  136. {
  137. deactivate();
  138. pData->active = false;
  139. }
  140. if (fDescriptor != nullptr)
  141. {
  142. if (fDescriptor->cleanup != nullptr)
  143. {
  144. if (fHandle != nullptr)
  145. fDescriptor->cleanup(fHandle);
  146. if (fHandle2 != nullptr)
  147. fDescriptor->cleanup(fHandle2);
  148. }
  149. fHandle = nullptr;
  150. fHandle2 = nullptr;
  151. fDescriptor = nullptr;
  152. }
  153. if (fHost.resourceDir != nullptr)
  154. {
  155. delete[] fHost.resourceDir;
  156. fHost.resourceDir = nullptr;
  157. }
  158. if (fHost.uiName != nullptr)
  159. {
  160. delete[] fHost.uiName;
  161. fHost.uiName = nullptr;
  162. }
  163. clearBuffers();
  164. }
  165. // -------------------------------------------------------------------
  166. // Information (base)
  167. PluginType getType() const noexcept override
  168. {
  169. return PLUGIN_INTERNAL;
  170. }
  171. PluginCategory getCategory() const override
  172. {
  173. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, PLUGIN_CATEGORY_NONE);
  174. return static_cast<PluginCategory>(fDescriptor->category);
  175. }
  176. // -------------------------------------------------------------------
  177. // Information (count)
  178. uint32_t getMidiInCount() const noexcept override
  179. {
  180. return fMidiIn.count;
  181. }
  182. uint32_t getMidiOutCount() const noexcept override
  183. {
  184. return fMidiOut.count;
  185. }
  186. uint32_t getParameterScalePointCount(const uint32_t parameterId) const override
  187. {
  188. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0);
  189. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr, 0);
  190. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0);
  191. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0);
  192. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  193. return param->scalePointCount;
  194. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  195. return 0;
  196. }
  197. // -------------------------------------------------------------------
  198. // Information (current data)
  199. // nothing
  200. // -------------------------------------------------------------------
  201. // Information (per-plugin data)
  202. unsigned int getOptionsAvailable() const override
  203. {
  204. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0x0);
  205. const bool hasMidiProgs(fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_count(fHandle) > 0);
  206. unsigned int options = 0x0;
  207. if (hasMidiProgs && (fDescriptor->supports & ::PLUGIN_SUPPORTS_PROGRAM_CHANGES) == 0)
  208. options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  209. if (getMidiInCount() == 0 && (fDescriptor->hints & ::PLUGIN_NEEDS_FIXED_BUFFERS) == 0)
  210. options |= PLUGIN_OPTION_FIXED_BUFFERS;
  211. if (pData->engine->getProccessMode() != ENGINE_PROCESS_MODE_CONTINUOUS_RACK)
  212. {
  213. if (pData->options & PLUGIN_OPTION_FORCE_STEREO)
  214. options |= PLUGIN_OPTION_FORCE_STEREO;
  215. else if (pData->audioIn.count <= 1 && pData->audioOut.count <= 1 && (pData->audioIn.count != 0 || pData->audioOut.count != 0))
  216. options |= PLUGIN_OPTION_FORCE_STEREO;
  217. }
  218. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_CONTROL_CHANGES)
  219. options |= PLUGIN_OPTION_SEND_CONTROL_CHANGES;
  220. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_CHANNEL_PRESSURE)
  221. options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  222. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_NOTE_AFTERTOUCH)
  223. options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  224. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_PITCHBEND)
  225. options |= PLUGIN_OPTION_SEND_PITCHBEND;
  226. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_ALL_SOUND_OFF)
  227. options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  228. return options;
  229. }
  230. float getParameterValue(const uint32_t parameterId) const override
  231. {
  232. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0.0f);
  233. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_value != nullptr, 0.0f);
  234. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0.0f);
  235. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  236. return fDescriptor->get_parameter_value(fHandle, parameterId);
  237. }
  238. float getParameterScalePointValue(const uint32_t parameterId, const uint32_t scalePointId) const override
  239. {
  240. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr, 0.0f);
  241. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr, 0.0f);
  242. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, 0.0f);
  243. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count, 0.0f);
  244. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId), 0.0f);
  245. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  246. {
  247. const NativeParameterScalePoint* scalePoint(&param->scalePoints[scalePointId]);
  248. return scalePoint->value;
  249. }
  250. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  251. return 0.0f;
  252. }
  253. void getLabel(char* const strBuf) const override
  254. {
  255. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  256. if (fDescriptor->label != nullptr)
  257. std::strncpy(strBuf, fDescriptor->label, STR_MAX);
  258. else
  259. CarlaPlugin::getLabel(strBuf);
  260. }
  261. void getMaker(char* const strBuf) const override
  262. {
  263. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  264. if (fDescriptor->maker != nullptr)
  265. std::strncpy(strBuf, fDescriptor->maker, STR_MAX);
  266. else
  267. CarlaPlugin::getMaker(strBuf);
  268. }
  269. void getCopyright(char* const strBuf) const override
  270. {
  271. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  272. if (fDescriptor->copyright != nullptr)
  273. std::strncpy(strBuf, fDescriptor->copyright, STR_MAX);
  274. else
  275. CarlaPlugin::getCopyright(strBuf);
  276. }
  277. void getRealName(char* const strBuf) const override
  278. {
  279. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  280. if (fDescriptor->name != nullptr)
  281. std::strncpy(strBuf, fDescriptor->name, STR_MAX);
  282. else
  283. CarlaPlugin::getRealName(strBuf);
  284. }
  285. void getParameterName(const uint32_t parameterId, char* const strBuf) const override
  286. {
  287. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  288. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr,);
  289. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  290. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  291. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  292. {
  293. if (param->name != nullptr)
  294. {
  295. std::strncpy(strBuf, param->name, STR_MAX);
  296. return;
  297. }
  298. carla_safe_assert("param->name != nullptr", __FILE__, __LINE__);
  299. return CarlaPlugin::getParameterName(parameterId, strBuf);
  300. }
  301. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  302. CarlaPlugin::getParameterName(parameterId, strBuf);
  303. }
  304. void getParameterText(const uint32_t parameterId, const float value, char* const strBuf) const override
  305. {
  306. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  307. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_text != nullptr,);
  308. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_value != nullptr,);
  309. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  310. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  311. if (const char* const text = fDescriptor->get_parameter_text(fHandle, parameterId, value))
  312. {
  313. std::strncpy(strBuf, text, STR_MAX);
  314. return;
  315. }
  316. carla_safe_assert("const char* const text = fDescriptor->get_parameter_text(fHandle, parameterId, value)", __FILE__, __LINE__);
  317. CarlaPlugin::getParameterText(parameterId, value, strBuf);
  318. }
  319. void getParameterUnit(const uint32_t parameterId, char* const strBuf) const override
  320. {
  321. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  322. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr,);
  323. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  324. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  325. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  326. {
  327. if (param->unit != nullptr)
  328. {
  329. std::strncpy(strBuf, param->unit, STR_MAX);
  330. return;
  331. }
  332. return CarlaPlugin::getParameterUnit(parameterId, strBuf);
  333. }
  334. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  335. CarlaPlugin::getParameterUnit(parameterId, strBuf);
  336. }
  337. void getParameterScalePointLabel(const uint32_t parameterId, const uint32_t scalePointId, char* const strBuf) const override
  338. {
  339. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  340. CARLA_SAFE_ASSERT_RETURN(fDescriptor->get_parameter_info != nullptr,);
  341. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  342. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  343. CARLA_SAFE_ASSERT_RETURN(scalePointId < getParameterScalePointCount(parameterId),);
  344. if (const NativeParameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId))
  345. {
  346. const NativeParameterScalePoint* scalePoint(&param->scalePoints[scalePointId]);
  347. if (scalePoint->label != nullptr)
  348. {
  349. std::strncpy(strBuf, scalePoint->label, STR_MAX);
  350. return;
  351. }
  352. carla_safe_assert("scalePoint->label != nullptr", __FILE__, __LINE__);
  353. return CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  354. }
  355. carla_safe_assert("const Parameter* const param = fDescriptor->get_parameter_info(fHandle, parameterId)", __FILE__, __LINE__);
  356. CarlaPlugin::getParameterScalePointLabel(parameterId, scalePointId, strBuf);
  357. }
  358. // -------------------------------------------------------------------
  359. // Set data (state)
  360. void prepareForSave() override
  361. {
  362. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  363. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  364. if (pData->midiprog.count > 0 && fDescriptor->category == ::PLUGIN_CATEGORY_SYNTH)
  365. {
  366. char strBuf[STR_MAX+1];
  367. std::snprintf(strBuf, STR_MAX, "%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i:%i",
  368. fCurMidiProgs[0], fCurMidiProgs[1], fCurMidiProgs[2], fCurMidiProgs[3],
  369. fCurMidiProgs[4], fCurMidiProgs[5], fCurMidiProgs[6], fCurMidiProgs[7],
  370. fCurMidiProgs[8], fCurMidiProgs[9], fCurMidiProgs[10], fCurMidiProgs[11],
  371. fCurMidiProgs[12], fCurMidiProgs[13], fCurMidiProgs[14], fCurMidiProgs[15]);
  372. strBuf[STR_MAX] = '\0';
  373. CarlaPlugin::setCustomData(CUSTOM_DATA_TYPE_STRING, "midiPrograms", strBuf, false);
  374. }
  375. if (fDescriptor == nullptr || fDescriptor->get_state == nullptr || (fDescriptor->hints & ::PLUGIN_USES_STATE) == 0)
  376. return;
  377. if (char* data = fDescriptor->get_state(fHandle))
  378. {
  379. CarlaPlugin::setCustomData(CUSTOM_DATA_TYPE_CHUNK, "State", data, false);
  380. std::free(data);
  381. }
  382. }
  383. // -------------------------------------------------------------------
  384. // Set data (internal stuff)
  385. void setName(const char* const newName) override
  386. {
  387. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  388. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  389. CARLA_SAFE_ASSERT_RETURN(newName != nullptr && newName[0] != '\0',);
  390. char uiName[std::strlen(newName)+6+1];
  391. std::strcpy(uiName, newName);
  392. std::strcat(uiName, " (GUI)");
  393. if (fHost.uiName != nullptr)
  394. delete[] fHost.uiName;
  395. fHost.uiName = carla_strdup(uiName);
  396. if (fDescriptor->dispatcher != nullptr)
  397. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_UI_NAME_CHANGED, 0, 0, uiName, 0.0f);
  398. CarlaPlugin::setName(newName);
  399. }
  400. void setCtrlChannel(const int8_t channel, const bool sendOsc, const bool sendCallback) override
  401. {
  402. if (channel < MAX_MIDI_CHANNELS && pData->midiprog.count > 0)
  403. pData->midiprog.current = fCurMidiProgs[channel];
  404. CarlaPlugin::setCtrlChannel(channel, sendOsc, sendCallback);
  405. }
  406. // -------------------------------------------------------------------
  407. // Set data (plugin-specific stuff)
  408. void setParameterValue(const uint32_t parameterId, const float value, const bool sendGui, const bool sendOsc, const bool sendCallback) override
  409. {
  410. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  411. CARLA_SAFE_ASSERT_RETURN(fDescriptor->set_parameter_value != nullptr,);
  412. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  413. CARLA_SAFE_ASSERT_RETURN(parameterId < pData->param.count,);
  414. const float fixedValue(pData->param.getFixedValue(parameterId, value));
  415. fDescriptor->set_parameter_value(fHandle, parameterId, fixedValue);
  416. if (fHandle2 != nullptr)
  417. fDescriptor->set_parameter_value(fHandle2, parameterId, fixedValue);
  418. CarlaPlugin::setParameterValue(parameterId, fixedValue, sendGui, sendOsc, sendCallback);
  419. }
  420. void setCustomData(const char* const type, const char* const key, const char* const value, const bool sendGui) override
  421. {
  422. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  423. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  424. CARLA_SAFE_ASSERT_RETURN(type != nullptr && type[0] != '\0',);
  425. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  426. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  427. carla_debug("NativePlugin::setCustomData(%s, %s, %s, %s)", type, key, value, bool2str(sendGui));
  428. if (std::strcmp(type, CUSTOM_DATA_TYPE_STRING) != 0 && std::strcmp(type, CUSTOM_DATA_TYPE_CHUNK) != 0)
  429. return carla_stderr2("NativePlugin::setCustomData(\"%s\", \"%s\", \"%s\", %s) - type is invalid", type, key, value, bool2str(sendGui));
  430. if (std::strcmp(type, CUSTOM_DATA_TYPE_CHUNK) == 0)
  431. {
  432. if (fDescriptor->set_state != nullptr && (fDescriptor->hints & ::PLUGIN_USES_STATE) != 0)
  433. {
  434. const ScopedSingleProcessLocker spl(this, true);
  435. fDescriptor->set_state(fHandle, value);
  436. if (fHandle2 != nullptr)
  437. fDescriptor->set_state(fHandle2, value);
  438. }
  439. }
  440. else if (std::strcmp(key, "midiPrograms") == 0 && fDescriptor->set_midi_program != nullptr)
  441. {
  442. #if 0 // TODO
  443. QStringList midiProgramList(QString(value).split(":", QString::SkipEmptyParts));
  444. if (midiProgramList.count() == MAX_MIDI_CHANNELS)
  445. {
  446. uint i = 0;
  447. foreach (const QString& midiProg, midiProgramList)
  448. {
  449. bool ok;
  450. const uint index(midiProg.toUInt(&ok));
  451. if (ok && index < pData->midiprog.count)
  452. {
  453. const uint32_t bank = pData->midiprog.data[index].bank;
  454. const uint32_t program = pData->midiprog.data[index].program;
  455. fDescriptor->set_midi_program(fHandle, i, bank, program);
  456. if (fHandle2 != nullptr)
  457. fDescriptor->set_midi_program(fHandle2, i, bank, program);
  458. fCurMidiProgs[i] = index;
  459. if (pData->ctrlChannel == static_cast<int32_t>(i))
  460. {
  461. pData->midiprog.current = index;
  462. pData->engine->callback(ENGINE_CALLBACK_MIDI_PROGRAM_CHANGED, pData->id, index, 0, 0.0f, nullptr);
  463. }
  464. }
  465. ++i;
  466. }
  467. }
  468. #endif
  469. }
  470. else
  471. {
  472. if (fDescriptor->set_custom_data != nullptr)
  473. {
  474. fDescriptor->set_custom_data(fHandle, key, value);
  475. if (fHandle2 != nullptr)
  476. fDescriptor->set_custom_data(fHandle2, key, value);
  477. }
  478. if (sendGui && fIsUiVisible && fDescriptor->ui_set_custom_data != nullptr)
  479. fDescriptor->ui_set_custom_data(fHandle, key, value);
  480. }
  481. CarlaPlugin::setCustomData(type, key, value, sendGui);
  482. }
  483. void setMidiProgram(int32_t index, const bool sendGui, const bool sendOsc, const bool sendCallback) override
  484. {
  485. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  486. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  487. CARLA_SAFE_ASSERT_RETURN(index >= -1 && index < static_cast<int32_t>(pData->midiprog.count),);
  488. if ((pData->hints & PLUGIN_IS_SYNTH) != 0 && (pData->ctrlChannel < 0 || pData->ctrlChannel >= MAX_MIDI_CHANNELS))
  489. return;
  490. if (index >= 0)
  491. {
  492. const uint8_t channel = (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS) ? pData->ctrlChannel : 0;
  493. const uint32_t bank = pData->midiprog.data[index].bank;
  494. const uint32_t program = pData->midiprog.data[index].program;
  495. const ScopedSingleProcessLocker spl(this, (sendGui || sendOsc || sendCallback));
  496. fDescriptor->set_midi_program(fHandle, channel, bank, program);
  497. if (fHandle2 != nullptr)
  498. fDescriptor->set_midi_program(fHandle2, channel, bank, program);
  499. fCurMidiProgs[channel] = index;
  500. }
  501. CarlaPlugin::setMidiProgram(index, sendGui, sendOsc, sendCallback);
  502. }
  503. // -------------------------------------------------------------------
  504. // Set gui stuff
  505. void showCustomUI(const bool yesNo) override
  506. {
  507. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  508. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  509. if (fDescriptor->ui_show == nullptr)
  510. return;
  511. fDescriptor->ui_show(fHandle, yesNo);
  512. fIsUiVisible = yesNo;
  513. if (! yesNo)
  514. return;
  515. if (fDescriptor->ui_set_custom_data != nullptr)
  516. {
  517. for (List<CustomData>::Itenerator it = pData->custom.begin(); it.valid(); it.next())
  518. {
  519. const CustomData& cData(*it);
  520. if (std::strcmp(cData.type, CUSTOM_DATA_TYPE_STRING) == 0 && std::strcmp(cData.key, "midiPrograms") != 0)
  521. fDescriptor->ui_set_custom_data(fHandle, cData.key, cData.value);
  522. }
  523. }
  524. if (fDescriptor->ui_set_midi_program != nullptr && pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  525. {
  526. const uint32_t index = pData->midiprog.current;
  527. const uint8_t channel = (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS) ? pData->ctrlChannel : 0;
  528. const uint32_t bank = pData->midiprog.data[index].bank;
  529. const uint32_t program = pData->midiprog.data[index].program;
  530. fDescriptor->ui_set_midi_program(fHandle, channel, bank, program);
  531. }
  532. if (fDescriptor->ui_set_parameter_value != nullptr)
  533. {
  534. for (uint32_t i=0; i < pData->param.count; ++i)
  535. fDescriptor->ui_set_parameter_value(fHandle, i, fDescriptor->get_parameter_value(fHandle, i));
  536. }
  537. }
  538. void idle() override
  539. {
  540. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  541. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  542. if (fIsUiVisible && fDescriptor->ui_idle != nullptr)
  543. fDescriptor->ui_idle(fHandle);
  544. CarlaPlugin::idle();
  545. }
  546. // -------------------------------------------------------------------
  547. // Plugin state
  548. void reload() override
  549. {
  550. CARLA_SAFE_ASSERT_RETURN(pData->engine != nullptr,);
  551. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  552. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  553. carla_debug("NativePlugin::reload() - start");
  554. const EngineProcessMode processMode(pData->engine->getProccessMode());
  555. // Safely disable plugin for reload
  556. const ScopedDisabler sd(this);
  557. if (pData->active)
  558. deactivate();
  559. clearBuffers();
  560. const float sampleRate((float)pData->engine->getSampleRate());
  561. uint32_t aIns, aOuts, mIns, mOuts, params, j;
  562. bool forcedStereoIn, forcedStereoOut;
  563. forcedStereoIn = forcedStereoOut = false;
  564. bool needsCtrlIn, needsCtrlOut;
  565. needsCtrlIn = needsCtrlOut = false;
  566. aIns = fDescriptor->audioIns;
  567. aOuts = fDescriptor->audioOuts;
  568. mIns = fDescriptor->midiIns;
  569. mOuts = fDescriptor->midiOuts;
  570. params = (fDescriptor->get_parameter_count != nullptr && fDescriptor->get_parameter_info != nullptr) ? fDescriptor->get_parameter_count(fHandle) : 0;
  571. if ((pData->options & PLUGIN_OPTION_FORCE_STEREO) != 0 && (aIns == 1 || aOuts == 1) && mIns <= 1 && mOuts <= 1)
  572. {
  573. if (fHandle2 == nullptr)
  574. fHandle2 = fDescriptor->instantiate(&fHost);
  575. if (fHandle2 != nullptr)
  576. {
  577. if (aIns == 1)
  578. {
  579. aIns = 2;
  580. forcedStereoIn = true;
  581. }
  582. if (aOuts == 1)
  583. {
  584. aOuts = 2;
  585. forcedStereoOut = true;
  586. }
  587. }
  588. }
  589. if (aIns > 0)
  590. {
  591. pData->audioIn.createNew(aIns);
  592. fAudioInBuffers = new float*[aIns];
  593. for (uint32_t i=0; i < aIns; ++i)
  594. fAudioInBuffers[i] = nullptr;
  595. }
  596. if (aOuts > 0)
  597. {
  598. pData->audioOut.createNew(aOuts);
  599. fAudioOutBuffers = new float*[aOuts];
  600. needsCtrlIn = true;
  601. for (uint32_t i=0; i < aOuts; ++i)
  602. fAudioOutBuffers[i] = nullptr;
  603. }
  604. if (mIns > 0)
  605. {
  606. fMidiIn.createNew(mIns);
  607. needsCtrlIn = (mIns == 1);
  608. }
  609. if (mOuts > 0)
  610. {
  611. fMidiOut.createNew(mOuts);
  612. needsCtrlOut = (mOuts == 1);
  613. }
  614. if (params > 0)
  615. {
  616. pData->param.createNew(params);
  617. }
  618. const uint portNameSize(pData->engine->getMaxPortNameSize());
  619. CarlaString portName;
  620. // Audio Ins
  621. for (j=0; j < aIns; ++j)
  622. {
  623. portName.clear();
  624. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  625. {
  626. portName = pData->name;
  627. portName += ":";
  628. }
  629. if (aIns > 1 && ! forcedStereoIn)
  630. {
  631. portName += "input_";
  632. portName += CarlaString(j+1);
  633. }
  634. else
  635. portName += "input";
  636. portName.truncate(portNameSize);
  637. pData->audioIn.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  638. pData->audioIn.ports[j].rindex = j;
  639. if (forcedStereoIn)
  640. {
  641. portName += "_2";
  642. pData->audioIn.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, true);
  643. pData->audioIn.ports[1].rindex = j;
  644. break;
  645. }
  646. }
  647. // Audio Outs
  648. for (j=0; j < aOuts; ++j)
  649. {
  650. portName.clear();
  651. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  652. {
  653. portName = pData->name;
  654. portName += ":";
  655. }
  656. if (aOuts > 1 && ! forcedStereoOut)
  657. {
  658. portName += "output_";
  659. portName += CarlaString(j+1);
  660. }
  661. else
  662. portName += "output";
  663. portName.truncate(portNameSize);
  664. pData->audioOut.ports[j].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  665. pData->audioOut.ports[j].rindex = j;
  666. if (forcedStereoOut)
  667. {
  668. portName += "_2";
  669. pData->audioOut.ports[1].port = (CarlaEngineAudioPort*)pData->client->addPort(kEnginePortTypeAudio, portName, false);
  670. pData->audioOut.ports[1].rindex = j;
  671. break;
  672. }
  673. }
  674. // MIDI Input (only if multiple)
  675. if (mIns > 1)
  676. {
  677. for (j=0; j < mIns; ++j)
  678. {
  679. portName.clear();
  680. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  681. {
  682. portName = pData->name;
  683. portName += ":";
  684. }
  685. portName += "midi-in_";
  686. portName += CarlaString(j+1);
  687. portName.truncate(portNameSize);
  688. fMidiIn.ports[j] = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  689. fMidiIn.indexes[j] = j;
  690. }
  691. }
  692. // MIDI Output (only if multiple)
  693. if (mOuts > 1)
  694. {
  695. for (j=0; j < mOuts; ++j)
  696. {
  697. portName.clear();
  698. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  699. {
  700. portName = pData->name;
  701. portName += ":";
  702. }
  703. portName += "midi-out_";
  704. portName += CarlaString(j+1);
  705. portName.truncate(portNameSize);
  706. fMidiOut.ports[j] = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  707. fMidiOut.indexes[j] = j;
  708. }
  709. }
  710. for (j=0; j < params; ++j)
  711. {
  712. const NativeParameter* const paramInfo(fDescriptor->get_parameter_info(fHandle, j));
  713. CARLA_SAFE_ASSERT_CONTINUE(paramInfo != nullptr);
  714. pData->param.data[j].index = j;
  715. pData->param.data[j].rindex = j;
  716. pData->param.data[j].hints = 0x0;
  717. pData->param.data[j].midiChannel = 0;
  718. pData->param.data[j].midiCC = -1;
  719. float min, max, def, step, stepSmall, stepLarge;
  720. // min value
  721. min = paramInfo->ranges.min;
  722. // max value
  723. max = paramInfo->ranges.max;
  724. if (min > max)
  725. max = min;
  726. else if (max < min)
  727. min = max;
  728. if (max - min == 0.0f)
  729. {
  730. carla_stderr2("WARNING - Broken plugin parameter '%s': max - min == 0.0f", paramInfo->name);
  731. max = min + 0.1f;
  732. }
  733. // default value
  734. def = paramInfo->ranges.def;
  735. if (def < min)
  736. def = min;
  737. else if (def > max)
  738. def = max;
  739. if (paramInfo->hints & ::PARAMETER_USES_SAMPLE_RATE)
  740. {
  741. min *= sampleRate;
  742. max *= sampleRate;
  743. def *= sampleRate;
  744. pData->param.data[j].hints |= PARAMETER_USES_SAMPLERATE;
  745. }
  746. if (paramInfo->hints & ::PARAMETER_IS_BOOLEAN)
  747. {
  748. step = max - min;
  749. stepSmall = step;
  750. stepLarge = step;
  751. pData->param.data[j].hints |= PARAMETER_IS_BOOLEAN;
  752. }
  753. else if (paramInfo->hints & ::PARAMETER_IS_INTEGER)
  754. {
  755. step = 1.0f;
  756. stepSmall = 1.0f;
  757. stepLarge = 10.0f;
  758. pData->param.data[j].hints |= PARAMETER_IS_INTEGER;
  759. }
  760. else
  761. {
  762. float range = max - min;
  763. step = range/100.0f;
  764. stepSmall = range/1000.0f;
  765. stepLarge = range/10.0f;
  766. }
  767. if (paramInfo->hints & ::PARAMETER_IS_OUTPUT)
  768. {
  769. needsCtrlOut = true;
  770. }
  771. else
  772. {
  773. //pData->param.data[j].hints |= PARAMETER_IS_INPUT;
  774. needsCtrlIn = true;
  775. }
  776. // extra parameter hints
  777. if (paramInfo->hints & ::PARAMETER_IS_ENABLED)
  778. pData->param.data[j].hints |= PARAMETER_IS_ENABLED;
  779. if (paramInfo->hints & ::PARAMETER_IS_AUTOMABLE)
  780. pData->param.data[j].hints |= PARAMETER_IS_AUTOMABLE;
  781. if (paramInfo->hints & ::PARAMETER_IS_LOGARITHMIC)
  782. pData->param.data[j].hints |= PARAMETER_IS_LOGARITHMIC;
  783. if (paramInfo->hints & ::PARAMETER_USES_SCALEPOINTS)
  784. pData->param.data[j].hints |= PARAMETER_USES_SCALEPOINTS;
  785. if (paramInfo->hints & ::PARAMETER_USES_CUSTOM_TEXT)
  786. pData->param.data[j].hints |= PARAMETER_USES_CUSTOM_TEXT;
  787. pData->param.ranges[j].min = min;
  788. pData->param.ranges[j].max = max;
  789. pData->param.ranges[j].def = def;
  790. pData->param.ranges[j].step = step;
  791. pData->param.ranges[j].stepSmall = stepSmall;
  792. pData->param.ranges[j].stepLarge = stepLarge;
  793. }
  794. if (needsCtrlIn)
  795. {
  796. portName.clear();
  797. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  798. {
  799. portName = pData->name;
  800. portName += ":";
  801. }
  802. portName += "events-in";
  803. portName.truncate(portNameSize);
  804. pData->event.portIn = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, true);
  805. }
  806. if (needsCtrlOut)
  807. {
  808. portName.clear();
  809. if (processMode == ENGINE_PROCESS_MODE_SINGLE_CLIENT)
  810. {
  811. portName = pData->name;
  812. portName += ":";
  813. }
  814. portName += "events-out";
  815. portName.truncate(portNameSize);
  816. pData->event.portOut = (CarlaEngineEventPort*)pData->client->addPort(kEnginePortTypeEvent, portName, false);
  817. }
  818. if (forcedStereoIn || forcedStereoOut)
  819. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  820. else
  821. pData->options &= ~PLUGIN_OPTION_FORCE_STEREO;
  822. // plugin hints
  823. pData->hints = 0x0;
  824. if (aOuts > 0 && (aIns == aOuts || aIns == 1))
  825. pData->hints |= PLUGIN_CAN_DRYWET;
  826. if (aOuts > 0)
  827. pData->hints |= PLUGIN_CAN_VOLUME;
  828. if (aOuts >= 2 && aOuts % 2 == 0)
  829. pData->hints |= PLUGIN_CAN_BALANCE;
  830. // native plugin hints
  831. if (fDescriptor->hints & ::PLUGIN_IS_RTSAFE)
  832. pData->hints |= PLUGIN_IS_RTSAFE;
  833. if (fDescriptor->hints & ::PLUGIN_IS_SYNTH)
  834. pData->hints |= PLUGIN_IS_SYNTH;
  835. if (fDescriptor->hints & ::PLUGIN_HAS_UI)
  836. pData->hints |= PLUGIN_HAS_CUSTOM_UI;
  837. if (fDescriptor->hints & ::PLUGIN_NEEDS_SINGLE_THREAD)
  838. pData->hints |= PLUGIN_NEEDS_SINGLE_THREAD;
  839. if (fDescriptor->hints & ::PLUGIN_NEEDS_FIXED_BUFFERS)
  840. pData->hints |= PLUGIN_NEEDS_FIXED_BUFFERS;
  841. // extra plugin hints
  842. pData->extraHints = 0x0;
  843. if (aIns <= 2 && aOuts <= 2 && (aIns == aOuts || aIns == 0 || aOuts == 0) && mIns <= 1 && mOuts <= 1)
  844. pData->extraHints |= PLUGIN_EXTRA_HINT_CAN_RUN_RACK;
  845. bufferSizeChanged(pData->engine->getBufferSize());
  846. reloadPrograms(true);
  847. if (pData->active)
  848. activate();
  849. carla_debug("NativePlugin::reload() - end");
  850. }
  851. void reloadPrograms(const bool init) override
  852. {
  853. carla_debug("NativePlugin::reloadPrograms(%s)", bool2str(init));
  854. uint32_t i, oldCount = pData->midiprog.count;
  855. const int32_t current = pData->midiprog.current;
  856. // Delete old programs
  857. pData->midiprog.clear();
  858. // Query new programs
  859. uint32_t count = 0;
  860. if (fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_info != nullptr && fDescriptor->set_midi_program != nullptr)
  861. count = fDescriptor->get_midi_program_count(fHandle);
  862. if (count > 0)
  863. {
  864. pData->midiprog.createNew(count);
  865. // Update data
  866. for (i=0; i < count; ++i)
  867. {
  868. const NativeMidiProgram* const mpDesc(fDescriptor->get_midi_program_info(fHandle, i));
  869. CARLA_ASSERT(mpDesc != nullptr);
  870. CARLA_ASSERT(mpDesc->name != nullptr);
  871. pData->midiprog.data[i].bank = mpDesc->bank;
  872. pData->midiprog.data[i].program = mpDesc->program;
  873. pData->midiprog.data[i].name = carla_strdup(mpDesc->name);
  874. }
  875. }
  876. #ifndef BUILD_BRIDGE
  877. // Update OSC Names
  878. if (pData->engine->isOscControlRegistered())
  879. {
  880. pData->engine->oscSend_control_set_midi_program_count(pData->id, count);
  881. for (i=0; i < count; ++i)
  882. pData->engine->oscSend_control_set_midi_program_data(pData->id, i, pData->midiprog.data[i].bank, pData->midiprog.data[i].program, pData->midiprog.data[i].name);
  883. }
  884. #endif
  885. if (init)
  886. {
  887. if (count > 0)
  888. setMidiProgram(0, false, false, false);
  889. }
  890. else
  891. {
  892. // Check if current program is invalid
  893. bool programChanged = false;
  894. if (count == oldCount+1)
  895. {
  896. // one midi program added, probably created by user
  897. pData->midiprog.current = oldCount;
  898. programChanged = true;
  899. }
  900. else if (current < 0 && count > 0)
  901. {
  902. // programs exist now, but not before
  903. pData->midiprog.current = 0;
  904. programChanged = true;
  905. }
  906. else if (current >= 0 && count == 0)
  907. {
  908. // programs existed before, but not anymore
  909. pData->midiprog.current = -1;
  910. programChanged = true;
  911. }
  912. else if (current >= static_cast<int32_t>(count))
  913. {
  914. // current midi program > count
  915. pData->midiprog.current = 0;
  916. programChanged = true;
  917. }
  918. else
  919. {
  920. // no change
  921. pData->midiprog.current = current;
  922. }
  923. if (programChanged)
  924. setMidiProgram(pData->midiprog.current, true, true, true);
  925. pData->engine->callback(ENGINE_CALLBACK_RELOAD_PROGRAMS, pData->id, 0, 0, 0.0f, nullptr);
  926. }
  927. }
  928. // -------------------------------------------------------------------
  929. // Plugin processing
  930. void activate() override
  931. {
  932. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  933. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  934. if (fDescriptor->activate != nullptr)
  935. {
  936. fDescriptor->activate(fHandle);
  937. if (fHandle2 != nullptr)
  938. fDescriptor->activate(fHandle2);
  939. }
  940. }
  941. void deactivate() override
  942. {
  943. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  944. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  945. if (fDescriptor->deactivate != nullptr)
  946. {
  947. fDescriptor->deactivate(fHandle);
  948. if (fHandle2 != nullptr)
  949. fDescriptor->deactivate(fHandle2);
  950. }
  951. }
  952. void process(float** const inBuffer, float** const outBuffer, const uint32_t frames) override
  953. {
  954. uint32_t i/*, k*/;
  955. // --------------------------------------------------------------------------------------------------------
  956. // Check if active
  957. if (! pData->active)
  958. {
  959. // disable any output sound
  960. for (i=0; i < pData->audioOut.count; ++i)
  961. FLOAT_CLEAR(outBuffer[i], frames);
  962. return;
  963. }
  964. fMidiEventCount = 0;
  965. carla_zeroStruct<NativeMidiEvent>(fMidiEvents, kPluginMaxMidiEvents*2);
  966. // --------------------------------------------------------------------------------------------------------
  967. // Check if needs reset
  968. if (pData->needsReset)
  969. {
  970. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  971. {
  972. for (uint8_t k=0, i=MAX_MIDI_CHANNELS; k < MAX_MIDI_CHANNELS; ++k)
  973. {
  974. fMidiEvents[k].data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + k);
  975. fMidiEvents[k].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  976. fMidiEvents[k].data[2] = 0;
  977. fMidiEvents[k].size = 3;
  978. fMidiEvents[k+i].data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + k);
  979. fMidiEvents[k+i].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  980. fMidiEvents[k+i].data[2] = 0;
  981. fMidiEvents[k+i].size = 3;
  982. }
  983. fMidiEventCount = MAX_MIDI_CHANNELS*2;
  984. }
  985. else if (pData->ctrlChannel >= 0 && pData->ctrlChannel < MAX_MIDI_CHANNELS)
  986. {
  987. for (uint8_t k=0; k < MAX_MIDI_NOTE; ++k)
  988. {
  989. fMidiEvents[k].data[0] = static_cast<uint8_t>(MIDI_STATUS_NOTE_OFF + pData->ctrlChannel);
  990. fMidiEvents[k].data[1] = k;
  991. fMidiEvents[k].data[2] = 0;
  992. fMidiEvents[k].size = 3;
  993. }
  994. fMidiEventCount = MAX_MIDI_NOTE;
  995. }
  996. pData->needsReset = false;
  997. }
  998. CARLA_PROCESS_CONTINUE_CHECK;
  999. // --------------------------------------------------------------------------------------------------------
  1000. // Set TimeInfo
  1001. const EngineTimeInfo& timeInfo(pData->engine->getTimeInfo());
  1002. fTimeInfo.playing = timeInfo.playing;
  1003. fTimeInfo.frame = timeInfo.frame;
  1004. fTimeInfo.usecs = timeInfo.usecs;
  1005. if (timeInfo.valid & EngineTimeInfo::kValidBBT)
  1006. {
  1007. fTimeInfo.bbt.valid = true;
  1008. fTimeInfo.bbt.bar = timeInfo.bbt.bar;
  1009. fTimeInfo.bbt.beat = timeInfo.bbt.beat;
  1010. fTimeInfo.bbt.tick = timeInfo.bbt.tick;
  1011. fTimeInfo.bbt.barStartTick = timeInfo.bbt.barStartTick;
  1012. fTimeInfo.bbt.beatsPerBar = timeInfo.bbt.beatsPerBar;
  1013. fTimeInfo.bbt.beatType = timeInfo.bbt.beatType;
  1014. fTimeInfo.bbt.ticksPerBeat = timeInfo.bbt.ticksPerBeat;
  1015. fTimeInfo.bbt.beatsPerMinute = timeInfo.bbt.beatsPerMinute;
  1016. }
  1017. else
  1018. fTimeInfo.bbt.valid = false;
  1019. CARLA_PROCESS_CONTINUE_CHECK;
  1020. // --------------------------------------------------------------------------------------------------------
  1021. // Event Input and Processing
  1022. if (pData->event.portIn != nullptr)
  1023. {
  1024. // ----------------------------------------------------------------------------------------------------
  1025. // MIDI Input (External)
  1026. if (pData->extNotes.mutex.tryLock())
  1027. {
  1028. while (fMidiEventCount < kPluginMaxMidiEvents*2 && ! pData->extNotes.data.isEmpty())
  1029. {
  1030. const ExternalMidiNote& note(pData->extNotes.data.getFirst(true));
  1031. CARLA_ASSERT(note.channel >= 0 && note.channel < MAX_MIDI_CHANNELS);
  1032. fMidiEvents[fMidiEventCount].data[0] = note.channel + (note.velo > 0) ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF;
  1033. fMidiEvents[fMidiEventCount].data[1] = note.note;
  1034. fMidiEvents[fMidiEventCount].data[2] = note.velo;
  1035. fMidiEvents[fMidiEventCount].size = 3;
  1036. fMidiEventCount += 1;
  1037. }
  1038. pData->extNotes.mutex.unlock();
  1039. } // End of MIDI Input (External)
  1040. // ----------------------------------------------------------------------------------------------------
  1041. // Event Input (System)
  1042. bool allNotesOffSent = false;
  1043. bool sampleAccurate = (pData->options & PLUGIN_OPTION_FIXED_BUFFERS) == 0;
  1044. uint32_t time, nEvents = pData->event.portIn->getEventCount();
  1045. uint32_t startTime = 0;
  1046. uint32_t timeOffset = 0;
  1047. uint32_t nextBankId = 0;
  1048. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1049. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1050. for (i=0; i < nEvents; ++i)
  1051. {
  1052. const EngineEvent& event(pData->event.portIn->getEvent(i));
  1053. time = event.time;
  1054. if (time >= frames)
  1055. continue;
  1056. CARLA_ASSERT_INT2(time >= timeOffset, time, timeOffset);
  1057. if (time > timeOffset && sampleAccurate)
  1058. {
  1059. if (processSingle(inBuffer, outBuffer, time - timeOffset, timeOffset))
  1060. {
  1061. startTime = 0;
  1062. timeOffset = time;
  1063. if (pData->midiprog.current >= 0 && pData->midiprog.count > 0)
  1064. nextBankId = pData->midiprog.data[pData->midiprog.current].bank;
  1065. else
  1066. nextBankId = 0;
  1067. if (fMidiEventCount > 0)
  1068. {
  1069. carla_zeroStruct<NativeMidiEvent>(fMidiEvents, fMidiEventCount);
  1070. fMidiEventCount = 0;
  1071. }
  1072. }
  1073. else
  1074. startTime += timeOffset;
  1075. }
  1076. // Control change
  1077. switch (event.type)
  1078. {
  1079. case kEngineEventTypeNull:
  1080. break;
  1081. case kEngineEventTypeControl:
  1082. {
  1083. const EngineControlEvent& ctrlEvent = event.ctrl;
  1084. switch (ctrlEvent.type)
  1085. {
  1086. case kEngineControlEventTypeNull:
  1087. break;
  1088. case kEngineControlEventTypeParameter:
  1089. {
  1090. #ifndef BUILD_BRIDGE
  1091. // Control backend stuff
  1092. if (event.channel == pData->ctrlChannel)
  1093. {
  1094. float value;
  1095. if (MIDI_IS_CONTROL_BREATH_CONTROLLER(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_DRYWET) > 0)
  1096. {
  1097. value = ctrlEvent.value;
  1098. setDryWet(value, false, false);
  1099. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_DRYWET, 0, value);
  1100. }
  1101. if (MIDI_IS_CONTROL_CHANNEL_VOLUME(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_VOLUME) > 0)
  1102. {
  1103. value = ctrlEvent.value*127.0f/100.0f;
  1104. setVolume(value, false, false);
  1105. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_VOLUME, 0, value);
  1106. }
  1107. if (MIDI_IS_CONTROL_BALANCE(ctrlEvent.param) && (pData->hints & PLUGIN_CAN_BALANCE) > 0)
  1108. {
  1109. float left, right;
  1110. value = ctrlEvent.value/0.5f - 1.0f;
  1111. if (value < 0.0f)
  1112. {
  1113. left = -1.0f;
  1114. right = (value*2.0f)+1.0f;
  1115. }
  1116. else if (value > 0.0f)
  1117. {
  1118. left = (value*2.0f)-1.0f;
  1119. right = 1.0f;
  1120. }
  1121. else
  1122. {
  1123. left = -1.0f;
  1124. right = 1.0f;
  1125. }
  1126. setBalanceLeft(left, false, false);
  1127. setBalanceRight(right, false, false);
  1128. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_LEFT, 0, left);
  1129. pData->postponeRtEvent(kPluginPostRtEventParameterChange, PARAMETER_BALANCE_RIGHT, 0, right);
  1130. }
  1131. }
  1132. #endif
  1133. // Control plugin parameters
  1134. for (uint32_t k=0; k < pData->param.count; ++k)
  1135. {
  1136. if (pData->param.data[k].midiChannel != event.channel)
  1137. continue;
  1138. if (pData->param.data[k].midiCC != ctrlEvent.param)
  1139. continue;
  1140. if (pData->param.data[k].type != PARAMETER_INPUT)
  1141. continue;
  1142. if ((pData->param.data[k].hints & PARAMETER_IS_AUTOMABLE) == 0)
  1143. continue;
  1144. float value;
  1145. if (pData->param.data[k].hints & PARAMETER_IS_BOOLEAN)
  1146. {
  1147. value = (ctrlEvent.value < 0.5f) ? pData->param.ranges[k].min : pData->param.ranges[k].max;
  1148. }
  1149. else
  1150. {
  1151. value = pData->param.ranges[k].getUnnormalizedValue(ctrlEvent.value);
  1152. if (pData->param.data[k].hints & PARAMETER_IS_INTEGER)
  1153. value = std::rint(value);
  1154. }
  1155. setParameterValue(k, value, false, false, false);
  1156. pData->postponeRtEvent(kPluginPostRtEventParameterChange, static_cast<int32_t>(k), 0, value);
  1157. }
  1158. if ((pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) != 0 && ctrlEvent.param <= 0x5F)
  1159. {
  1160. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1161. continue;
  1162. fMidiEvents[fMidiEventCount].port = 0;
  1163. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1164. fMidiEvents[fMidiEventCount].data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + event.channel);
  1165. fMidiEvents[fMidiEventCount].data[1] = static_cast<uint8_t>(ctrlEvent.param);
  1166. fMidiEvents[fMidiEventCount].data[2] = uint8_t(ctrlEvent.value*127.0f);
  1167. fMidiEvents[fMidiEventCount].size = 3;
  1168. fMidiEventCount += 1;
  1169. }
  1170. break;
  1171. }
  1172. case kEngineControlEventTypeMidiBank:
  1173. if (event.channel == pData->ctrlChannel && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1174. nextBankId = ctrlEvent.param;
  1175. break;
  1176. case kEngineControlEventTypeMidiProgram:
  1177. if (event.channel < MAX_MIDI_CHANNELS && (pData->options & PLUGIN_OPTION_MAP_PROGRAM_CHANGES) != 0)
  1178. {
  1179. const uint32_t nextProgramId(ctrlEvent.param);
  1180. for (uint32_t k=0; k < pData->midiprog.count; ++k)
  1181. {
  1182. if (pData->midiprog.data[k].bank == nextBankId && pData->midiprog.data[k].program == nextProgramId)
  1183. {
  1184. fDescriptor->set_midi_program(fHandle, event.channel, nextBankId, nextProgramId);
  1185. if (fHandle2 != nullptr)
  1186. fDescriptor->set_midi_program(fHandle2, event.channel, nextBankId, nextProgramId);
  1187. fCurMidiProgs[event.channel] = k;
  1188. if (event.channel == pData->ctrlChannel)
  1189. pData->postponeRtEvent(kPluginPostRtEventMidiProgramChange, k, 0, 0.0f);
  1190. break;
  1191. }
  1192. }
  1193. }
  1194. break;
  1195. case kEngineControlEventTypeAllSoundOff:
  1196. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1197. {
  1198. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1199. continue;
  1200. fMidiEvents[fMidiEventCount].port = 0;
  1201. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1202. fMidiEvents[fMidiEventCount].data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + event.channel);
  1203. fMidiEvents[fMidiEventCount].data[1] = MIDI_CONTROL_ALL_SOUND_OFF;
  1204. fMidiEvents[fMidiEventCount].data[2] = 0;
  1205. fMidiEvents[fMidiEventCount].size = 3;
  1206. fMidiEventCount += 1;
  1207. }
  1208. break;
  1209. case kEngineControlEventTypeAllNotesOff:
  1210. if (pData->options & PLUGIN_OPTION_SEND_ALL_SOUND_OFF)
  1211. {
  1212. if (event.channel == pData->ctrlChannel && ! allNotesOffSent)
  1213. {
  1214. allNotesOffSent = true;
  1215. sendMidiAllNotesOffToCallback();
  1216. }
  1217. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1218. continue;
  1219. fMidiEvents[fMidiEventCount].port = 0;
  1220. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1221. fMidiEvents[fMidiEventCount].data[0] = static_cast<uint8_t>(MIDI_STATUS_CONTROL_CHANGE + event.channel);
  1222. fMidiEvents[fMidiEventCount].data[1] = MIDI_CONTROL_ALL_NOTES_OFF;
  1223. fMidiEvents[fMidiEventCount].data[2] = 0;
  1224. fMidiEvents[fMidiEventCount].size = 3;
  1225. fMidiEventCount += 1;
  1226. }
  1227. break;
  1228. }
  1229. break;
  1230. }
  1231. case kEngineEventTypeMidi:
  1232. {
  1233. if (fMidiEventCount >= kPluginMaxMidiEvents*2)
  1234. continue;
  1235. const EngineMidiEvent& midiEvent(event.midi);
  1236. uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent.data);
  1237. uint8_t channel = event.channel;
  1238. if (MIDI_IS_STATUS_CHANNEL_PRESSURE(status) && (pData->options & PLUGIN_OPTION_SEND_CHANNEL_PRESSURE) == 0)
  1239. continue;
  1240. if (MIDI_IS_STATUS_CONTROL_CHANGE(status) && (pData->options & PLUGIN_OPTION_SEND_CONTROL_CHANGES) == 0)
  1241. continue;
  1242. if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status) && (pData->options & PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH) == 0)
  1243. continue;
  1244. if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status) && (pData->options & PLUGIN_OPTION_SEND_PITCHBEND) == 0)
  1245. continue;
  1246. // Fix bad note-off
  1247. if (status == MIDI_STATUS_NOTE_ON && midiEvent.data[2] == 0)
  1248. status = MIDI_STATUS_NOTE_OFF;
  1249. fMidiEvents[fMidiEventCount].port = 0;
  1250. fMidiEvents[fMidiEventCount].time = sampleAccurate ? startTime : time;
  1251. fMidiEvents[fMidiEventCount].size = midiEvent.size;
  1252. fMidiEvents[fMidiEventCount].data[0] = static_cast<uint8_t>(status + channel);
  1253. fMidiEvents[fMidiEventCount].data[1] = midiEvent.data[1];
  1254. fMidiEvents[fMidiEventCount].data[2] = midiEvent.data[2];
  1255. fMidiEvents[fMidiEventCount].data[3] = midiEvent.data[3];
  1256. fMidiEventCount += 1;
  1257. if (status == MIDI_STATUS_NOTE_ON)
  1258. pData->postponeRtEvent(kPluginPostRtEventNoteOn, channel, midiEvent.data[1], midiEvent.data[2]);
  1259. else if (status == MIDI_STATUS_NOTE_OFF)
  1260. pData->postponeRtEvent(kPluginPostRtEventNoteOff, channel, midiEvent.data[1], 0.0f);
  1261. break;
  1262. }
  1263. }
  1264. }
  1265. pData->postRtEvents.trySplice();
  1266. if (frames > timeOffset)
  1267. processSingle(inBuffer, outBuffer, frames - timeOffset, timeOffset);
  1268. } // End of Event Input and Processing
  1269. // --------------------------------------------------------------------------------------------------------
  1270. // Plugin processing (no events)
  1271. else
  1272. {
  1273. processSingle(inBuffer, outBuffer, frames, 0);
  1274. } // End of Plugin processing (no events)
  1275. CARLA_PROCESS_CONTINUE_CHECK;
  1276. // --------------------------------------------------------------------------------------------------------
  1277. // Control and MIDI Output
  1278. if (fMidiOut.count > 0 || pData->event.portOut != nullptr)
  1279. {
  1280. float value, curValue;
  1281. for (uint32_t k=0; k < pData->param.count; ++k)
  1282. {
  1283. if (pData->param.data[k].type != PARAMETER_OUTPUT)
  1284. continue;
  1285. curValue = fDescriptor->get_parameter_value(fHandle, k);
  1286. pData->param.ranges[k].fixValue(curValue);
  1287. if (pData->param.data[k].midiCC > 0)
  1288. {
  1289. value = pData->param.ranges[k].getNormalizedValue(curValue);
  1290. pData->event.portOut->writeControlEvent(0, pData->param.data[k].midiChannel, kEngineControlEventTypeParameter, pData->param.data[k].midiCC, value);
  1291. }
  1292. }
  1293. // reverse lookup MIDI events
  1294. for (uint32_t k = (kPluginMaxMidiEvents*2)-1; k >= fMidiEventCount; --k)
  1295. {
  1296. if (fMidiEvents[k].data[0] == 0)
  1297. break;
  1298. const uint8_t channel = MIDI_GET_CHANNEL_FROM_DATA(fMidiEvents[k].data);
  1299. const uint8_t port = fMidiEvents[k].port;
  1300. if (pData->event.portOut != nullptr)
  1301. pData->event.portOut->writeMidiEvent(fMidiEvents[k].time, channel, port, fMidiEvents[k].size, fMidiEvents[k].data);
  1302. else if (port < fMidiOut.count)
  1303. fMidiOut.ports[port]->writeMidiEvent(fMidiEvents[k].time, channel, port, fMidiEvents[k].size, fMidiEvents[k].data);
  1304. }
  1305. } // End of Control and MIDI Output
  1306. }
  1307. bool processSingle(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t timeOffset)
  1308. {
  1309. CARLA_ASSERT(frames > 0);
  1310. if (frames == 0)
  1311. return false;
  1312. if (pData->audioIn.count > 0)
  1313. {
  1314. CARLA_ASSERT(inBuffer != nullptr);
  1315. if (inBuffer == nullptr)
  1316. return false;
  1317. }
  1318. if (pData->audioOut.count > 0)
  1319. {
  1320. CARLA_ASSERT(outBuffer != nullptr);
  1321. if (outBuffer == nullptr)
  1322. return false;
  1323. }
  1324. uint32_t i, k;
  1325. // --------------------------------------------------------------------------------------------------------
  1326. // Try lock, silence otherwise
  1327. if (pData->engine->isOffline())
  1328. {
  1329. pData->singleMutex.lock();
  1330. }
  1331. else if (! pData->singleMutex.tryLock())
  1332. {
  1333. for (i=0; i < pData->audioOut.count; ++i)
  1334. {
  1335. for (k=0; k < frames; ++k)
  1336. outBuffer[i][k+timeOffset] = 0.0f;
  1337. }
  1338. return false;
  1339. }
  1340. // --------------------------------------------------------------------------------------------------------
  1341. // Reset audio buffers
  1342. for (i=0; i < pData->audioIn.count; ++i)
  1343. {
  1344. #ifdef USE_JUCE
  1345. FloatVectorOperations::copy(fAudioInBuffers[i], inBuffer[i]+timeOffset, frames);
  1346. #else
  1347. #endif
  1348. }
  1349. for (i=0; i < pData->audioOut.count; ++i)
  1350. {
  1351. #ifdef USE_JUCE
  1352. FloatVectorOperations::clear(fAudioOutBuffers[i], frames);
  1353. #else
  1354. #endif
  1355. }
  1356. // --------------------------------------------------------------------------------------------------------
  1357. // Run plugin
  1358. fIsProcessing = true;
  1359. if (fHandle2 == nullptr)
  1360. {
  1361. fDescriptor->process(fHandle, fAudioInBuffers, fAudioOutBuffers, frames, fMidiEvents, fMidiEventCount);
  1362. }
  1363. else
  1364. {
  1365. fDescriptor->process(fHandle,
  1366. (pData->audioIn.count > 0) ? &fAudioInBuffers[0] : nullptr,
  1367. (pData->audioOut.count > 0) ? &fAudioOutBuffers[0] : nullptr,
  1368. frames, fMidiEvents, fMidiEventCount);
  1369. fDescriptor->process(fHandle2,
  1370. (pData->audioIn.count > 0) ? &fAudioInBuffers[1] : nullptr,
  1371. (pData->audioOut.count > 0) ? &fAudioOutBuffers[1] : nullptr,
  1372. frames, fMidiEvents, fMidiEventCount);
  1373. }
  1374. fIsProcessing = false;
  1375. fTimeInfo.frame += frames;
  1376. #ifndef BUILD_BRIDGE
  1377. // --------------------------------------------------------------------------------------------------------
  1378. // Post-processing (dry/wet, volume and balance)
  1379. {
  1380. const bool doDryWet = (pData->hints & PLUGIN_CAN_DRYWET) != 0 && pData->postProc.dryWet != 1.0f;
  1381. const bool doBalance = (pData->hints & PLUGIN_CAN_BALANCE) != 0 && (pData->postProc.balanceLeft != -1.0f || pData->postProc.balanceRight != 1.0f);
  1382. bool isPair;
  1383. float bufValue, oldBufLeft[doBalance ? frames : 1];
  1384. for (i=0; i < pData->audioOut.count; ++i)
  1385. {
  1386. // Dry/Wet
  1387. if (doDryWet)
  1388. {
  1389. for (k=0; k < frames; ++k)
  1390. {
  1391. bufValue = fAudioInBuffers[(pData->audioIn.count == 1) ? 0 : i][k];
  1392. fAudioOutBuffers[i][k] = (fAudioOutBuffers[i][k] * pData->postProc.dryWet) + (bufValue * (1.0f - pData->postProc.dryWet));
  1393. }
  1394. }
  1395. // Balance
  1396. if (doBalance)
  1397. {
  1398. isPair = (i % 2 == 0);
  1399. if (isPair)
  1400. {
  1401. CARLA_ASSERT(i+1 < pData->audioOut.count);
  1402. #ifdef USE_JUCE
  1403. FloatVectorOperations::copy(oldBufLeft, fAudioOutBuffers[i], frames);
  1404. #else
  1405. #endif
  1406. }
  1407. float balRangeL = (pData->postProc.balanceLeft + 1.0f)/2.0f;
  1408. float balRangeR = (pData->postProc.balanceRight + 1.0f)/2.0f;
  1409. for (k=0; k < frames; ++k)
  1410. {
  1411. if (isPair)
  1412. {
  1413. // left
  1414. fAudioOutBuffers[i][k] = oldBufLeft[k] * (1.0f - balRangeL);
  1415. fAudioOutBuffers[i][k] += fAudioOutBuffers[i+1][k] * (1.0f - balRangeR);
  1416. }
  1417. else
  1418. {
  1419. // right
  1420. fAudioOutBuffers[i][k] = fAudioOutBuffers[i][k] * balRangeR;
  1421. fAudioOutBuffers[i][k] += oldBufLeft[k] * balRangeL;
  1422. }
  1423. }
  1424. }
  1425. // Volume (and buffer copy)
  1426. {
  1427. for (k=0; k < frames; ++k)
  1428. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k] * pData->postProc.volume;
  1429. }
  1430. }
  1431. } // End of Post-processing
  1432. #else
  1433. for (i=0; i < pData->audioOut.count; ++i)
  1434. {
  1435. for (k=0; k < frames; ++k)
  1436. outBuffer[i][k+timeOffset] = fAudioOutBuffers[i][k];
  1437. }
  1438. #endif
  1439. // --------------------------------------------------------------------------------------------------------
  1440. pData->singleMutex.unlock();
  1441. return true;
  1442. }
  1443. void bufferSizeChanged(const uint32_t newBufferSize) override
  1444. {
  1445. CARLA_ASSERT_INT(newBufferSize > 0, newBufferSize);
  1446. carla_debug("NativePlugin::bufferSizeChanged(%i)", newBufferSize);
  1447. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1448. {
  1449. if (fAudioInBuffers[i] != nullptr)
  1450. delete[] fAudioInBuffers[i];
  1451. fAudioInBuffers[i] = new float[newBufferSize];
  1452. }
  1453. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1454. {
  1455. if (fAudioOutBuffers[i] != nullptr)
  1456. delete[] fAudioOutBuffers[i];
  1457. fAudioOutBuffers[i] = new float[newBufferSize];
  1458. }
  1459. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1460. {
  1461. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, newBufferSize, nullptr, 0.0f);
  1462. if (fHandle2 != nullptr)
  1463. fDescriptor->dispatcher(fHandle2, PLUGIN_OPCODE_BUFFER_SIZE_CHANGED, 0, newBufferSize, nullptr, 0.0f);
  1464. }
  1465. }
  1466. void sampleRateChanged(const double newSampleRate) override
  1467. {
  1468. CARLA_ASSERT_INT(newSampleRate > 0.0, newSampleRate);
  1469. carla_debug("NativePlugin::sampleRateChanged(%g)", newSampleRate);
  1470. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1471. {
  1472. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  1473. if (fHandle2 != nullptr)
  1474. fDescriptor->dispatcher(fHandle2, PLUGIN_OPCODE_SAMPLE_RATE_CHANGED, 0, 0, nullptr, float(newSampleRate));
  1475. }
  1476. }
  1477. void offlineModeChanged(const bool isOffline) override
  1478. {
  1479. if (fDescriptor != nullptr && fDescriptor->dispatcher != nullptr)
  1480. {
  1481. fDescriptor->dispatcher(fHandle, PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  1482. if (fHandle2 != nullptr)
  1483. fDescriptor->dispatcher(fHandle2, PLUGIN_OPCODE_OFFLINE_CHANGED, 0, isOffline ? 1 : 0, nullptr, 0.0f);
  1484. }
  1485. }
  1486. // -------------------------------------------------------------------
  1487. // Plugin buffers
  1488. void initBuffers() override
  1489. {
  1490. fMidiIn.initBuffers();
  1491. fMidiOut.initBuffers();
  1492. CarlaPlugin::initBuffers();
  1493. }
  1494. void clearBuffers() override
  1495. {
  1496. carla_debug("NativePlugin::clearBuffers() - start");
  1497. if (fAudioInBuffers != nullptr)
  1498. {
  1499. for (uint32_t i=0; i < pData->audioIn.count; ++i)
  1500. {
  1501. if (fAudioInBuffers[i] != nullptr)
  1502. {
  1503. delete[] fAudioInBuffers[i];
  1504. fAudioInBuffers[i] = nullptr;
  1505. }
  1506. }
  1507. delete[] fAudioInBuffers;
  1508. fAudioInBuffers = nullptr;
  1509. }
  1510. if (fAudioOutBuffers != nullptr)
  1511. {
  1512. for (uint32_t i=0; i < pData->audioOut.count; ++i)
  1513. {
  1514. if (fAudioOutBuffers[i] != nullptr)
  1515. {
  1516. delete[] fAudioOutBuffers[i];
  1517. fAudioOutBuffers[i] = nullptr;
  1518. }
  1519. }
  1520. delete[] fAudioOutBuffers;
  1521. fAudioOutBuffers = nullptr;
  1522. }
  1523. fMidiIn.clear();
  1524. fMidiOut.clear();
  1525. CarlaPlugin::clearBuffers();
  1526. carla_debug("NativePlugin::clearBuffers() - end");
  1527. }
  1528. // -------------------------------------------------------------------
  1529. // Post-poned UI Stuff
  1530. void uiParameterChange(const uint32_t index, const float value) override
  1531. {
  1532. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1533. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1534. CARLA_ASSERT(index < pData->param.count);
  1535. if (! fIsUiVisible)
  1536. return;
  1537. if (fDescriptor == nullptr || fHandle == nullptr)
  1538. return;
  1539. if (index >= pData->param.count)
  1540. return;
  1541. if (fDescriptor->ui_set_parameter_value != nullptr)
  1542. fDescriptor->ui_set_parameter_value(fHandle, index, value);
  1543. }
  1544. void uiMidiProgramChange(const uint32_t index) override
  1545. {
  1546. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1547. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1548. CARLA_ASSERT(index < pData->midiprog.count);
  1549. if (! fIsUiVisible)
  1550. return;
  1551. if (fDescriptor == nullptr || fHandle == nullptr)
  1552. return;
  1553. if (index >= pData->midiprog.count)
  1554. return;
  1555. if (fDescriptor->ui_set_midi_program != nullptr) // TODO
  1556. fDescriptor->ui_set_midi_program(fHandle, 0, pData->midiprog.data[index].bank, pData->midiprog.data[index].program);
  1557. }
  1558. void uiNoteOn(const uint8_t channel, const uint8_t note, const uint8_t velo) override
  1559. {
  1560. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1561. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1562. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1563. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1564. CARLA_ASSERT(velo > 0 && velo < MAX_MIDI_VALUE);
  1565. if (! fIsUiVisible)
  1566. return;
  1567. if (fDescriptor == nullptr || fHandle == nullptr)
  1568. return;
  1569. if (channel >= MAX_MIDI_CHANNELS)
  1570. return;
  1571. if (note >= MAX_MIDI_NOTE)
  1572. return;
  1573. if (velo >= MAX_MIDI_VALUE)
  1574. return;
  1575. // TODO
  1576. }
  1577. void uiNoteOff(const uint8_t channel, const uint8_t note) override
  1578. {
  1579. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  1580. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  1581. CARLA_ASSERT(channel < MAX_MIDI_CHANNELS);
  1582. CARLA_ASSERT(note < MAX_MIDI_NOTE);
  1583. if (! fIsUiVisible)
  1584. return;
  1585. if (fDescriptor == nullptr || fHandle == nullptr)
  1586. return;
  1587. if (channel >= MAX_MIDI_CHANNELS)
  1588. return;
  1589. if (note >= MAX_MIDI_NOTE)
  1590. return;
  1591. // TODO
  1592. }
  1593. // -------------------------------------------------------------------
  1594. protected:
  1595. uint32_t handleGetBufferSize() const
  1596. {
  1597. return pData->engine->getBufferSize();
  1598. }
  1599. double handleGetSampleRate() const
  1600. {
  1601. return pData->engine->getSampleRate();
  1602. }
  1603. bool handleIsOffline() const
  1604. {
  1605. return pData->engine->isOffline();
  1606. }
  1607. const NativeTimeInfo* handleGetTimeInfo() const
  1608. {
  1609. CARLA_SAFE_ASSERT_RETURN(fIsProcessing, nullptr);
  1610. return &fTimeInfo;
  1611. }
  1612. bool handleWriteMidiEvent(const NativeMidiEvent* const event)
  1613. {
  1614. CARLA_ASSERT(pData->enabled);
  1615. CARLA_ASSERT(fIsProcessing);
  1616. CARLA_ASSERT(fMidiOut.count > 0 || pData->event.portOut != nullptr);
  1617. CARLA_ASSERT(event != nullptr);
  1618. CARLA_ASSERT(event->data[0] != 0);
  1619. if (! pData->enabled)
  1620. return false;
  1621. if (fMidiOut.count == 0)
  1622. return false;
  1623. if (event == nullptr)
  1624. return false;
  1625. if (event->data[0] == 0)
  1626. return false;
  1627. if (! fIsProcessing)
  1628. {
  1629. carla_stderr2("NativePlugin::handleWriteMidiEvent(%p) - received MIDI out event outside audio thread, ignoring", event);
  1630. return false;
  1631. }
  1632. // reverse-find first free event, and put it there
  1633. for (uint32_t i=(kPluginMaxMidiEvents*2)-1; i > fMidiEventCount; --i)
  1634. {
  1635. if (fMidiEvents[i].data[0] == 0)
  1636. {
  1637. std::memcpy(&fMidiEvents[i], event, sizeof(NativeMidiEvent));
  1638. return true;
  1639. }
  1640. }
  1641. return false;
  1642. }
  1643. void handleUiParameterChanged(const uint32_t index, const float value)
  1644. {
  1645. setParameterValue(index, value, false, true, true);
  1646. }
  1647. void handleUiCustomDataChanged(const char* const key, const char* const value)
  1648. {
  1649. setCustomData(CUSTOM_DATA_TYPE_STRING, key, value, false);
  1650. }
  1651. void handleUiClosed()
  1652. {
  1653. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, 0, 0, 0.0f, nullptr);
  1654. fIsUiVisible = false;
  1655. }
  1656. const char* handleUiOpenFile(const bool isDir, const char* const title, const char* const filter)
  1657. {
  1658. return carla_file_callback(FILE_CALLBACK_OPEN, isDir, title, filter);
  1659. }
  1660. const char* handleUiSaveFile(const bool isDir, const char* const title, const char* const filter)
  1661. {
  1662. return carla_file_callback(FILE_CALLBACK_SAVE, isDir, title, filter);
  1663. }
  1664. intptr_t handleDispatcher(const NativeHostDispatcherOpcode opcode, const int32_t index, const intptr_t value, void* const ptr, const float opt)
  1665. {
  1666. carla_debug("NativePlugin::handleDispatcher(%i, %i, " P_INTPTR ", %p, %f)", opcode, index, value, ptr, opt);
  1667. intptr_t ret = 0;
  1668. switch (opcode)
  1669. {
  1670. case ::HOST_OPCODE_NULL:
  1671. break;
  1672. #ifdef BUILD_BRIDGE
  1673. case ::HOST_OPCODE_SET_VOLUME:
  1674. case ::HOST_OPCODE_SET_DRYWET:
  1675. case ::HOST_OPCODE_SET_BALANCE_LEFT:
  1676. case ::HOST_OPCODE_SET_BALANCE_RIGHT:
  1677. case ::HOST_OPCODE_SET_PANNING:
  1678. break;
  1679. #else
  1680. case ::HOST_OPCODE_SET_VOLUME:
  1681. setVolume(opt, true, true);
  1682. break;
  1683. case ::HOST_OPCODE_SET_DRYWET:
  1684. setDryWet(opt, true, true);
  1685. break;
  1686. case ::HOST_OPCODE_SET_BALANCE_LEFT:
  1687. setBalanceLeft(opt, true, true);
  1688. break;
  1689. case ::HOST_OPCODE_SET_BALANCE_RIGHT:
  1690. setBalanceRight(opt, true, true);
  1691. break;
  1692. case ::HOST_OPCODE_SET_PANNING:
  1693. setPanning(opt, true, true);
  1694. break;
  1695. #endif
  1696. case HOST_OPCODE_GET_PARAMETER_MIDI_CC:
  1697. case HOST_OPCODE_SET_PARAMETER_MIDI_CC:
  1698. // TODO
  1699. break;
  1700. case ::HOST_OPCODE_SET_PROCESS_PRECISION:
  1701. // TODO
  1702. break;
  1703. case ::HOST_OPCODE_UPDATE_PARAMETER:
  1704. break;
  1705. case ::HOST_OPCODE_UPDATE_MIDI_PROGRAM:
  1706. break;
  1707. case ::HOST_OPCODE_RELOAD_PARAMETERS:
  1708. break;
  1709. case ::HOST_OPCODE_RELOAD_MIDI_PROGRAMS:
  1710. break;
  1711. case ::HOST_OPCODE_RELOAD_ALL:
  1712. break;
  1713. case HOST_OPCODE_UI_UNAVAILABLE:
  1714. pData->engine->callback(ENGINE_CALLBACK_UI_STATE_CHANGED, pData->id, -1, 0, 0.0f, nullptr);
  1715. break;
  1716. }
  1717. return ret;
  1718. // unused for now
  1719. (void)index;
  1720. (void)value;
  1721. (void)ptr;
  1722. }
  1723. // -------------------------------------------------------------------
  1724. public:
  1725. static size_t getPluginCount()
  1726. {
  1727. return sPluginDescriptors.count();
  1728. }
  1729. static const NativePluginDescriptor* getPluginDescriptor(const size_t index)
  1730. {
  1731. CARLA_ASSERT(index < sPluginDescriptors.count());
  1732. if (index < sPluginDescriptors.count())
  1733. return sPluginDescriptors.getAt(index);
  1734. return nullptr;
  1735. }
  1736. static void registerPlugin(const NativePluginDescriptor* desc)
  1737. {
  1738. sPluginDescriptors.append(desc);
  1739. }
  1740. // -------------------------------------------------------------------
  1741. bool init(const char* const name, const char* const label)
  1742. {
  1743. // ---------------------------------------------------------------
  1744. // first checks
  1745. if (pData->engine == nullptr)
  1746. {
  1747. return false;
  1748. }
  1749. if (pData->client != nullptr)
  1750. {
  1751. pData->engine->setLastError("Plugin client is already registered");
  1752. return false;
  1753. }
  1754. if (label == nullptr && label[0] != '\0')
  1755. {
  1756. pData->engine->setLastError("null label");
  1757. return false;
  1758. }
  1759. // ---------------------------------------------------------------
  1760. // get descriptor that matches label
  1761. for (List<const NativePluginDescriptor*>::Itenerator it = sPluginDescriptors.begin(); it.valid(); it.next())
  1762. {
  1763. fDescriptor = *it;
  1764. CARLA_SAFE_ASSERT_BREAK(fDescriptor != nullptr);
  1765. if (fDescriptor->label != nullptr && std::strcmp(fDescriptor->label, label) == 0)
  1766. break;
  1767. fDescriptor = nullptr;
  1768. }
  1769. if (fDescriptor == nullptr)
  1770. {
  1771. pData->engine->setLastError("Invalid internal plugin");
  1772. return false;
  1773. }
  1774. // ---------------------------------------------------------------
  1775. // set icon
  1776. if (std::strcmp(fDescriptor->label, "audiofile") == 0)
  1777. pData->iconName = "file";
  1778. else if (std::strcmp(fDescriptor->label, "midifile") == 0)
  1779. pData->iconName = "file";
  1780. else if (std::strcmp(fDescriptor->label, "sunvoxfile") == 0)
  1781. pData->iconName = "file";
  1782. else if (std::strcmp(fDescriptor->label, "3BandEQ") == 0)
  1783. pData->iconName = "distrho";
  1784. else if (std::strcmp(fDescriptor->label, "3BandSplitter") == 0)
  1785. pData->iconName = "distrho";
  1786. else if (std::strcmp(fDescriptor->label, "Nekobi") == 0)
  1787. pData->iconName = "distrho";
  1788. else if (std::strcmp(fDescriptor->label, "Notes") == 0)
  1789. pData->iconName = "distrho";
  1790. else if (std::strcmp(fDescriptor->label, "PingPongPan") == 0)
  1791. pData->iconName = "distrho";
  1792. else if (std::strcmp(fDescriptor->label, "StereoEnhancer") == 0)
  1793. pData->iconName = "distrho";
  1794. // ---------------------------------------------------------------
  1795. // get info
  1796. if (name != nullptr)
  1797. pData->name = pData->engine->getUniquePluginName(name);
  1798. else if (fDescriptor->name != nullptr)
  1799. pData->name = pData->engine->getUniquePluginName(fDescriptor->name);
  1800. else
  1801. pData->name = pData->engine->getUniquePluginName(label);
  1802. {
  1803. CARLA_ASSERT(fHost.uiName == nullptr);
  1804. char uiName[std::strlen(pData->name)+6+1];
  1805. std::strcpy(uiName, pData->name);
  1806. std::strcat(uiName, " (GUI)");
  1807. fHost.uiName = carla_strdup(uiName);
  1808. }
  1809. // ---------------------------------------------------------------
  1810. // register client
  1811. pData->client = pData->engine->addClient(this);
  1812. if (pData->client == nullptr || ! pData->client->isOk())
  1813. {
  1814. pData->engine->setLastError("Failed to register plugin client");
  1815. return false;
  1816. }
  1817. // ---------------------------------------------------------------
  1818. // initialize plugin
  1819. fHandle = fDescriptor->instantiate(&fHost);
  1820. if (fHandle == nullptr)
  1821. {
  1822. pData->engine->setLastError("Plugin failed to initialize");
  1823. return false;
  1824. }
  1825. // ---------------------------------------------------------------
  1826. // load plugin settings
  1827. {
  1828. const bool hasMidiProgs(fDescriptor->get_midi_program_count != nullptr && fDescriptor->get_midi_program_count(fHandle) > 0);
  1829. // set default options
  1830. pData->options = 0x0;
  1831. if (hasMidiProgs && (fDescriptor->supports & ::PLUGIN_SUPPORTS_PROGRAM_CHANGES) == 0)
  1832. pData->options |= PLUGIN_OPTION_MAP_PROGRAM_CHANGES;
  1833. if (getMidiInCount() > 0 || (fDescriptor->hints & ::PLUGIN_NEEDS_FIXED_BUFFERS) != 0)
  1834. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1835. if (pData->engine->getOptions().forceStereo)
  1836. pData->options |= PLUGIN_OPTION_FORCE_STEREO;
  1837. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_CHANNEL_PRESSURE)
  1838. pData->options |= PLUGIN_OPTION_SEND_CHANNEL_PRESSURE;
  1839. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_NOTE_AFTERTOUCH)
  1840. pData->options |= PLUGIN_OPTION_SEND_NOTE_AFTERTOUCH;
  1841. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_PITCHBEND)
  1842. pData->options |= PLUGIN_OPTION_SEND_PITCHBEND;
  1843. if (fDescriptor->supports & ::PLUGIN_SUPPORTS_ALL_SOUND_OFF)
  1844. pData->options |= PLUGIN_OPTION_SEND_ALL_SOUND_OFF;
  1845. // load settings TODO
  1846. pData->identifier = "Native/";
  1847. //pData->idStr += label;
  1848. pData->options = pData->loadSettings(pData->options, getOptionsAvailable());
  1849. // ignore settings, we need this anyway
  1850. if (getMidiInCount() > 0 || (fDescriptor->hints & ::PLUGIN_NEEDS_FIXED_BUFFERS) != 0)
  1851. pData->options |= PLUGIN_OPTION_FIXED_BUFFERS;
  1852. }
  1853. return true;
  1854. }
  1855. class ScopedInitializer
  1856. {
  1857. public:
  1858. ScopedInitializer()
  1859. {
  1860. carla_register_all_plugins();
  1861. }
  1862. ~ScopedInitializer()
  1863. {
  1864. sPluginDescriptors.clear();
  1865. }
  1866. };
  1867. private:
  1868. NativePluginHandle fHandle;
  1869. NativePluginHandle fHandle2;
  1870. NativeHostDescriptor fHost;
  1871. const NativePluginDescriptor* fDescriptor;
  1872. bool fIsProcessing;
  1873. bool fIsUiVisible;
  1874. float** fAudioInBuffers;
  1875. float** fAudioOutBuffers;
  1876. uint32_t fMidiEventCount;
  1877. NativeMidiEvent fMidiEvents[kPluginMaxMidiEvents*2];
  1878. int32_t fCurMidiProgs[MAX_MIDI_CHANNELS];
  1879. NativePluginMidiData fMidiIn;
  1880. NativePluginMidiData fMidiOut;
  1881. NativeTimeInfo fTimeInfo;
  1882. static List<const NativePluginDescriptor*> sPluginDescriptors;
  1883. // -------------------------------------------------------------------
  1884. #define handlePtr ((NativePlugin*)handle)
  1885. static uint32_t carla_host_get_buffer_size(NativeHostHandle handle)
  1886. {
  1887. return handlePtr->handleGetBufferSize();
  1888. }
  1889. static double carla_host_get_sample_rate(NativeHostHandle handle)
  1890. {
  1891. return handlePtr->handleGetSampleRate();
  1892. }
  1893. static bool carla_host_is_offline(NativeHostHandle handle)
  1894. {
  1895. return handlePtr->handleIsOffline();
  1896. }
  1897. static const NativeTimeInfo* carla_host_get_time_info(NativeHostHandle handle)
  1898. {
  1899. return handlePtr->handleGetTimeInfo();
  1900. }
  1901. static bool carla_host_write_midi_event(NativeHostHandle handle, const NativeMidiEvent* event)
  1902. {
  1903. return handlePtr->handleWriteMidiEvent(event);
  1904. }
  1905. static void carla_host_ui_parameter_changed(NativeHostHandle handle, uint32_t index, float value)
  1906. {
  1907. handlePtr->handleUiParameterChanged(index, value);
  1908. }
  1909. static void carla_host_ui_custom_data_changed(NativeHostHandle handle, const char* key, const char* value)
  1910. {
  1911. handlePtr->handleUiCustomDataChanged(key, value);
  1912. }
  1913. static void carla_host_ui_closed(NativeHostHandle handle)
  1914. {
  1915. handlePtr->handleUiClosed();
  1916. }
  1917. static const char* carla_host_ui_open_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  1918. {
  1919. return handlePtr->handleUiOpenFile(isDir, title, filter);
  1920. }
  1921. static const char* carla_host_ui_save_file(NativeHostHandle handle, bool isDir, const char* title, const char* filter)
  1922. {
  1923. return handlePtr->handleUiSaveFile(isDir, title, filter);
  1924. }
  1925. static intptr_t carla_host_dispatcher(NativeHostHandle handle, NativeHostDispatcherOpcode opcode, int32_t index, intptr_t value, void* ptr, float opt)
  1926. {
  1927. return handlePtr->handleDispatcher(opcode, index, value, ptr, opt);
  1928. }
  1929. #undef handlePtr
  1930. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(NativePlugin)
  1931. };
  1932. List<const NativePluginDescriptor*> NativePlugin::sPluginDescriptors;
  1933. static const NativePlugin::ScopedInitializer _si;
  1934. CARLA_BACKEND_END_NAMESPACE
  1935. void carla_register_native_plugin(const NativePluginDescriptor* desc)
  1936. {
  1937. CARLA_BACKEND_USE_NAMESPACE
  1938. NativePlugin::registerPlugin(desc);
  1939. }
  1940. #endif // WANT_NATIVE
  1941. // -----------------------------------------------------------------------
  1942. CARLA_BACKEND_START_NAMESPACE
  1943. #ifdef WANT_NATIVE
  1944. size_t CarlaPlugin::getNativePluginCount()
  1945. {
  1946. return NativePlugin::getPluginCount();
  1947. }
  1948. const NativePluginDescriptor* CarlaPlugin::getNativePluginDescriptor(const size_t index)
  1949. {
  1950. return NativePlugin::getPluginDescriptor(index);
  1951. }
  1952. #endif
  1953. // -----------------------------------------------------------------------
  1954. CarlaPlugin* CarlaPlugin::newNative(const Initializer& init)
  1955. {
  1956. carla_debug("CarlaPlugin::newNative({%p, \"%s\", \"%s\", \"%s\"})", init.engine, init.filename, init.name, init.label);
  1957. #ifdef WANT_NATIVE
  1958. NativePlugin* const plugin(new NativePlugin(init.engine, init.id));
  1959. if (! plugin->init(init.name, init.label))
  1960. {
  1961. delete plugin;
  1962. return nullptr;
  1963. }
  1964. plugin->reload();
  1965. if (init.engine->getProccessMode() == ENGINE_PROCESS_MODE_CONTINUOUS_RACK && ! plugin->canRunInRack())
  1966. {
  1967. init.engine->setLastError("Carla's rack mode can only work with Mono or Stereo Internal plugins, sorry!");
  1968. delete plugin;
  1969. return nullptr;
  1970. }
  1971. return plugin;
  1972. #else
  1973. init.engine->setLastError("Internal plugins support not available");
  1974. return nullptr;
  1975. #endif
  1976. }
  1977. CARLA_BACKEND_END_NAMESPACE
  1978. // -----------------------------------------------------------------------