"Sample Frame" 在 AudioInputStream 的上下文中

"Sample Frame" in Context of AudioInputStream

我目前正在为 Java 声音 SPI 实施编解码器。我已经到了必须更改 AudioInputStream 的某些行为(因此必须覆盖它)的阶段。但是, "sample frames" 的措辞对我来说没有任何意义。我的编解码器中的样本数量和帧数量是非常不同的数字和含义。那么在这个class的上下文中,它是什么意思?

例如,frameLength 记录为 "This stream's length, in sample frames."流的长度不等于流中的帧数。

framePos 记录为 "The current position in this stream, in sample frames (zero-based)." 这是否意味着它包含流当前所在的帧或流所在的样​​本?

等等

'sample frame' 表示在所有通道上包含单个样本的帧。因此,对于 8 位单声道,这是 1 个字节,对于 16 位立体声,它是 4 个字节(2 个通道,每个样本 2 个字节)。 AudioInputStream 从传递给构造函数的 AudioFormat 获取此大小。

frameLengthframePos 然后总是以这个大小为单位。

AudioInputStream 始终尝试读取完整数量的样本帧,以便应用程序代码不必处理获取帧的一部分。

来自AudioFormat docs

For encodings like PCM, a frame consists of the set of samples for all channels at a given point in time, and so the size of a frame (in bytes) is always equal to the size of a sample (in bytes) times the number of channels. However, with some other sorts of encodings a frame can contain a bundle of compressed data for a whole series of samples, as well as additional, non-sample data. For such encodings, the sample rate and sample size refer to the data after it is decoded into PCM, and so they are completely different from the frame rate and frame size.

因此,如果您的编解码器压缩音频,例如mp3,一个 frame 更像是一个压缩的数据块,它的大小与你在未压缩的 PCM 编码中遇到的样本大小关系不大(更多关于 mp3 帧的信息可以找到 here).

所以 AudioInputStream 文档中的 frameLength 实际上只是表示 此流中的总帧数 .

或者在压缩音频的上下文中更清楚:压缩块的数量

读取压缩音频时,使用 stream = AudioSystem.getAudioInputStream(file)压缩 流连同适当的 AudioFormat 实例一起返回。然后,用户需要使用 decompressedStream = AudioSystem.getAudioInputStream(PCM_SIGNED, stream).

之类的东西 解压缩

不幸的是,这经常被误解。