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
qsoundeffect.cpp
Go to the documentation of this file.
1
// Copyright (C) 2016 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
"qsoundeffect.h"
5
6
#
include
<
QtMultimedia
/
private
/
qaudiosystem_p
.
h
>
7
#
include
<
QtMultimedia
/
private
/
qplatformaudiodevices_p
.
h
>
8
#
include
<
QtMultimedia
/
private
/
qplatformmediaintegration_p
.
h
>
9
#
include
<
QtMultimedia
/
private
/
qsamplecache_p
.
h
>
10
#
include
<
QtMultimedia
/
private
/
qsoundeffectsynchronous_p
.
h
>
11
#
include
<
QtMultimedia
/
private
/
qsoundeffectwithplayer_p
.
h
>
12
#
include
<
QtMultimedia
/
private
/
qtmultimediaglobal_p
.
h
>
13
#
include
<
QtMultimedia
/
qaudiobuffer
.
h
>
14
#
include
<
QtMultimedia
/
qaudiodevice
.
h
>
15
#
include
<
QtMultimedia
/
qaudiosink
.
h
>
16
#
include
<
QtMultimedia
/
qmediadevices
.
h
>
17
#
include
<
QtCore
/
qfuture
.
h
>
18
#
include
<
QtCore
/
qloggingcategory
.
h
>
19
20
QT_BEGIN_NAMESPACE
21
22
Q_LOGGING_CATEGORY(qLcSoundEffect,
"qt.multimedia.soundeffect"
)
23
24
namespace
{
25
26
QSoundEffectPrivate
*
makeSoundEffectPrivate
(QSoundEffect *fx,
const
QAudioDevice &audioDevice)
27
{
28
bool
hasCallbackApi = QPlatformMediaIntegration::instance()->audioDevices()->hasCallbackApi();
29
if
(hasCallbackApi)
30
return
new
QtMultimediaPrivate::QSoundEffectPrivateWithPlayer(fx, audioDevice);
31
else
32
return
new
QSoundEffectPrivateSynchronous(fx, audioDevice);
33
}
34
35
}
// namespace
36
37
/*!
38
\class QSoundEffect
39
\brief The QSoundEffect class provides a way to play low latency sound effects.
40
41
\ingroup multimedia
42
\ingroup multimedia_audio
43
\inmodule QtMultimedia
44
45
This class allows you to play uncompressed audio files (typically WAV files) in
46
a generally lower latency way, and is suitable for "feedback" type sounds in
47
response to user actions (e.g. virtual keyboard sounds, positive or negative
48
feedback for popup dialogs, or game sounds). If low latency is not important,
49
consider using the QMediaPlayer class instead, since it supports a wider
50
variety of media formats and is less resource intensive.
51
52
This example shows how a looping, somewhat quiet sound effect
53
can be played:
54
55
\snippet multimedia-snippets/qsound.cpp 2
56
57
Typically the sound effect should be reused, which allows all the
58
parsing and preparation to be done ahead of time, and only triggered
59
when necessary. This assists with lower latency audio playback.
60
61
\snippet multimedia-snippets/qsound.cpp 3
62
63
Since QSoundEffect requires slightly more resources to achieve lower
64
latency playback, the platform may limit the number of simultaneously playing
65
sound effects.
66
*/
67
68
69
/*!
70
\qmltype SoundEffect
71
\nativetype QSoundEffect
72
\brief The SoundEffect type provides a way to play sound effects in QML.
73
74
\ingroup multimedia_qml
75
\ingroup multimedia_audio_qml
76
\inqmlmodule QtMultimedia
77
78
This type allows you to play uncompressed audio files (typically WAV files) in
79
a generally lower latency way, and is suitable for "feedback" type sounds in
80
response to user actions (e.g. virtual keyboard sounds, positive or negative
81
feedback for popup dialogs, or game sounds). If low latency is not important,
82
consider using the MediaPlayer type instead, since it support a wider
83
variety of media formats and is less resource intensive.
84
85
Typically the sound effect should be reused, which allows all the
86
parsing and preparation to be done ahead of time, and only triggered
87
when necessary. This is easy to achieve with QML, since you can declare your
88
SoundEffect instance and refer to it elsewhere.
89
90
The following example plays a WAV file on mouse click.
91
92
\snippet multimedia-snippets/soundeffect.qml complete snippet
93
94
\note QSoundEffect only supports mono or stereo sound files. Using sound files with
95
a sampling rate of 48000hz is recommended, as this is the typical native sampling rate
96
on most platforms (\l QAudioDevice::preferredFormat()).
97
*/
98
99
/*!
100
Creates a QSoundEffect with the given \a parent.
101
*/
102
QSoundEffect::QSoundEffect(QObject *parent)
103
: QSoundEffect(QAudioDevice(), parent)
104
{
105
}
106
107
/*!
108
Creates a QSoundEffect with the given \a audioDevice and \a parent.
109
*/
110
QSoundEffect::QSoundEffect(
const
QAudioDevice &audioDevice, QObject *parent)
111
: QObject(*makeSoundEffectPrivate(
this
, audioDevice), parent)
112
{
113
}
114
115
/*!
116
Destroys this sound effect.
117
*/
118
QSoundEffect::~QSoundEffect()
119
{
120
stop();
121
}
122
123
/*!
124
\fn QSoundEffect::supportedMimeTypes()
125
126
Returns a list of the supported mime types for this platform.
127
*/
128
QStringList QSoundEffect::supportedMimeTypes()
129
{
130
// Only return supported mime types if we have a audio device available
131
const
QList<QAudioDevice> devices = QMediaDevices::audioOutputs();
132
if
(devices.isEmpty())
133
return
QStringList();
134
135
using
namespace
Qt::Literals;
136
static
const
QStringList mimeTypes{
137
u"audio/aiff"_s,
138
u"audio/vnd.wave"_s,
139
u"audio/wav"_s,
140
u"audio/wave"_s,
141
u"audio/x-aiff"_s
142
u"audio/x-pn-wav"_s,
143
u"audio/x-wav"_s,
144
};
145
146
return
mimeTypes;
147
}
148
149
/*!
150
\qmlproperty url QtMultimedia::SoundEffect::source
151
152
This property holds the url for the sound to play. For the SoundEffect
153
to attempt to load the source, the URL must exist and the application must have read permission
154
in the specified directory. If the desired source is a local file the URL may be specified
155
using either absolute or relative (to the file that declared the SoundEffect) pathing.
156
*/
157
/*!
158
\property QSoundEffect::source
159
160
This property holds the url for the sound to play. For the SoundEffect
161
to attempt to load the source, the URL must exist and the application must have read permission
162
in the specified directory.
163
*/
164
165
/*! Returns the URL of the current source to play */
166
QUrl QSoundEffect::source()
const
167
{
168
Q_D(
const
QSoundEffect);
169
170
return
d->url();
171
}
172
173
/*! Set the current URL to play to \a url. */
174
void
QSoundEffect::setSource(
const
QUrl &url)
175
{
176
Q_D(QSoundEffect);
177
178
qCDebug(qLcSoundEffect) <<
this
<<
"setSource current="
<< d->url() <<
", to="
<< url;
179
if
(d->url() == url)
180
return
;
181
stop();
182
183
d->resolveAndSetSource(url, *QSampleCache::instance());
184
185
emit sourceChanged();
186
}
187
188
/*!
189
\qmlproperty int QtMultimedia::SoundEffect::loops
190
191
This property holds the number of times the sound is played. A value of 0 or 1 means
192
the sound will be played only once; set to SoundEffect.Infinite to enable infinite looping.
193
194
The value can be changed while the sound effect is playing, in which case it will update
195
the remaining loops to the new value.
196
*/
197
198
/*!
199
\property QSoundEffect::loops
200
This property holds the number of times the sound is played. A value of 0 or 1 means
201
the sound will be played only once; set to SoundEffect.Infinite to enable infinite looping.
202
203
The value can be changed while the sound effect is playing, in which case it will update
204
the remaining loops to the new value.
205
*/
206
207
/*!
208
Returns the total number of times that this sound effect will be played before stopping.
209
210
See the \l loopsRemaining() method for the number of loops currently remaining.
211
*/
212
int
QSoundEffect::loopCount()
const
213
{
214
Q_D(
const
QSoundEffect);
215
return
d->loopCount();
216
}
217
218
/*!
219
\enum QSoundEffect::Loop
220
221
\value Infinite Used as a parameter to \l setLoopCount() for infinite looping
222
*/
223
224
/*!
225
Set the total number of times to play this sound effect to \a loopCount.
226
227
Setting the loop count to 0 or 1 means the sound effect will be played only once;
228
pass \c QSoundEffect::Infinite to repeat indefinitely. The loop count can be changed while
229
the sound effect is playing, in which case it will update the remaining loops to
230
the new \a loopCount.
231
232
\sa loopsRemaining()
233
*/
234
void
QSoundEffect::setLoopCount(
int
loopCount)
235
{
236
Q_D(QSoundEffect);
237
238
if
(loopCount < 0 && loopCount != Infinite) {
239
qWarning(
"SoundEffect: loops should be SoundEffect.Infinite, 0 or positive integer"
);
240
return
;
241
}
242
243
if
(d->setLoopCount(loopCount))
244
emit loopCountChanged();
245
}
246
247
/*!
248
\property QSoundEffect::audioDevice
249
250
Returns the QAudioDevice instance.
251
*/
252
QAudioDevice QSoundEffect::audioDevice()
253
{
254
Q_D(
const
QSoundEffect);
255
return
d->audioDevice();
256
}
257
258
void
QSoundEffect::setAudioDevice(
const
QAudioDevice &device)
259
{
260
Q_D(QSoundEffect);
261
262
qCDebug(qLcSoundEffect) <<
this
<<
"setAudioDevice:"
<< device.description();
263
264
if
(d->setAudioDevice(device))
265
emit audioDeviceChanged();
266
}
267
268
/*!
269
\qmlproperty int QtMultimedia::SoundEffect::loopsRemaining
270
271
This property contains the number of loops remaining before the sound effect
272
stops by itself, or SoundEffect.Infinite if that's what has been set in \l loops.
273
*/
274
/*!
275
\property QSoundEffect::loopsRemaining
276
277
This property contains the number of loops remaining before the sound effect
278
stops by itself, or QSoundEffect::Infinite if that's what has been set in \l loops.
279
*/
280
int
QSoundEffect::loopsRemaining()
const
281
{
282
Q_D(
const
QSoundEffect);
283
return
d->loopsRemaining();
284
}
285
286
/*!
287
\qmlproperty real QtMultimedia::SoundEffect::volume
288
289
This property holds the volume of the sound effect playback.
290
291
The volume is scaled linearly from \c 0.0 (silence) to \c 1.0 (full volume). Values outside this
292
range will be clamped.
293
294
The default volume is \c 1.0.
295
296
UI volume controls should usually be scaled non-linearly. For example, using a logarithmic scale
297
will produce linear changes in perceived loudness, which is what a user would normally expect
298
from a volume control. See \l {QtAudio::convertVolume()}{convertVolume()}
299
for more details.
300
*/
301
/*!
302
\property QSoundEffect::volume
303
304
This property holds the volume of the sound effect playback, from 0.0 (silence) to 1.0 (full volume).
305
*/
306
307
/*!
308
Returns the current volume of this sound effect, from 0.0 (silent) to 1.0 (maximum volume).
309
*/
310
float
QSoundEffect::volume()
const
311
{
312
Q_D(
const
QSoundEffect);
313
return
d->volume();
314
}
315
316
/*!
317
Sets the sound effect volume to \a volume.
318
319
The volume is scaled linearly from \c 0.0 (silence) to \c 1.0 (full volume). Values outside this
320
range will be clamped.
321
322
The default volume is \c 1.0.
323
324
UI volume controls should usually be scaled non-linearly. For example, using a logarithmic scale
325
will produce linear changes in perceived loudness, which is what a user would normally expect
326
from a volume control. See QtAudio::convertVolume() for more details.
327
*/
328
void
QSoundEffect::setVolume(
float
volume)
329
{
330
Q_D(QSoundEffect);
331
if
(d->setVolume(volume))
332
emit volumeChanged();
333
}
334
335
/*!
336
\qmlproperty bool QtMultimedia::SoundEffect::muted
337
338
This property provides a way to control muting. A value of \c true will mute this effect.
339
Otherwise, playback will occur with the currently specified \l volume.
340
*/
341
/*!
342
\property QSoundEffect::muted
343
344
This property provides a way to control muting. A value of \c true will mute this effect.
345
*/
346
347
/*! Returns whether this sound effect is muted */
348
bool
QSoundEffect::isMuted()
const
349
{
350
Q_D(
const
QSoundEffect);
351
return
d->muted();
352
}
353
354
/*!
355
Sets whether to mute this sound effect's playback.
356
357
If \a muted is true, playback will be muted (silenced),
358
and otherwise playback will occur with the currently
359
specified volume().
360
*/
361
void
QSoundEffect::setMuted(
bool
muted)
362
{
363
Q_D(QSoundEffect);
364
if
(d->setMuted(muted))
365
emit mutedChanged();
366
}
367
368
/*!
369
\fn QSoundEffect::isLoaded() const
370
371
Returns whether the sound effect has finished loading the \l source().
372
*/
373
/*!
374
\qmlmethod bool QtMultimedia::SoundEffect::isLoaded()
375
376
Returns whether the sound effect has finished loading the \l source.
377
*/
378
bool
QSoundEffect::isLoaded()
const
379
{
380
Q_D(
const
QSoundEffect);
381
return
d->status() == QSoundEffect::Ready;
382
}
383
384
/*!
385
\qmlmethod void QtMultimedia::SoundEffect::play()
386
387
Start playback of the sound effect, looping the effect for the number of
388
times as specified in the loops property.
389
390
This is the default method for SoundEffect.
391
392
\snippet multimedia-snippets/soundeffect.qml play sound on click
393
*/
394
/*!
395
\fn QSoundEffect::play()
396
397
Start playback of the sound effect, looping the effect for the number of
398
times as specified in the loops property.
399
*/
400
void
QSoundEffect::play()
401
{
402
Q_D(QSoundEffect);
403
d->play();
404
}
405
406
/*!
407
\qmlproperty bool QtMultimedia::SoundEffect::playing
408
409
This property indicates whether the sound effect is playing or not.
410
*/
411
/*!
412
\property QSoundEffect::playing
413
414
This property indicates whether the sound effect is playing or not.
415
*/
416
417
/*! Returns true if the sound effect is currently playing, or false otherwise */
418
bool
QSoundEffect::isPlaying()
const
419
{
420
Q_D(
const
QSoundEffect);
421
return
d->playing();
422
}
423
424
/*!
425
\enum QSoundEffect::Status
426
427
\value Null No source has been set or the source is null.
428
\value Loading The SoundEffect is trying to load the source.
429
\value Ready The source is loaded and ready for play.
430
\value Error An error occurred during operation, such as failure of loading the source.
431
432
*/
433
434
/*!
435
\qmlproperty enumeration QtMultimedia::SoundEffect::status
436
437
This property indicates the current status of the SoundEffect
438
as enumerated within SoundEffect.
439
Possible statuses are listed below.
440
441
\table
442
\header \li Value \li Description
443
\row \li SoundEffect.Null \li No source has been set or the source is null.
444
\row \li SoundEffect.Loading \li The SoundEffect is trying to load the source.
445
\row \li SoundEffect.Ready \li The source is loaded and ready for play.
446
\row \li SoundEffect.Error \li An error occurred during operation, such as failure of loading the source.
447
\endtable
448
*/
449
/*!
450
\property QSoundEffect::status
451
452
This property indicates the current status of the sound effect
453
from the \l QSoundEffect::Status enumeration.
454
*/
455
456
/*!
457
Returns the current status of this sound effect.
458
*/
459
QSoundEffect::Status QSoundEffect::status()
const
460
{
461
Q_D(
const
QSoundEffect);
462
return
d->status();
463
}
464
465
/*!
466
\qmlmethod void QtMultimedia::SoundEffect::stop()
467
468
Stop current playback.
469
470
*/
471
/*!
472
\fn QSoundEffect::stop()
473
474
Stop current playback.
475
476
*/
477
void
QSoundEffect::stop()
478
{
479
Q_D(QSoundEffect);
480
d->stop();
481
}
482
483
/* Signals */
484
485
/*!
486
\fn void QSoundEffect::sourceChanged()
487
488
The \c sourceChanged signal is emitted when the source has been changed.
489
*/
490
/*!
491
\qmlsignal QtMultimedia::SoundEffect::sourceChanged()
492
493
The \c sourceChanged signal is emitted when the source has been changed.
494
*/
495
/*!
496
\fn void QSoundEffect::loadedChanged()
497
498
The \c loadedChanged signal is emitted when the loading state has changed.
499
*/
500
/*!
501
\qmlsignal QtMultimedia::SoundEffect::loadedChanged()
502
503
The \c loadedChanged signal is emitted when the loading state has changed.
504
*/
505
506
/*!
507
\fn void QSoundEffect::loopCountChanged()
508
509
The \c loopCountChanged signal is emitted when the initial number of loops has changed.
510
*/
511
/*!
512
\qmlsignal QtMultimedia::SoundEffect::loopCountChanged()
513
514
The \c loopCountChanged signal is emitted when the initial number of loops has changed.
515
*/
516
517
/*!
518
\fn void QSoundEffect::loopsRemainingChanged()
519
520
The \c loopsRemainingChanged signal is emitted when the remaining number of loops has changed.
521
*/
522
/*!
523
\qmlsignal QtMultimedia::SoundEffect::loopsRemainingChanged()
524
525
The \c loopsRemainingChanged signal is emitted when the remaining number of loops has changed.
526
*/
527
528
/*!
529
\fn void QSoundEffect::volumeChanged()
530
531
The \c volumeChanged signal is emitted when the volume has changed.
532
*/
533
/*!
534
\qmlsignal QtMultimedia::SoundEffect::volumeChanged()
535
536
The \c volumeChanged signal is emitted when the volume has changed.
537
*/
538
539
/*!
540
\fn void QSoundEffect::mutedChanged()
541
542
The \c mutedChanged signal is emitted when the mute state has changed.
543
*/
544
/*!
545
\qmlsignal QtMultimedia::SoundEffect::mutedChanged()
546
547
The \c mutedChanged signal is emitted when the mute state has changed.
548
*/
549
550
/*!
551
\fn void QSoundEffect::playingChanged()
552
553
The \c playingChanged signal is emitted when the playing property has changed.
554
*/
555
/*!
556
\qmlsignal QtMultimedia::SoundEffect::playingChanged()
557
558
The \c playingChanged signal is emitted when the playing property has changed.
559
*/
560
561
/*!
562
\fn void QSoundEffect::statusChanged()
563
564
The \c statusChanged signal is emitted when the status property has changed.
565
*/
566
/*!
567
\qmlsignal QtMultimedia::SoundEffect::statusChanged()
568
569
The \c statusChanged signal is emitted when the status property has changed.
570
*/
571
572
void
QSoundEffectPrivate::resolveAndSetSource(
const
QUrl &url, QSampleCache &cache)
573
{
574
m_unresolvedUrl = url;
575
QUrl resolvedUrl = m_sourceResolver->resolve(url);
576
setSource(std::move(resolvedUrl), cache);
577
}
578
579
QUrl QSoundEffectPrivate::url()
const
580
{
581
return
m_unresolvedUrl;
582
}
583
584
QSoundEffectPrivate *QSoundEffectPrivate::get(QSoundEffect *sfx)
585
{
586
return
sfx->d_func();
587
}
588
589
QT_END_NAMESPACE
590
591
#
include
"moc_qsoundeffect.cpp"
QSoundEffectPrivate
Definition
qsoundeffect_p.h:33
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
QT_BEGIN_NAMESPACE::makeSoundEffectPrivate
QSoundEffectPrivate * makeSoundEffectPrivate(QSoundEffect *fx, const QAudioDevice &audioDevice)
Definition
qsoundeffect.cpp:26
qtmultimedia
src
multimedia
audio
qsoundeffect.cpp
Generated on
for Qt by
1.16.1