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