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 on_delete_event_quit (unused (GtkWidget *widget),
105 unused (GdkEvent *event),
106 unused (gpointer user_data))
110 /* Returning FALSE allows the default handler for delete-event
111 * to proceed to cleanup the widget. */
116 on_expose_event_draw (GtkWidget *widget,
117 unused (GdkEventExpose *expose),
120 scherzo_t *scherzo = user_data;
121 score_t *score = scherzo->score;
123 GtkAllocation allocation;
124 static const int pad = 10;
127 gtk_widget_get_allocation (widget, &allocation);
128 widget_width = allocation.width;
130 cr = gdk_cairo_create (widget->window);
132 /* White background */
133 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
136 /* Add some padding on the sides and top */
137 cairo_translate (cr, pad, scherzo->staff_height);
138 score_set_staff_height (score, scherzo->staff_height);
139 score_set_width (score, widget_width - 2 * pad);
141 score_draw (score, cr);
147 on_key_press_event (GtkWidget *widget,
151 scherzo_t *scherzo = user_data;
153 pitch_t pitch = PITCH_NOT_A_PITCH;
155 if (scherzo->challenge.active)
156 octave = PITCH_OCTAVE (scherzo->challenge.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);
178 /* Map a piano keyboard onto the computer keyboard with white keys
179 * along the home row, (piano C on computer A, up to next octave
180 * higher C on computer K). Black keys are in the expected
181 * positions, (C# is W, D# is E, etc.).
185 pitch = PITCH_LITERAL (C, NATURAL, octave);
189 pitch = PITCH_LITERAL (D, NATURAL, octave);
193 pitch = PITCH_LITERAL (E, NATURAL, octave);
197 pitch = PITCH_LITERAL (F, NATURAL, octave);
201 pitch = PITCH_LITERAL (G, NATURAL, octave);
205 pitch = PITCH_LITERAL (A, NATURAL, octave);
209 pitch = PITCH_LITERAL (B, NATURAL, octave);
213 pitch = PITCH_LITERAL (C, NATURAL, octave + 1);
217 pitch = PITCH_LITERAL (D, NATURAL, octave + 1);
219 case GDK_KEY_semicolon:
221 pitch = PITCH_LITERAL (E, NATURAL, octave + 1);
223 case GDK_KEY_apostrophe:
224 case GDK_KEY_quotedbl:
225 pitch = PITCH_LITERAL (F, NATURAL, octave + 1);
229 pitch = PITCH_LITERAL (C, SHARP, octave);
233 pitch = PITCH_LITERAL (D, SHARP, octave);
237 pitch = PITCH_LITERAL (F, SHARP, octave);
241 pitch = PITCH_LITERAL (G, SHARP, octave);
245 pitch = PITCH_LITERAL (A, SHARP, octave);
249 pitch = PITCH_LITERAL (C, SHARP, octave + 1);
253 pitch = PITCH_LITERAL (D, SHARP, octave + 1);
255 case GDK_KEY_bracketright:
256 case GDK_KEY_braceright:
257 pitch = PITCH_LITERAL (F, SHARP, octave + 1);
268 scherzo->keyboard_octave = key->keyval - GDK_KEY_0;
271 scherzo_press_pedal (scherzo);
275 if (pitch != PITCH_NOT_A_PITCH)
277 scherzo_press_note (scherzo, pitch);
278 _judge_pitch (scherzo, pitch);
279 gtk_widget_queue_draw (scherzo->window);
285 /* Allow an unhandled event to propagate to other handlers. */
290 on_key_release_event (unused (GtkWidget *widget),
294 scherzo_t *scherzo = user_data;
296 pitch_t pitch = PITCH_NOT_A_PITCH;
298 if (scherzo->challenge.active)
299 octave = PITCH_OCTAVE (scherzo->challenge.pitch);
301 octave = scherzo->keyboard_octave;
303 switch (key->keyval) {
306 pitch = PITCH_LITERAL (C, NATURAL, octave);
310 pitch = PITCH_LITERAL (D, NATURAL, octave);
314 pitch = PITCH_LITERAL (E, NATURAL, octave);
318 pitch = PITCH_LITERAL (F, NATURAL, octave);
322 pitch = PITCH_LITERAL (G, NATURAL, octave);
326 pitch = PITCH_LITERAL (A, NATURAL, octave);
330 pitch = PITCH_LITERAL (B, NATURAL, octave);
334 pitch = PITCH_LITERAL (C, NATURAL, octave + 1);
338 pitch = PITCH_LITERAL (D, NATURAL, octave + 1);
340 case GDK_KEY_semicolon:
342 pitch = PITCH_LITERAL (E, NATURAL, octave + 1);
344 case GDK_KEY_apostrophe:
345 case GDK_KEY_quotedbl:
346 pitch = PITCH_LITERAL (F, NATURAL, octave + 1);
350 pitch = PITCH_LITERAL (C, SHARP, octave);
354 pitch = PITCH_LITERAL (D, SHARP, octave);
358 pitch = PITCH_LITERAL (F, SHARP, octave);
362 pitch = PITCH_LITERAL (G, SHARP, octave);
366 pitch = PITCH_LITERAL (A, SHARP, octave);
370 pitch = PITCH_LITERAL (C, SHARP, octave + 1);
374 pitch = PITCH_LITERAL (D, SHARP, octave + 1);
376 case GDK_KEY_bracketright:
377 case GDK_KEY_braceright:
378 pitch = PITCH_LITERAL (F, SHARP, octave + 1);
381 scherzo_release_pedal (scherzo);
385 if (pitch != PITCH_NOT_A_PITCH)
387 scherzo_release_note (scherzo, pitch);
388 _score_challenge (scherzo);
389 gtk_widget_queue_draw (scherzo->window);
395 /* Allow an unhandled event to propagate to other handlers. */
400 _pitch_to_midi (pitch_t pitch)
402 int octave = PITCH_OCTAVE (pitch);
403 unsigned char midi_note = 12 * (octave + 1);
405 switch (PITCH_NAME (pitch)) {
428 switch (PITCH_ACCIDENTAL (pitch)) {
429 case PITCH_ACCIDENTAL_DOUBLE_FLAT:
432 case PITCH_ACCIDENTAL_FLAT:
435 case PITCH_ACCIDENTAL_NATURAL:
437 case PITCH_ACCIDENTAL_SHARP:
440 case PITCH_ACCIDENTAL_DOUBLE_SHARP:
448 /* octave can optionally by NULL */
450 _midi_to_pitch (unsigned char midi_note)
452 int octave = octave = midi_note / 12 - 1;
454 switch (midi_note % 12)
458 return PITCH_LITERAL (C, NATURAL, octave);
461 return PITCH_LITERAL (C, SHARP, octave);
464 return PITCH_LITERAL (D, NATURAL, octave);
467 return PITCH_LITERAL (D, SHARP, octave);
470 return PITCH_LITERAL (E, NATURAL, octave);
473 return PITCH_LITERAL (F, NATURAL, octave);
476 return PITCH_LITERAL (F, SHARP, octave);
479 return PITCH_LITERAL (G, NATURAL, octave);
482 return PITCH_LITERAL (G, SHARP, octave);
485 return PITCH_LITERAL (A, NATURAL, octave);
488 return PITCH_LITERAL (A, SHARP, octave);
491 return PITCH_LITERAL (B, NATURAL, octave);
496 /* Return true if 'a' and 'b' sound identical, (even if spelled differently)
498 * This comparison considers octaves as significant. So Bb and A# in
499 * the same octave are considered enharmonic, but Bb and A# in
500 * different octaves are not. */
502 _pitches_are_enharmonic (pitch_t a, pitch_t b)
504 return _pitch_to_midi (a) == _pitch_to_midi (b);
507 /* Determine a chord name (if any) from the current notes pressed */
509 typedef struct analyzed_pitch {
510 /* Original note being analzyed. */
513 /* Absolute pitch (expressed as midi number). */
516 /* Pitch relative to bass note. */
521 _compare_analyzed_pitch_by_midi_pitch (const void *va, const void *vb)
523 const analyzed_pitch_t *a = va, *b = vb;
525 return a->midi_pitch - b->midi_pitch;
529 _compare_analyzed_pitch_by_relative_pitch (const void *va, const void *vb)
531 const analyzed_pitch_t *a = va, *b = vb;
533 return a->relative_pitch - b->relative_pitch;
536 typedef struct modified_degree
543 _modified_degree_to_half_steps (const modified_degree_t *degree)
546 int scale_degree = degree->degree;
548 /* Restrict to actual degrees within a scale. */
549 if (scale_degree > 7)
550 scale_degree = (scale_degree % 8) + 1;
552 /* Number of half steps from root to specified degree within a
554 switch (scale_degree) {
577 fprintf (stderr, "Internal: Invalid degree %d\n", degree->degree);
582 return half_steps + degree->modification;
585 /* Number of half steps from 'root' up to 'pitch' (that is, counting
586 * the steps up a chromatic scale), within the same octave. */
588 _pitch_from_root_in_half_steps (pitch_t pitch, pitch_t root)
590 int root_midi = _pitch_to_midi (root);
591 int pitch_midi = _pitch_to_midi (pitch);
593 while (pitch_midi < root_midi)
596 return (pitch_midi - root_midi) % 12;
600 _chord_signature_matches (analyzed_pitch_t *pitches,
602 modified_degree_t *degrees,
607 #define MAX_DEGREES 6
608 int relative_pitches[MAX_DEGREES];
611 assert (num_degrees <= MAX_DEGREES);
613 if (num_pitches != num_degrees)
616 if (inversion >= num_degrees)
619 /* We never spell simple intervals as inversions. */
620 if (num_degrees == 2 && inversion > 0)
623 for (i = 0; i < num_degrees; i++) {
624 /* The num_degrees is in the addition just to ensure all
625 * inputs to the modulus operator remain positive. */
626 int index = (i + num_degrees - inversion) % num_degrees;
628 /* Again, adding a 12 to keep things positive. */
629 relative_pitches[index] =
631 _modified_degree_to_half_steps (°rees[i]) -
632 _modified_degree_to_half_steps (°rees[inversion])) % 12;
636 for (i = 0; i < num_pitches; i++)
637 if (pitches[i].relative_pitch != relative_pitches[i])
640 root_index = (num_pitches - inversion) % num_pitches;
641 *root = pitches[root_index].pitch;
646 typedef struct chord_signature
649 modified_degree_t degrees[MAX_DEGREES];
653 /* XXX: The superscript rise value should be relative to the current
654 * font size. Best would be for pango to support this. See:
656 * https://bugzilla.gnome.org/show_bug.cgi?id=708780
658 * For now, these are emipirically-derived values that look reasonable
659 * at the default size that score.c is using to draw the staff. If the
660 * user scales the staff up or down then these will look wrong.
663 #define SUP "<span font='33' rise='30000'>"
664 #define PUS "</span>"
666 chord_signature_t signatures[] = {
668 { 1, {{1, 0}}, "P8" },
671 { 2, {{1, 0}, {2, -1}}, "m2" },
672 { 2, {{1, 0}, {2, 0}}, "M2" },
673 { 2, {{1, 0}, {3, -1}}, "m3" },
674 { 2, {{1, 0}, {3, 0}}, "M3" },
675 { 2, {{1, 0}, {4, 0}}, "P4" },
676 { 2, {{1, 0}, {5, -1}}, "Tri-tone" },
677 { 2, {{1, 0}, {5, 0}}, "P5" },
678 { 2, {{1, 0}, {6, -1}}, "m6" },
679 { 2, {{1, 0}, {6, 0}}, "M6" },
680 { 2, {{1, 0}, {7, -1}}, "m7" },
681 { 2, {{1, 0}, {7, 0}}, "M7" },
684 { 3, {{1, 0}, {4, 0}, {5, 0}}, "sus" },
685 { 3, {{1, 0}, {3, 0}, {5, +1}}, SUP "+" PUS },
686 { 3, {{1, 0}, {3, 0}, {5, 0}}, "" },
687 { 3, {{1, 0}, {3, -1}, {5, 0}}, "m" },
688 { 3, {{1, 0}, {3, -1}, {5, -1}}, "°" },
689 { 3, {{1, 0}, {2, 0}, {5, 0}}, "msus2" },
691 /* Seventh chords with no 3rd */
692 { 3, {{1, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS },
693 { 3, {{1, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS },
694 { 3, {{1, 0}, {5, 0}, {7, 0}}, "M7" },
695 { 3, {{1, 0}, {5, 0}, {7, -1}}, "7" },
696 { 3, {{1, 0}, {5, -1}, {7, -1}}, "7♭5" },
698 /* Seventh chords with no 5th */
699 { 3, {{1, 0}, {4, 0}, {7, 0}}, "M7sus" },
700 { 3, {{1, 0}, {4, 0}, {7, -1}}, "7sus" },
701 { 3, {{1, 0}, {3, 0}, {7, 0}}, "M7" },
702 { 3, {{1, 0}, {3, 0}, {7, -1}}, "7" },
703 { 3, {{1, 0}, {3, -1}, {7, 0}}, "m" SUP "M7" PUS },
704 { 3, {{1, 0}, {3, -1}, {7, -1}}, "m7" },
705 { 3, {{1, 0}, {2, 0}, {7, 0}}, "m" SUP "M7" PUS "sus2" },
706 { 3, {{1, 0}, {2, 0}, {7, -1}}, "m7sus2" },
709 { 4, {{1, 0}, {4, 0}, {5, 0}, {6, 0}}, "6sus" },
710 { 4, {{1, 0}, {3, 0}, {5, 0}, {6, 0}}, "6" }, /* Ambiguous with m7 */
711 { 4, {{1, 0}, {3, -1}, {5, 0}, {6, 0}}, "m6" },
712 { 4, {{1, 0}, {2, 0}, {5, 0}, {6, 0}}, "m6sus2" },
715 { 4, {{1, 0}, {4, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS "sus" },
716 { 4, {{1, 0}, {4, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS "sus" },
717 { 4, {{1, 0}, {4, 0}, {5, 0}, {7, 0}}, "M7sus" },
718 { 4, {{1, 0}, {4, 0}, {5, 0}, {7, -1}}, "7sus" },
719 { 4, {{1, 0}, {4, 0}, {5, -1}, {7, -1}}, "7♭5sus" },
720 { 4, {{1, 0}, {3, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS },
721 { 4, {{1, 0}, {3, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS },
722 { 4, {{1, 0}, {3, 0}, {5, 0}, {7, 0}}, "M7" },
723 { 4, {{1, 0}, {3, 0}, {5, 0}, {7, -1}}, "7" },
724 { 4, {{1, 0}, {3, 0}, {5, -1}, {7, -1}}, "7♭5" },
725 { 4, {{1, 0}, {3, -1}, {5, 0}, {7, 0}}, "m" SUP "M7" PUS },
726 { 4, {{1, 0}, {3, -1}, {5, 0}, {7, -1}}, "m7" },
727 { 4, {{1, 0}, {3, -1}, {5, -1}, {7, 0}}, "°" SUP "M7" PUS },
728 { 4, {{1, 0}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "7" PUS },
729 { 4, {{1, 0}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "7" PUS },
730 { 4, {{1, 0}, {2, 0}, {5, 0}, {7, 0}}, "m" SUP "M7" PUS "sus2" },
731 { 4, {{1, 0}, {2, 0}, {5, 0}, {7, -1}}, "m7sus2" },
732 { 4, {{1, 0}, {2, 0}, {5, -1}, {7, 0}}, "°" SUP "M7" PUS "sus2" },
733 { 4, {{1, 0}, {2, 0}, {5, -1}, {7, -1}}, "𝆩" SUP "7" PUS "sus2" },
734 { 4, {{1, 0}, {2, 0}, {5, -1}, {7, -2}}, "°" SUP "7" PUS "sus2" }, /* Ambiguous with 7 */
736 /* The sorting for chords with degree higher than 9 is funny
737 * to keep the array in degree order after reducing each
738 * degree to an actual scale degree, (9 -> 2, 11 -> 4, 13 ->
741 /* Ninth chords voiced with no 5th */
742 { 4, {{1, 0}, {9, 0}, {4, 0}, {7, 0}}, "M9sus" },
743 { 4, {{1, 0}, {9, 0}, {4, 0}, {7, -1}}, "9sus" },
744 { 4, {{1, 0}, {9, 0}, {3, 0}, {7, 0}}, "M9" },
745 { 4, {{1, 0}, {9, 0}, {3, 0}, {7, -1}}, "9" },
746 { 4, {{1, 0}, {9, 0}, {3, -1}, {7, 0}}, "m" SUP "M9" PUS },
747 { 4, {{1, 0}, {9, 0}, {3, -1}, {7, -1}}, "m9" },
748 { 4, {{1, 0}, {9, -1}, {3, -1}, {7, -1}}, "m♭9" },
749 { 4, {{1, 0}, {9, 0}, {3, -1}, {6, 0}}, "m6/9" },
750 { 4, {{1, 0}, {9, -1}, {3, -1}, {6, 0}}, "m6/♭9" },
753 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {6, 0}}, "6/9" },
754 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {6, 0}}, "m6/9" },
755 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, 0}, {6, 0}}, "m6/♭9" },
757 /* Seventh plus altered 9 */
758 { 5, {{1, 0}, {9, +1}, {3, 0}, {5, 0}, {7, -1}}, "7♯9" },
759 { 5, {{1, 0}, {9, -1}, {3, 0}, {5, 0}, {7, -1}}, "7♭9" },
760 { 5, {{1, 0}, {9, +1}, {3, 0}, {5, -1}, {7, -1}}, "7♭5♯9" },
761 { 5, {{1, 0}, {9, -1}, {3, 0}, {5, -1}, {7, -1}}, "7♭5♭9" },
764 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, +1}, {7, 0}}, SUP "+M9" PUS "sus" },
765 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, +1}, {7, -1}}, SUP "+9" PUS "sus" },
766 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, 0}, {7, 0}}, "M9sus" },
767 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, 0}, {7, -1}}, "9sus" },
768 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, +1}, {7, 0}}, SUP "+M9" PUS },
769 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, +1}, {7, -1}}, SUP "+9" PUS },
770 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {7, 0}}, "M9" },
771 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {7, -1}}, "9" },
772 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {7, 0}}, "m" SUP "M9" PUS },
773 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {7, -1}}, "m9" },
774 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, 0}, {7, -1}}, "m♭9" },
775 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, -1}, {7, 0}}, "M9" SUP "♭5" PUS },
776 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, -1}, {7, -1}}, "9" SUP "♭5" PUS },
777 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, 0}}, "m" SUP "M9♭5" PUS },
778 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "9" PUS },
779 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "♭9" PUS },
780 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "9" PUS },
781 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "♭9" PUS },
783 /* Eleventh chords */
784 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, +1}, {7, 0}}, SUP "+M11" PUS },
785 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, +1}, {7, -1}}, SUP "+11" PUS },
786 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, 0}, {7, 0}}, "M11" },
787 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, 0}, {7, -1}}, "11" },
788 { 6, {{1, 0}, {9, 0}, {3, -1}, {11, 0}, {5, 0}, {7, 0}}, "m" SUP "M11" PUS },
789 { 6, {{1, 0}, {9, 0}, {3, -1}, {11, 0}, {5, 0}, {7, -1}}, "m11" },
790 { 6, {{1, 0}, {9, -1}, {3, -1}, {11, 0}, {5, -1}, {7, -1}}, "𝆩" SUP "11" PUS },
791 { 6, {{1, 0}, {9, -1}, {3, -1}, {11, -1}, {5, -1}, {7, -2}}, "°" SUP "11" PUS }
795 scherzo_adjust_pitch_to_match_modified_degree (pitch_t *pitch,
797 modified_degree_t *degree)
799 pitch_name_t name = PITCH_NAME (*pitch);
800 pitch_accidental_t accidental = PITCH_ACCIDENTAL (*pitch);
801 int octave = PITCH_OCTAVE (*pitch);
802 int degree_zero_based = (degree->degree - 1) % 7;
805 int note_degree_zero_based = name - PITCH_NAME (root);
806 if (note_degree_zero_based < 0)
807 note_degree_zero_based += 7;
809 if (note_degree_zero_based == degree_zero_based)
812 degree_delta = note_degree_zero_based - degree_zero_based;
813 if (degree_delta > 3)
814 degree_delta = - (7 - degree_delta);
815 if (degree_delta < -3)
816 degree_delta = - (-7 - degree_delta);
818 if (abs (degree_delta) != 1) {
819 fprintf (stderr, "Internal error: Cannot adjust a note more than one degree (%d vs. %d).\n", note_degree_zero_based + 1, degree->degree);
823 if (degree_delta == -1) {
824 if (name == PITCH_NAME_B) {
845 if (degree_delta == +1) {
846 if (name == PITCH_NAME_C) {
866 if (accidental < 0 || accidental > PITCH_ACCIDENTAL_DOUBLE_SHARP) {
867 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);
871 *pitch = PITCH (name, accidental, octave);
875 _spell_chord_by_signature (pitch_group_t *chord,
876 chord_signature_t *signature,
879 int i, degree, note_half_steps, degree_half_steps;
882 /* Sanitize root to eliminate things like double-flats,
883 * double-sharps, Cb, E#, etc. */
884 root_midi = _pitch_to_midi (root);
885 root = _midi_to_pitch (root_midi);
887 for (i = 0; i < chord->num_pitches; i++) {
888 note_half_steps = _pitch_from_root_in_half_steps (chord->pitches[i],
890 for (degree = 0; degree < signature->num_degrees; degree++) {
891 degree_half_steps = _modified_degree_to_half_steps (&signature->degrees[degree]);
892 if (note_half_steps == degree_half_steps) {
893 scherzo_adjust_pitch_to_match_modified_degree (&chord->pitches[i],
895 &signature->degrees[degree]);
899 if (note_half_steps != degree_half_steps) {
900 fprintf (stderr, "Internal error: Chord and degree mis-match\n");
907 _analyze_chord (pitch_group_t *chord,
908 chord_signature_t **signature_ret,
911 void *local = talloc_new (NULL);
912 analyzed_pitch_t *pitches;
913 int i, j, num_pitches;
914 int bass_pitch, inversion;
916 chord_signature_t *match = NULL;
918 /* Default to no-match for any early return */
919 *signature_ret = NULL;
922 num_pitches = chord->num_pitches;
924 if (chord->num_pitches <= 1)
927 pitches = talloc_array (local, analyzed_pitch_t, num_pitches);
931 for (i = 0; i < num_pitches; i++) {
932 pitch_t pitch = chord->pitches[i];
933 pitches[i].pitch = pitch;
934 pitches[i].midi_pitch = _pitch_to_midi (pitch);
935 /* Relative pitch will be filled in after sorting. */
936 pitches[i].relative_pitch = 0;
939 /* First, sort by midi pitch to find the bass note. */
940 qsort (pitches, num_pitches, sizeof (analyzed_pitch_t),
941 _compare_analyzed_pitch_by_midi_pitch);
943 bass_pitch = pitches[0].midi_pitch;
945 /* With the bass note identified, we can find all relative pitches. */
946 for (i = 0; i < num_pitches; i++) {
947 pitches[i].relative_pitch = pitches[i].midi_pitch - bass_pitch;
948 while (pitches[i].relative_pitch >= 12)
949 pitches[i].relative_pitch -= 12;
952 /* Now, sort again by relative pitch. */
953 qsort (pitches, num_pitches, sizeof (analyzed_pitch_t),
954 _compare_analyzed_pitch_by_relative_pitch);
956 /* Finally, eliminate all duplicate notes. */
957 for (i = 0; i < num_pitches - 1; i++) {
958 if (pitches[i+1].relative_pitch == pitches[i].relative_pitch) {
960 while (j < num_pitches &&
961 pitches[j].relative_pitch == pitches[i].relative_pitch)
965 /* The loop incremented j one past the last
966 * duplicate. Decrement so that it points to the
967 * last duplicate (and is guaranteed to not exceed
968 * the array bounds).*/
971 if (j < num_pitches - 1) {
972 memmove (&pitches[i+1], &pitches[j+1],
973 (num_pitches - j) * sizeof (analyzed_pitch_t));
976 num_pitches -= (j - i);
980 for (inversion = 0; inversion < 4; inversion++) {
981 for (i = 0; i < ARRAY_SIZE (signatures); i++) {
982 if (_chord_signature_matches (pitches, num_pitches,
983 signatures[i].degrees,
984 signatures[i].num_degrees,
987 match = &signatures[i];
995 *signature_ret = match;
996 for (i = 0; i < chord->num_pitches; i++) {
997 if (chord->pitches[i] == root) {
998 *root_ret = &chord->pitches[i];
1005 talloc_free (local);
1009 pitch_group_init (void *ctx, pitch_group_t *group)
1012 group->pitches = NULL;
1014 group->num_pitches = 0;
1018 pitch_group_add_pitch (pitch_group_t *group, pitch_t pitch)
1022 /* Do nothing if pitch is already in group. */
1023 for (i = 0; i < group->num_pitches; i++)
1024 if (group->pitches[i] == pitch)
1027 group->num_pitches++;
1029 if (group->num_pitches > group->size) {
1031 group->pitches = talloc_realloc (group->ctx, group->pitches,
1032 pitch_t, group->size);
1034 if (group->pitches == NULL) {
1035 fprintf (stderr, "Out of memory.\n");
1040 group->pitches[group->num_pitches - 1] = pitch;
1044 pitch_group_remove_pitch_at (pitch_group_t *group, int i)
1046 if (i > group->num_pitches) {
1047 fprintf (stderr, "Internal error: Cannot remove pitch %d from group with only %d %s\n",
1048 i, group->num_pitches,
1049 group->num_pitches == 1 ? "pitch" : "pitches");
1053 if (i < group->num_pitches - 1) {
1054 memmove (group->pitches + i, group->pitches + i + 1,
1055 (group->num_pitches - 1 - i) * sizeof (pitch_t));
1058 group->num_pitches--;
1061 /* Remove a 'pitch' from 'group', (including any enharmonic pitch)
1062 * See also: pitch_group_remove_pitch
1065 pitch_group_remove_pitch_enharmonic (pitch_group_t *group, pitch_t pitch)
1069 for (i = 0; i < group->num_pitches; i++)
1070 if (_pitches_are_enharmonic (group->pitches[i], pitch))
1071 pitch_group_remove_pitch_at (group, i);
1074 /* Remove an exactly-matching pitch from 'group'
1075 * See also: pitch_group_remove_pitch_enharmonic
1078 pitch_group_remove_pitch (pitch_group_t *group, pitch_t pitch)
1082 for (i = 0; i < group->num_pitches; i++)
1083 if (group->pitches[i] == pitch)
1084 pitch_group_remove_pitch_at (group, i);
1089 scherzo_update_notes_and_chord (scherzo_t *scherzo)
1091 if (scherzo->notes_pressed.num_pitches == 0 &&
1092 scherzo->notes_pedaled.num_pitches == 0)
1094 score_remove_notes (scherzo->score);
1096 score_remove_chords (scherzo->score);
1098 gtk_widget_queue_draw (scherzo->window);
1103 scherzo_press_note (scherzo_t *scherzo, pitch_t pitch)
1105 void *local = talloc_new (NULL);
1106 pitch_group_t *chord;
1107 chord_signature_t *signature;
1108 char *chord_name = NULL;
1112 /* Do nothing if this note is already pressed. */
1113 for (i = 0; i < scherzo->notes_pressed.num_pitches; i++)
1114 if (scherzo->notes_pressed.pitches[i] == pitch)
1117 pitch_group_add_pitch (&scherzo->notes_pressed, pitch);
1119 if (scherzo->pedal_pressed)
1120 pitch_group_add_pitch (&scherzo->notes_pedaled, pitch);
1122 /* Remove all notes/chords from score, then add current notes. */
1123 score_remove_notes (scherzo->score);
1124 score_remove_chords (scherzo->score);
1126 if (scherzo->pedal_pressed)
1127 chord = &scherzo->notes_pedaled;
1129 chord = &scherzo->notes_pressed;
1131 _analyze_chord (chord, &signature, &root);
1134 /* Don't print root pitch for octaves and inversions,
1135 * (since a pitch name alone looks like a major triad) */
1136 if (signature->num_degrees < 3) {
1137 chord_name = talloc_strdup (local, signature->name);
1139 _spell_chord_by_signature (&scherzo->notes_pressed,
1141 _spell_chord_by_signature (&scherzo->notes_pedaled,
1144 chord_name = talloc_asprintf (local, "%s%s",
1145 pitch_string (*root),
1148 } else if (chord->num_pitches > 2) {
1149 chord_name = talloc_strdup (local, "?");
1153 scherzo->chord = score_add_chord (scherzo->treble, chord_name);
1155 for (i = 0; i < scherzo->notes_pressed.num_pitches; i++)
1157 score_add_note (scherzo->score, scherzo->notes_pressed.pitches[i],
1158 SCORE_DURATION_WHOLE);
1161 for (i = 0; i < scherzo->notes_pedaled.num_pitches; i++)
1163 score_add_note (scherzo->score, scherzo->notes_pedaled.pitches[i],
1164 SCORE_DURATION_WHOLE);
1167 talloc_free (local);
1171 scherzo_release_note (scherzo_t *scherzo, pitch_t pitch)
1173 pitch_group_remove_pitch_enharmonic (&scherzo->notes_pressed, pitch);
1175 if (scherzo->notes_pressed.num_pitches == 0)
1176 scherzo_update_notes_and_chord (scherzo);
1180 scherzo_press_note_midi (scherzo_t *scherzo, unsigned char midi_note)
1182 pitch_t pitch = _midi_to_pitch (midi_note);
1184 scherzo_press_note (scherzo, pitch);
1190 scherzo_release_note_midi (scherzo_t *scherzo, unsigned char midi_note)
1192 scherzo_release_note (scherzo, _midi_to_pitch (midi_note));
1196 scherzo_press_pedal (scherzo_t *scherzo)
1200 if (scherzo->pedal_pressed)
1203 scherzo->pedal_pressed = 1;
1205 /* Copy all pressed notes to pedaled notes */
1206 for (i = 0; i < scherzo->notes_pressed.num_pitches; i++)
1207 pitch_group_add_pitch (&scherzo->notes_pedaled, scherzo->notes_pressed.pitches[i]);
1211 scherzo_release_pedal (scherzo_t *scherzo)
1215 if (! scherzo->pedal_pressed)
1218 /* Remove all previously pedaled notes. */
1219 for (i = scherzo->notes_pedaled.num_pitches - 1; i >=0; i--)
1220 pitch_group_remove_pitch_at (&scherzo->notes_pedaled, i);
1222 scherzo->pedal_pressed = 0;
1224 scherzo_update_notes_and_chord (scherzo);
1228 _select_challenge (scherzo_t *scherzo)
1230 category_t *category_unused;
1231 bool_t introduced_unused;
1233 challenge_t *challenge = &scherzo->challenge;
1234 pitch_name_t pitch_name;
1238 mnemon_select_item (&scherzo->mnemon,
1240 &challenge->item_index,
1242 &introduced_unused);
1244 item = challenge->bin->items[challenge->item_index];
1246 s = item->challenge;
1247 if (strncmp (s, "treble:", 7) == 0) {
1249 challenge->staff = scherzo->treble;
1250 } else if (strncmp (s, "bass:", 5) == 0) {
1252 challenge->staff = scherzo->bass;
1255 "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
1262 pitch_name = PITCH_NAME_C;
1265 pitch_name = PITCH_NAME_D;
1268 pitch_name = PITCH_NAME_E;
1271 pitch_name = PITCH_NAME_F;
1274 pitch_name = PITCH_NAME_G;
1277 pitch_name = PITCH_NAME_A;
1280 pitch_name = PITCH_NAME_B;
1283 fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
1288 if (*s < '0' || *s > '9') {
1289 fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
1295 challenge->pitch = PITCH (pitch_name, PITCH_ACCIDENTAL_NATURAL, octave);
1297 challenge->active = 1;
1298 challenge->satisfied = 0;
1299 challenge->mistaken = 0;
1302 /* Determine whether the user hit the correct note. */
1304 _judge_pitch (scherzo_t *scherzo, pitch_t pitch)
1306 challenge_t *challenge = &scherzo->challenge;
1308 if (! challenge->active)
1311 if (pitch == challenge->pitch)
1312 challenge->satisfied = 1;
1314 challenge->mistaken = 1;
1317 /* If the user got the right note (eventually), then score it in
1318 * mnemon and show the next note. */
1320 _score_challenge (scherzo_t *scherzo)
1322 challenge_t *challenge = &scherzo->challenge;
1324 if (! challenge->active)
1327 if (! challenge->satisfied)
1330 mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
1331 ! challenge->mistaken);
1333 _select_challenge (scherzo);
1337 on_midi_input (unused (GIOChannel *channel),
1338 unused (GIOCondition condition),
1341 unsigned char buf[MIDI_BUF_SIZE], *next;
1342 scherzo_t *scherzo = user_data;
1344 snd_seq_event_t event;
1346 int need_redraw = FALSE;
1348 remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
1354 consumed = snd_midi_event_encode (scherzo->snd_midi_event,
1355 next, remaining, &event);
1357 remaining -= consumed;
1360 switch (event.type) {
1361 case SND_SEQ_EVENT_NONE:
1362 /* Incomplete event. Nothing to do. */
1364 case SND_SEQ_EVENT_NOTEON:
1365 pitch = scherzo_press_note_midi (scherzo, event.data.note.note);
1366 _judge_pitch (scherzo, pitch);
1369 case SND_SEQ_EVENT_NOTEOFF:
1370 scherzo_release_note_midi (scherzo, event.data.note.note);
1371 _score_challenge (scherzo);
1374 case SND_SEQ_EVENT_CLOCK:
1375 /* Ignore for now as my piano sends a constant stream of these. */
1377 case SND_SEQ_EVENT_SENSING:
1378 /* Ignore for now as my piano sends a constant stream of these. */
1380 case SND_SEQ_EVENT_CONTROLLER:
1381 /* XXX: My piano gives me 64 for the sustain pedal, is
1382 * that universal? */
1383 if (event.data.control.param == 64) {
1384 if (event.data.control.value == 0)
1385 scherzo_release_pedal (scherzo);
1387 scherzo_press_pedal (scherzo);
1389 fprintf (stderr, "Fixme: Unhandled MIDI Control event (param=%d, value=%d)\n",
1390 event.data.control.param, event.data.control.value);
1394 fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
1401 gtk_widget_queue_draw (scherzo->window);
1403 /* Return TRUE to continue to get called in the future. */
1408 main (int argc, char *argv[])
1410 GtkWidget *drawing_area;
1414 srand (time (NULL));
1416 gtk_init (&argc, &argv);
1418 scherzo.ctx = talloc_new (NULL);
1420 scherzo.score = score_create (scherzo.ctx);
1421 scherzo.staff_height = 100;
1422 score_set_staff_height (scherzo.score, scherzo.staff_height);
1424 score_add_brace (scherzo.score, 2);
1425 scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
1426 scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
1428 scherzo.chord = NULL;
1430 /* Default to octave 4 for computer keyboard keypresses. */
1431 scherzo.keyboard_octave = 4;
1433 pitch_group_init (scherzo.ctx, &scherzo.notes_pressed);
1434 pitch_group_init (scherzo.ctx, &scherzo.notes_pedaled);
1436 scherzo.pedal_pressed = 0;
1438 scherzo.key = PITCH_CLASS_LITERAL (C, NATURAL);
1439 score_set_key (scherzo.score, scherzo.key);
1441 mnemon_init (&scherzo.mnemon);
1442 /* XXX: Should create a default file if one cannot be loaded. */
1443 mnemon_load_category (&scherzo.mnemon, "scherzo-notes");
1445 scherzo.challenge.active = 0;
1447 _select_challenge (&scherzo);
1450 err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
1452 fprintf (stderr, "Out of memory.\n");
1456 #define MIDI_DEVICE "/dev/midi1"
1457 scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
1458 if (scherzo.midi_fd < 0) {
1459 printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
1461 GIOChannel *channel;
1463 channel = g_io_channel_unix_new (scherzo.midi_fd);
1464 g_io_channel_set_encoding (channel, NULL, NULL);
1465 g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
1468 scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1470 gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 1000, 600);
1472 g_signal_connect (scherzo.window, "delete-event",
1473 G_CALLBACK (on_delete_event_quit), NULL);
1475 drawing_area = gtk_drawing_area_new ();
1477 gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
1479 g_signal_connect (drawing_area, "expose-event",
1480 G_CALLBACK (on_expose_event_draw),
1483 g_signal_connect (scherzo.window, "key-press-event",
1484 G_CALLBACK (on_key_press_event),
1487 g_signal_connect (scherzo.window, "key-release-event",
1488 G_CALLBACK (on_key_release_event),
1491 gtk_widget_show_all (scherzo.window);
1495 mnemon_save (&scherzo.mnemon);
1497 snd_midi_event_free (scherzo.snd_midi_event);
1499 talloc_free (scherzo.ctx);