summaryrefslogtreecommitdiffstats
path: root/src/multimedia/doc/src/audiooverview.qdoc
blob: af5d000d822ab96916c8c1b111a156e4f2a787d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

/*!
\page audiooverview.html
\title Audio Overview
\inlineimage sound-wave-small.jpg
\brief Playback, recording and processing of Audio.

\section1 Audio Features

Qt Multimedia offers a range of audio classes that cover both low and
high level approaches to: audio input, output and processing.

\section1 Audio Implementation Details

\section2 Playing Compressed Audio

For playing media or audio files that are not simple, uncompressed audio, you
can use the QMediaPlayer C++ class, or the \l{MediaPlayer} QML type.
The QMediaPlayer class and associated QML types are also capable of playing
\l{multimedia-playing-video}{video}, if required.

See \l{Supported Media Formats} for more detail.

The media player needs to be connected to a QAudioOutput object (or the QML AudioOutput
element) to play back audio.

Here is how you play a local file using C++:

    \snippet multimedia-snippets/media.cpp Local playback

The same functionality in QML:

\qml
MediaPlayer {
    audioOutput: AudioOutput {}
    source: "file:///path/to/my/music.mp3"
    Component.onCompleted: { play() }
}
\endqml

\section2 Recording Audio to a File

To record audio to a file, you need to create a capture session and connect to it an audio
input and a recorder. These elements are implemented with the QMediaCaptureSession,
QAudioInput, and QMediaRecorder classes. The default constructed QAudioInput selects the
system default audio input. The recorder controls the recording process with a simple record()
and stop() functions. Additionally, you can use it to select the output location, audio
encoder, or file container format.

A session recording audio from the default microphone would look as follows in C++:

    \snippet multimedia-snippets/media.cpp Media recorder

In QML, the same can be achieved by:

\qml
CaptureSession {
    audioInput: AudioInput {}
    mediaRecorder: MediaRecorder {
        id: recorder
        outputLocation: "file:///path/to/test.mp3"
    }
    Component.onCompleted: { recorder.record() }
}
\endqml

QMediaCaptureSession also provides support for more complex use cases such as image
capturing or video recording.

\section2 Low Latency Sound Effects

In addition to \l{raw access} to sound devices, the QSoundEffect
class (and \l{SoundEffect} QML type) offers a more abstract way to play
sounds. This class allows you to specify a \b{WAV format} file, which can
then be played with low latency when necessary.

You can adjust the:
\list
    \li \l{QSoundEffect::loopCount()}{Number of loops} in which a sound effect
    is played.
    \li \l{QSoundEffect::setVolume()}{Volume} of the sound effect.
    \li \l{QSoundEffect::setMuted()}{Muting} of the sound effect.
\endlist

\target raw access
\section2 Low Level Audio Playback and Recording

The C++ API of Qt Multimedia offers classes for raw access to audio input and output
facilities, allowing applications to receive raw data from devices like
microphones, and to write raw data to speakers or other devices. Generally
these classes do not do any audio decoding, or other processing, but they
can support different types of raw audio data.

The QAudioSink class offers raw audio data output, while QAudioSource
offers raw audio data input. The available hardware
determines what audio outputs and inputs are available.

\section3 Push and Pull
The low level audio classes can operate in two modes - \c push and \c pull.
In \c pull mode, the audio device is started by giving it a QIODevice.  For
an output device, the QAudioSink class will pull data from the QIODevice
(using \l QIODevice::read()) when more audio data is required.  Conversely,
for \c pull mode with QAudioSource, when audio data is available then the
data will be written directly to the QIODevice.

In \c push mode, the audio device provides a QIODevice instance that
can be written or read to as needed. Typically, this results in simpler
code but more buffering, which may affect latency.

\section2 Decoding Compressed Audio to Memory

In some cases you may want to decode a compressed audio file and do further
processing yourself. For example, mixing multiple samples or using custom
digital signal processing algorithms. QAudioDecoder supports decoding local
files or data streams from QIODevice instances.

Here's an example of decoding a local file:

    \snippet multimedia-snippets/audio.cpp Local audio decoding

\section2 Spatial Audio

The \l {Qt Spatial Audio} module provides an API for implementation sound
fields in 3D space.

\section1 Reference Documentation

\section2 C++ Classes

\annotatedlist multimedia_audio

\section2 QML Types

\annotatedlist multimedia_audio_qml

\section2 Examples

\annotatedlist audio_examples
*/