Audio Overview¶
Audio playback, recording and processing
Audio Features¶
Qt Multimedia offers a range of audio classes, covering both low and high level approaches to audio input, output and processing. In addition to traditional audio usage, the Qt Audio Engine QML types offer high level 3D positional audio for QML applications. See that documentation for more information.
Audio Implementation Details¶
Playing Compressed Audio¶
For playing media or audio files that are not simple, uncompressed audio, you can use the
QMediaPlayerC++ class, or the Audio and MediaPlayer QML types. TheQMediaPlayerclass and associated QML types are also capable of playing video , if required. The compressed audio formats supported does depend on the operating system environment, and also what media plugins the user may have installed.Here is how you play a local file using C++:
player = new QMediaPlayer; // ... player->setMedia(QUrl::fromLocalFile("/Users/me/Music/coolsong.mp3")); player->setVolume(50); player->play();You can also put files (even remote URLs) into a playlist:
player = new QMediaPlayer; playlist = new QMediaPlaylist(player); playlist->addMedia(QUrl("http://example.com/myfile1.mp3")); playlist->addMedia(QUrl("http://example.com/myfile2.mp3")); // ... playlist->setCurrentIndex(1); player->play();
Recording Audio to a File¶
For recording audio to a file, the
QAudioRecorderclass allows you to compress audio data from an input device and record it.audioRecorder = new QAudioRecorder; QAudioEncoderSettings audioSettings; audioSettings.setCodec("audio/amr"); audioSettings.setQuality(QMultimedia::HighQuality); audioRecorder->setEncodingSettings(audioSettings); audioRecorder->setOutputLocation(QUrl::fromLocalFile("test.amr")); audioRecorder->record();
Low Latency Sound Effects¶
In addition to the raw access to sound devices described above, the
QSoundEffectclass (and SoundEffect QML type) offers a slightly higher level way to play sounds. These classes allow you to specify a WAV format file which can then be played with low latency when necessary. BothQSoundEffectand SoundEffect have essentially the same API.You can adjust the number of
loopsa sound effect is played, as well as thevolume(ormuting) of the effect.For older, Qt 4.x based applications
QSoundis also available. Applications are recommended to useQSoundEffectwhere possible.
Monitoring Audio Data During Playback or Recording¶
The
QAudioProbeclass allows you to monitor audio data being played or recorded in the higher level classes likeQMediaPlayer,QCameraandQAudioRecorder. After creating your high level class, you can simply set the source of the probe to your class, and receive audio buffers as they are processed. This is useful for several audio processing tasks, particularly for visualization or adjusting gain. You cannot modify the buffers, and they may arrive at a slightly different time than the media pipeline processes them.Here’s an example of installing a probe during recording:
audioRecorder = new QAudioRecorder; QAudioEncoderSettings audioSettings; audioSettings.setCodec("audio/amr"); audioSettings.setQuality(QMultimedia::HighQuality); audioRecorder->setEncodingSettings(audioSettings); audioRecorder->setOutputLocation(QUrl::fromLocalFile("test.amr")); audioProbe = new QAudioProbe(this); if (audioProbe->setSource(audioRecorder)) { // Probing succeeded, audioProbe->isValid() should be true. connect(audioProbe, SIGNAL(audioBufferProbed(QAudioBuffer)), this, SLOT(calculateLevel(QAudioBuffer))); } audioRecorder->record(); // Now audio buffers being recorded should be signaled // by the probe, so we can do things like calculating the // audio power level, or performing a frequency transform
Low Level Audio Playback and Recording¶
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
QAudioOutputclass offers raw audio data output, whileQAudioInputoffers raw audio data input. Both classes have adjustable buffers and latency, so they are suitable for both low latency use cases (like games or VOIP) and high latency (like music playback). The available hardware determines what audio outputs and inputs are available.
Push and Pull¶
The low level audio classes can operate in two modes -
pushandpull. Inpullmode, the audio device is started by giving it aQIODevice. For an output device, theQAudioOutputclass will pull data from theQIODevice(usingread()) when more audio data is required. Conversely, forpullmode withQAudioInput, when audio data is available then the data will be written directly to theQIODevice.In
pushmode, the audio device provides aQIODeviceinstance that can be written or read to as needed. Typically this results in simpler code but more buffering, which may affect latency.
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).
QAudioDecodersupports decoding local files or data streams fromQIODeviceinstances.Here’s an example of decoding a local file:
QAudioFormat desiredFormat; desiredFormat.setChannelCount(2); desiredFormat.setCodec("audio/x-raw"); desiredFormat.setSampleType(QAudioFormat::UnSignedInt); desiredFormat.setSampleRate(48000); desiredFormat.setSampleSize(16); QAudioDecoder *decoder = new QAudioDecoder(this); decoder->setAudioFormat(desiredFormat); decoder->setSourceFilename("level1.mp3"); connect(decoder, SIGNAL(bufferReady()), this, SLOT(readBuffer())); decoder->start(); // Now wait for bufferReady() signal and call decoder->read()
Examples¶
There are both C++ and QML examples available.
C++ Examples¶
Reference Documentation¶
C++ Classes¶
qaudio.html
The QAudio namespace contains enums used by the audio classes.
PySide2.QtMultimedia.QAudioBufferThe QAudioBuffer class represents a collection of audio samples with a specific format and sample rate.
QAudioBuffer.StereoFrameThe StereoFrame class provides a simple wrapper for a stereo audio frame.
PySide2.QtMultimedia.QAudioDecoderThe QAudioDecoder class allows decoding audio.
PySide2.QtMultimedia.QAudioDeviceInfoThe QAudioDeviceInfo class provides an interface to query audio devices and their functionality.
PySide2.QtMultimedia.QAudioFormatThe QAudioFormat class stores audio stream parameter information.
PySide2.QtMultimedia.QAudioInputThe QAudioInput class provides an interface for receiving audio data from an audio input device.
PySide2.QtMultimedia.QAudioOutputThe QAudioOutput class provides an interface for sending audio data to an audio output device.
PySide2.QtMultimedia.QAudioProbeThe QAudioProbe class allows you to monitor audio being played or recorded.
PySide2.QtMultimedia.QAbstractAudioDeviceInfoThe QAbstractAudioDeviceInfo class is a base class for audio backends.
QAudioSystemPluginThe QAudioSystemPlugin class provides an abstract base for audio plugins.
PySide2.QtMultimedia.QSoundThe QSound class provides a method to play .wav sound files.
PySide2.QtMultimedia.QSoundEffectThe QSoundEffect class provides a way to play low latency sound effects.
QML Types¶
qml-qtmultimedia-audio.html
Add audio playback to a scene.
qml-qtmultimedia-mediaplayer.html
Add media playback to a scene.
qml-qtmultimedia-playlistitem.html
Defines an item in a Playlist.
qml-qtmultimedia-playlist.html
For specifying a list of media to be played.
qml-qtmultimedia-soundeffect.html
The SoundEffect type provides a way to play sound effects in QML.
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.