2 * Copyright (C) 2019 Robin Gareus <robin@gareus.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <gtkmm/box.h>
21 #include "pbd/convert.h"
22 #include "ardour/async_midi_port.h"
23 #include "ardour/session.h"
24 #include "widgets/tooltips.h"
26 #include "ardour_ui.h"
27 #include "virtual_keyboard_window.h"
28 #include "ui_config.h"
35 using namespace ArdourWidgets;
37 #define PX_SCALE(px) std::max((float)px, rintf((float)px * UIConfiguration::instance().get_ui_scale()))
39 VirtualKeyboardWindow::VirtualKeyboardWindow ()
40 : ArdourWindow (_("Virtual Keyboard"))
41 , _piano_channel (*manage (new Adjustment (1, 1, 16, 1, 1)))
42 , _bank_msb (*manage (new Adjustment (0, 0, 127, 1, 16)))
43 , _bank_lsb (*manage (new Adjustment (0, 0, 127, 1, 16)))
44 , _patchpgm (*manage (new Adjustment (1, 1, 128, 1, 16)))
45 , _cfg_display ("Config", ArdourButton::led_default_elements)
46 , _pgm_display ("Bank/Patch", ArdourButton::led_default_elements)
47 , _yaxis_velocity ("Y-Axis Click Velocity", ArdourButton::led_default_elements)
48 , _send_panic ("Panic", ArdourButton::default_elements)
49 , _piano_key_velocity (*manage (new Adjustment (100, 1, 127, 1, 16)))
50 , _piano_min_velocity (*manage (new Adjustment (1 , 1, 127, 1, 16)))
51 , _piano_max_velocity (*manage (new Adjustment (127, 1, 127, 1, 16)))
52 , _pitch_adjustment (8192, 0, 16383, 1, 256)
54 _piano = (PianoKeyboard*)piano_keyboard_new();
55 _pianomm = Glib::wrap((GtkWidget*)_piano);
56 _pianomm->set_flags(Gtk::CAN_FOCUS);
57 _pianomm->add_events(Gdk::KEY_PRESS_MASK|Gdk::KEY_RELEASE_MASK);
58 piano_keyboard_set_keyboard_layout (_piano, "QWERTY");
60 using namespace Menu_Helpers;
61 _keyboard_layout.AddMenuElem (MenuElem ("QWERTY",
62 sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::select_keyboard_layout), 0)));
63 _keyboard_layout.AddMenuElem (MenuElem ("QWERTZ",
64 sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::select_keyboard_layout), 1)));
65 _keyboard_layout.AddMenuElem (MenuElem ("AZERTY",
66 sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::select_keyboard_layout), 2)));
67 _keyboard_layout.set_active (_("QWERTY"));
69 _cfg_display.set_active (false);
70 _pgm_display.set_active (false);
71 _yaxis_velocity.set_active (false);
73 _pitchbend = boost::shared_ptr<VKBDControl> (new VKBDControl ("PB", 8192, 16383));
74 _pitch_slider = manage (new VSliderController(&_pitch_adjustment, _pitchbend, 0, PX_SCALE (15)));
75 _pitch_slider_tooltip = new Gtkmm2ext::PersistentTooltip (_pitch_slider);
76 _pitch_adjustment.signal_value_changed().connect (
77 sigc::mem_fun (*this, &VirtualKeyboardWindow::pitch_slider_adjusted));
78 _pitchbend->ValueChanged.connect_same_thread (_cc_connections,
79 boost::bind (&VirtualKeyboardWindow::pitch_bend_event_handler, this, _1));
81 set_tooltip (_send_panic, "Send MIDI Panic message for current channel");
82 _pitch_slider_tooltip->set_tip ("Pitchbend: 8192");
85 HBox* cfg_box = manage (new HBox);
86 cfg_box->set_spacing (4);
87 cfg_box->pack_start (*manage (new Label (_("Key Velocity:"))), false, false);
88 cfg_box->pack_start (_piano_key_velocity, false, false);
89 cfg_box->pack_start (_yaxis_velocity, false, false, 8);
90 cfg_box->pack_start (*manage (new Label (_("Min Velocity:"))), false, false);
91 cfg_box->pack_start (_piano_min_velocity, false, false);
92 cfg_box->pack_start (*manage (new Label (_("Max Velocity:"))), false, false);
93 cfg_box->pack_start (_piano_max_velocity, false, false);
94 cfg_box->pack_start (_keyboard_layout, false, false, 8);
98 Table* pgm_tbl = manage (new Table);
100 Label* lbl = manage (new Label (_("Note: Prefer\nTrack-controls")));
101 lbl->set_justify (JUSTIFY_CENTER);
103 pgm_tbl->attach (*lbl, 0, 1, 0, 2, SHRINK, SHRINK, 4, 0);
104 pgm_tbl->attach (*manage (new ArdourVSpacer), 1, 2, 0, 2, SHRINK, FILL, 4, 0);
105 pgm_tbl->attach (_bank_msb, 2, 3, 0, 1, SHRINK, SHRINK, 4, 0);
106 pgm_tbl->attach (_bank_lsb, 3, 4, 0, 1, SHRINK, SHRINK, 4, 0);
107 pgm_tbl->attach (_patchpgm, 4, 5, 0, 1, SHRINK, SHRINK, 4, 0);
108 pgm_tbl->attach (*manage (new Label (_("MSB"))), 2, 3, 1, 2, SHRINK, SHRINK, 4, 0);
109 pgm_tbl->attach (*manage (new Label (_("LSB"))), 3, 4, 1, 2, SHRINK, SHRINK, 4, 0);
110 pgm_tbl->attach (*manage (new Label (_("PGM"))), 4, 5, 1, 2, SHRINK, SHRINK, 4, 0);
111 pgm_tbl->attach (*manage (new ArdourVSpacer), 5, 6, 0, 2, SHRINK, FILL, 4, 0);
112 pgm_tbl->attach (_send_panic, 6, 7, 0, 2, SHRINK, SHRINK, 4, 0);
113 pgm_tbl->show_all ();
116 Table* tbl = manage (new Table);
117 tbl->attach (_piano_channel, 0, 1, 0, 1, SHRINK, SHRINK, 4, 0);
118 tbl->attach (*manage (new Label (_("Channel"))), 0, 1, 1, 2, SHRINK, SHRINK, 4, 0);
119 tbl->attach (*manage (new ArdourVSpacer), 1, 2, 0, 2, SHRINK, FILL, 4, 0);
120 tbl->attach (*_pitch_slider, 2, 3, 0, 2, SHRINK, FILL, 4, 0);
122 const char* default_cc[VKBD_NCTRLS] = { "7", "8", "1", "11", "91", "92", "93", "94" };
124 for (int i = 0; i < VKBD_NCTRLS; ++i) {
125 _cc[i] = boost::shared_ptr<VKBDControl> (new VKBDControl ("CC"));
126 _cc_knob[i] = manage(new ArdourKnob (ArdourKnob::default_elements, ArdourKnob::Flags (0)));
127 _cc_knob[i]->set_controllable (_cc[i]);
128 _cc_knob[i]->set_size_request (PX_SCALE(21), PX_SCALE(21));
129 _cc_knob[i]->set_tooltip_prefix (_("CC: "));
130 _cc_knob[i]->set_name ("monitor section knob");
132 for (int c = 1; c < 120; ++c) {
137 sprintf (key, "%d", c);
138 _cc_key[i].append_text_item (key);
140 _cc_key[i].set_active (default_cc[i]);
142 tbl->attach (*_cc_knob[i], i+3, i+4, 0, 1, SHRINK, SHRINK, 4, 2);
143 tbl->attach (_cc_key[i] , i+3, i+4, 1, 2, SHRINK, SHRINK, 4, 2);
145 _cc[i]->ValueChanged.connect_same_thread (_cc_connections,
146 boost::bind (&VirtualKeyboardWindow::control_change_event_handler, this, i, _1));
150 Box* box1 = manage (new HBox ());
151 box1->pack_start (*tbl, true, false);
153 Box* box2 = manage (new VBox ());
154 box2->pack_start (_pgm_display, false, false);
155 box2->pack_start (_cfg_display, false, false);
156 box1->pack_start (*box2, false, false);
158 _cfg_box = manage (new HBox ());
159 _cfg_box->pack_start (*cfg_box, true, false);
160 _cfg_box->set_no_show_all (true);
162 _pgm_box = manage (new HBox ());
163 _pgm_box->pack_start (*pgm_tbl, true, false);
164 _pgm_box->set_no_show_all (true);
166 VBox* vbox = manage (new VBox);
167 vbox->pack_start (*box1, false, false, 4);
168 vbox->pack_start (*_pgm_box, false, false, 4);
169 vbox->pack_start (*_cfg_box, false, false, 4);
170 vbox->pack_start (*_pianomm, true, true);
173 _bank_msb.signal_value_changed ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::bank_patch));
174 _bank_lsb.signal_value_changed ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::bank_patch));
175 _patchpgm.signal_value_changed ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::bank_patch));
177 _piano_key_velocity.signal_value_changed ().connect (sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::update_velocity_settings), 0));
178 _piano_min_velocity.signal_value_changed ().connect (sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::update_velocity_settings), 1));
179 _piano_max_velocity.signal_value_changed ().connect (sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::update_velocity_settings), 2));
181 _cfg_display.signal_button_release_event().connect (sigc::mem_fun(*this, &VirtualKeyboardWindow::toggle_config), false);
182 _pgm_display.signal_button_release_event().connect (sigc::mem_fun(*this, &VirtualKeyboardWindow::toggle_bankpatch), false);
183 _yaxis_velocity.signal_button_release_event().connect (sigc::mem_fun(*this, &VirtualKeyboardWindow::toggle_yaxis_velocity), false);
184 _send_panic.signal_button_release_event().connect (sigc::mem_fun(*this, &VirtualKeyboardWindow::send_panic_message), false);
186 g_signal_connect (G_OBJECT (_piano), "note-on", G_CALLBACK (VirtualKeyboardWindow::_note_on_event_handler), this);
187 g_signal_connect (G_OBJECT (_piano), "note-off", G_CALLBACK (VirtualKeyboardWindow::_note_off_event_handler), this);
189 update_velocity_settings (0);
191 set_keep_above (true);
195 VirtualKeyboardWindow::~VirtualKeyboardWindow ()
198 delete _pitch_slider_tooltip;
202 VirtualKeyboardWindow::set_session (ARDOUR::Session* s)
204 ArdourWindow::set_session (s);
210 XMLNode* node = _session->instant_xml(X_("VirtualKeyboard"));
217 VirtualKeyboardWindow::get_state ()
219 XMLNode* node = new XMLNode (X_("VirtualKeyboard"));
220 node->set_property (X_("YAxisVelocity"), _yaxis_velocity.get_active());
221 node->set_property (X_("Layout"), _keyboard_layout.get_text ());
222 node->set_property (X_("Channel"), _piano_channel.get_value_as_int ());
223 node->set_property (X_("MinVelocity"), _piano_min_velocity.get_value_as_int ());
224 node->set_property (X_("MaxVelocity"), _piano_max_velocity.get_value_as_int ());
225 node->set_property (X_("KeyVelocity"), _piano_key_velocity.get_value_as_int ());
226 for (int i = 0; i < VKBD_NCTRLS; ++i) {
228 sprintf (buf, "CC-%d", i);
229 node->set_property (buf, _cc_key[i].get_text());
235 VirtualKeyboardWindow::set_state (const XMLNode &root)
237 if (root.name() != "VirtualKeyboard") {
241 XMLNode const* node = &root;
244 if (node->get_property(X_("Layout"), layout)) {
245 piano_keyboard_set_keyboard_layout (_piano, layout.c_str());
246 _keyboard_layout.set_active (layout);
249 for (int i = 0; i < VKBD_NCTRLS; ++i) {
251 sprintf (buf, "CC-%d", i);
253 if (node->get_property(buf, cckey)) {
254 _cc_key[i].set_active (cckey);
259 if (node->get_property(X_("YAxisVelocity"), a)) {
260 _yaxis_velocity.set_active (a);
264 if (node->get_property(X_("Channel"), v)) {
265 _piano_channel.set_value (v);
267 if (node->get_property(X_("MinVelocity"), v)) {
268 _piano_min_velocity.set_value (v);
270 if (node->get_property(X_("MaxVelocity"), v)) {
271 _piano_max_velocity.set_value (v);
273 if (node->get_property(X_("KeyVelocity"), v)) {
274 _piano_key_velocity.set_value (v);
277 update_velocity_settings (0);
281 VirtualKeyboardWindow::on_unmap ()
283 ArdourWindow::on_unmap ();
284 ARDOUR_UI::instance()->reset_focus (this);
288 VirtualKeyboardWindow::on_key_press_event (GdkEventKey* ev)
290 return ARDOUR_UI_UTILS::relay_key_press (ev, this);
294 VirtualKeyboardWindow::select_keyboard_layout (int l)
299 piano_keyboard_set_keyboard_layout (_piano, "QWERTY");
302 piano_keyboard_set_keyboard_layout (_piano, "QWERTZ");
305 piano_keyboard_set_keyboard_layout (_piano, "AZERTY");
311 VirtualKeyboardWindow::toggle_config (GdkEventButton* ev)
313 bool a = ! _cfg_display.get_active ();
314 _cfg_display.set_active (a);
324 VirtualKeyboardWindow::toggle_bankpatch (GdkEventButton*)
326 bool a = ! _pgm_display.get_active ();
327 _pgm_display.set_active (a);
337 VirtualKeyboardWindow::toggle_yaxis_velocity (GdkEventButton*)
339 _yaxis_velocity.set_active (!_yaxis_velocity.get_active ());
340 update_velocity_settings (0);
345 VirtualKeyboardWindow::send_panic_message (GdkEventButton*)
347 uint8_t channel = _piano_channel.get_value_as_int () - 1;
349 ev[0] = MIDI_CMD_CONTROL | channel;
350 ev[1] = MIDI_CTL_SUSTAIN;
352 _session->vkbd_output_port()->write (ev, 3, 0);
353 ev[1] = MIDI_CTL_ALL_NOTES_OFF;
354 _session->vkbd_output_port()->write (ev, 3, 0);
355 ev[1] = MIDI_CTL_RESET_CONTROLLERS;
356 _session->vkbd_output_port()->write (ev, 3, 0);
361 VirtualKeyboardWindow::bank_patch ()
363 int msb = _bank_msb.get_value_as_int ();
364 int lsb = _bank_lsb.get_value_as_int ();
365 int pgm = _patchpgm.get_value_as_int () - 1;
367 uint8_t channel = _piano_channel.get_value_as_int () - 1;
369 ev[0] = MIDI_CMD_CONTROL | channel;
370 ev[1] = MIDI_CTL_MSB_BANK;
371 ev[2] = (msb >> 7) & 0x7f;
372 _session->vkbd_output_port()->write (ev, 3, 0);
373 ev[1] = MIDI_CTL_LSB_BANK | channel;
375 _session->vkbd_output_port()->write (ev, 3, 0);
376 ev[0] = MIDI_CMD_PGM_CHANGE | channel;
378 _session->vkbd_output_port()->write (ev, 2, 0);
382 VirtualKeyboardWindow::update_velocity_settings (int ctrl)
384 if (_piano_min_velocity.get_value_as_int () > _piano_max_velocity.get_value_as_int ()) {
386 _piano_min_velocity.set_value (_piano_max_velocity.get_value_as_int ());
389 _piano_max_velocity.set_value (_piano_min_velocity.get_value_as_int ());
394 if (_yaxis_velocity.get_active ()) {
395 piano_keyboard_set_velocities (_piano,
396 _piano_min_velocity.get_value_as_int (),
397 _piano_max_velocity.get_value_as_int (),
398 _piano_key_velocity.get_value_as_int ()
401 piano_keyboard_set_velocities (_piano,
402 _piano_key_velocity.get_value_as_int (),
403 _piano_key_velocity.get_value_as_int (),
404 _piano_key_velocity.get_value_as_int ()
407 update_sensitivity ();
411 VirtualKeyboardWindow::update_sensitivity ()
413 bool c = _yaxis_velocity.get_active ();
414 _piano_min_velocity.set_sensitive (c);
415 _piano_max_velocity.set_sensitive (c);
419 VirtualKeyboardWindow::pitch_slider_adjusted ()
421 _pitchbend->set_value (_pitch_adjustment.get_value (), PBD::Controllable::NoGroup);
423 snprintf(buf, sizeof(buf), "Pitchbend: %.0f", _pitch_adjustment.get_value());
424 _pitch_slider_tooltip->set_tip (buf);
429 VirtualKeyboardWindow::note_on_event_handler (int note, int velocity)
431 _pianomm->grab_focus ();
435 uint8_t channel = _piano_channel.get_value_as_int () - 1;
437 ev[0] = MIDI_CMD_NOTE_ON | channel;
440 _session->vkbd_output_port()->write (ev, 3, 0);
444 VirtualKeyboardWindow::note_off_event_handler (int note)
450 uint8_t channel = _piano_channel.get_value_as_int () - 1;
452 ev[0] = MIDI_CMD_NOTE_OFF | channel;
455 _session->vkbd_output_port()->write (ev, 3, 0);
459 VirtualKeyboardWindow::control_change_event_handler (int key, int val)
464 assert (key >= 0 && key < VKBD_NCTRLS);
465 int ctrl = PBD::atoi (_cc_key[key].get_text());
466 assert (ctrl > 0 && ctrl < 127);
467 uint8_t channel = _piano_channel.get_value_as_int () - 1;
469 ev[0] = MIDI_CMD_CONTROL | channel;
472 _session->vkbd_output_port()->write (ev, 3, 0);
476 VirtualKeyboardWindow::pitch_bend_event_handler (int val)
481 uint8_t channel = _piano_channel.get_value_as_int () - 1;
483 ev[0] = MIDI_CMD_BENDER | channel;
485 ev[2] = (val >> 7) & 0x7f;
486 _session->vkbd_output_port()->write (ev, 3, 0);