00001 #include "MusimatChapter9.h" 00002 MusimatChapter9Section(C091406a) { 00003 Print("*** Accumulation ***"); 00004 /***************************************************************************** 00005 Accumulation 00006 00007 If we index the y-axis of figure 9.22 with a random value in the unit interval, the corresponding 00008 x-axis value will be one of the 12 pitches of the scale. Furthermore, the choice will more likely 00009 fall on a step that occupies a wider footprint on the y-axis, corresponding in this case to the 00010 lower pitches of the scale, just as we wanted. We can create the cumulative distribution function 00011 in figure 9.22 as follows: 00012 *****************************************************************************/ 00013 para1(); // Step into this function to continue. 00014 } 00015 00016 RealList Reference accumulate(RealList Reference L){ 00017 For(Integer i = 1; i < Length(L); i = i + 1) { 00018 L[i] = L[i] + L[i - 1]; 00019 } 00020 Return(L); 00021 } 00022 00023 Static Void para1() { 00024 /***************************************************************************** 00025 Starting with the second element in the list (indexed as 1), we replace this element with its original 00026 value plus the value of the previous element. As we proceed through the list, each list element will 00027 be equal to itself plus all previous elements. Given the preparation of the RealList r performed 00028 above, Print(accumulate(r)); prints {0.15, 0.29, 0.42, 0.54, 0.64, 0.73, 0.81, 00029 0.87, 0.92, 0.96, 0.99, 1.0}. 00030 00031 We have prepared the cumulative distribution function, and now we can access it with a random 00032 value to select a pitch. Pick a number in the unit interval to be the next note of the melody: 00033 *****************************************************************************/ 00034 00035 Real R = Random(); 00036 Print(R); 00037 00038 /***************************************************************************** 00039 R will fall within the range of one of the 12 steps in figure 9.22 because both Random() and the 00040 cumulative distribution function exactly span the unit interval, 0 to 1. For example, if R equals 0.1, 00041 then by inspection of figure 9.22, we can see that R lies within the first step, which covers the inter- 00042 val [0, 0.15], so the pitch that this value of R selects is C. 00043 *****************************************************************************/ 00044 } 00045 00047 /* $Revision: 1.3 $ $Date: 2006/09/05 08:02:46 $ $Author: dgl $ $Name: $ $Id: C091406a.cpp,v 1.3 2006/09/05 08:02:46 dgl Exp $ */ 00048 // The Musimat Tutorial � 2006 Gareth Loy 00049 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" � 2006 Gareth Loy 00050 // and published exclusively by The MIT Press. 00051 // This program is released WITHOUT ANY WARRANTY; without even the implied 00052 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00053 // For information on usage and redistribution, and for a DISCLAIMER OF ALL 00054 // WARRANTIES, see the file, "LICENSE.txt," in this distribution. 00055 // "Musimathics" is available here: http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916 00056 // Gareth Loy's Musimathics website: http://www.musimathics.com/ 00057 // The Musimat website: http://www.musimat.com/ 00058 // This program is released under the terms of the GNU General Public License 00059 // available here: http://www.gnu.org/licenses/gpl.txt 00060