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