本页内容

QAudioFormat Class

QAudioFormat 类用于存储音频流的参数信息。更多内容...

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

公共类型

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

公共函数

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)

静态公共成员

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)

详细说明

音频格式规定了原始音频流中数据的排列方式,例如该流应如何被解析。

QAudioFormat 包含用于指定音频采样数据排列方式的参数。这些参数包括采样率、声道数和采样格式。下表对此进行了更详细的说明。

参数描述
采样率每秒音频数据中的采样数,单位为赫兹。
声道数音频通道数(通常单声道为 1 个,立体声为 2 个)。这些是连续的采样数,它们共同构成流中的一帧
采样格式流中音频采样的格式

该类需与QAudioSource 或QAudioSink 配合使用,以便您指定正在读取或写入的音频流的参数;在处理内存中的采样时,则需与QAudioBuffer 配合使用。

您可以通过QAudioDevice 中的函数获取与所用音频设备兼容的音频格式。本类还允许您查询设备的可用参数值,以便您自行设置参数。详情请参阅QAudioDevice 类的说明。您需要了解要播放或录制的音频流的格式。

所有通道的采样点将交错排列。在Qt Multimedia (及其他地方)中,同一时间点各通道的单个采样点被称为一个帧。

成员类型文档

enum QAudioFormat::AudioChannelPosition

描述了音频通道的可能位置。这些位置遵循22.2环绕声配置中采用的标准定义。

常量值描述
QAudioFormat::UnknownPosition0未知位置
QAudioFormat::FrontLeft1 
QAudioFormat::FrontRight2 
QAudioFormat::FrontCenter3 
QAudioFormat::LFE4低频效果声道(低音炮)
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

该枚举描述了一种标准化的音频通道布局。最常见的配置包括单声道、立体声、2.1声道(立体声加低频)、5.1环绕声和7.1环绕声配置。

常量值描述
QAudioFormat::ChannelConfigUnknown0未知声道配置。
QAudioFormat::ChannelConfigMonoQtPrivate::channelConfig(FrontCenter)音频有一个中置声道。
QAudioFormat::ChannelConfigStereoQtPrivate::channelConfig(FrontLeft, FrontRight)音频有两个声道:左声道和右声道。
QAudioFormat::ChannelConfig2Dot1QtPrivate::channelConfig(FrontLeft, FrontRight, LFE)音频包含三个声道:左、右和 LFE(低频效果)。
QAudioFormat::ChannelConfig3Dot0QtPrivate::channelConfig(FrontLeft, FrontRight, FrontCenter)音频有三个声道:左、右和中。
QAudioFormat::ChannelConfig3Dot1QtPrivate::channelConfig(FrontLeft, FrontRight, FrontCenter, LFE)音频有四个声道:左、右、中置和 LFE(低频效果)。
QAudioFormat::ChannelConfigSurround5Dot0QtPrivate::channelConfig(FrontLeft, FrontRight, FrontCenter, BackLeft, BackRight)该音频有五个声道:左、右、中、BackLeft 和BackRight 。
QAudioFormat::ChannelConfigSurround5Dot1QtPrivate::channelConfig(FrontLeft, FrontRight, FrontCenter, LFE, BackLeft, BackRight)音频有 6 个声道:左、右、中、LFE、BackLeft 和BackRight 。
QAudioFormat::ChannelConfigSurround7Dot0QtPrivate::channelConfig(FrontLeft, FrontRight, FrontCenter, BackLeft, BackRight, SideLeft, SideRight)音频有 7 个声道:左、右、中、BackLeft 、BackRight 、SideLeft 和SideRight 。
QAudioFormat::ChannelConfigSurround7Dot1QtPrivate::channelConfig(FrontLeft, FrontRight, FrontCenter, LFE, BackLeft, BackRight, SideLeft, SideRight)音频包含8个声道:左、右、中、BackLeft 、BackRight 、SideLeft 以及SideRight 。

enum QAudioFormat::SampleFormat

Qt 始终会根据主机平台的字节序来处理和使用采样数据。当您自行处理来自外部源的音频数据时,请确保在将数据写入 `QAudioSink ` 或 `QAudioBuffer` 之前,将其转换为正确的字节序。

