summaryrefslogtreecommitdiffstats
path: root/src/vinylcontrol/vinylcontrolprocessor.cpp
blob: 9c4825bb5deef0299c215e45aa07a711f0f4a287 (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
#include "vinylcontrol/vinylcontrolprocessor.h"

#include <QMutexLocker>

#include "control/controlpushbutton.h"
#include "moc_vinylcontrolprocessor.cpp"
#include "util/defs.h"
#include "util/event.h"
#include "util/sample.h"
#include "util/timer.h"
#include "vinylcontrol/defs_vinylcontrol.h"
#include "vinylcontrol/vinylcontrol.h"
#include "vinylcontrol/vinylcontrolxwax.h"

#define SIGNAL_QUALITY_FIFO_SIZE 256
#define SAMPLE_PIPE_FIFO_SIZE 65536

VinylControlProcessor::VinylControlProcessor(QObject* pParent, UserSettingsPointer pConfig)
        : QThread(pParent),
          m_pConfig(pConfig),
          m_pToggle(new ControlPushButton(ConfigKey(VINYL_PREF_KEY, "Toggle"))),
          m_pWorkBuffer(SampleUtil::alloc(MAX_BUFFER_LEN)),
          m_processorsLock(QMutex::Recursive),
          m_processors(kMaximumVinylControlInputs, NULL),
          m_signalQualityFifo(SIGNAL_QUALITY_FIFO_SIZE),
          m_bReportSignalQuality(false),
          m_bQuit(false),
          m_bReloadConfig(false) {
    connect(m_pToggle,
            &ControlPushButton::valueChanged,
            this,
            &VinylControlProcessor::toggleDeck,
            Qt::DirectConnection);

    for (int i = 0; i < kMaximumVinylControlInputs; ++i) {
        m_samplePipes[i] = new FIFO<CSAMPLE>(SAMPLE_PIPE_FIFO_SIZE);
    }

    start(QThread::HighPriority);
}

VinylControlProcessor::~VinylControlProcessor() {
    m_bQuit = true;
    m_samplesAvailableSignal.wakeAll();
    wait();

    delete m_pToggle;
    SampleUtil::free(m_pWorkBuffer);

    {
        QMutexLocker locker(&m_processorsLock);
        for (int i = 0; i < kMaximumVinylControlInputs; ++i) {
            VinylControl* pProcessor = m_processors.at(i);
            m_processors[i] = NULL;
            delete pProcessor;

            delete m_samplePipes[i];
            m_samplePipes[i] = nullptr;
        }
    }

    // xwax has a global LUT that we need to free after we've shut down our
    // vinyl control threads because it's not thread-safe.
    VinylControlXwax::freeLUTs();
}

void VinylControlProcessor::setSignalQualityReporting(bool enable) {
    m_bReportSignalQuality = enable;
}

void VinylControlProcessor::shutdown() {
    m_bQuit = true;
    m_samplesAvailableSignal.wakeAll();
}

void VinylControlProcessor::requestReloadConfig() {
    m_bReloadConfig = true;
    m_samplesAvailableSignal.wakeAll();
}

void VinylControlProcessor::run() {
    unsigned static id = 0; //the id of this thread, for debugging purposes //XXX copypasta (should factor this out somehow), -kousu 2/2009
    QThread::currentThread()->setObjectName(QString("VinylControlProcessor %1").arg(++id));

    while (!m_bQuit) {
        if (m_bReloadConfig) {
            reloadConfig();
            m_bReloadConfig = false;
        }

        for (int i = 0; i < kMaximumVinylControlInputs; ++i) {
            QMutexLocker locker(&m_processorsLock);
            VinylControl* pProcessor = m_processors[i];
            locker.unlock();
            FIFO<CSAMPLE>* pSamplePipe = m_samplePipes[i];

            if (pSamplePipe->readAvailable() > 0) {
                int samplesRead = pSamplePipe->read(m_pWorkBuffer, MAX_BUFFER_LEN);

                if (samplesRead % 2 != 0) {
                    qWarning() << "VinylControlProcessor received non-even number of samples via sample FIFO.";
                    samplesRead--;
                }
                int framesRead = samplesRead / 2;

                if (pProcessor) {
                    pProcessor->analyzeSamples(m_pWorkBuffer, framesRead);
                } else {
                    // Samples are being written to a non-existent processor. Warning?
                    qWarning() << "Samples written to non-existent VinylControl processor:" << i;
                }
            }

            // TODO(rryan) define a time-based update rate. This will update way
            // too quickly.
            if (pProcessor && m_bReportSignalQuality) {
                VinylSignalQualityReport report;
                if (pProcessor->writeQualityReport(&report)) {
                    report.processor = i;
                    if (m_signalQualityFifo.write(&report, 1) != 1) {
                        qWarning() << "VinylControlProcessor could not write signal quality report for VC index:" << i;
                    }
                }
            }
        }

        if (m_bQuit) {
            break;
        }

        // Wait for a signal from the main thread or engine thread that we
        // should wake up and process input.
        m_waitForSampleMutex.lock();
        m_samplesAvailableSignal.wait(&m_waitForSampleMutex);
        m_waitForSampleMutex.unlock();
    }
}

void VinylControlProcessor::reloadConfig() {
    for (int i = 0; i < kMaximumVinylControlInputs; ++i) {
        QMutexLocker locker(&m_processorsLock);
        VinylControl* pCurrent = m_processors[i];

        if (pCurrent == nullptr) {
            continue;
        }

        VinylControl *pNew = new VinylControlXwax(
            m_pConfig, kVCGroup.arg(i + 1));
        m_processors.replace(i, pNew);
        locker.unlock();
        // Delete outside of the critical section to avoid deadlocks.
        delete pCurrent;
    }
}

void VinylControlProcessor::onInputConfigured(const AudioInput& input) {
    if (input.getType() != AudioInput::VINYLCONTROL) {
        qDebug() << "WARNING: AudioInput type is not VINYLCONTROL. Ignoring.";
        return;
    }
    unsigned char index = input.getIndex();

    if (index >= kMaximumVinylControlInputs) {
        // Should not be possible.
        qWarning() << "VinylControlProcessor::onInputConnected got invalid index:" << index;
        return;
    }

    VinylControl *pNew = new VinylControlXwax(
        m_pConfig, kVCGroup.arg(index + 1));

    QMutexLocker locker(&m_processorsLock);
    VinylControl* pCurrent = m_processors.at(index);
    m_processors.replace(index, pNew);
    locker.unlock();
    // Delete outside of the critical section to avoid deadlocks.
    delete pCurrent;
}

void VinylControlProcessor::onInputUnconfigured(const AudioInput& input) {
    if (input.getType() != AudioInput::VINYLCONTROL) {
        qDebug() << "WARNING: AudioInput type is not VINYLCONTROL. Ignoring.";
        return;
    }

    unsigned char index = input.getIndex();

    if (index >= kMaximumVinylControlInputs) {
        // Should not be possible.
        qWarning() << "VinylControlProcessor::onInputDisconnected got invalid index:" << index;
        return;
    }

    QMutexLocker locker(&m_processorsLock);
    VinylControl* pVC = m_processors.at(index);
    m_processors.replace(index, NULL);
    locker.unlock();
    // Delete outside of the critical section to avoid deadlocks.
    delete pVC;
}

bool VinylControlProcessor::deckConfigured(int index) const {
    return m_processors[index] != NULL;
}

void VinylControlProcessor::receiveBuffer(const AudioInput& input,
        const CSAMPLE* pBuffer,
        unsigned int nFrames) {
    ScopedTimer t("VinylControlProcessor::receiveBuffer");
    if (input.getType() != AudioInput::VINYLCONTROL) {
        qDebug() << "WARNING: AudioInput type is not VINYLCONTROL. Ignoring incoming buffer.";
        return;
    }

    unsigned char vcIndex = input.getIndex();

    if (vcIndex >= kMaximumVinylControlInputs) {
        // Should not be possible.
        return;
    }

    FIFO<CSAMPLE>* pSamplePipe = m_samplePipes[vcIndex];

    if (pSamplePipe == nullptr) {
        // Should not be possible.
        return;
    }

    const int kChannels = 2;
    const int nSamples = nFrames * kChannels;
    int samplesWritten = pSamplePipe->write(pBuffer, nSamples);

    if (samplesWritten < nSamples) {
        qWarning() << "ERROR: Buffer overflow in VinylControlProcessor. Dropping samples on the floor."
                   << "VCIndex:" << vcIndex;
    }

    m_samplesAvailableSignal.wakeAll();
}

void VinylControlProcessor::toggleDeck(double value) {
    if (value == 0) {
        return;
    }

    /** few different cases here:
     * 1. No decks have vinyl control enabled.
     * 2. One deck has vinyl control enabled.
     * 3. Many decks have vinyl control enabled.
     *
     * For case 1, we'll just enable vinyl control on the first deck. Case 2
     * is the most common one, we'll just turn off the vinyl control on the
     * deck currently using it and turn it on on the next one (sequentially,
     * wrapping as needed). Behavior in case 3 is totally non-obvious and
     * will be ignored.
     */

    // -1 means we haven't found a proxy that's enabled
    int enabled = -1;

    QMutexLocker locker(&m_processorsLock);

    for (int i = 0; i < m_processors.size(); ++i) {
        VinylControl* pProcessor = m_processors.at(i);
        if (pProcessor && pProcessor->isEnabled()) {
            if (enabled > -1) {
                return; // case 3
            }
            enabled = i;
        }
    }

    if (enabled >