有人可以解释 .wav(WAVE) 文件 headers 吗?
Can someone explain .wav(WAVE) file headers?
好的,所以我正在尝试编写一个程序来处理 .wav 文件,我已经看到 this question/answers,但我不完全确定文件中的每条数据是什么header指的是。例如,"chunk" 指的是什么?那是bits/bytes的具体数量吗?
如果有人能告诉我,至少在这个问题中使用的格式中,除了常量字符串文字和 'data' 数组之外,写入 .wav 的每个数据指的是什么?特别是我特别想知道 "chunk" 是什么,以及所有通道的采样率、字节率、每个样本的字节数和每个样本的字节数如何相关?(我怀疑字节率是采样率 * 每个样本的字节数示例,但是 'for all channels' 那个呢?)
感谢任何帮助。
只 post 一个 link 是违反董事会规则的,所以这是我从 http://www.topherlee.com/software/pcm-tut-wavformat.html
那里得到的 table
Positions Sample Value Description
1 - 4 "RIFF" Marks the file as a riff file. Characters are each 1. byte long.
5 - 8 File size (integer) Size of the overall file - 8 bytes, in bytes (32-bit integer). Typically, you'd fill this in after creation.
9 -12 "WAVE" File Type Header. For our purposes, it always equals "WAVE".
13-16 "fmt " Format chunk marker. Includes trailing null
17-20 16 Length of format data as listed above
21-22 1 Type of format (1 is PCM) - 2 byte integer
23-24 2 Number of Channels - 2 byte integer
25-28 44100 Sample Rate - 32 bit integer. Common values are 44100 (CD), 48000 (DAT). Sample Rate = Number of Samples per second, or Hertz.
29-32 176400 (Sample Rate * BitsPerSample * Channels) / 8.
33-34 4 (BitsPerSample * Channels) / 8.1 - 8 bit mono2 - 8 bit stereo/16 bit mono4 - 16 bit stereo
35-36 16 Bits per sample
37-40 "data" "data" chunk header. Marks the beginning of the data section.
41-44 File size (data) Size of the data section, i.e. file size - 44 bytes header.
上面给出了 16 位立体声源的示例值。
Update/Reminder
header 个整数都是最低有效字节顺序,
所以两个字节的通道信息 0x01 0x00 实际上是 0x00001 例如单声道。
我知道 OP 将问题标记为 Java,但这里有完整的 Kotlin 代码,用于读取 header 可以通过 Java。阅读 Little Endian 可能很棘手,但幸运的是我们不必那样做。
class WaveHeader(bytes: ByteArray) {
init {
require(bytes.size >= SIZE) { "Input size is must be at least $SIZE bytes" }
}
private var start = 0
private val riff = RiffChunk(
String(bytes.copyOfRange(start, start + 4))
.also {
require(it == "RIFF") { "$it must be 'RIFF'" }
start += it.length
},
ByteBuffer.wrap(bytes.copyOfRange(start, start + 4)).order(ByteOrder.LITTLE_ENDIAN)
.also { start += it.capacity() }.int,
String(bytes.copyOfRange(start, start + 4))
.also {
require(it == "WAVE") { "$it must be 'WAVE'" }
start += it.length
}
)
private val format = FormatChunk(
// null terminated
String(bytes.copyOfRange(start, start + 3))
.also {
require(it == "fmt") { "$it must be 'fmt'" }
start += 4
},
ByteBuffer.wrap(bytes.copyOfRange(start, start + 4)).order(ByteOrder.LITTLE_ENDIAN)
.also { start += it.capacity() }.int,
ByteBuffer.wrap(bytes.copyOfRange(start, start + 2)).order(ByteOrder.LITTLE_ENDIAN)
.also { start += it.capacity() }
.let { if (it.short == 1.toShort()) "PCM" else "OTHER (${it.short})" },
ByteBuffer.wrap(bytes.copyOfRange(start, start + 2)).order(ByteOrder.LITTLE_ENDIAN)
.also { start += it.capacity() }.short,
ByteBuffer.wrap(bytes.copyOfRange(start, start + 4)).order(ByteOrder.LITTLE_ENDIAN)
.also { start += it.capacity() }.int,
ByteBuffer.wrap(bytes.copyOfRange(start, start + 4)).order(ByteOrder.LITTLE_ENDIAN)
.also { start += it.capacity() }.int,
ByteBuffer.wrap(bytes.copyOfRange(start, start + 2)).order(ByteOrder.LITTLE_ENDIAN)
.also { start += it.capacity() }.short,
ByteBuffer.wrap(bytes.copyOfRange(start, start + 2)).order(ByteOrder.LITTLE_ENDIAN)
.also { start += it.capacity() }.short
)
private val `data` = DataChunk(
String(bytes.copyOfRange(start, start + 4))
// remove all null chars
.replace("\u0000", "")
.also { start += it.length },
ByteBuffer.wrap(bytes.copyOfRange(start, start + 4)).order(ByteOrder.LITTLE_ENDIAN)
.also { start += it.capacity() }.int
)
init {
assert(start == 44) { "Illegal state" }
}
data class RiffChunk(val id: String, val size: Int, val format: String)
data class FormatChunk(
val id: String, val size: Int, val format: String, val numChannels: Short,
val sampleRate: Int, val byteRate: Int, val blockAlign: Short, val bitsPerSample: Short
)
data class DataChunk(val id: String, val size: Int)
override fun toString(): String {
val ls = System.lineSeparator()
return "WaveHeader($ls\t$riff}$ls\t$format$ls\t$`data`$ls)"
}
companion object {
const val SIZE = 44
fun fromPath(path: String): WaveHeader = fromInputStream(WaveHeader::class.java.getResourceAsStream(path))
fun fromUrl(url: String): WaveHeader = fromInputStream(URL(url).openStream())
private fun fromInputStream(input: InputStream): WaveHeader {
val bytes = input.use {
it.readNBytes(SIZE)
}
return WaveHeader(bytes)
}
}
}
fun main(args: Array<String>) {
if (args.isEmpty()) {
System.err.println("Argument is missing")
}
println(WaveHeader.fromUrl(args[0]))
}
运行 this URL 产生输出:
WaveHeader(
RiffChunk(id=RIFF, size=168050, format=WAVE)}
FormatChunk(id=fmt, size=18, format=PCM, numChannels=1, sampleRate=16000, byteRate=32000, blockAlign=2, bitsPerSample=16)
DataChunk(id=fa, size=1952670054)
)
好的,所以我正在尝试编写一个程序来处理 .wav 文件,我已经看到 this question/answers,但我不完全确定文件中的每条数据是什么header指的是。例如,"chunk" 指的是什么?那是bits/bytes的具体数量吗?
如果有人能告诉我,至少在这个问题中使用的格式中,除了常量字符串文字和 'data' 数组之外,写入 .wav 的每个数据指的是什么?特别是我特别想知道 "chunk" 是什么,以及所有通道的采样率、字节率、每个样本的字节数和每个样本的字节数如何相关?(我怀疑字节率是采样率 * 每个样本的字节数示例,但是 'for all channels' 那个呢?)
感谢任何帮助。
只 post 一个 link 是违反董事会规则的,所以这是我从 http://www.topherlee.com/software/pcm-tut-wavformat.html
那里得到的 tablePositions Sample Value Description
1 - 4 "RIFF" Marks the file as a riff file. Characters are each 1. byte long.
5 - 8 File size (integer) Size of the overall file - 8 bytes, in bytes (32-bit integer). Typically, you'd fill this in after creation.
9 -12 "WAVE" File Type Header. For our purposes, it always equals "WAVE".
13-16 "fmt " Format chunk marker. Includes trailing null
17-20 16 Length of format data as listed above
21-22 1 Type of format (1 is PCM) - 2 byte integer
23-24 2 Number of Channels - 2 byte integer
25-28 44100 Sample Rate - 32 bit integer. Common values are 44100 (CD), 48000 (DAT). Sample Rate = Number of Samples per second, or Hertz.
29-32 176400 (Sample Rate * BitsPerSample * Channels) / 8.
33-34 4 (BitsPerSample * Channels) / 8.1 - 8 bit mono2 - 8 bit stereo/16 bit mono4 - 16 bit stereo
35-36 16 Bits per sample
37-40 "data" "data" chunk header. Marks the beginning of the data section.
41-44 File size (data) Size of the data section, i.e. file size - 44 bytes header.
上面给出了 16 位立体声源的示例值。
Update/Reminder
header 个整数都是最低有效字节顺序, 所以两个字节的通道信息 0x01 0x00 实际上是 0x00001 例如单声道。
我知道 OP 将问题标记为 Java,但这里有完整的 Kotlin 代码,用于读取 header 可以通过 Java。阅读 Little Endian 可能很棘手,但幸运的是我们不必那样做。
class WaveHeader(bytes: ByteArray) {
init {
require(bytes.size >= SIZE) { "Input size is must be at least $SIZE bytes" }
}
private var start = 0
private val riff = RiffChunk(
String(bytes.copyOfRange(start, start + 4))
.also {
require(it == "RIFF") { "$it must be 'RIFF'" }
start += it.length
},
ByteBuffer.wrap(bytes.copyOfRange(start, start + 4)).order(ByteOrder.LITTLE_ENDIAN)
.also { start += it.capacity() }.int,
String(bytes.copyOfRange(start, start + 4))
.also {
require(it == "WAVE") { "$it must be 'WAVE'" }
start += it.length
}
)
private val format = FormatChunk(
// null terminated
String(bytes.copyOfRange(start, start + 3))
.also {
require(it == "fmt") { "$it must be 'fmt'" }
start += 4
},
ByteBuffer.wrap(bytes.copyOfRange(start, start + 4)).order(ByteOrder.LITTLE_ENDIAN)
.also { start += it.capacity() }.int,
ByteBuffer.wrap(bytes.copyOfRange(start, start + 2)).order(ByteOrder.LITTLE_ENDIAN)
.also { start += it.capacity() }
.let { if (it.short == 1.toShort()) "PCM" else "OTHER (${it.short})" },
ByteBuffer.wrap(bytes.copyOfRange(start, start + 2)).order(ByteOrder.LITTLE_ENDIAN)
.also { start += it.capacity() }.short,
ByteBuffer.wrap(bytes.copyOfRange(start, start + 4)).order(ByteOrder.LITTLE_ENDIAN)
.also { start += it.capacity() }.int,
ByteBuffer.wrap(bytes.copyOfRange(start, start + 4)).order(ByteOrder.LITTLE_ENDIAN)
.also { start += it.capacity() }.int,
ByteBuffer.wrap(bytes.copyOfRange(start, start + 2)).order(ByteOrder.LITTLE_ENDIAN)
.also { start += it.capacity() }.short,
ByteBuffer.wrap(bytes.copyOfRange(start, start + 2)).order(ByteOrder.LITTLE_ENDIAN)
.also { start += it.capacity() }.short
)
private val `data` = DataChunk(
String(bytes.copyOfRange(start, start + 4))
// remove all null chars
.replace("\u0000", "")
.also { start += it.length },
ByteBuffer.wrap(bytes.copyOfRange(start, start + 4)).order(ByteOrder.LITTLE_ENDIAN)
.also { start += it.capacity() }.int
)
init {
assert(start == 44) { "Illegal state" }
}
data class RiffChunk(val id: String, val size: Int, val format: String)
data class FormatChunk(
val id: String, val size: Int, val format: String, val numChannels: Short,
val sampleRate: Int, val byteRate: Int, val blockAlign: Short, val bitsPerSample: Short
)
data class DataChunk(val id: String, val size: Int)
override fun toString(): String {
val ls = System.lineSeparator()
return "WaveHeader($ls\t$riff}$ls\t$format$ls\t$`data`$ls)"
}
companion object {
const val SIZE = 44
fun fromPath(path: String): WaveHeader = fromInputStream(WaveHeader::class.java.getResourceAsStream(path))
fun fromUrl(url: String): WaveHeader = fromInputStream(URL(url).openStream())
private fun fromInputStream(input: InputStream): WaveHeader {
val bytes = input.use {
it.readNBytes(SIZE)
}
return WaveHeader(bytes)
}
}
}
fun main(args: Array<String>) {
if (args.isEmpty()) {
System.err.println("Argument is missing")
}
println(WaveHeader.fromUrl(args[0]))
}
运行 this URL 产生输出:
WaveHeader(
RiffChunk(id=RIFF, size=168050, format=WAVE)}
FormatChunk(id=fmt, size=18, format=PCM, numChannels=1, sampleRate=16000, byteRate=32000, blockAlign=2, bitsPerSample=16)
DataChunk(id=fa, size=1952670054)
)