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 scherzo
51 score_staff_t *treble;
55 snd_midi_event_t *snd_midi_event;
58 challenge_t challenge;
60 int num_notes_pressed;
61 score_note_t **notes_pressed;
64 /* Forward declarations. */
66 scherzo_add_note (scherzo_t *scherzo, score_pitch_t pitch, int octave);
69 scherzo_remove_note (scherzo_t *scherzo, score_pitch_t pitch, int octave);
72 _judge_note (scherzo_t *scherzo, score_note_t *note);
75 _score_challenge (scherzo_t *scherzo);
78 on_delete_event_quit (unused (GtkWidget *widget),
79 unused (GdkEvent *event),
80 unused (gpointer user_data))
84 /* Returning FALSE allows the default handler for delete-event
85 * to proceed to cleanup the widget. */
90 on_expose_event_draw (GtkWidget *widget,
91 unused (GdkEventExpose *expose),
94 scherzo_t *scherzo = user_data;
95 score_t *score = scherzo->score;
97 GtkAllocation allocation;
98 static const int pad = 10;
101 gtk_widget_get_allocation (widget, &allocation);
102 widget_width = allocation.width;
104 cr = gdk_cairo_create (widget->window);
106 /* White background */
107 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
110 /* Add some padding on the sides and top */
111 cairo_translate (cr, pad, scherzo->staff_height);
112 score_set_staff_height (score, scherzo->staff_height);
113 score_set_width (score, widget_width - 2 * pad);
115 score_draw (score, cr);
121 on_key_press_event (GtkWidget *widget,
125 scherzo_t *scherzo = user_data;
129 if (scherzo->challenge.note)
130 octave = scherzo->challenge.note->octave;
134 switch (key->keyval) {
138 case GDK_KEY_KP_Equal:
139 scherzo->staff_height += 4;
140 gtk_widget_queue_draw (widget);
144 case GDK_KEY_KP_Subtract:
145 scherzo->staff_height -= 4;
146 gtk_widget_queue_draw (widget);
156 pitch = SCORE_PITCH_C;
160 pitch = SCORE_PITCH_D;
164 pitch = SCORE_PITCH_E;
168 pitch = SCORE_PITCH_F;
172 pitch = SCORE_PITCH_G;
176 pitch = SCORE_PITCH_A;
180 pitch = SCORE_PITCH_B;
184 if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) ||
185 (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g))
189 note = scherzo_add_note (scherzo, pitch, octave);
190 _judge_note (scherzo, note);
191 gtk_widget_queue_draw (scherzo->window);
197 /* Allow an unhandled event to propagate to other handlers. */
202 on_key_release_event (unused (GtkWidget *widget),
206 scherzo_t *scherzo = user_data;
210 if (scherzo->challenge.note)
211 octave = scherzo->challenge.note->octave;
215 switch (key->keyval) {
218 pitch = SCORE_PITCH_C;
222 pitch = SCORE_PITCH_D;
226 pitch = SCORE_PITCH_E;
230 pitch = SCORE_PITCH_F;
234 pitch = SCORE_PITCH_G;
238 pitch = SCORE_PITCH_A;
242 pitch = SCORE_PITCH_B;
246 if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) ||
247 (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g))
249 scherzo_remove_note (scherzo, pitch, octave);
250 _score_challenge (scherzo);
251 gtk_widget_queue_draw (scherzo->window);
257 /* Allow an unhandled event to propagate to other handlers. */
262 _score_pitch_and_octave_to_midi (score_pitch_t pitch,
265 unsigned char midi_note = 12 * (octave + 1);
267 switch (SCORE_PITCH_NAME (pitch)) {
268 case SCORE_PITCH_NAME_C:
270 case SCORE_PITCH_NAME_D:
273 case SCORE_PITCH_NAME_E:
276 case SCORE_PITCH_NAME_F:
279 case SCORE_PITCH_NAME_G:
282 case SCORE_PITCH_NAME_A:
285 case SCORE_PITCH_NAME_B:
290 switch (SCORE_PITCH_ACCIDENTAL (pitch)) {
291 case SCORE_PITCH_ACCIDENTAL_DOUBLE_FLAT:
294 case SCORE_PITCH_ACCIDENTAL_FLAT:
297 case SCORE_PITCH_ACCIDENTAL_NATURAL:
299 case SCORE_PITCH_ACCIDENTAL_SHARP:
302 case SCORE_PITCH_ACCIDENTAL_DOUBLE_SHARP:
311 _midi_to_score_pitch_and_octave (unsigned char midi_note,
312 score_pitch_t *pitch,
315 *octave = midi_note / 12 - 1;
317 switch (midi_note % 12)
320 *pitch = SCORE_PITCH_C;
323 *pitch = SCORE_PITCH_Cs;
326 *pitch = SCORE_PITCH_D;
329 *pitch = SCORE_PITCH_Ds;
332 *pitch = SCORE_PITCH_E;
335 *pitch = SCORE_PITCH_F;
338 *pitch = SCORE_PITCH_Fs;
341 *pitch = SCORE_PITCH_G;
344 *pitch = SCORE_PITCH_Gs;
347 *pitch = SCORE_PITCH_A;
350 *pitch = SCORE_PITCH_As;
353 *pitch = SCORE_PITCH_B;
358 /* Determine a chord name (if any) from the current notes pressed */
360 typedef struct analyzed_note {
361 /* Original note being analzyed. */
364 /* Absolute pitch (expressed as midi number). */
367 /* Pitch relative to bass note. */
372 _compare_analyzed_note (const void *va, const void *vb)
374 const analyzed_note_t *a = va, *b = vb;
376 return a->midi_pitch - b->midi_pitch;
380 _chord_signature_matches (analyzed_note_t *notes,
382 int *signature_pitches,
383 int num_signature_pitches)
387 for (n = 0, s = 0; s < num_signature_pitches; s++) {
390 if (notes[n].relative_pitch != signature_pitches[s])
393 /* Skip repeated notes in chord being tested. */
394 while (n < num_notes &&
395 notes[n].relative_pitch == signature_pitches[s])
401 /* If there are fewer notes in the signature than in the chord,
402 * then there is no match. */
410 scherzo_analyze_chord (scherzo_t *scherzo)
412 void *local = talloc_new (NULL);
413 analyzed_note_t *notes;
414 unsigned i, num_notes = scherzo->num_notes_pressed;
416 const char *chord_name = NULL;
418 struct { int pitches[2]; const char *name; } intervals[] = {
419 { {0, 1}, "Minor 2nd"},
420 { {0, 2}, "Major 2nd"},
421 { {0, 3}, "Minor 3rd"},
422 { {0, 4}, "Major 3rd"},
423 { {0, 5}, "Perfect 4th"},
424 { {0, 6}, "Diminished 5th/Augmented 4th"},
425 { {0, 7}, "Perfect 5th"},
426 { {0, 8}, "Minor 6th"},
427 { {0, 9}, "Major 6th"},
428 { {0, 10}, "Minor 7th"},
429 { {0, 11}, "Major 7th"}
432 struct { int pitches[3]; const char *name; } triads[] = {
433 { {0, 4, 8}, "Augmented triad" },
434 { {0, 4, 7}, "Major triad" },
435 { {0, 3, 7}, "Minor triad" },
436 { {0, 3, 6}, "Diminished triad" }
439 struct { int pitches[4]; const char *name; } sevenths[] = {
440 { {0, 4, 8, 11}, "Augmented/major 7" },
441 { {0, 4, 7, 11}, "Major 7" },
442 { {0, 4, 7, 10}, "Dominant 7" },
443 { {0, 3, 7, 11}, "Minor/major 7" },
444 { {0, 3, 7, 10}, "Minor 7" },
445 { {0, 3, 6, 10}, "Half-diminished 7" },
446 { {0, 3, 6, 9}, "Diminished 7" },
447 { {0, 4, 8, 10}, "Augmented 7" },
448 { {0, 3, 6, 11}, "Diminished/major 7" },
451 if (scherzo->chord) {
452 score_remove_chord (scherzo->chord);
453 scherzo->chord = NULL;
459 notes = talloc_array (local, analyzed_note_t, num_notes);
463 for (i = 0; i < num_notes; i++) {
464 score_note_t *note = scherzo->notes_pressed[i];
465 notes[i].note = note;
466 notes[i].midi_pitch = _score_pitch_and_octave_to_midi (note->pitch,
468 /* Relative pitch will be filled in after sorting. */
469 notes[i].relative_pitch = 0;
472 qsort (notes, num_notes, sizeof (analyzed_note_t), _compare_analyzed_note);
474 bass_pitch = notes[0].midi_pitch;
476 for (i = 0; i < num_notes; i++) {
477 notes[i].relative_pitch = notes[i].midi_pitch - bass_pitch;
480 for (i = 0; i < ARRAY_SIZE (intervals); i++) {
481 if (_chord_signature_matches (notes, num_notes, intervals[i].pitches, 2))
482 chord_name = intervals[i].name;
485 for (i = 0; i < ARRAY_SIZE (triads); i++) {
486 if (_chord_signature_matches (notes, num_notes, triads[i].pitches, 3))
487 chord_name = triads[i].name;
490 for (i = 0; i < ARRAY_SIZE(sevenths); i++) {
491 if (_chord_signature_matches (notes, num_notes, sevenths[i].pitches, 4))
492 chord_name = sevenths[i].name;
496 scherzo->chord = score_add_chord (scherzo->treble, chord_name);
498 scherzo->chord = score_add_chord (scherzo->treble, "Unknown or not a chord");
504 static score_note_t *
505 scherzo_add_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
507 score_staff_t *staff;
511 if (scherzo->challenge.note)
512 staff = scherzo->challenge.staff;
514 staff = scherzo->treble;
516 /* Do nothing if this note is already pressed. */
517 for (i = 0; i < scherzo->num_notes_pressed; i++) {
518 if (scherzo->notes_pressed[i]->pitch == pitch &&
519 scherzo->notes_pressed[i]->octave == octave)
521 return scherzo->notes_pressed[i];
525 note = score_add_note (staff, pitch, octave, SCORE_DURATION_WHOLE);
527 scherzo->num_notes_pressed++;
528 scherzo->notes_pressed = talloc_realloc (scherzo->ctx,
529 scherzo->notes_pressed,
531 scherzo->num_notes_pressed);
532 if (scherzo->notes_pressed == NULL) {
533 fprintf (stderr, "Out of memory.\n");
537 scherzo->notes_pressed[scherzo->num_notes_pressed - 1] = note;
539 scherzo_analyze_chord (scherzo);
546 scherzo_remove_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
551 for (i = 0; i < scherzo->num_notes_pressed; i++) {
552 note = scherzo->notes_pressed[i];
553 if (note->pitch == pitch && note->octave == octave) {
554 score_remove_note (note);
555 if (i < scherzo->num_notes_pressed - 1) {
556 memmove (scherzo->notes_pressed + i,
557 scherzo->notes_pressed + i + 1,
558 (scherzo->num_notes_pressed - 1 - i) * sizeof (score_note_t*));
560 scherzo->num_notes_pressed--;
565 scherzo_analyze_chord (scherzo);
568 static score_note_t *
569 scherzo_add_note_midi (scherzo_t *scherzo, unsigned char midi_note)
574 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
576 return scherzo_add_note (scherzo, pitch, octave);
581 scherzo_remove_note_midi (scherzo_t *scherzo, unsigned char midi_note)
586 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
588 scherzo_remove_note (scherzo, pitch, octave);
592 _select_challenge (scherzo_t *scherzo)
594 category_t *category_unused;
595 bool_t introduced_unused;
597 challenge_t *challenge = &scherzo->challenge;
602 if (challenge->note) {
603 score_remove_note (challenge->note);
604 challenge->note = NULL;
607 mnemon_select_item (&scherzo->mnemon,
609 &challenge->item_index,
613 item = challenge->bin->items[challenge->item_index];
616 if (strncmp (s, "treble:", 7) == 0) {
618 challenge->staff = scherzo->treble;
619 } else if (strncmp (s, "bass:", 5) == 0) {
621 challenge->staff = scherzo->bass;
624 "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
631 pitch = SCORE_PITCH_VALUE(C, NATURAL);
634 pitch = SCORE_PITCH_VALUE(D, NATURAL);
637 pitch = SCORE_PITCH_VALUE(E, NATURAL);
640 pitch = SCORE_PITCH_VALUE(F, NATURAL);
643 pitch = SCORE_PITCH_VALUE(G, NATURAL);
646 pitch = SCORE_PITCH_VALUE(A, NATURAL);
649 pitch = SCORE_PITCH_VALUE(B, NATURAL);
652 fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
657 if (*s < '0' || *s > '9') {
658 fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
664 challenge->note = score_add_note (challenge->staff, pitch, octave,
665 SCORE_DURATION_WHOLE);
666 challenge->satisfied = 0;
667 challenge->mistaken = 0;
670 /* Determine whether the user hit the correct note. */
672 _judge_note (scherzo_t *scherzo, score_note_t *note)
674 challenge_t *challenge = &scherzo->challenge;
676 if (! scherzo->challenge.note) {
677 score_set_note_color_rgb (note, 0.0, 0.0, 0.0); /* black */
681 if (note->pitch == challenge->note->pitch &&
682 note->octave == challenge->note->octave)
684 challenge->satisfied = 1;
685 score_set_note_color_rgb (note, 18/256., 130/256., 28/256.); /* green */
689 challenge->mistaken = 1;
690 score_set_note_color_rgb (note, 184/256., 4/256., 22/256.); /* red */
694 /* If the user got the right note (eventually), then score it in
695 * mnemon and show the next note. */
697 _score_challenge (scherzo_t *scherzo)
699 challenge_t *challenge = &scherzo->challenge;
701 if (! challenge->note)
704 if (! challenge->satisfied)
707 mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
708 ! challenge->mistaken);
710 _select_challenge (scherzo);
714 on_midi_input (unused (GIOChannel *channel),
715 unused (GIOCondition condition),
718 unsigned char buf[MIDI_BUF_SIZE], *next;
719 scherzo_t *scherzo = user_data;
721 snd_seq_event_t event;
723 int need_redraw = FALSE;
725 remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
731 consumed = snd_midi_event_encode (scherzo->snd_midi_event,
732 next, remaining, &event);
734 remaining -= consumed;
737 switch (event.type) {
738 case SND_SEQ_EVENT_NONE:
739 /* Incomplete event. Nothing to do. */
741 case SND_SEQ_EVENT_NOTEON:
742 note = scherzo_add_note_midi (scherzo, event.data.note.note);
743 _judge_note (scherzo, note);
746 case SND_SEQ_EVENT_NOTEOFF:
747 scherzo_remove_note_midi (scherzo, event.data.note.note);
748 _score_challenge (scherzo);
751 case SND_SEQ_EVENT_CLOCK:
752 /* Ignore for now as my piano sends a constant stream of these. */
754 case SND_SEQ_EVENT_SENSING:
755 /* Ignore for now as my piano sends a constant stream of these. */
758 fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
765 gtk_widget_queue_draw (scherzo->window);
767 /* Return TRUE to continue to get called in the future. */
772 main (int argc, char *argv[])
774 GtkWidget *drawing_area;
780 gtk_init (&argc, &argv);
782 scherzo.ctx = talloc_new (NULL);
784 scherzo.score = score_create (scherzo.ctx);
785 scherzo.staff_height = 100;
786 score_set_staff_height (scherzo.score, scherzo.staff_height);
788 score_add_brace (scherzo.score, 2);
789 scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
790 scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
792 scherzo.chord = NULL;
794 scherzo.num_notes_pressed = 0;
795 scherzo.notes_pressed = NULL;
797 mnemon_init (&scherzo.mnemon);
798 /* XXX: Should create a default file if one cannot be loaded. */
799 mnemon_load_category (&scherzo.mnemon, "scherzo-notes");
801 scherzo.challenge.note = NULL;
803 _select_challenge (&scherzo);
806 err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
808 fprintf (stderr, "Out of memory.\n");
812 #define MIDI_DEVICE "/dev/midi1"
813 scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
814 if (scherzo.midi_fd < 0) {
815 printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
819 channel = g_io_channel_unix_new (scherzo.midi_fd);
820 g_io_channel_set_encoding (channel, NULL, NULL);
821 g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
824 scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
826 gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 1000, 600);
828 g_signal_connect (scherzo.window, "delete-event",
829 G_CALLBACK (on_delete_event_quit), NULL);
831 drawing_area = gtk_drawing_area_new ();
833 gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
835 g_signal_connect (drawing_area, "expose-event",
836 G_CALLBACK (on_expose_event_draw),
839 g_signal_connect (scherzo.window, "key-press-event",
840 G_CALLBACK (on_key_press_event),
843 g_signal_connect (scherzo.window, "key-release-event",
844 G_CALLBACK (on_key_release_event),
847 gtk_widget_show_all (scherzo.window);
851 mnemon_save (&scherzo.mnemon);
853 snd_midi_event_free (scherzo.snd_midi_event);
855 talloc_free (scherzo.ctx);