summaryrefslogtreecommitdiffstats
path: root/src/effects/builtin/tremoloeffect.cpp
blob: 616cac0f345089270620c359b13ab588e4e99f75 (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
#include "effects/builtin/tremoloeffect.h"

namespace {
//  Used to avoid gain discontinuities when changing parameters too fast
constexpr double kMaxGainIncrement = 0.001;
}

// static
QString TremoloEffect::getId() {
    return "org.mixxx.effects.tremolo";
}

// static
EffectManifestPointer TremoloEffect::getManifest() {
    EffectManifestPointer pManifest(new EffectManifest());
    pManifest->setId(getId());
    pManifest->setName(QObject::tr("Tremolo"));
    pManifest->setShortName(QObject::tr("Tremolo"));
    pManifest->setAuthor("The Mixxx Team");
    pManifest->setVersion("1.0");
    pManifest->setDescription(QObject::tr(
        "Cycles the volume up and down"));
    pManifest->setMetaknobDefault(1.0);

    EffectManifestParameterPointer depth = pManifest->addParameter();
    depth->setId("depth");
    depth->setName(QObject::tr("Depth"));
    depth->setShortName(QObject::tr("Depth"));
    depth->setDescription(QObject::tr(
        "How much the effect changes the volume"));
    depth->setControlHint(EffectManifestParameter::ControlHint::KNOB_LINEAR);
    depth->setSemanticHint(EffectManifestParameter::SemanticHint::UNKNOWN);
    depth->setUnitsHint(EffectManifestParameter::UnitsHint::UNKNOWN);
    depth->setDefaultLinkType(EffectManifestParameter::LinkType::LINKED);
    depth->setDefault(1);
    depth->setMinimum(0);
    depth->setMaximum(1);

    EffectManifestParameterPointer rate = pManifest->addParameter();
    rate->setId("rate");
    rate->setName(QObject::tr("Rate"));
    rate->setShortName(QObject::tr("Rate"));
    rate->setDescription(QObject::tr(
        "Rate of the volume changes\n"
        "4 beats - 1/8 beat if tempo is detected\n"
        "1/4 Hz - 8 Hz if no tempo is detected"));
    rate->setControlHint(
        EffectManifestParameter::ControlHint::KNOB_LOGARITHMIC);
    rate->setSemanticHint(EffectManifestParameter::SemanticHint::UNKNOWN);
    rate->setUnitsHint(EffectManifestParameter::UnitsHint::BEATS);
    rate->setDefault(1);
    rate->setMinimum(1.0/4);
    rate->setMaximum(8);

    EffectManifestParameterPointer width = pManifest->addParameter();
    width->setId("width");
    width->setName(QObject::tr("Width"));
    width->setShortName(QObject::tr("Width"));
    width->setDescription(QObject::tr(
        "Width of the volume peak\n"
        "10% - 90% of the effect period"));
    width->setControlHint(EffectManifestParameter::ControlHint::KNOB_LINEAR);
    width->setSemanticHint(EffectManifestParameter::SemanticHint::UNKNOWN);
    width->setUnitsHint(EffectManifestParameter::UnitsHint::UNKNOWN);
    width->setMinimum(0.1);
    width->setDefault(0.5);
    width->setMaximum(0.9);

    EffectManifestParameterPointer waveform = pManifest->addParameter();
    waveform->setId("waveform");
    waveform->setName(QObject::tr("Waveform"));
    waveform->setShortName(QObject::tr("Waveform"));
    waveform->setDescription(QObject::tr(
        "Shape of the volume modulation wave\n"
        "Fully left: Square wave\n"
        "Fully right: Sine wave"));
    waveform->setControlHint(
        EffectManifestParameter::ControlHint::KNOB_LOGARITHMIC);
    waveform->setSemanticHint(EffectManifestParameter::SemanticHint::UNKNOWN);
    waveform->setUnitsHint(EffectManifestParameter::UnitsHint::UNKNOWN);
    waveform->setMinimum(0.005);
    waveform->setDefault(0.5);
    waveform->setMaximum(1);

    EffectManifestParameterPointer phase = pManifest->addParameter();
    phase->setId("phase");
    phase->setName(QObject::tr("Phase"));
    phase->setShortName(QObject::tr("Phase"));
    phase->setDescription(QObject::tr(
        "Shifts the position of the volume peak within the period\n"
        "Fully left: beginning of the effect period\n"
        "Fully right: end of the effect period"));
    phase->setControlHint(
        EffectManifestParameter::ControlHint::KNOB_LINEAR);
    phase->setSemanticHint(EffectManifestParameter::SemanticHint::UNKNOWN);
    phase->setUnitsHint(EffectManifestParameter::UnitsHint::UNKNOWN);
    phase->setDefault(0);
    phase->setMinimum(0);
    phase->setMaximum(1);

    EffectManifestParameterPointer quantize = pManifest->addParameter();
    quantize->setId("quantize");
    quantize->setName(QObject::tr("Quantize"));
    quantize->setShortName(QObject::tr("Quantize"));
    quantize->setDescription(QObject::tr(
        "Round the Rate parameter to the nearest whole division of a beat."));
    quantize->setControlHint(
        EffectManifestParameter::ControlHint::TOGGLE_STEPPING);
    quantize->setSemanticHint(EffectManifestParameter::SemanticHint::UNKNOWN);
    quantize->setUnitsHint(EffectManifestParameter::UnitsHint::UNKNOWN);
    quantize->setDefault(1);
    quantize->setMinimum(0);
    quantize->setMaximum(1);

    EffectManifestParameterPointer triplet = pManifest->addParameter();
    triplet->setId("triplet");
    triplet->setName(QObject::tr("Triplets"));
    triplet->setShortName(QObject::tr("Triplet"));
    triplet->setDescription(QObject::tr(
        "When the Quantize parameter is enabled, divide the effect period by 3."));
    triplet->setControlHint(
        EffectManifestParameter::ControlHint::TOGGLE_STEPPING);
    triplet->setSemanticHint(EffectManifestParameter::SemanticHint::UNKNOWN);
    triplet->setUnitsHint(EffectManifestParameter::UnitsHint::UNKNOWN);
    triplet->setDefault(0);
    triplet->setMinimum(0);
    triplet->setMaximum(1);

    return pManifest;
}

TremoloEffect::TremoloEffect(EngineEffect* pEffect)
        : m_pDepthParameter(pEffect->getParameterById("depth")),
          m_pRateParameter(pEffect->getParameterById("rate")),
          m_pWidthParameter(pEffect->getParameterById("width")),
          m_pWaveformParameter(pEffect->getParameterById("waveform")),
          m_pPhaseParameter(pEffect->getParameterById("phase")),
          m_pQuantizeParameter(pEffect->getParameterById("quantize")),
          m_pTripletParameter(pEffect->getParameterById("triplet")) {
}

void TremoloEffect::processChannel(const ChannelHandle& handle,
                                   TremoloState* pState,
                                   const CSAMPLE* pInput, CSAMPLE* pOutput,
                                   const mixxx::EngineParameters& bufferParameters,
                                   const EffectEnableState enableState,
                                   const GroupFeatureState& groupFeatures) {
    Q_UNUSED(handle);

    const double width = m_pWidthParameter->value();
    const double smooth = m_pWaveformParameter->value();
    const double depth = m_pDepthParameter->value();

    unsigned int currentFrame = pState->currentFrame;
    double gain = pState->gain;

    const GroupFeatureState& gf = groupFeatures;

    bool quantizeEnabling = !pState->quantizeEnabled
                          && m_pQuantizeParameter->toBool();
    bool tripletDisabling = pState->tripletEnabled
                          && !m_pTripletParameter->toBool();

    if (enableState == EffectEnableState::Enabling
     || quantizeEnabling
     || tripletDisabling) {
        if (gf.has_beat_length_sec && gf.has_beat_fraction) {
            currentFrame = static_cast<unsigned int>(gf.beat_fraction *
                    gf.beat_length_sec * bufferParameters.sampleRate());
        } else {
            currentFrame = 0;
        }
        gain = 0;
    }

    int framePerPeriod;
    double rate = m_pRateParameter->value();
    if (gf.has_beat_length_sec && gf.has_beat_fraction) {
        if (m_pQuantizeParameter->toBool()) {
            const auto divider = static_cast<int>(log2(rate));
            rate = pow(2, divider);

            if (m_pTripletParameter->toBool()) {
                rate *= 3.0;
            }
        }
        const auto framePerBeat = static_cast<int>(
                gf.beat_length_sec * bufferParameters.sampleRate());
        framePerPeriod = static_cast<int>(framePerBeat / rate);
    } else {
        framePerPeriod = static_cast<int>(bufferParameters.sampleRate() / rate);
    }

    const auto phaseOffsetFrame = static_cast<unsigned int>(
            m_pPhaseParameter->value() * framePerPeriod);
    currentFrame = currentFrame % framePerPeriod;

    for (SINT i = 0;
            i < bufferParameters.samplesPerBuffer();
            i += bufferParameters.channelCount()) {
        unsigned int positionFrame = (currentFrame - phaseOffsetFrame);
        positionFrame = positionFrame % framePerPeriod;

        //  Relative position (0 to 1) in the period
        double position = static_cast<double>(positionFrame) / framePerPeriod;

        //  Bend the position according to the width parameter
        //  This maps [0 width] to [0 0.5] and [width 1] to [0.5 1]
        if (position < width) {
            position = 0.5 / width * position;
        } else {
            position = 0.5 + 0.5 * (position - width) / (1 - width);
        }

        //  This is where the magic happens
        //  This function gives the gain to apply for position in [0 1]
        //  Plot the function to get a grasp :
        //  From a sine to a square wave depending on the smooth parameter
        double gainTarget = 1.0 - (depth / 2.