Import upstream version 0.99.2
[fmit.git] / libs / Music / Quantizer.h
1 // Copyright 2005 "Gilles Degottex"
2
3 // This file is part of "Music"
4
5 // "Music" is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Lesser General Public License as published by
7 // the Free Software Foundation; either version 2.1 of the License, or
8 // (at your option) any later version.
9 //
10 // "Music" 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 Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
19
20 #ifndef _QUANTIZER_H_
21 #define _QUANTIZER_H_
22
23 #include <string>
24 #include <deque>
25 #include <vector>
26 #include <iostream>
27 using namespace std;
28 #include <qdatetime.h>
29 #include <Music/Music.h>
30 using namespace Music;
31 #include <CppAddons/Observer.h>
32
33 struct QuantizerListener : Listener<QuantizerListener>
34 {
35         virtual void noteStarted(int tag, int ht, double dt)            {todo("noteStarted");}
36         virtual void noteFinished(int tag, int ht, double dt)           {todo("noteFinished");}
37         virtual void notePlayed(int ht, double duration, double dt)     {todo("notePlayed");}
38 };
39
40 /*!
41   a function object for merging small note events into note events with a duration.
42   - Fill small holes where the note should appears
43   - Ignore notes which are too small
44   */
45 class Quantizer : public Talker<QuantizerListener>
46 {
47         QTime m_time;
48
49         //! in millis
50         float m_tolerance;
51         float m_min_density;
52
53         struct State{
54                 double time;
55                 bool play;
56                 State(double t, bool p) : time(t), play(p) {}
57         };
58         struct Channel{
59                 deque<State> old_states;
60                 enum{QC_NOTHING, QC_STARTING, QC_PLAYING} state;
61                 QTime lag;
62                 QTime duration;
63                 double reliability;
64                 int last_tag;
65                 Channel() : state(QC_NOTHING) {}
66         };
67         int m_min_ht;
68
69   public:
70         Quantizer(float tolerance=1, float min_density=0.5);
71
72         double getTolerance()                                                   {return m_tolerance;}
73         void setTolerance(float tolerance)                              {m_tolerance=tolerance;}
74         double getMinDensity()                                                  {return m_min_density;}
75         void setMinDensity(float min_density)                   {m_min_density=min_density;}
76
77         void quantize(const vector<bool> hts, int min_ht);
78         void update(int ht);
79
80         int m_min_stored_recon;
81         int getMinStoredRecon(){return m_min_stored_recon;}
82
83         int getNbChannels()                                                             {return m_channels.size();}
84         int getSemitoneMin()                                                                    {return m_min_ht;}
85         vector<Channel> m_channels;
86
87         void cutAll();
88
89         double getLatency()                                                             {return m_tolerance;}
90
91         virtual ~Quantizer(){}
92 };
93
94 #endif // _QUANTIZER_H_
95