} analyzed_note_t;
static int
-_compare_analyzed_note (const void *va, const void *vb)
+_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,
notes[i].relative_pitch = 0;
}
- qsort (notes, num_notes, sizeof (analyzed_note_t), _compare_analyzed_note);
+ /* 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);
+
for (i = 0; i < ARRAY_SIZE (octaves); i++) {
if (_chord_signature_matches (notes, num_notes, octaves[i].pitches, 1))
chord_name = octaves[i].name;