Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqnxaudiodevice.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 Research In Motion
2// Copyright (C) 2021 The Qt Company
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
6
8
9#include <array>
10
11#include <sys/asoundlib.h>
12
13using namespace QnxAudioUtils;
14
15QT_BEGIN_NAMESPACE
16
17namespace {
18
20createQnxAudioDeviceFormatFromDeviceName(const QByteArray &deviceName, QAudioDevice::Mode mode)
21{
22 QAudioDevicePrivate::AudioDeviceFormat format;
23
24 format.preferredFormat.setChannelCount(mode == QAudioDevice::Input ? 1 : 2);
25
26 format.minimumChannelCount = 1;
27 format.maximumChannelCount = 2;
28
29 const std::optional<snd_pcm_channel_info_t> info = pcmChannelInfo(deviceName, mode);
30
31 if (!info)
32 return format;
33
34 format.minimumSampleRate = info->min_rate;
35 format.maximumSampleRate = info->max_rate;
36
37 constexpr std::array sampleRates { 48000, 44100, 22050, 16000, 11025, 8000 };
38
39 for (int rate : sampleRates) {
40 if (rate <= format.maximumSampleRate && rate >= format.minimumSampleRate) {
41 format.preferredFormat.setSampleRate(rate);
42 break;
43 }
44 }
45
46 if (info->formats & SND_PCM_FMT_U8) {
47 format.supportedSampleFormats << QAudioFormat::UInt8;
48 format.preferredFormat.setSampleFormat(QAudioFormat::UInt8);
49 }
50
51 if (info->formats & SND_PCM_FMT_S16) {
52 format.supportedSampleFormats << QAudioFormat::Int16;
53 format.preferredFormat.setSampleFormat(QAudioFormat::Int16);
54 }
55
56 if (info->formats & SND_PCM_FMT_S32)
57 format.supportedSampleFormats << QAudioFormat::Int32;
58
59 if (info->formats & SND_PCM_FMT_FLOAT)
60 format.supportedSampleFormats << QAudioFormat::Float;
61
62 return format;
63}
64
65} // namespace
66
67QnxAudioDeviceInfo::QnxAudioDeviceInfo(const QByteArray &deviceName, QAudioDevice::Mode mode)
68 : QAudioDevicePrivate(deviceName, mode, QString::fromUtf8(deviceName),
69 deviceName.contains("Preferred"),
70 createQnxAudioDeviceFormatFromDeviceName(deviceName, mode))
71{
72}
73
74QnxAudioDeviceInfo::~QnxAudioDeviceInfo()
75{
76}
77
78bool QnxAudioDeviceInfo::isFormatSupported(const QAudioFormat &format) const
79{
80 const HandleUniquePtr handle = openPcmDevice(id, mode);
81
82 if (!handle)
83 return false;
84
85 const std::optional<snd_pcm_channel_info_t> info = pcmChannelInfo(handle.get(), mode);
86
87 if (!info)
88 return false;
89
90 snd_pcm_channel_params_t params = formatToChannelParams(format, mode, info->max_fragment_size);
91 const int errorCode = snd_pcm_plugin_params(handle.get(), &params);
92
93 return errorCode == 0;
94}
95
96QT_END_NAMESPACE
QAudioDevicePrivate::AudioDeviceFormat createQnxAudioDeviceFormatFromDeviceName(const QByteArray &deviceName, QAudioDevice::Mode mode)
std::unique_ptr< snd_pcm_t, HandleDeleter > HandleUniquePtr