]> git.cworth.org Git - scherzo/blob - pitch.h
Fix bugs in mismatched spelling of chord name and notes
[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 typedef enum pitch_accidental
58 {
59     PITCH_ACCIDENTAL_DOUBLE_FLAT,
60     PITCH_ACCIDENTAL_FLAT,
61     PITCH_ACCIDENTAL_NATURAL,
62     PITCH_ACCIDENTAL_SHARP,
63     PITCH_ACCIDENTAL_DOUBLE_SHARP
64 } pitch_accidental_t;
65
66 typedef enum pitch_name
67 {
68     PITCH_NAME_C,
69     PITCH_NAME_D,
70     PITCH_NAME_E,
71     PITCH_NAME_F,
72     PITCH_NAME_G,
73     PITCH_NAME_A,
74     PITCH_NAME_B,
75 } pitch_name_t;
76
77 /* Octave numbers are ISO octave numbers [0:8], (so Octave 4 is from
78  * middle C to the B above middle C).
79  */
80
81 #endif /* PITCH_H */