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
qaudioinput.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <qaudioinput.h>
5#include <qaudiodevice.h>
6#include <qmediadevices.h>
7#include <private/qplatformaudioinput_p.h>
8#include <private/qplatformmediaintegration_p.h>
9
10#include <utility>
11
12/*!
13 \qmltype AudioInput
14 \nativetype QAudioInput
15 \brief An audio input to be used for capturing audio in a capture session.
16
17 \inqmlmodule QtMultimedia
18 \ingroup multimedia_qml
19 \ingroup multimedia_audio_qml
20
21 \qml
22 CaptureSession {
23 id: playMusic
24 audioInput: AudioInput {
25 volume: slider.value
26 }
27 recorder: MediaRecorder { ... }
28 }
29 Slider {
30 id: slider
31 from: 0.
32 to: 1.
33 }
34 \endqml
35
36 You can use AudioInput together with a QtMultiMedia::CaptureSession to capture audio from an
37 audio input device.
38
39 \sa Camera, AudioOutput
40*/
41
42/*!
43 \class QAudioInput
44 \brief Represents an input channel for audio.
45 \inmodule QtMultimedia
46 \ingroup multimedia
47 \ingroup multimedia_audio
48
49 This class represents an input channel that can be used together with
50 QMediaCaptureSession. It enables the selection of the physical input device
51 to be used, muting the channel, and changing the channel's volume.
52
53 \note On WebAssembly platform, due to its asynchronous nature,
54 QMediaDevices::audioInputsChanged() signal is emitted when the list of
55 audio inputs is ready. User permissions are required. Works only on secure https contexts.
56*/
57
58QAudioInput::QAudioInput(QObject *parent) : QAudioInput(QMediaDevices::defaultAudioInput(), parent)
59{
60}
61
62QAudioInput::QAudioInput(const QAudioDevice &device, QObject *parent)
63 : QObject(parent)
64{
65 auto maybeAudioInput = QPlatformMediaIntegration::instance()->createAudioInput(this);
66 if (maybeAudioInput) {
67 d = maybeAudioInput.value();
68 d->device = device.mode() == QAudioDevice::Input ? device : QMediaDevices::defaultAudioInput();
69 d->setAudioDevice(d->device);
70 } else {
71 d = new QPlatformAudioInput(nullptr);
72 qWarning() << "Failed to initialize QAudioInput" << maybeAudioInput.error();
73 }
74}
75
76QAudioInput::~QAudioInput()
77{
78 setDisconnectFunction({});
79 delete d;
80}
81
82/*!
83 \qmlproperty real QtMultimedia::AudioInput::volume
84
85 The volume is scaled linearly, ranging from \c 0 (silence) to \c 1 (full volume).
86 \note values outside this range will be clamped.
87
88 By default the volume is \c 1.
89
90 UI volume controls should usually be scaled non-linearly. For example,
91 using a logarithmic scale will produce linear changes in perceived loudness,
92 which is what a user would normally expect from a volume control.
93 \sa QtAudio::convertVolume()
94*/
95/*!
96 \property QAudioInput::volume
97
98 The property returns the volume of the audio input.
99*/
100float QAudioInput::volume() const
101{
102 return d->volume;
103}
104
105void QAudioInput::setVolume(float volume)
106{
107 volume = qBound(0., volume, 1.);
108 if (d->volume == volume)
109 return;
110 d->volume = volume;
111 d->setVolume(volume);
112 emit volumeChanged(volume);
113}
114
115/*!
116 \qmlproperty bool QtMultimedia::AudioInput::muted
117
118 This property holds whether the audio input is muted.
119
120 Defaults to \c{false}.
121*/
122
123/*!
124 \property QAudioInput::muted
125 \brief The muted state of the current media.
126
127 The value will be \c true if the input is muted; otherwise \c false.
128*/
129bool QAudioInput::isMuted() const
130{
131 return d->muted;
132}
133
134void QAudioInput::setMuted(bool muted)
135{
136 if (d->muted == muted)
137 return;
138 d->muted = muted;
139 d->setMuted(muted);
140 emit mutedChanged(muted);
141}
142
143/*!
144 \qmlproperty AudioDevice QtMultimedia::AudioInput::device
145
146 This property describes the audio device connected to this input.
147
148 The device property represents the audio device this input is connected to.
149 This property can be used to select an output device from the
150 QtMultimedia::MediaDevices::audioInputs() list.
151*/
152
153/*!
154 \property QAudioInput::device
155 \brief The audio device connected to this input.
156
157 The device property represents the audio device connected to this input.
158 This property can be used to select an input device from the
159 QMediaDevices::audioInputs() list.
160
161 You can select the system default audio input by setting this property to
162 a default constructed QAudioDevice object.
163*/
164QAudioDevice QAudioInput::device() const
165{
166 return d->device;
167}
168
169void QAudioInput::setDevice(const QAudioDevice &device)
170{
171 auto dev = device;
172 if (dev.isNull())
173 dev = QMediaDevices::defaultAudioInput();
174 if (dev.mode() != QAudioDevice::Input)
175 return;
176 if (d->device == dev)
177 return;
178 d->device = dev;
179 d->setAudioDevice(dev);
180 emit deviceChanged();
181}
182
183/*!
184 \internal
185*/
186void QAudioInput::setDisconnectFunction(std::function<void()> disconnectFunction)
187{
188 if (d->disconnectFunction) {
189 auto df = d->disconnectFunction;
190 d->disconnectFunction = {};
191 df();
192 }
193 d->disconnectFunction = std::move(disconnectFunction);
194}
195
196#include "moc_qaudioinput.cpp"