Pitch
This page details all the functions that create, transform or query Pitch
vectors.
Creating Pitches
Section titled “Creating Pitches”Pitch
vectors can be initialised directly:
Pitch p = { 25, 10 }; // initialised to middle C
It’s often more convenient, however, to be able to create a new Pitch
from existing vectors, or to assign them values by parsing strings in accepted formats.
pitch_from_chroma
Section titled “pitch_from_chroma”Pitch pitch_from_chroma(int chroma, int octave);
Creates a Pitch
vector from a specified chroma (signed distance from C in 5ths) and octave (following SPN numbering).
Common Queries
Section titled “Common Queries”These functions query commonly required information from Pitch
vectors.
pitch_chroma
Section titled “pitch_chroma”int pitch_chroma(Pitch p);
Returns the number of perfect fifths (signed) separating a Pitch
from C. Abstracts octave information away.
pitch_letter
Section titled “pitch_letter”int pitch_letter(Pitch p);
Returns the offset of a Pitch
’s letter from ‘A’ / ‘a’. It’s up to you to add ‘A’ or ‘a’ to the result to get an actual character though!
pitch_accidental
Section titled “pitch_accidental”int pitch_accidental(Pitch p);
Returns a Pitch
’s accidental as an offset from , so:
2 | |
1 | |
0 | |
-1 | |
-2 |
etc. for triple sharps/flats and beyond.
pitch_octave
Section titled “pitch_octave”int pitch_octave(Pitch p);
Returns the SPN octave number of a Pitch
. Middle C is 4.
pitch_midi
Section titled “pitch_midi”int pitch_midi(Pitch p);
Returns the standard MIDI value for a given Pitch
.
pitch_pc12
Section titled “pitch_pc12”int pitch_pc12(Pitch p);
Returns the 12-tone pitch class number of a Pitch
(C is 0).
steps_between
Section titled “steps_between”static inline int steps_between(Pitch p, Pitch q);
Returns the (signed) number of diatonic steps between two pitches. Calculated as .
pitches_equal
Section titled “pitches_equal”bool pitches_equal(Pitch p, Pitch q);
Predicate function that checks whether two Pitch
vectors are equal.
pitches_enharmonic
Section titled “pitches_enharmonic”bool pitches_enharmonic(Pitch m, Pitch n, int edo);
Predicate function that checks whether two Pitch
vectors are enharmonics in a given EDO tuning system. The third parameter ‘edo’ is for passing in the tuning system to check against. So for example:
Pitch p, q;pitch_from_spn("C#4", &p);pitch_from_spn("Db4", &q);pitches_enharmonic(p, q, 12); // true; C# and Db are enharmonic in 12tetpitches_enharmonic(p, q, 31); // false; C# and Db are not enharmonic in 31tet
pitch_from_spn("Gbb4", &p);pitch_from_spn("Ex4", &q);pitches_enharmonic(p, q, 31); // true; Gbb and Ex are enharmonic in 31tetpitches_enharmonic(p, q, 12); // false; Gbb and Ex are not enharmonic in 12tet
pitch_highest
Section titled “pitch_highest”Pitch pitch_highest(Pitch arr[], int len, TuningMap T);
This function takes a Pitch[]
array and a TuningMap
, and returns the highest Pitch
in the array. Uses the passed-in TuningMap
to decide which Pitch
is “higher” than the others.
char *names[10] = {"B#3", "C4", "D4", "D#4", "Eb4", "E4", "F4", "Ex4", "F#4", "Gb4"};Pitch arr[10];for (int i = 0; i < 10; i++) { pitch_from_spn(names[i], arr + i);}
TuningMap T = tuning_map_from_edo(12, (Pitch){29, 11}, 440);
Pitch highest = pitch_highest(arr, 10, T); // highest now holds Gb4
pitch_lowest
Section titled “pitch_lowest”Pitch pitch_lowest(Pitch arr[], int len, TuningMap T);
This function takes a Pitch[]
array and a TuningMap
, and returns the lowest Pitch
in the array. Uses the passed-in TuningMap
to decide which Pitch
is “lower” than the others.
char *names[10] = {"B#3", "C4", "D4", "D#4", "Eb4", "E4", "F4", "Ex4", "F#4", "Gb4"};Pitch arr[10];for (int i = 0; i < 10; i++) { pitch_from_spn(names[i], arr + i);}
TuningMap T = tuning_map_from_edo(12, (Pitch){29, 11}, 440);
Pitch lowest = pitch_lowest(arr, 10, T); // lowest now holds B#3
pitch_nearest
Section titled “pitch_nearest”Pitch pitch_nearest(Pitch p, Pitch arr[], int len, TuningMap T);
This function takes a Pitch
, a Pitch[]
array and a TuningMap
, and returns the closest Pitch
in the array to the Pitch
passed via the first argument. Uses the passed-in TuningMap
to decide which Pitch
is “closer” than the others.
char *names[10] = {"B#3", "C4", "D4", "D#4", "Eb4", "E4", "F4", "Ex4", "F#4", "Gb4"};Pitch arr[10];for (int i = 0; i < 10; i++) { pitch_from_spn(names[i], arr + i);}
Pitch target;pitch_from_spn("Gbb4", &target);
TuningMap T = tuning_map_from_edo(12, (Pitch){29, 11}, 440);Pitch nearest = pitch_nearest(target, arr, 10, T); // nearest now holds F4
T = tuning_map_from_edo(31, reference, 440);nearest = pitch_nearest(target, arr, 10, T); // nearest now holds Ex4
Transformations
Section titled “Transformations”transpose_real
Section titled “transpose_real”Pitch transpose_real(Pitch p, Interval m);
This function takes a Pitch
and an Interval
, and returns a new Pitch
formed by their sum.
axis_create
Section titled “axis_create”MirrorAxis axis_create(Pitch p, Pitch q);
Creates a MirrorAxis
about which to invert a Pitch
by adding two Pitch
vectors together. To be used in conjunction with pitch_invert
.
pitch_invert
Section titled “pitch_invert”Pitch pitch_invert(Pitch p, MirrorAxis a);
Invert a Pitch
about a passed in MirrorAxis
, returning a new Pitch
(the result of subtracting p
from a
). Inversion is real, not diatonic.
Conversions
Section titled “Conversions”pitch_to_standard
Section titled “pitch_to_standard”StandardPitch pitch_to_standard(Pitch p);
Converts from Meantonal’s standard Pitch
representation to the StandardPitch
representation. Useful as a preliminary step to rendering standard notation.
pitch_from_standard
Section titled “pitch_from_standard”Pitch pitch_from_standard(StandardPitch p);
Converts from the StandardPitch
type back to a regular Pitch
vector. Conversion in this direction is generally less used, but the StandardPitch
might represent a convenient parsing target to then extract Pitch
vectors from if you are working with a notation format Meantonal’s parse functions don’t cover.