summaryrefslogtreecommitdiffstats
path: root/src/multimedia/darwin/qcoreaudioutils.mm
blob: 46f98dcc95f4a8f2fdccef9e54daf46dbeb9198e (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
// Copyright (C) 2016 The Qt Company Ltd and/or its subsidiary(-ies).
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qcoreaudioutils_p.h"
#include <qdebug.h>
#include <mach/mach_time.h>

QT_BEGIN_NAMESPACE

double CoreAudioUtils::sFrequency = 0.0;
bool CoreAudioUtils::sIsInitialized = false;

void CoreAudioUtils::initialize()
{
    struct mach_timebase_info timeBaseInfo;
    mach_timebase_info(&timeBaseInfo);
    sFrequency = static_cast<double>(timeBaseInfo.denom) / static_cast<double>(timeBaseInfo.numer);
    sFrequency *= 1000000000.0;

    sIsInitialized = true;
}


quint64 CoreAudioUtils::currentTime()
{
    return mach_absolute_time();
}

double CoreAudioUtils::frequency()
{
    if (!sIsInitialized)
        initialize();
    return sFrequency;
}

QAudioFormat CoreAudioUtils::toQAudioFormat(AudioStreamBasicDescription const& sf)
{
    QAudioFormat    audioFormat;
    // all Darwin HW is little endian, we ignore those formats
    if ((sf.mFormatFlags & kAudioFormatFlagIsBigEndian) != 0 && QSysInfo::ByteOrder != QSysInfo::LittleEndian)
        return audioFormat;

    // filter out the formats we're interested in
    QAudioFormat::SampleFormat format = QAudioFormat::Unknown;
    switch (sf.mBitsPerChannel) {
    case 8:
        if ((sf.mFormatFlags & kAudioFormatFlagIsSignedInteger) == 0)
            format = QAudioFormat::UInt8;
        break;
    case 16:
        if ((sf.mFormatFlags & kAudioFormatFlagIsSignedInteger) != 0)
            format = QAudioFormat::Int16;
        break;
    case 32:
        if ((sf.mFormatFlags & kAudioFormatFlagIsSignedInteger) != 0)
            format = QAudioFormat::Int32;
        else if ((sf.mFormatFlags & kAudioFormatFlagIsFloat) != 0)
            format = QAudioFormat::Float;
        break;
    default:
        break;
    }

    audioFormat.setSampleFormat(format);
    audioFormat.setSampleRate(sf.mSampleRate);
    audioFormat.setChannelCount(sf.mChannelsPerFrame);

    return audioFormat;
}

AudioStreamBasicDescription CoreAudioUtils::toAudioStreamBasicDescription(QAudioFormat const& audioFormat)
{
    AudioStreamBasicDescription sf;

    sf.mFormatFlags         = kAudioFormatFlagIsPacked;
    sf.mSampleRate          = audioFormat.sampleRate();
    sf.mFramesPerPacket     = 1;
    sf.mChannelsPerFrame    = audioFormat.channelCount();
    sf.mBitsPerChannel      = audioFormat.bytesPerSample() * 8;
    sf.mBytesPerFrame       = audioFormat.bytesPerFrame();
    sf.mBytesPerPacket      = sf.mFramesPerPacket * sf.mBytesPerFrame;
    sf.mFormatID            = kAudioFormatLinearPCM;

    switch (audioFormat.sampleFormat()) {
    case QAudioFormat::Int16:
    case QAudioFormat::Int32:
        sf.mFormatFlags |= kAudioFormatFlagIsSignedInteger;
        break;
    case QAudioFormat::Float:
        sf.mFormatFlags |= kAudioFormatFlagIsFloat;
        break;
    case QAudioFormat::UInt8:
        /* default */
    case QAudioFormat::Unknown:
    case QAudioFormat::NSampleFormats:
        break;
    }

    return sf;
}


static constexpr struct {
    QAudioFormat::AudioChannelPosition pos;
    AudioChannelLabel label;
} channelMap[] =  {
        { QAudioFormat::FrontLeft, kAudioChannelLabel_Left },
        { QAudioFormat::FrontRight, kAudioChannelLabel_Right },
        { QAudioFormat::FrontCenter, kAudioChannelLabel_Center },
        { QAudioFormat::LFE, kAudioChannelLabel_LFEScreen },
        { QAudioFormat::BackLeft, kAudioChannelLabel_LeftSurround },
        { QAudioFormat::BackRight, kAudioChannelLabel_RightSurround },
        { QAudioFormat::FrontLeftOfCenter, kAudioChannelLabel_LeftCenter },
        { QAudioFormat::FrontRightOfCenter, kAudioChannelLabel_RightCenter },
        { QAudioFormat::BackCenter, kAudioChannelLabel_CenterSurround },
        { QAudioFormat::LFE2, kAudioChannelLabel_LFE2 },
        { QAudioFormat::SideLeft, kAudioChannelLabel_LeftSurroundDirect }, // ???
        { QAudioFormat::SideRight, kAudioChannelLabel_RightSurroundDirect }, // ???
        { QAudioFormat::TopFrontLeft, kAudioChannelLabel_VerticalHeightLeft },
        { QAudioFormat::TopFrontRight, kAudioChannelLabel_VerticalHeightRight },
        { QAudioFormat::TopFrontCenter, kAudioChannelLabel_VerticalHeightCenter },
        { QAudioFormat::TopCenter, kAudioChannelLabel_CenterTopMiddle },
        { QAudioFormat::TopBackLeft, kAudioChannelLabel_TopBackLeft },
        { QAudioFormat::TopBackRight, kAudioChannelLabel_TopBackRight },
        { QAudioFormat::TopSideLeft, kAudioChannelLabel_LeftTopMiddle },
        { QAudioFormat::TopSideRight, kAudioChannelLabel_RightTopMiddle },
        { QAudioFormat::TopBackCenter, kAudioChannelLabel_TopBackCenter },
};

std::unique_ptr<AudioChannelLayout> CoreAudioUtils::toAudioChannelLayout(const QAudioFormat &format, UInt32 *size)
{
    auto channelConfig = format.channelConfig();
    if (channelConfig == QAudioFormat::ChannelConfigUnknown)
        channelConfig = QAudioFormat::defaultChannelConfigForChannelCount(format.channelCount());

    *size = sizeof(AudioChannelLayout) + int(QAudioFormat::NChannelPositions)*sizeof(AudioChannelDescription);
    auto *layout = static_cast<AudioChannelLayout *>(malloc(*size));
    memset(layout, 0, *size);
    layout->mChannelLayoutTag = kAudioChannelLayoutTag_UseChannelDescriptions;

    for (const auto &m : channelMap) {
        if (channelConfig & QAudioFormat::channelConfig(m.pos))
            layout->mChannelDescriptions[layout->mNumberChannelDescriptions++].mChannelLabel = m.label;
    }

    if (channelConfig & QAudioFormat::channelConfig(QAudioFormat::BottomFrontCenter)) {
        auto &desc = layout->mChannelDescriptions[layout->mNumberChannelDescriptions++];
        desc.mChannelLabel = kAudioChannelLabel_UseCoordinates;
        desc.mChannelFlags = kAudioChannelFlags_SphericalCoordinates;
        desc.mCoordinates[kAudioChannelCoordinates_Azimuth] = 0.f;
        desc.mCoordinates[kAudioChannelCoordinates_Elevation] = -20.;
        desc.mCoordinates[kAudioChannelCoordinates_Distance] = 1.f;
    }
    if (channelConfig & QAudioFormat::channelConfig(QAudioFormat::BottomFrontLeft)) {
        auto &desc = layout->mChannelDescriptions[layout->mNumberChannelDescriptions++];
        desc.mChannelLabel = kAudioChannelLabel_UseCoordinates;
        desc.mChannelFlags = kAudioChannelFlags_SphericalCoordinates;
        desc.mCoordinates[kAudioChannelCoordinates_Azimuth] = -45.f;
        desc.mCoordinates[kAudioChannelCoordinates_Elevation] = -20.;
        desc.mCoordinates[kAudioChannelCoordinates_Distance] = 1.f;
    }
    if (channelConfig & QAudioFormat::channelConfig(QAudioFormat::BottomFrontRight)) {
        auto &desc = layout->mChannelDescriptions[layout->mNumberChannelDescriptions++];
        desc.mChannelLabel = kAudioChannelLabel_UseCoordinates;
        desc.mChannelFlags = kAudioChannelFlags_SphericalCoordinates;
        desc.mCoordinates[kAudioChannelCoordinates_Azimuth] = 45.f;
        desc.mCoordinates[kAudioChannelCoordinates_Elevation] = -20.;
        desc.mCoordinates[kAudioChannelCoordinates_Distance] = 1.f;
    }

    return std::unique_ptr<AudioChannelLayout>(layout);
}

static constexpr struct {
    AudioChannelLayoutTag tag;
    QAudioFormat::ChannelConfig channelConfig;
} layoutTagMap[] = {
    { kAudioChannelLayoutTag_Mono, QAudioFormat::ChannelConfigMono },
    { kAudioChannelLayoutTag_Stereo, QAudioFormat::ChannelConfigStereo },
    { kAudioChannelLayoutTag_StereoHeadphones, QAudioFormat::ChannelConfigStereo },
    { kAudioChannelLayoutTag_MPEG_1_0, QAudioFormat::ChannelConfigMono },
    { kAudioChannelLayoutTag_MPEG_2_0, QAudioFormat::ChannelConfigStereo },
    { kAudioChannelLayoutTag_MPEG_3_0_A, QAudioFormat::channelConfig(QAudioFormat::FrontLeft,
                                                                     QAudioFormat::FrontRight,
                                                                     QAudioFormat::FrontCenter) },
    { kAudioChannelLayoutTag_MPEG_4_0_A, QAudioFormat::channelConfig(QAudioFormat::FrontLeft,
                                                                     QAudioFormat::FrontRight,
                                                                     QAudioFormat::FrontCenter,
                                                                     QAudioFormat::BackCenter) },
    { kAudioChannelLayoutTag_MPEG_5_0_A, QAudioFormat::ChannelConfigSurround5Dot0 },
    { kAudioChannelLayoutTag_MPEG_5_1_A, QAudioFormat::ChannelConfigSurround5Dot1 },
    { kAudioChannelLayoutTag_MPEG_6_1_A, QAudioFormat::channelConfig(QAudioFormat::FrontLeft,
                                                                     QAudioFormat::FrontRight,
                                                                     QAudioFormat::FrontCenter,
                                                                     QAudioFormat::LFE,
                                                                     QAudioFormat::BackLeft,
                                                                     QAudioFormat::BackRight,
                                                                     QAudioFormat::BackCenter) },
    { kAudioChannelLayoutTag_MPEG_7_1_A, QAudioFormat::ChannelConfigSurround7Dot1 },
    { kAudioChannelLayoutTag_SMPTE_DTV, QAudioFormat::channelConfig(QAudioFormat::FrontLeft,
                                                                    QAudioFormat::FrontRight,
                                                                    QAudioFormat::FrontCenter,
                                                                    QAudioFormat::LFE,
                                                                    QAudioFormat::BackLeft,
                                                                    QAudioFormat::BackRight,
                                                                    QAudioFormat::TopFrontLeft,
                                                                    QAudioFormat::TopFrontRight) },

    { kAudioChannelLayoutTag_ITU_2_1, QAudioFormat::ChannelConfig2Dot1 },
    { kAudioChannelLayoutTag_ITU_2_2, QAudioFormat::channelConfig(QAudioFormat::FrontLeft,
                                                                  QAudioFormat::FrontRight,
                                                                  QAudioFormat::BackLeft,
                                                                  QAudioFormat::BackRight) }
};


QAudioFormat::ChannelConfig CoreAudioUtils::fromAudioChannelLayout(const AudioChannelLayout *layout)
{
    for (const auto &m : layoutTagMap) {
        if (m.tag == layout->mChannelLayoutTag)
            return m.channelConfig;
    }

    quint32 channels = 0;
    if (layout->mChannelLayoutTag == kAudioChannelLayoutTag_UseChannelDescriptions) {
        // special case 1 and 2 channel configs, as they are often reported without proper descriptions
        if (layout->mNumberChannelDescriptions == 1
            && (layout->mChannelDescriptions[0].mChannelLabel == kAudioChannelLabel_Unknown
                || layout->mChannelDescriptions[0].mChannelLabel == kAudioChannelLabel_Mono))
            return QAudioFormat::ChannelConfigMono;
        if (layout->mNumberChannelDescriptions == 2 &&
            layout->mChannelDescriptions[0].mChannelLabel == kAudioChannelLabel_Unknown &&
            layout->mChannelDescriptions[1].mChannelLabel == kAudioChannelLabel_Unknown)
            return QAudioFormat::ChannelConfigStereo;

        for (uint i = 0; i < layout->mNumberChannelDescriptions; ++i) {
            const auto channelLabel = layout->mChannelDescriptions[i].mChannelLabel;
            if (channelLabel == kAudioChannelLabel_Unknown) {
                // Any number of unknown channel labels occurs for loopback audio devices.
                // E.g. the case is reproduced with installed software Soundflower.
                continue;
            }

            const auto found = std::find_if(channelMap, std::end(channelMap),
                                            [channelLabel](const auto &labelWithPos) {
                                                return labelWithPos.label == channelLabel;
                                            });

            if (found == std::end(channelMap))
                qWarning() << "audio device has unrecognized channel, index:" << i
                           << "label:" << channelLabel;
            else
                channels |= QAudioFormat::channelConfig(found->pos);
        }
    } else {
        qWarning() << "Channel layout uses unimplemented format, channelLayoutTag:"
                   << layout->mChannelLayoutTag;
    }
    return QAudioFormat::ChannelConfig(channels);
}

// QAudioRingBuffer
CoreAudioRingBuffer::CoreAudioRingBuffer(int bufferSize):
    m_bufferSize(bufferSize)
{
    m_buffer = new char[m_bufferSize];
    reset();
}

CoreAudioRingBuffer::~CoreAudioRingBuffer()
{
    delete[] m_buffer;
}

CoreAudioRingBuffer::Region CoreAudioRingBuffer::acquireReadRegion(int size)
{
    const int used = m_bufferUsed.fetchAndAddAcquire(0);

    if (used > 0) {
        const int readSize = qMin(size, qMin(m_bufferSize - m_readPos, used));

        return readSize > 0 ? Region(m_buffer + m_readPos, readSize) : Region(0, 0);
    }

    return Region(0, 0);
}

void CoreAudioRingBuffer::releaseReadRegion(const CoreAudioRingBuffer::Region &region)
{
    m_readPos = (m_readPos + region.second) % m_bufferSize;

    m_bufferUsed.fetchAndAddRelease(-region.second);
}

CoreAudioRingBuffer::Region CoreAudioRingBuffer::acquireWriteRegion(int size)
{
    const int free = m_bufferSize - m_bufferUsed.fetchAndAddAcquire(0);

    Region output;

    if (free > 0) {
        const int writeSize = qMin(size, qMin(m_bufferSize - m_writePos, free));
        output =  writeSize > 0 ? Region(m_buffer + m_writePos, writeSize) : Region(0, 0);
    } else {
        output = Region(0, 0);
    }
#ifdef QT_DEBUG_COREAUDIO
    qDebug("acquireWriteRegion(%d) free: %d returning Region(%p, %d)", size, free, output.first, output.second);
#endif
    return output;
}
void CoreAudioRingBuffer::releaseWriteRegion(const CoreAudioRingBuffer::Region &region)
{
    m_writePos = (m_writePos + region.second) % m_bufferSize;

    m_bufferUsed.fetchAndAddRelease(region.second);
#ifdef QT_DEBUG_COREAUDIO
    qDebug("releaseWriteRegion(%p,%d): m_writePos:%d", region.first, region.second, m_writePos);
#endif
}

int CoreAudioRingBuffer::used() const
{
    return m_bufferUsed.loadRelaxed();
}

int CoreAudioRingBuffer::free() const
{
    return m_bufferSize - m_bufferUsed.loadRelaxed();
}

int CoreAudioRingBuffer::size() const
{
    return m_bufferSize;
}

void CoreAudioRingBuffer::reset()
{
    m_readPos = 0;
    m_writePos = 0;
    m_bufferUsed.storeRelaxed(0);
}

QT_END_NAMESPACE