QAudioFormat Class

The QAudioFormat class stores audio stream parameter information. More...

Header: #include <QAudioFormat>
CMake: find_package(Qt6 REQUIRED COMPONENTS Multimedia)
target_link_libraries(mytarget PRIVATE Qt6::Multimedia)
qmake: QT += multimedia

Public Types

enum AudioChannelPosition { UnknownPosition, FrontLeft, FrontRight, FrontCenter, LFE, …, BottomFrontRight }
enum ChannelConfig { ChannelConfigUnknown, ChannelConfigMono, ChannelConfigStereo, ChannelConfig2Dot1, ChannelConfig3Dot0, …, ChannelConfigSurround7Dot1 }
enum SampleFormat { Unknown, UInt8, Int16, Int32, Float }

Public Functions

QAudioFormat()
QAudioFormat(const QAudioFormat &other)
~QAudioFormat()
qint32 bytesForDuration(qint64 microseconds) const
qint32 bytesForFrames(qint32 frameCount) const
int bytesPerFrame() const
int bytesPerSample() const
QAudioFormat::ChannelConfig channelConfig() const
int channelCount() const
int channelOffset(QAudioFormat::AudioChannelPosition channel) const
qint64 durationForBytes(qint32 bytes) const
qint64 durationForFrames(qint32 frameCount) const
qint32 framesForBytes(qint32 byteCount) const
qint32 framesForDuration(qint64 microseconds) const
bool isValid() const
float normalizedSampleValue(const void *sample) const
QAudioFormat::SampleFormat sampleFormat() const
int sampleRate() const
void setChannelConfig(QAudioFormat::ChannelConfig config)
void setChannelCount(int channels)
void setSampleFormat(QAudioFormat::SampleFormat format)
void setSampleRate(int samplerate)

Static Public Members

QAudioFormat::ChannelConfig channelConfig(Args... channels)
QAudioFormat::ChannelConfig defaultChannelConfigForChannelCount(int channelCount)
bool operator!=(const QAudioFormat &a, const QAudioFormat &b)
bool operator==(const QAudioFormat &a, const QAudioFormat &b)

Detailed Description

An audio format specifies how data in a raw audio stream is arranged. For example, how the stream is to be interpreted.

QAudioFormat contains parameters that specify how the audio sample data is arranged. These are the frequency, the number of channels, and the sample format. The following table describes these in more detail.

ParameterDescription
Sample RateSamples per second of audio data in Hertz.
Number of channelsThe number of audio channels (typically one for mono or two for stereo). These are the amount of consecutive samples that together form one frame in the stream
Sample formatThe format of the audio samples in the stream

This class is used in conjunction with QAudioSource or QAudioSink to allow you to specify the parameters of the audio stream being read or written, or with QAudioBuffer when dealing with samples in memory.

You can obtain audio formats compatible with the audio device used through functions in QAudioDevice. This class also lets you query available parameter values for a device, so that you can set the parameters yourself. See the QAudioDevice class description for details. You need to know the format of the audio streams you wish to play or record.

Samples for all channels will be interleaved. One sample for each channel for the same instant in time is referred to as a frame in Qt Multimedia (and other places).

Member Type Documentation

enum QAudioFormat::AudioChannelPosition

Describes the possible audio channel positions. These follow the standard definition used in the 22.2 surround sound configuration.

ConstantValueDescription
QAudioFormat::UnknownPosition0Unknown position
QAudioFormat::FrontLeft1 
QAudioFormat::FrontRight2 
QAudioFormat::FrontCenter3 
QAudioFormat::LFE4Low Frequency Effect channel (Subwoofer)
QAudioFormat::BackLeft5 
QAudioFormat::BackRight6 
QAudioFormat::FrontLeftOfCenter7 
QAudioFormat::FrontRightOfCenter8 
QAudioFormat::BackCenter9 
QAudioFormat::LFE219 
QAudioFormat::SideLeft10 
QAudioFormat::SideRight11 
QAudioFormat::TopFrontLeft13 
QAudioFormat::TopFrontRight15 
QAudioFormat::TopFrontCenter14 
QAudioFormat::TopCenter12 
QAudioFormat::TopBackLeft16 
QAudioFormat::TopBackRight18 
QAudioFormat::TopSideLeft20 
QAudioFormat::TopSideRight21 
QAudioFormat::TopBackCenter17 
QAudioFormat::BottomFrontCenter22 
QAudioFormat::BottomFrontLeft23 
QAudioFormat::BottomFrontRight24 

