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 unused(foo) foo __attribute__((unused))
29 #define MIDI_BUF_SIZE 4096
31 typedef struct challenge
42 typedef struct scherzo
49 score_staff_t *treble;
52 snd_midi_event_t *snd_midi_event;
55 challenge_t challenge;
57 int num_notes_pressed;
58 score_note_t **notes_pressed;
62 on_delete_event_quit (unused (GtkWidget *widget),
63 unused (GdkEvent *event),
64 unused (gpointer user_data))
68 /* Returning FALSE allows the default handler for delete-event
69 * to proceed to cleanup the widget. */
74 on_expose_event_draw (GtkWidget *widget,
75 unused (GdkEventExpose *expose),
78 scherzo_t *scherzo = user_data;
79 score_t *score = scherzo->score;
81 GtkAllocation allocation;
82 static const int pad = 10;
85 gtk_widget_get_allocation (widget, &allocation);
86 widget_width = allocation.width;
88 cr = gdk_cairo_create (widget->window);
90 /* White background */
91 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
94 /* Add some padding on the sides and top */
95 cairo_translate (cr, pad, scherzo->staff_height);
96 score_set_staff_height (score, scherzo->staff_height);
97 score_set_width (score, widget_width - 2 * pad);
99 score_draw (score, cr);
105 on_key_press_event (GtkWidget *widget,
109 scherzo_t *scherzo = user_data;
111 switch (key->keyval) {
115 case GDK_KEY_KP_Equal:
116 scherzo->staff_height += 4;
117 gtk_widget_queue_draw (widget);
121 case GDK_KEY_KP_Subtract:
122 scherzo->staff_height -= 4;
123 gtk_widget_queue_draw (widget);
133 /* Allow the event to propagate to other handlers. */
138 _midi_to_score_pitch_and_octave (unsigned char midi_note,
139 score_pitch_t *pitch,
142 *octave = midi_note / 12 - 1;
144 switch (midi_note % 12)
147 *pitch = SCORE_PITCH_C;
150 *pitch = SCORE_PITCH_Cs;
153 *pitch = SCORE_PITCH_D;
156 *pitch = SCORE_PITCH_Ds;
159 *pitch = SCORE_PITCH_E;
162 *pitch = SCORE_PITCH_F;
165 *pitch = SCORE_PITCH_Fs;
168 *pitch = SCORE_PITCH_G;
171 *pitch = SCORE_PITCH_Gs;
174 *pitch = SCORE_PITCH_A;
177 *pitch = SCORE_PITCH_As;
180 *pitch = SCORE_PITCH_B;
185 static score_note_t *
186 scherzo_add_note_midi (scherzo_t *scherzo, unsigned char midi_note)
188 score_staff_t *staff;
193 staff = scherzo->challenge.staff;
195 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
197 note = score_add_note (staff, pitch, octave, SCORE_DURATION_WHOLE);
199 scherzo->num_notes_pressed++;
200 scherzo->notes_pressed = talloc_realloc (scherzo->ctx,
201 scherzo->notes_pressed,
203 scherzo->num_notes_pressed);
204 if (scherzo->notes_pressed == NULL) {
205 fprintf (stderr, "Out of memory.\n");
209 scherzo->notes_pressed[scherzo->num_notes_pressed - 1] = note;
215 scherzo_remove_note_midi (scherzo_t *scherzo, unsigned char midi_note)
222 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
224 for (i = 0; i < scherzo->num_notes_pressed; i++) {
225 note = scherzo->notes_pressed[i];
226 if (note->pitch == pitch && note->octave == octave) {
227 score_remove_note (note);
228 if (i < scherzo->num_notes_pressed - 1) {
229 memmove (scherzo->notes_pressed + i,
230 scherzo->notes_pressed + i + 1,
231 (scherzo->num_notes_pressed - 1 - i) * sizeof (score_note_t*));
233 scherzo->num_notes_pressed--;
240 _select_challenge (scherzo_t *scherzo)
242 category_t *category_unused;
243 bool_t introduced_unused;
245 challenge_t *challenge = &scherzo->challenge;
250 if (challenge->note) {
251 score_remove_note (challenge->note);
252 challenge->note = NULL;
255 mnemon_select_item (&scherzo->mnemon,
257 &challenge->item_index,
261 item = challenge->bin->items[challenge->item_index];
264 if (strncmp (s, "treble:", 7) == 0) {
266 challenge->staff = scherzo->treble;
267 } else if (strncmp (s, "bass:", 5) == 0) {
269 challenge->staff = scherzo->bass;
272 "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
279 pitch = SCORE_PITCH_VALUE(C, NATURAL);
282 pitch = SCORE_PITCH_VALUE(D, NATURAL);
285 pitch = SCORE_PITCH_VALUE(E, NATURAL);
288 pitch = SCORE_PITCH_VALUE(F, NATURAL);
291 pitch = SCORE_PITCH_VALUE(G, NATURAL);
294 pitch = SCORE_PITCH_VALUE(A, NATURAL);
297 pitch = SCORE_PITCH_VALUE(B, NATURAL);
300 fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
305 if (*s < '0' || *s > '9') {
306 fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
312 challenge->note = score_add_note (challenge->staff, pitch, octave,
313 SCORE_DURATION_WHOLE);
314 challenge->satisfied = 0;
315 challenge->mistaken = 0;
318 /* Determine whether the user hit the correct note. */
320 _judge_note (scherzo_t *scherzo, score_note_t *note)
322 challenge_t *challenge = &scherzo->challenge;
324 if (note->pitch == challenge->note->pitch &&
325 note->octave == challenge->note->octave)
327 challenge->satisfied = 1;
328 score_set_note_color_rgb (note, 18/256., 130/256., 28/256.); /* green */
332 challenge->mistaken = 1;
333 score_set_note_color_rgb (note, 184/256., 4/256., 22/256.); /* red */
337 /* If the user got the right note (eventually), then score it in
338 * mnemon and show the next note. */
340 _score_challenge (scherzo_t *scherzo)
342 challenge_t *challenge = &scherzo->challenge;
344 if (! challenge->satisfied)
347 mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
348 ! challenge->mistaken);
350 _select_challenge (scherzo);
354 on_midi_input (unused (GIOChannel *channel),
355 unused (GIOCondition condition),
358 unsigned char buf[MIDI_BUF_SIZE], *next;
359 scherzo_t *scherzo = user_data;
361 snd_seq_event_t event;
363 int need_redraw = FALSE;
365 remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
371 consumed = snd_midi_event_encode (scherzo->snd_midi_event,
372 next, remaining, &event);
374 remaining -= consumed;
377 switch (event.type) {
378 case SND_SEQ_EVENT_NONE:
379 /* Incomplete event. Nothing to do. */
381 case SND_SEQ_EVENT_NOTEON:
382 note = scherzo_add_note_midi (scherzo, event.data.note.note);
383 _judge_note (scherzo, note);
386 case SND_SEQ_EVENT_NOTEOFF:
387 scherzo_remove_note_midi (scherzo, event.data.note.note);
388 _score_challenge (scherzo);
391 case SND_SEQ_EVENT_CLOCK:
392 /* Ignore for now as my piano sends a constant stream of these. */
394 case SND_SEQ_EVENT_SENSING:
395 /* Ignore for now as my piano sends a constant stream of these. */
398 fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
405 gtk_widget_queue_draw (scherzo->window);
407 /* Return TRUE to continue to get called in the future. */
412 main (int argc, char *argv[])
414 GtkWidget *drawing_area;
420 gtk_init (&argc, &argv);
422 scherzo.ctx = talloc_new (NULL);
424 scherzo.score = score_create (scherzo.ctx);
425 scherzo.staff_height = 100;
426 score_set_staff_height (scherzo.score, scherzo.staff_height);
428 score_add_brace (scherzo.score, 2);
429 scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
430 scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
432 scherzo.num_notes_pressed = 0;
433 scherzo.notes_pressed = NULL;
435 mnemon_init (&scherzo.mnemon);
436 /* XXX: Should create a default file if one cannot be loaded. */
437 mnemon_load_category (&scherzo.mnemon, "scherzo");
439 scherzo.challenge.note = NULL;
440 _select_challenge (&scherzo);
442 err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
444 fprintf (stderr, "Out of memory.\n");
448 #define MIDI_DEVICE "/dev/midi1"
449 scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
450 if (scherzo.midi_fd < 0) {
451 printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
455 channel = g_io_channel_unix_new (scherzo.midi_fd);
456 g_io_channel_set_encoding (channel, NULL, NULL);
457 g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
460 scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
462 gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 1000, 600);
464 g_signal_connect (scherzo.window, "delete-event",
465 G_CALLBACK (on_delete_event_quit), NULL);
467 drawing_area = gtk_drawing_area_new ();
469 gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
471 g_signal_connect (drawing_area, "expose-event",
472 G_CALLBACK (on_expose_event_draw),
475 g_signal_connect (scherzo.window, "key-press-event",
476 G_CALLBACK (on_key_press_event),
479 gtk_widget_show_all (scherzo.window);
483 mnemon_save (&scherzo.mnemon);
485 snd_midi_event_free (scherzo.snd_midi_event);
487 talloc_free (scherzo.ctx);