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