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 note_group
52 typedef struct scherzo
59 score_staff_t *treble;
63 /* This is for a "computer keyboard". Any "piano keyboard" key
64 * knows its own octave. */
68 snd_midi_event_t *snd_midi_event;
71 challenge_t challenge;
73 note_group_t notes_pressed;
74 note_group_t notes_pedaled;
79 /* Forward declarations. */
81 scherzo_press_note (scherzo_t *scherzo, score_pitch_t pitch, int octave);
84 scherzo_release_note (scherzo_t *scherzo, score_pitch_t pitch, int octave);
87 scherzo_press_pedal (scherzo_t *scherzo);
90 scherzo_release_pedal (scherzo_t *scherzo);
93 _judge_note (scherzo_t *scherzo, score_note_t *note);
96 _score_challenge (scherzo_t *scherzo);
99 on_delete_event_quit (unused (GtkWidget *widget),
100 unused (GdkEvent *event),
101 unused (gpointer user_data))
105 /* Returning FALSE allows the default handler for delete-event
106 * to proceed to cleanup the widget. */
111 on_expose_event_draw (GtkWidget *widget,
112 unused (GdkEventExpose *expose),
115 scherzo_t *scherzo = user_data;
116 score_t *score = scherzo->score;
118 GtkAllocation allocation;
119 static const int pad = 10;
122 gtk_widget_get_allocation (widget, &allocation);
123 widget_width = allocation.width;
125 cr = gdk_cairo_create (widget->window);
127 /* White background */
128 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
131 /* Add some padding on the sides and top */
132 cairo_translate (cr, pad, scherzo->staff_height);
133 score_set_staff_height (score, scherzo->staff_height);
134 score_set_width (score, widget_width - 2 * pad);
136 score_draw (score, cr);
142 on_key_press_event (GtkWidget *widget,
146 scherzo_t *scherzo = user_data;
148 score_pitch_name_t pitch_name;
151 if (scherzo->challenge.note)
152 octave = scherzo->challenge.note->octave;
154 octave = scherzo->keyboard_octave;
156 switch (key->keyval) {
160 case GDK_KEY_KP_Equal:
161 scherzo->staff_height += 4;
162 gtk_widget_queue_draw (widget);
166 case GDK_KEY_KP_Subtract:
167 scherzo->staff_height -= 4;
168 gtk_widget_queue_draw (widget);
178 pitch_name = SCORE_PITCH_NAME_C;
182 pitch_name = SCORE_PITCH_NAME_D;
186 pitch_name = SCORE_PITCH_NAME_E;
190 pitch_name = SCORE_PITCH_NAME_F;
194 pitch_name = SCORE_PITCH_NAME_G;
198 pitch_name = SCORE_PITCH_NAME_A;
202 pitch_name = SCORE_PITCH_NAME_B;
213 scherzo->keyboard_octave = key->keyval - GDK_KEY_0;
216 scherzo_press_pedal (scherzo);
220 pitch = SCORE_PITCH (pitch_name, SCORE_PITCH_ACCIDENTAL_NATURAL);
222 if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) ||
223 (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g))
227 note = scherzo_press_note (scherzo, pitch, octave);
228 _judge_note (scherzo, note);
229 gtk_widget_queue_draw (scherzo->window);
235 /* Allow an unhandled event to propagate to other handlers. */
240 on_key_release_event (unused (GtkWidget *widget),
244 scherzo_t *scherzo = user_data;
246 score_pitch_name_t pitch_name;
249 if (scherzo->challenge.note)
250 octave = scherzo->challenge.note->octave;
252 octave = scherzo->keyboard_octave;
254 switch (key->keyval) {
257 pitch_name = SCORE_PITCH_NAME_C;
261 pitch_name = SCORE_PITCH_NAME_D;
265 pitch_name = SCORE_PITCH_NAME_E;
269 pitch_name = SCORE_PITCH_NAME_F;
273 pitch_name = SCORE_PITCH_NAME_G;
277 pitch_name = SCORE_PITCH_NAME_A;
281 pitch_name = SCORE_PITCH_NAME_B;
284 scherzo_release_pedal (scherzo);
288 pitch = SCORE_PITCH (pitch_name, SCORE_PITCH_ACCIDENTAL_NATURAL);
290 if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) ||
291 (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g))
293 scherzo_release_note (scherzo, pitch, octave);
294 _score_challenge (scherzo);
295 gtk_widget_queue_draw (scherzo->window);
301 /* Allow an unhandled event to propagate to other handlers. */
306 _score_pitch_and_octave_to_midi (score_pitch_t pitch,
309 unsigned char midi_note = 12 * (octave + 1);
311 switch (SCORE_PITCH_NAME (pitch)) {
312 case SCORE_PITCH_NAME_C:
314 case SCORE_PITCH_NAME_D:
317 case SCORE_PITCH_NAME_E:
320 case SCORE_PITCH_NAME_F:
323 case SCORE_PITCH_NAME_G:
326 case SCORE_PITCH_NAME_A:
329 case SCORE_PITCH_NAME_B:
334 switch (SCORE_PITCH_ACCIDENTAL (pitch)) {
335 case SCORE_PITCH_ACCIDENTAL_DOUBLE_FLAT:
338 case SCORE_PITCH_ACCIDENTAL_FLAT:
341 case SCORE_PITCH_ACCIDENTAL_NATURAL:
343 case SCORE_PITCH_ACCIDENTAL_SHARP:
346 case SCORE_PITCH_ACCIDENTAL_DOUBLE_SHARP:
355 _midi_to_score_pitch_and_octave (unsigned char midi_note,
356 score_pitch_t *pitch,
359 *octave = midi_note / 12 - 1;
361 switch (midi_note % 12)
364 *pitch = SCORE_PITCH_C;
367 *pitch = SCORE_PITCH_Cs;
370 *pitch = SCORE_PITCH_D;
373 *pitch = SCORE_PITCH_Ds;
376 *pitch = SCORE_PITCH_E;
379 *pitch = SCORE_PITCH_F;
382 *pitch = SCORE_PITCH_Fs;
385 *pitch = SCORE_PITCH_G;
388 *pitch = SCORE_PITCH_Gs;
391 *pitch = SCORE_PITCH_A;
394 *pitch = SCORE_PITCH_As;
397 *pitch = SCORE_PITCH_B;
402 /* Determine a chord name (if any) from the current notes pressed */
404 typedef struct analyzed_note {
405 /* Original note being analzyed. */
408 /* Absolute pitch (expressed as midi number). */
411 /* Pitch relative to bass note. */
416 _compare_analyzed_note_by_midi_pitch (const void *va, const void *vb)
418 const analyzed_note_t *a = va, *b = vb;
420 return a->midi_pitch - b->midi_pitch;
424 _compare_analyzed_note_by_relative_pitch (const void *va, const void *vb)
426 const analyzed_note_t *a = va, *b = vb;
428 return a->relative_pitch - b->relative_pitch;
432 _chord_signature_matches (analyzed_note_t *notes,
434 int *signature_pitches,
435 int num_signature_pitches)
439 if (num_notes != num_signature_pitches)
442 for (i = 0; i < num_notes; i++)
443 if (notes[i].relative_pitch != signature_pitches[i])
450 scherzo_analyze_chord (scherzo_t *scherzo)
452 void *local = talloc_new (NULL);
453 analyzed_note_t *notes;
454 note_group_t *note_group;
455 unsigned i, j, num_notes;
457 const char *chord_name = NULL;
459 if (scherzo->pedal_pressed)
460 note_group = &scherzo->notes_pedaled;
462 note_group = &scherzo->notes_pressed;
464 num_notes = note_group->num_notes;
466 struct { int pitches[1]; const char *name; } octaves[] = {
470 struct { int pitches[2]; const char *name; } intervals[] = {
471 { {0, 1}, "Minor 2nd"},
472 { {0, 2}, "Major 2nd"},
473 { {0, 3}, "Minor 3rd"},
474 { {0, 4}, "Major 3rd"},
475 { {0, 5}, "Perfect 4th"},
476 { {0, 6}, "Diminished 5th/Augmented 4th"},
477 { {0, 7}, "Perfect 5th"},
478 { {0, 8}, "Minor 6th"},
479 { {0, 9}, "Major 6th"},
480 { {0, 10}, "Minor 7th"},
481 { {0, 11}, "Major 7th"}
484 struct { int pitches[3]; const char *name; } triads[] = {
485 { {0, 4, 8}, "Augmented triad" },
486 { {0, 4, 7}, "Major triad" },
487 { {0, 3, 7}, "Minor triad" },
488 { {0, 3, 6}, "Diminished triad" }
491 struct { int pitches[4]; const char *name; } sevenths[] = {
492 { {0, 4, 8, 11}, "Augmented/major 7" },
493 { {0, 4, 8, 10}, "Augmented 7" },
494 { {0, 4, 7, 11}, "Major 7" },
495 { {0, 4, 7, 10}, "Dominant 7" },
496 { {0, 3, 7, 11}, "Minor/major 7" },
497 { {0, 3, 7, 10}, "Minor 7" },
498 { {0, 3, 6, 11}, "Diminished/major 7" },
499 { {0, 3, 6, 10}, "Half-diminished 7" },
500 { {0, 3, 6, 9}, "Diminished 7" }
503 if (scherzo->chord) {
504 score_remove_chord (scherzo->chord);
505 scherzo->chord = NULL;
511 notes = talloc_array (local, analyzed_note_t, num_notes);
515 for (i = 0; i < num_notes; i++) {
516 score_note_t *note = note_group->notes[i];
517 notes[i].note = note;
518 notes[i].midi_pitch = _score_pitch_and_octave_to_midi (note->pitch,
520 /* Relative pitch will be filled in after sorting. */
521 notes[i].relative_pitch = 0;
524 /* First, sort by midi pitch to find the bass note. */
525 qsort (notes, num_notes, sizeof (analyzed_note_t),
526 _compare_analyzed_note_by_midi_pitch);
528 bass_pitch = notes[0].midi_pitch;
530 /* With the bass note identified, we can find all relative pitches. */
531 for (i = 0; i < num_notes; i++) {
532 notes[i].relative_pitch = notes[i].midi_pitch - bass_pitch;
533 while (notes[i].relative_pitch >= 12)
534 notes[i].relative_pitch -= 12;
537 /* Now, sort again by relative pitch. */
538 qsort (notes, num_notes, sizeof (analyzed_note_t),
539 _compare_analyzed_note_by_relative_pitch);
541 /* Finally, eliminate all duplicate notes. */
542 for (i = 0; i < num_notes - 1; i++) {
543 if (notes[i+1].relative_pitch == notes[i].relative_pitch) {
545 while (j < num_notes &&
546 notes[j].relative_pitch == notes[i].relative_pitch)
550 /* The loop incremented j one past the last
551 * duplicate. Decrement so that it points to the
552 * last duplicate (and is guaranteed to not exceed
553 * the array bounds).*/
556 if (j < num_notes - 1) {
557 memmove (¬es[i+1], ¬es[j+1],
558 (num_notes - j) * sizeof (analyzed_note_t));
561 num_notes -= (j - i);
567 for (i = 0; i < ARRAY_SIZE (octaves); i++) {
568 if (_chord_signature_matches (notes, num_notes, octaves[i].pitches, 1))
569 chord_name = octaves[i].name;
573 for (i = 0; i < ARRAY_SIZE (intervals); i++) {
574 if (_chord_signature_matches (notes, num_notes, intervals[i].pitches, 2))
575 chord_name = intervals[i].name;
579 for (i = 0; i < ARRAY_SIZE (triads); i++) {
580 if (_chord_signature_matches (notes, num_notes, triads[i].pitches, 3))
581 chord_name = triads[i].name;
585 for (i = 0; i < ARRAY_SIZE(sevenths); i++) {
586 if (_chord_signature_matches (notes, num_notes, sevenths[i].pitches, 4))
587 chord_name = sevenths[i].name;
593 scherzo->chord = score_add_chord (scherzo->treble, chord_name);
595 scherzo->chord = score_add_chord (scherzo->treble, "Unknown or not a chord");
602 note_group_init (void *ctx, note_group_t *group)
607 group->num_notes = 0;
611 note_group_add_note (note_group_t *group, score_note_t *note)
615 /* Do nothing if note is already in group. */
616 for (i = 0; i < group->num_notes; i++) {
617 if (group->notes[i]->pitch == note->pitch &&
618 group->notes[i]->octave == note->octave)
626 if (group->num_notes > group->size) {
628 group->notes = talloc_realloc (group->ctx, group->notes,
629 score_note_t*, group->size);
631 if (group->notes == NULL) {
632 fprintf (stderr, "Out of memory.\n");
637 group->notes[group->num_notes - 1] = note;
641 note_group_remove_note_at (note_group_t *group, int i)
643 if (i >= group->num_notes) {
644 fprintf (stderr, "Internal error: No note to remove at index %d\n", i);
648 if (i < group->num_notes - 1) {
649 memmove (group->notes + i, group->notes + i + 1,
650 (group->num_notes - 1 - i) * sizeof (score_note_t*));
655 static score_note_t *
656 scherzo_press_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
658 score_staff_t *staff;
662 if (scherzo->challenge.note) {
663 staff = scherzo->challenge.staff;
664 } else if (octave >= 4) {
665 staff = scherzo->treble;
667 staff = scherzo->bass;
670 /* Do nothing if this note is already pressed. */
671 for (i = 0; i < scherzo->notes_pressed.num_notes; i++) {
672 if (scherzo->notes_pressed.notes[i]->pitch == pitch &&
673 scherzo->notes_pressed.notes[i]->octave == octave)
675 return scherzo->notes_pressed.notes[i];
679 note = score_add_note (staff, pitch, octave, SCORE_DURATION_WHOLE);
681 note_group_add_note (&scherzo->notes_pressed, note);
683 if (scherzo->pedal_pressed)
684 note_group_add_note (&scherzo->notes_pedaled, note);
686 scherzo_analyze_chord (scherzo);
692 scherzo_release_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
698 for (i = scherzo->notes_pressed.num_notes - 1; i >=0; i--) {
699 note = scherzo->notes_pressed.notes[i];
700 if (note->pitch == pitch && note->octave == octave) {
702 if (! scherzo->pedal_pressed)
703 score_remove_note (note);
704 note_group_remove_note_at (&scherzo->notes_pressed, i);
709 fprintf (stderr, "Internal error: Failed to find note to release.\n");
712 scherzo_analyze_chord (scherzo);
715 static score_note_t *
716 scherzo_press_note_midi (scherzo_t *scherzo, unsigned char midi_note)
721 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
723 return scherzo_press_note (scherzo, pitch, octave);
727 scherzo_release_note_midi (scherzo_t *scherzo, unsigned char midi_note)
732 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
734 scherzo_release_note (scherzo, pitch, octave);
738 scherzo_press_pedal (scherzo_t *scherzo)
742 scherzo->pedal_pressed = 1;
744 /* Copy all pressed notes to pedaled notes */
745 for (i = 0; i < scherzo->notes_pressed.num_notes; i++)
746 note_group_add_note (&scherzo->notes_pedaled, scherzo->notes_pressed.notes[i]);
750 scherzo_release_pedal (scherzo_t *scherzo)
752 score_note_t *note, *new_note;
755 /* Make new notes in score for all pressed notes. */
756 for (i = 0; i < scherzo->notes_pressed.num_notes; i++) {
757 note = scherzo->notes_pressed.notes[i];
758 new_note = score_add_note (note->staff, note->pitch, note->octave, note->duration);
759 scherzo->notes_pressed.notes[i] = new_note;
762 /* Then remove all previously pedaled notes from the score. */
763 for (i = scherzo->notes_pedaled.num_notes - 1; i >=0; i--) {
764 note = scherzo->notes_pedaled.notes[i];
765 score_remove_note (note);
766 note_group_remove_note_at (&scherzo->notes_pedaled, i);
769 scherzo->pedal_pressed = 0;
771 scherzo_analyze_chord (scherzo);
773 gtk_widget_queue_draw (scherzo->window);
777 _select_challenge (scherzo_t *scherzo)
779 category_t *category_unused;
780 bool_t introduced_unused;
782 challenge_t *challenge = &scherzo->challenge;
787 if (challenge->note) {
788 score_remove_note (challenge->note);
789 challenge->note = NULL;
792 mnemon_select_item (&scherzo->mnemon,
794 &challenge->item_index,
798 item = challenge->bin->items[challenge->item_index];
801 if (strncmp (s, "treble:", 7) == 0) {
803 challenge->staff = scherzo->treble;
804 } else if (strncmp (s, "bass:", 5) == 0) {
806 challenge->staff = scherzo->bass;
809 "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
816 pitch = SCORE_PITCH_C;
819 pitch = SCORE_PITCH_D;
822 pitch = SCORE_PITCH_E;
825 pitch = SCORE_PITCH_F;
828 pitch = SCORE_PITCH_G;
831 pitch = SCORE_PITCH_A;
834 pitch = SCORE_PITCH_B;
837 fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
842 if (*s < '0' || *s > '9') {
843 fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
849 challenge->note = score_add_note (challenge->staff, pitch, octave,
850 SCORE_DURATION_WHOLE);
851 challenge->satisfied = 0;
852 challenge->mistaken = 0;
855 /* Determine whether the user hit the correct note. */
857 _judge_note (scherzo_t *scherzo, score_note_t *note)
859 challenge_t *challenge = &scherzo->challenge;
861 if (! scherzo->challenge.note) {
862 score_set_note_color_rgb (note, 0.0, 0.0, 0.0); /* black */
866 if (note->pitch == challenge->note->pitch &&
867 note->octave == challenge->note->octave)
869 challenge->satisfied = 1;
870 score_set_note_color_rgb (note, 18/256., 130/256., 28/256.); /* green */
874 challenge->mistaken = 1;
875 score_set_note_color_rgb (note, 184/256., 4/256., 22/256.); /* red */
879 /* If the user got the right note (eventually), then score it in
880 * mnemon and show the next note. */
882 _score_challenge (scherzo_t *scherzo)
884 challenge_t *challenge = &scherzo->challenge;
886 if (! challenge->note)
889 if (! challenge->satisfied)
892 mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
893 ! challenge->mistaken);
895 _select_challenge (scherzo);
899 on_midi_input (unused (GIOChannel *channel),
900 unused (GIOCondition condition),
903 unsigned char buf[MIDI_BUF_SIZE], *next;
904 scherzo_t *scherzo = user_data;
906 snd_seq_event_t event;
908 int need_redraw = FALSE;
910 remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
916 consumed = snd_midi_event_encode (scherzo->snd_midi_event,
917 next, remaining, &event);
919 remaining -= consumed;
922 switch (event.type) {
923 case SND_SEQ_EVENT_NONE:
924 /* Incomplete event. Nothing to do. */
926 case SND_SEQ_EVENT_NOTEON:
927 note = scherzo_press_note_midi (scherzo, event.data.note.note);
928 _judge_note (scherzo, note);
931 case SND_SEQ_EVENT_NOTEOFF:
932 scherzo_release_note_midi (scherzo, event.data.note.note);
933 _score_challenge (scherzo);
936 case SND_SEQ_EVENT_CLOCK:
937 /* Ignore for now as my piano sends a constant stream of these. */
939 case SND_SEQ_EVENT_SENSING:
940 /* Ignore for now as my piano sends a constant stream of these. */
942 case SND_SEQ_EVENT_CONTROLLER:
943 /* XXX: My piano gives me 64 for the sustain pedal, is
945 if (event.data.control.param == 64) {
946 if (event.data.control.value == 0)
947 scherzo_release_pedal (scherzo);
949 scherzo_press_pedal (scherzo);
951 fprintf (stderr, "Fixme: Unhandled MIDI Control event (param=%d, value=%d)\n",
952 event.data.control.param, event.data.control.value);
956 fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
963 gtk_widget_queue_draw (scherzo->window);
965 /* Return TRUE to continue to get called in the future. */
970 main (int argc, char *argv[])
972 GtkWidget *drawing_area;
978 gtk_init (&argc, &argv);
980 scherzo.ctx = talloc_new (NULL);
982 scherzo.score = score_create (scherzo.ctx);
983 scherzo.staff_height = 100;
984 score_set_staff_height (scherzo.score, scherzo.staff_height);
986 score_add_brace (scherzo.score, 2);
987 scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
988 scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
990 scherzo.chord = NULL;
992 /* Default to octave 4 for computer keyboard keypresses. */
993 scherzo.keyboard_octave = 4;
995 note_group_init (scherzo.ctx, &scherzo.notes_pressed);
996 note_group_init (scherzo.ctx, &scherzo.notes_pedaled);
998 scherzo.pedal_pressed = 0;
1000 mnemon_init (&scherzo.mnemon);
1001 /* XXX: Should create a default file if one cannot be loaded. */
1002 mnemon_load_category (&scherzo.mnemon, "scherzo-notes");
1004 scherzo.challenge.note = NULL;
1006 _select_challenge (&scherzo);
1009 err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
1011 fprintf (stderr, "Out of memory.\n");
1015 #define MIDI_DEVICE "/dev/midi1"
1016 scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
1017 if (scherzo.midi_fd < 0) {
1018 printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
1020 GIOChannel *channel;
1022 channel = g_io_channel_unix_new (scherzo.midi_fd);
1023 g_io_channel_set_encoding (channel, NULL, NULL);
1024 g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
1027 scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1029 gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 1000, 600);
1031 g_signal_connect (scherzo.window, "delete-event",
1032 G_CALLBACK (on_delete_event_quit), NULL);
1034 drawing_area = gtk_drawing_area_new ();
1036 gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
1038 g_signal_connect (drawing_area, "expose-event",
1039 G_CALLBACK (on_expose_event_draw),
1042 g_signal_connect (scherzo.window, "key-press-event",
1043 G_CALLBACK (on_key_press_event),
1046 g_signal_connect (scherzo.window, "key-release-event",
1047 G_CALLBACK (on_key_release_event),
1050 gtk_widget_show_all (scherzo.window);
1054 mnemon_save (&scherzo.mnemon);
1056 snd_midi_event_free (scherzo.snd_midi_event);
1058 talloc_free (scherzo.ctx);