1 /* scherzo - Music notation training
3 * key.c - Common structures and functions for keys
5 * Copyright © 2013 Carl Worth
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see http://www.gnu.org/licenses/ .
25 #include "scherzo-key.h"
27 #define ARRAY_SIZE(arr) ((int) (sizeof(arr) / sizeof(arr[0])))
30 scherzo_key_init (scherzo_key_t *key, pitch_t pitch)
34 pitch_t flat_keys[] = {
35 PITCH_CLASS_LITERAL (C, NATURAL),
36 PITCH_CLASS_LITERAL (F, NATURAL),
37 PITCH_CLASS_LITERAL (B, FLAT),
38 PITCH_CLASS_LITERAL (E, FLAT),
39 PITCH_CLASS_LITERAL (A, FLAT),
40 PITCH_CLASS_LITERAL (D, FLAT),
41 PITCH_CLASS_LITERAL (G, FLAT),
42 PITCH_CLASS_LITERAL (C, FLAT)
45 pitch_t sharp_keys[] = {
46 PITCH_CLASS_LITERAL (C, NATURAL),
47 PITCH_CLASS_LITERAL (G, NATURAL),
48 PITCH_CLASS_LITERAL (D, NATURAL),
49 PITCH_CLASS_LITERAL (A, NATURAL),
50 PITCH_CLASS_LITERAL (E, NATURAL),
51 PITCH_CLASS_LITERAL (B, NATURAL),
52 PITCH_CLASS_LITERAL (F, SHARP),
53 PITCH_CLASS_LITERAL (C, SHARP),
56 /* Remove octave from 'pitch' */
57 pitch = PITCH_CLASS (pitch);
62 /* First, look for a key that exactly matches the specified pitch. */
63 for (i = 0; i < ARRAY_SIZE (flat_keys); i++) {
64 if (flat_keys[i] == pitch) {
65 key->pitch = flat_keys[i];
71 for (i = 0; i < ARRAY_SIZE (sharp_keys); i++) {
72 if (sharp_keys[i] == pitch) {
73 key->pitch = sharp_keys[i];
79 /* Second, if we haven't found a key, look for something enharmonic. */
80 for (i = 0; i < ARRAY_SIZE (flat_keys); i++) {
81 if (pitch_enharmonic_to (flat_keys[i], pitch)) {
82 key->pitch = flat_keys[i];
88 for (i = 0; i < ARRAY_SIZE (sharp_keys); i++) {
89 if (pitch_enharmonic_to (sharp_keys[i], pitch)) {
90 key->pitch = sharp_keys[i];
96 /* The pitch_enharmonic_to function won't catch this wraparound. */
97 if (pitch == PITCH_CLASS_LITERAL (B, SHARP)) {
98 key->pitch = PITCH_CLASS_LITERAL (C, NATURAL);
102 fprintf (stderr, "Interal error: Failed to find a key for %s.\n",
103 pitch_string (pitch));
108 scherzo_key_contains_pitch (scherzo_key_t *key, pitch_t pitch)
110 pitch_accidental_t accidental = PITCH_ACCIDENTAL (pitch);
113 pitch_name_t sharps_order[] = {
123 pitch_name_t flats_order[] = {
133 if (accidental == PITCH_ACCIDENTAL_DOUBLE_SHARP ||
134 accidental == PITCH_ACCIDENTAL_DOUBLE_FLAT)
139 if (key->num_sharps) {
140 if (accidental == PITCH_ACCIDENTAL_FLAT)
142 for (i = 0; i < key->num_sharps; i++) {
143 if (sharps_order[i] == PITCH_NAME (pitch)) {
144 if (accidental == PITCH_ACCIDENTAL_SHARP)
150 if (accidental == PITCH_ACCIDENTAL_SHARP)
156 if (key->num_flats) {
157 if (accidental == PITCH_ACCIDENTAL_SHARP)
159 for (i = 0; i < key->num_flats; i++) {
160 if (flats_order[i] == PITCH_NAME (pitch)) {
161 if (accidental == PITCH_ACCIDENTAL_FLAT)
167 if (accidental == PITCH_ACCIDENTAL_FLAT)
173 if (accidental == PITCH_ACCIDENTAL_NATURAL)
180 scherzo_key_spell_pitch (scherzo_key_t *key, pitch_t pitch)
182 pitch_t root = key->pitch;
183 int half_steps = pitch_from_root_in_half_steps (pitch, root);
184 pitch_name_t new_pitch_name;
186 int half_steps_per_degree[] = {
190 for (i = 0; i < ARRAY_SIZE (half_steps_per_degree); i++)
191 if (half_steps_per_degree[i] == half_steps)
197 new_pitch_name = (PITCH_NAME (root) + i) % 7;
199 /* Original pitch is spelled correctly. Return it as is. */
200 if (new_pitch_name == PITCH_NAME (pitch))
203 return pitch_spell_as_degree (pitch, root, i + 1);