Pitch implements representation and manipulation of musical pitches. More...
#include <Pitch.h>
Public Member Functions | |
operator Integer () | |
operator Real () | |
Pitch () | |
Default Pitch constructor -- sets all fields to 0. | |
Pitch (Integer degree) | |
Pitch constructor from scale degree. | |
Pitch (Integer pitchClass, Integer accidental, Integer octave) | |
Pitch constructor from degree/accidental/octave. | |
Pitch (String s) | |
Pitch constructor from string encoding. | |
Pitch (Real hz) | |
Pitch constructor from frequency in Hz. | |
Integer | degree () Const |
Degree is the index of this pitch in pitch space. | |
Integer | degree (Integer pc, Integer acc, Integer oct) Const |
Find the degree corresponding to pitch. | |
Integer | pianoKey () Const |
The piano key index. | |
Integer | pianoKey (Integer pc, Integer acc, Integer oct) Const |
Find the piano key corresponding to pitch. | |
Void | transpose (Integer x, Integer &pc, Integer &acc, Integer &oct) |
Transpose pitch. | |
Void | transModuloOctave (Integer x) |
Void transModuloOctave(Integer x) Transposition modulo the number of degrees in the scale (s_nDegrees) | |
Void | invert () |
Void invert() Inversion modulo the number of degrees in the scale (s_nDegrees) | |
Bool | operator== (Const Pitch &x) Const |
Pitch equality operator. | |
Bool | operator== (Const Integer &x) Const |
Pitch equality operator with Integer. | |
Bool | operator== (Const Real &x) Const |
Pitch equality operator with Real. | |
Bool | operator!= (Const Pitch &x) Const |
Pitch inequality operator. | |
Bool | operator!= (Const Integer &x) Const |
Pitch inequality operator with Integer. | |
Bool | operator!= (Const Real &x) Const |
Pitch inequality operator with Real. | |
Bool | operator>= (Const Pitch &x) Const |
Pitch >= operator. | |
Bool | operator>= (Const Integer &x) Const |
Pitch >= operator with Integer. | |
Bool | operator>= (Const Real &x) Const |
Pitch >= operator with Real. | |
Bool | operator> (Const Pitch &x) Const |
Pitch > operator. | |
Bool | operator> (Const Integer &x) Const |
Pitch > operator with Integer. | |
Bool | operator> (Const Real &x) Const |
Pitch > operator with Real. | |
Bool | operator< (Const Pitch &x) Const |
Pitch < operator. | |
Bool | operator< (Const Integer &x) Const |
Pitch < operator with Integer. | |
Bool | operator< (Const Real &x) Const |
Pitch < operator with Real. | |
Bool | operator<= (Const Pitch &x) Const |
Pitch <= operator. | |
Bool | operator<= (Const Integer &x) Const |
Pitch <= operator with Integer. | |
Bool | operator<= (Const Real &x) Const |
Pitch <= operator with Real. | |
Pitch & | operator+= (Const Pitch x) |
Pitch increment operator. | |
Pitch & | operator+= (Const Integer x) |
Pitch increment operator with Integer. | |
Pitch & | operator+= (Const Real x) |
Pitch increment operator with Real. | |
Pitch & | operator-= (Const Pitch x) |
Pitch decrement operator. | |
Pitch & | operator-= (Const Integer x) |
Pitch decrement operator with Integer. | |
Pitch & | operator-= (Const Real x) |
Pitch decrement operator with Real. | |
Pitch & | operator*= (Const Pitch x) |
Pitch multiply scale operator. | |
Pitch & | operator*= (Const Integer x) |
Pitch multiply scale operator with Integer. | |
Pitch & | operator*= (Const Real x) |
Pitch multiply scale operator with Real. | |
Pitch & | operator/= (Const Pitch x) |
Pitch divide scale operator. | |
Pitch & | operator/= (Const Integer x) |
Pitch divide scale operator with Integer. | |
Pitch & | operator/= (Const Real x) |
Pitch divide scale operator with Real. | |
Pitch | operator+ (Const Pitch &x) |
Pitch addition operator. | |
Pitch | operator+ (Const Integer x) |
Pitch addition operator with Integer. | |
Pitch | operator+ (Const Real x) |
Pitch addition operator with Real. | |
Pitch | operator- (Const Pitch &x) |
Pitch subtraction operator. | |
Pitch | operator- (Const Integer x) |
Pitch subtraction operator with Integer. | |
Pitch | operator- (Const Real x) |
Pitch subtraction operator with Real. | |
Pitch | operator* (Const Pitch &x) |
Pitch multiplication operator. | |
Pitch | operator* (Const Integer x) |
Pitch multiplication operator with Integer. | |
Pitch | operator* (Const Real x) |
Pitch multiplication operator with Real. | |
Pitch | operator/ (Const Pitch &x) |
Pitch division operator. | |
Pitch | operator/ (Const Integer x) |
Pitch division operator with Integer. | |
Pitch | operator/ (Const Real x) |
Pitch division operator with Real. | |
Pitch | operator% (Const Pitch x) |
Pitch modulo operator. | |
Pitch | operator% (Const Integer x) |
Pitch modulo operator with Integer. | |
Pitch | operator% (Const Real x) |
Pitch modulo operator with Real. | |
String | print (String s=0) Const |
Print pitch prefixed by string. | |
Integer | pitchClass () Const |
The pitch class of this pitch. | |
Integer | accidental () Const |
The accidental of this pitch. | |
Integer | octave () Const |
The octave of this pitch. | |
Real | hertz () Const |
The frequency in Hz of this pitch. | |
Static Void | HzReference (Real ref) |
Set the reference frequency of the scale. | |
Static Real | HzReference () |
Get the reference frequency of the scale. | |
Static Void | SetLowestKey (Integer ref) |
Set the index of the lowest degree in the scale's gamut. | |
Static Void | SetHighestKey (Integer ref) |
Set the index of the highest degree in the scale's gamut. | |
Static Void | SetDegrees (Integer ref) |
Set the number of degrees in the scale's gamut. | |
Integer | isPitchClass (char c) Const |
Returns the chromatic degree of a diatonic step or -1 if not matched. | |
Integer | isAccidental (char c) Const |
Returns an offset for a chromatic degree. | |
Friends | |
ostream & | operator<< (ostream &, Pitch &) |
istream & | operator>> (istream &, Pitch &) |
Bool | operator== (Integer &i, Pitch &p) |
Bool | operator== (Pitch &p, Integer &i) |
Bool | operator!= (Integer &i, Pitch &p) |
Bool | operator!= (Pitch &p, Integer &i) |
Bool | operator>= (Pitch &p, Integer &r) |
Bool | operator>= (Integer &i, Pitch &p) |
Bool | operator> (Pitch &p, Integer &i) |
Bool | operator> (Integer &i, Pitch &p) |
Bool | operator<= (Pitch &p, Integer &i) |
Bool | operator<= (Integer &i, Pitch &p) |
Bool | operator== (Real &r, Pitch &p) |
Bool | operator== (Pitch &p, Real &r) |
Bool | operator!= (Real &r, Pitch &p) |
Bool | operator!= (Pitch &p, Real &r) |
Bool | operator>= (Pitch &p, Real &r) |
Bool | operator>= (Real &r, Pitch &p) |
Bool | operator> (Pitch &p, Real &r) |
Bool | operator> (Real &r, Pitch &p) |
Bool | operator<= (Pitch &p, Real &r) |
Bool | operator<= (Real &r, Pitch &p) |
Pitch implements representation and manipulation of musical pitches.
It operates by default on the equal tempered dodecaphonic scale, but can trivially be configured to just scales, microtonal scales, and non octave-based scales. Pitch class implements a set of data types, conversions, and operations for musical pitches, scales, and tuning systems.
Definition at line 37 of file Pitch.h.
Pitch::Pitch | ( | ) | [inline] |
Default Pitch constructor -- sets all fields to 0.
Definition at line 70 of file Pitch.h.
Referenced by operator%(), operator*=(), operator+(), operator+=(), operator-=(), operator/(), operator/=(), and Pitch().
: m_pc(0), m_acc(0), m_oct(0) { }
Pitch::Pitch | ( | Integer | degree ) | [inline] |
Pitch constructor from degree/accidental/octave.
pitchClass | Scale degree |
accidental | Accidental |
octave | Octave |
Definition at line 82 of file Pitch.h.
: m_pc(pitchClass), m_acc(accidental), m_oct(octave) { }
Pitch::Pitch | ( | String | s ) |
Pitch constructor from string encoding.
s | String containing pitch encoding |
Definition at line 77 of file Pitch.cpp.
References Else, If, isAccidental(), isPitchClass(), Return, and While.
: m_pc(0), m_acc(0), m_oct(0) { If ((m_pc = isPitchClass(*s)) == -1) Return; Integer acc = 0; While ((acc = isAccidental(*++s)) != -3) { m_acc += acc; } If (*s == 0) Return; Else m_oct = atoi(s); }
Pitch::Pitch | ( | Real | hz ) | [inline] |
Pitch constructor from frequency in Hz.
hz | Frequency of pitch in Hz. The nearest lower pitch class is chosen. For example, Pitch(440.5) chooses A4. |
Definition at line 91 of file Pitch.h.
References For, hertz(), If, Pitch(), and Return.
: m_pc(0), m_acc(0), m_oct(0) { For(Integer pc = s_lowestDegree; pc < s_lowestDegree+s_highestDegree; pc++) { Pitch p = Pitch(pc); Real x = p.hertz(); // get the Hz equivalent of this pitch If ( x > hz) { // have we passed our target? pc -= 1; *this = Pitch(pc); // return the pitch Return; } } // If we get here, the hz value is out of range *this = Pitch(s_highestDegree); // Pitch is out of range, clip at top of range }
Integer Pitch::accidental | ( | ) | [inline] |
Integer Pitch::degree | ( | ) | [inline] |
Degree is the index of this pitch in pitch space.
Definition at line 108 of file Pitch.h.
References Return.
Referenced by LinearInterpolate(), operator Integer(), operator!=(), operator%(), operator*(), operator*=(), operator+(), operator-(), operator/(), operator/=(), operator<(), operator<=(), operator==(), operator>(), and operator>=().
Real Pitch::hertz | ( | ) | [inline] |
The frequency in Hz of this pitch.
Definition at line 476 of file Pitch.h.
References accidental(), octave(), pitchClass(), Pow(), and Return.
Referenced by operator Real(), operator!=(), operator%(), operator*(), operator*=(), operator+(), operator+=(), operator-(), operator-=(), operator/(), operator/=(), operator<(), operator<=(), operator==(), operator>(), operator>=(), Pitch(), and PitchToHertz().
{ Real R = s_HzReference; Real k = pitchClass() + accidental() - 9.0; Real o = octave() - 4.0; Return( R * Pow(2.0, o + k / Real(s_nDegrees) ) ); }
Static Void Pitch::HzReference | ( | Real | ref ) | [inline] |
Static Real Pitch::HzReference | ( | ) | [inline] |
Get the reference frequency of the scale.
Definition at line 487 of file Pitch.h.
References Return.
Referenced by KeyToHertz().
{ Return( s_HzReference); }
Void Pitch::invert | ( | ) | [inline] |
Integer Pitch::isAccidental | ( | char | c ) | [inline] |
Returns an offset for a chromatic degree.
Accidentals are {#,S,s}=sharp, {N,n,<nil>}=natural, {B,b,F,f}=flat, X=double-sharp, V=double-flat Accidentals in a pitch specification may be repeated, so that Abb equals AV equals Bbbbb, etc.
Definition at line 518 of file Pitch.h.
Referenced by operator>>(), and Pitch().
{ switch (c) { case '#': Return 1; Break; // sharp case 'S': case 's': Return 1; Break; // sharp case 'N': case 'n': Return 0; Break; // natural case 'B': case 'b': Return -1; Break; // flat case 'F': case 'f': Return -1; Break; // flat case 'X': case 'x': Return 2; Break; // double-sharp case 'V': case 'v': Return -2; Break; // double-flat default: Return -3; Break; // may be an octave spec. with no accidental } }
Integer Pitch::isPitchClass | ( | char | c ) | [inline] |
Returns the chromatic degree of a diatonic step or -1 if not matched.
Pitch classes are the upper- or lower-case letters A-G
Definition at line 501 of file Pitch.h.
Referenced by operator>>(), and Pitch().
Integer Pitch::octave | ( | ) | [inline] |
Pitch::operator Integer | ( | ) | [inline] |
Pitch::operator Real | ( | ) | [inline] |
Integer Pitch::pianoKey | ( | ) | [inline] |
Integer Pitch::pitchClass | ( | ) | [inline] |
Print pitch prefixed by string.
s | String to prefix |
Definition at line 428 of file Pitch.h.
References Break, Character, Else, If, and Return.
Referenced by operator<<().
{ If (s) cout << s; Character pc = s_pcNames[ m_pc ]; Character acc; switch (m_acc) { case 0: acc = 0; Break; // natural case 1: acc = 's'; Break; // sharp case -1: acc = 'b'; Break; // flat case 2: acc = 'x'; Break; // double-sharp case -2: acc = 'v'; Break; // double-flat default: acc = 0; Break; // natural } If (acc) #ifdef _MSC_VER // Disable Microsoft Visual Studio warning about sprintf() // until is universal agreement about how to handle its insecurities #pragma warning(push) #pragma warning(disable: 4996) sprintf(s_buf, "%c%c%d", pc, acc, m_oct); #pragma warning(pop) #else sprintf(s_buf, "%c%c%d", pc, acc, m_oct); #endif Else #ifdef _MSC_VER // Disable Microsoft Visual Studio warning about sprintf() // until is universal agreement about how to handle its insecurities #pragma warning(push) #pragma warning(disable: 4996) sprintf(s_buf, "%c%d", pc, m_oct); #pragma warning(pop) #else sprintf(s_buf, "%c%d", pc, m_oct); #endif Return s_buf; }
Static Void Pitch::SetDegrees | ( | Integer | ref ) | [inline] |
Set the number of degrees in the scale's gamut.
Definition at line 496 of file Pitch.h.
Referenced by SetDegrees().
{ s_nDegrees = ref; }
Static Void Pitch::SetHighestKey | ( | Integer | ref ) | [inline] |
Static Void Pitch::SetLowestKey | ( | Integer | ref ) | [inline] |
Void Pitch::transModuloOctave | ( | Integer | x ) | [inline] |
Void transModuloOctave(Integer x) Transposition modulo the number of degrees in the scale (s_nDegrees)
x | Degrees to transpose |
Definition at line 167 of file Pitch.h.
References transpose().
Transpose pitch.
x | Number of degrees to transpose by |
pc | pitch class |
acc | accidental |
oct | octave |
Definition at line 142 of file Pitch.h.
References Break, Else, If, and While.
Referenced by invert(), operator+=(), operator-=(), Pitch(), and transModuloOctave().
{ pc += acc + x; // add the pitch, its accidental and the transposition to get raw semitones acc = 0; // clear the accidental While ( pc >= s_nDegrees ) { pc = pc - s_nDegrees; oct++; } While ( pc < 0 ) { pc = pc + s_nDegrees; oct--; } // cvt. to diatonic switch (pc) { // adjust for enharmonic spelling case 0: Break; // C case 1: If (x>0) { pc--; acc++; } Else { pc++; acc--; } Break; // C# Db case 2: Break; // D case 3: If (x>0) { pc--; acc++; } Else { pc++; acc--; } Break; // D# Eb case 4: Break; // E case 5: Break; // F case 6: If (x>0) { pc--; acc++; } Else { pc++; acc--; } Break; // F# Gb case 7: Break; // G case 8: If (x>0) { pc--; acc++; } Else { pc++; acc--; } Break; // G# Ab case 9: Break; // A case 10:If (x>0) { pc--; acc++; } Else { pc++; acc--; } Break; // A# Bb case 11: Break; // B } }
ostream& operator<< | ( | ostream & | os, |
Pitch & | p | ||
) | [friend] |
istream& operator>> | ( | istream & | is, |
Pitch & | p | ||
) | [friend] |
Definition at line 40 of file Pitch.cpp.
{ //char buf[16]; //cin >> buf; //p = Pitch(buf); //Return is; char c; cin >> c; If ((p.m_pc = p.isPitchClass(c)) == -1) { cin.unget(); Return is; } Integer acc = 0; While (cin >> c && ((acc = p.isAccidental(c)) != -3)) { p.m_acc += acc; } If (!inRange(c, '0', '9')) { cin.unget(); Return is; } Else { char buf[16]; char* b = buf; Integer cnt = 0; cin.unget(); While (!cin.eof() && cin >> c && isnumeric(c) && cnt++ < 16) { // char y = cin.eof(); *b++ = c; } cin.unget(); *b = '\0'; p.m_oct = atoi(buf); } Return is; }