00001 #include "MusimatTutorial.h" 00002 MusimatTutorialSection(B0201) { 00003 Print("*** B.2.1 Pitch ***"); 00004 /***************************************************************************** 00005 00006 B.2.1 Pitch 00007 00008 We would ideally like to have a uniform way to represent all pitch systems discussed in chapter 3. 00009 It would be convenient if we could do arithmetic on pitches, for example, to find the size of an 00010 interval by subtracting two pitches, to calculate the frequency of a pitch, or to get the pitch of 00011 a frequency. 00012 00013 Solving the simplest problem first, I designed a data type for the equal-tempered scale using the 00014 piano keyboard. This can be generalized to other scales. The gamut of a standard piano keyboard 00015 is 88 keys, indexed from 0 to 87, lowest to highest. We start by associating each key number with 00016 a name. The lowest pitch on standard pianos is A0, corresponding to key 0, and the highest pitch 00017 is C8, corresponding to key 87. Interval size in degrees is the difference between key indexes. For 00018 example, C4 is key 48 and F4 is key 53, so the interval C4 - F4 corresponds to five semitones, 00019 which is the diatonic interval of a fourth. 00020 00021 Musimat comes with a built-in data type called Pitch. By default, it assumes 12 degrees per 00022 octave, but the degrees can correspond to any frequencies, so for example, it can be used directly 00023 to create any dodecaphonic scale. It also can be adjusted to handle scales with other than 12 degrees 00024 per octave. 00025 00026 By default, the Pitch data type emulates common musical notation conventions regarding 00027 scale degrees, interval sizes, and transposition. For example, the pitch As4 (pitch class A# in the 00028 fourth piano octave) is defined as 00029 *****************************************************************************/ 00030 00031 Pitch As4 = Pitch(9,1,4); 00032 00033 /***************************************************************************** 00034 The first number, 9, represents the diatonic degree as the number of semitones above C. 00035 Diatonic pitch A is the ninth semitone above C (see figure B.1). The second number, 1, indi- 00036 cates the accidental. In this case, the A is sharped (raised by a semitone). The chromatic scale 00037 degree is obtained by adding the diatonic scale degree, 9, and the accidental, 1, which for A# 00038 yields 10 (see figure B.1). The third number, 4, indicates the octave on the standard piano 00039 keyboard. 00040 00041 The chromatic degrees from A0 to C8 are predefined in Musimat in both flats and sharps. Since 00042 As4 and Bb4 represent the same chromatic degree, the statement 00043 *****************************************************************************/ 00044 00045 Print(Bb4 == As4); 00046 00047 /***************************************************************************** 00048 prints True. In general, Pitch is defined by the triple (pitch-class, accidental, octave), where 00049 pitch-class is an integer from 0 to N-1, and N is the number of degrees in an octave. 00050 00051 In defining the pitch A#4, the triple (9, 1, 4) is assigned to the variable As4. Variable As4 00052 contains these three values as one compound entity. This compound value can be passed from one 00053 Pitch variable to the next. For example, the statements 00054 *****************************************************************************/ 00055 00056 Pitch x = As4; // assign As4 to x 00057 Print(x == As4); 00058 00059 /***************************************************************************** 00060 print True. Arithmetic can be performed on pitches to sharp or flat them. For example, 00061 *****************************************************************************/ 00062 00063 Print(Pitch(A4) + 1); 00064 00065 /***************************************************************************** 00066 prints As4, and 00067 *****************************************************************************/ 00068 00069 Print(Pitch(A4) - 3); 00070 00071 /***************************************************************************** 00072 prints Gb4. Similarly, 00073 *****************************************************************************/ 00074 00075 Print(Pitch(A4) * 3); 00076 00077 /***************************************************************************** 00078 prints C12, and 00079 *****************************************************************************/ 00080 00081 Print(Pitch(A4)/3); 00082 00083 /***************************************************************************** 00084 prints E1. 00085 00086 Each element of a Pitch can be accessed using these built-in functions: 00087 00088 PitchClass(Pitch p) 00089 Returns the diatonic pitch class. For example, if p is As4, 9 00090 is returned (see figure B.1). 00091 00092 Accidental(Pitch p) 00093 Returns the accidental as an integer, where 0 is natural, 00094 negative values are increasingly flat, and positive values are 00095 increasingly sharp. For example, if p is As4, 1 is returned. 00096 00097 Octave(Pitch p) 00098 Returns the octave on the piano keyboard. For example, if 00099 p is As4, 4 is returned. 00100 00101 These elements can be used to determine the piano key index corresponding to 00102 a particular pitch, as shown by the key() function defined below. 00103 *****************************************************************************/ 00104 00105 para1(); // Step into this function to continue the tutorial 00106 } 00107 00108 Integer key(Pitch p) { 00109 Integer pc = PitchClass(p); // from 0 .. 11 00110 Integer acc = Accidental(p); // -1=flat, 0=natural, 1=sharp 00111 Integer oct = Octave(p); // from 0 .. 8 00112 Return((pc + acc) + 12 * (oct - 1) + 3); // combine 00113 } 00114 00115 Static Void para1() { 00116 00117 /***************************************************************************** 00118 A way to think about the expression in the Return() statement is as follows. Say we want to find 00119 the piano key index for A0. We know it's the bottom note on the piano, so it should return an index 00120 value of 0. The triple of A0 is (9, 0, 0). The expression in the Return() statement equals 0 for 00121 this triple. Similarly, the triple of A4 is (9, 0, 4), and its corresponding key index is 48. 00122 00123 *****************************************************************************/ 00124 } 00125 00127 /* $Revision: 1.3 $ $Date: 2006/09/05 08:03:08 $ $Author: dgl $ $Name: $ $Id: B0201.cpp,v 1.3 2006/09/05 08:03:08 dgl Exp $ */ 00128 // The Musimat Tutorial © 2006 Gareth Loy 00129 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy 00130 // and published exclusively by The MIT Press. 00131 // This program is released WITHOUT ANY WARRANTY; without even the implied 00132 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00133 // For information on usage and redistribution, and for a DISCLAIMER OF ALL 00134 // WARRANTIES, see the file, "LICENSE.txt," in this distribution. 00135 // "Musimathics" is available here: http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916 00136 // Gareth Loy's Musimathics website: http://www.musimathics.com/ 00137 // The Musimat website: http://www.musimat.com/ 00138 // This program is released under the terms of the GNU General Public License 00139 // available here: http://www.gnu.org/licenses/gpl.txt 00140