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/ .
23 #include "scherzo-key.h"
25 #define ARRAY_SIZE(arr) ((int) (sizeof(arr) / sizeof(arr[0])))
28 scherzo_key_init (scherzo_key_t *key, pitch_t pitch)
32 pitch_t sharp_keys[] = {
33 PITCH_CLASS_LITERAL (C, NATURAL),
34 PITCH_CLASS_LITERAL (G, NATURAL),
35 PITCH_CLASS_LITERAL (D, NATURAL),
36 PITCH_CLASS_LITERAL (A, NATURAL),
37 PITCH_CLASS_LITERAL (E, NATURAL),
38 PITCH_CLASS_LITERAL (B, NATURAL),
39 PITCH_CLASS_LITERAL (F, SHARP),
40 PITCH_CLASS_LITERAL (C, SHARP),
43 pitch_t flat_keys[] = {
44 PITCH_CLASS_LITERAL (C, NATURAL),
45 PITCH_CLASS_LITERAL (F, NATURAL),
46 PITCH_CLASS_LITERAL (B, FLAT),
47 PITCH_CLASS_LITERAL (E, FLAT),
48 PITCH_CLASS_LITERAL (A, FLAT),
49 PITCH_CLASS_LITERAL (D, FLAT),
50 PITCH_CLASS_LITERAL (G, FLAT),
51 PITCH_CLASS_LITERAL (C, FLAT)
54 key->pitch = PITCH_CLASS (PITCH_NAME (pitch), PITCH_ACCIDENTAL (pitch));
57 for (i = 0; i < ARRAY_SIZE (sharp_keys); i++)
58 if (sharp_keys[i] == key->pitch)
62 for (i = 0; i < ARRAY_SIZE (flat_keys); i++)
63 if (flat_keys[i] == key->pitch)
68 scherzo_key_contains_pitch (scherzo_key_t *key, pitch_t pitch)
70 pitch_accidental_t accidental = PITCH_ACCIDENTAL (pitch);
73 pitch_name_t sharps_order[] = {
83 pitch_name_t flats_order[] = {
93 if (accidental == PITCH_ACCIDENTAL_DOUBLE_SHARP ||
94 accidental == PITCH_ACCIDENTAL_DOUBLE_FLAT)
99 if (key->num_sharps) {
100 if (accidental == PITCH_ACCIDENTAL_FLAT)
102 for (i = 0; i < key->num_sharps; i++) {
103 if (sharps_order[i] == PITCH_NAME (pitch)) {
104 if (accidental == PITCH_ACCIDENTAL_SHARP)
110 if (accidental == PITCH_ACCIDENTAL_SHARP)
116 if (key->num_flats) {
117 if (accidental == PITCH_ACCIDENTAL_SHARP)
119 for (i = 0; i < key->num_flats; i++) {
120 if (flats_order[i] == PITCH_NAME (pitch)) {
121 if (accidental == PITCH_ACCIDENTAL_FLAT)
127 if (accidental == PITCH_ACCIDENTAL_FLAT)
133 if (accidental == PITCH_ACCIDENTAL_NATURAL)
140 scherzo_key_spell_pitch (scherzo_key_t *key, pitch_t pitch)
142 pitch_t root = key->pitch;
143 int half_steps = pitch_from_root_in_half_steps (pitch, root);
144 pitch_name_t new_pitch_name;
146 int half_steps_per_degree[] = {
150 for (i = 0; i < ARRAY_SIZE (half_steps_per_degree); i++)
151 if (half_steps_per_degree[i] == half_steps)
157 new_pitch_name = (PITCH_NAME (root) + i) % 7;
159 /* Original pitch is spelled correctly. Return it as is. */
160 if (new_pitch_name == PITCH_NAME (pitch))
163 return pitch_spell_as_degree (pitch, root, i + 1);