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