C:/Musimathics_local/Musimat/include/Rhythm.h

Go to the documentation of this file.
00001 /* $Revision: 1.4 $ $Date: 2006/09/12 17:37:42 $ $Author: dgl $ $Name:  $ $Id: _rhythm_8h-source.html,v 1.4 2006/09/12 17:37:42 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 #include "Musimat.h"
00022 #include <iostream>
00023 using namespace std;
00024 
00026 class Rhythm
00027 {
00028         friend ostream& operator<<(ostream&, Rhythm&);
00029         friend istream& operator>>( istream& is, Rhythm& p );
00030 
00031         friend Bool operator==(Integer& i, Rhythm& p) { Return i == p.quotient(); }
00032         friend Bool operator==(Rhythm& p, Integer& i) { Return p.quotient() == i; }
00033         friend Bool operator!=(Integer& i, Rhythm& p) { Return i != p.quotient(); }
00034         friend Bool operator!=(Rhythm& p, Integer& i) { Return p.quotient() != i; }
00035         friend Bool operator>=(Rhythm& p, Integer& r) { Return p.quotient() >= r; }
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>(Integer& i, Rhythm& p) { Return i > p.quotient(); }
00039         friend Bool operator<=(Rhythm& p, Integer& i) { Return p.quotient() <= i; }
00040         friend Bool operator<=(Integer& i, Rhythm& p) { Return i <= p.quotient(); }
00041 
00042         friend Bool operator==(Real& r, Rhythm& p) { Return r == p.quotient(); }
00043         friend Bool operator==(Rhythm& p, Real& r) { Return p.quotient() == r; }
00044         friend Bool operator!=(Real& r, Rhythm& p) { Return r != p.quotient(); }
00045         friend Bool operator!=(Rhythm& p, Real& r){ Return p.quotient() != r; }
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>(Real& r, Rhythm& p) { Return r > p.quotient(); }
00050         friend Bool operator<=(Rhythm& p, Real& r) { Return p.quotient() <= r; }
00051         friend Bool operator<=(Real& r, Rhythm& p) { Return r <= p.quotient(); }
00052 
00053 
00054 public:
00055 
00057         Rhythm() : m_num(0), m_den(0) { }
00058 
00060         Rhythm(Integer i) : m_num(i), m_den(i) { }
00061 
00064         Rhythm(Real x) : m_num(0), m_den(0) { RealToRational( x, m_num, m_den ); }
00065 
00069         Rhythm(Integer num, Integer den) : m_num(num), m_den(den) { 
00070                 If (m_den == 0)
00071                         m_den = 1;
00072         }
00073 
00078         Rhythm(Real x, Integer& num, Integer& den) : m_num(0), m_den(0) { 
00079                 RealToRational( x, num, den ); 
00080                 m_num = num;
00081                 m_den = den;
00082         }
00083 
00088         Rhythm(Rhythm x, Integer& num, Integer& den) : m_num(0), m_den(0) { 
00089                 RealToRational( x.quotient(), num, den ); 
00090                 m_num = num;
00091                 m_den = den;
00092         }
00093 
00095         Bool operator==(Const Rhythm& x) Const {
00096                 Return m_num == x.m_num  And m_den == x.m_den;
00097         }
00098 
00100         Bool operator!=(Const Rhythm& x) Const {
00101                 Return m_num != x.m_num || m_den != x.m_den;
00102         }
00103         
00105         Bool operator>=(Const Rhythm& x) Const {
00106                 Return quotient() >= x.quotient();
00107         }
00108 
00110         Bool operator>(Const Rhythm& x) Const {
00111                 Return quotient() > x.quotient();
00112         }
00113 
00115         Bool operator<(Const Rhythm& x) Const {
00116                 Return quotient() < x.quotient();
00117         }
00118 
00120         Bool operator<=(Const Rhythm& x) Const {
00121                 Return quotient() <= x.quotient();
00122         }
00123 
00125         Rhythm& operator+=(Const Real x) {
00126                 Return( *this = Rhythm(quotient() + x) );
00127         }
00128 
00130         Rhythm& operator+=(Rhythm x) {
00131                 Return( *this = Rhythm(quotient() + x.quotient()) );
00132         }
00133 
00135         Rhythm& operator-=(Const Real x) {
00136                 Return( *this = quotient() - x );
00137         }
00138 
00140         Rhythm& operator-=(Rhythm x) {
00141                 Return( *this = quotient() - x.quotient() );
00142         }
00143 
00145         Rhythm& operator*=(Const Real x) {
00146                 Return( *this = quotient() * x );
00147         }
00148 
00150         Rhythm& operator*=(Rhythm x) {
00151                 Return( *this = quotient() * x.quotient() );
00152         }
00153 
00155         Rhythm& operator/=(Const Real x) {
00156                 Return( *this = quotient() / x );
00157         }
00158 
00160         Rhythm& operator/=(Rhythm x) {
00161                 Return( *this = quotient() / x.quotient() );
00162         }
00163 
00165         Rhythm operator+(Rhythm& x) {
00166                 Return( Rhythm(quotient() + x.quotient()) );
00167         }
00168 
00170         Rhythm operator-(Rhythm& x) {
00171                 Return( Rhythm(quotient() - x.quotient()) );
00172         }
00173 
00175         Rhythm operator*(Rhythm& x) {
00176                 Return( Rhythm(quotient() * x.quotient()) );
00177         }
00178 
00180         Rhythm operator/(Rhythm& x) {
00181                 Return( Rhythm(quotient() / x.quotient()) );
00182         }
00183 
00185         Rhythm operator+(Const Real x) {
00186                 Return( Rhythm(quotient() + x) );
00187         }
00188 
00190         Rhythm operator-(Const Real x) {
00191                 Return( Rhythm(quotient() - x) );
00192         }
00193 
00195         Rhythm operator*(Const Real x) {
00196                 Return( Rhythm(quotient() * x) );
00197         }
00198 
00200         Rhythm operator/(Const Real x) {
00201                 Return( Rhythm(quotient() / x ) );
00202         }
00203 
00206         String print(String s = 0) Const {
00207                 If (s)
00208                         cout << s;
00209                 sprintf(s_buf, "{%d,%d}", m_num, m_den);
00210                 Return s_buf;
00211         }
00212         
00214         Static Real tempo(Void) { 
00215                 Return s_tempo; 
00216         }
00217 
00220         Static Real tempo(Real t) {
00221                 Real s = s_tempo;
00222                 s_tempo = t;
00223                 Return s;
00224         }
00225 
00230         Real mm( Rhythm beats, Real perMinute ) Const { 
00231                 Return( 1.0 / (4.0 * beats.quotient()) * 60.0 / perMinute ); 
00232         }
00233 
00235         Real duration( ) { 
00236                 Return ( quotient() * s_fourQuarters );
00237         }
00238 
00240         Real absDuration( ) Const { 
00241                 Return ( Real(m_num) / Real(m_den) );
00242         }
00243 
00244 private:
00245         
00246         Real quotient() Const { Return( s_tempo * m_num / m_den ); }
00247         Static Real mmInit( Rhythm beats, Real perMinute ) { 
00248                 Real x = ( 1.0 / (4.0 * (Real(beats.m_num) / Real(beats.m_den))) * 60.0 / perMinute );
00249                 Return x;
00250         }
00251 
00252 private:
00253         Integer m_num;
00254         Integer m_den;
00255         Static Real s_tempo;
00256         Static Real s_fourQuarters;
00257         Static char s_buf[128];
00258 };
00259 
00266 Real metronome( Rhythm B, Real T );
00267 
00273 Real Duration( Real x ); 
00274 
00277 Real Duration( Rhythm x ); 
00278 
00281 Real AbsDuration( Real x ); 
00282 
00285 Real AbsDuration( Rhythm x ); 
00286 
00288 Void SetTempoScale( Real ts );
00289 
00294 
00295 extern Rhythm 
00296         Whole,          
00297         Half,           
00298         Quarter,        
00299         Eighth,         
00300         Sixteenth       
00301 ; 
00303 
00304 #endif // RHYTHM_H

Generated on Tue Sep 12 10:14:18 2006 for MusimatLib by  doxygen 1.4.7