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.

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