2019-10-25 12:01:08 -04:00
|
|
|
/* Piano-keyboard based on jack-keyboard
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 Robin Gareus <robin@gareus.org>
|
2019-10-24 13:43:46 -04:00
|
|
|
* Copyright (c) 2007, 2008 Edward Tomasz Napierała <trasz@FreeBSD.org>
|
|
|
|
*
|
2019-10-25 12:01:08 -04:00
|
|
|
* 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.
|
2019-10-24 13:43:46 -04:00
|
|
|
*
|
2019-10-25 12:01:08 -04:00
|
|
|
* 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.
|
2019-10-24 13:43:46 -04:00
|
|
|
*
|
2019-10-25 12:01:08 -04:00
|
|
|
* 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.
|
2019-10-24 13:43:46 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <cairo/cairo.h>
|
|
|
|
#include <pango/pango.h>
|
|
|
|
#include <pango/pangocairo.h>
|
|
|
|
|
|
|
|
#include <gdk/gdkkeysyms.h>
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2019-12-15 14:34:14 -05:00
|
|
|
#include "gtkmm2ext/keyboard.h"
|
|
|
|
|
2019-10-25 12:01:08 -04:00
|
|
|
#include "pianokeyboard.h"
|
2019-10-24 13:43:46 -04:00
|
|
|
|
|
|
|
#ifndef M_PI
|
|
|
|
# define M_PI 3.14159265358979323846
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MIN
|
|
|
|
# define MIN(A, B) ((A) < (B)) ? (A) : (B)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MAX
|
|
|
|
# define MAX(A, B) ((A) > (B)) ? (A) : (B)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define PIANO_KEYBOARD_DEFAULT_WIDTH 730
|
|
|
|
#define PIANO_KEYBOARD_DEFAULT_HEIGHT 70
|
|
|
|
|
2019-10-25 12:05:28 -04:00
|
|
|
#define PIANO_MIN_NOTE 21
|
|
|
|
#define PIANO_MAX_NOTE 108
|
|
|
|
#define OCTAVE_MIN (-1)
|
|
|
|
#define OCTAVE_MAX (7)
|
|
|
|
|
2019-10-24 13:43:46 -04:00
|
|
|
void
|
2019-10-25 12:01:08 -04:00
|
|
|
APianoKeyboard::annotate_layout (cairo_t* cr, int note) const
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
|
|
|
int nkey = note - _octave * 12;
|
2019-10-25 12:05:28 -04:00
|
|
|
|
2019-10-24 13:43:46 -04:00
|
|
|
if (nkey < 0 || nkey >= NNOTES) {
|
|
|
|
return;
|
|
|
|
}
|
2019-10-28 10:36:27 -04:00
|
|
|
|
2020-04-03 15:17:53 -04:00
|
|
|
const char* key_name = _keyboard_layout.note_binding (nkey);
|
|
|
|
if (!key_name) {
|
2019-10-24 13:43:46 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-25 12:05:28 -04:00
|
|
|
int x = _notes[note].x;
|
|
|
|
int w = _notes[note].w;
|
|
|
|
int h = _notes[note].h;
|
2019-10-24 13:43:46 -04:00
|
|
|
|
|
|
|
int tw, th;
|
|
|
|
char buf[32];
|
2019-10-25 12:05:28 -04:00
|
|
|
snprintf (buf, 16, "%lc",
|
2020-04-03 15:17:53 -04:00
|
|
|
gdk_keyval_to_unicode (gdk_keyval_to_upper (gdk_keyval_from_name (key_name))));
|
2019-10-24 13:43:46 -04:00
|
|
|
PangoLayout* pl = pango_cairo_create_layout (cr);
|
2019-10-25 12:05:28 -04:00
|
|
|
pango_layout_set_font_description (pl, _font_cue);
|
2019-10-24 13:43:46 -04:00
|
|
|
pango_layout_set_text (pl, buf, -1);
|
|
|
|
pango_layout_set_alignment (pl, PANGO_ALIGN_LEFT);
|
|
|
|
pango_layout_get_pixel_size (pl, &tw, &th);
|
|
|
|
|
2019-10-25 12:05:28 -04:00
|
|
|
if (_notes[note].white) {
|
2019-10-24 13:43:46 -04:00
|
|
|
cairo_set_source_rgba (cr, 0.0, 0.0, 0.5, 1.0);
|
|
|
|
} else {
|
|
|
|
cairo_set_source_rgba (cr, 1.0, 1.0, 0.5, 1.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tw < w) {
|
|
|
|
cairo_save (cr);
|
2019-10-25 12:05:28 -04:00
|
|
|
if (_notes[note].white) {
|
|
|
|
cairo_move_to (cr, x + (w - tw) / 2, h * 2 / 3 + 3);
|
|
|
|
} else {
|
|
|
|
cairo_move_to (cr, x + (w - tw) / 2, h - th - 3);
|
|
|
|
}
|
2019-10-24 13:43:46 -04:00
|
|
|
pango_cairo_show_layout (cr, pl);
|
|
|
|
cairo_restore (cr);
|
|
|
|
}
|
|
|
|
g_object_unref (pl);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-10-25 12:01:08 -04:00
|
|
|
APianoKeyboard::annotate_note (cairo_t* cr, int note) const
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
2019-10-25 12:05:28 -04:00
|
|
|
assert ((note % 12) == 0);
|
2019-10-24 13:43:46 -04:00
|
|
|
|
2019-10-25 12:05:28 -04:00
|
|
|
int x = _notes[note].x;
|
|
|
|
int w = _notes[note].w;
|
|
|
|
int h = _notes[note].h;
|
2019-10-24 13:43:46 -04:00
|
|
|
|
2019-10-25 12:05:28 -04:00
|
|
|
int tw, th;
|
|
|
|
char buf[32];
|
2019-10-25 15:23:27 -04:00
|
|
|
sprintf (buf, "C%d", (note / 12) - 1);
|
2019-10-25 12:05:28 -04:00
|
|
|
PangoLayout* pl = pango_cairo_create_layout (cr);
|
|
|
|
pango_layout_set_font_description (pl, _font_octave);
|
|
|
|
pango_layout_set_text (pl, buf, -1);
|
|
|
|
pango_layout_set_alignment (pl, PANGO_ALIGN_LEFT);
|
|
|
|
pango_layout_get_pixel_size (pl, &tw, &th);
|
|
|
|
|
|
|
|
if (th < w && tw < h * .3) {
|
|
|
|
cairo_save (cr);
|
|
|
|
cairo_move_to (cr, x + (w - th) / 2, h - 3);
|
|
|
|
cairo_rotate (cr, M_PI / -2.0);
|
|
|
|
|
|
|
|
cairo_set_line_width (cr, 1.0);
|
|
|
|
cairo_set_source_rgba (cr, 0, 0, 0, 1.0);
|
|
|
|
pango_cairo_show_layout (cr, pl);
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
cairo_rel_move_to (cr, -.5, -.5);
|
|
|
|
pango_cairo_update_layout (cr, pl);
|
|
|
|
cairo_set_source_rgba (cr, 1, 1, 1, 0.3);
|
|
|
|
pango_cairo_layout_path (cr, pl);
|
|
|
|
cairo_set_line_width (cr, 1.5);
|
|
|
|
cairo_stroke (cr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
cairo_restore (cr);
|
|
|
|
}
|
|
|
|
g_object_unref (pl);
|
2019-10-24 13:43:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-10-25 12:01:08 -04:00
|
|
|
APianoKeyboard::draw_note (cairo_t* cr, int note) const
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
|
|
|
if (note < _min_note || note > _max_note) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int is_white = _notes[note].white;
|
|
|
|
int x = _notes[note].x;
|
|
|
|
int w = _notes[note].w;
|
|
|
|
int h = _notes[note].h;
|
|
|
|
|
|
|
|
if (_notes[note].pressed || _notes[note].sustained) {
|
|
|
|
if (is_white) {
|
|
|
|
cairo_set_source_rgb (cr, 0.7, 0.5, 0.5);
|
|
|
|
} else {
|
|
|
|
cairo_set_source_rgb (cr, 0.6, 0.4, 0.4);
|
|
|
|
}
|
|
|
|
} else if (_highlight_grand_piano_range && (note < PIANO_MIN_NOTE || note > PIANO_MAX_NOTE)) {
|
|
|
|
if (is_white) {
|
|
|
|
cairo_set_source_rgb (cr, 0.7, 0.7, 0.7);
|
|
|
|
} else {
|
|
|
|
cairo_set_source_rgb (cr, 0.3, 0.3, 0.3);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (is_white) {
|
|
|
|
cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
|
|
|
|
} else {
|
|
|
|
cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cairo_set_line_width (cr, 1.0);
|
|
|
|
|
|
|
|
cairo_rectangle (cr, x, 0, w, h);
|
|
|
|
cairo_fill (cr);
|
|
|
|
|
|
|
|
cairo_set_source_rgb (cr, 0.0f, 0.0f, 0.0f); /* black outline */
|
|
|
|
cairo_rectangle (cr, x, 0, w, h);
|
|
|
|
cairo_stroke (cr);
|
|
|
|
|
2019-10-25 15:23:27 -04:00
|
|
|
if (_annotate_octave && (note % 12) == 0) {
|
2019-10-25 12:05:28 -04:00
|
|
|
annotate_note (cr, note);
|
|
|
|
}
|
|
|
|
|
2019-10-25 15:23:27 -04:00
|
|
|
if (_annotate_layout) {
|
2019-10-25 12:05:28 -04:00
|
|
|
annotate_layout (cr, note);
|
2019-10-24 13:43:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We need to redraw black keys that partially obscure the white one. */
|
|
|
|
if (note < NNOTES - 2 && !_notes[note + 1].white) {
|
|
|
|
draw_note (cr, note + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (note > 0 && !_notes[note - 1].white) {
|
|
|
|
draw_note (cr, note - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-25 12:05:28 -04:00
|
|
|
void
|
2019-10-25 12:01:08 -04:00
|
|
|
APianoKeyboard::queue_note_draw (int note)
|
2019-10-25 12:05:28 -04:00
|
|
|
{
|
|
|
|
queue_draw_area (_notes[note].x, 0, _notes[note].w, _notes[note].h);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-10-25 12:01:08 -04:00
|
|
|
APianoKeyboard::press_key (int key, int vel)
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
|
|
|
assert (key >= 0);
|
|
|
|
assert (key < NNOTES);
|
|
|
|
|
|
|
|
/* This is for keyboard autorepeat protection. */
|
2019-10-25 12:05:28 -04:00
|
|
|
if (_notes[key].pressed) {
|
|
|
|
return;
|
|
|
|
}
|
2019-10-24 13:43:46 -04:00
|
|
|
|
|
|
|
if (_sustain_new_notes) {
|
2019-10-25 12:05:28 -04:00
|
|
|
_notes[key].sustained = true;
|
2019-10-24 13:43:46 -04:00
|
|
|
} else {
|
2019-10-25 12:05:28 -04:00
|
|
|
_notes[key].sustained = false;
|
2019-10-24 13:43:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_monophonic && _last_key != key) {
|
2019-10-25 12:05:28 -04:00
|
|
|
bool signal_off = _notes[_last_key].pressed || _notes[_last_key].sustained;
|
|
|
|
_notes[_last_key].pressed = false;
|
|
|
|
_notes[_last_key].sustained = false;
|
|
|
|
if (signal_off) {
|
|
|
|
NoteOff (_last_key); /* EMIT SIGNAL */
|
|
|
|
}
|
2019-10-24 13:43:46 -04:00
|
|
|
queue_note_draw (_last_key);
|
|
|
|
}
|
|
|
|
_last_key = key;
|
|
|
|
|
2019-10-25 12:05:28 -04:00
|
|
|
_notes[key].pressed = true;
|
2019-10-24 13:43:46 -04:00
|
|
|
|
|
|
|
NoteOn (key, vel); /* EMIT SIGNAL */
|
|
|
|
queue_note_draw (key);
|
|
|
|
}
|
|
|
|
|
2019-10-25 12:05:28 -04:00
|
|
|
void
|
2019-10-25 12:01:08 -04:00
|
|
|
APianoKeyboard::release_key (int key)
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
|
|
|
assert (key >= 0);
|
|
|
|
assert (key < NNOTES);
|
|
|
|
|
2019-10-25 12:05:28 -04:00
|
|
|
if (!_notes[key].pressed) {
|
|
|
|
return;
|
|
|
|
}
|
2019-10-24 13:43:46 -04:00
|
|
|
|
|
|
|
if (_sustain_new_notes) {
|
2019-10-25 12:05:28 -04:00
|
|
|
_notes[key].sustained = true;
|
2019-10-24 13:43:46 -04:00
|
|
|
}
|
|
|
|
|
2019-10-25 12:05:28 -04:00
|
|
|
_notes[key].pressed = false;
|
2019-10-24 13:43:46 -04:00
|
|
|
|
2019-10-25 12:05:28 -04:00
|
|
|
if (_notes[key].sustained) {
|
|
|
|
return;
|
|
|
|
}
|
2019-10-24 13:43:46 -04:00
|
|
|
|
|
|
|
NoteOff (key); /* EMIT SIGNAL */
|
|
|
|
queue_note_draw (key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-10-25 12:01:08 -04:00
|
|
|
APianoKeyboard::stop_unsustained_notes ()
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
2019-10-25 12:05:28 -04:00
|
|
|
for (int i = 0; i < NNOTES; ++i) {
|
2019-10-24 13:43:46 -04:00
|
|
|
if (_notes[i].pressed && !_notes[i].sustained) {
|
2019-10-25 12:05:28 -04:00
|
|
|
_notes[i].pressed = false;
|
2019-10-24 13:43:46 -04:00
|
|
|
NoteOff (i); /* EMIT SIGNAL */
|
|
|
|
queue_note_draw (i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-10-25 12:01:08 -04:00
|
|
|
APianoKeyboard::stop_sustained_notes ()
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
2019-10-25 12:05:28 -04:00
|
|
|
for (int i = 0; i < NNOTES; ++i) {
|
2019-10-24 13:43:46 -04:00
|
|
|
if (_notes[i].sustained) {
|
2019-10-25 12:05:28 -04:00
|
|
|
_notes[i].sustained = false;
|
2019-11-02 18:52:29 -04:00
|
|
|
if (_notes[i].pressed) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-10-24 13:43:46 -04:00
|
|
|
NoteOff (i); /* EMIT SIGNAL */
|
|
|
|
queue_note_draw (i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-16 09:02:39 -05:00
|
|
|
bool
|
|
|
|
APianoKeyboard::handle_fixed_keys (GdkEventKey* ev)
|
|
|
|
{
|
|
|
|
if (ev->type == GDK_KEY_PRESS) {
|
|
|
|
switch (ev->keyval) {
|
|
|
|
case GDK_KEY_Left:
|
|
|
|
SwitchOctave (false);
|
|
|
|
return true;
|
|
|
|
case GDK_KEY_Right:
|
|
|
|
SwitchOctave (true);
|
|
|
|
return true;
|
|
|
|
case GDK_KEY_F1:
|
|
|
|
PitchBend (0, false);
|
|
|
|
return true;
|
|
|
|
case GDK_KEY_F2:
|
|
|
|
PitchBend (4096, false);
|
|
|
|
return true;
|
|
|
|
case GDK_KEY_F3:
|
|
|
|
PitchBend (12288, false);
|
|
|
|
return true;
|
|
|
|
case GDK_KEY_F4:
|
|
|
|
PitchBend (16383, false);
|
|
|
|
return true;
|
2023-06-10 08:59:49 -04:00
|
|
|
case GDK_KEY_F5:
|
|
|
|
SetVelocity (32);
|
|
|
|
return true;
|
|
|
|
case GDK_KEY_F6:
|
|
|
|
SetVelocity (64);
|
|
|
|
return true;
|
|
|
|
case GDK_KEY_F7:
|
|
|
|
SetVelocity (96);
|
|
|
|
return true;
|
|
|
|
case GDK_KEY_F8:
|
|
|
|
SetVelocity (127);
|
|
|
|
return true;
|
2019-12-16 09:02:39 -05:00
|
|
|
case GDK_KEY_Down:
|
|
|
|
PitchBend (0, true);
|
|
|
|
return true;
|
|
|
|
case GDK_KEY_Up:
|
|
|
|
PitchBend (16383, true);
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (ev->type == GDK_KEY_RELEASE) {
|
|
|
|
switch (ev->keyval) {
|
|
|
|
case GDK_KEY_F1:
|
|
|
|
/* fallthrough */
|
|
|
|
case GDK_KEY_F2:
|
|
|
|
/* fallthrough */
|
|
|
|
case GDK_KEY_F3:
|
|
|
|
/* fallthrough */
|
|
|
|
case GDK_KEY_F4:
|
2019-12-17 10:47:46 -05:00
|
|
|
PitchBend (8192, false);
|
|
|
|
break;
|
2019-12-16 09:02:39 -05:00
|
|
|
case GDK_KEY_Up:
|
|
|
|
/* fallthrough */
|
|
|
|
case GDK_KEY_Down:
|
2019-12-17 10:47:46 -05:00
|
|
|
PitchBend (8192, true);
|
2019-12-16 09:02:39 -05:00
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-10-27 08:54:47 -04:00
|
|
|
bool
|
|
|
|
APianoKeyboard::on_key_press_event (GdkEventKey* event)
|
|
|
|
{
|
2019-12-27 12:42:20 -05:00
|
|
|
if (Gtkmm2ext::Keyboard::modifier_state_contains (event->state, Gtkmm2ext::Keyboard::PrimaryModifier)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (handle_fixed_keys (event)) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-12-15 14:34:14 -05:00
|
|
|
|
2020-04-07 12:38:33 -04:00
|
|
|
char const* key = PianoKeyBindings::get_keycode (event);
|
2020-04-03 15:17:53 -04:00
|
|
|
int note = _keyboard_layout.key_binding (key);
|
2019-10-24 13:43:46 -04:00
|
|
|
|
2019-12-05 16:12:49 -05:00
|
|
|
if (note < -1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (note < 0) {
|
2019-10-24 13:43:46 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (note == 128) {
|
2019-10-27 08:54:47 -04:00
|
|
|
/* Rest is used on release */
|
2019-10-25 15:23:27 -04:00
|
|
|
return false;
|
2019-10-24 13:43:46 -04:00
|
|
|
}
|
2019-11-02 18:52:29 -04:00
|
|
|
if (note == 129) {
|
2019-11-03 11:48:36 -05:00
|
|
|
sustain_press ();
|
2019-11-02 18:52:29 -04:00
|
|
|
return true;
|
|
|
|
}
|
2019-10-24 13:43:46 -04:00
|
|
|
|
2020-03-28 19:47:26 -04:00
|
|
|
std::map<std::string, int>::const_iterator kv = _note_stack.find (key);
|
|
|
|
if (kv != _note_stack.end ()) {
|
|
|
|
/* key is already pressed, ignore event.
|
|
|
|
* this can happen when changing the octave with the mouse
|
|
|
|
* while playing.
|
|
|
|
*/
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-10-24 13:43:46 -04:00
|
|
|
note += _octave * 12;
|
|
|
|
|
2019-10-27 08:54:47 -04:00
|
|
|
assert (key);
|
2019-10-24 13:43:46 -04:00
|
|
|
assert (note >= 0);
|
|
|
|
assert (note < NNOTES);
|
|
|
|
|
2019-10-27 08:54:47 -04:00
|
|
|
_note_stack[key] = note;
|
|
|
|
|
|
|
|
press_key (note, _key_velocity);
|
|
|
|
|
2019-10-24 13:43:46 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-10-25 12:01:08 -04:00
|
|
|
APianoKeyboard::on_key_release_event (GdkEventKey* event)
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
2019-12-27 12:42:20 -05:00
|
|
|
if (Gtkmm2ext::Keyboard::modifier_state_contains (event->state, Gtkmm2ext::Keyboard::PrimaryModifier)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (handle_fixed_keys (event)) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-12-16 09:02:39 -05:00
|
|
|
|
2020-04-07 12:38:33 -04:00
|
|
|
char const* key = PianoKeyBindings::get_keycode (event);
|
2019-10-27 08:54:47 -04:00
|
|
|
|
2019-10-28 10:36:27 -04:00
|
|
|
if (!key) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-03 15:17:53 -04:00
|
|
|
int note = _keyboard_layout.key_binding (key);
|
2019-11-03 11:48:36 -05:00
|
|
|
|
|
|
|
if (note == 128) {
|
2019-10-27 08:54:47 -04:00
|
|
|
Rest (); /* EMIT SIGNAL */
|
|
|
|
return true;
|
|
|
|
}
|
2019-11-03 11:48:36 -05:00
|
|
|
if (note == 129) {
|
|
|
|
sustain_release ();
|
|
|
|
return true;
|
|
|
|
}
|
2019-12-05 16:12:49 -05:00
|
|
|
if (note < -1) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-10-27 08:54:47 -04:00
|
|
|
|
2019-10-28 10:36:27 -04:00
|
|
|
std::map<std::string, int>::const_iterator kv = _note_stack.find (key);
|
|
|
|
if (kv == _note_stack.end ()) {
|
2019-11-03 11:48:36 -05:00
|
|
|
return note != -1;
|
2019-10-27 08:54:47 -04:00
|
|
|
}
|
|
|
|
|
2019-10-28 10:36:27 -04:00
|
|
|
release_key (kv->second);
|
2019-10-27 08:54:47 -04:00
|
|
|
_note_stack.erase (key);
|
|
|
|
|
|
|
|
return true;
|
2019-10-24 13:43:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-10-25 12:01:08 -04:00
|
|
|
APianoKeyboard::get_note_for_xy (int x, int y) const
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
|
|
|
int height = get_height ();
|
|
|
|
int note;
|
|
|
|
|
|
|
|
if (y <= ((height * 2) / 3)) { /* might be a black key */
|
|
|
|
for (note = 0; note <= _max_note; ++note) {
|
|
|
|
if (_notes[note].white) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x >= _notes[note].x && x <= _notes[note].x + _notes[note].w) {
|
|
|
|
return note;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (note = 0; note <= _max_note; ++note) {
|
|
|
|
if (!_notes[note].white) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x >= _notes[note].x && x <= _notes[note].x + _notes[note].w) {
|
|
|
|
return note;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-10-25 12:01:08 -04:00
|
|
|
APianoKeyboard::get_velocity_for_note_at_y (int note, int y) const
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
|
|
|
if (note < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int vel = _min_velocity + (_max_velocity - _min_velocity) * y / _notes[note].h;
|
|
|
|
|
|
|
|
if (vel < 1) {
|
|
|
|
return 1;
|
|
|
|
} else if (vel > 127) {
|
|
|
|
return 127;
|
|
|
|
}
|
|
|
|
return vel;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-10-25 12:01:08 -04:00
|
|
|
APianoKeyboard::on_button_press_event (GdkEventButton* event)
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
|
|
|
int x = event->x;
|
|
|
|
int y = event->y;
|
|
|
|
|
|
|
|
int note = get_note_for_xy (x, y);
|
|
|
|
|
|
|
|
if (event->button != 1)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (event->type == GDK_BUTTON_PRESS) {
|
|
|
|
if (note < 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_note_being_pressed_using_mouse >= 0) {
|
|
|
|
release_key (_note_being_pressed_using_mouse);
|
|
|
|
}
|
|
|
|
|
2019-10-25 08:35:38 -04:00
|
|
|
press_key (note, get_velocity_for_note_at_y (note, y));
|
2019-10-24 13:43:46 -04:00
|
|
|
_note_being_pressed_using_mouse = note;
|
|
|
|
|
|
|
|
} else if (event->type == GDK_BUTTON_RELEASE) {
|
|
|
|
if (note >= 0) {
|
|
|
|
release_key (note);
|
|
|
|
} else {
|
|
|
|
if (_note_being_pressed_using_mouse >= 0) {
|
|
|
|
release_key (_note_being_pressed_using_mouse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_note_being_pressed_using_mouse = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-10-25 12:01:08 -04:00
|
|
|
APianoKeyboard::on_button_release_event (GdkEventButton* event)
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
|
|
|
return on_button_press_event (event);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-10-25 12:01:08 -04:00
|
|
|
APianoKeyboard::on_motion_notify_event (GdkEventMotion* event)
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
|
|
|
int note;
|
|
|
|
|
|
|
|
if ((event->state & GDK_BUTTON1_MASK) == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
int x = event->x;
|
|
|
|
int y = event->y;
|
|
|
|
|
|
|
|
note = get_note_for_xy (x, y);
|
|
|
|
|
|
|
|
if (note != _note_being_pressed_using_mouse && note >= 0) {
|
|
|
|
if (_note_being_pressed_using_mouse >= 0) {
|
|
|
|
release_key (_note_being_pressed_using_mouse);
|
|
|
|
}
|
|
|
|
press_key (note, get_velocity_for_note_at_y (note, y));
|
|
|
|
_note_being_pressed_using_mouse = note;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-10-25 12:01:08 -04:00
|
|
|
APianoKeyboard::on_expose_event (GdkEventExpose* event)
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
|
|
|
cairo_t* cr = gdk_cairo_create (GDK_DRAWABLE (get_window ()->gobj ()));
|
|
|
|
cairo_rectangle (cr, event->area.x, event->area.y, event->area.width, event->area.height);
|
|
|
|
cairo_clip (cr);
|
|
|
|
|
2019-10-25 12:05:28 -04:00
|
|
|
char buf[32];
|
|
|
|
sprintf (buf, "ArdourMono %dpx", MAX (8, MIN (20, _notes[1].w / 2 + 3)));
|
|
|
|
_font_cue = pango_font_description_from_string (buf);
|
2019-10-25 15:23:27 -04:00
|
|
|
sprintf (buf, "ArdourMono %dpx", MAX (8, MIN (20, MIN (_notes[0].w * 11 / 15 , _notes[0].h / 7))));
|
2019-10-25 12:05:28 -04:00
|
|
|
_font_octave = pango_font_description_from_string (buf);
|
|
|
|
|
2019-10-24 13:43:46 -04:00
|
|
|
for (int i = 0; i < NNOTES; ++i) {
|
|
|
|
GdkRectangle r;
|
|
|
|
|
|
|
|
r.x = _notes[i].x;
|
|
|
|
r.y = 0;
|
|
|
|
r.width = _notes[i].w;
|
|
|
|
r.height = _notes[i].h;
|
|
|
|
|
|
|
|
switch (gdk_region_rect_in (event->region, &r)) {
|
|
|
|
case GDK_OVERLAP_RECTANGLE_PART:
|
|
|
|
case GDK_OVERLAP_RECTANGLE_IN:
|
|
|
|
draw_note (cr, i);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-25 12:05:28 -04:00
|
|
|
pango_font_description_free (_font_cue);
|
|
|
|
pango_font_description_free (_font_octave);
|
|
|
|
|
2019-10-24 13:43:46 -04:00
|
|
|
cairo_destroy (cr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-10-25 12:01:08 -04:00
|
|
|
APianoKeyboard::on_size_request (Gtk::Requisition* requisition)
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
|
|
|
requisition->width = PIANO_KEYBOARD_DEFAULT_WIDTH;
|
|
|
|
requisition->height = PIANO_KEYBOARD_DEFAULT_HEIGHT;
|
2019-10-26 18:05:22 -04:00
|
|
|
if (_annotate_layout) {
|
|
|
|
requisition->height += 16;
|
|
|
|
}
|
|
|
|
if (_annotate_octave) {
|
|
|
|
requisition->height += 24;
|
|
|
|
}
|
2019-10-24 13:43:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-10-25 12:01:08 -04:00
|
|
|
APianoKeyboard::is_black (int key) const
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
|
|
|
int note_in_octave = key % 12;
|
|
|
|
switch (note_in_octave) {
|
|
|
|
case 1:
|
|
|
|
case 3:
|
|
|
|
case 6:
|
|
|
|
case 8:
|
|
|
|
case 10:
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
2019-10-25 12:01:08 -04:00
|
|
|
APianoKeyboard::black_key_left_shift (int key) const
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
|
|
|
int note_in_octave = key % 12;
|
|
|
|
switch (note_in_octave) {
|
|
|
|
case 1:
|
|
|
|
return 2.0 / 3.0;
|
|
|
|
case 3:
|
|
|
|
return 1.0 / 3.0;
|
|
|
|
case 6:
|
|
|
|
return 2.0 / 3.0;
|
|
|
|
case 8:
|
|
|
|
return 0.5;
|
|
|
|
case 10:
|
|
|
|
return 1.0 / 3.0;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-10-25 12:01:08 -04:00
|
|
|
APianoKeyboard::recompute_dimensions ()
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
|
|
|
int note;
|
|
|
|
int number_of_white_keys = 0;
|
|
|
|
int skipped_white_keys = 0;
|
|
|
|
|
|
|
|
for (note = _min_note; note <= _max_note; ++note) {
|
|
|
|
if (!is_black (note)) {
|
|
|
|
++number_of_white_keys;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (note = 0; note < _min_note; ++note) {
|
|
|
|
if (!is_black (note)) {
|
|
|
|
++skipped_white_keys;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-26 21:32:18 -05:00
|
|
|
assert (number_of_white_keys > 0);
|
|
|
|
|
2019-10-24 13:43:46 -04:00
|
|
|
int width = get_width ();
|
|
|
|
int height = get_height ();
|
|
|
|
|
|
|
|
int key_width = width / number_of_white_keys;
|
|
|
|
int black_key_width = key_width * 0.8;
|
|
|
|
int useful_width = number_of_white_keys * key_width;
|
|
|
|
|
|
|
|
int widget_margin = (width - useful_width) / 2;
|
|
|
|
|
|
|
|
int white_key;
|
|
|
|
for (note = 0, white_key = -skipped_white_keys; note < NNOTES; ++note) {
|
|
|
|
if (is_black (note)) {
|
|
|
|
/* This note is black key. */
|
|
|
|
_notes[note].x = widget_margin +
|
2019-10-25 08:35:38 -04:00
|
|
|
(white_key * key_width) -
|
|
|
|
(black_key_width * black_key_left_shift (note));
|
2019-10-24 13:43:46 -04:00
|
|
|
_notes[note].w = black_key_width;
|
|
|
|
_notes[note].h = (height * 2) / 3;
|
|
|
|
_notes[note].white = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This note is white key. */
|
|
|
|
_notes[note].x = widget_margin + white_key * key_width;
|
|
|
|
_notes[note].w = key_width;
|
|
|
|
_notes[note].h = height;
|
|
|
|
_notes[note].white = 1;
|
|
|
|
|
|
|
|
white_key++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-10-25 12:01:08 -04:00
|
|
|
APianoKeyboard::on_size_allocate (Gtk::Allocation& allocation)
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
|
|
|
DrawingArea::on_size_allocate (allocation);
|
|
|
|
recompute_dimensions ();
|
|
|
|
}
|
|
|
|
|
2019-10-25 12:01:08 -04:00
|
|
|
APianoKeyboard::APianoKeyboard ()
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
|
|
|
using namespace Gdk;
|
2019-10-25 08:35:38 -04:00
|
|
|
add_events (KEY_PRESS_MASK | KEY_RELEASE_MASK | BUTTON_PRESS_MASK | BUTTON_RELEASE_MASK | POINTER_MOTION_MASK | POINTER_MOTION_HINT_MASK);
|
2019-10-24 13:43:46 -04:00
|
|
|
|
|
|
|
_sustain_new_notes = false;
|
2019-10-25 15:23:27 -04:00
|
|
|
_highlight_grand_piano_range = true;
|
|
|
|
_annotate_layout = false;
|
|
|
|
_annotate_octave = false;
|
2019-10-24 13:43:46 -04:00
|
|
|
_octave = 4;
|
|
|
|
_octave_range = 7;
|
|
|
|
_note_being_pressed_using_mouse = -1;
|
|
|
|
_min_note = 0;
|
|
|
|
_max_note = 127;
|
|
|
|
_last_key = 0;
|
|
|
|
_monophonic = false;
|
|
|
|
|
|
|
|
_min_velocity = 1;
|
|
|
|
_max_velocity = 127;
|
|
|
|
_key_velocity = 100;
|
|
|
|
}
|
|
|
|
|
2019-10-25 12:01:08 -04:00
|
|
|
APianoKeyboard::~APianoKeyboard ()
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-10-25 15:23:27 -04:00
|
|
|
APianoKeyboard::set_grand_piano_highlight (bool enabled)
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
2019-10-25 15:23:27 -04:00
|
|
|
_highlight_grand_piano_range = enabled;
|
2019-10-24 13:43:46 -04:00
|
|
|
queue_draw ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-10-25 15:23:27 -04:00
|
|
|
APianoKeyboard::set_annotate_layout (bool enabled)
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
2019-10-25 15:23:27 -04:00
|
|
|
_annotate_layout = enabled;
|
2019-10-24 13:43:46 -04:00
|
|
|
queue_draw ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-10-25 15:23:27 -04:00
|
|
|
APianoKeyboard::set_annotate_octave (bool enabled)
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
2019-10-25 15:23:27 -04:00
|
|
|
_annotate_octave = enabled;
|
2019-10-24 13:43:46 -04:00
|
|
|
queue_draw ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-10-25 12:01:08 -04:00
|
|
|
APianoKeyboard::set_monophonic (bool monophonic)
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
|
|
|
_monophonic = monophonic;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-10-25 12:01:08 -04:00
|
|
|
APianoKeyboard::set_velocities (int min_vel, int max_vel, int key_vel)
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
|
|
|
if (min_vel <= max_vel && min_vel > 0 && max_vel < 128) {
|
|
|
|
_min_velocity = min_vel;
|
|
|
|
_max_velocity = max_vel;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key_vel > 0 && key_vel < 128) {
|
|
|
|
_key_velocity = key_vel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-10-25 12:01:08 -04:00
|
|
|
APianoKeyboard::sustain_press ()
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
2019-11-02 18:52:29 -04:00
|
|
|
if (_sustain_new_notes) {
|
|
|
|
return;
|
2019-10-24 13:43:46 -04:00
|
|
|
}
|
2019-11-02 18:52:29 -04:00
|
|
|
_sustain_new_notes = true;
|
|
|
|
SustainChanged (true); /* EMIT SIGNAL */
|
2019-10-24 13:43:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-10-25 12:01:08 -04:00
|
|
|
APianoKeyboard::sustain_release ()
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
2019-11-02 18:52:29 -04:00
|
|
|
stop_sustained_notes ();
|
|
|
|
if (_sustain_new_notes) {
|
|
|
|
_sustain_new_notes = false;
|
|
|
|
SustainChanged (false); /* EMIT SIGNAL */
|
2019-10-24 13:43:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-26 06:17:20 -05:00
|
|
|
void
|
|
|
|
APianoKeyboard::reset ()
|
|
|
|
{
|
|
|
|
sustain_release ();
|
|
|
|
stop_unsustained_notes ();
|
|
|
|
}
|
|
|
|
|
2019-10-24 13:43:46 -04:00
|
|
|
void
|
2019-10-25 12:01:08 -04:00
|
|
|
APianoKeyboard::set_note_on (int note)
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
2019-10-25 12:05:28 -04:00
|
|
|
if (!_notes[note].pressed) {
|
|
|
|
_notes[note].pressed = true;
|
2019-10-24 13:43:46 -04:00
|
|
|
queue_note_draw (note);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-10-25 12:01:08 -04:00
|
|
|
APianoKeyboard::set_note_off (int note)
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
|
|
|
if (_notes[note].pressed || _notes[note].sustained) {
|
2019-10-25 12:05:28 -04:00
|
|
|
_notes[note].pressed = false;
|
|
|
|
_notes[note].sustained = false;
|
2019-10-24 13:43:46 -04:00
|
|
|
queue_note_draw (note);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-10-25 12:01:08 -04:00
|
|
|
APianoKeyboard::set_octave (int octave)
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
|
|
|
if (octave < -1) {
|
|
|
|
octave = -1;
|
|
|
|
} else if (octave > 7) {
|
|
|
|
octave = 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
_octave = octave;
|
|
|
|
set_octave_range (_octave_range);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-10-25 12:01:08 -04:00
|
|
|
APianoKeyboard::set_octave_range (int octave_range)
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
|
|
|
if (octave_range < 2) {
|
|
|
|
octave_range = 2;
|
|
|
|
}
|
|
|
|
if (octave_range > 11) {
|
|
|
|
octave_range = 11;
|
|
|
|
}
|
|
|
|
|
|
|
|
_octave_range = octave_range;
|
|
|
|
|
|
|
|
/* -1 <= _octave <= 7
|
|
|
|
* key-bindings are at offset 12 .. 40
|
|
|
|
* default piano range: _octave = 4, range = 7 -> note 21..108
|
|
|
|
*/
|
|
|
|
|
|
|
|
switch (_octave_range) {
|
|
|
|
default:
|
|
|
|
assert (0);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
case 3:
|
|
|
|
_min_note = (_octave + 1) * 12;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
case 5:
|
|
|
|
_min_note = (_octave + 0) * 12;
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
_min_note = (_octave - 1) * 12;
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
case 8:
|
|
|
|
_min_note = (_octave - 2) * 12;
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
case 10:
|
|
|
|
_min_note = (_octave - 3) * 12;
|
|
|
|
break;
|
|
|
|
case 11:
|
|
|
|
_min_note = (_octave - 4) * 12;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
int upper_offset = 0;
|
|
|
|
|
|
|
|
if (_min_note < 3) {
|
|
|
|
upper_offset = 0;
|
2019-10-25 08:35:38 -04:00
|
|
|
_min_note = 0;
|
2019-10-24 13:43:46 -04:00
|
|
|
} else if (_octave_range > 5) {
|
|
|
|
/* extend down to A */
|
|
|
|
upper_offset = 3;
|
|
|
|
_min_note -= 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
_max_note = MIN (127, upper_offset + _min_note + _octave_range * 12);
|
|
|
|
|
|
|
|
if (_max_note == 127) {
|
|
|
|
_min_note = MAX (0, _max_note - _octave_range * 12);
|
|
|
|
}
|
|
|
|
|
|
|
|
recompute_dimensions ();
|
|
|
|
queue_draw ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-04-07 12:38:33 -04:00
|
|
|
APianoKeyboard::set_keyboard_layout (PianoKeyBindings::Layout layout)
|
2019-10-24 13:43:46 -04:00
|
|
|
{
|
2020-04-04 03:36:51 -04:00
|
|
|
_keyboard_layout.set_layout (layout);
|
2019-10-24 13:43:46 -04:00
|
|
|
queue_draw ();
|
|
|
|
}
|