Functions

/Users/garethloy/Musimathics/Musimat1.2/MusimatChapter9/C091406b.cpp File Reference

#include "MusimatChapter9.h"

Go to the source code of this file.

Functions

 MusimatChapter9Section (C091406b)
Integer getIndex (RealList L, Real R)
Static Void para1 ()
Static Void para2 ()

Function Documentation

Integer getIndex ( RealList  L,
Real  R 
)

Definition at line 16 of file C091406b.cpp.

                                    {
        Integer i;
        For (i = Length(L) - 1; i >= 0; i = i - 1) {
                If (R > L[i]) {
                        Return(i + 1);
                }
        }
        Return( 0 );
}
MusimatChapter9Section ( C091406b   )

Definition at line 2 of file C091406b.cpp.

References para1(), and para2().

                                 {
        Print("*** Traversing the Cumulative Distribution Function ***");
        /*****************************************************************************
         
         Traversing the Cumulative Distribution Function
         
         To automate this, we start at the top end of the cumulative distribution function and work 
         down. As we go, we compare the value of R to the current step size. We've gone one step too far 
         when the value of R exceeds the step size, so we return the previous step as the answer, and stop.
         *****************************************************************************/
        para1(); // Step into this function to continue.)
        para2(); // Step into this function to continue.)
}
Static Void para1 (  )

Definition at line 26 of file C091406b.cpp.

References f(), and getIndex().

                    {
        Print("*** Invoking getIndex() ***");
        /*****************************************************************************
         We can invoke getIndex() as follows:
         *****************************************************************************/
        extern RealList f; // f was defined in C091406.cpp line 24
        
        Real R = Random();
        Integer p = getIndex(f, R);  
        Print(p);
}
Static Void para2 (  )

Definition at line 38 of file C091406b.cpp.

References accumulate(), f(), getIndex(), guido(), n(), normalize(), and sum().

                    {
        /*****************************************************************************
         If R is 0.1, then p prints 0. Now let's bring all the pieces together. Here is a program that creates 
         a melody of 25 pitches favoring pitches that are at the low end of the chromatic scale:
         *****************************************************************************/
        Print("*** Melody of 25 low pitches ***");
        RealList f(12.0, 11.0, 10.0, 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0);
        StringList n("C", "Cs", "D", "Ds", "E", "F", "Fs", "G", "Gs", "A", "As", "B", "c");
        f = normalize(f, sum(f));       // replace f with its normalized form
        f = accumulate(f);      // calculate cumulative distribution function
        StringList s;   // a place to put the result
        
        For (Integer i = 0; i < 25; i = i + 1) {
                Integer p = getIndex(f, Random());
                s[i] = n[p];
        }
        
        Print("Melody: ", s);                                                   // print the melody
        
        /*****************************************************************************
         Running this program will generate something like figure 9.23, depending upon the values pro-
         duced by Random(). As we see, lower pitches are favored in approximately the proportions we 
         specified. The longer the sample melody, the more likely the pitch choices would conform on aver-
         age to the distribution function.
         *****************************************************************************/
}