Generate an accompaniement for a given melody
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Music.hpp
Go to the documentation of this file.
1 
10 #ifndef MUSIC_HPP_INCLUDED
11 #define MUSIC_HPP_INCLUDED
12 
13 #define BOOST
14 
15 #include <map>
16 #include <vector>
17 #include <climits>
18 #include <iostream>
19 #include <stdexcept>
20 #include <cassert>
21 #include <functional>
22 #ifdef BOOST
23 #pragma GCC diagnostic push
24 #pragma GCC diagnostic ignored "-Wall"
25 #pragma GCC diagnostic ignored "-Wextra"
26 #pragma GCC diagnostic ignored "-Wconversion"
27 #pragma GCC diagnostic ignored "-Wunused-parameter"
28 #pragma GCC diagnostic ignored "-Wsign-conversion"
29 #pragma GCC diagnostic ignored "-Wsuggest-attribute=const"
30 #pragma GCC diagnostic ignored "-Wsuggest-attribute=pure"
31 #pragma GCC diagnostic ignored "-Wstrict-overflow"
32 #pragma GCC diagnostic ignored "-Weffc++"
33 #include <boost/rational.hpp>
34 #pragma GCC diagnostic pop
35 #endif
36 
43 enum class NoteName{C,D,E,F,G,A,B};
44 
52 std::ostream& operator <<(std::ostream& o, NoteName n);
53 
62 std::istream& operator >>(std::istream& i, NoteName& n);
63 
70 enum class Accidental
71 {
72  Flat,
73  Sharp,
74  None
75 };
76 
84 std::ostream& operator <<(std::ostream& o, Accidental a);
85 
93 std::istream& operator >>(std::istream& i, Accidental& a);
94 
95 using Octave = unsigned;
96 #ifdef BOOST
97 using Fraction = boost::rational<int>;
99 #else
100 #warning "Not using boost is a problem"
101 using Fraction = float;
102 #endif
103 
107 {
110 
123  bool operator==(const CompleteNoteName& o) const __attribute__((pure));
128  bool operator!=(const CompleteNoteName& o) const __attribute__((pure));
129 };
130 
138 std::ostream& operator <<(std::ostream& o, const CompleteNoteName& n);
139 
147 std::istream& operator >>(std::istream& i, CompleteNoteName& n);
148 
149 #pragma GCC diagnostic push
150 #pragma GCC diagnostic ignored "-Weffc++"
151 
153 struct Pitch : public CompleteNoteName
154 {
156 
160  Pitch(const CompleteNoteName& n, Octave o = 4);
166  Pitch(NoteName n = NoteName::C, Accidental mod=Accidental::None, Octave o = 4);
174  Pitch& operator+=(int nb);
182  Pitch operator+(int nb) const __attribute__((pure));
190  Pitch& operator-=(int nb);
198  Pitch operator-(int nb) const __attribute__((pure));
203  bool operator==(const Pitch& o) const __attribute__((pure));
208  bool operator!=(const Pitch& o) const __attribute__((pure));
212  CompleteNoteName getNoteName() const __attribute__((pure));
213 };
214 
228 std::ostream& operator <<(std::ostream& o, const Pitch& n);
229 
244 std::istream& operator >>(std::istream& i, Pitch& n);
245 
249 struct Note : public Pitch
250 {
252 
257  Note(const Pitch& n = Pitch(), const Fraction& duration = 1);
263  Note(NoteName n, Accidental mod=Accidental::None, Octave o = 4, const Fraction& duration = Fraction(1));
268  bool operator==(const Note& o) const __attribute__((pure));
273  bool operator!=(const Note& o) const __attribute__((pure));
278  static unsigned noteToHalfTone(const CompleteNoteName& n) __attribute__((const));
279 };
280 
281 #pragma GCC diagnostic pop
282 
291 std::ostream& operator <<(std::ostream& o, const Note& n);
292 
304 std::istream& operator >>(std::istream& i, Note& n);
305 
312 class Measure
313 {
314 public :
315  //Constructor must verify Measure invariants
316 
318  using ConstIterator = std::map<Position, Note>::const_iterator;
319 
320  //Constructor
321 
329  Measure(bool binary = true, unsigned duration = 4);
330 
335  ConstIterator begin() const __attribute__((pure));
340  ConstIterator end() const __attribute__((pure));
345  unsigned getDuration() const __attribute__((pure));
346 
353  void changeDuration(unsigned d);
358  bool isBinary() const __attribute__((pure));
359 
365  void setBinary(bool b);
366 
367 
368 
379  ConstIterator getNoteBeingPlayedAt(const Position& p) const __attribute__((pure));
380 
390  void addNote(const Position& p, const Note& n);
391 
396  bool operator==(const Measure& o) const __attribute__((pure));
397 
402  bool operator!=(const Measure& o) const __attribute__((pure));
403 private :
404 
405  std::map<Position, Note> m_starts;
406  unsigned m_duration;
407  bool m_binary;
408 
409 
410 };
411 
420 std::ostream& operator <<(std::ostream& o, const Measure& m);
421 
430 std::istream& operator >>(std::istream& i, Measure& m);
431 
438 struct Key
439 {
442 
447  Key(const CompleteNoteName& base=CompleteNoteName(), const NoteName& mode = NoteName::C);
448 
454  std::vector<CompleteNoteName> getNotes() const;
455 
461  void normalize(CompleteNoteName& c) const;
466  bool operator==(const Key& k) const __attribute__((pure));
467 
472  bool operator!=(const Key& k) const __attribute__((pure));
473  //What about == and != operators ?
474 };
475 
484 std::ostream& operator <<(std::ostream& o, const Key& k);
485 
494 Key readKey(const std::string& str);
495 
502 struct Chord
503 {
514  enum class ChordType
515  {
516  Minor,
517  Major,
518  Sus4,
519  Sus2,
520  Dim,
521  Aug
522  };
525 
528  enum class SeventhType
529  {
530  Major,
531  Minor,
532  Dim,
533  None
534  };
537 
542  std::vector<CompleteNoteName> getNotes() const __attribute__((pure));
543 
552  Chord(Fraction d = 4, const CompleteNoteName& b= CompleteNoteName(), const SeventhType& s = SeventhType::None, const ChordType& c = ChordType::Major);
553 
558  bool operator==(const Chord& o) const __attribute__((pure));
563  bool operator!=(const Chord& o) const __attribute__((pure));
564 
565  using ChordID = unsigned;
566  explicit Chord(ChordID id);
567  explicit operator ChordID() const;
568  static const unsigned nbChords = 6*4*12;
569  static const std::vector<CompleteNoteName>& getNotes(ChordID c);
570 
571 };
572 
580 std::ostream& operator <<(std::ostream& o, const Chord& c);
581 
589 Chord readChord(const std::string& s);
590 
592 using ChordProgression = std::vector<Chord>;
593 
601 std::ostream& operator <<(std::ostream& o, const ChordProgression& c);
602 
610 ChordProgression readChordProgression(const std::string& str);
611 
620 ChordProgression moveCP(const ChordProgression& cp, int i, const Key& k);
621 
629 class Melody
630 {
631 public:
632 
633  using ConstIterator = std::vector<Measure>::const_iterator;
634  //Constructor
635 
640  Melody(const std::vector<Measure>& v,Key k, unsigned tempo = 120);
641 
645  Melody(const Key& k = Key(NoteName::C));
646 
647 
653  std::vector<Measure> convertToMeasures() const __attribute__((pure));
654 
658  const Key& getKey() const __attribute__((pure));
659 
660  // Set_functions
661 
662 
667  ConstIterator begin() const __attribute__((pure));
672  ConstIterator end() const __attribute__((pure));
677  unsigned size() const;
678 
685  Melody transpose(const Key& k) const;
686 
690  bool isBinary() const;
691 
696  bool operator==(const Melody& o) const __attribute__((pure));
701  bool operator!=(const Melody& o) const __attribute__((pure));
702 
708  std::ostream& createABCChordFile(std::ostream& out, const ChordProgression& chords) const;
709 
710  unsigned m_BPM;
711 private:
712  std::vector<Measure> m_measures;
713  Key m_k;
714 };
715 
727 std::ostream& operator <<(std::ostream& o, const Melody& m);
728 
736 Melody readMelody(const std::string& str);
737 
738 #endif // MUSIC_HPP_INCLUDED
Pitch & operator-=(int nb)
Substract half tones to pitch.
ChordType m_type
Type of the chord.
Definition: Music.hpp:536
ChordProgression readChordProgression(const std::string &str)
parser for ChordProgression The format is as follows : Chord1;Chord2;...LastChord; ...
bool operator==(const CompleteNoteName &o) const __attribute__((pure))
Comparison operator for CompleteNoteName.
Accidental m_modifier
Accidental of the note (sharp, flat,none)
Definition: Music.hpp:109
Chord readChord(const std::string &s)
parser for chord The format follows the guitar chord format
Key readKey(const std::string &str)
input parser for Key The format is as shown in ABC notation Link is : http://abcnotation.com/wiki/abc:standard:v2.1#kkey
Melody readMelody(const std::string &str)
parser for Melody The format is the ABC format. We ignore |, :, >, ... when reading something that is...
Pitch operator-(int nb) const __attribute__((pure))
Creates new Pitch by substracting nb half tones to this one.
boost::rational< int > Fraction
Fraction class to easily manipulate fractions of beats, measures,...
Definition: Music.hpp:98
Fraction m_duration
Hold the duration of the chord, in fraction of the bar it is played in. SHOULD BE DELETED ...
Definition: Music.hpp:523
Means that we use the natural note (no accidental)
NoteName
Describe the name of the note.
Definition: Music.hpp:43
ChordProgression moveCP(const ChordProgression &cp, int i, const Key &k)
change the base notes of a chord progression
Fraction m_duration
Duration of the note, as a fraction of a beat.
Definition: Music.hpp:251
CompleteNoteName getNoteName() const __attribute__((pure))
Returns the name of the note, ommiting the octave.
CompleteNoteName m_base
Name of the base name of the note.
Definition: Music.hpp:524
Describes a melody.
Definition: Music.hpp:629
std::map< Position, Note >::const_iterator ConstIterator
Allows to iterate on the notes of the measure.
Definition: Music.hpp:318
std::istream & operator>>(std::istream &i, NoteName &n)
input operator for NoteName The format is capital or small letters for the NoteName ...
Describe a note with its absolute pitch.
Definition: Music.hpp:153
NoteName m_name
Name of the note (C,D,...)
Definition: Music.hpp:108
Pitch operator+(int nb) const __attribute__((pure))
Creates new Pitch by adding nb half tones to this one.
bool operator==(const Pitch &o) const __attribute__((pure))
Comparison operator for Pitch done.
Describe a chord.
Definition: Music.hpp:502
Describe the key of a melody.
Definition: Music.hpp:438
CompleteNoteName m_base
Base note of the key.
Definition: Music.hpp:440
NoteName m_mode
Mode of the key.
Definition: Music.hpp:441
std::vector< Chord > ChordProgression
ChordProgression is the class used to give the set of chords used.
Definition: Music.hpp:592
bool operator!=(const Pitch &o) const __attribute__((pure))
Difference operator for Pitch done.
Octave m_octave
octave of the note (absolute height of the note)
Definition: Music.hpp:155
Fully describes a note (name + accidental)
Definition: Music.hpp:106
ChordType
Describes the type of the third note in the chord.
Definition: Music.hpp:514
SeventhType m_seventh
if it's not None, the seventh is added according to this value (Dim = double Flat) ...
Definition: Music.hpp:535
CompleteNoteName(NoteName n=NoteName::C, Accidental mod=Accidental::None)
Constructor for CompleteNoteName.
Describe a musical bar.
Definition: Music.hpp:312
std::ostream & operator<<(std::ostream &o, NoteName n)
output operator for NoteName The format is capital letters for the NoteName
bool operator!=(const CompleteNoteName &o) const __attribute__((pure))
Difference operator for CompleteNoteName.
Fraction Position
The Position type is a Fraction of Measure.
Definition: Music.hpp:317
SeventhType
Describes the Type of the fourth note of the chord.
Definition: Music.hpp:528
Pitch & operator+=(int nb)
Adds half tones to pitch.
Pitch(const CompleteNoteName &n, Octave o=4)
Construtor for Pitch with a CompleteNotename.
Describe a note as a musical event (pitch + duration)
Definition: Music.hpp:249
Accidental
Describe the accidental of the note.
Definition: Music.hpp:70
unsigned Octave
Octave 4 is A 440Hz.
Definition: Music.hpp:95
std::vector< Measure >::const_iterator ConstIterator
Allows to iterate on the measures of the Melody.
Definition: Music.hpp:633
Means that we add a semitone.
Means that we subtract a semitone.