]> git.cworth.org Git - scherzo/blob - pitch.h
Fix high octave numbers (8+) to not be interpreted as 0.
[scherzo] / pitch.h
1 /* scherzo - Music notation training
2  *
3  *      pitch.h - Common structures and functions for pitches, etc.
4  *
5  * Copyright © 2010,2013 Carl Worth
6  *
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.
11  *
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.
16  *
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/ .
19  */
20
21 #include <stdint.h>
22
23 #ifndef PITCH_H
24 #define PITCH_H
25
26 typedef uint32_t pitch_t;
27
28 #define PITCH_ACCIDENTAL_SHIFT  (0)
29 #define PITCH_ACCIDENTAL_MASK   (0x07 << PITCH_ACCIDENTAL_SHIFT)
30
31 #define PITCH_NAME_SHIFT        (3)
32 #define PITCH_NAME_MASK         (0x07 << PITCH_NAME_SHIFT)
33
34 #define PITCH_OCTAVE_SHIFT      (6)
35 #define PITCH_OCTAVE_MASK       (0x0F << PITCH_OCTAVE_SHIFT)
36
37 #define PITCH_ACCIDENTAL(pitch) \
38     (((pitch) & PITCH_ACCIDENTAL_MASK) >> PITCH_ACCIDENTAL_SHIFT)
39 #define PITCH_NAME(pitch)       \
40     (((pitch) & PITCH_NAME_MASK) >> PITCH_NAME_SHIFT)
41 #define PITCH_OCTAVE(pitch)     \
42     (((pitch) & PITCH_OCTAVE_MASK) >> PITCH_OCTAVE_SHIFT)
43
44 #define PITCH(name, accidental, octave)           \
45     (((octave) << PITCH_OCTAVE_SHIFT)           | \
46      ((name) << PITCH_NAME_SHIFT)               | \
47      ((accidental) << PITCH_ACCIDENTAL_SHIFT))
48
49 #define PITCH_LITERAL(literal_name, literal_accidental, octave) \
50     PITCH(PITCH_NAME_##literal_name,                            \
51           PITCH_ACCIDENTAL_##literal_accidental,                \
52           octave)
53
54 #define PITCH_NATURAL(literal_name, octave) \
55     PITCH_LITERAL(literal_name, NATURAL, octave)
56
57 /* PITCH_CLASS is useful for comparing pitches while ignoring any octave. */
58 #define PITCH_CLASS(pitch) PITCH(PITCH_NAME(pitch), PITCH_ACCIDENTAL(pitch), 0)
59
60 #define PITCH_CLASS_LITERAL(literal_name, literal_accidental) \
61     PITCH_LITERAL(literal_name, literal_accidental, 0)
62
63 #define PITCH_NOT_A_PITCH ((pitch_t) -1)
64
65 typedef enum pitch_accidental
66 {
67     PITCH_ACCIDENTAL_DOUBLE_FLAT,
68     PITCH_ACCIDENTAL_FLAT,
69     PITCH_ACCIDENTAL_NATURAL,
70     PITCH_ACCIDENTAL_SHARP,
71     PITCH_ACCIDENTAL_DOUBLE_SHARP
72 } pitch_accidental_t;
73
74 typedef enum pitch_name
75 {
76     PITCH_NAME_C,
77     PITCH_NAME_D,
78     PITCH_NAME_E,
79     PITCH_NAME_F,
80     PITCH_NAME_G,
81     PITCH_NAME_A,
82     PITCH_NAME_B,
83 } pitch_name_t;
84
85 /* Octave numbers are ISO octave numbers [0:8], (so Octave 4 is from
86  * middle C to the B above middle C).
87  */
88
89 /* Get a string representation of a pitch_t value.
90  *
91  * Note: The returned value is static to the pithc_string function and
92  * may be modified by future calls to pitch_string. So the caller
93  * should copy the value if needed. */
94 const char *
95 pitch_string (pitch_t pitch);
96
97 /* Return a new pitch, a number of octaves above 'pitch'
98  *
99  * Note: Maximum octave value is 8. A pitch with octave 8 will not be
100  * raised.
101  */
102 pitch_t
103 pitch_raise_by_octaves (pitch_t pitch, int octaves);
104
105 /* Return a new pitch, a number of octaves below 'pitch'
106  *
107  * Note: Minimum octave value is 9. A pitch with octave 8 will not be
108  * lowered.
109  */
110 pitch_t
111 pitch_lower_by_octaves (pitch_t pitch, int octaves);
112
113 /* Number of half steps from 'root' up to 'pitch' (that is, counting
114  * the steps up a chromatic scale), within the same octave. */
115 int
116 pitch_from_root_in_half_steps (pitch_t pitch, pitch_t root);
117
118 /* Respell 'pitch' to have a name that matches the (diatonic) scale
119  * 'degree' from 'root'.
120  *
121  * If the pitch is too distant from the specified degree to be
122  * respelled, the original pitch will be returned unmodified.
123  */
124 pitch_t
125 pitch_spell_as_degree (pitch_t pitch, pitch_t root, int degree);
126
127 /* Return a MIDI note value corresponding to 'pitch' */
128 unsigned char
129 pitch_to_midi (pitch_t pitch);
130
131 /* Return the pitch_t value corresponding to the given MIDI note value. */
132 pitch_t
133 pitch_from_midi (unsigned char midi_note);
134
135 /* Return true if 'a' and 'b' sound identical, (even if spelled differently)
136  *
137  * This comparison considers octaves as significant. So Bb and A# in
138  * the same octave are considered enharmonic, but Bb and A# in
139  * different octaves are not. */
140 int
141 pitch_enharmonic_to (pitch_t a, pitch_t b);
142
143 #endif /* PITCH_H */