]> git.cworth.org Git - scherzo/blob - pitch.h
4467aa86a825c239faa6842819c21d85b58fb1e6
[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       (0x07 << 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(name, accidental) PITCH(name, accidental, 0)
59
60 #define PITCH_CLASS_LITERAL(literal_name, literal_accidental) \
61     PITCH_LITERAL(literal_name, literal_accidental, 0)
62
63 typedef enum pitch_accidental
64 {
65     PITCH_ACCIDENTAL_DOUBLE_FLAT,
66     PITCH_ACCIDENTAL_FLAT,
67     PITCH_ACCIDENTAL_NATURAL,
68     PITCH_ACCIDENTAL_SHARP,
69     PITCH_ACCIDENTAL_DOUBLE_SHARP
70 } pitch_accidental_t;
71
72 typedef enum pitch_name
73 {
74     PITCH_NAME_C,
75     PITCH_NAME_D,
76     PITCH_NAME_E,
77     PITCH_NAME_F,
78     PITCH_NAME_G,
79     PITCH_NAME_A,
80     PITCH_NAME_B,
81 } pitch_name_t;
82
83 /* Octave numbers are ISO octave numbers [0:8], (so Octave 4 is from
84  * middle C to the B above middle C).
85  */
86
87 /* Get a string representation of a pitch_t value.
88  *
89  * Note: The returned value is static to the pithc_string function and
90  * may be modified by future calls to pitch_string. So the caller
91  * should copy the value if needed. */
92 const char *
93 pitch_string (pitch_t pitch);
94
95 /* Return a new pitch, a number of octaves above 'pitch'
96  *
97  * Note: Maximum octave value is 8. A pitch with octave 8 will not be
98  * raised.
99  */
100 pitch_t
101 pitch_raise_by_octaves (pitch_t pitch, int octaves);
102
103 /* Return a new pitch, a number of octaves below 'pitch'
104  *
105  * Note: Minimum octave value is 9. A pitch with octave 8 will not be
106  * lowered.
107  */
108 pitch_t
109 pitch_lower_by_octaves (pitch_t pitch, int octaves);
110
111 #endif /* PITCH_H */