summaryrefslogtreecommitdiffstats
path: root/src/engine/enginemaster.h
blob: 304c63730ffabf0847f8f6299fd9cfd02e4bd7f3 (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
/***************************************************************************
                          enginemaster.h  -  description
                             -------------------
    begin                : Sun Apr 28 2002
    copyright            : (C) 2002 by
    email                :
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#pragma once

#include <QObject>
#include <QVarLengthArray>

#include "preferences/usersettings.h"
#include "control/controlobject.h"
#include "control/controlpushbutton.h"
#include "engine/engineobject.h"
#include "engine/channels/enginechannel.h"
#include "engine/channelhandle.h"
#include "soundio/soundmanager.h"
#include "soundio/soundmanagerutil.h"
#include "recording/recordingmanager.h"

class EngineWorkerScheduler;
class EngineBuffer;
class EngineChannel;
class EngineDeck;
class EngineFlanger;
class EngineVuMeter;
class ControlPotmeter;
class ControlPushButton;
class EngineSideChain;
class EffectsManager;
class EngineEffectsManager;
class SyncWorker;
class GuiTick;
class EngineSync;
class EngineTalkoverDucking;
class EngineDelay;

// The number of channels to pre-allocate in various structures in the
// engine. Prevents memory allocation in EngineMaster::addChannel.
static const int kPreallocatedChannels = 64;

class EngineMaster : public QObject, public AudioSource {
    Q_OBJECT
  public:
    EngineMaster(UserSettingsPointer pConfig,
            const QString& group,
            EffectsManager* pEffectsManager,
            ChannelHandleFactoryPointer pChannelHandleFactory,
            bool bEnableSidechain);
    virtual ~EngineMaster();

    // Get access to the sample buffers. None of these are thread safe. Only to
    // be called by SoundManager.
    const CSAMPLE* buffer(AudioOutput output) const;

    ChannelHandleAndGroup registerChannelGroup(const QString& group) {
        return ChannelHandleAndGroup(
                   m_pChannelHandleFactory->getOrCreateHandle(group), group);
    }

    // Register the sound I/O that does not correspond to any EngineChannel object
    void registerNonEngineChannelSoundIO(SoundManager* pSoundManager);

    // WARNING: These methods are called by the main thread. They should only
    // touch the volatile bool connected indicators (see below). However, when
    // these methods are called the callback is guaranteed to be inactive
    // (SoundManager closes all devices before calling these). This may change
    // in the future.
    virtual void onOutputConnected(AudioOutput output);
    virtual void onOutputDisconnected(AudioOutput output);
    void onInputConnected(AudioInput input);
    void onInputDisconnected(AudioInput input);

    void process(const int iBufferSize);

    // Add an EngineChannel to the mixing engine. This is not thread safe --
    // only call it before the engine has started mixing.
    void addChannel(EngineChannel* pChannel);
    EngineChannel* getChannel(const QString& group);
    static inline CSAMPLE_GAIN gainForOrientation(EngineChannel::ChannelOrientation orientation,
            CSAMPLE_GAIN leftGain,
            CSAMPLE_GAIN centerGain,
            CSAMPLE_GAIN rightGain) {
        switch (orientation) {
            case EngineChannel::LEFT:
                return leftGain;
            case EngineChannel::RIGHT:
                return rightGain;
            case EngineChannel::CENTER:
            default:
                return centerGain;
        }
    }

    // Provide access to the master sync so enginebuffers can know what their rate controller is.
    EngineSync* getEngineSync() const{
        return m_pMasterSync;
    }

    // These are really only exposed for tests to use.
    const CSAMPLE* getMasterBuffer() const;
    const CSAMPLE* getBoothBuffer() const;
    const CSAMPLE* getHeadphoneBuffer() const;
    const CSAMPLE* getOutputBusBuffer(unsigned int i) const;
    const CSAMPLE* getDeckBuffer(unsigned int i) const;
    const CSAMPLE* getChannelBuffer(QString name) const;
    const CSAMPLE* getSidechainBuffer() const;

    EngineSideChain* getSideChain() const {
        return m_pEngineSideChain;
    }

    struct ChannelInfo {
        ChannelInfo(int index)
                : m_pChannel(NULL),
                  m_pBuffer(NULL),
                  m_pVolumeControl(NULL),
                  m_pMuteControl(NULL),
                  m_index(index) {
        }
        ChannelHandle m_handle;
        EngineChannel* m_pChannel;
        CSAMPLE* m_pBuffer;
        ControlObject* m_pVolumeControl;
        ControlPushButton* m_pMuteControl;
        GroupFeatureState m_features;
        int m_index;
    };

    struct GainCache {
        CSAMPLE m_gain;
        bool m_fadeout;
    };

    class GainCalculator {
      public:
        virtual CSAMPLE_GAIN getGain(ChannelInfo* pChannelInfo) const = 0;
    };
    class PflGainCalculator : public GainCalculator {
      public:
        inline CSAMPLE_GAIN getGain(ChannelInfo* pChannelInfo) const override {
            Q_UNUSED(pChannelInfo);
            return m_dGain;
        }
        inline void setGain(CSAMPLE_GAIN dGain) {
            m_dGain = dGain;
        }

      private:
        CSAMPLE_GAIN m_dGain;
    };
    class TalkoverGainCalculator : public GainCalculator {
      public:
        inline CSAMPLE_GAIN getGain(ChannelInfo* pChannelInfo) const override {
            return static_cast<CSAMPLE_GAIN>(pChannelInfo->m_pVolumeControl->get());
        }
    };
    class OrientationVolumeGainCalculator : public GainCalculator {
      public:
        OrientationVolumeGainCalculator()
                : m_dLeftGain(1.0),
                  m_dCenterGain(1.0),
                  m_dRightGain(1.0),
                  m_dTalkoverDuckingGain(1.0) {
        }

        inline CSAMPLE_GAIN getGain(ChannelInfo* pChannelInfo) const {
            const CSAMPLE_GAIN channelVolume = static_cast<CSAMPLE_GAIN>(
                    pChannelInfo->m_pVolumeControl->get());
            const CSAMPLE_GAIN orientationGain = EngineMaster::gainForOrientation(
                    pChannelInfo->m_pChannel->getOrientation(),
                    m_dLeftGain,
                    m_dCenterGain,
                    m_dRightGain);
            return channelVolume * orientationGain * m_dTalkoverDuckingGain;
        }

        inline void setGains(CSAMPLE_GAIN leftGain,
                CSAMPLE_GAIN centerGain,
                CSAMPLE_GAIN rightGain,
                CSAMPLE_GAIN talkoverDuckingGain) {
            m_dLeftGain = leftGain;
            m_dCenterGain = centerGain;
            m_dRightGain = rightGain;
            m_dTalkoverDuckingGain = talkoverDuckingGain;
        }

      private:
        CSAMPLE_GAIN m_dLeftGain;
        CSAMPLE_GAIN m_dCenterGain;
        CSAMPLE_GAIN m_dRightGain;
        CSAMPLE_GAIN m_dTalkoverDuckingGain;
    };

    enum class MicMonitorMode {
        // These are out of order with how they are listed in DlgPrefSound for backwards
        // compatibility with Mixxx 2.0 user settings. In Mixxx 2.0, before the
        // booth output was added, this was a binary option without
        // the MASTER_AND_BOOTH mode.
        MASTER = 0,
        DIRECT_MONITOR,
        MASTER_AND_BOOTH
    };

    template<typename T, unsigned int CAPACITY>
    class FastVector {
      public:
        inline FastVector() : m_size(0), m_data((T*)((void *)m_buffer)) {};
        inline ~FastVector() {
            if (QTypeInfo<T>::isComplex) {
                for (int i = 0; i < m_size; ++i) {
                    m_data[i].~T();
                }
            }
        }
        inline void append(const T& t) {
            if (QTypeInfo<T>::isComplex) {
                new (&m_data[m_size++]) T(t);
            } else {
                m_data[m_size++] = t;
            }
        };
        inline const T& operator[](unsigned int i) const {
            return m_data[i];
        }
        inline T& operator[](unsigned int i) {
            return m_data[i];
        }
        inline const T& at(unsigned int i) const {
            return m_data[i];
        }
        inline void replace(unsigned int i, const T& t) {
            T copy(t);
            m_data[i] = copy;
        }
        inline int size () const {
            return m_size;
        }
      private:
        int m_size;
        T* const m_data;
        // Using a long double buffer guarantees the alignment for any type
        // but avoids the constructor call T();
        long double m_buffer[(CAPACITY * sizeof(T) + sizeof(long double) - 1) /
                             sizeof(long double)];
    };

  protected:
    // The master buffer is protected so it can be accessed by test subclasses.
    CSAMPLE* m_pMaster;

    // ControlObjects for switching off unnecessary processing
    // These are protected so tests can set them
    ControlObject* m_pMasterEnabled;
    ControlObject* m_pHeadphoneEnabled;
    ControlObject* m_pBoothEnabled;

  private:
    // Processes active channels. The master sync channel (if any) is processed
    // first and all others are processed afte