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.

1069 lines
32KB

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