Planeshift
musicutil.h
Go to the documentation of this file.
1 /*
2  * musicutil.h, Author: Andrea Rizzi <88whacko@gmail.com>
3  *
4  * Copyright (C) 2001-2013 Atomic Blue (info@planeshift.it, http://www.atomicblue.org)
5  *
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation (version 2 of the License)
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */
19 
20 #ifndef MUSIC_UTIL_H
21 #define MUSIC_UTIL_H
22 
23 
24 //====================================================================================
25 // Crystal Space Includes
26 //====================================================================================
27 #include <cssysdef.h>
28 #include <csutil/refarr.h>
29 #include <csutil/csstring.h>
30 #include <iutil/document.h>
31 
44 {
51  {
52  Reset();
53  }
54 
59  {
60  nNotes = copy.nNotes;
61  nChords = copy.nChords;
62  nBb = copy.nBb;
63  nGb = copy.nGb;
64  nEb = copy.nEb;
65  nDb = copy.nDb;
66  nAb = copy.nAb;
67 
68  totalLength = copy.totalLength;
70 
75 
76  tempo = copy.tempo;
77  fifths = copy.fifths;
78  beatType = copy.beatType;
79  }
80 
88  {
89  nNotes += addend.nNotes;
90  nChords += addend.nChords;
91  nBb += addend.nBb;
92  nGb += addend.nGb;
93  nEb += addend.nEb;
94  nDb += addend.nDb;
95  nAb += addend.nAb;
96 
97  totalLength += addend.totalLength;
99 
100  return *this;
101  }
102 
110  {
111  nNotes -= subtrahend.nNotes;
112  nChords -= subtrahend.nChords;
113  nBb -= subtrahend.nBb;
114  nGb -= subtrahend.nGb;
115  nEb -= subtrahend.nEb;
116  nDb -= subtrahend.nDb;
117  nAb -= subtrahend.nAb;
118 
119  totalLength -= subtrahend.totalLength;
121 
122  return *this;
123  }
124 
132  ScoreStatistics &operator*=(const int scalar)
133  {
134  nNotes *= scalar;
135  nChords *= scalar;
136  nBb *= scalar;
137  nGb *= scalar;
138  nEb *= scalar;
139  nDb *= scalar;
140  nAb *= scalar;
141 
142  totalLength *= scalar;
143  totalLengthNoRests *= scalar;
144 
145  return *this;
146  }
147 
156  const ScoreStatistics operator*(const int scalar)
157  {
158  ScoreStatistics result(*this);
159  result *= scalar;
160  return result;
161  }
162 
166  void Reset()
167  {
168  nNotes = 0;
169  nChords = 0;
170  nBb = 0;
171  nGb = 0;
172  nEb = 0;
173  nDb = 0;
174  nAb = 0;
175 
176  totalLength = 0.0;
177  totalLengthNoRests = 0.0;
178 
179  minimumDuration = 0;
180  maximumPolyphony = 0;
181  averageDuration = 0.0;
182  averagePolyphony = 0.0;
183 
184  tempo = 0;
185  fifths = 0;
186  beatType = 0;
187  }
188 
189  int nNotes;
190  int nChords;
191  int nBb;
192  int nGb;
193  int nEb;
194  int nDb;
195  int nAb;
196 
197  float totalLength;
199 
204 
205  int tempo;
206  int fifths;
207  int beatType;
208 };
209 
210 
215 namespace psMusic
216 {
217 
219 {
225  DOUBLE_SHARP
226 };
227 
228 
229 // Unit of measure is a sixteenth. If you change this constant you must change the values
230 // in the enum Duration and the method CheckDuration() too.
234 #define DURATION_QUARTER_DIVISIONS 16
235 
241 {
251 };
252 
253 
262 bool CheckDuration(int duration);
263 
271 Duration GetBiggestDuration(int duration);
272 
279 void NextPitch(char &pitch, uint &octave);
280 
287 void PreviousPitch(char &pitch, uint &octave);
288 
294 void EnharmonicPitch(char &pitch, int &accidental);
295 
303 bool GetMeasures(iDocument* score, csRefArray<iDocumentNode> &measures);
304 
312 bool GetStatistics(iDocument* musicalScore, ScoreStatistics &stats);
313 
330 bool GetAttributes(iDocument* musicalScore, int &quarterDivisions,
331  int &fifths, int &beats, int &beatType, int &tempo);
332 
343 bool ZCompressSong(const csString &inputScore, csString &outputScore);
344 
352 bool ZDecompressSong(const csString &inputScore, csString &outputScore);
353 
361 bool CheckValidity(iDocument* musicalScore, csRef<iDocumentNode> &partNode);
362 
363 }
364 
367 #endif // MUSIC_UTIL_H
368 
bool CheckDuration(int duration)
Check wheather the given number of DURATION_QUARTER_DIVISIONS correspond to a specific duration taken...
int nBb
total number of played Bb.
Definition: musicutil.h:191
int fifths
tonality as number of sharps (if > 0) or flats (if < 0).
Definition: musicutil.h:206
bool GetMeasures(iDocument *score, csRefArray< iDocumentNode > &measures)
Gets the XML nodes representing the measures contained in the musical score.
bool GetStatistics(iDocument *musicalScore, ScoreStatistics &stats)
Returns the statistics of the score.
bool CheckValidity(iDocument *musicalScore, csRef< iDocumentNode > &partNode)
Checks if the given document is a valid musical score and provide the <part> node.
int nNotes
total number of played notes in the score.
Definition: musicutil.h:189
ScoreStatistics & operator*=(const int scalar)
Multiplies all statistics concerning number of notes/chords and length for a integer scalar...
Definition: musicutil.h:132
float averageDuration
average duration of the score&#39;s notes in ms.
Definition: musicutil.h:202
bool ZDecompressSong(const csString &inputScore, csString &outputScore)
Decompress a song with the zlib compression algorithm.
Duration GetBiggestDuration(int duration)
Returns the biggest duration that can be represented on the score which is less or equal to the given...
float totalLengthNoRests
total duration of the score in ms excluding rests.
Definition: musicutil.h:198
Duration
The number associated to each duration is the number of quarter divisions as specified in DURATION_QU...
Definition: musicutil.h:240
float averagePolyphony
average number of notes played at the same time.
Definition: musicutil.h:203
int nEb
total number of played Eb.
Definition: musicutil.h:193
const ScoreStatistics operator*(const int scalar)
Multiplies all statistics concerning number of notes/chords and length for a integer scalar...
Definition: musicutil.h:156
This namespace contains a set of functions that are usefull for the processing of music and musical s...
Definition: musicutil.h:215
int nAb
total number of played Ab.
Definition: musicutil.h:195
bool ZCompressSong(const csString &inputScore, csString &outputScore)
Compress a song with the zlib compression algorithm.
int nGb
total number of played Gb.
Definition: musicutil.h:192
int minimumDuration
duration of the shortest note in the score in ms.
Definition: musicutil.h:200
ScoreStatistics & operator-=(const ScoreStatistics &subtrahend)
Subtracts all statistics concerning number of notes/chords and length.
Definition: musicutil.h:109
void EnharmonicPitch(char &pitch, int &accidental)
Turns the given pitch into the enharmonic equivalent.
bool GetAttributes(iDocument *musicalScore, int &quarterDivisions, int &fifths, int &beats, int &beatType, int &tempo)
Gets the attributes in the first measure of the given score.
void Reset()
Set all statistics to 0.
Definition: musicutil.h:166
float totalLength
total duration of the score in milliseconds.
Definition: musicutil.h:197
int nDb
total number of played Db.
Definition: musicutil.h:194
ScoreStatistics & operator+=(const ScoreStatistics &addend)
Adds all statistics concerning number of notes/chords and length.
Definition: musicutil.h:87
This struct keeps general information about a score.
Definition: musicutil.h:43
ScoreStatistics(const ScoreStatistics &copy)
Copy constructor.
Definition: musicutil.h:58
int maximumPolyphony
maximum number of notes played at the same time.
Definition: musicutil.h:201
void PreviousPitch(char &pitch, uint &octave)
Turns the given pitch into the previous one in the scale.
int tempo
tempo of the score.
Definition: musicutil.h:205
int nChords
total number of played chords in the score.
Definition: musicutil.h:190
ScoreStatistics()
Constructor.
Definition: musicutil.h:50
int beatType
beat type of the score.
Definition: musicutil.h:207
void NextPitch(char &pitch, uint &octave)
Turns the given pitch into the next one in the scale.