Java中解析wav音頻文件信息:音頻聲道數,采樣頻率,采樣位數、聲音尺寸


前言:請各大網友尊重本人原創知識分享,謹記本人博客:南國以南i

音頻解析方法:

 1 public static int toInt(byte[] b) {
 2         return ((b[3] << 24) + (b[2] << 16) + (b[1] << 8) + (b[0] << 0));
 3     }
 4    
 5     public static short toShort(byte[] b) {
 6         return (short)((b[1] << 8) + (b[0] << 0));
 7     }
 8    
 9    
10     public static byte[] read(RandomAccessFile rdf, int pos, int length) throws IOException {
11         rdf.seek(pos);
12         byte result[] = new byte[length];
13         for (int i = 0; i < length; i++) {
14             result[i] = rdf.readByte();
15         }
16         return result;
17     }

音頻解析方法調用:

 1  public 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     }

控制台打印結果:

 

 參考文章:https://www.iteye.com/blog/mzhj-1068237

總語

我是南國以南i記錄點滴每天成長一點點,學習是永無止境的!轉載請附原文鏈接!!!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM