1 /* scherzo - Music notation training
3 * Copyright © 2010 Carl Worth
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see http://www.gnu.org/licenses/ .
20 #include <gdk/gdkkeysyms.h>
22 #include <asoundlib.h>
27 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
29 #define unused(foo) foo __attribute__((unused))
31 #define MIDI_BUF_SIZE 4096
33 typedef struct challenge
44 typedef struct pitch_group
52 typedef struct scherzo
60 score_staff_t *treble;
64 /* The word "keyboard" here is referring to a "computer
65 * keyboard". Any "piano keyboard" key knows its own octave and
66 * accidental already. */
68 pitch_accidental_t keyboard_accidental;
71 snd_midi_event_t *snd_midi_event;
74 challenge_t challenge;
76 pitch_group_t notes_pressed;
77 pitch_group_t notes_pedaled;
82 /* Forward declarations. */
84 scherzo_press_note (scherzo_t *scherzo, pitch_t pitch);
87 scherzo_release_note (scherzo_t *scherzo, pitch_t pitch);
90 scherzo_press_pedal (scherzo_t *scherzo);
93 scherzo_release_pedal (scherzo_t *scherzo);
96 _judge_pitch (scherzo_t *scherzo, pitch_t pitch);
99 _score_challenge (scherzo_t *scherzo);
102 on_delete_event_quit (unused (GtkWidget *widget),
103 unused (GdkEvent *event),
104 unused (gpointer user_data))
108 /* Returning FALSE allows the default handler for delete-event
109 * to proceed to cleanup the widget. */
114 on_expose_event_draw (GtkWidget *widget,
115 unused (GdkEventExpose *expose),
118 scherzo_t *scherzo = user_data;
119 score_t *score = scherzo->score;
121 GtkAllocation allocation;
122 static const int pad = 10;
125 gtk_widget_get_allocation (widget, &allocation);
126 widget_width = allocation.width;
128 cr = gdk_cairo_create (widget->window);
130 /* White background */
131 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
134 /* Add some padding on the sides and top */
135 cairo_translate (cr, pad, scherzo->staff_height);
136 score_set_staff_height (score, scherzo->staff_height);
137 score_set_width (score, widget_width - 2 * pad);
139 score_draw (score, cr);
145 on_key_press_event (GtkWidget *widget,
149 scherzo_t *scherzo = user_data;
151 /* Initialize to keep the compiler quiet. */
152 pitch_name_t pitch_name = PITCH_NATURAL (C, 0);
155 if (scherzo->challenge.note)
156 octave = PITCH_OCTAVE (scherzo->challenge.note->pitch);
158 octave = scherzo->keyboard_octave;
160 switch (key->keyval) {
164 case GDK_KEY_KP_Equal:
165 scherzo->staff_height += 4;
166 gtk_widget_queue_draw (widget);
170 case GDK_KEY_KP_Subtract:
171 scherzo->staff_height -= 4;
172 gtk_widget_queue_draw (widget);
182 pitch_name = PITCH_NAME_C;
186 pitch_name = PITCH_NAME_D;
190 pitch_name = PITCH_NAME_E;
194 pitch_name = PITCH_NAME_F;
198 pitch_name = PITCH_NAME_G;
202 pitch_name = PITCH_NAME_A;
206 pitch_name = PITCH_NAME_B;
217 scherzo->keyboard_octave = key->keyval - GDK_KEY_0;
220 scherzo_press_pedal (scherzo);
223 if (scherzo->keyboard_accidental < PITCH_ACCIDENTAL_DOUBLE_SHARP)
224 scherzo->keyboard_accidental++;
227 if (scherzo->keyboard_accidental > PITCH_ACCIDENTAL_DOUBLE_FLAT)
228 scherzo->keyboard_accidental--;
232 pitch = PITCH (pitch_name, scherzo->keyboard_accidental, octave);
234 if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) ||
235 (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g))
237 scherzo_press_note (scherzo, pitch);
238 _judge_pitch (scherzo, pitch);
239 gtk_widget_queue_draw (scherzo->window);
245 /* Allow an unhandled event to propagate to other handlers. */
250 on_key_release_event (unused (GtkWidget *widget),
254 scherzo_t *scherzo = user_data;
256 /* Initialize to keep the compiler quiet. */
257 pitch_name_t pitch_name = PITCH_NAME_C;
260 if (scherzo->challenge.note)
261 octave = PITCH_OCTAVE (scherzo->challenge.note->pitch);
263 octave = scherzo->keyboard_octave;
265 switch (key->keyval) {
268 pitch_name = PITCH_NAME_C;
272 pitch_name = PITCH_NAME_D;
276 pitch_name = PITCH_NAME_E;
280 pitch_name = PITCH_NAME_F;
284 pitch_name = PITCH_NAME_G;
288 pitch_name = PITCH_NAME_A;
292 pitch_name = PITCH_NAME_B;
295 scherzo_release_pedal (scherzo);
299 pitch = PITCH (pitch_name, scherzo->keyboard_accidental, octave);
301 if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) ||
302 (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g))
304 scherzo_release_note (scherzo, pitch);
305 _score_challenge (scherzo);
306 gtk_widget_queue_draw (scherzo->window);
312 /* Allow an unhandled event to propagate to other handlers. */
317 _pitch_to_midi (pitch_t pitch)
319 int octave = PITCH_OCTAVE (pitch);
320 unsigned char midi_note = 12 * (octave + 1);
322 switch (PITCH_NAME (pitch)) {
345 switch (PITCH_ACCIDENTAL (pitch)) {
346 case PITCH_ACCIDENTAL_DOUBLE_FLAT:
349 case PITCH_ACCIDENTAL_FLAT:
352 case PITCH_ACCIDENTAL_NATURAL:
354 case PITCH_ACCIDENTAL_SHARP:
357 case PITCH_ACCIDENTAL_DOUBLE_SHARP:
365 /* octave can optionally by NULL */
367 _midi_to_pitch (unsigned char midi_note)
369 int octave = octave = midi_note / 12 - 1;
371 switch (midi_note % 12)
375 return PITCH_LITERAL (C, NATURAL, octave);
378 return PITCH_LITERAL (C, SHARP, octave);
381 return PITCH_LITERAL (D, NATURAL, octave);
384 return PITCH_LITERAL (D, SHARP, octave);
387 return PITCH_LITERAL (E, NATURAL, octave);
390 return PITCH_LITERAL (F, NATURAL, octave);
393 return PITCH_LITERAL (F, SHARP, octave);
396 return PITCH_LITERAL (G, NATURAL, octave);
399 return PITCH_LITERAL (G, SHARP, octave);
402 return PITCH_LITERAL (A, NATURAL, octave);
405 return PITCH_LITERAL (A, SHARP, octave);
408 return PITCH_LITERAL (B, NATURAL, octave);
413 /* Determine a chord name (if any) from the current notes pressed */
415 typedef struct analyzed_pitch {
416 /* Original note being analzyed. */
419 /* Absolute pitch (expressed as midi number). */
422 /* Pitch relative to bass note. */
427 _compare_analyzed_pitch_by_midi_pitch (const void *va, const void *vb)
429 const analyzed_pitch_t *a = va, *b = vb;
431 return a->midi_pitch - b->midi_pitch;
435 _compare_analyzed_pitch_by_relative_pitch (const void *va, const void *vb)
437 const analyzed_pitch_t *a = va, *b = vb;
439 return a->relative_pitch - b->relative_pitch;
442 typedef struct modified_degree
449 _modified_degree_to_half_steps (const modified_degree_t *degree)
452 int scale_degree = degree->degree;
454 /* Restrict to actual degrees within a scale. */
455 if (scale_degree > 7)
456 scale_degree = (scale_degree % 8) + 1;
458 /* Number of half steps from root to specified degree within a
460 switch (scale_degree) {
483 fprintf (stderr, "Internal: Invalid degree %d\n", degree->degree);
488 return half_steps + degree->modification;
491 /* Number of half steps from 'root' up to 'pitch' (that is, counting
492 * the steps up a chromatic scale), within the same octave. */
494 _pitch_from_root_in_half_steps (pitch_t pitch, pitch_t root)
496 int root_midi = _pitch_to_midi (root);
497 int pitch_midi = _pitch_to_midi (pitch);
499 while (pitch_midi < root_midi)
502 return (pitch_midi - root_midi) % 12;
506 _chord_signature_matches (analyzed_pitch_t *pitches,
508 modified_degree_t *degrees,
513 #define MAX_DEGREES 6
514 int relative_pitches[MAX_DEGREES];
517 assert (num_degrees <= MAX_DEGREES);
519 if (num_pitches != num_degrees)
522 if (inversion >= num_degrees)
525 /* We never spell simple intervals as inversions. */
526 if (num_degrees == 2 && inversion > 0)
529 for (i = 0; i < num_degrees; i++) {
530 /* The num_degrees is in the addition just to ensure all
531 * inputs to the modulus operator remain positive. */
532 int index = (i + num_degrees - inversion) % num_degrees;
534 /* Again, adding a 12 to keep things positive. */
535 relative_pitches[index] =
537 _modified_degree_to_half_steps (°rees[i]) -
538 _modified_degree_to_half_steps (°rees[inversion])) % 12;
542 for (i = 0; i < num_pitches; i++)
543 if (pitches[i].relative_pitch != relative_pitches[i])
546 root_index = (num_pitches - inversion) % num_pitches;
547 *root = pitches[root_index].pitch;
553 _pitch_str (pitch_t pitch)
555 static char double_flat[] = "X𝄫";
556 static char flat[] = "X♭";
557 static char natural[] = "X";
558 static char sharp[] = "X♯";
559 static char double_sharp[] = "X𝄪";
562 switch (PITCH_ACCIDENTAL (pitch)) {
563 case PITCH_ACCIDENTAL_DOUBLE_FLAT:
566 case PITCH_ACCIDENTAL_FLAT:
569 case PITCH_ACCIDENTAL_NATURAL:
572 case PITCH_ACCIDENTAL_SHARP:
575 case PITCH_ACCIDENTAL_DOUBLE_SHARP:
580 switch (PITCH_NAME(pitch)) {
607 typedef struct chord_signature
610 modified_degree_t degrees[MAX_DEGREES];
614 /* XXX: The superscript rise value should be relative to the current
615 * font size. Best would be for pango to support this. See:
617 * https://bugzilla.gnome.org/show_bug.cgi?id=708780
619 * For now, these are emipirically-derived values that look reasonable
620 * at the default size that score.c is using to draw the staff. If the
621 * user scales the staff up or down then these will look wrong.
624 #define SUP "<span font='33' rise='30000'>"
625 #define PUS "</span>"
627 chord_signature_t signatures[] = {
629 { 1, {{1, 0}}, "P8" },
632 { 2, {{1, 0}, {2, -1}}, "m2" },
633 { 2, {{1, 0}, {2, 0}}, "M2" },
634 { 2, {{1, 0}, {3, -1}}, "m3" },
635 { 2, {{1, 0}, {3, 0}}, "M3" },
636 { 2, {{1, 0}, {4, 0}}, "P4" },
637 { 2, {{1, 0}, {5, -1}}, "Tri-tone" },
638 { 2, {{1, 0}, {5, 0}}, "P5" },
639 { 2, {{1, 0}, {6, -1}}, "m6" },
640 { 2, {{1, 0}, {6, 0}}, "M6" },
641 { 2, {{1, 0}, {7, -1}}, "m7" },
642 { 2, {{1, 0}, {7, 0}}, "M7" },
645 { 3, {{1, 0}, {4, 0}, {5, 0}}, "sus" },
646 { 3, {{1, 0}, {3, 0}, {5, +1}}, SUP "+" PUS },
647 { 3, {{1, 0}, {3, 0}, {5, 0}}, "" },
648 { 3, {{1, 0}, {3, -1}, {5, 0}}, "m" },
649 { 3, {{1, 0}, {3, -1}, {5, -1}}, "°" },
650 { 3, {{1, 0}, {2, 0}, {5, 0}}, "msus2" },
652 /* Seventh chords with no 3rd */
653 { 3, {{1, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS },
654 { 3, {{1, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS },
655 { 3, {{1, 0}, {5, 0}, {7, 0}}, "M7" },
656 { 3, {{1, 0}, {5, 0}, {7, -1}}, "7" },
657 { 3, {{1, 0}, {5, -1}, {7, -1}}, "7♭5" },
659 /* Seventh chords with no 5th */
660 { 3, {{1, 0}, {4, 0}, {7, 0}}, "M7sus" },
661 { 3, {{1, 0}, {4, 0}, {7, -1}}, "7sus" },
662 { 3, {{1, 0}, {3, 0}, {7, 0}}, "M7" },
663 { 3, {{1, 0}, {3, 0}, {7, -1}}, "7" },
664 { 3, {{1, 0}, {3, -1}, {7, 0}}, "m" SUP "M7" PUS },
665 { 3, {{1, 0}, {3, -1}, {7, -1}}, "m7" },
666 { 3, {{1, 0}, {2, 0}, {7, 0}}, "m" SUP "M7" PUS "sus2" },
667 { 3, {{1, 0}, {2, 0}, {7, -1}}, "m7sus2" },
670 { 4, {{1, 0}, {4, 0}, {5, 0}, {6, 0}}, "6sus" },
671 { 4, {{1, 0}, {3, 0}, {5, 0}, {6, 0}}, "6" }, /* Ambiguous with m7 */
672 { 4, {{1, 0}, {3, -1}, {5, 0}, {6, 0}}, "m6" },
673 { 4, {{1, 0}, {2, 0}, {5, 0}, {6, 0}}, "m6sus2" },
676 { 4, {{1, 0}, {4, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS "sus" },
677 { 4, {{1, 0}, {4, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS "sus" },
678 { 4, {{1, 0}, {4, 0}, {5, 0}, {7, 0}}, "M7sus" },
679 { 4, {{1, 0}, {4, 0}, {5, 0}, {7, -1}}, "7sus" },
680 { 4, {{1, 0}, {4, 0}, {5, -1}, {7, -1}}, "7♭5sus" },
681 { 4, {{1, 0}, {3, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS },
682 { 4, {{1, 0}, {3, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS },
683 { 4, {{1, 0}, {3, 0}, {5, 0}, {7, 0}}, "M7" },
684 { 4, {{1, 0}, {3, 0}, {5, 0}, {7, -1}}, "7" },
685 { 4, {{1, 0}, {3, 0}, {5, -1}, {7, -1}}, "7♭5" },
686 { 4, {{1, 0}, {3, -1}, {5, 0}, {7, 0}}, "m" SUP "M7" PUS },
687 { 4, {{1, 0}, {3, -1}, {5, 0}, {7, -1}}, "m7" },
688 { 4, {{1, 0}, {3, -1}, {5, -1}, {7, 0}}, "°" SUP "M7" PUS },
689 { 4, {{1, 0}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "7" PUS },
690 { 4, {{1, 0}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "7" PUS },
691 { 4, {{1, 0}, {2, 0}, {5, 0}, {7, 0}}, "m" SUP "M7" PUS "sus2" },
692 { 4, {{1, 0}, {2, 0}, {5, 0}, {7, -1}}, "m7sus2" },
693 { 4, {{1, 0}, {2, 0}, {5, -1}, {7, 0}}, "°" SUP "M7" PUS "sus2" },
694 { 4, {{1, 0}, {2, 0}, {5, -1}, {7, -1}}, "𝆩" SUP "7" PUS "sus2" },
695 { 4, {{1, 0}, {2, 0}, {5, -1}, {7, -2}}, "°" SUP "7" PUS "sus2" }, /* Ambiguous with 7 */
697 /* The sorting for chords with degree higher than 9 is funny
698 * to keep the array in degree order after reducing each
699 * degree to an actual scale degree, (9 -> 2, 11 -> 4, 13 ->
702 /* Ninth chords voiced with no 5th */
703 { 4, {{1, 0}, {9, 0}, {4, 0}, {7, 0}}, "M9sus" },
704 { 4, {{1, 0}, {9, 0}, {4, 0}, {7, -1}}, "9sus" },
705 { 4, {{1, 0}, {9, 0}, {3, 0}, {7, 0}}, "M9" },
706 { 4, {{1, 0}, {9, 0}, {3, 0}, {7, -1}}, "9" },
707 { 4, {{1, 0}, {9, 0}, {3, -1}, {7, 0}}, "m" SUP "M9" PUS },
708 { 4, {{1, 0}, {9, 0}, {3, -1}, {7, -1}}, "m9" },
709 { 4, {{1, 0}, {9, -1}, {3, -1}, {7, -1}}, "m♭9" },
710 { 4, {{1, 0}, {9, 0}, {3, -1}, {6, 0}}, "m6/9" },
711 { 4, {{1, 0}, {9, -1}, {3, -1}, {6, 0}}, "m6/♭9" },
714 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {6, 0}}, "6/9" },
715 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {6, 0}}, "m6/9" },
716 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, 0}, {6, 0}}, "m6/♭9" },
718 /* Seventh plus altered 9 */
719 { 5, {{1, 0}, {9, +1}, {3, 0}, {5, 0}, {7, -1}}, "7♯9" },
720 { 5, {{1, 0}, {9, -1}, {3, 0}, {5, 0}, {7, -1}}, "7♭9" },
721 { 5, {{1, 0}, {9, +1}, {3, 0}, {5, -1}, {7, -1}}, "7♭5♯9" },
722 { 5, {{1, 0}, {9, -1}, {3, 0}, {5, -1}, {7, -1}}, "7♭5♭9" },
725 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, +1}, {7, 0}}, SUP "+M9" PUS "sus" },
726 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, +1}, {7, -1}}, SUP "+9" PUS "sus" },
727 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, 0}, {7, 0}}, "M9sus" },
728 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, 0}, {7, -1}}, "9sus" },
729 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, +1}, {7, 0}}, SUP "+M9" PUS },
730 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, +1}, {7, -1}}, SUP "+9" PUS },
731 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {7, 0}}, "M9" },
732 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {7, -1}}, "9" },
733 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {7, 0}}, "m" SUP "M9" PUS },
734 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {7, -1}}, "m9" },
735 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, 0}, {7, -1}}, "m♭9" },
736 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, -1}, {7, 0}}, "M9" SUP "♭5" PUS },
737 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, -1}, {7, -1}}, "9" SUP "♭5" PUS },
738 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, 0}}, "m" SUP "M9♭5" PUS },
739 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "9" PUS },
740 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "♭9" PUS },
741 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "9" PUS },
742 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "♭9" PUS },
744 /* Eleventh chords */
745 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, +1}, {7, 0}}, SUP "+M11" PUS },
746 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, +1}, {7, -1}}, SUP "+11" PUS },
747 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, 0}, {7, 0}}, "M11" },
748 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, 0}, {7, -1}}, "11" },
749 { 6, {{1, 0}, {9, 0}, {3, -1}, {11, 0}, {5, 0}, {7, 0}}, "m" SUP "M11" PUS },
750 { 6, {{1, 0}, {9, 0}, {3, -1}, {11, 0}, {5, 0}, {7, -1}}, "m11" },
751 { 6, {{1, 0}, {9, -1}, {3, -1}, {11, 0}, {5, -1}, {7, -1}}, "𝆩" SUP "11" PUS },
752 { 6, {{1, 0}, {9, -1}, {3, -1}, {11, -1}, {5, -1}, {7, -2}}, "°" SUP "11" PUS }
756 scherzo_adjust_pitch_to_match_modified_degree (pitch_t *pitch,
758 modified_degree_t *degree)
760 pitch_name_t name = PITCH_NAME (*pitch);
761 pitch_accidental_t accidental = PITCH_ACCIDENTAL (*pitch);
762 int octave = PITCH_OCTAVE (*pitch);
763 int degree_zero_based = (degree->degree - 1) % 7;
766 int note_degree_zero_based = name - PITCH_NAME (root);
767 if (note_degree_zero_based < 0)
768 note_degree_zero_based += 7;
770 if (note_degree_zero_based == degree_zero_based)
773 degree_delta = note_degree_zero_based - degree_zero_based;
774 if (degree_delta > 3)
775 degree_delta = - (7 - degree_delta);
776 if (degree_delta < -3)
777 degree_delta = - (-7 - degree_delta);
779 if (abs (degree_delta) != 1) {
780 fprintf (stderr, "Internal error: Cannot adjust a note more than one degree (%d vs. %d).\n", note_degree_zero_based + 1, degree->degree);
784 if (degree_delta == -1) {
785 if (name == PITCH_NAME_B) {
806 if (degree_delta == +1) {
807 if (name == PITCH_NAME_C) {
827 if (accidental < 0 || accidental > PITCH_ACCIDENTAL_DOUBLE_SHARP) {
828 fprintf (stderr, "Internal error: Trying to adjust an accidental out of range (degree_delta == %d (%d - %d), new accidental == %d).\n", degree_delta, note_degree_zero_based, degree_zero_based, accidental);
832 *pitch = PITCH (name, accidental, octave);
836 _spell_chord_by_signature (pitch_group_t *chord,
838 chord_signature_t *signature,
841 int i, degree, note_half_steps, degree_half_steps;
844 /* Sanitize root to eliminate things like double-flats,
845 * double-sharps, Cb, E#, etc. */
846 root_midi = _pitch_to_midi (root);
847 root = _midi_to_pitch (root_midi);
849 for (i = 0; i < num_notes; i++) {
850 note_half_steps = _pitch_from_root_in_half_steps (chord->pitches[i],
852 for (degree = 0; degree < signature->num_degrees; degree++) {
853 degree_half_steps = _modified_degree_to_half_steps (&signature->degrees[degree]);
854 if (note_half_steps == degree_half_steps) {
855 scherzo_adjust_pitch_to_match_modified_degree (&chord->pitches[i],
857 &signature->degrees[degree]);
861 if (note_half_steps != degree_half_steps) {
862 fprintf (stderr, "Internal error: Chord and degree mis-match\n");
869 scherzo_analyze_chord (scherzo_t *scherzo)
871 void *local = talloc_new (NULL);
872 analyzed_pitch_t *pitches;
873 pitch_group_t *pitch_group;
874 unsigned i, j, num_pitches;
875 int bass_pitch, inversion;
877 chord_signature_t *match = NULL;
880 if (scherzo->pedal_pressed)
881 pitch_group = &scherzo->notes_pedaled;
883 pitch_group = &scherzo->notes_pressed;
885 num_pitches = pitch_group->num_pitches;
887 if (scherzo->chord) {
888 score_remove_chord (scherzo->chord);
889 scherzo->chord = NULL;
892 if (num_pitches <= 1)
895 pitches = talloc_array (local, analyzed_pitch_t, num_pitches);
899 for (i = 0; i < num_pitches; i++) {
900 pitch_t pitch = pitch_group->pitches[i];
901 pitches[i].pitch = pitch;
902 pitches[i].midi_pitch = _pitch_to_midi (pitch);
903 /* Relative pitch will be filled in after sorting. */
904 pitches[i].relative_pitch = 0;
907 /* First, sort by midi pitch to find the bass note. */
908 qsort (pitches, num_pitches, sizeof (analyzed_pitch_t),
909 _compare_analyzed_pitch_by_midi_pitch);
911 bass_pitch = pitches[0].midi_pitch;
913 /* With the bass note identified, we can find all relative pitches. */
914 for (i = 0; i < num_pitches; i++) {
915 pitches[i].relative_pitch = pitches[i].midi_pitch - bass_pitch;
916 while (pitches[i].relative_pitch >= 12)
917 pitches[i].relative_pitch -= 12;
920 /* Now, sort again by relative pitch. */
921 qsort (pitches, num_pitches, sizeof (analyzed_pitch_t),
922 _compare_analyzed_pitch_by_relative_pitch);
924 /* Finally, eliminate all duplicate notes. */
925 for (i = 0; i < num_pitches - 1; i++) {
926 if (pitches[i+1].relative_pitch == pitches[i].relative_pitch) {
928 while (j < num_pitches &&
929 pitches[j].relative_pitch == pitches[i].relative_pitch)
933 /* The loop incremented j one past the last
934 * duplicate. Decrement so that it points to the
935 * last duplicate (and is guaranteed to not exceed
936 * the array bounds).*/
939 if (j < num_pitches - 1) {
940 memmove (&pitches[i+1], &pitches[j+1],
941 (num_pitches - j) * sizeof (analyzed_pitch_t));
944 num_pitches -= (j - i);
948 for (inversion = 0; inversion < 4; inversion++) {
949 for (i = 0; i < ARRAY_SIZE (signatures); i++) {
950 if (_chord_signature_matches (pitches, num_pitches,
951 signatures[i].degrees,
952 signatures[i].num_degrees,
955 match = &signatures[i];
962 chord_name = (char *) signatures[i].name;
965 /* Don't print root pitch for octaves and inversions,
966 * (since a pitch name alone looks like a major triad) */
967 if (num_pitches < 3) {
968 chord_name = talloc_strdup (local, chord_name);
970 chord_name = talloc_asprintf (local, "%s%s",
975 _spell_chord_by_signature (pitch_group, num_pitches,
978 chord_name = talloc_strdup (local, "?");
981 scherzo->chord = score_add_chord (scherzo->treble, chord_name);
988 pitch_group_init (void *ctx, pitch_group_t *group)
991 group->pitches = NULL;
993 group->num_pitches = 0;
997 pitch_group_add_pitch (pitch_group_t *group, pitch_t pitch)
1001 /* Do nothing if pitch is already in group. */
1002 for (i = 0; i < group->num_pitches; i++)
1003 if (group->pitches[i] == pitch)
1006 group->num_pitches++;
1008 if (group->num_pitches > group->size) {
1010 group->pitches = talloc_realloc (group->ctx, group->pitches,
1011 pitch_t, group->size);
1013 if (group->pitches == NULL) {
1014 fprintf (stderr, "Out of memory.\n");
1019 group->pitches[group->num_pitches - 1] = pitch;
1023 pitch_group_remove_pitch_at (pitch_group_t *group, int i)
1025 if (i > group->num_pitches) {
1026 fprintf (stderr, "Internal error: Cannot remove pitch %d from group with only %d %s\n",
1027 i, group->num_pitches,
1028 group->num_pitches == 1 ? "pitch" : "pitches");
1032 if (i < group->num_pitches - 1) {
1033 memmove (group->pitches + i, group->pitches + i + 1,
1034 (group->num_pitches - 1 - i) * sizeof (pitch_t));
1037 group->num_pitches--;
1041 pitch_group_remove_pitch (pitch_group_t *group, pitch_t pitch)
1045 for (i = 0; i < group->num_pitches; i++)
1046 if (group->pitches[i] == pitch)
1047 pitch_group_remove_pitch_at (group, i);
1051 scherzo_update_notes_and_chord (scherzo_t *scherzo)
1053 if (scherzo->notes_pressed.num_pitches == 0 &&
1054 scherzo->notes_pedaled.num_pitches == 0)
1056 score_remove_notes (scherzo->score);
1059 score_remove_chord (scherzo->chord);
1061 gtk_widget_queue_draw (scherzo->window);
1066 scherzo_press_note (scherzo_t *scherzo, pitch_t pitch)
1070 /* Do nothing if this note is already pressed. */
1071 for (i = 0; i < scherzo->notes_pressed.num_pitches; i++)
1072 if (scherzo->notes_pressed.pitches[i] == pitch)
1075 pitch_group_add_pitch (&scherzo->notes_pressed, pitch);
1077 if (scherzo->pedal_pressed)
1078 pitch_group_add_pitch (&scherzo->notes_pedaled, pitch);
1080 /* Remove all notes from score, then add current notes. */
1081 score_remove_notes (scherzo->score);
1083 for (i = 0; i < scherzo->notes_pressed.num_pitches; i++)
1085 score_add_note (scherzo->score, scherzo->notes_pressed.pitches[i],
1086 SCORE_DURATION_WHOLE);
1089 for (i = 0; i < scherzo->notes_pedaled.num_pitches; i++)
1091 score_add_note (scherzo->score, scherzo->notes_pedaled.pitches[i],
1092 SCORE_DURATION_WHOLE);
1095 scherzo_analyze_chord (scherzo);
1099 scherzo_release_note (scherzo_t *scherzo, pitch_t pitch)
1101 pitch_group_remove_pitch (&scherzo->notes_pressed, pitch);
1103 if (scherzo->notes_pressed.num_pitches == 0)
1104 scherzo_update_notes_and_chord (scherzo);
1108 scherzo_press_note_midi (scherzo_t *scherzo, unsigned char midi_note)
1110 pitch_t pitch = _midi_to_pitch (midi_note);
1112 scherzo_press_note (scherzo, pitch);
1118 scherzo_release_note_midi (scherzo_t *scherzo, unsigned char midi_note)
1120 scherzo_release_note (scherzo, _midi_to_pitch (midi_note));
1124 scherzo_press_pedal (scherzo_t *scherzo)
1128 scherzo->pedal_pressed = 1;
1130 /* Copy all pressed notes to pedaled notes */
1131 for (i = 0; i < scherzo->notes_pressed.num_pitches; i++)
1132 pitch_group_add_pitch (&scherzo->notes_pedaled, scherzo->notes_pressed.pitches[i]);
1136 scherzo_release_pedal (scherzo_t *scherzo)
1140 /* Remove all previously pedaled notes. */
1141 for (i = scherzo->notes_pedaled.num_pitches - 1; i >=0; i--)
1142 pitch_group_remove_pitch_at (&scherzo->notes_pedaled, i);
1144 scherzo->pedal_pressed = 0;
1146 scherzo_update_notes_and_chord (scherzo);
1150 _select_challenge (scherzo_t *scherzo)
1152 category_t *category_unused;
1153 bool_t introduced_unused;
1155 challenge_t *challenge = &scherzo->challenge;
1156 pitch_name_t pitch_name;
1161 if (challenge->note) {
1162 score_remove_note (challenge->note);
1163 challenge->note = NULL;
1166 mnemon_select_item (&scherzo->mnemon,
1168 &challenge->item_index,
1170 &introduced_unused);
1172 item = challenge->bin->items[challenge->item_index];
1174 s = item->challenge;
1175 if (strncmp (s, "treble:", 7) == 0) {
1177 challenge->staff = scherzo->treble;
1178 } else if (strncmp (s, "bass:", 5) == 0) {
1180 challenge->staff = scherzo->bass;
1183 "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
1190 pitch_name = PITCH_NAME_C;
1193 pitch_name = PITCH_NAME_D;
1196 pitch_name = PITCH_NAME_E;
1199 pitch_name = PITCH_NAME_F;
1202 pitch_name = PITCH_NAME_G;
1205 pitch_name = PITCH_NAME_A;
1208 pitch_name = PITCH_NAME_B;
1211 fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
1216 if (*s < '0' || *s > '9') {
1217 fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
1223 pitch = PITCH (pitch_name, PITCH_ACCIDENTAL_NATURAL, octave);
1225 challenge->note = score_staff_add_note (challenge->staff, pitch,
1226 SCORE_DURATION_WHOLE);
1227 challenge->satisfied = 0;
1228 challenge->mistaken = 0;
1231 /* Determine whether the user hit the correct note. */
1233 _judge_pitch (scherzo_t *scherzo, pitch_t pitch)
1235 challenge_t *challenge = &scherzo->challenge;
1237 if (! challenge->note)
1240 if (pitch == challenge->note->pitch)
1241 challenge->satisfied = 1;
1243 challenge->mistaken = 1;
1246 /* If the user got the right note (eventually), then score it in
1247 * mnemon and show the next note. */
1249 _score_challenge (scherzo_t *scherzo)
1251 challenge_t *challenge = &scherzo->challenge;
1253 if (! challenge->note)
1256 if (! challenge->satisfied)
1259 mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
1260 ! challenge->mistaken);
1262 _select_challenge (scherzo);
1266 on_midi_input (unused (GIOChannel *channel),
1267 unused (GIOCondition condition),
1270 unsigned char buf[MIDI_BUF_SIZE], *next;
1271 scherzo_t *scherzo = user_data;
1273 snd_seq_event_t event;
1275 int need_redraw = FALSE;
1277 remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
1283 consumed = snd_midi_event_encode (scherzo->snd_midi_event,
1284 next, remaining, &event);
1286 remaining -= consumed;
1289 switch (event.type) {
1290 case SND_SEQ_EVENT_NONE:
1291 /* Incomplete event. Nothing to do. */
1293 case SND_SEQ_EVENT_NOTEON:
1294 pitch = scherzo_press_note_midi (scherzo, event.data.note.note);
1295 _judge_pitch (scherzo, pitch);
1298 case SND_SEQ_EVENT_NOTEOFF:
1299 scherzo_release_note_midi (scherzo, event.data.note.note);
1300 _score_challenge (scherzo);
1303 case SND_SEQ_EVENT_CLOCK:
1304 /* Ignore for now as my piano sends a constant stream of these. */
1306 case SND_SEQ_EVENT_SENSING:
1307 /* Ignore for now as my piano sends a constant stream of these. */
1309 case SND_SEQ_EVENT_CONTROLLER:
1310 /* XXX: My piano gives me 64 for the sustain pedal, is
1311 * that universal? */
1312 if (event.data.control.param == 64) {
1313 if (event.data.control.value == 0)
1314 scherzo_release_pedal (scherzo);
1316 scherzo_press_pedal (scherzo);
1318 fprintf (stderr, "Fixme: Unhandled MIDI Control event (param=%d, value=%d)\n",
1319 event.data.control.param, event.data.control.value);
1323 fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
1330 gtk_widget_queue_draw (scherzo->window);
1332 /* Return TRUE to continue to get called in the future. */
1337 main (int argc, char *argv[])
1339 GtkWidget *drawing_area;
1343 srand (time (NULL));
1345 gtk_init (&argc, &argv);
1347 scherzo.ctx = talloc_new (NULL);
1349 scherzo.score = score_create (scherzo.ctx);
1350 scherzo.staff_height = 100;
1351 score_set_staff_height (scherzo.score, scherzo.staff_height);
1353 score_add_brace (scherzo.score, 2);
1354 scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
1355 scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
1357 scherzo.chord = NULL;
1359 /* Default to octave 4 and natural for computer keyboard keypresses. */
1360 scherzo.keyboard_octave = 4;
1361 scherzo.keyboard_accidental = PITCH_ACCIDENTAL_NATURAL;
1363 pitch_group_init (scherzo.ctx, &scherzo.notes_pressed);
1364 pitch_group_init (scherzo.ctx, &scherzo.notes_pedaled);
1366 scherzo.pedal_pressed = 0;
1368 mnemon_init (&scherzo.mnemon);
1369 /* XXX: Should create a default file if one cannot be loaded. */
1370 mnemon_load_category (&scherzo.mnemon, "scherzo-notes");
1372 scherzo.challenge.note = NULL;
1374 _select_challenge (&scherzo);
1377 err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
1379 fprintf (stderr, "Out of memory.\n");
1383 #define MIDI_DEVICE "/dev/midi1"
1384 scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
1385 if (scherzo.midi_fd < 0) {
1386 printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
1388 GIOChannel *channel;
1390 channel = g_io_channel_unix_new (scherzo.midi_fd);
1391 g_io_channel_set_encoding (channel, NULL, NULL);
1392 g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
1395 scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1397 gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 1000, 600);
1399 g_signal_connect (scherzo.window, "delete-event",
1400 G_CALLBACK (on_delete_event_quit), NULL);
1402 drawing_area = gtk_drawing_area_new ();
1404 gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
1406 g_signal_connect (drawing_area, "expose-event",
1407 G_CALLBACK (on_expose_event_draw),
1410 g_signal_connect (scherzo.window, "key-press-event",
1411 G_CALLBACK (on_key_press_event),
1414 g_signal_connect (scherzo.window, "key-release-event",
1415 G_CALLBACK (on_key_release_event),
1418 gtk_widget_show_all (scherzo.window);
1422 mnemon_save (&scherzo.mnemon);
1424 snd_midi_event_free (scherzo.snd_midi_event);
1426 talloc_free (scherzo.ctx);