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-1068237dom

我的總結:

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

相關文章
相關標籤/搜索