Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Visualize distance to a key when pitch shifting #215

Merged
merged 9 commits into from
Apr 11, 2014
8 changes: 6 additions & 2 deletions src/engine/keycontrol.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <QtDebug>
#include <QPair>

#include "engine/keycontrol.h"

Expand Down Expand Up @@ -43,6 +44,8 @@ KeyControl::KeyControl(const char* pGroup,
this, SLOT(slotSetEngineKey(double)),
Qt::DirectConnection);

m_pEngineKeyDistance = new ControlPotmeter(ConfigKey(pGroup, "visual_key_distance"), -0.5, 0.5);

m_pRateSlider = ControlObject::getControl(ConfigKey(pGroup, "rate"));
connect(m_pRateSlider, SIGNAL(valueChanged(double)),
this, SLOT(slotRateChanged()),
Expand Down Expand Up @@ -146,10 +149,11 @@ void KeyControl::slotFileKeyChanged(double value) {
pitch_adjust += KeyUtils::powerOf2ToOctaveChange(m_dOldRate);
}

mixxx::track::io::key::ChromaticKey adjusted =
QPair<mixxx::track::io::key::ChromaticKey, double> adjusted =
KeyUtils::scaleKeyOctaves(key, pitch_adjust);

m_pEngineKey->set(KeyUtils::keyToNumericValue(adjusted));
m_pEngineKey->set(KeyUtils::keyToNumericValue(adjusted.first));
m_pEngineKeyDistance->set(adjusted.second);
}

