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;
150 if (scherzo->challenge.note)
151 octave = scherzo->challenge.note->octave;
153 octave = scherzo->keyboard_octave;
155 switch (key->keyval) {
159 case GDK_KEY_KP_Equal:
160 scherzo->staff_height += 4;
161 gtk_widget_queue_draw (widget);
165 case GDK_KEY_KP_Subtract:
166 scherzo->staff_height -= 4;
167 gtk_widget_queue_draw (widget);
177 pitch = SCORE_PITCH_C;
181 pitch = SCORE_PITCH_D;
185 pitch = SCORE_PITCH_E;
189 pitch = SCORE_PITCH_F;
193 pitch = SCORE_PITCH_G;
197 pitch = SCORE_PITCH_A;
201 pitch = SCORE_PITCH_B;
212 scherzo->keyboard_octave = key->keyval - GDK_KEY_0;
215 scherzo_press_pedal (scherzo);
219 if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) ||
220 (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g))
224 note = scherzo_press_note (scherzo, pitch, octave);
225 _judge_note (scherzo, note);
226 gtk_widget_queue_draw (scherzo->window);
232 /* Allow an unhandled event to propagate to other handlers. */
237 on_key_release_event (unused (GtkWidget *widget),
241 scherzo_t *scherzo = user_data;
245 if (scherzo->challenge.note)
246 octave = scherzo->challenge.note->octave;
248 octave = scherzo->keyboard_octave;
250 switch (key->keyval) {
253 pitch = SCORE_PITCH_C;
257 pitch = SCORE_PITCH_D;
261 pitch = SCORE_PITCH_E;
265 pitch = SCORE_PITCH_F;
269 pitch = SCORE_PITCH_G;
273 pitch = SCORE_PITCH_A;
277 pitch = SCORE_PITCH_B;
280 scherzo_release_pedal (scherzo);
284 if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) ||
285 (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g))
287 scherzo_release_note (scherzo, pitch, octave);
288 _score_challenge (scherzo);
289 gtk_widget_queue_draw (scherzo->window);
295 /* Allow an unhandled event to propagate to other handlers. */
300 _score_pitch_and_octave_to_midi (score_pitch_t pitch,
303 unsigned char midi_note = 12 * (octave + 1);
305 switch (SCORE_PITCH_NAME (pitch)) {
306 case SCORE_PITCH_NAME_C:
308 case SCORE_PITCH_NAME_D:
311 case SCORE_PITCH_NAME_E:
314 case SCORE_PITCH_NAME_F:
317 case SCORE_PITCH_NAME_G:
320 case SCORE_PITCH_NAME_A:
323 case SCORE_PITCH_NAME_B:
328 switch (SCORE_PITCH_ACCIDENTAL (pitch)) {
329 case SCORE_PITCH_ACCIDENTAL_DOUBLE_FLAT:
332 case SCORE_PITCH_ACCIDENTAL_FLAT:
335 case SCORE_PITCH_ACCIDENTAL_NATURAL:
337 case SCORE_PITCH_ACCIDENTAL_SHARP:
340 case SCORE_PITCH_ACCIDENTAL_DOUBLE_SHARP:
349 _midi_to_score_pitch_and_octave (unsigned char midi_note,
350 score_pitch_t *pitch,
353 *octave = midi_note / 12 - 1;
355 switch (midi_note % 12)
358 *pitch = SCORE_PITCH_C;
361 *pitch = SCORE_PITCH_Cs;
364 *pitch = SCORE_PITCH_D;
367 *pitch = SCORE_PITCH_Ds;
370 *pitch = SCORE_PITCH_E;
373 *pitch = SCORE_PITCH_F;
376 *pitch = SCORE_PITCH_Fs;
379 *pitch = SCORE_PITCH_G;
382 *pitch = SCORE_PITCH_Gs;
385 *pitch = SCORE_PITCH_A;
388 *pitch = SCORE_PITCH_As;
391 *pitch = SCORE_PITCH_B;
396 /* Determine a chord name (if any) from the current notes pressed */
398 typedef struct analyzed_note {
399 /* Original note being analzyed. */
402 /* Absolute pitch (expressed as midi number). */
405 /* Pitch relative to bass note. */
410 _compare_analyzed_note (const void *va, const void *vb)
412 const analyzed_note_t *a = va, *b = vb;
414 return a->midi_pitch - b->midi_pitch;
418 _chord_signature_matches (analyzed_note_t *notes,
420 int *signature_pitches,
421 int num_signature_pitches)
425 for (n = 0, s = 0; s < num_signature_pitches; s++) {
428 if (notes[n].relative_pitch != signature_pitches[s])
431 /* Skip repeated notes in chord being tested. */
432 while (n < num_notes &&
433 notes[n].relative_pitch == signature_pitches[s])
439 /* If there are fewer notes in the signature than in the chord,
440 * then there is no match. */
448 scherzo_analyze_chord (scherzo_t *scherzo)
450 void *local = talloc_new (NULL);
451 analyzed_note_t *notes;
452 note_group_t *note_group;
453 unsigned i, num_notes;
455 const char *chord_name = NULL;
457 if (scherzo->pedal_pressed)
458 note_group = &scherzo->notes_pedaled;
460 note_group = &scherzo->notes_pressed;
462 num_notes = note_group->num_notes;
464 struct { int pitches[2]; const char *name; } intervals[] = {
465 { {0, 1}, "Minor 2nd"},
466 { {0, 2}, "Major 2nd"},
467 { {0, 3}, "Minor 3rd"},
468 { {0, 4}, "Major 3rd"},
469 { {0, 5}, "Perfect 4th"},
470 { {0, 6}, "Diminished 5th/Augmented 4th"},
471 { {0, 7}, "Perfect 5th"},
472 { {0, 8}, "Minor 6th"},
473 { {0, 9}, "Major 6th"},
474 { {0, 10}, "Minor 7th"},
475 { {0, 11}, "Major 7th"}
478 struct { int pitches[3]; const char *name; } triads[] = {
479 { {0, 4, 8}, "Augmented triad" },
480 { {0, 4, 7}, "Major triad" },
481 { {0, 3, 7}, "Minor triad" },
482 { {0, 3, 6}, "Diminished triad" }
485 struct { int pitches[4]; const char *name; } sevenths[] = {
486 { {0, 4, 8, 11}, "Augmented/major 7" },
487 { {0, 4, 7, 11}, "Major 7" },
488 { {0, 4, 7, 10}, "Dominant 7" },
489 { {0, 3, 7, 11}, "Minor/major 7" },
490 { {0, 3, 7, 10}, "Minor 7" },
491 { {0, 3, 6, 10}, "Half-diminished 7" },
492 { {0, 3, 6, 9}, "Diminished 7" },
493 { {0, 4, 8, 10}, "Augmented 7" },
494 { {0, 3, 6, 11}, "Diminished/major 7" },
497 if (scherzo->chord) {
498 score_remove_chord (scherzo->chord);
499 scherzo->chord = NULL;
505 notes = talloc_array (local, analyzed_note_t, num_notes);
509 for (i = 0; i < num_notes; i++) {
510 score_note_t *note = note_group->notes[i];
511 notes[i].note = note;
512 notes[i].midi_pitch = _score_pitch_and_octave_to_midi (note->pitch,
514 /* Relative pitch will be filled in after sorting. */
515 notes[i].relative_pitch = 0;
518 qsort (notes, num_notes, sizeof (analyzed_note_t), _compare_analyzed_note);
520 bass_pitch = notes[0].midi_pitch;
522 for (i = 0; i < num_notes; i++) {
523 notes[i].relative_pitch = notes[i].midi_pitch - bass_pitch;
524 while (notes[i].relative_pitch > 12)
525 notes[i].relative_pitch -= 12;
528 for (i = 0; i < ARRAY_SIZE (intervals); i++) {
529 if (_chord_signature_matches (notes, num_notes, intervals[i].pitches, 2))
530 chord_name = intervals[i].name;
533 for (i = 0; i < ARRAY_SIZE (triads); i++) {
534 if (_chord_signature_matches (notes, num_notes, triads[i].pitches, 3))
535 chord_name = triads[i].name;
538 for (i = 0; i < ARRAY_SIZE(sevenths); i++) {
539 if (_chord_signature_matches (notes, num_notes, sevenths[i].pitches, 4))
540 chord_name = sevenths[i].name;
544 scherzo->chord = score_add_chord (scherzo->treble, chord_name);
546 scherzo->chord = score_add_chord (scherzo->treble, "Unknown or not a chord");
553 note_group_init (void *ctx, note_group_t *group)
558 group->num_notes = 0;
562 note_group_add_note (note_group_t *group, score_note_t *note)
566 if (group->num_notes > group->size) {
568 group->notes = talloc_realloc (group->ctx, group->notes,
569 score_note_t*, group->size);
571 if (group->notes == NULL) {
572 fprintf (stderr, "Out of memory.\n");
577 group->notes[group->num_notes - 1] = note;
581 note_group_remove_note_at (note_group_t *group, int i)
583 if (i >= group->num_notes) {
584 fprintf (stderr, "Internal error: No note to remove at index %d\n", i);
588 if (i < group->num_notes - 1) {
589 memmove (group->notes + i, group->notes + i + 1,
590 (group->num_notes - 1 - i) * sizeof (score_note_t*));
595 static score_note_t *
596 scherzo_press_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
598 score_staff_t *staff;
602 if (scherzo->challenge.note) {
603 staff = scherzo->challenge.staff;
604 } else if (octave >= 4) {
605 staff = scherzo->treble;
607 staff = scherzo->bass;
610 /* Do nothing if this note is already pressed. */
611 for (i = 0; i < scherzo->notes_pressed.num_notes; i++) {
612 if (scherzo->notes_pressed.notes[i]->pitch == pitch &&
613 scherzo->notes_pressed.notes[i]->octave == octave)
615 return scherzo->notes_pressed.notes[i];
619 note = score_add_note (staff, pitch, octave, SCORE_DURATION_WHOLE);
621 note_group_add_note (&scherzo->notes_pressed, note);
623 if (scherzo->pedal_pressed)
624 note_group_add_note (&scherzo->notes_pedaled, note);
626 scherzo_analyze_chord (scherzo);
632 scherzo_release_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
638 for (i = scherzo->notes_pressed.num_notes - 1; i >=0; i--) {
639 note = scherzo->notes_pressed.notes[i];
640 if (note->pitch == pitch && note->octave == octave) {
642 if (! scherzo->pedal_pressed)
643 score_remove_note (note);
644 note_group_remove_note_at (&scherzo->notes_pressed, i);
649 fprintf (stderr, "Internal error: Failed to find note to release.\n");
652 scherzo_analyze_chord (scherzo);
655 static score_note_t *
656 scherzo_press_note_midi (scherzo_t *scherzo, unsigned char midi_note)
661 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
663 return scherzo_press_note (scherzo, pitch, octave);
667 scherzo_release_note_midi (scherzo_t *scherzo, unsigned char midi_note)
672 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
674 scherzo_release_note (scherzo, pitch, octave);
678 scherzo_press_pedal (scherzo_t *scherzo)
682 scherzo->pedal_pressed = 1;
684 /* Copy all pressed notes to pedaled notes */
685 for (i = 0; i < scherzo->notes_pressed.num_notes; i++)
686 note_group_add_note (&scherzo->notes_pedaled, scherzo->notes_pressed.notes[i]);
690 scherzo_release_pedal (scherzo_t *scherzo)
692 score_note_t *note, *new_note;
695 /* Make new notes in score for all pressed notes. */
696 for (i = 0; i < scherzo->notes_pressed.num_notes; i++) {
697 note = scherzo->notes_pressed.notes[i];
698 new_note = score_add_note (note->staff, note->pitch, note->octave, note->duration);
699 scherzo->notes_pressed.notes[i] = new_note;
702 /* Then remove all previously pedaled notes from the score. */
703 for (i = scherzo->notes_pedaled.num_notes - 1; i >=0; i--) {
704 note = scherzo->notes_pedaled.notes[i];
705 score_remove_note (note);
706 note_group_remove_note_at (&scherzo->notes_pedaled, i);
709 scherzo->pedal_pressed = 0;
711 scherzo_analyze_chord (scherzo);
713 gtk_widget_queue_draw (scherzo->window);
717 _select_challenge (scherzo_t *scherzo)
719 category_t *category_unused;
720 bool_t introduced_unused;
722 challenge_t *challenge = &scherzo->challenge;
727 if (challenge->note) {
728 score_remove_note (challenge->note);
729 challenge->note = NULL;
732 mnemon_select_item (&scherzo->mnemon,
734 &challenge->item_index,
738 item = challenge->bin->items[challenge->item_index];
741 if (strncmp (s, "treble:", 7) == 0) {
743 challenge->staff = scherzo->treble;
744 } else if (strncmp (s, "bass:", 5) == 0) {
746 challenge->staff = scherzo->bass;
749 "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
756 pitch = SCORE_PITCH_VALUE(C, NATURAL);
759 pitch = SCORE_PITCH_VALUE(D, NATURAL);
762 pitch = SCORE_PITCH_VALUE(E, NATURAL);
765 pitch = SCORE_PITCH_VALUE(F, NATURAL);
768 pitch = SCORE_PITCH_VALUE(G, NATURAL);
771 pitch = SCORE_PITCH_VALUE(A, NATURAL);
774 pitch = SCORE_PITCH_VALUE(B, NATURAL);
777 fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
782 if (*s < '0' || *s > '9') {
783 fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
789 challenge->note = score_add_note (challenge->staff, pitch, octave,
790 SCORE_DURATION_WHOLE);
791 challenge->satisfied = 0;
792 challenge->mistaken = 0;
795 /* Determine whether the user hit the correct note. */
797 _judge_note (scherzo_t *scherzo, score_note_t *note)
799 challenge_t *challenge = &scherzo->challenge;
801 if (! scherzo->challenge.note) {
802 score_set_note_color_rgb (note, 0.0, 0.0, 0.0); /* black */
806 if (note->pitch == challenge->note->pitch &&
807 note->octave == challenge->note->octave)
809 challenge->satisfied = 1;
810 score_set_note_color_rgb (note, 18/256., 130/256., 28/256.); /* green */
814 challenge->mistaken = 1;
815 score_set_note_color_rgb (note, 184/256., 4/256., 22/256.); /* red */
819 /* If the user got the right note (eventually), then score it in
820 * mnemon and show the next note. */
822 _score_challenge (scherzo_t *scherzo)
824 challenge_t *challenge = &scherzo->challenge;
826 if (! challenge->note)
829 if (! challenge->satisfied)
832 mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
833 ! challenge->mistaken);
835 _select_challenge (scherzo);
839 on_midi_input (unused (GIOChannel *channel),
840 unused (GIOCondition condition),
843 unsigned char buf[MIDI_BUF_SIZE], *next;
844 scherzo_t *scherzo = user_data;
846 snd_seq_event_t event;
848 int need_redraw = FALSE;
850 remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
856 consumed = snd_midi_event_encode (scherzo->snd_midi_event,
857 next, remaining, &event);
859 remaining -= consumed;
862 switch (event.type) {
863 case SND_SEQ_EVENT_NONE:
864 /* Incomplete event. Nothing to do. */
866 case SND_SEQ_EVENT_NOTEON:
867 note = scherzo_press_note_midi (scherzo, event.data.note.note);
868 _judge_note (scherzo, note);
871 case SND_SEQ_EVENT_NOTEOFF:
872 scherzo_release_note_midi (scherzo, event.data.note.note);
873 _score_challenge (scherzo);
876 case SND_SEQ_EVENT_CLOCK:
877 /* Ignore for now as my piano sends a constant stream of these. */
879 case SND_SEQ_EVENT_SENSING:
880 /* Ignore for now as my piano sends a constant stream of these. */
882 case SND_SEQ_EVENT_CONTROLLER:
883 /* XXX: My piano gives me 64 for the sustain pedal, is
885 if (event.data.control.param == 64) {
886 if (event.data.control.value == 0)
887 scherzo_release_pedal (scherzo);
889 scherzo_press_pedal (scherzo);
891 fprintf (stderr, "Fixme: Unhandled MIDI Control event (param=%d, value=%d)\n",
892 event.data.control.param, event.data.control.value);
896 fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
903 gtk_widget_queue_draw (scherzo->window);
905 /* Return TRUE to continue to get called in the future. */
910 main (int argc, char *argv[])
912 GtkWidget *drawing_area;
918 gtk_init (&argc, &argv);
920 scherzo.ctx = talloc_new (NULL);
922 scherzo.score = score_create (scherzo.ctx);
923 scherzo.staff_height = 100;
924 score_set_staff_height (scherzo.score, scherzo.staff_height);
926 score_add_brace (scherzo.score, 2);
927 scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
928 scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
930 scherzo.chord = NULL;
932 /* Default to octave 4 for computer keyboard keypresses. */
933 scherzo.keyboard_octave = 4;
935 note_group_init (scherzo.ctx, &scherzo.notes_pressed);
936 note_group_init (scherzo.ctx, &scherzo.notes_pedaled);
938 scherzo.pedal_pressed = 0;
940 mnemon_init (&scherzo.mnemon);
941 /* XXX: Should create a default file if one cannot be loaded. */
942 mnemon_load_category (&scherzo.mnemon, "scherzo-notes");
944 scherzo.challenge.note = NULL;
946 _select_challenge (&scherzo);
949 err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
951 fprintf (stderr, "Out of memory.\n");
955 #define MIDI_DEVICE "/dev/midi1"
956 scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
957 if (scherzo.midi_fd < 0) {
958 printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
962 channel = g_io_channel_unix_new (scherzo.midi_fd);
963 g_io_channel_set_encoding (channel, NULL, NULL);
964 g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
967 scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
969 gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 1000, 600);
971 g_signal_connect (scherzo.window, "delete-event",
972 G_CALLBACK (on_delete_event_quit), NULL);
974 drawing_area = gtk_drawing_area_new ();
976 gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
978 g_signal_connect (drawing_area, "expose-event",
979 G_CALLBACK (on_expose_event_draw),
982 g_signal_connect (scherzo.window, "key-press-event",
983 G_CALLBACK (on_key_press_event),
986 g_signal_connect (scherzo.window, "key-release-event",
987 G_CALLBACK (on_key_release_event),
990 gtk_widget_show_all (scherzo.window);
994 mnemon_save (&scherzo.mnemon);
996 snd_midi_event_free (scherzo.snd_midi_event);
998 talloc_free (scherzo.ctx);