From 328d5958483ceb33cee74def6c947111182b5af0 Mon Sep 17 00:00:00 2001 From: RJ Ryan Date: Fri, 3 Jan 2014 16:21:55 -0500 Subject: Normalize potmeter widget values to [0.0, 1.0] instead of [0.0, 127.0]. This is another step towards getting rid of MIDI assumptions that are baked into the widget system. * Control parameter space for potmeters is now [0.0, 1.0] instead of [0.0, 127.0]. * Non-linearity of parameter space (64 being the center value) now only applies to MIDI, not widgets. * Fix all potmeter-style widgets to use [0.0, 1.0] for parameter values. --- src/widget/knobeventhandler.h | 16 ++++++++-------- src/widget/wdisplay.cpp | 3 +-- src/widget/wknob.cpp | 4 +--- src/widget/wknobcomposed.cpp | 4 ++-- src/widget/woverview.cpp | 32 ++++++++++++++++++++++---------- src/widget/woverview.h | 10 +++++----- src/widget/wslidercomposed.cpp | 30 +++++++++++++++++++----------- src/widget/wvumeter.cpp | 9 ++++----- src/widget/wwaveformviewer.cpp | 15 ++++++++++----- 9 files changed, 72 insertions(+), 51 deletions(-) (limited to 'src/widget') diff --git a/src/widget/knobeventhandler.h b/src/widget/knobeventhandler.h index f638f5a87e..61a11951be 100644 --- a/src/widget/knobeventhandler.h +++ b/src/widget/knobeventhandler.h @@ -31,13 +31,12 @@ class KnobEventHandler { } double value = pWidget->getValue(); - value += dist; + // For legacy (MIDI) reasons this is tuned to 127. + value += dist / 127.0; QCursor::setPos(m_startPos); - if (value > 127.0) - value = 127.0; - else if (value < 0.0) - value = 0.0; + // Clamp to [0.0, 1.0] + value = math_max(0.0, math_min(1.0, value)); pWidget->setValue(value); emit(pWidget->valueChangedLeftDown(value)); @@ -80,11 +79,12 @@ class KnobEventHandler { } void wheelEvent(T* pWidget, QWheelEvent* e) { - double wheelDirection = e->delta() / 120.; + // For legacy (MIDI) reasons this is tuned to 127. + double wheelDirection = e->delta() / (120.0 * 127.0); double newValue = pWidget->getValue() + wheelDirection; - // Clamp to [0.0, 127.0] - newValue = math_max(0.0, math_min(127.0, newValue)); + // Clamp to [0.0, 1.0] + newValue = math_max(0.0, math_min(1.0, newValue)); pWidget->updateValue(newValue); e->accept(); diff --git a/src/widget/wdisplay.cpp b/src/widget/wdisplay.cpp index 0d8bbf9cb5..7c325e598a 100644 --- a/src/widget/wdisplay.cpp +++ b/src/widget/wdisplay.cpp @@ -108,8 +108,7 @@ void WDisplay::setPixmap(QVector* pPixmaps, int iPos, } int WDisplay::getActivePixmapIndex() const { - return static_cast( - m_value * static_cast(m_pixmaps.size()) / 128.0); + return static_cast(m_value * m_pixmaps.size()); } void WDisplay::paintEvent(QPaintEvent* ) { diff --git a/src/widget/wknob.cpp b/src/widget/wknob.cpp index e379643f29..d2b4f306ea 100644 --- a/src/widget/wknob.cpp +++ b/src/widget/wknob.cpp @@ -29,9 +29,7 @@ WKnob::~WKnob() { } int WKnob::getActivePixmapIndex() const { - // TODO(rryan): Ew. - int iNoPos = numPixmaps(); - return (int)(((m_value-64.)*(((float)iNoPos-1.)/127.))+((float)iNoPos/2.)); + return static_cast(m_value * numPixmaps()); } void WKnob::mouseMoveEvent(QMouseEvent* e) { diff --git a/src/widget/wknobcomposed.cpp b/src/widget/wknobcomposed.cpp index e1ef2d32f8..ef7e865ba1 100644 --- a/src/widget/wknobcomposed.cpp +++ b/src/widget/wknobcomposed.cpp @@ -69,8 +69,8 @@ void WKnobComposed::paintEvent(QPaintEvent* e) { if (!m_pKnob.isNull() && !m_pKnob->isNull()) { p.translate(width() / 2.0, height() / 2.0); - // Value is now in the range [0, 1]. - double value = getValue() / 127.0; + // Value is in the range [0, 1]. + double value = getValue(); double angle = m_dMinAngle + (m_dMaxAngle - m_dMinAngle) * value; p.rotate(angle); diff --git a/src/widget/woverview.cpp b/src/widget/woverview.cpp index e7ae137379..f17f607932 100644 --- a/src/widget/woverview.cpp +++ b/src/widget/woverview.cpp @@ -120,14 +120,14 @@ void WOverview::setup(QDomNode node) { //qDebug() << "WOverview : m_markRanges" << m_markRanges.size(); } -void WOverview::setValue(double fValue) { +void WOverview::setValue(double dValue) { if (!m_bDrag) { // Calculate handle position - int iPos = valueToPosition(fValue); + int iPos = valueToPosition(dValue); if (iPos != m_iPos) { m_iPos = iPos; - //qDebug() << "WOverview::setValue" << fValue << ">>" << m_iPos; + //qDebug() << "WOverview::setValue" << dValue << ">>" << m_iPos; update(); } } @@ -247,14 +247,14 @@ void WOverview::mouseMoveEvent(QMouseEvent* e) { void WOverview::mouseReleaseEvent(QMouseEvent* e) { mouseMoveEvent(e); - float fValue = positionToValue(m_iPos); + double dValue = positionToValue(m_iPos); - //qDebug() << "WOverview::mouseReleaseEvent" << e->pos() << m_iPos << ">>" << fValue; + //qDebug() << "WOverview::mouseReleaseEvent" << e->pos() << m_iPos << ">>" << dValue; if (e->button() == Qt::RightButton) { - emit(valueChangedRightUp(fValue)); + emit(valueChangedRightUp(dValue)); } else { - emit(valueChangedLeftUp(fValue)); + emit(valueChangedLeftUp(dValue)); } m_bDrag = false; } @@ -466,9 +466,21 @@ void WOverview::paintText(const QString &text, QPainter *painter) { } void WOverview::resizeEvent(QResizeEvent *) { - //Those coeficient map position from [0;width-1] to value [14;114] - m_a = (float)((width()-1))/( 114.f - 14.f); - m_b = 14.f * m_a; + // Play-position potmeters range from -0.14 to 1.14. This is to give VC and + // MIDI control access to the pre-roll area. + // TODO(rryan): get these limits from the CO itself. + const double kMaxPlayposRange = 1.14; + const double kMinPlayposRange = -0.14; + + // Values of zero and one in normalized space. + const double zero = (0.0 - kMinPlayposRange) / (kMaxPlayposRange - kMinPlayposRange); + const double one = (1.0 - kMinPlayposRange) / (kMaxPlayposRange - kMinPlayposRange); + + // These coeficients convert between widget space and normalized value + // space. + m_a = (width() - 1) / (one - zero); + m_b = zero * m_a; + m_waveformImageScaled = QImage(); m_diffGain = 0; } diff --git a/src/widget/woverview.h b/src/widget/woverview.h index e5ffd81798..dd94276c44 100644 --- a/src/widget/woverview.h +++ b/src/widget/woverview.h @@ -84,11 +84,11 @@ class WOverview : public WWidget { // Append the waveform overview pixmap according to available data in waveform virtual bool drawNextPixmapPart() = 0; void paintText(const QString &text, QPainter *painter); - inline int valueToPosition(float value) const { - return static_cast(m_a * value - m_b + 0.5); + inline int valueToPosition(double value) const { + return static_cast(m_a * value - m_b); } inline double positionToValue(int position) const { - return (static_cast(position) + m_b) / m_a; + return (static_cast(position) + m_b) / m_a; } const QString m_group; @@ -116,8 +116,8 @@ class WOverview : public WWidget { std::vector m_markRanges; // Coefficient value-position linear transposition - float m_a; - float m_b; + double m_a; + double m_b; double m_dAnalyserProgress; bool m_bAnalyserFinalizing; diff --git a/src/widget/wslidercomposed.cpp b/src/widget/wslidercomposed.cpp index 189aa3b426..ced64e0506 100644 --- a/src/widget/wslidercomposed.cpp +++ b/src/widget/wslidercomposed.cpp @@ -101,16 +101,19 @@ void WSliderComposed::mouseMoveEvent(QMouseEvent * e) { int sliderLength = m_bHorizontal ? width() : height(); + // Clamp to the range [0, sliderLength - m_iHandleLength]. if (m_iPos > (sliderLength - m_iHandleLength)) { m_iPos = sliderLength - m_iHandleLength; } else if (m_iPos < 0) { m_iPos = 0; } - // value ranges from 0 to 127 - m_value = (double)m_iPos * (127. / (double)(sliderLength - m_iHandleLength)); + // Divide by (sliderLength - m_iHandleLength) to produce a normalized + // value in the range of [0.0, 1.0]. 1.0 + m_value = static_cast(m_iPos) / + static_cast(sliderLength - m_iHandleLength); if (!m_bHorizontal) { - m_value = 127. - m_value; + m_value = 1.0 - m_value; } // Emit valueChanged signal @@ -128,9 +131,14 @@ void WSliderComposed::mouseMoveEvent(QMouseEvent * e) { } void WSliderComposed::wheelEvent(QWheelEvent *e) { - double wheelDirection = ((QWheelEvent *)e)->delta() / 120.; - double newValue = getValue() + (wheelDirection); - this->updateValue(newValue); + // For legacy (MIDI) reasons this is tuned to 127. + double wheelDirection = ((QWheelEvent *)e)->delta() / (120.0 * 127.0); + double newValue = getValue() + wheelDirection; + + // Clamp to [0.0, 1.0] + newValue = math_max(0.0, math_min(1.0, newValue)); + + updateValue(newValue); e->accept(); @@ -192,18 +200,18 @@ void WSliderComposed::paintEvent(QPaintEvent *) { } } -void WSliderComposed::setValue(double fValue) { - if (!m_bDrag && m_value != fValue) { +void WSliderComposed::setValue(double dValue) { + if (!m_bDrag && m_value != dValue) { // Set value without emitting a valueChanged signal // and force display update - m_value = fValue; + m_value = dValue; // Calculate handle position if (!m_bHorizontal) { - fValue = 127-fValue; + dValue = 1.0 - dValue; } int sliderLength = m_bHorizontal ? width() : height(); - m_iPos = (int)((fValue / 127.) * (double)(sliderLength - m_iHandleLength)); + m_iPos = static_cast(dValue * (sliderLength - m_iHandleLength)); if (m_iPos > (sliderLength - m_iHandleLength)) { m_iPos = sliderLength - m_iHandleLength; diff --git a/src/widget/wvumeter.cpp b/src/widget/wvumeter.cpp index 363f114005..e099343619 100644 --- a/src/widget/wvumeter.cpp +++ b/src/widget/wvumeter.cpp @@ -103,9 +103,8 @@ void WVuMeter::setPixmaps(const QString &vuFilename, } } -void WVuMeter::setValue(double fValue) -{ - int idx = (int)(fValue * (float)(m_iNoPos)/128.); +void WVuMeter::setValue(double dValue) { + int idx = static_cast(dValue * m_iNoPos); // Range check if (idx > m_iNoPos) idx = m_iNoPos; @@ -113,7 +112,7 @@ void WVuMeter::setValue(double fValue) idx = 0; setPeak(idx); - m_value = fValue; + m_value = dValue; QTime currentTime = QTime::currentTime(); int msecsElapsed = m_lastUpdate.msecsTo(currentTime); @@ -160,7 +159,7 @@ void WVuMeter::paintEvent(QPaintEvent *) { } if (!m_pPixmapVu.isNull() && !m_pPixmapVu->isNull()) { - int idx = (int)(m_value*(float)(m_iNoPos)/128.); + int idx = static_cast(m_value * m_iNoPos); // Range check if (idx > m_iNoPos) diff --git a/src/widget/wwaveformviewer.cpp b/src/widget/wwaveformviewer.cpp index 1c5b842215..cf10c0a511 100644 --- a/src/widget/wwaveformviewer.cpp +++ b/src/widget/wwaveformviewer.cpp @@ -100,11 +100,16 @@ void WWaveformViewer::mouseMoveEvent(QMouseEvent* event) { m_pScratchPosition->slotSet(targetPosition); } else if (m_bBending) { QPoint diff = event->pos() - m_mouseAnchor; - // start at the middle of 0-127, and emit values based on - // how far the mouse has traveled horizontally - double v = 64.0 + diff.x()/10.0f; - // clamp to [0, 127] - v = math_min(127.0, math_max(0.0, v)); + // Start at the middle of [0.0, 1.0], and emit values based on how far + // the mouse has traveled horizontally. Note, for legacy (MIDI) reasons, + // this is tuned to 127. + // NOTE(rryan): This is basically a direct connection to the "wheel" + // control since we manually connect it in LegacySkinParser regardless + // of whether the skin specifies it. See ControlTTRotaryBehavior to see + // where this value is handled. + double v = 0.5 + (diff.x() / 1270.0); + // clamp to [0.0, 1.0] + v = math_min(1.0, math_max(0.0, v)); emit(valueChangedRightDown(v)); } } -- cgit v1.2.3