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