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;
61 /* Forward declarations. */
63 scherzo_add_note (scherzo_t *scherzo, score_pitch_t pitch, int octave);
66 scherzo_remove_note (scherzo_t *scherzo, score_pitch_t pitch, int octave);
69 _judge_note (scherzo_t *scherzo, score_note_t *note);
72 _score_challenge (scherzo_t *scherzo);
75 on_delete_event_quit (unused (GtkWidget *widget),
76 unused (GdkEvent *event),
77 unused (gpointer user_data))
81 /* Returning FALSE allows the default handler for delete-event
82 * to proceed to cleanup the widget. */
87 on_expose_event_draw (GtkWidget *widget,
88 unused (GdkEventExpose *expose),
91 scherzo_t *scherzo = user_data;
92 score_t *score = scherzo->score;
94 GtkAllocation allocation;
95 static const int pad = 10;
98 gtk_widget_get_allocation (widget, &allocation);
99 widget_width = allocation.width;
101 cr = gdk_cairo_create (widget->window);
103 /* White background */
104 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
107 /* Add some padding on the sides and top */
108 cairo_translate (cr, pad, scherzo->staff_height);
109 score_set_staff_height (score, scherzo->staff_height);
110 score_set_width (score, widget_width - 2 * pad);
112 score_draw (score, cr);
118 on_key_press_event (GtkWidget *widget,
122 scherzo_t *scherzo = user_data;
123 int octave = scherzo->challenge.note->octave;
126 switch (key->keyval) {
130 case GDK_KEY_KP_Equal:
131 scherzo->staff_height += 4;
132 gtk_widget_queue_draw (widget);
136 case GDK_KEY_KP_Subtract:
137 scherzo->staff_height -= 4;
138 gtk_widget_queue_draw (widget);
148 pitch = SCORE_PITCH_C;
152 pitch = SCORE_PITCH_D;
156 pitch = SCORE_PITCH_E;
160 pitch = SCORE_PITCH_F;
164 pitch = SCORE_PITCH_G;
168 pitch = SCORE_PITCH_A;
172 pitch = SCORE_PITCH_B;
176 if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) ||
177 (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g))
181 note = scherzo_add_note (scherzo, pitch, octave);
182 _judge_note (scherzo, note);
183 gtk_widget_queue_draw (scherzo->window);
189 /* Allow an unhandled event to propagate to other handlers. */
194 on_key_release_event (unused (GtkWidget *widget),
198 scherzo_t *scherzo = user_data;
199 int octave = scherzo->challenge.note->octave;
202 switch (key->keyval) {
205 pitch = SCORE_PITCH_C;
209 pitch = SCORE_PITCH_D;
213 pitch = SCORE_PITCH_E;
217 pitch = SCORE_PITCH_F;
221 pitch = SCORE_PITCH_G;
225 pitch = SCORE_PITCH_A;
229 pitch = SCORE_PITCH_B;
233 if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) ||
234 (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g))
236 scherzo_remove_note (scherzo, pitch, octave);
237 _score_challenge (scherzo);
238 gtk_widget_queue_draw (scherzo->window);
244 /* Allow an unhandled event to propagate to other handlers. */
249 _midi_to_score_pitch_and_octave (unsigned char midi_note,
250 score_pitch_t *pitch,
253 *octave = midi_note / 12 - 1;
255 switch (midi_note % 12)
258 *pitch = SCORE_PITCH_C;
261 *pitch = SCORE_PITCH_Cs;
264 *pitch = SCORE_PITCH_D;
267 *pitch = SCORE_PITCH_Ds;
270 *pitch = SCORE_PITCH_E;
273 *pitch = SCORE_PITCH_F;
276 *pitch = SCORE_PITCH_Fs;
279 *pitch = SCORE_PITCH_G;
282 *pitch = SCORE_PITCH_Gs;
285 *pitch = SCORE_PITCH_A;
288 *pitch = SCORE_PITCH_As;
291 *pitch = SCORE_PITCH_B;
296 static score_note_t *
297 scherzo_add_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
299 score_staff_t *staff;
302 staff = scherzo->challenge.staff;
304 note = score_add_note (staff, pitch, octave, SCORE_DURATION_WHOLE);
306 scherzo->num_notes_pressed++;
307 scherzo->notes_pressed = talloc_realloc (scherzo->ctx,
308 scherzo->notes_pressed,
310 scherzo->num_notes_pressed);
311 if (scherzo->notes_pressed == NULL) {
312 fprintf (stderr, "Out of memory.\n");
316 scherzo->notes_pressed[scherzo->num_notes_pressed - 1] = note;
323 scherzo_remove_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
328 for (i = 0; i < scherzo->num_notes_pressed; i++) {
329 note = scherzo->notes_pressed[i];
330 if (note->pitch == pitch && note->octave == octave) {
331 score_remove_note (note);
332 if (i < scherzo->num_notes_pressed - 1) {
333 memmove (scherzo->notes_pressed + i,
334 scherzo->notes_pressed + i + 1,
335 (scherzo->num_notes_pressed - 1 - i) * sizeof (score_note_t*));
337 scherzo->num_notes_pressed--;
343 static score_note_t *
344 scherzo_add_note_midi (scherzo_t *scherzo, unsigned char midi_note)
349 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
351 return scherzo_add_note (scherzo, pitch, octave);
356 scherzo_remove_note_midi (scherzo_t *scherzo, unsigned char midi_note)
361 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
363 scherzo_remove_note (scherzo, pitch, octave);
367 _select_challenge (scherzo_t *scherzo)
369 category_t *category_unused;
370 bool_t introduced_unused;
372 challenge_t *challenge = &scherzo->challenge;
377 if (challenge->note) {
378 score_remove_note (challenge->note);
379 challenge->note = NULL;
382 mnemon_select_item (&scherzo->mnemon,
384 &challenge->item_index,
388 item = challenge->bin->items[challenge->item_index];
391 if (strncmp (s, "treble:", 7) == 0) {
393 challenge->staff = scherzo->treble;
394 } else if (strncmp (s, "bass:", 5) == 0) {
396 challenge->staff = scherzo->bass;
399 "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
406 pitch = SCORE_PITCH_VALUE(C, NATURAL);
409 pitch = SCORE_PITCH_VALUE(D, NATURAL);
412 pitch = SCORE_PITCH_VALUE(E, NATURAL);
415 pitch = SCORE_PITCH_VALUE(F, NATURAL);
418 pitch = SCORE_PITCH_VALUE(G, NATURAL);
421 pitch = SCORE_PITCH_VALUE(A, NATURAL);
424 pitch = SCORE_PITCH_VALUE(B, NATURAL);
427 fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
432 if (*s < '0' || *s > '9') {
433 fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
439 challenge->note = score_add_note (challenge->staff, pitch, octave,
440 SCORE_DURATION_WHOLE);
441 challenge->satisfied = 0;
442 challenge->mistaken = 0;
445 /* Determine whether the user hit the correct note. */
447 _judge_note (scherzo_t *scherzo, score_note_t *note)
449 challenge_t *challenge = &scherzo->challenge;
451 if (note->pitch == challenge->note->pitch &&
452 note->octave == challenge->note->octave)
454 challenge->satisfied = 1;
455 score_set_note_color_rgb (note, 18/256., 130/256., 28/256.); /* green */
459 challenge->mistaken = 1;
460 score_set_note_color_rgb (note, 184/256., 4/256., 22/256.); /* red */
464 /* If the user got the right note (eventually), then score it in
465 * mnemon and show the next note. */
467 _score_challenge (scherzo_t *scherzo)
469 challenge_t *challenge = &scherzo->challenge;
471 if (! challenge->satisfied)
474 mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
475 ! challenge->mistaken);
477 _select_challenge (scherzo);
481 on_midi_input (unused (GIOChannel *channel),
482 unused (GIOCondition condition),
485 unsigned char buf[MIDI_BUF_SIZE], *next;
486 scherzo_t *scherzo = user_data;
488 snd_seq_event_t event;
490 int need_redraw = FALSE;
492 remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
498 consumed = snd_midi_event_encode (scherzo->snd_midi_event,
499 next, remaining, &event);
501 remaining -= consumed;
504 switch (event.type) {
505 case SND_SEQ_EVENT_NONE:
506 /* Incomplete event. Nothing to do. */
508 case SND_SEQ_EVENT_NOTEON:
509 note = scherzo_add_note_midi (scherzo, event.data.note.note);
510 _judge_note (scherzo, note);
513 case SND_SEQ_EVENT_NOTEOFF:
514 scherzo_remove_note_midi (scherzo, event.data.note.note);
515 _score_challenge (scherzo);
518 case SND_SEQ_EVENT_CLOCK:
519 /* Ignore for now as my piano sends a constant stream of these. */
521 case SND_SEQ_EVENT_SENSING:
522 /* Ignore for now as my piano sends a constant stream of these. */
525 fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
532 gtk_widget_queue_draw (scherzo->window);
534 /* Return TRUE to continue to get called in the future. */
539 main (int argc, char *argv[])
541 GtkWidget *drawing_area;
547 gtk_init (&argc, &argv);
549 scherzo.ctx = talloc_new (NULL);
551 scherzo.score = score_create (scherzo.ctx);
552 scherzo.staff_height = 100;
553 score_set_staff_height (scherzo.score, scherzo.staff_height);
555 score_add_brace (scherzo.score, 2);
556 scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
557 scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
559 scherzo.num_notes_pressed = 0;
560 scherzo.notes_pressed = NULL;
562 mnemon_init (&scherzo.mnemon);
563 /* XXX: Should create a default file if one cannot be loaded. */
564 mnemon_load_category (&scherzo.mnemon, "scherzo-notes");
566 scherzo.challenge.note = NULL;
567 _select_challenge (&scherzo);
569 err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
571 fprintf (stderr, "Out of memory.\n");
575 #define MIDI_DEVICE "/dev/midi1"
576 scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
577 if (scherzo.midi_fd < 0) {
578 printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
582 channel = g_io_channel_unix_new (scherzo.midi_fd);
583 g_io_channel_set_encoding (channel, NULL, NULL);
584 g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
587 scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
589 gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 1000, 600);
591 g_signal_connect (scherzo.window, "delete-event",
592 G_CALLBACK (on_delete_event_quit), NULL);
594 drawing_area = gtk_drawing_area_new ();
596 gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
598 g_signal_connect (drawing_area, "expose-event",
599 G_CALLBACK (on_expose_event_draw),
602 g_signal_connect (scherzo.window, "key-press-event",
603 G_CALLBACK (on_key_press_event),
606 g_signal_connect (scherzo.window, "key-release-event",
607 G_CALLBACK (on_key_release_event),
610 gtk_widget_show_all (scherzo.window);
614 mnemon_save (&scherzo.mnemon);
616 snd_midi_event_free (scherzo.snd_midi_event);
618 talloc_free (scherzo.ctx);