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
17QnxAudioDeviceInfo::QnxAudioDeviceInfo(const QByteArray &deviceName, QAudioDevice::Mode mode)
18 : QAudioDevicePrivate(deviceName, mode)
19{
20 isDefault = id.contains("Preferred");
21
22 preferredFormat.setChannelCount(mode == QAudioDevice::Input ? 1 : 2);
23
24 description = QString::fromUtf8(id);
25
26 minimumChannelCount = 1;
27 maximumChannelCount = 2;
28
29 const std::optional<snd_pcm_channel_info_t> info = pcmChannelInfo(id, mode);
30
31 if (!info)
32 return;
33
34 minimumSampleRate = info->min_rate;
35 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 <= maximumSampleRate && rate >= minimumSampleRate) {
41 preferredFormat.setSampleRate(rate);
42 break;
43 }
44 }
45
46 if (info->formats & SND_PCM_FMT_U8) {
47 supportedSampleFormats << QAudioFormat::UInt8;
48 preferredFormat.setSampleFormat(QAudioFormat::UInt8);
49 }
50
51 if (info->formats & SND_PCM_FMT_S16) {
52 supportedSampleFormats << QAudioFormat::Int16;
53 preferredFormat.setSampleFormat(QAudioFormat::Int16);
54 }
55
56 if (info->formats & SND_PCM_FMT_S32)
57 supportedSampleFormats << QAudioFormat::Int32;
58
59 if (info->formats & SND_PCM_FMT_FLOAT)
60 supportedSampleFormats << QAudioFormat::Float;
61}
62
63QnxAudioDeviceInfo::~QnxAudioDeviceInfo()
64{
65}
66
67bool QnxAudioDeviceInfo::isFormatSupported(const QAudioFormat &format) const
68{
69 const HandleUniquePtr handle = openPcmDevice(id, mode);
70
71 if (!handle)
72 return false;
73
74 const std::optional<snd_pcm_channel_info_t> info = pcmChannelInfo(handle.get(), mode);
75
76 if (!info)
77 return false;
78
79 snd_pcm_channel_params_t params = formatToChannelParams(format, mode, info->max_fragment_size);
80 const int errorCode = snd_pcm_plugin_params(handle.get(), &params);
81
82 return errorCode == 0;
83}
84
85QT_END_NAMESPACE