-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAudioToScoreAligner.cpp
232 lines (195 loc) · 6.55 KB
/
AudioToScoreAligner.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
Yucong Jiang, June 2021
*/
#include "AudioToScoreAligner.h"
#include "Templates.h"
#include "Paths.h"
#include "SimpleHMM.h"
#include <cmath>
#include <filesystem>
#include <vector>
using namespace std;
AudioToScoreAligner::AudioToScoreAligner(float inputSampleRate, int hopSize,
int startEvent, int endEvent, int startFrame, int endFrame) :
m_inputSampleRate{inputSampleRate} , m_hopSize{hopSize}, m_startEvent{startEvent},
m_endEvent{endEvent}, m_startFrame{startFrame}, m_endFrame{endFrame}
{
}
AudioToScoreAligner::~AudioToScoreAligner()
{
}
bool AudioToScoreAligner::loadAScore(string scoreName, int blockSize)
{
cerr << "In loadAScore: scoreName is -> " << scoreName << '\n';
auto scores = Paths::getScores();
if (scores.find(scoreName) == scores.end()) {
cerr << "Score not found: " << scoreName << '\n';
return false;
}
filesystem::path targetPath = scores[scoreName];
// Paths::getScores() has already verified that these exist
string scorePath = targetPath.string() + "/" + scoreName + ".solo";
string scoreMeterPath = targetPath.string() + "/" + scoreName + ".meter";
bool success = m_score.initialize(scorePath);
if (success) success = m_score.readMeter(scoreMeterPath);
NoteTemplates t =
CreateNoteTemplates::getNoteTemplates(m_inputSampleRate, blockSize);
m_score.setEventTemplates(t);
return success;
}
void AudioToScoreAligner::supplyFeature(DataSpectrum s)
{
m_dataFeatures.push_back(s);
}
void AudioToScoreAligner::initializeLikelihoods()
{
int frames = m_dataFeatures.size();
int events = m_score.getMusicalEvents().size();
if (frames == 0) {
cerr << "AudioToScoreAligner::initializeLikelihoods:\
features are not supplied." << '\n';
}
cerr << "AudioToScoreAligner::initializeLikelihoods:\
features are indeed supplied. Number of frames = " << frames << '\n';
for (int frame = 0; frame < frames; frame++) {
vector<Likelihood> l;
for (int event = 0; event < events; event++) {
l.push_back(Likelihood(0, false));
}
m_likelihoods.push_back(l);
}
// event = -1 and -2:
for (int frame = 0; frame < frames; frame++) {
vector<Likelihood> l;
l.push_back(Likelihood(0, false)); // event = -1
l.push_back(Likelihood(0, false)); // event = -2
m_silenceLikelihoods.push_back(l);
}
}
double AudioToScoreAligner::getLikelihood(int frame, int event)
{
if (m_dataFeatures.size() == 0) {
cerr << "AudioToScoreAligner::getLikelihood:\
features are not supplied." << '\n';
}
static Template silenceTemplate;
static bool haveInitializedSilenceTemplates = false;
if (!haveInitializedSilenceTemplates) { // TODO: Change it later.
int bins = m_dataFeatures[frame].size();
double low_freq = 20.;
double low_proportion = .5;
double p1 = low_proportion / low_freq;
double p2 = (1 - low_proportion) / (double)bins;
for (int bin = 0; bin < bins; bin++) {
if (bin < low_freq) {
silenceTemplate.push_back(p1 + p2);
} else {
silenceTemplate.push_back(p2);
}
}
haveInitializedSilenceTemplates = true;
}
// TODO: check the range for frame and event
// If event < 0, use a different template:
if (event < 0) {
if(!m_silenceLikelihoods[frame][abs(event)-1].calculated) {
double score = 0;
for (int bin = 0; bin < int(m_dataFeatures[frame].size()); bin++) {
score += m_dataFeatures[frame][bin]*log(silenceTemplate[bin]);
/*
if (frame == 2 && event == -1) {
if (isnan(score)) {
cout << "bin="<<bin<<", score="<<score << '\n';
cout << "bin value: "<<m_dataFeatures[frame][bin] << '\n';
cout << "template value: "<<silenceTemplate[bin] << '\n';
}
}
*/
}
m_silenceLikelihoods[frame][abs(event)-1].likelihood = exp(score);
m_silenceLikelihoods[frame][abs(event)-1].calculated = true;
/*
if (isnan(m_silenceLikelihoods[frame][abs(event)-1].likelihood)) {
cerr << "Frame="<<frame << ", event="<<event<<'\n';
cerr << "like="<<m_silenceLikelihoods[frame][abs(event)-1].likelihood << '\n';
}
*/
}
return m_silenceLikelihoods[frame][abs(event)-1].likelihood;
}
if (!m_likelihoods[frame][event].calculated) {
const Score::MusicalEventList& eventList = m_score.getMusicalEvents();
double score = 0;
for (int bin = 0; bin < int(m_dataFeatures[frame].size()); bin++) {
score += m_dataFeatures[frame][bin]*log(eventList[event].eventTemplate[bin]);
}
m_likelihoods[frame][event].likelihood = exp(score);
m_likelihoods[frame][event].calculated = true;
}
return m_likelihoods[frame][event].likelihood;
}
AudioToScoreAligner::AlignmentResults AudioToScoreAligner::align()
{
initializeLikelihoods(); // all zeros
AlignmentResults results;
SimpleHMM hmm = SimpleHMM(*this); // build state graph
results = hmm.getAlignmentResults();
return results;
/*
// Maximum Likelihood Method:
int frame = 0;
for (const auto& feature: m_dataFeatures) {
int maxEvent = 0;
double maxL = 0;
for (int event = 0; event < m_score.getMusicalEvents().size(); event++) {
double l = getLikelihood(frame, event);
if (l > maxL) {
maxL = l;
maxEvent = event;
}
}
results.push_back(maxEvent);
frame++;
}
return results;
*/
}
float AudioToScoreAligner::getSampleRate() const
{
return m_inputSampleRate;
}
float AudioToScoreAligner::getHopSize() const
{
return m_hopSize;
}
Score AudioToScoreAligner::getScore() const
{
return m_score;
}
AudioToScoreAligner::DataFeatures AudioToScoreAligner::getDataFeatures() const
{
return m_dataFeatures;
}
void AudioToScoreAligner::setAlignmentConstraints(int se, int ee, int sf, int ef)
{
m_startEvent = se;
m_endEvent = ee;
m_startFrame = sf;
m_endFrame = ef;
}
int AudioToScoreAligner::getStartEvent() const
{
return m_startEvent;
}
int AudioToScoreAligner::getEndEvent() const
{
return m_endEvent;
}
int AudioToScoreAligner::getStartFrame() const
{
return m_startFrame;
}
int AudioToScoreAligner::getEndFrame() const
{
return m_endFrame;
}