6#include <QtCore/private/qcore_mac_p.h>
8#include <QtMultimedia/private/qcoreaudioutils_p.h>
9#include <QtMultimedia/private/qaudioformat_p.h>
11#include <QtMultimedia/private/qmacosaudiodatautils_p.h>
21 QAudioDevice::Mode mode,
22 QAudioFormat::ChannelConfig channelConfig)
25 format.setSampleRate(44100);
26 format.setSampleFormat(QAudioFormat::Int16);
27 format.setChannelCount(mode == QAudioDevice::Input ? 1 : 2);
28 format.setChannelConfig(channelConfig);
34 return (mode == QAudioDevice::Input) ? QAudioFormat::ChannelConfigMono : QAudioFormat::ChannelConfigStereo;
39 return QString::fromUtf8(id);
44[[nodiscard]] std::optional<QAudioFormat> qGetPreferredFormatForCoreAudioDevice(
45 QAudioDevice::Mode mode,
46 AudioDeviceID deviceId)
48 using namespace QCoreAudioUtils;
50 const auto audioDevicePropertyStreamsAddress =
51 makePropertyAddress(kAudioDevicePropertyStreams, mode);
52 const auto audioDevicePhysicalFormatPropertyAddress =
53 makePropertyAddress(kAudioStreamPropertyPhysicalFormat, mode);
56 getAudioPropertyList<AudioStreamID>(deviceId, audioDevicePropertyStreamsAddress);
57 if (!streamIDs || streamIDs->empty())
60 for (
auto streamID : *streamIDs) {
61 auto streamDescription = getAudioProperty<AudioStreamBasicDescription>(
62 streamID, audioDevicePhysicalFormatPropertyAddress);
63 if (!streamDescription)
66 QAudioFormat fmt = QCoreAudioUtils::toPreferredQAudioFormat(*streamDescription);
73[[nodiscard]] std::optional<QAudioFormat::ChannelConfig> qGetChannelLayoutForCoreAudioDevice(
74 QAudioDevice::Mode mode,
75 AudioDeviceID deviceId)
77 using namespace QCoreAudioUtils;
79 const auto propertyAddress =
80 makePropertyAddress(kAudioDevicePropertyPreferredChannelLayout, mode);
82 if (
auto layout = getAudioPropertyWithFlexibleArrayMember<AudioChannelLayout>(deviceId, propertyAddress))
83 return QCoreAudioUtils::fromAudioChannelLayout(layout.get());
88[[nodiscard]] std::optional<QString> qGetDescriptionForCoreAudioDevice(
89 QAudioDevice::Mode mode,
90 AudioDeviceID deviceId)
92 using namespace QCoreAudioUtils;
94 const auto propertyAddress = makePropertyAddress(kAudioObjectPropertyName, mode);
95 if (
auto name = getAudioProperty<QCFString>(deviceId, propertyAddress))
101[[nodiscard]] std::optional<
int> qSupportedNumberOfChannels(
102 QAudioDevice::Mode mode,
103 AudioDeviceID deviceId)
105 using namespace QCoreAudioUtils;
107 const auto audioDevicePropertyStreamsAddress =
108 makePropertyAddress(kAudioDevicePropertyStreams, mode);
110 auto streamIDs = getAudioPropertyList<AudioStreamID>(deviceId, audioDevicePropertyStreamsAddress);
114 const auto propVirtualFormat = makePropertyAddress(kAudioStreamPropertyVirtualFormat, mode);
118 for (
auto streamID : *streamIDs) {
119 auto streamDescription = getAudioProperty<AudioStreamBasicDescription>(streamID, propVirtualFormat);
120 if (!streamDescription)
122 ret += streamDescription->mChannelsPerFrame;
134static QString getDescription(AudioDeviceID id,
const QByteArray &device, QAudioDevice::Mode mode)
136 if (
auto optionalDescription = qGetDescriptionForCoreAudioDevice(mode, id))
137 return *optionalDescription;
138 return qGetDefaultDescription(device);
141QCoreAudioDeviceInfo::QCoreAudioDeviceInfo(AudioDeviceID id,
const QByteArray &device, QAudioDevice::Mode mode):
145 getDescription(id, device, mode),
148 const std::optional<QAudioFormat::ChannelConfig> channelConfigOpt =
149 qGetChannelLayoutForCoreAudioDevice(mode, id);
150 if (channelConfigOpt.has_value())
151 channelConfiguration = channelConfigOpt.value();
153 channelConfiguration = qGetDefaultChannelLayout(mode);
155 const std::optional<QAudioFormat> preferredFormatOpt =
156 qGetPreferredFormatForCoreAudioDevice(mode, id);
157 if (preferredFormatOpt.has_value())
158 preferredFormat = preferredFormatOpt.value();
160 preferredFormat = qDefaultPreferredFormat(mode, channelConfiguration);
162 minimumSampleRate = QtMultimediaPrivate::allSupportedSampleRates.front();
163 maximumSampleRate = QtMultimediaPrivate::allSupportedSampleRates.back();
164 minimumChannelCount = 1;
165 maximumChannelCount = qSupportedNumberOfChannels(mode, id).value_or(16);
167 supportedSampleFormats = qAllSupportedSampleFormats();
172QCoreAudioDeviceInfo::QCoreAudioDeviceInfo(
const QByteArray &device, QAudioDevice::Mode mode)
173 : QAudioDevicePrivate(device, mode, qGetDefaultDescription(device))
175 channelConfiguration = qGetDefaultChannelLayout(mode);
176 preferredFormat = qDefaultPreferredFormat(mode, channelConfiguration);
178 minimumSampleRate = 1;
179 maximumSampleRate = 96000;
180 minimumChannelCount = 1;
181 maximumChannelCount = 16;
182 supportedSampleFormats = qAllSupportedSampleFormats();
QAudioFormat::ChannelConfig qGetDefaultChannelLayout(QAudioDevice::Mode mode)
QAudioFormat qDefaultPreferredFormat(QAudioDevice::Mode mode, QAudioFormat::ChannelConfig channelConfig)
QString qGetDefaultDescription(const QByteArray &id)