enum QAudioFormat::ChannelConfig

This enum describes a standardized audio channel layout. The most common configurations are Mono, Stereo, 2.1 (stereo plus low frequency), 5.1 surround, and 7.1 surround configurations.

ConstantValueDescription
QAudioFormat::ChannelConfigUnknown0The channel configuration is not known.
QAudioFormat::ChannelConfigMonoQtPrivate::channelConfig(FrontCenter)The audio has one Center channel.
QAudioFormat::ChannelConfigStereoQtPrivate::channelConfig(FrontLeft, FrontRight)The audio has two channels, Left and Right.
QAudioFormat::ChannelConfig2Dot1QtPrivate::channelConfig(FrontLeft, FrontRight, LFE)The audio has three channels, Left, Right and LFE (low frequency effect).
QAudioFormat::ChannelConfig3Dot0QtPrivate::channelConfig(FrontLeft, FrontRight, FrontCenter)The audio has three channels, Left, Right, and Center.
QAudioFormat::ChannelConfig3Dot1QtPrivate::channelConfig(FrontLeft, FrontRight, FrontCenter, LFE)The audio has four channels, Left, Right, Center, and LFE (low frequency effect).
QAudioFormat::ChannelConfigSurround5Dot0QtPrivate::channelConfig(FrontLeft, FrontRight, FrontCenter, BackLeft, BackRight)The audio has five channels, Left, Right, Center, BackLeft, and BackRight.
QAudioFormat::ChannelConfigSurround5Dot1QtPrivate::channelConfig(FrontLeft, FrontRight, FrontCenter, LFE, BackLeft, BackRight)The audio has 6 channels, Left, Right, Center, LFE, BackLeft, and BackRight.
QAudioFormat::ChannelConfigSurround7Dot0QtPrivate::channelConfig(FrontLeft, FrontRight, FrontCenter, BackLeft, BackRight, SideLeft, SideRight)The audio has 7 channels, Left, Right, Center, BackLeft, BackRight, SideLeft, and SideRight.
QAudioFormat::ChannelConfigSurround7Dot1QtPrivate::channelConfig(FrontLeft, FrontRight, FrontCenter, LFE, BackLeft, BackRight, SideLeft, SideRight)The audio has 8 channels, Left, Right, Center, LFE, BackLeft, BackRight, SideLeft, and SideRight.

enum QAudioFormat::SampleFormat

Qt will always expect and use samples in the endianness of the host platform. When processing audio data from external sources yourself, ensure you convert them to the correct endianness before writing them to a QAudioSink or QAudioBuffer.

ConstantValueDescription
QAudioFormat::Unknown0Not Set
QAudioFormat::UInt81Samples are 8 bit unsigned integers
QAudioFormat::Int162Samples are 16 bit signed integers
QAudioFormat::Int323Samples are 32 bit signed integers
QAudioFormat::Float4Samples are floats

Member Function Documentation

[default] QAudioFormat::QAudioFormat()

Constructs a new audio format.

Values are initialized as follows:

[default] QAudioFormat::QAudioFormat(const QAudioFormat &other)

Construct a new audio format using other.

[noexcept default] QAudioFormat::~QAudioFormat()

Destroy this audio format.

qint32 QAudioFormat::bytesForDuration(qint64 microseconds) const

Returns the number of bytes required for this audio format for microseconds.

Returns 0 if this format is not valid.

Note that some rounding may occur if microseconds is not an exact fraction of the sampleRate().

See also durationForBytes().

qint32 QAudioFormat::bytesForFrames(qint32 frameCount) const

Returns the number of bytes required for frameCount frames of this format.

