summaryrefslogtreecommitdiff
path: root/gtk2_ardour/pianokeyboard.cc
blob: ba5a2499177ebee200f27c3dc5be893203be4fcd (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
/* Piano-keyboard based on jack-keyboard
 *
 * Copyright (C) 2019 Robin Gareus <robin@gareus.org>
 * Copyright (c) 2007, 2008 Edward Tomasz Napierała <trasz@FreeBSD.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 <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>

#include "gtkmm2ext/keyboard.h"

#include "pianokeyboard.h"

#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

#define PIANO_MIN_NOTE 21
#define PIANO_MAX_NOTE 108
#define OCTAVE_MIN (-1)
#define OCTAVE_MAX (7)

void
APianoKeyboard::annotate_layout (cairo_t* cr, int note) const
{
	int nkey = note - _octave * 12;

	if (nkey < 0 || nkey >= NNOTES) {
		return;
	}

	const char* key_name = _keyboard_layout.note_binding (nkey);
	if (!key_name) {
		return;
	}

	int x = _notes[note].x;
	int w = _notes[note].w;
	int h = _notes[note].h;

	int  tw, th;
	char buf[32];
	snprintf (buf, 16, "%lc",
			gdk_keyval_to_unicode (gdk_keyval_to_upper (gdk_keyval_from_name (key_name))));
	PangoLayout* pl = pango_cairo_create_layout (cr);
	pango_layout_set_font_description (pl, _font_cue);
	pango_layout_set_text (pl, buf, -1);
	pango_layout_set_alignment (pl, PANGO_ALIGN_LEFT);
	pango_layout_get_pixel_size (pl, &tw, &th);

	if (_notes[note].white) {
		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);
		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);
		}
		pango_cairo_show_layout (cr, pl);
		cairo_restore (cr);
	}
	g_object_unref (pl);
}

void
APianoKeyboard::annotate_note (cairo_t* cr, int note) const
{
	assert ((note % 12) == 0);

	int x = _notes[note].x;
	int w = _notes[note].w;
	int h = _notes[note].h;

	int  tw, th;
	char buf[32];
	sprintf (buf, "C%d", (note / 12) - 1);
	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);
}

void
APianoKeyboard::draw_note (cairo_t* cr, int note) const
{
	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);

	if (_annotate_octave && (note % 12) == 0) {
		annotate_note (cr, note);
	}

	if (_annotate_layout) {
		annotate_layout (cr, note);
	}

	/* 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);
	}
}

void
APianoKeyboard::queue_note_draw (int note)
{
	queue_draw_area (_notes[note].x, 0, _notes[note].w, _notes[note].h);
}

void
APianoKeyboard::press_key (int key, int vel)
{
	assert (key >= 0);
	assert (key < NNOTES);

	/* This is for keyboard autorepeat protection. */
	if (_notes[key].pressed) {
		return;
	}

	if (_sustain_new_notes) {
		_notes[key].sustained = true;
	} else {
		_notes[key].sustained = false;
	}

	if (_monophonic && _last_key != key) {
		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 */
		}
		queue_note_draw (_last_key);
	}
	_last_key = key;

	_notes[key].pressed = true;

	NoteOn (key, vel); /* EMIT SIGNAL */
	queue_note_draw (key);
}

void
APianoKeyboard::release_key (int key)
{
	assert (key >= 0);
	assert (key < NNOTES);

	if (!_notes[key].pressed) {
		return;
	}

	if (_sustain_new_notes) {
		_notes[key].sustained = true;
	}

	_notes[key].pressed = false;

	if (_notes[key].sustained) {
		return;
	}

	NoteOff (key); /* EMIT SIGNAL */
	queue_note_draw (key);
}

void
APianoKeyboard::stop_unsustained_notes ()
{
	for (int i = 0; i < NNOTES; ++i) {
		if (_notes[i].pressed && !_notes[i].sustained) {
			_notes[i].pressed = false;
			NoteOff (i); /* EMIT SIGNAL */
			queue_note_draw (i);
		}
	}
}

void
APianoKeyboard::stop_sustained_notes ()
{
	for (int i = 0; i < NNOTES; ++i) {
		if (_notes[i].sustained) {
			_notes[i].sustained = false;
			if (_notes[i].pressed) {
				continue;
			}
			NoteOff (i); /* EMIT SIGNAL */
			queue_note_draw (i);
		}
	}
}

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;
			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:
				PitchBend (8192, false);
				break;
			case GDK_KEY_Up:
				/* fallthrough */
			case GDK_KEY_Down:
				PitchBend (8192, true);
				return true;
			default:
				break;
		}
	}
	return false;
}

bool
APianoKeyboard::on_key_press_event (GdkEventKey* event)
{
	if (Gtkmm2ext::Keyboard::modifier_state_contains (event->state, Gtkmm2ext::Keyboard::PrimaryModifier)) {
		return false;
	}
	if (handle_fixed_keys (event)) {
		return true;
	}

	char const* key = PianoKeyBindings::get_keycode (event);
	int note = _keyboard_layout.key_binding (key);

	if (note < -1) {
		return true;
	}
	else if (note < 0) {
		return false;
	}

	if (note == 128) {
		/* Rest is used on release */
		return false;
	}
	if (note == 129) {
		sustain_press ();
		return true;
	}

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

	note += _octave * 12;

	assert (key);
	assert (note >= 0);
	assert (note < NNOTES);

	_note_stack[key] = note;

	press_key (note, _key_velocity);

	return true;
}

