X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=scherzo-key.c;h=18cd261f6d5823e6727978b19397a415af23c592;hb=f4431e3d1f117bf91014743f9cb18c88c5ae4459;hp=67d2c1e2875f7a2a4388b5726a338e38bf91ac43;hpb=45d36bd8372f38186ceebf74b48f1a67bd67c573;p=scherzo diff --git a/scherzo-key.c b/scherzo-key.c index 67d2c1e..18cd261 100644 --- a/scherzo-key.c +++ b/scherzo-key.c @@ -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); +}