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 \sa QAudioOutput
58*/
59
60QAudioInput::QAudioInput(QObject *parent) : QAudioInput(QMediaDevices::defaultAudioInput(), parent)
61{
62}
63
64QAudioInput::QAudioInput(const QAudioDevice &device, QObject *parent)
65 : QObject(parent)
66{
67 auto maybeAudioInput = QPlatformMediaIntegration::instance()->createAudioInput(this);
68 if (maybeAudioInput) {
69 d = maybeAudioInput.value();
70 d->device = device.mode() == QAudioDevice::Input ? device : QMediaDevices::defaultAudioInput();
71 d->setAudioDevice(d->device);
72 } else {
73 d = new QPlatformAudioInput(nullptr);
74 qWarning() << "Failed to initialize QAudioInput" << maybeAudioInput.error();
75 }
76}
77
78QAudioInput::~QAudioInput()
79{
80 setDisconnectFunction({});
81 delete d;
82}
83
84/*!
85 \qmlproperty real QtMultimedia::AudioInput::volume
86
87 The volume is scaled linearly, ranging from \c 0 (silence) to \c 1 (full volume).
88 \note values outside this range will be clamped.
89
90 By default the volume is \c 1.
91
92 UI volume controls should usually be scaled non-linearly. For example,
93 using a logarithmic scale will produce linear changes in perceived loudness,
94 which is what a user would normally expect from a volume control.
95 \sa QtAudio::convertVolume()
96*/
97/*!
98 \property QAudioInput::volume
99
100 The property returns the volume of the audio input.
101*/
102float QAudioInput::volume() const
103{
104 return d->volume;
105}
106
107void QAudioInput::setVolume(float volume)
108{
109 volume = qBound(0., volume, 1.);
110 if (d->volume == volume)
111 return;
112 d->volume = volume;
113 d->setVolume(volume);
114 emit volumeChanged(volume);
115}
116
117/*!
118 \qmlproperty bool QtMultimedia::AudioInput::muted
119
120 This property holds whether the audio input is muted.
121
122 Defaults to \c{false}.
123*/
124
125/*!
126 \property QAudioInput::muted
127 \brief The muted state of the current media.
128
129 The value will be \c true if the input is muted; otherwise \c false.
130*/
131bool QAudioInput::isMuted() const
132{
133 return d->muted;
134}
135
136void QAudioInput::setMuted(bool muted)
137{
138 if (d->muted == muted)
139 return;
140 d->muted = muted;
141 d->setMuted(muted);
142 emit mutedChanged(muted);
143}
144
145/*!
146 \qmlproperty AudioDevice QtMultimedia::AudioInput::device
147
148 This property describes the audio device connected to this input.
149
150 The device property represents the audio device this input is connected to.
151 This property can be used to select an output device from the
152 QtMultimedia::MediaDevices::audioInputs() list.
153*/
154
155/*!
156 \property QAudioInput::device
157 \brief The audio device connected to this input.
158
159 The device property represents the audio device connected to this input.
160 This property can be used to select an input device from the
161 QMediaDevices::audioInputs() list.
162
163 You can select the system default audio input by setting this property to
164 a default constructed QAudioDevice object.
165*/
166QAudioDevice QAudioInput::device() const
167{
168 return d->device;
169}
170
171void QAudioInput::setDevice(const QAudioDevice &device)
172{
173 auto dev = device;
174 if (dev.isNull())
175 dev = QMediaDevices::defaultAudioInput();
176 if (dev.mode() != QAudioDevice::Input)
177 return;
178 if (d->device == dev)
179 return;
180 d->device = dev;
181 d->setAudioDevice(dev);
182 emit deviceChanged();
183}
184
185/*!
186 \internal
187*/
188void QAudioInput::setDisconnectFunction(std::function<void()> disconnectFunction)
189{
190 if (d->disconnectFunction) {
191 auto df = d->disconnectFunction;
192 d->disconnectFunction = {};
193 df();
194 }
195 d->disconnectFunction = std::move(disconnectFunction);
196}
197
198#include "moc_qaudioinput.cpp"