summaryrefslogtreecommitdiff
path: root/gtk2_ardour/virtual_keyboard_window.cc
blob: bda15edce4727e8802ba54a7f724cac31f6c6776 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * Copyright (C) 2019 Robin Gareus <robin@gareus.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <gtkmm/box.h>

#include "ardour/async_midi_port.h"

#include "ardour_ui.h"
#include "virtual_keyboard_window.h"
#include "utils.h"

#include "pbd/i18n.h"

using namespace Glib;
using namespace Gtk;

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)))
{
	_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);

	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");

	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);

	set_keep_above (true);
	vbox->show_all();
}

VirtualKeyboardWindow::~VirtualKeyboardWindow ()
{
	delete _pianomm;
}

void
VirtualKeyboardWindow::on_unmap ()
{
	ArdourWindow::on_unmap ();
	ARDOUR_UI::instance()->reset_focus (this);
}

bool
VirtualKeyboardWindow::on_key_press_event (GdkEventKey* ev)
{
	return ARDOUR_UI_UTILS::relay_key_press (ev, this);
}

void
VirtualKeyboardWindow::note_on_event_handler (int note)
{
	_pianomm->grab_focus ();
	if (!_session) {
		return;
	}
	uint8_t channel = _piano_channel.get_value_as_int () - 1;
	uint8_t ev[3];
	ev[0] = (MIDI_CMD_NOTE_ON | channel);
	ev[1] = note;
	ev[2] = _piano_velocity.get_value_as_int ();
	_session->vkbd_output_port()->write (ev, 3, 0);
}

void
VirtualKeyboardWindow::note_off_event_handler (int note)
{
	if (!_session) {
		return;
	}

	uint8_t channel = _piano_channel.get_value_as_int () - 1;
	uint8_t ev[3];
	ev[0] = (MIDI_CMD_NOTE_OFF | channel);
	ev[1] = note;
	ev[2] = 0;
	_session->vkbd_output_port()->write (ev, 3, 0);
}