C:/Musimathics_local/Musimat/MusimatChapter9/C091406b.cpp

Go to the documentation of this file.
00001 #include "MusimatChapter9.h"
00002 MusimatChapter9Section(C091406b) {
00003 Print("*** Traversing the Cumulative Distribution Function ***");
00004 /*****************************************************************************
00005 
00006 Traversing the Cumulative Distribution Function
00007 
00008 To automate this, we start at the top end of the cumulative distribution function and work 
00009 down. As we go, we compare the value of R to the current step size. We’ve gone one step too far 
00010 when the value of R exceeds the step size, so we return the previous step as the answer, and stop.
00011 *****************************************************************************/
00012         para1(); // Step into this function to continue.)
00013 }
00014 
00015 Integer getIndex(RealList L, Real R){
00016         Integer i;
00017         For (i = Length(L) - 1; i >= 0; i = i - 1){
00018                 If (R > L[i]){
00019                         Return(i + 1);
00020                 }
00021         }
00022         Return( 0 );
00023 }
00024 
00025 Static Void para1() {
00026 Print("*** Invoking getIndex() ***");
00027 /*****************************************************************************
00028 We can invoke getIndex() as follows:
00029 *****************************************************************************/
00030 
00031 Real R = Random();
00032 Integer p = getIndex(f, R); // where f was defined previously
00033 Print(p);
00034 
00035 /*****************************************************************************
00036 If R is 0.1, then p prints 0. Now let’s bring all the pieces together. Here is a program that creates 
00037 a melody of 25 pitches favoring pitches that are at the low end of the chromatic scale:
00038 *****************************************************************************/
00039 Print("*** Melody of 25 low pitches ***");
00040 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);
00041 StringList n("C", "Cs", "D", "Ds", "E", "F", "Fs", "G", "Gs", "A", "As", "B", "c");
00042 f = normalize(f, sum(f));       // replace f with its normalized form
00043 f = accumulate(f);      // calculate cumulative distribution function
00044 StringList s;   // a place to put the result
00045 
00046 For (Integer i = 0; i < 25; i = i + 1){
00047         Integer p = getIndex(f, Random());
00048         s[i] = n[p];
00049 }
00050 
00051 Print("Melody: ", s);                                                   // print the melody
00052 
00053 /*****************************************************************************
00054 Running this program will generate something like figure 9.23, depending upon the values pro-
00055 duced by Random(). As we see, lower pitches are favored in approximately the proportions we 
00056 specified. The longer the sample melody, the more likely the pitch choices would conform on aver-
00057 age to the distribution function.
00058 *****************************************************************************/
00059 }}
00060 
00062 /* $Revision: 1.4 $ $Date: 2006/09/12 17:37:27 $ $Author: dgl $ $Name:  $ $Id: _c091406b_8cpp-source.html,v 1.4 2006/09/12 17:37:27 dgl Exp $ */
00063 // The Musimat Tutorial © 2006 Gareth Loy
00064 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy 
00065 // and published exclusively by The MIT Press.
00066 // This program is released WITHOUT ANY WARRANTY; without even the implied 
00067 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
00068 // For information on usage and redistribution, and for a DISCLAIMER OF ALL
00069 // WARRANTIES, see the file, "LICENSE.txt," in this distribution.
00070 // "Musimathics" is available here:     http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916
00071 // Gareth Loy's Musimathics website:    http://www.musimathics.com/
00072 // The Musimat website:                 http://www.musimat.com/
00073 // This program is released under the terms of the GNU General Public License
00074 // available here:                      http://www.gnu.org/licenses/gpl.txt
00075 

Generated on Tue Sep 12 10:14:15 2006 for Musimat Chapter 9 Code Examples by  doxygen 1.4.7