|
From: Lasse Kärkkäi. <tr...@us...> - 2011-01-31 17:06:10
|
Module: editor
Branch: master
Commit: 810e8a1325273594dab12ab4250605cfc43a5239
Author: Lasse Karkkainen <tro...@tr...>
Date: Mon Jan 31 18:05:57 2011 +0100
Automatically match floating notes to pitch
---
notegraphwidget.cc | 5 ++++-
pitchvis.cc | 17 +++++++++++++++++
pitchvis.hh | 2 +-
3 files changed, 22 insertions(+), 2 deletions(-)
diff --git a/notegraphwidget.cc b/notegraphwidget.cc
index bd551e7..b0ed152 100644
--- a/notegraphwidget.cc
+++ b/notegraphwidget.cc
@@ -204,7 +204,10 @@ void NoteGraphWidget::updateNotes()
double step = (gap.width() - w * gap.notes.size()) / double(gap.notes.size() + 1);
double x = gap.begin + step;
for (NoteLabels::iterator it2 = gap.notes.begin(); it2 != gap.notes.end(); ++it2) {
- (*it2)->move(x, (*it2)->y());
+ double y = (*it2)->y();
+ // Try to find optimal pitch
+ if (m_pitch) y = n2px(m_pitch->guessNote(px2s(x), px2s(x + w + step), 24));
+ (*it2)->move(x, y);
(*it2)->resize(w, (*it2)->height());
x += w + step;
}
diff --git a/pitchvis.cc b/pitchvis.cc
index 9a324f6..95e1e53 100644
--- a/pitchvis.cc
+++ b/pitchvis.cc
@@ -111,6 +111,23 @@ void PitchVis::paint(QPaintDevice* widget, int x1, int x2) {
painter.end();
}
+int PitchVis::guessNote(double begin, double end, int note) {
+ const unsigned scoreSz = 48;
+ double score[scoreSz] = {};
+ if (note >= 0 || note < 48) score[note] = 10.0; // Slightly prefer the current note
+ // Score against paths
+ for (PitchVis::Paths::const_iterator it = paths.begin(), itend = paths.end(); it != itend; ++it) {
+ for (PitchPath::const_iterator it2 = it->begin(), it2end = it->end(); it2 != it2end; ++it2) {
+ if (it2->time < begin) continue;
+ if (it2->time > end) break;
+ unsigned n = round(it2->note);
+ if (n < scoreSz) score[n] += 100 + it2->level;
+ }
+ }
+ // Return the idx with best score
+ return std::max_element(score + 1, score + scoreSz) - score;
+}
+
unsigned PitchVis::freq2px(double freq) const { return note2px(scale.getNote(freq)); }
/* static */ unsigned PitchVis::note2px(double tone) { return height - static_cast<unsigned>(16.0 * tone); }
/* static */ double PitchVis::px2note(unsigned px) { return (height - px) / 16.0; }
diff --git a/pitchvis.hh b/pitchvis.hh
index ff24bfb..17567cb 100644
--- a/pitchvis.hh
+++ b/pitchvis.hh
@@ -32,7 +32,7 @@ public:
Paths const& getPaths() { moreAvailable = false; return paths; }
bool newDataAvailable() const { return moreAvailable; }
int getXValue() const { return curX; }
-
+ int guessNote(double begin, double end, int initial);
std::size_t width() const { return m_width; }
unsigned freq2px(double freq) const;
|