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
5\page audiooverview.html
7\inlineimage sound-wave-small.jpg
8\brief Playback, recording and processing of Audio.
10\section1 Audio Features
12Qt Multimedia offers a range of audio classes that cover both low and
13high level approaches to: audio input, output and processing.
15\section1 Audio Implementation Details
17\section2 Playing Compressed Audio
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.
24See \l{Supported Media Formats} for more detail.
26The media player needs to be connected to a QAudioOutput object (or the QML AudioOutput
27element) to play back audio.
29Here is how you play a local file using C++:
31 \snippet multimedia-snippets/media.cpp Local playback
33The same functionality in QML:
37 audioOutput: AudioOutput {}
38 source: "file:///path/to/my/music.mp3"
39 Component.onCompleted: { play() }
43\section2 Recording Audio to a File
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.
52A session recording audio from the default microphone would look as follows in C++:
54 \snippet multimedia-snippets/media.cpp Media recorder
56In QML, the same can be achieved by:
60 audioInput: AudioInput {}
61 mediaRecorder: MediaRecorder {
63 outputLocation: "file:///path/to/test.mp3"
65 Component.onCompleted: { recorder.record() }
69QMediaCaptureSession also provides support for more complex use cases such as image
70capturing or video recording.
72\section2 Low Latency Sound Effects
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.
81 \li \l{QSoundEffect::loopCount()}{Number of loops} in which a sound effect
83 \li \l{QSoundEffect::setVolume()}{Volume} of the sound effect.
84 \li \l{QSoundEffect::setMuted()}{Muting} of the sound effect.
88\section2 Low Level Audio Playback and Recording
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.
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.
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.
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.
112\section2 Decoding Compressed Audio to Memory
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.
119Here's an example of decoding a local file:
121 \snippet multimedia-snippets/audio.cpp Local audio decoding
123\section2 Spatial Audio
125The \l {Qt Spatial Audio} module provides an API for implementation sound
128\section1 Reference Documentation
132\annotatedlist multimedia_audio
136\annotatedlist multimedia_audio_qml
140\annotatedlist audio_examples