-
Notifications
You must be signed in to change notification settings - Fork 0
/
Analyser.cpp
123 lines (99 loc) · 3.63 KB
/
Analyser.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//
// Created by Max on 30/03/2021.
//
#include "Analyser.h"
Analyser::Analyser(DBConnector& connector)
: dbConnector(connector){
}
void Analyser::analyseAndSaveToDB(AudioBuffer<float>& buffer, const string& filename, const string& path){
int index = 0;
int counter = 1;
vector<Grain> grains;
while(index + GRAIN_LENGTH < buffer.getNumSamples()){
// Clear essentia audiobuffer
eAudioBuffer.clear();
auto* reader = buffer.getReadPointer(0);
for (int i = index; i < index + GRAIN_LENGTH; i++){
eAudioBuffer.push_back(reader[i]);
}
computeFeatures();
grains.emplace_back(Grain(filename + to_string(counter),
path,
index,
eLoudness,
eSpectralCentroid,
eSpectralFlux,
ePitch
));
// Update index -> start of next grain
index += GRAIN_LENGTH;
counter++;
}
// Save grains to database
dbConnector.insertGrains(grains);
}
vector<Grain> Analyser::audioBufferToGrains(AudioBuffer<float>& buffer){
int index = 0;
int counter = 1;
vector<Grain> grains;
while(index + GRAIN_LENGTH < buffer.getNumSamples()){
// Clear essentia audiobuffer
eAudioBuffer.clear();
auto* reader = buffer.getReadPointer(0);
for (int i = index; i < index + GRAIN_LENGTH; i++){
eAudioBuffer.push_back(reader[i]);
}
computeFeatures();
Grain* grain = new Grain(eLoudness, eSpectralCentroid, eSpectralFlux, ePitch);
grains.emplace_back(*grain);
// Update index -> start of next grain
index += GRAIN_LENGTH;
counter++;
}
return grains;
}
void Analyser::computeFeatures(){
// Essentia algorithms compute routines
aWindowing->compute();
aSpectrum->compute();
aSpectralCentroid->compute();
aLoudness->compute();
aSpectralFlux->compute();
aPitchYINFFT->compute();
}
void Analyser::initialise(double sr) {
this->sampleRate = sr;
// Create algorithms
standard::AlgorithmFactory& factory = standard::AlgorithmFactory::instance();
// Initialise essentia algorithms
aWindowing.reset(factory.create("Windowing", "type", "blackmanharris62"));
aSpectrum.reset(factory.create("Spectrum"));
aMFCC.reset(factory.create("MFCC"));
aSpectralCentroid.reset(factory.create("SpectralCentroidTime", "sampleRate", sr));
aLoudness.reset(factory.create("Loudness"));
aSpectralFlux.reset(factory.create("Flux"));
aPitchYINFFT.reset(factory.create("PitchYinFFT"));
aRMS.reset(factory.create("RMS"));
// Connect algorithms
aWindowing->input("frame").set(eAudioBuffer);
aWindowing->output("frame").set(windowedFrame);
aSpectrum->input("frame").set(windowedFrame);
aSpectrum->output("spectrum").set(eSpectrumData);
// Spectral centroid
aSpectralCentroid->input("array").set(eAudioBuffer);
aSpectralCentroid->output("centroid").set(eSpectralCentroid);
// Loudness
aLoudness->input("signal").set(eAudioBuffer);
aLoudness->output("loudness").set(eLoudness);
// Spectral flux
aSpectralFlux->input("spectrum").set(eSpectrumData);
aSpectralFlux->output("flux").set(eSpectralFlux);
// Pitch
aPitchYINFFT->input("spectrum").set(eSpectrumData);
aPitchYINFFT->output("pitch").set(ePitch);
aPitchYINFFT->output("pitchConfidence").set(ePitchConfidence);
// RMS
aRMS->input("array").set(eAudioBuffer);
aRMS->output("rms").set(eRMS);
}
Analyser::~Analyser() = default;