f555ef9e3b7c6ec51ab4522d680793022ba29363
[ardour.git] / libs / qm-dsp / dsp / tonal / TonalEstimator.h
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
2
3 /*
4     QM DSP Library
5
6     Centre for Digital Music, Queen Mary, University of London.
7     This file copyright 2006 Martin Gasser.
8
9     This program is free software; you can redistribute it and/or
10     modify it under the terms of the GNU General Public License as
11     published by the Free Software Foundation; either version 2 of the
12     License, or (at your option) any later version.  See the file
13     COPYING included with this distribution for more information.
14 */
15
16 #ifndef _TONALESTIMATOR_
17 #define _TONALESTIMATOR_
18
19
20 #include <valarray>
21 #include <numeric>
22 #include <algorithm>
23 #include <iostream>
24
25 class ChromaVector : public std::valarray<double>
26 {
27 public:
28         ChromaVector(size_t uSize = 12) : std::valarray<double>()
29         { resize(uSize, 0.0f); }
30         
31         virtual ~ChromaVector() {};
32         
33         void printDebug()
34         {
35                 for (int i = 0; i < size(); i++)
36                 {
37                         std::cout <<  (*this)[i] << ";";
38                 }
39                 
40                 std::cout << std::endl;
41         }
42         
43         void normalizeL1()
44         {
45                 // normalize the chroma vector (L1 norm)
46                 double dSum = 0.0;
47         
48                 for (size_t i = 0; i < 12; (dSum += std::abs((*this)[i++]))) ;
49                 for (size_t i = 0; i < 12; dSum > 0.0000001?((*this)[i] /= dSum):(*this)[i]=0.0, i++) ;
50
51         }
52
53     void clear()
54     {
55         for (size_t i = 0; i < 12; ++i) (*this)[i] = 0.0;
56     }
57         
58         
59 };
60
61 class TCSVector : public std::valarray<double>
62 {
63 public:
64         TCSVector() : std::valarray<double>()
65         { resize(6, 0.0f); }
66         
67         virtual ~TCSVector() {};
68
69         void printDebug()
70         {
71                 for (int i = 0; i < size(); i++)
72                 {
73                         std::cout <<  (*this)[i] << ";";
74                 }
75                 
76                 std::cout << std::endl;
77         }
78         
79         double magnitude() const
80         {
81                 double dMag = 0.0;
82                 
83                 for (size_t i = 0; i < 6; i++)
84                 {
85                         dMag += std::pow((*this)[i], 2.0);
86                 }
87                 
88                 return std::sqrt(dMag);
89         }
90
91 };
92
93
94
95 class TonalEstimator
96 {
97 public:
98         TonalEstimator();
99         virtual ~TonalEstimator();
100         TCSVector transform2TCS(const ChromaVector& rVector);
101 protected:
102         std::valarray< std::valarray<double> > m_Basis;
103 };
104
105 #endif // _TONALESTIMATOR_