Warning

This section contains snippets that were automatically translated from C++ to Python and may contain errors.

Audio Overview

sound-wave-small1

Playback, recording and processing of Audio.

Audio Features

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

Audio Implementation Details

Playing Compressed Audio

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

See 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++:

player = QMediaPlayer()
audioOutput = QAudioOutput()
player.setAudioOutput(audioOutput)
# ...
player.setSource(QUrl.fromLocalFile("/Users/me/Music/coolsong.mp3"))
audioOutput.setVolume(0.5)
player.play()

The same functionality in QML:

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++:

session = QMediaCaptureSession()
audioInput = QAudioInput()
session.setAudioInput(input)
recorder = QMediaRecorder()
session.setRecorder(recorder)
recorder.setQuality(QMediaRecorder.HighQuality)
recorder.setOutputLocation(QUrl.fromLocalFile("test.mp3"))
recorder.record()

In QML, the same can be achieved by:

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

Low Latency Sound Effects

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

You can adjust the:

Low Level Audio Input and Output

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.

Push and Pull using QIODevice

The low level audio classes can operate in two modes - push and pull. In 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 QIODevice::read()) when more audio data is required. Conversely, for pull mode with QAudioSource , when audio data is available then the data will be written directly to the QIODevice.

In push mode, the audio device provides a QIODevice instance that can be written or read to as needed.

Note

The QIODevice does not immediately access the audio device, but instead buffers the data internally. This means that the QIODevice can be used to write or read data at any time from the application thread and buffering of typically 250ms is added. If the application is not delivering audio data to the QIODevice quickly enough (for QAudioSink ) or is not reading the QIODevice quickly enough, audio dropouts will occur.

Callback-based Interface

In addition to using a QIODevice based interface, the low level audio classes provide a callback-based interface that allows users to register a callback which is called on the audio thread whenever the audio device needs or delivers more data. This allows much lower latency audio processing, as the application can process the data directly on the audio thread.

    format = QAudioFormat()
# Set up the format, eg.
    format.setSampleRate(44100)
    format.setChannelCount(2)
    format.setSampleFormat(QAudioFormat.Float)
    info = QAudioDevice(QMediaDevices.defaultAudioOutput())
    if not info.isFormatSupported(format):
        qWarning() << "Raw audio format not supported by backend, cannot play audio."
        return

    audio = QAudioSink(format, self)
    phaseIncrement = 2 * M_PI * 220.0 / format.sampleRate() # 220 Hz sine wave
    audio.start([phase, phaseIncrement] (QSpan<float> interleavedAudioBuffer) {
    # The audio callback should not call any functions that may potentially be blocking
    # Fill the audio buffer with a sine wave
        sampleCount = interleavedAudioBuffer.size() / 2 # Stereo, so divide by 2
        for i in range(0, sampleCount):
            sample = std::sin(phase)
            interleavedAudioBuffer[i * 2] = sample # Left channel
            interleavedAudioBuffer[i * 2 + 1] = sample # Right channel
            phase += phaseIncrement # Increment phase for next sample

})
if not audio.error() == QtAudio.Error.NoError:
    # in addition to the other start() signatures, starting the audio callback will fail if
    # * the backend does not implement callback-based IO (the API is available on all major
    #   platforms)
    # * the signature of the audio callback does not match format.sampleFormat()
    qWarning() << "Error starting audio output:" << audio.errorString()

Note

This API is only available on platforms that support the callback API: Apple’s CoreAudio (macOS, iOS, etc), Windows, Linux (using the PulseAudio or PipeWire backend) and Android.

Note

The callback will be called on a soft-realtime audio thread. It is important to ensure that the callback does not block, as this can cause audio glitches or dropouts. This includes performing blocking IO, locking mutexes, allocating memories or any other operations that may block. For best practices consult Ross Bencina’s article Real-time audio programming 101: time waits for nothing . Also consider using clang’s Realtime sanitizer to validate the audio callback.

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:

desiredFormat = QAudioFormat()
desiredFormat.setChannelCount(2)
desiredFormat.setSampleFormat(QAudioFormat.Int16)
desiredFormat.setSampleRate(48000)
decoder = QAudioDecoder(self)
decoder.setAudioFormat(desiredFormat)
decoder.setSource("level1.mp3")
decoder.bufferReady.connect(self.readBuffer)
decoder.start()
# Now wait for bufferReady() signal and call decoder->read()

Spatial Audio

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

Reference Documentation

C++ Classes

PySide6.QtMultimedia.QAudioBuffer

The QAudioBuffer class represents a collection of audio samples with a specific format and sample rate.

PySide6.QtMultimedia.QAudioBufferInput

The QAudioBufferInput class is used for providing custom audio buffers to QMediaRecorder through QMediaCaptureSession.

PySide6.QtMultimedia.QAudioBufferOutput

The QAudioBufferOutput class is used for capturing audio data provided by QMediaPlayer.

PySide6.QtMultimedia.QAudioDecoder

The QAudioDecoder class implements decoding audio.

PySide6.QtMultimedia.QAudioDevice

The QAudioDevice class provides an information about audio devices and their functionality.

PySide6.QtMultimedia.QAudioFormat

The QAudioFormat class stores audio stream parameter information.

PySide6.QtMultimedia.QAudioInput

Represents an input channel for audio.

PySide6.QtMultimedia.QAudioOutput

Represents an output channel for audio.

PySide6.QtMultimedia.QAudioSink

The QAudioSink class provides an interface for sending audio data to an audio output device.

PySide6.QtMultimedia.QAudioSource

The QAudioSource class provides an interface for receiving audio data from an audio input device.

PySide6.QtMultimedia.QSoundEffect

The QSoundEffect class provides a way to play low latency sound effects.

qtaudio.html

The QtAudio namespace contains enums used by the audio classes.

PySide6.QtMultimedia.QMediaCaptureSession

The QMediaCaptureSession class allows capturing of audio and video content.

PySide6.QtMultimedia.QMediaRecorder

The QMediaRecorder class is used for encoding and recording a capture session.

QML Types

qml-audiodevice.html

Describes an audio device.

qml-qtmultimedia-audioinput.html

An audio input to be used for capturing audio in a capture session.

qml-qtmultimedia-audiooutput.html

An audio output to be used for playback or monitoring of a capture session.

qml-qtmultimedia-soundeffect.html

The SoundEffect type provides a way to play sound effects in QML.

qml-qtmultimedia-mediaplayer.html

Adds media playback to a scene.

qml-qtmultimedia-playbackoptions.html

Low level media playback options.

qml-mediametadata.html

Provides meta-data for media files.

qml-qtmultimedia-capturesession.html

Allows capturing of audio and video content.

qml-qtmultimedia-mediarecorder.html

For encoding and recording media generated in a CaptureSession.

Examples

Audio-Devices-Example

List available audio devices and their configuration.

Audio-Output-Example

Enabling audio playback using the QAudioSink class.

Audio-Recorder-Example

Discovering the available devices and supported codecs.

Audio-Source-Example

Recording audio using the QAudioSource class.