Data Structures - lazychords/lazychords-lib GitHub Wiki
In this page, we're presenting the data structures that are used throughout the project.
Music Structures
This structures are used to represent the musical elements we are manipulating. Note that they are stored with a computational point of view, hence we do not store any information regarding the aesthetic of the score (for example, all legato information are discarded).
Common interface
All the basic structures share a common interface that is designed to allow easier use.
Constructors/Destructor
All the classes implement the standard constructors, ie the empty constructor, the copy constructor, and the (c++11) move constructor. Sometimes, they will just be the default one generated by the compiler, but it will be always explicitly specified. The destructor is also defined, in the same way.
Saving facilities
All the structures shall implement a save and load function. This functions are used to save the structure in a stream, but not necessarily in a human-readable format.
Printing facilities
The stream operators are also usually overloaded. They allow pretty printing of the structures, so that you can print what's going on. We also provide a fromStream function, that allows to reconstruct the object from its printed version.
Debugging facilities
All the structures shall implement a check function, that performs some internal check on the structure to make sure that all its invariants are preserved. This function returns a bool, and is intended to be used in an assertion where appropriate, so that in release mode, this checking can be disabled. Please note that such a function may write debug info to stderr to tell what's going wrong.
For example, if you want to test the validity of a pitch object:
Pitch p;
assert(p.check());
Iterating facilities
When appropriate, the structures implement an id mechanism. The idea is to provide a uniform mechanism to iterate over all the possible elements. For instance, if the class represent a chord, it should be easy to iterate over all the possible chords that we can represent.
For example, if you want to print all the possible pitches, you can do:
for(unsigned i = 0;i < Pitch::maxId(); i++){
Pitch p = Pitch::fromId(i);
cout<<p<<endl;
}
Random generation
We finally provide a way to generate a random instance of each structure we provide, through the function randomInstance
Pitch
This class represents the height of a note. The octave is not taken into account, since for harmonic consideration it has usually a negligible importance (it is enforced by the tessitura of the instrument playing it). We also make the representation independent of the mode and the tonality, and we do not distinguish flats and sharps. Hence, a pitch can be represented by an integer that indicates the number of semi-tones between itself and the base note, which by convention is a C. For instance, a C will be denoted by 0, a C sharp (and a D flat) by 1, a D by 2, and so on until 11 which represents a B.
Arithmetic
We overload the common mathematical operators to allow more intuitive manipulation of the object. Adding an integer to a Pitch adds the corresponding number of semi-tones to it (modulo the size of the scale). Hence, adding 2 to a C gives a D, and adding 1 to a B gives a C.
Comparison
We also provide some equality operator to test if two pitches are equal.
Note Structure
It adds a temporal dimension to the pitch structure, ie a duration. This duration is measured as a fraction of a beat. A note can also be a rest. Overall, the interface of this structure is very similar to the Pitch structure, except that we can test whether this note is a rest or not.
Figure Structure
A musical figure is a set of notes that form a coherent progression. Intuitively these notes can be accompanied with the same chord. Internally, it is represented as a vector of notes, with some overloading of the function to make the name of the functions more intuitive (push_back -> addNote) In addition, each note has a weight that corresponds to its importance in the progression.