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 score_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, score_pitch_t pitch, int octave);
86 scherzo_release_note (scherzo_t *scherzo, score_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 score_pitch_name_t pitch_name = SCORE_PITCH_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 = SCORE_PITCH_NAME_C;
185 pitch_name = SCORE_PITCH_NAME_D;
189 pitch_name = SCORE_PITCH_NAME_E;
193 pitch_name = SCORE_PITCH_NAME_F;
197 pitch_name = SCORE_PITCH_NAME_G;
201 pitch_name = SCORE_PITCH_NAME_A;
205 pitch_name = SCORE_PITCH_NAME_B;
216 scherzo->keyboard_octave = key->keyval - GDK_KEY_0;
219 scherzo_press_pedal (scherzo);
222 if (scherzo->keyboard_accidental < SCORE_PITCH_ACCIDENTAL_DOUBLE_SHARP)
223 scherzo->keyboard_accidental++;
226 if (scherzo->keyboard_accidental > SCORE_PITCH_ACCIDENTAL_DOUBLE_FLAT)
227 scherzo->keyboard_accidental--;
231 pitch = SCORE_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 score_pitch_name_t pitch_name = SCORE_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 = SCORE_PITCH_NAME_C;
273 pitch_name = SCORE_PITCH_NAME_D;
277 pitch_name = SCORE_PITCH_NAME_E;
281 pitch_name = SCORE_PITCH_NAME_F;
285 pitch_name = SCORE_PITCH_NAME_G;
289 pitch_name = SCORE_PITCH_NAME_A;
293 pitch_name = SCORE_PITCH_NAME_B;
296 scherzo_release_pedal (scherzo);
300 pitch = SCORE_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 _score_pitch_and_octave_to_midi (score_pitch_t pitch,
321 unsigned char midi_note = 12 * (octave + 1);
323 switch (SCORE_PITCH_NAME (pitch)) {
324 case SCORE_PITCH_NAME_C:
326 case SCORE_PITCH_NAME_D:
329 case SCORE_PITCH_NAME_E:
332 case SCORE_PITCH_NAME_F:
335 case SCORE_PITCH_NAME_G:
338 case SCORE_PITCH_NAME_A:
341 case SCORE_PITCH_NAME_B:
346 switch (SCORE_PITCH_ACCIDENTAL (pitch)) {
347 case SCORE_PITCH_ACCIDENTAL_DOUBLE_FLAT:
350 case SCORE_PITCH_ACCIDENTAL_FLAT:
353 case SCORE_PITCH_ACCIDENTAL_NATURAL:
355 case SCORE_PITCH_ACCIDENTAL_SHARP:
358 case SCORE_PITCH_ACCIDENTAL_DOUBLE_SHARP:
367 _midi_to_score_pitch_and_octave (unsigned char midi_note,
368 score_pitch_t *pitch,
371 *octave = midi_note / 12 - 1;
373 switch (midi_note % 12)
376 *pitch = SCORE_PITCH_C;
379 *pitch = SCORE_PITCH_Cs;
382 *pitch = SCORE_PITCH_D;
385 *pitch = SCORE_PITCH_Ds;
388 *pitch = SCORE_PITCH_E;
391 *pitch = SCORE_PITCH_F;
394 *pitch = SCORE_PITCH_Fs;
397 *pitch = SCORE_PITCH_G;
400 *pitch = SCORE_PITCH_Gs;
403 *pitch = SCORE_PITCH_A;
406 *pitch = SCORE_PITCH_As;
409 *pitch = SCORE_PITCH_B;
414 /* Determine a chord name (if any) from the current notes pressed */
416 typedef struct analyzed_note {
417 /* Original note being analzyed. */
420 /* Absolute pitch (expressed as midi number). */
423 /* Pitch relative to bass note. */
428 _compare_analyzed_note_by_midi_pitch (const void *va, const void *vb)
430 const analyzed_note_t *a = va, *b = vb;
432 return a->midi_pitch - b->midi_pitch;
436 _compare_analyzed_note_by_relative_pitch (const void *va, const void *vb)
438 const analyzed_note_t *a = va, *b = vb;
440 return a->relative_pitch - b->relative_pitch;
443 typedef struct modified_degree
450 _modified_degree_to_half_steps (const modified_degree_t *degree)
453 int scale_degree = degree->degree;
455 /* Restrict to actual degrees within a scale. */
456 if (scale_degree > 7)
457 scale_degree = (scale_degree % 8) + 1;
459 /* Number of half steps from root to specified degree within a
460 * diatonic scaled. */
461 switch (scale_degree) {
484 fprintf (stderr, "Internal: Invalid degree %d\n", degree->degree);
489 return half_steps + degree->modification;
493 _chord_signature_matches (analyzed_note_t *notes,
495 modified_degree_t *degrees,
500 #define MAX_DEGREES 6
501 int relative_pitches[MAX_DEGREES];
504 assert (num_degrees <= MAX_DEGREES);
506 if (num_notes != num_degrees)
509 if (inversion >= num_degrees)
512 /* We never spell simple intervals as inversions. */
513 if (num_degrees == 2 && inversion > 0)
516 for (i = 0; i < num_degrees; i++) {
517 /* The num_degrees is in the addition just to ensure all
518 * inputs to the modulus operator remain positive. */
519 int index = (i + num_degrees - inversion) % num_degrees;
521 /* Again, adding a 12 to keep things positive. */
522 relative_pitches[index] =
524 _modified_degree_to_half_steps (°rees[i]) -
525 _modified_degree_to_half_steps (°rees[inversion])) % 12;
529 for (i = 0; i < num_notes; i++)
530 if (notes[i].relative_pitch != relative_pitches[i])
533 root_index = (num_notes - inversion) % num_notes;
534 *root = notes[root_index].note->pitch;
540 _pitch_str (score_pitch_t pitch)
543 case SCORE_PITCH_Cff:
551 case SCORE_PITCH_Css:
553 case SCORE_PITCH_Dff:
561 case SCORE_PITCH_Dss:
563 case SCORE_PITCH_Eff:
571 case SCORE_PITCH_Ess:
573 case SCORE_PITCH_Fff:
581 case SCORE_PITCH_Fss:
583 case SCORE_PITCH_Gff:
591 case SCORE_PITCH_Gss:
593 case SCORE_PITCH_Aff:
601 case SCORE_PITCH_Ass:
603 case SCORE_PITCH_Bff:
611 case SCORE_PITCH_Bss:
615 fprintf (stderr, "Internal error: Unknown pitch %d\n", pitch);
620 scherzo_analyze_chord (scherzo_t *scherzo)
622 void *local = talloc_new (NULL);
623 analyzed_note_t *notes;
624 note_group_t *note_group;
625 unsigned i, j, num_notes;
626 int bass_pitch, inversion;
628 const char *chord_name = NULL;
630 if (scherzo->pedal_pressed)
631 note_group = &scherzo->notes_pedaled;
633 note_group = &scherzo->notes_pressed;
635 num_notes = note_group->num_notes;
637 struct { modified_degree_t degrees[1]; const char *name; } octaves[] = {
638 { {{1, 0}}, " Octave"}
641 struct { modified_degree_t degrees[2]; const char *name; } intervals[] = {
642 { {{1, 0}, {2, -1}}, " Minor 2nd"},
643 { {{1, 0}, {2, 0}}, " Major 2nd"},
644 { {{1, 0}, {3, -1}}, " Minor 3rd"},
645 { {{1, 0}, {3, 0}}, " Major 3rd"},
646 { {{1, 0}, {4, 0}}, " Perfect 4th"},
647 { {{1, 0}, {5, -1}}, " Diminished 5th"},
648 { {{1, 0}, {5, 0}}, " Perfect 5th"},
649 { {{1, 0}, {6, -1}}, " Minor 6th"},
650 { {{1, 0}, {6, 0}}, " Major 6th"},
651 { {{1, 0}, {7, -1}}, " Minor 7th"},
652 { {{1, 0}, {7, 0}}, " Major 7th"}
655 /* XXX: The superscript rise value should be relative to the current
656 * font size. Best would be for pango to support this. See:
658 * https://bugzilla.gnome.org/show_bug.cgi?id=708780
660 * For now, these are emipirically-derived values that look reasonable
661 * at the default size that score.c is using to draw the staff. If the
662 * user scales the staff up or down then these will look wrong.
665 #define SUP "<span font='33' rise='30000'>"
666 #define PUS "</span>"
668 struct { modified_degree_t degrees[3]; const char *name; } triads[] = {
669 { {{1, 0}, {3, +1}, {5, 0}}, "sus" },
670 { {{1, 0}, {3, 0}, {5, +1}}, SUP "+" PUS },
671 { {{1, 0}, {3, 0}, {5, 0}}, "" },
672 { {{1, 0}, {3, -1}, {5, 0}}, "m" },
673 { {{1, 0}, {3, -1}, {5, -1}}, "°" },
674 { {{1, 0}, {2, 0}, {5, 0}}, "msus2" }
677 struct { modified_degree_t degrees[4]; const char *name; } tetrachords[] = {
679 { {{1, 0}, {4, 0}, {5, 0}, {6, 0}}, "6sus" },
680 { {{1, 0}, {3, 0}, {5, 0}, {6, 0}}, "6" }, /* Ambiguous with m7 */
681 { {{1, 0}, {3, -1}, {5, 0}, {6, 0}}, "m6" },
682 { {{1, 0}, {2, 0}, {5, 0}, {6, 0}}, "m6sus2" },
684 { {{1, 0}, {4, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS "sus" },
685 { {{1, 0}, {4, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS "sus" },
686 { {{1, 0}, {4, 0}, {5, 0}, {7, 0}}, "M7sus" },
687 { {{1, 0}, {4, 0}, {5, 0}, {7, -1}}, "7sus" },
688 { {{1, 0}, {4, 0}, {5, -1}, {7, -1}}, "7♭5sus" },
689 { {{1, 0}, {3, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS },
690 { {{1, 0}, {3, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS },
691 { {{1, 0}, {3, 0}, {5, 0}, {7, 0}}, "M7" },
692 { {{1, 0}, {3, 0}, {5, 0}, {7, -1}}, "7" },
693 { {{1, 0}, {3, 0}, {5, -1}, {7, -1}}, "7♭5" },
694 { {{1, 0}, {3, -1}, {5, 0}, {7, 0}}, "m" SUP "M7" PUS },
695 { {{1, 0}, {3, -1}, {5, 0}, {7, -1}}, "m7" },
696 { {{1, 0}, {3, -1}, {5, -1}, {7, 0}}, "°" SUP "M7" PUS },
697 { {{1, 0}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "7" PUS },
698 { {{1, 0}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "7" PUS },
699 { {{1, 0}, {2, 0}, {5, 0}, {7, 0}}, "m" SUP "M7" PUS "sus2" },
700 { {{1, 0}, {2, 0}, {5, 0}, {7, -1}}, "m7sus2" },
701 { {{1, 0}, {2, 0}, {5, -1}, {7, 0}}, "°" SUP "M7" PUS "sus2" },
702 { {{1, 0}, {2, 0}, {5, -1}, {7, -1}}, "𝆩" SUP "7" PUS "sus2" },
703 { {{1, 0}, {2, 0}, {5, -1}, {7, -2}}, "°" SUP "7" PUS "sus2" }, /* Ambiguous with 7 */
704 /* Ninth chords voiced with no 5th */
705 { {{1, 0}, {9, 0}, {4, 0}, {7, 0}}, "M9sus" },
706 { {{1, 0}, {9, 0}, {4, 0}, {7, -1}}, "9sus" },
707 { {{1, 0}, {9, 0}, {3, 0}, {7, 0}}, "M9" },
708 { {{1, 0}, {9, 0}, {3, 0}, {7, -1}}, "9" },
709 { {{1, 0}, {9, 0}, {3, -1}, {7, 0}}, "m" SUP "M9" PUS },
710 { {{1, 0}, {9, 0}, {3, -1}, {7, -1}}, "m9" },
711 { {{1, 0}, {9, -1}, {3, -1}, {7, -1}}, "m♭9" },
714 /* The sorting here is funny to keep the array in degree order
715 * after reducing each degree to an actual scale degree, (9 -> 2,
716 * 11 -> 4, 13 -> 6) */
717 struct { modified_degree_t degrees[5]; const char *name; } pentachords[] = {
719 { {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {6, 0}}, "6/9" },
720 { {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {6, 0}}, "m6/9" },
721 /* Seventh plus altered 9 */
722 { {{1, 0}, {9, +1}, {3, 0}, {5, 0}, {7, -1}}, "7♯9" },
723 { {{1, 0}, {9, -1}, {3, 0}, {5, 0}, {7, -1}}, "7♭9" },
724 { {{1, 0}, {9, +1}, {3, 0}, {5, -1}, {7, -1}}, "7♭5♯9" },
725 { {{1, 0}, {9, -1}, {3, 0}, {5, -1}, {7, -1}}, "7♭5♭9" },
727 { {{1, 0}, {9, 0}, {4, 0}, {5, +1}, {7, 0}}, SUP "+M9" PUS "sus" },
728 { {{1, 0}, {9, 0}, {4, 0}, {5, +1}, {7, -1}}, SUP "+9" PUS "sus" },
729 { {{1, 0}, {9, 0}, {4, 0}, {5, 0}, {7, 0}}, "M9sus" },
730 { {{1, 0}, {9, 0}, {4, 0}, {5, 0}, {7, -1}}, "9sus" },
732 { {{1, 0}, {9, 0}, {3, 0}, {5, +1}, {7, 0}}, SUP "+M9" PUS },
733 { {{1, 0}, {9, 0}, {3, 0}, {5, +1}, {7, -1}}, SUP "+9" PUS },
735 { {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {7, 0}}, "M9" },
736 { {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {7, -1}}, "9" },
737 { {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {7, 0}}, "m" SUP "M9" PUS },
738 { {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {7, -1}}, "m9" },
739 { {{1, 0}, {9, -1}, {3, -1}, {5, 0}, {7, -1}}, "m♭9" },
740 { {{1, 0}, {9, 0}, {3, 0}, {5, -1}, {7, 0}}, "M9" SUP "♭5" PUS },
741 { {{1, 0}, {9, 0}, {3, 0}, {5, -1}, {7, -1}}, "9" SUP "♭5" PUS },
742 { {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, 0}}, "m" SUP "M9♭5" PUS },
743 { {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "9" PUS },
745 /* FIXME: I don't have names for these last three after
746 * dropping the 5th from the voicing. That suggests to me that
747 * I'm missing names for these with a perfect 5th in the list
749 { {{1, 0}, {9, -1}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "♭9" PUS },
750 { {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "9" PUS },
751 { {{1, 0}, {9, -1}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "♭9" PUS },
754 struct { modified_degree_t degrees[6]; const char *name; } hexachords[] = {
755 { {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, +1}, {7, 0}}, SUP "+M11" PUS },
756 { {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, +1}, {7, -1}}, SUP "+11" PUS },
757 { {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, 0}, {7, 0}}, "M11" },
758 { {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, 0}, {7, -1}}, "11" },
759 { {{1, 0}, {9, 0}, {3, -1}, {11, 0}, {5, 0}, {7, 0}}, "m" SUP "M11" PUS },
760 { {{1, 0}, {9, 0}, {3, -1}, {11, 0}, {5, 0}, {7, -1}}, "m11" },
761 { {{1, 0}, {9, -1}, {3, -1}, {11, 0}, {5, -1}, {7, -1}}, "𝆩" SUP "11" PUS },
762 { {{1, 0}, {9, -1}, {3, -1}, {11, -1}, {5, -1}, {7, -2}}, "°" SUP "11" PUS }
765 if (scherzo->chord) {
766 score_remove_chord (scherzo->chord);
767 scherzo->chord = NULL;
773 notes = talloc_array (local, analyzed_note_t, num_notes);
777 for (i = 0; i < num_notes; i++) {
778 score_note_t *note = note_group->notes[i];
779 notes[i].note = note;
780 notes[i].midi_pitch = _score_pitch_and_octave_to_midi (note->pitch,
782 /* Relative pitch will be filled in after sorting. */
783 notes[i].relative_pitch = 0;
786 /* First, sort by midi pitch to find the bass note. */
787 qsort (notes, num_notes, sizeof (analyzed_note_t),
788 _compare_analyzed_note_by_midi_pitch);
790 bass_pitch = notes[0].midi_pitch;
792 /* With the bass note identified, we can find all relative pitches. */
793 for (i = 0; i < num_notes; i++) {
794 notes[i].relative_pitch = notes[i].midi_pitch - bass_pitch;
795 while (notes[i].relative_pitch >= 12)
796 notes[i].relative_pitch -= 12;
799 /* Now, sort again by relative pitch. */
800 qsort (notes, num_notes, sizeof (analyzed_note_t),
801 _compare_analyzed_note_by_relative_pitch);
803 /* Finally, eliminate all duplicate notes. */
804 for (i = 0; i < num_notes - 1; i++) {
805 if (notes[i+1].relative_pitch == notes[i].relative_pitch) {
807 while (j < num_notes &&
808 notes[j].relative_pitch == notes[i].relative_pitch)
812 /* The loop incremented j one past the last
813 * duplicate. Decrement so that it points to the
814 * last duplicate (and is guaranteed to not exceed
815 * the array bounds).*/
818 if (j < num_notes - 1) {
819 memmove (¬es[i+1], ¬es[j+1],
820 (num_notes - j) * sizeof (analyzed_note_t));
823 num_notes -= (j - i);
827 for (inversion = 0; inversion < 4; inversion++) {
830 for (i = 0; i < ARRAY_SIZE (octaves); i++) {
831 if (_chord_signature_matches (notes, num_notes,
832 octaves[i].degrees, 1,
835 chord_name = octaves[i].name;
836 goto CHORD_NAME_KNOWN;
841 for (i = 0; i < ARRAY_SIZE (intervals); i++) {
842 if (_chord_signature_matches (notes, num_notes,
843 intervals[i].degrees, 2,
846 chord_name = intervals[i].name;
847 goto CHORD_NAME_KNOWN;
852 for (i = 0; i < ARRAY_SIZE (triads); i++) {
853 if (_chord_signature_matches (notes, num_notes,
854 triads[i].degrees, 3,
857 chord_name = triads[i].name;
858 goto CHORD_NAME_KNOWN;
863 for (i = 0; i < ARRAY_SIZE (tetrachords); i++) {
864 if (_chord_signature_matches (notes, num_notes,
865 tetrachords[i].degrees, 4,
868 chord_name = tetrachords[i].name;
869 goto CHORD_NAME_KNOWN;
874 for (i = 0; i < ARRAY_SIZE (pentachords); i++) {
875 if (_chord_signature_matches (notes, num_notes,
876 pentachords[i].degrees, 5,
879 chord_name = pentachords[i].name;
880 goto CHORD_NAME_KNOWN;
885 for (i = 0; i < ARRAY_SIZE (hexachords); i++) {
886 if (_chord_signature_matches (notes, num_notes,
887 hexachords[i].degrees, 6,
890 chord_name = hexachords[i].name;
891 goto CHORD_NAME_KNOWN;
901 const char *inversion_str;
904 inversion_str = "1st inversion";
907 inversion_str = "2nd inversion";
910 inversion_str = "3rd inversion";
913 fprintf (stderr, "Internal error: Unexpected inversion: %d\n",
917 chord_name = talloc_asprintf (local, "%s%s %s",
919 chord_name, inversion_str);
921 chord_name = talloc_asprintf (local, "%s%s",
926 chord_name = talloc_strdup (local, "Unknown chord");
929 scherzo->chord = score_add_chord (scherzo->treble, chord_name);
936 note_group_init (void *ctx, note_group_t *group)
941 group->num_notes = 0;
945 note_group_add_note (note_group_t *group, score_note_t *note)
949 /* Do nothing if note is already in group. */
950 for (i = 0; i < group->num_notes; i++) {
951 if (group->notes[i]->pitch == note->pitch &&
952 group->notes[i]->octave == note->octave)
960 if (group->num_notes > group->size) {
962 group->notes = talloc_realloc (group->ctx, group->notes,
963 score_note_t*, group->size);
965 if (group->notes == NULL) {
966 fprintf (stderr, "Out of memory.\n");
971 group->notes[group->num_notes - 1] = note;
975 note_group_remove_note_at (note_group_t *group, int i)
977 if (i >= group->num_notes) {
978 fprintf (stderr, "Internal error: No note to remove at index %d\n", i);
982 if (i < group->num_notes - 1) {
983 memmove (group->notes + i, group->notes + i + 1,
984 (group->num_notes - 1 - i) * sizeof (score_note_t*));
989 static score_note_t *
990 scherzo_press_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
992 score_staff_t *staff;
996 if (scherzo->challenge.note) {
997 staff = scherzo->challenge.staff;
998 } else if (octave >= 4) {
999 staff = scherzo->treble;
1001 staff = scherzo->bass;
1004 /* Do nothing if this note is already pressed. */
1005 for (i = 0; i < scherzo->notes_pressed.num_notes; i++) {
1006 if (scherzo->notes_pressed.notes[i]->pitch == pitch &&
1007 scherzo->notes_pressed.notes[i]->octave == octave)
1009 return scherzo->notes_pressed.notes[i];
1013 note = score_add_note (staff, pitch, octave, SCORE_DURATION_WHOLE);
1015 note_group_add_note (&scherzo->notes_pressed, note);
1017 if (scherzo->pedal_pressed)
1018 note_group_add_note (&scherzo->notes_pedaled, note);
1020 scherzo_analyze_chord (scherzo);
1026 scherzo_release_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
1032 for (i = scherzo->notes_pressed.num_notes - 1; i >=0; i--) {
1033 note = scherzo->notes_pressed.notes[i];
1034 if (note->pitch == pitch && note->octave == octave) {
1036 if (! scherzo->pedal_pressed)
1037 score_remove_note (note);
1038 note_group_remove_note_at (&scherzo->notes_pressed, i);
1043 fprintf (stderr, "Internal error: Failed to find note to release.\n");
1046 scherzo_analyze_chord (scherzo);
1049 static score_note_t *
1050 scherzo_press_note_midi (scherzo_t *scherzo, unsigned char midi_note)
1052 score_pitch_t pitch;
1055 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
1057 return scherzo_press_note (scherzo, pitch, octave);
1061 scherzo_release_note_midi (scherzo_t *scherzo, unsigned char midi_note)
1063 score_pitch_t pitch;
1066 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
1068 scherzo_release_note (scherzo, pitch, octave);
1072 scherzo_press_pedal (scherzo_t *scherzo)
1076 scherzo->pedal_pressed = 1;
1078 /* Copy all pressed notes to pedaled notes */
1079 for (i = 0; i < scherzo->notes_pressed.num_notes; i++)
1080 note_group_add_note (&scherzo->notes_pedaled, scherzo->notes_pressed.notes[i]);
1084 scherzo_release_pedal (scherzo_t *scherzo)
1086 score_note_t *note, *new_note;
1089 /* Make new notes in score for all pressed notes. */
1090 for (i = 0; i < scherzo->notes_pressed.num_notes; i++) {
1091 note = scherzo->notes_pressed.notes[i];
1092 new_note = score_add_note (note->staff, note->pitch, note->octave, note->duration);
1093 scherzo->notes_pressed.notes[i] = new_note;
1096 /* Then remove all previously pedaled notes from the score. */
1097 for (i = scherzo->notes_pedaled.num_notes - 1; i >=0; i--) {
1098 note = scherzo->notes_pedaled.notes[i];
1099 score_remove_note (note);
1100 note_group_remove_note_at (&scherzo->notes_pedaled, i);
1103 scherzo->pedal_pressed = 0;
1105 scherzo_analyze_chord (scherzo);
1107 gtk_widget_queue_draw (scherzo->window);
1111 _select_challenge (scherzo_t *scherzo)
1113 category_t *category_unused;
1114 bool_t introduced_unused;
1116 challenge_t *challenge = &scherzo->challenge;
1117 score_pitch_t pitch;
1121 if (challenge->note) {
1122 score_remove_note (challenge->note);
1123 challenge->note = NULL;
1126 mnemon_select_item (&scherzo->mnemon,
1128 &challenge->item_index,
1130 &introduced_unused);
1132 item = challenge->bin->items[challenge->item_index];
1134 s = item->challenge;
1135 if (strncmp (s, "treble:", 7) == 0) {
1137 challenge->staff = scherzo->treble;
1138 } else if (strncmp (s, "bass:", 5) == 0) {
1140 challenge->staff = scherzo->bass;
1143 "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
1150 pitch = SCORE_PITCH_C;
1153 pitch = SCORE_PITCH_D;
1156 pitch = SCORE_PITCH_E;
1159 pitch = SCORE_PITCH_F;
1162 pitch = SCORE_PITCH_G;
1165 pitch = SCORE_PITCH_A;
1168 pitch = SCORE_PITCH_B;
1171 fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
1176 if (*s < '0' || *s > '9') {
1177 fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
1183 challenge->note = score_add_note (challenge->staff, pitch, octave,
1184 SCORE_DURATION_WHOLE);
1185 challenge->satisfied = 0;
1186 challenge->mistaken = 0;
1189 /* Determine whether the user hit the correct note. */
1191 _judge_note (scherzo_t *scherzo, score_note_t *note)
1193 challenge_t *challenge = &scherzo->challenge;
1195 if (! scherzo->challenge.note) {
1196 score_set_note_color_rgb (note, 0.0, 0.0, 0.0); /* black */
1200 if (note->pitch == challenge->note->pitch &&
1201 note->octave == challenge->note->octave)
1203 challenge->satisfied = 1;
1204 score_set_note_color_rgb (note, 18/256., 130/256., 28/256.); /* green */
1208 challenge->mistaken = 1;
1209 score_set_note_color_rgb (note, 184/256., 4/256., 22/256.); /* red */
1213 /* If the user got the right note (eventually), then score it in
1214 * mnemon and show the next note. */
1216 _score_challenge (scherzo_t *scherzo)
1218 challenge_t *challenge = &scherzo->challenge;
1220 if (! challenge->note)
1223 if (! challenge->satisfied)
1226 mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
1227 ! challenge->mistaken);
1229 _select_challenge (scherzo);
1233 on_midi_input (unused (GIOChannel *channel),
1234 unused (GIOCondition condition),
1237 unsigned char buf[MIDI_BUF_SIZE], *next;
1238 scherzo_t *scherzo = user_data;
1240 snd_seq_event_t event;
1242 int need_redraw = FALSE;
1244 remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
1250 consumed = snd_midi_event_encode (scherzo->snd_midi_event,
1251 next, remaining, &event);
1253 remaining -= consumed;
1256 switch (event.type) {
1257 case SND_SEQ_EVENT_NONE:
1258 /* Incomplete event. Nothing to do. */
1260 case SND_SEQ_EVENT_NOTEON:
1261 note = scherzo_press_note_midi (scherzo, event.data.note.note);
1262 _judge_note (scherzo, note);
1265 case SND_SEQ_EVENT_NOTEOFF:
1266 scherzo_release_note_midi (scherzo, event.data.note.note);
1267 _score_challenge (scherzo);
1270 case SND_SEQ_EVENT_CLOCK:
1271 /* Ignore for now as my piano sends a constant stream of these. */
1273 case SND_SEQ_EVENT_SENSING:
1274 /* Ignore for now as my piano sends a constant stream of these. */
1276 case SND_SEQ_EVENT_CONTROLLER:
1277 /* XXX: My piano gives me 64 for the sustain pedal, is
1278 * that universal? */
1279 if (event.data.control.param == 64) {
1280 if (event.data.control.value == 0)
1281 scherzo_release_pedal (scherzo);
1283 scherzo_press_pedal (scherzo);
1285 fprintf (stderr, "Fixme: Unhandled MIDI Control event (param=%d, value=%d)\n",
1286 event.data.control.param, event.data.control.value);
1290 fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
1297 gtk_widget_queue_draw (scherzo->window);
1299 /* Return TRUE to continue to get called in the future. */
1304 main (int argc, char *argv[])
1306 GtkWidget *drawing_area;
1310 srand (time (NULL));
1312 gtk_init (&argc, &argv);
1314 scherzo.ctx = talloc_new (NULL);
1316 scherzo.score = score_create (scherzo.ctx);
1317 scherzo.staff_height = 100;
1318 score_set_staff_height (scherzo.score, scherzo.staff_height);
1320 score_add_brace (scherzo.score, 2);
1321 scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
1322 scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
1324 scherzo.chord = NULL;
1326 /* Default to octave 4 and natural for computer keyboard keypresses. */
1327 scherzo.keyboard_octave = 4;
1328 scherzo.keyboard_accidental = SCORE_PITCH_ACCIDENTAL_NATURAL;
1330 note_group_init (scherzo.ctx, &scherzo.notes_pressed);
1331 note_group_init (scherzo.ctx, &scherzo.notes_pedaled);
1333 scherzo.pedal_pressed = 0;
1335 mnemon_init (&scherzo.mnemon);
1336 /* XXX: Should create a default file if one cannot be loaded. */
1337 mnemon_load_category (&scherzo.mnemon, "scherzo-notes");
1339 scherzo.challenge.note = NULL;
1341 _select_challenge (&scherzo);
1344 err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
1346 fprintf (stderr, "Out of memory.\n");
1350 #define MIDI_DEVICE "/dev/midi1"
1351 scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
1352 if (scherzo.midi_fd < 0) {
1353 printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
1355 GIOChannel *channel;
1357 channel = g_io_channel_unix_new (scherzo.midi_fd);
1358 g_io_channel_set_encoding (channel, NULL, NULL);
1359 g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
1362 scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1364 gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 1000, 600);
1366 g_signal_connect (scherzo.window, "delete-event",
1367 G_CALLBACK (on_delete_event_quit), NULL);
1369 drawing_area = gtk_drawing_area_new ();
1371 gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
1373 g_signal_connect (drawing_area, "expose-event",
1374 G_CALLBACK (on_expose_event_draw),
1377 g_signal_connect (scherzo.window, "key-press-event",
1378 G_CALLBACK (on_key_press_event),
1381 g_signal_connect (scherzo.window, "key-release-event",
1382 G_CALLBACK (on_key_release_event),
1385 gtk_widget_show_all (scherzo.window);
1389 mnemon_save (&scherzo.mnemon);
1391 snd_midi_event_free (scherzo.snd_midi_event);
1393 talloc_free (scherzo.ctx);