void KeyControl::slotSetEngineKey(double key) {
Expand Down
1 change: 1 addition & 0 deletions src/engine/keycontrol.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class KeyControl : public EngineControl {

/** The current effective key of the engine */
ControlObject* m_pEngineKey;
ControlPotmeter* m_pEngineKeyDistance;

TrackPointer m_pTrack;
};
Expand Down
10 changes: 9 additions & 1 deletion src/skin/legacyskinparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ QList<QWidget*> LegacySkinParser::parseNode(QDomElement node) {
} else if (nodeName == "Library") {
result = wrapWidget(parseLibrary(node));
} else if (nodeName == "Key") {
result = wrapWidget(parseLabelWidget<WKey>(node));
result = wrapWidget(parseEngineKey(node));
} else if (nodeName == "SetVariable") {
m_pContext->updateVariable(node);
} else if (nodeName == "Template") {
Expand Down Expand Up @@ -872,6 +872,14 @@ QWidget* LegacySkinParser::parseNumberPos(QDomElement node) {
return p;
}

QWidget* LegacySkinParser::parseEngineKey(QDomElement node) {
QString channelStr = lookupNodeGroup(node);
const char* pSafeChannelStr = safeChannelString(channelStr);
WKey* pEngineKey = new WKey(pSafeChannelStr, m_pParent);
setupLabelWidget(node, pEngineKey);
return pEngineKey;
}

QWidget* LegacySkinParser::parseSpinny(QDomElement node) {
QString channelStr = lookupNodeGroup(node);
const char* pSafeChannelStr = safeChannelString(channelStr);
Expand Down
1 change: 1 addition & 0 deletions src/skin/legacyskinparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class LegacySkinParser : public QObject, public SkinParser {
QWidget* parseTrackProperty(QDomElement node);
QWidget* parseNumberRate(QDomElement node);
QWidget* parseNumberPos(QDomElement node);
QWidget* parseEngineKey(QDomElement node);
QWidget* parseEffectChainName(QDomElement node);
QWidget* parseEffectName(QDomElement node);
QWidget* parseEffectParameterName(QDomElement node);
Expand Down
1 change: 0 additions & 1 deletion src/skin/tooltips.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#include "skin/tooltips.h"

Tooltips::Tooltips() {
Expand Down
11 changes: 7 additions & 4 deletions src/track/keyutils.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include <QtDebug>
#include <QMap>
#include <QRegExp>
#include <QMutexLocker>
#include <QPair>
#include <QRegExp>

#include "track/keyutils.h"
#include "mathstuff.h"
Expand Down Expand Up @@ -233,15 +234,17 @@ double KeyUtils::keyToNumericValue(ChromaticKey key) {
}

// static
ChromaticKey KeyUtils::scaleKeyOctaves(ChromaticKey key, double octave_change) {
QPair<ChromaticKey, double> KeyUtils::scaleKeyOctaves(ChromaticKey key, double octave_change) {
// Convert the octave_change from percentage of octave to the nearest
// integer of key changes. We need the rounding to be in the same direction
// so that a -1.0 and 1.0 scale of C makes it back to C.
double key_changes_scaled = octave_change * 12;
int key_changes = int(key_changes_scaled +
int key_changes = static_cast<int>(key_changes_scaled +
(key_changes_scaled > 0 ? 0.5 : -0.5));

return scaleKeySteps(key, key_changes);
// Distance to the nearest key
double diff_to_key = key_changes_scaled - key_changes;
return QPair<ChromaticKey, double>(scaleKeySteps(key, key_changes), diff_to_key);
}

// static
Expand Down
2 changes: 1 addition & 1 deletion src/track/keyutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class KeyUtils {

static double keyToNumericValue(mixxx::track::io::key::ChromaticKey key);

static mixxx::track::io::key::ChromaticKey scaleKeyOctaves(
static QPair<mixxx::track::io::key::ChromaticKey, double> scaleKeyOctaves(
mixxx::track::io::key::ChromaticKey key, double scale);

static mixxx::track::io::key::ChromaticKey scaleKeySteps(
Expand Down
40 changes: 35 additions & 5 deletions src/widget/wkey.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#include "widget/wkey.h"

#include "track/keyutils.h"
#include "track/keys.h"
#include "track/keyutils.h"

WKey::WKey(QWidget* pParent)
WKey::WKey(const char* group, QWidget* pParent)
: WLabel(pParent),
m_dOldValue(0),
m_preferencesUpdated(ConfigKey("[Preferences]", "updated")) {
m_preferencesUpdated(ConfigKey("[Preferences]", "updated")),
m_engineKeyDistance(ConfigKey(group, "visual_key_distance")) {
setValue(m_dOldValue);
connect(&m_preferencesUpdated, SIGNAL(valueChanged(double)),
this, SLOT(preferencesUpdated(double)));
connect(&m_engineKeyDistance, SIGNAL(valueChanged(double)),
this, SLOT(setCents()));
}

WKey::~WKey() {
Expand All @@ -20,13 +22,22 @@ void WKey::onConnectedControlChanged(double dParameter, double dValue) {
// Enums are not currently represented using parameter space so it doesn't
// make sense to use the parameter here yet.
setValue(dValue);
setCents();
}

void WKey::setup(QDomNode node, const SkinContext& context) {
WLabel::setup(node, context);
if (context.selectBool(node, "DisplayCents", false)) {
m_displayCents = true;
} else {
m_displayCents = false;
}
}

void WKey::setValue(double dValue) {
m_dOldValue = dValue;
mixxx::track::io::key::ChromaticKey key =
KeyUtils::keyFromNumericValue(dValue);

if (key != mixxx::track::io::key::INVALID) {
// Render this key with the user-provided notation.
setText(KeyUtils::keyToString(key));
Expand All @@ -35,6 +46,25 @@ void WKey::setValue(double dValue) {
}
}

void WKey::setCents() {
if (m_displayCents) {
double diff_cents = m_engineKeyDistance.get();
int cents_to_display = static_cast<int>(diff_cents * 100);
char sign;
if (diff_cents < 0) {
sign = '-';
} else {
sign = '+';
}
// Remove the previous cent difference
QString old = text();
if (old.contains(' ')) {
old = old.section(' ', 0, 0);
}
setText(old + QString(" %1%2c").arg(sign).arg(qAbs(cents_to_display)));
}
}

void WKey::preferencesUpdated(double dValue) {
if (dValue > 0) {
setValue(m_dOldValue);
Expand Down
6 changes: 5 additions & 1 deletion src/widget/wkey.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,22 @@
class WKey : public WLabel {
Q_OBJECT
public:
WKey(QWidget* pParent=NULL);
WKey(const char* group, QWidget* pParent=NULL);
virtual ~WKey();

virtual void onConnectedControlChanged(double dParameter, double dValue);
void setup(QDomNode node, const SkinContext& context);

private slots:
void setValue(double dValue);
void preferencesUpdated(double dValue);
void setCents();

private:
double m_dOldValue;
bool m_displayCents;
ControlObjectThread m_preferencesUpdated;
ControlObjectThread m_engineKeyDistance;
};

#endif /* WKEY_H */