常量值描述
QAudioFormat::Unknown0未设置
QAudioFormat::UInt81采样值为 8 位无符号整数
QAudioFormat::Int162采样值为16位有符号整数
QAudioFormat::Int323采样值为 32 位有符号整数
QAudioFormat::Float4采样值为浮点数

成员函数文档

[implicit] QAudioFormat::QAudioFormat()

创建一种新的音频格式。

值的初始化如下:

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

使用other 构建一种新的音频格式。

[noexcept implicit] QAudioFormat::~QAudioFormat()

删除此音频格式。

qint32 QAudioFormat::bytesForDuration(qint64 microseconds) const

返回microseconds 中该音频格式所需的字节数。

如果该格式无效,则返回 0。

请注意,如果 `microseconds ` 不是 `sampleRate()` 的精确分数,可能会发生舍入误差。

另请参阅 durationForBytes()。

qint32 QAudioFormat::bytesForFrames(qint32 frameCount) const

返回此格式下frameCount 帧所需的字节数。

如果该格式无效,则返回 0。

另请参阅 bytesForDuration()。

[constexpr] int QAudioFormat::bytesPerFrame() const

返回以该格式表示一个帧(每个通道中的一个采样)所需的字节数。

如果该格式无效,则返回 0。

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

返回以该格式表示一个采样所需的字节数。

如果此格式无效,则返回 0。

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

返回当前通道配置。

另请参阅 setChannelConfig()。

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

返回给定channels 的当前通道配置。

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

返回当前的通道数量值。

另请参阅 setChannelCount()。

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

返回给定格式下音频帧中某个音频channel 的位置。如果该格式不存在该通道,或者通道配置未知,则返回-1。

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

返回channelCount 的默认声道配置。

默认配置最多支持 8 个声道,分别对应标准的单声道、立体声和环绕声配置。对于更多声道的配置,该方法将直接使用在 `QAudioFormat::AudioChannelPosition` 中定义的首批 `channelCount ` 音频声道。

qint64 QAudioFormat::durationForBytes(qint32 bytes) const

返回bytes 在此格式下所表示的微秒数。

如果此格式无效,则返回 0。

请注意,如果 `bytes ` 不是每帧字节数的精确倍数,可能会发生舍入误差。

另请参阅 bytesForDuration()。

qint64 QAudioFormat::durationForFrames(qint32 frameCount) const

返回以该格式表示的frameCount 帧所对应的微秒数。

qint32 QAudioFormat::framesForBytes(qint32 byteCount) const

返回以该格式表示的、由 `byteCount ` 所代表的帧数。

请注意,如果 `byteCount ` 不是每帧字节数的精确倍数,可能会发生舍入误差。

每个帧中每个通道有一个采样点。

另请参阅 framesForDuration()。

qint32 QAudioFormat::framesForDuration(qint64 microseconds) const

返回以该格式表示microseconds 所需的帧数。

请注意,如果microseconds 不是sampleRate()的精确分数,可能会发生舍入误差。

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

如果所有参数均有效,则返回true 。

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

将sample 的值归一化为-1到1之间的数值。该方法取决于QaudioFormat。

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

返回当前的采样格式。

另请参阅 ` setSampleFormat()`。

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

返回当前采样率(单位:赫兹)。

另请参阅 setSampleRate()。

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

将声道配置设置为config 。

将音频格式的声道配置设置为标准音频声道配置之一。

注意: 此操作还会修改声道数。

另请参阅 channelConfig()。

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

将通道数量设置为channels 。设置此选项也会将通道配置设置为ChannelConfigUnknown 。

另请参阅 channelCount()。

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

将采样格式设置为format 。

另请参阅 sampleFormat() 和QAudioFormat::SampleFormat 。

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

将采样率设置为samplerate 赫兹。

另请参阅 sampleRate()。

相关非成员

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

如果音频格式 `a ` 不等于 `b`,则返回 `true `;否则返回 `false`。

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

如果音频格式 `a ` 等于 `b`,则返回 `true `;否则返回 `false`。

© 2026 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.