1 /* scherzo - Music notation training
3 * pitch.h - Common structures and functions for pitches, etc.
5 * Copyright © 2010,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/ .
26 typedef uint32_t pitch_t;
28 #define PITCH_ACCIDENTAL_SHIFT (0)
29 #define PITCH_ACCIDENTAL_MASK (0x07 << PITCH_ACCIDENTAL_SHIFT)
31 #define PITCH_NAME_SHIFT (3)
32 #define PITCH_NAME_MASK (0x07 << PITCH_NAME_SHIFT)
34 #define PITCH_OCTAVE_SHIFT (6)
35 #define PITCH_OCTAVE_MASK (0x07 << PITCH_OCTAVE_SHIFT)
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)
44 #define PITCH(name, accidental, octave) \
45 (((octave) << PITCH_OCTAVE_SHIFT) | \
46 ((name) << PITCH_NAME_SHIFT) | \
47 ((accidental) << PITCH_ACCIDENTAL_SHIFT))
49 #define PITCH_LITERAL(literal_name, literal_accidental, octave) \
50 PITCH(PITCH_NAME_##literal_name, \
51 PITCH_ACCIDENTAL_##literal_accidental, \
54 #define PITCH_NATURAL(literal_name, octave) \
55 PITCH_LITERAL(literal_name, NATURAL, octave)
57 /* PITCH_CLASS is useful for comparing pitches while ignoring any octave. */
58 #define PITCH_CLASS(name, accidental) PITCH(name, accidental, 0)
60 #define PITCH_CLASS_LITERAL(literal_name, literal_accidental) \
61 PITCH_LITERAL(literal_name, literal_accidental, 0)
63 #define PITCH_NOT_A_PITCH ((pitch_t) -1)
65 typedef enum pitch_accidental
67 PITCH_ACCIDENTAL_DOUBLE_FLAT,
68 PITCH_ACCIDENTAL_FLAT,
69 PITCH_ACCIDENTAL_NATURAL,
70 PITCH_ACCIDENTAL_SHARP,
71 PITCH_ACCIDENTAL_DOUBLE_SHARP
74 typedef enum pitch_name
85 /* Octave numbers are ISO octave numbers [0:8], (so Octave 4 is from
86 * middle C to the B above middle C).
89 /* Get a string representation of a pitch_t value.
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. */
95 pitch_string (pitch_t pitch);
97 /* Return a new pitch, a number of octaves above 'pitch'
99 * Note: Maximum octave value is 8. A pitch with octave 8 will not be
103 pitch_raise_by_octaves (pitch_t pitch, int octaves);
105 /* Return a new pitch, a number of octaves below 'pitch'
107 * Note: Minimum octave value is 9. A pitch with octave 8 will not be
111 pitch_lower_by_octaves (pitch_t pitch, int octaves);
113 /* Number of half steps from 'root' up to 'pitch' (that is, counting
114 * the steps up a chromatic scale), within the same octave. */
116 pitch_from_root_in_half_steps (pitch_t pitch, pitch_t root);
118 /* Respell 'pitch' to have a name that matches the (diatonic) scale
119 * 'degree' from 'root'.
121 * If the pitch is too distant from the specified degree to be
122 * respelled, the original pitch will be returned unmodified.
125 pitch_spell_as_degree (pitch_t pitch, pitch_t root, int degree);
127 /* Return a MIDI note value corresponding to 'pitch' */
129 pitch_to_midi (pitch_t pitch);
131 /* Return the pitch_t value corresponding to the given MIDI note value. */
133 pitch_from_midi (unsigned char midi_note);