|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071 |
- /*
- * Carla Native Plugins
- * Copyright (C) 2012-2017 Filipe Coelho <falktx@falktx.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * For a full copy of the GNU General Public License see the doc/GPL.txt file.
- */
-
- #include "CarlaNativeExtUI.hpp"
- #include "CarlaMIDI.h"
- #include "CarlaThread.hpp"
- #include "LinkedList.hpp"
-
- #include "CarlaMathUtils.hpp"
-
- #include "Misc/Master.h"
- #include "Misc/MiddleWare.h"
- #include "Misc/Part.h"
- #include "Misc/Util.h"
-
- #include <ctime>
- #include <set>
- #include <string>
-
- #include "AppConfig.h"
- #include "juce_audio_basics/juce_audio_basics.h"
-
- using juce::roundToIntAccurate;
- using juce::FloatVectorOperations;
- using juce::ScopedPointer;
-
- // #define ZYN_MSG_ANYWHERE
-
- // -----------------------------------------------------------------------
-
- class ZynAddSubFxPrograms
- {
- public:
- ZynAddSubFxPrograms() noexcept
- : fInitiated(false),
- #ifdef CARLA_PROPER_CPP11_SUPPORT
- fRetProgram({0, 0, nullptr}),
- #endif
- fProgramCount(0),
- fPrograms(nullptr) {}
-
- ~ZynAddSubFxPrograms() noexcept
- {
- if (! fInitiated)
- return;
-
- for (uint32_t i=0; i<fProgramCount; ++i)
- delete fPrograms[i];
-
- delete[] fPrograms;
- }
-
- void initIfNeeded()
- {
- if (fInitiated)
- return;
- fInitiated = true;
-
- std::vector<const ProgramInfo*> programs;
- programs.push_back(new ProgramInfo(0, 0, "default", ""));
-
- CarlaConfig config;
- config.init();
-
- SYNTH_T synth;
- Master master(synth, &config);
-
- // refresh banks
- master.bank.rescanforbanks();
-
- for (std::size_t i=0, size=master.bank.banks.size(); i<size; ++i)
- {
- const std::string dir(master.bank.banks[i].dir);
-
- if (dir.empty())
- continue;
-
- master.bank.loadbank(dir);
-
- for (uint ninstrument = 0; ninstrument < BANK_SIZE; ++ninstrument)
- {
- const Bank::ins_t& instrument(master.bank.ins[ninstrument]);
-
- if (instrument.name.empty() || instrument.name[0] == ' ')
- continue;
-
- programs.push_back(new ProgramInfo(i+1, ninstrument, instrument.name.c_str(), instrument.filename.c_str()));
- }
- }
-
- fPrograms = new const ProgramInfo*[programs.size()];
-
- for (const ProgramInfo* p : programs)
- fPrograms[fProgramCount++] = p;
- }
-
- uint32_t getNativeMidiProgramCount() const noexcept
- {
- return fProgramCount;
- }
-
- const NativeMidiProgram* getNativeMidiProgramInfo(const uint32_t index) const noexcept
- {
- if (index >= fProgramCount)
- return nullptr;
-
- const ProgramInfo* const pInfo(fPrograms[index]);
- CARLA_SAFE_ASSERT_RETURN(pInfo != nullptr, nullptr);
-
- fRetProgram.bank = pInfo->bank;
- fRetProgram.program = pInfo->prog;
- fRetProgram.name = pInfo->name;
-
- return &fRetProgram;
- }
-
- const char* getZynProgramFilename(const uint32_t bank, const uint32_t program) const noexcept
- {
- for (uint32_t i=0; i<fProgramCount; ++i)
- {
- const ProgramInfo* const pInfo(fPrograms[i]);
-
- if (pInfo->bank != bank)
- continue;
- if (pInfo->prog != program)
- continue;
-
- return pInfo->filename;
- }
-
- return nullptr;
- }
-
- private:
- struct ProgramInfo {
- uint32_t bank;
- uint32_t prog;
- const char* name;
- const char* filename;
-
- ProgramInfo(uint32_t b, uint32_t p, const char* n, const char* fn) noexcept
- : bank(b),
- prog(p),
- name(carla_strdup_safe(n)),
- filename(carla_strdup_safe(fn)) {}
-
- ~ProgramInfo() noexcept
- {
- if (name != nullptr)
- {
- delete[] name;
- name = nullptr;
- }
-
- if (filename != nullptr)
- {
- delete[] filename;
- filename = nullptr;
- }
- }
-
- #ifdef CARLA_PROPER_CPP11_SUPPORT
- ProgramInfo() = delete;
- ProgramInfo(ProgramInfo&) = delete;
- ProgramInfo(const ProgramInfo&) = delete;
- ProgramInfo& operator=(ProgramInfo&);
- ProgramInfo& operator=(const ProgramInfo&);
- #endif
- };
-
- bool fInitiated;
- mutable NativeMidiProgram fRetProgram;
- uint32_t fProgramCount;
- const ProgramInfo** fPrograms;
-
- CARLA_PREVENT_HEAP_ALLOCATION
- CARLA_DECLARE_NON_COPY_CLASS(ZynAddSubFxPrograms)
- };
-
- static ZynAddSubFxPrograms sPrograms;
-
- // -----------------------------------------------------------------------
-
- class MiddleWareThread : private CarlaThread
- {
- public:
- class ScopedStopper
- {
- public:
- ScopedStopper(MiddleWareThread& mwt) noexcept
- : wasRunning(mwt.isThreadRunning()),
- thread(mwt),
- middleWare(mwt.fMiddleWare)
- {
- if (wasRunning)
- thread.stop();
- }
-
- ~ScopedStopper() noexcept
- {
- if (wasRunning)
- thread.start(middleWare);
- }
-
- void updateMiddleWare(MiddleWare* const mw) noexcept
- {
- middleWare = mw;
- }
-
- private:
- const bool wasRunning;
- MiddleWareThread& thread;
- MiddleWare* middleWare;
-
- CARLA_PREVENT_HEAP_ALLOCATION
- CARLA_DECLARE_NON_COPY_CLASS(ScopedStopper)
- };
-
- MiddleWareThread()
- : CarlaThread("ZynMiddleWare"),
- fMiddleWare(nullptr) {}
-
- void start(MiddleWare* const mw) noexcept
- {
- fMiddleWare = mw;
- startThread();
- }
-
- void stop() noexcept
- {
- stopThread(1000);
- fMiddleWare = nullptr;
- }
-
- private:
- MiddleWare* fMiddleWare;
-
- void run() noexcept override
- {
- for (; ! shouldThreadExit();)
- {
- CARLA_SAFE_ASSERT_RETURN(fMiddleWare != nullptr,);
-
- try {
- fMiddleWare->tick();
- } CARLA_SAFE_EXCEPTION("ZynAddSubFX MiddleWare tick");
-
- carla_msleep(1);
- }
- }
-
- CARLA_DECLARE_NON_COPY_CLASS(MiddleWareThread)
- };
-
- // -----------------------------------------------------------------------
-
- class ZynAddSubFxPlugin : public NativePluginAndUiClass
- {
- public:
- enum Parameters {
- kParamPart01Enabled ,
- kParamPart16Enabled = kParamPart01Enabled + 15,
- kParamPart01Volume,
- kParamPart16Volume = kParamPart01Volume + 15,
- kParamPart01Panning,
- kParamPart16Panning = kParamPart01Panning + 15,
- kParamFilterCutoff, // Filter Frequency
- kParamFilterQ, // Filter Resonance
- kParamBandwidth, // Bandwidth
- kParamModAmp, // FM Gain
- kParamResCenter, // Resonance center frequency
- kParamResBandwidth, // Resonance bandwidth
- kParamCount
- };
-
- ZynAddSubFxPlugin(const NativeHostDescriptor* const host)
- : NativePluginAndUiClass(host, "zynaddsubfx-ui"),
- fMiddleWare(nullptr),
- fMaster(nullptr),
- fSynth(),
- fDefaultState(nullptr),
- fMutex(),
- fMiddleWareThread(new MiddleWareThread())
- {
- isPlugin = true;
-
- sPrograms.initIfNeeded();
- fConfig.init();
-
- // init parameters to default
- fParameters[kParamPart01Enabled] = 1.0f;
-
- for (int i=kParamPart16Enabled+1; --i>kParamPart01Enabled;)
- fParameters[i] = 0.0f;
-
- for (int i=kParamPart16Volume+1; --i>=kParamPart01Volume;)
- fParameters[i] = 100.0f;
-
- for (int i=kParamPart16Panning+1; --i>=kParamPart01Panning;)
- fParameters[i] = 64.0f;
-
- fParameters[kParamFilterCutoff] = 64.0f;
- fParameters[kParamFilterQ] = 64.0f;
- fParameters[kParamBandwidth] = 64.0f;
- fParameters[kParamModAmp] = 127.0f;
- fParameters[kParamResCenter] = 64.0f;
- fParameters[kParamResBandwidth] = 64.0f;
-
- fSynth.buffersize = static_cast<int>(getBufferSize());
- fSynth.samplerate = static_cast<uint>(getSampleRate());
-
- if (fSynth.buffersize > 32)
- fSynth.buffersize = 32;
-
- fSynth.alias();
-
- _initMaster();
- _setMasterParameters();
-
- fMaster->getalldata(&fDefaultState);
-
- fMiddleWareThread->start(fMiddleWare);
- }
-
- ~ZynAddSubFxPlugin() override
- {
- fMiddleWareThread->stop();
- _deleteMaster();
- std::free(fDefaultState);
- }
-
- protected:
- // -------------------------------------------------------------------
- // Plugin parameter calls
-
- uint32_t getParameterCount() const final
- {
- return kParamCount;
- }
-
- const NativeParameter* getParameterInfo(const uint32_t index) const override
- {
- CARLA_SAFE_ASSERT_RETURN(index < kParamCount, nullptr);
-
- static NativeParameter param;
-
- int hints = NATIVE_PARAMETER_IS_ENABLED|NATIVE_PARAMETER_IS_AUTOMABLE;
-
- param.name = nullptr;
- param.unit = nullptr;
- param.ranges.def = 64.0f;
- param.ranges.min = 0.0f;
- param.ranges.max = 127.0f;
- param.ranges.step = 1.0f;
- param.ranges.stepSmall = 1.0f;
- param.ranges.stepLarge = 20.0f;
- param.scalePointCount = 0;
- param.scalePoints = nullptr;
-
- if (index <= kParamPart16Enabled)
- {
- hints |= NATIVE_PARAMETER_IS_BOOLEAN;
- param.ranges.def = 0.0f;
- param.ranges.min = 0.0f;
- param.ranges.max = 1.0f;
- param.ranges.step = 1.0f;
- param.ranges.stepSmall = 1.0f;
- param.ranges.stepLarge = 1.0f;
-
- #define PARAM_PART_ENABLE_DESC(N) \
- case kParamPart01Enabled + N - 1: \
- param.name = "Part " #N " Enabled"; break;
-
- switch (index)
- {
- PARAM_PART_ENABLE_DESC( 1)
- PARAM_PART_ENABLE_DESC( 2)
- PARAM_PART_ENABLE_DESC( 3)
- PARAM_PART_ENABLE_DESC( 4)
- PARAM_PART_ENABLE_DESC( 5)
- PARAM_PART_ENABLE_DESC( 6)
- PARAM_PART_ENABLE_DESC( 7)
- PARAM_PART_ENABLE_DESC( 8)
- PARAM_PART_ENABLE_DESC( 9)
- PARAM_PART_ENABLE_DESC(10)
- PARAM_PART_ENABLE_DESC(11)
- PARAM_PART_ENABLE_DESC(12)
- PARAM_PART_ENABLE_DESC(13)
- PARAM_PART_ENABLE_DESC(14)
- PARAM_PART_ENABLE_DESC(15)
- PARAM_PART_ENABLE_DESC(16)
- }
-
- #undef PARAM_PART_ENABLE_DESC
- }
- else if (index <= kParamPart16Volume)
- {
- hints |= NATIVE_PARAMETER_IS_INTEGER;
- param.ranges.def = 100.0f;
-
- #define PARAM_PART_ENABLE_DESC(N) \
- case kParamPart01Volume + N - 1: \
- param.name = "Part " #N " Volume"; break;
-
- switch (index)
- {
- PARAM_PART_ENABLE_DESC( 1)
- PARAM_PART_ENABLE_DESC( 2)
- PARAM_PART_ENABLE_DESC( 3)
- PARAM_PART_ENABLE_DESC( 4)
- PARAM_PART_ENABLE_DESC( 5)
- PARAM_PART_ENABLE_DESC( 6)
- PARAM_PART_ENABLE_DESC( 7)
- PARAM_PART_ENABLE_DESC( 8)
- PARAM_PART_ENABLE_DESC( 9)
- PARAM_PART_ENABLE_DESC(10)
- PARAM_PART_ENABLE_DESC(11)
- PARAM_PART_ENABLE_DESC(12)
- PARAM_PART_ENABLE_DESC(13)
- PARAM_PART_ENABLE_DESC(14)
- PARAM_PART_ENABLE_DESC(15)
- PARAM_PART_ENABLE_DESC(16)
- }
-
- #undef PARAM_PART_ENABLE_DESC
- }
- else if (index <= kParamPart16Panning)
- {
- hints |= NATIVE_PARAMETER_IS_INTEGER;
-
- #define PARAM_PART_ENABLE_DESC(N) \
- case kParamPart01Panning + N - 1: \
- param.name = "Part " #N " Panning"; break;
-
- switch (index)
- {
- PARAM_PART_ENABLE_DESC( 1)
- PARAM_PART_ENABLE_DESC( 2)
- PARAM_PART_ENABLE_DESC( 3)
- PARAM_PART_ENABLE_DESC( 4)
- PARAM_PART_ENABLE_DESC( 5)
- PARAM_PART_ENABLE_DESC( 6)
- PARAM_PART_ENABLE_DESC( 7)
- PARAM_PART_ENABLE_DESC( 8)
- PARAM_PART_ENABLE_DESC( 9)
- PARAM_PART_ENABLE_DESC(10)
- PARAM_PART_ENABLE_DESC(11)
- PARAM_PART_ENABLE_DESC(12)
- PARAM_PART_ENABLE_DESC(13)
- PARAM_PART_ENABLE_DESC(14)
- PARAM_PART_ENABLE_DESC(15)
- PARAM_PART_ENABLE_DESC(16)
- }
-
- #undef PARAM_PART_ENABLE_DESC
- }
- else if (index <= kParamResBandwidth)
- {
- hints |= NATIVE_PARAMETER_IS_INTEGER;
-
- switch (index)
- {
- case kParamFilterCutoff:
- param.name = "Filter Cutoff";
- break;
- case kParamFilterQ:
- param.name = "Filter Q";
- break;
- case kParamBandwidth:
- param.name = "Bandwidth";
- break;
- case kParamModAmp:
- param.name = "FM Gain";
- param.ranges.def = 127.0f;
- break;
- case kParamResCenter:
- param.name = "Res Center Freq";
- break;
- case kParamResBandwidth:
- param.name = "Res Bandwidth";
- break;
- }
- }
-
- param.hints = static_cast<NativeParameterHints>(hints);
-
- return ¶m;
- }
-
- float getParameterValue(const uint32_t index) const final
- {
- CARLA_SAFE_ASSERT_RETURN(index < kParamCount, 0.0f);
-
- return fParameters[index];
- }
-
- // -------------------------------------------------------------------
- // Plugin midi-program calls
-
- uint32_t getMidiProgramCount() const noexcept override
- {
- return sPrograms.getNativeMidiProgramCount();
- }
-
- const NativeMidiProgram* getMidiProgramInfo(const uint32_t index) const noexcept override
- {
- return sPrograms.getNativeMidiProgramInfo(index);
- }
-
- // -------------------------------------------------------------------
- // Plugin state calls
-
- void setParameterValue(const uint32_t index, const float value) final
- {
- CARLA_SAFE_ASSERT_RETURN(index < kParamCount,);
-
- if (index <= kParamPart16Enabled)
- {
- fParameters[index] = (value >= 0.5f) ? 1.0f : 0.0f;
-
- char msg[24];
- std::sprintf(msg, "/part%i/Penabled", index-kParamPart01Enabled);
-
- #ifdef ZYN_MSG_ANYWHERE
- fMiddleWare->messageAnywhere(msg, (value >= 0.5f) ? "T" : "F");
- #else
- fMiddleWare->transmitMsg("/echo", "ss", "OSC_URL", "");
- fMiddleWare->activeUrl("");
- fMiddleWare->transmitMsg(msg, (value >= 0.5f) ? "T" : "F");
- #endif
- }
- else if (index <= kParamPart16Volume)
- {
- if (carla_isEqual(fParameters[index], value))
- return;
-
- fParameters[index] = std::round(carla_fixedValue(0.0f, 127.0f, value));
-
- char msg[24];
- std::sprintf(msg, "/part%i/Pvolume", index-kParamPart01Volume);
-
- #ifdef ZYN_MSG_ANYWHERE
- fMiddleWare->messageAnywhere(msg, "i", static_cast<int>(fParameters[index]));
- #else
- fMiddleWare->transmitMsg("/echo", "ss", "OSC_URL", "");
- fMiddleWare->activeUrl("");
- fMiddleWare->transmitMsg(msg, "i", static_cast<int>(fParameters[index]));
- #endif
- }
- else if (index <= kParamPart16Panning)
- {
- if (carla_isEqual(fParameters[index], value))
- return;
-
- fParameters[index] = std::round(carla_fixedValue(0.0f, 127.0f, value));
-
- char msg[24];
- std::sprintf(msg, "/part%i/Ppanning", index-kParamPart01Panning);
-
- #ifdef ZYN_MSG_ANYWHERE
- fMiddleWare->messageAnywhere(msg, "i", static_cast<int>(fParameters[index]));
- #else
- fMiddleWare->transmitMsg("/echo", "ss", "OSC_URL", "");
- fMiddleWare->activeUrl("");
- fMiddleWare->transmitMsg(msg, "i", static_cast<int>(fParameters[index]));
- #endif
- }
- else if (index <= kParamResBandwidth)
- {
- const MidiControllers zynControl(getZynControlFromIndex(index));
- CARLA_SAFE_ASSERT_RETURN(zynControl != C_NULL,);
-
- fParameters[index] = std::round(carla_fixedValue(0.0f, 127.0f, value));
-
- for (int npart=0; npart<NUM_MIDI_PARTS; ++npart)
- {
- if (fMaster->part[npart] != nullptr)
- fMaster->part[npart]->SetController(zynControl, static_cast<int>(value));
- }
- }
- }
-
- void setMidiProgram(const uint8_t channel, const uint32_t bank, const uint32_t program) override
- {
- CARLA_SAFE_ASSERT_RETURN(program < BANK_SIZE,);
-
- if (bank == 0)
- {
- // reset part to default
- setState(fDefaultState);
- return;
- }
-
- const char* const filename(sPrograms.getZynProgramFilename(bank, program));
- CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0',);
-
- #ifdef ZYN_MSG_ANYWHERE
- fMiddleWare->messageAnywhere("/load-part", "is", channel, filename);
- #else
- fMiddleWare->transmitMsg("/load-part", "is", channel, filename);
- #endif
- }
-
- void setCustomData(const char* const key, const char* const value) override
- {
- CARLA_SAFE_ASSERT_RETURN(key != nullptr && key[0] != '\0',);
- CARLA_SAFE_ASSERT_RETURN(value != nullptr,);
-
- /**/ if (std::strcmp(key, "CarlaAlternateFile1") == 0) // xmz
- {
- #ifdef ZYN_MSG_ANYWHERE
- fMiddleWare->messageAnywhere("/load_xmz", "s", value);
- #else
- fMiddleWare->transmitMsg("/load_xmz", "s", value);
- #endif
- }
- else if (std::strcmp(key, "CarlaAlternateFile2") == 0) // xiz
- {
- #ifdef ZYN_MSG_ANYWHERE
- fMiddleWare->messageAnywhere("/load_xiz", "is", 0, value);
- #else
- fMiddleWare->transmitMsg("/load_xiz", "is", 0, value);
- #endif
- }
- }
-
- // -------------------------------------------------------------------
- // Plugin process calls
-
- void process(float**, float** const outBuffer, const uint32_t frames,
- const NativeMidiEvent* const midiEvents, const uint32_t midiEventCount) override
- {
- if (! fMutex.tryLock())
- {
- if (! isOffline())
- {
- FloatVectorOperations::clear(outBuffer[0], static_cast<int>(frames));
- FloatVectorOperations::clear(outBuffer[1], static_cast<int>(frames));
- return;
- }
-
- fMutex.lock();
- }
-
- uint32_t framesOffset = 0;
-
- for (uint32_t i=0; i < midiEventCount; ++i)
- {
- const NativeMidiEvent* const midiEvent(&midiEvents[i]);
-
- if (midiEvent->time >= frames)
- continue;
-
- if (midiEvent->time > framesOffset)
- {
- fMaster->GetAudioOutSamples(midiEvent->time-framesOffset, fSynth.samplerate, outBuffer[0]+framesOffset,
- outBuffer[1]+framesOffset);
- framesOffset = midiEvent->time;
- }
-
- const uint8_t status = MIDI_GET_STATUS_FROM_DATA(midiEvent->data);
- const char channel = MIDI_GET_CHANNEL_FROM_DATA(midiEvent->data);
-
- if (MIDI_IS_STATUS_NOTE_OFF(status))
- {
- const char note = static_cast<char>(midiEvent->data[1]);
-
- fMaster->noteOff(channel, note);
- }
- else if (MIDI_IS_STATUS_NOTE_ON(status))
- {
- const char note = static_cast<char>(midiEvent->data[1]);
- const char velo = static_cast<char>(midiEvent->data[2]);
-
- fMaster->noteOn(channel, note, velo);
- }
- else if (MIDI_IS_STATUS_POLYPHONIC_AFTERTOUCH(status))
- {
- const char note = static_cast<char>(midiEvent->data[1]);
- const char pressure = static_cast<char>(midiEvent->data[2]);
-
- fMaster->polyphonicAftertouch(channel, note, pressure);
- }
- else if (MIDI_IS_STATUS_CONTROL_CHANGE(status))
- {
- // skip controls which we map to parameters
- if (getIndexFromZynControl(midiEvent->data[1]) != kParamCount)
- continue;
-
- const int control = midiEvent->data[1];
- const int value = midiEvent->data[2];
-
- fMaster->setController(channel, control, value);
- }
- else if (MIDI_IS_STATUS_PITCH_WHEEL_CONTROL(status))
- {
- const uint8_t lsb = midiEvent->data[1];
- const uint8_t msb = midiEvent->data[2];
- const int value = ((msb << 7) | lsb) - 8192;
-
- fMaster->setController(channel, C_pitchwheel, value);
- }
- }
-
- if (frames > framesOffset)
- fMaster->GetAudioOutSamples(frames-framesOffset, fSynth.samplerate, outBuffer[0]+framesOffset,
- outBuffer[1]+framesOffset);
-
- fMutex.unlock();
- }
-
- // -------------------------------------------------------------------
- // Plugin UI calls
-
- #ifdef HAVE_ZYN_UI_DEPS
- void uiShow(const bool show) override
- {
- if (show)
- {
- if (isPipeRunning())
- {
- const CarlaMutexLocker cml(getPipeLock());
- writeMessage("focus\n", 6);
- flushMessages();
- return;
- }
-
- carla_stdout("Trying to start UI using \"%s\"", getExtUiPath());
-
- CarlaExternalUI::setData(getExtUiPath(), fMiddleWare->getServerAddress(), getUiName());
-
- if (! CarlaExternalUI::startPipeServer(true))
- {
- uiClosed();
- hostUiUnavailable();
- }
- }
- else
- {
- CarlaExternalUI::stopPipeServer(2000);
- }
- }
- #endif
-
- // -------------------------------------------------------------------
- // Plugin state calls
-
- char* getState() const override
- {
- const MiddleWareThread::ScopedStopper mwss(*fMiddleWareThread);
-
- char* data = nullptr;
- fMaster->getalldata(&data);
- return data;
- }
-
- void setState(const char* const data) override
- {
- CARLA_SAFE_ASSERT_RETURN(data != nullptr,);
-
- const MiddleWareThread::ScopedStopper mwss(*fMiddleWareThread);
- const CarlaMutexLocker cml(fMutex);
-
- fMaster->defaults();
- fMaster->putalldata(data);
- fMaster->applyparameters();
- fMaster->initialize_rt();
-
- fMiddleWare->updateResources(fMaster);
-
- _setMasterParameters();
- }
-
- // -------------------------------------------------------------------
- // Plugin dispatcher
-
- void bufferSizeChanged(const uint32_t bufferSize) final
- {
- MiddleWareThread::ScopedStopper mwss(*fMiddleWareThread);
-
- char* const state(getState());
-
- _deleteMaster();
-
- fSynth.buffersize = static_cast<int>(bufferSize);
-
- if (fSynth.buffersize > 32)
- fSynth.buffersize = 32;
-
- fSynth.alias();
-
- _initMaster();
- mwss.updateMiddleWare(fMiddleWare);
-
- setState(state);
- std::free(state);
- }
-
- void sampleRateChanged(const double sampleRate) final
- {
- MiddleWareThread::ScopedStopper mwss(*fMiddleWareThread);
-
- char* const state(getState());
-
- _deleteMaster();
-
- fSynth.samplerate = static_cast<uint>(sampleRate);
- fSynth.alias();
-
- _initMaster();
- mwss.updateMiddleWare(fMiddleWare);
-
- setState(state);
- std::free(state);
- }
-
- // -------------------------------------------------------------------
-
- private:
- MiddleWare* fMiddleWare;
- Master* fMaster;
- SYNTH_T fSynth;
- CarlaConfig fConfig;
- char* fDefaultState;
-
- float fParameters[kParamCount];
-
- CarlaMutex fMutex;
- ScopedPointer<MiddleWareThread> fMiddleWareThread;
-
- static MidiControllers getZynControlFromIndex(const uint index)
- {
- switch (index)
- {
- case kParamFilterCutoff:
- return C_filtercutoff;
- case kParamFilterQ:
- return C_filterq;
- case kParamBandwidth:
- return C_bandwidth;
- case kParamModAmp:
- return C_fmamp;
- case kParamResCenter:
- return C_resonance_center;
- case kParamResBandwidth:
- return C_resonance_bandwidth;
- default:
- return C_NULL;
- }
- }
-
- static Parameters getIndexFromZynControl(const uint8_t control)
- {
- switch (control)
- {
- case C_filtercutoff:
- return kParamFilterCutoff;
- case C_filterq:
- return kParamFilterQ;
- case C_bandwidth:
- return kParamBandwidth;
- case C_fmamp:
- return kParamModAmp;
- case C_resonance_center:
- return kParamResCenter;
- case C_resonance_bandwidth:
- return kParamResBandwidth;
- default:
- return kParamCount;
- }
- }
-
- // -------------------------------------------------------------------
-
- void _initMaster()
- {
- fMiddleWare = new MiddleWare(std::move(fSynth), &fConfig);
- fMiddleWare->setUiCallback(__uiCallback, this);
- fMiddleWare->setIdleCallback(_idleCallback, this);
- _masterChangedCallback(fMiddleWare->spawnMaster());
- }
-
- void _setMasterParameters()
- {
- #ifndef ZYN_MSG_ANYWHERE
- fMiddleWare->transmitMsg("/echo", "ss", "OSC_URL", "");
- fMiddleWare->activeUrl("");
- #endif
- char msg[24];
-
- for (int i=kParamPart16Enabled+1; --i>=kParamPart01Enabled;)
- {
- std::sprintf(msg, "/part%i/Penabled", i-kParamPart01Enabled);
-
- #ifdef ZYN_MSG_ANYWHERE
- fMiddleWare->messageAnywhere(msg, (fParameters[i] >= 0.5f) ? "T" : "F");
- #else
- fMiddleWare->transmitMsg(msg, (fParameters[i] >= 0.5f) ? "T" : "F");
- #endif
- }
-
- for (int i=kParamPart16Volume+1; --i>=kParamPart01Volume;)
- {
- std::sprintf(msg, "/part%i/Pvolume", i-kParamPart01Volume);
-
- #ifdef ZYN_MSG_ANYWHERE
- fMiddleWare->messageAnywhere(msg, "i", static_cast<int>(fParameters[i]));
- #else
- fMiddleWare->transmitMsg(msg, "i", static_cast<int>(fParameters[i]));
- #endif
- }
-
- for (int i=kParamPart16Panning+1; --i>=kParamPart01Panning;)
- {
- std::sprintf(msg, "/part%i/Ppanning", i-kParamPart01Panning);
-
- #ifdef ZYN_MSG_ANYWHERE
- fMiddleWare->messageAnywhere(msg, "i", static_cast<int>(fParameters[i]));
- #else
- fMiddleWare->transmitMsg(msg, "i", static_cast<int>(fParameters[i]));
- #endif
- }
-
- for (int i=0; i<NUM_MIDI_PARTS; ++i)
- {
- fMaster->part[i]->SetController(C_filtercutoff, static_cast<int>(fParameters[kParamFilterCutoff]));
- fMaster->part[i]->SetController(C_filterq, static_cast<int>(fParameters[kParamFilterQ]));
- fMaster->part[i]->SetController(C_bandwidth, static_cast<int>(fParameters[kParamBandwidth]));
- fMaster->part[i]->SetController(C_fmamp, static_cast<int>(fParameters[kParamModAmp]));
- fMaster->part[i]->SetController(C_resonance_center, static_cast<int>(fParameters[kParamResCenter]));
- fMaster->part[i]->SetController(C_resonance_bandwidth, static_cast<int>(fParameters[kParamResBandwidth]));
- }
- }
-
- void _deleteMaster()
- {
- fMaster = nullptr;
- delete fMiddleWare;
- fMiddleWare = nullptr;
- }
-
- void _masterChangedCallback(Master* m)
- {
- fMaster = m;
- fMaster->setMasterChangedCallback(__masterChangedCallback, this);
- }
-
- static void __masterChangedCallback(void* ptr, Master* m)
- {
- ((ZynAddSubFxPlugin*)ptr)->_masterChangedCallback(m);
- }
-
- void _uiCallback(const char* const msg)
- {
- if (std::strncmp(msg, "/part", 5) != 0)
- return;
-
- const char* msgtmp = msg;
- msgtmp += 5;
- CARLA_SAFE_ASSERT_RETURN( msgtmp[0] >= '0' && msgtmp[0] <= '9',);
- CARLA_SAFE_ASSERT_RETURN((msgtmp[1] >= '0' && msgtmp[1] <= '9') || msgtmp[1] == '/',);
-
- char partnstr[3] = { '\0', '\0', '\0' };
-
- partnstr[0] = msgtmp[0];
- ++msgtmp;
-
- if (msgtmp[0] >= '0' && msgtmp[0] <= '9')
- {
- partnstr[1] = msgtmp[0];
- ++msgtmp;
- }
-
- const int partn = std::atoi(partnstr);
- ++msgtmp;
-
- /**/ if (std::strcmp(msgtmp, "Penabled") == 0)
- {
- const int index = kParamPart01Enabled+partn;
- const bool enbl = rtosc_argument(msg,0).T;
-
- fParameters[index] = enbl ? 1.0f : 0.0f;
- uiParameterChanged(kParamPart01Enabled+partn, enbl ? 1.0f : 0.0f);
- }
- else if (std::strcmp(msgtmp, "Pvolume") == 0)
- {
- const int index = kParamPart01Volume+partn;
- const int value = rtosc_argument(msg,0).i;
-
- fParameters[index] = value;
- uiParameterChanged(kParamPart01Volume+partn, value);
- }
- else if (std::strcmp(msgtmp, "Ppanning") == 0)
- {
- const int index = kParamPart01Panning+partn;
- const int value = rtosc_argument(msg,0).i;
-
- fParameters[index] = value;
- uiParameterChanged(kParamPart01Panning+partn, value);
- }
- }
-
- static void __uiCallback(void* ptr, const char* msg)
- {
- ((ZynAddSubFxPlugin*)ptr)->_uiCallback(msg);
- }
-
- static void _idleCallback(void* ptr)
- {
- ((ZynAddSubFxPlugin*)ptr)->hostGiveIdle();
- }
-
- // -------------------------------------------------------------------
-
- PluginClassEND(ZynAddSubFxPlugin)
- CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ZynAddSubFxPlugin)
- };
-
- // -----------------------------------------------------------------------
-
- static const NativePluginDescriptor zynaddsubfxDesc = {
- /* category */ NATIVE_PLUGIN_CATEGORY_SYNTH,
- /* hints */ static_cast<NativePluginHints>(NATIVE_PLUGIN_IS_SYNTH
- #ifdef HAVE_ZYN_UI_DEPS
- |NATIVE_PLUGIN_HAS_UI
- #endif
- |NATIVE_PLUGIN_USES_MULTI_PROGS
- |NATIVE_PLUGIN_USES_STATE),
- /* supports */ static_cast<NativePluginSupports>(NATIVE_PLUGIN_SUPPORTS_CONTROL_CHANGES
- |NATIVE_PLUGIN_SUPPORTS_NOTE_AFTERTOUCH
- |NATIVE_PLUGIN_SUPPORTS_PITCHBEND
- |NATIVE_PLUGIN_SUPPORTS_ALL_SOUND_OFF),
- /* audioIns */ 0,
- /* audioOuts */ 2,
- /* midiIns */ 1,
- /* midiOuts */ 0,
- /* paramIns */ ZynAddSubFxPlugin::kParamCount,
- /* paramOuts */ 0,
- /* name */ "ZynAddSubFX",
- /* label */ "zynaddsubfx",
- /* maker */ "falkTX, Mark McCurry, Nasca Octavian Paul",
- /* copyright */ "GNU GPL v2+",
- PluginDescriptorFILL(ZynAddSubFxPlugin)
- };
-
- // -----------------------------------------------------------------------
-
- CARLA_EXPORT
- void carla_register_native_plugin_zynaddsubfx_synth();
-
- CARLA_EXPORT
- void carla_register_native_plugin_zynaddsubfx_synth()
- {
- carla_register_native_plugin(&zynaddsubfxDesc);
- }
-
- // -----------------------------------------------------------------------
|