+/* Determine a chord name (if any) from the current notes pressed */
+
+typedef struct analyzed_note {
+ /* Original note being analzyed. */
+ score_note_t *note;
+
+ /* Absolute pitch (expressed as midi number). */
+ int midi_pitch;
+
+ /* Pitch relative to bass note. */
+ int relative_pitch;
+} analyzed_note_t;
+
+static int
+_compare_analyzed_note_by_midi_pitch (const void *va, const void *vb)
+{
+ const analyzed_note_t *a = va, *b = vb;
+
+ return a->midi_pitch - b->midi_pitch;
+}
+
+static int
+_compare_analyzed_note_by_relative_pitch (const void *va, const void *vb)
+{
+ const analyzed_note_t *a = va, *b = vb;
+
+ return a->relative_pitch - b->relative_pitch;
+}
+
+static int
+_chord_signature_matches (analyzed_note_t *notes,
+ int num_notes,
+ int *signature_pitches,
+ int num_signature_pitches)
+{
+ int i;
+
+ if (num_notes != num_signature_pitches)
+ return 0;
+
+ for (i = 0; i < num_notes; i++)
+ if (notes[i].relative_pitch != signature_pitches[i])
+ return 0;
+
+ return 1;
+}
+
+static void
+scherzo_analyze_chord (scherzo_t *scherzo)
+{
+ void *local = talloc_new (NULL);
+ analyzed_note_t *notes;
+ note_group_t *note_group;
+ unsigned i, j, num_notes;
+ int bass_pitch;
+ const char *chord_name = NULL;
+
+ if (scherzo->pedal_pressed)
+ note_group = &scherzo->notes_pedaled;
+ else
+ note_group = &scherzo->notes_pressed;
+
+ num_notes = note_group->num_notes;
+
+ struct { int pitches[1]; const char *name; } octaves[] = {
+ { {0}, "Octave"}
+ };
+
+ struct { int pitches[2]; const char *name; } intervals[] = {
+ { {0, 1}, "Minor 2nd"},
+ { {0, 2}, "Major 2nd"},
+ { {0, 3}, "Minor 3rd"},
+ { {0, 4}, "Major 3rd"},
+ { {0, 5}, "Perfect 4th"},
+ { {0, 6}, "Diminished 5th/Augmented 4th"},
+ { {0, 7}, "Perfect 5th"},
+ { {0, 8}, "Minor 6th"},
+ { {0, 9}, "Major 6th"},
+ { {0, 10}, "Minor 7th"},
+ { {0, 11}, "Major 7th"}
+ };
+
+ struct { int pitches[3]; const char *name; } triads[] = {
+ { {0, 4, 8}, "Augmented triad" },
+ { {0, 4, 7}, "Major triad" },
+ { {0, 3, 7}, "Minor triad" },
+ { {0, 3, 6}, "Diminished triad" }
+ };
+
+ struct { int pitches[4]; const char *name; } sevenths[] = {
+ { {0, 4, 8, 11}, "Augmented/major 7" },
+ { {0, 4, 8, 10}, "Augmented 7" },
+ { {0, 4, 7, 11}, "Major 7" },
+ { {0, 4, 7, 10}, "Dominant 7" },
+ { {0, 3, 7, 11}, "Minor/major 7" },
+ { {0, 3, 7, 10}, "Minor 7" },
+ { {0, 3, 6, 11}, "Diminished/major 7" },
+ { {0, 3, 6, 10}, "Half-diminished 7" },
+ { {0, 3, 6, 9}, "Diminished 7" }
+ };
+
+ if (scherzo->chord) {
+ score_remove_chord (scherzo->chord);
+ scherzo->chord = NULL;
+ }
+
+ if (num_notes <= 1)
+ goto DONE;
+
+ notes = talloc_array (local, analyzed_note_t, num_notes);
+ if (notes == NULL)
+ goto DONE;
+
+ for (i = 0; i < num_notes; i++) {
+ score_note_t *note = note_group->notes[i];
+ notes[i].note = note;
+ notes[i].midi_pitch = _score_pitch_and_octave_to_midi (note->pitch,
+ note->octave);
+ /* Relative pitch will be filled in after sorting. */
+ notes[i].relative_pitch = 0;
+ }
+
+ /* First, sort by midi pitch to find the bass note. */
+ qsort (notes, num_notes, sizeof (analyzed_note_t),
+ _compare_analyzed_note_by_midi_pitch);
+
+ bass_pitch = notes[0].midi_pitch;
+
+ /* With the bass note identified, we can find all relative pitches. */
+ for (i = 0; i < num_notes; i++) {
+ notes[i].relative_pitch = notes[i].midi_pitch - bass_pitch;
+ while (notes[i].relative_pitch >= 12)
+ notes[i].relative_pitch -= 12;
+ }
+
+ /* Now, sort again by relative pitch. */
+ qsort (notes, num_notes, sizeof (analyzed_note_t),
+ _compare_analyzed_note_by_relative_pitch);
+
+ /* Finally, eliminate all duplicate notes. */
+ for (i = 0; i < num_notes - 1; i++) {
+ if (notes[i+1].relative_pitch == notes[i].relative_pitch) {
+ j = i+1;
+ while (j < num_notes &&
+ notes[j].relative_pitch == notes[i].relative_pitch)
+ {
+ j++;
+ }
+ /* The loop incremented j one past the last
+ * duplicate. Decrement so that it points to the
+ * last duplicate (and is guaranteed to not exceed
+ * the array bounds).*/
+ j--;
+
+ if (j < num_notes - 1) {
+ memmove (¬es[i+1], ¬es[j+1],
+ (num_notes - j) * sizeof (analyzed_note_t));
+ }
+
+ num_notes -= (j - i);
+ }
+ }
+
+ switch (num_notes) {
+ case 1:
+ for (i = 0; i < ARRAY_SIZE (octaves); i++) {
+ if (_chord_signature_matches (notes, num_notes, octaves[i].pitches, 1))
+ chord_name = octaves[i].name;
+ }
+ break;
+ case 2:
+ for (i = 0; i < ARRAY_SIZE (intervals); i++) {
+ if (_chord_signature_matches (notes, num_notes, intervals[i].pitches, 2))
+ chord_name = intervals[i].name;
+ }
+ break;
+ case 3:
+ for (i = 0; i < ARRAY_SIZE (triads); i++) {
+ if (_chord_signature_matches (notes, num_notes, triads[i].pitches, 3))
+ chord_name = triads[i].name;
+ }
+ break;
+ case 4:
+ for (i = 0; i < ARRAY_SIZE(sevenths); i++) {
+ if (_chord_signature_matches (notes, num_notes, sevenths[i].pitches, 4))
+ chord_name = sevenths[i].name;
+ }
+ break;
+ }
+
+ if (chord_name)
+ scherzo->chord = score_add_chord (scherzo->treble, chord_name);
+ else
+ scherzo->chord = score_add_chord (scherzo->treble, "Unknown or not a chord");
+
+DONE:
+ talloc_free (local);
+}
+
+static void
+note_group_init (void *ctx, note_group_t *group)
+{
+ group->ctx = ctx;
+ group->notes = NULL;
+ group->size = 0;
+ group->num_notes = 0;
+}
+
+static void
+note_group_add_note (note_group_t *group, score_note_t *note)
+{
+ int i;
+
+ /* Do nothing if note is already in group. */
+ for (i = 0; i < group->num_notes; i++) {
+ if (group->notes[i]->pitch == note->pitch &&
+ group->notes[i]->octave == note->octave)
+ {
+ return;
+ }
+ }
+
+ group->num_notes++;
+
+ if (group->num_notes > group->size) {
+ group->size++;
+ group->notes = talloc_realloc (group->ctx, group->notes,
+ score_note_t*, group->size);
+
+ if (group->notes == NULL) {
+ fprintf (stderr, "Out of memory.\n");
+ exit (1);
+ }
+ }
+
+ group->notes[group->num_notes - 1] = note;
+}
+
+static void
+note_group_remove_note_at (note_group_t *group, int i)
+{
+ if (i >= group->num_notes) {
+ fprintf (stderr, "Internal error: No note to remove at index %d\n", i);
+ exit (1);
+ }
+
+ if (i < group->num_notes - 1) {
+ memmove (group->notes + i, group->notes + i + 1,
+ (group->num_notes - 1 - i) * sizeof (score_note_t*));
+ }
+ group->num_notes--;
+}
+