forked from ibsh/libKeyFinder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spectrumanalyser.cpp
executable file
·62 lines (50 loc) · 2.26 KB
/
spectrumanalyser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*************************************************************************
Copyright 2011-2013 Ibrahim Sha'ath
This file is part of LibKeyFinder.
LibKeyFinder is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LibKeyFinder is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with LibKeyFinder. If not, see <http://www.gnu.org/licenses/>.
*************************************************************************/
#include "spectrumanalyser.h"
namespace KeyFinder{
SpectrumAnalyser::SpectrumAnalyser(unsigned int f, const Parameters& params, ChromaTransformFactory* spFactory){
bins = params.getOctaves() * params.getBandsPerOctave();
hopSize = params.getHopSize();
ct = spFactory->getChromaTransform(f, params);
fft = new FftAdapter(params.getFftFrameSize());
tw = params.getTemporalWindow();
}
SpectrumAnalyser::~SpectrumAnalyser(){
// don't delete the chroma transform; it's stored in the factory
delete fft;
}
Chromagram* SpectrumAnalyser::chromagram(AudioData* audio) const{
if (audio->getChannels() != 1)
throw Exception("Audio must be monophonic to be analysed");
WindowFunction win;
Chromagram* ch = new Chromagram((audio->getSampleCount()/hopSize) + 1,bins);
unsigned int fftFrameSize = fft->getFrameSize();
for (unsigned int i = 0; i < audio->getSampleCount(); i += hopSize){
for (unsigned int j = 0; j < fftFrameSize; j++){
if(i+j < audio->getSampleCount()){
fft->setInput(j, audio->getSample(i+j) * win.window(tw, j, fftFrameSize)); // real part, windowed
}else{
fft->setInput(j, 0.0); // zero-pad if no PCM data remaining
}
}
fft->execute();
std::vector<float> cv = ct->chromaVector(fft);
for (unsigned int j=0; j<bins; j++){
ch->setMagnitude(i/hopSize, j, cv[j]);
}
}
return ch;
}
}