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)
454 /* Number of half steps from root to specified degree within a
455 * diatonic scaled. */
456 switch (degree->degree) {
479 fprintf (stderr, "Internal: Invalid degree %d\n", degree->degree);
484 return half_steps + degree->modification;
488 _chord_signature_matches (analyzed_note_t *notes,
490 modified_degree_t *degrees,
495 #define MAX_DEGREES 4
496 int relative_pitches[MAX_DEGREES];
499 assert (num_degrees <= MAX_DEGREES);
501 if (num_notes != num_degrees)
504 if (inversion >= num_degrees)
507 /* We never spell simple intervals as inversions. */
508 if (num_degrees == 2 && inversion > 0)
511 for (i = 0; i < num_degrees; i++) {
512 /* The num_degrees is in the addition just to ensure all
513 * inputs to the modulus operator remain positive. */
514 int index = (i + num_degrees - inversion) % num_degrees;
516 /* Again, adding a 12 to keep things positive. */
517 relative_pitches[index] =
519 _modified_degree_to_half_steps (°rees[i]) -
520 _modified_degree_to_half_steps (°rees[inversion])) % 12;
524 for (i = 0; i < num_notes; i++)
525 if (notes[i].relative_pitch != relative_pitches[i])
528 root_index = (num_notes - inversion) % num_notes;
529 *root = notes[root_index].note->pitch;
535 _pitch_str (score_pitch_t pitch)
538 case SCORE_PITCH_Cff:
546 case SCORE_PITCH_Css:
548 case SCORE_PITCH_Dff:
556 case SCORE_PITCH_Dss:
558 case SCORE_PITCH_Eff:
566 case SCORE_PITCH_Ess:
568 case SCORE_PITCH_Fff:
576 case SCORE_PITCH_Fss:
578 case SCORE_PITCH_Gff:
586 case SCORE_PITCH_Gss:
588 case SCORE_PITCH_Aff:
596 case SCORE_PITCH_Ass:
598 case SCORE_PITCH_Bff:
606 case SCORE_PITCH_Bss:
610 fprintf (stderr, "Internal error: Unknown pitch %d\n", pitch);
615 scherzo_analyze_chord (scherzo_t *scherzo)
617 void *local = talloc_new (NULL);
618 analyzed_note_t *notes;
619 note_group_t *note_group;
620 unsigned i, j, num_notes;
621 int bass_pitch, inversion;
623 const char *chord_name = NULL;
625 if (scherzo->pedal_pressed)
626 note_group = &scherzo->notes_pedaled;
628 note_group = &scherzo->notes_pressed;
630 num_notes = note_group->num_notes;
632 struct { modified_degree_t degrees[1]; const char *name; } octaves[] = {
633 { {{1, 0}}, " Octave"}
636 struct { modified_degree_t degrees[2]; const char *name; } intervals[] = {
637 { {{1, 0}, {2, -1}}, " Minor 2nd"},
638 { {{1, 0}, {2, 0}}, " Major 2nd"},
639 { {{1, 0}, {3, -1}}, " Minor 3rd"},
640 { {{1, 0}, {3, 0}}, " Major 3rd"},
641 { {{1, 0}, {4, 0}}, " Perfect 4th"},
642 { {{1, 0}, {5, -1}}, " Diminished 5th"},
643 { {{1, 0}, {5, 0}}, " Perfect 5th"},
644 { {{1, 0}, {6, -1}}, " Minor 6th"},
645 { {{1, 0}, {6, 0}}, " Major 6th"},
646 { {{1, 0}, {7, -1}}, " Minor 7th"},
647 { {{1, 0}, {7, 0}}, " Major 7th"}
650 /* XXX: The superscript rise value should be relative to the current
651 * font size. Best would be for pango to support this. See:
653 * https://bugzilla.gnome.org/show_bug.cgi?id=708780
655 * For now, these are emipirically-derived values that look reasonable
656 * at the default size that score.c is using to draw the staff. If the
657 * user scales the staff up or down then these will look wrong.
660 #define SUP "<span font='33' rise='30000'>"
661 #define PUS "</span>"
663 struct { modified_degree_t degrees[3]; const char *name; } triads[] = {
664 { {{1, 0}, {3, 0}, {5, +1}}, SUP "+" PUS },
665 { {{1, 0}, {3, 0}, {5, 0}}, "" },
666 { {{1, 0}, {3, -1}, {5, 0}}, "m" },
667 { {{1, 0}, {3, -1}, {5, -1}}, "°" }
670 struct { modified_degree_t degrees[4]; const char *name; } tetrachords[] = {
672 { {{1, 0}, {3, 0}, {5, 0}, {6, 0}}, "6" },
673 { {{1, 0}, {3, -1}, {5, 0}, {6, 0}}, "m6" },
675 { {{1, 0}, {3, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS },
676 { {{1, 0}, {3, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS },
677 { {{1, 0}, {3, 0}, {5, 0}, {7, 0}}, "M7" },
678 { {{1, 0}, {3, 0}, {5, 0}, {7, -1}}, "7" },
679 { {{1, 0}, {3, 0}, {5, -1}, {7, -1}}, SUP "7♭5" PUS },
680 { {{1, 0}, {3, -1}, {5, 0}, {7, 0}}, "m" SUP "M7" PUS },
681 { {{1, 0}, {3, -1}, {5, 0}, {7, -1}}, "m7" },
682 { {{1, 0}, {3, -1}, {5, -1}, {7, 0}}, "°" SUP "M7" PUS },
683 { {{1, 0}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "7" PUS },
684 { {{1, 0}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "7" PUS }
687 if (scherzo->chord) {
688 score_remove_chord (scherzo->chord);
689 scherzo->chord = NULL;
695 notes = talloc_array (local, analyzed_note_t, num_notes);
699 for (i = 0; i < num_notes; i++) {
700 score_note_t *note = note_group->notes[i];
701 notes[i].note = note;
702 notes[i].midi_pitch = _score_pitch_and_octave_to_midi (note->pitch,
704 /* Relative pitch will be filled in after sorting. */
705 notes[i].relative_pitch = 0;
708 /* First, sort by midi pitch to find the bass note. */
709 qsort (notes, num_notes, sizeof (analyzed_note_t),
710 _compare_analyzed_note_by_midi_pitch);
712 bass_pitch = notes[0].midi_pitch;
714 /* With the bass note identified, we can find all relative pitches. */
715 for (i = 0; i < num_notes; i++) {
716 notes[i].relative_pitch = notes[i].midi_pitch - bass_pitch;
717 while (notes[i].relative_pitch >= 12)
718 notes[i].relative_pitch -= 12;
721 /* Now, sort again by relative pitch. */
722 qsort (notes, num_notes, sizeof (analyzed_note_t),
723 _compare_analyzed_note_by_relative_pitch);
725 /* Finally, eliminate all duplicate notes. */
726 for (i = 0; i < num_notes - 1; i++) {
727 if (notes[i+1].relative_pitch == notes[i].relative_pitch) {
729 while (j < num_notes &&
730 notes[j].relative_pitch == notes[i].relative_pitch)
734 /* The loop incremented j one past the last
735 * duplicate. Decrement so that it points to the
736 * last duplicate (and is guaranteed to not exceed
737 * the array bounds).*/
740 if (j < num_notes - 1) {
741 memmove (¬es[i+1], ¬es[j+1],
742 (num_notes - j) * sizeof (analyzed_note_t));
745 num_notes -= (j - i);
749 for (inversion = 0; inversion < 4; inversion++) {
752 for (i = 0; i < ARRAY_SIZE (octaves); i++) {
753 if (_chord_signature_matches (notes, num_notes,
754 octaves[i].degrees, 1,
757 chord_name = octaves[i].name;
758 goto CHORD_NAME_KNOWN;
763 for (i = 0; i < ARRAY_SIZE (intervals); i++) {
764 if (_chord_signature_matches (notes, num_notes,
765 intervals[i].degrees, 2,
768 chord_name = intervals[i].name;
769 goto CHORD_NAME_KNOWN;
774 for (i = 0; i < ARRAY_SIZE (triads); i++) {
775 if (_chord_signature_matches (notes, num_notes,
776 triads[i].degrees, 3,
779 chord_name = triads[i].name;
780 goto CHORD_NAME_KNOWN;
785 for (i = 0; i < ARRAY_SIZE(tetrachords); i++) {
786 if (_chord_signature_matches (notes, num_notes,
787 tetrachords[i].degrees, 4,
790 chord_name = tetrachords[i].name;
791 goto CHORD_NAME_KNOWN;
801 const char *inversion_str;
804 inversion_str = "1st inversion";
807 inversion_str = "2nd inversion";
810 inversion_str = "3rd inversion";
813 fprintf (stderr, "Internal error: Unexpected inversion: %d\n",
817 chord_name = talloc_asprintf (local, "%s%s %s",
819 chord_name, inversion_str);
821 chord_name = talloc_asprintf (local, "%s%s",
826 chord_name = talloc_strdup (local, "Unknown chord");
829 scherzo->chord = score_add_chord (scherzo->treble, chord_name);
836 note_group_init (void *ctx, note_group_t *group)
841 group->num_notes = 0;
845 note_group_add_note (note_group_t *group, score_note_t *note)
849 /* Do nothing if note is already in group. */
850 for (i = 0; i < group->num_notes; i++) {
851 if (group->notes[i]->pitch == note->pitch &&
852 group->notes[i]->octave == note->octave)
860 if (group->num_notes > group->size) {
862 group->notes = talloc_realloc (group->ctx, group->notes,
863 score_note_t*, group->size);
865 if (group->notes == NULL) {
866 fprintf (stderr, "Out of memory.\n");
871 group->notes[group->num_notes - 1] = note;
875 note_group_remove_note_at (note_group_t *group, int i)
877 if (i >= group->num_notes) {
878 fprintf (stderr, "Internal error: No note to remove at index %d\n", i);
882 if (i < group->num_notes - 1) {
883 memmove (group->notes + i, group->notes + i + 1,
884 (group->num_notes - 1 - i) * sizeof (score_note_t*));
889 static score_note_t *
890 scherzo_press_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
892 score_staff_t *staff;
896 if (scherzo->challenge.note) {
897 staff = scherzo->challenge.staff;
898 } else if (octave >= 4) {
899 staff = scherzo->treble;
901 staff = scherzo->bass;
904 /* Do nothing if this note is already pressed. */
905 for (i = 0; i < scherzo->notes_pressed.num_notes; i++) {
906 if (scherzo->notes_pressed.notes[i]->pitch == pitch &&
907 scherzo->notes_pressed.notes[i]->octave == octave)
909 return scherzo->notes_pressed.notes[i];
913 note = score_add_note (staff, pitch, octave, SCORE_DURATION_WHOLE);
915 note_group_add_note (&scherzo->notes_pressed, note);
917 if (scherzo->pedal_pressed)
918 note_group_add_note (&scherzo->notes_pedaled, note);
920 scherzo_analyze_chord (scherzo);
926 scherzo_release_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
932 for (i = scherzo->notes_pressed.num_notes - 1; i >=0; i--) {
933 note = scherzo->notes_pressed.notes[i];
934 if (note->pitch == pitch && note->octave == octave) {
936 if (! scherzo->pedal_pressed)
937 score_remove_note (note);
938 note_group_remove_note_at (&scherzo->notes_pressed, i);
943 fprintf (stderr, "Internal error: Failed to find note to release.\n");
946 scherzo_analyze_chord (scherzo);
949 static score_note_t *
950 scherzo_press_note_midi (scherzo_t *scherzo, unsigned char midi_note)
955 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
957 return scherzo_press_note (scherzo, pitch, octave);
961 scherzo_release_note_midi (scherzo_t *scherzo, unsigned char midi_note)
966 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
968 scherzo_release_note (scherzo, pitch, octave);
972 scherzo_press_pedal (scherzo_t *scherzo)
976 scherzo->pedal_pressed = 1;
978 /* Copy all pressed notes to pedaled notes */
979 for (i = 0; i < scherzo->notes_pressed.num_notes; i++)
980 note_group_add_note (&scherzo->notes_pedaled, scherzo->notes_pressed.notes[i]);
984 scherzo_release_pedal (scherzo_t *scherzo)
986 score_note_t *note, *new_note;
989 /* Make new notes in score for all pressed notes. */
990 for (i = 0; i < scherzo->notes_pressed.num_notes; i++) {
991 note = scherzo->notes_pressed.notes[i];
992 new_note = score_add_note (note->staff, note->pitch, note->octave, note->duration);
993 scherzo->notes_pressed.notes[i] = new_note;
996 /* Then remove all previously pedaled notes from the score. */
997 for (i = scherzo->notes_pedaled.num_notes - 1; i >=0; i--) {
998 note = scherzo->notes_pedaled.notes[i];
999 score_remove_note (note);
1000 note_group_remove_note_at (&scherzo->notes_pedaled, i);
1003 scherzo->pedal_pressed = 0;
1005 scherzo_analyze_chord (scherzo);
1007 gtk_widget_queue_draw (scherzo->window);
1011 _select_challenge (scherzo_t *scherzo)
1013 category_t *category_unused;
1014 bool_t introduced_unused;
1016 challenge_t *challenge = &scherzo->challenge;
1017 score_pitch_t pitch;
1021 if (challenge->note) {
1022 score_remove_note (challenge->note);
1023 challenge->note = NULL;
1026 mnemon_select_item (&scherzo->mnemon,
1028 &challenge->item_index,
1030 &introduced_unused);
1032 item = challenge->bin->items[challenge->item_index];
1034 s = item->challenge;
1035 if (strncmp (s, "treble:", 7) == 0) {
1037 challenge->staff = scherzo->treble;
1038 } else if (strncmp (s, "bass:", 5) == 0) {
1040 challenge->staff = scherzo->bass;
1043 "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
1050 pitch = SCORE_PITCH_C;
1053 pitch = SCORE_PITCH_D;
1056 pitch = SCORE_PITCH_E;
1059 pitch = SCORE_PITCH_F;
1062 pitch = SCORE_PITCH_G;
1065 pitch = SCORE_PITCH_A;
1068 pitch = SCORE_PITCH_B;
1071 fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
1076 if (*s < '0' || *s > '9') {
1077 fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
1083 challenge->note = score_add_note (challenge->staff, pitch, octave,
1084 SCORE_DURATION_WHOLE);
1085 challenge->satisfied = 0;
1086 challenge->mistaken = 0;
1089 /* Determine whether the user hit the correct note. */
1091 _judge_note (scherzo_t *scherzo, score_note_t *note)
1093 challenge_t *challenge = &scherzo->challenge;
1095 if (! scherzo->challenge.note) {
1096 score_set_note_color_rgb (note, 0.0, 0.0, 0.0); /* black */
1100 if (note->pitch == challenge->note->pitch &&
1101 note->octave == challenge->note->octave)
1103 challenge->satisfied = 1;
1104 score_set_note_color_rgb (note, 18/256., 130/256., 28/256.); /* green */
1108 challenge->mistaken = 1;
1109 score_set_note_color_rgb (note, 184/256., 4/256., 22/256.); /* red */
1113 /* If the user got the right note (eventually), then score it in
1114 * mnemon and show the next note. */
1116 _score_challenge (scherzo_t *scherzo)
1118 challenge_t *challenge = &scherzo->challenge;
1120 if (! challenge->note)
1123 if (! challenge->satisfied)
1126 mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
1127 ! challenge->mistaken);
1129 _select_challenge (scherzo);
1133 on_midi_input (unused (GIOChannel *channel),
1134 unused (GIOCondition condition),
1137 unsigned char buf[MIDI_BUF_SIZE], *next;
1138 scherzo_t *scherzo = user_data;
1140 snd_seq_event_t event;
1142 int need_redraw = FALSE;
1144 remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
1150 consumed = snd_midi_event_encode (scherzo->snd_midi_event,
1151 next, remaining, &event);
1153 remaining -= consumed;
1156 switch (event.type) {
1157 case SND_SEQ_EVENT_NONE:
1158 /* Incomplete event. Nothing to do. */
1160 case SND_SEQ_EVENT_NOTEON:
1161 note = scherzo_press_note_midi (scherzo, event.data.note.note);
1162 _judge_note (scherzo, note);
1165 case SND_SEQ_EVENT_NOTEOFF:
1166 scherzo_release_note_midi (scherzo, event.data.note.note);
1167 _score_challenge (scherzo);
1170 case SND_SEQ_EVENT_CLOCK:
1171 /* Ignore for now as my piano sends a constant stream of these. */
1173 case SND_SEQ_EVENT_SENSING:
1174 /* Ignore for now as my piano sends a constant stream of these. */
1176 case SND_SEQ_EVENT_CONTROLLER:
1177 /* XXX: My piano gives me 64 for the sustain pedal, is
1178 * that universal? */
1179 if (event.data.control.param == 64) {
1180 if (event.data.control.value == 0)
1181 scherzo_release_pedal (scherzo);
1183 scherzo_press_pedal (scherzo);
1185 fprintf (stderr, "Fixme: Unhandled MIDI Control event (param=%d, value=%d)\n",
1186 event.data.control.param, event.data.control.value);
1190 fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
1197 gtk_widget_queue_draw (scherzo->window);
1199 /* Return TRUE to continue to get called in the future. */
1204 main (int argc, char *argv[])
1206 GtkWidget *drawing_area;
1210 srand (time (NULL));
1212 gtk_init (&argc, &argv);
1214 scherzo.ctx = talloc_new (NULL);
1216 scherzo.score = score_create (scherzo.ctx);
1217 scherzo.staff_height = 100;
1218 score_set_staff_height (scherzo.score, scherzo.staff_height);
1220 score_add_brace (scherzo.score, 2);
1221 scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
1222 scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
1224 scherzo.chord = NULL;
1226 /* Default to octave 4 and natural for computer keyboard keypresses. */
1227 scherzo.keyboard_octave = 4;
1228 scherzo.keyboard_accidental = SCORE_PITCH_ACCIDENTAL_NATURAL;
1230 note_group_init (scherzo.ctx, &scherzo.notes_pressed);
1231 note_group_init (scherzo.ctx, &scherzo.notes_pedaled);
1233 scherzo.pedal_pressed = 0;
1235 mnemon_init (&scherzo.mnemon);
1236 /* XXX: Should create a default file if one cannot be loaded. */
1237 mnemon_load_category (&scherzo.mnemon, "scherzo-notes");
1239 scherzo.challenge.note = NULL;
1241 _select_challenge (&scherzo);
1244 err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
1246 fprintf (stderr, "Out of memory.\n");
1250 #define MIDI_DEVICE "/dev/midi1"
1251 scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
1252 if (scherzo.midi_fd < 0) {
1253 printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
1255 GIOChannel *channel;
1257 channel = g_io_channel_unix_new (scherzo.midi_fd);
1258 g_io_channel_set_encoding (channel, NULL, NULL);
1259 g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
1262 scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1264 gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 1000, 600);
1266 g_signal_connect (scherzo.window, "delete-event",
1267 G_CALLBACK (on_delete_event_quit), NULL);
1269 drawing_area = gtk_drawing_area_new ();
1271 gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
1273 g_signal_connect (drawing_area, "expose-event",
1274 G_CALLBACK (on_expose_event_draw),
1277 g_signal_connect (scherzo.window, "key-press-event",
1278 G_CALLBACK (on_key_press_event),
1281 g_signal_connect (scherzo.window, "key-release-event",
1282 G_CALLBACK (on_key_release_event),
1285 gtk_widget_show_all (scherzo.window);
1289 mnemon_save (&scherzo.mnemon);
1291 snd_midi_event_free (scherzo.snd_midi_event);
1293 talloc_free (scherzo.ctx);