summaryrefslogtreecommitdiffstats
path: root/src/engine/keycontrol.cpp
blob: 2d648e4f39088218b593b759326f57712e5e3b5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include <QtDebug>
#include <QPair>

#include "engine/keycontrol.h"

#include "control/controlobject.h"
#include "control/controlpushbutton.h"
#include "control/controlpotmeter.h"
#include "engine/enginebuffer.h"
#include "track/keyutils.h"

//static const double kLockOriginalKey = 0;
static const double kLockCurrentKey = 1;
static const double kKeepUnlockedKey = 1;

KeyControl::KeyControl(QString group,
                       UserSettingsPointer pConfig)
        : EngineControl(group, pConfig) {
    m_pitchRateInfo.pitchRatio = 1.0;
    m_pitchRateInfo.tempoRatio = 1.0;
    m_pitchRateInfo.pitchTweakRatio = 1.0;
    m_pitchRateInfo.keylock = false;

    // pitch is the distance to the original pitch in semitones
    // knob in semitones; 9.4 ct per midi step allowOutOfBounds = true;
    m_pPitch = new ControlPotmeter(ConfigKey(group, "pitch"), -6.0, 6.0, true);
    // Coarse adjust by full semitone steps.
    m_pPitch->setStepCount(12);
    // Fine adjust with semitone / 10 = 10 ct;.
    m_pPitch->setSmallStepCount(120);
    connect(m_pPitch, &ControlObject::valueChanged,
            this, &KeyControl::slotPitchChanged,
            Qt::DirectConnection);

    // pitch_adjust is the distance to the linear pitch in semitones
    // set by the speed slider or to the locked key.
    // pitch_adjust knob in semitones; 4.7 ct per midi step; allowOutOfBounds = true;
    m_pPitchAdjust = new ControlPotmeter(ConfigKey(group, "pitch_adjust"), -3.0, 3.0, true);
    // Coarse adjust by full semitone steps.
    m_pPitchAdjust->setStepCount(6);
    // Fine adjust with semitone / 10 = 10 ct;.
    m_pPitchAdjust->setSmallStepCount(60);
    connect(m_pPitchAdjust, &ControlObject::valueChanged,
            this, &KeyControl::slotPitchAdjustChanged,
            Qt::DirectConnection);

    m_pButtonSyncKey = new ControlPushButton(ConfigKey(group, "sync_key"));
    connect(m_pButtonSyncKey, &ControlObject::valueChanged,
            this, &KeyControl::slotSyncKey,
            Qt::DirectConnection);

    m_pButtonResetKey = new ControlPushButton(ConfigKey(group, "reset_key"));
    connect(m_pButtonResetKey, &ControlObject::valueChanged,
            this, &KeyControl::slotResetKey,
            Qt::DirectConnection);

    m_pFileKey = new ControlObject(ConfigKey(group, "file_key"));
    connect(m_pFileKey, &ControlObject::valueChanged,
            this, &KeyControl::slotFileKeyChanged,
            Qt::DirectConnection);

    m_pEngineKey = new ControlObject(ConfigKey(group, "key"));
    connect(m_pEngineKey, &ControlObject::valueChanged,
            this, &KeyControl::slotSetEngineKey,
            Qt::DirectConnection);

    m_pEngineKeyDistance = new ControlPotmeter(ConfigKey(group, "visual_key_distance"),
                                               -0.5, 0.5);
    connect(m_pEngineKeyDistance, &ControlObject::valueChanged,
            this, &KeyControl::slotSetEngineKeyDistance,
            Qt::DirectConnection);

    m_keylockMode = new ControlPushButton(ConfigKey(group, "keylockMode"));
    m_keylockMode->setButtonMode(ControlPushButton::TOGGLE);

    m_keyunlockMode = new ControlPushButton(ConfigKey(group, "keyunlockMode"));
    m_keyunlockMode->setButtonMode(ControlPushButton::TOGGLE);

    // In case of vinyl control "rate" is a filtered mean value for display
    m_pRateSlider = new ControlProxy(group, "rate", this);
    m_pRateSlider->connectValueChanged(this, &KeyControl::slotRateChanged,
            Qt::DirectConnection);

    m_pRateRange = new ControlProxy(group, "rateRange", this);
    m_pRateRange->connectValueChanged(this, &KeyControl::slotRateChanged,
            Qt::DirectConnection);

    m_pRateDir = new ControlProxy(group, "rate_dir", this);
    m_pRateDir->connectValueChanged(this, &KeyControl::slotRateChanged,
            Qt::DirectConnection);

    m_pVCEnabled = new ControlProxy(group, "vinylcontrol_enabled", this);
    m_pVCEnabled->connectValueChanged(this, &KeyControl::slotRateChanged,
            Qt::DirectConnection);

    m_pVCRate = new ControlProxy(group, "vinylcontrol_rate", this);
    m_pVCRate->connectValueChanged(this, &KeyControl::slotRateChanged,
                Qt::DirectConnection);

    m_pKeylock = new ControlProxy(group, "keylock", this);
    m_pKeylock->connectValueChanged(this, &KeyControl::slotRateChanged,
            Qt::DirectConnection);
}

KeyControl::~KeyControl() {
    delete m_pPitch;
    delete m_pPitchAdjust;
    delete m_pButtonSyncKey;
    delete m_pButtonResetKey;
    delete m_pFileKey;
    delete m_pEngineKey;
    delete m_pEngineKeyDistance;
    delete m_keylockMode;
    delete m_keyunlockMode;
}

