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
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;
83 /* Forward declarations. */
85 scherzo_press_note (scherzo_t *scherzo, pitch_t pitch);
88 scherzo_release_note (scherzo_t *scherzo, pitch_t pitch);
91 scherzo_press_pedal (scherzo_t *scherzo);
94 scherzo_release_pedal (scherzo_t *scherzo);
97 _judge_pitch (scherzo_t *scherzo, pitch_t pitch);
100 _score_challenge (scherzo_t *scherzo);
103 on_delete_event_quit (unused (GtkWidget *widget),
104 unused (GdkEvent *event),
105 unused (gpointer user_data))
109 /* Returning FALSE allows the default handler for delete-event
110 * to proceed to cleanup the widget. */
115 on_expose_event_draw (GtkWidget *widget,
116 unused (GdkEventExpose *expose),
119 scherzo_t *scherzo = user_data;
120 score_t *score = scherzo->score;
122 GtkAllocation allocation;
123 static const int pad = 10;
126 gtk_widget_get_allocation (widget, &allocation);
127 widget_width = allocation.width;
129 cr = gdk_cairo_create (widget->window);
131 /* White background */
132 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
135 /* Add some padding on the sides and top */
136 cairo_translate (cr, pad, scherzo->staff_height);
137 score_set_staff_height (score, scherzo->staff_height);
138 score_set_width (score, widget_width - 2 * pad);
140 score_draw (score, cr);
146 on_key_press_event (GtkWidget *widget,
150 scherzo_t *scherzo = user_data;
152 /* Initialize to keep the compiler quiet. */
153 pitch_name_t pitch_name = PITCH_NATURAL (C, 0);
156 if (scherzo->challenge.active)
157 octave = PITCH_OCTAVE (scherzo->challenge.pitch);
159 octave = scherzo->keyboard_octave;
161 switch (key->keyval) {
165 case GDK_KEY_KP_Equal:
166 scherzo->staff_height += 4;
167 gtk_widget_queue_draw (widget);
171 case GDK_KEY_KP_Subtract:
172 scherzo->staff_height -= 4;
173 gtk_widget_queue_draw (widget);
183 pitch_name = PITCH_NAME_C;
187 pitch_name = PITCH_NAME_D;
191 pitch_name = PITCH_NAME_E;
195 pitch_name = PITCH_NAME_F;
199 pitch_name = PITCH_NAME_G;
203 pitch_name = PITCH_NAME_A;
207 pitch_name = PITCH_NAME_B;
218 scherzo->keyboard_octave = key->keyval - GDK_KEY_0;
221 scherzo_press_pedal (scherzo);
224 if (scherzo->keyboard_accidental < PITCH_ACCIDENTAL_DOUBLE_SHARP)
225 scherzo->keyboard_accidental++;
228 if (scherzo->keyboard_accidental > PITCH_ACCIDENTAL_DOUBLE_FLAT)
229 scherzo->keyboard_accidental--;
233 pitch = PITCH (pitch_name, scherzo->keyboard_accidental, octave);
235 if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) ||
236 (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g))
238 scherzo_press_note (scherzo, pitch);
239 _judge_pitch (scherzo, pitch);
240 gtk_widget_queue_draw (scherzo->window);
246 /* Allow an unhandled event to propagate to other handlers. */
251 on_key_release_event (unused (GtkWidget *widget),
255 scherzo_t *scherzo = user_data;
257 /* Initialize to keep the compiler quiet. */
258 pitch_name_t pitch_name = PITCH_NAME_C;
261 if (scherzo->challenge.active)
262 octave = PITCH_OCTAVE (scherzo->challenge.pitch);
264 octave = scherzo->keyboard_octave;
266 switch (key->keyval) {
269 pitch_name = PITCH_NAME_C;
273 pitch_name = PITCH_NAME_D;
277 pitch_name = PITCH_NAME_E;
281 pitch_name = PITCH_NAME_F;
285 pitch_name = PITCH_NAME_G;
289 pitch_name = PITCH_NAME_A;
293 pitch_name = PITCH_NAME_B;
296 scherzo_release_pedal (scherzo);
300 pitch = PITCH (pitch_name, scherzo->keyboard_accidental, octave);
302 if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) ||
303 (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g))
305 scherzo_release_note (scherzo, pitch);
306 _score_challenge (scherzo);
307 gtk_widget_queue_draw (scherzo->window);
313 /* Allow an unhandled event to propagate to other handlers. */
318 _pitch_to_midi (pitch_t pitch)
320 int octave = PITCH_OCTAVE (pitch);
321 unsigned char midi_note = 12 * (octave + 1);
323 switch (PITCH_NAME (pitch)) {
346 switch (PITCH_ACCIDENTAL (pitch)) {
347 case PITCH_ACCIDENTAL_DOUBLE_FLAT:
350 case PITCH_ACCIDENTAL_FLAT:
353 case PITCH_ACCIDENTAL_NATURAL:
355 case PITCH_ACCIDENTAL_SHARP:
358 case PITCH_ACCIDENTAL_DOUBLE_SHARP:
366 /* octave can optionally by NULL */
368 _midi_to_pitch (unsigned char midi_note)
370 int octave = octave = midi_note / 12 - 1;
372 switch (midi_note % 12)
376 return PITCH_LITERAL (C, NATURAL, octave);
379 return PITCH_LITERAL (C, SHARP, octave);
382 return PITCH_LITERAL (D, NATURAL, octave);
385 return PITCH_LITERAL (D, SHARP, octave);
388 return PITCH_LITERAL (E, NATURAL, octave);
391 return PITCH_LITERAL (F, NATURAL, octave);
394 return PITCH_LITERAL (F, SHARP, octave);
397 return PITCH_LITERAL (G, NATURAL, octave);
400 return PITCH_LITERAL (G, SHARP, octave);
403 return PITCH_LITERAL (A, NATURAL, octave);
406 return PITCH_LITERAL (A, SHARP, octave);
409 return PITCH_LITERAL (B, NATURAL, octave);
414 /* Determine a chord name (if any) from the current notes pressed */
416 typedef struct analyzed_pitch {
417 /* Original note being analzyed. */
420 /* Absolute pitch (expressed as midi number). */
423 /* Pitch relative to bass note. */
428 _compare_analyzed_pitch_by_midi_pitch (const void *va, const void *vb)
430 const analyzed_pitch_t *a = va, *b = vb;
432 return a->midi_pitch - b->midi_pitch;
436 _compare_analyzed_pitch_by_relative_pitch (const void *va, const void *vb)
438 const analyzed_pitch_t *a = va, *b = vb;
440 return a->relative_pitch - b->relative_pitch;
443 typedef struct modified_degree
450 _modified_degree_to_half_steps (const modified_degree_t *degree)
453 int scale_degree = degree->degree;
455 /* Restrict to actual degrees within a scale. */
456 if (scale_degree > 7)
457 scale_degree = (scale_degree % 8) + 1;
459 /* Number of half steps from root to specified degree within a
461 switch (scale_degree) {
484 fprintf (stderr, "Internal: Invalid degree %d\n", degree->degree);
489 return half_steps + degree->modification;
492 /* Number of half steps from 'root' up to 'pitch' (that is, counting
493 * the steps up a chromatic scale), within the same octave. */
495 _pitch_from_root_in_half_steps (pitch_t pitch, pitch_t root)
497 int root_midi = _pitch_to_midi (root);
498 int pitch_midi = _pitch_to_midi (pitch);
500 while (pitch_midi < root_midi)
503 return (pitch_midi - root_midi) % 12;
507 _chord_signature_matches (analyzed_pitch_t *pitches,
509 modified_degree_t *degrees,
514 #define MAX_DEGREES 6
515 int relative_pitches[MAX_DEGREES];
518 assert (num_degrees <= MAX_DEGREES);
520 if (num_pitches != num_degrees)
523 if (inversion >= num_degrees)
526 /* We never spell simple intervals as inversions. */
527 if (num_degrees == 2 && inversion > 0)
530 for (i = 0; i < num_degrees; i++) {
531 /* The num_degrees is in the addition just to ensure all
532 * inputs to the modulus operator remain positive. */
533 int index = (i + num_degrees - inversion) % num_degrees;
535 /* Again, adding a 12 to keep things positive. */
536 relative_pitches[index] =
538 _modified_degree_to_half_steps (°rees[i]) -
539 _modified_degree_to_half_steps (°rees[inversion])) % 12;
543 for (i = 0; i < num_pitches; i++)
544 if (pitches[i].relative_pitch != relative_pitches[i])
547 root_index = (num_pitches - inversion) % num_pitches;
548 *root = pitches[root_index].pitch;
554 _pitch_str (pitch_t pitch)
556 static char double_flat[] = "X𝄫";
557 static char flat[] = "X♭";
558 static char natural[] = "X";
559 static char sharp[] = "X♯";
560 static char double_sharp[] = "X𝄪";
563 switch (PITCH_ACCIDENTAL (pitch)) {
564 case PITCH_ACCIDENTAL_DOUBLE_FLAT:
567 case PITCH_ACCIDENTAL_FLAT:
570 case PITCH_ACCIDENTAL_NATURAL:
573 case PITCH_ACCIDENTAL_SHARP:
576 case PITCH_ACCIDENTAL_DOUBLE_SHARP:
581 switch (PITCH_NAME(pitch)) {
608 typedef struct chord_signature
611 modified_degree_t degrees[MAX_DEGREES];
615 /* XXX: The superscript rise value should be relative to the current
616 * font size. Best would be for pango to support this. See:
618 * https://bugzilla.gnome.org/show_bug.cgi?id=708780
620 * For now, these are emipirically-derived values that look reasonable
621 * at the default size that score.c is using to draw the staff. If the
622 * user scales the staff up or down then these will look wrong.
625 #define SUP "<span font='33' rise='30000'>"
626 #define PUS "</span>"
628 chord_signature_t signatures[] = {
630 { 1, {{1, 0}}, "P8" },
633 { 2, {{1, 0}, {2, -1}}, "m2" },
634 { 2, {{1, 0}, {2, 0}}, "M2" },
635 { 2, {{1, 0}, {3, -1}}, "m3" },
636 { 2, {{1, 0}, {3, 0}}, "M3" },
637 { 2, {{1, 0}, {4, 0}}, "P4" },
638 { 2, {{1, 0}, {5, -1}}, "Tri-tone" },
639 { 2, {{1, 0}, {5, 0}}, "P5" },
640 { 2, {{1, 0}, {6, -1}}, "m6" },
641 { 2, {{1, 0}, {6, 0}}, "M6" },
642 { 2, {{1, 0}, {7, -1}}, "m7" },
643 { 2, {{1, 0}, {7, 0}}, "M7" },
646 { 3, {{1, 0}, {4, 0}, {5, 0}}, "sus" },
647 { 3, {{1, 0}, {3, 0}, {5, +1}}, SUP "+" PUS },
648 { 3, {{1, 0}, {3, 0}, {5, 0}}, "" },
649 { 3, {{1, 0}, {3, -1}, {5, 0}}, "m" },
650 { 3, {{1, 0}, {3, -1}, {5, -1}}, "°" },
651 { 3, {{1, 0}, {2, 0}, {5, 0}}, "msus2" },
653 /* Seventh chords with no 3rd */
654 { 3, {{1, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS },
655 { 3, {{1, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS },
656 { 3, {{1, 0}, {5, 0}, {7, 0}}, "M7" },
657 { 3, {{1, 0}, {5, 0}, {7, -1}}, "7" },
658 { 3, {{1, 0}, {5, -1}, {7, -1}}, "7♭5" },
660 /* Seventh chords with no 5th */
661 { 3, {{1, 0}, {4, 0}, {7, 0}}, "M7sus" },
662 { 3, {{1, 0}, {4, 0}, {7, -1}}, "7sus" },
663 { 3, {{1, 0}, {3, 0}, {7, 0}}, "M7" },
664 { 3, {{1, 0}, {3, 0}, {7, -1}}, "7" },
665 { 3, {{1, 0}, {3, -1}, {7, 0}}, "m" SUP "M7" PUS },
666 { 3, {{1, 0}, {3, -1}, {7, -1}}, "m7" },
667 { 3, {{1, 0}, {2, 0}, {7, 0}}, "m" SUP "M7" PUS "sus2" },
668 { 3, {{1, 0}, {2, 0}, {7, -1}}, "m7sus2" },
671 { 4, {{1, 0}, {4, 0}, {5, 0}, {6, 0}}, "6sus" },
672 { 4, {{1, 0}, {3, 0}, {5, 0}, {6, 0}}, "6" }, /* Ambiguous with m7 */
673 { 4, {{1, 0}, {3, -1}, {5, 0}, {6, 0}}, "m6" },
674 { 4, {{1, 0}, {2, 0}, {5, 0}, {6, 0}}, "m6sus2" },
677 { 4, {{1, 0}, {4, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS "sus" },
678 { 4, {{1, 0}, {4, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS "sus" },
679 { 4, {{1, 0}, {4, 0}, {5, 0}, {7, 0}}, "M7sus" },
680 { 4, {{1, 0}, {4, 0}, {5, 0}, {7, -1}}, "7sus" },
681 { 4, {{1, 0}, {4, 0}, {5, -1}, {7, -1}}, "7♭5sus" },
682 { 4, {{1, 0}, {3, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS },
683 { 4, {{1, 0}, {3, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS },
684 { 4, {{1, 0}, {3, 0}, {5, 0}, {7, 0}}, "M7" },
685 { 4, {{1, 0}, {3, 0}, {5, 0}, {7, -1}}, "7" },
686 { 4, {{1, 0}, {3, 0}, {5, -1}, {7, -1}}, "7♭5" },
687 { 4, {{1, 0}, {3, -1}, {5, 0}, {7, 0}}, "m" SUP "M7" PUS },
688 { 4, {{1, 0}, {3, -1}, {5, 0}, {7, -1}}, "m7" },
689 { 4, {{1, 0}, {3, -1}, {5, -1}, {7, 0}}, "°" SUP "M7" PUS },
690 { 4, {{1, 0}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "7" PUS },
691 { 4, {{1, 0}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "7" PUS },
692 { 4, {{1, 0}, {2, 0}, {5, 0}, {7, 0}}, "m" SUP "M7" PUS "sus2" },
693 { 4, {{1, 0}, {2, 0}, {5, 0}, {7, -1}}, "m7sus2" },
694 { 4, {{1, 0}, {2, 0}, {5, -1}, {7, 0}}, "°" SUP "M7" PUS "sus2" },
695 { 4, {{1, 0}, {2, 0}, {5, -1}, {7, -1}}, "𝆩" SUP "7" PUS "sus2" },
696 { 4, {{1, 0}, {2, 0}, {5, -1}, {7, -2}}, "°" SUP "7" PUS "sus2" }, /* Ambiguous with 7 */
698 /* The sorting for chords with degree higher than 9 is funny
699 * to keep the array in degree order after reducing each
700 * degree to an actual scale degree, (9 -> 2, 11 -> 4, 13 ->
703 /* Ninth chords voiced with no 5th */
704 { 4, {{1, 0}, {9, 0}, {4, 0}, {7, 0}}, "M9sus" },
705 { 4, {{1, 0}, {9, 0}, {4, 0}, {7, -1}}, "9sus" },
706 { 4, {{1, 0}, {9, 0}, {3, 0}, {7, 0}}, "M9" },
707 { 4, {{1, 0}, {9, 0}, {3, 0}, {7, -1}}, "9" },
708 { 4, {{1, 0}, {9, 0}, {3, -1}, {7, 0}}, "m" SUP "M9" PUS },
709 { 4, {{1, 0}, {9, 0}, {3, -1}, {7, -1}}, "m9" },
710 { 4, {{1, 0}, {9, -1}, {3, -1}, {7, -1}}, "m♭9" },
711 { 4, {{1, 0}, {9, 0}, {3, -1}, {6, 0}}, "m6/9" },
712 { 4, {{1, 0}, {9, -1}, {3, -1}, {6, 0}}, "m6/♭9" },
715 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {6, 0}}, "6/9" },
716 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {6, 0}}, "m6/9" },
717 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, 0}, {6, 0}}, "m6/♭9" },
719 /* Seventh plus altered 9 */
720 { 5, {{1, 0}, {9, +1}, {3, 0}, {5, 0}, {7, -1}}, "7♯9" },
721 { 5, {{1, 0}, {9, -1}, {3, 0}, {5, 0}, {7, -1}}, "7♭9" },
722 { 5, {{1, 0}, {9, +1}, {3, 0}, {5, -1}, {7, -1}}, "7♭5♯9" },
723 { 5, {{1, 0}, {9, -1}, {3, 0}, {5, -1}, {7, -1}}, "7♭5♭9" },
726 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, +1}, {7, 0}}, SUP "+M9" PUS "sus" },
727 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, +1}, {7, -1}}, SUP "+9" PUS "sus" },
728 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, 0}, {7, 0}}, "M9sus" },
729 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, 0}, {7, -1}}, "9sus" },
730 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, +1}, {7, 0}}, SUP "+M9" PUS },
731 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, +1}, {7, -1}}, SUP "+9" PUS },
732 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {7, 0}}, "M9" },
733 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {7, -1}}, "9" },
734 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {7, 0}}, "m" SUP "M9" PUS },
735 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {7, -1}}, "m9" },
736 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, 0}, {7, -1}}, "m♭9" },
737 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, -1}, {7, 0}}, "M9" SUP "♭5" PUS },
738 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, -1}, {7, -1}}, "9" SUP "♭5" PUS },
739 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, 0}}, "m" SUP "M9♭5" PUS },
740 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "9" PUS },
741 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "♭9" PUS },
742 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "9" PUS },
743 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "♭9" PUS },
745 /* Eleventh chords */
746 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, +1}, {7, 0}}, SUP "+M11" PUS },
747 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, +1}, {7, -1}}, SUP "+11" PUS },
748 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, 0}, {7, 0}}, "M11" },
749 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, 0}, {7, -1}}, "11" },
750 { 6, {{1, 0}, {9, 0}, {3, -1}, {11, 0}, {5, 0}, {7, 0}}, "m" SUP "M11" PUS },
751 { 6, {{1, 0}, {9, 0}, {3, -1}, {11, 0}, {5, 0}, {7, -1}}, "m11" },
752 { 6, {{1, 0}, {9, -1}, {3, -1}, {11, 0}, {5, -1}, {7, -1}}, "𝆩" SUP "11" PUS },
753 { 6, {{1, 0}, {9, -1}, {3, -1}, {11, -1}, {5, -1}, {7, -2}}, "°" SUP "11" PUS }
757 scherzo_adjust_pitch_to_match_modified_degree (pitch_t *pitch,
759 modified_degree_t *degree)
761 pitch_name_t name = PITCH_NAME (*pitch);
762 pitch_accidental_t accidental = PITCH_ACCIDENTAL (*pitch);
763 int octave = PITCH_OCTAVE (*pitch);
764 int degree_zero_based = (degree->degree - 1) % 7;
767 int note_degree_zero_based = name - PITCH_NAME (root);
768 if (note_degree_zero_based < 0)
769 note_degree_zero_based += 7;
771 if (note_degree_zero_based == degree_zero_based)
774 degree_delta = note_degree_zero_based - degree_zero_based;
775 if (degree_delta > 3)
776 degree_delta = - (7 - degree_delta);
777 if (degree_delta < -3)
778 degree_delta = - (-7 - degree_delta);
780 if (abs (degree_delta) != 1) {
781 fprintf (stderr, "Internal error: Cannot adjust a note more than one degree (%d vs. %d).\n", note_degree_zero_based + 1, degree->degree);
785 if (degree_delta == -1) {
786 if (name == PITCH_NAME_B) {
807 if (degree_delta == +1) {
808 if (name == PITCH_NAME_C) {
828 if (accidental < 0 || accidental > PITCH_ACCIDENTAL_DOUBLE_SHARP) {
829 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);
833 *pitch = PITCH (name, accidental, octave);
837 _spell_chord_by_signature (pitch_group_t *chord,
839 chord_signature_t *signature,
842 int i, degree, note_half_steps, degree_half_steps;
845 /* Sanitize root to eliminate things like double-flats,
846 * double-sharps, Cb, E#, etc. */
847 root_midi = _pitch_to_midi (root);
848 root = _midi_to_pitch (root_midi);
850 for (i = 0; i < num_notes; i++) {
851 note_half_steps = _pitch_from_root_in_half_steps (chord->pitches[i],
853 for (degree = 0; degree < signature->num_degrees; degree++) {
854 degree_half_steps = _modified_degree_to_half_steps (&signature->degrees[degree]);
855 if (note_half_steps == degree_half_steps) {
856 scherzo_adjust_pitch_to_match_modified_degree (&chord->pitches[i],
858 &signature->degrees[degree]);
862 if (note_half_steps != degree_half_steps) {
863 fprintf (stderr, "Internal error: Chord and degree mis-match\n");
870 scherzo_analyze_chord (scherzo_t *scherzo)
872 void *local = talloc_new (NULL);
873 analyzed_pitch_t *pitches;
874 pitch_group_t *pitch_group;
875 unsigned i, j, num_pitches;
876 int bass_pitch, inversion;
878 chord_signature_t *match = NULL;
881 if (scherzo->pedal_pressed)
882 pitch_group = &scherzo->notes_pedaled;
884 pitch_group = &scherzo->notes_pressed;
886 num_pitches = pitch_group->num_pitches;
888 if (scherzo->chord) {
889 score_remove_chord (scherzo->chord);
890 scherzo->chord = NULL;
893 if (num_pitches <= 1)
896 pitches = talloc_array (local, analyzed_pitch_t, num_pitches);
900 for (i = 0; i < num_pitches; i++) {
901 pitch_t pitch = pitch_group->pitches[i];
902 pitches[i].pitch = pitch;
903 pitches[i].midi_pitch = _pitch_to_midi (pitch);
904 /* Relative pitch will be filled in after sorting. */
905 pitches[i].relative_pitch = 0;
908 /* First, sort by midi pitch to find the bass note. */
909 qsort (pitches, num_pitches, sizeof (analyzed_pitch_t),
910 _compare_analyzed_pitch_by_midi_pitch);
912 bass_pitch = pitches[0].midi_pitch;
914 /* With the bass note identified, we can find all relative pitches. */
915 for (i = 0; i < num_pitches; i++) {
916 pitches[i].relative_pitch = pitches[i].midi_pitch - bass_pitch;
917 while (pitches[i].relative_pitch >= 12)
918 pitches[i].relative_pitch -= 12;
921 /* Now, sort again by relative pitch. */
922 qsort (pitches, num_pitches, sizeof (analyzed_pitch_t),
923 _compare_analyzed_pitch_by_relative_pitch);
925 /* Finally, eliminate all duplicate notes. */
926 for (i = 0; i < num_pitches - 1; i++) {
927 if (pitches[i+1].relative_pitch == pitches[i].relative_pitch) {
929 while (j < num_pitches &&
930 pitches[j].relative_pitch == pitches[i].relative_pitch)
934 /* The loop incremented j one past the last
935 * duplicate. Decrement so that it points to the
936 * last duplicate (and is guaranteed to not exceed
937 * the array bounds).*/
940 if (j < num_pitches - 1) {
941 memmove (&pitches[i+1], &pitches[j+1],
942 (num_pitches - j) * sizeof (analyzed_pitch_t));
945 num_pitches -= (j - i);
949 for (inversion = 0; inversion < 4; inversion++) {
950 for (i = 0; i < ARRAY_SIZE (signatures); i++) {
951 if (_chord_signature_matches (pitches, num_pitches,
952 signatures[i].degrees,
953 signatures[i].num_degrees,
956 match = &signatures[i];
963 chord_name = (char *) signatures[i].name;
966 /* Don't print root pitch for octaves and inversions,
967 * (since a pitch name alone looks like a major triad) */
968 if (num_pitches < 3) {
969 chord_name = talloc_strdup (local, chord_name);
971 chord_name = talloc_asprintf (local, "%s%s",
976 _spell_chord_by_signature (pitch_group, num_pitches,
979 chord_name = talloc_strdup (local, "?");
982 scherzo->chord = score_add_chord (scherzo->treble, chord_name);
989 pitch_group_init (void *ctx, pitch_group_t *group)
992 group->pitches = NULL;
994 group->num_pitches = 0;
998 pitch_group_add_pitch (pitch_group_t *group, pitch_t pitch)
1002 /* Do nothing if pitch is already in group. */
1003 for (i = 0; i < group->num_pitches; i++)
1004 if (group->pitches[i] == pitch)
1007 group->num_pitches++;
1009 if (group->num_pitches > group->size) {
1011 group->pitches = talloc_realloc (group->ctx, group->pitches,
1012 pitch_t, group->size);
1014 if (group->pitches == NULL) {
1015 fprintf (stderr, "Out of memory.\n");
1020 group->pitches[group->num_pitches - 1] = pitch;
1024 pitch_group_remove_pitch_at (pitch_group_t *group, int i)
1026 if (i > group->num_pitches) {
1027 fprintf (stderr, "Internal error: Cannot remove pitch %d from group with only %d %s\n",
1028 i, group->num_pitches,
1029 group->num_pitches == 1 ? "pitch" : "pitches");
1033 if (i < group->num_pitches - 1) {
1034 memmove (group->pitches + i, group->pitches + i + 1,
1035 (group->num_pitches - 1 - i) * sizeof (pitch_t));
1038 group->num_pitches--;
1042 pitch_group_remove_pitch (pitch_group_t *group, pitch_t pitch)
1046 for (i = 0; i < group->num_pitches; i++)
1047 if (group->pitches[i] == pitch)
1048 pitch_group_remove_pitch_at (group, i);
1052 scherzo_update_notes_and_chord (scherzo_t *scherzo)
1054 if (scherzo->notes_pressed.num_pitches == 0 &&
1055 scherzo->notes_pedaled.num_pitches == 0)
1057 score_remove_notes (scherzo->score);
1060 score_remove_chord (scherzo->chord);
1062 gtk_widget_queue_draw (scherzo->window);
1067 scherzo_press_note (scherzo_t *scherzo, pitch_t pitch)
1071 /* Do nothing if this note is already pressed. */
1072 for (i = 0; i < scherzo->notes_pressed.num_pitches; i++)
1073 if (scherzo->notes_pressed.pitches[i] == pitch)
1076 pitch_group_add_pitch (&scherzo->notes_pressed, pitch);
1078 if (scherzo->pedal_pressed)
1079 pitch_group_add_pitch (&scherzo->notes_pedaled, pitch);
1081 /* Remove all notes from score, then add current notes. */
1082 score_remove_notes (scherzo->score);
1084 for (i = 0; i < scherzo->notes_pressed.num_pitches; i++)
1086 score_add_note (scherzo->score, scherzo->notes_pressed.pitches[i],
1087 SCORE_DURATION_WHOLE);
1090 for (i = 0; i < scherzo->notes_pedaled.num_pitches; i++)
1092 score_add_note (scherzo->score, scherzo->notes_pedaled.pitches[i],
1093 SCORE_DURATION_WHOLE);
1096 scherzo_analyze_chord (scherzo);
1100 scherzo_release_note (scherzo_t *scherzo, pitch_t pitch)
1102 pitch_group_remove_pitch (&scherzo->notes_pressed, pitch);
1104 if (scherzo->notes_pressed.num_pitches == 0)
1105 scherzo_update_notes_and_chord (scherzo);
1109 scherzo_press_note_midi (scherzo_t *scherzo, unsigned char midi_note)
1111 pitch_t pitch = _midi_to_pitch (midi_note);
1113 scherzo_press_note (scherzo, pitch);
1119 scherzo_release_note_midi (scherzo_t *scherzo, unsigned char midi_note)
1121 scherzo_release_note (scherzo, _midi_to_pitch (midi_note));
1125 scherzo_press_pedal (scherzo_t *scherzo)
1129 scherzo->pedal_pressed = 1;
1131 /* Copy all pressed notes to pedaled notes */
1132 for (i = 0; i < scherzo->notes_pressed.num_pitches; i++)
1133 pitch_group_add_pitch (&scherzo->notes_pedaled, scherzo->notes_pressed.pitches[i]);
1137 scherzo_release_pedal (scherzo_t *scherzo)
1141 /* Remove all previously pedaled notes. */
1142 for (i = scherzo->notes_pedaled.num_pitches - 1; i >=0; i--)
1143 pitch_group_remove_pitch_at (&scherzo->notes_pedaled, i);
1145 scherzo->pedal_pressed = 0;
1147 scherzo_update_notes_and_chord (scherzo);
1151 _select_challenge (scherzo_t *scherzo)
1153 category_t *category_unused;
1154 bool_t introduced_unused;
1156 challenge_t *challenge = &scherzo->challenge;
1157 pitch_name_t pitch_name;
1161 mnemon_select_item (&scherzo->mnemon,
1163 &challenge->item_index,
1165 &introduced_unused);
1167 item = challenge->bin->items[challenge->item_index];
1169 s = item->challenge;
1170 if (strncmp (s, "treble:", 7) == 0) {
1172 challenge->staff = scherzo->treble;
1173 } else if (strncmp (s, "bass:", 5) == 0) {
1175 challenge->staff = scherzo->bass;
1178 "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
1185 pitch_name = PITCH_NAME_C;
1188 pitch_name = PITCH_NAME_D;
1191 pitch_name = PITCH_NAME_E;
1194 pitch_name = PITCH_NAME_F;
1197 pitch_name = PITCH_NAME_G;
1200 pitch_name = PITCH_NAME_A;
1203 pitch_name = PITCH_NAME_B;
1206 fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
1211 if (*s < '0' || *s > '9') {
1212 fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
1218 challenge->pitch = PITCH (pitch_name, PITCH_ACCIDENTAL_NATURAL, octave);
1220 challenge->active = 1;
1221 challenge->satisfied = 0;
1222 challenge->mistaken = 0;
1225 /* Determine whether the user hit the correct note. */
1227 _judge_pitch (scherzo_t *scherzo, pitch_t pitch)
1229 challenge_t *challenge = &scherzo->challenge;
1231 if (! challenge->active)
1234 if (pitch == challenge->pitch)
1235 challenge->satisfied = 1;
1237 challenge->mistaken = 1;
1240 /* If the user got the right note (eventually), then score it in
1241 * mnemon and show the next note. */
1243 _score_challenge (scherzo_t *scherzo)
1245 challenge_t *challenge = &scherzo->challenge;
1247 if (! challenge->active)
1250 if (! challenge->satisfied)
1253 mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
1254 ! challenge->mistaken);
1256 _select_challenge (scherzo);
1260 on_midi_input (unused (GIOChannel *channel),
1261 unused (GIOCondition condition),
1264 unsigned char buf[MIDI_BUF_SIZE], *next;
1265 scherzo_t *scherzo = user_data;
1267 snd_seq_event_t event;
1269 int need_redraw = FALSE;
1271 remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
1277 consumed = snd_midi_event_encode (scherzo->snd_midi_event,
1278 next, remaining, &event);
1280 remaining -= consumed;
1283 switch (event.type) {
1284 case SND_SEQ_EVENT_NONE:
1285 /* Incomplete event. Nothing to do. */
1287 case SND_SEQ_EVENT_NOTEON:
1288 pitch = scherzo_press_note_midi (scherzo, event.data.note.note);
1289 _judge_pitch (scherzo, pitch);
1292 case SND_SEQ_EVENT_NOTEOFF:
1293 scherzo_release_note_midi (scherzo, event.data.note.note);
1294 _score_challenge (scherzo);
1297 case SND_SEQ_EVENT_CLOCK:
1298 /* Ignore for now as my piano sends a constant stream of these. */
1300 case SND_SEQ_EVENT_SENSING:
1301 /* Ignore for now as my piano sends a constant stream of these. */
1303 case SND_SEQ_EVENT_CONTROLLER:
1304 /* XXX: My piano gives me 64 for the sustain pedal, is
1305 * that universal? */
1306 if (event.data.control.param == 64) {
1307 if (event.data.control.value == 0)
1308 scherzo_release_pedal (scherzo);
1310 scherzo_press_pedal (scherzo);
1312 fprintf (stderr, "Fixme: Unhandled MIDI Control event (param=%d, value=%d)\n",
1313 event.data.control.param, event.data.control.value);
1317 fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
1324 gtk_widget_queue_draw (scherzo->window);
1326 /* Return TRUE to continue to get called in the future. */
1331 main (int argc, char *argv[])
1333 GtkWidget *drawing_area;
1337 srand (time (NULL));
1339 gtk_init (&argc, &argv);
1341 scherzo.ctx = talloc_new (NULL);
1343 scherzo.score = score_create (scherzo.ctx);
1344 scherzo.staff_height = 100;
1345 score_set_staff_height (scherzo.score, scherzo.staff_height);
1347 score_add_brace (scherzo.score, 2);
1348 scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
1349 scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
1351 scherzo.chord = NULL;
1353 /* Default to octave 4 and natural for computer keyboard keypresses. */
1354 scherzo.keyboard_octave = 4;
1355 scherzo.keyboard_accidental = PITCH_ACCIDENTAL_NATURAL;
1357 pitch_group_init (scherzo.ctx, &scherzo.notes_pressed);
1358 pitch_group_init (scherzo.ctx, &scherzo.notes_pedaled);
1360 scherzo.pedal_pressed = 0;
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);