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];
497 int inversion, max_inversions;
500 assert (num_degrees <= MAX_DEGREES);
502 if (num_notes != num_degrees)
505 max_inversions = num_degrees;
507 /* We never spell simple intervals as inversions. */
508 if (num_degrees == 2)
511 for (inversion = 0; inversion < max_inversions; inversion++) {
512 for (i = 0; i < num_degrees; i++) {
513 /* The num_degrees is in the addition just to ensure all
514 * inputs to the modulus operator remain positive. */
515 int index = (i + num_degrees - inversion) % num_degrees;
517 /* Again, adding a 12 to keep things positive. */
518 relative_pitches[index] =
520 _modified_degree_to_half_steps (°rees[i]) -
521 _modified_degree_to_half_steps (°rees[inversion])) % 12;
525 for (i = 0; i < num_notes; i++)
526 if (notes[i].relative_pitch != relative_pitches[i])
529 root_index = (num_notes - inversion) % num_notes;
530 *root = notes[root_index].note->pitch;
532 *inversion_ret = inversion;
544 _pitch_str (score_pitch_t pitch)
547 case SCORE_PITCH_Cff:
555 case SCORE_PITCH_Css:
557 case SCORE_PITCH_Dff:
565 case SCORE_PITCH_Dss:
567 case SCORE_PITCH_Eff:
575 case SCORE_PITCH_Ess:
577 case SCORE_PITCH_Fff:
585 case SCORE_PITCH_Fss:
587 case SCORE_PITCH_Gff:
595 case SCORE_PITCH_Gss:
597 case SCORE_PITCH_Aff:
605 case SCORE_PITCH_Ass:
607 case SCORE_PITCH_Bff:
615 case SCORE_PITCH_Bss:
619 fprintf (stderr, "Internal error: Unknown pitch %d\n", pitch);
624 scherzo_analyze_chord (scherzo_t *scherzo)
626 void *local = talloc_new (NULL);
627 analyzed_note_t *notes;
628 note_group_t *note_group;
629 unsigned i, j, num_notes;
630 int bass_pitch, inversion;
632 const char *chord_name = NULL;
634 if (scherzo->pedal_pressed)
635 note_group = &scherzo->notes_pedaled;
637 note_group = &scherzo->notes_pressed;
639 num_notes = note_group->num_notes;
641 struct { modified_degree_t degrees[1]; const char *name; } octaves[] = {
642 { {{1, 0}}, " Octave"}
645 struct { modified_degree_t degrees[2]; const char *name; } intervals[] = {
646 { {{1, 0}, {2, -1}}, " Minor 2nd"},
647 { {{1, 0}, {2, 0}}, " Major 2nd"},
648 { {{1, 0}, {3, -1}}, " Minor 3rd"},
649 { {{1, 0}, {3, 0}}, " Major 3rd"},
650 { {{1, 0}, {4, 0}}, " Perfect 4th"},
651 { {{1, 0}, {5, -1}}, " Diminished 5th"},
652 { {{1, 0}, {5, 0}}, " Perfect 5th"},
653 { {{1, 0}, {6, -1}}, " Minor 6th"},
654 { {{1, 0}, {6, 0}}, " Major 6th"},
655 { {{1, 0}, {7, -1}}, " Minor 7th"},
656 { {{1, 0}, {7, 0}}, " Major 7th"}
659 /* XXX: The superscript rise value should be relative to the current
660 * font size. Best would be for pango to support this. See:
662 * https://bugzilla.gnome.org/show_bug.cgi?id=708780
664 * For now, these are emipirically-derived values that look reasonable
665 * at the default size that score.c is using to draw the staff. If the
666 * user scales the staff up or down then these will look wrong.
669 #define SUP "<span font='33' rise='30000'>"
670 #define PUS "</span>"
672 struct { modified_degree_t degrees[3]; const char *name; } triads[] = {
673 { {{1, 0}, {3, 0}, {5, +1}}, SUP "+" PUS },
674 { {{1, 0}, {3, 0}, {5, 0}}, "M" },
675 { {{1, 0}, {3, -1}, {5, 0}}, "m" },
676 { {{1, 0}, {3, -1}, {5, -1}}, "°" }
679 struct { modified_degree_t degrees[4]; const char *name; } sevenths[] = {
680 { {{1, 0}, {3, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS },
681 { {{1, 0}, {3, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS },
682 { {{1, 0}, {3, 0}, {5, 0}, {7, 0}}, "M7" },
683 { {{1, 0}, {3, 0}, {5, 0}, {7, -1}}, "7" },
684 { {{1, 0}, {3, -1}, {5, 0}, {7, 0}}, "m" SUP "M7" PUS },
685 { {{1, 0}, {3, -1}, {5, 0}, {7, -1}}, "m7" },
686 { {{1, 0}, {3, -1}, {5, -1}, {7, 0}}, "°" SUP "M7" PUS },
687 { {{1, 0}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "7" PUS },
688 { {{1, 0}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "7" PUS }
691 if (scherzo->chord) {
692 score_remove_chord (scherzo->chord);
693 scherzo->chord = NULL;
699 notes = talloc_array (local, analyzed_note_t, num_notes);
703 for (i = 0; i < num_notes; i++) {
704 score_note_t *note = note_group->notes[i];
705 notes[i].note = note;
706 notes[i].midi_pitch = _score_pitch_and_octave_to_midi (note->pitch,
708 /* Relative pitch will be filled in after sorting. */
709 notes[i].relative_pitch = 0;
712 /* First, sort by midi pitch to find the bass note. */
713 qsort (notes, num_notes, sizeof (analyzed_note_t),
714 _compare_analyzed_note_by_midi_pitch);
716 bass_pitch = notes[0].midi_pitch;
718 /* With the bass note identified, we can find all relative pitches. */
719 for (i = 0; i < num_notes; i++) {
720 notes[i].relative_pitch = notes[i].midi_pitch - bass_pitch;
721 while (notes[i].relative_pitch >= 12)
722 notes[i].relative_pitch -= 12;
725 /* Now, sort again by relative pitch. */
726 qsort (notes, num_notes, sizeof (analyzed_note_t),
727 _compare_analyzed_note_by_relative_pitch);
729 /* Finally, eliminate all duplicate notes. */
730 for (i = 0; i < num_notes - 1; i++) {
731 if (notes[i+1].relative_pitch == notes[i].relative_pitch) {
733 while (j < num_notes &&
734 notes[j].relative_pitch == notes[i].relative_pitch)
738 /* The loop incremented j one past the last
739 * duplicate. Decrement so that it points to the
740 * last duplicate (and is guaranteed to not exceed
741 * the array bounds).*/
744 if (j < num_notes - 1) {
745 memmove (¬es[i+1], ¬es[j+1],
746 (num_notes - j) * sizeof (analyzed_note_t));
749 num_notes -= (j - i);
755 for (i = 0; i < ARRAY_SIZE (octaves); i++) {
756 if (_chord_signature_matches (notes, num_notes,
757 octaves[i].degrees, 1,
760 chord_name = octaves[i].name;
766 for (i = 0; i < ARRAY_SIZE (intervals); i++) {
767 if (_chord_signature_matches (notes, num_notes,
768 intervals[i].degrees, 2,
771 chord_name = intervals[i].name;
777 for (i = 0; i < ARRAY_SIZE (triads); i++) {
778 if (_chord_signature_matches (notes, num_notes,
779 triads[i].degrees, 3,
782 chord_name = triads[i].name;
788 for (i = 0; i < ARRAY_SIZE(sevenths); i++) {
789 if (_chord_signature_matches (notes, num_notes,
790 sevenths[i].degrees, 4,
793 chord_name = sevenths[i].name;
802 const char *inversion_str;
805 inversion_str = "1st inversion";
808 inversion_str = "2nd inversion";
811 inversion_str = "3rd inversion";
814 fprintf (stderr, "Internal error: Unexpected inversion: %d\n",
818 chord_name = talloc_asprintf (local, "%s%s %s",
820 chord_name, inversion_str);
822 chord_name = talloc_asprintf (local, "%s%s",
827 chord_name = talloc_strdup (local, "Unknown chord");
830 scherzo->chord = score_add_chord (scherzo->treble, chord_name);
837 note_group_init (void *ctx, note_group_t *group)
842 group->num_notes = 0;
846 note_group_add_note (note_group_t *group, score_note_t *note)
850 /* Do nothing if note is already in group. */
851 for (i = 0; i < group->num_notes; i++) {
852 if (group->notes[i]->pitch == note->pitch &&
853 group->notes[i]->octave == note->octave)
861 if (group->num_notes > group->size) {
863 group->notes = talloc_realloc (group->ctx, group->notes,
864 score_note_t*, group->size);
866 if (group->notes == NULL) {
867 fprintf (stderr, "Out of memory.\n");
872 group->notes[group->num_notes - 1] = note;
876 note_group_remove_note_at (note_group_t *group, int i)
878 if (i >= group->num_notes) {
879 fprintf (stderr, "Internal error: No note to remove at index %d\n", i);
883 if (i < group->num_notes - 1) {
884 memmove (group->notes + i, group->notes + i + 1,
885 (group->num_notes - 1 - i) * sizeof (score_note_t*));
890 static score_note_t *
891 scherzo_press_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
893 score_staff_t *staff;
897 if (scherzo->challenge.note) {
898 staff = scherzo->challenge.staff;
899 } else if (octave >= 4) {
900 staff = scherzo->treble;
902 staff = scherzo->bass;
905 /* Do nothing if this note is already pressed. */
906 for (i = 0; i < scherzo->notes_pressed.num_notes; i++) {
907 if (scherzo->notes_pressed.notes[i]->pitch == pitch &&
908 scherzo->notes_pressed.notes[i]->octave == octave)
910 return scherzo->notes_pressed.notes[i];
914 note = score_add_note (staff, pitch, octave, SCORE_DURATION_WHOLE);
916 note_group_add_note (&scherzo->notes_pressed, note);
918 if (scherzo->pedal_pressed)
919 note_group_add_note (&scherzo->notes_pedaled, note);
921 scherzo_analyze_chord (scherzo);
927 scherzo_release_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
933 for (i = scherzo->notes_pressed.num_notes - 1; i >=0; i--) {
934 note = scherzo->notes_pressed.notes[i];
935 if (note->pitch == pitch && note->octave == octave) {
937 if (! scherzo->pedal_pressed)
938 score_remove_note (note);
939 note_group_remove_note_at (&scherzo->notes_pressed, i);
944 fprintf (stderr, "Internal error: Failed to find note to release.\n");
947 scherzo_analyze_chord (scherzo);
950 static score_note_t *
951 scherzo_press_note_midi (scherzo_t *scherzo, unsigned char midi_note)
956 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
958 return scherzo_press_note (scherzo, pitch, octave);
962 scherzo_release_note_midi (scherzo_t *scherzo, unsigned char midi_note)
967 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
969 scherzo_release_note (scherzo, pitch, octave);
973 scherzo_press_pedal (scherzo_t *scherzo)
977 scherzo->pedal_pressed = 1;
979 /* Copy all pressed notes to pedaled notes */
980 for (i = 0; i < scherzo->notes_pressed.num_notes; i++)
981 note_group_add_note (&scherzo->notes_pedaled, scherzo->notes_pressed.notes[i]);
985 scherzo_release_pedal (scherzo_t *scherzo)
987 score_note_t *note, *new_note;
990 /* Make new notes in score for all pressed notes. */
991 for (i = 0; i < scherzo->notes_pressed.num_notes; i++) {
992 note = scherzo->notes_pressed.notes[i];
993 new_note = score_add_note (note->staff, note->pitch, note->octave, note->duration);
994 scherzo->notes_pressed.notes[i] = new_note;
997 /* Then remove all previously pedaled notes from the score. */
998 for (i = scherzo->notes_pedaled.num_notes - 1; i >=0; i--) {
999 note = scherzo->notes_pedaled.notes[i];
1000 score_remove_note (note);
1001 note_group_remove_note_at (&scherzo->notes_pedaled, i);
1004 scherzo->pedal_pressed = 0;
1006 scherzo_analyze_chord (scherzo);
1008 gtk_widget_queue_draw (scherzo->window);
1012 _select_challenge (scherzo_t *scherzo)
1014 category_t *category_unused;
1015 bool_t introduced_unused;
1017 challenge_t *challenge = &scherzo->challenge;
1018 score_pitch_t pitch;
1022 if (challenge->note) {
1023 score_remove_note (challenge->note);
1024 challenge->note = NULL;
1027 mnemon_select_item (&scherzo->mnemon,
1029 &challenge->item_index,
1031 &introduced_unused);
1033 item = challenge->bin->items[challenge->item_index];
1035 s = item->challenge;
1036 if (strncmp (s, "treble:", 7) == 0) {
1038 challenge->staff = scherzo->treble;
1039 } else if (strncmp (s, "bass:", 5) == 0) {
1041 challenge->staff = scherzo->bass;
1044 "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
1051 pitch = SCORE_PITCH_C;
1054 pitch = SCORE_PITCH_D;
1057 pitch = SCORE_PITCH_E;
1060 pitch = SCORE_PITCH_F;
1063 pitch = SCORE_PITCH_G;
1066 pitch = SCORE_PITCH_A;
1069 pitch = SCORE_PITCH_B;
1072 fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
1077 if (*s < '0' || *s > '9') {
1078 fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
1084 challenge->note = score_add_note (challenge->staff, pitch, octave,
1085 SCORE_DURATION_WHOLE);
1086 challenge->satisfied = 0;
1087 challenge->mistaken = 0;
1090 /* Determine whether the user hit the correct note. */
1092 _judge_note (scherzo_t *scherzo, score_note_t *note)
1094 challenge_t *challenge = &scherzo->challenge;
1096 if (! scherzo->challenge.note) {
1097 score_set_note_color_rgb (note, 0.0, 0.0, 0.0); /* black */
1101 if (note->pitch == challenge->note->pitch &&
1102 note->octave == challenge->note->octave)
1104 challenge->satisfied = 1;
1105 score_set_note_color_rgb (note, 18/256., 130/256., 28/256.); /* green */
1109 challenge->mistaken = 1;
1110 score_set_note_color_rgb (note, 184/256., 4/256., 22/256.); /* red */
1114 /* If the user got the right note (eventually), then score it in
1115 * mnemon and show the next note. */
1117 _score_challenge (scherzo_t *scherzo)
1119 challenge_t *challenge = &scherzo->challenge;
1121 if (! challenge->note)
1124 if (! challenge->satisfied)
1127 mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
1128 ! challenge->mistaken);
1130 _select_challenge (scherzo);
1134 on_midi_input (unused (GIOChannel *channel),
1135 unused (GIOCondition condition),
1138 unsigned char buf[MIDI_BUF_SIZE], *next;
1139 scherzo_t *scherzo = user_data;
1141 snd_seq_event_t event;
1143 int need_redraw = FALSE;
1145 remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
1151 consumed = snd_midi_event_encode (scherzo->snd_midi_event,
1152 next, remaining, &event);
1154 remaining -= consumed;
1157 switch (event.type) {
1158 case SND_SEQ_EVENT_NONE:
1159 /* Incomplete event. Nothing to do. */
1161 case SND_SEQ_EVENT_NOTEON:
1162 note = scherzo_press_note_midi (scherzo, event.data.note.note);
1163 _judge_note (scherzo, note);
1166 case SND_SEQ_EVENT_NOTEOFF:
1167 scherzo_release_note_midi (scherzo, event.data.note.note);
1168 _score_challenge (scherzo);
1171 case SND_SEQ_EVENT_CLOCK:
1172 /* Ignore for now as my piano sends a constant stream of these. */
1174 case SND_SEQ_EVENT_SENSING:
1175 /* Ignore for now as my piano sends a constant stream of these. */
1177 case SND_SEQ_EVENT_CONTROLLER:
1178 /* XXX: My piano gives me 64 for the sustain pedal, is
1179 * that universal? */
1180 if (event.data.control.param == 64) {
1181 if (event.data.control.value == 0)
1182 scherzo_release_pedal (scherzo);
1184 scherzo_press_pedal (scherzo);
1186 fprintf (stderr, "Fixme: Unhandled MIDI Control event (param=%d, value=%d)\n",
1187 event.data.control.param, event.data.control.value);
1191 fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
1198 gtk_widget_queue_draw (scherzo->window);
1200 /* Return TRUE to continue to get called in the future. */
1205 main (int argc, char *argv[])
1207 GtkWidget *drawing_area;
1211 srand (time (NULL));
1213 gtk_init (&argc, &argv);
1215 scherzo.ctx = talloc_new (NULL);
1217 scherzo.score = score_create (scherzo.ctx);
1218 scherzo.staff_height = 100;
1219 score_set_staff_height (scherzo.score, scherzo.staff_height);
1221 score_add_brace (scherzo.score, 2);
1222 scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
1223 scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
1225 scherzo.chord = NULL;
1227 /* Default to octave 4 and natural for computer keyboard keypresses. */
1228 scherzo.keyboard_octave = 4;
1229 scherzo.keyboard_accidental = SCORE_PITCH_ACCIDENTAL_NATURAL;
1231 note_group_init (scherzo.ctx, &scherzo.notes_pressed);
1232 note_group_init (scherzo.ctx, &scherzo.notes_pedaled);
1234 scherzo.pedal_pressed = 0;
1236 mnemon_init (&scherzo.mnemon);
1237 /* XXX: Should create a default file if one cannot be loaded. */
1238 mnemon_load_category (&scherzo.mnemon, "scherzo-notes");
1240 scherzo.challenge.note = NULL;
1242 _select_challenge (&scherzo);
1245 err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
1247 fprintf (stderr, "Out of memory.\n");
1251 #define MIDI_DEVICE "/dev/midi1"
1252 scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
1253 if (scherzo.midi_fd < 0) {
1254 printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
1256 GIOChannel *channel;
1258 channel = g_io_channel_unix_new (scherzo.midi_fd);
1259 g_io_channel_set_encoding (channel, NULL, NULL);
1260 g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
1263 scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1265 gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 1000, 600);
1267 g_signal_connect (scherzo.window, "delete-event",
1268 G_CALLBACK (on_delete_event_quit), NULL);
1270 drawing_area = gtk_drawing_area_new ();
1272 gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
1274 g_signal_connect (drawing_area, "expose-event",
1275 G_CALLBACK (on_expose_event_draw),
1278 g_signal_connect (scherzo.window, "key-press-event",
1279 G_CALLBACK (on_key_press_event),
1282 g_signal_connect (scherzo.window, "key-release-event",
1283 G_CALLBACK (on_key_release_event),
1286 gtk_widget_show_all (scherzo.window);
1290 mnemon_save (&scherzo.mnemon);
1292 snd_midi_event_free (scherzo.snd_midi_event);
1294 talloc_free (scherzo.ctx);