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