| 1 |
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/*************************************************************************** |
| 3 |
* pluginlv2.h |
| 4 |
* |
| 5 |
* Sun Feb 7 15:15:23 CET 2016 |
| 6 |
* Copyright 2016 Bent Bisballe Nyeng |
| 7 |
* [email protected] |
| 8 |
****************************************************************************/ |
| 9 |
|
| 10 |
/* |
| 11 |
* This file is part of PluginGizmo. |
| 12 |
* |
| 13 |
* PluginGizmo is free software; you can redistribute it and/or modify |
| 14 |
* it under the terms of the GNU Lesser General Public License as published by |
| 15 |
* the Free Software Foundation; either version 3 of the License, or |
| 16 |
* (at your option) any later version. |
| 17 |
* |
| 18 |
* PluginGizmo is distributed in the hope that it will be useful, |
| 19 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
* GNU Lesser General Public License for more details. |
| 22 |
* |
| 23 |
* You should have received a copy of the GNU Lesser General Public License |
| 24 |
* along with PluginGizmo; if not, write to the Free Software |
| 25 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 26 |
*/ |
| 27 |
#pragma once |
| 28 |
|
| 29 |
#include <plugin.h> |
| 30 |
|
| 31 |
#include <array> |
| 32 |
#include <atomic> |
| 33 |
|
| 34 |
#include <lv2.h> |
| 35 |
#include <lv2/lv2plug.in/ns/ext/urid/urid.h> |
| 36 |
#include <lv2/lv2plug.in/ns/ext/state/state.h> |
| 37 |
#include <lv2/lv2plug.in/ns/ext/atom/atom.h> |
| 38 |
#include <lv2/lv2plug.in/ns/ext/dynmanifest/dynmanifest.h> |
| 39 |
#include <lv2/lv2plug.in/ns/extensions/ui/ui.h> |
| 40 |
|
| 41 |
#define DISPLAY_INTERFACE |
| 42 |
#define MIDNAM_INTERFACE |
| 43 |
|
| 44 |
#ifdef DISPLAY_INTERFACE |
| 45 |
#include "inline-display.h" |
| 46 |
#endif |
| 47 |
|
| 48 |
#ifdef MIDNAM_INTERFACE |
| 49 |
#include "midnam_lv2.h" |
| 50 |
#endif |
| 51 |
|
| 52 |
enum class LV2Ports |
| 53 |
{ |
| 54 |
FreeWheel = 0, |
| 55 |
Latency = 1, |
| 56 |
PortOffset = 2, |
| 57 |
}; |
| 58 |
|
| 59 |
class PluginLV2 |
| 60 |
: public Plugin |
| 61 |
{ |
| 62 |
public: |
| 63 |
virtual ~PluginLV2() = default; |
| 64 |
|
| 65 |
//! Not used in LV2 |
| 66 |
void init() override; |
| 67 |
|
| 68 |
//! Get current free-wheel mode. |
| 69 |
bool getFreeWheel() const override; |
| 70 |
|
| 71 |
|
| 72 |
//! Call this to get current samplerate. |
| 73 |
float getSamplerate() override; |
| 74 |
|
| 75 |
//! This method is called by the host when the samplerate changes. |
| 76 |
virtual void onSamplerateChange(float samplerate) override = 0; |
| 77 |
|
| 78 |
//! Call this to get current frame-size. |
| 79 |
std::size_t getFramesize() override; |
| 80 |
|
| 81 |
//! This method is called by the host when the frame-size changes. |
| 82 |
virtual void onFramesizeChange(std::size_t framesize) override = 0; |
| 83 |
|
| 84 |
|
| 85 |
//! Call this to get current active state |
| 86 |
bool getActive() override; |
| 87 |
|
| 88 |
//! This method is called by the host when the active state changes. |
| 89 |
virtual void onActiveChange(bool active) override = 0; |
| 90 |
|
| 91 |
|
| 92 |
//! This method is called by the host to get the current state for storing. |
| 93 |
virtual std::string onStateSave() override = 0; |
| 94 |
|
| 95 |
//! This method is called by the host when a new state has been loaded. |
| 96 |
virtual void onStateRestore(const std::string& config) override = 0; |
| 97 |
|
| 98 |
|
| 99 |
//! This is method is called by the host to get the current latency. |
| 100 |
//! \param The latency in samples. |
| 101 |
float getLatency() override; |
| 102 |
|
| 103 |
//! Call this method to signal a latency change to the host. |
| 104 |
//! \param latency The latency in samples. |
| 105 |
void setLatency(float latency) override; |
| 106 |
|
| 107 |
|
| 108 |
//! Called by the the host to get the number of audio input channels. |
| 109 |
//! This must remain constant during the lifespan of the plugin instance. |
| 110 |
virtual std::size_t getNumberOfAudioInputs() override = 0; |
| 111 |
|
| 112 |
//! Called by the the host to get the number of audio output channels. |
| 113 |
//! This must remain constant during the lifespan of the plugin instance. |
| 114 |
virtual std::size_t getNumberOfAudioOutputs() override = 0; |
| 115 |
|
| 116 |
|
| 117 |
//! Called by the the host to get the number of midi input channels. |
| 118 |
//! This must remain constant during the lifespan of the plugin instance. |
| 119 |
virtual std::size_t getNumberOfMidiInputs() override = 0; |
| 120 |
|
| 121 |
//! Called by the the host to get the number of midi output channels. |
| 122 |
//! This must remain constant during the lifespan of the plugin instance. |
| 123 |
virtual std::size_t getNumberOfMidiOutputs() override = 0; |
| 124 |
|
| 125 |
//! Call this method to set midnam data for midi input |
| 126 |
virtual void setMidnamData(const std::vector<std::pair<int, std::string>>& midnam) override; |
| 127 |
|
| 128 |
//! Get unique plugin id. |
| 129 |
std::string getId() override = 0; |
| 130 |
|
| 131 |
// Functions used to set plugin information. |
| 132 |
std::string getEffectName() override = 0; |
| 133 |
std::string getVendorString() override = 0; |
| 134 |
std::string getProductString() override = 0; |
| 135 |
PluginCategory getPluginCategory() override = 0; |
| 136 |
|
| 137 |
virtual void process(std::size_t pos, |
| 138 |
const std::vector<MidiEvent>& input_events, |
| 139 |
std::vector<MidiEvent>& output_events, |
| 140 |
const std::vector<const float*>& input_samples, |
| 141 |
const std::vector<float*>& output_samples, |
| 142 |
std::size_t count) override = 0; |
| 143 |
|
| 144 |
// |
| 145 |
// Inline GUI (optional) |
| 146 |
// |
| 147 |
|
| 148 |
//! Return true if a GUI implementation is to be used. |
| 149 |
virtual bool hasInlineGUI() override |
| 150 |
{ |
| 151 |
return false; |
| 152 |
} |
| 153 |
|
| 154 |
//! Render call back. |
| 155 |
//! \param width The client area width as specified by the host. |
| 156 |
//! \param max_height The maximum allowed clieant area height as specified |
| 157 |
//! by the host. |
| 158 |
//! \param context The render context filled an maintained by the plugin. |
| 159 |
virtual void onInlineRedraw(std::size_t width, |
| 160 |
std::size_t max_height, |
| 161 |
InlineDrawContext& context) override {} |
| 162 |
|
| 163 |
// |
| 164 |
// GUI (optional) |
| 165 |
// |
| 166 |
|
| 167 |
//! Return true if a GUI implementation is to be used. |
| 168 |
virtual bool hasGUI() override |
| 169 |
{ |
| 170 |
return false; |
| 171 |
} |
| 172 |
|
| 173 |
//! Create new window. |
| 174 |
virtual void* createWindow(void *parent) override { return nullptr; } |
| 175 |
|
| 176 |
//! Destroy window. |
| 177 |
virtual void onDestroyWindow() override {} |
| 178 |
|
| 179 |
//! Show window. |
| 180 |
virtual void onShowWindow() override {} |
| 181 |
|
| 182 |
//! Hide window. |
| 183 |
virtual void onHideWindow() override {} |
| 184 |
|
| 185 |
//! Called regularly by host; process ui events. |
| 186 |
virtual void onIdle() override {} |
| 187 |
|
| 188 |
//! Signal new window size to host. |
| 189 |
void resizeWindow(std::size_t width, std::size_t height) override; |
| 190 |
|
| 191 |
//! Signal close window event to the host. |
| 192 |
void closeWindow() override; |
| 193 |
|
| 194 |
public: |
| 195 |
static LV2_Handle instantiate(const LV2_Descriptor* descriptor, |
| 196 |
double sample_rate, |
| 197 |
const char* bundle_path, |
| 198 |
const LV2_Feature* const * features); |
| 199 |
|
| 200 |
static void connectPort(LV2_Handle instance, uint32_t port, |
| 201 |
void *data_location); |
| 202 |
|
| 203 |
static void run(LV2_Handle instance, uint32_t sample_count); |
| 204 |
|
| 205 |
static void activate(LV2_Handle instance); |
| 206 |
|
| 207 |
static void deactivate(LV2_Handle instance); |
| 208 |
|
| 209 |
static void cleanup(LV2_Handle instance); |
| 210 |
|
| 211 |
static const void* extensionData(const char *uri); |
| 212 |
|
| 213 |
static LV2_State_Status save(LV2_Handle instance, |
| 214 |
LV2_State_Store_Function store, |
| 215 |
LV2_State_Handle handle, |
| 216 |
uint32_t flags, |
| 217 |
const LV2_Feature *const * features); |
| 218 |
|
| 219 |
static LV2_State_Status restore(LV2_Handle instance, |
| 220 |
LV2_State_Retrieve_Function retrieve, |
| 221 |
LV2_State_Handle handle, |
| 222 |
uint32_t flags, |
| 223 |
const LV2_Feature *const * features); |
| 224 |
|
| 225 |
static LV2UI_Handle uiInstantiate(const LV2UI_Descriptor * descriptor, |
| 226 |
const char * plugin_uri, |
| 227 |
const char * bundle_path, |
| 228 |
LV2UI_Write_Function write_function, |
| 229 |
LV2UI_Controller controller, |
| 230 |
LV2UI_Widget * widget, |
| 231 |
const LV2_Feature * const * features); |
| 232 |
|
| 233 |
static void uiCleanup(LV2UI_Handle handle); |
| 234 |
|
| 235 |
static int uiIdle(LV2UI_Handle handle); |
| 236 |
|
| 237 |
static const void* uiExtensionData(const char* uri); |
| 238 |
|
| 239 |
private: |
| 240 |
float* free_wheel_port{nullptr}; |
| 241 |
bool free_wheel{false}; |
| 242 |
|
| 243 |
float sample_rate{0}; |
| 244 |
|
| 245 |
float* latency_port{nullptr}; |
| 246 |
|
| 247 |
std::size_t frame_size{0}; |
| 248 |
|
| 249 |
std::size_t pos{0}; |
| 250 |
|
| 251 |
std::vector<LV2_Atom_Sequence*> input_event_ports; |
| 252 |
std::vector<LV2_Atom_Sequence*> output_event_ports; |
| 253 |
std::vector<const float*> input_audio_ports; |
| 254 |
std::vector<float*> output_audio_ports; |
| 255 |
|
| 256 |
LV2_URID_Map* map{nullptr}; |
| 257 |
|
| 258 |
#ifdef DISPLAY_INTERFACE |
| 259 |
LV2_Inline_Display_Image_Surface surf; |
| 260 |
LV2_Inline_Display* queue_draw{nullptr}; |
| 261 |
|
| 262 |
InlineDrawContext drawContext; |
| 263 |
|
| 264 |
static LV2_Inline_Display_Image_Surface *inlineRender(LV2_Handle instance, |
| 265 |
uint32_t w, |
| 266 |
uint32_t max_h); |
| 267 |
#endif |
| 268 |
|
| 269 |
#ifdef MIDNAM_INTERFACE |
| 270 |
LV2_Midnam* midnam{nullptr}; |
| 271 |
static char* MidnamFile (LV2_Handle instance); |
| 272 |
static char* MidnamModel (LV2_Handle instance); |
| 273 |
static void MidnamFree (char*); |
| 274 |
#endif |
| 275 |
std::atomic<bool> midnam_changed{false}; |
| 276 |
std::array<std::pair<int, std::string>, 127> midnamData; // At most 127 different midinotes. |
| 277 |
|
| 278 |
bool active{false}; |
| 279 |
|
| 280 |
// |
| 281 |
// GUI |
| 282 |
// |
| 283 |
LV2UI_Resize* resize{nullptr}; |
| 284 |
}; |
| 285 |
|
| 286 |
PG_EXPORT PluginLV2* createEffectInstance(); |