Cont'd work on Virtual-keyboard

* allow gtk_pianokeyboard to respond to y-axis click position
  and include MIDI velocity with note-on signal
* add configuration settings to virual-keyboard
  min/max velocity, keyboard-layout
* add a CC7 controller knob
This commit is contained in:
Robin Gareus 2019-10-18 17:26:25 +02:00
parent 148a9682be
commit 19e4f47b4b
Signed by: rgareus
GPG Key ID: A090BCE02CF57F04
9 changed files with 311 additions and 36 deletions

View File

@ -1392,7 +1392,7 @@ GenericPluginUI::toggle_pianokeyboard ()
}
void
GenericPluginUI::_note_on_event_handler(GtkWidget*, int note, gpointer arg)
GenericPluginUI::_note_on_event_handler(GtkWidget*, int note, int, gpointer arg)
{
((GenericPluginUI*)arg)->note_on_event_handler(note);
}

View File

@ -139,7 +139,7 @@ draw_note(PianoKeyboard *pk, cairo_t* cr, int note)
}
static int
press_key(PianoKeyboard *pk, int key)
press_key(PianoKeyboard *pk, int key, int vel)
{
assert(key >= 0);
assert(key < NNOTES);
@ -164,7 +164,7 @@ press_key(PianoKeyboard *pk, int key)
pk->notes[key].pressed = 1;
g_signal_emit_by_name(GTK_WIDGET(pk), "note-on", key);
g_signal_emit_by_name(GTK_WIDGET(pk), "note-on", key, vel);
queue_note_draw(pk, key);
return 1;
@ -405,8 +405,7 @@ keyboard_event_handler(GtkWidget *mk, GdkEventKey *event, gpointer ignored)
assert(note < NNOTES);
if (event->type == GDK_KEY_PRESS) {
press_key(pk, note);
press_key(pk, note, pk->key_velocity);
} else if (event->type == GDK_KEY_RELEASE) {
release_key(pk, note);
}
@ -441,6 +440,22 @@ get_note_for_xy(PianoKeyboard *pk, int x, int y)
return -1;
}
static int
get_velocity_for_note_at_y(PianoKeyboard *pk, int note, int y)
{
if (note < 0) {
return 0;
}
int vel = pk->min_velocity + (pk->max_velocity - pk->min_velocity) * y / pk->notes[note].h;
if (vel < 1) {
return 1;
} else if (vel > 127) {
return 127;
}
return vel;
}
static gboolean
mouse_button_event_handler(PianoKeyboard *pk, GdkEventButton *event, gpointer ignored)
{
@ -464,7 +479,7 @@ mouse_button_event_handler(PianoKeyboard *pk, GdkEventButton *event, gpointer ig
if (pk->note_being_pressed_using_mouse >= 0)
release_key(pk, pk->note_being_pressed_using_mouse);
press_key(pk, note);
press_key(pk, note, get_velocity_for_note_at_y (pk, note, y));
pk->note_being_pressed_using_mouse = note;
} else if (event->type == GDK_BUTTON_RELEASE) {
@ -493,13 +508,16 @@ mouse_motion_event_handler(PianoKeyboard *pk, GdkEventMotion *event, gpointer ig
if ((event->state & GDK_BUTTON1_MASK) == 0)
return TRUE;
note = get_note_for_xy(pk, event->x, event->y);
int x = event->x;
int y = event->y;
note = get_note_for_xy(pk, x, y);
if (note != pk->note_being_pressed_using_mouse && note >= 0) {
if (pk->note_being_pressed_using_mouse >= 0)
if (pk->note_being_pressed_using_mouse >= 0) {
release_key(pk, pk->note_being_pressed_using_mouse);
press_key(pk, note);
}
press_key(pk, note, get_velocity_for_note_at_y (pk, note, y));
pk->note_being_pressed_using_mouse = note;
}
@ -610,6 +628,42 @@ piano_keyboard_size_allocate(GtkWidget *widget, GtkAllocation *allocation)
}
}
typedef void (*GMarshalFunc_VOID__INT_INT) (gpointer data1,
gint arg1,
gint arg2,
gpointer data2);
static void
g_cclosure_user_marshal_VOID__INT_INT (GClosure *closure,
GValue *return_value G_GNUC_UNUSED,
guint n_param_values,
const GValue *param_values,
gpointer invocation_hint G_GNUC_UNUSED,
gpointer marshal_data)
{
GCClosure *cc = (GCClosure *) closure;
gpointer data1, data2;
GMarshalFunc_VOID__INT_INT callback;
g_return_if_fail (n_param_values == 3);
if (G_CCLOSURE_SWAP_DATA (closure))
{
data1 = closure->data;
data2 = g_value_peek_pointer (param_values + 0);
}
else
{
data1 = g_value_peek_pointer (param_values + 0);
data2 = closure->data;
}
callback = (GMarshalFunc_VOID__INT_INT) (marshal_data ? marshal_data : cc->callback);
callback (data1,
(param_values + 1)->data[0].v_int,
(param_values + 2)->data[0].v_int,
data2);
}
static void
piano_keyboard_class_init(PianoKeyboardClass *klass)
{
@ -618,7 +672,7 @@ piano_keyboard_class_init(PianoKeyboardClass *klass)
/* Set up signals. */
piano_keyboard_signals[NOTE_ON_SIGNAL] = g_signal_new ("note-on",
G_TYPE_FROM_CLASS (klass), (GSignalFlags)(G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION),
0, NULL, NULL, g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 1, G_TYPE_INT);
0, NULL, NULL, g_cclosure_user_marshal_VOID__INT_INT, G_TYPE_NONE, 2, G_TYPE_INT, G_TYPE_INT);
piano_keyboard_signals[NOTE_OFF_SIGNAL] = g_signal_new ("note-off",
G_TYPE_FROM_CLASS (klass), (GSignalFlags)(G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION),
@ -687,6 +741,10 @@ piano_keyboard_new(void)
pk->last_key = 0;
pk->monophonic = FALSE;
pk->min_velocity = 1;
pk->max_velocity = 127;
pk->key_velocity = 100;
memset((void *)pk->notes, 0, sizeof(struct PKNote) * NNOTES);
pk->key_bindings = g_hash_table_new(g_str_hash, g_str_equal);
@ -707,6 +765,19 @@ piano_keyboard_set_monophonic(PianoKeyboard *pk, gboolean monophonic)
pk->monophonic = monophonic;
}
void
piano_keyboard_set_velocities(PianoKeyboard *pk, int min_vel, int max_vel, int key_vel)
{
if (min_vel <= max_vel && min_vel > 0 && max_vel < 128) {
pk->min_velocity = min_vel;
pk->max_velocity = max_vel;
}
if (key_vel > 0 && key_vel < 128) {
pk->key_velocity = key_vel;
}
}
void
piano_keyboard_sustain_press(PianoKeyboard *pk)
{

View File

@ -64,6 +64,9 @@ struct _PianoKeyboard
struct PKNote notes[NNOTES];
/* Table used to translate from PC keyboard character to MIDI note number. */
GHashTable* key_bindings;
int min_velocity;
int max_velocity;
int key_velocity;
};
struct _PianoKeyboardClass
@ -83,6 +86,7 @@ void piano_keyboard_set_monophonic (PianoKeyboard *pk, gboolean monophonic);
void piano_keyboard_set_octave (PianoKeyboard *pk, int octave);
gboolean piano_keyboard_set_keyboard_layout (PianoKeyboard *pk, const char *layout);
void piano_keyboard_set_velocities (PianoKeyboard *pk, int min_vel, int max_vel, int key_vel);
G_END_DECLS

View File

@ -24,7 +24,7 @@
#include "pbd/i18n.h"
static void
_note_on_event_handler(GtkWidget* /*widget*/, int note, gpointer arg)
_note_on_event_handler(GtkWidget* /*widget*/, int note, int, gpointer arg)
{
((NoteSelectDialog*)arg)->note_on_event_handler(note);
}

View File

@ -477,7 +477,7 @@ PatchChangeWidget::audition_next ()
}
void
PatchChangeWidget::_note_on_event_handler(GtkWidget*, int note, gpointer arg)
PatchChangeWidget::_note_on_event_handler(GtkWidget*, int note, int, gpointer arg)
{
((PatchChangeWidget*)arg)->note_on_event_handler(note, false);
}

