|
From: Lasse Kärkkäi. <tr...@us...> - 2011-01-18 15:29:48
|
Module: editor
Branch: master
Commit: 69359edadb203d83029122b00223f8159c05131e
Author: Lasse Karkkainen <tro...@tr...>
Date: Tue Jan 18 16:29:34 2011 +0100
Make pitchvis do all pix<->time/note mapping so that notes are synchronized with pitchvis
---
notegraphwidget.cc | 42 +++++++++---------------------------------
notegraphwidget.hh | 7 +------
pitchvis.cc | 15 +++++++++------
pitchvis.hh | 8 ++++++++
4 files changed, 27 insertions(+), 45 deletions(-)
diff --git a/notegraphwidget.cc b/notegraphwidget.cc
index 5f0c8f2..48cc7d1 100644
--- a/notegraphwidget.cc
+++ b/notegraphwidget.cc
@@ -15,12 +15,8 @@ namespace {
}
}
-
-const int NoteGraphWidget::noteYStep = 28;
-
NoteGraphWidget::NoteGraphWidget(QWidget *parent)
- : QLabel(parent), m_pixPerSec(200), m_lowestNote(MusicalScale::getBaseId()), m_octaves(3),
- m_panHotSpot(), m_selectedNote(), m_selectedAction(NONE), m_actionHappened(), m_pitch("music.raw")
+ : QLabel(parent), m_pitch("music.raw"), m_panHotSpot(), m_selectedNote(), m_selectedAction(NONE), m_actionHappened()
{
unsigned width = m_pitch.width(), height = m_pitch.height;
QProgressDialog progress(tr("Rendering pitch data..."), tr("&Abort"), 0, width, this);
@@ -85,7 +81,7 @@ void NoteGraphWidget::setLyrics(QString lyrics)
QString word;
ts >> word;
if (!word.isEmpty()) {
- m_notes.push_back(new NoteLabel(Note(word.toStdString()), this, QPoint(0, n2px(m_lowestNote + 12 * m_octaves - 6)), QSize(), !first));
+ m_notes.push_back(new NoteLabel(Note(word.toStdString()), this, QPoint(0, m_pitch.note2px(6)), QSize(), !first));
doOperation(opFromNote(*m_notes.back(), m_notes.size()-1), Operation::NO_EXEC);
first = false;
}
@@ -98,12 +94,7 @@ void NoteGraphWidget::setLyrics(const VocalTrack &track)
{
clear();
- // Determine how many octaves are needed and what is the base line
- int diff = track.noteMax - track.noteMin;
- m_octaves = std::ceil(diff / 12.0f) + 1;
- m_lowestNote = clamp(track.noteMin - 6, 0, 1000);
- //std::cout << "Octaves: " << m_octaves << " Lowest:" << m_lowestNote << std::endl;
- setFixedSize(s2px(track.endTime), ndiff2px(m_octaves*12));
+ setFixedSize(s2px(track.endTime), h());
bool first = true;
const Notes ¬es = track.notes;
@@ -349,12 +340,12 @@ void NoteGraphWidget::keyPressEvent(QKeyEvent *event)
break;
case Qt::Key_Up: // Move note up
if (m_selectedNote) {
- m_selectedNote->move(m_selectedNote->x(), m_selectedNote->y() - noteYStep);
+ m_selectedNote->move(m_selectedNote->x(), m_pitch.note2px(m_pitch.px2note(m_selectedNote->y()) + 1));
}
break;
case Qt::Key_Down: // Move note down
if (m_selectedNote) {
- m_selectedNote->move(m_selectedNote->x(), m_selectedNote->y() + noteYStep);
+ m_selectedNote->move(m_selectedNote->x(), m_pitch.note2px(m_pitch.px2note(m_selectedNote->y()) - 1));
}
break;
case Qt::Key_Delete: // Delete selected note
@@ -417,25 +408,10 @@ void NoteGraphWidget::doOperation(const Operation& op, Operation::OperationFlags
}
-int NoteGraphWidget::s2px(double sec) const {
- return sec * m_pixPerSec;
-}
-double NoteGraphWidget::px2s(int px) const {
- return px / m_pixPerSec;
-}
-int NoteGraphWidget::ndiff2px(int dn) const {
- return dn * noteYStep;
-}
-int NoteGraphWidget::n2px(int note) const {
- int highestNote = m_lowestNote + 12 * m_octaves;
- return (highestNote - note) * noteYStep;
-}
-int NoteGraphWidget::px2n(int px) const {
- int highestNote = m_lowestNote + 12 * m_octaves;
- return highestNote - round(px / (float)noteYStep);
-}
-
-
+int NoteGraphWidget::s2px(double sec) const { return m_pitch.time2px(sec); }
+double NoteGraphWidget::px2s(int px) const { return m_pitch.px2time(px); }
+int NoteGraphWidget::n2px(int note) const { return m_pitch.note2px(note); }
+int NoteGraphWidget::px2n(int px) const { return m_pitch.px2note(px); }
void FloatingGap::addNote(NoteLabel* n)
{
diff --git a/notegraphwidget.hh b/notegraphwidget.hh
index 4d40379..7d1deea 100644
--- a/notegraphwidget.hh
+++ b/notegraphwidget.hh
@@ -33,10 +33,9 @@ public:
int s2px(double sec) const;
double px2s(int px) const;
- int ndiff2px(int dn) const;
int n2px(int note) const;
int px2n(int px) const;
- int h() const { return 12 * m_octaves * noteYStep; }
+ int h() const { return m_pitch.height; }
signals:
@@ -54,10 +53,6 @@ protected:
private:
void finalizeNewLyrics();
- double m_pixPerSec; ///< Pixels per second
- int m_lowestNote; ///< Note id / midi pitch of the lowest note in the view
- int m_octaves; ///< How many octaves are displayed in the view
-
int m_requiredWidth;
QPoint m_panHotSpot;
NoteLabel* m_selectedNote;
diff --git a/pitchvis.cc b/pitchvis.cc
index 271e50d..be9ab74 100644
--- a/pitchvis.cc
+++ b/pitchvis.cc
@@ -1,6 +1,5 @@
#include "pitchvis.hh"
-#include "notes.hh"
#include "pitch.hh"
#include <fstream>
#include <iostream>
@@ -21,14 +20,12 @@ template <typename T> void readVec(std::string const& filename, std::vector<T>&
if (!f) throw std::runtime_error("Error reading " + filename);
}
-PitchVis::PitchVis(std::string const& filename): height(1024) {
+PitchVis::PitchVis(std::string const& filename): step(1024), height(768) {
std::vector<float> data;
try { readVec(filename, data); } catch(std::exception& e) { std::cerr << e.what() << std::endl; return; }
- unsigned step = 1024;
unsigned width = data.size() / step;
img.resize(width * height);
Analyzer analyzer(44100, "");
- MusicalScale scale;
QProgressDialog progress(tr("Analyzing pitch data..."), tr("&Abort"), 0, width, this);
progress.setWindowModality(Qt::WindowModal);
@@ -40,7 +37,7 @@ PitchVis::PitchVis(std::string const& filename): height(1024) {
analyzer.process();
Analyzer::Peaks peaks = analyzer.getPeaks();
for (unsigned i = 0; i < peaks.size(); ++i) {
- unsigned y = height - static_cast<unsigned>(16.0 * scale.getNote(peaks[i].freq));
+ unsigned y = freq2px(peaks[i].freq);
if (y == 0 || y >= height - 1) continue;
float value = 0.003 * (level2dB(peaks[i].level) + 80.0);
if (value <= 0.0) continue;
@@ -56,7 +53,7 @@ PitchVis::PitchVis(std::string const& filename): height(1024) {
unsigned int i = 0;
for (Analyzer::Tones::const_iterator it = tones.begin(), itend = tones.end(); it != itend && i < 3; ++it) {
if (it->age < Tone::MINAGE) continue;
- unsigned y = height - static_cast<unsigned>(16.0 * scale.getNote(it->freqSlow));
+ unsigned y = freq2px(it->freqSlow);
if (y == 0 || y >= height - 1) continue;
float value = 0.003 * (level2dB(it->levelSlow) + 80.0);
if (value <= 0.0) continue;
@@ -83,3 +80,9 @@ PitchVis::PitchVis(std::string const& filename): height(1024) {
progress.setValue(width);
}
+unsigned PitchVis::freq2px(double freq) const { return note2px(scale.getNote(freq)); }
+unsigned PitchVis::note2px(double note) const { return height - static_cast<unsigned>(16.0 * note); }
+double PitchVis::px2note(unsigned px) const { return (height - px) / 16.0; }
+unsigned PitchVis::time2px(double t) const { return t * 44100.0 / step; }
+double PitchVis::px2time(double px) const { return px / 44100.0 * step; }
+
diff --git a/pitchvis.hh b/pitchvis.hh
index a85a2ff..1275a1a 100644
--- a/pitchvis.hh
+++ b/pitchvis.hh
@@ -1,5 +1,6 @@
#pragma once
+#include "notes.hh"
#include "util.hh"
#include <QWidget>
#include <cmath>
@@ -31,11 +32,18 @@ struct Pixel {
};
class PitchVis: public QWidget {
+ unsigned step;
std::vector<Pixel> img;
+ MusicalScale scale;
public:
const std::size_t height;
Pixel& operator()(std::size_t x, std::size_t y) { return img[x * height + y]; }
std::size_t width() const { return img.size() / height; }
PitchVis(std::string const& filename);
+ unsigned freq2px(double freq) const;
+ unsigned note2px(double note) const;
+ double px2note(unsigned px) const;
+ unsigned time2px(double t) const;
+ double px2time(double px) const;
};
|