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,
494 #define MAX_DEGREES 4
495 int relative_pitches[MAX_DEGREES];
496 int inversion, max_inversions;
499 assert (num_degrees <= MAX_DEGREES);
501 if (num_notes != num_degrees)
504 max_inversions = num_degrees;
506 /* We never spell simple intervals as inversions. */
507 if (num_degrees == 2)
510 for (inversion = 0; inversion < max_inversions; inversion++) {
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 *inversion_ret = inversion;
540 scherzo_analyze_chord (scherzo_t *scherzo)
542 void *local = talloc_new (NULL);
543 analyzed_note_t *notes;
544 note_group_t *note_group;
545 unsigned i, j, num_notes;
546 int bass_pitch, inversion;
547 const char *chord_name = NULL;
549 if (scherzo->pedal_pressed)
550 note_group = &scherzo->notes_pedaled;
552 note_group = &scherzo->notes_pressed;
554 num_notes = note_group->num_notes;
556 struct { modified_degree_t degrees[1]; const char *name; } octaves[] = {
557 { {{1, 0}}, "Octave"}
560 struct { modified_degree_t degrees[2]; const char *name; } intervals[] = {
561 { {{1, 0}, {2, -1}}, "Minor 2nd"},
562 { {{1, 0}, {2, 0}}, "Major 2nd"},
563 { {{1, 0}, {3, -1}}, "Minor 3rd"},
564 { {{1, 0}, {3, 0}}, "Major 3rd"},
565 { {{1, 0}, {4, 0}}, "Perfect 4th"},
566 { {{1, 0}, {5, -1}}, "Diminished 5th"},
567 { {{1, 0}, {5, 0}}, "Perfect 5th"},
568 { {{1, 0}, {6, -1}}, "Minor 6th"},
569 { {{1, 0}, {6, 0}}, "Major 6th"},
570 { {{1, 0}, {7, -1}}, "Minor 7th"},
571 { {{1, 0}, {7, 0}}, "Major 7th"}
574 struct { modified_degree_t degrees[3]; const char *name; } triads[] = {
575 { {{1, 0}, {3, 0}, {5, +1}}, "Augmented triad" },
576 { {{1, 0}, {3, 0}, {5, 0}}, "Major triad" },
577 { {{1, 0}, {3, -1}, {5, 0}}, "Minor triad" },
578 { {{1, 0}, {3, -1}, {5, -1}}, "Diminished triad" }
581 struct { modified_degree_t degrees[4]; const char *name; } sevenths[] = {
582 { {{1, 0}, {3, 0}, {5, +1}, {7, 0}}, "Augmented/major 7" },
583 { {{1, 0}, {3, 0}, {5, +1}, {7, -1}}, "Augmented 7" },
584 { {{1, 0}, {3, 0}, {5, 0}, {7, 0}}, "Major 7" },
585 { {{1, 0}, {3, 0}, {5, 0}, {7, -1}}, "Dominant 7" },
586 { {{1, 0}, {3, -1}, {5, 0}, {7, 0}}, "Minor/major 7" },
587 { {{1, 0}, {3, -1}, {5, 0}, {7, -1}}, "Minor 7" },
588 { {{1, 0}, {3, -1}, {5, -1}, {7, 0}}, "Diminished/major 7" },
589 { {{1, 0}, {3, -1}, {5, -1}, {7, -1}}, "Half-diminished 7" },
590 { {{1, 0}, {3, -1}, {5, -1}, {7, -2}}, "Diminished 7" }
593 if (scherzo->chord) {
594 score_remove_chord (scherzo->chord);
595 scherzo->chord = NULL;
601 notes = talloc_array (local, analyzed_note_t, num_notes);
605 for (i = 0; i < num_notes; i++) {
606 score_note_t *note = note_group->notes[i];
607 notes[i].note = note;
608 notes[i].midi_pitch = _score_pitch_and_octave_to_midi (note->pitch,
610 /* Relative pitch will be filled in after sorting. */
611 notes[i].relative_pitch = 0;
614 /* First, sort by midi pitch to find the bass note. */
615 qsort (notes, num_notes, sizeof (analyzed_note_t),
616 _compare_analyzed_note_by_midi_pitch);
618 bass_pitch = notes[0].midi_pitch;
620 /* With the bass note identified, we can find all relative pitches. */
621 for (i = 0; i < num_notes; i++) {
622 notes[i].relative_pitch = notes[i].midi_pitch - bass_pitch;
623 while (notes[i].relative_pitch >= 12)
624 notes[i].relative_pitch -= 12;
627 /* Now, sort again by relative pitch. */
628 qsort (notes, num_notes, sizeof (analyzed_note_t),
629 _compare_analyzed_note_by_relative_pitch);
631 /* Finally, eliminate all duplicate notes. */
632 for (i = 0; i < num_notes - 1; i++) {
633 if (notes[i+1].relative_pitch == notes[i].relative_pitch) {
635 while (j < num_notes &&
636 notes[j].relative_pitch == notes[i].relative_pitch)
640 /* The loop incremented j one past the last
641 * duplicate. Decrement so that it points to the
642 * last duplicate (and is guaranteed to not exceed
643 * the array bounds).*/
646 if (j < num_notes - 1) {
647 memmove (¬es[i+1], ¬es[j+1],
648 (num_notes - j) * sizeof (analyzed_note_t));
651 num_notes -= (j - i);
657 for (i = 0; i < ARRAY_SIZE (octaves); i++) {
658 if (_chord_signature_matches (notes, num_notes,
659 octaves[i].degrees, 1, &inversion))
661 chord_name = octaves[i].name;
667 for (i = 0; i < ARRAY_SIZE (intervals); i++) {
668 if (_chord_signature_matches (notes, num_notes,
669 intervals[i].degrees, 2, &inversion))
671 chord_name = intervals[i].name;
677 for (i = 0; i < ARRAY_SIZE (triads); i++) {
678 if (_chord_signature_matches (notes, num_notes,
679 triads[i].degrees, 3, &inversion))
681 chord_name = triads[i].name;
687 for (i = 0; i < ARRAY_SIZE(sevenths); i++) {
688 if (_chord_signature_matches (notes, num_notes,
689 sevenths[i].degrees, 4, &inversion))
691 chord_name = sevenths[i].name;
700 const char *inversion_str;
703 inversion_str = "1st inversion";
706 inversion_str = "2nd inversion";
709 inversion_str = "3rd inversion";
712 fprintf (stderr, "Internal error: Unexpected inversion: %d\n",
716 chord_name = talloc_asprintf (local, "%s %s",
717 chord_name, inversion_str);
719 chord_name = talloc_strdup (local, chord_name);
722 chord_name = talloc_strdup (local, "Unknown chord");
725 scherzo->chord = score_add_chord (scherzo->treble, chord_name);
732 note_group_init (void *ctx, note_group_t *group)
737 group->num_notes = 0;
741 note_group_add_note (note_group_t *group, score_note_t *note)
745 /* Do nothing if note is already in group. */
746 for (i = 0; i < group->num_notes; i++) {
747 if (group->notes[i]->pitch == note->pitch &&
748 group->notes[i]->octave == note->octave)
756 if (group->num_notes > group->size) {
758 group->notes = talloc_realloc (group->ctx, group->notes,
759 score_note_t*, group->size);
761 if (group->notes == NULL) {
762 fprintf (stderr, "Out of memory.\n");
767 group->notes[group->num_notes - 1] = note;
771 note_group_remove_note_at (note_group_t *group, int i)
773 if (i >= group->num_notes) {
774 fprintf (stderr, "Internal error: No note to remove at index %d\n", i);
778 if (i < group->num_notes - 1) {
779 memmove (group->notes + i, group->notes + i + 1,
780 (group->num_notes - 1 - i) * sizeof (score_note_t*));
785 static score_note_t *
786 scherzo_press_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
788 score_staff_t *staff;
792 if (scherzo->challenge.note) {
793 staff = scherzo->challenge.staff;
794 } else if (octave >= 4) {
795 staff = scherzo->treble;
797 staff = scherzo->bass;
800 /* Do nothing if this note is already pressed. */
801 for (i = 0; i < scherzo->notes_pressed.num_notes; i++) {
802 if (scherzo->notes_pressed.notes[i]->pitch == pitch &&
803 scherzo->notes_pressed.notes[i]->octave == octave)
805 return scherzo->notes_pressed.notes[i];
809 note = score_add_note (staff, pitch, octave, SCORE_DURATION_WHOLE);
811 note_group_add_note (&scherzo->notes_pressed, note);
813 if (scherzo->pedal_pressed)
814 note_group_add_note (&scherzo->notes_pedaled, note);
816 scherzo_analyze_chord (scherzo);
822 scherzo_release_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
828 for (i = scherzo->notes_pressed.num_notes - 1; i >=0; i--) {
829 note = scherzo->notes_pressed.notes[i];
830 if (note->pitch == pitch && note->octave == octave) {
832 if (! scherzo->pedal_pressed)
833 score_remove_note (note);
834 note_group_remove_note_at (&scherzo->notes_pressed, i);
839 fprintf (stderr, "Internal error: Failed to find note to release.\n");
842 scherzo_analyze_chord (scherzo);
845 static score_note_t *
846 scherzo_press_note_midi (scherzo_t *scherzo, unsigned char midi_note)
851 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
853 return scherzo_press_note (scherzo, pitch, octave);
857 scherzo_release_note_midi (scherzo_t *scherzo, unsigned char midi_note)
862 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
864 scherzo_release_note (scherzo, pitch, octave);
868 scherzo_press_pedal (scherzo_t *scherzo)
872 scherzo->pedal_pressed = 1;
874 /* Copy all pressed notes to pedaled notes */
875 for (i = 0; i < scherzo->notes_pressed.num_notes; i++)
876 note_group_add_note (&scherzo->notes_pedaled, scherzo->notes_pressed.notes[i]);
880 scherzo_release_pedal (scherzo_t *scherzo)
882 score_note_t *note, *new_note;
885 /* Make new notes in score for all pressed notes. */
886 for (i = 0; i < scherzo->notes_pressed.num_notes; i++) {
887 note = scherzo->notes_pressed.notes[i];
888 new_note = score_add_note (note->staff, note->pitch, note->octave, note->duration);
889 scherzo->notes_pressed.notes[i] = new_note;
892 /* Then remove all previously pedaled notes from the score. */
893 for (i = scherzo->notes_pedaled.num_notes - 1; i >=0; i--) {
894 note = scherzo->notes_pedaled.notes[i];
895 score_remove_note (note);
896 note_group_remove_note_at (&scherzo->notes_pedaled, i);
899 scherzo->pedal_pressed = 0;
901 scherzo_analyze_chord (scherzo);
903 gtk_widget_queue_draw (scherzo->window);
907 _select_challenge (scherzo_t *scherzo)
909 category_t *category_unused;
910 bool_t introduced_unused;
912 challenge_t *challenge = &scherzo->challenge;
917 if (challenge->note) {
918 score_remove_note (challenge->note);
919 challenge->note = NULL;
922 mnemon_select_item (&scherzo->mnemon,
924 &challenge->item_index,
928 item = challenge->bin->items[challenge->item_index];
931 if (strncmp (s, "treble:", 7) == 0) {
933 challenge->staff = scherzo->treble;
934 } else if (strncmp (s, "bass:", 5) == 0) {
936 challenge->staff = scherzo->bass;
939 "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
946 pitch = SCORE_PITCH_C;
949 pitch = SCORE_PITCH_D;
952 pitch = SCORE_PITCH_E;
955 pitch = SCORE_PITCH_F;
958 pitch = SCORE_PITCH_G;
961 pitch = SCORE_PITCH_A;
964 pitch = SCORE_PITCH_B;
967 fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
972 if (*s < '0' || *s > '9') {
973 fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
979 challenge->note = score_add_note (challenge->staff, pitch, octave,
980 SCORE_DURATION_WHOLE);
981 challenge->satisfied = 0;
982 challenge->mistaken = 0;
985 /* Determine whether the user hit the correct note. */
987 _judge_note (scherzo_t *scherzo, score_note_t *note)
989 challenge_t *challenge = &scherzo->challenge;
991 if (! scherzo->challenge.note) {
992 score_set_note_color_rgb (note, 0.0, 0.0, 0.0); /* black */
996 if (note->pitch == challenge->note->pitch &&
997 note->octave == challenge->note->octave)
999 challenge->satisfied = 1;
1000 score_set_note_color_rgb (note, 18/256., 130/256., 28/256.); /* green */
1004 challenge->mistaken = 1;
1005 score_set_note_color_rgb (note, 184/256., 4/256., 22/256.); /* red */
1009 /* If the user got the right note (eventually), then score it in
1010 * mnemon and show the next note. */
1012 _score_challenge (scherzo_t *scherzo)
1014 challenge_t *challenge = &scherzo->challenge;
1016 if (! challenge->note)
1019 if (! challenge->satisfied)
1022 mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
1023 ! challenge->mistaken);
1025 _select_challenge (scherzo);
1029 on_midi_input (unused (GIOChannel *channel),
1030 unused (GIOCondition condition),
1033 unsigned char buf[MIDI_BUF_SIZE], *next;
1034 scherzo_t *scherzo = user_data;
1036 snd_seq_event_t event;
1038 int need_redraw = FALSE;
1040 remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
1046 consumed = snd_midi_event_encode (scherzo->snd_midi_event,
1047 next, remaining, &event);
1049 remaining -= consumed;
1052 switch (event.type) {
1053 case SND_SEQ_EVENT_NONE:
1054 /* Incomplete event. Nothing to do. */
1056 case SND_SEQ_EVENT_NOTEON:
1057 note = scherzo_press_note_midi (scherzo, event.data.note.note);
1058 _judge_note (scherzo, note);
1061 case SND_SEQ_EVENT_NOTEOFF:
1062 scherzo_release_note_midi (scherzo, event.data.note.note);
1063 _score_challenge (scherzo);
1066 case SND_SEQ_EVENT_CLOCK:
1067 /* Ignore for now as my piano sends a constant stream of these. */
1069 case SND_SEQ_EVENT_SENSING:
1070 /* Ignore for now as my piano sends a constant stream of these. */
1072 case SND_SEQ_EVENT_CONTROLLER:
1073 /* XXX: My piano gives me 64 for the sustain pedal, is
1074 * that universal? */
1075 if (event.data.control.param == 64) {
1076 if (event.data.control.value == 0)
1077 scherzo_release_pedal (scherzo);
1079 scherzo_press_pedal (scherzo);
1081 fprintf (stderr, "Fixme: Unhandled MIDI Control event (param=%d, value=%d)\n",
1082 event.data.control.param, event.data.control.value);
1086 fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
1093 gtk_widget_queue_draw (scherzo->window);
1095 /* Return TRUE to continue to get called in the future. */
1100 main (int argc, char *argv[])
1102 GtkWidget *drawing_area;
1106 srand (time (NULL));
1108 gtk_init (&argc, &argv);
1110 scherzo.ctx = talloc_new (NULL);
1112 scherzo.score = score_create (scherzo.ctx);
1113 scherzo.staff_height = 100;
1114 score_set_staff_height (scherzo.score, scherzo.staff_height);
1116 score_add_brace (scherzo.score, 2);
1117 scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
1118 scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
1120 scherzo.chord = NULL;
1122 /* Default to octave 4 and natural for computer keyboard keypresses. */
1123 scherzo.keyboard_octave = 4;
1124 scherzo.keyboard_accidental = SCORE_PITCH_ACCIDENTAL_NATURAL;
1126 note_group_init (scherzo.ctx, &scherzo.notes_pressed);
1127 note_group_init (scherzo.ctx, &scherzo.notes_pedaled);
1129 scherzo.pedal_pressed = 0;
1131 mnemon_init (&scherzo.mnemon);
1132 /* XXX: Should create a default file if one cannot be loaded. */
1133 mnemon_load_category (&scherzo.mnemon, "scherzo-notes");
1135 scherzo.challenge.note = NULL;
1137 _select_challenge (&scherzo);
1140 err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
1142 fprintf (stderr, "Out of memory.\n");
1146 #define MIDI_DEVICE "/dev/midi1"
1147 scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
1148 if (scherzo.midi_fd < 0) {
1149 printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
1151 GIOChannel *channel;
1153 channel = g_io_channel_unix_new (scherzo.midi_fd);
1154 g_io_channel_set_encoding (channel, NULL, NULL);
1155 g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
1158 scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1160 gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 1000, 600);
1162 g_signal_connect (scherzo.window, "delete-event",
1163 G_CALLBACK (on_delete_event_quit), NULL);
1165 drawing_area = gtk_drawing_area_new ();
1167 gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
1169 g_signal_connect (drawing_area, "expose-event",
1170 G_CALLBACK (on_expose_event_draw),
1173 g_signal_connect (scherzo.window, "key-press-event",
1174 G_CALLBACK (on_key_press_event),
1177 g_signal_connect (scherzo.window, "key-release-event",
1178 G_CALLBACK (on_key_release_event),
1181 gtk_widget_show_all (scherzo.window);
1185 mnemon_save (&scherzo.mnemon);
1187 snd_midi_event_free (scherzo.snd_midi_event);
1189 talloc_free (scherzo.ctx);