|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476 |
- /*
- * DISTRHO Plugin Toolkit (DPT)
- * Copyright (C) 2012-2013 Filipe Coelho <falktx@falktx.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation.
- *
- * 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 Lesser General Public License for more details.
- *
- * For a full copy of the license see the LGPL.txt file
- */
-
- #include "carla_native.hpp"
- #include "carla_utils.hpp"
-
- #include <QtGui/QMainWindow>
-
- #include "DistrhoPluginMain.cpp"
-
- #if DISTRHO_PLUGIN_HAS_UI
- # include "DistrhoUIMain.cpp"
- #endif
-
- // -------------------------------------------------
-
- START_NAMESPACE_DISTRHO
-
- #if DISTRHO_PLUGIN_HAS_UI
- // -----------------------------------------------------------------------
- // Carla UI
-
- class UICarla : public QMainWindow
- {
- public:
- UICarla(const HostDescriptor* const host_, PluginInternal* const plugin_)
- : QMainWindow(nullptr),
- host(host_),
- plugin(plugin_),
- widget(this),
- ui(this, (intptr_t)widget.winId(), setParameterCallback, setStateCallback, uiEditParameterCallback, uiSendNoteCallback, uiResizeCallback)
- {
- setCentralWidget(&widget);
- setFixedSize(ui.getWidth(), ui.getHeight());
- setWindowTitle(DISTRHO_PLUGIN_NAME);
- }
-
- ~UICarla()
- {
- }
-
- // ---------------------------------------------
-
- void carla_show(const bool yesNo)
- {
- setVisible(yesNo);
- }
-
- void carla_idle()
- {
- ui.idle();
- }
-
- void carla_setParameterValue(const uint32_t index, const float value)
- {
- ui.parameterChanged(index, value);
- }
-
- # if DISTRHO_PLUGIN_WANT_PROGRAMS
- void carla_setMidiProgram(const uint32_t realProgram)
- {
- ui.programChanged(realProgram);
- }
- # endif
-
- # if DISTRHO_PLUGIN_WANT_STATE
- void carla_setCustomData(const char* const key, const char* const value)
- {
- ui.stateChanged(key, value);
- }
- # endif
-
- // ---------------------------------------------
-
- protected:
- void setParameterValue(uint32_t rindex, float value)
- {
- host->ui_parameter_changed(host->handle, rindex, value);
- }
-
- # if DISTRHO_PLUGIN_WANT_STATE
- void setState(const char* key, const char* value)
- {
- host->ui_custom_data_changed(host->handle, key, value);
- }
- # endif
-
- void uiEditParameter(uint32_t, bool)
- {
- // TODO
- }
-
- # if DISTRHO_PLUGIN_IS_SYNTH
- void uiSendNote(bool, uint8_t, uint8_t, uint8_t)
- {
- // TODO
- }
- # endif
-
- void uiResize(int width, int height)
- {
- setFixedSize(width, height);
- }
-
- void closeEvent(QCloseEvent* event)
- {
- host->ui_closed(host->handle);
-
- // FIXME - ignore event?
- QMainWindow::closeEvent(event);
- }
-
- private:
- // Plugin stuff
- const HostDescriptor* const host;
- PluginInternal* const plugin;
-
- // Qt4 stuff
- QWidget widget;
-
- // UI
- UIInternal ui;
-
- // ---------------------------------------------
- // Callbacks
-
- static void setParameterCallback(void* ptr, uint32_t rindex, float value)
- {
- if (UICarla* _this_ = (UICarla*)ptr)
- _this_->setParameterValue(rindex, value);
- }
-
- static void setStateCallback(void* ptr, const char* key, const char* value)
- {
- # if DISTRHO_PLUGIN_WANT_STATE
- if (UICarla* _this_ = (UICarla*)ptr)
- _this_->setState(key, value);
- # else
- return;
-
- // unused
- Q_UNUSED(ptr);
- Q_UNUSED(key);
- Q_UNUSED(value);
- # endif
- }
-
- static void uiEditParameterCallback(void* ptr, uint32_t index, bool started)
- {
- if (UICarla* _this_ = (UICarla*)ptr)
- _this_->uiEditParameter(index, started);
- }
-
- static void uiSendNoteCallback(void* ptr, bool onOff, uint8_t channel, uint8_t note, uint8_t velocity)
- {
- # if DISTRHO_PLUGIN_IS_SYNTH
- if (UICarla* _this_ = (UICarla*)ptr)
- _this_->uiSendNote(onOff, channel, note, velocity);
- # else
- return;
-
- // unused
- Q_UNUSED(ptr);
- Q_UNUSED(onOff);
- Q_UNUSED(channel);
- Q_UNUSED(note);
- Q_UNUSED(velocity);
- # endif
- }
-
- static void uiResizeCallback(void* ptr, int width, int height)
- {
- if (UICarla* _this_ = (UICarla*)ptr)
- _this_->uiResize(width, height);
- }
-
- CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(UICarla)
- };
- #endif
-
- // -----------------------------------------------------------------------
- // Carla Plugin
-
- class PluginCarla : public PluginDescriptorClass
- {
- public:
- PluginCarla(const HostDescriptor* const host)
- : PluginDescriptorClass(host)
- {
- #if DISTRHO_PLUGIN_HAS_UI
- uiPtr = nullptr;
- #endif
- }
-
- ~PluginCarla()
- {
- #if DISTRHO_PLUGIN_HAS_UI
- uiPtr = nullptr;
- #endif
- }
-
- protected:
- // -------------------------------------------------------------------
- // Plugin parameter calls
-
- uint32_t getParameterCount()
- {
- return plugin.parameterCount();
- }
-
- const ::Parameter* getParameterInfo(const uint32_t index)
- {
- CARLA_ASSERT(index < getParameterCount());
-
- static ::Parameter param;
-
- // reset
- param.hints = ::PARAMETER_IS_ENABLED;
- param.scalePointCount = 0;
- param.scalePoints = nullptr;
-
- {
- int nativeparamHints = 0;
- const uint32_t paramHints = plugin.parameterHints(index);
-
- if (paramHints & PARAMETER_IS_AUTOMABLE)
- nativeparamHints |= ::PARAMETER_IS_AUTOMABLE;
- if (paramHints & PARAMETER_IS_BOOLEAN)
- nativeparamHints |= ::PARAMETER_IS_BOOLEAN;
- if (paramHints & PARAMETER_IS_INTEGER)
- nativeparamHints |= ::PARAMETER_IS_INTEGER;
- if (paramHints & PARAMETER_IS_LOGARITHMIC)
- nativeparamHints |= ::PARAMETER_IS_LOGARITHMIC;
- if (paramHints & PARAMETER_IS_OUTPUT)
- nativeparamHints |= ::PARAMETER_IS_OUTPUT;
-
- param.hints = static_cast<ParameterHints>(nativeparamHints);
- }
-
- param.name = plugin.parameterName(index);
- param.unit = plugin.parameterUnit(index);
-
- {
- const ParameterRanges& ranges(plugin.parameterRanges(index));
-
- param.ranges.def = ranges.def;
- param.ranges.min = ranges.min;
- param.ranges.max = ranges.max;
- param.ranges.step = ranges.step;
- param.ranges.stepSmall = ranges.stepSmall;
- param.ranges.stepLarge = ranges.stepLarge;
- }
-
- return ¶m;
- }
-
- float getParameterValue(const uint32_t index)
- {
- CARLA_ASSERT(index < getParameterCount());
-
- return plugin.parameterValue(index);
- }
-
- // getParameterText unused
-
- // -------------------------------------------------------------------
- // Plugin midi-program calls
-
- #if DISTRHO_PLUGIN_WANT_PROGRAMS
- virtual uint32_t getMidiProgramCount()
- {
- return plugin.programCount();
- }
-
- virtual const ::MidiProgram* getMidiProgramInfo(const uint32_t index)
- {
- CARLA_ASSERT(index < getMidiProgramCount());
-
- if (index >= plugin.programCount())
- return nullptr;
-
- static ::MidiProgram midiProgram;
-
- midiProgram.bank = index / 128;
- midiProgram.program = index % 128;
- midiProgram.name = plugin.programName(index);
-
- return &midiProgram;
- }
- #endif
-
- // -------------------------------------------------------------------
- // Plugin state calls
-
- void setParameterValue(const uint32_t index, const float value)
- {
- CARLA_ASSERT(index < getParameterCount());
-
- plugin.setParameterValue(index, value);
- }
-
- #if DISTRHO_PLUGIN_WANT_PROGRAMS
- void setMidiProgram(const uint32_t bank, const uint32_t program)
- {
- const uint32_t realProgram = bank * 128 + program;
-
- if (realProgram >= plugin.programCount())
- return;
-
- plugin.setProgram(realProgram);
- }
- #endif
-
- #if DISTRHO_PLUGIN_WANT_STATE
- void setCustomData(const char* const key, const char* const value)
- {
- CARLA_ASSERT(key);
- CARLA_ASSERT(value);
-
- plugin.setState(key, value);
- }
- #endif
-
- // -------------------------------------------------------------------
- // Plugin process calls
-
- void activate()
- {
- plugin.activate();
- }
-
- void deactivate()
- {
- plugin.deactivate();
- }
-
- #if DISTRHO_PLUGIN_IS_SYNTH
- void process(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t midiEventCount, const ::MidiEvent* const midiEvents)
- {
- uint32_t i;
-
- for (i=0; i < midiEventCount && i < MAX_MIDI_EVENTS; i++)
- {
- const ::MidiEvent* const midiEvent = &midiEvents[i];
- MidiEvent* const realMidiEvent = &realMidiEvents[i];
-
- realMidiEvent->buffer[0] = midiEvent->data[0];
- realMidiEvent->buffer[1] = midiEvent->data[1];
- realMidiEvent->buffer[2] = midiEvent->data[2];
- realMidiEvent->frame = midiEvent->time;
- }
-
- plugin.run(inBuffer, outBuffer, frames, i, realMidiEvents);
- }
- #else
- void process(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t, const ::MidiEvent* const)
- {
- plugin.run(inBuffer, outBuffer, frames, 0, nullptr);
- }
- #endif
-
- // -------------------------------------------------------------------
- // Plugin UI calls
-
- #if DISTRHO_PLUGIN_HAS_UI
- void uiShow(const bool show)
- {
- if (show)
- createUiIfNeeded();
-
- if (uiPtr != nullptr)
- uiPtr->carla_show(show);
- }
-
- void uiIdle()
- {
- CARLA_ASSERT(uiPtr);
-
- if (uiPtr != nullptr)
- uiPtr->carla_idle();
- }
-
- void uiSetParameterValue(const uint32_t index, const float value)
- {
- CARLA_ASSERT(uiPtr);
- CARLA_ASSERT(index < getParameterCount());
-
- if (uiPtr != nullptr)
- uiPtr->carla_setParameterValue(index, value);
- }
-
- # if DISTRHO_PLUGIN_WANT_PROGRAMS
- void uiSetMidiProgram(const uint32_t bank, const uint32_t program)
- {
- CARLA_ASSERT(uiPtr);
-
- uint32_t realProgram = bank * 128 + program;
-
- if (realProgram >= plugin.programCount())
- return;
-
- if (uiPtr != nullptr)
- uiPtr->carla_setMidiProgram(realProgram);
- }
- # endif
-
- # if DISTRHO_PLUGIN_WANT_STATE
- void uiSetCustomData(const char* const key, const char* const value)
- {
- CARLA_ASSERT(uiPtr);
- CARLA_ASSERT(key);
- CARLA_ASSERT(value);
-
- if (uiPtr != nullptr)
- uiPtr->carla_setCustomData(key, value);
- }
- # endif
- #endif
-
- // -------------------------------------------------------------------
-
- private:
- PluginInternal plugin;
-
- #if DISTRHO_PLUGIN_IS_SYNTH
- MidiEvent realMidiEvents[MAX_MIDI_EVENTS];
- #endif
-
- #if DISTRHO_PLUGIN_HAS_UI
- // UI
- ScopedPointer<UICarla> uiPtr;
-
- void createUiIfNeeded()
- {
- if (uiPtr == nullptr)
- {
- d_lastUiSampleRate = getSampleRate();
- uiPtr = new UICarla(getHostHandle(), &plugin);
- }
- }
- #endif
-
- CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(PluginCarla)
-
- // -------------------------------------------------------------------
-
- public:
- static PluginHandle _instantiate(const PluginDescriptor*, HostDescriptor* host)
- {
- d_lastBufferSize = host->get_buffer_size(host->handle);
- d_lastSampleRate = host->get_sample_rate(host->handle);
- return new PluginCarla(host);
- }
-
- static void _cleanup(PluginHandle handle)
- {
- delete (PluginCarla*)handle;
- }
- };
-
- END_NAMESPACE_DISTRHO
-
- // -----------------------------------------------------------------------
|