]> git.cworth.org Git - scherzo/blobdiff - scherzo-key.c
Spell input notes according to key
[scherzo] / scherzo-key.c
index 67d2c1e2875f7a2a4388b5726a338e38bf91ac43..18cd261f6d5823e6727978b19397a415af23c592 100644 (file)
@@ -135,3 +135,30 @@ scherzo_key_contains_pitch (scherzo_key_t *key, pitch_t pitch)
     else
        return false;
 }
+
+pitch_t
+scherzo_key_spell_pitch (scherzo_key_t *key, pitch_t pitch)
+{
+    pitch_t root = key->pitch;
+    int half_steps = pitch_from_root_in_half_steps (pitch, root);
+    pitch_name_t new_pitch_name;
+    int i;
+    int half_steps_per_degree[] = {
+       0, 2, 4, 5, 7, 9, 11
+    };
+
+    for (i = 0; i < ARRAY_SIZE (half_steps_per_degree); i++)
+       if (half_steps_per_degree[i] == half_steps)
+           goto WITHIN_SCALE;
+
+    return pitch;
+
+WITHIN_SCALE:
+    new_pitch_name = (PITCH_NAME (root) + i) % 7;
+
+    /* Original pitch is spelled correctly. Return it as is. */
+    if (new_pitch_name == PITCH_NAME (pitch))
+       return pitch;
+
+    return pitch_spell_as_degree (pitch, root, i + 1);
+}