The example of this article tells the way Java reads WAV files (waveform files) and draws the waveform diagram. Share it for everyone for your reference. The specifics are as follows:
Because many netizens recently asked me about the reading and writing of waveform files, because it made everyone more convenient and made the code better improve, I opened this part (read and write of waveform files) on the Github.
The address is https://github.com/sintrb/waveaccess/. Interested netizens can join the open source project.
The following content is basically expired. You can go to Github to read and download the project.
Because the project needs to read the .wav file (waveform file) and draw the waveform diagram, it was simply packaged in this area.
In fact, it is mainly the packaging of WAV files. Below is a package of a WAV file reader:
// filename: WavefileRereader.java // Robintang // 2012-08-23 Import Java.io.*; Public Class WaveFileRereader {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; PRI Vate String Waveflag = NULL; Static Private Int Lenwareflag = 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 LenaudiFormat = 2; PRIVATE I nt numghannels = 0; Static Private Int Lennnnnnnnnnnnnnnnnnnnnnnnnls = 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 bitspersample = 0; Static Private Int lenbitsaprsample = 2; Private String DataSubchunk = Null; Static; Static Private int Lendatasubchunk = 4; Private Long Subchunk2size = 0; Static Private Int Lensubchunk2Size = 4; Private FileInputStream fis = null; Private FFEREDINPUTSTREAM BIS = NULL; Private Boolean Issuccess = False; Public WaveFileReader (String Filename) {this.initreader (FILENAME) ;} // Determine whether to create a WAV reader successfully Public Boolean Issuccess () {Return issSuccess;} // Get the coding length of each sampling, 8bit or 16bit public intBitpersample () {Return itspersample;} // Get The sampling rate pblic long getsamplerate () {Return this.samplerate;} // Get the number of channels, 1 represents a single channel 2 represents the stereo public into, Data length, that is, How many public int Getdatalen () {Return this.Len;} // Get the data // data is a two -dimensional array, [n] [m] represents the n -sample value Public int [ ] [] Getdata () {Return This.data;} Private void Initreader (String FileName) {this.filename = filename; try {fis = NewputStream (this.filenamam e); bis = new bufferedInputStream (FIS); this.chunkDescriptor = ReadString (LENCHUNKDEScriptor); if (! ChunkDescriptor.endswith ("Riff") Throw New illegalargumexception ("RIFF MISS," + Filename + "is not a w ave file. "); this.chunksize = readlong (); this. Waveflag = ReadString (lenwaveflag); if (! Waveflag.endswith ("Wave") Throw New ILLEGALARGUMENCEPTION ("Wave Miss," + Filename + "is not a wave f iLe. "); This.fmtubchunk = ReadString (lenfmtubchunk); if (! FMTUBCHUNDSWITH ("FMT")) Throw New iLleGALARGUMENTEXCEPTION ("FMT MISS," + FILENAME + "is Not a Wave File."); ong (); this.Audioformat = Readint (); This.numchannels = Readint (); this.samplerate = Readlong (); this.byterate = Readlong (); this.BlockAlign = Readint (); this.Bitspersample = Rea dint (); this.datasubchunk = ReadString (lendatasubchunk); if (! DataSUBCHUNK.ENDSWITH ("Data")) Throw New iLlegalargumentexception ("Data Miss," + FileName + "is not a wave file."); this.subchunk2size = g (); 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.bitSersample == 8) {this.data [n] [i] = bis.read (); .bitsaprsAmple == 16) {this.data [n] [i] = this.readint ();}}} isccess = true;} Catch (Exception E) {e.printstacktrace ();} Finally {try {if (if bis! = Null) bis.close (); if (fis! = Null) fis.close ();} Catch (Exception E1) {e1.printstacktrace ();}}} Private String (Int Len) {byte [ ] buf = new byte [len]; try {if (bis.read (buf)! = Len) Throw new ioException ("no more data !!!"); } 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))]) << 8);} Catch (ioException e) {e.printstacktrace (); ;} 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 !!!"); [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;}}
In order to draw the waveform, a waveform drawing panel from the JPANEL tutorial was made:
// filename: Drawpanel.java // Robintang // 2012-08-23 Import Java.Color; Import Java.awt.graphics; Import javax.swing.jpanel (" IAL ") Public Class DrawPanel Extends Jpanel {Private int [] data = null; Public Drawpanel (int [] data) {this.data = data;} @Override ProteCTED Paintcomponent (Graphics G) {int WW = th (); 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; // Previous coordinates 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; // The following is a three-point take out and draw // In fact, the interval is set according to the sampling rate y = hh- (int) (data [i*3]*k+hh/2); Systemm .out.print (y); System.out.print (""); if (i! = 0) {g.drawline (x, y, prex, prey);} prex = x; prey = y;}}} }
After these these, you can call and draw, simple:
// WaveFileRereadDemo.java // Robintang // 2012-08-23 Import javax.swing.jframe; Public Class WaveFileRererayDemo {/ ** * @param ARGS d main (string [] args) {// todo auto -GieRATED METHOD String Filename = "File.wav"; JFRAME FRAME = New JFRAME (); Success ()) {int [] data = reader.getdata () [) [) 0]; //获取第一声道DrawPanel drawPanel = new DrawPanel(data); // 创建一个绘制波形的面板frame.add(drawPanel); frame.setTitle(filename); frame.setSize(800, 400); Frame.setLocationRelationoTo (NULL); Frame.setDefaultCloseoperation (jframe.exit_on_close); frame.setvisible (true);} else {system.errr.errin. tln (FILENAME + "is not a normal wav file");}}}
The source code of the project can be found on my Baidu network disk, and directly to the open source Java
Put a renderings:
It is hoped that this article is helpful to everyone's Java program design.