java讀取wav檔案(波形檔案)並繪製波形圖的方法_java

來源:互聯網
上載者:User

本文執行個體講述了java讀取wav檔案(波形檔案)並繪製波形圖的方法。分享給大家供大家參考。具體如下:

因為最近有不少網友詢問我波形檔案讀寫方面的問題,出於讓大家更方便以及讓代碼能夠得到更好的改進,我將這部分(波形檔案的讀寫)代碼開源在GitHub上面。

地址為https://github.com/sintrb/WaveAccess/,最新的代碼、例子、文檔都在那上面,我會在我時間精力允許的前提下對該項目進行維護,同時也希望對這方面有興趣的網友能夠加入到該開源項目上。

以下內容基本都到期了,你可以直接去GitHub上面閱讀、下載該項目。

因項目需要讀取.wav檔案(波形檔案)並繪製波形圖,因此簡單的做了這方面的封裝。

其實主要是對wav檔案讀取的封裝,下面是一個wav檔案讀取器的封裝:

// filename: WaveFileReader.java // RobinTang // 2012-08-23 import java.io.*; public class WaveFileReader {   private String filename = null;   private int[][] data = null;   private int len = 0;   private String chunkdescriptor = null;   static private int lenchunkdescriptor = 4;   private long chunksize = 0;   static private int lenchunksize = 4;   private String waveflag = null;   static private int lenwaveflag = 4;   private String fmtubchunk = null;   static private int lenfmtubchunk = 4;   private long subchunk1size = 0;   static private int lensubchunk1size = 4;   private int audioformat = 0;   static private int lenaudioformat = 2;   private int numchannels = 0;   static private int lennumchannels = 2;   private long samplerate = 0;   static private int lensamplerate = 2;   private long byterate = 0;   static private int lenbyterate = 4;   private int blockalign = 0;   static private int lenblockling = 2;   private int bitspersample = 0;   static private int lenbitspersample = 2;   private String datasubchunk = null;   static private int lendatasubchunk = 4;   private long subchunk2size = 0;   static private int lensubchunk2size = 4;   private FileInputStream fis = null;   private BufferedInputStream bis = null;   private boolean issuccess = false;   public WaveFileReader(String filename) {     this.initReader(filename);   }   // 判斷是否建立wav讀取器成功   public boolean isSuccess() {     return issuccess;   }   // 擷取每個採樣的編碼長度,8bit或者16bit   public int getBitPerSample(){     return this.bitspersample;   }   // 擷取採樣率   public long getSampleRate(){     return this.samplerate;   }   // 擷取聲道個數,1代表單聲道 2代表立體聲   public int getNumChannels(){     return this.numchannels;   }   // 擷取資料長度,也就是一共採樣多少個   public int getDataLen(){     return this.len;   }   // 擷取資料   // 資料是一個二維數組,[n][m]代表第n個聲道的第m個採樣值   public int[][] getData(){     return this.data;   }   private void initReader(String filename){     this.filename = filename;     try {       fis = new FileInputStream(this.filename);       bis = new BufferedInputStream(fis);       this.chunkdescriptor = readString(lenchunkdescriptor);       if(!chunkdescriptor.endsWith("RIFF"))         throw new IllegalArgumentException("RIFF miss, " + filename + " is not a wave file.");       this.chunksize = readLong();       this.waveflag = readString(lenwaveflag);       if(!waveflag.endsWith("WAVE"))         throw new IllegalArgumentException("WAVE miss, " + filename + " is not a wave file.");       this.fmtubchunk = readString(lenfmtubchunk);       if(!fmtubchunk.endsWith("fmt "))         throw new IllegalArgumentException("fmt miss, " + filename + " is not a wave file.");       this.subchunk1size = readLong();       this.audioformat = readInt();       this.numchannels = readInt();       this.samplerate = readLong();       this.byterate = readLong();       this.blockalign = readInt();       this.bitspersample = readInt();       this.datasubchunk = readString(lendatasubchunk);       if(!datasubchunk.endsWith("data"))         throw new IllegalArgumentException("data miss, " + filename + " is not a wave file.");       this.subchunk2size = readLong();       this.len = (int)(this.subchunk2size/(this.bitspersample/8)/this.numchannels);       this.data = new int[this.numchannels][this.len];              for(int i=0; i<this.len; ++i){         for(int n=0; n<this.numchannels; ++n){           if(this.bitspersample == 8){             this.data[n][i] = bis.read();           }           else if(this.bitspersample == 16){             this.data[n][i] = this.readInt();           }         }       }       issuccess = true;     } catch (Exception e) {       e.printStackTrace();     }     finally{       try{       if(bis != null)         bis.close();       if(fis != null)         fis.close();       }       catch(Exception e1){         e1.printStackTrace();       }     }   }   private String readString(int len){     byte[] buf = new byte[len];     try {       if(bis.read(buf)!=len)         throw new IOException("no more data!!!");     } catch (IOException e) {       e.printStackTrace();     }     return new String(buf);   }   private int readInt(){     byte[] buf = new byte[2];     int res = 0;     try {       if(bis.read(buf)!=2)         throw new IOException("no more data!!!");       res = (buf[0]&0x000000FF) | (((int)buf[1])<<8);     } catch (IOException e) {       e.printStackTrace();     }     return res;   }   private long readLong(){     long res = 0;     try {       long[] l = new long[4];       for(int i=0; i<4; ++i){         l[i] = bis.read();         if(l[i]==-1){           throw new IOException("no more data!!!");         }       }       res = l[0] | (l[1]<<8) | (l[2]<<16) | (l[3]<<24);     } catch (IOException e) {       e.printStackTrace();     }     return res;   }   private byte[] readBytes(int len){     byte[] buf = new byte[len];     try {       if(bis.read(buf)!=len)         throw new IOException("no more data!!!");     } catch (IOException e) {       e.printStackTrace();     }     return buf;   } } 

