1 /* scherzo - Music notation training
3 * Copyright © 2010 Carl Worth
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see http://www.gnu.org/licenses/ .
20 #include <gdk/gdkkeysyms.h>
22 #include <asoundlib.h>
27 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
29 #define unused(foo) foo __attribute__((unused))
31 #define MIDI_BUF_SIZE 4096
33 typedef struct challenge
44 typedef struct note_group
52 typedef struct scherzo
59 score_staff_t *treble;
63 /* The word "keyboard" here is referring to a "computer
64 * keyboard". Any "piano keyboard" key knows its own octave and
65 * accidental already. */
67 score_pitch_accidental_t keyboard_accidental;
70 snd_midi_event_t *snd_midi_event;
73 challenge_t challenge;
75 note_group_t notes_pressed;
76 note_group_t notes_pedaled;
81 /* Forward declarations. */
83 scherzo_press_note (scherzo_t *scherzo, score_pitch_t pitch, int octave);
86 scherzo_release_note (scherzo_t *scherzo, score_pitch_t pitch, int octave);
89 scherzo_press_pedal (scherzo_t *scherzo);
92 scherzo_release_pedal (scherzo_t *scherzo);
95 _judge_note (scherzo_t *scherzo, score_note_t *note);
98 _score_challenge (scherzo_t *scherzo);
101 on_delete_event_quit (unused (GtkWidget *widget),
102 unused (GdkEvent *event),
103 unused (gpointer user_data))
107 /* Returning FALSE allows the default handler for delete-event
108 * to proceed to cleanup the widget. */
113 on_expose_event_draw (GtkWidget *widget,
114 unused (GdkEventExpose *expose),
117 scherzo_t *scherzo = user_data;
118 score_t *score = scherzo->score;
120 GtkAllocation allocation;
121 static const int pad = 10;
124 gtk_widget_get_allocation (widget, &allocation);
125 widget_width = allocation.width;
127 cr = gdk_cairo_create (widget->window);
129 /* White background */
130 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
133 /* Add some padding on the sides and top */
134 cairo_translate (cr, pad, scherzo->staff_height);
135 score_set_staff_height (score, scherzo->staff_height);
136 score_set_width (score, widget_width - 2 * pad);
138 score_draw (score, cr);
144 on_key_press_event (GtkWidget *widget,
148 scherzo_t *scherzo = user_data;
150 /* Initialize to keep the compiler quiet. */
151 score_pitch_name_t pitch_name = SCORE_PITCH_C;
154 if (scherzo->challenge.note)
155 octave = scherzo->challenge.note->octave;
157 octave = scherzo->keyboard_octave;
159 switch (key->keyval) {
163 case GDK_KEY_KP_Equal:
164 scherzo->staff_height += 4;
165 gtk_widget_queue_draw (widget);
169 case GDK_KEY_KP_Subtract:
170 scherzo->staff_height -= 4;
171 gtk_widget_queue_draw (widget);
181 pitch_name = SCORE_PITCH_NAME_C;
185 pitch_name = SCORE_PITCH_NAME_D;
189 pitch_name = SCORE_PITCH_NAME_E;
193 pitch_name = SCORE_PITCH_NAME_F;
197 pitch_name = SCORE_PITCH_NAME_G;
201 pitch_name = SCORE_PITCH_NAME_A;
205 pitch_name = SCORE_PITCH_NAME_B;
216 scherzo->keyboard_octave = key->keyval - GDK_KEY_0;
219 scherzo_press_pedal (scherzo);
222 if (scherzo->keyboard_accidental < SCORE_PITCH_ACCIDENTAL_DOUBLE_SHARP)
223 scherzo->keyboard_accidental++;
226 if (scherzo->keyboard_accidental > SCORE_PITCH_ACCIDENTAL_DOUBLE_FLAT)
227 scherzo->keyboard_accidental--;
231 pitch = SCORE_PITCH (pitch_name, scherzo->keyboard_accidental);
233 if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) ||
234 (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g))
238 note = scherzo_press_note (scherzo, pitch, octave);
239 _judge_note (scherzo, note);
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 score_pitch_name_t pitch_name = SCORE_PITCH_NAME_C;
261 if (scherzo->challenge.note)
262 octave = scherzo->challenge.note->octave;
264 octave = scherzo->keyboard_octave;
266 switch (key->keyval) {
269 pitch_name = SCORE_PITCH_NAME_C;
273 pitch_name = SCORE_PITCH_NAME_D;
277 pitch_name = SCORE_PITCH_NAME_E;
281 pitch_name = SCORE_PITCH_NAME_F;
285 pitch_name = SCORE_PITCH_NAME_G;
289 pitch_name = SCORE_PITCH_NAME_A;
293 pitch_name = SCORE_PITCH_NAME_B;
296 scherzo_release_pedal (scherzo);
300 pitch = SCORE_PITCH (pitch_name, scherzo->keyboard_accidental);
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, octave);
306 _score_challenge (scherzo);
307 gtk_widget_queue_draw (scherzo->window);
313 /* Allow an unhandled event to propagate to other handlers. */
318 _score_pitch_and_octave_to_midi (score_pitch_t pitch,
321 unsigned char midi_note = 12 * (octave + 1);
323 switch (SCORE_PITCH_NAME (pitch)) {
324 case SCORE_PITCH_NAME_C:
326 case SCORE_PITCH_NAME_D:
329 case SCORE_PITCH_NAME_E:
332 case SCORE_PITCH_NAME_F:
335 case SCORE_PITCH_NAME_G:
338 case SCORE_PITCH_NAME_A:
341 case SCORE_PITCH_NAME_B:
346 switch (SCORE_PITCH_ACCIDENTAL (pitch)) {
347 case SCORE_PITCH_ACCIDENTAL_DOUBLE_FLAT:
350 case SCORE_PITCH_ACCIDENTAL_FLAT:
353 case SCORE_PITCH_ACCIDENTAL_NATURAL:
355 case SCORE_PITCH_ACCIDENTAL_SHARP:
358 case SCORE_PITCH_ACCIDENTAL_DOUBLE_SHARP:
366 /* octave can optionally by NULL */
368 _midi_to_score_pitch_and_octave (unsigned char midi_note,
369 score_pitch_t *pitch,
373 *octave = midi_note / 12 - 1;
375 switch (midi_note % 12)
378 *pitch = SCORE_PITCH_C;
381 *pitch = SCORE_PITCH_Cs;
384 *pitch = SCORE_PITCH_D;
387 *pitch = SCORE_PITCH_Ds;
390 *pitch = SCORE_PITCH_E;
393 *pitch = SCORE_PITCH_F;
396 *pitch = SCORE_PITCH_Fs;
399 *pitch = SCORE_PITCH_G;
402 *pitch = SCORE_PITCH_Gs;
405 *pitch = SCORE_PITCH_A;
408 *pitch = SCORE_PITCH_As;
411 *pitch = SCORE_PITCH_B;
416 /* Determine a chord name (if any) from the current notes pressed */
418 typedef struct analyzed_note {
419 /* Original note being analzyed. */
422 /* Absolute pitch (expressed as midi number). */
425 /* Pitch relative to bass note. */
430 _compare_analyzed_note_by_midi_pitch (const void *va, const void *vb)
432 const analyzed_note_t *a = va, *b = vb;
434 return a->midi_pitch - b->midi_pitch;
438 _compare_analyzed_note_by_relative_pitch (const void *va, const void *vb)
440 const analyzed_note_t *a = va, *b = vb;
442 return a->relative_pitch - b->relative_pitch;
445 typedef struct modified_degree
452 _modified_degree_to_half_steps (const modified_degree_t *degree)
455 int scale_degree = degree->degree;
457 /* Restrict to actual degrees within a scale. */
458 if (scale_degree > 7)
459 scale_degree = (scale_degree % 8) + 1;
461 /* Number of half steps from root to specified degree within a
463 switch (scale_degree) {
486 fprintf (stderr, "Internal: Invalid degree %d\n", degree->degree);
491 return half_steps + degree->modification;
494 /* Number of half steps from 'root' up to 'pitch' (that is, counting
495 * the steps up a chromatic scale). */
497 _pitch_from_root_in_half_steps (score_pitch_t pitch, score_pitch_t root)
499 int root_midi = _score_pitch_and_octave_to_midi (root, 0);
500 int pitch_midi = _score_pitch_and_octave_to_midi (pitch, 0);
502 if (pitch_midi < root_midi)
505 if (pitch_midi < root_midi) {
506 fprintf (stderr, "Internal error: midi values differ by more than expected.\n");
510 return (pitch_midi - root_midi) % 12;
514 _chord_signature_matches (analyzed_note_t *notes,
516 modified_degree_t *degrees,
521 #define MAX_DEGREES 6
522 int relative_pitches[MAX_DEGREES];
525 assert (num_degrees <= MAX_DEGREES);
527 if (num_notes != num_degrees)
530 if (inversion >= num_degrees)
533 /* We never spell simple intervals as inversions. */
534 if (num_degrees == 2 && inversion > 0)
537 for (i = 0; i < num_degrees; i++) {
538 /* The num_degrees is in the addition just to ensure all
539 * inputs to the modulus operator remain positive. */
540 int index = (i + num_degrees - inversion) % num_degrees;
542 /* Again, adding a 12 to keep things positive. */
543 relative_pitches[index] =
545 _modified_degree_to_half_steps (°rees[i]) -
546 _modified_degree_to_half_steps (°rees[inversion])) % 12;
550 for (i = 0; i < num_notes; i++)
551 if (notes[i].relative_pitch != relative_pitches[i])
554 root_index = (num_notes - inversion) % num_notes;
555 *root = notes[root_index].note->pitch;
561 _pitch_str (score_pitch_t pitch)
564 case SCORE_PITCH_Cff:
572 case SCORE_PITCH_Css:
574 case SCORE_PITCH_Dff:
582 case SCORE_PITCH_Dss:
584 case SCORE_PITCH_Eff:
592 case SCORE_PITCH_Ess:
594 case SCORE_PITCH_Fff:
602 case SCORE_PITCH_Fss:
604 case SCORE_PITCH_Gff:
612 case SCORE_PITCH_Gss:
614 case SCORE_PITCH_Aff:
622 case SCORE_PITCH_Ass:
624 case SCORE_PITCH_Bff:
632 case SCORE_PITCH_Bss:
636 fprintf (stderr, "Internal error: Unknown pitch %d\n", pitch);
640 typedef struct chord_signature
643 modified_degree_t degrees[MAX_DEGREES];
647 /* XXX: The superscript rise value should be relative to the current
648 * font size. Best would be for pango to support this. See:
650 * https://bugzilla.gnome.org/show_bug.cgi?id=708780
652 * For now, these are emipirically-derived values that look reasonable
653 * at the default size that score.c is using to draw the staff. If the
654 * user scales the staff up or down then these will look wrong.
657 #define SUP "<span font='33' rise='30000'>"
658 #define PUS "</span>"
660 chord_signature_t signatures[] = {
662 { 1, {{1, 0}}, "P8" },
665 { 2, {{1, 0}, {2, -1}}, "m2" },
666 { 2, {{1, 0}, {2, 0}}, "M2" },
667 { 2, {{1, 0}, {3, -1}}, "m3" },
668 { 2, {{1, 0}, {3, 0}}, "M3" },
669 { 2, {{1, 0}, {4, 0}}, "P4" },
670 { 2, {{1, 0}, {5, -1}}, "Tri-tone" },
671 { 2, {{1, 0}, {5, 0}}, "P5" },
672 { 2, {{1, 0}, {6, -1}}, "m6" },
673 { 2, {{1, 0}, {6, 0}}, "M6" },
674 { 2, {{1, 0}, {7, -1}}, "m7" },
675 { 2, {{1, 0}, {7, 0}}, "M7" },
678 { 3, {{1, 0}, {4, 0}, {5, 0}}, "sus" },
679 { 3, {{1, 0}, {3, 0}, {5, +1}}, SUP "+" PUS },
680 { 3, {{1, 0}, {3, 0}, {5, 0}}, "" },
681 { 3, {{1, 0}, {3, -1}, {5, 0}}, "m" },
682 { 3, {{1, 0}, {3, -1}, {5, -1}}, "°" },
683 { 3, {{1, 0}, {2, 0}, {5, 0}}, "msus2" },
685 /* Seventh chords with no 3rd */
686 { 3, {{1, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS },
687 { 3, {{1, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS },
688 { 3, {{1, 0}, {5, 0}, {7, 0}}, "M7" },
689 { 3, {{1, 0}, {5, 0}, {7, -1}}, "7" },
690 { 3, {{1, 0}, {5, -1}, {7, -1}}, "7♭5" },
692 /* Seventh chords with no 5th */
693 { 3, {{1, 0}, {4, 0}, {7, 0}}, "M7sus" },
694 { 3, {{1, 0}, {4, 0}, {7, -1}}, "7sus" },
695 { 3, {{1, 0}, {3, 0}, {7, 0}}, "M7" },
696 { 3, {{1, 0}, {3, 0}, {7, -1}}, "7" },
697 { 3, {{1, 0}, {3, -1}, {7, 0}}, "m" SUP "M7" PUS },
698 { 3, {{1, 0}, {3, -1}, {7, -1}}, "m7" },
699 { 3, {{1, 0}, {2, 0}, {7, 0}}, "m" SUP "M7" PUS "sus2" },
700 { 3, {{1, 0}, {2, 0}, {7, -1}}, "m7sus2" },
703 { 4, {{1, 0}, {4, 0}, {5, 0}, {6, 0}}, "6sus" },
704 { 4, {{1, 0}, {3, 0}, {5, 0}, {6, 0}}, "6" }, /* Ambiguous with m7 */
705 { 4, {{1, 0}, {3, -1}, {5, 0}, {6, 0}}, "m6" },
706 { 4, {{1, 0}, {2, 0}, {5, 0}, {6, 0}}, "m6sus2" },
709 { 4, {{1, 0}, {4, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS "sus" },
710 { 4, {{1, 0}, {4, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS "sus" },
711 { 4, {{1, 0}, {4, 0}, {5, 0}, {7, 0}}, "M7sus" },
712 { 4, {{1, 0}, {4, 0}, {5, 0}, {7, -1}}, "7sus" },
713 { 4, {{1, 0}, {4, 0}, {5, -1}, {7, -1}}, "7♭5sus" },
714 { 4, {{1, 0}, {3, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS },
715 { 4, {{1, 0}, {3, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS },
716 { 4, {{1, 0}, {3, 0}, {5, 0}, {7, 0}}, "M7" },
717 { 4, {{1, 0}, {3, 0}, {5, 0}, {7, -1}}, "7" },
718 { 4, {{1, 0}, {3, 0}, {5, -1}, {7, -1}}, "7♭5" },
719 { 4, {{1, 0}, {3, -1}, {5, 0}, {7, 0}}, "m" SUP "M7" PUS },
720 { 4, {{1, 0}, {3, -1}, {5, 0}, {7, -1}}, "m7" },
721 { 4, {{1, 0}, {3, -1}, {5, -1}, {7, 0}}, "°" SUP "M7" PUS },
722 { 4, {{1, 0}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "7" PUS },
723 { 4, {{1, 0}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "7" PUS },
724 { 4, {{1, 0}, {2, 0}, {5, 0}, {7, 0}}, "m" SUP "M7" PUS "sus2" },
725 { 4, {{1, 0}, {2, 0}, {5, 0}, {7, -1}}, "m7sus2" },
726 { 4, {{1, 0}, {2, 0}, {5, -1}, {7, 0}}, "°" SUP "M7" PUS "sus2" },
727 { 4, {{1, 0}, {2, 0}, {5, -1}, {7, -1}}, "𝆩" SUP "7" PUS "sus2" },
728 { 4, {{1, 0}, {2, 0}, {5, -1}, {7, -2}}, "°" SUP "7" PUS "sus2" }, /* Ambiguous with 7 */
730 /* The sorting for chords with degree higher than 9 is funny
731 * to keep the array in degree order after reducing each
732 * degree to an actual scale degree, (9 -> 2, 11 -> 4, 13 ->
735 /* Ninth chords voiced with no 5th */
736 { 4, {{1, 0}, {9, 0}, {4, 0}, {7, 0}}, "M9sus" },
737 { 4, {{1, 0}, {9, 0}, {4, 0}, {7, -1}}, "9sus" },
738 { 4, {{1, 0}, {9, 0}, {3, 0}, {7, 0}}, "M9" },
739 { 4, {{1, 0}, {9, 0}, {3, 0}, {7, -1}}, "9" },
740 { 4, {{1, 0}, {9, 0}, {3, -1}, {7, 0}}, "m" SUP "M9" PUS },
741 { 4, {{1, 0}, {9, 0}, {3, -1}, {7, -1}}, "m9" },
742 { 4, {{1, 0}, {9, -1}, {3, -1}, {7, -1}}, "m♭9" },
743 { 4, {{1, 0}, {9, 0}, {3, -1}, {6, 0}}, "m6/9" },
744 { 4, {{1, 0}, {9, -1}, {3, -1}, {6, 0}}, "m6/♭9" },
747 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {6, 0}}, "6/9" },
748 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {6, 0}}, "m6/9" },
749 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, 0}, {6, 0}}, "m6/♭9" },
751 /* Seventh plus altered 9 */
752 { 5, {{1, 0}, {9, +1}, {3, 0}, {5, 0}, {7, -1}}, "7♯9" },
753 { 5, {{1, 0}, {9, -1}, {3, 0}, {5, 0}, {7, -1}}, "7♭9" },
754 { 5, {{1, 0}, {9, +1}, {3, 0}, {5, -1}, {7, -1}}, "7♭5♯9" },
755 { 5, {{1, 0}, {9, -1}, {3, 0}, {5, -1}, {7, -1}}, "7♭5♭9" },
758 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, +1}, {7, 0}}, SUP "+M9" PUS "sus" },
759 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, +1}, {7, -1}}, SUP "+9" PUS "sus" },
760 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, 0}, {7, 0}}, "M9sus" },
761 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, 0}, {7, -1}}, "9sus" },
762 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, +1}, {7, 0}}, SUP "+M9" PUS },
763 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, +1}, {7, -1}}, SUP "+9" PUS },
764 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {7, 0}}, "M9" },
765 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {7, -1}}, "9" },
766 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {7, 0}}, "m" SUP "M9" PUS },
767 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {7, -1}}, "m9" },
768 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, 0}, {7, -1}}, "m♭9" },
769 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, -1}, {7, 0}}, "M9" SUP "♭5" PUS },
770 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, -1}, {7, -1}}, "9" SUP "♭5" PUS },
771 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, 0}}, "m" SUP "M9♭5" PUS },
772 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "9" PUS },
773 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "♭9" PUS },
774 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "9" PUS },
775 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "♭9" PUS },
777 /* Eleventh chords */
778 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, +1}, {7, 0}}, SUP "+M11" PUS },
779 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, +1}, {7, -1}}, SUP "+11" PUS },
780 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, 0}, {7, 0}}, "M11" },
781 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, 0}, {7, -1}}, "11" },
782 { 6, {{1, 0}, {9, 0}, {3, -1}, {11, 0}, {5, 0}, {7, 0}}, "m" SUP "M11" PUS },
783 { 6, {{1, 0}, {9, 0}, {3, -1}, {11, 0}, {5, 0}, {7, -1}}, "m11" },
784 { 6, {{1, 0}, {9, -1}, {3, -1}, {11, 0}, {5, -1}, {7, -1}}, "𝆩" SUP "11" PUS },
785 { 6, {{1, 0}, {9, -1}, {3, -1}, {11, -1}, {5, -1}, {7, -2}}, "°" SUP "11" PUS }
789 scherzo_adjust_note_to_match_modified_degree (score_note_t *note,
791 modified_degree_t *degree)
793 score_pitch_name_t name = SCORE_PITCH_NAME (note->pitch);
794 score_pitch_accidental_t accidental = SCORE_PITCH_ACCIDENTAL (note->pitch);
795 int degree_zero_based = (degree->degree - 1) % 7;
798 int note_degree_zero_based = name - SCORE_PITCH_NAME (root);
799 if (note_degree_zero_based < 0)
800 note_degree_zero_based += 7;
802 if (note_degree_zero_based == degree_zero_based)
805 degree_delta = note_degree_zero_based - degree_zero_based;
806 if (degree_delta > 3)
807 degree_delta = - (7 - degree_delta);
808 if (degree_delta < -3)
809 degree_delta = - (-7 - degree_delta);
811 if (abs (degree_delta) != 1) {
812 fprintf (stderr, "Internal error: Cannot adjust a note more than one degree (%d vs. %d).\n", note_degree_zero_based + 1, degree->degree);
816 if (degree_delta == -1) {
817 if (name == SCORE_PITCH_NAME_B) {
818 name = SCORE_PITCH_NAME_C;
824 case SCORE_PITCH_NAME_D:
825 case SCORE_PITCH_NAME_E:
826 case SCORE_PITCH_NAME_G:
827 case SCORE_PITCH_NAME_A:
828 case SCORE_PITCH_NAME_B:
831 case SCORE_PITCH_NAME_C:
832 case SCORE_PITCH_NAME_F:
838 if (degree_delta == +1) {
839 if (name == SCORE_PITCH_NAME_C) {
840 name = SCORE_PITCH_NAME_B;
846 case SCORE_PITCH_NAME_C:
847 case SCORE_PITCH_NAME_D:
848 case SCORE_PITCH_NAME_F:
849 case SCORE_PITCH_NAME_G:
850 case SCORE_PITCH_NAME_A:
853 case SCORE_PITCH_NAME_E:
854 case SCORE_PITCH_NAME_B:
859 if (accidental < 0 || accidental > SCORE_PITCH_ACCIDENTAL_DOUBLE_SHARP) {
860 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);
864 note->pitch = SCORE_PITCH (name, accidental);
868 _spell_chord_by_signature (note_group_t *chord,
870 chord_signature_t *signature,
873 int i, degree, note_half_steps, degree_half_steps;
876 /* Sanitize root to eliminate things like double-flats,
877 * double-sharps, Cb, E#, etc. */
878 root_midi = _score_pitch_and_octave_to_midi (root, 0);
879 _midi_to_score_pitch_and_octave (root_midi, &root, NULL);
881 for (i = 0; i < num_notes; i++) {
882 note_half_steps = _pitch_from_root_in_half_steps (chord->notes[i]->pitch,
884 for (degree = 0; degree < signature->num_degrees; degree++) {
885 degree_half_steps = _modified_degree_to_half_steps (&signature->degrees[degree]);
886 if (note_half_steps == degree_half_steps) {
887 scherzo_adjust_note_to_match_modified_degree (chord->notes[i],
889 &signature->degrees[degree]);
893 if (note_half_steps != degree_half_steps) {
894 fprintf (stderr, "Internal error: Chord and degree mis-match\n");
901 scherzo_analyze_chord (scherzo_t *scherzo)
903 void *local = talloc_new (NULL);
904 analyzed_note_t *notes;
905 note_group_t *note_group;
906 unsigned i, j, num_notes;
907 int bass_pitch, inversion;
909 chord_signature_t *match = NULL;
912 if (scherzo->pedal_pressed)
913 note_group = &scherzo->notes_pedaled;
915 note_group = &scherzo->notes_pressed;
917 num_notes = note_group->num_notes;
919 if (scherzo->chord) {
920 score_remove_chord (scherzo->chord);
921 scherzo->chord = NULL;
927 notes = talloc_array (local, analyzed_note_t, num_notes);
931 for (i = 0; i < num_notes; i++) {
932 score_note_t *note = note_group->notes[i];
933 notes[i].note = note;
934 notes[i].midi_pitch = _score_pitch_and_octave_to_midi (note->pitch,
936 /* Relative pitch will be filled in after sorting. */
937 notes[i].relative_pitch = 0;
940 /* First, sort by midi pitch to find the bass note. */
941 qsort (notes, num_notes, sizeof (analyzed_note_t),
942 _compare_analyzed_note_by_midi_pitch);
944 bass_pitch = notes[0].midi_pitch;
946 /* With the bass note identified, we can find all relative pitches. */
947 for (i = 0; i < num_notes; i++) {
948 notes[i].relative_pitch = notes[i].midi_pitch - bass_pitch;
949 while (notes[i].relative_pitch >= 12)
950 notes[i].relative_pitch -= 12;
953 /* Now, sort again by relative pitch. */
954 qsort (notes, num_notes, sizeof (analyzed_note_t),
955 _compare_analyzed_note_by_relative_pitch);
957 /* Finally, eliminate all duplicate notes. */
958 for (i = 0; i < num_notes - 1; i++) {
959 if (notes[i+1].relative_pitch == notes[i].relative_pitch) {
961 while (j < num_notes &&
962 notes[j].relative_pitch == notes[i].relative_pitch)
966 /* The loop incremented j one past the last
967 * duplicate. Decrement so that it points to the
968 * last duplicate (and is guaranteed to not exceed
969 * the array bounds).*/
972 if (j < num_notes - 1) {
973 memmove (¬es[i+1], ¬es[j+1],
974 (num_notes - j) * sizeof (analyzed_note_t));
977 num_notes -= (j - i);
981 for (inversion = 0; inversion < 4; inversion++) {
982 for (i = 0; i < ARRAY_SIZE (signatures); i++) {
983 if (_chord_signature_matches (notes, num_notes,
984 signatures[i].degrees,
985 signatures[i].num_degrees,
988 match = &signatures[i];
995 chord_name = (char *) signatures[i].name;
998 /* Don't print root pitch for octaves and inversions,
999 * (since a pitch name alone looks like a major triad) */
1000 if (num_notes < 3) {
1001 chord_name = talloc_strdup (local, chord_name);
1003 chord_name = talloc_asprintf (local, "%s%s",
1008 _spell_chord_by_signature (note_group, num_notes,
1011 chord_name = talloc_strdup (local, "Unknown chord");
1014 scherzo->chord = score_add_chord (scherzo->treble, chord_name);
1017 talloc_free (local);
1021 note_group_init (void *ctx, note_group_t *group)
1024 group->notes = NULL;
1026 group->num_notes = 0;
1030 note_group_add_note (note_group_t *group, score_note_t *note)
1034 /* Do nothing if note is already in group. */
1035 for (i = 0; i < group->num_notes; i++) {
1036 if (group->notes[i]->pitch == note->pitch &&
1037 group->notes[i]->octave == note->octave)
1045 if (group->num_notes > group->size) {
1047 group->notes = talloc_realloc (group->ctx, group->notes,
1048 score_note_t*, group->size);
1050 if (group->notes == NULL) {
1051 fprintf (stderr, "Out of memory.\n");
1056 group->notes[group->num_notes - 1] = note;
1060 note_group_remove_note_at (note_group_t *group, int i)
1062 if (i >= group->num_notes) {
1063 fprintf (stderr, "Internal error: No note to remove at index %d\n", i);
1067 if (i < group->num_notes - 1) {
1068 memmove (group->notes + i, group->notes + i + 1,
1069 (group->num_notes - 1 - i) * sizeof (score_note_t*));
1074 static score_note_t *
1075 scherzo_press_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
1077 score_staff_t *staff;
1081 if (scherzo->challenge.note) {
1082 staff = scherzo->challenge.staff;
1083 } else if (octave >= 4) {
1084 staff = scherzo->treble;
1086 staff = scherzo->bass;
1089 /* Do nothing if this note is already pressed. */
1090 for (i = 0; i < scherzo->notes_pressed.num_notes; i++) {
1091 if (scherzo->notes_pressed.notes[i]->pitch == pitch &&
1092 scherzo->notes_pressed.notes[i]->octave == octave)
1094 return scherzo->notes_pressed.notes[i];
1098 note = score_add_note (staff, pitch, octave, SCORE_DURATION_WHOLE);
1100 note_group_add_note (&scherzo->notes_pressed, note);
1102 if (scherzo->pedal_pressed)
1103 note_group_add_note (&scherzo->notes_pedaled, note);
1105 scherzo_analyze_chord (scherzo);
1111 scherzo_release_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
1116 int target_midi = _score_pitch_and_octave_to_midi (pitch, octave);
1119 for (i = scherzo->notes_pressed.num_notes - 1; i >=0; i--) {
1120 note = scherzo->notes_pressed.notes[i];
1121 note_midi = _score_pitch_and_octave_to_midi (note->pitch, note->octave);
1122 if (note_midi == target_midi) {
1124 if (! scherzo->pedal_pressed)
1125 score_remove_note (note);
1126 note_group_remove_note_at (&scherzo->notes_pressed, i);
1131 fprintf (stderr, "Internal error: Failed to find note to release.\n");
1134 scherzo_analyze_chord (scherzo);
1137 static score_note_t *
1138 scherzo_press_note_midi (scherzo_t *scherzo, unsigned char midi_note)
1140 score_pitch_t pitch;
1143 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
1145 return scherzo_press_note (scherzo, pitch, octave);
1149 scherzo_release_note_midi (scherzo_t *scherzo, unsigned char midi_note)
1151 score_pitch_t pitch;
1154 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
1156 scherzo_release_note (scherzo, pitch, octave);
1160 scherzo_press_pedal (scherzo_t *scherzo)
1164 scherzo->pedal_pressed = 1;
1166 /* Copy all pressed notes to pedaled notes */
1167 for (i = 0; i < scherzo->notes_pressed.num_notes; i++)
1168 note_group_add_note (&scherzo->notes_pedaled, scherzo->notes_pressed.notes[i]);
1172 scherzo_release_pedal (scherzo_t *scherzo)
1174 score_note_t *note, *new_note;
1177 /* Make new notes in score for all pressed notes. */
1178 for (i = 0; i < scherzo->notes_pressed.num_notes; i++) {
1179 note = scherzo->notes_pressed.notes[i];
1180 new_note = score_add_note (note->staff, note->pitch, note->octave, note->duration);
1181 scherzo->notes_pressed.notes[i] = new_note;
1184 /* Then remove all previously pedaled notes from the score. */
1185 for (i = scherzo->notes_pedaled.num_notes - 1; i >=0; i--) {
1186 note = scherzo->notes_pedaled.notes[i];
1187 score_remove_note (note);
1188 note_group_remove_note_at (&scherzo->notes_pedaled, i);
1191 scherzo->pedal_pressed = 0;
1193 scherzo_analyze_chord (scherzo);
1195 gtk_widget_queue_draw (scherzo->window);
1199 _select_challenge (scherzo_t *scherzo)
1201 category_t *category_unused;
1202 bool_t introduced_unused;
1204 challenge_t *challenge = &scherzo->challenge;
1205 score_pitch_t pitch;
1209 if (challenge->note) {
1210 score_remove_note (challenge->note);
1211 challenge->note = NULL;
1214 mnemon_select_item (&scherzo->mnemon,
1216 &challenge->item_index,
1218 &introduced_unused);
1220 item = challenge->bin->items[challenge->item_index];
1222 s = item->challenge;
1223 if (strncmp (s, "treble:", 7) == 0) {
1225 challenge->staff = scherzo->treble;
1226 } else if (strncmp (s, "bass:", 5) == 0) {
1228 challenge->staff = scherzo->bass;
1231 "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
1238 pitch = SCORE_PITCH_C;
1241 pitch = SCORE_PITCH_D;
1244 pitch = SCORE_PITCH_E;
1247 pitch = SCORE_PITCH_F;
1250 pitch = SCORE_PITCH_G;
1253 pitch = SCORE_PITCH_A;
1256 pitch = SCORE_PITCH_B;
1259 fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
1264 if (*s < '0' || *s > '9') {
1265 fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
1271 challenge->note = score_add_note (challenge->staff, pitch, octave,
1272 SCORE_DURATION_WHOLE);
1273 challenge->satisfied = 0;
1274 challenge->mistaken = 0;
1277 /* Determine whether the user hit the correct note. */
1279 _judge_note (scherzo_t *scherzo, score_note_t *note)
1281 challenge_t *challenge = &scherzo->challenge;
1283 if (! scherzo->challenge.note) {
1284 score_set_note_color_rgb (note, 0.0, 0.0, 0.0); /* black */
1288 if (note->pitch == challenge->note->pitch &&
1289 note->octave == challenge->note->octave)
1291 challenge->satisfied = 1;
1292 score_set_note_color_rgb (note, 18/256., 130/256., 28/256.); /* green */
1296 challenge->mistaken = 1;
1297 score_set_note_color_rgb (note, 184/256., 4/256., 22/256.); /* red */
1301 /* If the user got the right note (eventually), then score it in
1302 * mnemon and show the next note. */
1304 _score_challenge (scherzo_t *scherzo)
1306 challenge_t *challenge = &scherzo->challenge;
1308 if (! challenge->note)
1311 if (! challenge->satisfied)
1314 mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
1315 ! challenge->mistaken);
1317 _select_challenge (scherzo);
1321 on_midi_input (unused (GIOChannel *channel),
1322 unused (GIOCondition condition),
1325 unsigned char buf[MIDI_BUF_SIZE], *next;
1326 scherzo_t *scherzo = user_data;
1328 snd_seq_event_t event;
1330 int need_redraw = FALSE;
1332 remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
1338 consumed = snd_midi_event_encode (scherzo->snd_midi_event,
1339 next, remaining, &event);
1341 remaining -= consumed;
1344 switch (event.type) {
1345 case SND_SEQ_EVENT_NONE:
1346 /* Incomplete event. Nothing to do. */
1348 case SND_SEQ_EVENT_NOTEON:
1349 note = scherzo_press_note_midi (scherzo, event.data.note.note);
1350 _judge_note (scherzo, note);
1353 case SND_SEQ_EVENT_NOTEOFF:
1354 scherzo_release_note_midi (scherzo, event.data.note.note);
1355 _score_challenge (scherzo);
1358 case SND_SEQ_EVENT_CLOCK:
1359 /* Ignore for now as my piano sends a constant stream of these. */
1361 case SND_SEQ_EVENT_SENSING:
1362 /* Ignore for now as my piano sends a constant stream of these. */
1364 case SND_SEQ_EVENT_CONTROLLER:
1365 /* XXX: My piano gives me 64 for the sustain pedal, is
1366 * that universal? */
1367 if (event.data.control.param == 64) {
1368 if (event.data.control.value == 0)
1369 scherzo_release_pedal (scherzo);
1371 scherzo_press_pedal (scherzo);
1373 fprintf (stderr, "Fixme: Unhandled MIDI Control event (param=%d, value=%d)\n",
1374 event.data.control.param, event.data.control.value);
1378 fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
1385 gtk_widget_queue_draw (scherzo->window);
1387 /* Return TRUE to continue to get called in the future. */
1392 main (int argc, char *argv[])
1394 GtkWidget *drawing_area;
1398 srand (time (NULL));
1400 gtk_init (&argc, &argv);
1402 scherzo.ctx = talloc_new (NULL);
1404 scherzo.score = score_create (scherzo.ctx);
1405 scherzo.staff_height = 100;
1406 score_set_staff_height (scherzo.score, scherzo.staff_height);
1408 score_add_brace (scherzo.score, 2);
1409 scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
1410 scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
1412 scherzo.chord = NULL;
1414 /* Default to octave 4 and natural for computer keyboard keypresses. */
1415 scherzo.keyboard_octave = 4;
1416 scherzo.keyboard_accidental = SCORE_PITCH_ACCIDENTAL_NATURAL;
1418 note_group_init (scherzo.ctx, &scherzo.notes_pressed);
1419 note_group_init (scherzo.ctx, &scherzo.notes_pedaled);
1421 scherzo.pedal_pressed = 0;
1423 mnemon_init (&scherzo.mnemon);
1424 /* XXX: Should create a default file if one cannot be loaded. */
1425 mnemon_load_category (&scherzo.mnemon, "scherzo-notes");
1427 scherzo.challenge.note = NULL;
1429 _select_challenge (&scherzo);
1432 err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
1434 fprintf (stderr, "Out of memory.\n");
1438 #define MIDI_DEVICE "/dev/midi1"
1439 scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
1440 if (scherzo.midi_fd < 0) {
1441 printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
1443 GIOChannel *channel;
1445 channel = g_io_channel_unix_new (scherzo.midi_fd);
1446 g_io_channel_set_encoding (channel, NULL, NULL);
1447 g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
1450 scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1452 gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 1000, 600);
1454 g_signal_connect (scherzo.window, "delete-event",
1455 G_CALLBACK (on_delete_event_quit), NULL);
1457 drawing_area = gtk_drawing_area_new ();
1459 gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
1461 g_signal_connect (drawing_area, "expose-event",
1462 G_CALLBACK (on_expose_event_draw),
1465 g_signal_connect (scherzo.window, "key-press-event",
1466 G_CALLBACK (on_key_press_event),
1469 g_signal_connect (scherzo.window, "key-release-event",
1470 G_CALLBACK (on_key_release_event),
1473 gtk_widget_show_all (scherzo.window);
1477 mnemon_save (&scherzo.mnemon);
1479 snd_midi_event_free (scherzo.snd_midi_event);
1481 talloc_free (scherzo.ctx);