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.

984 lines
30KB

  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,
  445. const NativeMidiEvent* const midiEvents, const uint32_t midiEventCount) override
  446. {
  447. if (! fMutex.tryLock())
  448. {
  449. if (! isOffline())
  450. {
  451. FloatVectorOperations::clear(outBuffer[0], static_cast<int>(frames));
  452. FloatVectorOperations::clear(outBuffer[1], static_cast<int>(frames));
  453. return;
  454. }
  455. fMutex.lock();
  456. }
  457. // the following code is based on ZynAddSubFX DSSI plugin code by Stephen G. Parry
  458. uint32_t fromFrame = 0;
  459. uint32_t toFrame = 0;
  460. uint32_t midiEventIndex = 0;
  461. uint32_t nextEventFrame = 0;
  462. do {
  463. /* Find the time of the next event, if any */
  464. if (midiEvents == nullptr || midiEventIndex >= midiEventCount)
  465. nextEventFrame = (uint32_t)-1;
  466. else
  467. nextEventFrame = midiEvents[midiEventIndex].time;
  468. /* find the end of the sub-sample to be processed this time round... */
  469. /* if the next event falls within the desired sample interval... */
  470. if (nextEventFrame < frames && nextEventFrame >= toFrame)
  471. /* set the end to be at that event */
  472. toFrame = nextEventFrame;
  473. else
  474. /* ...else go for the whole remaining sample */
  475. toFrame = frames;
  476. if (fromFrame < toFrame)
  477. {
  478. // call master to fill from `fromFrame` to `toFrame`:
  479. fMaster->GetAudioOutSamples(toFrame - fromFrame, fSynth.samplerate, outBuffer[0], outBuffer[1]);
  480. // next sub-sample please...
  481. fromFrame = toFrame;
  482. }
  483. // Now process any event(s) at the current timing point
  484. for (; midiEventIndex < midiEventCount && midiEvents[midiEventIndex].time == toFrame;)
  485. {
  486. const NativeMidiEvent* const midiEvent(&midiEvents[midiEventIndex++]);
  487. const uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent->data);
  488. const char channel = MIDI_GET_CHANNEL_FROM_DATA(midiEvent->data);
  489. if (MIDI_IS_STATUS_NOTE_OFF(status))
  490. {
  491. const char note = static_cast<char>(midiEvent->data[1]);
  492. fMaster->noteOff(channel, note);
  493. }
  494. else if (MIDI_IS_STATUS_NOTE_ON(status))
  495. {
  496. const char note = static_cast<char>(midiEvent->data[1]);
  497. const char velo = static_cast<char>(midiEvent->data[2]);
  498. fMaster->noteOn(channel, note, velo);
  499. }
  500. else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status))
  501. {
  502. const char note = static_cast<char>(midiEvent->data[1]);
  503. const char pressure = static_cast<char>(midiEvent->data[2]);
  504. fMaster->polyphonicAftertouch(channel, note, pressure);
  505. }
  506. else if (MIDI_IS_STATUS_CONTROL_CHANGE(status))
  507. {
  508. // skip controls which we map to parameters
  509. if (getIndexFromZynControl(midiEvent->data[1]) != kParamCount)
  510. continue;
  511. const int control = midiEvent->data[1];
  512. const int value = midiEvent->data[2];
  513. fMaster->setController(channel, control, value);
  514. }
  515. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status))
  516. {
  517. const uint8_t lsb = midiEvent->data[1];
  518. const uint8_t msb = midiEvent->data[2];
  519. const int value = ((msb << 7) | lsb) - 8192;
  520. fMaster->setController(channel, C_pitchwheel, value);
  521. }
  522. }
  523. // Keep going until we have the desired total length of sample...
  524. } while (toFrame < frames);
  525. fMutex.unlock();
  526. }
  527. // -------------------------------------------------------------------
  528. // Plugin UI calls
  529. #ifdef HAVE_ZYN_UI_DEPS
  530. void uiShow(const bool show) override
  531. {
  532. if (show)
  533. {
  534. if (isPipeRunning())
  535. {
  536. const CarlaMutexLocker cml(getPipeLock());
  537. writeMessage("focus\n", 6);
  538. flushMessages();
  539. return;
  540. }
  541. carla_stdout("Trying to start UI using \"%s\"", getExtUiPath());
  542. CarlaExternalUI::setData(getExtUiPath(), fMiddleWare->getServerAddress(), getUiName());
  543. if (! CarlaExternalUI::startPipeServer(true))
  544. {
  545. uiClosed();
  546. hostUiUnavailable();
  547. }
  548. }
  549. else
  550. {
  551. CarlaExternalUI::stopPipeServer(2000);
  552. }
  553. }
  554. #endif
  555. // -------------------------------------------------------------------
  556. // Plugin state calls
  557. char* getState() const override
  558. {
  559. char* data = nullptr;
  560. if (fIsActive)
  561. {
  562. fMiddleWare->doReadOnlyOp([this, &data]{
  563. fMaster->getalldata(&data);
  564. });
  565. }
  566. else
  567. {
  568. fMaster->getalldata(&data);
  569. }
  570. return data;
  571. }
  572. void setState(const char* const data) override
  573. {
  574. CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
  575. const CarlaMutexLocker cml(fMutex);
  576. fMaster->putalldata(data);
  577. fMaster->applyparameters();
  578. fMiddleWare->updateResources(fMaster);
  579. _setMasterParameters();
  580. }
  581. // -------------------------------------------------------------------
  582. // Plugin dispatcher
  583. void bufferSizeChanged(const uint32_t bufferSize) final
  584. {
  585. char* const state(getState());
  586. _deleteMaster();
  587. fSynth.buffersize = static_cast<int>(bufferSize);
  588. fSynth.alias();
  589. _initMaster();
  590. setState(state);
  591. std::free(state);
  592. }
  593. void sampleRateChanged(const double sampleRate) final
  594. {
  595. char* const state(getState());
  596. _deleteMaster();
  597. fSynth.samplerate = static_cast<uint>(sampleRate);
  598. fSynth.alias();
  599. _initMaster();
  600. setState(state);
  601. std::free(state);
  602. }
  603. // -------------------------------------------------------------------
  604. private:
  605. MiddleWare* fMiddleWare;
  606. Master* fMaster;
  607. SYNTH_T fSynth;
  608. Config fConfig;
  609. bool fIsActive;
  610. float fParameters[kParamCount];
  611. CarlaMutex fMutex;
  612. static MidiControllers getZynControlFromIndex(const uint index)
  613. {
  614. switch (index)
  615. {
  616. case kParamFilterCutoff:
  617. return C_filtercutoff;
  618. case kParamFilterQ:
  619. return C_filterq;
  620. case kParamBandwidth:
  621. return C_bandwidth;
  622. case kParamModAmp:
  623. return C_fmamp;
  624. case kParamResCenter:
  625. return C_resonance_center;
  626. case kParamResBandwidth:
  627. return C_resonance_bandwidth;
  628. default:
  629. return C_NULL;
  630. }
  631. }
  632. static Parameters getIndexFromZynControl(const uint8_t control)
  633. {
  634. switch (control)
  635. {
  636. case C_filtercutoff:
  637. return kParamFilterCutoff;
  638. case C_filterq:
  639. return kParamFilterQ;
  640. case C_bandwidth:
  641. return kParamBandwidth;
  642. case C_fmamp:
  643. return kParamModAmp;
  644. case C_resonance_center:
  645. return kParamResCenter;
  646. case C_resonance_bandwidth:
  647. return kParamResBandwidth;
  648. default:
  649. return kParamCount;
  650. }
  651. }
  652. void run() noexcept override
  653. {
  654. for (; ! shouldThreadExit();)
  655. {
  656. try {
  657. fMiddleWare->tick();
  658. } CARLA_SAFE_EXCEPTION("ZynAddSubFX MiddleWare tick");
  659. carla_msleep(1);
  660. }
  661. }
  662. // -------------------------------------------------------------------
  663. void _initMaster()
  664. {
  665. fMiddleWare = new MiddleWare(std::move(fSynth), &fConfig);
  666. fMiddleWare->setUiCallback(__uiCallback, this);
  667. fMiddleWare->setIdleCallback(_idleCallback, this);
  668. _masterChangedCallback(fMiddleWare->spawnMaster());
  669. startThread();
  670. }
  671. void _setMasterParameters()
  672. {
  673. fMiddleWare->transmitMsg("/echo", "ss", "OSC_URL", "");
  674. fMiddleWare->activeUrl("");
  675. for (int i=kParamPart16Enabled+1; --i>=kParamPart01Enabled;)
  676. {
  677. char msg[24];
  678. std::sprintf(msg, "/part%i/Penabled", i-kParamPart01Enabled);
  679. fMiddleWare->transmitMsg(msg, (fParameters[i] >= 0.5f) ? "T" : "F");
  680. }
  681. for (int i=kParamPart16Volume+1; --i>=kParamPart01Volume;)
  682. {
  683. char msg[24];
  684. std::sprintf(msg, "/part%i/Pvolume", i-kParamPart01Volume);
  685. fMiddleWare->transmitMsg(msg, "i", static_cast<int>(fParameters[i]));
  686. }
  687. for (int i=kParamPart16Panning+1; --i>=kParamPart01Panning;)
  688. {
  689. char msg[24];
  690. std::sprintf(msg, "/part%i/Ppanning", i-kParamPart01Panning);
  691. fMiddleWare->transmitMsg(msg, "i", static_cast<int>(fParameters[i]));
  692. }
  693. for (int i=0; i<NUM_MIDI_PARTS; ++i)
  694. {
  695. fMaster->part[i]->SetController(C_filtercutoff, static_cast<int>(fParameters[kParamFilterCutoff]));
  696. fMaster->part[i]->SetController(C_filterq, static_cast<int>(fParameters[kParamFilterQ]));
  697. fMaster->part[i]->SetController(C_bandwidth, static_cast<int>(fParameters[kParamBandwidth]));
  698. fMaster->part[i]->SetController(C_fmamp, static_cast<int>(fParameters[kParamModAmp]));
  699. fMaster->part[i]->SetController(C_resonance_center, static_cast<int>(fParameters[kParamResCenter]));
  700. fMaster->part[i]->SetController(C_resonance_bandwidth, static_cast<int>(fParameters[kParamResBandwidth]));
  701. }
  702. }
  703. void _deleteMaster()
  704. {
  705. stopThread(1000);
  706. fMaster = nullptr;
  707. delete fMiddleWare;
  708. fMiddleWare = nullptr;
  709. }
  710. void _masterChangedCallback(Master* m)
  711. {
  712. fMaster = m;
  713. fMaster->setMasterChangedCallback(__masterChangedCallback, this);
  714. }
  715. static void __masterChangedCallback(void* ptr, Master* m)
  716. {
  717. ((ZynAddSubFxPlugin*)ptr)->_masterChangedCallback(m);
  718. }
  719. void _uiCallback(const char* const msg)
  720. {
  721. if (std::strncmp(msg, "/part", 5) != 0)
  722. return;
  723. const char* msgtmp = msg;
  724. msgtmp += 5;
  725. CARLA_SAFE_ASSERT_RETURN( msgtmp[0] >= '0' && msgtmp[0] <= '9',);
  726. CARLA_SAFE_ASSERT_RETURN((msgtmp[1] >= '0' && msgtmp[1] <= '9') || msgtmp[1] == '/',);
  727. char partnstr[3] = { '\0', '\0', '\0' };
  728. partnstr[0] = msgtmp[0];
  729. ++msgtmp;
  730. if (msgtmp[0] >= '0' && msgtmp[0] <= '9')
  731. {
  732. partnstr[1] = msgtmp[0];
  733. ++msgtmp;
  734. }
  735. const int partn = std::atoi(partnstr);
  736. ++msgtmp;
  737. /**/ if (std::strcmp(msgtmp, "Penabled") == 0)
  738. {
  739. const int index = kParamPart01Enabled+partn;
  740. const bool enbl = rtosc_argument(msg,0).T;
  741. fParameters[index] = enbl ? 1.0f : 0.0f;
  742. uiParameterChanged(kParamPart01Enabled+partn, enbl ? 1.0f : 0.0f);
  743. }
  744. else if (std::strcmp(msgtmp, "Pvolume") == 0)
  745. {
  746. const int index = kParamPart01Volume+partn;
  747. const int value = rtosc_argument(msg,0).i;
  748. fParameters[index] = value;
  749. uiParameterChanged(kParamPart01Volume+partn, value);
  750. }
  751. else if (std::strcmp(msgtmp, "Ppanning") == 0)
  752. {
  753. const int index = kParamPart01Panning+partn;
  754. const int value = rtosc_argument(msg,0).i;
  755. fParameters[index] = value;
  756. uiParameterChanged(kParamPart01Panning+partn, value);
  757. }
  758. }
  759. static void __uiCallback(void* ptr, const char* msg)
  760. {
  761. ((ZynAddSubFxPlugin*)ptr)->_uiCallback(msg);
  762. }
  763. static void _idleCallback(void* ptr)
  764. {
  765. ((ZynAddSubFxPlugin*)ptr)->hostGiveIdle();
  766. }
  767. // -------------------------------------------------------------------
  768. PluginClassEND(ZynAddSubFxPlugin)
  769. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ZynAddSubFxPlugin)
  770. };
  771. // -----------------------------------------------------------------------
  772. static const NativePluginDescriptor zynaddsubfxDesc = {
  773. /* category */ NATIVE_PLUGIN_CATEGORY_SYNTH,
  774. /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_SYNTH
  775. #ifdef HAVE_ZYN_UI_DEPS
  776. |NATIVE_PLUGIN_HAS_UI
  777. #endif
  778. |NATIVE_PLUGIN_USES_MULTI_PROGS
  779. |NATIVE_PLUGIN_USES_STATE),
  780. /* supports */ static_cast<NativePluginSupports>(NATIVE_PLUGIN_SUPPORTS_CONTROL_CHANGES
  781. |NATIVE_PLUGIN_SUPPORTS_NOTE_AFTERTOUCH
  782. |NATIVE_PLUGIN_SUPPORTS_PITCHBEND
  783. |NATIVE_PLUGIN_SUPPORTS_ALL_SOUND_OFF),
  784. /* audioIns */ 0,
  785. /* audioOuts */ 2,
  786. /* midiIns */ 1,
  787. /* midiOuts */ 0,
  788. /* paramIns */ ZynAddSubFxPlugin::kParamCount,
  789. /* paramOuts */ 0,
  790. /* name */ "ZynAddSubFX",
  791. /* label */ "zynaddsubfx",
  792. /* maker */ "falkTX, Mark McCurry, Nasca Octavian Paul",
  793. /* copyright */ "GNU GPL v2+",
  794. PluginDescriptorFILL(ZynAddSubFxPlugin)
  795. };
  796. // -----------------------------------------------------------------------
  797. CARLA_EXPORT
  798. void carla_register_native_plugin_zynaddsubfx_synth();
  799. CARLA_EXPORT
  800. void carla_register_native_plugin_zynaddsubfx_synth()
  801. {
  802. carla_register_native_plugin(&zynaddsubfxDesc);
  803. }
  804. // -----------------------------------------------------------------------