Functions

/Users/garethloy/Musimathics/Musimat1.2/MusimatTutorial/B0201.cpp File Reference

#include "MusimatTutorial.h"

Go to the source code of this file.

Functions

 MusimatTutorialSection (B0201)
Integer key (Pitch p)
Static Void para1 ()

Function Documentation

Integer key ( Pitch  p )

Definition at line 108 of file B0201.cpp.

                     {
        Integer pc = PitchClass(p);                                                     // from 0 .. 11
        Integer acc = Accidental(p);                                            // -1=flat, 0=natural, 1=sharp
        Integer oct = Octave(p);                                                        // from 0 .. 8
        Return((pc + acc) + 12 * (oct - 1) + 3);                        // combine
}
MusimatTutorialSection ( B0201   )

Definition at line 2 of file B0201.cpp.

References para1(), and x.

                              {
        Print("*** B.2.1 Pitch ***");
        /*****************************************************************************
         
         B.2.1 Pitch
         
         We would ideally like to have a uniform way to represent all pitch systems discussed in chapter 3. 
         It would be convenient if we could do arithmetic on pitches, for example, to find the size of an 
         interval by subtracting two pitches, to calculate the frequency of a pitch, or to get the pitch of 
         a frequency.
         
         Solving the simplest problem first, I designed a data type for the equal-tempered scale using the 
         piano keyboard. This can be generalized to other scales. The gamut of a standard piano keyboard 
         is 88 keys, indexed from 0 to 87, lowest to highest. We start by associating each key number with 
         a name. The lowest pitch on standard pianos is A0, corresponding to key 0, and the highest pitch 
         is C8, corresponding to key 87. Interval size in degrees is the difference between key indexes. For 
         example, C4 is key 48 and F4 is key 53, so the interval C4 - F4 corresponds to five semitones, 
         which is the diatonic interval of a fourth.
         
         Musimat comes with a built-in data type called Pitch. By default, it assumes 12 degrees per 
         octave, but the degrees can correspond to any frequencies, so for example, it can be used directly 
         to create any dodecaphonic scale. It also can be adjusted to handle scales with other than 12 degrees 
         per octave.
         
         By default, the Pitch data type emulates common musical notation conventions regarding 
         scale degrees, interval sizes, and transposition. For example, the pitch As4 (pitch class A# in the 
         fourth piano octave) is defined as
         *****************************************************************************/
        
        Pitch As4 = Pitch(9,1,4);
        
        /*****************************************************************************
         The first number, 9, represents the diatonic degree as the number of semitones above C. 
         Diatonic pitch A is the ninth semitone above C (see figure B.1). The second number, 1, indi-
         cates the accidental. In this case, the A is sharped (raised by a semitone). The chromatic scale 
         degree is obtained by adding the diatonic scale degree, 9, and the accidental, 1, which for A# 
         yields 10 (see figure B.1). The third number, 4, indicates the octave on the standard piano 
         keyboard.
         
         The chromatic degrees from A0 to C8 are predefined in Musimat in both flats and sharps. Since 
         As4 and Bb4 represent the same chromatic degree, the statement
         *****************************************************************************/
        
        Print(Bb4 == As4);
        
        /*****************************************************************************
         prints True. In general, Pitch is defined by the triple (pitch-class, accidental, octave), where 
         pitch-class is an integer from 0 to N-1, and N is the number of degrees in an octave.
         
         In defining the pitch A#4, the triple (9, 1, 4) is assigned to the variable As4. Variable As4 
         contains these three values as one compound entity. This compound value can be passed from one 
         Pitch variable to the next. For example, the statements
         *****************************************************************************/
        
        Pitch x = As4; // assign As4 to x
        Print(x == As4);
        
        /*****************************************************************************
         print True. Arithmetic can be performed on pitches to sharp or flat them. For example, 
         *****************************************************************************/
        
        Print(Pitch(A4) + 1);
        
        /*****************************************************************************
         prints As4, and 
         *****************************************************************************/
        
        Print(Pitch(A4) - 3);
        
        /*****************************************************************************
         prints Gb4. Similarly, 
         *****************************************************************************/
        
        Print(Pitch(A4) * 3);
        
        /*****************************************************************************
         prints C12, and 
         *****************************************************************************/
        
        Print(Pitch(A4)/3);
        
        /*****************************************************************************
         prints E1.
         
         Each element of a Pitch can be accessed using these built-in functions:
         
         PitchClass(Pitch p)    
         Returns the diatonic pitch class. For example, if p is As4, 9 
         is returned (see figure B.1).
         
         Accidental(Pitch p)    
         Returns the accidental as an integer, where 0 is natural, 
         negative values are increasingly flat, and positive values are 
         increasingly sharp. For example, if p is As4, 1 is returned.
         
         Octave(Pitch p)        
         Returns the octave on the piano keyboard. For example, if 
         p is As4, 4 is returned.
         
         These elements can be used to determine the piano key index corresponding to 
         a particular pitch, as shown by the key() function defined below.
         *****************************************************************************/
        
        para1(); // Step into this function to continue the tutorial
}
Static Void para1 (  )

Definition at line 115 of file B0201.cpp.

                    {
        
        /*****************************************************************************
         A way to think about the expression in the Return() statement is as follows. Say we want to find 
         the piano key index for A0. We know it's the bottom note on the piano, so it should return an index 
         value of 0. The triple of A0 is (9, 0, 0). The expression in the Return() statement equals 0 for 
         this triple. Similarly, the triple of A4 is (9, 0, 4), and its corresponding key index is 48.
         
         *****************************************************************************/
}