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 score_pitch_name_t pitch_name;
153 if (scherzo->challenge.note)
154 octave = scherzo->challenge.note->octave;
156 octave = scherzo->keyboard_octave;
158 switch (key->keyval) {
162 case GDK_KEY_KP_Equal:
163 scherzo->staff_height += 4;
164 gtk_widget_queue_draw (widget);
168 case GDK_KEY_KP_Subtract:
169 scherzo->staff_height -= 4;
170 gtk_widget_queue_draw (widget);
180 pitch_name = SCORE_PITCH_NAME_C;
184 pitch_name = SCORE_PITCH_NAME_D;
188 pitch_name = SCORE_PITCH_NAME_E;
192 pitch_name = SCORE_PITCH_NAME_F;
196 pitch_name = SCORE_PITCH_NAME_G;
200 pitch_name = SCORE_PITCH_NAME_A;
204 pitch_name = SCORE_PITCH_NAME_B;
215 scherzo->keyboard_octave = key->keyval - GDK_KEY_0;
218 scherzo_press_pedal (scherzo);
221 if (scherzo->keyboard_accidental < SCORE_PITCH_ACCIDENTAL_DOUBLE_SHARP)
222 scherzo->keyboard_accidental++;
225 if (scherzo->keyboard_accidental > SCORE_PITCH_ACCIDENTAL_DOUBLE_FLAT)
226 scherzo->keyboard_accidental--;
230 pitch = SCORE_PITCH (pitch_name, scherzo->keyboard_accidental);
232 if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) ||
233 (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g))
237 note = scherzo_press_note (scherzo, pitch, octave);
238 _judge_note (scherzo, note);
239 gtk_widget_queue_draw (scherzo->window);
245 /* Allow an unhandled event to propagate to other handlers. */
250 on_key_release_event (unused (GtkWidget *widget),
254 scherzo_t *scherzo = user_data;
256 score_pitch_name_t pitch_name;
259 if (scherzo->challenge.note)
260 octave = scherzo->challenge.note->octave;
262 octave = scherzo->keyboard_octave;
264 switch (key->keyval) {
267 pitch_name = SCORE_PITCH_NAME_C;
271 pitch_name = SCORE_PITCH_NAME_D;
275 pitch_name = SCORE_PITCH_NAME_E;
279 pitch_name = SCORE_PITCH_NAME_F;
283 pitch_name = SCORE_PITCH_NAME_G;
287 pitch_name = SCORE_PITCH_NAME_A;
291 pitch_name = SCORE_PITCH_NAME_B;
294 scherzo_release_pedal (scherzo);
298 pitch = SCORE_PITCH (pitch_name, scherzo->keyboard_accidental);
300 if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) ||
301 (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g))
303 scherzo_release_note (scherzo, pitch, octave);
304 _score_challenge (scherzo);
305 gtk_widget_queue_draw (scherzo->window);
311 /* Allow an unhandled event to propagate to other handlers. */
316 _score_pitch_and_octave_to_midi (score_pitch_t pitch,
319 unsigned char midi_note = 12 * (octave + 1);
321 switch (SCORE_PITCH_NAME (pitch)) {
322 case SCORE_PITCH_NAME_C:
324 case SCORE_PITCH_NAME_D:
327 case SCORE_PITCH_NAME_E:
330 case SCORE_PITCH_NAME_F:
333 case SCORE_PITCH_NAME_G:
336 case SCORE_PITCH_NAME_A:
339 case SCORE_PITCH_NAME_B:
344 switch (SCORE_PITCH_ACCIDENTAL (pitch)) {
345 case SCORE_PITCH_ACCIDENTAL_DOUBLE_FLAT:
348 case SCORE_PITCH_ACCIDENTAL_FLAT:
351 case SCORE_PITCH_ACCIDENTAL_NATURAL:
353 case SCORE_PITCH_ACCIDENTAL_SHARP:
356 case SCORE_PITCH_ACCIDENTAL_DOUBLE_SHARP:
365 _midi_to_score_pitch_and_octave (unsigned char midi_note,
366 score_pitch_t *pitch,
369 *octave = midi_note / 12 - 1;
371 switch (midi_note % 12)
374 *pitch = SCORE_PITCH_C;
377 *pitch = SCORE_PITCH_Cs;
380 *pitch = SCORE_PITCH_D;
383 *pitch = SCORE_PITCH_Ds;
386 *pitch = SCORE_PITCH_E;
389 *pitch = SCORE_PITCH_F;
392 *pitch = SCORE_PITCH_Fs;
395 *pitch = SCORE_PITCH_G;
398 *pitch = SCORE_PITCH_Gs;
401 *pitch = SCORE_PITCH_A;
404 *pitch = SCORE_PITCH_As;
407 *pitch = SCORE_PITCH_B;
412 /* Determine a chord name (if any) from the current notes pressed */
414 typedef struct analyzed_note {
415 /* Original note being analzyed. */
418 /* Absolute pitch (expressed as midi number). */
421 /* Pitch relative to bass note. */
426 _compare_analyzed_note_by_midi_pitch (const void *va, const void *vb)
428 const analyzed_note_t *a = va, *b = vb;
430 return a->midi_pitch - b->midi_pitch;
434 _compare_analyzed_note_by_relative_pitch (const void *va, const void *vb)
436 const analyzed_note_t *a = va, *b = vb;
438 return a->relative_pitch - b->relative_pitch;
441 typedef struct modified_degree
448 _modified_degree_to_half_steps (const modified_degree_t *degree)
452 /* Number of half steps from root to specified degree within a
453 * diatonic scaled. */
454 switch (degree->degree) {
477 fprintf (stderr, "Internal: Invalid degree %d\n", degree->degree);
482 return half_steps + degree->modification;
486 _chord_signature_matches (analyzed_note_t *notes,
488 modified_degree_t *degrees,
491 #define MAX_DEGREES 4
492 int relative_pitches[MAX_DEGREES];
495 assert (num_degrees <= MAX_DEGREES);
497 if (num_notes != num_degrees)
500 for (i = 0; i < num_degrees; i++)
501 relative_pitches[i] = _modified_degree_to_half_steps (°rees[i]);
503 for (i = 0; i < num_notes; i++)
504 if (notes[i].relative_pitch != relative_pitches[i])
511 scherzo_analyze_chord (scherzo_t *scherzo)
513 void *local = talloc_new (NULL);
514 analyzed_note_t *notes;
515 note_group_t *note_group;
516 unsigned i, j, num_notes;
518 const char *chord_name = NULL;
520 if (scherzo->pedal_pressed)
521 note_group = &scherzo->notes_pedaled;
523 note_group = &scherzo->notes_pressed;
525 num_notes = note_group->num_notes;
527 struct { modified_degree_t degrees[1]; const char *name; } octaves[] = {
528 { {{0, 0}}, "Octave"}
531 struct { modified_degree_t degrees[2]; const char *name; } intervals[] = {
532 { {{1, 0}, {2, -1}}, "Minor 2nd"},
533 { {{1, 0}, {2, 0}}, "Major 2nd"},
534 { {{1, 0}, {3, -1}}, "Minor 3rd"},
535 { {{1, 0}, {3, 0}}, "Major 3rd"},
536 { {{1, 0}, {4, 0}}, "Perfect 4th"},
537 { {{1, 0}, {5, -1}}, "Diminished 5th"},
538 { {{1, 0}, {5, 0}}, "Perfect 5th"},
539 { {{1, 0}, {6, -1}}, "Minor 6th"},
540 { {{1, 0}, {6, 0}}, "Major 6th"},
541 { {{1, 0}, {7, -1}}, "Minor 7th"},
542 { {{1, 0}, {7, 0}}, "Major 7th"}
545 struct { modified_degree_t degrees[3]; const char *name; } triads[] = {
546 { {{1, 0}, {3, 0}, {5, +1}}, "Augmented triad" },
547 { {{1, 0}, {3, 0}, {5, 0}}, "Major triad" },
548 { {{1, 0}, {3, -1}, {5, 0}}, "Minor triad" },
549 { {{1, 0}, {3, -1}, {5, -1}}, "Diminished triad" }
552 struct { modified_degree_t degrees[4]; const char *name; } sevenths[] = {
553 { {{1, 0}, {3, 0}, {5, +1}, {7, 0}}, "Augmented/major 7" },
554 { {{1, 0}, {3, 0}, {5, +1}, {7, -1}}, "Augmented 7" },
555 { {{1, 0}, {3, 0}, {5, 0}, {7, 0}}, "Major 7" },
556 { {{1, 0}, {3, 0}, {5, 0}, {7, -1}}, "Dominant 7" },
557 { {{1, 0}, {3, -1}, {5, 0}, {7, 0}}, "Minor/major 7" },
558 { {{1, 0}, {3, -1}, {5, 0}, {7, -1}}, "Minor 7" },
559 { {{1, 0}, {3, -1}, {5, -1}, {7, 0}}, "Diminished/major 7" },
560 { {{1, 0}, {3, -1}, {5, -1}, {7, -1}}, "Half-diminished 7" },
561 { {{1, 0}, {3, -1}, {5, -1}, {7, -2}}, "Diminished 7" }
564 if (scherzo->chord) {
565 score_remove_chord (scherzo->chord);
566 scherzo->chord = NULL;
572 notes = talloc_array (local, analyzed_note_t, num_notes);
576 for (i = 0; i < num_notes; i++) {
577 score_note_t *note = note_group->notes[i];
578 notes[i].note = note;
579 notes[i].midi_pitch = _score_pitch_and_octave_to_midi (note->pitch,
581 /* Relative pitch will be filled in after sorting. */
582 notes[i].relative_pitch = 0;
585 /* First, sort by midi pitch to find the bass note. */
586 qsort (notes, num_notes, sizeof (analyzed_note_t),
587 _compare_analyzed_note_by_midi_pitch);
589 bass_pitch = notes[0].midi_pitch;
591 /* With the bass note identified, we can find all relative pitches. */
592 for (i = 0; i < num_notes; i++) {
593 notes[i].relative_pitch = notes[i].midi_pitch - bass_pitch;
594 while (notes[i].relative_pitch >= 12)
595 notes[i].relative_pitch -= 12;
598 /* Now, sort again by relative pitch. */
599 qsort (notes, num_notes, sizeof (analyzed_note_t),
600 _compare_analyzed_note_by_relative_pitch);
602 /* Finally, eliminate all duplicate notes. */
603 for (i = 0; i < num_notes - 1; i++) {
604 if (notes[i+1].relative_pitch == notes[i].relative_pitch) {
606 while (j < num_notes &&
607 notes[j].relative_pitch == notes[i].relative_pitch)
611 /* The loop incremented j one past the last
612 * duplicate. Decrement so that it points to the
613 * last duplicate (and is guaranteed to not exceed
614 * the array bounds).*/
617 if (j < num_notes - 1) {
618 memmove (¬es[i+1], ¬es[j+1],
619 (num_notes - j) * sizeof (analyzed_note_t));
622 num_notes -= (j - i);
628 for (i = 0; i < ARRAY_SIZE (octaves); i++) {
629 if (_chord_signature_matches (notes, num_notes,
630 octaves[i].degrees, 1))
631 chord_name = octaves[i].name;
635 for (i = 0; i < ARRAY_SIZE (intervals); i++) {
636 if (_chord_signature_matches (notes, num_notes,
637 intervals[i].degrees, 2))
638 chord_name = intervals[i].name;
642 for (i = 0; i < ARRAY_SIZE (triads); i++) {
643 if (_chord_signature_matches (notes, num_notes,
644 triads[i].degrees, 3))
645 chord_name = triads[i].name;
649 for (i = 0; i < ARRAY_SIZE(sevenths); i++) {
650 if (_chord_signature_matches (notes, num_notes,
651 sevenths[i].degrees, 4))
652 chord_name = sevenths[i].name;
658 scherzo->chord = score_add_chord (scherzo->treble, chord_name);
660 scherzo->chord = score_add_chord (scherzo->treble, "Unknown or not a chord");
667 note_group_init (void *ctx, note_group_t *group)
672 group->num_notes = 0;
676 note_group_add_note (note_group_t *group, score_note_t *note)
680 /* Do nothing if note is already in group. */
681 for (i = 0; i < group->num_notes; i++) {
682 if (group->notes[i]->pitch == note->pitch &&
683 group->notes[i]->octave == note->octave)
691 if (group->num_notes > group->size) {
693 group->notes = talloc_realloc (group->ctx, group->notes,
694 score_note_t*, group->size);
696 if (group->notes == NULL) {
697 fprintf (stderr, "Out of memory.\n");
702 group->notes[group->num_notes - 1] = note;
706 note_group_remove_note_at (note_group_t *group, int i)
708 if (i >= group->num_notes) {
709 fprintf (stderr, "Internal error: No note to remove at index %d\n", i);
713 if (i < group->num_notes - 1) {
714 memmove (group->notes + i, group->notes + i + 1,
715 (group->num_notes - 1 - i) * sizeof (score_note_t*));
720 static score_note_t *
721 scherzo_press_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
723 score_staff_t *staff;
727 if (scherzo->challenge.note) {
728 staff = scherzo->challenge.staff;
729 } else if (octave >= 4) {
730 staff = scherzo->treble;
732 staff = scherzo->bass;
735 /* Do nothing if this note is already pressed. */
736 for (i = 0; i < scherzo->notes_pressed.num_notes; i++) {
737 if (scherzo->notes_pressed.notes[i]->pitch == pitch &&
738 scherzo->notes_pressed.notes[i]->octave == octave)
740 return scherzo->notes_pressed.notes[i];
744 note = score_add_note (staff, pitch, octave, SCORE_DURATION_WHOLE);
746 note_group_add_note (&scherzo->notes_pressed, note);
748 if (scherzo->pedal_pressed)
749 note_group_add_note (&scherzo->notes_pedaled, note);
751 scherzo_analyze_chord (scherzo);
757 scherzo_release_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
763 for (i = scherzo->notes_pressed.num_notes - 1; i >=0; i--) {
764 note = scherzo->notes_pressed.notes[i];
765 if (note->pitch == pitch && note->octave == octave) {
767 if (! scherzo->pedal_pressed)
768 score_remove_note (note);
769 note_group_remove_note_at (&scherzo->notes_pressed, i);
774 fprintf (stderr, "Internal error: Failed to find note to release.\n");
777 scherzo_analyze_chord (scherzo);
780 static score_note_t *
781 scherzo_press_note_midi (scherzo_t *scherzo, unsigned char midi_note)
786 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
788 return scherzo_press_note (scherzo, pitch, octave);
792 scherzo_release_note_midi (scherzo_t *scherzo, unsigned char midi_note)
797 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
799 scherzo_release_note (scherzo, pitch, octave);
803 scherzo_press_pedal (scherzo_t *scherzo)
807 scherzo->pedal_pressed = 1;
809 /* Copy all pressed notes to pedaled notes */
810 for (i = 0; i < scherzo->notes_pressed.num_notes; i++)
811 note_group_add_note (&scherzo->notes_pedaled, scherzo->notes_pressed.notes[i]);
815 scherzo_release_pedal (scherzo_t *scherzo)
817 score_note_t *note, *new_note;
820 /* Make new notes in score for all pressed notes. */
821 for (i = 0; i < scherzo->notes_pressed.num_notes; i++) {
822 note = scherzo->notes_pressed.notes[i];
823 new_note = score_add_note (note->staff, note->pitch, note->octave, note->duration);
824 scherzo->notes_pressed.notes[i] = new_note;
827 /* Then remove all previously pedaled notes from the score. */
828 for (i = scherzo->notes_pedaled.num_notes - 1; i >=0; i--) {
829 note = scherzo->notes_pedaled.notes[i];
830 score_remove_note (note);
831 note_group_remove_note_at (&scherzo->notes_pedaled, i);
834 scherzo->pedal_pressed = 0;
836 scherzo_analyze_chord (scherzo);
838 gtk_widget_queue_draw (scherzo->window);
842 _select_challenge (scherzo_t *scherzo)
844 category_t *category_unused;
845 bool_t introduced_unused;
847 challenge_t *challenge = &scherzo->challenge;
852 if (challenge->note) {
853 score_remove_note (challenge->note);
854 challenge->note = NULL;
857 mnemon_select_item (&scherzo->mnemon,
859 &challenge->item_index,
863 item = challenge->bin->items[challenge->item_index];
866 if (strncmp (s, "treble:", 7) == 0) {
868 challenge->staff = scherzo->treble;
869 } else if (strncmp (s, "bass:", 5) == 0) {
871 challenge->staff = scherzo->bass;
874 "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
881 pitch = SCORE_PITCH_C;
884 pitch = SCORE_PITCH_D;
887 pitch = SCORE_PITCH_E;
890 pitch = SCORE_PITCH_F;
893 pitch = SCORE_PITCH_G;
896 pitch = SCORE_PITCH_A;
899 pitch = SCORE_PITCH_B;
902 fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
907 if (*s < '0' || *s > '9') {
908 fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
914 challenge->note = score_add_note (challenge->staff, pitch, octave,
915 SCORE_DURATION_WHOLE);
916 challenge->satisfied = 0;
917 challenge->mistaken = 0;
920 /* Determine whether the user hit the correct note. */
922 _judge_note (scherzo_t *scherzo, score_note_t *note)
924 challenge_t *challenge = &scherzo->challenge;
926 if (! scherzo->challenge.note) {
927 score_set_note_color_rgb (note, 0.0, 0.0, 0.0); /* black */
931 if (note->pitch == challenge->note->pitch &&
932 note->octave == challenge->note->octave)
934 challenge->satisfied = 1;
935 score_set_note_color_rgb (note, 18/256., 130/256., 28/256.); /* green */
939 challenge->mistaken = 1;
940 score_set_note_color_rgb (note, 184/256., 4/256., 22/256.); /* red */
944 /* If the user got the right note (eventually), then score it in
945 * mnemon and show the next note. */
947 _score_challenge (scherzo_t *scherzo)
949 challenge_t *challenge = &scherzo->challenge;
951 if (! challenge->note)
954 if (! challenge->satisfied)
957 mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
958 ! challenge->mistaken);
960 _select_challenge (scherzo);
964 on_midi_input (unused (GIOChannel *channel),
965 unused (GIOCondition condition),
968 unsigned char buf[MIDI_BUF_SIZE], *next;
969 scherzo_t *scherzo = user_data;
971 snd_seq_event_t event;
973 int need_redraw = FALSE;
975 remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
981 consumed = snd_midi_event_encode (scherzo->snd_midi_event,
982 next, remaining, &event);
984 remaining -= consumed;
987 switch (event.type) {
988 case SND_SEQ_EVENT_NONE:
989 /* Incomplete event. Nothing to do. */
991 case SND_SEQ_EVENT_NOTEON:
992 note = scherzo_press_note_midi (scherzo, event.data.note.note);
993 _judge_note (scherzo, note);
996 case SND_SEQ_EVENT_NOTEOFF:
997 scherzo_release_note_midi (scherzo, event.data.note.note);
998 _score_challenge (scherzo);
1001 case SND_SEQ_EVENT_CLOCK:
1002 /* Ignore for now as my piano sends a constant stream of these. */
1004 case SND_SEQ_EVENT_SENSING:
1005 /* Ignore for now as my piano sends a constant stream of these. */
1007 case SND_SEQ_EVENT_CONTROLLER:
1008 /* XXX: My piano gives me 64 for the sustain pedal, is
1009 * that universal? */
1010 if (event.data.control.param == 64) {
1011 if (event.data.control.value == 0)
1012 scherzo_release_pedal (scherzo);
1014 scherzo_press_pedal (scherzo);
1016 fprintf (stderr, "Fixme: Unhandled MIDI Control event (param=%d, value=%d)\n",
1017 event.data.control.param, event.data.control.value);
1021 fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
1028 gtk_widget_queue_draw (scherzo->window);
1030 /* Return TRUE to continue to get called in the future. */
1035 main (int argc, char *argv[])
1037 GtkWidget *drawing_area;
1041 srand (time (NULL));
1043 gtk_init (&argc, &argv);
1045 scherzo.ctx = talloc_new (NULL);
1047 scherzo.score = score_create (scherzo.ctx);
1048 scherzo.staff_height = 100;
1049 score_set_staff_height (scherzo.score, scherzo.staff_height);
1051 score_add_brace (scherzo.score, 2);
1052 scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
1053 scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
1055 scherzo.chord = NULL;
1057 /* Default to octave 4 and natural for computer keyboard keypresses. */
1058 scherzo.keyboard_octave = 4;
1059 scherzo.keyboard_accidental = SCORE_PITCH_ACCIDENTAL_NATURAL;
1061 note_group_init (scherzo.ctx, &scherzo.notes_pressed);
1062 note_group_init (scherzo.ctx, &scherzo.notes_pedaled);
1064 scherzo.pedal_pressed = 0;
1066 mnemon_init (&scherzo.mnemon);
1067 /* XXX: Should create a default file if one cannot be loaded. */
1068 mnemon_load_category (&scherzo.mnemon, "scherzo-notes");
1070 scherzo.challenge.note = NULL;
1072 _select_challenge (&scherzo);
1075 err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
1077 fprintf (stderr, "Out of memory.\n");
1081 #define MIDI_DEVICE "/dev/midi1"
1082 scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
1083 if (scherzo.midi_fd < 0) {
1084 printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
1086 GIOChannel *channel;
1088 channel = g_io_channel_unix_new (scherzo.midi_fd);
1089 g_io_channel_set_encoding (channel, NULL, NULL);
1090 g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
1093 scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1095 gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 1000, 600);
1097 g_signal_connect (scherzo.window, "delete-event",
1098 G_CALLBACK (on_delete_event_quit), NULL);
1100 drawing_area = gtk_drawing_area_new ();
1102 gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
1104 g_signal_connect (drawing_area, "expose-event",
1105 G_CALLBACK (on_expose_event_draw),
1108 g_signal_connect (scherzo.window, "key-press-event",
1109 G_CALLBACK (on_key_press_event),
1112 g_signal_connect (scherzo.window, "key-release-event",
1113 G_CALLBACK (on_key_release_event),
1116 gtk_widget_show_all (scherzo.window);
1120 mnemon_save (&scherzo.mnemon);
1122 snd_midi_event_free (scherzo.snd_midi_event);
1124 talloc_free (scherzo.ctx);