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.

816 lines
20KB

  1. /*
  2. * Carla Native Plugins
  3. * Copyright (C) 2012-2013 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. // for UINT32_MAX
  18. #define __STDC_LIMIT_MACROS
  19. #include <cstdint>
  20. #include "CarlaNative.hpp"
  21. #include "CarlaMIDI.h"
  22. #include "CarlaString.hpp"
  23. #include "RtList.hpp"
  24. #include "zynaddsubfx/DSP/FFTwrapper.h"
  25. #include "zynaddsubfx/Misc/Master.h"
  26. #include "zynaddsubfx/Misc/Part.h"
  27. #include "zynaddsubfx/Misc/Util.h"
  28. #ifdef WANT_ZYNADDSUBFX_UI
  29. # ifdef override
  30. # define override_hack
  31. # undef override
  32. # endif
  33. # include "zynaddsubfx/UI/common.H"
  34. # include "zynaddsubfx/UI/MasterUI.h"
  35. # include <FL/Fl_Shared_Image.H>
  36. # include <FL/Fl_Tiled_Image.H>
  37. # include <FL/Fl_Theme.H>
  38. # ifdef override_hack
  39. # define override
  40. # undef override_hack
  41. # endif
  42. #endif
  43. #include <ctime>
  44. #include <set>
  45. #include <string>
  46. #include "juce_core.h"
  47. #ifdef WANT_ZYNADDSUBFX_UI
  48. static Fl_Tiled_Image* gModuleBackdrop = nullptr;
  49. static CarlaString gPixmapPath;
  50. extern CarlaString gUiPixmapPath;
  51. void set_module_parameters(Fl_Widget* o)
  52. {
  53. CARLA_ASSERT(gModuleBackdrop != nullptr);
  54. o->box(FL_DOWN_FRAME);
  55. o->align(o->align() | FL_ALIGN_IMAGE_BACKDROP);
  56. o->color(FL_BLACK);
  57. o->labeltype(FL_SHADOW_LABEL);
  58. if (gModuleBackdrop != nullptr)
  59. o->image(gModuleBackdrop);
  60. }
  61. #endif
  62. // -----------------------------------------------------------------------
  63. class ZynAddSubFxPrograms
  64. {
  65. public:
  66. ZynAddSubFxPrograms()
  67. : fInitiated(false)
  68. {
  69. }
  70. ~ZynAddSubFxPrograms()
  71. {
  72. if (! fInitiated)
  73. return;
  74. for (auto it = fPrograms.begin(); it.valid(); it.next())
  75. {
  76. const ProgramInfo*& pInfo(*it);
  77. delete pInfo;
  78. }
  79. fPrograms.clear();
  80. FFT_cleanup();
  81. }
  82. void init()
  83. {
  84. if (fInitiated)
  85. return;
  86. fInitiated = true;
  87. fPrograms.append(new ProgramInfo(0, 0, "default"));
  88. Master& master(Master::getInstance());
  89. pthread_mutex_lock(&master.mutex);
  90. // refresh banks
  91. master.bank.rescanforbanks();
  92. for (uint32_t i=0, size = master.bank.banks.size(); i < size; ++i)
  93. {
  94. if (master.bank.banks[i].dir.empty())
  95. continue;
  96. master.bank.loadbank(master.bank.banks[i].dir);
  97. for (unsigned int instrument = 0; instrument < BANK_SIZE; ++instrument)
  98. {
  99. const std::string insName(master.bank.getname(instrument));
  100. if (insName.empty() || insName[0] == '\0' || insName[0] == ' ')
  101. continue;
  102. fPrograms.append(new ProgramInfo(i+1, instrument, insName.c_str()));
  103. }
  104. }
  105. pthread_mutex_unlock(&master.mutex);
  106. }
  107. void load(Master* const master, const uint8_t channel, const uint32_t bank, const uint32_t program)
  108. {
  109. if (bank == 0)
  110. {
  111. pthread_mutex_lock(&master->mutex);
  112. master->partonoff(channel, 1);
  113. master->part[channel]->defaults();
  114. master->part[channel]->applyparameters(false);
  115. pthread_mutex_unlock(&master->mutex);
  116. return;
  117. }
  118. const std::string& bankdir(master->bank.banks[bank-1].dir);
  119. if (! bankdir.empty())
  120. {
  121. pthread_mutex_lock(&master->mutex);
  122. master->partonoff(channel, 1);
  123. master->bank.loadbank(bankdir);
  124. master->bank.loadfromslot(program, master->part[channel]);
  125. master->part[channel]->applyparameters(false);
  126. pthread_mutex_unlock(&master->mutex);
  127. }
  128. }
  129. uint32_t count()
  130. {
  131. return fPrograms.count();
  132. }
  133. const MidiProgram* getInfo(const uint32_t index)
  134. {
  135. if (index >= fPrograms.count())
  136. return nullptr;
  137. const ProgramInfo*& pInfo(fPrograms.getAt(index));
  138. fRetProgram.bank = pInfo->bank;
  139. fRetProgram.program = pInfo->prog;
  140. fRetProgram.name = pInfo->name;
  141. return &fRetProgram;
  142. }
  143. private:
  144. struct ProgramInfo {
  145. uint32_t bank;
  146. uint32_t prog;
  147. const char* name;
  148. ProgramInfo(uint32_t bank_, uint32_t prog_, const char* name_)
  149. : bank(bank_),
  150. prog(prog_),
  151. name(carla_strdup(name_)) {}
  152. ~ProgramInfo()
  153. {
  154. if (name != nullptr)
  155. {
  156. delete[] name;
  157. name = nullptr;
  158. }
  159. }
  160. #ifdef CARLA_PROPER_CPP11_SUPPORT
  161. ProgramInfo() = delete;
  162. ProgramInfo(ProgramInfo&) = delete;
  163. ProgramInfo(const ProgramInfo&) = delete;
  164. ProgramInfo& operator=(ProgramInfo&);
  165. ProgramInfo& operator=(const ProgramInfo&);
  166. #endif
  167. };
  168. bool fInitiated;
  169. MidiProgram fRetProgram;
  170. NonRtList<const ProgramInfo*> fPrograms;
  171. CARLA_DECLARE_NON_COPY_CLASS(ZynAddSubFxPrograms)
  172. };
  173. static ZynAddSubFxPrograms sPrograms;
  174. // -----------------------------------------------------------------------
  175. class ZynAddSubFxInstanceCount
  176. {
  177. public:
  178. ZynAddSubFxInstanceCount()
  179. : fCount(0)
  180. {
  181. }
  182. ~ZynAddSubFxInstanceCount()
  183. {
  184. CARLA_ASSERT(fCount == 0);
  185. }
  186. void addOne(const HostDescriptor* const host)
  187. {
  188. if (fCount++ == 0)
  189. {
  190. CARLA_ASSERT(synth == nullptr);
  191. CARLA_ASSERT(denormalkillbuf == nullptr);
  192. reinit(host);
  193. #ifdef WANT_ZYNADDSUBFX_UI
  194. if (gPixmapPath.isEmpty())
  195. {
  196. gPixmapPath = host->resourceDir;
  197. gPixmapPath += "/zynaddsubfx/";
  198. gUiPixmapPath = gPixmapPath;
  199. }
  200. #endif
  201. }
  202. }
  203. void removeOne()
  204. {
  205. if (--fCount == 0)
  206. {
  207. CARLA_ASSERT(synth != nullptr);
  208. CARLA_ASSERT(denormalkillbuf != nullptr);
  209. Master::deleteInstance();
  210. delete[] denormalkillbuf;
  211. denormalkillbuf = nullptr;
  212. delete synth;
  213. synth = nullptr;
  214. }
  215. }
  216. void reinit(const HostDescriptor* const host)
  217. {
  218. Master::deleteInstance();
  219. if (denormalkillbuf != nullptr)
  220. {
  221. delete[] denormalkillbuf;
  222. denormalkillbuf = nullptr;
  223. }
  224. if (synth != nullptr)
  225. {
  226. delete synth;
  227. synth = nullptr;
  228. }
  229. synth = new SYNTH_T();
  230. synth->buffersize = host->get_buffer_size(host->handle);
  231. synth->samplerate = host->get_sample_rate(host->handle);
  232. synth->alias();
  233. config.init();
  234. config.cfg.SoundBufferSize = synth->buffersize;
  235. config.cfg.SampleRate = synth->samplerate;
  236. config.cfg.GzipCompression = 0;
  237. sprng(std::time(nullptr));
  238. denormalkillbuf = new float[synth->buffersize];
  239. for (int i=0; i < synth->buffersize; ++i)
  240. denormalkillbuf[i] = (RND - 0.5f) * 1e-16;
  241. Master::getInstance();
  242. }
  243. void maybeReinit(const HostDescriptor* const host)
  244. {
  245. if (host->get_buffer_size(host->handle) == static_cast<uint32_t>(synth->buffersize) &&
  246. host->get_sample_rate(host->handle) == static_cast<double>(synth->samplerate))
  247. return;
  248. reinit(host);
  249. }
  250. private:
  251. int fCount;
  252. CARLA_DECLARE_NON_COPY_CLASS(ZynAddSubFxInstanceCount)
  253. };
  254. static ZynAddSubFxInstanceCount sInstanceCount;
  255. // -----------------------------------------------------------------------
  256. class ZynAddSubFxThread : public juce::Thread
  257. {
  258. public:
  259. ZynAddSubFxThread(Master* const master, const HostDescriptor* const host)
  260. : juce::Thread("ZynAddSubFxThread"),
  261. fMaster(master),
  262. kHost(host),
  263. #ifdef WANT_ZYNADDSUBFX_UI
  264. fUi(nullptr),
  265. fUiClosed(0),
  266. fNextUiAction(-1),
  267. #endif
  268. fChangeProgram(false),
  269. fNextChannel(0),
  270. fNextBank(0),
  271. fNextProgram(0)
  272. {
  273. }
  274. ~ZynAddSubFxThread()
  275. {
  276. #ifdef WANT_ZYNADDSUBFX_UI
  277. // must be closed by now
  278. CARLA_ASSERT(fUi == nullptr);
  279. #endif
  280. }
  281. void loadProgramLater(const uint8_t channel, const uint32_t bank, const uint32_t program)
  282. {
  283. fNextChannel = channel;
  284. fNextBank = bank;
  285. fNextProgram = program;
  286. fChangeProgram = true;
  287. }
  288. void stopLoadProgramLater()
  289. {
  290. fChangeProgram = false;
  291. fNextChannel = 0;
  292. fNextBank = 0;
  293. fNextProgram = 0;
  294. }
  295. void setMaster(Master* const master)
  296. {
  297. fMaster = master;
  298. }
  299. #ifdef WANT_ZYNADDSUBFX_UI
  300. void uiHide()
  301. {
  302. fNextUiAction = 0;
  303. }
  304. void uiShow()
  305. {
  306. fNextUiAction = 1;
  307. }
  308. void uiRepaint()
  309. {
  310. if (fUi != nullptr)
  311. fNextUiAction = 2;
  312. }
  313. void uiChangeName(const char* const name)
  314. {
  315. if (fUi != nullptr)
  316. {
  317. Fl::lock();
  318. fUi->masterwindow->label(name);
  319. Fl::unlock();
  320. }
  321. }
  322. #endif
  323. protected:
  324. void run() override
  325. {
  326. while (! threadShouldExit())
  327. {
  328. #ifdef WANT_ZYNADDSUBFX_UI
  329. Fl::lock();
  330. if (fNextUiAction == 2) // repaint
  331. {
  332. CARLA_ASSERT(fUi != nullptr);
  333. if (fUi != nullptr)
  334. fUi->refresh_master_ui();
  335. }
  336. else if (fNextUiAction == 1) // init/show
  337. {
  338. static bool initialized = false;
  339. if (! initialized)
  340. {
  341. initialized = true;
  342. fl_register_images();
  343. Fl_Dial::default_style(Fl_Dial::PIXMAP_DIAL);
  344. if (Fl_Shared_Image* const img = Fl_Shared_Image::get(gPixmapPath + "knob.png"))
  345. Fl_Dial::default_image(img);
  346. if (Fl_Shared_Image* const img = Fl_Shared_Image::get(gPixmapPath + "window_backdrop.png"))
  347. Fl::scheme_bg(new Fl_Tiled_Image(img));
  348. if(Fl_Shared_Image* const img = Fl_Shared_Image::get(gPixmapPath + "module_backdrop.png"))
  349. gModuleBackdrop = new Fl_Tiled_Image(img);
  350. Fl::background(50, 50, 50);
  351. Fl::background2(70, 70, 70);
  352. Fl::foreground(255, 255, 255);
  353. Fl_Theme::set("Cairo");
  354. }
  355. CARLA_ASSERT(fUi == nullptr);
  356. if (fUi == nullptr)
  357. {
  358. fUiClosed = 0;
  359. fUi = new MasterUI(fMaster, &fUiClosed);
  360. fUi->masterwindow->label(kHost->uiName);
  361. fUi->showUI();
  362. }
  363. }
  364. else if (fNextUiAction == 0) // close
  365. {
  366. CARLA_ASSERT(fUi != nullptr);
  367. if (fUi != nullptr)
  368. {
  369. delete fUi;
  370. fUi = nullptr;
  371. }
  372. }
  373. fNextUiAction = -1;
  374. if (fUiClosed != 0)
  375. {
  376. fUiClosed = 0;
  377. fNextUiAction = 0;
  378. kHost->ui_closed(kHost->handle);
  379. }
  380. Fl::check();
  381. Fl::unlock();
  382. #endif
  383. if (fChangeProgram)
  384. {
  385. fChangeProgram = false;
  386. sPrograms.load(fMaster, fNextChannel, fNextBank, fNextProgram);
  387. fNextChannel = 0;
  388. fNextBank = 0;
  389. fNextProgram = 0;
  390. #ifdef WANT_ZYNADDSUBFX_UI
  391. if (fUi != nullptr)
  392. {
  393. Fl::lock();
  394. fUi->refresh_master_ui();
  395. Fl::unlock();
  396. }
  397. #endif
  398. carla_msleep(15);
  399. }
  400. else
  401. {
  402. carla_msleep(30);
  403. }
  404. }
  405. #ifdef WANT_ZYNADDSUBFX_UI
  406. if (threadShouldExit() || fUi != nullptr)
  407. {
  408. Fl::lock();
  409. delete fUi;
  410. fUi = nullptr;
  411. Fl::check();
  412. Fl::unlock();
  413. }
  414. #endif
  415. }
  416. private:
  417. Master* fMaster;
  418. const HostDescriptor* const kHost;
  419. #ifdef WANT_ZYNADDSUBFX_UI
  420. MasterUI* fUi;
  421. int fUiClosed;
  422. volatile int fNextUiAction;
  423. #endif
  424. volatile bool fChangeProgram;
  425. volatile uint8_t fNextChannel;
  426. volatile uint32_t fNextBank;
  427. volatile uint32_t fNextProgram;
  428. };
  429. // -----------------------------------------------------------------------
  430. class ZynAddSubFxPlugin : public PluginClass
  431. {
  432. public:
  433. ZynAddSubFxPlugin(const HostDescriptor* const host)
  434. : PluginClass(host),
  435. fMaster(new Master()),
  436. fSampleRate(getSampleRate()),
  437. fIsActive(false),
  438. fThread(fMaster, host)
  439. {
  440. fThread.startThread(3);
  441. for (int i = 0; i < NUM_MIDI_PARTS; ++i)
  442. fMaster->partonoff(i, 1);
  443. sPrograms.init();
  444. }
  445. ~ZynAddSubFxPlugin() override
  446. {
  447. deleteMaster();
  448. }
  449. protected:
  450. // -------------------------------------------------------------------
  451. // Plugin midi-program calls
  452. uint32_t getMidiProgramCount() const override
  453. {
  454. return sPrograms.count();
  455. }
  456. const MidiProgram* getMidiProgramInfo(const uint32_t index) const override
  457. {
  458. return sPrograms.getInfo(index);
  459. }
  460. // -------------------------------------------------------------------
  461. // Plugin state calls
  462. void setMidiProgram(const uint8_t channel, const uint32_t bank, const uint32_t program) override
  463. {
  464. if (bank >= fMaster->bank.banks.size())
  465. return;
  466. if (program >= BANK_SIZE)
  467. return;
  468. if (isOffline() || ! fIsActive)
  469. {
  470. sPrograms.load(fMaster, channel, bank, program);
  471. #ifdef WANT_ZYNADDSUBFX_UI
  472. fThread.uiRepaint();
  473. #endif
  474. }
  475. else
  476. fThread.loadProgramLater(channel, bank, program);
  477. }
  478. void setCustomData(const char* const key, const char* const value) override
  479. {
  480. CARLA_SAFE_ASSERT_RETURN(key != nullptr,);
  481. CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
  482. if (std::strcmp(key, "CarlaAlternateFile1") == 0) // xmz
  483. fMaster->loadXML(value);
  484. else if (std::strcmp(key, "CarlaAlternateFile2") == 0) // xiz
  485. fMaster->part[0]->loadXMLinstrument(value);
  486. }
  487. // -------------------------------------------------------------------
  488. // Plugin process calls
  489. void activate() override
  490. {
  491. fIsActive = true;
  492. }
  493. void deactivate() override
  494. {
  495. fIsActive = false;
  496. }
  497. void process(float**, float** const outBuffer, const uint32_t frames, const MidiEvent* const midiEvents, const uint32_t midiEventCount) override
  498. {
  499. if (pthread_mutex_trylock(&fMaster->mutex) != 0)
  500. {
  501. carla_zeroFloat(outBuffer[0], frames);
  502. carla_zeroFloat(outBuffer[1], frames);
  503. return;
  504. }
  505. for (uint32_t i=0; i < midiEventCount; ++i)
  506. {
  507. const MidiEvent* const midiEvent(&midiEvents[i]);
  508. const uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent->data);
  509. const uint8_t channel = MIDI_GET_CHANNEL_FROM_DATA(midiEvent->data);
  510. if (MIDI_IS_STATUS_NOTE_OFF(status))
  511. {
  512. const uint8_t note = midiEvent->data[1];
  513. fMaster->noteOff(channel, note);
  514. }
  515. else if (MIDI_IS_STATUS_NOTE_ON(status))
  516. {
  517. const uint8_t note = midiEvent->data[1];
  518. const uint8_t velo = midiEvent->data[2];
  519. fMaster->noteOn(channel, note, velo);
  520. }
  521. else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status))
  522. {
  523. const uint8_t note = midiEvent->data[1];
  524. const uint8_t pressure = midiEvent->data[2];
  525. fMaster->polyphonicAftertouch(channel, note, pressure);
  526. }
  527. else if (MIDI_IS_STATUS_CONTROL_CHANGE(status))
  528. {
  529. const uint8_t control = midiEvent->data[1];
  530. const uint8_t value = midiEvent->data[2];
  531. fMaster->setController(channel, control, value);
  532. }
  533. else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status))
  534. {
  535. const uint8_t lsb = midiEvent->data[1];
  536. const uint8_t msb = midiEvent->data[2];
  537. const int value = ((msb << 7) | lsb) - 8192;
  538. fMaster->setController(channel, C_pitchwheel, value);
  539. }
  540. }
  541. fMaster->GetAudioOutSamples(frames, fSampleRate, outBuffer[0], outBuffer[1]);
  542. pthread_mutex_unlock(&fMaster->mutex);
  543. }
  544. #ifdef WANT_ZYNADDSUBFX_UI
  545. // -------------------------------------------------------------------
  546. // Plugin UI calls
  547. void uiShow(const bool show) override
  548. {
  549. if (show)
  550. fThread.uiShow();
  551. else
  552. fThread.uiHide();
  553. }
  554. #endif
  555. // -------------------------------------------------------------------
  556. // Plugin state calls
  557. char* getState() const override
  558. {
  559. config.save();
  560. char* data = nullptr;
  561. fMaster->getalldata(&data);
  562. return data;
  563. }
  564. void setState(const char* const data) override
  565. {
  566. fThread.stopLoadProgramLater();
  567. fMaster->putalldata((char*)data, 0);
  568. fMaster->applyparameters(true);
  569. }
  570. // -------------------------------------------------------------------
  571. // Plugin dispatcher
  572. // TODO - save&load current state
  573. void bufferSizeChanged(const uint32_t) final
  574. {
  575. deleteMaster();
  576. sInstanceCount.maybeReinit(getHostHandle());
  577. initMaster();
  578. }
  579. void sampleRateChanged(const double sampleRate) final
  580. {
  581. fSampleRate = sampleRate;
  582. deleteMaster();
  583. sInstanceCount.maybeReinit(getHostHandle());
  584. initMaster();
  585. }
  586. void initMaster()
  587. {
  588. fMaster = new Master();
  589. fThread.setMaster(fMaster);
  590. fThread.startThread(3);
  591. for (int i = 0; i < NUM_MIDI_PARTS; ++i)
  592. fMaster->partonoff(i, 1);
  593. }
  594. void deleteMaster()
  595. {
  596. //ensure that everything has stopped
  597. pthread_mutex_lock(&fMaster->mutex);
  598. pthread_mutex_unlock(&fMaster->mutex);
  599. fThread.stopThread(-1);
  600. delete fMaster;
  601. fMaster = nullptr;
  602. }
  603. #ifdef WANT_ZYNADDSUBFX_UI
  604. void uiNameChanged(const char* const uiName) override
  605. {
  606. fThread.uiChangeName(uiName);
  607. }
  608. #endif
  609. // -------------------------------------------------------------------
  610. private:
  611. Master* fMaster;
  612. unsigned fSampleRate;
  613. bool fIsActive;
  614. ZynAddSubFxThread fThread;
  615. public:
  616. static PluginHandle _instantiate(const HostDescriptor* host)
  617. {
  618. sInstanceCount.addOne(host);
  619. return new ZynAddSubFxPlugin(host);
  620. }
  621. static void _cleanup(PluginHandle handle)
  622. {
  623. delete (ZynAddSubFxPlugin*)handle;
  624. sInstanceCount.removeOne();
  625. }
  626. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ZynAddSubFxPlugin)
  627. };
  628. // -----------------------------------------------------------------------
  629. static const PluginDescriptor zynaddsubfxDesc = {
  630. /* category */ PLUGIN_CATEGORY_SYNTH,
  631. #ifdef WANT_ZYNADDSUBFX_UI
  632. /* hints */ static_cast<PluginHints>(PLUGIN_HAS_GUI|PLUGIN_USES_STATE),
  633. #else
  634. /* hints */ static_cast<PluginHints>(PLUGIN_USES_STATE),
  635. #endif
  636. /* supports */ static_cast<PluginSupports>(PLUGIN_SUPPORTS_CONTROL_CHANGES|PLUGIN_SUPPORTS_NOTE_AFTERTOUCH|PLUGIN_SUPPORTS_PITCHBEND|PLUGIN_SUPPORTS_ALL_SOUND_OFF),
  637. /* audioIns */ 0,
  638. /* audioOuts */ 2,
  639. /* midiIns */ 1,
  640. /* midiOuts */ 0,
  641. /* paramIns */ 0,
  642. /* paramOuts */ 0,
  643. /* name */ "ZynAddSubFX",
  644. /* label */ "zynaddsubfx",
  645. /* maker */ "falkTX",
  646. /* copyright */ "GNU GPL v2+",
  647. PluginDescriptorFILL(ZynAddSubFxPlugin)
  648. };
  649. // -----------------------------------------------------------------------
  650. CARLA_EXPORT
  651. void carla_register_native_plugin_zynaddsubfx_synth()
  652. {
  653. carla_register_native_plugin(&zynaddsubfxDesc);
  654. }
  655. // -----------------------------------------------------------------------