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