Generate an accompaniement for a given melody
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Markov.hpp
Go to the documentation of this file.
1 
10 #ifndef MARKOV_HPP_INCLUDED
11 #define MARKOV_HPP_INCLUDED
12 
13 #include "Music.hpp"
14 #include "Traits.hpp"
15 #include "Database.hpp"
16 #include <vector>
17 #include <map>
18 #include <exception>
19 #include <string>
20 #include <iostream>
21 #include <boost/type_traits.hpp>
22 #include <boost/rational.hpp>
23 
24 
30 class HMM
31 {
32 public:
33  friend HMM* loadFromFile(std::string fname);
43  using Proba = double;
44  using ObsId = unsigned;
45  using StateId = unsigned;
46  using Score = double;
47  //using Proba = boost::rational<int>;
48  //using Score = boost::rational<int>;
49 
50  virtual ~HMM() {};
51 
62  virtual std::vector<std::pair<ChordProgression,Score> > getBestProgressions(Melody M, unsigned n =1) const = 0;
63 
64 
69  virtual unsigned getMaxProgressionsNbr() const = 0;
70 
76  virtual void saveToFile(const std::string & fname) const = 0;
77 
78 
79  virtual void learn(Database& db, const std::string& style) = 0;
80 
81 protected:
82 
83 };
84 
85 
86 #include "Observation.hpp"
87 
88 CREATE_MEMBER_DETECTOR(getClosestObs) //macro that generates a trait has_getClosestObs<> that checks for the implementation of the public method "getClosestObs"
90 
91 template <class Obs>
92 class HMM_backend;
93 
100 template <class Obs>
101 class HMM_backend : public HMM
102 {
103  static_assert(boost::has_left_shift<std::ostream,Obs>::value,"error : the template argument Obs must provide a stream operator to ostream"); //ensure that the template class can be printed to ostream
104  static_assert(function_traits::has_TypeID<Obs>::value,"error : the template argument Obs must provide a static member describing its type id");
105  static_assert(function_traits::has_getClosestObs<Obs,HMM::ObsId(const std::vector<Obs>&)>::value,"error : the template argument must provide a public getClosestObs method of type std::vector<Obs> -> ObsId");
106 public :
107  class State
108  {
109  public :
110  friend class HMM_backend;
112  unsigned m_position;
113  private :
114  State(const Chord & c,unsigned p,unsigned id) : m_c(c),m_position(p),m_stateID(id){}
115  unsigned m_stateID;
116  operator unsigned() const{return m_stateID;};
117  };
118 
119  HMM_backend();
120  ~HMM_backend(){};
131  std::vector<std::pair<ChordProgression,Score> > getBestProgressions(Melody M, unsigned n =1) const;
132 
137  unsigned getMaxProgressionsNbr() const;
138 
144  void saveToFile(const std::string & fname) const;
145 
146 
147 
148  void learn(Database& db, const std::string& style);
149  void learn_unsupervised(Database& db, const std::string& style);
150 
151 protected:
152  void forwardProcedure(const std::vector<Obs> & o);
153  void backwardProcedure(const std::vector<Obs> & o);
154  void updateProcedure(const std::vector<Obs> & o);
155  void generateObsSet(const std::vector<Data>& entries);
156 
157 private :
158  std::vector<Proba> m_pi;
159  std::vector<State> m_states;
160  std::vector<std::vector<Proba> > m_transition;
161  std::vector<std::vector<Proba> > m_observation;
162  std::vector<Obs> m_obsSet;
163 
164  std::vector<std::vector<Proba>> m_alpha; // used for forward procedure
165  std::vector<std::vector<Proba>> m_beta; // used for backward procedure
166 };
167 
168 
174 HMM * loadFromFile(std::string fname);
175 
176 #include "Markov.cpp"
177 #endif // MARKOV_HPP_INCLUDED
virtual void saveToFile(const std::string &fname) const =0
Save the class to a given file.
#define CREATE_MEMBER_DETECTOR(X)
Definition: Traits.hpp:106
This file contains the descriptions of what can be observation types. Each observation class should i...
unsigned m_position
Position of the chord in the progression (rank, between 1 and 4)
Definition: Markov.hpp:112
double Proba
Return a list of chord progressions with scores.
Definition: Markov.hpp:43
This file contains useful traits to check some properties of class (namely the implementation of stat...
This file defines all the data structures related to music objects.
#define CREATE_STATIC_MEMBER_DETECTOR(X)
Definition: Traits.hpp:127
Describes a melody.
Definition: Music.hpp:629
Definition: Markov.hpp:107
Describe a chord.
Definition: Music.hpp:502
HMM * loadFromFile(std::string fname)
Load the HMM from a given file.
Unified interface for HMMs, independant of the observation type.
Definition: Markov.hpp:30
input operator for NoteName The format is capital or small letters for the NoteName ...
Definition: Database.hpp:101
Backend for a Hidden Markov Model object, templated over the observation type.
Definition: Markov.hpp:101
void saveToFile(const std::string &fname) const
Save the class to a given file.
virtual std::vector< std::pair< ChordProgression, Score > > getBestProgressions(Melody M, unsigned n=1) const =0
Return a list of chord progressions with scores.
unsigned getMaxProgressionsNbr() const
virtual unsigned getMaxProgressionsNbr() const =0
friend HMM * loadFromFile(std::string fname)
Load the HMM from a given file.
std::vector< std::pair< ChordProgression, Score > > getBestProgressions(Melody M, unsigned n=1) const
Return a list of chord progressions with scores.
Chord m_c
Description of the chord.
Definition: Markov.hpp:111