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>
22 #include "pbd/convert.h"
23 #include "pbd/compose.h"
25 #include "ardour/async_midi_port.h"
26 #include "ardour/session.h"
28 #include "gtkmm2ext/utils.h"
29 #include "widgets/tooltips.h"
31 #include "ardour_ui.h"
32 #include "ui_config.h"
34 #include "virtual_keyboard_window.h"
40 using namespace ArdourWidgets;
42 #define PX_SCALE(px) std::max ((float)px, rintf ((float)px* UIConfiguration::instance ().get_ui_scale ()))
44 VirtualKeyboardWindow::VirtualKeyboardWindow ()
45 : ArdourWindow (_("Virtual MIDI Keyboard"))
46 , _send_panic (_("Panic"), ArdourButton::default_elements)
47 , _pitch_adjustment (8192, 0, 16383, 1, 256)
48 , _modwheel_adjustment (0, 0, 127, 1, 8)
50 UIConfiguration::instance().ParameterChanged.connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::parameter_changed));
52 _piano.set_flags (Gtk::CAN_FOCUS);
54 _piano.set_keyboard_layout (APianoKeyboard::S_QWERTY);
55 _piano.set_annotate_octave (true);
56 _piano.set_grand_piano_highlight (false);
57 _piano.set_annotate_layout (true);
58 _piano.set_annotate_octave (true);
60 for (int c = 0; c < 16; ++c) {
62 sprintf (buf, "%d", c + 1);
63 _midi_channel.append_text_item (buf);
67 for (int v = 0; v <= 128; v += 16) {
69 sprintf (buf, "%d", std::min (127, std::max (1, v)));
70 _piano_velocity.append_text_item (buf);
73 _piano_velocity.append_text_item ("8");
74 _piano_velocity.append_text_item ("32");
75 _piano_velocity.append_text_item ("64");
76 _piano_velocity.append_text_item ("82");
77 _piano_velocity.append_text_item ("100");
78 _piano_velocity.append_text_item ("127");
81 for (int k = -1; k < 8; ++k) {
83 sprintf (buf, "%d", k);
84 _piano_octave_key.append_text_item (buf);
86 for (int r = 2; r < 12; ++r) {
88 sprintf (buf, "%d", r);
89 _piano_octave_range.append_text_item (buf);
91 for (int t = -12; t < 13; ++t) {
93 sprintf (buf, "%d", t);
94 _transpose_output.append_text_item (buf);
97 _midi_channel.set_active ("1");
98 _piano_velocity.set_active ("100");
99 _piano_octave_key.set_active ("4");
100 _piano_octave_range.set_active ("7");
101 _transpose_output.set_active ("0");
103 _pitchbend = boost::shared_ptr<VKBDControl> (new VKBDControl ("PB", 8192, 16383));
104 _pitch_slider = manage (new VSliderController (&_pitch_adjustment, _pitchbend, 0, PX_SCALE (15)));
105 _pitch_slider_tooltip = new Gtkmm2ext::PersistentTooltip (_pitch_slider);
107 _modwheel = boost::shared_ptr<VKBDControl> (new VKBDControl ("MW", 0, 127));
108 _modwheel_slider = manage (new VSliderController (&_modwheel_adjustment, _modwheel, 0, PX_SCALE (15)));
109 _modwheel_tooltip = new Gtkmm2ext::PersistentTooltip (_modwheel_slider);
112 set_tooltip (_midi_channel, _("Set the MIDI Channel of the produced MIDI events"));
113 set_tooltip (_piano_octave_key, _("The center octave, and lowest octave for keyboard control. Change with Arrow left/right."));
114 set_tooltip (_piano_octave_range, _("Available octave range, centered around the key-octave."));
115 set_tooltip (_piano_velocity, _("The velocity to use with keyboard control. Use mouse-scroll for fine-grained control"));
116 set_tooltip (_transpose_output, _("Chromatic transpose note events. Notes transposed outside the range of 0,,127 are discarded."));
118 set_tooltip (_send_panic, _("Send MIDI Panic message for current channel"));
120 modwheel_update_tooltip (0);
121 pitch_bend_update_tooltip (8192);
123 /* prevent focus grab, let MIDI keyboard to handle key events */
124 _send_panic.set_can_focus (false);
125 _modwheel_slider->set_can_focus (false);
126 _pitch_slider->set_can_focus (false);
129 Table* tbl = manage (new Table);
130 tbl->attach (_midi_channel, 0, 1, 0, 1, SHRINK, SHRINK, 4, 0);
131 tbl->attach (*manage (new Label (_("Channel"))), 0, 1, 1, 2, SHRINK, SHRINK, 4, 0);
132 tbl->attach (*manage (new ArdourVSpacer), 1, 2, 0, 2, SHRINK, FILL, 4, 0);
133 tbl->attach (*_pitch_slider, 2, 3, 0, 2, SHRINK, FILL, 4, 0);
134 tbl->attach (*_modwheel_slider, 3, 4, 0, 2, SHRINK, FILL, 4, 0);
136 const int default_cc[VKBD_NCTRLS] = { 7, 8, 91, 93};
139 for (size_t i = 0; i < VKBD_NCTRLS; ++i, ++col) {
140 _cc[i] = boost::shared_ptr<VKBDControl> (new VKBDControl ("CC"));
141 _cc_knob[i] = manage (new ArdourKnob (ArdourKnob::default_elements, ArdourKnob::Flags (0)));
142 _cc_knob[i]->set_controllable (_cc[i]);
143 _cc_knob[i]->set_size_request (PX_SCALE (21), PX_SCALE (21));
144 _cc_knob[i]->set_name ("monitor section knob");
146 for (int c = 2; c < 120; ++c) {
151 sprintf (key, "%d", c);
152 _cc_key[i].append_text_item (key);
154 update_cc (i, default_cc[i]);
156 tbl->attach (*_cc_knob[i], col, col + 1, 0, 1, SHRINK, SHRINK, 4, 2);
157 tbl->attach (_cc_key[i], col, col + 1, 1, 2, SHRINK, SHRINK, 4, 2);
159 _cc_key[i].StateChanged.connect (sigc::bind (sigc::mem_fun (*this, &VirtualKeyboardWindow::cc_key_changed), i));
160 _cc[i]->ValueChanged.connect_same_thread (_cc_connections,
161 boost::bind (&VirtualKeyboardWindow::control_change_knob_event_handler, this, i, _1));
164 tbl->attach (*manage (new ArdourVSpacer), col, col + 1, 0, 2, SHRINK, FILL, 4, 0);
166 tbl->attach (_piano_octave_key, col, col + 1, 0, 1, SHRINK, SHRINK, 4, 0);
167 tbl->attach (*manage (new Label (_("Octave"))), col, col + 1, 1, 2, SHRINK, SHRINK, 4, 0);
169 tbl->attach (_piano_octave_range, col, col + 1, 0, 1, SHRINK, SHRINK, 4, 0);
170 tbl->attach (*manage (new Label (_("Range"))), col, col + 1, 1, 2, SHRINK, SHRINK, 4, 0);
173 tbl->attach (*manage (new ArdourVSpacer), col, col + 1, 0, 2, SHRINK, FILL, 4, 0);
175 tbl->attach (_piano_velocity, col, col + 1, 0, 1, SHRINK, SHRINK, 4, 0);
176 tbl->attach (*manage (new Label (_("Velocity"))), col, col + 1, 1, 2, SHRINK, SHRINK, 4, 0);
179 tbl->attach (*manage (new ArdourVSpacer), col, col + 1, 0, 2, SHRINK, FILL, 4, 0);
181 tbl->attach (_transpose_output, col, col + 1, 0, 1, SHRINK, SHRINK, 4, 0);
182 tbl->attach (*manage (new Label (_("Transpose"))), col, col + 1, 1, 2, SHRINK, SHRINK, 4, 0);
184 tbl->attach (_send_panic, col, col + 1, 0, 2, SHRINK, SHRINK, 4, 0);
187 Box* box1 = manage (new HBox ());
188 box1->pack_start (*tbl, true, false);
190 VBox* vbox = manage (new VBox);
191 vbox->pack_start (*box1, false, false, 4);
192 vbox->pack_start (_piano, true, true);
195 set_size_request_to_display_given_text (_piano_octave_key, "88", 19, 2);
196 set_size_request_to_display_given_text (_piano_octave_range, "88", 19, 2);
197 set_size_request_to_display_given_text (_piano_velocity, "888", 19, 2);
201 _pitch_adjustment.signal_value_changed ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::pitch_slider_adjusted));
202 _pitchbend->ValueChanged.connect_same_thread (_cc_connections, boost::bind (&VirtualKeyboardWindow::pitch_bend_event_handler, this, _1));
203 _pitch_slider->StopGesture.connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::pitch_bend_release));
205 _modwheel_adjustment.signal_value_changed ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::modwheel_slider_adjusted));
206 _modwheel->ValueChanged.connect_same_thread (_cc_connections, boost::bind (&VirtualKeyboardWindow::control_change_event_handler, this, 1, _1));
208 _piano_velocity.StateChanged.connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::update_velocity_settings));
209 _piano_octave_key.StateChanged.connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::update_octave_key));
210 _piano_octave_range.StateChanged.connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::update_octave_range));
212 _send_panic.signal_button_release_event ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::send_panic_message), false);
214 _piano_velocity.disable_scrolling ();
215 _piano_velocity.signal_scroll_event().connect (sigc::mem_fun(*this, &VirtualKeyboardWindow::on_velocity_scroll_event), false);
217 /* piano keyboard signals */
219 _piano.NoteOn.connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::note_on_event_handler));
220 _piano.NoteOff.connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::note_off_event_handler));
221 _piano.SwitchOctave.connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::octave_key_event_handler));
222 _piano.PitchBend.connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::pitch_bend_key_event_handler));
226 update_velocity_settings ();
227 update_octave_range ();
229 set_keep_above (true);
233 VirtualKeyboardWindow::~VirtualKeyboardWindow ()
235 delete _pitch_slider_tooltip;
239 VirtualKeyboardWindow::set_session (ARDOUR::Session* s)
241 ArdourWindow::set_session (s);
247 XMLNode* node = _session->instant_xml (X_("VirtualKeyboard"));
253 VirtualKeyboardWindow::parameter_changed (std::string const& p)
255 if (p == "vkeybd-layout") {
256 select_keyboard_layout (UIConfiguration::instance().get_vkeybd_layout ());
261 VirtualKeyboardWindow::get_state ()
263 XMLNode* node = new XMLNode (X_("VirtualKeyboard"));
264 node->set_property (X_("Channel"), _midi_channel.get_text ());
265 node->set_property (X_("Transpose"), _transpose_output.get_text ());
266 node->set_property (X_("KeyVelocity"), _piano_velocity.get_text ());
267 node->set_property (X_("Octave"), _piano_octave_key.get_text ());
268 node->set_property (X_("Range"), _piano_octave_range.get_text ());
269 for (int i = 0; i < VKBD_NCTRLS; ++i) {
271 sprintf (buf, "CC-%d", i);
272 node->set_property (buf, _cc_key[i].get_text ());
278 VirtualKeyboardWindow::set_state (const XMLNode& root)
280 if (root.name () != "VirtualKeyboard") {
284 XMLNode const* node = &root;
286 for (int i = 0; i < VKBD_NCTRLS; ++i) {
288 sprintf (buf, "CC-%d", i);
290 if (node->get_property (buf, cckey)) {
291 update_cc (i, PBD::atoi (cckey));
296 if (node->get_property (X_("Channel"), s)) {
297 uint8_t channel = PBD::atoi (_midi_channel.get_text ());
298 if (channel > 0 && channel < 17) {
299 _midi_channel.set_active (s);
302 if (node->get_property (X_("Transpose"), s)) {
303 _transpose_output.set_active (s);
305 if (node->get_property (X_("KeyVelocity"), s)) {
306 _piano_velocity.set_active (s);
308 if (node->get_property (X_("Octave"), s)) {
309 _piano_octave_key.set_active (s);
311 if (node->get_property (X_("Range"), s)) {
312 _piano_octave_range.set_active (s);
315 update_velocity_settings ();
316 update_octave_range ();
317 update_octave_key ();
321 VirtualKeyboardWindow::on_focus_in_event (GdkEventFocus* ev)
323 _piano.grab_focus ();
324 return ArdourWindow::on_focus_in_event (ev);
328 VirtualKeyboardWindow::on_unmap ()
330 ArdourWindow::on_unmap ();
331 ARDOUR_UI::instance ()->reset_focus (this);
335 VirtualKeyboardWindow::on_key_press_event (GdkEventKey* ev)
337 /* try propagate unmodified events first */
338 if ((ev->state & 0xf) == 0) {
339 if (gtk_window_propagate_key_event (gobj(), ev)) {
344 _piano.grab_focus ();
346 return ARDOUR_UI_UTILS::relay_key_press (ev, this);
350 VirtualKeyboardWindow::on_key_release_event (GdkEventKey* ev)
352 /* try propagate unmodified events first */
353 if ((ev->state & 0xf) == 0) {
354 if (gtk_window_propagate_key_event (gobj(), ev)) {
359 _piano.grab_focus ();
361 return ArdourWindow::on_key_release_event (ev);
365 VirtualKeyboardWindow::select_keyboard_layout (std::string const& l)
368 _piano.set_keyboard_layout (APianoKeyboard::QWERTY);
369 } else if (l == "QWERTZ") {
370 _piano.set_keyboard_layout (APianoKeyboard::QWERTZ);
371 } else if (l == "AZERTY") {
372 _piano.set_keyboard_layout (APianoKeyboard::AZERTY);
373 } else if (l == "DVORAK") {
374 _piano.set_keyboard_layout (APianoKeyboard::DVORAK);
375 } else if (l == "QWERTY Single") {
376 _piano.set_keyboard_layout (APianoKeyboard::S_QWERTY);
377 } else if (l == "QWERTZ Single") {
378 _piano.set_keyboard_layout (APianoKeyboard::S_QWERTZ);
380 _piano.grab_focus ();
384 VirtualKeyboardWindow::update_octave_key ()
386 _piano.set_octave (PBD::atoi (_piano_octave_key.get_text ()));
387 _piano.grab_focus ();
391 VirtualKeyboardWindow::update_octave_range ()
393 _piano.set_octave_range (PBD::atoi (_piano_octave_range.get_text ()));
394 _piano.set_grand_piano_highlight (PBD::atoi (_piano_octave_range.get_text ()) > 3);
395 _piano.grab_focus ();
399 VirtualKeyboardWindow::send_panic_message (GdkEventButton*)
402 uint8_t channel = PBD::atoi (_midi_channel.get_text ()) - 1;
404 ev[0] = MIDI_CMD_CONTROL | channel;
405 ev[1] = MIDI_CTL_SUSTAIN;
407 _session->vkbd_output_port ()->write (ev, 3, 0);
408 ev[1] = MIDI_CTL_ALL_NOTES_OFF;
409 _session->vkbd_output_port ()->write (ev, 3, 0);
410 ev[1] = MIDI_CTL_RESET_CONTROLLERS;
411 _session->vkbd_output_port ()->write (ev, 3, 0);
416 VirtualKeyboardWindow::on_velocity_scroll_event (GdkEventScroll* ev)
418 int v = PBD::atoi (_piano_velocity.get_text ());
419 switch (ev->direction) {
420 case GDK_SCROLL_DOWN:
421 v = std::min (127, v + 1);
424 v = std::max (1, v - 1);
430 sprintf (buf, "%d", v);
431 _piano_velocity.set_active (buf);
436 VirtualKeyboardWindow::update_velocity_settings ()
438 int v = PBD::atoi (_piano_velocity.get_text ());
439 _piano.set_velocities (v, v, v);
443 VirtualKeyboardWindow::cc_key_changed (size_t i)
445 int ctrl = PBD::atoi (_cc_key[i].get_text ());
450 VirtualKeyboardWindow::update_cc (size_t i, int cc)
452 assert (i < VKBD_NCTRLS);
453 if (cc < 0 || cc > 120) {
457 sprintf (buf, "%d", cc);
458 _cc_key[i].set_active (buf);
459 _cc_knob[i]->set_tooltip_prefix (string_compose (_("CC-%1: "), cc));
460 // TODO update _cc[i]->normal
464 VirtualKeyboardWindow::octave_key_event_handler (bool up)
466 int k = PBD::atoi (_piano_octave_key.get_text ()) + (up ? 1 : -1);
467 k = std::min (7, std::max (-1, k));
469 sprintf (buf, "%d", k);
470 _piano_octave_key.set_active (buf);
474 VirtualKeyboardWindow::pitch_bend_key_event_handler (int target, bool interpolate)
476 int cur = _pitch_adjustment.get_value();
481 _pitch_bend_target = target;
482 if (!_bender_connection.connected ()) {
483 float tc = _pitch_bend_target == 8192 ? .35 : .51;
484 cur = rintf (cur + tc * (_pitch_bend_target - cur));
485 _pitch_adjustment.set_value (cur);
486 _bender_connection = Glib::signal_timeout().connect (sigc::mem_fun(*this, &VirtualKeyboardWindow::pitch_bend_timeout), 20 /*ms*/);
490 _bender_connection.disconnect ();
491 _pitch_adjustment.set_value (target);
492 _pitch_bend_target = target;
496 VirtualKeyboardWindow::pitch_bend_timeout ()
498 int cur = _pitch_adjustment.get_value();
500 /* a spring would be 2nd order with overshoot,
501 * but we assume it's critically damped */
502 float tc = _pitch_bend_target == 8192 ? .35 : .51;
503 cur = rintf (cur + tc * (_pitch_bend_target - cur));
504 if (abs (cur - _pitch_bend_target) < 2) {
505 cur = _pitch_bend_target;
507 _pitch_adjustment.set_value (cur);
508 return _pitch_bend_target != cur;
512 VirtualKeyboardWindow::pitch_slider_adjusted ()
514 _pitchbend->set_value (_pitch_adjustment.get_value (), PBD::Controllable::NoGroup);
515 pitch_bend_update_tooltip (_pitch_adjustment.get_value ());
519 VirtualKeyboardWindow::pitch_bend_update_tooltip (int value)
521 _pitch_slider_tooltip->set_tip (string_compose (
523 "Use mouse-drag for sprung mode,\n"
524 "mouse-wheel for presisent bends.\n"
525 "F1-F4 and arrow-up/down keys jump\n"
526 "to select values."), value));
530 VirtualKeyboardWindow::modwheel_slider_adjusted ()
532 _modwheel->set_value (_modwheel_adjustment.get_value (), PBD::Controllable::NoGroup);
533 modwheel_update_tooltip (_modwheel_adjustment.get_value ());
537 VirtualKeyboardWindow::modwheel_update_tooltip (int value)
539 _modwheel_tooltip->set_tip (string_compose (_("Modulation: %1"), value));
543 VirtualKeyboardWindow::note_on_event_handler (int note, int velocity)
545 _piano.grab_focus ();
549 note += PBD::atoi (_transpose_output.get_text ());
550 if (note < 0 || note > 127) {
553 uint8_t channel = PBD::atoi (_midi_channel.get_text ()) - 1;
555 ev[0] = MIDI_CMD_NOTE_ON | channel;
558 _session->vkbd_output_port ()->write (ev, 3, 0);
562 VirtualKeyboardWindow::note_off_event_handler (int note)
567 note += PBD::atoi (_transpose_output.get_text ());
568 if (note < 0 || note > 127) {
571 uint8_t channel = PBD::atoi (_midi_channel.get_text ()) - 1;
573 ev[0] = MIDI_CMD_NOTE_OFF | channel;
576 _session->vkbd_output_port ()->write (ev, 3, 0);
580 VirtualKeyboardWindow::control_change_knob_event_handler (int key, int val)
582 assert (key >= 0 && key < VKBD_NCTRLS);
583 int ctrl = PBD::atoi (_cc_key[key].get_text ());
584 assert (ctrl > 0 && ctrl < 127);
585 control_change_event_handler (ctrl, val);
589 VirtualKeyboardWindow::control_change_event_handler (int ctrl, int val)
594 uint8_t channel = PBD::atoi (_midi_channel.get_text ()) - 1;
596 ev[0] = MIDI_CMD_CONTROL | channel;
599 _session->vkbd_output_port ()->write (ev, 3, 0);
603 VirtualKeyboardWindow::pitch_bend_event_handler (int val)
608 uint8_t channel = PBD::atoi (_midi_channel.get_text ()) - 1;
610 ev[0] = MIDI_CMD_BENDER | channel;
612 ev[2] = (val >> 7) & 0x7f;
613 _session->vkbd_output_port ()->write (ev, 3, 0);
617 VirtualKeyboardWindow::pitch_bend_release ()
619 _pitch_adjustment.set_value (8192);