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 pitch_group_remove_pitches (pitch_group_t *group);
101 scherzo_update_notes_and_chord (scherzo_t *scherzo);
104 on_delete_event_quit (unused (GtkWidget *widget),
105 unused (GdkEvent *event),
106 unused (gpointer user_data))
110 /* Returning FALSE allows the default handler for delete-event
111 * to proceed to cleanup the widget. */
116 on_expose_event_draw (GtkWidget *widget,
117 unused (GdkEventExpose *expose),
120 scherzo_t *scherzo = user_data;
121 score_t *score = scherzo->score;
123 GtkAllocation allocation;
124 static const int pad = 10;
127 gtk_widget_get_allocation (widget, &allocation);
128 widget_width = allocation.width;
130 cr = gdk_cairo_create (widget->window);
132 /* White background */
133 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
136 /* Add some padding on the sides and top */
137 cairo_translate (cr, pad, scherzo->staff_height);
138 score_set_staff_height (score, scherzo->staff_height);
139 score_set_width (score, widget_width - 2 * pad);
141 score_draw (score, cr);
147 on_key_press_event (GtkWidget *widget,
151 scherzo_t *scherzo = user_data;
153 pitch_t pitch = PITCH_NOT_A_PITCH;
155 if (scherzo->challenge.active)
156 octave = PITCH_OCTAVE (scherzo->challenge.pitch);
158 octave = scherzo->keyboard_octave;
160 switch (key->keyval) {
164 case GDK_KEY_KP_Equal:
165 scherzo->staff_height += 4;
166 gtk_widget_queue_draw (widget);
170 case GDK_KEY_KP_Subtract:
171 scherzo->staff_height -= 4;
172 gtk_widget_queue_draw (widget);
178 /* Map a piano keyboard onto the computer keyboard with white keys
179 * along the home row, (piano C on computer A, up to next octave
180 * higher C on computer K). Black keys are in the expected
181 * positions, (C# is W, D# is E, etc.).
185 pitch = PITCH_LITERAL (C, NATURAL, octave);
189 pitch = PITCH_LITERAL (D, NATURAL, octave);
193 pitch = PITCH_LITERAL (E, NATURAL, octave);
197 pitch = PITCH_LITERAL (F, NATURAL, octave);
201 pitch = PITCH_LITERAL (G, NATURAL, octave);
205 pitch = PITCH_LITERAL (A, NATURAL, octave);
209 pitch = PITCH_LITERAL (B, NATURAL, octave);
213 pitch = PITCH_LITERAL (C, NATURAL, octave + 1);
217 pitch = PITCH_LITERAL (D, NATURAL, octave + 1);
219 case GDK_KEY_semicolon:
221 pitch = PITCH_LITERAL (E, NATURAL, octave + 1);
223 case GDK_KEY_apostrophe:
224 case GDK_KEY_quotedbl:
225 pitch = PITCH_LITERAL (F, NATURAL, octave + 1);
229 pitch = PITCH_LITERAL (C, SHARP, octave);
233 pitch = PITCH_LITERAL (D, SHARP, octave);
237 pitch = PITCH_LITERAL (F, SHARP, octave);
241 pitch = PITCH_LITERAL (G, SHARP, octave);
245 pitch = PITCH_LITERAL (A, SHARP, octave);
249 pitch = PITCH_LITERAL (C, SHARP, octave + 1);
253 pitch = PITCH_LITERAL (D, SHARP, octave + 1);
255 case GDK_KEY_bracketright:
256 case GDK_KEY_braceright:
257 pitch = PITCH_LITERAL (F, SHARP, octave + 1);
268 scherzo->keyboard_octave = key->keyval - GDK_KEY_0;
271 scherzo_press_pedal (scherzo);
274 /* Clear all notes when Return is pressed. */
275 pitch_group_remove_pitches (&scherzo->notes_pressed);
276 pitch_group_remove_pitches (&scherzo->notes_pedaled);
277 scherzo_update_notes_and_chord (scherzo);
281 if (pitch != PITCH_NOT_A_PITCH)
283 scherzo_press_note (scherzo, pitch);
288 /* Allow an unhandled event to propagate to other handlers. */
293 on_key_release_event (unused (GtkWidget *widget),
297 scherzo_t *scherzo = user_data;
299 pitch_t pitch = PITCH_NOT_A_PITCH;
301 if (scherzo->challenge.active)
302 octave = PITCH_OCTAVE (scherzo->challenge.pitch);
304 octave = scherzo->keyboard_octave;
306 switch (key->keyval) {
309 pitch = PITCH_LITERAL (C, NATURAL, octave);
313 pitch = PITCH_LITERAL (D, NATURAL, octave);
317 pitch = PITCH_LITERAL (E, NATURAL, octave);
321 pitch = PITCH_LITERAL (F, NATURAL, octave);
325 pitch = PITCH_LITERAL (G, NATURAL, octave);
329 pitch = PITCH_LITERAL (A, NATURAL, octave);
333 pitch = PITCH_LITERAL (B, NATURAL, octave);
337 pitch = PITCH_LITERAL (C, NATURAL, octave + 1);
341 pitch = PITCH_LITERAL (D, NATURAL, octave + 1);
343 case GDK_KEY_semicolon:
345 pitch = PITCH_LITERAL (E, NATURAL, octave + 1);
347 case GDK_KEY_apostrophe:
348 case GDK_KEY_quotedbl:
349 pitch = PITCH_LITERAL (F, NATURAL, octave + 1);
353 pitch = PITCH_LITERAL (C, SHARP, octave);
357 pitch = PITCH_LITERAL (D, SHARP, octave);
361 pitch = PITCH_LITERAL (F, SHARP, octave);
365 pitch = PITCH_LITERAL (G, SHARP, octave);
369 pitch = PITCH_LITERAL (A, SHARP, octave);
373 pitch = PITCH_LITERAL (C, SHARP, octave + 1);
377 pitch = PITCH_LITERAL (D, SHARP, octave + 1);
379 case GDK_KEY_bracketright:
380 case GDK_KEY_braceright:
381 pitch = PITCH_LITERAL (F, SHARP, octave + 1);
384 scherzo_release_pedal (scherzo);
388 if (pitch != PITCH_NOT_A_PITCH)
390 scherzo_release_note (scherzo, pitch);
396 /* Allow an unhandled event to propagate to other handlers. */
400 /* Return true if 'a' and 'b' sound identical, (even if spelled differently)
402 * This comparison considers octaves as significant. So Bb and A# in
403 * the same octave are considered enharmonic, but Bb and A# in
404 * different octaves are not. */
406 _pitches_are_enharmonic (pitch_t a, pitch_t b)
408 return pitch_to_midi (a) == pitch_to_midi (b);
411 /* Determine a chord name (if any) from the current notes pressed */
413 typedef struct analyzed_pitch {
414 /* Original note being analzyed. */
417 /* Absolute pitch (expressed as midi number). */
420 /* Pitch relative to bass note. */
425 _compare_analyzed_pitch_by_midi_pitch (const void *va, const void *vb)
427 const analyzed_pitch_t *a = va, *b = vb;
429 return a->midi_pitch - b->midi_pitch;
433 _compare_analyzed_pitch_by_relative_pitch (const void *va, const void *vb)
435 const analyzed_pitch_t *a = va, *b = vb;
437 return a->relative_pitch - b->relative_pitch;
440 typedef struct modified_degree
447 _modified_degree_to_half_steps (const modified_degree_t *degree)
450 int scale_degree = degree->degree;
452 /* Restrict to actual degrees within a scale. */
453 if (scale_degree > 7)
454 scale_degree = (scale_degree % 8) + 1;
456 /* Number of half steps from root to specified degree within a
458 switch (scale_degree) {
481 fprintf (stderr, "Internal: Invalid degree %d\n", degree->degree);
486 return half_steps + degree->modification;
490 _chord_signature_matches (analyzed_pitch_t *pitches,
492 modified_degree_t *degrees,
497 #define MAX_DEGREES 6
498 int relative_pitches[MAX_DEGREES];
501 assert (num_degrees <= MAX_DEGREES);
503 if (num_pitches != num_degrees)
506 if (inversion >= num_degrees)
509 /* We never spell simple intervals as inversions. */
510 if (num_degrees == 2 && inversion > 0)
513 for (i = 0; i < num_degrees; i++) {
514 /* The num_degrees is in the addition just to ensure all
515 * inputs to the modulus operator remain positive. */
516 int index = (i + num_degrees - inversion) % num_degrees;
518 /* Again, adding a 12 to keep things positive. */
519 relative_pitches[index] =
521 _modified_degree_to_half_steps (°rees[i]) -
522 _modified_degree_to_half_steps (°rees[inversion])) % 12;
526 for (i = 0; i < num_pitches; i++)
527 if (pitches[i].relative_pitch != relative_pitches[i])
530 root_index = (num_pitches - inversion) % num_pitches;
531 *root = pitches[root_index].pitch;
536 typedef struct chord_signature
539 modified_degree_t degrees[MAX_DEGREES];
543 /* XXX: The superscript rise value should be relative to the current
544 * font size. Best would be for pango to support this. See:
546 * https://bugzilla.gnome.org/show_bug.cgi?id=708780
548 * For now, these are emipirically-derived values that look reasonable
549 * at the default size that score.c is using to draw the staff. If the
550 * user scales the staff up or down then these will look wrong.
553 #define SUP "<span font='33' rise='30000'>"
554 #define PUS "</span>"
556 chord_signature_t signatures[] = {
558 { 1, {{1, 0}}, "P8" },
561 { 2, {{1, 0}, {2, -1}}, "m2" },
562 { 2, {{1, 0}, {2, 0}}, "M2" },
563 { 2, {{1, 0}, {3, -1}}, "m3" },
564 { 2, {{1, 0}, {3, 0}}, "M3" },
565 { 2, {{1, 0}, {4, 0}}, "P4" },
566 { 2, {{1, 0}, {5, -1}}, "Tri-tone" },
567 { 2, {{1, 0}, {5, 0}}, "P5" },
568 { 2, {{1, 0}, {6, -1}}, "m6" },
569 { 2, {{1, 0}, {6, 0}}, "M6" },
570 { 2, {{1, 0}, {7, -1}}, "m7" },
571 { 2, {{1, 0}, {7, 0}}, "M7" },
574 { 3, {{1, 0}, {4, 0}, {5, 0}}, "sus" },
575 { 3, {{1, 0}, {3, 0}, {5, +1}}, SUP "+" PUS },
576 { 3, {{1, 0}, {3, 0}, {5, 0}}, "" },
577 { 3, {{1, 0}, {3, -1}, {5, 0}}, "m" },
578 { 3, {{1, 0}, {3, -1}, {5, -1}}, "°" },
579 { 3, {{1, 0}, {2, 0}, {5, 0}}, "msus2" },
581 /* Seventh chords with no 3rd */
582 { 3, {{1, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS },
583 { 3, {{1, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS },
584 { 3, {{1, 0}, {5, 0}, {7, 0}}, "M7" },
585 { 3, {{1, 0}, {5, 0}, {7, -1}}, "7" },
586 { 3, {{1, 0}, {5, -1}, {7, -1}}, "7♭5" },
588 /* Seventh chords with no 5th */
589 { 3, {{1, 0}, {4, 0}, {7, 0}}, "M7sus" },
590 { 3, {{1, 0}, {4, 0}, {7, -1}}, "7sus" },
591 { 3, {{1, 0}, {3, 0}, {7, 0}}, "M7" },
592 { 3, {{1, 0}, {3, 0}, {7, -1}}, "7" },
593 { 3, {{1, 0}, {3, -1}, {7, 0}}, "m" SUP "M7" PUS },
594 { 3, {{1, 0}, {3, -1}, {7, -1}}, "m7" },
595 { 3, {{1, 0}, {2, 0}, {7, 0}}, "m" SUP "M7" PUS "sus2" },
596 { 3, {{1, 0}, {2, 0}, {7, -1}}, "m7sus2" },
599 { 4, {{1, 0}, {4, 0}, {5, 0}, {6, 0}}, "6sus" },
600 { 4, {{1, 0}, {3, 0}, {5, 0}, {6, 0}}, "6" }, /* Ambiguous with m7 */
601 { 4, {{1, 0}, {3, -1}, {5, 0}, {6, 0}}, "m6" },
602 { 4, {{1, 0}, {2, 0}, {5, 0}, {6, 0}}, "m6sus2" },
605 { 4, {{1, 0}, {4, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS "sus" },
606 { 4, {{1, 0}, {4, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS "sus" },
607 { 4, {{1, 0}, {4, 0}, {5, 0}, {7, 0}}, "M7sus" },
608 { 4, {{1, 0}, {4, 0}, {5, 0}, {7, -1}}, "7sus" },
609 { 4, {{1, 0}, {4, 0}, {5, -1}, {7, -1}}, "7♭5sus" },
610 { 4, {{1, 0}, {3, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS },
611 { 4, {{1, 0}, {3, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS },
612 { 4, {{1, 0}, {3, 0}, {5, 0}, {7, 0}}, "M7" },
613 { 4, {{1, 0}, {3, 0}, {5, 0}, {7, -1}}, "7" },
614 { 4, {{1, 0}, {3, 0}, {5, -1}, {7, -1}}, "7♭5" },
615 { 4, {{1, 0}, {3, -1}, {5, 0}, {7, 0}}, "m" SUP "M7" PUS },
616 { 4, {{1, 0}, {3, -1}, {5, 0}, {7, -1}}, "m7" },
617 { 4, {{1, 0}, {3, -1}, {5, -1}, {7, 0}}, "°" SUP "M7" PUS },
618 { 4, {{1, 0}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "7" PUS },
619 { 4, {{1, 0}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "7" PUS },
620 { 4, {{1, 0}, {2, 0}, {5, 0}, {7, 0}}, "m" SUP "M7" PUS "sus2" },
621 { 4, {{1, 0}, {2, 0}, {5, 0}, {7, -1}}, "m7sus2" },
622 { 4, {{1, 0}, {2, 0}, {5, -1}, {7, 0}}, "°" SUP "M7" PUS "sus2" },
623 { 4, {{1, 0}, {2, 0}, {5, -1}, {7, -1}}, "𝆩" SUP "7" PUS "sus2" },
624 { 4, {{1, 0}, {2, 0}, {5, -1}, {7, -2}}, "°" SUP "7" PUS "sus2" }, /* Ambiguous with 7 */
626 /* The sorting for chords with degree higher than 9 is funny
627 * to keep the array in degree order after reducing each
628 * degree to an actual scale degree, (9 -> 2, 11 -> 4, 13 ->
631 /* Ninth chords voiced with no 5th */
632 { 4, {{1, 0}, {9, 0}, {4, 0}, {7, 0}}, "M9sus" },
633 { 4, {{1, 0}, {9, 0}, {4, 0}, {7, -1}}, "9sus" },
634 { 4, {{1, 0}, {9, 0}, {3, 0}, {7, 0}}, "M9" },
635 { 4, {{1, 0}, {9, 0}, {3, 0}, {7, -1}}, "9" },
636 { 4, {{1, 0}, {9, 0}, {3, -1}, {7, 0}}, "m" SUP "M9" PUS },
637 { 4, {{1, 0}, {9, 0}, {3, -1}, {7, -1}}, "m9" },
638 { 4, {{1, 0}, {9, -1}, {3, -1}, {7, -1}}, "m♭9" },
639 { 4, {{1, 0}, {9, 0}, {3, -1}, {6, 0}}, "m6/9" },
640 { 4, {{1, 0}, {9, -1}, {3, -1}, {6, 0}}, "m6/♭9" },
643 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {6, 0}}, "6/9" },
644 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {6, 0}}, "m6/9" },
645 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, 0}, {6, 0}}, "m6/♭9" },
647 /* Seventh plus altered 9 */
648 { 5, {{1, 0}, {9, +1}, {3, 0}, {5, 0}, {7, -1}}, "7♯9" },
649 { 5, {{1, 0}, {9, -1}, {3, 0}, {5, 0}, {7, -1}}, "7♭9" },
650 { 5, {{1, 0}, {9, +1}, {3, 0}, {5, -1}, {7, -1}}, "7♭5♯9" },
651 { 5, {{1, 0}, {9, -1}, {3, 0}, {5, -1}, {7, -1}}, "7♭5♭9" },
654 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, +1}, {7, 0}}, SUP "+M9" PUS "sus" },
655 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, +1}, {7, -1}}, SUP "+9" PUS "sus" },
656 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, 0}, {7, 0}}, "M9sus" },
657 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, 0}, {7, -1}}, "9sus" },
658 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, +1}, {7, 0}}, SUP "+M9" PUS },
659 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, +1}, {7, -1}}, SUP "+9" PUS },
660 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {7, 0}}, "M9" },
661 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {7, -1}}, "9" },
662 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {7, 0}}, "m" SUP "M9" PUS },
663 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {7, -1}}, "m9" },
664 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, 0}, {7, -1}}, "m♭9" },
665 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, -1}, {7, 0}}, "M9" SUP "♭5" PUS },
666 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, -1}, {7, -1}}, "9" SUP "♭5" PUS },
667 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, 0}}, "m" SUP "M9♭5" PUS },
668 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "9" PUS },
669 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "♭9" PUS },
670 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "9" PUS },
671 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "♭9" PUS },
673 /* Eleventh chords */
674 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, +1}, {7, 0}}, SUP "+M11" PUS },
675 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, +1}, {7, -1}}, SUP "+11" PUS },
676 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, 0}, {7, 0}}, "M11" },
677 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, 0}, {7, -1}}, "11" },
678 { 6, {{1, 0}, {9, 0}, {3, -1}, {11, 0}, {5, 0}, {7, 0}}, "m" SUP "M11" PUS },
679 { 6, {{1, 0}, {9, 0}, {3, -1}, {11, 0}, {5, 0}, {7, -1}}, "m11" },
680 { 6, {{1, 0}, {9, -1}, {3, -1}, {11, 0}, {5, -1}, {7, -1}}, "𝆩" SUP "11" PUS },
681 { 6, {{1, 0}, {9, -1}, {3, -1}, {11, -1}, {5, -1}, {7, -2}}, "°" SUP "11" PUS }
685 _spell_chord_by_signature (pitch_group_t *chord,
686 chord_signature_t *signature,
689 int i, degree, note_half_steps, degree_half_steps;
691 for (i = 0; i < chord->num_pitches; i++) {
692 note_half_steps = pitch_from_root_in_half_steps (chord->pitches[i],
694 for (degree = 0; degree < signature->num_degrees; degree++) {
695 degree_half_steps = _modified_degree_to_half_steps (&signature->degrees[degree]);
696 if (note_half_steps == degree_half_steps) {
697 chord->pitches[i] = pitch_spell_as_degree (chord->pitches[i],
699 signature->degrees[degree].degree);
703 if (note_half_steps != degree_half_steps) {
704 fprintf (stderr, "Internal error: Chord and degree mis-match\n");
711 _analyze_chord (pitch_group_t *chord,
712 chord_signature_t **signature_ret,
715 void *local = talloc_new (NULL);
716 analyzed_pitch_t *pitches;
717 int i, j, num_pitches;
718 int bass_pitch, inversion;
720 chord_signature_t *match = NULL;
722 /* Default to no-match for any early return */
723 *signature_ret = NULL;
726 num_pitches = chord->num_pitches;
728 if (chord->num_pitches <= 1)
731 pitches = talloc_array (local, analyzed_pitch_t, num_pitches);
735 for (i = 0; i < num_pitches; i++) {
736 pitch_t pitch = chord->pitches[i];
737 pitches[i].pitch = pitch;
738 pitches[i].midi_pitch = pitch_to_midi (pitch);
739 /* Relative pitch will be filled in after sorting. */
740 pitches[i].relative_pitch = 0;
743 /* First, sort by midi pitch to find the bass note. */
744 qsort (pitches, num_pitches, sizeof (analyzed_pitch_t),
745 _compare_analyzed_pitch_by_midi_pitch);
747 bass_pitch = pitches[0].midi_pitch;
749 /* With the bass note identified, we can find all relative pitches. */
750 for (i = 0; i < num_pitches; i++) {
751 pitches[i].relative_pitch = pitches[i].midi_pitch - bass_pitch;
752 while (pitches[i].relative_pitch >= 12)
753 pitches[i].relative_pitch -= 12;
756 /* Now, sort again by relative pitch. */
757 qsort (pitches, num_pitches, sizeof (analyzed_pitch_t),
758 _compare_analyzed_pitch_by_relative_pitch);
760 /* Finally, eliminate all duplicate notes. */
761 for (i = 0; i < num_pitches - 1; i++) {
762 if (pitches[i+1].relative_pitch == pitches[i].relative_pitch) {
764 while (j < num_pitches &&
765 pitches[j].relative_pitch == pitches[i].relative_pitch)
769 /* The loop incremented j one past the last
770 * duplicate. Decrement so that it points to the
771 * last duplicate (and is guaranteed to not exceed
772 * the array bounds).*/
775 if (j < num_pitches - 1) {
776 memmove (&pitches[i+1], &pitches[j+1],
777 (num_pitches - j) * sizeof (analyzed_pitch_t));
780 num_pitches -= (j - i);
784 for (inversion = 0; inversion < 4; inversion++) {
785 for (i = 0; i < ARRAY_SIZE (signatures); i++) {
786 if (_chord_signature_matches (pitches, num_pitches,
787 signatures[i].degrees,
788 signatures[i].num_degrees,
791 match = &signatures[i];
799 *signature_ret = match;
800 for (i = 0; i < chord->num_pitches; i++) {
801 if (chord->pitches[i] == root) {
802 *root_ret = &chord->pitches[i];
813 pitch_group_init (void *ctx, pitch_group_t *group)
816 group->pitches = NULL;
818 group->num_pitches = 0;
822 pitch_group_add_pitch (pitch_group_t *group, pitch_t pitch)
826 /* Do nothing if pitch is already in group. */
827 for (i = 0; i < group->num_pitches; i++)
828 if (group->pitches[i] == pitch)
831 group->num_pitches++;
833 if (group->num_pitches > group->size) {
835 group->pitches = talloc_realloc (group->ctx, group->pitches,
836 pitch_t, group->size);
838 if (group->pitches == NULL) {
839 fprintf (stderr, "Out of memory.\n");
844 group->pitches[group->num_pitches - 1] = pitch;
848 pitch_group_remove_pitch_at (pitch_group_t *group, int i)
850 if (i > group->num_pitches) {
851 fprintf (stderr, "Internal error: Cannot remove pitch %d from group with only %d %s\n",
852 i, group->num_pitches,
853 group->num_pitches == 1 ? "pitch" : "pitches");
857 if (i < group->num_pitches - 1) {
858 memmove (group->pitches + i, group->pitches + i + 1,
859 (group->num_pitches - 1 - i) * sizeof (pitch_t));
862 group->num_pitches--;
865 /* Remove a 'pitch' from 'group', (including any enharmonic pitch)
866 * See also: pitch_group_remove_pitch
869 pitch_group_remove_pitch_enharmonic (pitch_group_t *group, pitch_t pitch)
873 for (i = 0; i < group->num_pitches; i++)
874 if (_pitches_are_enharmonic (group->pitches[i], pitch))
875 pitch_group_remove_pitch_at (group, i);
878 /* Remove an exactly-matching pitch from 'group'
879 * See also: pitch_group_remove_pitch_enharmonic
882 pitch_group_remove_pitch (pitch_group_t *group, pitch_t pitch)
886 for (i = 0; i < group->num_pitches; i++)
887 if (group->pitches[i] == pitch)
888 pitch_group_remove_pitch_at (group, i);
892 /* Remove all pitches from a pitch_group_t */
894 pitch_group_remove_pitches (pitch_group_t *group)
896 talloc_free (group->pitches);
897 pitch_group_init (group->ctx, group);
901 scherzo_update_notes_and_chord (scherzo_t *scherzo)
903 void *local = talloc_new (NULL);
904 pitch_group_t *chord;
905 chord_signature_t *signature;
906 char *chord_name = NULL;
910 /* Remove all notes/chords from score, then add current notes. */
911 score_remove_notes (scherzo->score);
912 score_remove_chords (scherzo->score);
914 if (scherzo->pedal_pressed)
915 chord = &scherzo->notes_pedaled;
917 chord = &scherzo->notes_pressed;
919 _analyze_chord (chord, &signature, &root);
922 /* Don't print root pitch for octaves and inversions,
923 * (since a pitch name alone looks like a major triad) */
924 if (signature->num_degrees < 3) {
925 /* In fact, don't print anything for octaves and
926 * inversions for now. It leads to a bit of flashing when
929 * FIXME: What I'd like to have here is a small timeout,
930 * such that names only appear once an interval has been
931 * held for at least 100 ms or so. With that, it would be
932 * nice to display the interval names once again.
934 chord_name = talloc_strdup (local, signature->name);
937 _spell_chord_by_signature (&scherzo->notes_pressed,
939 _spell_chord_by_signature (&scherzo->notes_pedaled,
942 chord_name = talloc_asprintf (local, "%s%s",
943 pitch_string (*root),
946 } else if (chord->num_pitches > 2) {
947 chord_name = talloc_strdup (local, "?");
951 scherzo->chord = score_add_chord (scherzo->treble, chord_name);
953 for (i = 0; i < scherzo->notes_pressed.num_pitches; i++)
955 score_add_note (scherzo->score, scherzo->notes_pressed.pitches[i],
956 SCORE_DURATION_WHOLE);
959 for (i = 0; i < scherzo->notes_pedaled.num_pitches; i++)
961 score_add_note (scherzo->score, scherzo->notes_pedaled.pitches[i],
962 SCORE_DURATION_WHOLE);
965 gtk_widget_queue_draw (scherzo->window);
971 scherzo_set_key (scherzo_t *scherzo, pitch_t pitch)
973 scherzo_key_init (&scherzo->key, pitch);
974 score_set_key (scherzo->score, pitch);
978 pitches_are_diatonic_scale (pitch_t *pitches, int num_pitches)
980 int diatonic_half_steps[7] = {
992 for (i = 0; i < 7; i++) {
993 half_steps = pitch_from_root_in_half_steps (pitches[i], root);
994 if (half_steps != diatonic_half_steps[i])
1002 scherzo_press_note (scherzo_t *scherzo, pitch_t pitch)
1006 pitch = scherzo_key_spell_pitch (&scherzo->key, pitch);
1008 #define NUM_RECENT_PITCHES 7
1009 static pitch_t recent_pitches[NUM_RECENT_PITCHES];
1011 memmove (recent_pitches, recent_pitches + 1,
1012 (NUM_RECENT_PITCHES - 1) * sizeof (pitch_t));
1013 recent_pitches[NUM_RECENT_PITCHES - 1] = pitch;
1015 if (pitches_are_diatonic_scale (recent_pitches, NUM_RECENT_PITCHES))
1016 scherzo_set_key (scherzo, recent_pitches[0]);
1018 /* Do nothing if this note is already pressed. */
1019 for (i = 0; i < scherzo->notes_pressed.num_pitches; i++)
1020 if (scherzo->notes_pressed.pitches[i] == pitch)
1023 pitch_group_add_pitch (&scherzo->notes_pressed, pitch);
1025 if (scherzo->pedal_pressed)
1026 pitch_group_add_pitch (&scherzo->notes_pedaled, pitch);
1028 scherzo_update_notes_and_chord (scherzo);
1032 scherzo_release_note (scherzo_t *scherzo, pitch_t pitch)
1034 pitch = scherzo_key_spell_pitch (&scherzo->key, pitch);
1036 pitch_group_remove_pitch_enharmonic (&scherzo->notes_pressed, pitch);
1038 scherzo_update_notes_and_chord (scherzo);
1042 scherzo_press_note_midi (scherzo_t *scherzo, unsigned char midi_note)
1044 scherzo_press_note (scherzo, pitch_from_midi (midi_note));
1048 scherzo_release_note_midi (scherzo_t *scherzo, unsigned char midi_note)
1050 scherzo_release_note (scherzo, pitch_from_midi (midi_note));
1054 scherzo_press_pedal (scherzo_t *scherzo)
1058 if (scherzo->pedal_pressed)
1061 scherzo->pedal_pressed = 1;
1063 /* Copy all pressed notes to pedaled notes */
1064 for (i = 0; i < scherzo->notes_pressed.num_pitches; i++)
1065 pitch_group_add_pitch (&scherzo->notes_pedaled, scherzo->notes_pressed.pitches[i]);
1069 scherzo_release_pedal (scherzo_t *scherzo)
1073 if (! scherzo->pedal_pressed)
1076 /* Remove all previously pedaled notes. */
1077 for (i = scherzo->notes_pedaled.num_pitches - 1; i >=0; i--)
1078 pitch_group_remove_pitch_at (&scherzo->notes_pedaled, i);
1080 scherzo->pedal_pressed = 0;
1082 scherzo_update_notes_and_chord (scherzo);
1087 _select_challenge (scherzo_t *scherzo)
1089 category_t *category_unused;
1090 bool_t introduced_unused;
1092 challenge_t *challenge = &scherzo->challenge;
1093 pitch_name_t pitch_name;
1097 mnemon_select_item (&scherzo->mnemon,
1099 &challenge->item_index,
1101 &introduced_unused);
1103 item = challenge->bin->items[challenge->item_index];
1105 s = item->challenge;
1106 if (strncmp (s, "treble:", 7) == 0) {
1108 challenge->staff = scherzo->treble;
1109 } else if (strncmp (s, "bass:", 5) == 0) {
1111 challenge->staff = scherzo->bass;
1114 "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
1121 pitch_name = PITCH_NAME_C;
1124 pitch_name = PITCH_NAME_D;
1127 pitch_name = PITCH_NAME_E;
1130 pitch_name = PITCH_NAME_F;
1133 pitch_name = PITCH_NAME_G;
1136 pitch_name = PITCH_NAME_A;
1139 pitch_name = PITCH_NAME_B;
1142 fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
1147 if (*s < '0' || *s > '9') {
1148 fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
1154 challenge->pitch = PITCH (pitch_name, PITCH_ACCIDENTAL_NATURAL, octave);
1156 challenge->active = 1;
1157 challenge->satisfied = 0;
1158 challenge->mistaken = 0;
1161 /* Determine whether the user hit the correct note. */
1163 _judge_pitch (scherzo_t *scherzo, pitch_t pitch)
1165 challenge_t *challenge = &scherzo->challenge;
1167 if (! challenge->active)
1170 if (pitch == challenge->pitch)
1171 challenge->satisfied = 1;
1173 challenge->mistaken = 1;
1176 /* If the user got the right note (eventually), then score it in
1177 * mnemon and show the next note. */
1179 _score_challenge (scherzo_t *scherzo)
1181 challenge_t *challenge = &scherzo->challenge;
1183 if (! challenge->active)
1186 if (! challenge->satisfied)
1189 mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
1190 ! challenge->mistaken);
1192 _select_challenge (scherzo);
1197 on_midi_input (unused (GIOChannel *channel),
1198 unused (GIOCondition condition),
1201 unsigned char buf[MIDI_BUF_SIZE], *next;
1202 scherzo_t *scherzo = user_data;
1204 snd_seq_event_t event;
1206 remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
1212 consumed = snd_midi_event_encode (scherzo->snd_midi_event,
1213 next, remaining, &event);
1215 remaining -= consumed;
1218 switch (event.type) {
1219 case SND_SEQ_EVENT_NONE:
1220 /* Incomplete event. Nothing to do. */
1222 case SND_SEQ_EVENT_NOTEON:
1223 scherzo_press_note_midi (scherzo, event.data.note.note);
1225 case SND_SEQ_EVENT_NOTEOFF:
1226 scherzo_release_note_midi (scherzo, event.data.note.note);
1228 case SND_SEQ_EVENT_CLOCK:
1229 /* Ignore for now as my piano sends a constant stream of these. */
1231 case SND_SEQ_EVENT_SENSING:
1232 /* Ignore for now as my piano sends a constant stream of these. */
1234 case SND_SEQ_EVENT_CONTROLLER:
1235 /* XXX: My piano gives me 64 for the sustain pedal, is
1236 * that universal? */
1237 if (event.data.control.param == 64) {
1238 if (event.data.control.value == 0)
1239 scherzo_release_pedal (scherzo);
1241 scherzo_press_pedal (scherzo);
1243 fprintf (stderr, "Fixme: Unhandled MIDI Control event (param=%d, value=%d)\n",
1244 event.data.control.param, event.data.control.value);
1248 fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
1254 /* Return TRUE to continue to get called in the future. */
1259 main (int argc, char *argv[])
1261 GtkWidget *drawing_area;
1265 srand (time (NULL));
1267 gtk_init (&argc, &argv);
1269 scherzo.ctx = talloc_new (NULL);
1271 scherzo.score = score_create (scherzo.ctx);
1272 scherzo.staff_height = 100;
1273 score_set_staff_height (scherzo.score, scherzo.staff_height);
1275 score_add_brace (scherzo.score, 2);
1276 scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
1277 scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
1279 scherzo.chord = NULL;
1281 /* Default to octave 4 for computer keyboard keypresses. */
1282 scherzo.keyboard_octave = 4;
1284 pitch_group_init (scherzo.ctx, &scherzo.notes_pressed);
1285 pitch_group_init (scherzo.ctx, &scherzo.notes_pedaled);
1287 scherzo.pedal_pressed = 0;
1289 /* Default to key of C Major, naturally. */
1290 scherzo_set_key (&scherzo, PITCH_CLASS_LITERAL (C, NATURAL));
1292 mnemon_init (&scherzo.mnemon);
1293 /* XXX: Should create a default file if one cannot be loaded. */
1294 mnemon_load_category (&scherzo.mnemon, "scherzo-notes");
1296 scherzo.challenge.active = 0;
1298 _select_challenge (&scherzo);
1301 err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
1303 fprintf (stderr, "Out of memory.\n");
1307 #define MIDI_DEVICE "/dev/midi1"
1308 scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
1309 if (scherzo.midi_fd < 0) {
1310 printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
1312 GIOChannel *channel;
1314 channel = g_io_channel_unix_new (scherzo.midi_fd);
1315 g_io_channel_set_encoding (channel, NULL, NULL);
1316 g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
1319 scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1321 gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 1000, 600);
1323 g_signal_connect (scherzo.window, "delete-event",
1324 G_CALLBACK (on_delete_event_quit), NULL);
1326 drawing_area = gtk_drawing_area_new ();
1328 gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
1330 g_signal_connect (drawing_area, "expose-event",
1331 G_CALLBACK (on_expose_event_draw),
1334 g_signal_connect (scherzo.window, "key-press-event",
1335 G_CALLBACK (on_key_press_event),
1338 g_signal_connect (scherzo.window, "key-release-event",
1339 G_CALLBACK (on_key_release_event),
1342 gtk_widget_show_all (scherzo.window);
1346 mnemon_save (&scherzo.mnemon);
1348 snd_midi_event_free (scherzo.snd_midi_event);
1350 talloc_free (scherzo.ctx);