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