X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=pitch.h;h=3d1dd2e125b98cda6cc4c2ecfe150c54278258db;hb=fbfb0b3acfebeb22fae538588557f523ee539617;hp=525f14c8ae5038426bfe97c0c879ee8c67a3d637;hpb=659845dcf5781c10a2b292f0b7f6fe372874ff8a;p=scherzo diff --git a/pitch.h b/pitch.h index 525f14c..3d1dd2e 100644 --- a/pitch.h +++ b/pitch.h @@ -23,8 +23,44 @@ #ifndef PITCH_H #define PITCH_H -#define PITCH_ACCIDENTAL_MASK 0x07 -#define PITCH_ACCIDENTAL_SHIFT 0 +typedef uint32_t pitch_t; + +#define PITCH_ACCIDENTAL_SHIFT (0) +#define PITCH_ACCIDENTAL_MASK (0x07 << PITCH_ACCIDENTAL_SHIFT) + +#define PITCH_NAME_SHIFT (3) +#define PITCH_NAME_MASK (0x07 << PITCH_NAME_SHIFT) + +#define PITCH_OCTAVE_SHIFT (6) +#define PITCH_OCTAVE_MASK (0x07 << PITCH_OCTAVE_SHIFT) + +#define PITCH_ACCIDENTAL(pitch) \ + (((pitch) & PITCH_ACCIDENTAL_MASK) >> PITCH_ACCIDENTAL_SHIFT) +#define PITCH_NAME(pitch) \ + (((pitch) & PITCH_NAME_MASK) >> PITCH_NAME_SHIFT) +#define PITCH_OCTAVE(pitch) \ + (((pitch) & PITCH_OCTAVE_MASK) >> PITCH_OCTAVE_SHIFT) + +#define PITCH(name, accidental, octave) \ + (((octave) << PITCH_OCTAVE_SHIFT) | \ + ((name) << PITCH_NAME_SHIFT) | \ + ((accidental) << PITCH_ACCIDENTAL_SHIFT)) + +#define PITCH_LITERAL(literal_name, literal_accidental, octave) \ + PITCH(PITCH_NAME_##literal_name, \ + PITCH_ACCIDENTAL_##literal_accidental, \ + octave) + +#define PITCH_NATURAL(literal_name, octave) \ + PITCH_LITERAL(literal_name, NATURAL, octave) + +/* PITCH_CLASS is useful for comparing pitches while ignoring any octave. */ +#define PITCH_CLASS(name, accidental) PITCH(name, accidental, 0) + +#define PITCH_CLASS_LITERAL(literal_name, literal_accidental) \ + PITCH_LITERAL(literal_name, literal_accidental, 0) + +#define PITCH_NOT_A_PITCH ((pitch_t) -1) typedef enum pitch_accidental { @@ -35,11 +71,6 @@ typedef enum pitch_accidental PITCH_ACCIDENTAL_DOUBLE_SHARP } pitch_accidental_t; -#define PITCH_ACCIDENTAL(pitch) (((pitch) & PITCH_ACCIDENTAL_MASK) >> PITCH_ACCIDENTAL_SHIFT) - -#define PITCH_NAME_MASK 0x38 -#define PITCH_NAME_SHIFT 3 - typedef enum pitch_name { PITCH_NAME_C, @@ -51,13 +82,32 @@ typedef enum pitch_name PITCH_NAME_B, } pitch_name_t; -#define PITCH_NAME(pitch) (((pitch) & PITCH_NAME_MASK) >> PITCH_NAME_SHIFT) +/* Octave numbers are ISO octave numbers [0:8], (so Octave 4 is from + * middle C to the B above middle C). + */ -#define PITCH(name, accidental) (((name) << PITCH_NAME_SHIFT) | (accidental)) +/* Get a string representation of a pitch_t value. + * + * Note: The returned value is static to the pithc_string function and + * may be modified by future calls to pitch_string. So the caller + * should copy the value if needed. */ +const char * +pitch_string (pitch_t pitch); -#define PITCH_LITERAL(literal_name, literal_accidental) PITCH(PITCH_NAME_##literal_name, PITCH_ACCIDENTAL_##literal_accidental) -#define PITCH_NATURAL(literal_name) PITCH_LITERAL(literal_name, NATURAL) +/* Return a new pitch, a number of octaves above 'pitch' + * + * Note: Maximum octave value is 8. A pitch with octave 8 will not be + * raised. + */ +pitch_t +pitch_raise_by_octaves (pitch_t pitch, int octaves); -typedef uint32_t pitch_t; +/* Return a new pitch, a number of octaves below 'pitch' + * + * Note: Minimum octave value is 9. A pitch with octave 8 will not be + * lowered. + */ +pitch_t +pitch_lower_by_octaves (pitch_t pitch, int octaves); #endif /* PITCH_H */