Skip to content

Commit

Permalink
Pass sample rate and scale factor to LV2 UIs
Browse files Browse the repository at this point in the history
Signed-off-by: falkTX <falktx@falktx.com>
  • Loading branch information
falkTX committed May 14, 2020
1 parent 3c52461 commit 1b4f479
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/lv2vst.cc
Expand Up @@ -357,6 +357,11 @@ VstPlugCategory LV2Vst::get_category ()
return kPlugCategUnknown;
}

float LV2Vst::get_sample_rate ()
{
return _sample_rate;
}

/* state.cc implements
* LV2Vst::get_chunk ();
* LV2Vst::set_chunk ();
Expand Down
8 changes: 7 additions & 1 deletion src/lv2vst.h
Expand Up @@ -72,7 +72,7 @@ class Lv2VstUI : public VstGui
virtual ~Lv2VstUI ();

virtual bool get_rect (ERect** rect);
virtual bool open (void* ptr);
virtual bool open (void* ptr, float scale_factor);
virtual void close ();
virtual bool is_open () const;
virtual void idle ();
Expand Down Expand Up @@ -105,9 +105,14 @@ class Lv2VstUI : public VstGui
LV2UI_Idle_Interface* _idle_iface;
LV2_Atom_Sequence* _atombuf;
LV2_URID _uri_atom_EventTransfer;
LV2_URID _uri_atom_Float;

void* _lib_handle;
uint32_t _port_event_recursion;

// used for UI options
float _sample_rate;
float _scale_factor;
};

class LV2Vst : public VstPlugin
Expand Down Expand Up @@ -145,6 +150,7 @@ class LV2Vst : public VstPlugin
virtual int32_t bypass_plugin (bool bypass);

virtual VstPlugCategory get_category ();
virtual float get_sample_rate ();

virtual int32_t process_events (VstEvents* events);

Expand Down
23 changes: 22 additions & 1 deletion src/lv2vstui.cc
Expand Up @@ -23,6 +23,12 @@
#include "loadlib.h"
#include "lv2vst.h"

#include "lv2/lv2plug.in/ns/ext/parameters/parameters.h"

#ifndef LV2_UI__scaleFactor
#define LV2_UI__scaleFactor LV2_UI_PREFIX "scaleFactor"
#endif

static void write_function (
LV2UI_Controller controller,
uint32_t port_index,
Expand Down Expand Up @@ -73,6 +79,7 @@ Lv2VstUI::Lv2VstUI (LV2Vst* effect)

_atombuf = (LV2_Atom_Sequence*) malloc (desc->min_atom_bufsiz * sizeof (uint8_t));
_uri_atom_EventTransfer = _lv2vst->map_uri (LV2_ATOM__eventTransfer);
_uri_atom_Float = _lv2vst->map_uri (LV2_ATOM__Float);

_rect.top = 0;
_rect.left = 0;
Expand Down Expand Up @@ -107,12 +114,15 @@ void Lv2VstUI::set_size (int width, int height) {
_lv2vst->size_window (width, height);
}

bool Lv2VstUI::open (void* ptr)
bool Lv2VstUI::open (void* ptr, float scale_factor)
{
if (!plugin_gui || gui_instance) {
return false;
}

_sample_rate = _lv2vst->get_sample_rate ();
_scale_factor = scale_factor;

uri_map.handle = _lv2vst->map_instance ();
uri_map.map = &Lv2UriMap::uri_to_id;
uri_unmap.handle = uri_map.handle;
Expand All @@ -121,17 +131,28 @@ bool Lv2VstUI::open (void* ptr)
lv2ui_resize.handle = this;
lv2ui_resize.ui_resize = &Lv2VstUI::ui_resize;

/* options to pass to UI */
const LV2_Options_Option options[] = {
{ LV2_OPTIONS_INSTANCE, 0, _lv2vst->map_uri (LV2_PARAMETERS__sampleRate),
sizeof(float), _uri_atom_Float, &_sample_rate },
{ LV2_OPTIONS_INSTANCE, 0, _lv2vst->map_uri (LV2_UI__scaleFactor),
sizeof(float), _uri_atom_Float, &_scale_factor },
{ LV2_OPTIONS_INSTANCE, 0, 0, 0, 0, NULL }
};

const LV2_Feature resize_feature = { LV2_UI__resize, &lv2ui_resize};
const LV2_Feature parent_feature = { LV2_UI__parent, ptr};
const LV2_Feature map_feature = { LV2_URID__map, &uri_map};
const LV2_Feature unmap_feature = { LV2_URID__unmap, &uri_unmap };
const LV2_Feature instance_feature = { LV2_INSTANCE_ACCESS_URI, _lv2vst->plugin_instance ()};
const LV2_Feature options_feature = { LV2_OPTIONS__options, (void*)&options };

const LV2_Feature* ui_features[] = {
&map_feature, &unmap_feature,
&resize_feature,
&parent_feature,
&instance_feature,
&options_feature,
NULL
};

Expand Down
10 changes: 8 additions & 2 deletions src/vst.h
Expand Up @@ -44,7 +44,7 @@ class VstGui
virtual ~VstGui () {}

virtual bool get_rect (ERect** rect) = 0;
virtual bool open (void* ptr) = 0;
virtual bool open (void* ptr, float scale_factor) = 0;
virtual void close () = 0;
virtual bool is_open () const = 0;
virtual void idle () {}
Expand All @@ -62,6 +62,7 @@ class VstPlugin
, _block_size (8192)
, _n_params (_n_params)
, _editor (0)
, _ui_scale_factor (1.0f)
{
memset (&_effect, 0, sizeof (AEffect));
_effect.magic = kEffectMagic;
Expand Down Expand Up @@ -117,7 +118,7 @@ class VstPlugin
if (_editor) { v = _editor->get_rect ((ERect**)ptr) ? 1 : 0; }
break;
case effEditOpen:
if (_editor) { v = _editor->open (ptr) ? 1 : 0; }
if (_editor) { v = _editor->open (ptr, _ui_scale_factor) ? 1 : 0; }
break;
case effEditClose:
if (_editor) { _editor->close (); }
Expand Down Expand Up @@ -167,6 +168,10 @@ class VstPlugin
case effGetVstVersion:
v = 2400;
break;
case 50: // effVendorSpecific
if (index == CCONST('P', 'r', 'e', 'S') && value == CCONST('A', 'e', 'C', 's'))
_ui_scale_factor = opt;
break;
default:
#ifndef NDEBUG
printf ("LV2Host: Unhandled opcode: %d\n", opcode);
Expand Down Expand Up @@ -266,6 +271,7 @@ class VstPlugin

VstGui* _editor;
AEffect _effect;
float _ui_scale_factor;

private:
static intptr_t _dispatcher_ (AEffect* e, int32_t opcode, int32_t index, intptr_t value, void* ptr, float opt)
Expand Down

0 comments on commit 1b4f479

Please sign in to comment.