+static void
+_midi_to_score_pitch_and_octave (unsigned char midi_note,
+ score_pitch_t *pitch,
+ int *octave)
+{
+ *octave = midi_note / 12 - 1;
+
+ switch (midi_note % 12)
+ {
+ case 0:
+ *pitch = SCORE_PITCH_C;
+ break;
+ case 1:
+ *pitch = SCORE_PITCH_Cs;
+ break;
+ case 2:
+ *pitch = SCORE_PITCH_D;
+ break;
+ case 3:
+ *pitch = SCORE_PITCH_Ds;
+ break;
+ case 4:
+ *pitch = SCORE_PITCH_E;
+ break;
+ case 5:
+ *pitch = SCORE_PITCH_F;
+ break;
+ case 6:
+ *pitch = SCORE_PITCH_Fs;
+ break;
+ case 7:
+ *pitch = SCORE_PITCH_G;
+ break;
+ case 8:
+ *pitch = SCORE_PITCH_Gs;
+ break;
+ case 9:
+ *pitch = SCORE_PITCH_A;
+ break;
+ case 10:
+ *pitch = SCORE_PITCH_As;
+ break;
+ case 11:
+ *pitch = SCORE_PITCH_B;
+ break;
+ }
+}
+
+static score_note_t *
+scherzo_add_note_midi (scherzo_t *scherzo, unsigned char midi_note)
+{
+ score_staff_t *staff;
+ score_pitch_t pitch;
+ int octave;
+ score_note_t *note;
+
+ staff = scherzo->challenge.staff;
+
+ _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
+
+ note = score_add_note (staff, pitch, octave, SCORE_DURATION_WHOLE);
+
+ scherzo->num_notes_pressed++;
+ scherzo->notes_pressed = talloc_realloc (scherzo->ctx,
+ scherzo->notes_pressed,
+ score_note_t*,
+ scherzo->num_notes_pressed);
+ if (scherzo->notes_pressed == NULL) {
+ fprintf (stderr, "Out of memory.\n");
+ exit (1);
+ }
+
+ scherzo->notes_pressed[scherzo->num_notes_pressed - 1] = note;
+
+ return note;
+}
+
+static void
+scherzo_remove_note_midi (scherzo_t *scherzo, unsigned char midi_note)
+{
+ score_pitch_t pitch;
+ int octave;
+ score_note_t *note;
+ int i;
+
+ _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
+
+ for (i = 0; i < scherzo->num_notes_pressed; i++) {
+ note = scherzo->notes_pressed[i];
+ if (note->pitch == pitch && note->octave == octave) {
+ score_remove_note (note);
+ if (i < scherzo->num_notes_pressed - 1) {
+ memmove (scherzo->notes_pressed + i,
+ scherzo->notes_pressed + i + 1,
+ (scherzo->num_notes_pressed - 1 - i) * sizeof (score_note_t*));
+ }
+ scherzo->num_notes_pressed--;
+ i--;
+ }
+ }
+}
+
+void
+_select_challenge (scherzo_t *scherzo)
+{
+ category_t *category_unused;
+ bool_t introduced_unused;
+ item_t *item;
+ challenge_t *challenge = &scherzo->challenge;
+ score_pitch_t pitch;
+ int octave;
+ char *s;
+
+ if (challenge->note) {
+ score_remove_note (challenge->note);
+ challenge->note = NULL;
+ }
+
+ mnemon_select_item (&scherzo->mnemon,
+ &challenge->bin,
+ &challenge->item_index,
+ &category_unused,
+ &introduced_unused);
+
+ item = challenge->bin->items[challenge->item_index];
+
+ s = item->challenge;
+ if (strncmp (s, "treble:", 7) == 0) {
+ s += 7;
+ challenge->staff = scherzo->treble;
+ } else if (strncmp (s, "bass:", 5) == 0) {
+ s += 5;
+ challenge->staff = scherzo->bass;
+ } else {
+ fprintf (stderr,
+ "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
+ s);
+ exit (1);
+ }
+
+ switch (*s) {
+ case 'C':
+ pitch = SCORE_PITCH_VALUE(C, NATURAL);
+ break;
+ case 'D':
+ pitch = SCORE_PITCH_VALUE(D, NATURAL);
+ break;
+ case 'E':
+ pitch = SCORE_PITCH_VALUE(E, NATURAL);
+ break;
+ case 'F':
+ pitch = SCORE_PITCH_VALUE(F, NATURAL);
+ break;
+ case 'G':
+ pitch = SCORE_PITCH_VALUE(G, NATURAL);
+ break;
+ case 'A':
+ pitch = SCORE_PITCH_VALUE(A, NATURAL);
+ break;
+ case 'B':
+ pitch = SCORE_PITCH_VALUE(B, NATURAL);
+ break;
+ default:
+ fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
+ exit (1);
+ }
+ s++;
+
+ if (*s < '0' || *s > '9') {
+ fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
+ exit (1);
+ }
+
+ octave = *s - '0';
+
+ challenge->note = score_add_note (challenge->staff, pitch, octave,
+ SCORE_DURATION_WHOLE);
+ challenge->satisfied = 0;
+ challenge->mistaken = 0;
+}
+
+/* Determine whether the user hit the correct note. */
+static void
+_judge_note (scherzo_t *scherzo, score_note_t *note)
+{
+ challenge_t *challenge = &scherzo->challenge;
+
+ if (note->pitch == challenge->note->pitch &&
+ note->octave == challenge->note->octave)
+ {
+ challenge->satisfied = 1;
+ }
+ else
+ {
+ challenge->mistaken = 1;
+ }
+}
+
+/* If the user got the right note (eventually), then score it in
+ * mnemon and show the next note. */
+static void
+_score_challenge (scherzo_t *scherzo)
+{
+ challenge_t *challenge = &scherzo->challenge;
+
+ if (! challenge->satisfied)
+ return;
+
+ mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
+ ! challenge->mistaken);
+
+ _select_challenge (scherzo);
+}
+
+static int
+on_midi_input (unused (GIOChannel *channel),
+ unused (GIOCondition condition),
+ void *user_data)
+{
+ unsigned char buf[MIDI_BUF_SIZE], *next;
+ scherzo_t *scherzo = user_data;
+ ssize_t remaining;
+ snd_seq_event_t event;
+ score_note_t *note;
+ int need_redraw = FALSE;
+
+ remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
+
+ next = buf;
+ while (remaining) {
+ long consumed;
+
+ consumed = snd_midi_event_encode (scherzo->snd_midi_event,
+ next, remaining, &event);
+
+ remaining -= consumed;
+ next += consumed;
+
+ switch (event.type) {
+ case SND_SEQ_EVENT_NONE:
+ /* Incomplete event. Nothing to do. */
+ break;
+ case SND_SEQ_EVENT_NOTEON:
+ note = scherzo_add_note_midi (scherzo, event.data.note.note);
+ _judge_note (scherzo, note);
+ need_redraw = TRUE;
+ break;
+ case SND_SEQ_EVENT_NOTEOFF:
+ scherzo_remove_note_midi (scherzo, event.data.note.note);
+ _score_challenge (scherzo);
+ need_redraw = TRUE;
+ break;
+ case SND_SEQ_EVENT_CLOCK:
+ /* Ignore for now as my piano sends a constant stream of these. */
+ break;
+ case SND_SEQ_EVENT_SENSING:
+ /* Ignore for now as my piano sends a constant stream of these. */
+ break;
+ default:
+ fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
+ event.type);
+ break;
+ }
+ }
+
+ if (need_redraw)
+ gtk_widget_queue_draw (scherzo->window);
+
+ /* Return TRUE to continue to get called in the future. */
+ return TRUE;
+}
+