Planeshift

musicutil.h

Go to the documentation of this file.
00001 /*
00002  * musicutil.h, Author: Andrea Rizzi <[email protected]>
00003  *
00004  * Copyright (C) 2001-2013 Atomic Blue ([email protected], http://www.atomicblue.org) 
00005  *
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation (version 2 of the License)
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017  *
00018  */
00019 
00020 #ifndef MUSIC_UTIL_H
00021 #define MUSIC_UTIL_H
00022 
00023 
00024 //====================================================================================
00025 // Crystal Space Includes
00026 //====================================================================================
00027 #include <cssysdef.h>
00028 #include <csutil/refarr.h>
00029 #include <csutil/csstring.h>
00030 #include <iutil/document.h>
00031 
00043 struct ScoreStatistics
00044 {
00050     ScoreStatistics()
00051     {
00052         Reset();
00053     }
00054 
00058     ScoreStatistics(const ScoreStatistics &copy)
00059     {
00060         nNotes = copy.nNotes;
00061         nChords = copy.nChords;
00062         nBb = copy.nBb;
00063         nGb = copy.nGb;
00064         nEb = copy.nEb;
00065         nDb = copy.nDb;
00066         nAb = copy.nAb;
00067 
00068         totalLength = copy.totalLength;
00069         totalLengthNoRests = copy.totalLengthNoRests;
00070 
00071         minimumDuration = copy.minimumDuration;
00072         maximumPolyphony = copy.maximumPolyphony;
00073         averageDuration =copy.averageDuration;
00074         averagePolyphony = copy.averagePolyphony;
00075 
00076         tempo = copy.tempo;
00077         fifths = copy.fifths;
00078         beatType = copy.beatType;
00079     }
00080 
00087     ScoreStatistics &operator+=(const ScoreStatistics &addend)
00088     {
00089         nNotes += addend.nNotes;
00090         nChords += addend.nChords;
00091         nBb += addend.nBb;
00092         nGb += addend.nGb;
00093         nEb += addend.nEb;
00094         nDb += addend.nDb;
00095         nAb += addend.nAb;
00096 
00097         totalLength += addend.totalLength;
00098         totalLengthNoRests += addend.totalLengthNoRests;
00099 
00100         return *this;
00101     }
00102 
00109     ScoreStatistics &operator-=(const ScoreStatistics &subtrahend)
00110     {
00111         nNotes -= subtrahend.nNotes;
00112         nChords -= subtrahend.nChords;
00113         nBb -= subtrahend.nBb;
00114         nGb -= subtrahend.nGb;
00115         nEb -= subtrahend.nEb;
00116         nDb -= subtrahend.nDb;
00117         nAb -= subtrahend.nAb;
00118 
00119         totalLength -= subtrahend.totalLength;
00120         totalLengthNoRests -= subtrahend.totalLengthNoRests;
00121 
00122         return *this;
00123     }
00124 
00132     ScoreStatistics &operator*=(const int scalar)
00133     {
00134         nNotes *= scalar;
00135         nChords *= scalar;
00136         nBb *= scalar;
00137         nGb *= scalar;
00138         nEb *= scalar;
00139         nDb *= scalar;
00140         nAb *= scalar;
00141 
00142         totalLength *= scalar;
00143         totalLengthNoRests *= scalar;
00144 
00145         return *this;
00146     }
00147 
00156     const ScoreStatistics operator*(const int scalar)
00157     {
00158         ScoreStatistics result(*this);
00159         result *= scalar;
00160         return result;
00161     }
00162 
00166     void Reset()
00167     {
00168         nNotes = 0;
00169         nChords = 0;
00170         nBb = 0;
00171         nGb = 0;
00172         nEb = 0;
00173         nDb = 0;
00174         nAb = 0;
00175 
00176         totalLength = 0.0;
00177         totalLengthNoRests = 0.0;
00178 
00179         minimumDuration = 0;
00180         maximumPolyphony = 0;
00181         averageDuration = 0.0;
00182         averagePolyphony = 0.0;
00183 
00184         tempo = 0;
00185         fifths = 0;
00186         beatType = 0;
00187     }
00188 
00189     int nNotes;                 
00190     int nChords;                
00191     int nBb;                    
00192     int nGb;                    
00193     int nEb;                    
00194     int nDb;                    
00195     int nAb;                    
00196 
00197     float totalLength;          
00198     float totalLengthNoRests;   
00199 
00200     int minimumDuration;        
00201     int maximumPolyphony;       
00202     float averageDuration;      
00203     float averagePolyphony;     
00204 
00205     int tempo;                  
00206     int fifths;                 
00207     int beatType;               
00208 };
00209 
00210 
00215 namespace psMusic
00216 {
00217 
00218 enum Accidental
00219 {
00220     NO_ACCIDENTAL,
00221     DOUBLE_FLAT,
00222     FLAT,
00223     NATURAL,
00224     SHARP,
00225     DOUBLE_SHARP
00226 };
00227 
00228 
00229 // Unit of measure is a sixteenth. If you change this constant you must change the values
00230 // in the enum Duration and the method CheckDuration() too.
00234 #define DURATION_QUARTER_DIVISIONS 16
00235 
00240 enum Duration
00241 {
00242     SIXTEENTH_DURATION      = 1,
00243     EIGHTH_DURATION         = 2,
00244     DOTTED_EIGHTH_DURATION  = 3,
00245     QUARTER_DURATION        = 4,
00246     DOTTED_QUARTER_DURATION = 6,
00247     HALF_DURATION           = 8,
00248     DOTTED_HALF_DURATION    = 12,
00249     WHOLE_DURATION          = 16,
00250     DOTTED_WHOLE_DURATION   = 24
00251 };
00252 
00253 
00262 bool CheckDuration(int duration);
00263 
00271 Duration GetBiggestDuration(int duration);
00272 
00279 void NextPitch(char &pitch, uint &octave);
00280 
00287 void PreviousPitch(char &pitch, uint &octave);
00288 
00294 void EnharmonicPitch(char &pitch, int &accidental);
00295 
00303 bool GetMeasures(iDocument* score, csRefArray<iDocumentNode> &measures);
00304 
00312 bool GetStatistics(iDocument* musicalScore, ScoreStatistics &stats);
00313 
00330 bool GetAttributes(iDocument* musicalScore, int &quarterDivisions,
00331                    int &fifths, int &beats, int &beatType, int &tempo);
00332 
00343 bool ZCompressSong(const csString &inputScore, csString &outputScore);
00344 
00352 bool ZDecompressSong(const csString &inputScore, csString &outputScore);
00353 
00361 bool CheckValidity(iDocument* musicalScore, csRef<iDocumentNode> &partNode);
00362 
00363 }
00364 
00367 #endif // MUSIC_UTIL_H
00368