#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) |
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.
Definition at line 34 of file Pitch.h.
| Pitch::Pitch | ( | ) | [inline] |
Default Pitch constructor -- sets all fields to 0.
Definition at line 67 of file Pitch.h.
Referenced by operator *=(), operator%(), operator+(), operator+=(), operator-=(), operator/(), operator/=(), and Pitch().
| Pitch::Pitch | ( | Integer | degree | ) | [inline] |
Pitch constructor from degree/accidental/octave.
| pitchClass | Scale degree | |
| accidental | Accidental | |
| octave | Octave |
Definition at line 79 of file Pitch.h.
00079 : 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.
00077 : m_pc(0), m_acc(0), m_oct(0) { 00078 If ((m_pc = isPitchClass(*s)) == -1) 00079 Return; 00080 00081 Integer acc = 0; 00082 While ((acc = isAccidental(*++s)) != -3) { 00083 m_acc += acc; 00084 } 00085 00086 If (*s == 0) 00087 Return; 00088 Else 00089 m_oct = atoi(s); 00090 }
| 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 88 of file Pitch.h.
References For, If, Pitch(), and Return.
00088 : m_pc(0), m_acc(0), m_oct(0) { 00089 For(Integer pc = s_lowestDegree; pc < s_lowestDegree+s_highestDegree; pc++) { 00090 Pitch p = Pitch(pc); 00091 Real x = p.hertz(); // get the Hz equivalent of this pitch 00092 If ( x > hz) { // have we passed our target? 00093 pc -= 1; 00094 *this = Pitch(pc); // return the pitch 00095 Return; 00096 } 00097 } 00098 // If we get here, the hz value is out of range 00099 *this = Pitch(s_highestDegree); // Pitch is out of range, clip at top of range 00100 }
| Integer Pitch::accidental | ( | ) | [inline] |
| Integer Pitch::degree | ( | ) | [inline] |
Degree is the index of this pitch in pitch space.
Definition at line 105 of file Pitch.h.
References Return.
Referenced by LinearInterpolate(), operator *(), operator *=(), operator Integer(), 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 455 of file Pitch.h.
References accidental(), octave(), pitchClass(), Pow(), and Return.
Referenced by operator *(), operator *=(), operator Real(), operator!=(), operator%(), operator+(), operator+=(), operator-(), operator-=(), operator/(), operator/=(), operator<(), operator<=(), operator==(), operator>(), operator>=(), and PitchToHertz().
00455 { 00456 Real R = s_HzReference; 00457 Real k = pitchClass() + accidental() - 9.0; 00458 Real o = octave() - 4.0; 00459 Return( R * Pow(2.0, o + k / Real(s_nDegrees) ) ); 00460 }
| Static Real Pitch::HzReference | ( | ) | [inline] |
Get the reference frequency of the scale.
Definition at line 466 of file Pitch.h.
References Return.
Referenced by KeyToHertz().
00466 { Return( s_HzReference); }
| Static Void Pitch::HzReference | ( | Real | ref | ) | [inline] |
| Void Pitch::invert | ( | ) | [inline] |
Void invert() Inversion modulo the number of degrees in the scale (s_nDegrees).
Definition at line 172 of file Pitch.h.
References Mod(), and transpose().
00172 { 00173 Integer p = Mod( m_pc + m_acc, s_nDegrees ); 00174 p = Mod( s_nDegrees - p, s_nDegrees ); 00175 transpose( p, m_pc = 0, m_acc = 0, m_oct ); 00176 }
| 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 497 of file Pitch.h.
Referenced by operator>>(), and Pitch().
00497 { 00498 switch (c) { 00499 case '#': Return 1; Break; // sharp 00500 case 'S': case 's': Return 1; Break; // sharp 00501 case 'N': case 'n': Return 0; Break; // natural 00502 case 'B': case 'b': Return -1; Break; // flat 00503 case 'F': case 'f': Return -1; Break; // flat 00504 case 'X': case 'x': Return 2; Break; // double-sharp 00505 case 'V': case 'v': Return -2; Break; // double-flat 00506 default: Return -3; Break; // may be an octave spec. with no accidental 00507 } 00508 }
| 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 480 of file Pitch.h.
Referenced by operator>>(), and Pitch().
00480 { 00481 switch (c) { 00482 case 'C': case 'c': Return 0; Break; 00483 case 'D': case 'd': Return 2; Break; 00484 case 'E': case 'e': Return 4; Break; 00485 case 'F': case 'f': Return 5; Break; 00486 case 'G': case 'g': Return 7; Break; 00487 case 'A': case 'a': Return 9; Break; 00488 case 'B': case 'b': Return 11; Break; 00489 default: Return -1; 00490 } 00491 }
| 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 425 of file Pitch.h.
References Break, Character, Else, If, and Return.
Referenced by operator<<().
00425 { 00426 If (s) 00427 cout << s; 00428 Character pc = s_pcNames[ m_pc ]; 00429 Character acc; 00430 switch (m_acc) { 00431 case 0: acc = 0; Break; // natural 00432 case 1: acc = 's'; Break; // sharp 00433 case -1: acc = 'b'; Break; // flat 00434 case 2: acc = 'x'; Break; // double-sharp 00435 case -2: acc = 'v'; Break; // double-flat 00436 default: acc = 0; Break; // natural 00437 } 00438 If (acc) 00439 sprintf(s_buf, "%c%c%d", pc, acc, m_oct); 00440 Else 00441 sprintf(s_buf, "%c%d", pc, m_oct); 00442 Return s_buf; 00443 }
| Static Void Pitch::SetDegrees | ( | Integer | ref | ) | [inline] |
Set the number of degrees in the scale's gamut.
Definition at line 475 of file Pitch.h.
Referenced by SetDegrees().
| 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 164 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 139 of file Pitch.h.
References Break, Else, If, and While.
Referenced by invert(), operator+=(), operator-=(), Pitch(), and transModuloOctave().
00139 { 00140 pc += acc + x; // add the pitch, its accidental and the transposition to get raw semitones 00141 acc = 0; // clear the accidental 00142 While ( pc >= s_nDegrees ) { pc = pc - s_nDegrees; oct++; } 00143 While ( pc < 0 ) { pc = pc + s_nDegrees; oct--; } 00144 // cvt. to diatonic 00145 switch (pc) { // adjust for enharmonic spelling 00146 case 0: Break; // C 00147 case 1: If (x>0) { pc--; acc++; } Else { pc++; acc--; } Break; // C# Db 00148 case 2: Break; // D 00149 case 3: If (x>0) { pc--; acc++; } Else { pc++; acc--; } Break; // D# Eb 00150 case 4: Break; // E 00151 case 5: Break; // F 00152 case 6: If (x>0) { pc--; acc++; } Else { pc++; acc--; } Break; // F# Gb 00153 case 7: Break; // G 00154 case 8: If (x>0) { pc--; acc++; } Else { pc++; acc--; } Break; // G# Ab 00155 case 9: Break; // A 00156 case 10:If (x>0) { pc--; acc++; } Else { pc++; acc--; } Break; // A# Bb 00157 case 11: Break; // B 00158 } 00159 }
| ostream& operator<< | ( | ostream & | os, | |
| Pitch & | p | |||
| ) | [friend] |
| istream& operator>> | ( | istream & | is, | |
| Pitch & | p | |||
| ) | [friend] |
Definition at line 40 of file Pitch.cpp.
00041 { 00042 //char buf[16]; 00043 //cin >> buf; 00044 //p = Pitch(buf); 00045 //Return is; 00046 char c; 00047 cin >> c; 00048 If ((p.m_pc = p.isPitchClass(c)) == -1) { 00049 cin.unget(); 00050 Return is; 00051 } 00052 00053 Integer acc = 0; 00054 While (cin >> c && ((acc = p.isAccidental(c)) != -3)) { 00055 p.m_acc += acc; 00056 } 00057 00058 If (!inRange(c, '0', '9')) { 00059 cin.unget(); 00060 Return is; 00061 } Else { 00062 char buf[16]; 00063 char* b = buf; 00064 Integer cnt = 0; 00065 cin.unget(); 00066 While (!cin.eof() && cin >> c && isnumeric(c) && cnt++ < 16) { 00067 char y = cin.eof(); 00068 *b++ = c; 00069 } 00070 cin.unget(); 00071 *b = '\0'; 00072 p.m_oct = atoi(buf); 00073 } 00074 Return is; 00075 }
1.4.7