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
audiooverview.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2021 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/*!
5
\page audiooverview.html
6
\title Audio Overview
7
\inlineimage sound-wave-small.jpg
8
{Sound wave}
9
\brief Playback, recording and processing of Audio.
10
\ingroup explanations-graphicsandmultimedia
11
12
\section1 Audio Features
13
14
Qt Multimedia offers a range of audio classes that cover both low and
15
high level approaches to: audio input, output and processing.
16
17
\section1 Audio Implementation Details
18
19
\section2 Playing Compressed Audio
20
21
For playing media or audio files that are not simple, uncompressed audio, you
22
can use the QMediaPlayer C++ class, or the \l{MediaPlayer} QML type.
23
The QMediaPlayer class and associated QML types are also capable of playing
24
\l{multimedia-playing-video}{video}, if required.
25
26
See \l{Supported Media Formats} for more detail.
27
28
The media player needs to be connected to a QAudioOutput object (or the QML AudioOutput
29
element) to play back audio.
30
31
Here is how you play a local file using C++:
32
33
\snippet multimedia-snippets/media.cpp Local playback
34
35
The same functionality in QML:
36
37
\qml
38
MediaPlayer {
39
audioOutput: AudioOutput {}
40
source: "file:///path/to/my/music.mp3"
41
Component.onCompleted: { play() }
42
}
43
\endqml
44
45
\section2 Recording Audio to a File
46
47
To record audio to a file, you need to create a capture session and connect to it an audio
48
input and a recorder. These elements are implemented with the QMediaCaptureSession,
49
QAudioInput, and QMediaRecorder classes. The default constructed QAudioInput selects the
50
system default audio input. The recorder controls the recording process with a simple record()
51
and stop() functions. Additionally, you can use it to select the output location, audio
52
encoder, or file container format.
53
54
A session recording audio from the default microphone would look as follows in C++:
55
56
\snippet multimedia-snippets/media.cpp Media recorder
57
58
In QML, the same can be achieved by:
59
60
\qml
61
CaptureSession {
62
audioInput: AudioInput {}
63
mediaRecorder: MediaRecorder {
64
id: recorder
65
outputLocation: "file:///path/to/test.mp3"
66
}
67
Component.onCompleted: { recorder.record() }
68
}
69
\endqml
70
71
QMediaCaptureSession also provides support for more complex use cases such as image
72
capturing or video recording.
73
74
\section2 Low Latency Sound Effects
75
76
In addition to \l{raw access} to sound devices, the QSoundEffect
77
class (and \l{SoundEffect} QML type) offers a more abstract way to play
78
sounds. This class allows you to specify a \b{WAV format} file, which can
79
then be played with low latency when necessary.
80
81
You can adjust the:
82
\list
83
\li \l{QSoundEffect::loopCount()}{Number of loops} in which a sound effect
84
is played.
85
\li \l{QSoundEffect::setVolume()}{Volume} of the sound effect.
86
\li \l{QSoundEffect::setMuted()}{Muting} of the sound effect.
87
\endlist
88
89
\target raw access
90
\section2 Low Level Audio Playback and Recording
91
92
The C++ API of Qt Multimedia offers classes for raw access to audio input and output
93
facilities, allowing applications to receive raw data from devices like
94
microphones, and to write raw data to speakers or other devices. Generally
95
these classes do not do any audio decoding, or other processing, but they
96
can support different types of raw audio data.
97
98
The QAudioSink class offers raw audio data output, while QAudioSource
99
offers raw audio data input. The available hardware
100
determines what audio outputs and inputs are available.
101
102
\section3 Push and Pull
103
The low level audio classes can operate in two modes - \c push and \c pull.
104
In \c pull mode, the audio device is started by giving it a QIODevice. For
105
an output device, the QAudioSink class will pull data from the QIODevice
106
(using \l QIODevice::read()) when more audio data is required. Conversely,
107
for \c pull mode with QAudioSource, when audio data is available then the
108
data will be written directly to the QIODevice.
109
110
In \c push mode, the audio device provides a QIODevice instance that
111
can be written or read to as needed. Typically, this results in simpler
112
code but more buffering, which may affect latency.
113
114
\section2 Decoding Compressed Audio to Memory
115
116
In some cases you may want to decode a compressed audio file and do further
117
processing yourself. For example, mixing multiple samples or using custom
118
digital signal processing algorithms. QAudioDecoder supports decoding local
119
files or data streams from QIODevice instances.
120
121
Here's an example of decoding a local file:
122
123
\snippet multimedia-snippets/audio.cpp Local audio decoding
124
125
\section2 Spatial Audio
126
127
The \l {Qt Spatial Audio} module provides an API for implementation sound
128
fields in 3D space.
129
130
\section1 Reference Documentation
131
132
\section2 C++ Classes
133
134
\annotatedlist multimedia_audio
135
136
\section2 QML Types
137
138
\annotatedlist multimedia_audio_qml
139
140
\section2 Examples
141
142
\annotatedlist audio_examples
143
*/
qtmultimedia
src
multimedia
doc
src
audiooverview.qdoc
Generated on
for Qt by
1.14.0