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:
367 _midi_to_score_pitch_and_octave (unsigned char midi_note,
368 score_pitch_t *pitch,
371 *octave = midi_note / 12 - 1;
373 switch (midi_note % 12)
376 *pitch = SCORE_PITCH_C;
379 *pitch = SCORE_PITCH_Cs;
382 *pitch = SCORE_PITCH_D;
385 *pitch = SCORE_PITCH_Ds;
388 *pitch = SCORE_PITCH_E;
391 *pitch = SCORE_PITCH_F;
394 *pitch = SCORE_PITCH_Fs;
397 *pitch = SCORE_PITCH_G;
400 *pitch = SCORE_PITCH_Gs;
403 *pitch = SCORE_PITCH_A;
406 *pitch = SCORE_PITCH_As;
409 *pitch = SCORE_PITCH_B;
414 /* Determine a chord name (if any) from the current notes pressed */
416 typedef struct analyzed_note {
417 /* Original note being analzyed. */
420 /* Absolute pitch (expressed as midi number). */
423 /* Pitch relative to bass note. */
428 _compare_analyzed_note_by_midi_pitch (const void *va, const void *vb)
430 const analyzed_note_t *a = va, *b = vb;
432 return a->midi_pitch - b->midi_pitch;
436 _compare_analyzed_note_by_relative_pitch (const void *va, const void *vb)
438 const analyzed_note_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
460 * diatonic scaled. */
461 switch (scale_degree) {
484 fprintf (stderr, "Internal: Invalid degree %d\n", degree->degree);
489 return half_steps + degree->modification;
493 _chord_signature_matches (analyzed_note_t *notes,
495 modified_degree_t *degrees,
500 #define MAX_DEGREES 6
501 int relative_pitches[MAX_DEGREES];
504 assert (num_degrees <= MAX_DEGREES);
506 if (num_notes != num_degrees)
509 if (inversion >= num_degrees)
512 /* We never spell simple intervals as inversions. */
513 if (num_degrees == 2 && inversion > 0)
516 for (i = 0; i < num_degrees; i++) {
517 /* The num_degrees is in the addition just to ensure all
518 * inputs to the modulus operator remain positive. */
519 int index = (i + num_degrees - inversion) % num_degrees;
521 /* Again, adding a 12 to keep things positive. */
522 relative_pitches[index] =
524 _modified_degree_to_half_steps (°rees[i]) -
525 _modified_degree_to_half_steps (°rees[inversion])) % 12;
529 for (i = 0; i < num_notes; i++)
530 if (notes[i].relative_pitch != relative_pitches[i])
533 root_index = (num_notes - inversion) % num_notes;
534 *root = notes[root_index].note->pitch;
540 _pitch_str (score_pitch_t pitch)
543 case SCORE_PITCH_Cff:
551 case SCORE_PITCH_Css:
553 case SCORE_PITCH_Dff:
561 case SCORE_PITCH_Dss:
563 case SCORE_PITCH_Eff:
571 case SCORE_PITCH_Ess:
573 case SCORE_PITCH_Fff:
581 case SCORE_PITCH_Fss:
583 case SCORE_PITCH_Gff:
591 case SCORE_PITCH_Gss:
593 case SCORE_PITCH_Aff:
601 case SCORE_PITCH_Ass:
603 case SCORE_PITCH_Bff:
611 case SCORE_PITCH_Bss:
615 fprintf (stderr, "Internal error: Unknown pitch %d\n", pitch);
619 typedef struct chord_signature
622 modified_degree_t degrees[MAX_DEGREES];
626 /* XXX: The superscript rise value should be relative to the current
627 * font size. Best would be for pango to support this. See:
629 * https://bugzilla.gnome.org/show_bug.cgi?id=708780
631 * For now, these are emipirically-derived values that look reasonable
632 * at the default size that score.c is using to draw the staff. If the
633 * user scales the staff up or down then these will look wrong.
636 #define SUP "<span font='33' rise='30000'>"
637 #define PUS "</span>"
639 chord_signature_t signatures[] = {
641 { 1, {{1, 0}}, "P8" },
644 { 2, {{1, 0}, {2, -1}}, "m2" },
645 { 2, {{1, 0}, {2, 0}}, "M2" },
646 { 2, {{1, 0}, {3, -1}}, "m3" },
647 { 2, {{1, 0}, {3, 0}}, "M3" },
648 { 2, {{1, 0}, {4, 0}}, "P4" },
649 { 2, {{1, 0}, {5, -1}}, "Tri-tone" },
650 { 2, {{1, 0}, {5, 0}}, "P5" },
651 { 2, {{1, 0}, {6, -1}}, "m6" },
652 { 2, {{1, 0}, {6, 0}}, "M6" },
653 { 2, {{1, 0}, {7, -1}}, "m7" },
654 { 2, {{1, 0}, {7, 0}}, "M7" },
657 { 3, {{1, 0}, {4, 0}, {5, 0}}, "sus" },
658 { 3, {{1, 0}, {3, 0}, {5, +1}}, SUP "+" PUS },
659 { 3, {{1, 0}, {3, 0}, {5, 0}}, "" },
660 { 3, {{1, 0}, {3, -1}, {5, 0}}, "m" },
661 { 3, {{1, 0}, {3, -1}, {5, -1}}, "°" },
662 { 3, {{1, 0}, {2, 0}, {5, 0}}, "msus2" },
664 /* Seventh chords with no 3rd */
665 { 3, {{1, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS },
666 { 3, {{1, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS },
667 { 3, {{1, 0}, {5, 0}, {7, 0}}, "M7" },
668 { 3, {{1, 0}, {5, 0}, {7, -1}}, "7" },
669 { 3, {{1, 0}, {5, -1}, {7, -1}}, "7♭5" },
671 /* Seventh chords with no 5th */
672 { 3, {{1, 0}, {4, 0}, {7, 0}}, "M7sus" },
673 { 3, {{1, 0}, {4, 0}, {7, -1}}, "7sus" },
674 { 3, {{1, 0}, {3, 0}, {7, 0}}, "M7" },
675 { 3, {{1, 0}, {3, 0}, {7, -1}}, "7" },
676 { 3, {{1, 0}, {3, -1}, {7, 0}}, "m" SUP "M7" PUS },
677 { 3, {{1, 0}, {3, -1}, {7, -1}}, "m7" },
678 { 3, {{1, 0}, {2, 0}, {7, 0}}, "m" SUP "M7" PUS "sus2" },
679 { 3, {{1, 0}, {2, 0}, {7, -1}}, "m7sus2" },
682 { 4, {{1, 0}, {4, 0}, {5, 0}, {6, 0}}, "6sus" },
683 { 4, {{1, 0}, {3, 0}, {5, 0}, {6, 0}}, "6" }, /* Ambiguous with m7 */
684 { 4, {{1, 0}, {3, -1}, {5, 0}, {6, 0}}, "m6" },
685 { 4, {{1, 0}, {2, 0}, {5, 0}, {6, 0}}, "m6sus2" },
688 { 4, {{1, 0}, {4, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS "sus" },
689 { 4, {{1, 0}, {4, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS "sus" },
690 { 4, {{1, 0}, {4, 0}, {5, 0}, {7, 0}}, "M7sus" },
691 { 4, {{1, 0}, {4, 0}, {5, 0}, {7, -1}}, "7sus" },
692 { 4, {{1, 0}, {4, 0}, {5, -1}, {7, -1}}, "7♭5sus" },
693 { 4, {{1, 0}, {3, 0}, {5, +1}, {7, 0}}, SUP "+M7" PUS },
694 { 4, {{1, 0}, {3, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS },
695 { 4, {{1, 0}, {3, 0}, {5, 0}, {7, 0}}, "M7" },
696 { 4, {{1, 0}, {3, 0}, {5, 0}, {7, -1}}, "7" },
697 { 4, {{1, 0}, {3, 0}, {5, -1}, {7, -1}}, "7♭5" },
698 { 4, {{1, 0}, {3, -1}, {5, 0}, {7, 0}}, "m" SUP "M7" PUS },
699 { 4, {{1, 0}, {3, -1}, {5, 0}, {7, -1}}, "m7" },
700 { 4, {{1, 0}, {3, -1}, {5, -1}, {7, 0}}, "°" SUP "M7" PUS },
701 { 4, {{1, 0}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "7" PUS },
702 { 4, {{1, 0}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "7" PUS },
703 { 4, {{1, 0}, {2, 0}, {5, 0}, {7, 0}}, "m" SUP "M7" PUS "sus2" },
704 { 4, {{1, 0}, {2, 0}, {5, 0}, {7, -1}}, "m7sus2" },
705 { 4, {{1, 0}, {2, 0}, {5, -1}, {7, 0}}, "°" SUP "M7" PUS "sus2" },
706 { 4, {{1, 0}, {2, 0}, {5, -1}, {7, -1}}, "𝆩" SUP "7" PUS "sus2" },
707 { 4, {{1, 0}, {2, 0}, {5, -1}, {7, -2}}, "°" SUP "7" PUS "sus2" }, /* Ambiguous with 7 */
709 /* The sorting for chords with degree higher than 9 is funny
710 * to keep the array in degree order after reducing each
711 * degree to an actual scale degree, (9 -> 2, 11 -> 4, 13 ->
714 /* Ninth chords voiced with no 5th */
715 { 4, {{1, 0}, {9, 0}, {4, 0}, {7, 0}}, "M9sus" },
716 { 4, {{1, 0}, {9, 0}, {4, 0}, {7, -1}}, "9sus" },
717 { 4, {{1, 0}, {9, 0}, {3, 0}, {7, 0}}, "M9" },
718 { 4, {{1, 0}, {9, 0}, {3, 0}, {7, -1}}, "9" },
719 { 4, {{1, 0}, {9, 0}, {3, -1}, {7, 0}}, "m" SUP "M9" PUS },
720 { 4, {{1, 0}, {9, 0}, {3, -1}, {7, -1}}, "m9" },
721 { 4, {{1, 0}, {9, -1}, {3, -1}, {7, -1}}, "m♭9" },
722 { 4, {{1, 0}, {9, 0}, {3, -1}, {6, 0}}, "m6/9" },
723 { 4, {{1, 0}, {9, -1}, {3, -1}, {6, 0}}, "m6/♭9" },
726 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {6, 0}}, "6/9" },
727 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {6, 0}}, "m6/9" },
728 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, 0}, {6, 0}}, "m6/♭9" },
730 /* Seventh plus altered 9 */
731 { 5, {{1, 0}, {9, +1}, {3, 0}, {5, 0}, {7, -1}}, "7♯9" },
732 { 5, {{1, 0}, {9, -1}, {3, 0}, {5, 0}, {7, -1}}, "7♭9" },
733 { 5, {{1, 0}, {9, +1}, {3, 0}, {5, -1}, {7, -1}}, "7♭5♯9" },
734 { 5, {{1, 0}, {9, -1}, {3, 0}, {5, -1}, {7, -1}}, "7♭5♭9" },
737 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, +1}, {7, 0}}, SUP "+M9" PUS "sus" },
738 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, +1}, {7, -1}}, SUP "+9" PUS "sus" },
739 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, 0}, {7, 0}}, "M9sus" },
740 { 5, {{1, 0}, {9, 0}, {4, 0}, {5, 0}, {7, -1}}, "9sus" },
741 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, +1}, {7, 0}}, SUP "+M9" PUS },
742 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, +1}, {7, -1}}, SUP "+9" PUS },
743 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {7, 0}}, "M9" },
744 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, 0}, {7, -1}}, "9" },
745 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {7, 0}}, "m" SUP "M9" PUS },
746 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, 0}, {7, -1}}, "m9" },
747 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, 0}, {7, -1}}, "m♭9" },
748 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, -1}, {7, 0}}, "M9" SUP "♭5" PUS },
749 { 5, {{1, 0}, {9, 0}, {3, 0}, {5, -1}, {7, -1}}, "9" SUP "♭5" PUS },
750 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, 0}}, "m" SUP "M9♭5" PUS },
751 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "9" PUS },
752 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "♭9" PUS },
753 { 5, {{1, 0}, {9, 0}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "9" PUS },
754 { 5, {{1, 0}, {9, -1}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "♭9" PUS },
756 /* Eleventh chords */
757 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, +1}, {7, 0}}, SUP "+M11" PUS },
758 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, +1}, {7, -1}}, SUP "+11" PUS },
759 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, 0}, {7, 0}}, "M11" },
760 { 6, {{1, 0}, {9, 0}, {3, 0}, {11, 0}, {5, 0}, {7, -1}}, "11" },
761 { 6, {{1, 0}, {9, 0}, {3, -1}, {11, 0}, {5, 0}, {7, 0}}, "m" SUP "M11" PUS },
762 { 6, {{1, 0}, {9, 0}, {3, -1}, {11, 0}, {5, 0}, {7, -1}}, "m11" },
763 { 6, {{1, 0}, {9, -1}, {3, -1}, {11, 0}, {5, -1}, {7, -1}}, "𝆩" SUP "11" PUS },
764 { 6, {{1, 0}, {9, -1}, {3, -1}, {11, -1}, {5, -1}, {7, -2}}, "°" SUP "11" PUS }
768 scherzo_analyze_chord (scherzo_t *scherzo)
770 void *local = talloc_new (NULL);
771 analyzed_note_t *notes;
772 note_group_t *note_group;
773 unsigned i, j, num_notes;
774 int bass_pitch, inversion;
776 const char *chord_name = NULL;
778 if (scherzo->pedal_pressed)
779 note_group = &scherzo->notes_pedaled;
781 note_group = &scherzo->notes_pressed;
783 num_notes = note_group->num_notes;
785 if (scherzo->chord) {
786 score_remove_chord (scherzo->chord);
787 scherzo->chord = NULL;
793 notes = talloc_array (local, analyzed_note_t, num_notes);
797 for (i = 0; i < num_notes; i++) {
798 score_note_t *note = note_group->notes[i];
799 notes[i].note = note;
800 notes[i].midi_pitch = _score_pitch_and_octave_to_midi (note->pitch,
802 /* Relative pitch will be filled in after sorting. */
803 notes[i].relative_pitch = 0;
806 /* First, sort by midi pitch to find the bass note. */
807 qsort (notes, num_notes, sizeof (analyzed_note_t),
808 _compare_analyzed_note_by_midi_pitch);
810 bass_pitch = notes[0].midi_pitch;
812 /* With the bass note identified, we can find all relative pitches. */
813 for (i = 0; i < num_notes; i++) {
814 notes[i].relative_pitch = notes[i].midi_pitch - bass_pitch;
815 while (notes[i].relative_pitch >= 12)
816 notes[i].relative_pitch -= 12;
819 /* Now, sort again by relative pitch. */
820 qsort (notes, num_notes, sizeof (analyzed_note_t),
821 _compare_analyzed_note_by_relative_pitch);
823 /* Finally, eliminate all duplicate notes. */
824 for (i = 0; i < num_notes - 1; i++) {
825 if (notes[i+1].relative_pitch == notes[i].relative_pitch) {
827 while (j < num_notes &&
828 notes[j].relative_pitch == notes[i].relative_pitch)
832 /* The loop incremented j one past the last
833 * duplicate. Decrement so that it points to the
834 * last duplicate (and is guaranteed to not exceed
835 * the array bounds).*/
838 if (j < num_notes - 1) {
839 memmove (¬es[i+1], ¬es[j+1],
840 (num_notes - j) * sizeof (analyzed_note_t));
843 num_notes -= (j - i);
847 for (inversion = 0; inversion < 4; inversion++) {
848 for (i = 0; i < ARRAY_SIZE (signatures); i++) {
849 if (_chord_signature_matches (notes, num_notes,
850 signatures[i].degrees,
851 signatures[i].num_degrees,
854 chord_name = signatures[i].name;
855 goto CHORD_NAME_KNOWN;
862 /* Don't print root pitch for octaves and inversions,
863 * (since a pitch name alone looks like a major triad) */
865 chord_name = talloc_strdup (local, chord_name);
867 chord_name = talloc_asprintf (local, "%s%s",
872 chord_name = talloc_strdup (local, "Unknown chord");
875 scherzo->chord = score_add_chord (scherzo->treble, chord_name);
882 note_group_init (void *ctx, note_group_t *group)
887 group->num_notes = 0;
891 note_group_add_note (note_group_t *group, score_note_t *note)
895 /* Do nothing if note is already in group. */
896 for (i = 0; i < group->num_notes; i++) {
897 if (group->notes[i]->pitch == note->pitch &&
898 group->notes[i]->octave == note->octave)
906 if (group->num_notes > group->size) {
908 group->notes = talloc_realloc (group->ctx, group->notes,
909 score_note_t*, group->size);
911 if (group->notes == NULL) {
912 fprintf (stderr, "Out of memory.\n");
917 group->notes[group->num_notes - 1] = note;
921 note_group_remove_note_at (note_group_t *group, int i)
923 if (i >= group->num_notes) {
924 fprintf (stderr, "Internal error: No note to remove at index %d\n", i);
928 if (i < group->num_notes - 1) {
929 memmove (group->notes + i, group->notes + i + 1,
930 (group->num_notes - 1 - i) * sizeof (score_note_t*));
935 static score_note_t *
936 scherzo_press_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
938 score_staff_t *staff;
942 if (scherzo->challenge.note) {
943 staff = scherzo->challenge.staff;
944 } else if (octave >= 4) {
945 staff = scherzo->treble;
947 staff = scherzo->bass;
950 /* Do nothing if this note is already pressed. */
951 for (i = 0; i < scherzo->notes_pressed.num_notes; i++) {
952 if (scherzo->notes_pressed.notes[i]->pitch == pitch &&
953 scherzo->notes_pressed.notes[i]->octave == octave)
955 return scherzo->notes_pressed.notes[i];
959 note = score_add_note (staff, pitch, octave, SCORE_DURATION_WHOLE);
961 note_group_add_note (&scherzo->notes_pressed, note);
963 if (scherzo->pedal_pressed)
964 note_group_add_note (&scherzo->notes_pedaled, note);
966 scherzo_analyze_chord (scherzo);
972 scherzo_release_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
978 for (i = scherzo->notes_pressed.num_notes - 1; i >=0; i--) {
979 note = scherzo->notes_pressed.notes[i];
980 if (note->pitch == pitch && note->octave == octave) {
982 if (! scherzo->pedal_pressed)
983 score_remove_note (note);
984 note_group_remove_note_at (&scherzo->notes_pressed, i);
989 fprintf (stderr, "Internal error: Failed to find note to release.\n");
992 scherzo_analyze_chord (scherzo);
995 static score_note_t *
996 scherzo_press_note_midi (scherzo_t *scherzo, unsigned char midi_note)
1001 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
1003 return scherzo_press_note (scherzo, pitch, octave);
1007 scherzo_release_note_midi (scherzo_t *scherzo, unsigned char midi_note)
1009 score_pitch_t pitch;
1012 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
1014 scherzo_release_note (scherzo, pitch, octave);
1018 scherzo_press_pedal (scherzo_t *scherzo)
1022 scherzo->pedal_pressed = 1;
1024 /* Copy all pressed notes to pedaled notes */
1025 for (i = 0; i < scherzo->notes_pressed.num_notes; i++)
1026 note_group_add_note (&scherzo->notes_pedaled, scherzo->notes_pressed.notes[i]);
1030 scherzo_release_pedal (scherzo_t *scherzo)
1032 score_note_t *note, *new_note;
1035 /* Make new notes in score for all pressed notes. */
1036 for (i = 0; i < scherzo->notes_pressed.num_notes; i++) {
1037 note = scherzo->notes_pressed.notes[i];
1038 new_note = score_add_note (note->staff, note->pitch, note->octave, note->duration);
1039 scherzo->notes_pressed.notes[i] = new_note;
1042 /* Then remove all previously pedaled notes from the score. */
1043 for (i = scherzo->notes_pedaled.num_notes - 1; i >=0; i--) {
1044 note = scherzo->notes_pedaled.notes[i];
1045 score_remove_note (note);
1046 note_group_remove_note_at (&scherzo->notes_pedaled, i);
1049 scherzo->pedal_pressed = 0;
1051 scherzo_analyze_chord (scherzo);
1053 gtk_widget_queue_draw (scherzo->window);
1057 _select_challenge (scherzo_t *scherzo)
1059 category_t *category_unused;
1060 bool_t introduced_unused;
1062 challenge_t *challenge = &scherzo->challenge;
1063 score_pitch_t pitch;
1067 if (challenge->note) {
1068 score_remove_note (challenge->note);
1069 challenge->note = NULL;
1072 mnemon_select_item (&scherzo->mnemon,
1074 &challenge->item_index,
1076 &introduced_unused);
1078 item = challenge->bin->items[challenge->item_index];
1080 s = item->challenge;
1081 if (strncmp (s, "treble:", 7) == 0) {
1083 challenge->staff = scherzo->treble;
1084 } else if (strncmp (s, "bass:", 5) == 0) {
1086 challenge->staff = scherzo->bass;
1089 "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
1096 pitch = SCORE_PITCH_C;
1099 pitch = SCORE_PITCH_D;
1102 pitch = SCORE_PITCH_E;
1105 pitch = SCORE_PITCH_F;
1108 pitch = SCORE_PITCH_G;
1111 pitch = SCORE_PITCH_A;
1114 pitch = SCORE_PITCH_B;
1117 fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
1122 if (*s < '0' || *s > '9') {
1123 fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
1129 challenge->note = score_add_note (challenge->staff, pitch, octave,
1130 SCORE_DURATION_WHOLE);
1131 challenge->satisfied = 0;
1132 challenge->mistaken = 0;
1135 /* Determine whether the user hit the correct note. */
1137 _judge_note (scherzo_t *scherzo, score_note_t *note)
1139 challenge_t *challenge = &scherzo->challenge;
1141 if (! scherzo->challenge.note) {
1142 score_set_note_color_rgb (note, 0.0, 0.0, 0.0); /* black */
1146 if (note->pitch == challenge->note->pitch &&
1147 note->octave == challenge->note->octave)
1149 challenge->satisfied = 1;
1150 score_set_note_color_rgb (note, 18/256., 130/256., 28/256.); /* green */
1154 challenge->mistaken = 1;
1155 score_set_note_color_rgb (note, 184/256., 4/256., 22/256.); /* red */
1159 /* If the user got the right note (eventually), then score it in
1160 * mnemon and show the next note. */
1162 _score_challenge (scherzo_t *scherzo)
1164 challenge_t *challenge = &scherzo->challenge;
1166 if (! challenge->note)
1169 if (! challenge->satisfied)
1172 mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
1173 ! challenge->mistaken);
1175 _select_challenge (scherzo);
1179 on_midi_input (unused (GIOChannel *channel),
1180 unused (GIOCondition condition),
1183 unsigned char buf[MIDI_BUF_SIZE], *next;
1184 scherzo_t *scherzo = user_data;
1186 snd_seq_event_t event;
1188 int need_redraw = FALSE;
1190 remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
1196 consumed = snd_midi_event_encode (scherzo->snd_midi_event,
1197 next, remaining, &event);
1199 remaining -= consumed;
1202 switch (event.type) {
1203 case SND_SEQ_EVENT_NONE:
1204 /* Incomplete event. Nothing to do. */
1206 case SND_SEQ_EVENT_NOTEON:
1207 note = scherzo_press_note_midi (scherzo, event.data.note.note);
1208 _judge_note (scherzo, note);
1211 case SND_SEQ_EVENT_NOTEOFF:
1212 scherzo_release_note_midi (scherzo, event.data.note.note);
1213 _score_challenge (scherzo);
1216 case SND_SEQ_EVENT_CLOCK:
1217 /* Ignore for now as my piano sends a constant stream of these. */
1219 case SND_SEQ_EVENT_SENSING:
1220 /* Ignore for now as my piano sends a constant stream of these. */
1222 case SND_SEQ_EVENT_CONTROLLER:
1223 /* XXX: My piano gives me 64 for the sustain pedal, is
1224 * that universal? */
1225 if (event.data.control.param == 64) {
1226 if (event.data.control.value == 0)
1227 scherzo_release_pedal (scherzo);
1229 scherzo_press_pedal (scherzo);
1231 fprintf (stderr, "Fixme: Unhandled MIDI Control event (param=%d, value=%d)\n",
1232 event.data.control.param, event.data.control.value);
1236 fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
1243 gtk_widget_queue_draw (scherzo->window);
1245 /* Return TRUE to continue to get called in the future. */
1250 main (int argc, char *argv[])
1252 GtkWidget *drawing_area;
1256 srand (time (NULL));
1258 gtk_init (&argc, &argv);
1260 scherzo.ctx = talloc_new (NULL);
1262 scherzo.score = score_create (scherzo.ctx);
1263 scherzo.staff_height = 100;
1264 score_set_staff_height (scherzo.score, scherzo.staff_height);
1266 score_add_brace (scherzo.score, 2);
1267 scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
1268 scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
1270 scherzo.chord = NULL;
1272 /* Default to octave 4 and natural for computer keyboard keypresses. */
1273 scherzo.keyboard_octave = 4;
1274 scherzo.keyboard_accidental = SCORE_PITCH_ACCIDENTAL_NATURAL;
1276 note_group_init (scherzo.ctx, &scherzo.notes_pressed);
1277 note_group_init (scherzo.ctx, &scherzo.notes_pedaled);
1279 scherzo.pedal_pressed = 0;
1281 mnemon_init (&scherzo.mnemon);
1282 /* XXX: Should create a default file if one cannot be loaded. */
1283 mnemon_load_category (&scherzo.mnemon, "scherzo-notes");
1285 scherzo.challenge.note = NULL;
1287 _select_challenge (&scherzo);
1290 err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
1292 fprintf (stderr, "Out of memory.\n");
1296 #define MIDI_DEVICE "/dev/midi1"
1297 scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
1298 if (scherzo.midi_fd < 0) {
1299 printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
1301 GIOChannel *channel;
1303 channel = g_io_channel_unix_new (scherzo.midi_fd);
1304 g_io_channel_set_encoding (channel, NULL, NULL);
1305 g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
1308 scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1310 gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 1000, 600);
1312 g_signal_connect (scherzo.window, "delete-event",
1313 G_CALLBACK (on_delete_event_quit), NULL);
1315 drawing_area = gtk_drawing_area_new ();
1317 gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
1319 g_signal_connect (drawing_area, "expose-event",
1320 G_CALLBACK (on_expose_event_draw),
1323 g_signal_connect (scherzo.window, "key-press-event",
1324 G_CALLBACK (on_key_press_event),
1327 g_signal_connect (scherzo.window, "key-release-event",
1328 G_CALLBACK (on_key_release_event),
1331 gtk_widget_show_all (scherzo.window);
1335 mnemon_save (&scherzo.mnemon);
1337 snd_midi_event_free (scherzo.snd_midi_event);
1339 talloc_free (scherzo.ctx);