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 /* This is for a "computer keyboard". Any "piano keyboard" key
56 * knows its own octave. */
60 snd_midi_event_t *snd_midi_event;
63 challenge_t challenge;
65 int num_notes_pressed;
66 score_note_t **notes_pressed;
69 /* Forward declarations. */
71 scherzo_add_note (scherzo_t *scherzo, score_pitch_t pitch, int octave);
74 scherzo_remove_note (scherzo_t *scherzo, score_pitch_t pitch, int octave);
77 _judge_note (scherzo_t *scherzo, score_note_t *note);
80 _score_challenge (scherzo_t *scherzo);
83 on_delete_event_quit (unused (GtkWidget *widget),
84 unused (GdkEvent *event),
85 unused (gpointer user_data))
89 /* Returning FALSE allows the default handler for delete-event
90 * to proceed to cleanup the widget. */
95 on_expose_event_draw (GtkWidget *widget,
96 unused (GdkEventExpose *expose),
99 scherzo_t *scherzo = user_data;
100 score_t *score = scherzo->score;
102 GtkAllocation allocation;
103 static const int pad = 10;
106 gtk_widget_get_allocation (widget, &allocation);
107 widget_width = allocation.width;
109 cr = gdk_cairo_create (widget->window);
111 /* White background */
112 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
115 /* Add some padding on the sides and top */
116 cairo_translate (cr, pad, scherzo->staff_height);
117 score_set_staff_height (score, scherzo->staff_height);
118 score_set_width (score, widget_width - 2 * pad);
120 score_draw (score, cr);
126 on_key_press_event (GtkWidget *widget,
130 scherzo_t *scherzo = user_data;
134 if (scherzo->challenge.note)
135 octave = scherzo->challenge.note->octave;
137 octave = scherzo->keyboard_octave;
139 switch (key->keyval) {
143 case GDK_KEY_KP_Equal:
144 scherzo->staff_height += 4;
145 gtk_widget_queue_draw (widget);
149 case GDK_KEY_KP_Subtract:
150 scherzo->staff_height -= 4;
151 gtk_widget_queue_draw (widget);
161 pitch = SCORE_PITCH_C;
165 pitch = SCORE_PITCH_D;
169 pitch = SCORE_PITCH_E;
173 pitch = SCORE_PITCH_F;
177 pitch = SCORE_PITCH_G;
181 pitch = SCORE_PITCH_A;
185 pitch = SCORE_PITCH_B;
196 scherzo->keyboard_octave = key->keyval - GDK_KEY_0;
200 if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) ||
201 (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g))
205 note = scherzo_add_note (scherzo, pitch, octave);
206 _judge_note (scherzo, note);
207 gtk_widget_queue_draw (scherzo->window);
213 /* Allow an unhandled event to propagate to other handlers. */
218 on_key_release_event (unused (GtkWidget *widget),
222 scherzo_t *scherzo = user_data;
226 if (scherzo->challenge.note)
227 octave = scherzo->challenge.note->octave;
229 octave = scherzo->keyboard_octave;
231 switch (key->keyval) {
234 pitch = SCORE_PITCH_C;
238 pitch = SCORE_PITCH_D;
242 pitch = SCORE_PITCH_E;
246 pitch = SCORE_PITCH_F;
250 pitch = SCORE_PITCH_G;
254 pitch = SCORE_PITCH_A;
258 pitch = SCORE_PITCH_B;
262 if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) ||
263 (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g))
265 scherzo_remove_note (scherzo, pitch, octave);
266 _score_challenge (scherzo);
267 gtk_widget_queue_draw (scherzo->window);
273 /* Allow an unhandled event to propagate to other handlers. */
278 _score_pitch_and_octave_to_midi (score_pitch_t pitch,
281 unsigned char midi_note = 12 * (octave + 1);
283 switch (SCORE_PITCH_NAME (pitch)) {
284 case SCORE_PITCH_NAME_C:
286 case SCORE_PITCH_NAME_D:
289 case SCORE_PITCH_NAME_E:
292 case SCORE_PITCH_NAME_F:
295 case SCORE_PITCH_NAME_G:
298 case SCORE_PITCH_NAME_A:
301 case SCORE_PITCH_NAME_B:
306 switch (SCORE_PITCH_ACCIDENTAL (pitch)) {
307 case SCORE_PITCH_ACCIDENTAL_DOUBLE_FLAT:
310 case SCORE_PITCH_ACCIDENTAL_FLAT:
313 case SCORE_PITCH_ACCIDENTAL_NATURAL:
315 case SCORE_PITCH_ACCIDENTAL_SHARP:
318 case SCORE_PITCH_ACCIDENTAL_DOUBLE_SHARP:
327 _midi_to_score_pitch_and_octave (unsigned char midi_note,
328 score_pitch_t *pitch,
331 *octave = midi_note / 12 - 1;
333 switch (midi_note % 12)
336 *pitch = SCORE_PITCH_C;
339 *pitch = SCORE_PITCH_Cs;
342 *pitch = SCORE_PITCH_D;
345 *pitch = SCORE_PITCH_Ds;
348 *pitch = SCORE_PITCH_E;
351 *pitch = SCORE_PITCH_F;
354 *pitch = SCORE_PITCH_Fs;
357 *pitch = SCORE_PITCH_G;
360 *pitch = SCORE_PITCH_Gs;
363 *pitch = SCORE_PITCH_A;
366 *pitch = SCORE_PITCH_As;
369 *pitch = SCORE_PITCH_B;
374 /* Determine a chord name (if any) from the current notes pressed */
376 typedef struct analyzed_note {
377 /* Original note being analzyed. */
380 /* Absolute pitch (expressed as midi number). */
383 /* Pitch relative to bass note. */
388 _compare_analyzed_note (const void *va, const void *vb)
390 const analyzed_note_t *a = va, *b = vb;
392 return a->midi_pitch - b->midi_pitch;
396 _chord_signature_matches (analyzed_note_t *notes,
398 int *signature_pitches,
399 int num_signature_pitches)
403 for (n = 0, s = 0; s < num_signature_pitches; s++) {
406 if (notes[n].relative_pitch != signature_pitches[s])
409 /* Skip repeated notes in chord being tested. */
410 while (n < num_notes &&
411 notes[n].relative_pitch == signature_pitches[s])
417 /* If there are fewer notes in the signature than in the chord,
418 * then there is no match. */
426 scherzo_analyze_chord (scherzo_t *scherzo)
428 void *local = talloc_new (NULL);
429 analyzed_note_t *notes;
430 unsigned i, num_notes = scherzo->num_notes_pressed;
432 const char *chord_name = NULL;
434 struct { int pitches[2]; const char *name; } intervals[] = {
435 { {0, 1}, "Minor 2nd"},
436 { {0, 2}, "Major 2nd"},
437 { {0, 3}, "Minor 3rd"},
438 { {0, 4}, "Major 3rd"},
439 { {0, 5}, "Perfect 4th"},
440 { {0, 6}, "Diminished 5th/Augmented 4th"},
441 { {0, 7}, "Perfect 5th"},
442 { {0, 8}, "Minor 6th"},
443 { {0, 9}, "Major 6th"},
444 { {0, 10}, "Minor 7th"},
445 { {0, 11}, "Major 7th"}
448 struct { int pitches[3]; const char *name; } triads[] = {
449 { {0, 4, 8}, "Augmented triad" },
450 { {0, 4, 7}, "Major triad" },
451 { {0, 3, 7}, "Minor triad" },
452 { {0, 3, 6}, "Diminished triad" }
455 struct { int pitches[4]; const char *name; } sevenths[] = {
456 { {0, 4, 8, 11}, "Augmented/major 7" },
457 { {0, 4, 7, 11}, "Major 7" },
458 { {0, 4, 7, 10}, "Dominant 7" },
459 { {0, 3, 7, 11}, "Minor/major 7" },
460 { {0, 3, 7, 10}, "Minor 7" },
461 { {0, 3, 6, 10}, "Half-diminished 7" },
462 { {0, 3, 6, 9}, "Diminished 7" },
463 { {0, 4, 8, 10}, "Augmented 7" },
464 { {0, 3, 6, 11}, "Diminished/major 7" },
467 if (scherzo->chord) {
468 score_remove_chord (scherzo->chord);
469 scherzo->chord = NULL;
475 notes = talloc_array (local, analyzed_note_t, num_notes);
479 for (i = 0; i < num_notes; i++) {
480 score_note_t *note = scherzo->notes_pressed[i];
481 notes[i].note = note;
482 notes[i].midi_pitch = _score_pitch_and_octave_to_midi (note->pitch,
484 /* Relative pitch will be filled in after sorting. */
485 notes[i].relative_pitch = 0;
488 qsort (notes, num_notes, sizeof (analyzed_note_t), _compare_analyzed_note);
490 bass_pitch = notes[0].midi_pitch;
492 for (i = 0; i < num_notes; i++) {
493 notes[i].relative_pitch = notes[i].midi_pitch - bass_pitch;
496 for (i = 0; i < ARRAY_SIZE (intervals); i++) {
497 if (_chord_signature_matches (notes, num_notes, intervals[i].pitches, 2))
498 chord_name = intervals[i].name;
501 for (i = 0; i < ARRAY_SIZE (triads); i++) {
502 if (_chord_signature_matches (notes, num_notes, triads[i].pitches, 3))
503 chord_name = triads[i].name;
506 for (i = 0; i < ARRAY_SIZE(sevenths); i++) {
507 if (_chord_signature_matches (notes, num_notes, sevenths[i].pitches, 4))
508 chord_name = sevenths[i].name;
512 scherzo->chord = score_add_chord (scherzo->treble, chord_name);
514 scherzo->chord = score_add_chord (scherzo->treble, "Unknown or not a chord");
520 static score_note_t *
521 scherzo_add_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
523 score_staff_t *staff;
527 if (scherzo->challenge.note) {
528 staff = scherzo->challenge.staff;
529 } else if (octave >= 4) {
530 staff = scherzo->treble;
532 staff = scherzo->bass;
535 /* Do nothing if this note is already pressed. */
536 for (i = 0; i < scherzo->num_notes_pressed; i++) {
537 if (scherzo->notes_pressed[i]->pitch == pitch &&
538 scherzo->notes_pressed[i]->octave == octave)
540 return scherzo->notes_pressed[i];
544 note = score_add_note (staff, pitch, octave, SCORE_DURATION_WHOLE);
546 scherzo->num_notes_pressed++;
547 scherzo->notes_pressed = talloc_realloc (scherzo->ctx,
548 scherzo->notes_pressed,
550 scherzo->num_notes_pressed);
551 if (scherzo->notes_pressed == NULL) {
552 fprintf (stderr, "Out of memory.\n");
556 scherzo->notes_pressed[scherzo->num_notes_pressed - 1] = note;
558 scherzo_analyze_chord (scherzo);
565 scherzo_remove_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
570 for (i = 0; i < scherzo->num_notes_pressed; i++) {
571 note = scherzo->notes_pressed[i];
572 if (note->pitch == pitch && note->octave == octave) {
573 score_remove_note (note);
574 if (i < scherzo->num_notes_pressed - 1) {
575 memmove (scherzo->notes_pressed + i,
576 scherzo->notes_pressed + i + 1,
577 (scherzo->num_notes_pressed - 1 - i) * sizeof (score_note_t*));
579 scherzo->num_notes_pressed--;
584 scherzo_analyze_chord (scherzo);
587 static score_note_t *
588 scherzo_add_note_midi (scherzo_t *scherzo, unsigned char midi_note)
593 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
595 return scherzo_add_note (scherzo, pitch, octave);
600 scherzo_remove_note_midi (scherzo_t *scherzo, unsigned char midi_note)
605 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
607 scherzo_remove_note (scherzo, pitch, octave);
611 _select_challenge (scherzo_t *scherzo)
613 category_t *category_unused;
614 bool_t introduced_unused;
616 challenge_t *challenge = &scherzo->challenge;
621 if (challenge->note) {
622 score_remove_note (challenge->note);
623 challenge->note = NULL;
626 mnemon_select_item (&scherzo->mnemon,
628 &challenge->item_index,
632 item = challenge->bin->items[challenge->item_index];
635 if (strncmp (s, "treble:", 7) == 0) {
637 challenge->staff = scherzo->treble;
638 } else if (strncmp (s, "bass:", 5) == 0) {
640 challenge->staff = scherzo->bass;
643 "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
650 pitch = SCORE_PITCH_VALUE(C, NATURAL);
653 pitch = SCORE_PITCH_VALUE(D, NATURAL);
656 pitch = SCORE_PITCH_VALUE(E, NATURAL);
659 pitch = SCORE_PITCH_VALUE(F, NATURAL);
662 pitch = SCORE_PITCH_VALUE(G, NATURAL);
665 pitch = SCORE_PITCH_VALUE(A, NATURAL);
668 pitch = SCORE_PITCH_VALUE(B, NATURAL);
671 fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
676 if (*s < '0' || *s > '9') {
677 fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
683 challenge->note = score_add_note (challenge->staff, pitch, octave,
684 SCORE_DURATION_WHOLE);
685 challenge->satisfied = 0;
686 challenge->mistaken = 0;
689 /* Determine whether the user hit the correct note. */
691 _judge_note (scherzo_t *scherzo, score_note_t *note)
693 challenge_t *challenge = &scherzo->challenge;
695 if (! scherzo->challenge.note) {
696 score_set_note_color_rgb (note, 0.0, 0.0, 0.0); /* black */
700 if (note->pitch == challenge->note->pitch &&
701 note->octave == challenge->note->octave)
703 challenge->satisfied = 1;
704 score_set_note_color_rgb (note, 18/256., 130/256., 28/256.); /* green */
708 challenge->mistaken = 1;
709 score_set_note_color_rgb (note, 184/256., 4/256., 22/256.); /* red */
713 /* If the user got the right note (eventually), then score it in
714 * mnemon and show the next note. */
716 _score_challenge (scherzo_t *scherzo)
718 challenge_t *challenge = &scherzo->challenge;
720 if (! challenge->note)
723 if (! challenge->satisfied)
726 mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
727 ! challenge->mistaken);
729 _select_challenge (scherzo);
733 on_midi_input (unused (GIOChannel *channel),
734 unused (GIOCondition condition),
737 unsigned char buf[MIDI_BUF_SIZE], *next;
738 scherzo_t *scherzo = user_data;
740 snd_seq_event_t event;
742 int need_redraw = FALSE;
744 remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
750 consumed = snd_midi_event_encode (scherzo->snd_midi_event,
751 next, remaining, &event);
753 remaining -= consumed;
756 switch (event.type) {
757 case SND_SEQ_EVENT_NONE:
758 /* Incomplete event. Nothing to do. */
760 case SND_SEQ_EVENT_NOTEON:
761 note = scherzo_add_note_midi (scherzo, event.data.note.note);
762 _judge_note (scherzo, note);
765 case SND_SEQ_EVENT_NOTEOFF:
766 scherzo_remove_note_midi (scherzo, event.data.note.note);
767 _score_challenge (scherzo);
770 case SND_SEQ_EVENT_CLOCK:
771 /* Ignore for now as my piano sends a constant stream of these. */
773 case SND_SEQ_EVENT_SENSING:
774 /* Ignore for now as my piano sends a constant stream of these. */
777 fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
784 gtk_widget_queue_draw (scherzo->window);
786 /* Return TRUE to continue to get called in the future. */
791 main (int argc, char *argv[])
793 GtkWidget *drawing_area;
799 gtk_init (&argc, &argv);
801 scherzo.ctx = talloc_new (NULL);
803 scherzo.score = score_create (scherzo.ctx);
804 scherzo.staff_height = 100;
805 score_set_staff_height (scherzo.score, scherzo.staff_height);
807 score_add_brace (scherzo.score, 2);
808 scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
809 scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
811 scherzo.chord = NULL;
813 /* Default to octave 4 for computer keyboard keypresses. */
814 scherzo.keyboard_octave = 4;
816 scherzo.num_notes_pressed = 0;
817 scherzo.notes_pressed = NULL;
819 mnemon_init (&scherzo.mnemon);
820 /* XXX: Should create a default file if one cannot be loaded. */
821 mnemon_load_category (&scherzo.mnemon, "scherzo-notes");
823 scherzo.challenge.note = NULL;
825 _select_challenge (&scherzo);
828 err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
830 fprintf (stderr, "Out of memory.\n");
834 #define MIDI_DEVICE "/dev/midi1"
835 scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
836 if (scherzo.midi_fd < 0) {
837 printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
841 channel = g_io_channel_unix_new (scherzo.midi_fd);
842 g_io_channel_set_encoding (channel, NULL, NULL);
843 g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
846 scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
848 gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 1000, 600);
850 g_signal_connect (scherzo.window, "delete-event",
851 G_CALLBACK (on_delete_event_quit), NULL);
853 drawing_area = gtk_drawing_area_new ();
855 gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
857 g_signal_connect (drawing_area, "expose-event",
858 G_CALLBACK (on_expose_event_draw),
861 g_signal_connect (scherzo.window, "key-press-event",
862 G_CALLBACK (on_key_press_event),
865 g_signal_connect (scherzo.window, "key-release-event",
866 G_CALLBACK (on_key_release_event),
869 gtk_widget_show_all (scherzo.window);
873 mnemon_save (&scherzo.mnemon);
875 snd_midi_event_free (scherzo.snd_midi_event);
877 talloc_free (scherzo.ctx);