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
14Qt Multimedia offers a range of audio classes that cover both low and
15high level approaches to: audio input, output and processing.
16
17\section1 Audio Implementation Details
18
19\section2 Playing Compressed Audio
20
21For playing media or audio files that are not simple, uncompressed audio, you
22can use the QMediaPlayer C++ class, or the \l{MediaPlayer} QML type.
23The QMediaPlayer class and associated QML types are also capable of playing
24\l{multimedia-playing-video}{video}, if required.
25
26See \l{Supported Media Formats} for more detail.
27
28The media player needs to be connected to a QAudioOutput object (or the QML AudioOutput
29element) to play back audio.
30
31Here is how you play a local file using C++:
32
33 \snippet multimedia-snippets/media.cpp Local playback
34
35The same functionality in QML:
36
37\qml
38MediaPlayer {
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
47To record audio to a file, you need to create a capture session and connect to it an audio
48input and a recorder. These elements are implemented with the QMediaCaptureSession,
49QAudioInput, and QMediaRecorder classes. The default constructed QAudioInput selects the
50system default audio input. The recorder controls the recording process with a simple record()
51and stop() functions. Additionally, you can use it to select the output location, audio
52encoder, or file container format.
53
54A session recording audio from the default microphone would look as follows in C++:
55
56 \snippet multimedia-snippets/media.cpp Media recorder
57
58In QML, the same can be achieved by:
59
60\qml
61CaptureSession {
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
71QMediaCaptureSession also provides support for more complex use cases such as image
72capturing or video recording.
73
74\section2 Low Latency Sound Effects
75
76In addition to \l{raw access} to sound devices, the QSoundEffect
77class (and \l{SoundEffect} QML type) offers a more abstract way to play
78sounds. This class allows you to specify a \b{WAV format} file, which can
79then be played with low latency when necessary.
80
81You 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 Input and Output
91
92The C++ API of Qt Multimedia offers classes for raw access to audio input and output
93facilities, allowing applications to receive raw data from devices like
94microphones, and to write raw data to speakers or other devices. Generally
95these classes do not do any audio decoding, or other processing, but they
96can support different types of raw audio data.
97
98The QAudioSink class offers raw audio data output, while QAudioSource
99offers raw audio data input. The available hardware
100determines what audio outputs and inputs are available.
101
102\section3 Push and Pull using QIODevice
103The low level audio classes can operate in two modes - \c push and \c pull.
104In \c pull mode, the audio device is started by giving it a QIODevice. For
105an output device, the QAudioSink class will pull data from the QIODevice
106(using \l QIODevice::read()) when more audio data is required. Conversely,
107for \c pull mode with QAudioSource, when audio data is available then the
108data will be written directly to the QIODevice.
109
110In \c push mode, the audio device provides a QIODevice instance that
111can be written or read to as needed.
112
113 \note The QIODevice does not immediately access the audio device, but
114 instead buffers the data internally. This means that the QIODevice
115 can be used to write or read data at any time from the application
116 thread and buffering of typically 250ms is added. If the application
117 is not delivering audio data to the QIODevice quickly enough (for QAudioSink)
118 or is not reading the QIODevice quickly enough, audio dropouts will occur.
119
120
121\section3 Callback-based Interface
122
123In addition to using a QIODevice based interface, the low level audio classes provide
124a callback-based interface that allows users to register a callback which is called
125on the audio thread whenever the audio device needs or delivers more data. This allows
126much lower latency audio processing, as the application can process the data directly
127on the audio thread.
128
129\snippet multimedia-snippets/audio.cpp Audio callback output setup sine
130
131\qtmmaudiocallbacksupportednote
132\qtmmaudiocallbacknote
133
134\section2 Decoding Compressed Audio to Memory
135
136In some cases you may want to decode a compressed audio file and do further
137processing yourself. For example, mixing multiple samples or using custom
138digital signal processing algorithms. QAudioDecoder supports decoding local
139files or data streams from QIODevice instances.
140
141Here's an example of decoding a local file:
142
143 \snippet multimedia-snippets/audio.cpp Local audio decoding
144
145\section2 Spatial Audio
146
147The \l {Qt Spatial Audio} module provides an API for implementation sound
148fields in 3D space.
149
150\section1 Reference Documentation
151
152\section2 C++ Classes
153
154\annotatedlist multimedia_audio
155
156\section2 QML Types
157
158\annotatedlist multimedia_audio_qml
159
160\section2 Examples
161
162\annotatedlist audio_examples
163*/