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 @@ -38,6 +39,8 @@ KeyControl::KeyControl(const char* pGroup,
this, SLOT(slotSetEngineKey(double)),
Qt::DirectConnection);

m_pEngineKeyDistance = new ControlObject(ConfigKey(pGroup, "visual_key_distance"));

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

mixxx::track::io::key::ChromaticKey adjusted =
QPair<mixxx::track::io::key::ChromaticKey, int> 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 @@ -50,6 +50,7 @@ class KeyControl : public EngineControl {

/** The current effective key of the engine */
ControlObject* m_pEngineKey;
ControlObject* 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 @@ -440,7 +440,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 @@ -882,6 +882,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 @@ -69,6 +69,7 @@ class LegacySkinParser : public QObject, public SkinParser {
QWidget* parseTrackProperty(QDomElement node);
QWidget* parseNumberRate(QDomElement node);
QWidget* parseNumberPos(QDomElement node);
QWidget* parseEngineKey(QDomElement node);

// Legacy pre-1.12.0 skin support.
QWidget* parseBackground(QDomElement node, QWidget* pOuterWidget, QWidget* pInnerWidget);
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, int> 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 in cents
int cents = static_cast<int>((key_changes_scaled - key_changes) * 100);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of providing cents and rounding off the precision I think you should just keep this as a fraction in floating point.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(because units and round-off should be decided by the caller of this utility function)

return QPair<ChromaticKey, int>(scaleKeySteps(key, key_changes), cents);
}

// 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, int> scaleKeyOctaves(
mixxx::track::io::key::ChromaticKey key, double scale);

static mixxx::track::io::key::ChromaticKey scaleKeySteps(
Expand Down
26 changes: 23 additions & 3 deletions src/widget/wkey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,30 @@
#include "track/keyutils.h"
#include "track/keys.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() {
}

void WKey::onConnectedControlValueChanged(double v) {
setValue(v);
setCents();
}

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 @@ -32,6 +35,23 @@ void WKey::setValue(double dValue) {
}
}

void WKey::setCents() {
int diff_cents = m_engineKeyDistance.get();
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%2 c)").arg(sign).arg(diff_cents));
}


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

virtual void onConnectedControlValueChanged(double v);

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

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

#endif /* WKEY_H */