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