Returns 0 if this format is not valid.

See also bytesForDuration().

[constexpr] int QAudioFormat::bytesPerFrame() const

Returns the number of bytes required to represent one frame (a sample in each channel) in this format.

Returns 0 if this format is invalid.

[constexpr noexcept] int QAudioFormat::bytesPerSample() const

Returns the number of bytes required to represent one sample in this format.

Returns 0 if this format is invalid.

[constexpr noexcept] QAudioFormat::ChannelConfig QAudioFormat::channelConfig() const

Returns the current channel configuration.

See also setChannelConfig().

[static constexpr] template <typename... Args> QAudioFormat::ChannelConfig QAudioFormat::channelConfig(Args... channels)

Returns the current channel configuration for the given channels.

[constexpr noexcept] int QAudioFormat::channelCount() const

Returns the current channel count value.

See also setChannelCount().

[noexcept] int QAudioFormat::channelOffset(QAudioFormat::AudioChannelPosition channel) const

Returns the position of a certain audio channel inside an audio frame for the given format. Returns -1 if the channel does not exist for this format or the channel configuration is unknown.

[static] QAudioFormat::ChannelConfig QAudioFormat::defaultChannelConfigForChannelCount(int channelCount)

Returns a default channel configuration for channelCount.

Default configurations are defined for up to 8 channels, and correspond to standard Mono, Stereo and Surround configurations. For higher channel counts, this simply uses the first channelCount audio channels defined in QAudioFormat::AudioChannelPosition.

qint64 QAudioFormat::durationForBytes(qint32 bytes) const

Returns the number of microseconds represented by bytes in this format.

Returns 0 if this format is not valid.

Note that some rounding may occur if bytes is not an exact multiple of the number of bytes per frame.

See also bytesForDuration().

qint64 QAudioFormat::durationForFrames(qint32 frameCount) const

Return the number of microseconds represented by frameCount frames in this format.

qint32 QAudioFormat::framesForBytes(qint32 byteCount) const

Returns the number of frames represented by byteCount in this format.

Note that some rounding may occur if byteCount is not an exact multiple of the number of bytes per frame.

Each frame has one sample per channel.

See also framesForDuration().

qint32 QAudioFormat::framesForDuration(qint64 microseconds) const

Returns the number of frames required to represent microseconds in this format.

Note that some rounding may occur if microseconds is not an exact fraction of the sampleRate().

[constexpr noexcept] bool QAudioFormat::isValid() const

Returns true if all of the parameters are valid.

float QAudioFormat::normalizedSampleValue(const void *sample) const

Normalizes the sample value to a number between -1 and 1. The method depends on the QaudioFormat.

[constexpr noexcept] QAudioFormat::SampleFormat QAudioFormat::sampleFormat() const

Returns the current sample format.

See also setSampleFormat().

[constexpr noexcept] int QAudioFormat::sampleRate() const

Returns the current sample rate in Hertz.

See also setSampleRate().

[noexcept] void QAudioFormat::setChannelConfig(QAudioFormat::ChannelConfig config)

Sets the channel configuration to config.

Sets the channel configuration of the audio format to one of the standard audio channel configurations.

Note: that this will also modify the channel count.

See also channelConfig().

[constexpr noexcept] void QAudioFormat::setChannelCount(int channels)

Sets the channel count to channels. Setting this also sets the channel config to ChannelConfigUnknown.

See also channelCount().

[constexpr noexcept] void QAudioFormat::setSampleFormat(QAudioFormat::SampleFormat format)

Sets the sample format to format.

See also sampleFormat() and QAudioFormat::SampleFormat.

[constexpr noexcept] void QAudioFormat::setSampleRate(int samplerate)

Sets the sample rate to samplerate in Hertz.

See also sampleRate().

Related Non-Members

bool operator!=(const QAudioFormat &a, const QAudioFormat &b)

Returns true if audio format a is not equal to b, otherwise returns false.

bool operator==(const QAudioFormat &a, const QAudioFormat &b)

Returns true if audio format a is equal to b, otherwise returns false.