View File

@ -102,7 +102,7 @@ private:
PianoKeyboard* _piano;
Gtk::Widget* _pianomm;
static void _note_on_event_handler (GtkWidget*, int, gpointer);
static void _note_on_event_handler (GtkWidget*, int, int, gpointer);
static void _note_off_event_handler (GtkWidget*, int, gpointer);
void note_on_event_handler (int, bool for_audition);
void note_off_event_handler (int);

View File

@ -324,7 +324,7 @@ private:
Gtk::SpinButton _piano_velocity;
Gtk::SpinButton _piano_channel;
static void _note_on_event_handler (GtkWidget*, int, gpointer);
static void _note_on_event_handler (GtkWidget*, int, int, gpointer);
static void _note_off_event_handler (GtkWidget*, int, gpointer);
void note_on_event_handler (int);
void note_off_event_handler (int);

View File

@ -22,41 +22,95 @@
#include "ardour_ui.h"
#include "virtual_keyboard_window.h"
#include "ui_config.h"
#include "utils.h"
#include "pbd/i18n.h"
using namespace Glib;
using namespace Gtk;
using namespace ArdourWidgets;
#define PX_SCALE(px) std::max((float)px, rintf((float)px * UIConfiguration::instance().get_ui_scale()))
VirtualKeyboardWindow::VirtualKeyboardWindow ()
: ArdourWindow (_("Virtual Keyboard"))
, _piano_velocity (*manage (new Adjustment (100, 1, 127, 1, 16)))
, _piano_channel (*manage (new Adjustment (0, 1, 16, 1, 1)))
, _yaxis_velocity ("Y-Axis Click Velocity", ArdourButton::led_default_elements)
, _piano_key_velocity (*manage (new Adjustment (100, 1, 127, 1, 16)))
, _piano_min_velocity (*manage (new Adjustment (1 , 1, 127, 1, 16)))
, _piano_max_velocity (*manage (new Adjustment (127, 1, 127, 1, 16)))
, _cc7 (new VKBDControl ("CC7", 127))
, _cc7_knob (ArdourKnob::default_elements, ArdourKnob::Flags (0))
{
_piano = (PianoKeyboard*)piano_keyboard_new();
_pianomm = Glib::wrap((GtkWidget*)_piano);
_pianomm->set_flags(Gtk::CAN_FOCUS);
_pianomm->add_events(Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
piano_keyboard_set_keyboard_layout (_piano, "QWERTY");
using namespace Menu_Helpers;
_keyboard_layout.AddMenuElem (MenuElem (_("QWERTY"),
sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::select_keyboard_layout), 0)));
_keyboard_layout.AddMenuElem (MenuElem (_("QWERTZ"),
sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::select_keyboard_layout), 1)));
_keyboard_layout.AddMenuElem (MenuElem (_("AZERTY"),
sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::select_keyboard_layout), 2)));
_keyboard_layout.set_text (_("QWERTY"));
_yaxis_velocity.set_active (false);
_cc7_knob.set_controllable (_cc7);
_cc7_knob.set_size_request (PX_SCALE(21), PX_SCALE(21));
_cc7_knob.set_tooltip_prefix (_("CC7: "));
_cc7_knob.set_name ("monitor section knob");
// TODO allow to hide config panel
// save/restore settings
/* config */
HBox* cfg_box = manage (new HBox);
cfg_box->set_spacing (4);
cfg_box->pack_start (*manage (new Label (_("Key Velocity:"))), false, false);
cfg_box->pack_start (_piano_key_velocity, false, false);
cfg_box->pack_start (_yaxis_velocity, false, false, 8);
cfg_box->pack_start (*manage (new Label (_("Min Velocity:"))), false, false);
cfg_box->pack_start (_piano_min_velocity, false, false);
cfg_box->pack_start (*manage (new Label (_("Max Velocity:"))), false, false);
cfg_box->pack_start (_piano_max_velocity, false, false);
cfg_box->pack_start (_keyboard_layout, false, false, 8);
/* settings */
HBox* set_box = manage (new HBox);
set_box->set_spacing (4);
set_box->pack_start (*manage (new Label (_("Channel:"))), false, false);
set_box->pack_start (_piano_channel, false, false, 8);
set_box->pack_start (*manage (new Label (_("CC7:"))), false, false);
set_box->pack_start (_cc7_knob, false, false);
/* layout */
Box* box1 = manage (new HBox ());
box1->pack_start (*cfg_box, true, false);
Box* box2 = manage (new HBox ());
box2->pack_start (*set_box, true, false);
VBox* vbox = manage (new VBox);
vbox->pack_start (*box1, false, false, 4);
vbox->pack_start (*box2, false, false, 4);
vbox->pack_start (*_pianomm, true, true);
add (*vbox);
_piano_key_velocity.signal_value_changed ().connect (sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::update_velocity_settings), 0));
_piano_min_velocity.signal_value_changed ().connect (sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::update_velocity_settings), 1));
_piano_max_velocity.signal_value_changed ().connect (sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::update_velocity_settings), 2));
_yaxis_velocity.signal_button_release_event().connect (sigc::mem_fun(*this, &VirtualKeyboardWindow::yaxis_velocity_button_release), false);
g_signal_connect (G_OBJECT (_piano), "note-on", G_CALLBACK (VirtualKeyboardWindow::_note_on_event_handler), this);
g_signal_connect (G_OBJECT (_piano), "note-off", G_CALLBACK (VirtualKeyboardWindow::_note_off_event_handler), this);
piano_keyboard_set_keyboard_layout (_piano, "QWERTY");
_cc7->ValueChanged.connect_same_thread (_cc_connections, boost::bind (&VirtualKeyboardWindow::control_change_event_handler, this, 7, _1));
HBox* box = manage (new HBox);
box->pack_start (*manage (new Label (_("Channel:"))), false, false);
box->pack_start (_piano_channel, false, false);
box->pack_start (*manage (new Label (_("Velocity:"))), false, false);
box->pack_start (_piano_velocity, false, false);
Box* box2 = manage (new HBox ());
box2->pack_start (*box, true, false);
VBox* vbox = manage (new VBox);
vbox->pack_start (*box2, false, false, 4);
vbox->pack_start (*_pianomm, true, true);
add (*vbox);
update_velocity_settings (0);
set_keep_above (true);
vbox->show_all();
@ -81,7 +135,72 @@ VirtualKeyboardWindow::on_key_press_event (GdkEventKey* ev)
}
void
VirtualKeyboardWindow::note_on_event_handler (int note)
VirtualKeyboardWindow::select_keyboard_layout (int l)
{
switch (l) {
default:
case 0:
piano_keyboard_set_keyboard_layout (_piano, "QWERTY");
_keyboard_layout.set_text (_("QWERTY"));
break;
case 1:
piano_keyboard_set_keyboard_layout (_piano, "QWERTZ");
_keyboard_layout.set_text (_("QWERTZ"));
break;
case 2:
piano_keyboard_set_keyboard_layout (_piano, "AZERTY");
_keyboard_layout.set_text (_("AZERTY"));
break;
}
}
bool
VirtualKeyboardWindow::yaxis_velocity_button_release (GdkEventButton* ev)
{
_yaxis_velocity.set_active (!_yaxis_velocity.get_active ());
update_velocity_settings (0);
return false;
}
void
VirtualKeyboardWindow::update_velocity_settings (int ctrl)
{
if (_piano_min_velocity.get_value_as_int () > _piano_max_velocity.get_value_as_int ()) {
if (ctrl == 2) {
_piano_min_velocity.set_value (_piano_max_velocity.get_value_as_int ());
return;
} else {
_piano_max_velocity.set_value (_piano_min_velocity.get_value_as_int ());
return;
}
}
if (_yaxis_velocity.get_active ()) {
piano_keyboard_set_velocities (_piano,
_piano_min_velocity.get_value_as_int (),
_piano_max_velocity.get_value_as_int (),
_piano_key_velocity.get_value_as_int ()
);
} else {
piano_keyboard_set_velocities (_piano,
_piano_key_velocity.get_value_as_int (),
_piano_key_velocity.get_value_as_int (),
_piano_key_velocity.get_value_as_int ()
);
}
update_sensitivity ();
}
void
VirtualKeyboardWindow::update_sensitivity ()
{
bool c = _yaxis_velocity.get_active ();
_piano_min_velocity.set_sensitive (c);
_piano_max_velocity.set_sensitive (c);
}
void
VirtualKeyboardWindow::note_on_event_handler (int note, int velocity)
{
_pianomm->grab_focus ();
if (!_session) {
@ -91,7 +210,7 @@ VirtualKeyboardWindow::note_on_event_handler (int note)
uint8_t ev[3];
ev[0] = (MIDI_CMD_NOTE_ON | channel);
ev[1] = note;
ev[2] = _piano_velocity.get_value_as_int ();
ev[2] = velocity;
_session->vkbd_output_port()->write (ev, 3, 0);
}
@ -109,3 +228,14 @@ VirtualKeyboardWindow::note_off_event_handler (int note)
ev[2] = 0;
_session->vkbd_output_port()->write (ev, 3, 0);
}
void
VirtualKeyboardWindow::control_change_event_handler (int key, int val)
{
uint8_t channel = _piano_channel.get_value_as_int () - 1;
uint8_t ev[3];
ev[0] = (MIDI_CMD_CONTROL | channel);
ev[1] = key;
ev[2] = val;
_session->vkbd_output_port()->write (ev, 3, 0);
}

