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