KeyControl::PitchTempoRatio KeyControl::getPitchTempoRatio() {
    // TODO(XXX) remove code duplication by adding this
    // "Update pending" atomic flag to the ControlObject API
    if (m_updatePitchRequest.fetchAndStoreAcquire(0)) {
        updatePitch();
    }
    if (m_updatePitchAdjustRequest.fetchAndStoreAcquire(0)) {
        updatePitchAdjust();
    }
    if (m_updateRateRequest.fetchAndStoreAcquire(0)) {
        updateRate();
    }
    return m_pitchRateInfo;
}

double KeyControl::getKey() {
    return m_pEngineKey->get();
}

void KeyControl::slotRateChanged() {
    m_updateRateRequest = 1;
    updateRate();
}

void KeyControl::updateRate() {
    //qDebug() << "KeyControl::slotRateChanged 1" << m_pitchRateInfo.pitchRatio;

    // If rate is not 1.0 then we have to try and calculate the octave change
    // caused by it.

    if(m_pVCEnabled->toBool()) {
        m_pitchRateInfo.tempoRatio = m_pVCRate->get();
    } else {
        m_pitchRateInfo.tempoRatio = 1.0
                + m_pRateDir->get() * m_pRateRange->get()
                        * m_pRateSlider->get();
    }

    if (m_pitchRateInfo.tempoRatio == 0) {
        // no transport, no pitch
        // so we can skip pitch calculation
        return;
    }


    // |-----------------------|-----------------|
    //   SpeedSliderPitchRatio   pitchTweakRatio
    //
    // |-----------------------------------------|
    //   m_pitchRatio
    //
    //                         |-----------------|
    //                           m_pPitchAdjust
    //
    // |-----------------------------------------|
    //   m_pPitch

    double speedSliderPitchRatio =
            m_pitchRateInfo.pitchRatio / m_pitchRateInfo.pitchTweakRatio;

    if (m_pKeylock->toBool()) {
        if (!m_pitchRateInfo.keylock) {
            // Enabling Keylock
            if (m_keylockMode->get() == kLockCurrentKey) {
                // Lock at current pitch
                speedSliderPitchRatio = m_pitchRateInfo.tempoRatio;
            } else {
                // kOffsetScaleLockOriginalKey
                // Lock at original track pitch
                speedSliderPitchRatio = 1.0;
            }
            m_pitchRateInfo.keylock = true;
        }
    } else {
        // !bKeylock
        if (m_pitchRateInfo.keylock) {
            // Disabling Keylock
            // If "Keep unlocked key" is enabled
            if (m_keyunlockMode->get() == kKeepUnlockedKey) {
                // don't reset to linear pitch, instead adopt speedSliderPitchRatio
                // change as pitchTweakRatio
                m_pitchRateInfo.pitchTweakRatio *=
                 (speedSliderPitchRatio / m_pitchRateInfo.tempoRatio);
                // adopt pitch_adjust now so that it doesn't jump and resets key
                // when touching pitch_adjust knob after unlock with offset key
                m_pPitchAdjust->set(
                    KeyUtils::powerOf2ToSemitoneChange(m_pitchRateInfo.pitchTweakRatio));
            } else {
                // If 'current' aka 'not original' key was locked
                if (m_keylockMode->get() == kLockCurrentKey) {
                    // reset to linear pitch
                    m_pitchRateInfo.pitchTweakRatio = 1.0;
                }
            }
            m_pitchRateInfo.keylock = false;
        }
        speedSliderPitchRatio = m_pitchRateInfo.tempoRatio;
    }

    m_pitchRateInfo.pitchRatio = m_pitchRateInfo.pitchTweakRatio * speedSliderPitchRatio;

    double pitchOctaves = KeyUtils::powerOf2ToOctaveChange(m_pitchRateInfo.pitchRatio);
    double dFileKey = m_pFileKey->get();
    updateKeyCOs(dFileKey, pitchOctaves);

    // qDebug() << "KeyControl::slotRateChanged 2" << m_pitchRatio << m_speedSliderPitchRatio;
}

void KeyControl::slotFileKeyChanged(double value) {
    updateKeyCOs(value,  m_pPitch->get() / 12);
}

void KeyControl::updateKeyCOs(double fileKeyNumeric, double pitchOctaves) {
    //qDebug() << "updateKeyCOs 1" << pitchOctaves;
    mixxx::track::io::key::ChromaticKey fileKey =
            KeyUtils::keyFromNumericValue(fileKeyNumeric);

    QPair<mixxx::track::io::key::ChromaticKey, double> adjusted =
            KeyUtils::scaleKeyOctaves(fileKey, pitchOctaves);
    m_pEngineKey->set(KeyUtils::keyToNumericValue(adjusted.first));
    double diff_to_nearest_full_key = adjusted.second;
    m_pEngineKeyDistance->set(diff_to_nearest_full_key);
    m_pPitch->set(pitchOctaves * 12);
    //qDebug() << "updateKeyCOs 2" << diff_to_nearest_full_key;
}

void KeyControl::slotSetEngineKey(double key) {
    // Always set to a full key, reset key_distance
    setEngineKey(key, 0.0);
}

void KeyControl::slotSetEngineKeyDistance(double key_distance) {