• Main Page
  • Files
  • File List
  • File Members

/Users/garethloy/Musimathics/Musimat1.2/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         para2(); // Step into this function to continue.)
00014 }
00015 
00016 Integer getIndex(RealList L, Real R){
00017         Integer i;
00018         For (i = Length(L) - 1; i >= 0; i = i - 1) {
00019                 If (R > L[i]) {
00020                         Return(i + 1);
00021                 }
00022         }
00023         Return( 0 );
00024 }
00025 
00026 Static Void para1() {
00027         Print("*** Invoking getIndex() ***");
00028         /*****************************************************************************
00029          We can invoke getIndex() as follows:
00030          *****************************************************************************/
00031         extern RealList f; // f was defined in C091406.cpp line 24
00032         
00033         Real R = Random();
00034         Integer p = getIndex(f, R);  
00035         Print(p);
00036 }
00037 
00038 Static Void para2() {
00039         /*****************************************************************************
00040          If R is 0.1, then p prints 0. Now let's bring all the pieces together. Here is a program that creates 
00041          a melody of 25 pitches favoring pitches that are at the low end of the chromatic scale:
00042          *****************************************************************************/
00043         Print("*** Melody of 25 low pitches ***");
00044         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);
00045         StringList n("C", "Cs", "D", "Ds", "E", "F", "Fs", "G", "Gs", "A", "As", "B", "c");
00046         f = normalize(f, sum(f));       // replace f with its normalized form
00047         f = accumulate(f);      // calculate cumulative distribution function
00048         StringList s;   // a place to put the result
00049         
00050         For (Integer i = 0; i < 25; i = i + 1) {
00051                 Integer p = getIndex(f, Random());
00052                 s[i] = n[p];
00053         }
00054         
00055         Print("Melody: ", s);                                                   // print the melody
00056         
00057         /*****************************************************************************
00058          Running this program will generate something like figure 9.23, depending upon the values pro-
00059          duced by Random(). As we see, lower pitches are favored in approximately the proportions we 
00060          specified. The longer the sample melody, the more likely the pitch choices would conform on aver-
00061          age to the distribution function.
00062          *****************************************************************************/
00063 }
00064 
00066 /* $Revision: 1.3 $ $Date: 2006/09/05 08:02:46 $ $Author: dgl $ $Name:  $ $Id: C091406b.cpp,v 1.3 2006/09/05 08:02:46 dgl Exp $ */
00067 // The Musimat Tutorial © 2006 Gareth Loy
00068 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy 
00069 // and published exclusively by The MIT Press.
00070 // This program is released WITHOUT ANY WARRANTY; without even the implied 
00071 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
00072 // For information on usage and redistribution, and for a DISCLAIMER OF ALL
00073 // WARRANTIES, see the file, "LICENSE.txt," in this distribution.
00074 // "Musimathics" is available here:     http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916
00075 // Gareth Loy's Musimathics website:    http://www.musimathics.com/
00076 // The Musimat website:                 http://www.musimat.com/
00077 // This program is released under the terms of the GNU General Public License
00078 // available here:                      http://www.gnu.org/licenses/gpl.txt
00079 

Generated on Fri Nov 26 2010 16:18:24 for Musimat Chapter 9 Code Examples by  doxygen 1.7.2