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 struct { modified_degree_t degrees[3]; const char *name; } triads[] = {
660 { {{1, 0}, {3, 0}, {5, +1}}, "Augmented triad" },
661 { {{1, 0}, {3, 0}, {5, 0}}, "Major triad" },
662 { {{1, 0}, {3, -1}, {5, 0}}, "Minor triad" },
663 { {{1, 0}, {3, -1}, {5, -1}}, "Diminished triad" }
666 struct { modified_degree_t degrees[4]; const char *name; } sevenths[] = {
667 { {{1, 0}, {3, 0}, {5, +1}, {7, 0}}, "Augmented/major 7" },
668 { {{1, 0}, {3, 0}, {5, +1}, {7, -1}}, "Augmented 7" },
669 { {{1, 0}, {3, 0}, {5, 0}, {7, 0}}, "Major 7" },
670 { {{1, 0}, {3, 0}, {5, 0}, {7, -1}}, "Dominant 7" },
671 { {{1, 0}, {3, -1}, {5, 0}, {7, 0}}, "Minor/major 7" },
672 { {{1, 0}, {3, -1}, {5, 0}, {7, -1}}, "Minor 7" },
673 { {{1, 0}, {3, -1}, {5, -1}, {7, 0}}, "Diminished/major 7" },
674 { {{1, 0}, {3, -1}, {5, -1}, {7, -1}}, "Half-diminished 7" },
675 { {{1, 0}, {3, -1}, {5, -1}, {7, -2}}, "Diminished 7" }
678 if (scherzo->chord) {
679 score_remove_chord (scherzo->chord);
680 scherzo->chord = NULL;
686 notes = talloc_array (local, analyzed_note_t, num_notes);
690 for (i = 0; i < num_notes; i++) {
691 score_note_t *note = note_group->notes[i];
692 notes[i].note = note;
693 notes[i].midi_pitch = _score_pitch_and_octave_to_midi (note->pitch,
695 /* Relative pitch will be filled in after sorting. */
696 notes[i].relative_pitch = 0;
699 /* First, sort by midi pitch to find the bass note. */
700 qsort (notes, num_notes, sizeof (analyzed_note_t),
701 _compare_analyzed_note_by_midi_pitch);
703 bass_pitch = notes[0].midi_pitch;
705 /* With the bass note identified, we can find all relative pitches. */
706 for (i = 0; i < num_notes; i++) {
707 notes[i].relative_pitch = notes[i].midi_pitch - bass_pitch;
708 while (notes[i].relative_pitch >= 12)
709 notes[i].relative_pitch -= 12;
712 /* Now, sort again by relative pitch. */
713 qsort (notes, num_notes, sizeof (analyzed_note_t),
714 _compare_analyzed_note_by_relative_pitch);
716 /* Finally, eliminate all duplicate notes. */
717 for (i = 0; i < num_notes - 1; i++) {
718 if (notes[i+1].relative_pitch == notes[i].relative_pitch) {
720 while (j < num_notes &&
721 notes[j].relative_pitch == notes[i].relative_pitch)
725 /* The loop incremented j one past the last
726 * duplicate. Decrement so that it points to the
727 * last duplicate (and is guaranteed to not exceed
728 * the array bounds).*/
731 if (j < num_notes - 1) {
732 memmove (¬es[i+1], ¬es[j+1],
733 (num_notes - j) * sizeof (analyzed_note_t));
736 num_notes -= (j - i);
742 for (i = 0; i < ARRAY_SIZE (octaves); i++) {
743 if (_chord_signature_matches (notes, num_notes,
744 octaves[i].degrees, 1,
747 chord_name = octaves[i].name;
753 for (i = 0; i < ARRAY_SIZE (intervals); i++) {
754 if (_chord_signature_matches (notes, num_notes,
755 intervals[i].degrees, 2,
758 chord_name = intervals[i].name;
764 for (i = 0; i < ARRAY_SIZE (triads); i++) {
765 if (_chord_signature_matches (notes, num_notes,
766 triads[i].degrees, 3,
769 chord_name = triads[i].name;
775 for (i = 0; i < ARRAY_SIZE(sevenths); i++) {
776 if (_chord_signature_matches (notes, num_notes,
777 sevenths[i].degrees, 4,
780 chord_name = sevenths[i].name;
789 const char *inversion_str;
792 inversion_str = "1st inversion";
795 inversion_str = "2nd inversion";
798 inversion_str = "3rd inversion";
801 fprintf (stderr, "Internal error: Unexpected inversion: %d\n",
805 chord_name = talloc_asprintf (local, "%s %s %s",
807 chord_name, inversion_str);
809 chord_name = talloc_asprintf (local, "%s %s",
814 chord_name = talloc_strdup (local, "Unknown chord");
817 scherzo->chord = score_add_chord (scherzo->treble, chord_name);
824 note_group_init (void *ctx, note_group_t *group)
829 group->num_notes = 0;
833 note_group_add_note (note_group_t *group, score_note_t *note)
837 /* Do nothing if note is already in group. */
838 for (i = 0; i < group->num_notes; i++) {
839 if (group->notes[i]->pitch == note->pitch &&
840 group->notes[i]->octave == note->octave)
848 if (group->num_notes > group->size) {
850 group->notes = talloc_realloc (group->ctx, group->notes,
851 score_note_t*, group->size);
853 if (group->notes == NULL) {
854 fprintf (stderr, "Out of memory.\n");
859 group->notes[group->num_notes - 1] = note;
863 note_group_remove_note_at (note_group_t *group, int i)
865 if (i >= group->num_notes) {
866 fprintf (stderr, "Internal error: No note to remove at index %d\n", i);
870 if (i < group->num_notes - 1) {
871 memmove (group->notes + i, group->notes + i + 1,
872 (group->num_notes - 1 - i) * sizeof (score_note_t*));
877 static score_note_t *
878 scherzo_press_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
880 score_staff_t *staff;
884 if (scherzo->challenge.note) {
885 staff = scherzo->challenge.staff;
886 } else if (octave >= 4) {
887 staff = scherzo->treble;
889 staff = scherzo->bass;
892 /* Do nothing if this note is already pressed. */
893 for (i = 0; i < scherzo->notes_pressed.num_notes; i++) {
894 if (scherzo->notes_pressed.notes[i]->pitch == pitch &&
895 scherzo->notes_pressed.notes[i]->octave == octave)
897 return scherzo->notes_pressed.notes[i];
901 note = score_add_note (staff, pitch, octave, SCORE_DURATION_WHOLE);
903 note_group_add_note (&scherzo->notes_pressed, note);
905 if (scherzo->pedal_pressed)
906 note_group_add_note (&scherzo->notes_pedaled, note);
908 scherzo_analyze_chord (scherzo);
914 scherzo_release_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
920 for (i = scherzo->notes_pressed.num_notes - 1; i >=0; i--) {
921 note = scherzo->notes_pressed.notes[i];
922 if (note->pitch == pitch && note->octave == octave) {
924 if (! scherzo->pedal_pressed)
925 score_remove_note (note);
926 note_group_remove_note_at (&scherzo->notes_pressed, i);
931 fprintf (stderr, "Internal error: Failed to find note to release.\n");
934 scherzo_analyze_chord (scherzo);
937 static score_note_t *
938 scherzo_press_note_midi (scherzo_t *scherzo, unsigned char midi_note)
943 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
945 return scherzo_press_note (scherzo, pitch, octave);
949 scherzo_release_note_midi (scherzo_t *scherzo, unsigned char midi_note)
954 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
956 scherzo_release_note (scherzo, pitch, octave);
960 scherzo_press_pedal (scherzo_t *scherzo)
964 scherzo->pedal_pressed = 1;
966 /* Copy all pressed notes to pedaled notes */
967 for (i = 0; i < scherzo->notes_pressed.num_notes; i++)
968 note_group_add_note (&scherzo->notes_pedaled, scherzo->notes_pressed.notes[i]);
972 scherzo_release_pedal (scherzo_t *scherzo)
974 score_note_t *note, *new_note;
977 /* Make new notes in score for all pressed notes. */
978 for (i = 0; i < scherzo->notes_pressed.num_notes; i++) {
979 note = scherzo->notes_pressed.notes[i];
980 new_note = score_add_note (note->staff, note->pitch, note->octave, note->duration);
981 scherzo->notes_pressed.notes[i] = new_note;
984 /* Then remove all previously pedaled notes from the score. */
985 for (i = scherzo->notes_pedaled.num_notes - 1; i >=0; i--) {
986 note = scherzo->notes_pedaled.notes[i];
987 score_remove_note (note);
988 note_group_remove_note_at (&scherzo->notes_pedaled, i);
991 scherzo->pedal_pressed = 0;
993 scherzo_analyze_chord (scherzo);
995 gtk_widget_queue_draw (scherzo->window);
999 _select_challenge (scherzo_t *scherzo)
1001 category_t *category_unused;
1002 bool_t introduced_unused;
1004 challenge_t *challenge = &scherzo->challenge;
1005 score_pitch_t pitch;
1009 if (challenge->note) {
1010 score_remove_note (challenge->note);
1011 challenge->note = NULL;
1014 mnemon_select_item (&scherzo->mnemon,
1016 &challenge->item_index,
1018 &introduced_unused);
1020 item = challenge->bin->items[challenge->item_index];
1022 s = item->challenge;
1023 if (strncmp (s, "treble:", 7) == 0) {
1025 challenge->staff = scherzo->treble;
1026 } else if (strncmp (s, "bass:", 5) == 0) {
1028 challenge->staff = scherzo->bass;
1031 "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
1038 pitch = SCORE_PITCH_C;
1041 pitch = SCORE_PITCH_D;
1044 pitch = SCORE_PITCH_E;
1047 pitch = SCORE_PITCH_F;
1050 pitch = SCORE_PITCH_G;
1053 pitch = SCORE_PITCH_A;
1056 pitch = SCORE_PITCH_B;
1059 fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
1064 if (*s < '0' || *s > '9') {
1065 fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
1071 challenge->note = score_add_note (challenge->staff, pitch, octave,
1072 SCORE_DURATION_WHOLE);
1073 challenge->satisfied = 0;
1074 challenge->mistaken = 0;
1077 /* Determine whether the user hit the correct note. */
1079 _judge_note (scherzo_t *scherzo, score_note_t *note)
1081 challenge_t *challenge = &scherzo->challenge;
1083 if (! scherzo->challenge.note) {
1084 score_set_note_color_rgb (note, 0.0, 0.0, 0.0); /* black */
1088 if (note->pitch == challenge->note->pitch &&
1089 note->octave == challenge->note->octave)
1091 challenge->satisfied = 1;
1092 score_set_note_color_rgb (note, 18/256., 130/256., 28/256.); /* green */
1096 challenge->mistaken = 1;
1097 score_set_note_color_rgb (note, 184/256., 4/256., 22/256.); /* red */
1101 /* If the user got the right note (eventually), then score it in
1102 * mnemon and show the next note. */
1104 _score_challenge (scherzo_t *scherzo)
1106 challenge_t *challenge = &scherzo->challenge;
1108 if (! challenge->note)
1111 if (! challenge->satisfied)
1114 mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
1115 ! challenge->mistaken);
1117 _select_challenge (scherzo);
1121 on_midi_input (unused (GIOChannel *channel),
1122 unused (GIOCondition condition),
1125 unsigned char buf[MIDI_BUF_SIZE], *next;
1126 scherzo_t *scherzo = user_data;
1128 snd_seq_event_t event;
1130 int need_redraw = FALSE;
1132 remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
1138 consumed = snd_midi_event_encode (scherzo->snd_midi_event,
1139 next, remaining, &event);
1141 remaining -= consumed;
1144 switch (event.type) {
1145 case SND_SEQ_EVENT_NONE:
1146 /* Incomplete event. Nothing to do. */
1148 case SND_SEQ_EVENT_NOTEON:
1149 note = scherzo_press_note_midi (scherzo, event.data.note.note);
1150 _judge_note (scherzo, note);
1153 case SND_SEQ_EVENT_NOTEOFF:
1154 scherzo_release_note_midi (scherzo, event.data.note.note);
1155 _score_challenge (scherzo);
1158 case SND_SEQ_EVENT_CLOCK:
1159 /* Ignore for now as my piano sends a constant stream of these. */
1161 case SND_SEQ_EVENT_SENSING:
1162 /* Ignore for now as my piano sends a constant stream of these. */
1164 case SND_SEQ_EVENT_CONTROLLER:
1165 /* XXX: My piano gives me 64 for the sustain pedal, is
1166 * that universal? */
1167 if (event.data.control.param == 64) {
1168 if (event.data.control.value == 0)
1169 scherzo_release_pedal (scherzo);
1171 scherzo_press_pedal (scherzo);
1173 fprintf (stderr, "Fixme: Unhandled MIDI Control event (param=%d, value=%d)\n",
1174 event.data.control.param, event.data.control.value);
1178 fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
1185 gtk_widget_queue_draw (scherzo->window);
1187 /* Return TRUE to continue to get called in the future. */
1192 main (int argc, char *argv[])
1194 GtkWidget *drawing_area;
1198 srand (time (NULL));
1200 gtk_init (&argc, &argv);
1202 scherzo.ctx = talloc_new (NULL);
1204 scherzo.score = score_create (scherzo.ctx);
1205 scherzo.staff_height = 100;
1206 score_set_staff_height (scherzo.score, scherzo.staff_height);
1208 score_add_brace (scherzo.score, 2);
1209 scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
1210 scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
1212 scherzo.chord = NULL;
1214 /* Default to octave 4 and natural for computer keyboard keypresses. */
1215 scherzo.keyboard_octave = 4;
1216 scherzo.keyboard_accidental = SCORE_PITCH_ACCIDENTAL_NATURAL;
1218 note_group_init (scherzo.ctx, &scherzo.notes_pressed);
1219 note_group_init (scherzo.ctx, &scherzo.notes_pedaled);
1221 scherzo.pedal_pressed = 0;
1223 mnemon_init (&scherzo.mnemon);
1224 /* XXX: Should create a default file if one cannot be loaded. */
1225 mnemon_load_category (&scherzo.mnemon, "scherzo-notes");
1227 scherzo.challenge.note = NULL;
1229 _select_challenge (&scherzo);
1232 err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
1234 fprintf (stderr, "Out of memory.\n");
1238 #define MIDI_DEVICE "/dev/midi1"
1239 scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
1240 if (scherzo.midi_fd < 0) {
1241 printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
1243 GIOChannel *channel;
1245 channel = g_io_channel_unix_new (scherzo.midi_fd);
1246 g_io_channel_set_encoding (channel, NULL, NULL);
1247 g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
1250 scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1252 gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 1000, 600);
1254 g_signal_connect (scherzo.window, "delete-event",
1255 G_CALLBACK (on_delete_event_quit), NULL);
1257 drawing_area = gtk_drawing_area_new ();
1259 gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
1261 g_signal_connect (drawing_area, "expose-event",
1262 G_CALLBACK (on_expose_event_draw),
1265 g_signal_connect (scherzo.window, "key-press-event",
1266 G_CALLBACK (on_key_press_event),
1269 g_signal_connect (scherzo.window, "key-release-event",
1270 G_CALLBACK (on_key_release_event),
1273 gtk_widget_show_all (scherzo.window);
1277 mnemon_save (&scherzo.mnemon);
1279 snd_midi_event_free (scherzo.snd_midi_event);
1281 talloc_free (scherzo.ctx);