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