QAudioFormat Class

QAudioFormat 类存储音频流参数信息。更多

头文件: #include <QAudioFormat>
CMake.QAudioFormat 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 包含指定音频采样数据排列方式的参数。这些参数包括频率、通道数和采样格式。下表详细介绍了这些参数。

参数说明
采样率每秒音频数据的采样率(赫兹)。
通道数音频通道的数量(单声道通常为一个,立体声通常为两个)。这些连续采样的数量共同构成了音频流中的一帧。
采样格式音频流中音频样本的格式

该类可与QAudioSourceQAudioSink 结合使用,以便指定正在读取或写入的音频流的参数,或在处理内存中的样本时与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)音频有五个通道:左、右、中、BackLeftBackRight
QAudioFormat::ChannelConfigSurround5Dot1QtPrivate::channelConfig(FrontLeft, FrontRight, FrontCenter, LFE, BackLeft, BackRight)音频有 6 个通道:左、右、中、LFE、BackLeftBackRight
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 将始终期望并使用主机平台的 endianness 采样。在自行处理来自外部的音频数据时,请确保在将其写入QAudioSinkQAudioBuffer 之前将其转换为正确的字节序。

常数说明
QAudioFormat::Unknown0不设置
QAudioFormat::UInt81采样为 8 位无符号整数
QAudioFormat::Int162采样为 16 位有符号整数
QAudioFormat::Int323样本为 32 位有符号整数
QAudioFormat::Float4采样为浮点数

成员函数文档

[noexcept default] QAudioFormat::QAudioFormat()

构建新的音频格式。

值的初始化如下:

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

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

[noexcept default] 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

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