summaryrefslogtreecommitdiffstats
path: root/src/effects/lv2/lv2effectprocessor.cpp
blob: d455c4a4f6e810fd85365b0889d63f08e971774b (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
#include "effects/lv2/lv2effectprocessor.h"
#include "engine/effects/engineeffect.h"
#include "control/controlobject.h"
#include "util/sample.h"
#include "util/defs.h"

LV2EffectProcessor::LV2EffectProcessor(EngineEffect* pEngineEffect,
        EffectManifestPointer pManifest,
        const LilvPlugin* plugin,
        const QList<int>& audioPortIndices,
        const QList<int>& controlPortIndices)
        : m_pPlugin(plugin),
          m_audioPortIndices(audioPortIndices),
          m_controlPortIndices(controlPortIndices),
          m_pEffectsManager(nullptr) {
    m_inputL = new float[MAX_BUFFER_LEN];
    m_inputR = new float[MAX_BUFFER_LEN];
    m_outputL = new float[MAX_BUFFER_LEN];
    m_outputR = new float[MAX_BUFFER_LEN];
    m_params = new float[pManifest->parameters().size()];

    const QList<EffectManifestParameterPointer>& effectManifestParameterList =
            pManifest->parameters();

    // Initialize EngineEffectParameters
    for (const auto& pParam: effectManifestParameterList) {
        m_parameters.append(pEngineEffect->getParameterById(pParam->id()));
    }
}

LV2EffectProcessor::~LV2EffectProcessor() {
    if (kEffectDebugOutput) {
        qDebug() << "~LV2EffectProcessor" << this;
    }
    int inputChannelHandleNumber = 0;
    for (auto& outputsMap : m_channelStateMatrix) {
        int outputChannelHandleNumber = 0;
        for (LV2EffectGroupState* pState : outputsMap) {
              VERIFY_OR_DEBUG_ASSERT(pState != nullptr) {
                    continue;
              }
              if (kEffectDebugOutput) {
                  qDebug() << "~LV2EffectProcessor deleting LilvInstance" << pState
                           << "for input ChannelHandle(" << inputChannelHandleNumber << ")"
                           << "and output ChannelHandle(" << outputChannelHandleNumber << ")";
              }
              delete pState;
              outputChannelHandleNumber++;
        }
        outputsMap.clear();
    }
    m_channelStateMatrix.clear();

    delete[] m_inputL;
    delete[] m_inputR;
    delete[] m_outputL;
    delete[] m_outputR;
    delete[] m_params;
}

void LV2EffectProcessor::initialize(
        const QSet<ChannelHandleAndGroup>& activeInputChannels,
        EffectsManager* pEffectsManager,
        const mixxx::EngineParameters& bufferParameters) {
    Q_UNUSED(pEffectsManager);
    Q_UNUSED(bufferParameters);


    for (const ChannelHandleAndGroup& inputChannel : activeInputChannels) {
        if (kEffectDebugOutput) {
            qDebug() << this << "LV2EffectProcessor::initialize allocating "
                        "EffectStates for input" << inputChannel;
        }
        ChannelHandleMap<LV2EffectGroupState*> outputChannelMap;
        for (const ChannelHandleAndGroup& outputChannel :
                pEffectsManager->registeredOutputChannels()) {
            LV2EffectGroupState* pGroupState = createGroupState(bufferParameters);
            if (pGroupState) {
                outputChannelMap.insert(outputChannel.handle(), pGroupState);
                if (kEffectDebugOutput) {
                    qDebug() << this << "EffectProcessorImpl::initialize "
                            "registering output" << outputChannel << outputChannelMap[outputChannel.handle()];
                }
            }
        }
        m_channelStateMatrix.insert(inputChannel.handle(), outputChannelMap);
    }
    m_pEffectsManager = pEffectsManager;
    DEBUG_ASSERT(m_pEffectsManager != nullptr);
}

void LV2EffectProcessor::process(const ChannelHandle& inputHandle,
        const ChannelHandle& outputHandle,
        const CSAMPLE* pInput, CSAMPLE* pOutput,
        const mixxx::EngineParameters& bufferParameters,
        const EffectEnableState enableState,
        const GroupFeatureState& groupFeatures) {
    Q_UNUSED(groupFeatures);
    Q_UNUSED(enableState);

    LV2EffectGroupState* pState = m_channelStateMatrix[inputHandle][outputHandle];
    VERIFY_OR_DEBUG_ASSERT(pState != nullptr) {
        if (kEffectDebugOutput) {
            qWarning() << "LV2EffectProcessor::process could not retrieve"
                          "handle for input" << inputHandle
                       << "and output" << outputHandle
                       << "Handle should have been preallocated in the"
                          "main thread.";
        }
        pState = createGroupState(bufferParameters);
        m_channelStateMatrix[inputHandle][outputHandle] = pState;
    }

    if (!pState) {
        SampleUtil::copyWithGain(pOutput, pInput, 1.0, bufferParameters.samplesPerBuffer());
        return;
    }

    for (int i = 0; i < m_parameters.size(); i++) {
        m_params[i] = static_cast<float>(m_parameters[i]->value());
    }

    int j = 0;
    for (SINT i = 0; i < bufferParameters.samplesPerBuffer(); i += 2) {
        m_inputL[j] = pInput[i];
        m_inputR[j] = pInput[i + 1];
        j++;
    }

    lilv_instance_run(pState->lilvIinstance(), bufferParameters.framesPerBuffer());

    j = 0;
    for (SINT i = 0; i < bufferParameters.samplesPerBuffer(); i += 2) {
        pOutput[i] = m_outputL[j];
        pOutput[i + 1] = m_outputR[j];
        j++;
    }
}

LV2EffectGroupState* LV2EffectProcessor::createGroupState(const mixxx::EngineParameters& bufferParameters) {
    LV2EffectGroupState * pState = new LV2EffectGroupState(bufferParameters, m_pPlugin);
    LilvInstance* handle = pState->lilvIinstance();
    if (handle) {
        for (int i = 0; i < m_parameters.size(); i++) {
            m_params[i] = static_cast<float>(m_parameters[i]->value());
            lilv_instance_connect_port(handle, m_controlPortIndices[i], &m_params[i]);
        }

        // We assume the audio ports are in the following order:
        // input_left, input_right, output_left, output_right
        lilv_instance_connect_port(handle, m_audioPortIndices[0], m_inputL);
        lilv_instance_connect_port(handle, m_audioPortIndices[1], m_inputR);
        lilv_instance_connect_port(handle, m_audioPortIndices[2], m_outputL);
        lilv_instance_connect_port(handle, m_audioPortIndices[3], m_outputR);

        lilv_instance_activate(handle);
    }
    if (kEffectDebugOutput) {
        qDebug() << this << "LV2EffectProcessor creating EffectState" << pState;
    }
    return pState;
};

EffectState* LV2EffectProcessor::createState(const mixxx::EngineParameters& bufferParameters) {
    return createGroupState(bufferParameters);
};

bool LV2EffectProcessor::loadStatesForInputChannel(const ChannelHandle* inputChannel,
      const EffectStatesMap* pStatesMap) {
    if (kEffectDebugOutput) {
        qDebug() << "LV2EffectProcessor::loadStatesForInputChannel" << this
                 << "input" << *inputChannel;
    }

    // NOTE: ChannelHandleMap is like a map in that it associates an
    // object with a ChannelHandle key, but it actually backed by a
    // QVarLengthArray, not a QMap. So it is okay that
    // m_channelStateMatrix may be accessed concurrently in the main
    // thread in deleteStatesForInputChannel.

    // Can't directly cast a ChannelHandleMap from containing the base
    // EffectState* type to EffectSpecificState* type, so iterate through
    // pStatesMap to build a new ChannelHandleMap with
    // dynamic_cast'ed states.
    ChannelHandleMap<LV2EffectGroupState*>& effectSpecificStatesMap =
            m_channelStateMatrix[*inputChannel];

    // deleteStatesForInputChannel should have been called before a new
    // map of EffectStates was sent to this function, or this is the first
    // time states are being loaded for this input channel, so
    // effectSpecificStatesMap should be empty and this loop should
    // not go through any iterations.
    for (LV2EffectGroupState* pState : effectSpecificStatesMap) {
        VERIFY_OR_DEBUG_ASSERT(pState == nullptr) {
            delete pState;
            qDebug() << "for effectSpecificStatesMap";
        }
    }

    for (const ChannelHandleAndGroup& outputChannel :
            m_pEffectsManager->registeredOutputChannels()) {
        if (kEffectDebugOutput) {
            qDebug() << "LV2EffectProcessor::loadStatesForInputChannel"
                     << this << "output" << outputChannel;
        }

        auto* pState = dynamic_cast<LV2EffectGroupState*>(
                pStatesMap->at(outputChannel.handle()));
        VERIFY_OR_DEBUG_ASSERT(pState != nullptr) {
              return false;
        }
        effectSpecificStatesMap.insert(outputChannel.handle(), pState);
    }
    return true;
}

// Called from main thread for garbage collection after the last audio thread
// callback executes process() with EffectEnableState::Disabling
void LV2EffectProcessor::deleteStatesForInputChannel(const ChannelHandle* inputChannel) {
    if (kEffectDebugOutput) {
        qDebug() << "LV2EffectProcessor::deleteStatesForInputChannel"
                 << this << *inputChannel;
    }

    // NOTE: ChannelHandleMap is like a map in that it associates an
    // object with a ChannelHandle key, but it actually backed by a
    // QVarLengthArray, not a QMap. So it is okay that
    // m_channelStateMatrix may be accessed concurrently in the audio
    // engine thread in loadStatesForInputChannel.

    ChannelHandleMap<LV2EffectGroupState*>& stateMap =
            m_channelStateMatrix[*inputChannel];
    for (LV2EffectGroupState* pState : stateMap) {
          VERIFY_OR_DEBUG_ASSERT(pState != nullptr) {
                continue;
          }
          if (kEffectDebugOutput) {
                qDebug() << "LV2EffectProcessor::deleteStatesForInputChannel"
                         << this << "deleting state" << pState;
          }
          delete pState;
          qDebug() <<