Java中解析wav音频文件信息:音频声道数,采样频率,采样位数、声音尺寸
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Java中解析wav⾳频⽂件信息:⾳频声道数,采样频率,采样位数、声⾳尺⼨前⾔:请各⼤⽹友尊重本⼈原创知识分享,谨记本⼈博客:
⾳频解析⽅法:
1public static int toInt(byte[] b) {
2return ((b[3] << 24) + (b[2] << 16) + (b[1] << 8) + (b[0] << 0));
3 }
4
5public static short toShort(byte[] b) {
6return (short)((b[1] << 8) + (b[0] << 0));
7 }
8
9
10public static byte[] read(RandomAccessFile rdf, int pos, int length) throws IOException {
11 rdf.seek(pos);
12byte result[] = new byte[length];
13for (int i = 0; i < length; i++) {
14 result[i] = rdf.readByte();
15 }
16return result;
17 }
⾳频解析⽅法调⽤:
1public static void main(String[] args) throws IOException {
2 File f = new File("E:/zmj-3011-32779/audio.wav");
3 RandomAccessFile rdf = null;
4 rdf = new RandomAccessFile(f,"r");
5
6 System.out.println("声⾳尺⼨: " + toInt(read(rdf, 4, 4))); // 声⾳尺⼨
7
8 System.out.println("⾳频格式: " + toShort(read(rdf, 20, 2))); // ⾳频格式 1 = PCM
9
10 System.out.println("声道数: " + toShort(read(rdf, 22, 2))); // 1 单声道 2 双声道
11
12 System.out.println("采样率: " + toInt(read(rdf, 24, 4))); // 采样率、⾳频采样级别 8000 = 8KHz
13
14 System.out.println("波形的数据量: " + toInt(read(rdf, 28, 4))); // 每秒波形的数据量
15
16 System.out.println("采样帧: " + toShort(read(rdf, 32, 2))); // 采样帧的⼤⼩
17
18 System.out.println("采样位数: " + toShort(read(rdf, 34, 2))); // 采样位数
19
20 rdf.close();
21
22
23 }
控制台打印结果:
总语
我是记录点滴每天成长⼀点点,学习是永⽆⽌境的!转载请附原⽂链接。