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 Playback and Recording
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
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. Typically, this results in simpler
112code but more buffering, which may affect latency.
113
114\section2 Decoding Compressed Audio to Memory
115
116In some cases you may want to decode a compressed audio file and do further
117processing yourself. For example, mixing multiple samples or using custom
118digital signal processing algorithms. QAudioDecoder supports decoding local
119files or data streams from QIODevice instances.
120
121Here's an example of decoding a local file:
122
123 \snippet multimedia-snippets/audio.cpp Local audio decoding
124
125\section2 Spatial Audio
126
127The \l {Qt Spatial Audio} module provides an API for implementation sound
128fields 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*/