W3cubDocs

/Qt

QAudioFormat Class

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

Header: #include <QAudioFormat>
CMake: find_package(Qt6 COMPONENTS Multimedia REQUIRED) 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, ChannelConfigSurround5Dot0, …, ChannelConfigSurround7Dot1 }
enum SampleFormat { Unknown, UInt8, Int16, Int32, Float }

Public Functions

QAudioFormat(const QAudioFormat &other)
QAudioFormat()
QAudioFormat & operator=(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)

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.

Parameter Description
Sample Rate Samples per second of audio data in Hertz.
Number of channels The 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 format The 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.

Constant Value Description
QAudioFormat::UnknownPosition 0 Unknown position
QAudioFormat::FrontLeft 1
QAudioFormat::FrontRight 2
QAudioFormat::FrontCenter 3
QAudioFormat::LFE 4 Low Frequency Effect channel (Subwoofer)
QAudioFormat::BackLeft 5
QAudioFormat::BackRight 6
QAudioFormat::FrontLeftOfCenter 7
QAudioFormat::FrontRightOfCenter 8
QAudioFormat::BackCenter 9
QAudioFormat::LFE2 10
QAudioFormat::SideLeft 11
QAudioFormat::SideRight 12
QAudioFormat::TopFrontLeft 13
QAudioFormat::TopFrontRight 14
QAudioFormat::TopFrontCenter 15
QAudioFormat::TopCenter 16
QAudioFormat::TopBackLeft 17
QAudioFormat::TopBackRight 18
QAudioFormat::TopSideLeft 19
QAudioFormat::TopSideRight 20
QAudioFormat::TopBackCenter 21
QAudioFormat::BottomFrontCenter 22
QAudioFormat::BottomFrontLeft 23
QAudioFormat::BottomFrontRight 24

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.

Constant Value Description
QAudioFormat::ChannelConfigUnknown 0 The channel configuration is not known.
QAudioFormat::ChannelConfigMono QtPrivate::channelConfig(FrontCenter) The audio has one Center channel
QAudioFormat::ChannelConfigStereo QtPrivate::channelConfig(FrontLeft, FrontRight) The audio has two channels, Left and Right
QAudioFormat::ChannelConfig2Dot1 QtPrivate::channelConfig(FrontLeft, FrontRight, LFE) The audio has three channels, Left, Right and LFE (low frequency effect)
QAudioFormat::ChannelConfigSurround5Dot0 QtPrivate::channelConfig(FrontLeft, FrontRight, FrontCenter, BackLeft, BackRight) The audio has five channels, Left, Right, Center, BackLeft, BackRight
QAudioFormat::ChannelConfigSurround5Dot1 QtPrivate::channelConfig(FrontLeft, FrontRight, FrontCenter, LFE, BackLeft, BackRight) The audio has 6 channels, Left, Right, Center, LFE, BackLeft and BackRight
QAudioFormat::ChannelConfigSurround7Dot0 QtPrivate::channelConfig(FrontLeft, FrontRight, FrontCenter, BackLeft, BackRight, SideLeft, SideRight) The audio has 7 channels, Left, Right, Center, BackLeft, BackRight, SideLeft and SideRight
QAudioFormat::ChannelConfigSurround7Dot1 QtPrivate::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.

Constant Value Description
QAudioFormat::Unknown 0 Not Set
QAudioFormat::UInt8 1 Samples are unsigned 8 bit signed integers
QAudioFormat::Int16 2 Samples are 16 bit signed integers
QAudioFormat::Int32 3 Samples are 32 bit signed integers
QAudioFormat::Float 4 Samples are floats

Member Function Documentation

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

Construct a new audio format using other.

[default] QAudioFormat::QAudioFormat()

Constructs a new audio format.

Values are initialized as follows:

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

Assigns other to this QAudioFormat implementation.

[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().

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.

int QAudioFormat::bytesPerSample() const

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

Returns 0 if this format is invalid.

QAudioFormat::ChannelConfig QAudioFormat::channelConfig() const

Returns the current channel configuration.

int QAudioFormat::channelCount() const

Returns the current channel count value.

See also setChannelCount().

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.

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().

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.

QAudioFormat::SampleFormat QAudioFormat::sampleFormat() const

Returns the current sample format.

See also setSampleFormat().

int QAudioFormat::sampleRate() const

Returns the current sample rate in Hertz.

See also setSampleRate().

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().

void QAudioFormat::setChannelCount(int channels)

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

See also channelCount().

void QAudioFormat::setSampleFormat(QAudioFormat::SampleFormat format)

Sets the sample format to format.

See also sampleFormat() and QAudioFormat::SampleFormat.

void QAudioFormat::setSampleRate(int samplerate)

Sets the sample rate to samplerate in Hertz.

See also sampleRate().

© The Qt Company Ltd
Licensed under the GNU Free Documentation License, Version 1.3.
https://doc.qt.io/qt-6.2/qaudioformat.html