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,
492 #define MAX_DEGREES 4
493 int relative_pitches[MAX_DEGREES];
494 int inversion, max_inversions;
497 assert (num_degrees <= MAX_DEGREES);
499 if (num_notes != num_degrees)
502 max_inversions = num_degrees;
504 /* We never spell simple intervals as inversions. */
505 if (num_degrees == 2)
508 for (inversion = 0; inversion < max_inversions; inversion++) {
509 for (i = 0; i < num_degrees; i++) {
510 /* The num_degrees is in the addition just to ensure all
511 * inputs to the modulus operator remain positive. */
512 int index = (i + num_degrees - inversion) % num_degrees;
514 /* Again, adding a 12 to keep things positive. */
515 relative_pitches[index] =
517 _modified_degree_to_half_steps (°rees[i]) -
518 _modified_degree_to_half_steps (°rees[inversion])) % 12;
522 for (i = 0; i < num_notes; i++)
523 if (notes[i].relative_pitch != relative_pitches[i])
526 *inversion_ret = inversion;
538 scherzo_analyze_chord (scherzo_t *scherzo)
540 void *local = talloc_new (NULL);
541 analyzed_note_t *notes;
542 note_group_t *note_group;
543 unsigned i, j, num_notes;
544 int bass_pitch, inversion;
545 const char *chord_name = NULL;
547 if (scherzo->pedal_pressed)
548 note_group = &scherzo->notes_pedaled;
550 note_group = &scherzo->notes_pressed;
552 num_notes = note_group->num_notes;
554 struct { modified_degree_t degrees[1]; const char *name; } octaves[] = {
555 { {{1, 0}}, "Octave"}
558 struct { modified_degree_t degrees[2]; const char *name; } intervals[] = {
559 { {{1, 0}, {2, -1}}, "Minor 2nd"},
560 { {{1, 0}, {2, 0}}, "Major 2nd"},
561 { {{1, 0}, {3, -1}}, "Minor 3rd"},
562 { {{1, 0}, {3, 0}}, "Major 3rd"},
563 { {{1, 0}, {4, 0}}, "Perfect 4th"},
564 { {{1, 0}, {5, -1}}, "Diminished 5th"},
565 { {{1, 0}, {5, 0}}, "Perfect 5th"},
566 { {{1, 0}, {6, -1}}, "Minor 6th"},
567 { {{1, 0}, {6, 0}}, "Major 6th"},
568 { {{1, 0}, {7, -1}}, "Minor 7th"},
569 { {{1, 0}, {7, 0}}, "Major 7th"}
572 struct { modified_degree_t degrees[3]; const char *name; } triads[] = {
573 { {{1, 0}, {3, 0}, {5, +1}}, "Augmented triad" },
574 { {{1, 0}, {3, 0}, {5, 0}}, "Major triad" },
575 { {{1, 0}, {3, -1}, {5, 0}}, "Minor triad" },
576 { {{1, 0}, {3, -1}, {5, -1}}, "Diminished triad" }
579 struct { modified_degree_t degrees[4]; const char *name; } sevenths[] = {
580 { {{1, 0}, {3, 0}, {5, +1}, {7, 0}}, "Augmented/major 7" },
581 { {{1, 0}, {3, 0}, {5, +1}, {7, -1}}, "Augmented 7" },
582 { {{1, 0}, {3, 0}, {5, 0}, {7, 0}}, "Major 7" },
583 { {{1, 0}, {3, 0}, {5, 0}, {7, -1}}, "Dominant 7" },
584 { {{1, 0}, {3, -1}, {5, 0}, {7, 0}}, "Minor/major 7" },
585 { {{1, 0}, {3, -1}, {5, 0}, {7, -1}}, "Minor 7" },
586 { {{1, 0}, {3, -1}, {5, -1}, {7, 0}}, "Diminished/major 7" },
587 { {{1, 0}, {3, -1}, {5, -1}, {7, -1}}, "Half-diminished 7" },
588 { {{1, 0}, {3, -1}, {5, -1}, {7, -2}}, "Diminished 7" }
591 if (scherzo->chord) {
592 score_remove_chord (scherzo->chord);
593 scherzo->chord = NULL;
599 notes = talloc_array (local, analyzed_note_t, num_notes);
603 for (i = 0; i < num_notes; i++) {
604 score_note_t *note = note_group->notes[i];
605 notes[i].note = note;
606 notes[i].midi_pitch = _score_pitch_and_octave_to_midi (note->pitch,
608 /* Relative pitch will be filled in after sorting. */
609 notes[i].relative_pitch = 0;
612 /* First, sort by midi pitch to find the bass note. */
613 qsort (notes, num_notes, sizeof (analyzed_note_t),
614 _compare_analyzed_note_by_midi_pitch);
616 bass_pitch = notes[0].midi_pitch;
618 /* With the bass note identified, we can find all relative pitches. */
619 for (i = 0; i < num_notes; i++) {
620 notes[i].relative_pitch = notes[i].midi_pitch - bass_pitch;
621 while (notes[i].relative_pitch >= 12)
622 notes[i].relative_pitch -= 12;
625 /* Now, sort again by relative pitch. */
626 qsort (notes, num_notes, sizeof (analyzed_note_t),
627 _compare_analyzed_note_by_relative_pitch);
629 /* Finally, eliminate all duplicate notes. */
630 for (i = 0; i < num_notes - 1; i++) {
631 if (notes[i+1].relative_pitch == notes[i].relative_pitch) {
633 while (j < num_notes &&
634 notes[j].relative_pitch == notes[i].relative_pitch)
638 /* The loop incremented j one past the last
639 * duplicate. Decrement so that it points to the
640 * last duplicate (and is guaranteed to not exceed
641 * the array bounds).*/
644 if (j < num_notes - 1) {
645 memmove (¬es[i+1], ¬es[j+1],
646 (num_notes - j) * sizeof (analyzed_note_t));
649 num_notes -= (j - i);
655 for (i = 0; i < ARRAY_SIZE (octaves); i++) {
656 if (_chord_signature_matches (notes, num_notes,
657 octaves[i].degrees, 1, &inversion))
659 chord_name = octaves[i].name;
665 for (i = 0; i < ARRAY_SIZE (intervals); i++) {
666 if (_chord_signature_matches (notes, num_notes,
667 intervals[i].degrees, 2, &inversion))
669 chord_name = intervals[i].name;
675 for (i = 0; i < ARRAY_SIZE (triads); i++) {
676 if (_chord_signature_matches (notes, num_notes,
677 triads[i].degrees, 3, &inversion))
679 chord_name = triads[i].name;
685 for (i = 0; i < ARRAY_SIZE(sevenths); i++) {
686 if (_chord_signature_matches (notes, num_notes,
687 sevenths[i].degrees, 4, &inversion))
689 chord_name = sevenths[i].name;
698 const char *inversion_str;
701 inversion_str = "1st inversion";
704 inversion_str = "2nd inversion";
707 inversion_str = "3rd inversion";
710 fprintf (stderr, "Internal error: Unexpected inversion: %d\n",
714 chord_name = talloc_asprintf (local, "%s %s",
715 chord_name, inversion_str);
717 chord_name = talloc_strdup (local, chord_name);
720 chord_name = talloc_strdup (local, "Unknown chord");
723 scherzo->chord = score_add_chord (scherzo->treble, chord_name);
730 note_group_init (void *ctx, note_group_t *group)
735 group->num_notes = 0;
739 note_group_add_note (note_group_t *group, score_note_t *note)
743 /* Do nothing if note is already in group. */
744 for (i = 0; i < group->num_notes; i++) {
745 if (group->notes[i]->pitch == note->pitch &&
746 group->notes[i]->octave == note->octave)
754 if (group->num_notes > group->size) {
756 group->notes = talloc_realloc (group->ctx, group->notes,
757 score_note_t*, group->size);
759 if (group->notes == NULL) {
760 fprintf (stderr, "Out of memory.\n");
765 group->notes[group->num_notes - 1] = note;
769 note_group_remove_note_at (note_group_t *group, int i)
771 if (i >= group->num_notes) {
772 fprintf (stderr, "Internal error: No note to remove at index %d\n", i);
776 if (i < group->num_notes - 1) {
777 memmove (group->notes + i, group->notes + i + 1,
778 (group->num_notes - 1 - i) * sizeof (score_note_t*));
783 static score_note_t *
784 scherzo_press_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
786 score_staff_t *staff;
790 if (scherzo->challenge.note) {
791 staff = scherzo->challenge.staff;
792 } else if (octave >= 4) {
793 staff = scherzo->treble;
795 staff = scherzo->bass;
798 /* Do nothing if this note is already pressed. */
799 for (i = 0; i < scherzo->notes_pressed.num_notes; i++) {
800 if (scherzo->notes_pressed.notes[i]->pitch == pitch &&
801 scherzo->notes_pressed.notes[i]->octave == octave)
803 return scherzo->notes_pressed.notes[i];
807 note = score_add_note (staff, pitch, octave, SCORE_DURATION_WHOLE);
809 note_group_add_note (&scherzo->notes_pressed, note);
811 if (scherzo->pedal_pressed)
812 note_group_add_note (&scherzo->notes_pedaled, note);
814 scherzo_analyze_chord (scherzo);
820 scherzo_release_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
826 for (i = scherzo->notes_pressed.num_notes - 1; i >=0; i--) {
827 note = scherzo->notes_pressed.notes[i];
828 if (note->pitch == pitch && note->octave == octave) {
830 if (! scherzo->pedal_pressed)
831 score_remove_note (note);
832 note_group_remove_note_at (&scherzo->notes_pressed, i);
837 fprintf (stderr, "Internal error: Failed to find note to release.\n");
840 scherzo_analyze_chord (scherzo);
843 static score_note_t *
844 scherzo_press_note_midi (scherzo_t *scherzo, unsigned char midi_note)
849 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
851 return scherzo_press_note (scherzo, pitch, octave);
855 scherzo_release_note_midi (scherzo_t *scherzo, unsigned char midi_note)
860 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
862 scherzo_release_note (scherzo, pitch, octave);
866 scherzo_press_pedal (scherzo_t *scherzo)
870 scherzo->pedal_pressed = 1;
872 /* Copy all pressed notes to pedaled notes */
873 for (i = 0; i < scherzo->notes_pressed.num_notes; i++)
874 note_group_add_note (&scherzo->notes_pedaled, scherzo->notes_pressed.notes[i]);
878 scherzo_release_pedal (scherzo_t *scherzo)
880 score_note_t *note, *new_note;
883 /* Make new notes in score for all pressed notes. */
884 for (i = 0; i < scherzo->notes_pressed.num_notes; i++) {
885 note = scherzo->notes_pressed.notes[i];
886 new_note = score_add_note (note->staff, note->pitch, note->octave, note->duration);
887 scherzo->notes_pressed.notes[i] = new_note;
890 /* Then remove all previously pedaled notes from the score. */
891 for (i = scherzo->notes_pedaled.num_notes - 1; i >=0; i--) {
892 note = scherzo->notes_pedaled.notes[i];
893 score_remove_note (note);
894 note_group_remove_note_at (&scherzo->notes_pedaled, i);
897 scherzo->pedal_pressed = 0;
899 scherzo_analyze_chord (scherzo);
901 gtk_widget_queue_draw (scherzo->window);
905 _select_challenge (scherzo_t *scherzo)
907 category_t *category_unused;
908 bool_t introduced_unused;
910 challenge_t *challenge = &scherzo->challenge;
915 if (challenge->note) {
916 score_remove_note (challenge->note);
917 challenge->note = NULL;
920 mnemon_select_item (&scherzo->mnemon,
922 &challenge->item_index,
926 item = challenge->bin->items[challenge->item_index];
929 if (strncmp (s, "treble:", 7) == 0) {
931 challenge->staff = scherzo->treble;
932 } else if (strncmp (s, "bass:", 5) == 0) {
934 challenge->staff = scherzo->bass;
937 "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
944 pitch = SCORE_PITCH_C;
947 pitch = SCORE_PITCH_D;
950 pitch = SCORE_PITCH_E;
953 pitch = SCORE_PITCH_F;
956 pitch = SCORE_PITCH_G;
959 pitch = SCORE_PITCH_A;
962 pitch = SCORE_PITCH_B;
965 fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
970 if (*s < '0' || *s > '9') {
971 fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
977 challenge->note = score_add_note (challenge->staff, pitch, octave,
978 SCORE_DURATION_WHOLE);
979 challenge->satisfied = 0;
980 challenge->mistaken = 0;
983 /* Determine whether the user hit the correct note. */
985 _judge_note (scherzo_t *scherzo, score_note_t *note)
987 challenge_t *challenge = &scherzo->challenge;
989 if (! scherzo->challenge.note) {
990 score_set_note_color_rgb (note, 0.0, 0.0, 0.0); /* black */
994 if (note->pitch == challenge->note->pitch &&
995 note->octave == challenge->note->octave)
997 challenge->satisfied = 1;
998 score_set_note_color_rgb (note, 18/256., 130/256., 28/256.); /* green */
1002 challenge->mistaken = 1;
1003 score_set_note_color_rgb (note, 184/256., 4/256., 22/256.); /* red */
1007 /* If the user got the right note (eventually), then score it in
1008 * mnemon and show the next note. */
1010 _score_challenge (scherzo_t *scherzo)
1012 challenge_t *challenge = &scherzo->challenge;
1014 if (! challenge->note)
1017 if (! challenge->satisfied)
1020 mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
1021 ! challenge->mistaken);
1023 _select_challenge (scherzo);
1027 on_midi_input (unused (GIOChannel *channel),
1028 unused (GIOCondition condition),
1031 unsigned char buf[MIDI_BUF_SIZE], *next;
1032 scherzo_t *scherzo = user_data;
1034 snd_seq_event_t event;
1036 int need_redraw = FALSE;
1038 remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
1044 consumed = snd_midi_event_encode (scherzo->snd_midi_event,
1045 next, remaining, &event);
1047 remaining -= consumed;
1050 switch (event.type) {
1051 case SND_SEQ_EVENT_NONE:
1052 /* Incomplete event. Nothing to do. */
1054 case SND_SEQ_EVENT_NOTEON:
1055 note = scherzo_press_note_midi (scherzo, event.data.note.note);
1056 _judge_note (scherzo, note);
1059 case SND_SEQ_EVENT_NOTEOFF:
1060 scherzo_release_note_midi (scherzo, event.data.note.note);
1061 _score_challenge (scherzo);
1064 case SND_SEQ_EVENT_CLOCK:
1065 /* Ignore for now as my piano sends a constant stream of these. */
1067 case SND_SEQ_EVENT_SENSING:
1068 /* Ignore for now as my piano sends a constant stream of these. */
1070 case SND_SEQ_EVENT_CONTROLLER:
1071 /* XXX: My piano gives me 64 for the sustain pedal, is
1072 * that universal? */
1073 if (event.data.control.param == 64) {
1074 if (event.data.control.value == 0)
1075 scherzo_release_pedal (scherzo);
1077 scherzo_press_pedal (scherzo);
1079 fprintf (stderr, "Fixme: Unhandled MIDI Control event (param=%d, value=%d)\n",
1080 event.data.control.param, event.data.control.value);
1084 fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
1091 gtk_widget_queue_draw (scherzo->window);
1093 /* Return TRUE to continue to get called in the future. */
1098 main (int argc, char *argv[])
1100 GtkWidget *drawing_area;
1104 srand (time (NULL));
1106 gtk_init (&argc, &argv);
1108 scherzo.ctx = talloc_new (NULL);
1110 scherzo.score = score_create (scherzo.ctx);
1111 scherzo.staff_height = 100;
1112 score_set_staff_height (scherzo.score, scherzo.staff_height);
1114 score_add_brace (scherzo.score, 2);
1115 scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
1116 scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
1118 scherzo.chord = NULL;
1120 /* Default to octave 4 and natural for computer keyboard keypresses. */
1121 scherzo.keyboard_octave = 4;
1122 scherzo.keyboard_accidental = SCORE_PITCH_ACCIDENTAL_NATURAL;
1124 note_group_init (scherzo.ctx, &scherzo.notes_pressed);
1125 note_group_init (scherzo.ctx, &scherzo.notes_pedaled);
1127 scherzo.pedal_pressed = 0;
1129 mnemon_init (&scherzo.mnemon);
1130 /* XXX: Should create a default file if one cannot be loaded. */
1131 mnemon_load_category (&scherzo.mnemon, "scherzo-notes");
1133 scherzo.challenge.note = NULL;
1135 _select_challenge (&scherzo);
1138 err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
1140 fprintf (stderr, "Out of memory.\n");
1144 #define MIDI_DEVICE "/dev/midi1"
1145 scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
1146 if (scherzo.midi_fd < 0) {
1147 printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
1149 GIOChannel *channel;
1151 channel = g_io_channel_unix_new (scherzo.midi_fd);
1152 g_io_channel_set_encoding (channel, NULL, NULL);
1153 g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
1156 scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1158 gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 1000, 600);
1160 g_signal_connect (scherzo.window, "delete-event",
1161 G_CALLBACK (on_delete_event_quit), NULL);
1163 drawing_area = gtk_drawing_area_new ();
1165 gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
1167 g_signal_connect (drawing_area, "expose-event",
1168 G_CALLBACK (on_expose_event_draw),
1171 g_signal_connect (scherzo.window, "key-press-event",
1172 G_CALLBACK (on_key_press_event),
1175 g_signal_connect (scherzo.window, "key-release-event",
1176 G_CALLBACK (on_key_release_event),
1179 gtk_widget_show_all (scherzo.window);
1183 mnemon_save (&scherzo.mnemon);
1185 snd_midi_event_free (scherzo.snd_midi_event);
1187 talloc_free (scherzo.ctx);