Reenable the correct sort column and type when redisplaying regions
[ardour.git] / gtk2_ardour / virtual_keyboard_window.cc
1 /*
2  * Copyright (C) 2019 Robin Gareus <robin@gareus.org>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <gtkmm/box.h>
20
21
22 #include "pbd/convert.h"
23 #include "pbd/compose.h"
24
25 #include "ardour/async_midi_port.h"
26 #include "ardour/session.h"
27
28 #include "gtkmm2ext/utils.h"
29 #include "widgets/tooltips.h"
30
31 #include "ardour_ui.h"
32 #include "ui_config.h"
33 #include "utils.h"
34 #include "virtual_keyboard_window.h"
35
36 #include "pbd/i18n.h"
37
38 using namespace Glib;
39 using namespace Gtk;
40 using namespace ArdourWidgets;
41
42 #define PX_SCALE(px) std::max ((float)px, rintf ((float)px* UIConfiguration::instance ().get_ui_scale ()))
43
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)
49 {
50         UIConfiguration::instance().ParameterChanged.connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::parameter_changed));
51
52         _piano.set_flags (Gtk::CAN_FOCUS);
53
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);
59
60         for (int c = 0; c < 16; ++c) {
61                 char buf[16];
62                 sprintf (buf, "%d", c + 1);
63                 _midi_channel.append_text_item (buf);
64         }
65
66 #if 0
67         for (int v = 0; v <= 128; v += 16) {
68                 char buf[16];
69                 sprintf (buf, "%d", std::min (127, std::max (1, v)));
70                 _piano_velocity.append_text_item (buf);
71         }
72 #else
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");
79 #endif
80
81         for (int k = -1; k < 8; ++k) {
82                 char buf[16];
83                 sprintf (buf, "%d", k);
84                 _piano_octave_key.append_text_item (buf);
85         }
86         for (int r = 2; r < 12; ++r) {
87                 char buf[16];
88                 sprintf (buf, "%d", r);
89                 _piano_octave_range.append_text_item (buf);
90         }
91         for (int t = -12; t < 13; ++t) {
92                 char buf[16];
93                 sprintf (buf, "%d", t);
94                 _transpose_output.append_text_item (buf);
95         }
96
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");
102
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);
106
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);
110
111         /* tooltips */
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."));
117
118         set_tooltip (_send_panic, _("Send MIDI Panic message for current channel"));
119
120         modwheel_update_tooltip (0);
121         pitch_bend_update_tooltip (8192);
122
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);
127
128         /* layout */
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);
135
136         const int default_cc[VKBD_NCTRLS] = { 7, 8, 91, 93};
137
138         int col = 4;
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");
145
146                 for (int c = 2; c < 120; ++c) {
147                         if (c == 32) {
148                                 continue;
149                         }
150                         char key[32];
151                         sprintf (key, "%d", c);
152                         _cc_key[i].append_text_item (key);
153                 }
154                 update_cc (i, default_cc[i]);
155
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);
158
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));
162         }
163
164         tbl->attach (*manage (new ArdourVSpacer),       col, col + 1, 0, 2, SHRINK, FILL, 4, 0);
165         ++col;
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);
168         ++col;
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);
171         ++col;
172
173         tbl->attach (*manage (new ArdourVSpacer),     col, col + 1, 0, 2, SHRINK, FILL, 4, 0);
174         ++col;
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);
177         ++col;
178
179         tbl->attach (*manage (new ArdourVSpacer),          col, col + 1, 0, 2, SHRINK, FILL, 4, 0);
180         ++col;
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);
183         ++col;
184         tbl->attach (_send_panic,                      col, col + 1, 0, 2, SHRINK, SHRINK, 4, 0);
185
186         /* main layout */
187         Box* box1 = manage (new HBox ());
188         box1->pack_start (*tbl, true, false);
189
190         VBox* vbox = manage (new VBox);
191         vbox->pack_start (*box1, false, false, 4);
192         vbox->pack_start (_piano, true, true);
193         add (*vbox);
194
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);
198
199         /* GUI signals */
200
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));
204
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));
207
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));
211
212         _send_panic.signal_button_release_event ().connect (sigc::mem_fun (*this, &VirtualKeyboardWindow::send_panic_message), false);
213
214         _piano_velocity.disable_scrolling ();
215         _piano_velocity.signal_scroll_event().connect (sigc::mem_fun(*this, &VirtualKeyboardWindow::on_velocity_scroll_event), false);
216
217         /* piano keyboard signals */
218
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));
223
224         /* initialize GUI */
225
226         update_velocity_settings ();
227         update_octave_range ();
228
229         set_keep_above (true);
230         vbox->show_all ();
231 }
232
233 VirtualKeyboardWindow::~VirtualKeyboardWindow ()
234 {
235         delete _pitch_slider_tooltip;
236 }
237
238 void
239 VirtualKeyboardWindow::set_session (ARDOUR::Session* s)
240 {
241         ArdourWindow::set_session (s);
242
243         if (!_session) {
244                 return;
245         }
246
247         XMLNode* node = _session->instant_xml (X_("VirtualKeyboard"));
248         if (node) {
249                 set_state (*node);
250         }
251 }
252 void
253 VirtualKeyboardWindow::parameter_changed (std::string const& p)
254 {
255         if (p == "vkeybd-layout") {
256                 select_keyboard_layout (UIConfiguration::instance().get_vkeybd_layout ());
257         }
258 }
259
260 XMLNode&
261 VirtualKeyboardWindow::get_state ()
262 {
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) {
270                 char buf[16];
271                 sprintf (buf, "CC-%d", i);
272                 node->set_property (buf, _cc_key[i].get_text ());
273         }
274         return *node;
275 }
276
277 void
278 VirtualKeyboardWindow::set_state (const XMLNode& root)
279 {
280         if (root.name () != "VirtualKeyboard") {
281                 return;
282         }
283
284         XMLNode const* node = &root;
285
286         for (int i = 0; i < VKBD_NCTRLS; ++i) {
287                 char buf[16];
288                 sprintf (buf, "CC-%d", i);
289                 std::string cckey;
290                 if (node->get_property (buf, cckey)) {
291                         update_cc (i, PBD::atoi (cckey));
292                 }
293         }
294
295         std::string s;
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);
300                 }
301         }
302         if (node->get_property (X_("Transpose"), s)) {
303                 _transpose_output.set_active (s);
304         }
305         if (node->get_property (X_("KeyVelocity"), s)) {
306                 _piano_velocity.set_active (s);
307         }
308         if (node->get_property (X_("Octave"), s)) {
309                 _piano_octave_key.set_active (s);
310         }
311         if (node->get_property (X_("Range"), s)) {
312                 _piano_octave_range.set_active (s);
313         }
314
315         update_velocity_settings ();
316         update_octave_range ();
317         update_octave_key ();
318 }
319
320 bool
321 VirtualKeyboardWindow::on_focus_in_event (GdkEventFocus* ev)
322 {
323         _piano.grab_focus ();
324         return ArdourWindow::on_focus_in_event (ev);
325 }
326
327 void
328 VirtualKeyboardWindow::on_unmap ()
329 {
330         ArdourWindow::on_unmap ();
331         ARDOUR_UI::instance ()->reset_focus (this);
332 }
333
334 bool
335 VirtualKeyboardWindow::on_key_press_event (GdkEventKey* ev)
336 {
337         /* try propagate unmodified events first */
338         if ((ev->state & 0xf) == 0) {
339                 if (gtk_window_propagate_key_event (gobj(), ev)) {
340                         return true;
341                 }
342         }
343
344         _piano.grab_focus ();
345
346         return ARDOUR_UI_UTILS::relay_key_press (ev, this);
347 }
348
349 bool
350 VirtualKeyboardWindow::on_key_release_event (GdkEventKey* ev)
351 {
352         /* try propagate unmodified events first */
353         if ((ev->state & 0xf) == 0) {
354                 if (gtk_window_propagate_key_event (gobj(), ev)) {
355                         return true;
356                 }
357         }
358
359         _piano.grab_focus ();
360
361         return ArdourWindow::on_key_release_event (ev);
362 }
363
364 void
365 VirtualKeyboardWindow::select_keyboard_layout (std::string const& l)
366 {
367         if (l == "QWERTY") {
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);
379         }
380         _piano.grab_focus ();
381 }
382
383 void
384 VirtualKeyboardWindow::update_octave_key ()
385 {
386         _piano.set_octave (PBD::atoi (_piano_octave_key.get_text ()));
387         _piano.grab_focus ();
388 }
389
390 void
391 VirtualKeyboardWindow::update_octave_range ()
392 {
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 ();
396 }
397
398 bool
399 VirtualKeyboardWindow::send_panic_message (GdkEventButton*)
400 {
401         _piano.reset ();
402         uint8_t channel = PBD::atoi (_midi_channel.get_text ()) - 1;
403         uint8_t ev[3];
404         ev[0] = MIDI_CMD_CONTROL | channel;
405         ev[1] = MIDI_CTL_SUSTAIN;
406         ev[2] = 0;
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);
412         return false;
413 }
414
415 bool
416 VirtualKeyboardWindow::on_velocity_scroll_event (GdkEventScroll* ev)
417 {
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);
422                         break;
423                 case GDK_SCROLL_UP:
424                         v = std::max (1, v - 1);
425                         break;
426                 default:
427                         return false;
428         }
429         char buf[16];
430         sprintf (buf, "%d", v);
431         _piano_velocity.set_active (buf);
432         return true;
433 }
434
435 void
436 VirtualKeyboardWindow::update_velocity_settings ()
437 {
438         int v = PBD::atoi (_piano_velocity.get_text ());
439         _piano.set_velocities (v, v, v);
440 }
441
442 void
443 VirtualKeyboardWindow::cc_key_changed (size_t i)
444 {
445         int ctrl = PBD::atoi (_cc_key[i].get_text ());
446         update_cc (i, ctrl);
447 }
448
449 void
450 VirtualKeyboardWindow::update_cc (size_t i, int cc)
451 {
452         assert (i < VKBD_NCTRLS);
453         if (cc < 0 || cc > 120) {
454                 return;
455         }
456         char buf[16];
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
461 }
462
463 void
464 VirtualKeyboardWindow::octave_key_event_handler (bool up)
465 {
466         int k = PBD::atoi (_piano_octave_key.get_text ()) + (up ? 1 : -1);
467         k = std::min (7, std::max (-1, k));
468         char buf[16];
469         sprintf (buf, "%d", k);
470         _piano_octave_key.set_active (buf);
471 }
472
473 void
474 VirtualKeyboardWindow::pitch_bend_key_event_handler (int target, bool interpolate)
475 {
476         int cur = _pitch_adjustment.get_value();
477         if (cur == target) {
478                 return;
479         }
480         if (interpolate) {
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*/);
487                 }
488                 return;
489         }
490                 _bender_connection.disconnect ();
491         _pitch_adjustment.set_value (target);
492         _pitch_bend_target = target;
493 }
494
495 bool
496 VirtualKeyboardWindow::pitch_bend_timeout ()
497 {
498         int cur = _pitch_adjustment.get_value();
499
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;
506         }
507         _pitch_adjustment.set_value (cur);
508         return _pitch_bend_target != cur;
509 }
510
511 void
512 VirtualKeyboardWindow::pitch_slider_adjusted ()
513 {
514         _pitchbend->set_value (_pitch_adjustment.get_value (), PBD::Controllable::NoGroup);
515         pitch_bend_update_tooltip (_pitch_adjustment.get_value ());
516 }
517
518 void
519 VirtualKeyboardWindow::pitch_bend_update_tooltip (int value)
520 {
521         _pitch_slider_tooltip->set_tip (string_compose (
522               _("Pitchbend: %1\n"
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));
527 }
528
529 void
530 VirtualKeyboardWindow::modwheel_slider_adjusted ()
531 {
532         _modwheel->set_value (_modwheel_adjustment.get_value (), PBD::Controllable::NoGroup);
533         modwheel_update_tooltip (_modwheel_adjustment.get_value ());
534 }
535
536 void
537 VirtualKeyboardWindow::modwheel_update_tooltip (int value)
538 {
539         _modwheel_tooltip->set_tip (string_compose (_("Modulation: %1"), value));
540 }
541
542 void
543 VirtualKeyboardWindow::note_on_event_handler (int note, int velocity)
544 {
545         _piano.grab_focus ();
546         if (!_session) {
547                 return;
548         }
549         note += PBD::atoi (_transpose_output.get_text ());
550         if (note < 0 || note > 127) {
551                 return;
552         }
553         uint8_t channel = PBD::atoi (_midi_channel.get_text ()) - 1;
554         uint8_t ev[3];
555         ev[0] = MIDI_CMD_NOTE_ON | channel;
556         ev[1] = note;
557         ev[2] = velocity;
558         _session->vkbd_output_port ()->write (ev, 3, 0);
559 }
560
561 void
562 VirtualKeyboardWindow::note_off_event_handler (int note)
563 {
564         if (!_session) {
565                 return;
566         }
567         note += PBD::atoi (_transpose_output.get_text ());
568         if (note < 0 || note > 127) {
569                 return;
570         }
571         uint8_t channel = PBD::atoi (_midi_channel.get_text ()) - 1;
572         uint8_t ev[3];
573         ev[0] = MIDI_CMD_NOTE_OFF | channel;
574         ev[1] = note;
575         ev[2] = 0;
576         _session->vkbd_output_port ()->write (ev, 3, 0);
577 }
578
579 void
580 VirtualKeyboardWindow::control_change_knob_event_handler (int key, int val)
581 {
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);
586 }
587
588 void
589 VirtualKeyboardWindow::control_change_event_handler (int ctrl, int val)
590 {
591         if (!_session) {
592                 return;
593         }
594         uint8_t channel = PBD::atoi (_midi_channel.get_text ()) - 1;
595         uint8_t ev[3];
596         ev[0] = MIDI_CMD_CONTROL | channel;
597         ev[1] = ctrl & 0x7f;
598         ev[2] = val & 0x7f;
599         _session->vkbd_output_port ()->write (ev, 3, 0);
600 }
601
602 void
603 VirtualKeyboardWindow::pitch_bend_event_handler (int val)
604 {
605         if (!_session) {
606                 return;
607         }
608         uint8_t channel = PBD::atoi (_midi_channel.get_text ()) - 1;
609         uint8_t ev[3];
610         ev[0] = MIDI_CMD_BENDER | channel;
611         ev[1] = val & 0x7f;
612         ev[2] = (val >> 7) & 0x7f;
613         _session->vkbd_output_port ()->write (ev, 3, 0);
614 }
615
616 void
617 VirtualKeyboardWindow::pitch_bend_release ()
618 {
619         _pitch_adjustment.set_value (8192);
620 }