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) ((int) (sizeof(arr) / sizeof(arr[0])))
29 #define unused(foo) foo __attribute__((unused))
31 #define MIDI_BUF_SIZE 4096
33 typedef struct challenge
45 typedef struct pitch_group
53 typedef struct scherzo
61 score_staff_t *treble;
65 /* The word "keyboard" here is referring to a "computer
66 * keyboard". Any "piano keyboard" key knows its own octave and
67 * accidental already. */
71 snd_midi_event_t *snd_midi_event;
74 challenge_t challenge;
76 pitch_group_t notes_pressed;
77 pitch_group_t notes_pedaled;
84 /* Forward declarations. */
86 scherzo_press_note (scherzo_t *scherzo, pitch_t pitch);
89 scherzo_release_note (scherzo_t *scherzo, pitch_t pitch);
92 scherzo_press_pedal (scherzo_t *scherzo);
95 scherzo_release_pedal (scherzo_t *scherzo);
98 _judge_pitch (scherzo_t *scherzo, pitch_t pitch);
101 _score_challenge (scherzo_t *scherzo);
104 pitch_group_remove_pitches (pitch_group_t *group);
107 scherzo_update_notes_and_chord (scherzo_t *scherzo);
110 on_delete_event_quit (unused (GtkWidget *widget),
111 unused (GdkEvent *event),
112 unused (gpointer user_data))
116 /* Returning FALSE allows the default handler for delete-event
117 * to proceed to cleanup the widget. */
122 on_expose_event_draw (GtkWidget *widget,
123 unused (GdkEventExpose *expose),
126 scherzo_t *scherzo = user_data;
127 score_t *score = scherzo->score;
129 GtkAllocation allocation;
130 static const int pad = 10;
133 gtk_widget_get_allocation (widget, &allocation);
134 widget_width = allocation.width;
136 cr = gdk_cairo_create (widget->window);
138 /* White background */
139 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
142 /* Add some padding on the sides and top */
143 cairo_translate (cr, pad, scherzo->staff_height);
144 score_set_staff_height (score, scherzo->staff_height);
145 score_set_width (score, widget_width - 2 * pad);
147 score_draw (score, cr);
153 on_key_press_event (GtkWidget *widget,
157 scherzo_t *scherzo = user_data;
159 pitch_t pitch = PITCH_NOT_A_PITCH;
161 if (scherzo->challenge.active)
162 octave = PITCH_OCTAVE (scherzo->challenge.pitch);
164 octave = scherzo->keyboard_octave;
166 switch (key->keyval) {
170 case GDK_KEY_KP_Equal:
171 scherzo->staff_height += 4;
172 gtk_widget_queue_draw (widget);
176 case GDK_KEY_KP_Subtract:
177 scherzo->staff_height -= 4;
178 gtk_widget_queue_draw (widget);
184 /* Map a piano keyboard onto the computer keyboard with white keys
185 * along the home row, (piano C on computer A, up to next octave
186 * higher C on computer K). Black keys are in the expected
187 * positions, (C# is W, D# is E, etc.).
191 pitch = PITCH_LITERAL (C, NATURAL, octave);
195 pitch = PITCH_LITERAL (D, NATURAL, octave);
199 pitch = PITCH_LITERAL (E, NATURAL, octave);
203 pitch = PITCH_LITERAL (F, NATURAL, octave);
207 pitch = PITCH_LITERAL (G, NATURAL, octave);
211 pitch = PITCH_LITERAL (A, NATURAL, octave);
215 pitch = PITCH_LITERAL (B, NATURAL, octave);
219 pitch = PITCH_LITERAL (C, NATURAL, octave + 1);
223 pitch = PITCH_LITERAL (D, NATURAL, octave + 1);
225 case GDK_KEY_semicolon:
227 pitch = PITCH_LITERAL (E, NATURAL, octave + 1);
229 case GDK_KEY_apostrophe:
230 case GDK_KEY_quotedbl:
231 pitch = PITCH_LITERAL (F, NATURAL, octave + 1);
235 pitch = PITCH_LITERAL (C, SHARP, octave);
239 pitch = PITCH_LITERAL (D, SHARP, octave);
243 pitch = PITCH_LITERAL (F, SHARP, octave);
247 pitch = PITCH_LITERAL (G, SHARP, octave);
251 pitch = PITCH_LITERAL (A, SHARP, octave);
255 pitch = PITCH_LITERAL (C, SHARP, octave + 1);
259 pitch = PITCH_LITERAL (D, SHARP, octave + 1);
261 case GDK_KEY_bracketright:
262 case GDK_KEY_braceright:
263 pitch = PITCH_LITERAL (F, SHARP, octave + 1);
274 scherzo->keyboard_octave = key->keyval - GDK_KEY_0;
277 scherzo_press_pedal (scherzo);
280 /* Clear all notes when Return is pressed. */
281 pitch_group_remove_pitches (&scherzo->notes_pressed);
282 pitch_group_remove_pitches (&scherzo->notes_pedaled);
283 scherzo_update_notes_and_chord (scherzo);
287 if (pitch != PITCH_NOT_A_PITCH)
289 pitch = scherzo_key_spell_pitch (&scherzo->key, pitch);
291 scherzo_press_note (scherzo, pitch);
292 _judge_pitch (scherzo, pitch);
293 gtk_widget_queue_draw (scherzo->window);
298 /* Allow an unhandled event to propagate to other handlers. */
303 on_key_release_event (unused (GtkWidget *widget),
307 scherzo_t *scherzo = user_data;
309 pitch_t pitch = PITCH_NOT_A_PITCH;
311 if (scherzo->challenge.active)
312 octave = PITCH_OCTAVE (scherzo->challenge.pitch);
314 octave = scherzo->keyboard_octave;
316 switch (key->keyval) {
319 pitch = PITCH_LITERAL (C, NATURAL, octave);
323 pitch = PITCH_LITERAL (D, NATURAL, octave);
327 pitch = PITCH_LITERAL (E, NATURAL, octave);
331 pitch = PITCH_LITERAL (F, NATURAL, octave);
335 pitch = PITCH_LITERAL (G, NATURAL, octave);
339 pitch = PITCH_LITERAL (A, NATURAL, octave);
343 pitch = PITCH_LITERAL (B, NATURAL, octave);
347 pitch = PITCH_LITERAL (C, NATURAL, octave + 1);
351 pitch = PITCH_LITERAL (D, NATURAL, octave + 1);
353 case GDK_KEY_semicolon:
355 pitch = PITCH_LITERAL (E, NATURAL, octave + 1);
357 case GDK_KEY_apostrophe:
358 case GDK_KEY_quotedbl:
359 pitch = PITCH_LITERAL (F, NATURAL, octave + 1);
363 pitch = PITCH_LITERAL (C, SHARP, octave);
367 pitch = PITCH_LITERAL (D, SHARP, octave);
371 pitch = PITCH_LITERAL (F, SHARP, octave);
375 pitch = PITCH_LITERAL (G, SHARP, octave);
379 pitch = PITCH_LITERAL (A, SHARP, octave);
383 pitch = PITCH_LITERAL (C, SHARP, octave + 1);
387 pitch = PITCH_LITERAL (D, SHARP, octave + 1);
389 case GDK_KEY_bracketright:
390 case GDK_KEY_braceright:
391 pitch = PITCH_LITERAL (F, SHARP, octave + 1);
394 scherzo_release_pedal (scherzo);
398 if (pitch != PITCH_NOT_A_PITCH)
400 pitch = scherzo_key_spell_pitch (&scherzo->key, pitch);
402 scherzo_release_note (scherzo, pitch);
403 _score_challenge (scherzo);
404 gtk_widget_queue_draw (scherzo->window);
410 /* Allow an unhandled event to propagate to other handlers. */
414 /* Return true if 'a' and 'b' sound identical, (even if spelled differently)
416 * This comparison considers octaves as significant. So Bb and A# in
417 * the same octave are considered enharmonic, but Bb and A# in
418 * different octaves are not. */
420 _pitches_are_enharmonic (pitch_t a, pitch_t b)
422 return pitch_to_midi (a) == pitch_to_midi (b);
425 /* Determine a chord name (if any) from the current notes pressed */
427 typedef struct analyzed_pitch {
428 /* Original note being analzyed. */
431 /* Absolute pitch (expressed as midi number). */
434 /* Pitch relative to bass note. */
439 _compare_analyzed_pitch_by_midi_pitch (const void *va, const void *vb)
441 const analyzed_pitch_t *a = va, *b = vb;
443 return a->midi_pitch - b->midi_pitch;
447 _compare_analyzed_pitch_by_relative_pitch (const void *va, const void *vb)
449 const analyzed_pitch_t *a = va, *b = vb;
451 return a->relative_pitch - b->relative_pitch;
454 typedef struct modified_degree
461 _modified_degree_to_half_steps (const modified_degree_t *degree)
464 int scale_degree = degree->degree;
466 /* Restrict to actual degrees within a scale. */
467 if (scale_degree > 7)
468 scale_degree = (scale_degree % 8) + 1;
470 /* Number of half steps from root to specified degree within a
472 switch (scale_degree) {
495 fprintf (stderr, "Internal: Invalid degree %d\n", degree->degree);
500 return half_steps + degree->modification;
504 _chord_signature_matches (analyzed_pitch_t *pitches,
506 modified_degree_t *degrees,
511 #define MAX_DEGREES 6
512 int relative_pitches[MAX_DEGREES];
515 assert (num_degrees <= MAX_DEGREES);
517 if (num_pitches != num_degrees)
520 if (inversion >= num_degrees)
523 /* We never spell simple intervals as inversions. */
524 if (num_degrees == 2 && inversion > 0)
527 for (i = 0; i < num_degrees; i++) {
528 /* The num_degrees is in the addition just to ensure all
529 * inputs to the modulus operator remain positive. */
530 int index = (i + num_degrees - inversion) % num_degrees;
532 /* Again, adding a 12 to keep things positive. */
533 relative_pitches[index] =
535 _modified_degree_to_half_steps (°rees[i]) -
536 _modified_degree_to_half_steps (°rees[inversion])) % 12;
540 for (i = 0; i < num_pitches; i++)
541 if (pitches[i].relative_pitch != relative_pitches[i])
544 root_index = (num_pitches - inversion) % num_pitches;
545 *root = pitches[root_index].pitch;
550 typedef struct chord_signature
553 modified_degree_t degrees[MAX_DEGREES];
557 /* XXX: The superscript rise value should be relative to the current
558 * font size. Best would be for pango to support this. See:
560 * https://bugzilla.gnome.org/show_bug.cgi?id=708780
562 * For now, these are emipirically-derived values that look reasonable
563 * at the default size that score.c is using to draw the staff. If the
564 * user scales the staff up or down then these will look wrong.
567 #define SUP "<span font='33' rise='30000'>"
568 #define PUS "</span>"
570 chord_signature_t signatures[] = {
572 { 1, {{1, 0}}, "P8" },
575 { 2, {{1, 0}, {2, -1}}, "m2" },
576 { 2, {{1, 0}, {2, 0}}, "M2" },
577 { 2, {{1, 0}, {3, -1}}, "m3" },
578 { 2, {{1, 0}, {3, 0}}, "M3" },
579 { 2, {{1, 0}, {4, 0}}, "P4" },
580 { 2, {{1, 0}, {5, -1}}, "Tri-tone" },
581 { 2, {{1, 0}, {5, 0}}, "P5" },
582 { 2, {{1, 0}, {6, -1}}, "m6" },
583 { 2, {{1, 0}, {6, 0}}, "M6" },
584 { 2, {{1, 0}, {7, -1}}, "m7" },
585 { 2, {{1, 0}, {7, 0}}, "M7" },
588 { 3, {{1, 0}, {4, 0}, {5, 0}}, "sus" },
589 { 3, {{1, 0}, {3, 0}, {5, +1}}, SUP "+" PUS },
590 { 3, {{1, 0}, {3, 0}, {5, 0}}, "" },
591 { 3, {{1, 0}, {3, -1}, {5, 0}}, "m" },
592 { 3, {{1, 0}, {3, -1}, {5, -1}}, "°" },
593 { 3, {{1, 0}, {2, 0}, {5, 0}}, "msus2" },
595 /* Seventh chords with no 3rd */
596 { 3, {{1, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS },
597 { 3, {{1, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS },
598 { 3, {{1, 0}, {5, 0}, {7, 0}}, "M7" },
599 { 3, {{1, 0}, {5, 0}, {7, -1}}, "7" },
600 { 3, {{1, 0}, {5, -1}, {7, -1}}, "7♭5" },
602 /* Seventh chords with no 5th */
603 { 3, {{1, 0}, {4, 0}, {7, 0}}, "M7sus" },
604 { 3, {{1, 0}, {4, 0}, {7, -1}}, "7sus" },
605 { 3, {{1, 0}, {3, 0}, {7, 0}}, "M7" },
606 { 3, {{1, 0}, {3, 0}, {7, -1}}, "7" },
607 { 3, {{1, 0}, {3, -1}, {7, 0}}, "m" SUP "M7" PUS },
608 { 3, {{1, 0}, {3, -1}, {7, -1}}, "m7" },
609 { 3, {{1, 0}, {2, 0}, {7, 0}}, "m" SUP "M7" PUS "sus2" },
610 { 3, {{1, 0}, {2, 0}, {7, -1}}, "m7sus2" },
613 { 4, {{1, 0}, {4, 0}, {5, 0}, {6, 0}}, "6sus" },
614 { 4, {{1, 0}, {3, 0}, {5, 0}, {6, 0}}, "6" }, /* Ambiguous with m7 */
615 { 4, {{1, 0}, {3, -1}, {5, 0}, {6, 0}}, "m6" },
616 { 4, {{1, 0}, {2, 0}, {5, 0}, {6, 0}}, "m6sus2" },
619 { 4, {{1, 0}, {4, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS "sus" },
620 { 4, {{1, 0}, {4, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS "sus" },
621 { 4, {{1, 0}, {4, 0}, {5, 0}, {7, 0}}, "M7sus" },
622 { 4, {{1, 0}, {4, 0}, {5, 0}, {7, -1}}, "7sus" },
623 { 4, {{1, 0}, {4, 0}, {5, -1}, {7, -1}}, "7♭5sus" },
624 { 4, {{1, 0}, {3, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS },
625 { 4, {{1, 0}, {3, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS },
626 { 4, {{1, 0}, {3, 0}, {5, 0}, {7, 0}}, "M7" },
627 { 4, {{1, 0}, {3, 0}, {5, 0}, {7, -1}}, "7" },
628 { 4, {{1, 0}, {3, 0}, {5, -1}, {7, -1}}, "7♭5" },
629 { 4, {{1, 0}, {3, -1}, {5, 0}, {7, 0}}, "m" SUP "M7" PUS },
630 { 4, {{1, 0}, {3, -1}, {5, 0}, {7, -1}}, "m7" },
631 { 4, {{1, 0}, {3, -1}, {5, -1}, {7, 0}}, "°" SUP "M7" PUS },
632 { 4, {{1, 0}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "7" PUS },
633 { 4, {{1, 0}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "7" PUS },
634 { 4, {{1, 0}, {2, 0}, {5, 0}, {7, 0}}, "m" SUP "M7" PUS "sus2" },
635 { 4, {{1, 0}, {2, 0}, {5, 0}, {7, -1}}, "m7sus2" },
636 { 4, {{1, 0}, {2, 0}, {5, -1}, {7, 0}}, "°" SUP "M7" PUS "sus2" },
637 { 4, {{1, 0}, {2, 0}, {5, -1}, {7, -1}}, "𝆩" SUP "7" PUS "sus2" },
638 { 4, {{1, 0}, {2, 0}, {5, -1}, {7, -2}}, "°" SUP "7" PUS "sus2" }, /* Ambiguous with 7 */
640 /* The sorting for chords with degree higher than 9 is funny
641 * to keep the array in degree order after reducing each
642 * degree to an actual scale degree, (9 -> 2, 11 -> 4, 13 ->
645 /* Ninth chords voiced with no 5th */
646 { 4, {{1, 0}, {9, 0}, {4, 0}, {7, 0}}, "M9sus" },
647 { 4, {{1, 0}, {9, 0}, {4, 0}, {7, -1}}, "9sus" },
648 { 4, {{1, 0}, {9, 0}, {3, 0}, {7, 0}}, "M9" },
649 { 4, {{1, 0}, {9, 0}, {3, 0}, {7, -1}}, "9" },
650 { 4, {{1, 0}, {9, 0}, {3, -1}, {7, 0}}, "m" SUP "M9" PUS },
651 { 4, {{1, 0}, {9, 0}, {3, -1}, {7, -1}}, "m9" },
652 { 4, {{1, 0}, {9, -1}, {3, -1}, {7, -1}}, "m♭9" },
653 { 4, {{1, 0}, {9, 0}, {3, -1}, {6, 0}}, "m6/9" },
654 { 4, {{1, 0}, {9, -1}, {3, -1}, {6, 0}}, "m6/♭9" },
657 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {6, 0}}, "6/9" },
658 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {6, 0}}, "m6/9" },
659 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, 0}, {6, 0}}, "m6/♭9" },
661 /* Seventh plus altered 9 */
662 { 5, {{1, 0}, {9, +1}, {3, 0}, {5, 0}, {7, -1}}, "7♯9" },
663 { 5, {{1, 0}, {9, -1}, {3, 0}, {5, 0}, {7, -1}}, "7♭9" },
664 { 5, {{1, 0}, {9, +1}, {3, 0}, {5, -1}, {7, -1}}, "7♭5♯9" },
665 { 5, {{1, 0}, {9, -1}, {3, 0}, {5, -1}, {7, -1}}, "7♭5♭9" },
668 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, +1}, {7, 0}}, SUP "+M9" PUS "sus" },
669 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, +1}, {7, -1}}, SUP "+9" PUS "sus" },
670 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, 0}, {7, 0}}, "M9sus" },
671 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, 0}, {7, -1}}, "9sus" },
672 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, +1}, {7, 0}}, SUP "+M9" PUS },
673 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, +1}, {7, -1}}, SUP "+9" PUS },
674 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {7, 0}}, "M9" },
675 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {7, -1}}, "9" },
676 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {7, 0}}, "m" SUP "M9" PUS },
677 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {7, -1}}, "m9" },
678 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, 0}, {7, -1}}, "m♭9" },
679 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, -1}, {7, 0}}, "M9" SUP "♭5" PUS },
680 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, -1}, {7, -1}}, "9" SUP "♭5" PUS },
681 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, 0}}, "m" SUP "M9♭5" PUS },
682 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "9" PUS },
683 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "♭9" PUS },
684 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "9" PUS },
685 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "♭9" PUS },
687 /* Eleventh chords */
688 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, +1}, {7, 0}}, SUP "+M11" PUS },
689 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, +1}, {7, -1}}, SUP "+11" PUS },
690 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, 0}, {7, 0}}, "M11" },
691 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, 0}, {7, -1}}, "11" },
692 { 6, {{1, 0}, {9, 0}, {3, -1}, {11, 0}, {5, 0}, {7, 0}}, "m" SUP "M11" PUS },
693 { 6, {{1, 0}, {9, 0}, {3, -1}, {11, 0}, {5, 0}, {7, -1}}, "m11" },
694 { 6, {{1, 0}, {9, -1}, {3, -1}, {11, 0}, {5, -1}, {7, -1}}, "𝆩" SUP "11" PUS },
695 { 6, {{1, 0}, {9, -1}, {3, -1}, {11, -1}, {5, -1}, {7, -2}}, "°" SUP "11" PUS }
699 _spell_chord_by_signature (pitch_group_t *chord,
700 chord_signature_t *signature,
703 int i, degree, note_half_steps, degree_half_steps;
705 for (i = 0; i < chord->num_pitches; i++) {
706 note_half_steps = pitch_from_root_in_half_steps (chord->pitches[i],
708 for (degree = 0; degree < signature->num_degrees; degree++) {
709 degree_half_steps = _modified_degree_to_half_steps (&signature->degrees[degree]);
710 if (note_half_steps == degree_half_steps) {
711 chord->pitches[i] = pitch_spell_as_degree (chord->pitches[i],
713 signature->degrees[degree].degree);
717 if (note_half_steps != degree_half_steps) {
718 fprintf (stderr, "Internal error: Chord and degree mis-match\n");
725 _analyze_chord (pitch_group_t *chord,
726 chord_signature_t **signature_ret,
729 void *local = talloc_new (NULL);
730 analyzed_pitch_t *pitches;
731 int i, j, num_pitches;
732 int bass_pitch, inversion;
734 chord_signature_t *match = NULL;
736 /* Default to no-match for any early return */
737 *signature_ret = NULL;
740 num_pitches = chord->num_pitches;
742 if (chord->num_pitches <= 1)
745 pitches = talloc_array (local, analyzed_pitch_t, num_pitches);
749 for (i = 0; i < num_pitches; i++) {
750 pitch_t pitch = chord->pitches[i];
751 pitches[i].pitch = pitch;
752 pitches[i].midi_pitch = pitch_to_midi (pitch);
753 /* Relative pitch will be filled in after sorting. */
754 pitches[i].relative_pitch = 0;
757 /* First, sort by midi pitch to find the bass note. */
758 qsort (pitches, num_pitches, sizeof (analyzed_pitch_t),
759 _compare_analyzed_pitch_by_midi_pitch);
761 bass_pitch = pitches[0].midi_pitch;
763 /* With the bass note identified, we can find all relative pitches. */
764 for (i = 0; i < num_pitches; i++) {
765 pitches[i].relative_pitch = pitches[i].midi_pitch - bass_pitch;
766 while (pitches[i].relative_pitch >= 12)
767 pitches[i].relative_pitch -= 12;
770 /* Now, sort again by relative pitch. */
771 qsort (pitches, num_pitches, sizeof (analyzed_pitch_t),
772 _compare_analyzed_pitch_by_relative_pitch);
774 /* Finally, eliminate all duplicate notes. */
775 for (i = 0; i < num_pitches - 1; i++) {
776 if (pitches[i+1].relative_pitch == pitches[i].relative_pitch) {
778 while (j < num_pitches &&
779 pitches[j].relative_pitch == pitches[i].relative_pitch)
783 /* The loop incremented j one past the last
784 * duplicate. Decrement so that it points to the
785 * last duplicate (and is guaranteed to not exceed
786 * the array bounds).*/
789 if (j < num_pitches - 1) {
790 memmove (&pitches[i+1], &pitches[j+1],
791 (num_pitches - j) * sizeof (analyzed_pitch_t));
794 num_pitches -= (j - i);
798 for (inversion = 0; inversion < 4; inversion++) {
799 for (i = 0; i < ARRAY_SIZE (signatures); i++) {
800 if (_chord_signature_matches (pitches, num_pitches,
801 signatures[i].degrees,
802 signatures[i].num_degrees,
805 match = &signatures[i];
813 *signature_ret = match;
814 for (i = 0; i < chord->num_pitches; i++) {
815 if (chord->pitches[i] == root) {
816 *root_ret = &chord->pitches[i];
827 pitch_group_init (void *ctx, pitch_group_t *group)
830 group->pitches = NULL;
832 group->num_pitches = 0;
836 pitch_group_add_pitch (pitch_group_t *group, pitch_t pitch)
840 /* Do nothing if pitch is already in group. */
841 for (i = 0; i < group->num_pitches; i++)
842 if (group->pitches[i] == pitch)
845 group->num_pitches++;
847 if (group->num_pitches > group->size) {
849 group->pitches = talloc_realloc (group->ctx, group->pitches,
850 pitch_t, group->size);
852 if (group->pitches == NULL) {
853 fprintf (stderr, "Out of memory.\n");
858 group->pitches[group->num_pitches - 1] = pitch;
862 pitch_group_remove_pitch_at (pitch_group_t *group, int i)
864 if (i > group->num_pitches) {
865 fprintf (stderr, "Internal error: Cannot remove pitch %d from group with only %d %s\n",
866 i, group->num_pitches,
867 group->num_pitches == 1 ? "pitch" : "pitches");
871 if (i < group->num_pitches - 1) {
872 memmove (group->pitches + i, group->pitches + i + 1,
873 (group->num_pitches - 1 - i) * sizeof (pitch_t));
876 group->num_pitches--;
879 /* Remove a 'pitch' from 'group', (including any enharmonic pitch)
880 * See also: pitch_group_remove_pitch
883 pitch_group_remove_pitch_enharmonic (pitch_group_t *group, pitch_t pitch)
887 for (i = 0; i < group->num_pitches; i++)
888 if (_pitches_are_enharmonic (group->pitches[i], pitch))
889 pitch_group_remove_pitch_at (group, i);
892 /* Remove an exactly-matching pitch from 'group'
893 * See also: pitch_group_remove_pitch_enharmonic
896 pitch_group_remove_pitch (pitch_group_t *group, pitch_t pitch)
900 for (i = 0; i < group->num_pitches; i++)
901 if (group->pitches[i] == pitch)
902 pitch_group_remove_pitch_at (group, i);
906 /* Remove all pitches from a pitch_group_t */
908 pitch_group_remove_pitches (pitch_group_t *group)
910 talloc_free (group->pitches);
911 pitch_group_init (group->ctx, group);
915 scherzo_update_notes_and_chord (scherzo_t *scherzo)
917 if (scherzo->notes_pressed.num_pitches == 0 &&
918 scherzo->notes_pedaled.num_pitches == 0)
920 score_remove_notes (scherzo->score);
922 score_remove_chords (scherzo->score);
924 gtk_widget_queue_draw (scherzo->window);
929 scherzo_press_note (scherzo_t *scherzo, pitch_t pitch)
931 void *local = talloc_new (NULL);
932 pitch_group_t *chord;
933 chord_signature_t *signature;
934 char *chord_name = NULL;
938 /* Do nothing if this note is already pressed. */
939 for (i = 0; i < scherzo->notes_pressed.num_pitches; i++)
940 if (scherzo->notes_pressed.pitches[i] == pitch)
943 pitch_group_add_pitch (&scherzo->notes_pressed, pitch);
945 if (scherzo->pedal_pressed)
946 pitch_group_add_pitch (&scherzo->notes_pedaled, pitch);
948 /* Remove all notes/chords from score, then add current notes. */
949 score_remove_notes (scherzo->score);
950 score_remove_chords (scherzo->score);
952 if (scherzo->pedal_pressed)
953 chord = &scherzo->notes_pedaled;
955 chord = &scherzo->notes_pressed;
957 _analyze_chord (chord, &signature, &root);
960 /* Don't print root pitch for octaves and inversions,
961 * (since a pitch name alone looks like a major triad) */
962 if (signature->num_degrees < 3) {
963 chord_name = talloc_strdup (local, signature->name);
965 _spell_chord_by_signature (&scherzo->notes_pressed,
967 _spell_chord_by_signature (&scherzo->notes_pedaled,
970 chord_name = talloc_asprintf (local, "%s%s",
971 pitch_string (*root),
974 } else if (chord->num_pitches > 2) {
975 chord_name = talloc_strdup (local, "?");
979 scherzo->chord = score_add_chord (scherzo->treble, chord_name);
981 for (i = 0; i < scherzo->notes_pressed.num_pitches; i++)
983 score_add_note (scherzo->score, scherzo->notes_pressed.pitches[i],
984 SCORE_DURATION_WHOLE);
987 for (i = 0; i < scherzo->notes_pedaled.num_pitches; i++)
989 score_add_note (scherzo->score, scherzo->notes_pedaled.pitches[i],
990 SCORE_DURATION_WHOLE);
997 scherzo_release_note (scherzo_t *scherzo, pitch_t pitch)
999 pitch_group_remove_pitch_enharmonic (&scherzo->notes_pressed, pitch);
1001 if (scherzo->notes_pressed.num_pitches == 0)
1002 scherzo_update_notes_and_chord (scherzo);
1006 scherzo_press_note_midi (scherzo_t *scherzo, unsigned char midi_note)
1008 pitch_t pitch = pitch_from_midi (midi_note);
1010 scherzo_press_note (scherzo, pitch);
1016 scherzo_release_note_midi (scherzo_t *scherzo, unsigned char midi_note)
1018 scherzo_release_note (scherzo, pitch_from_midi (midi_note));
1022 scherzo_press_pedal (scherzo_t *scherzo)
1026 if (scherzo->pedal_pressed)
1029 scherzo->pedal_pressed = 1;
1031 /* Copy all pressed notes to pedaled notes */
1032 for (i = 0; i < scherzo->notes_pressed.num_pitches; i++)
1033 pitch_group_add_pitch (&scherzo->notes_pedaled, scherzo->notes_pressed.pitches[i]);
1037 scherzo_release_pedal (scherzo_t *scherzo)
1041 if (! scherzo->pedal_pressed)
1044 /* Remove all previously pedaled notes. */
1045 for (i = scherzo->notes_pedaled.num_pitches - 1; i >=0; i--)
1046 pitch_group_remove_pitch_at (&scherzo->notes_pedaled, i);
1048 scherzo->pedal_pressed = 0;
1050 scherzo_update_notes_and_chord (scherzo);
1054 _select_challenge (scherzo_t *scherzo)
1056 category_t *category_unused;
1057 bool_t introduced_unused;
1059 challenge_t *challenge = &scherzo->challenge;
1060 pitch_name_t pitch_name;
1064 mnemon_select_item (&scherzo->mnemon,
1066 &challenge->item_index,
1068 &introduced_unused);
1070 item = challenge->bin->items[challenge->item_index];
1072 s = item->challenge;
1073 if (strncmp (s, "treble:", 7) == 0) {
1075 challenge->staff = scherzo->treble;
1076 } else if (strncmp (s, "bass:", 5) == 0) {
1078 challenge->staff = scherzo->bass;
1081 "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
1088 pitch_name = PITCH_NAME_C;
1091 pitch_name = PITCH_NAME_D;
1094 pitch_name = PITCH_NAME_E;
1097 pitch_name = PITCH_NAME_F;
1100 pitch_name = PITCH_NAME_G;
1103 pitch_name = PITCH_NAME_A;
1106 pitch_name = PITCH_NAME_B;
1109 fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
1114 if (*s < '0' || *s > '9') {
1115 fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
1121 challenge->pitch = PITCH (pitch_name, PITCH_ACCIDENTAL_NATURAL, octave);
1123 challenge->active = 1;
1124 challenge->satisfied = 0;
1125 challenge->mistaken = 0;
1128 /* Determine whether the user hit the correct note. */
1130 _judge_pitch (scherzo_t *scherzo, pitch_t pitch)
1132 challenge_t *challenge = &scherzo->challenge;
1134 if (! challenge->active)
1137 if (pitch == challenge->pitch)
1138 challenge->satisfied = 1;
1140 challenge->mistaken = 1;
1143 /* If the user got the right note (eventually), then score it in
1144 * mnemon and show the next note. */
1146 _score_challenge (scherzo_t *scherzo)
1148 challenge_t *challenge = &scherzo->challenge;
1150 if (! challenge->active)
1153 if (! challenge->satisfied)
1156 mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
1157 ! challenge->mistaken);
1159 _select_challenge (scherzo);
1163 on_midi_input (unused (GIOChannel *channel),
1164 unused (GIOCondition condition),
1167 unsigned char buf[MIDI_BUF_SIZE], *next;
1168 scherzo_t *scherzo = user_data;
1170 snd_seq_event_t event;
1172 int need_redraw = FALSE;
1174 remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
1180 consumed = snd_midi_event_encode (scherzo->snd_midi_event,
1181 next, remaining, &event);
1183 remaining -= consumed;
1186 switch (event.type) {
1187 case SND_SEQ_EVENT_NONE:
1188 /* Incomplete event. Nothing to do. */
1190 case SND_SEQ_EVENT_NOTEON:
1191 pitch = scherzo_press_note_midi (scherzo, event.data.note.note);
1192 _judge_pitch (scherzo, pitch);
1195 case SND_SEQ_EVENT_NOTEOFF:
1196 scherzo_release_note_midi (scherzo, event.data.note.note);
1197 _score_challenge (scherzo);
1200 case SND_SEQ_EVENT_CLOCK:
1201 /* Ignore for now as my piano sends a constant stream of these. */
1203 case SND_SEQ_EVENT_SENSING:
1204 /* Ignore for now as my piano sends a constant stream of these. */
1206 case SND_SEQ_EVENT_CONTROLLER:
1207 /* XXX: My piano gives me 64 for the sustain pedal, is
1208 * that universal? */
1209 if (event.data.control.param == 64) {
1210 if (event.data.control.value == 0)
1211 scherzo_release_pedal (scherzo);
1213 scherzo_press_pedal (scherzo);
1215 fprintf (stderr, "Fixme: Unhandled MIDI Control event (param=%d, value=%d)\n",
1216 event.data.control.param, event.data.control.value);
1220 fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
1227 gtk_widget_queue_draw (scherzo->window);
1229 /* Return TRUE to continue to get called in the future. */
1234 main (int argc, char *argv[])
1236 GtkWidget *drawing_area;
1240 srand (time (NULL));
1242 gtk_init (&argc, &argv);
1244 scherzo.ctx = talloc_new (NULL);
1246 scherzo.score = score_create (scherzo.ctx);
1247 scherzo.staff_height = 100;
1248 score_set_staff_height (scherzo.score, scherzo.staff_height);
1250 score_add_brace (scherzo.score, 2);
1251 scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
1252 scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
1254 scherzo.chord = NULL;
1256 /* Default to octave 4 for computer keyboard keypresses. */
1257 scherzo.keyboard_octave = 4;
1259 pitch_group_init (scherzo.ctx, &scherzo.notes_pressed);
1260 pitch_group_init (scherzo.ctx, &scherzo.notes_pedaled);
1262 scherzo.pedal_pressed = 0;
1264 /* Default to key of C Major, naturally. */
1265 scherzo_key_init (&scherzo.key, PITCH_CLASS_LITERAL (C, NATURAL));
1266 score_set_key (scherzo.score, scherzo.key.pitch);
1268 mnemon_init (&scherzo.mnemon);
1269 /* XXX: Should create a default file if one cannot be loaded. */
1270 mnemon_load_category (&scherzo.mnemon, "scherzo-notes");
1272 scherzo.challenge.active = 0;
1274 _select_challenge (&scherzo);
1277 err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
1279 fprintf (stderr, "Out of memory.\n");
1283 #define MIDI_DEVICE "/dev/midi1"
1284 scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
1285 if (scherzo.midi_fd < 0) {
1286 printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
1288 GIOChannel *channel;
1290 channel = g_io_channel_unix_new (scherzo.midi_fd);
1291 g_io_channel_set_encoding (channel, NULL, NULL);
1292 g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
1295 scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1297 gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 1000, 600);
1299 g_signal_connect (scherzo.window, "delete-event",
1300 G_CALLBACK (on_delete_event_quit), NULL);
1302 drawing_area = gtk_drawing_area_new ();
1304 gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
1306 g_signal_connect (drawing_area, "expose-event",
1307 G_CALLBACK (on_expose_event_draw),
1310 g_signal_connect (scherzo.window, "key-press-event",
1311 G_CALLBACK (on_key_press_event),
1314 g_signal_connect (scherzo.window, "key-release-event",
1315 G_CALLBACK (on_key_release_event),
1318 gtk_widget_show_all (scherzo.window);
1322 mnemon_save (&scherzo.mnemon);
1324 snd_midi_event_free (scherzo.snd_midi_event);
1326 talloc_free (scherzo.ctx);