為了繪製波形,因此做了一個從JPanel教程而來的波形繪製面板:

// filename: DrawPanel.java // RobinTang // 2012-08-23 import java.awt.Color; import java.awt.Graphics; import javax.swing.JPanel; @SuppressWarnings("serial") public class DrawPanel extends JPanel {   private int[] data = null;   public DrawPanel(int[] data) {     this.data = data;   }   @Override   protected void paintComponent(Graphics g) {     int ww = getWidth();     int hh = getHeight();     g.setColor(Color.WHITE);     g.fillRect(0, 0, ww, hh);     int len = data.length;     int step = len/ww;     if(step==0)       step = 1;     int prex = 0, prey = 0; //上一個座標     int x = 0, y = 0;     g.setColor(Color.RED);     double k = hh/2.0/32768.0;     for(int i=0; i<ww; ++i){       x = i;       // 下面是個三點取出並繪製       // 實際中應該按照採樣率來設定間隔       y = hh-(int)(data[i*3]*k+hh/2);       System.out.print(y);       System.out.print(" ");       if(i!=0){         g.drawLine(x, y, prex, prey);       }       prex = x;       prey = y;     }   } } 

有了這些之後就可以調用繪製了,簡單的:

// WaveFileReadDemo.java // RobinTang // 2012-08-23 import javax.swing.JFrame; public class WaveFileReadDemo {   /**    * @param args    */   public static void main(String[] args) {     // TODO Auto-generated method stub     String filename = "file.wav";     JFrame frame = new JFrame();     WaveFileReader reader = new WaveFileReader(filename);     if(reader.isSuccess()){       int[] data = reader.getData()[0]; //擷取第一聲道       DrawPanel drawPanel = new DrawPanel(data); // 建立一個繪製波形的面板       frame.add(drawPanel);       frame.setTitle(filename);       frame.setSize(800, 400);       frame.setLocationRelativeTo(null);       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       frame.setVisible(true);     }     else{       System.err.println(filename + "不是一個正常的wav檔案");     }   } } 

工程的原始碼可以在我的百度網盤上找到,直接到開源JAVA

放上效果圖一張:

希望本文所述對大家的java程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.