#include <asoundlib.h>
#include "score.h"
+#include "mnemon.h"
#define unused(foo) foo __attribute__((unused))
#define MIDI_BUF_SIZE 4096
+typedef struct challenge
+{
+ bin_t *bin;
+ int item_index;
+ score_staff_t *staff;
+ score_note_t *note;
+
+ int satisfied;
+ int mistaken;
+} challenge_t;
+
typedef struct scherzo
{
+ void *ctx;
+
GtkWidget *window;
score_t *score;
int staff_height;
score_staff_t *bass;
int midi_fd;
snd_midi_event_t *snd_midi_event;
+
+ mnemon_t mnemon;
+ challenge_t challenge;
+
+ int num_notes_pressed;
+ score_note_t **notes_pressed;
} scherzo_t;
+/* Forward declarations. */
+static score_note_t *
+scherzo_add_note (scherzo_t *scherzo, score_pitch_t pitch, int octave);
+
+static void
+scherzo_remove_note (scherzo_t *scherzo, score_pitch_t pitch, int octave);
+
+static void
+_judge_note (scherzo_t *scherzo, score_note_t *note);
+
+static void
+_score_challenge (scherzo_t *scherzo);
+
static int
on_delete_event_quit (unused (GtkWidget *widget),
unused (GdkEvent *event),
cairo_paint (cr);
/* Add some padding on the sides and top */
- cairo_translate (cr, pad, (int) scherzo->staff_height / 2);
+ cairo_translate (cr, pad, scherzo->staff_height);
score_set_staff_height (score, scherzo->staff_height);
score_set_width (score, widget_width - 2 * pad);
void *user_data)
{
scherzo_t *scherzo = user_data;
+ int octave = scherzo->challenge.note->octave;
+ score_pitch_t pitch;
switch (key->keyval) {
case GDK_KEY_plus:
case GDK_KEY_Escape:
gtk_main_quit ();
return FALSE;
+ case GDK_KEY_c:
+ case GDK_KEY_C:
+ pitch = SCORE_PITCH_C;
+ break;
+ case GDK_KEY_d:
+ case GDK_KEY_D:
+ pitch = SCORE_PITCH_D;
+ break;
+ case GDK_KEY_e:
+ case GDK_KEY_E:
+ pitch = SCORE_PITCH_E;
+ break;
+ case GDK_KEY_f:
+ case GDK_KEY_F:
+ pitch = SCORE_PITCH_F;
+ break;
+ case GDK_KEY_g:
+ case GDK_KEY_G:
+ pitch = SCORE_PITCH_G;
+ break;
+ case GDK_KEY_a:
+ case GDK_KEY_A:
+ pitch = SCORE_PITCH_A;
+ break;
+ case GDK_KEY_b:
+ case GDK_KEY_B:
+ pitch = SCORE_PITCH_B;
+ break;
+ }
+
+ if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) ||
+ (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g))
+ {
+ score_note_t *note;
+
+ note = scherzo_add_note (scherzo, pitch, octave);
+ _judge_note (scherzo, note);
+ gtk_widget_queue_draw (scherzo->window);
+
+ return TRUE;
+ }
+
+
+ /* Allow an unhandled event to propagate to other handlers. */
+ return FALSE;
+}
+
+static int
+on_key_release_event (unused (GtkWidget *widget),
+ GdkEventKey *key,
+ void *user_data)
+{
+ scherzo_t *scherzo = user_data;
+ int octave = scherzo->challenge.note->octave;
+ score_pitch_t pitch;
+
+ switch (key->keyval) {
+ case GDK_KEY_c:
+ case GDK_KEY_C:
+ pitch = SCORE_PITCH_C;
+ break;
+ case GDK_KEY_d:
+ case GDK_KEY_D:
+ pitch = SCORE_PITCH_D;
+ break;
+ case GDK_KEY_e:
+ case GDK_KEY_E:
+ pitch = SCORE_PITCH_E;
+ break;
+ case GDK_KEY_f:
+ case GDK_KEY_F:
+ pitch = SCORE_PITCH_F;
+ break;
+ case GDK_KEY_g:
+ case GDK_KEY_G:
+ pitch = SCORE_PITCH_G;
+ break;
+ case GDK_KEY_a:
+ case GDK_KEY_A:
+ pitch = SCORE_PITCH_A;
+ break;
+ case GDK_KEY_b:
+ case GDK_KEY_B:
+ pitch = SCORE_PITCH_B;
+ break;
}
- /* Allow the event to propagate to other handlers. */
+ if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) ||
+ (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g))
+ {
+ scherzo_remove_note (scherzo, pitch, octave);
+ _score_challenge (scherzo);
+ gtk_widget_queue_draw (scherzo->window);
+
+ return TRUE;
+ }
+
+
+ /* Allow an unhandled event to propagate to other handlers. */
return FALSE;
}
}
}
+static score_note_t *
+scherzo_add_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
+{
+ score_staff_t *staff;
+ score_note_t *note;
+
+ staff = scherzo->challenge.staff;
+
+ 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 (scherzo_t *scherzo, score_pitch_t pitch, int octave)
+{
+ score_note_t *note;
+ int i;
+
+ 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--;
+ }
+ }
+}
+
+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;
- /* Anything at Middle C and above goes on the treble staff by default. */
- if (midi_note >= 60)
- staff = scherzo->treble;
- else
- staff = scherzo->bass;
-
_midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
- score_staff_add_note (staff, pitch, octave, SCORE_DURATION_WHOLE);
+ return scherzo_add_note (scherzo, pitch, octave);
}
+
static void
scherzo_remove_note_midi (scherzo_t *scherzo, unsigned char midi_note)
{
- score_staff_t *staff;
score_pitch_t pitch;
int octave;
- score_note_t *note;
+
+ _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
+
+ scherzo_remove_note (scherzo, pitch, octave);
+}
+
+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';
- /* Anything at Middle C and above goes on the treble staff by default. */
- if (midi_note >= 60)
- staff = scherzo->treble;
+ 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;
+ score_set_note_color_rgb (note, 18/256., 130/256., 28/256.); /* green */
+ }
else
- staff = scherzo->bass;
+ {
+ challenge->mistaken = 1;
+ score_set_note_color_rgb (note, 184/256., 4/256., 22/256.); /* red */
+ }
+}
- _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
+/* 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;
- note = score_staff_find_note (staff, pitch, octave, SCORE_DURATION_WHOLE);
- score_staff_remove_note (staff, note);
+ mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
+ ! challenge->mistaken);
+
+ _select_challenge (scherzo);
}
static int
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, 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:
- scherzo_add_note_midi (scherzo, event.data.note.note);
- gtk_widget_queue_draw (scherzo->window);
+ 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);
- gtk_widget_queue_draw (scherzo->window);
+ _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;
}
}
-
+
+ if (need_redraw)
+ gtk_widget_queue_draw (scherzo->window);
+
/* Return TRUE to continue to get called in the future. */
return TRUE;
}
scherzo_t scherzo;
int err;
+ srand (time (NULL));
+
gtk_init (&argc, &argv);
- scherzo.score = score_create (NULL);
- scherzo.staff_height = 48;
+ scherzo.ctx = talloc_new (NULL);
+
+ scherzo.score = score_create (scherzo.ctx);
+ scherzo.staff_height = 100;
score_set_staff_height (scherzo.score, scherzo.staff_height);
score_add_brace (scherzo.score, 2);
scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
+ scherzo.num_notes_pressed = 0;
+ scherzo.notes_pressed = NULL;
+
+ mnemon_init (&scherzo.mnemon);
+ /* XXX: Should create a default file if one cannot be loaded. */
+ mnemon_load_category (&scherzo.mnemon, "scherzo-notes");
+
+ scherzo.challenge.note = NULL;
+ _select_challenge (&scherzo);
+
err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
if (err) {
fprintf (stderr, "Out of memory.\n");
scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
- gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 600, 400);
+ gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 1000, 600);
g_signal_connect (scherzo.window, "delete-event",
G_CALLBACK (on_delete_event_quit), NULL);
G_CALLBACK (on_expose_event_draw),
&scherzo);
- g_signal_connect (scherzo.window, "key-press-event",
+ g_signal_connect (scherzo.window, "key-press-event",
G_CALLBACK (on_key_press_event),
&scherzo);
+
+ g_signal_connect (scherzo.window, "key-release-event",
+ G_CALLBACK (on_key_release_event),
+ &scherzo);
gtk_widget_show_all (scherzo.window);
gtk_main ();
+ mnemon_save (&scherzo.mnemon);
+
snd_midi_event_free (scherzo.snd_midi_event);
- talloc_free (scherzo.score);
+
+ talloc_free (scherzo.ctx);
return 0;
}