音频处理

AudioFormat音频格式参数

以Java中读取音频文件格式AudioFormat举例学习

public class AudioFormat {
    protected Encoding encoding;
    protected float sampleRate;
    protected int sampleSizeInBits;
    protected int channels;
    protected int frameSize;
    protected float frameRate;
    protected boolean bigEndian;
    private HashMap<String, Object> properties;

    /**
     * Constructs an {@code AudioFormat} with the given parameters. The encoding
     * specifies the convention used to represent the data. The other parameters
     * are further explained in the {@link AudioFormat class description}.
     *
     * @param  encoding the audio encoding technique
     * @param  sampleRate the number of samples per second
     * @param  sampleSizeInBits the number of bits in each sample
     * @param  channels the number of channels (1 for mono, 2 for stereo, and so
     *         on)
     * @param  frameSize the number of bytes in each frame
     * @param  frameRate the number of frames per second
     * @param  bigEndian indicates whether the data for a single sample is
     *         stored in big-endian byte order ({@code false} means
     *         little-endian)
     */
    public AudioFormat(Encoding encoding, float sampleRate, int sampleSizeInBits,
                       int channels, int frameSize, float frameRate, boolean bigEndian) {

        this.encoding = encoding;
        this.sampleRate = sampleRate;
        this.sampleSizeInBits = sampleSizeInBits;
        this.channels = channels;
        this.frameSize = frameSize;
        this.frameRate = frameRate;
        this.bigEndian = bigEndian;
        this.properties = null;
    }
}

encoding(编码方式)

  • 音频数据的编码格式

  • 常见类型:

    • Encoding.PCM_SIGNED:有符号的脉冲编码调制,常用于无损音频。
    • Encoding.PCM_UNSIGNED:无符号PCM,较少使用。
    • Encoding.ULAW、Encoding.ALAW:压缩编码,用于电话语音等低带宽场景。
    • Encoding.PCM_FLOAT:浮点型PCM,提供更高动态范围。
  • 作用:决定音频数据的存储方式和压缩算法,影响音质和文件大小

sampleRate(采样率)

  • 每秒采集的音频样本数,即采集的总帧数,单位赫兹(HZ)
  • 每个声道独立采样
  • 常见值
    • 8000 Hz(电话质量)
    • 44100 Hz(CD质量)
    • 48000 Hz(专业音频)
    • 96000 Hz(高保真音频)
  • 作用:采样率越高,能还原的音频频率上限越高(根据奈奎斯特定理,最高频率为采样率的一半)。

sampleSizeInBits位深|样本大小

  • 每个样本的位数(位深),决定动态范围和精度
  • 常见值:8位、16位、24位、32位
  • 常见值:
    • 8位:动态范围约48 dB,适用于低质量音频。
    • 16位:动态范围约96 dB,CD标准。
    • 24位:专业录音,减少量化噪声
    • 32位:专业录音,减少量化噪声,浮点音频处理
  • 作用:
    • 决定音频的动态范围和信噪比。
    • 位深越高,声音细节越丰富,噪声越低。

channels(声道数)

  • 音频的独立声道数量。
  • 常见值:
    • 1(单声道,Mono)
    • 2(立体声,Stereo)
    • 6(5.1环绕声)
  • 作用:声道数越多,空间感越强,但数据量成倍增加。

frameSize(帧大小)

  • 每帧字节数
  • 计算公式
    $$frameSize = \frac{sampleSizeInBits \times channels }{8}$$
  • 示例:16位立体声表示为 $\frac{16 \times 2}{8}$=4字节/帧

frameRate(帧率)

  • 每秒传输的帧数
  • 与采样率关系
    • 在PCM编码中,通常等于采样率(每帧包含每个通道的一个样本)
    • 在压缩编码中,可能不同(如MP3的一帧包含多个样本)。
  • 作用:确保音频流的时间同步,影响实时处理时的延迟。

bigEndian(字节顺序)

  • 含义:多字节样本的存储顺序。
  • true:大端序(高位字节在前)。
  • false:小端序(低位字节在前)。
  • 影响范围:主要适用于PCM编码的音频数据。
  • 示例:16位样本 0x1234:
    • 大端序存储为 0x12 0x34。
    • 小端序存储为 0x34 0x12。

Frame(帧)

  • 一个帧对应 同一时间点 内所有声道的样本集合。
    • 单声道:每帧包含 1 个样本(如 [左声道])。
    • 立体声:每帧包含 2 个样本(如 [左声道, 右声道])。
    • 5.1 环绕声:每帧包含 6 个样本(如 [左前, 右前, 中置, 低音, 左后, 右后])。
  • 每帧的样本数=声道数(Channels)

配置示例

AudioFormat format = new AudioFormat(
    Encoding.PCM_SIGNED,  // 编码方式
    44100.0F,             // 采样率 44.1 kHz
    16,                   // 样本大小 16位
    2,                    // 立体声(2声道)
    4,                    // 帧大小 4字节(16位×2声道÷8)
    44100.0F,             // 帧率 44.1 kHz(与采样率相同)
    false                 // 小端序
);

注意事项

  • ​编码与参数兼容性:
    • PCM编码需要明确指定sampleSizeInBits、channels和bigEndian。
    • 压缩编码(如ULAW)可能忽略某些参数(如字节顺序)。
  • 平台字节顺序:
    • x86/x64架构通常为小端序,需确保与硬件兼容。
  • 帧大小与缓冲区:
    • 读取音频数据时,缓冲区大小应为帧大小的整数倍,避免数据错位。
  • 实时流处理:
    • 采样率和多声道会增加CPU和内存负担,需优化处理逻辑。

参数间关系

每帧数据量
  • 单帧的数据量大小(字节) =
    $$ \frac{sampleSizeInBits \times channels }{8}$$
音频时长与帧数
  • 总帧数 = 采样率 × 音频时长
  • 示例:44.1 kHz、3 分钟的音频总帧数:
    • 44100×180=7,938,000 帧
音频总数据量
  • 基于采样率
    • 总数据量(字节)= 采样率× 时长(秒)× 单帧大小(字节)
      基于总帧数
    • 总数据量(字节) = 总帧数×单帧大小(字节)
  • 示例:
    • 采样率44.1kHZ
    • 时长3分钟
    • 16位立体声
    • 44100×180×4=31,752,000 字节≈30.3 MB
常见误区
  • 误区 1:混淆“采样”与“帧”

    • 采样(Sample):单个声道在某一时间点的数据。

    • 帧(Frame):所有声道在同一时间点的数据集合。

    • 例如:

      • 立体声(2 声道)的 1 帧包含 2 个采样。
      • 总采样数 = 总帧数 × 声道数。
  • 误区 2:压缩编码中的帧

    • 在压缩音频格式(如 MP3、AAC)中,一帧可能包含多个时间点的样本。
      • 此时,“帧”的定义与 PCM 不同,需要参考具体编码规范。
      • 例如,MP3 一帧通常包含 1152 个样本(对应约 26 ms 的音频)。
posted @ 2025-05-19 14:58  dev-yze  阅读(83)  评论(0)    收藏  举报