Planeshift
scoreelements.h
Go to the documentation of this file.
1 /*
2  * scoreelements.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 SCORE_ELEMENTS_H
21 #define SCORE_ELEMENTS_H
22 
23 
24 //====================================================================================
25 // Crystal Space Includes
26 //====================================================================================
27 #include <cssysdef.h>
28 #include <csutil/hash.h>
29 
30 //====================================================================================
31 // Project Includes
32 //====================================================================================
33 
34 //====================================================================================
35 // Local Includes
36 //====================================================================================
37 #include "musicutil.h"
38 
39 //------------------------------------------------------------------------------------
40 // Forward Declarations
41 //------------------------------------------------------------------------------------
42 // A chord rarely have more than 6 notes
43 #define MEAS_ELEM_NOTES_CAPACITY_GROWTH 4
44 #define UNDEFINED_MEASURE_ATTRIBUTE -100
45 
46 class ScoreContext;
47 class MeasureElement;
48 
56 class Note
57 {
58 public:
65  {
66  public:
70  NoteContext();
71 
81 
85  void ResetContext();
86 
93  void UpdateContext(const MeasureElement &element);
94 
100  void UpdateContext(const Note &note);
101 
102  private:
107  csHash<csHash<psMusic::Accidental, char>, int> prevAccidentals;
108  };
109 
113  Note();
114 
126  Note(char name, int octave, psMusic::Accidental writtenAccidental);
127 
133  char GetName() const { return name; }
134 
141  int GetOctave() const { return octave; }
142 
153 
159  psMusic::Accidental GetWrittenAccidental() const { return writtenAccidental; }
160 
167  bool operator==(const Note &note) const;
168 
174  void SetName(char name);
175 
182  void SetOctave(int octave);
183 
189  void SetWrittenAccidental(psMusic::Accidental accidental);
190 
191 private:
192  char name;
193  int octave;
194 
200  psMusic::Accidental writtenAccidental;
201 };
202 
203 //--------------------------------------------------
204 
210 {
211 public:
218 
229  bool AddNote(char name, int octave, psMusic::Accidental writtenAccidental);
230 
236  psMusic::Duration GetDuration() { return duration; }
237 
243  size_t GetNNotes() const { return notes.GetSize(); }
244 
251  Note &GetNote(size_t n) { return notes[n]; }
252 
256  const Note &GetNote(size_t n) const { return notes[n]; }
257 
263  bool IsRest() const { return notes.GetSize() == 0; }
264 
271  bool RemoveNote(char name, int octave);
272 
278  void SetDuration(psMusic::Duration duration) { this->duration = duration; }
279 
283  void SetRest() { notes.Empty(); }
284 
285 private:
286  psMusic::Duration duration;
287 
288  typedef csArrayCapacityFixedGrow<MEAS_ELEM_NOTES_CAPACITY_GROWTH> NotesArrayCapacity;
289  csArray<Note, csArrayElementHandler<Note>, CS::Container::ArrayAllocDefault,
290  NotesArrayCapacity> notes;
291 };
292 
293 //--------------------------------------------------
294 
298 template<typename MeasureElementType>
299 class Measure
300 {
301 public:
302 
310  {
311  public:
316 
322  int GetBeats() const { return beats; }
323 
329  int GetBeatType() const { return beatType; }
330 
337  int GetFifths() const { return fifths; }
338 
344  int GetTempo() const { return tempo; }
345 
351  bool IsUndefined() const;
352 
358  void SetBeats(int beats) { this->beats = beats; }
359 
365  void SetBeatType(int beatType) { this->beatType = beatType; }
366 
373  void SetFifths(int fifths) { this->fifths = fifths; }
374 
380  void SetTempo(int tempo) { this->tempo = tempo; }
381 
388  void UpdateAttributes(const MeasureAttributes &attributes);
389 
390  private:
391  int tempo;
392  int beats;
393  int beatType;
394 
399  int fifths;
400  };
401 
405  Measure();
406 
410  ~Measure();
411 
417  void DeleteElement(size_t n) { elements.DeleteIndex(n); }
418 
422  void DeleteAllElements() { elements.Empty(); }
423 
435  void Fit(const MeasureAttributes* const attributes);
436 
442  MeasureAttributes GetAttributes() const;
443 
450  MeasureElementType &GetElement(size_t n) { return elements[n]; }
451 
455  const MeasureElementType &GetElement(size_t n) const { return elements[n]; }
456 
462  size_t GetNElements() const { return elements.GetSize(); }
463 
470  int GetNEndRepeat() const { return nEndRepeat; }
471 
479  void InsertElement(size_t n, const MeasureElementType &element);
480 
486  bool IsEmpty() const { return elements.GetSize() == 0; }
487 
493  bool IsEnding() const { return isEnding; }
494 
500  bool IsEndRepeat() const { return nEndRepeat > 0; }
501 
507  bool IsStartRepeat() const { return isStartRepeat; }
508 
515  size_t PushElement(const MeasureElementType &element)
516  {
517  return elements.Push(element);
518  }
519 
527  void SetBeat(int beats, int beatType);
528 
534  void SetEnding(bool isEnding);
535 
539  void SetFifths(int fifths);
540 
547  void SetNEndRepeat(int nEndRepeat);
548 
554  void SetStartRepeat(bool isStartRepeat);
555 
559  void SetTempo(int tempo);
560 
561 private:
562  bool isEnding;
563  bool isStartRepeat;
564  int nEndRepeat;
565  csArray<MeasureElementType> elements;
566 
571  MeasureAttributes* attributes;
572 
576  void CreateAttributes();
577 
581  void DeleteAttributes();
582 
586  void UpdateAttributes();
587 };
588 
589 //--------------------------------------------------
590 
597 {
598 public:
603 
608 
612  ScoreContext();
613 
621  int GetNPerformedRepeats(int measureID) const;
622 
630  int RestoreLastStartRepeat();
631 
640  void Update(int measureID, const Measure<MeasureElement> &measure);
641 
647  void Update(const MeasureElement &element);
648 
649 private:
650  int lastStartRepeatID;
651 
656  Measure<MeasureElement>::MeasureAttributes lastStartRepeatAttributes;
657 
662  csHash<int, int> repeatsDone;
663 };
664 
665 #include "scoreelements.hpp"
666 
669 #endif // SCORE_ELEMENTS_H
A measure containing measure elements.
int GetBeats() const
Get the numerator of the time signature.
bool IsEnding() const
Return true if this is an ending measure.
bool IsStartRepeat() const
Return true if this measure starts a repeat section.
void SetBeatType(int beatType)
Set the denominator of the time signature.
psMusic::Accidental GetPlayedAccidental(const ScoreContext &context) const
Get the accidental of the played note.
Measure< MeasureElement >::MeasureAttributes measureAttributes
Attributes specified in the score up to now.
Note()
Default constructor.
void SetBeats(int beats)
Set the numerator of the time signature.
bool IsRest() const
Return whether this element is a rest or not.
bool IsEmpty() const
Check if the measure has at least one element.
A single note in a musical score.
Definition: pawssheetline.h:61
int GetTempo() const
Get the tempo.
const Note & GetNote(size_t n) const
int GetBeatType() const
Get the denominator of the time signature.
void UpdateContext(const MeasureElement &element)
Update the list of previous accidental with all the notes in the given measure element.
void SetRest()
Set this element to be a rest.
Duration
The number associated to each duration is the number of quarter divisions as specified in DURATION_QU...
Definition: musicutil.h:240
psMusic::Accidental GetPreviousAccidental(const Note &note) const
Get an eventual accidental that previously appeared in the same measure.
Note & GetNote(size_t n)
Return the n-th note in the element.
bool operator==(const Note &note) const
Equal operator.
bool IsEndRepeat() const
Return true if this measure ends a repeat section.
void SetDuration(psMusic::Duration duration)
Set the duration of this element.
Note::NoteContext noteContext
Used to keep track of previous accidentals in the same measure.
MeasureElementType & GetElement(size_t n)
Get the element at position n.
int GetNEndRepeat() const
Get the number of time the repeat at the end of this measure must be performed.
int GetOctave() const
Get the note octave number.
size_t PushElement(const MeasureElementType &element)
Push a copy of the given element after all the elements currently in the measure. ...
size_t GetNNotes() const
Get the number of notes in this chord.
size_t GetNElements() const
Get the number of elements in this measure.
void DeleteAllElements()
Delete all elements from the measure.
This is used to keep track of everything needed to play a score and provide some utility functions fo...
void SetOctave(int octave)
Set the note octave number.
int GetFifths() const
Get the tonality as the number of accidentals in the key signature.
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.
psMusic::Accidental GetWrittenAccidental() const
Get the written accidental.
void SetTempo(int tempo)
Set the tempo.
void SetWrittenAccidental(psMusic::Accidental accidental)
Set the written accidental.
const MeasureElementType & GetElement(size_t n) const
Get the element at position n.
void SetFifths(int fifths)
Set the key signature as the number of accidentals.
Used to keep track of previous altered notes in the current measure.
Definition: scoreelements.h:64
psMusic::Duration GetDuration()
Get the duration of this element.
NoteContext()
Constructor.
void DeleteElement(size_t n)
Delete the element in position n.
An element of a measure with a given duration.
void SetName(char name)
Set the note name.
char GetName() const
Get the note name.
Keep general attributes that can change from a measure to another like key signature, beats and tempo.
void ResetContext()
Empty the list of previously altered note.