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.

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