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. */
69 pitch_accidental_t keyboard_accidental;
72 snd_midi_event_t *snd_midi_event;
75 challenge_t challenge;
77 pitch_group_t notes_pressed;
78 pitch_group_t notes_pedaled;
85 /* Forward declarations. */
87 scherzo_press_note (scherzo_t *scherzo, pitch_t pitch);
90 scherzo_release_note (scherzo_t *scherzo, pitch_t pitch);
93 scherzo_press_pedal (scherzo_t *scherzo);
96 scherzo_release_pedal (scherzo_t *scherzo);
99 _judge_pitch (scherzo_t *scherzo, pitch_t pitch);
102 _score_challenge (scherzo_t *scherzo);
105 on_delete_event_quit (unused (GtkWidget *widget),
106 unused (GdkEvent *event),
107 unused (gpointer user_data))
111 /* Returning FALSE allows the default handler for delete-event
112 * to proceed to cleanup the widget. */
117 on_expose_event_draw (GtkWidget *widget,
118 unused (GdkEventExpose *expose),
121 scherzo_t *scherzo = user_data;
122 score_t *score = scherzo->score;
124 GtkAllocation allocation;
125 static const int pad = 10;
128 gtk_widget_get_allocation (widget, &allocation);
129 widget_width = allocation.width;
131 cr = gdk_cairo_create (widget->window);
133 /* White background */
134 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
137 /* Add some padding on the sides and top */
138 cairo_translate (cr, pad, scherzo->staff_height);
139 score_set_staff_height (score, scherzo->staff_height);
140 score_set_width (score, widget_width - 2 * pad);
142 score_draw (score, cr);
148 on_key_press_event (GtkWidget *widget,
152 scherzo_t *scherzo = user_data;
154 /* Initialize to keep the compiler quiet. */
155 pitch_name_t pitch_name = PITCH_NATURAL (C, 0);
158 if (scherzo->challenge.active)
159 octave = PITCH_OCTAVE (scherzo->challenge.pitch);
161 octave = scherzo->keyboard_octave;
163 switch (key->keyval) {
167 case GDK_KEY_KP_Equal:
168 scherzo->staff_height += 4;
169 gtk_widget_queue_draw (widget);
173 case GDK_KEY_KP_Subtract:
174 scherzo->staff_height -= 4;
175 gtk_widget_queue_draw (widget);
185 pitch_name = PITCH_NAME_C;
189 pitch_name = PITCH_NAME_D;
193 pitch_name = PITCH_NAME_E;
197 pitch_name = PITCH_NAME_F;
201 pitch_name = PITCH_NAME_G;
205 pitch_name = PITCH_NAME_A;
209 pitch_name = PITCH_NAME_B;
220 scherzo->keyboard_octave = key->keyval - GDK_KEY_0;
223 scherzo_press_pedal (scherzo);
226 if (scherzo->keyboard_accidental < PITCH_ACCIDENTAL_DOUBLE_SHARP)
227 scherzo->keyboard_accidental++;
230 if (scherzo->keyboard_accidental > PITCH_ACCIDENTAL_DOUBLE_FLAT)
231 scherzo->keyboard_accidental--;
235 pitch = PITCH (pitch_name, scherzo->keyboard_accidental, octave);
237 if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) ||
238 (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g))
240 scherzo_press_note (scherzo, pitch);
241 _judge_pitch (scherzo, pitch);
242 gtk_widget_queue_draw (scherzo->window);
248 /* Allow an unhandled event to propagate to other handlers. */
253 on_key_release_event (unused (GtkWidget *widget),
257 scherzo_t *scherzo = user_data;
259 /* Initialize to keep the compiler quiet. */
260 pitch_name_t pitch_name = PITCH_NAME_C;
263 if (scherzo->challenge.active)
264 octave = PITCH_OCTAVE (scherzo->challenge.pitch);
266 octave = scherzo->keyboard_octave;
268 switch (key->keyval) {
271 pitch_name = PITCH_NAME_C;
275 pitch_name = PITCH_NAME_D;
279 pitch_name = PITCH_NAME_E;
283 pitch_name = PITCH_NAME_F;
287 pitch_name = PITCH_NAME_G;
291 pitch_name = PITCH_NAME_A;
295 pitch_name = PITCH_NAME_B;
298 scherzo_release_pedal (scherzo);
302 pitch = PITCH (pitch_name, scherzo->keyboard_accidental, octave);
304 if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) ||
305 (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g))
307 scherzo_release_note (scherzo, pitch);
308 _score_challenge (scherzo);
309 gtk_widget_queue_draw (scherzo->window);
315 /* Allow an unhandled event to propagate to other handlers. */
320 _pitch_to_midi (pitch_t pitch)
322 int octave = PITCH_OCTAVE (pitch);
323 unsigned char midi_note = 12 * (octave + 1);
325 switch (PITCH_NAME (pitch)) {
348 switch (PITCH_ACCIDENTAL (pitch)) {
349 case PITCH_ACCIDENTAL_DOUBLE_FLAT:
352 case PITCH_ACCIDENTAL_FLAT:
355 case PITCH_ACCIDENTAL_NATURAL:
357 case PITCH_ACCIDENTAL_SHARP:
360 case PITCH_ACCIDENTAL_DOUBLE_SHARP:
368 /* octave can optionally by NULL */
370 _midi_to_pitch (unsigned char midi_note)
372 int octave = octave = midi_note / 12 - 1;
374 switch (midi_note % 12)
378 return PITCH_LITERAL (C, NATURAL, octave);
381 return PITCH_LITERAL (C, SHARP, octave);
384 return PITCH_LITERAL (D, NATURAL, octave);
387 return PITCH_LITERAL (D, SHARP, octave);
390 return PITCH_LITERAL (E, NATURAL, octave);
393 return PITCH_LITERAL (F, NATURAL, octave);
396 return PITCH_LITERAL (F, SHARP, octave);
399 return PITCH_LITERAL (G, NATURAL, octave);
402 return PITCH_LITERAL (G, SHARP, octave);
405 return PITCH_LITERAL (A, NATURAL, octave);
408 return PITCH_LITERAL (A, SHARP, octave);
411 return PITCH_LITERAL (B, NATURAL, octave);
416 /* Return true if 'a' and 'b' sound identical, (even if spelled differently)
418 * This comparison considers octaves as significant. So Bb and A# in
419 * the same octave are considered enharmonic, but Bb and A# in
420 * different octaves are not. */
422 _pitches_are_enharmonic (pitch_t a, pitch_t b)
424 return _pitch_to_midi (a) == _pitch_to_midi (b);
427 /* Determine a chord name (if any) from the current notes pressed */
429 typedef struct analyzed_pitch {
430 /* Original note being analzyed. */
433 /* Absolute pitch (expressed as midi number). */
436 /* Pitch relative to bass note. */
441 _compare_analyzed_pitch_by_midi_pitch (const void *va, const void *vb)
443 const analyzed_pitch_t *a = va, *b = vb;
445 return a->midi_pitch - b->midi_pitch;
449 _compare_analyzed_pitch_by_relative_pitch (const void *va, const void *vb)
451 const analyzed_pitch_t *a = va, *b = vb;
453 return a->relative_pitch - b->relative_pitch;
456 typedef struct modified_degree
463 _modified_degree_to_half_steps (const modified_degree_t *degree)
466 int scale_degree = degree->degree;
468 /* Restrict to actual degrees within a scale. */
469 if (scale_degree > 7)
470 scale_degree = (scale_degree % 8) + 1;
472 /* Number of half steps from root to specified degree within a
474 switch (scale_degree) {
497 fprintf (stderr, "Internal: Invalid degree %d\n", degree->degree);
502 return half_steps + degree->modification;
505 /* Number of half steps from 'root' up to 'pitch' (that is, counting
506 * the steps up a chromatic scale), within the same octave. */
508 _pitch_from_root_in_half_steps (pitch_t pitch, pitch_t root)
510 int root_midi = _pitch_to_midi (root);
511 int pitch_midi = _pitch_to_midi (pitch);
513 while (pitch_midi < root_midi)
516 return (pitch_midi - root_midi) % 12;
520 _chord_signature_matches (analyzed_pitch_t *pitches,
522 modified_degree_t *degrees,
527 #define MAX_DEGREES 6
528 int relative_pitches[MAX_DEGREES];
531 assert (num_degrees <= MAX_DEGREES);
533 if (num_pitches != num_degrees)
536 if (inversion >= num_degrees)
539 /* We never spell simple intervals as inversions. */
540 if (num_degrees == 2 && inversion > 0)
543 for (i = 0; i < num_degrees; i++) {
544 /* The num_degrees is in the addition just to ensure all
545 * inputs to the modulus operator remain positive. */
546 int index = (i + num_degrees - inversion) % num_degrees;
548 /* Again, adding a 12 to keep things positive. */
549 relative_pitches[index] =
551 _modified_degree_to_half_steps (°rees[i]) -
552 _modified_degree_to_half_steps (°rees[inversion])) % 12;
556 for (i = 0; i < num_pitches; i++)
557 if (pitches[i].relative_pitch != relative_pitches[i])
560 root_index = (num_pitches - inversion) % num_pitches;
561 *root = pitches[root_index].pitch;
566 typedef struct chord_signature
569 modified_degree_t degrees[MAX_DEGREES];
573 /* XXX: The superscript rise value should be relative to the current
574 * font size. Best would be for pango to support this. See:
576 * https://bugzilla.gnome.org/show_bug.cgi?id=708780
578 * For now, these are emipirically-derived values that look reasonable
579 * at the default size that score.c is using to draw the staff. If the
580 * user scales the staff up or down then these will look wrong.
583 #define SUP "<span font='33' rise='30000'>"
584 #define PUS "</span>"
586 chord_signature_t signatures[] = {
588 { 1, {{1, 0}}, "P8" },
591 { 2, {{1, 0}, {2, -1}}, "m2" },
592 { 2, {{1, 0}, {2, 0}}, "M2" },
593 { 2, {{1, 0}, {3, -1}}, "m3" },
594 { 2, {{1, 0}, {3, 0}}, "M3" },
595 { 2, {{1, 0}, {4, 0}}, "P4" },
596 { 2, {{1, 0}, {5, -1}}, "Tri-tone" },
597 { 2, {{1, 0}, {5, 0}}, "P5" },
598 { 2, {{1, 0}, {6, -1}}, "m6" },
599 { 2, {{1, 0}, {6, 0}}, "M6" },
600 { 2, {{1, 0}, {7, -1}}, "m7" },
601 { 2, {{1, 0}, {7, 0}}, "M7" },
604 { 3, {{1, 0}, {4, 0}, {5, 0}}, "sus" },
605 { 3, {{1, 0}, {3, 0}, {5, +1}}, SUP "+" PUS },
606 { 3, {{1, 0}, {3, 0}, {5, 0}}, "" },
607 { 3, {{1, 0}, {3, -1}, {5, 0}}, "m" },
608 { 3, {{1, 0}, {3, -1}, {5, -1}}, "°" },
609 { 3, {{1, 0}, {2, 0}, {5, 0}}, "msus2" },
611 /* Seventh chords with no 3rd */
612 { 3, {{1, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS },
613 { 3, {{1, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS },
614 { 3, {{1, 0}, {5, 0}, {7, 0}}, "M7" },
615 { 3, {{1, 0}, {5, 0}, {7, -1}}, "7" },
616 { 3, {{1, 0}, {5, -1}, {7, -1}}, "7♭5" },
618 /* Seventh chords with no 5th */
619 { 3, {{1, 0}, {4, 0}, {7, 0}}, "M7sus" },
620 { 3, {{1, 0}, {4, 0}, {7, -1}}, "7sus" },
621 { 3, {{1, 0}, {3, 0}, {7, 0}}, "M7" },
622 { 3, {{1, 0}, {3, 0}, {7, -1}}, "7" },
623 { 3, {{1, 0}, {3, -1}, {7, 0}}, "m" SUP "M7" PUS },
624 { 3, {{1, 0}, {3, -1}, {7, -1}}, "m7" },
625 { 3, {{1, 0}, {2, 0}, {7, 0}}, "m" SUP "M7" PUS "sus2" },
626 { 3, {{1, 0}, {2, 0}, {7, -1}}, "m7sus2" },
629 { 4, {{1, 0}, {4, 0}, {5, 0}, {6, 0}}, "6sus" },
630 { 4, {{1, 0}, {3, 0}, {5, 0}, {6, 0}}, "6" }, /* Ambiguous with m7 */
631 { 4, {{1, 0}, {3, -1}, {5, 0}, {6, 0}}, "m6" },
632 { 4, {{1, 0}, {2, 0}, {5, 0}, {6, 0}}, "m6sus2" },
635 { 4, {{1, 0}, {4, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS "sus" },
636 { 4, {{1, 0}, {4, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS "sus" },
637 { 4, {{1, 0}, {4, 0}, {5, 0}, {7, 0}}, "M7sus" },
638 { 4, {{1, 0}, {4, 0}, {5, 0}, {7, -1}}, "7sus" },
639 { 4, {{1, 0}, {4, 0}, {5, -1}, {7, -1}}, "7♭5sus" },
640 { 4, {{1, 0}, {3, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS },
641 { 4, {{1, 0}, {3, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS },
642 { 4, {{1, 0}, {3, 0}, {5, 0}, {7, 0}}, "M7" },
643 { 4, {{1, 0}, {3, 0}, {5, 0}, {7, -1}}, "7" },
644 { 4, {{1, 0}, {3, 0}, {5, -1}, {7, -1}}, "7♭5" },
645 { 4, {{1, 0}, {3, -1}, {5, 0}, {7, 0}}, "m" SUP "M7" PUS },
646 { 4, {{1, 0}, {3, -1}, {5, 0}, {7, -1}}, "m7" },
647 { 4, {{1, 0}, {3, -1}, {5, -1}, {7, 0}}, "°" SUP "M7" PUS },
648 { 4, {{1, 0}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "7" PUS },
649 { 4, {{1, 0}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "7" PUS },
650 { 4, {{1, 0}, {2, 0}, {5, 0}, {7, 0}}, "m" SUP "M7" PUS "sus2" },
651 { 4, {{1, 0}, {2, 0}, {5, 0}, {7, -1}}, "m7sus2" },
652 { 4, {{1, 0}, {2, 0}, {5, -1}, {7, 0}}, "°" SUP "M7" PUS "sus2" },
653 { 4, {{1, 0}, {2, 0}, {5, -1}, {7, -1}}, "𝆩" SUP "7" PUS "sus2" },
654 { 4, {{1, 0}, {2, 0}, {5, -1}, {7, -2}}, "°" SUP "7" PUS "sus2" }, /* Ambiguous with 7 */
656 /* The sorting for chords with degree higher than 9 is funny
657 * to keep the array in degree order after reducing each
658 * degree to an actual scale degree, (9 -> 2, 11 -> 4, 13 ->
661 /* Ninth chords voiced with no 5th */
662 { 4, {{1, 0}, {9, 0}, {4, 0}, {7, 0}}, "M9sus" },
663 { 4, {{1, 0}, {9, 0}, {4, 0}, {7, -1}}, "9sus" },
664 { 4, {{1, 0}, {9, 0}, {3, 0}, {7, 0}}, "M9" },
665 { 4, {{1, 0}, {9, 0}, {3, 0}, {7, -1}}, "9" },
666 { 4, {{1, 0}, {9, 0}, {3, -1}, {7, 0}}, "m" SUP "M9" PUS },
667 { 4, {{1, 0}, {9, 0}, {3, -1}, {7, -1}}, "m9" },
668 { 4, {{1, 0}, {9, -1}, {3, -1}, {7, -1}}, "m♭9" },
669 { 4, {{1, 0}, {9, 0}, {3, -1}, {6, 0}}, "m6/9" },
670 { 4, {{1, 0}, {9, -1}, {3, -1}, {6, 0}}, "m6/♭9" },
673 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {6, 0}}, "6/9" },
674 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {6, 0}}, "m6/9" },
675 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, 0}, {6, 0}}, "m6/♭9" },
677 /* Seventh plus altered 9 */
678 { 5, {{1, 0}, {9, +1}, {3, 0}, {5, 0}, {7, -1}}, "7♯9" },
679 { 5, {{1, 0}, {9, -1}, {3, 0}, {5, 0}, {7, -1}}, "7♭9" },
680 { 5, {{1, 0}, {9, +1}, {3, 0}, {5, -1}, {7, -1}}, "7♭5♯9" },
681 { 5, {{1, 0}, {9, -1}, {3, 0}, {5, -1}, {7, -1}}, "7♭5♭9" },
684 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, +1}, {7, 0}}, SUP "+M9" PUS "sus" },
685 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, +1}, {7, -1}}, SUP "+9" PUS "sus" },
686 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, 0}, {7, 0}}, "M9sus" },
687 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, 0}, {7, -1}}, "9sus" },
688 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, +1}, {7, 0}}, SUP "+M9" PUS },
689 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, +1}, {7, -1}}, SUP "+9" PUS },
690 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {7, 0}}, "M9" },
691 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {7, -1}}, "9" },
692 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {7, 0}}, "m" SUP "M9" PUS },
693 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {7, -1}}, "m9" },
694 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, 0}, {7, -1}}, "m♭9" },
695 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, -1}, {7, 0}}, "M9" SUP "♭5" PUS },
696 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, -1}, {7, -1}}, "9" SUP "♭5" PUS },
697 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, 0}}, "m" SUP "M9♭5" PUS },
698 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "9" PUS },
699 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "♭9" PUS },
700 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "9" PUS },
701 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "♭9" PUS },
703 /* Eleventh chords */
704 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, +1}, {7, 0}}, SUP "+M11" PUS },
705 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, +1}, {7, -1}}, SUP "+11" PUS },
706 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, 0}, {7, 0}}, "M11" },
707 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, 0}, {7, -1}}, "11" },
708 { 6, {{1, 0}, {9, 0}, {3, -1}, {11, 0}, {5, 0}, {7, 0}}, "m" SUP "M11" PUS },
709 { 6, {{1, 0}, {9, 0}, {3, -1}, {11, 0}, {5, 0}, {7, -1}}, "m11" },
710 { 6, {{1, 0}, {9, -1}, {3, -1}, {11, 0}, {5, -1}, {7, -1}}, "𝆩" SUP "11" PUS },
711 { 6, {{1, 0}, {9, -1}, {3, -1}, {11, -1}, {5, -1}, {7, -2}}, "°" SUP "11" PUS }
715 scherzo_adjust_pitch_to_match_modified_degree (pitch_t *pitch,
717 modified_degree_t *degree)
719 pitch_name_t name = PITCH_NAME (*pitch);
720 pitch_accidental_t accidental = PITCH_ACCIDENTAL (*pitch);
721 int octave = PITCH_OCTAVE (*pitch);
722 int degree_zero_based = (degree->degree - 1) % 7;
725 int note_degree_zero_based = name - PITCH_NAME (root);
726 if (note_degree_zero_based < 0)
727 note_degree_zero_based += 7;
729 if (note_degree_zero_based == degree_zero_based)
732 degree_delta = note_degree_zero_based - degree_zero_based;
733 if (degree_delta > 3)
734 degree_delta = - (7 - degree_delta);
735 if (degree_delta < -3)
736 degree_delta = - (-7 - degree_delta);
738 if (abs (degree_delta) != 1) {
739 fprintf (stderr, "Internal error: Cannot adjust a note more than one degree (%d vs. %d).\n", note_degree_zero_based + 1, degree->degree);
743 if (degree_delta == -1) {
744 if (name == PITCH_NAME_B) {
765 if (degree_delta == +1) {
766 if (name == PITCH_NAME_C) {
786 if (accidental < 0 || accidental > PITCH_ACCIDENTAL_DOUBLE_SHARP) {
787 fprintf (stderr, "Internal error: Trying to adjust an accidental out of range (degree_delta == %d (%d - %d), new accidental == %d).\n", degree_delta, note_degree_zero_based, degree_zero_based, accidental);
791 *pitch = PITCH (name, accidental, octave);
795 _spell_chord_by_signature (pitch_group_t *chord,
796 chord_signature_t *signature,
799 int i, degree, note_half_steps, degree_half_steps;
802 /* Sanitize root to eliminate things like double-flats,
803 * double-sharps, Cb, E#, etc. */
804 root_midi = _pitch_to_midi (root);
805 root = _midi_to_pitch (root_midi);
807 for (i = 0; i < chord->num_pitches; i++) {
808 note_half_steps = _pitch_from_root_in_half_steps (chord->pitches[i],
810 for (degree = 0; degree < signature->num_degrees; degree++) {
811 degree_half_steps = _modified_degree_to_half_steps (&signature->degrees[degree]);
812 if (note_half_steps == degree_half_steps) {
813 scherzo_adjust_pitch_to_match_modified_degree (&chord->pitches[i],
815 &signature->degrees[degree]);
819 if (note_half_steps != degree_half_steps) {
820 fprintf (stderr, "Internal error: Chord and degree mis-match\n");
827 _analyze_chord (pitch_group_t *chord,
828 chord_signature_t **signature_ret,
831 void *local = talloc_new (NULL);
832 analyzed_pitch_t *pitches;
833 int i, j, num_pitches;
834 int bass_pitch, inversion;
836 chord_signature_t *match = NULL;
838 /* Default to no-match for any early return */
839 *signature_ret = NULL;
842 num_pitches = chord->num_pitches;
844 if (chord->num_pitches <= 1)
847 pitches = talloc_array (local, analyzed_pitch_t, num_pitches);
851 for (i = 0; i < num_pitches; i++) {
852 pitch_t pitch = chord->pitches[i];
853 pitches[i].pitch = pitch;
854 pitches[i].midi_pitch = _pitch_to_midi (pitch);
855 /* Relative pitch will be filled in after sorting. */
856 pitches[i].relative_pitch = 0;
859 /* First, sort by midi pitch to find the bass note. */
860 qsort (pitches, num_pitches, sizeof (analyzed_pitch_t),
861 _compare_analyzed_pitch_by_midi_pitch);
863 bass_pitch = pitches[0].midi_pitch;
865 /* With the bass note identified, we can find all relative pitches. */
866 for (i = 0; i < num_pitches; i++) {
867 pitches[i].relative_pitch = pitches[i].midi_pitch - bass_pitch;
868 while (pitches[i].relative_pitch >= 12)
869 pitches[i].relative_pitch -= 12;
872 /* Now, sort again by relative pitch. */
873 qsort (pitches, num_pitches, sizeof (analyzed_pitch_t),
874 _compare_analyzed_pitch_by_relative_pitch);
876 /* Finally, eliminate all duplicate notes. */
877 for (i = 0; i < num_pitches - 1; i++) {
878 if (pitches[i+1].relative_pitch == pitches[i].relative_pitch) {
880 while (j < num_pitches &&
881 pitches[j].relative_pitch == pitches[i].relative_pitch)
885 /* The loop incremented j one past the last
886 * duplicate. Decrement so that it points to the
887 * last duplicate (and is guaranteed to not exceed
888 * the array bounds).*/
891 if (j < num_pitches - 1) {
892 memmove (&pitches[i+1], &pitches[j+1],
893 (num_pitches - j) * sizeof (analyzed_pitch_t));
896 num_pitches -= (j - i);
900 for (inversion = 0; inversion < 4; inversion++) {
901 for (i = 0; i < ARRAY_SIZE (signatures); i++) {
902 if (_chord_signature_matches (pitches, num_pitches,
903 signatures[i].degrees,
904 signatures[i].num_degrees,
907 match = &signatures[i];
915 *signature_ret = match;
916 for (i = 0; i < chord->num_pitches; i++) {
917 if (chord->pitches[i] == root) {
918 *root_ret = &chord->pitches[i];
929 pitch_group_init (void *ctx, pitch_group_t *group)
932 group->pitches = NULL;
934 group->num_pitches = 0;
938 pitch_group_add_pitch (pitch_group_t *group, pitch_t pitch)
942 /* Do nothing if pitch is already in group. */
943 for (i = 0; i < group->num_pitches; i++)
944 if (group->pitches[i] == pitch)
947 group->num_pitches++;
949 if (group->num_pitches > group->size) {
951 group->pitches = talloc_realloc (group->ctx, group->pitches,
952 pitch_t, group->size);
954 if (group->pitches == NULL) {
955 fprintf (stderr, "Out of memory.\n");
960 group->pitches[group->num_pitches - 1] = pitch;
964 pitch_group_remove_pitch_at (pitch_group_t *group, int i)
966 if (i > group->num_pitches) {
967 fprintf (stderr, "Internal error: Cannot remove pitch %d from group with only %d %s\n",
968 i, group->num_pitches,
969 group->num_pitches == 1 ? "pitch" : "pitches");
973 if (i < group->num_pitches - 1) {
974 memmove (group->pitches + i, group->pitches + i + 1,
975 (group->num_pitches - 1 - i) * sizeof (pitch_t));
978 group->num_pitches--;
981 /* Remove a 'pitch' from 'group', (including any enharmonic pitch)
982 * See also: pitch_group_remove_pitch
985 pitch_group_remove_pitch_enharmonic (pitch_group_t *group, pitch_t pitch)
989 for (i = 0; i < group->num_pitches; i++)
990 if (_pitches_are_enharmonic (group->pitches[i], pitch))
991 pitch_group_remove_pitch_at (group, i);
994 /* Remove an exactly-matching pitch from 'group'
995 * See also: pitch_group_remove_pitch_enharmonic
998 pitch_group_remove_pitch (pitch_group_t *group, pitch_t pitch)
1002 for (i = 0; i < group->num_pitches; i++)
1003 if (group->pitches[i] == pitch)
1004 pitch_group_remove_pitch_at (group, i);
1009 scherzo_update_notes_and_chord (scherzo_t *scherzo)
1011 if (scherzo->notes_pressed.num_pitches == 0 &&
1012 scherzo->notes_pedaled.num_pitches == 0)
1014 score_remove_notes (scherzo->score);
1016 score_remove_chords (scherzo->score);
1018 gtk_widget_queue_draw (scherzo->window);
1023 scherzo_press_note (scherzo_t *scherzo, pitch_t pitch)
1025 void *local = talloc_new (NULL);
1026 pitch_group_t *chord;
1027 chord_signature_t *signature;
1028 char *chord_name = NULL;
1032 /* Do nothing if this note is already pressed. */
1033 for (i = 0; i < scherzo->notes_pressed.num_pitches; i++)
1034 if (scherzo->notes_pressed.pitches[i] == pitch)
1037 pitch_group_add_pitch (&scherzo->notes_pressed, pitch);
1039 if (scherzo->pedal_pressed)
1040 pitch_group_add_pitch (&scherzo->notes_pedaled, pitch);
1042 /* Remove all notes/chords from score, then add current notes. */
1043 score_remove_notes (scherzo->score);
1044 score_remove_chords (scherzo->score);
1046 if (scherzo->pedal_pressed)
1047 chord = &scherzo->notes_pedaled;
1049 chord = &scherzo->notes_pressed;
1051 _analyze_chord (chord, &signature, &root);
1054 /* Don't print root pitch for octaves and inversions,
1055 * (since a pitch name alone looks like a major triad) */
1056 if (signature->num_degrees < 3) {
1057 chord_name = talloc_strdup (local, signature->name);
1059 _spell_chord_by_signature (&scherzo->notes_pressed,
1061 _spell_chord_by_signature (&scherzo->notes_pedaled,
1064 chord_name = talloc_asprintf (local, "%s%s",
1065 pitch_string (*root),
1068 } else if (chord->num_pitches > 2) {
1069 chord_name = talloc_strdup (local, "?");
1073 scherzo->chord = score_add_chord (scherzo->treble, chord_name);
1075 for (i = 0; i < scherzo->notes_pressed.num_pitches; i++)
1077 score_add_note (scherzo->score, scherzo->notes_pressed.pitches[i],
1078 SCORE_DURATION_WHOLE);
1081 for (i = 0; i < scherzo->notes_pedaled.num_pitches; i++)
1083 score_add_note (scherzo->score, scherzo->notes_pedaled.pitches[i],
1084 SCORE_DURATION_WHOLE);
1087 talloc_free (local);
1091 scherzo_release_note (scherzo_t *scherzo, pitch_t pitch)
1093 pitch_group_remove_pitch_enharmonic (&scherzo->notes_pressed, pitch);
1095 if (scherzo->notes_pressed.num_pitches == 0)
1096 scherzo_update_notes_and_chord (scherzo);
1100 scherzo_press_note_midi (scherzo_t *scherzo, unsigned char midi_note)
1102 pitch_t pitch = _midi_to_pitch (midi_note);
1104 scherzo_press_note (scherzo, pitch);
1110 scherzo_release_note_midi (scherzo_t *scherzo, unsigned char midi_note)
1112 scherzo_release_note (scherzo, _midi_to_pitch (midi_note));
1116 scherzo_press_pedal (scherzo_t *scherzo)
1120 if (scherzo->pedal_pressed)
1123 scherzo->pedal_pressed = 1;
1125 /* Copy all pressed notes to pedaled notes */
1126 for (i = 0; i < scherzo->notes_pressed.num_pitches; i++)
1127 pitch_group_add_pitch (&scherzo->notes_pedaled, scherzo->notes_pressed.pitches[i]);
1131 scherzo_release_pedal (scherzo_t *scherzo)
1135 if (! scherzo->pedal_pressed)
1138 /* Remove all previously pedaled notes. */
1139 for (i = scherzo->notes_pedaled.num_pitches - 1; i >=0; i--)
1140 pitch_group_remove_pitch_at (&scherzo->notes_pedaled, i);
1142 scherzo->pedal_pressed = 0;
1144 scherzo_update_notes_and_chord (scherzo);
1148 _select_challenge (scherzo_t *scherzo)
1150 category_t *category_unused;
1151 bool_t introduced_unused;
1153 challenge_t *challenge = &scherzo->challenge;
1154 pitch_name_t pitch_name;
1158 mnemon_select_item (&scherzo->mnemon,
1160 &challenge->item_index,
1162 &introduced_unused);
1164 item = challenge->bin->items[challenge->item_index];
1166 s = item->challenge;
1167 if (strncmp (s, "treble:", 7) == 0) {
1169 challenge->staff = scherzo->treble;
1170 } else if (strncmp (s, "bass:", 5) == 0) {
1172 challenge->staff = scherzo->bass;
1175 "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
1182 pitch_name = PITCH_NAME_C;
1185 pitch_name = PITCH_NAME_D;
1188 pitch_name = PITCH_NAME_E;
1191 pitch_name = PITCH_NAME_F;
1194 pitch_name = PITCH_NAME_G;
1197 pitch_name = PITCH_NAME_A;
1200 pitch_name = PITCH_NAME_B;
1203 fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
1208 if (*s < '0' || *s > '9') {
1209 fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
1215 challenge->pitch = PITCH (pitch_name, PITCH_ACCIDENTAL_NATURAL, octave);
1217 challenge->active = 1;
1218 challenge->satisfied = 0;
1219 challenge->mistaken = 0;
1222 /* Determine whether the user hit the correct note. */
1224 _judge_pitch (scherzo_t *scherzo, pitch_t pitch)
1226 challenge_t *challenge = &scherzo->challenge;
1228 if (! challenge->active)
1231 if (pitch == challenge->pitch)
1232 challenge->satisfied = 1;
1234 challenge->mistaken = 1;
1237 /* If the user got the right note (eventually), then score it in
1238 * mnemon and show the next note. */
1240 _score_challenge (scherzo_t *scherzo)
1242 challenge_t *challenge = &scherzo->challenge;
1244 if (! challenge->active)
1247 if (! challenge->satisfied)
1250 mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
1251 ! challenge->mistaken);
1253 _select_challenge (scherzo);
1257 on_midi_input (unused (GIOChannel *channel),
1258 unused (GIOCondition condition),
1261 unsigned char buf[MIDI_BUF_SIZE], *next;
1262 scherzo_t *scherzo = user_data;
1264 snd_seq_event_t event;
1266 int need_redraw = FALSE;
1268 remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
1274 consumed = snd_midi_event_encode (scherzo->snd_midi_event,
1275 next, remaining, &event);
1277 remaining -= consumed;
1280 switch (event.type) {
1281 case SND_SEQ_EVENT_NONE:
1282 /* Incomplete event. Nothing to do. */
1284 case SND_SEQ_EVENT_NOTEON:
1285 pitch = scherzo_press_note_midi (scherzo, event.data.note.note);
1286 _judge_pitch (scherzo, pitch);
1289 case SND_SEQ_EVENT_NOTEOFF:
1290 scherzo_release_note_midi (scherzo, event.data.note.note);
1291 _score_challenge (scherzo);
1294 case SND_SEQ_EVENT_CLOCK:
1295 /* Ignore for now as my piano sends a constant stream of these. */
1297 case SND_SEQ_EVENT_SENSING:
1298 /* Ignore for now as my piano sends a constant stream of these. */
1300 case SND_SEQ_EVENT_CONTROLLER:
1301 /* XXX: My piano gives me 64 for the sustain pedal, is
1302 * that universal? */
1303 if (event.data.control.param == 64) {
1304 if (event.data.control.value == 0)
1305 scherzo_release_pedal (scherzo);
1307 scherzo_press_pedal (scherzo);
1309 fprintf (stderr, "Fixme: Unhandled MIDI Control event (param=%d, value=%d)\n",
1310 event.data.control.param, event.data.control.value);
1314 fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
1321 gtk_widget_queue_draw (scherzo->window);
1323 /* Return TRUE to continue to get called in the future. */
1328 main (int argc, char *argv[])
1330 GtkWidget *drawing_area;
1334 srand (time (NULL));
1336 gtk_init (&argc, &argv);
1338 scherzo.ctx = talloc_new (NULL);
1340 scherzo.score = score_create (scherzo.ctx);
1341 scherzo.staff_height = 100;
1342 score_set_staff_height (scherzo.score, scherzo.staff_height);
1344 score_add_brace (scherzo.score, 2);
1345 scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
1346 scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
1348 scherzo.chord = NULL;
1350 /* Default to octave 4 and natural for computer keyboard keypresses. */
1351 scherzo.keyboard_octave = 4;
1352 scherzo.keyboard_accidental = PITCH_ACCIDENTAL_NATURAL;
1354 pitch_group_init (scherzo.ctx, &scherzo.notes_pressed);
1355 pitch_group_init (scherzo.ctx, &scherzo.notes_pedaled);
1357 scherzo.pedal_pressed = 0;
1359 scherzo.key = PITCH_CLASS_LITERAL (C, NATURAL);
1360 score_set_key (scherzo.score, scherzo.key);
1362 mnemon_init (&scherzo.mnemon);
1363 /* XXX: Should create a default file if one cannot be loaded. */
1364 mnemon_load_category (&scherzo.mnemon, "scherzo-notes");
1366 scherzo.challenge.active = 0;
1368 _select_challenge (&scherzo);
1371 err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
1373 fprintf (stderr, "Out of memory.\n");
1377 #define MIDI_DEVICE "/dev/midi1"
1378 scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
1379 if (scherzo.midi_fd < 0) {
1380 printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
1382 GIOChannel *channel;
1384 channel = g_io_channel_unix_new (scherzo.midi_fd);
1385 g_io_channel_set_encoding (channel, NULL, NULL);
1386 g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
1389 scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1391 gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 1000, 600);
1393 g_signal_connect (scherzo.window, "delete-event",
1394 G_CALLBACK (on_delete_event_quit), NULL);
1396 drawing_area = gtk_drawing_area_new ();
1398 gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
1400 g_signal_connect (drawing_area, "expose-event",
1401 G_CALLBACK (on_expose_event_draw),
1404 g_signal_connect (scherzo.window, "key-press-event",
1405 G_CALLBACK (on_key_press_event),
1408 g_signal_connect (scherzo.window, "key-release-event",
1409 G_CALLBACK (on_key_release_event),
1412 gtk_widget_show_all (scherzo.window);
1416 mnemon_save (&scherzo.mnemon);
1418 snd_midi_event_free (scherzo.snd_midi_event);
1420 talloc_free (scherzo.ctx);