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.

1071 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. fProgramCount(0),
  44. fPrograms(nullptr) {}
  45. ~ZynAddSubFxPrograms() noexcept
  46. {
  47. if (! fInitiated)
  48. return;
  49. for (uint32_t i=0; i<fProgramCount; ++i)
  50. delete fPrograms[i];
  51. delete[] fPrograms;
  52. }
  53. void initIfNeeded()
  54. {
  55. if (fInitiated)
  56. return;
  57. fInitiated = true;
  58. std::vector<const ProgramInfo*> programs;
  59. programs.push_back(new ProgramInfo(0, 0, "default", ""));
  60. Config config;
  61. config.init();
  62. SYNTH_T synth;
  63. Master master(synth, &config);
  64. // refresh banks
  65. master.bank.rescanforbanks();
  66. for (std::size_t i=0, size=master.bank.banks.size(); i<size; ++i)
  67. {
  68. const std::string dir(master.bank.banks[i].dir);
  69. if (dir.empty())
  70. continue;
  71. master.bank.loadbank(dir);
  72. for (uint ninstrument = 0; ninstrument < BANK_SIZE; ++ninstrument)
  73. {
  74. const Bank::ins_t& instrument(master.bank.ins[ninstrument]);
  75. if (instrument.name.empty() || instrument.name[0] == ' ')
  76. continue;
  77. programs.push_back(new ProgramInfo(i+1, ninstrument, instrument.name.c_str(), instrument.filename.c_str()));
  78. }
  79. }
  80. fPrograms = new const ProgramInfo*[programs.size()];
  81. for (const ProgramInfo* p : programs)
  82. fPrograms[fProgramCount++] = p;
  83. }
  84. uint32_t getNativeMidiProgramCount() const noexcept
  85. {
  86. return fProgramCount;
  87. }
  88. const NativeMidiProgram* getNativeMidiProgramInfo(const uint32_t index) const noexcept
  89. {
  90. if (index >= fProgramCount)
  91. return nullptr;
  92. const ProgramInfo* const pInfo(fPrograms[index]);
  93. CARLA_SAFE_ASSERT_RETURN(pInfo != nullptr, nullptr);
  94. fRetProgram.bank = pInfo->bank;
  95. fRetProgram.program = pInfo->prog;
  96. fRetProgram.name = pInfo->name;
  97. return &fRetProgram;
  98. }
  99. const char* getZynProgramFilename(const uint32_t bank, const uint32_t program) const noexcept
  100. {
  101. for (uint32_t i=0; i<fProgramCount; ++i)
  102. {
  103. const ProgramInfo* const pInfo(fPrograms[i]);
  104. if (pInfo->bank != bank)
  105. continue;
  106. if (pInfo->prog != program)
  107. continue;
  108. return pInfo->filename;
  109. }
  110. return nullptr;
  111. }
  112. private:
  113. struct ProgramInfo {
  114. uint32_t bank;
  115. uint32_t prog;
  116. const char* name;
  117. const char* filename;
  118. ProgramInfo(uint32_t b, uint32_t p, const char* n, const char* fn) noexcept
  119. : bank(b),
  120. prog(p),
  121. name(carla_strdup_safe(n)),
  122. filename(carla_strdup_safe(fn)) {}
  123. ~ProgramInfo() noexcept
  124. {
  125. if (name != nullptr)
  126. {
  127. delete[] name;
  128. name = nullptr;
  129. }
  130. if (filename != nullptr)
  131. {
  132. delete[] filename;
  133. filename = nullptr;
  134. }
  135. }
  136. #ifdef CARLA_PROPER_CPP11_SUPPORT
  137. ProgramInfo() = delete;
  138. ProgramInfo(ProgramInfo&) = delete;
  139. ProgramInfo(const ProgramInfo&) = delete;
  140. ProgramInfo& operator=(ProgramInfo&);
  141. ProgramInfo& operator=(const ProgramInfo&);
  142. #endif
  143. };
  144. bool fInitiated;
  145. mutable NativeMidiProgram fRetProgram;
  146. uint32_t fProgramCount;
  147. const ProgramInfo** fPrograms;
  148. CARLA_PREVENT_HEAP_ALLOCATION
  149. CARLA_DECLARE_NON_COPY_CLASS(ZynAddSubFxPrograms)
  150. };
  151. static ZynAddSubFxPrograms sPrograms;
  152. // -----------------------------------------------------------------------
  153. class MiddleWareThread : private CarlaThread
  154. {
  155. public:
  156. class ScopedStopper
  157. {
  158. public:
  159. ScopedStopper(MiddleWareThread& mwt) noexcept
  160. : wasRunning(mwt.isThreadRunning()),
  161. thread(mwt),
  162. middleWare(mwt.fMiddleWare)
  163. {
  164. if (wasRunning)
  165. thread.stop();
  166. }
  167. ~ScopedStopper() noexcept
  168. {
  169. if (wasRunning)
  170. thread.start(middleWare);
  171. }
  172. void updateMiddleWare(MiddleWare* const mw) noexcept
  173. {
  174. middleWare = mw;
  175. }
  176. private:
  177. const bool wasRunning;
  178. MiddleWareThread& thread;
  179. MiddleWare* middleWare;
  180. CARLA_PREVENT_HEAP_ALLOCATION
  181. CARLA_DECLARE_NON_COPY_CLASS(ScopedStopper)
  182. };
  183. MiddleWareThread()
  184. : CarlaThread("ZynMiddleWare"),
  185. fMiddleWare(nullptr) {}
  186. void start(MiddleWare* const mw) noexcept
  187. {
  188. fMiddleWare = mw;
  189. startThread();
  190. }
  191. void stop() noexcept
  192. {
  193. stopThread(1000);
  194. fMiddleWare = nullptr;
  195. }
  196. private:
  197. MiddleWare* fMiddleWare;
  198. void run() noexcept override
  199. {
  200. for (; ! shouldThreadExit();)
  201. {
  202. CARLA_SAFE_ASSERT_RETURN(fMiddleWare != nullptr,);
  203. try {
  204. fMiddleWare->tick();
  205. } CARLA_SAFE_EXCEPTION("ZynAddSubFX MiddleWare tick");
  206. carla_msleep(1);
  207. }
  208. }
  209. CARLA_DECLARE_NON_COPY_CLASS(MiddleWareThread)
  210. };
  211. // -----------------------------------------------------------------------
  212. class ZynAddSubFxPlugin : public NativePluginAndUiClass
  213. {
  214. public:
  215. enum Parameters {
  216. kParamPart01Enabled ,
  217. kParamPart16Enabled = kParamPart01Enabled + 15,
  218. kParamPart01Volume,
  219. kParamPart16Volume = kParamPart01Volume + 15,
  220. kParamPart01Panning,
  221. kParamPart16Panning = kParamPart01Panning + 15,
  222. kParamFilterCutoff, // Filter Frequency
  223. kParamFilterQ, // Filter Resonance
  224. kParamBandwidth, // Bandwidth
  225. kParamModAmp, // FM Gain
  226. kParamResCenter, // Resonance center frequency
  227. kParamResBandwidth, // Resonance bandwidth
  228. kParamCount
  229. };
  230. ZynAddSubFxPlugin(const NativeHostDescriptor* const host)
  231. : NativePluginAndUiClass(host, "zynaddsubfx-ui"),
  232. fMiddleWare(nullptr),
  233. fMaster(nullptr),
  234. fSynth(),
  235. fDefaultState(nullptr),
  236. fMutex(),
  237. fMiddleWareThread(new MiddleWareThread())
  238. {
  239. isPlugin = true;
  240. sPrograms.initIfNeeded();
  241. fConfig.init();
  242. // init parameters to default
  243. fParameters[kParamPart01Enabled] = 1.0f;
  244. for (int i=kParamPart16Enabled+1; --i>kParamPart01Enabled;)
  245. fParameters[i] = 0.0f;
  246. for (int i=kParamPart16Volume+1; --i>=kParamPart01Volume;)
  247. fParameters[i] = 100.0f;
  248. for (int i=kParamPart16Panning+1; --i>=kParamPart01Panning;)
  249. fParameters[i] = 64.0f;
  250. fParameters[kParamFilterCutoff] = 64.0f;
  251. fParameters[kParamFilterQ] = 64.0f;
  252. fParameters[kParamBandwidth] = 64.0f;
  253. fParameters[kParamModAmp] = 127.0f;
  254. fParameters[kParamResCenter] = 64.0f;
  255. fParameters[kParamResBandwidth] = 64.0f;
  256. fSynth.buffersize = static_cast<int>(getBufferSize());
  257. fSynth.samplerate = static_cast<uint>(getSampleRate());
  258. if (fSynth.buffersize > 32)
  259. fSynth.buffersize = 32;
  260. fSynth.alias();
  261. _initMaster();
  262. _setMasterParameters();
  263. fMaster->getalldata(&fDefaultState);
  264. fMiddleWareThread->start(fMiddleWare);
  265. }
  266. ~ZynAddSubFxPlugin() override
  267. {
  268. fMiddleWareThread->stop();
  269. _deleteMaster();
  270. std::free(fDefaultState);
  271. }
  272. protected:
  273. // -------------------------------------------------------------------
  274. // Plugin parameter calls
  275. uint32_t getParameterCount() const final
  276. {
  277. return kParamCount;
  278. }
  279. const NativeParameter* getParameterInfo(const uint32_t index) const override
  280. {
  281. CARLA_SAFE_ASSERT_RETURN(index < kParamCount, nullptr);
  282. static NativeParameter param;
  283. int hints = NATIVE_PARAMETER_IS_ENABLED|NATIVE_PARAMETER_IS_AUTOMABLE;
  284. param.name = nullptr;
  285. param.unit = nullptr;
  286. param.ranges.def = 64.0f;
  287. param.ranges.min = 0.0f;
  288. param.ranges.max = 127.0f;
  289. param.ranges.step = 1.0f;
  290. param.ranges.stepSmall = 1.0f;
  291. param.ranges.stepLarge = 20.0f;
  292. param.scalePointCount = 0;
  293. param.scalePoints = nullptr;
  294. if (index <= kParamPart16Enabled)
  295. {
  296. hints |= NATIVE_PARAMETER_IS_BOOLEAN;
  297. param.ranges.def = 0.0f;
  298. param.ranges.min = 0.0f;
  299. param.ranges.max = 1.0f;
  300. param.ranges.step = 1.0f;
  301. param.ranges.stepSmall = 1.0f;
  302. param.ranges.stepLarge = 1.0f;
  303. #define PARAM_PART_ENABLE_DESC(N) \
  304. case kParamPart01Enabled + N - 1: \
  305. param.name = "Part " #N " Enabled"; break;
  306. switch (index)
  307. {
  308. PARAM_PART_ENABLE_DESC( 1)
  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. // -----------------------------------------------------------------------