View File

@ -19,6 +19,15 @@
#ifndef _virtual_keyboard_window_h_
#define _virtual_keyboard_window_h_
#include <gtkmm/spinbutton.h>
#include "pbd/signals.h"
#include "pbd/controllable.h"
#include "widgets/ardour_button.h"
#include "widgets/ardour_dropdown.h"
#include "widgets/ardour_knob.h"
#include "ardour_window.h"
#include "gtk_pianokeyboard.h"
@ -26,6 +35,50 @@ namespace ARDOUR {
class Session;
}
class VKBDControl : public PBD::Controllable {
public:
VKBDControl (const std::string& name, double normal = 127)
: PBD::Controllable (name, Flag(0))
, _lower (0)
, _upper (127)
, _normal (normal)
, _value (normal)
{}
/* Controllable API */
void set_value (double v, PBD::Controllable::GroupControlDisposition gcd) {
if (v != _value) {
_value = std::max (_lower, std::min (_upper, v));
Changed (true, gcd); /* EMIT SIGNAL */
ValueChanged ((int)_value); /* EMIT SIGNAL */
}
}
double get_value () const {
return _value;
}
std::string get_user_string () const
{
char buf[32];
sprintf (buf, "%.0f", get_value());
return std::string(buf);
}
double lower () const { return _lower; }
double upper () const { return _upper; }
double normal () const { return _normal; }
PBD::Signal1<void, int> ValueChanged;
protected:
double _lower;
double _upper;
double _normal;
double _value;
};
class VirtualKeyboardWindow : public ArdourWindow
{
public:
@ -37,9 +90,9 @@ protected:
bool on_key_press_event (GdkEventKey*);
private:
static void _note_on_event_handler (GtkWidget*, int note, gpointer arg)
static void _note_on_event_handler (GtkWidget*, int note, int vel, gpointer arg)
{
static_cast<VirtualKeyboardWindow*>(arg)->note_on_event_handler(note);
static_cast<VirtualKeyboardWindow*>(arg)->note_on_event_handler(note, vel);
}
static void _note_off_event_handler (GtkWidget*, int note, gpointer arg)
@ -47,13 +100,30 @@ private:
static_cast<VirtualKeyboardWindow*>(arg)->note_off_event_handler(note);
}
void note_on_event_handler (int);
void note_on_event_handler (int, int);
void note_off_event_handler (int);
void control_change_event_handler (int, int);
void select_keyboard_layout (int);
void update_velocity_settings (int);
void update_sensitivity ();
bool yaxis_velocity_button_release (GdkEventButton* ev);
PianoKeyboard* _piano;
Gtk::Widget* _pianomm;
Gtk::SpinButton _piano_velocity;
Gtk::SpinButton _piano_channel;
ArdourWidgets::ArdourButton _yaxis_velocity;
ArdourWidgets::ArdourDropdown _keyboard_layout;
Gtk::SpinButton _piano_key_velocity;
Gtk::SpinButton _piano_min_velocity;
Gtk::SpinButton _piano_max_velocity;
boost::shared_ptr<VKBDControl> _cc7;
ArdourWidgets::ArdourKnob _cc7_knob;
PBD::ScopedConnectionList _cc_connections;
};
#endif