Mercurial > hg > qm-dsp
comparison dsp/keydetection/GetKeyMode.cpp @ 509:3f0a96460c33
Switch to config-struct constructor; add frameOverlapFactor parameter
author | Chris Cannam <cannam@all-day-breakfast.com> |
---|---|
date | Thu, 06 Jun 2019 14:20:53 +0100 |
parents | af5b7ef02aa7 |
children |
comparison
equal
deleted
inserted
replaced
508:855d862cf02b | 509:3f0a96460c33 |
---|---|
14 License, or (at your option) any later version. See the file | 14 License, or (at your option) any later version. See the file |
15 COPYING included with this distribution for more information. | 15 COPYING included with this distribution for more information. |
16 */ | 16 */ |
17 | 17 |
18 #include "GetKeyMode.h" | 18 #include "GetKeyMode.h" |
19 | |
20 #include "dsp/rateconversion/Decimator.h" | |
21 #include "dsp/chromagram/Chromagram.h" | |
22 | |
19 #include "maths/MathUtilities.h" | 23 #include "maths/MathUtilities.h" |
20 #include "base/Pitch.h" | 24 #include "base/Pitch.h" |
21 | 25 |
22 #include <iostream> | 26 #include <iostream> |
23 | 27 |
45 | 49 |
46 ////////////////////////////////////////////////////////////////////// | 50 ////////////////////////////////////////////////////////////////////// |
47 // Construction/Destruction | 51 // Construction/Destruction |
48 ////////////////////////////////////////////////////////////////////// | 52 ////////////////////////////////////////////////////////////////////// |
49 | 53 |
50 GetKeyMode::GetKeyMode( int sampleRate, float tuningFrequency, | 54 GetKeyMode::GetKeyMode(Config config) : |
51 double hpcpAverage, double medianAverage ) : | 55 m_hpcpAverage(config.hpcpAverage), |
52 m_hpcpAverage( hpcpAverage ), | 56 m_medianAverage(config.medianAverage), |
53 m_medianAverage( medianAverage ), | 57 m_decimationFactor(config.decimationFactor), |
54 m_chrPointer(0), | 58 m_chrPointer(0), |
55 m_decimatedBuffer(0), | 59 m_decimatedBuffer(0), |
56 m_chromaBuffer(0), | 60 m_chromaBuffer(0), |
57 m_meanHPCP(0), | 61 m_meanHPCP(0), |
58 m_majCorr(0), | 62 m_majCorr(0), |
59 m_minCorr(0), | 63 m_minCorr(0), |
60 m_medianFilterBuffer(0), | 64 m_medianFilterBuffer(0), |
61 m_sortedBuffer(0), | 65 m_sortedBuffer(0), |
62 m_keyStrengths(0) | 66 m_keyStrengths(0) |
63 { | 67 { |
64 m_decimationFactor = 8; | 68 ChromaConfig chromaConfig; |
65 | 69 |
66 // Chromagram configuration parameters | 70 // Chromagram configuration parameters |
67 m_chromaConfig.normalise = MathUtilities::NormaliseUnitMax; | 71 chromaConfig.normalise = MathUtilities::NormaliseUnitMax; |
68 m_chromaConfig.FS = sampleRate / (double)m_decimationFactor; | 72 chromaConfig.FS = config.sampleRate / (double)m_decimationFactor; |
69 if (m_chromaConfig.FS < 1) { | 73 if (chromaConfig.FS < 1) { |
70 m_chromaConfig.FS = 1; | 74 chromaConfig.FS = 1; |
71 } | 75 } |
72 | 76 |
73 // Set C3 (= MIDI #48) as our base: | 77 // Set C3 (= MIDI #48) as our base: |
74 // This implies that key = 1 => Cmaj, key = 12 => Bmaj, key = 13 => Cmin, etc. | 78 // This implies that key = 1 => Cmaj, key = 12 => Bmaj, key = 13 => Cmin, etc. |
75 m_chromaConfig.min = Pitch::getFrequencyForPitch( 48, 0, tuningFrequency ); | 79 chromaConfig.min = |
76 m_chromaConfig.max = Pitch::getFrequencyForPitch( 96, 0, tuningFrequency ); | 80 Pitch::getFrequencyForPitch( 48, 0, config.tuningFrequency ); |
77 | 81 chromaConfig.max = |
78 m_chromaConfig.BPO = kBinsPerOctave; | 82 Pitch::getFrequencyForPitch( 96, 0, config.tuningFrequency ); |
79 m_chromaConfig.CQThresh = 0.0054; | 83 |
84 chromaConfig.BPO = kBinsPerOctave; | |
85 chromaConfig.CQThresh = 0.0054; | |
80 | 86 |
81 // Chromagram inst. | 87 // Chromagram inst. |
82 m_chroma = new Chromagram( m_chromaConfig ); | 88 m_chroma = new Chromagram(chromaConfig); |
83 | 89 |
84 // Get calculated parameters from chroma object | 90 // Get calculated parameters from chroma object |
85 m_chromaFrameSize = m_chroma->getFrameSize(); | 91 m_chromaFrameSize = m_chroma->getFrameSize(); |
92 | |
86 // override hopsize for this application | 93 // override hopsize for this application |
87 m_chromaHopSize = m_chromaFrameSize; | 94 m_chromaHopSize = m_chromaFrameSize / config.frameOverlapFactor; |
88 | 95 |
89 // std::cerr << "chroma frame size = " << m_ChromaFrameSize << ", decimation factor = " << m_DecimationFactor << " therefore block size = " << getBlockSize() << std::endl; | 96 // std::cerr << "chroma frame size = " << m_ChromaFrameSize << ", decimation factor = " << m_DecimationFactor << " therefore block size = " << getBlockSize() << std::endl; |
90 | 97 |
91 // Chromagram average and estimated key median filter lengths | 98 // Chromagram average and estimated key median filter lengths |
92 m_chromaBufferSize = (int)ceil | 99 m_chromaBufferSize = (int)ceil |
93 (m_hpcpAverage * m_chromaConfig.FS / m_chromaFrameSize); | 100 (m_hpcpAverage * chromaConfig.FS / m_chromaFrameSize); |
94 m_medianWinSize = (int)ceil | 101 m_medianWinSize = (int)ceil |
95 (m_medianAverage * m_chromaConfig.FS / m_chromaFrameSize); | 102 (m_medianAverage * chromaConfig.FS / m_chromaFrameSize); |
96 | 103 |
97 // Reset counters | 104 // Reset counters |
98 m_bufferIndex = 0; | 105 m_bufferIndex = 0; |
99 m_chromaBufferFilling = 0; | 106 m_chromaBufferFilling = 0; |
100 m_medianBufferFilling = 0; | 107 m_medianBufferFilling = 0; |
280 key = m_sortedBuffer[midpoint-1]; | 287 key = m_sortedBuffer[midpoint-1]; |
281 | 288 |
282 return key; | 289 return key; |
283 } | 290 } |
284 | 291 |
285 | |
286 bool GetKeyMode::isModeMinor( int key ) | |
287 { | |
288 return (key > 12); | |
289 } | |
290 | |
291 int GetKeyMode::getChromaSize() | |
292 { | |
293 return kBinsPerOctave; | |
294 } | |
295 | |
296 double* GetKeyMode::getKeyStrengths() { | 292 double* GetKeyMode::getKeyStrengths() { |
297 int k; | 293 int k; |
298 | 294 |
299 for (k = 0; k < 24; ++k) { | 295 for (k = 0; k < 24; ++k) { |
300 m_keyStrengths[k] = 0; | 296 m_keyStrengths[k] = 0; |