• Main Page
  • Modules
  • Classes
  • Files
  • File List
  • File Members

/Users/garethloy/Musimathics/Musimat1.2/include/Row.h

Go to the documentation of this file.
00001 /* $Revision: 1.4 $ $Date: 2006/09/08 18:56:52 $ $Author: dgl $ $Name:  $ $Id: Row.h,v 1.4 2006/09/08 18:56:52 dgl Exp $ */
00002 #ifndef ROW_H
00003 #define ROW_H
00004 
00005 // The Musimat Tutorial © 2006 Gareth Loy
00006 // Derived from Chapter 9 and Appendix B of "Musimathics Vol. 1" © 2006 Gareth Loy 
00007 // and published exclusively by The MIT Press.
00008 // This program is released WITHOUT ANY WARRANTY; without even the implied 
00009 // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
00010 // For information on usage and redistribution, and for a DISCLAIMER OF ALL
00011 // WARRANTIES, see the file, "LICENSE.txt," in this distribution.
00012 // "Musimathics" is available here:     http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=10916
00013 // Gareth Loy's Musimathics website:    http://www.musimathics.com/
00014 // The Musimat website:                 http://www.musimat.com/
00015 // This program is released under the terms of the GNU General Public License
00016 // available here:                      http://www.gnu.org/licenses/gpl.txt
00017 
00018 #include "Musimat.h"
00019 
00022 
00027 
00031 template<class Type> Type Reference Rotate( Type Reference f, Integer n )
00032 {
00033         f.rotate(n);
00034         Return( f );
00035 }
00036 
00042 template<class Type1, class Type2> Type1 Transpose( Type1 L, Type2 t, Type2 lim ) {
00043         L.transpose(t, lim);
00044         Return( L );
00045 }
00046 
00047 
00054 inline PitchList Transpose( PitchList L, Integer t ) {
00055         For ( Integer i = 0; i < L.length(); i = i + 1 ) {
00056                 L[i].transModuloOctave(t);
00057         }
00058         Return( L );
00059 }
00060 
00065 template<class Type1, class Type2> Type1 Invert( Type1 L, Type2 lim ) {
00066         L.invert(lim);
00067         Return( L );
00068 }
00069 
00074 inline PitchList Invert( PitchList L, Integer lim = 12 ) {
00075         For ( Integer i = 0; i < L.length(); i = i + 1 ) {
00076                 L[i].invert();
00077         }
00078         Return( L );
00079 }
00080 
00084 template<class Type> Type Reference Retrograde( Type Reference L ) {
00085         L.retrograde();
00086         Return( L );
00087 }
00088 
00092 template<class MatrixType, class ListType> MatrixType SetComplex( ListType prime ) {
00093         Integer len = Length( prime );
00094         MatrixType M( len, len );
00095         ListType inverted = Invert( prime, 12 );
00096         Integer i;
00097 
00098         For ( i = 0; i < len; i = i + 1 ) {
00099                  M[0][i] = prime[i];
00100                  M[i][0] = inverted[i];
00101         }
00102 
00103         For ( i = 1; i < len; i = i + 1 ) {
00104                 For ( Integer j = 1; j < len; j = j + 1 ) {
00105                           M[ i ][ j ] = Mod( M[ i ][ 0 ] + M[ 0 ][ j ], len);
00106                 }
00107         }
00108 
00109         Return( M );
00110 }
00111 
00119 template<class ElementType, class ListType> ElementType cycle( ListType L, Integer Reference pos, Integer inc ) {
00120         Integer i = PosMod( pos, Length( L ) );
00121         pos = PosMod( pos + inc, Length( L ) );
00122         Return( L[ i ] );
00123 }
00124 
00133 template<class ElementType, class ListType> ElementType palindrome( ListType L, Integer Reference pos, Integer Reference inc ) {
00134         Integer curPos = pos;
00135         ElementType x = cycle<ElementType,ListType>( L, pos, inc );
00136         If ( curPos + inc != pos ) {
00137                 inc = inc * (-1); // change direction
00138                 pos = curPos;
00139         }
00140         Return( x ); 
00141 }
00142 
00150 template<class ElementType, class ListType> ElementType permute( ListType L, Integer Reference pos, Integer Reference count, Integer inc ) { 
00151         Integer curPos = pos;                   // save the current position
00152         ElementType x = cycle<ElementType,ListType>( L, pos, 1 );       // update pos and get list value
00153         count = count + 1;                              // increment counter
00154         If ( count == Length( L ) ) {   // have we output L items from list?
00155                 count = 0;                                      // reset count
00156                 pos = curPos + inc;                     // permute position for next time
00157         }
00158         Return( x ); 
00159 }
00160 
00169 Integer InterpTendency( Real f, IntegerList L1, Integer Reference pos1, IntegerList L2, Integer Reference pos2, Integer inc );
00170 
00178 Pitch LinearInterpolate( Pitch x, Pitch xMin, Pitch xMax, Pitch yMin, Pitch yMax );
00179 
00187 template<class ElementType, class ListType> ListType stretch( ListType L, ElementType xMin, ElementType xMax, ElementType yMin, ElementType yMax ) {
00188         For ( Integer i = 0; i < Length( L ); i = i + 1 ) {
00189                  L[ i ] = ElementType( linearInterpolate( L[ i ], xMin, xMax, yMin, yMax ) );
00190         }
00191         Return( L );
00192 }
00193 
00194 
00198 template<class ListType> ListType RandomRow( Integer N ) {
00199         IntegerList L;                          // Keep track of the pitches chosen so far
00200         ListType M;                             // Used to build up our random 12-tone row 
00201         Integer i;
00202         // First, set all list elements to zero, which means "unused"
00203         For ( i = 0; i < N; i = i + 1 ) {
00204                  L[ i ] = 0;    
00205         }
00206         // Now build up M, marking off elements in L when they are chosen
00207         For ( i = 0; i < N; i = i ) {
00208                 Integer x = IRandom( 0, N - 1 );
00209                 If ( L[ x ] == 0 ) {    // Hasn't been chosen yet?
00210                          L[ x ] = 1;    // Mark it "used"
00211                          M[ i ] = x;    // Save the result
00212                         i = i + 1;      // Now increment the control variable
00213                 }
00214         }
00215         Return( M );
00216 }
00217 
00223 template<class ElementType, class ListType> Void swap( ListType Reference L, Integer from, Integer to ) {
00224         ElementType x = L[ to ];                // save the target value
00225          L[ to ] = L[ from ];           // swap from -> to
00226          L[ from ] = x;                 // swap to -> from
00227 }
00228 
00232 template<class ElementType, class ListType> ListType shuffle( ListType L ) {
00233         // SeedRandom( 788429 );        // Uncomment to test alternative shuffle
00234         ListType M = RandomRow<ListType>( Length( L ) ); // Determine what elements to swap
00235         Print( "M=", M );
00236         For ( Integer i = 0; i < Length( L ); i = i + 1 ) {
00237                 Integer j = M[ i ];
00238                 swap<ElementType,ListType>( L, i, j );
00239         }
00240         Return( L );
00241 }
00242 
00250 template<class ElementType, class ListType> ElementType randTendency( ListType L1, Integer Reference pos1, ListType L2, Integer Reference pos2, Integer inc ) {
00251         Integer x = cycle<ElementType,ListType>( L1, pos1, inc );
00252         Integer y = cycle<ElementType,ListType>( L2, pos2, inc );
00253         If ( x < y )
00254                 Return( IRandom( x, y ) );
00255         Else
00256                 Return( IRandom( y, x ) );              
00257 }
00258 
00259 
00261 #endif // ROW_H

Generated on Fri Nov 26 2010 16:18:25 for MusimatLib by  doxygen 1.7.2