summaryrefslogtreecommitdiffstats
path: root/src/soundio/soundmanagerutil.h
blob: dddb140795a5fe49b8b08f1f87cb054346281571 (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
#pragma once

#include <QDomElement>
#include <QList>
#include <QMutex>
#include <QString>
#include <QtDebug>

#include "util/fifo.h"
#include "util/types.h"

/// Describes a group of channels, typically a pair for stereo sound in Mixxx.
class ChannelGroup {
  public:
    ChannelGroup(unsigned char channelBase, unsigned char channels);
    unsigned char getChannelBase() const;
    unsigned char getChannelCount() const;
    bool clashesWith(const ChannelGroup& other) const;

    uint hashValue() const {
        return (m_channels << 8) |
                m_channelBase;
    }
    friend uint qHash(
            const ChannelGroup& group,
            uint seed = 0) {
        return qHash(group.hashValue(), seed);
    }

    friend bool operator==(
            const ChannelGroup& lhs,
            const ChannelGroup& rhs) {
        return lhs.m_channelBase == rhs.m_channelBase &&
                lhs.m_channels == rhs.m_channels;
    }

  private:
    unsigned char m_channelBase; // base (first) channel used on device
    unsigned char m_channels; // number of channels used (s/b 2 in most cases)
};

inline bool operator!=(
        const ChannelGroup& lhs,
        const ChannelGroup& rhs) {
    return !(lhs == rhs);
}

/// Describes a path for audio to take.
///
/// TODO: Choose a better name for this class
class AudioPath {
public:
    /// Predefined types.
    ///
    /// If you add a new type here, be sure to add it to the various
    /// methods including getStringFromType, isIndexed, getTypeFromInt,
    /// channelsNeededForType (if necessary), the subclasses' getSupportedTypes
    /// (if necessary), etc.
    enum AudioPathType {
        MASTER,
        HEADPHONES,
        BOOTH,
        BUS,
        DECK,
        VINYLCONTROL,
        MICROPHONE,
        AUXILIARY,
        RECORD_BROADCAST,
        INVALID, // if this isn't last bad things will happen -bkgood
    };
    AudioPath(unsigned char channelBase, unsigned char channels);
    virtual ~AudioPath() = default;
    AudioPathType getType() const;
    ChannelGroup getChannelGroup() const;
    unsigned char getIndex() const;
    bool channelsClash(const AudioPath &other) const;
    QString getString() const;
    static QString getStringFromType(AudioPathType type);
    static QString getTrStringFromType(AudioPathType type, unsigned char index);
    static AudioPathType getTypeFromString(QString string);
    static bool isIndexed(AudioPathType type);
    static AudioPathType getTypeFromInt(int typeInt);

    /// Returns the minimum number of channels needed on a sound device for an
    /// AudioPathType.
    static unsigned char minChannelsForType(AudioPathType type);

    // Returns the maximum number of channels needed on a sound device for an
    // AudioPathType.
    static unsigned char maxChannelsForType(AudioPathType type);

    uint hashValue() const {
        // Exclude m_channelGroup from hash value!
        // See also: operator==()
        // TODO: Why??
        return (m_type << 8) |
                m_index;
    }
    friend uint qHash(
            const AudioPath& path,
            uint seed = 0) {
        return qHash(path.hashValue(), seed);
    }

    friend bool operator==(
            const AudioPath& lhs,
            const AudioPath& rhs) {
        // Exclude m_channelGroup from comparison!
        // See also: hashValue()/qHash()
        // TODO: Why??
        return lhs.m_type == rhs.m_type &&
                lhs.m_index == rhs.m_index;
    }

protected:
    virtual void setType(AudioPathType type) = 0;
    ChannelGroup m_channelGroup;
    AudioPathType m_type;
    unsigned char m_index;
};

inline bool operator!=(
        const AudioPath& lhs,
        const AudioPath& rhs) {
    return !(lhs == rhs);
}

/// A source of audio in Mixxx that is to be output to a group of
/// channels on an audio interface.
class AudioOutput : public AudioPath {
  public:
    AudioOutput(AudioPathType type, unsigned char channelBase,
                unsigned char channels,
                unsigned char index = 0);
    ~AudioOutput() override = default;
    QDomElement toXML(QDomElement *element) const;
    static AudioOutput fromXML(const QDomElement &xml);
    static QList<AudioPathType> getSupportedTypes();
    bool isHidden();
  protected:
    void setType(AudioPathType type) override;
};

// This class is required to add the buffer, without changing the hash used as ID
class AudioOutputBuffer : public AudioOutput {
  public:
    AudioOutputBuffer(const AudioOutput& out, const CSAMPLE* pBuffer)
           : AudioOutput(out),
             m_pBuffer(pBuffer) {

    };
    ~AudioOutputBuffer() override = default;
    inline const CSAMPLE* getBuffer() const { return m_pBuffer; }
  private:
    const CSAMPLE* m_pBuffer;
};

/// A source of audio at a group of channels on an audio interface
/// that is be processed in Mixxx.
class AudioInput : public AudioPath {
  public:
    AudioInput(AudioPathType type = INVALID, unsigned char channelBase = 0,
               unsigned char channels = 0, unsigned char index = 0);
    ~AudioInput() override;
    QDomElement toXML(QDomElement *element) const;
    static AudioInput fromXML(const QDomElement &xml);
    static QList<AudioPathType> getSupportedTypes();
  protected:
    void setType(AudioPathType type) override;
};

// This class is required to add the buffer, without changing the hash used as
// ID
class AudioInputBuffer : public AudioInput {
  public:
    AudioInputBuffer(const AudioInput& id, CSAMPLE* pBuffer)
            : AudioInput(id),
              m_pBuffer(pBuffer) {

    }
    ~AudioInputBuffer() override = default;
    inline CSAMPLE* getBuffer() const { return m_pBuffer; }
  private:
    CSAMPLE* m_pBuffer;
};


class AudioSource {
public:
    virtual ~AudioSource() = default;

    virtual const CSAMPLE* buffer(const AudioOutput& output) const = 0;

    /// This is called by SoundManager whenever an output is connected for this
    /// source. When this is called it is guaranteed that no callback is
    /// active.
    virtual void onOutputConnected(const AudioOutput& output) {
        Q_UNUSED(output);
    };

    /// This is called by SoundManager whenever an output is disconnected for
    /// this source. When this is called it is guaranteed that no callback is
    /// active.
    virtual void onOutputDisconnected(const AudioOutput& output) {
        Q_UNUSED(output);
    };
};

class AudioDestination {
public:
    virtual ~AudioDestination() = default;

    /// This is called by SoundManager whenever there are new samples from the
    /// configured input to be processed. This is run in the clock reference
    /// callback thread
    virtual void receiveBuffer(const AudioInput& input,
            const CSAMPLE* pBuffer,
            unsigned int iNumFrames) = 0;

    /// This is called by SoundManager whenever an input is configured for this
    /// destination. When this is called it is guaranteed that no callback is
    /// active.
    virtual void onInputConfigured(const AudioInput& input) {
        Q_UNUSED(input);
    };

    /// This is called by SoundManager whenever an input is unconfigured for this
    /// destination. When this is called it is guaranteed that no callback is
    /// active.
    virtual void onInputUnconfigured(const AudioInput& input) {
        Q_UNUSED(input);
    };
};

typedef AudioPath::AudioPathType AudioPathType;

class SoundDeviceId final {
  public:
    QString name;
    /// The "hw:X,Y" device name. Remains an empty string if not using ALSA
    /// or using a non-hw ALSA device such as "default" or "pulse".
    QString alsaHwDevice;
    int portAudioIndex;

    QString debugName() const;

    SoundDeviceId()
       : portAudioIndex(-1) {}
};

/// This must be registered with QMetaType::registerComparators for
/// QVariant::operator== to use it, which is required for QComboBox::findData to
/// work in DlgPrefSoundItem.
inline bool operator==(
        const SoundDeviceId& lhs,
        const SoundDeviceId& rhs) {
    return lhs.name == rhs.name
            && lhs.alsaHwDevice == rhs.alsaHwDevice
            && lhs.portAudioIndex == rhs.portAudioIndex;
}

inline bool operator!=(
        const SoundDeviceId& lhs,
        const SoundDeviceId& rhs) {
    return !(lhs == rhs);
}

/// There is not really a use case for this, but it is required for QMetaType::registerComparators.
inline bool operator<(const SoundDeviceId& lhs, const SoundDeviceId& rhs) {
    DEBUG_ASSERT(!"should never be invoked");
    return lhs.portAudioIndex < rhs.portAudioIndex;
}

Q_DECLARE_METATYPE(SoundDeviceId);

inline uint qHash(
        const SoundDeviceId& id,
        uint seed = 0) {
    return qHash(id.name, seed) ^
            qHash(id.alsaHwDevice, seed) ^
            qHash(id.portAudioIndex, seed);