22 #ifndef ChordExtractor_hxx
23 #define ChordExtractor_hxx
43 double _sparseConstantQKernelThreshold;
55 bool _peakWindowingEnabled;
57 unsigned _estimatedChord;
58 unsigned _secondCandidate;
59 double _squaredRootEnergy;
64 ChordExtractor(
unsigned sampleRate=44100,
double minimumFrequency=98,
unsigned binsPerOctave=36)
65 : _sparseConstantQKernelThreshold(0.0054)
66 , _constantQTransform(sampleRate, minimumFrequency,
maximumFrequency(sampleRate), binsPerOctave)
67 , _constantQFolder(_constantQTransform.getK(), binsPerOctave)
68 , _fourierTransform(_constantQTransform.getfftlength(),1,0)
69 , _circularPeakPicking(binsPerOctave, 12.0/binsPerOctave)
70 , _instantTunningEstimator( 1.0)
71 , _circularPeakTunner( 0.0)
73 , _tunningEnabled(true)
74 , _peakWindowingEnabled(true)
79 _constantQTransform.
sparsekernel(_sparseConstantQKernelThreshold);
80 if (_peakWindowingEnabled)
92 void enableTunning(
bool tunningEnabled=
true) { _tunningEnabled=tunningEnabled; }
93 void enablePeakWindowing(
bool peakWindowingEnabled=
true) { _peakWindowingEnabled=peakWindowingEnabled; }
102 _squaredRootEnergy = 0.0;
104 _squaredRootEnergy += input[i]*input[i];
106 _fourierTransform.
doIt(input);
110 _instantTunningEstimator.
doIt(_circularPeakPicking.
output());
111 _circularPeakTunner.
doIt(_instantTunningEstimator.
output().first, _circularPeakPicking.
output());
113 _circularPeaksToPCP.
doIt(_circularPeakTunner.
output());
115 _circularPeaksToPCP.
doIt(_circularPeakPicking.
output());
119 _chordSegmentator.
doIt(currentTime, _chordCorrelator.
output(), _estimatedChord, _secondCandidate);
123 double maxCorrelation = 0;
124 double underMaxCorrelation = 0;
125 unsigned maxIndex = 0;
126 unsigned underMaxIndex = 0;
127 for (
unsigned i=0; i<correlation.size(); i++)
129 if (correlation[i]<underMaxCorrelation)
continue;
130 if (correlation[i]<maxCorrelation)
133 underMaxCorrelation=correlation[i];
136 underMaxIndex=maxIndex;
137 underMaxCorrelation=maxCorrelation;
139 maxCorrelation=correlation[i];
141 _estimatedChord = maxIndex;
142 _secondCandidate = underMaxIndex;
148 std::string
root(
unsigned chordIndex)
const
150 return _chordCorrelator.
root(chordIndex);
152 std::string
mode(
unsigned chordIndex)
const
154 return _chordCorrelator.
mode(chordIndex);
159 double maxCorrelation=correlation[_estimatedChord];
160 double underMaxCorrelation=correlation[_secondCandidate];
161 if (maxCorrelation*0.7<=correlation[0])
return "None";
162 bool estimationIsClear = maxCorrelation*0.9>underMaxCorrelation;
163 std::ostringstream os;
165 if (!estimationIsClear)
167 os <<
" (" << (correlation[0]/maxCorrelation) <<
")";
168 if (!estimationIsClear)
169 os <<
" (" << (underMaxCorrelation/(underMaxCorrelation+maxCorrelation)) <<
")";
176 const std::vector<double> &
pcp()
const
178 return _circularPeaksToPCP.
output();
180 const std::vector<std::pair<double, double> > &
peaks()
const
182 return _circularPeakPicking.
output();
186 return _chordCorrelator.
output();
207 double energy()
const {
return _squaredRootEnergy; }
215 #endif//ChordExtractor