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

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

Go to the documentation of this file.
00001 /* $Revision: 1.5 $ $Date: 2006/09/08 18:56:52 $ $Author: dgl $ $Name:  $ $Id: Rhythm.h,v 1.5 2006/09/08 18:56:52 dgl Exp $ */
00002 #ifndef RHYTHM_H
00003 #define RHYTHM_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
00020 
00021 #ifdef _MSC_VER
00022 #define _CRT_SECURE_NO_WARNINGS
00023 #endif
00024 
00025 #include "Musimat.h"
00026 #include <iostream>
00027 
00029 class Rhythm
00030 {
00031         friend ostream& operator<<(ostream&, Rhythm&);
00032         friend istream& operator>>( istream& is, Rhythm& p );
00033 
00034         friend Bool operator==(Integer& i, Rhythm& p) { Return i == p.quotient(); }
00035         friend Bool operator==(Rhythm& p, Integer& i) { Return p.quotient() == i; }
00036         friend Bool operator!=(Integer& i, Rhythm& p) { Return i != p.quotient(); }
00037         friend Bool operator!=(Rhythm& p, Integer& i) { Return p.quotient() != i; }
00038         friend Bool operator>=(Rhythm& p, Integer& r) { Return p.quotient() >= r; }
00039         friend Bool operator>=(Integer& i, Rhythm& p) { Return i >= p.quotient(); }
00040         friend Bool operator>(Rhythm& p, Integer& i) { Return p.quotient() > i; }
00041         friend Bool operator>(Integer& i, Rhythm& p) { Return i > p.quotient(); }
00042         friend Bool operator<=(Rhythm& p, Integer& i) { Return p.quotient() <= i; }
00043         friend Bool operator<=(Integer& i, Rhythm& p) { Return i <= p.quotient(); }
00044 
00045         friend Bool operator==(Real& r, Rhythm& p) { Return r == p.quotient(); }
00046         friend Bool operator==(Rhythm& p, Real& r) { Return p.quotient() == r; }
00047         friend Bool operator!=(Real& r, Rhythm& p) { Return r != p.quotient(); }
00048         friend Bool operator!=(Rhythm& p, Real& r){ Return p.quotient() != r; }
00049         friend Bool operator>=(Rhythm& p, Real& r) { Return p.quotient() >= r; }
00050         friend Bool operator>=(Real& r, Rhythm& p) { Return r >= p.quotient(); }
00051         friend Bool operator>(Rhythm& p, Real& r) { Return p.quotient() > r; }
00052         friend Bool operator>(Real& r, Rhythm& p) { Return r > p.quotient(); }
00053         friend Bool operator<=(Rhythm& p, Real& r) { Return p.quotient() <= r; }
00054         friend Bool operator<=(Real& r, Rhythm& p) { Return r <= p.quotient(); }
00055 
00056 
00057 public:
00058 
00060         Rhythm() : m_num(0), m_den(0) { }
00061 
00063         Rhythm(Integer i) : m_num(i), m_den(i) { }
00064 
00067         Rhythm(Real x) : m_num(0), m_den(0) { RealToRational( x, m_num, m_den ); }
00068 
00072         Rhythm(Integer num, Integer den) : m_num(num), m_den(den) { 
00073                 If (m_den == 0)
00074                         m_den = 1;
00075         }
00076 
00081         Rhythm(Real x, Integer& num, Integer& den) : m_num(0), m_den(0) { 
00082                 RealToRational( x, num, den ); 
00083                 m_num = num;
00084                 m_den = den;
00085         }
00086 
00091         Rhythm(Rhythm x, Integer& num, Integer& den) : m_num(0), m_den(0) { 
00092                 RealToRational( x.quotient(), num, den ); 
00093                 m_num = num;
00094                 m_den = den;
00095         }
00096 
00098         Bool operator==(Const Rhythm& x) Const {
00099                 Return m_num == x.m_num  And m_den == x.m_den;
00100         }
00101 
00103         Bool operator!=(Const Rhythm& x) Const {
00104                 Return m_num != x.m_num || m_den != x.m_den;
00105         }
00106         
00108         Bool operator>=(Const Rhythm& x) Const {
00109                 Return quotient() >= x.quotient();
00110         }
00111 
00113         Bool operator>(Const Rhythm& x) Const {
00114                 Return quotient() > x.quotient();
00115         }
00116 
00118         Bool operator<(Const Rhythm& x) Const {
00119                 Return quotient() < x.quotient();
00120         }
00121 
00123         Bool operator<=(Const Rhythm& x) Const {
00124                 Return quotient() <= x.quotient();
00125         }
00126 
00128         Rhythm& operator+=(Const Real x) {
00129                 Return( *this = Rhythm(quotient() + x) );
00130         }
00131 
00133         Rhythm& operator+=(Rhythm x) {
00134                 Return( *this = Rhythm(quotient() + x.quotient()) );
00135         }
00136 
00138         Rhythm& operator-=(Const Real x) {
00139                 Return( *this = quotient() - x );
00140         }
00141 
00143         Rhythm& operator-=(Rhythm x) {
00144                 Return( *this = quotient() - x.quotient() );
00145         }
00146 
00148         Rhythm& operator*=(Const Real x) {
00149                 Return( *this = quotient() * x );
00150         }
00151 
00153         Rhythm& operator*=(Rhythm x) {
00154                 Return( *this = quotient() * x.quotient() );
00155         }
00156 
00158         Rhythm& operator/=(Const Real x) {
00159                 Return( *this = quotient() / x );
00160         }
00161 
00163         Rhythm& operator/=(Rhythm x) {
00164                 Return( *this = quotient() / x.quotient() );
00165         }
00166 
00168         Rhythm operator+(Rhythm& x) {
00169                 Return( Rhythm(quotient() + x.quotient()) );
00170         }
00171 
00173         Rhythm operator-(Rhythm& x) {
00174                 Return( Rhythm(quotient() - x.quotient()) );
00175         }
00176 
00178         Rhythm operator*(Rhythm& x) {
00179                 Return( Rhythm(quotient() * x.quotient()) );
00180         }
00181 
00183         Rhythm operator/(Rhythm& x) {
00184                 Return( Rhythm(quotient() / x.quotient()) );
00185         }
00186 
00188         Rhythm operator+(Const Real x) {
00189                 Return( Rhythm(quotient() + x) );
00190         }
00191 
00193         Rhythm operator-(Const Real x) {
00194                 Return( Rhythm(quotient() - x) );
00195         }
00196 
00198         Rhythm operator*(Const Real x) {
00199                 Return( Rhythm(quotient() * x) );
00200         }
00201 
00203         Rhythm operator/(Const Real x) {
00204                 Return( Rhythm(quotient() / x ) );
00205         }
00206 
00209         String print(String s = 0) Const {
00210                 If (s)
00211                         cout << s;
00212 #ifdef _MSC_VER
00213 // Disable Microsoft Visual Studio warning about sprintf()
00214 // until is universal agreement about how to handle its insecurities
00215 #pragma warning(push)
00216 #pragma warning(disable: 4996)
00217                 sprintf(s_buf, "{%d,%d}", m_num, m_den);
00218 #pragma warning(pop)
00219 #else
00220                 sprintf(s_buf, "{%d,%d}", m_num, m_den);
00221 #endif
00222                 Return s_buf;
00223         }
00224         
00226         Static Real tempo(Void) { 
00227                 Return s_tempo; 
00228         }
00229 
00232         Static Real tempo(Real t) {
00233                 Real s = s_tempo;
00234                 s_tempo = t;
00235                 Return s;
00236         }
00237 
00242         Real mm( Rhythm beats, Real perMinute ) Const { 
00243                 Return( 1.0 / (4.0 * beats.quotient()) * 60.0 / perMinute ); 
00244         }
00245 
00247         Real duration( ) { 
00248                 Return ( quotient() * s_fourQuarters );
00249         }
00250 
00252         Real absDuration( ) Const { 
00253                 Return ( Real(m_num) / Real(m_den) );
00254         }
00255 
00256 private:
00257         
00258         Real quotient() Const { Return( s_tempo * m_num / m_den ); }
00259         Static Real mmInit( Rhythm beats, Real perMinute ) { 
00260                 Real x = ( 1.0 / (4.0 * (Real(beats.m_num) / Real(beats.m_den))) * 60.0 / perMinute );
00261                 Return x;
00262         }
00263 
00264 private:
00265         Integer m_num;
00266         Integer m_den;
00267         Static Real s_tempo;
00268         Static Real s_fourQuarters;
00269         Static char s_buf[128];
00270 };
00271 
00278 Real metronome( Rhythm B, Real T );
00279 
00285 Real Duration( Real x ); 
00286 
00289 Real Duration( Rhythm x ); 
00290 
00293 Real AbsDuration( Real x ); 
00294 
00297 Real AbsDuration( Rhythm x ); 
00298 
00300 Void SetTempoScale( Real ts );
00301 
00306 
00307 extern Rhythm 
00308         Whole,          
00309         Half,           
00310         Quarter,        
00311         Eighth,         
00312         Sixteenth       
00313 ; 
00315 
00316 #endif // RHYTHM_H

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