bool
APianoKeyboard::on_key_release_event (GdkEventKey* event)
{
	if (Gtkmm2ext::Keyboard::modifier_state_contains (event->state, Gtkmm2ext::Keyboard::PrimaryModifier)) {
		return false;
	}
	if (handle_fixed_keys (event)) {
		return true;
	}

	char const* key = PianoKeyBindings::get_keycode (event);

	if (!key) {
		return false;
	}

	int note = _keyboard_layout.key_binding (key);

	if (note == 128) {
		Rest (); /* EMIT SIGNAL */
		return true;
	}
	if (note == 129) {
		sustain_release ();
		return true;
	}
	if (note < -1) {
		return true;
	}

	std::map<std::string, int>::const_iterator kv = _note_stack.find (key);
	if (kv == _note_stack.end ()) {
		return note != -1;
	}

	release_key (kv->second);
	_note_stack.erase (key);

	return true;
}

int
APianoKeyboard::get_note_for_xy (int x, int y) const
{
	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
APianoKeyboard::get_velocity_for_note_at_y (int note, int y) const
{
	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
APianoKeyboard::on_button_press_event (GdkEventButton* event)
{
	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);
		}

		press_key (note, get_velocity_for_note_at_y (note, y));
		_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
APianoKeyboard::on_button_release_event (GdkEventButton* event)
{
	return on_button_press_event (event);
}

bool
APianoKeyboard::on_motion_notify_event (GdkEventMotion* event)
{
	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
APianoKeyboard::on_expose_event (GdkEventExpose* event)
{
	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);

	char buf[32];
	sprintf (buf, "ArdourMono %dpx", MAX (8, MIN (20, _notes[1].w / 2 + 3)));
	_font_cue = pango_font_description_from_string (buf);
	sprintf (buf, "ArdourMono %dpx", MAX (8, MIN (20, MIN (_notes[0].w * 11 / 15 , _notes[0].h / 7))));
	_font_octave = pango_font_description_from_string (buf);

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

	pango_font_description_free (_font_cue);
	pango_font_description_free (_font_octave);

	cairo_destroy (cr);
	return true;
}

void
APianoKeyboard::on_size_request (Gtk::Requisition* requisition)
{
	requisition->width  = PIANO_KEYBOARD_DEFAULT_WIDTH;
	requisition->height = PIANO_KEYBOARD_DEFAULT_HEIGHT;
	if (_annotate_layout) {
		requisition->height += 16;
	}
	if (_annotate_octave) {
		requisition->height += 24;
	}
}

int
APianoKeyboard::is_black (int key) const
{
	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
APianoKeyboard::black_key_left_shift (int key) const
{
	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
APianoKeyboard::recompute_dimensions ()
{
	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;
		}
	}

	assert (number_of_white_keys > 0);

	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 +
			                 (white_key * key_width) -
			                 (black_key_width * black_key_left_shift (note));
			_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
APianoKeyboard::on_size_allocate (Gtk::Allocation& allocation)
{
	DrawingArea::on_size_allocate (allocation);
	recompute_dimensions ();
}

APianoKeyboard::APianoKeyboard ()
{
	using namespace Gdk;
	add_events (KEY_PRESS_MASK | KEY_RELEASE_MASK | BUTTON_PRESS_MASK | BUTTON_RELEASE_MASK | POINTER_MOTION_MASK | POINTER_MOTION_HINT_MASK);

	_sustain_new_notes              = false;
	_highlight_grand_piano_range    = true;
	_annotate_layout                = false;
	_annotate_octave                = false;
	_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;
}

APianoKeyboard::~APianoKeyboard ()
{
}

void
APianoKeyboard::set_grand_piano_highlight (bool enabled)
{
	_highlight_grand_piano_range = enabled;
	queue_draw ();
}

void
APianoKeyboard::set_annotate_layout (bool enabled)
{
	_annotate_layout = enabled;
	queue_draw ();
}

void
APianoKeyboard::set_annotate_octave (bool enabled)
{
	_annotate_octave = enabled;
	queue_draw ();
}

void
APianoKeyboard::set_monophonic (bool monophonic)
{
	_monophonic = monophonic;
}

void
APianoKeyboard::set_velocities (int min_vel, int max_vel, int key_vel)
{
	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
APianoKeyboard::sustain_press ()
{
	if (_sustain_new_notes) {
		return;
	}
	_sustain_new_notes = true;
	SustainChanged (true); /* EMIT SIGNAL */
}

void
APianoKeyboard::sustain_release ()
{
	stop_sustained_notes ();
	if (_sustain_new_notes) {
		_sustain_new_notes = false;
		SustainChanged (false); /* EMIT SIGNAL */
	}
}

void
APianoKeyboard::reset ()
{
	sustain_release ();
	stop_unsustained_notes ();
}

void
APianoKeyboard::set_note_on (int note)
{
	if (!_notes[note].pressed) {
		_notes[note].pressed = true;
		queue_note_draw (note);
	}
}

void
APianoKeyboard::set_note_off (int note)
{
	if (_notes[note].pressed || _notes[note].sustained) {
		_notes[note].pressed   = false;
		_notes[note].sustained = false;
		queue_note_draw (note);
	}
}

void
APianoKeyboard::set_octave (int octave)
{
	if (octave < -1) {
		octave = -1;
	} else if (octave > 7) {
		octave = 7;
	}

	_octave = octave;
	set_octave_range (_octave_range);
}

void
APianoKeyboard::set_octave_range (int octave_range)
{
	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;
		_min_note    = 0;
	} 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
APianoKeyboard::set_keyboard_layout (PianoKeyBindings::Layout layout)
{
	_keyboard_layout.set_layout (layout);
	queue_draw ();
}