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.

953 lines
28KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2012-2015 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 "CarlaNativeExtUI.hpp"
  18. #include "CarlaMIDI.h"
  19. #include "CarlaThread.hpp"
  20. #include "LinkedList.hpp"
  21. #include "CarlaMathUtils.hpp"
  22. #include "Misc/Master.h"
  23. #include "Misc/MiddleWare.h"
  24. #include "Misc/Part.h"
  25. #include "Misc/Util.h"
  26. #include <ctime>
  27. #include <set>
  28. #include <string>
  29. #include "juce_audio_basics.h"
  30. using juce::roundToIntAccurate;
  31. using juce::FloatVectorOperations;
  32. // -----------------------------------------------------------------------
  33. class ZynAddSubFxPrograms
  34. {
  35. public:
  36. ZynAddSubFxPrograms() noexcept
  37. : fInitiated(false),
  38. #ifdef CARLA_PROPER_CPP11_SUPPORT
  39. fRetProgram({0, 0, nullptr}),
  40. #endif
  41. fPrograms() {}
  42. ~ZynAddSubFxPrograms() noexcept
  43. {
  44. if (! fInitiated)
  45. return;
  46. for (LinkedList<const ProgramInfo*>::Itenerator it = fPrograms.begin2(); it.valid(); it.next())
  47. {
  48. const ProgramInfo* const& pInfo(it.getValue(nullptr));
  49. delete pInfo;
  50. }
  51. fPrograms.clear();
  52. }
  53. void initIfNeeded()
  54. {
  55. if (fInitiated)
  56. return;
  57. fInitiated = true;
  58. fPrograms.append(new ProgramInfo(0, 0, "default", ""));
  59. Config config;
  60. config.init();
  61. SYNTH_T synth;
  62. Master master(synth, &config);
  63. // refresh banks
  64. master.bank.rescanforbanks();
  65. for (std::size_t i=0, size=master.bank.banks.size(); i<size; ++i)
  66. {
  67. const std::string dir(master.bank.banks[i].dir);
  68. if (dir.empty())
  69. continue;
  70. master.bank.loadbank(dir);
  71. for (uint ninstrument = 0; ninstrument < BANK_SIZE; ++ninstrument)
  72. {
  73. const Bank::ins_t& instrument(master.bank.ins[ninstrument]);
  74. if (instrument.name.empty() || instrument.name[0] == ' ')
  75. continue;
  76. fPrograms.append(new ProgramInfo(i+1, ninstrument, instrument.name.c_str(), instrument.filename.c_str()));
  77. }
  78. }
  79. }
  80. uint32_t getNativeMidiProgramCount() const noexcept
  81. {
  82. return static_cast<uint32_t>(fPrograms.count());
  83. }
  84. const NativeMidiProgram* getNativeMidiProgramInfo(const uint32_t index) const noexcept
  85. {
  86. if (index >= fPrograms.count())
  87. return nullptr;
  88. const ProgramInfo* const pInfo(fPrograms.getAt(index, nullptr));
  89. CARLA_SAFE_ASSERT_RETURN(pInfo != nullptr, nullptr);
  90. fRetProgram.bank = pInfo->bank;
  91. fRetProgram.program = pInfo->prog;
  92. fRetProgram.name = pInfo->name;
  93. return &fRetProgram;
  94. }
  95. const char* getZynProgramFilename(const uint32_t bank, const uint32_t program) const noexcept
  96. {
  97. for (LinkedList<const ProgramInfo*>::Itenerator it = fPrograms.begin2(); it.valid(); it.next())
  98. {
  99. const ProgramInfo* const& pInfo(it.getValue(nullptr));
  100. if (pInfo->bank != bank)
  101. continue;
  102. if (pInfo->prog != program)
  103. continue;
  104. return pInfo->filename;
  105. }
  106. return nullptr;
  107. }
  108. private:
  109. struct ProgramInfo {
  110. uint32_t bank;
  111. uint32_t prog;
  112. const char* name;
  113. const char* filename;
  114. ProgramInfo(uint32_t b, uint32_t p, const char* n, const char* fn) noexcept
  115. : bank(b),
  116. prog(p),
  117. name(carla_strdup_safe(n)),
  118. filename(carla_strdup_safe(fn)) {}
  119. ~ProgramInfo() noexcept
  120. {
  121. if (name != nullptr)
  122. {
  123. delete[] name;
  124. name = nullptr;
  125. }
  126. if (filename != nullptr)
  127. {
  128. delete[] filename;
  129. filename = nullptr;
  130. }
  131. }
  132. #ifdef CARLA_PROPER_CPP11_SUPPORT
  133. ProgramInfo() = delete;
  134. ProgramInfo(ProgramInfo&) = delete;
  135. ProgramInfo(const ProgramInfo&) = delete;
  136. ProgramInfo& operator=(ProgramInfo&);
  137. ProgramInfo& operator=(const ProgramInfo&);
  138. #endif
  139. };
  140. bool fInitiated;
  141. mutable NativeMidiProgram fRetProgram;
  142. LinkedList<const ProgramInfo*> fPrograms;
  143. CARLA_PREVENT_HEAP_ALLOCATION
  144. CARLA_DECLARE_NON_COPY_CLASS(ZynAddSubFxPrograms)
  145. };
  146. static ZynAddSubFxPrograms sPrograms;
  147. // -----------------------------------------------------------------------
  148. class ZynAddSubFxPlugin : public NativePluginAndUiClass,
  149. private CarlaThread
  150. {
  151. public:
  152. enum Parameters {
  153. kParamPart01Enabled ,
  154. kParamPart16Enabled = kParamPart01Enabled + 15,
  155. kParamPart01Volume,
  156. kParamPart16Volume = kParamPart01Volume + 15,
  157. kParamPart01Panning,
  158. kParamPart16Panning = kParamPart01Panning + 15,
  159. kParamFilterCutoff, // Filter Frequency
  160. kParamFilterQ, // Filter Resonance
  161. kParamBandwidth, // Bandwidth
  162. kParamModAmp, // FM Gain
  163. kParamResCenter, // Resonance center frequency
  164. kParamResBandwidth, // Resonance bandwidth
  165. kParamCount
  166. };
  167. ZynAddSubFxPlugin(const NativeHostDescriptor* const host)
  168. : NativePluginAndUiClass(host, "zynaddsubfx-ui"),
  169. fMiddleWare(nullptr),
  170. fMaster(nullptr),
  171. fSynth(),
  172. fIsActive(false),
  173. fMutex()
  174. {
  175. sPrograms.initIfNeeded();
  176. fConfig.init();
  177. // init parameters to default
  178. fParameters[kParamPart01Enabled] = 1.0f;
  179. for (int i=kParamPart16Enabled+1; --i>kParamPart01Enabled;)
  180. fParameters[i] = 0.0f;
  181. for (int i=kParamPart16Volume+1; --i>=kParamPart01Volume;)
  182. fParameters[i] = 100.0f;
  183. for (int i=kParamPart16Panning+1; --i>=kParamPart01Panning;)
  184. fParameters[i] = 64.0f;
  185. fParameters[kParamFilterCutoff] = 64.0f;
  186. fParameters[kParamFilterQ] = 64.0f;
  187. fParameters[kParamBandwidth] = 64.0f;
  188. fParameters[kParamModAmp] = 127.0f;
  189. fParameters[kParamResCenter] = 64.0f;
  190. fParameters[kParamResBandwidth] = 64.0f;
  191. fSynth.buffersize = static_cast<int>(getBufferSize());
  192. fSynth.samplerate = static_cast<uint>(getSampleRate());
  193. if (fSynth.buffersize > 32)
  194. fSynth.buffersize = 32;
  195. fSynth.alias();
  196. _initMaster();
  197. _setMasterParameters();
  198. }
  199. ~ZynAddSubFxPlugin() override
  200. {
  201. _deleteMaster();
  202. }
  203. protected:
  204. // -------------------------------------------------------------------
  205. // Plugin parameter calls
  206. uint32_t getParameterCount() const final
  207. {
  208. return kParamCount;
  209. }
  210. const NativeParameter* getParameterInfo(const uint32_t index) const override
  211. {
  212. CARLA_SAFE_ASSERT_RETURN(index < kParamCount, nullptr);
  213. static NativeParameter param;
  214. int hints = NATIVE_PARAMETER_IS_ENABLED|NATIVE_PARAMETER_IS_AUTOMABLE;
  215. param.name = nullptr;
  216. param.unit = nullptr;
  217. param.ranges.def = 64.0f;
  218. param.ranges.min = 0.0f;
  219. param.ranges.max = 127.0f;
  220. param.ranges.step = 1.0f;
  221. param.ranges.stepSmall = 1.0f;
  222. param.ranges.stepLarge = 20.0f;
  223. param.scalePointCount = 0;
  224. param.scalePoints = nullptr;
  225. if (index <= kParamPart16Enabled)
  226. {
  227. hints |= NATIVE_PARAMETER_IS_BOOLEAN;
  228. param.ranges.def = 0.0f;
  229. param.ranges.min = 0.0f;
  230. param.ranges.max = 1.0f;
  231. param.ranges.step = 1.0f;
  232. param.ranges.stepSmall = 1.0f;
  233. param.ranges.stepLarge = 1.0f;
  234. #define PARAM_PART_ENABLE_DESC(N) \
  235. case kParamPart01Enabled + N - 1: \
  236. param.name = "Part " #N " Enabled"; break;
  237. switch (index)
  238. {
  239. case kParamPart01Enabled:
  240. param.name = "Part 01 Enabled";
  241. param.ranges.def = 1.0f;
  242. break;
  243. PARAM_PART_ENABLE_DESC( 2)
  244. PARAM_PART_ENABLE_DESC( 3)
  245. PARAM_PART_ENABLE_DESC( 4)
  246. PARAM_PART_ENABLE_DESC( 5)
  247. PARAM_PART_ENABLE_DESC( 6)
  248. PARAM_PART_ENABLE_DESC( 7)
  249. PARAM_PART_ENABLE_DESC( 8)
  250. PARAM_PART_ENABLE_DESC( 9)
  251. PARAM_PART_ENABLE_DESC(10)
  252. PARAM_PART_ENABLE_DESC(11)
  253. PARAM_PART_ENABLE_DESC(12)
  254. PARAM_PART_ENABLE_DESC(13)
  255. PARAM_PART_ENABLE_DESC(14)
  256. PARAM_PART_ENABLE_DESC(15)
  257. PARAM_PART_ENABLE_DESC(16)
  258. }
  259. #undef PARAM_PART_ENABLE_DESC
  260. }
  261. else if (index <= kParamPart16Volume)
  262. {
  263. hints |= NATIVE_PARAMETER_IS_INTEGER;
  264. param.ranges.def = 100.0f;
  265. #define PARAM_PART_ENABLE_DESC(N) \
  266. case kParamPart01Volume + N - 1: \
  267. param.name = "Part " #N " Volume"; break;
  268. switch (index)
  269. {
  270. PARAM_PART_ENABLE_DESC( 1)
  271. PARAM_PART_ENABLE_DESC( 2)
  272. PARAM_PART_ENABLE_DESC( 3)
  273. PARAM_PART_ENABLE_DESC( 4)
  274. PARAM_PART_ENABLE_DESC( 5)
  275. PARAM_PART_ENABLE_DESC( 6)
  276. PARAM_PART_ENABLE_DESC( 7)
  277. PARAM_PART_ENABLE_DESC( 8)
  278. PARAM_PART_ENABLE_DESC( 9)
  279. PARAM_PART_ENABLE_DESC(10)
  280. PARAM_PART_ENABLE_DESC(11)
  281. PARAM_PART_ENABLE_DESC(12)
  282. PARAM_PART_ENABLE_DESC(13)
  283. PARAM_PART_ENABLE_DESC(14)
  284. PARAM_PART_ENABLE_DESC(15)
  285. PARAM_PART_ENABLE_DESC(16)
  286. }
  287. #undef PARAM_PART_ENABLE_DESC
  288. }
  289. else if (index <= kParamPart16Panning)
  290. {
  291. hints |= NATIVE_PARAMETER_IS_INTEGER;
  292. #define PARAM_PART_ENABLE_DESC(N) \
  293. case kParamPart01Panning + N - 1: \
  294. param.name = "Part " #N " Panning"; break;
  295. switch (index)
  296. {
  297. PARAM_PART_ENABLE_DESC( 1)
  298. PARAM_PART_ENABLE_DESC( 2)
  299. PARAM_PART_ENABLE_DESC( 3)
  300. PARAM_PART_ENABLE_DESC( 4)
  301. PARAM_PART_ENABLE_DESC( 5)
  302. PARAM_PART_ENABLE_DESC( 6)
  303. PARAM_PART_ENABLE_DESC( 7)
  304. PARAM_PART_ENABLE_DESC( 8)
  305. PARAM_PART_ENABLE_DESC( 9)
  306. PARAM_PART_ENABLE_DESC(10)
  307. PARAM_PART_ENABLE_DESC(11)
  308. PARAM_PART_ENABLE_DESC(12)
  309. PARAM_PART_ENABLE_DESC(13)
  310. PARAM_PART_ENABLE_DESC(14)
  311. PARAM_PART_ENABLE_DESC(15)
  312. PARAM_PART_ENABLE_DESC(16)
  313. }
  314. #undef PARAM_PART_ENABLE_DESC
  315. }
  316. else if (index <= kParamResBandwidth)
  317. {
  318. hints |= NATIVE_PARAMETER_IS_INTEGER;
  319. switch (index)
  320. {
  321. case kParamFilterCutoff:
  322. param.name = "Filter Cutoff";
  323. break;
  324. case kParamFilterQ:
  325. param.name = "Filter Q";
  326. break;
  327. case kParamBandwidth:
  328. param.name = "Bandwidth";
  329. break;
  330. case kParamModAmp:
  331. param.name = "FM Gain";
  332. param.ranges.def = 127.0f;
  333. break;
  334. case kParamResCenter:
  335. param.name = "Res Center Freq";
  336. break;
  337. case kParamResBandwidth:
  338. param.name = "Res Bandwidth";
  339. break;
  340. }
  341. }
  342. param.hints = static_cast<NativeParameterHints>(hints);
  343. return &param;
  344. }
  345. float getParameterValue(const uint32_t index) const final
  346. {
  347. CARLA_SAFE_ASSERT_RETURN(index < kParamCount, 0.0f);
  348. return fParameters[index];
  349. }
  350. // -------------------------------------------------------------------
  351. // Plugin midi-program calls
  352. uint32_t getMidiProgramCount() const noexcept override
  353. {
  354. return sPrograms.getNativeMidiProgramCount();
  355. }
  356. const NativeMidiProgram* getMidiProgramInfo(const uint32_t index) const noexcept override
  357. {
  358. return sPrograms.getNativeMidiProgramInfo(index);
  359. }
  360. // -------------------------------------------------------------------
  361. // Plugin state calls
  362. void setParameterValue(const uint32_t index, const float value) final
  363. {
  364. CARLA_SAFE_ASSERT_RETURN(index < kParamCount,);
  365. if (index <= kParamPart16Enabled)
  366. {
  367. fParameters[index] = (value >= 0.5f) ? 1.0f : 0.0f;
  368. fMiddleWare->transmitMsg("/echo", "ss", "OSC_URL", "");
  369. fMiddleWare->activeUrl("");
  370. char msg[24];
  371. std::sprintf(msg, "/part%i/Penabled", index-kParamPart01Enabled);
  372. fMiddleWare->transmitMsg(msg, (value >= 0.5f) ? "T" : "F");
  373. }
  374. else if (index <= kParamPart16Volume)
  375. {
  376. if (carla_isEqual(fParameters[index], value))
  377. return;
  378. fParameters[index] = std::round(carla_fixedValue(0.0f, 127.0f, value));
  379. fMiddleWare->transmitMsg("/echo", "ss", "OSC_URL", "");
  380. fMiddleWare->activeUrl("");
  381. char msg[24];
  382. std::sprintf(msg, "/part%i/Pvolume", index-kParamPart01Volume);
  383. fMiddleWare->transmitMsg(msg, "i", static_cast<int>(fParameters[index]));
  384. }
  385. else if (index <= kParamPart16Panning)
  386. {
  387. if (carla_isEqual(fParameters[index], value))
  388. return;
  389. fParameters[index] = std::round(carla_fixedValue(0.0f, 127.0f, value));
  390. fMiddleWare->transmitMsg("/echo", "ss", "OSC_URL", "");
  391. fMiddleWare->activeUrl("");
  392. char msg[24];
  393. std::sprintf(msg, "/part%i/Ppanning", index-kParamPart01Panning);
  394. fMiddleWare->transmitMsg(msg, "i", static_cast<int>(fParameters[index]));
  395. }
  396. else if (index <= kParamResBandwidth)
  397. {
  398. const MidiControllers zynControl(getZynControlFromIndex(index));
  399. CARLA_SAFE_ASSERT_RETURN(zynControl != C_NULL,);
  400. fParameters[index] = std::round(carla_fixedValue(0.0f, 127.0f, value));
  401. for (int npart=0; npart<NUM_MIDI_PARTS; ++npart)
  402. {
  403. if (fMaster->part[npart] != nullptr)
  404. fMaster->part[npart]->SetController(zynControl, static_cast<int>(value));
  405. }
  406. }
  407. }
  408. void setMidiProgram(const uint8_t channel, const uint32_t bank, const uint32_t program) override
  409. {
  410. CARLA_SAFE_ASSERT_RETURN(program < BANK_SIZE,);
  411. if (bank == 0)
  412. {
  413. // reset part to default
  414. // TODO
  415. return;
  416. }
  417. const char* const filename(sPrograms.getZynProgramFilename(bank, program));
  418. CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0',);
  419. fMiddleWare->transmitMsg("/load-part", "is", channel, filename);
  420. }
  421. void setCustomData(const char* const key, const char* const value) override
  422. {
  423. CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
  424. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  425. /**/ if (std::strcmp(key, "CarlaAlternateFile1") == 0) // xmz
  426. {
  427. fMiddleWare->transmitMsg("/load_xmz", "s", value);
  428. }
  429. else if (std::strcmp(key, "CarlaAlternateFile2") == 0) // xiz
  430. {
  431. fMiddleWare->transmitMsg("/load_xiz", "is", 0, value);
  432. }
  433. }
  434. // -------------------------------------------------------------------
  435. // Plugin process calls
  436. void activate() override
  437. {
  438. fIsActive = true;
  439. }
  440. void deactivate() override
  441. {
  442. fIsActive = false;
  443. }
  444. void process(float**, float** const outBuffer, const uint32_t frames, const NativeMidiEvent* const midiEvents, const uint32_t midiEventCount) override
  445. {
  446. if (! fMutex.tryLock())
  447. {
  448. if (! isOffline())
  449. {
  450. FloatVectorOperations::clear(outBuffer[0], static_cast<int>(frames));
  451. FloatVectorOperations::clear(outBuffer[1], static_cast<int>(frames));
  452. return;
  453. }
  454. fMutex.lock();
  455. }
  456. for (uint32_t i=0; i < midiEventCount; ++i)
  457. {
  458. const NativeMidiEvent* const midiEvent(&midiEvents[i]);
  459. const uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent->data);
  460. const char channel = MIDI_GET_CHANNEL_FROM_DATA(midiEvent->data);
  461. if (MIDI_IS_STATUS_NOTE_OFF(status))
  462. {
  463. const char note = static_cast<char>(midiEvent->data[1]);
  464. fMaster->noteOff(channel, note);
  465. }
  466. else if (MIDI_IS_STATUS_NOTE_ON(status))
  467. {
  468. const char note = static_cast<char>(midiEvent->data[1]);
  469. const char velo = static_cast<char>(midiEvent->data[2]);
  470. fMaster->noteOn(channel, note, velo);
  471. }
  472. else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status))
  473. {
  474. const char note = static_cast<char>(midiEvent->data[1]);
  475. const char pressure = static_cast<char>(midiEvent->data[2]);
  476. fMaster->polyphonicAftertouch(channel, note, pressure);
  477. }
  478. else if (MIDI_IS_STATUS_CONTROL_CHANGE(status))
  479. {
  480. // skip controls which we map to parameters
  481. if (getIndexFromZynControl(midiEvent->data[1]) != kParamCount)
  482. continue;
  483. const int control = midiEvent->data[1];
  484. const int value = midiEvent->data[2];
  485. fMaster->setController(channel, control, value);
  486. }
  487. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status))
  488. {
  489. const uint8_t lsb = midiEvent->data[1];
  490. const uint8_t msb = midiEvent->data[2];
  491. const int value = ((msb << 7) | lsb) - 8192;
  492. fMaster->setController(channel, C_pitchwheel, value);
  493. }
  494. }
  495. fMaster->GetAudioOutSamples(frames, fSynth.samplerate, outBuffer[0], outBuffer[1]);
  496. fMutex.unlock();
  497. }
  498. // -------------------------------------------------------------------
  499. // Plugin UI calls
  500. #ifdef HAVE_ZYN_UI_DEPS
  501. void uiShow(const bool show) override
  502. {
  503. if (show)
  504. {
  505. if (isPipeRunning())
  506. {
  507. const CarlaMutexLocker cml(getPipeLock());
  508. writeMessage("focus\n", 6);
  509. flushMessages();
  510. return;
  511. }
  512. carla_stdout("Trying to start UI using \"%s\"", getExtUiPath());
  513. CarlaExternalUI::setData(getExtUiPath(), fMiddleWare->getServerAddress(), getUiName());
  514. if (! CarlaExternalUI::startPipeServer(true))
  515. {
  516. uiClosed();
  517. hostUiUnavailable();
  518. }
  519. }
  520. else
  521. {
  522. CarlaExternalUI::stopPipeServer(2000);
  523. }
  524. }
  525. #endif
  526. // -------------------------------------------------------------------
  527. // Plugin state calls
  528. char* getState() const override
  529. {
  530. char* data = nullptr;
  531. if (fIsActive)
  532. {
  533. fMiddleWare->doReadOnlyOp([this, &data]{
  534. fMaster->getalldata(&data);
  535. });
  536. }
  537. else
  538. {
  539. fMaster->getalldata(&data);
  540. }
  541. return data;
  542. }
  543. void setState(const char* const data) override
  544. {
  545. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  546. const CarlaMutexLocker cml(fMutex);
  547. fMaster->putalldata(data);
  548. fMaster->applyparameters();
  549. fMiddleWare->updateResources(fMaster);
  550. _setMasterParameters();
  551. }
  552. // -------------------------------------------------------------------
  553. // Plugin dispatcher
  554. void bufferSizeChanged(const uint32_t bufferSize) final
  555. {
  556. char* const state(getState());
  557. _deleteMaster();
  558. fSynth.buffersize = static_cast<int>(bufferSize);
  559. fSynth.alias();
  560. _initMaster();
  561. setState(state);
  562. std::free(state);
  563. }
  564. void sampleRateChanged(const double sampleRate) final
  565. {
  566. char* const state(getState());
  567. _deleteMaster();
  568. fSynth.samplerate = static_cast<uint>(sampleRate);
  569. fSynth.alias();
  570. _initMaster();
  571. setState(state);
  572. std::free(state);
  573. }
  574. // -------------------------------------------------------------------
  575. private:
  576. MiddleWare* fMiddleWare;
  577. Master* fMaster;
  578. SYNTH_T fSynth;
  579. Config fConfig;
  580. bool fIsActive;
  581. float fParameters[kParamCount];
  582. CarlaMutex fMutex;
  583. static MidiControllers getZynControlFromIndex(const uint index)
  584. {
  585. switch (index)
  586. {
  587. case kParamFilterCutoff:
  588. return C_filtercutoff;
  589. case kParamFilterQ:
  590. return C_filterq;
  591. case kParamBandwidth:
  592. return C_bandwidth;
  593. case kParamModAmp:
  594. return C_fmamp;
  595. case kParamResCenter:
  596. return C_resonance_center;
  597. case kParamResBandwidth:
  598. return C_resonance_bandwidth;
  599. default:
  600. return C_NULL;
  601. }
  602. }
  603. static Parameters getIndexFromZynControl(const uint8_t control)
  604. {
  605. switch (control)
  606. {
  607. case C_filtercutoff:
  608. return kParamFilterCutoff;
  609. case C_filterq:
  610. return kParamFilterQ;
  611. case C_bandwidth:
  612. return kParamBandwidth;
  613. case C_fmamp:
  614. return kParamModAmp;
  615. case C_resonance_center:
  616. return kParamResCenter;
  617. case C_resonance_bandwidth:
  618. return kParamResBandwidth;
  619. default:
  620. return kParamCount;
  621. }
  622. }
  623. void run() noexcept override
  624. {
  625. for (;;)
  626. {
  627. if (MiddleWare* const mw = fMiddleWare)
  628. mw->tick();
  629. else
  630. break;
  631. carla_msleep(1);
  632. }
  633. }
  634. // -------------------------------------------------------------------
  635. void _initMaster()
  636. {
  637. fMiddleWare = new MiddleWare(std::move(fSynth), &fConfig);
  638. fMiddleWare->setUiCallback(__uiCallback, this);
  639. fMiddleWare->setIdleCallback(_idleCallback, this);
  640. _masterChangedCallback(fMiddleWare->spawnMaster());
  641. startThread();
  642. }
  643. void _setMasterParameters()
  644. {
  645. fMiddleWare->transmitMsg("/echo", "ss", "OSC_URL", "");
  646. fMiddleWare->activeUrl("");
  647. for (int i=kParamPart16Enabled+1; --i>=kParamPart01Enabled;)
  648. {
  649. char msg[24];
  650. std::sprintf(msg, "/part%i/Penabled", i-kParamPart01Enabled);
  651. fMiddleWare->transmitMsg(msg, (fParameters[i] >= 0.5f) ? "T" : "F");
  652. }
  653. for (int i=kParamPart16Volume+1; --i>=kParamPart01Volume;)
  654. {
  655. char msg[24];
  656. std::sprintf(msg, "/part%i/Pvolume", i-kParamPart01Volume);
  657. fMiddleWare->transmitMsg(msg, "i", static_cast<int>(fParameters[i]));
  658. }
  659. for (int i=kParamPart16Panning+1; --i>=kParamPart01Panning;)
  660. {
  661. char msg[24];
  662. std::sprintf(msg, "/part%i/Ppanning", i-kParamPart01Panning);
  663. fMiddleWare->transmitMsg(msg, "i", static_cast<int>(fParameters[i]));
  664. }
  665. for (int i=0; i<NUM_MIDI_PARTS; ++i)
  666. {
  667. fMaster->part[i]->SetController(C_filtercutoff, static_cast<int>(fParameters[kParamFilterCutoff]));
  668. fMaster->part[i]->SetController(C_filterq, static_cast<int>(fParameters[kParamFilterQ]));
  669. fMaster->part[i]->SetController(C_bandwidth, static_cast<int>(fParameters[kParamBandwidth]));
  670. fMaster->part[i]->SetController(C_fmamp, static_cast<int>(fParameters[kParamModAmp]));
  671. fMaster->part[i]->SetController(C_resonance_center, static_cast<int>(fParameters[kParamResCenter]));
  672. fMaster->part[i]->SetController(C_resonance_bandwidth, static_cast<int>(fParameters[kParamResBandwidth]));
  673. }
  674. }
  675. void _deleteMaster()
  676. {
  677. fMaster = nullptr;
  678. MiddleWare* const tmp(fMiddleWare);
  679. fMiddleWare = nullptr;
  680. stopThread(1000);
  681. delete tmp;
  682. }
  683. void _masterChangedCallback(Master* m)
  684. {
  685. fMaster = m;
  686. fMaster->setMasterChangedCallback(__masterChangedCallback, this);
  687. }
  688. static void __masterChangedCallback(void* ptr, Master* m)
  689. {
  690. ((ZynAddSubFxPlugin*)ptr)->_masterChangedCallback(m);
  691. }
  692. void _uiCallback(const char* const msg)
  693. {
  694. if (std::strncmp(msg, "/part", 5) != 0)
  695. return;
  696. const char* msgtmp = msg;
  697. msgtmp += 5;
  698. CARLA_SAFE_ASSERT_RETURN( msgtmp[0] >= '0' && msgtmp[0] <= '9',);
  699. CARLA_SAFE_ASSERT_RETURN((msgtmp[1] >= '0' && msgtmp[1] <= '9') || msgtmp[1] == '/',);
  700. char partnstr[3] = { '\0', '\0', '\0' };
  701. partnstr[0] = msgtmp[0];
  702. ++msgtmp;
  703. if (msgtmp[0] >= '0' && msgtmp[0] <= '9')
  704. {
  705. partnstr[1] = msgtmp[0];
  706. ++msgtmp;
  707. }
  708. const int partn = std::atoi(partnstr);
  709. ++msgtmp;
  710. /**/ if (std::strcmp(msgtmp, "Penabled") == 0)
  711. {
  712. const int index = kParamPart01Enabled+partn;
  713. const bool enbl = rtosc_argument(msg,0).T;
  714. fParameters[index] = enbl ? 1.0f : 0.0f;
  715. uiParameterChanged(kParamPart01Enabled+partn, enbl ? 1.0f : 0.0f);
  716. }
  717. else if (std::strcmp(msgtmp, "Pvolume") == 0)
  718. {
  719. const int index = kParamPart01Volume+partn;
  720. const int value = rtosc_argument(msg,0).i;
  721. fParameters[index] = value;
  722. uiParameterChanged(kParamPart01Volume+partn, value);
  723. }
  724. else if (std::strcmp(msgtmp, "Ppanning") == 0)
  725. {
  726. const int index = kParamPart01Panning+partn;
  727. const int value = rtosc_argument(msg,0).i;
  728. fParameters[index] = value;
  729. uiParameterChanged(kParamPart01Panning+partn, value);
  730. }
  731. }
  732. static void __uiCallback(void* ptr, const char* msg)
  733. {
  734. ((ZynAddSubFxPlugin*)ptr)->_uiCallback(msg);
  735. }
  736. static void _idleCallback(void* ptr)
  737. {
  738. ((ZynAddSubFxPlugin*)ptr)->hostGiveIdle();
  739. }
  740. // -------------------------------------------------------------------
  741. PluginClassEND(ZynAddSubFxPlugin)
  742. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ZynAddSubFxPlugin)
  743. };
  744. // -----------------------------------------------------------------------
  745. static const NativePluginDescriptor zynaddsubfxDesc = {
  746. /* category */ NATIVE_PLUGIN_CATEGORY_SYNTH,
  747. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_SYNTH
  748. #ifdef HAVE_ZYN_UI_DEPS
  749. |NATIVE_PLUGIN_HAS_UI
  750. #endif
  751. |NATIVE_PLUGIN_USES_MULTI_PROGS
  752. |NATIVE_PLUGIN_USES_STATE),
  753. /* supports */ static_cast<NativePluginSupports>(NATIVE_PLUGIN_SUPPORTS_CONTROL_CHANGES
  754. |NATIVE_PLUGIN_SUPPORTS_NOTE_AFTERTOUCH
  755. |NATIVE_PLUGIN_SUPPORTS_PITCHBEND
  756. |NATIVE_PLUGIN_SUPPORTS_ALL_SOUND_OFF),
  757. /* audioIns */ 0,
  758. /* audioOuts */ 2,
  759. /* midiIns */ 1,
  760. /* midiOuts */ 0,
  761. /* paramIns */ ZynAddSubFxPlugin::kParamCount,
  762. /* paramOuts */ 0,
  763. /* name */ "ZynAddSubFX",
  764. /* label */ "zynaddsubfx",
  765. /* maker */ "falkTX, Mark McCurry, Nasca Octavian Paul",
  766. /* copyright */ "GNU GPL v2+",
  767. PluginDescriptorFILL(ZynAddSubFxPlugin)
  768. };
  769. // -----------------------------------------------------------------------
  770. CARLA_EXPORT
  771. void carla_register_native_plugin_zynaddsubfx_synth();
  772. CARLA_EXPORT
  773. void carla_register_native_plugin_zynaddsubfx_synth()
  774. {
  775. carla_register_native_plugin(&zynaddsubfxDesc);
  776. }
  777. // -----------------------------------------------------------------------