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) ((int) (sizeof(arr) / sizeof(arr[0])))
29 #define unused(foo) foo __attribute__((unused))
31 #define MIDI_BUF_SIZE 4096
33 typedef struct challenge
45 typedef struct pitch_group
53 typedef struct scherzo
61 score_staff_t *treble;
65 /* The word "keyboard" here is referring to a "computer
66 * keyboard". Any "piano keyboard" key knows its own octave and
67 * accidental already. */
71 snd_midi_event_t *snd_midi_event;
74 challenge_t challenge;
76 pitch_group_t notes_pressed;
77 pitch_group_t notes_pedaled;
84 /* Forward declarations. */
86 scherzo_press_note (scherzo_t *scherzo, pitch_t pitch);
89 scherzo_release_note (scherzo_t *scherzo, pitch_t pitch);
92 scherzo_press_pedal (scherzo_t *scherzo);
95 scherzo_release_pedal (scherzo_t *scherzo);
98 _judge_pitch (scherzo_t *scherzo, pitch_t pitch);
101 _score_challenge (scherzo_t *scherzo);
104 pitch_group_remove_pitches (pitch_group_t *group);
107 scherzo_update_notes_and_chord (scherzo_t *scherzo);
110 on_delete_event_quit (unused (GtkWidget *widget),
111 unused (GdkEvent *event),
112 unused (gpointer user_data))
116 /* Returning FALSE allows the default handler for delete-event
117 * to proceed to cleanup the widget. */
122 on_expose_event_draw (GtkWidget *widget,
123 unused (GdkEventExpose *expose),
126 scherzo_t *scherzo = user_data;
127 score_t *score = scherzo->score;
129 GtkAllocation allocation;
130 static const int pad = 10;
133 gtk_widget_get_allocation (widget, &allocation);
134 widget_width = allocation.width;
136 cr = gdk_cairo_create (widget->window);
138 /* White background */
139 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
142 /* Add some padding on the sides and top */
143 cairo_translate (cr, pad, scherzo->staff_height);
144 score_set_staff_height (score, scherzo->staff_height);
145 score_set_width (score, widget_width - 2 * pad);
147 score_draw (score, cr);
153 on_key_press_event (GtkWidget *widget,
157 scherzo_t *scherzo = user_data;
159 pitch_t pitch = PITCH_NOT_A_PITCH;
161 if (scherzo->challenge.active)
162 octave = PITCH_OCTAVE (scherzo->challenge.pitch);
164 octave = scherzo->keyboard_octave;
166 switch (key->keyval) {
170 case GDK_KEY_KP_Equal:
171 scherzo->staff_height += 4;
172 gtk_widget_queue_draw (widget);
176 case GDK_KEY_KP_Subtract:
177 scherzo->staff_height -= 4;
178 gtk_widget_queue_draw (widget);
184 /* Map a piano keyboard onto the computer keyboard with white keys
185 * along the home row, (piano C on computer A, up to next octave
186 * higher C on computer K). Black keys are in the expected
187 * positions, (C# is W, D# is E, etc.).
191 pitch = PITCH_LITERAL (C, NATURAL, octave);
195 pitch = PITCH_LITERAL (D, NATURAL, octave);
199 pitch = PITCH_LITERAL (E, NATURAL, octave);
203 pitch = PITCH_LITERAL (F, NATURAL, octave);
207 pitch = PITCH_LITERAL (G, NATURAL, octave);
211 pitch = PITCH_LITERAL (A, NATURAL, octave);
215 pitch = PITCH_LITERAL (B, NATURAL, octave);
219 pitch = PITCH_LITERAL (C, NATURAL, octave + 1);
223 pitch = PITCH_LITERAL (D, NATURAL, octave + 1);
225 case GDK_KEY_semicolon:
227 pitch = PITCH_LITERAL (E, NATURAL, octave + 1);
229 case GDK_KEY_apostrophe:
230 case GDK_KEY_quotedbl:
231 pitch = PITCH_LITERAL (F, NATURAL, octave + 1);
235 pitch = PITCH_LITERAL (C, SHARP, octave);
239 pitch = PITCH_LITERAL (D, SHARP, octave);
243 pitch = PITCH_LITERAL (F, SHARP, octave);
247 pitch = PITCH_LITERAL (G, SHARP, octave);
251 pitch = PITCH_LITERAL (A, SHARP, octave);
255 pitch = PITCH_LITERAL (C, SHARP, octave + 1);
259 pitch = PITCH_LITERAL (D, SHARP, octave + 1);
261 case GDK_KEY_bracketright:
262 case GDK_KEY_braceright:
263 pitch = PITCH_LITERAL (F, SHARP, octave + 1);
274 scherzo->keyboard_octave = key->keyval - GDK_KEY_0;
277 scherzo_press_pedal (scherzo);
280 /* Clear all notes when Return is pressed. */
281 pitch_group_remove_pitches (&scherzo->notes_pressed);
282 pitch_group_remove_pitches (&scherzo->notes_pedaled);
283 scherzo_update_notes_and_chord (scherzo);
287 if (pitch != PITCH_NOT_A_PITCH)
289 scherzo_press_note (scherzo, pitch);
290 _judge_pitch (scherzo, pitch);
291 gtk_widget_queue_draw (scherzo->window);
296 /* Allow an unhandled event to propagate to other handlers. */
301 on_key_release_event (unused (GtkWidget *widget),
305 scherzo_t *scherzo = user_data;
307 pitch_t pitch = PITCH_NOT_A_PITCH;
309 if (scherzo->challenge.active)
310 octave = PITCH_OCTAVE (scherzo->challenge.pitch);
312 octave = scherzo->keyboard_octave;
314 switch (key->keyval) {
317 pitch = PITCH_LITERAL (C, NATURAL, octave);
321 pitch = PITCH_LITERAL (D, NATURAL, octave);
325 pitch = PITCH_LITERAL (E, NATURAL, octave);
329 pitch = PITCH_LITERAL (F, NATURAL, octave);
333 pitch = PITCH_LITERAL (G, NATURAL, octave);
337 pitch = PITCH_LITERAL (A, NATURAL, octave);
341 pitch = PITCH_LITERAL (B, NATURAL, octave);
345 pitch = PITCH_LITERAL (C, NATURAL, octave + 1);
349 pitch = PITCH_LITERAL (D, NATURAL, octave + 1);
351 case GDK_KEY_semicolon:
353 pitch = PITCH_LITERAL (E, NATURAL, octave + 1);
355 case GDK_KEY_apostrophe:
356 case GDK_KEY_quotedbl:
357 pitch = PITCH_LITERAL (F, NATURAL, octave + 1);
361 pitch = PITCH_LITERAL (C, SHARP, octave);
365 pitch = PITCH_LITERAL (D, SHARP, octave);
369 pitch = PITCH_LITERAL (F, SHARP, octave);
373 pitch = PITCH_LITERAL (G, SHARP, octave);
377 pitch = PITCH_LITERAL (A, SHARP, octave);
381 pitch = PITCH_LITERAL (C, SHARP, octave + 1);
385 pitch = PITCH_LITERAL (D, SHARP, octave + 1);
387 case GDK_KEY_bracketright:
388 case GDK_KEY_braceright:
389 pitch = PITCH_LITERAL (F, SHARP, octave + 1);
392 scherzo_release_pedal (scherzo);
396 if (pitch != PITCH_NOT_A_PITCH)
398 scherzo_release_note (scherzo, pitch);
399 _score_challenge (scherzo);
400 gtk_widget_queue_draw (scherzo->window);
406 /* Allow an unhandled event to propagate to other handlers. */
411 _pitch_to_midi (pitch_t pitch)
413 int octave = PITCH_OCTAVE (pitch);
414 unsigned char midi_note = 12 * (octave + 1);
416 switch (PITCH_NAME (pitch)) {
439 switch (PITCH_ACCIDENTAL (pitch)) {
440 case PITCH_ACCIDENTAL_DOUBLE_FLAT:
443 case PITCH_ACCIDENTAL_FLAT:
446 case PITCH_ACCIDENTAL_NATURAL:
448 case PITCH_ACCIDENTAL_SHARP:
451 case PITCH_ACCIDENTAL_DOUBLE_SHARP:
459 /* octave can optionally by NULL */
461 _midi_to_pitch (unsigned char midi_note)
463 int octave = octave = midi_note / 12 - 1;
465 switch (midi_note % 12)
469 return PITCH_LITERAL (C, NATURAL, octave);
472 return PITCH_LITERAL (C, SHARP, octave);
475 return PITCH_LITERAL (D, NATURAL, octave);
478 return PITCH_LITERAL (D, SHARP, octave);
481 return PITCH_LITERAL (E, NATURAL, octave);
484 return PITCH_LITERAL (F, NATURAL, octave);
487 return PITCH_LITERAL (F, SHARP, octave);
490 return PITCH_LITERAL (G, NATURAL, octave);
493 return PITCH_LITERAL (G, SHARP, octave);
496 return PITCH_LITERAL (A, NATURAL, octave);
499 return PITCH_LITERAL (A, SHARP, octave);
502 return PITCH_LITERAL (B, NATURAL, octave);
507 /* Return true if 'a' and 'b' sound identical, (even if spelled differently)
509 * This comparison considers octaves as significant. So Bb and A# in
510 * the same octave are considered enharmonic, but Bb and A# in
511 * different octaves are not. */
513 _pitches_are_enharmonic (pitch_t a, pitch_t b)
515 return _pitch_to_midi (a) == _pitch_to_midi (b);
518 /* Determine a chord name (if any) from the current notes pressed */
520 typedef struct analyzed_pitch {
521 /* Original note being analzyed. */
524 /* Absolute pitch (expressed as midi number). */
527 /* Pitch relative to bass note. */
532 _compare_analyzed_pitch_by_midi_pitch (const void *va, const void *vb)
534 const analyzed_pitch_t *a = va, *b = vb;
536 return a->midi_pitch - b->midi_pitch;
540 _compare_analyzed_pitch_by_relative_pitch (const void *va, const void *vb)
542 const analyzed_pitch_t *a = va, *b = vb;
544 return a->relative_pitch - b->relative_pitch;
547 typedef struct modified_degree
554 _modified_degree_to_half_steps (const modified_degree_t *degree)
557 int scale_degree = degree->degree;
559 /* Restrict to actual degrees within a scale. */
560 if (scale_degree > 7)
561 scale_degree = (scale_degree % 8) + 1;
563 /* Number of half steps from root to specified degree within a
565 switch (scale_degree) {
588 fprintf (stderr, "Internal: Invalid degree %d\n", degree->degree);
593 return half_steps + degree->modification;
596 /* Number of half steps from 'root' up to 'pitch' (that is, counting
597 * the steps up a chromatic scale), within the same octave. */
599 _pitch_from_root_in_half_steps (pitch_t pitch, pitch_t root)
601 int root_midi = _pitch_to_midi (root);
602 int pitch_midi = _pitch_to_midi (pitch);
604 while (pitch_midi < root_midi)
607 return (pitch_midi - root_midi) % 12;
611 _chord_signature_matches (analyzed_pitch_t *pitches,
613 modified_degree_t *degrees,
618 #define MAX_DEGREES 6
619 int relative_pitches[MAX_DEGREES];
622 assert (num_degrees <= MAX_DEGREES);
624 if (num_pitches != num_degrees)
627 if (inversion >= num_degrees)
630 /* We never spell simple intervals as inversions. */
631 if (num_degrees == 2 && inversion > 0)
634 for (i = 0; i < num_degrees; i++) {
635 /* The num_degrees is in the addition just to ensure all
636 * inputs to the modulus operator remain positive. */
637 int index = (i + num_degrees - inversion) % num_degrees;
639 /* Again, adding a 12 to keep things positive. */
640 relative_pitches[index] =
642 _modified_degree_to_half_steps (°rees[i]) -
643 _modified_degree_to_half_steps (°rees[inversion])) % 12;
647 for (i = 0; i < num_pitches; i++)
648 if (pitches[i].relative_pitch != relative_pitches[i])
651 root_index = (num_pitches - inversion) % num_pitches;
652 *root = pitches[root_index].pitch;
657 typedef struct chord_signature
660 modified_degree_t degrees[MAX_DEGREES];
664 /* XXX: The superscript rise value should be relative to the current
665 * font size. Best would be for pango to support this. See:
667 * https://bugzilla.gnome.org/show_bug.cgi?id=708780
669 * For now, these are emipirically-derived values that look reasonable
670 * at the default size that score.c is using to draw the staff. If the
671 * user scales the staff up or down then these will look wrong.
674 #define SUP "<span font='33' rise='30000'>"
675 #define PUS "</span>"
677 chord_signature_t signatures[] = {
679 { 1, {{1, 0}}, "P8" },
682 { 2, {{1, 0}, {2, -1}}, "m2" },
683 { 2, {{1, 0}, {2, 0}}, "M2" },
684 { 2, {{1, 0}, {3, -1}}, "m3" },
685 { 2, {{1, 0}, {3, 0}}, "M3" },
686 { 2, {{1, 0}, {4, 0}}, "P4" },
687 { 2, {{1, 0}, {5, -1}}, "Tri-tone" },
688 { 2, {{1, 0}, {5, 0}}, "P5" },
689 { 2, {{1, 0}, {6, -1}}, "m6" },
690 { 2, {{1, 0}, {6, 0}}, "M6" },
691 { 2, {{1, 0}, {7, -1}}, "m7" },
692 { 2, {{1, 0}, {7, 0}}, "M7" },
695 { 3, {{1, 0}, {4, 0}, {5, 0}}, "sus" },
696 { 3, {{1, 0}, {3, 0}, {5, +1}}, SUP "+" PUS },
697 { 3, {{1, 0}, {3, 0}, {5, 0}}, "" },
698 { 3, {{1, 0}, {3, -1}, {5, 0}}, "m" },
699 { 3, {{1, 0}, {3, -1}, {5, -1}}, "°" },
700 { 3, {{1, 0}, {2, 0}, {5, 0}}, "msus2" },
702 /* Seventh chords with no 3rd */
703 { 3, {{1, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS },
704 { 3, {{1, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS },
705 { 3, {{1, 0}, {5, 0}, {7, 0}}, "M7" },
706 { 3, {{1, 0}, {5, 0}, {7, -1}}, "7" },
707 { 3, {{1, 0}, {5, -1}, {7, -1}}, "7♭5" },
709 /* Seventh chords with no 5th */
710 { 3, {{1, 0}, {4, 0}, {7, 0}}, "M7sus" },
711 { 3, {{1, 0}, {4, 0}, {7, -1}}, "7sus" },
712 { 3, {{1, 0}, {3, 0}, {7, 0}}, "M7" },
713 { 3, {{1, 0}, {3, 0}, {7, -1}}, "7" },
714 { 3, {{1, 0}, {3, -1}, {7, 0}}, "m" SUP "M7" PUS },
715 { 3, {{1, 0}, {3, -1}, {7, -1}}, "m7" },
716 { 3, {{1, 0}, {2, 0}, {7, 0}}, "m" SUP "M7" PUS "sus2" },
717 { 3, {{1, 0}, {2, 0}, {7, -1}}, "m7sus2" },
720 { 4, {{1, 0}, {4, 0}, {5, 0}, {6, 0}}, "6sus" },
721 { 4, {{1, 0}, {3, 0}, {5, 0}, {6, 0}}, "6" }, /* Ambiguous with m7 */
722 { 4, {{1, 0}, {3, -1}, {5, 0}, {6, 0}}, "m6" },
723 { 4, {{1, 0}, {2, 0}, {5, 0}, {6, 0}}, "m6sus2" },
726 { 4, {{1, 0}, {4, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS "sus" },
727 { 4, {{1, 0}, {4, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS "sus" },
728 { 4, {{1, 0}, {4, 0}, {5, 0}, {7, 0}}, "M7sus" },
729 { 4, {{1, 0}, {4, 0}, {5, 0}, {7, -1}}, "7sus" },
730 { 4, {{1, 0}, {4, 0}, {5, -1}, {7, -1}}, "7♭5sus" },
731 { 4, {{1, 0}, {3, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS },
732 { 4, {{1, 0}, {3, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS },
733 { 4, {{1, 0}, {3, 0}, {5, 0}, {7, 0}}, "M7" },
734 { 4, {{1, 0}, {3, 0}, {5, 0}, {7, -1}}, "7" },
735 { 4, {{1, 0}, {3, 0}, {5, -1}, {7, -1}}, "7♭5" },
736 { 4, {{1, 0}, {3, -1}, {5, 0}, {7, 0}}, "m" SUP "M7" PUS },
737 { 4, {{1, 0}, {3, -1}, {5, 0}, {7, -1}}, "m7" },
738 { 4, {{1, 0}, {3, -1}, {5, -1}, {7, 0}}, "°" SUP "M7" PUS },
739 { 4, {{1, 0}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "7" PUS },
740 { 4, {{1, 0}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "7" PUS },
741 { 4, {{1, 0}, {2, 0}, {5, 0}, {7, 0}}, "m" SUP "M7" PUS "sus2" },
742 { 4, {{1, 0}, {2, 0}, {5, 0}, {7, -1}}, "m7sus2" },
743 { 4, {{1, 0}, {2, 0}, {5, -1}, {7, 0}}, "°" SUP "M7" PUS "sus2" },
744 { 4, {{1, 0}, {2, 0}, {5, -1}, {7, -1}}, "𝆩" SUP "7" PUS "sus2" },
745 { 4, {{1, 0}, {2, 0}, {5, -1}, {7, -2}}, "°" SUP "7" PUS "sus2" }, /* Ambiguous with 7 */
747 /* The sorting for chords with degree higher than 9 is funny
748 * to keep the array in degree order after reducing each
749 * degree to an actual scale degree, (9 -> 2, 11 -> 4, 13 ->
752 /* Ninth chords voiced with no 5th */
753 { 4, {{1, 0}, {9, 0}, {4, 0}, {7, 0}}, "M9sus" },
754 { 4, {{1, 0}, {9, 0}, {4, 0}, {7, -1}}, "9sus" },
755 { 4, {{1, 0}, {9, 0}, {3, 0}, {7, 0}}, "M9" },
756 { 4, {{1, 0}, {9, 0}, {3, 0}, {7, -1}}, "9" },
757 { 4, {{1, 0}, {9, 0}, {3, -1}, {7, 0}}, "m" SUP "M9" PUS },
758 { 4, {{1, 0}, {9, 0}, {3, -1}, {7, -1}}, "m9" },
759 { 4, {{1, 0}, {9, -1}, {3, -1}, {7, -1}}, "m♭9" },
760 { 4, {{1, 0}, {9, 0}, {3, -1}, {6, 0}}, "m6/9" },
761 { 4, {{1, 0}, {9, -1}, {3, -1}, {6, 0}}, "m6/♭9" },
764 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {6, 0}}, "6/9" },
765 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {6, 0}}, "m6/9" },
766 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, 0}, {6, 0}}, "m6/♭9" },
768 /* Seventh plus altered 9 */
769 { 5, {{1, 0}, {9, +1}, {3, 0}, {5, 0}, {7, -1}}, "7♯9" },
770 { 5, {{1, 0}, {9, -1}, {3, 0}, {5, 0}, {7, -1}}, "7♭9" },
771 { 5, {{1, 0}, {9, +1}, {3, 0}, {5, -1}, {7, -1}}, "7♭5♯9" },
772 { 5, {{1, 0}, {9, -1}, {3, 0}, {5, -1}, {7, -1}}, "7♭5♭9" },
775 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, +1}, {7, 0}}, SUP "+M9" PUS "sus" },
776 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, +1}, {7, -1}}, SUP "+9" PUS "sus" },
777 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, 0}, {7, 0}}, "M9sus" },
778 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, 0}, {7, -1}}, "9sus" },
779 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, +1}, {7, 0}}, SUP "+M9" PUS },
780 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, +1}, {7, -1}}, SUP "+9" PUS },
781 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {7, 0}}, "M9" },
782 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {7, -1}}, "9" },
783 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {7, 0}}, "m" SUP "M9" PUS },
784 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {7, -1}}, "m9" },
785 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, 0}, {7, -1}}, "m♭9" },
786 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, -1}, {7, 0}}, "M9" SUP "♭5" PUS },
787 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, -1}, {7, -1}}, "9" SUP "♭5" PUS },
788 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, 0}}, "m" SUP "M9♭5" PUS },
789 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "9" PUS },
790 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "♭9" PUS },
791 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "9" PUS },
792 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "♭9" PUS },
794 /* Eleventh chords */
795 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, +1}, {7, 0}}, SUP "+M11" PUS },
796 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, +1}, {7, -1}}, SUP "+11" PUS },
797 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, 0}, {7, 0}}, "M11" },
798 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, 0}, {7, -1}}, "11" },
799 { 6, {{1, 0}, {9, 0}, {3, -1}, {11, 0}, {5, 0}, {7, 0}}, "m" SUP "M11" PUS },
800 { 6, {{1, 0}, {9, 0}, {3, -1}, {11, 0}, {5, 0}, {7, -1}}, "m11" },
801 { 6, {{1, 0}, {9, -1}, {3, -1}, {11, 0}, {5, -1}, {7, -1}}, "𝆩" SUP "11" PUS },
802 { 6, {{1, 0}, {9, -1}, {3, -1}, {11, -1}, {5, -1}, {7, -2}}, "°" SUP "11" PUS }
806 scherzo_adjust_pitch_to_match_modified_degree (pitch_t *pitch,
808 modified_degree_t *degree)
810 pitch_name_t name = PITCH_NAME (*pitch);
811 pitch_accidental_t accidental = PITCH_ACCIDENTAL (*pitch);
812 int octave = PITCH_OCTAVE (*pitch);
813 int degree_zero_based = (degree->degree - 1) % 7;
816 int note_degree_zero_based = name - PITCH_NAME (root);
817 if (note_degree_zero_based < 0)
818 note_degree_zero_based += 7;
820 if (note_degree_zero_based == degree_zero_based)
823 degree_delta = note_degree_zero_based - degree_zero_based;
824 if (degree_delta > 3)
825 degree_delta = - (7 - degree_delta);
826 if (degree_delta < -3)
827 degree_delta = - (-7 - degree_delta);
829 if (abs (degree_delta) != 1) {
830 fprintf (stderr, "Internal error: Cannot adjust a note more than one degree (%d vs. %d).\n", note_degree_zero_based + 1, degree->degree);
834 if (degree_delta == -1) {
835 if (name == PITCH_NAME_B) {
856 if (degree_delta == +1) {
857 if (name == PITCH_NAME_C) {
877 if (accidental < 0 || accidental > PITCH_ACCIDENTAL_DOUBLE_SHARP) {
878 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);
882 *pitch = PITCH (name, accidental, octave);
886 _spell_chord_by_signature (pitch_group_t *chord,
887 chord_signature_t *signature,
890 int i, degree, note_half_steps, degree_half_steps;
893 /* Sanitize root to eliminate things like double-flats,
894 * double-sharps, Cb, E#, etc. */
895 root_midi = _pitch_to_midi (root);
896 root = _midi_to_pitch (root_midi);
898 for (i = 0; i < chord->num_pitches; i++) {
899 note_half_steps = _pitch_from_root_in_half_steps (chord->pitches[i],
901 for (degree = 0; degree < signature->num_degrees; degree++) {
902 degree_half_steps = _modified_degree_to_half_steps (&signature->degrees[degree]);
903 if (note_half_steps == degree_half_steps) {
904 scherzo_adjust_pitch_to_match_modified_degree (&chord->pitches[i],
906 &signature->degrees[degree]);
910 if (note_half_steps != degree_half_steps) {
911 fprintf (stderr, "Internal error: Chord and degree mis-match\n");
918 _analyze_chord (pitch_group_t *chord,
919 chord_signature_t **signature_ret,
922 void *local = talloc_new (NULL);
923 analyzed_pitch_t *pitches;
924 int i, j, num_pitches;
925 int bass_pitch, inversion;
927 chord_signature_t *match = NULL;
929 /* Default to no-match for any early return */
930 *signature_ret = NULL;
933 num_pitches = chord->num_pitches;
935 if (chord->num_pitches <= 1)
938 pitches = talloc_array (local, analyzed_pitch_t, num_pitches);
942 for (i = 0; i < num_pitches; i++) {
943 pitch_t pitch = chord->pitches[i];
944 pitches[i].pitch = pitch;
945 pitches[i].midi_pitch = _pitch_to_midi (pitch);
946 /* Relative pitch will be filled in after sorting. */
947 pitches[i].relative_pitch = 0;
950 /* First, sort by midi pitch to find the bass note. */
951 qsort (pitches, num_pitches, sizeof (analyzed_pitch_t),
952 _compare_analyzed_pitch_by_midi_pitch);
954 bass_pitch = pitches[0].midi_pitch;
956 /* With the bass note identified, we can find all relative pitches. */
957 for (i = 0; i < num_pitches; i++) {
958 pitches[i].relative_pitch = pitches[i].midi_pitch - bass_pitch;
959 while (pitches[i].relative_pitch >= 12)
960 pitches[i].relative_pitch -= 12;
963 /* Now, sort again by relative pitch. */
964 qsort (pitches, num_pitches, sizeof (analyzed_pitch_t),
965 _compare_analyzed_pitch_by_relative_pitch);
967 /* Finally, eliminate all duplicate notes. */
968 for (i = 0; i < num_pitches - 1; i++) {
969 if (pitches[i+1].relative_pitch == pitches[i].relative_pitch) {
971 while (j < num_pitches &&
972 pitches[j].relative_pitch == pitches[i].relative_pitch)
976 /* The loop incremented j one past the last
977 * duplicate. Decrement so that it points to the
978 * last duplicate (and is guaranteed to not exceed
979 * the array bounds).*/
982 if (j < num_pitches - 1) {
983 memmove (&pitches[i+1], &pitches[j+1],
984 (num_pitches - j) * sizeof (analyzed_pitch_t));
987 num_pitches -= (j - i);
991 for (inversion = 0; inversion < 4; inversion++) {
992 for (i = 0; i < ARRAY_SIZE (signatures); i++) {
993 if (_chord_signature_matches (pitches, num_pitches,
994 signatures[i].degrees,
995 signatures[i].num_degrees,
998 match = &signatures[i];
1006 *signature_ret = match;
1007 for (i = 0; i < chord->num_pitches; i++) {
1008 if (chord->pitches[i] == root) {
1009 *root_ret = &chord->pitches[i];
1016 talloc_free (local);
1020 pitch_group_init (void *ctx, pitch_group_t *group)
1023 group->pitches = NULL;
1025 group->num_pitches = 0;
1029 pitch_group_add_pitch (pitch_group_t *group, pitch_t pitch)
1033 /* Do nothing if pitch is already in group. */
1034 for (i = 0; i < group->num_pitches; i++)
1035 if (group->pitches[i] == pitch)
1038 group->num_pitches++;
1040 if (group->num_pitches > group->size) {
1042 group->pitches = talloc_realloc (group->ctx, group->pitches,
1043 pitch_t, group->size);
1045 if (group->pitches == NULL) {
1046 fprintf (stderr, "Out of memory.\n");
1051 group->pitches[group->num_pitches - 1] = pitch;
1055 pitch_group_remove_pitch_at (pitch_group_t *group, int i)
1057 if (i > group->num_pitches) {
1058 fprintf (stderr, "Internal error: Cannot remove pitch %d from group with only %d %s\n",
1059 i, group->num_pitches,
1060 group->num_pitches == 1 ? "pitch" : "pitches");
1064 if (i < group->num_pitches - 1) {
1065 memmove (group->pitches + i, group->pitches + i + 1,
1066 (group->num_pitches - 1 - i) * sizeof (pitch_t));
1069 group->num_pitches--;
1072 /* Remove a 'pitch' from 'group', (including any enharmonic pitch)
1073 * See also: pitch_group_remove_pitch
1076 pitch_group_remove_pitch_enharmonic (pitch_group_t *group, pitch_t pitch)
1080 for (i = 0; i < group->num_pitches; i++)
1081 if (_pitches_are_enharmonic (group->pitches[i], pitch))
1082 pitch_group_remove_pitch_at (group, i);
1085 /* Remove an exactly-matching pitch from 'group'
1086 * See also: pitch_group_remove_pitch_enharmonic
1089 pitch_group_remove_pitch (pitch_group_t *group, pitch_t pitch)
1093 for (i = 0; i < group->num_pitches; i++)
1094 if (group->pitches[i] == pitch)
1095 pitch_group_remove_pitch_at (group, i);
1099 /* Remove all pitches from a pitch_group_t */
1101 pitch_group_remove_pitches (pitch_group_t *group)
1103 talloc_free (group->pitches);
1104 pitch_group_init (group->ctx, group);
1108 scherzo_update_notes_and_chord (scherzo_t *scherzo)
1110 if (scherzo->notes_pressed.num_pitches == 0 &&
1111 scherzo->notes_pedaled.num_pitches == 0)
1113 score_remove_notes (scherzo->score);
1115 score_remove_chords (scherzo->score);
1117 gtk_widget_queue_draw (scherzo->window);
1122 scherzo_press_note (scherzo_t *scherzo, pitch_t pitch)
1124 void *local = talloc_new (NULL);
1125 pitch_group_t *chord;
1126 chord_signature_t *signature;
1127 char *chord_name = NULL;
1131 /* Do nothing if this note is already pressed. */
1132 for (i = 0; i < scherzo->notes_pressed.num_pitches; i++)
1133 if (scherzo->notes_pressed.pitches[i] == pitch)
1136 pitch_group_add_pitch (&scherzo->notes_pressed, pitch);
1138 if (scherzo->pedal_pressed)
1139 pitch_group_add_pitch (&scherzo->notes_pedaled, pitch);
1141 /* Remove all notes/chords from score, then add current notes. */
1142 score_remove_notes (scherzo->score);
1143 score_remove_chords (scherzo->score);
1145 if (scherzo->pedal_pressed)
1146 chord = &scherzo->notes_pedaled;
1148 chord = &scherzo->notes_pressed;
1150 _analyze_chord (chord, &signature, &root);
1153 /* Don't print root pitch for octaves and inversions,
1154 * (since a pitch name alone looks like a major triad) */
1155 if (signature->num_degrees < 3) {
1156 chord_name = talloc_strdup (local, signature->name);
1158 _spell_chord_by_signature (&scherzo->notes_pressed,
1160 _spell_chord_by_signature (&scherzo->notes_pedaled,
1163 chord_name = talloc_asprintf (local, "%s%s",
1164 pitch_string (*root),
1167 } else if (chord->num_pitches > 2) {
1168 chord_name = talloc_strdup (local, "?");
1172 scherzo->chord = score_add_chord (scherzo->treble, chord_name);
1174 for (i = 0; i < scherzo->notes_pressed.num_pitches; i++)
1176 score_add_note (scherzo->score, scherzo->notes_pressed.pitches[i],
1177 SCORE_DURATION_WHOLE);
1180 for (i = 0; i < scherzo->notes_pedaled.num_pitches; i++)
1182 score_add_note (scherzo->score, scherzo->notes_pedaled.pitches[i],
1183 SCORE_DURATION_WHOLE);
1186 talloc_free (local);
1190 scherzo_release_note (scherzo_t *scherzo, pitch_t pitch)
1192 pitch_group_remove_pitch_enharmonic (&scherzo->notes_pressed, pitch);
1194 if (scherzo->notes_pressed.num_pitches == 0)
1195 scherzo_update_notes_and_chord (scherzo);
1199 scherzo_press_note_midi (scherzo_t *scherzo, unsigned char midi_note)
1201 pitch_t pitch = _midi_to_pitch (midi_note);
1203 scherzo_press_note (scherzo, pitch);
1209 scherzo_release_note_midi (scherzo_t *scherzo, unsigned char midi_note)
1211 scherzo_release_note (scherzo, _midi_to_pitch (midi_note));
1215 scherzo_press_pedal (scherzo_t *scherzo)
1219 if (scherzo->pedal_pressed)
1222 scherzo->pedal_pressed = 1;
1224 /* Copy all pressed notes to pedaled notes */
1225 for (i = 0; i < scherzo->notes_pressed.num_pitches; i++)
1226 pitch_group_add_pitch (&scherzo->notes_pedaled, scherzo->notes_pressed.pitches[i]);
1230 scherzo_release_pedal (scherzo_t *scherzo)
1234 if (! scherzo->pedal_pressed)
1237 /* Remove all previously pedaled notes. */
1238 for (i = scherzo->notes_pedaled.num_pitches - 1; i >=0; i--)
1239 pitch_group_remove_pitch_at (&scherzo->notes_pedaled, i);
1241 scherzo->pedal_pressed = 0;
1243 scherzo_update_notes_and_chord (scherzo);
1247 _select_challenge (scherzo_t *scherzo)
1249 category_t *category_unused;
1250 bool_t introduced_unused;
1252 challenge_t *challenge = &scherzo->challenge;
1253 pitch_name_t pitch_name;
1257 mnemon_select_item (&scherzo->mnemon,
1259 &challenge->item_index,
1261 &introduced_unused);
1263 item = challenge->bin->items[challenge->item_index];
1265 s = item->challenge;
1266 if (strncmp (s, "treble:", 7) == 0) {
1268 challenge->staff = scherzo->treble;
1269 } else if (strncmp (s, "bass:", 5) == 0) {
1271 challenge->staff = scherzo->bass;
1274 "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
1281 pitch_name = PITCH_NAME_C;
1284 pitch_name = PITCH_NAME_D;
1287 pitch_name = PITCH_NAME_E;
1290 pitch_name = PITCH_NAME_F;
1293 pitch_name = PITCH_NAME_G;
1296 pitch_name = PITCH_NAME_A;
1299 pitch_name = PITCH_NAME_B;
1302 fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
1307 if (*s < '0' || *s > '9') {
1308 fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
1314 challenge->pitch = PITCH (pitch_name, PITCH_ACCIDENTAL_NATURAL, octave);
1316 challenge->active = 1;
1317 challenge->satisfied = 0;
1318 challenge->mistaken = 0;
1321 /* Determine whether the user hit the correct note. */
1323 _judge_pitch (scherzo_t *scherzo, pitch_t pitch)
1325 challenge_t *challenge = &scherzo->challenge;
1327 if (! challenge->active)
1330 if (pitch == challenge->pitch)
1331 challenge->satisfied = 1;
1333 challenge->mistaken = 1;
1336 /* If the user got the right note (eventually), then score it in
1337 * mnemon and show the next note. */
1339 _score_challenge (scherzo_t *scherzo)
1341 challenge_t *challenge = &scherzo->challenge;
1343 if (! challenge->active)
1346 if (! challenge->satisfied)
1349 mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
1350 ! challenge->mistaken);
1352 _select_challenge (scherzo);
1356 on_midi_input (unused (GIOChannel *channel),
1357 unused (GIOCondition condition),
1360 unsigned char buf[MIDI_BUF_SIZE], *next;
1361 scherzo_t *scherzo = user_data;
1363 snd_seq_event_t event;
1365 int need_redraw = FALSE;
1367 remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
1373 consumed = snd_midi_event_encode (scherzo->snd_midi_event,
1374 next, remaining, &event);
1376 remaining -= consumed;
1379 switch (event.type) {
1380 case SND_SEQ_EVENT_NONE:
1381 /* Incomplete event. Nothing to do. */
1383 case SND_SEQ_EVENT_NOTEON:
1384 pitch = scherzo_press_note_midi (scherzo, event.data.note.note);
1385 _judge_pitch (scherzo, pitch);
1388 case SND_SEQ_EVENT_NOTEOFF:
1389 scherzo_release_note_midi (scherzo, event.data.note.note);
1390 _score_challenge (scherzo);
1393 case SND_SEQ_EVENT_CLOCK:
1394 /* Ignore for now as my piano sends a constant stream of these. */
1396 case SND_SEQ_EVENT_SENSING:
1397 /* Ignore for now as my piano sends a constant stream of these. */
1399 case SND_SEQ_EVENT_CONTROLLER:
1400 /* XXX: My piano gives me 64 for the sustain pedal, is
1401 * that universal? */
1402 if (event.data.control.param == 64) {
1403 if (event.data.control.value == 0)
1404 scherzo_release_pedal (scherzo);
1406 scherzo_press_pedal (scherzo);
1408 fprintf (stderr, "Fixme: Unhandled MIDI Control event (param=%d, value=%d)\n",
1409 event.data.control.param, event.data.control.value);
1413 fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
1420 gtk_widget_queue_draw (scherzo->window);
1422 /* Return TRUE to continue to get called in the future. */
1427 main (int argc, char *argv[])
1429 GtkWidget *drawing_area;
1433 srand (time (NULL));
1435 gtk_init (&argc, &argv);
1437 scherzo.ctx = talloc_new (NULL);
1439 scherzo.score = score_create (scherzo.ctx);
1440 scherzo.staff_height = 100;
1441 score_set_staff_height (scherzo.score, scherzo.staff_height);
1443 score_add_brace (scherzo.score, 2);
1444 scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
1445 scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
1447 scherzo.chord = NULL;
1449 /* Default to octave 4 for computer keyboard keypresses. */
1450 scherzo.keyboard_octave = 4;
1452 pitch_group_init (scherzo.ctx, &scherzo.notes_pressed);
1453 pitch_group_init (scherzo.ctx, &scherzo.notes_pedaled);
1455 scherzo.pedal_pressed = 0;
1457 scherzo.key = PITCH_CLASS_LITERAL (C, NATURAL);
1458 score_set_key (scherzo.score, scherzo.key);
1460 mnemon_init (&scherzo.mnemon);
1461 /* XXX: Should create a default file if one cannot be loaded. */
1462 mnemon_load_category (&scherzo.mnemon, "scherzo-notes");
1464 scherzo.challenge.active = 0;
1466 _select_challenge (&scherzo);
1469 err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
1471 fprintf (stderr, "Out of memory.\n");
1475 #define MIDI_DEVICE "/dev/midi1"
1476 scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
1477 if (scherzo.midi_fd < 0) {
1478 printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
1480 GIOChannel *channel;
1482 channel = g_io_channel_unix_new (scherzo.midi_fd);
1483 g_io_channel_set_encoding (channel, NULL, NULL);
1484 g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
1487 scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1489 gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 1000, 600);
1491 g_signal_connect (scherzo.window, "delete-event",
1492 G_CALLBACK (on_delete_event_quit), NULL);
1494 drawing_area = gtk_drawing_area_new ();
1496 gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
1498 g_signal_connect (drawing_area, "expose-event",
1499 G_CALLBACK (on_expose_event_draw),
1502 g_signal_connect (scherzo.window, "key-press-event",
1503 G_CALLBACK (on_key_press_event),
1506 g_signal_connect (scherzo.window, "key-release-event",
1507 G_CALLBACK (on_key_release_event),
1510 gtk_widget_show_all (scherzo.window);
1514 mnemon_save (&scherzo.mnemon);
1516 snd_midi_event_free (scherzo.snd_midi_event);
1518 talloc_free (scherzo.ctx);