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 note_group
52 typedef struct scherzo
59 score_staff_t *treble;
63 /* The word "keyboard" here is referring to a "computer
64 * keyboard". Any "piano keyboard" key knows its own octave and
65 * accidental already. */
67 pitch_accidental_t keyboard_accidental;
70 snd_midi_event_t *snd_midi_event;
73 challenge_t challenge;
75 note_group_t notes_pressed;
76 note_group_t notes_pedaled;
81 /* Forward declarations. */
83 scherzo_press_note (scherzo_t *scherzo, pitch_t pitch, int octave);
86 scherzo_release_note (scherzo_t *scherzo, pitch_t pitch, int octave);
89 scherzo_press_pedal (scherzo_t *scherzo);
92 scherzo_release_pedal (scherzo_t *scherzo);
95 _judge_note (scherzo_t *scherzo, score_note_t *note);
98 _score_challenge (scherzo_t *scherzo);
101 on_delete_event_quit (unused (GtkWidget *widget),
102 unused (GdkEvent *event),
103 unused (gpointer user_data))
107 /* Returning FALSE allows the default handler for delete-event
108 * to proceed to cleanup the widget. */
113 on_expose_event_draw (GtkWidget *widget,
114 unused (GdkEventExpose *expose),
117 scherzo_t *scherzo = user_data;
118 score_t *score = scherzo->score;
120 GtkAllocation allocation;
121 static const int pad = 10;
124 gtk_widget_get_allocation (widget, &allocation);
125 widget_width = allocation.width;
127 cr = gdk_cairo_create (widget->window);
129 /* White background */
130 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
133 /* Add some padding on the sides and top */
134 cairo_translate (cr, pad, scherzo->staff_height);
135 score_set_staff_height (score, scherzo->staff_height);
136 score_set_width (score, widget_width - 2 * pad);
138 score_draw (score, cr);
144 on_key_press_event (GtkWidget *widget,
148 scherzo_t *scherzo = user_data;
150 /* Initialize to keep the compiler quiet. */
151 pitch_name_t pitch_name = PITCH_NATURAL (C);
154 if (scherzo->challenge.note)
155 octave = scherzo->challenge.note->octave;
157 octave = scherzo->keyboard_octave;
159 switch (key->keyval) {
163 case GDK_KEY_KP_Equal:
164 scherzo->staff_height += 4;
165 gtk_widget_queue_draw (widget);
169 case GDK_KEY_KP_Subtract:
170 scherzo->staff_height -= 4;
171 gtk_widget_queue_draw (widget);
181 pitch_name = PITCH_NAME_C;
185 pitch_name = PITCH_NAME_D;
189 pitch_name = PITCH_NAME_E;
193 pitch_name = PITCH_NAME_F;
197 pitch_name = PITCH_NAME_G;
201 pitch_name = PITCH_NAME_A;
205 pitch_name = PITCH_NAME_B;
216 scherzo->keyboard_octave = key->keyval - GDK_KEY_0;
219 scherzo_press_pedal (scherzo);
222 if (scherzo->keyboard_accidental < PITCH_ACCIDENTAL_DOUBLE_SHARP)
223 scherzo->keyboard_accidental++;
226 if (scherzo->keyboard_accidental > PITCH_ACCIDENTAL_DOUBLE_FLAT)
227 scherzo->keyboard_accidental--;
231 pitch = PITCH (pitch_name, scherzo->keyboard_accidental);
233 if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) ||
234 (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g))
238 note = scherzo_press_note (scherzo, pitch, octave);
239 _judge_note (scherzo, note);
240 gtk_widget_queue_draw (scherzo->window);
246 /* Allow an unhandled event to propagate to other handlers. */
251 on_key_release_event (unused (GtkWidget *widget),
255 scherzo_t *scherzo = user_data;
257 /* Initialize to keep the compiler quiet. */
258 pitch_name_t pitch_name = PITCH_NAME_C;
261 if (scherzo->challenge.note)
262 octave = scherzo->challenge.note->octave;
264 octave = scherzo->keyboard_octave;
266 switch (key->keyval) {
269 pitch_name = PITCH_NAME_C;
273 pitch_name = PITCH_NAME_D;
277 pitch_name = PITCH_NAME_E;
281 pitch_name = PITCH_NAME_F;
285 pitch_name = PITCH_NAME_G;
289 pitch_name = PITCH_NAME_A;
293 pitch_name = PITCH_NAME_B;
296 scherzo_release_pedal (scherzo);
300 pitch = PITCH (pitch_name, scherzo->keyboard_accidental);
302 if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) ||
303 (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g))
305 scherzo_release_note (scherzo, pitch, octave);
306 _score_challenge (scherzo);
307 gtk_widget_queue_draw (scherzo->window);
313 /* Allow an unhandled event to propagate to other handlers. */
318 _pitch_and_octave_to_midi (pitch_t pitch,
321 unsigned char midi_note = 12 * (octave + 1);
323 switch (PITCH_NAME (pitch)) {
346 switch (PITCH_ACCIDENTAL (pitch)) {
347 case PITCH_ACCIDENTAL_DOUBLE_FLAT:
350 case PITCH_ACCIDENTAL_FLAT:
353 case PITCH_ACCIDENTAL_NATURAL:
355 case PITCH_ACCIDENTAL_SHARP:
358 case PITCH_ACCIDENTAL_DOUBLE_SHARP:
366 /* octave can optionally by NULL */
368 _midi_to_pitch_and_octave (unsigned char midi_note,
373 *octave = midi_note / 12 - 1;
375 switch (midi_note % 12)
378 *pitch = PITCH_LITERAL (C, NATURAL);
381 *pitch = PITCH_LITERAL (C, SHARP);
384 *pitch = PITCH_LITERAL (D, NATURAL);
387 *pitch = PITCH_LITERAL (D, SHARP);
390 *pitch = PITCH_LITERAL (E, NATURAL);
393 *pitch = PITCH_LITERAL (F, NATURAL);
396 *pitch = PITCH_LITERAL (F, SHARP);
399 *pitch = PITCH_LITERAL (G, NATURAL);
402 *pitch = PITCH_LITERAL (G, SHARP);
405 *pitch = PITCH_LITERAL (A, NATURAL);
408 *pitch = PITCH_LITERAL (A, SHARP);
411 *pitch = PITCH_LITERAL (B, NATURAL);
416 /* Determine a chord name (if any) from the current notes pressed */
418 typedef struct analyzed_note {
419 /* Original note being analzyed. */
422 /* Absolute pitch (expressed as midi number). */
425 /* Pitch relative to bass note. */
430 _compare_analyzed_note_by_midi_pitch (const void *va, const void *vb)
432 const analyzed_note_t *a = va, *b = vb;
434 return a->midi_pitch - b->midi_pitch;
438 _compare_analyzed_note_by_relative_pitch (const void *va, const void *vb)
440 const analyzed_note_t *a = va, *b = vb;
442 return a->relative_pitch - b->relative_pitch;
445 typedef struct modified_degree
452 _modified_degree_to_half_steps (const modified_degree_t *degree)
455 int scale_degree = degree->degree;
457 /* Restrict to actual degrees within a scale. */
458 if (scale_degree > 7)
459 scale_degree = (scale_degree % 8) + 1;
461 /* Number of half steps from root to specified degree within a
463 switch (scale_degree) {
486 fprintf (stderr, "Internal: Invalid degree %d\n", degree->degree);
491 return half_steps + degree->modification;
494 /* Number of half steps from 'root' up to 'pitch' (that is, counting
495 * the steps up a chromatic scale). */
497 _pitch_from_root_in_half_steps (pitch_t pitch, pitch_t root)
499 int root_midi = _pitch_and_octave_to_midi (root, 0);
500 int pitch_midi = _pitch_and_octave_to_midi (pitch, 0);
502 if (pitch_midi < root_midi)
505 if (pitch_midi < root_midi) {
506 fprintf (stderr, "Internal error: midi values differ by more than expected.\n");
510 return (pitch_midi - root_midi) % 12;
514 _chord_signature_matches (analyzed_note_t *notes,
516 modified_degree_t *degrees,
521 #define MAX_DEGREES 6
522 int relative_pitches[MAX_DEGREES];
525 assert (num_degrees <= MAX_DEGREES);
527 if (num_notes != num_degrees)
530 if (inversion >= num_degrees)
533 /* We never spell simple intervals as inversions. */
534 if (num_degrees == 2 && inversion > 0)
537 for (i = 0; i < num_degrees; i++) {
538 /* The num_degrees is in the addition just to ensure all
539 * inputs to the modulus operator remain positive. */
540 int index = (i + num_degrees - inversion) % num_degrees;
542 /* Again, adding a 12 to keep things positive. */
543 relative_pitches[index] =
545 _modified_degree_to_half_steps (°rees[i]) -
546 _modified_degree_to_half_steps (°rees[inversion])) % 12;
550 for (i = 0; i < num_notes; i++)
551 if (notes[i].relative_pitch != relative_pitches[i])
554 root_index = (num_notes - inversion) % num_notes;
555 *root = notes[root_index].note->pitch;
561 _pitch_str (pitch_t pitch)
563 static char double_flat[] = "X𝄫";
564 static char flat[] = "X♭";
565 static char natural[] = "X";
566 static char sharp[] = "X♯";
567 static char double_sharp[] = "X𝄪";
570 switch (PITCH_ACCIDENTAL (pitch)) {
571 case PITCH_ACCIDENTAL_DOUBLE_FLAT:
574 case PITCH_ACCIDENTAL_FLAT:
577 case PITCH_ACCIDENTAL_NATURAL:
580 case PITCH_ACCIDENTAL_SHARP:
583 case PITCH_ACCIDENTAL_DOUBLE_SHARP:
588 switch (PITCH_NAME(pitch)) {
615 typedef struct chord_signature
618 modified_degree_t degrees[MAX_DEGREES];
622 /* XXX: The superscript rise value should be relative to the current
623 * font size. Best would be for pango to support this. See:
625 * https://bugzilla.gnome.org/show_bug.cgi?id=708780
627 * For now, these are emipirically-derived values that look reasonable
628 * at the default size that score.c is using to draw the staff. If the
629 * user scales the staff up or down then these will look wrong.
632 #define SUP "<span font='33' rise='30000'>"
633 #define PUS "</span>"
635 chord_signature_t signatures[] = {
637 { 1, {{1, 0}}, "P8" },
640 { 2, {{1, 0}, {2, -1}}, "m2" },
641 { 2, {{1, 0}, {2, 0}}, "M2" },
642 { 2, {{1, 0}, {3, -1}}, "m3" },
643 { 2, {{1, 0}, {3, 0}}, "M3" },
644 { 2, {{1, 0}, {4, 0}}, "P4" },
645 { 2, {{1, 0}, {5, -1}}, "Tri-tone" },
646 { 2, {{1, 0}, {5, 0}}, "P5" },
647 { 2, {{1, 0}, {6, -1}}, "m6" },
648 { 2, {{1, 0}, {6, 0}}, "M6" },
649 { 2, {{1, 0}, {7, -1}}, "m7" },
650 { 2, {{1, 0}, {7, 0}}, "M7" },
653 { 3, {{1, 0}, {4, 0}, {5, 0}}, "sus" },
654 { 3, {{1, 0}, {3, 0}, {5, +1}}, SUP "+" PUS },
655 { 3, {{1, 0}, {3, 0}, {5, 0}}, "" },
656 { 3, {{1, 0}, {3, -1}, {5, 0}}, "m" },
657 { 3, {{1, 0}, {3, -1}, {5, -1}}, "°" },
658 { 3, {{1, 0}, {2, 0}, {5, 0}}, "msus2" },
660 /* Seventh chords with no 3rd */
661 { 3, {{1, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS },
662 { 3, {{1, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS },
663 { 3, {{1, 0}, {5, 0}, {7, 0}}, "M7" },
664 { 3, {{1, 0}, {5, 0}, {7, -1}}, "7" },
665 { 3, {{1, 0}, {5, -1}, {7, -1}}, "7♭5" },
667 /* Seventh chords with no 5th */
668 { 3, {{1, 0}, {4, 0}, {7, 0}}, "M7sus" },
669 { 3, {{1, 0}, {4, 0}, {7, -1}}, "7sus" },
670 { 3, {{1, 0}, {3, 0}, {7, 0}}, "M7" },
671 { 3, {{1, 0}, {3, 0}, {7, -1}}, "7" },
672 { 3, {{1, 0}, {3, -1}, {7, 0}}, "m" SUP "M7" PUS },
673 { 3, {{1, 0}, {3, -1}, {7, -1}}, "m7" },
674 { 3, {{1, 0}, {2, 0}, {7, 0}}, "m" SUP "M7" PUS "sus2" },
675 { 3, {{1, 0}, {2, 0}, {7, -1}}, "m7sus2" },
678 { 4, {{1, 0}, {4, 0}, {5, 0}, {6, 0}}, "6sus" },
679 { 4, {{1, 0}, {3, 0}, {5, 0}, {6, 0}}, "6" }, /* Ambiguous with m7 */
680 { 4, {{1, 0}, {3, -1}, {5, 0}, {6, 0}}, "m6" },
681 { 4, {{1, 0}, {2, 0}, {5, 0}, {6, 0}}, "m6sus2" },
684 { 4, {{1, 0}, {4, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS "sus" },
685 { 4, {{1, 0}, {4, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS "sus" },
686 { 4, {{1, 0}, {4, 0}, {5, 0}, {7, 0}}, "M7sus" },
687 { 4, {{1, 0}, {4, 0}, {5, 0}, {7, -1}}, "7sus" },
688 { 4, {{1, 0}, {4, 0}, {5, -1}, {7, -1}}, "7♭5sus" },
689 { 4, {{1, 0}, {3, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS },
690 { 4, {{1, 0}, {3, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS },
691 { 4, {{1, 0}, {3, 0}, {5, 0}, {7, 0}}, "M7" },
692 { 4, {{1, 0}, {3, 0}, {5, 0}, {7, -1}}, "7" },
693 { 4, {{1, 0}, {3, 0}, {5, -1}, {7, -1}}, "7♭5" },
694 { 4, {{1, 0}, {3, -1}, {5, 0}, {7, 0}}, "m" SUP "M7" PUS },
695 { 4, {{1, 0}, {3, -1}, {5, 0}, {7, -1}}, "m7" },
696 { 4, {{1, 0}, {3, -1}, {5, -1}, {7, 0}}, "°" SUP "M7" PUS },
697 { 4, {{1, 0}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "7" PUS },
698 { 4, {{1, 0}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "7" PUS },
699 { 4, {{1, 0}, {2, 0}, {5, 0}, {7, 0}}, "m" SUP "M7" PUS "sus2" },
700 { 4, {{1, 0}, {2, 0}, {5, 0}, {7, -1}}, "m7sus2" },
701 { 4, {{1, 0}, {2, 0}, {5, -1}, {7, 0}}, "°" SUP "M7" PUS "sus2" },
702 { 4, {{1, 0}, {2, 0}, {5, -1}, {7, -1}}, "𝆩" SUP "7" PUS "sus2" },
703 { 4, {{1, 0}, {2, 0}, {5, -1}, {7, -2}}, "°" SUP "7" PUS "sus2" }, /* Ambiguous with 7 */
705 /* The sorting for chords with degree higher than 9 is funny
706 * to keep the array in degree order after reducing each
707 * degree to an actual scale degree, (9 -> 2, 11 -> 4, 13 ->
710 /* Ninth chords voiced with no 5th */
711 { 4, {{1, 0}, {9, 0}, {4, 0}, {7, 0}}, "M9sus" },
712 { 4, {{1, 0}, {9, 0}, {4, 0}, {7, -1}}, "9sus" },
713 { 4, {{1, 0}, {9, 0}, {3, 0}, {7, 0}}, "M9" },
714 { 4, {{1, 0}, {9, 0}, {3, 0}, {7, -1}}, "9" },
715 { 4, {{1, 0}, {9, 0}, {3, -1}, {7, 0}}, "m" SUP "M9" PUS },
716 { 4, {{1, 0}, {9, 0}, {3, -1}, {7, -1}}, "m9" },
717 { 4, {{1, 0}, {9, -1}, {3, -1}, {7, -1}}, "m♭9" },
718 { 4, {{1, 0}, {9, 0}, {3, -1}, {6, 0}}, "m6/9" },
719 { 4, {{1, 0}, {9, -1}, {3, -1}, {6, 0}}, "m6/♭9" },
722 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {6, 0}}, "6/9" },
723 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {6, 0}}, "m6/9" },
724 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, 0}, {6, 0}}, "m6/♭9" },
726 /* Seventh plus altered 9 */
727 { 5, {{1, 0}, {9, +1}, {3, 0}, {5, 0}, {7, -1}}, "7♯9" },
728 { 5, {{1, 0}, {9, -1}, {3, 0}, {5, 0}, {7, -1}}, "7♭9" },
729 { 5, {{1, 0}, {9, +1}, {3, 0}, {5, -1}, {7, -1}}, "7♭5♯9" },
730 { 5, {{1, 0}, {9, -1}, {3, 0}, {5, -1}, {7, -1}}, "7♭5♭9" },
733 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, +1}, {7, 0}}, SUP "+M9" PUS "sus" },
734 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, +1}, {7, -1}}, SUP "+9" PUS "sus" },
735 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, 0}, {7, 0}}, "M9sus" },
736 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, 0}, {7, -1}}, "9sus" },
737 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, +1}, {7, 0}}, SUP "+M9" PUS },
738 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, +1}, {7, -1}}, SUP "+9" PUS },
739 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {7, 0}}, "M9" },
740 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {7, -1}}, "9" },
741 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {7, 0}}, "m" SUP "M9" PUS },
742 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {7, -1}}, "m9" },
743 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, 0}, {7, -1}}, "m♭9" },
744 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, -1}, {7, 0}}, "M9" SUP "♭5" PUS },
745 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, -1}, {7, -1}}, "9" SUP "♭5" PUS },
746 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, 0}}, "m" SUP "M9♭5" PUS },
747 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "9" PUS },
748 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "♭9" PUS },
749 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "9" PUS },
750 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "♭9" PUS },
752 /* Eleventh chords */
753 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, +1}, {7, 0}}, SUP "+M11" PUS },
754 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, +1}, {7, -1}}, SUP "+11" PUS },
755 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, 0}, {7, 0}}, "M11" },
756 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, 0}, {7, -1}}, "11" },
757 { 6, {{1, 0}, {9, 0}, {3, -1}, {11, 0}, {5, 0}, {7, 0}}, "m" SUP "M11" PUS },
758 { 6, {{1, 0}, {9, 0}, {3, -1}, {11, 0}, {5, 0}, {7, -1}}, "m11" },
759 { 6, {{1, 0}, {9, -1}, {3, -1}, {11, 0}, {5, -1}, {7, -1}}, "𝆩" SUP "11" PUS },
760 { 6, {{1, 0}, {9, -1}, {3, -1}, {11, -1}, {5, -1}, {7, -2}}, "°" SUP "11" PUS }
764 scherzo_adjust_note_to_match_modified_degree (score_note_t *note,
766 modified_degree_t *degree)
768 pitch_name_t name = PITCH_NAME (note->pitch);
769 pitch_accidental_t accidental = PITCH_ACCIDENTAL (note->pitch);
770 int degree_zero_based = (degree->degree - 1) % 7;
773 int note_degree_zero_based = name - PITCH_NAME (root);
774 if (note_degree_zero_based < 0)
775 note_degree_zero_based += 7;
777 if (note_degree_zero_based == degree_zero_based)
780 degree_delta = note_degree_zero_based - degree_zero_based;
781 if (degree_delta > 3)
782 degree_delta = - (7 - degree_delta);
783 if (degree_delta < -3)
784 degree_delta = - (-7 - degree_delta);
786 if (abs (degree_delta) != 1) {
787 fprintf (stderr, "Internal error: Cannot adjust a note more than one degree (%d vs. %d).\n", note_degree_zero_based + 1, degree->degree);
791 if (degree_delta == -1) {
792 if (name == PITCH_NAME_B) {
813 if (degree_delta == +1) {
814 if (name == PITCH_NAME_C) {
834 if (accidental < 0 || accidental > PITCH_ACCIDENTAL_DOUBLE_SHARP) {
835 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);
839 note->pitch = PITCH (name, accidental);
843 _spell_chord_by_signature (note_group_t *chord,
845 chord_signature_t *signature,
848 int i, degree, note_half_steps, degree_half_steps;
851 /* Sanitize root to eliminate things like double-flats,
852 * double-sharps, Cb, E#, etc. */
853 root_midi = _pitch_and_octave_to_midi (root, 0);
854 _midi_to_pitch_and_octave (root_midi, &root, NULL);
856 for (i = 0; i < num_notes; i++) {
857 note_half_steps = _pitch_from_root_in_half_steps (chord->notes[i]->pitch,
859 for (degree = 0; degree < signature->num_degrees; degree++) {
860 degree_half_steps = _modified_degree_to_half_steps (&signature->degrees[degree]);
861 if (note_half_steps == degree_half_steps) {
862 scherzo_adjust_note_to_match_modified_degree (chord->notes[i],
864 &signature->degrees[degree]);
868 if (note_half_steps != degree_half_steps) {
869 fprintf (stderr, "Internal error: Chord and degree mis-match\n");
876 scherzo_analyze_chord (scherzo_t *scherzo)
878 void *local = talloc_new (NULL);
879 analyzed_note_t *notes;
880 note_group_t *note_group;
881 unsigned i, j, num_notes;
882 int bass_pitch, inversion;
884 chord_signature_t *match = NULL;
887 if (scherzo->pedal_pressed)
888 note_group = &scherzo->notes_pedaled;
890 note_group = &scherzo->notes_pressed;
892 num_notes = note_group->num_notes;
894 if (scherzo->chord) {
895 score_remove_chord (scherzo->chord);
896 scherzo->chord = NULL;
902 notes = talloc_array (local, analyzed_note_t, num_notes);
906 for (i = 0; i < num_notes; i++) {
907 score_note_t *note = note_group->notes[i];
908 notes[i].note = note;
909 notes[i].midi_pitch = _pitch_and_octave_to_midi (note->pitch,
911 /* Relative pitch will be filled in after sorting. */
912 notes[i].relative_pitch = 0;
915 /* First, sort by midi pitch to find the bass note. */
916 qsort (notes, num_notes, sizeof (analyzed_note_t),
917 _compare_analyzed_note_by_midi_pitch);
919 bass_pitch = notes[0].midi_pitch;
921 /* With the bass note identified, we can find all relative pitches. */
922 for (i = 0; i < num_notes; i++) {
923 notes[i].relative_pitch = notes[i].midi_pitch - bass_pitch;
924 while (notes[i].relative_pitch >= 12)
925 notes[i].relative_pitch -= 12;
928 /* Now, sort again by relative pitch. */
929 qsort (notes, num_notes, sizeof (analyzed_note_t),
930 _compare_analyzed_note_by_relative_pitch);
932 /* Finally, eliminate all duplicate notes. */
933 for (i = 0; i < num_notes - 1; i++) {
934 if (notes[i+1].relative_pitch == notes[i].relative_pitch) {
936 while (j < num_notes &&
937 notes[j].relative_pitch == notes[i].relative_pitch)
941 /* The loop incremented j one past the last
942 * duplicate. Decrement so that it points to the
943 * last duplicate (and is guaranteed to not exceed
944 * the array bounds).*/
947 if (j < num_notes - 1) {
948 memmove (¬es[i+1], ¬es[j+1],
949 (num_notes - j) * sizeof (analyzed_note_t));
952 num_notes -= (j - i);
956 for (inversion = 0; inversion < 4; inversion++) {
957 for (i = 0; i < ARRAY_SIZE (signatures); i++) {
958 if (_chord_signature_matches (notes, num_notes,
959 signatures[i].degrees,
960 signatures[i].num_degrees,
963 match = &signatures[i];
970 chord_name = (char *) signatures[i].name;
973 /* Don't print root pitch for octaves and inversions,
974 * (since a pitch name alone looks like a major triad) */
976 chord_name = talloc_strdup (local, chord_name);
978 chord_name = talloc_asprintf (local, "%s%s",
983 _spell_chord_by_signature (note_group, num_notes,
986 chord_name = talloc_strdup (local, "?");
989 scherzo->chord = score_add_chord (scherzo->treble, chord_name);
996 note_group_init (void *ctx, note_group_t *group)
1001 group->num_notes = 0;
1005 note_group_add_note (note_group_t *group, score_note_t *note)
1009 /* Do nothing if note is already in group. */
1010 for (i = 0; i < group->num_notes; i++) {
1011 if (group->notes[i]->pitch == note->pitch &&
1012 group->notes[i]->octave == note->octave)
1020 if (group->num_notes > group->size) {
1022 group->notes = talloc_realloc (group->ctx, group->notes,
1023 score_note_t*, group->size);
1025 if (group->notes == NULL) {
1026 fprintf (stderr, "Out of memory.\n");
1031 group->notes[group->num_notes - 1] = note;
1035 note_group_remove_note_at (note_group_t *group, int i)
1037 if (i >= group->num_notes) {
1038 fprintf (stderr, "Internal error: No note to remove at index %d\n", i);
1042 if (i < group->num_notes - 1) {
1043 memmove (group->notes + i, group->notes + i + 1,
1044 (group->num_notes - 1 - i) * sizeof (score_note_t*));
1049 static score_note_t *
1050 scherzo_press_note (scherzo_t *scherzo, pitch_t pitch, int octave)
1052 score_staff_t *staff;
1056 if (scherzo->challenge.note) {
1057 staff = scherzo->challenge.staff;
1058 } else if (octave >= 4) {
1059 staff = scherzo->treble;
1061 staff = scherzo->bass;
1064 /* Do nothing if this note is already pressed. */
1065 for (i = 0; i < scherzo->notes_pressed.num_notes; i++) {
1066 if (scherzo->notes_pressed.notes[i]->pitch == pitch &&
1067 scherzo->notes_pressed.notes[i]->octave == octave)
1069 return scherzo->notes_pressed.notes[i];
1073 note = score_add_note (staff, pitch, octave, SCORE_DURATION_WHOLE);
1075 note_group_add_note (&scherzo->notes_pressed, note);
1077 if (scherzo->pedal_pressed)
1078 note_group_add_note (&scherzo->notes_pedaled, note);
1080 scherzo_analyze_chord (scherzo);
1086 scherzo_release_note (scherzo_t *scherzo, pitch_t pitch, int octave)
1091 int target_midi = _pitch_and_octave_to_midi (pitch, octave);
1094 for (i = scherzo->notes_pressed.num_notes - 1; i >=0; i--) {
1095 note = scherzo->notes_pressed.notes[i];
1096 note_midi = _pitch_and_octave_to_midi (note->pitch, note->octave);
1097 if (note_midi == target_midi) {
1099 if (! scherzo->pedal_pressed)
1100 score_remove_note (note);
1101 note_group_remove_note_at (&scherzo->notes_pressed, i);
1106 fprintf (stderr, "Internal error: Failed to find note to release.\n");
1109 scherzo_analyze_chord (scherzo);
1112 static score_note_t *
1113 scherzo_press_note_midi (scherzo_t *scherzo, unsigned char midi_note)
1118 _midi_to_pitch_and_octave (midi_note, &pitch, &octave);
1120 return scherzo_press_note (scherzo, pitch, octave);
1124 scherzo_release_note_midi (scherzo_t *scherzo, unsigned char midi_note)
1129 _midi_to_pitch_and_octave (midi_note, &pitch, &octave);
1131 scherzo_release_note (scherzo, pitch, octave);
1135 scherzo_press_pedal (scherzo_t *scherzo)
1139 scherzo->pedal_pressed = 1;
1141 /* Copy all pressed notes to pedaled notes */
1142 for (i = 0; i < scherzo->notes_pressed.num_notes; i++)
1143 note_group_add_note (&scherzo->notes_pedaled, scherzo->notes_pressed.notes[i]);
1147 scherzo_release_pedal (scherzo_t *scherzo)
1149 score_note_t *note, *new_note;
1152 /* Make new notes in score for all pressed notes. */
1153 for (i = 0; i < scherzo->notes_pressed.num_notes; i++) {
1154 note = scherzo->notes_pressed.notes[i];
1155 new_note = score_add_note (note->staff, note->pitch, note->octave, note->duration);
1156 scherzo->notes_pressed.notes[i] = new_note;
1159 /* Then remove all previously pedaled notes from the score. */
1160 for (i = scherzo->notes_pedaled.num_notes - 1; i >=0; i--) {
1161 note = scherzo->notes_pedaled.notes[i];
1162 score_remove_note (note);
1163 note_group_remove_note_at (&scherzo->notes_pedaled, i);
1166 scherzo->pedal_pressed = 0;
1168 scherzo_analyze_chord (scherzo);
1170 gtk_widget_queue_draw (scherzo->window);
1174 _select_challenge (scherzo_t *scherzo)
1176 category_t *category_unused;
1177 bool_t introduced_unused;
1179 challenge_t *challenge = &scherzo->challenge;
1184 if (challenge->note) {
1185 score_remove_note (challenge->note);
1186 challenge->note = NULL;
1189 mnemon_select_item (&scherzo->mnemon,
1191 &challenge->item_index,
1193 &introduced_unused);
1195 item = challenge->bin->items[challenge->item_index];
1197 s = item->challenge;
1198 if (strncmp (s, "treble:", 7) == 0) {
1200 challenge->staff = scherzo->treble;
1201 } else if (strncmp (s, "bass:", 5) == 0) {
1203 challenge->staff = scherzo->bass;
1206 "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
1213 pitch = PITCH_NATURAL (C);
1216 pitch = PITCH_NATURAL (D);
1219 pitch = PITCH_NATURAL (E);
1222 pitch = PITCH_NATURAL (F);
1225 pitch = PITCH_NATURAL (G);
1228 pitch = PITCH_NATURAL (A);
1231 pitch = PITCH_NATURAL (B);
1234 fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
1239 if (*s < '0' || *s > '9') {
1240 fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
1246 challenge->note = score_add_note (challenge->staff, pitch, octave,
1247 SCORE_DURATION_WHOLE);
1248 challenge->satisfied = 0;
1249 challenge->mistaken = 0;
1252 /* Determine whether the user hit the correct note. */
1254 _judge_note (scherzo_t *scherzo, score_note_t *note)
1256 challenge_t *challenge = &scherzo->challenge;
1258 if (! scherzo->challenge.note) {
1259 score_set_note_color_rgb (note, 0.0, 0.0, 0.0); /* black */
1263 if (note->pitch == challenge->note->pitch &&
1264 note->octave == challenge->note->octave)
1266 challenge->satisfied = 1;
1267 score_set_note_color_rgb (note, 18/256., 130/256., 28/256.); /* green */
1271 challenge->mistaken = 1;
1272 score_set_note_color_rgb (note, 184/256., 4/256., 22/256.); /* red */
1276 /* If the user got the right note (eventually), then score it in
1277 * mnemon and show the next note. */
1279 _score_challenge (scherzo_t *scherzo)
1281 challenge_t *challenge = &scherzo->challenge;
1283 if (! challenge->note)
1286 if (! challenge->satisfied)
1289 mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
1290 ! challenge->mistaken);
1292 _select_challenge (scherzo);
1296 on_midi_input (unused (GIOChannel *channel),
1297 unused (GIOCondition condition),
1300 unsigned char buf[MIDI_BUF_SIZE], *next;
1301 scherzo_t *scherzo = user_data;
1303 snd_seq_event_t event;
1305 int need_redraw = FALSE;
1307 remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
1313 consumed = snd_midi_event_encode (scherzo->snd_midi_event,
1314 next, remaining, &event);
1316 remaining -= consumed;
1319 switch (event.type) {
1320 case SND_SEQ_EVENT_NONE:
1321 /* Incomplete event. Nothing to do. */
1323 case SND_SEQ_EVENT_NOTEON:
1324 note = scherzo_press_note_midi (scherzo, event.data.note.note);
1325 _judge_note (scherzo, note);
1328 case SND_SEQ_EVENT_NOTEOFF:
1329 scherzo_release_note_midi (scherzo, event.data.note.note);
1330 _score_challenge (scherzo);
1333 case SND_SEQ_EVENT_CLOCK:
1334 /* Ignore for now as my piano sends a constant stream of these. */
1336 case SND_SEQ_EVENT_SENSING:
1337 /* Ignore for now as my piano sends a constant stream of these. */
1339 case SND_SEQ_EVENT_CONTROLLER:
1340 /* XXX: My piano gives me 64 for the sustain pedal, is
1341 * that universal? */
1342 if (event.data.control.param == 64) {
1343 if (event.data.control.value == 0)
1344 scherzo_release_pedal (scherzo);
1346 scherzo_press_pedal (scherzo);
1348 fprintf (stderr, "Fixme: Unhandled MIDI Control event (param=%d, value=%d)\n",
1349 event.data.control.param, event.data.control.value);
1353 fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
1360 gtk_widget_queue_draw (scherzo->window);
1362 /* Return TRUE to continue to get called in the future. */
1367 main (int argc, char *argv[])
1369 GtkWidget *drawing_area;
1373 srand (time (NULL));
1375 gtk_init (&argc, &argv);
1377 scherzo.ctx = talloc_new (NULL);
1379 scherzo.score = score_create (scherzo.ctx);
1380 scherzo.staff_height = 100;
1381 score_set_staff_height (scherzo.score, scherzo.staff_height);
1383 score_add_brace (scherzo.score, 2);
1384 scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
1385 scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
1387 scherzo.chord = NULL;
1389 /* Default to octave 4 and natural for computer keyboard keypresses. */
1390 scherzo.keyboard_octave = 4;
1391 scherzo.keyboard_accidental = PITCH_ACCIDENTAL_NATURAL;
1393 note_group_init (scherzo.ctx, &scherzo.notes_pressed);
1394 note_group_init (scherzo.ctx, &scherzo.notes_pedaled);
1396 scherzo.pedal_pressed = 0;
1398 mnemon_init (&scherzo.mnemon);
1399 /* XXX: Should create a default file if one cannot be loaded. */
1400 mnemon_load_category (&scherzo.mnemon, "scherzo-notes");
1402 scherzo.challenge.note = NULL;
1404 _select_challenge (&scherzo);
1407 err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
1409 fprintf (stderr, "Out of memory.\n");
1413 #define MIDI_DEVICE "/dev/midi1"
1414 scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
1415 if (scherzo.midi_fd < 0) {
1416 printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
1418 GIOChannel *channel;
1420 channel = g_io_channel_unix_new (scherzo.midi_fd);
1421 g_io_channel_set_encoding (channel, NULL, NULL);
1422 g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
1425 scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1427 gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 1000, 600);
1429 g_signal_connect (scherzo.window, "delete-event",
1430 G_CALLBACK (on_delete_event_quit), NULL);
1432 drawing_area = gtk_drawing_area_new ();
1434 gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
1436 g_signal_connect (drawing_area, "expose-event",
1437 G_CALLBACK (on_expose_event_draw),
1440 g_signal_connect (scherzo.window, "key-press-event",
1441 G_CALLBACK (on_key_press_event),
1444 g_signal_connect (scherzo.window, "key-release-event",
1445 G_CALLBACK (on_key_release_event),
1448 gtk_widget_show_all (scherzo.window);
1452 mnemon_save (&scherzo.mnemon);
1454 snd_midi_event_free (scherzo.snd_midi_event);
1456 talloc_free (scherzo.ctx);