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