summaryrefslogtreecommitdiffstats
path: root/src/test/metaknob_link_test.cpp
blob: 15217ae7c842a7ad6bb19d9e0e99c2e5d846f7ff (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
#include <gtest/gtest.h>

#include <QtDebug>
#include <QScopedPointer>

#include "controllers/softtakeover.h"
#include "effects/effectparameterslot.h"
#include "effects/effect.h"
#include "mixxxtest.h"
#include "test/baseeffecttest.h"
#include "util/time.h"
#include "control/controlproxy.h"

class MetaLinkTest : public BaseEffectTest {
  protected:
    MetaLinkTest()
            : m_master(m_factory.getOrCreateHandle("[Master]"), "[Master]"),
              m_headphone(m_factory.getOrCreateHandle("[Headphone]"), "[Headphone]") {
        mixxx::Time::setTestMode(true);
        mixxx::Time::setTestElapsedTime(mixxx::Duration::fromNanos(0));
        m_pEffectsManager->registerChannel(m_master);
        m_pEffectsManager->registerChannel(m_headphone);
        registerTestBackend();

        EffectChainPointer pChain(new EffectChain(m_pEffectsManager.data(),
                                                  "org.mixxx.test.chain1"));
        int iRackNumber = 0;
        int iChainNumber = 0;
        int iEffectNumber = 0;

        StandardEffectRackPointer pRack = m_pEffectsManager->addStandardEffectRack();
        m_pChainSlot = pRack->addEffectChainSlot();
        m_pChainSlot->loadEffectChain(pChain);
        m_pEffectSlot = m_pChainSlot->getEffectSlot(0);

        QString group = StandardEffectRack::formatEffectSlotGroupString(
            iRackNumber, iChainNumber, iEffectNumber);

        EffectManifest manifest;
        manifest.setId("org.mixxx.test.effect");
        manifest.setName("Test Effect");

        EffectManifestParameter* low = manifest.addParameter();
        low->setId("low");
        low->setName(QObject::tr("Low"));
        low->setDescription(QObject::tr("Gain for Low Filter"));
        low->setControlHint(EffectManifestParameter::ControlHint::KNOB_LINEAR);
        low->setSemanticHint(EffectManifestParameter::SemanticHint::UNKNOWN);
        low->setUnitsHint(EffectManifestParameter::UnitsHint::UNKNOWN);
        low->setNeutralPointOnScale(0.25);
        low->setDefault(1.0);
        low->setMinimum(0);
        low->setMaximum(1.0);

        registerTestEffect(manifest, false);

        // Check the controls reflect the state of their loaded effect.
        EffectPointer pEffect = m_pEffectsManager->instantiateEffect(manifest.id());

        m_pEffectSlot->loadEffect(pEffect);

        QString itemPrefix = EffectParameterSlot::formatItemPrefix(0);

        m_pControlValue.reset(new ControlProxy(group, itemPrefix));

        m_pControlLinkType.reset(new ControlProxy(group,
                itemPrefix + QString("_link_type")));

        m_pControlLinkInverse.reset(new ControlProxy(group,
                itemPrefix + QString("_link_inverse")));
    }

    ChannelHandleFactory m_factory;
    ChannelHandleAndGroup m_master;
    ChannelHandleAndGroup m_headphone;

    EffectSlotPointer m_pEffectSlot;
    EffectChainSlotPointer m_pChainSlot;
    QScopedPointer<ControlProxy> m_pControlValue;
    QScopedPointer<ControlProxy> m_pControlLinkType;
    QScopedPointer<ControlProxy> m_pControlLinkInverse;
};

TEST_F(MetaLinkTest, LinkDefault) {
    // default is not Linked, value must be unchanged
    m_pEffectSlot->syncSofttakeover();
    EXPECT_EQ(1.0, m_pControlValue->get());
    m_pEffectSlot->slotEffectMetaParameter(1.0);
    EXPECT_EQ(1.0, m_pControlValue->get());
    m_pEffectSlot->slotEffectMetaParameter(0.5);
    EXPECT_EQ(1.0, m_pControlValue->get());
    m_pEffectSlot->slotEffectMetaParameter(0.3);
    EXPECT_EQ(1.0, m_pControlValue->get());
}

TEST_F(MetaLinkTest, LinkLinked) {
    m_pEffectSlot->syncSofttakeover();
    m_pControlLinkType->set(
        static_cast<double>(EffectManifestParameter::LinkType::LINKED));
    m_pEffectSlot->slotEffectMetaParameter(1.0);
    EXPECT_EQ(1.0, m_pControlValue->get());
    m_pEffectSlot->slotEffectMetaParameter(0.5);
    EXPECT_EQ(0.25, m_pControlValue->get());
}

TEST_F(MetaLinkTest, LinkLinkedInverse) {
    m_pEffectSlot->syncSofttakeover();
    m_pControlLinkType->set(
        static_cast<double>(EffectManifestParameter::LinkType::LINKED));
    m_pControlLinkInverse->set(1.0);
    m_pEffectSlot->slotEffectMetaParameter(0.0);
    EXPECT_EQ(1.0, m_pControlValue->get());
    m_pEffectSlot->slotEffectMetaParameter(0.5);
    EXPECT_EQ(0.25, m_pControlValue->get());
}

TEST_F(MetaLinkTest, MetaToParameter_Softtakeover_EffectEnabled) {
    m_pControlLinkType->set(
        static_cast<double>(EffectManifestParameter::LinkType::LINKED));
    // Soft takeover should only occur when the effect is enabled.
    m_pEffectSlot->slotEnabled(1.0);

    // Soft takeover always ignores the first change.
    m_pEffectSlot->slotEffectMetaParameter(0.5);
    EXPECT_EQ(1.0, m_pControlValue->get());

    m_pControlValue->set(0.0);

    // Let enough time pass by to exceed soft-takeover's override interval.
    mixxx::Time::setTestElapsedTime(SoftTakeover::TestAccess::getTimeThreshold() +
                             mixxx::Duration::fromMillis(2));

    // Ignored by SoftTakeover since it is too far from the current
    // parameter value of 0.0.
    m_pEffectSlot->slotEffectMetaParameter(1.0);
    EXPECT_EQ(0.0, m_pControlValue->get());
}

TEST_F(MetaLinkTest, MetaToParameter_Softtakeover_EffectDisabled) {
    // Soft takeover should not occur when the effect is disabled;
    // parameter values should always jump to match the metaknob.
    // Effects are disabled by default.
    m_pControlLinkType->set(
        static_cast<double>(EffectManifestParameter::LinkType::LINKED));

    m_pEffectSlot->slotEffectMetaParameter(1.0);
    EXPECT_EQ(1.0, m_pControlValue->get());

    m_pControlValue->set(0.0);

    // Let enough time pass by to exceed soft-takeover's override interval.
    mixxx::Time::setTestElapsedTime(SoftTakeover::TestAccess::getTimeThreshold() +
                             mixxx::Duration::fromMillis(2));

    m_pEffectSlot->slotEffectMetaParameter(1.0);
    EXPECT_EQ(1.0, m_pControlValue->get());
}

TEST_F(MetaLinkTest, SuperToMeta_Softtakeover_EffectEnabled) {
    // Soft takeover should only occur when the effect is enabled.
    m_pEffectSlot->slotEnabled(1.0);

    // Soft takeover always ignores the first change.
    m_pChainSlot->setSuperParameter(0.5);
    EXPECT_EQ(0.0, m_pEffectSlot->getMetaParameter());

    m_pEffectSlot->setMetaParameter(1.0, true);

    // Let enough time pass by to exceed soft-takeover's override interval.
    mixxx::Time::setTestElapsedTime(SoftTakeover::TestAccess::getTimeThreshold() +
                             mixxx::Duration::fromMillis(2));

    // Ignored by SoftTakeover since it is too far from the current
    // metaknob value of 1.0.
    m_pChainSlot->setSuperParameter(0.5);
    EXPECT_EQ(1.0, m_pEffectSlot->getMetaParameter());
}

TEST_F(MetaLinkTest, SuperToMeta_Softtakeover_EffectDisabled) {
    // Soft takeover should not occur when the effect is disabled;
    // metaknob values should always jump to match the superknob.
    // Effects are disabled by default.
    m_pChainSlot->setSuperParameter(0.5);
    EXPECT_EQ(0.5, m_pEffectSlot->getMetaParameter());

    m_pEffectSlot->setMetaParameter(1.0, true);

    // Let enough time pass by to exceed soft-takeover's override interval.
    mixxx::Time::setTestElapsedTime(SoftTakeover::TestAccess::getTimeThreshold() +
                             mixxx::Duration::fromMillis(2));

    m_pChainSlot->setSuperParameter(0.0);
    EXPECT_EQ(0.0, m_pEffectSlot->getMetaParameter());
}

TEST_F(MetaLinkTest, HalfLinkTakeover) {
    // An effect that is linked to half of a knob should be more tolerant of
    // takeover changes.

    m_pEffectSlot->slotEnabled(1.0);

    // We have to recreate the effect because we want a neutral point at
    // 0 or 1.
    QString group = StandardEffectRack::formatEffectSlotGroupString(
        0, 0, 0);
    EffectManifest manifest;
    manifest.setId("org.mixxx.test.effect2");
    manifest.setName("Test Effect2");
    EffectManifestParameter* low = manifest.addParameter();
    low->setId("low");
    low->setName(QObject::tr("Low"));
    low->setDescription(QObject::tr("Gain for Low Filter (neutral at 1.0)"));
    low->setControlHint(EffectManifestParameter::ControlHint::KNOB_LINEAR);
    low->setSemanticHint(EffectManifestParameter::SemanticHint::UNKNOWN);
    low->setUnitsHint(EffectManifestParameter::UnitsHint::UNKNOWN);
    low->setNeutralPointOnScale(1.0);
    low->setDefault(1.0);
    low->setMinimum(0);
    low->setMaximum(1.0);
    registerTestEffect(manifest, false);
    // Check the controls reflect the state of their loaded effect.
    EffectPointer pEffect = m_pEffectsManager->instantiateEffect(manifest.id());
    m_pEffectSlot->loadEffect(pEffect);
    QString itemPrefix = EffectParameterSlot::formatItemPrefix(0);
    m_pControlValue.reset(new ControlProxy(group, itemPrefix));
    m_pControlLinkType.reset(new ControlProxy(group,
            itemPrefix + QString("_link_type")));
    m_pControlLinkInverse.reset(new ControlProxy(group,
            itemPrefix + QString("_link_inverse")));

    // OK now the actual test.
    // 1.5 is a bit of a magic number, but it's enough that a regular
    // linked control will fail the soft takeover test and not change the
    // value...
    double newParam = 0.5 - SoftTakeover::kDefaultTakeoverThreshold * 1.5;
    m_pEffectSlot->slotEffectMetaParameter(newParam);
    EXPECT_EQ(1.0, m_pControlValue->get());

    // ...but that value is still within the tolerance of a linked-left
    // and linked-right control.  So if we set the exact same newParam,
    // we should see the control change as expected.
    m_pEffectSlot->slotEffectMetaParameter(0.5);
    m_pControlValue->set(1.0);
    m_pControlLinkType->set(
        static_cast<double>(EffectManifestParameter::LinkType::LINKED_LEFT));
    m_pEffectSlot->syncSofttakeover();
    m_pEffectSlot->slotEffectMetaParameter(newParam);
    EXPECT_EQ(newParam * 2.0, m_pControlValue->get());

    // This tolerance change should also work for linked-right controls.
    m_pEffectSlot->slotEffectMetaParameter(0.5);
    m_pControlValue->set(1.0);
    m_pControlLinkType->set(
        static_cast<double>(EffectManifestParameter::LinkType::LINKED_RIGHT));
    m_pEffectSlot->syncSofttakeover();
    newParam = 0.5 + SoftTakeover::kDefaultTakeoverThreshold * 1.5;
    m_pEffectSlot->slotEffectMetaParameter(newParam);
    EXPECT_FLOAT_EQ(0.0703125, m_pControlValue->get());
}