+
+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);
+}