import java.io.*; public class ReadWave{ private int WINDOW_SIZE; private DataInputStream in; private int sampling_rate= 0; private int mono_stereo=0; private int sampling_bit=0; /** * Constructer of this. */ public ReadWave(int WINDOW_SIZE){ this.WINDOW_SIZE= WINDOW_SIZE; } /** * open */ public int openWavFile(String input_file){ int ret=0; try{ in= new DataInputStream(new FileInputStream(input_file)); }catch(IOException e){ e.printStackTrace(); return -1; } return 0; } /** * close */ public int closeWavFile(){ int ret=0; try{ in.close(); }catch(IOException e){ e.printStackTrace(); return -1; } return 0; } /** * read wave format header * @param in - DataInputStream * @return wave data size */ private int readWavHeader(){ int size=0; int b; try{ /* RIFF header */ size= read4Char(in,(byte)'R',(byte)'I',(byte)'F',(byte)'F'); if(size<0){ System.err.println("error: not RIFF format."); System.exit(4); } read4BytesSize(in); size= read4Char(in,(byte)'W',(byte)'A',(byte)'V',(byte)'E'); if(size<0){ System.err.println("error: WAVE error"); System.exit(5); } /* fmt chunk */ size= read4Char(in,(byte)'f',(byte)'m',(byte)'t',(byte)' '); if(size<0){ System.err.println("error: fmt error"); System.exit(6); } size= read4BytesSize(in); readFmtSubChunk(in, size); /* fact/data chunk check */ b= in.readByte(); if((byte)'f'==b){ /* fact chunk */ size= read3Char(in,(byte)'a',(byte)'c',(byte)'t'); if(size<0){ System.err.println("error: fact error"); System.exit(7); } size= read4BytesSize(in); for(int i=0; i=1 read byte */ private int read4Char(DataInputStream in, byte c1, byte c2, byte c3, byte c4){ byte b; try{ b= in.readByte(); if(c1!=b){ in.close(); System.err.println("Not wave format (0)\n"); return -1; } b= in.readByte(); if(c2!=b){ in.close(); System.err.println("Not wave format (1)\n"); return -1; } b= in.readByte(); if(c3!=b){ in.close(); System.err.println("Not wave format (2)\n"); return -1; } b= in.readByte(); if(c4!=b){ in.close(); System.err.println("Not wave format (3)\n"); return -1; } }catch(IOException e){ e.printStackTrace(); }finally{ return (4); } } /** * @retrun -1 error * @return >=1 read byte */ private int read3Char(DataInputStream in, byte c2, byte c3, byte c4){ byte b; try{ b= in.readByte(); if(c2!=b){ in.close(); System.err.println("Not wave format (1)\n"); return -1; } b= in.readByte(); if(c3!=b){ in.close(); System.err.println("Not wave format (2)\n"); return -1; } b= in.readByte(); if(c4!=b){ in.close(); System.err.println("Not wave format (3)\n"); return -1; } }catch(IOException e){ e.printStackTrace(); }finally{ return (3); } } /** * read 4bytes data * @reaturn size */ private int read4BytesSize(DataInputStream in){ int size=0; byte b1,b2,b3,b4; try{ b1= in.readByte(); b2= in.readByte(); b3= in.readByte(); b4= in.readByte(); size= b4<<24 | b3<<16 | b2<<8 | b1; }catch(IOException e){ e.printStackTrace(); } return size; } /** * read fmt sub-chunk */ private void readFmtSubChunk(DataInputStream in, int fmt_size){ byte b1,b2,b3,b4; int size; int read_bytes=0; try{ /* format tag */ b1= in.readByte(); read_bytes++; b2= in.readByte(); read_bytes++; size= b2<<8 | b1; /* channels */ b1= in.readByte(); read_bytes++; b2= in.readByte(); read_bytes++; size= b2<<8 | b1; if(size==1){ System.out.println("channels : monoral"); mono_stereo= 1; }else if(size==2){ System.out.println("channels : stereo"); mono_stereo= 2; } /* sampling */ b1= in.readByte(); read_bytes++; b2= in.readByte(); read_bytes++; b3= in.readByte(); read_bytes++; b4= in.readByte(); read_bytes++; size= ((0x00ff&b4)<<24) | ((0x00ff&b3)<<16) | ((0x00ff&b2)<<8) | (0x00ff&b1); System.out.println("sampling : "+size); sampling_rate= size; /* bytes par sec */ b1= in.readByte(); read_bytes++; b2= in.readByte(); read_bytes++; b3= in.readByte(); read_bytes++; b4= in.readByte(); read_bytes++; size= (0x00ff&b4<<24) | (0x00ff&b3<<16) | (0x00ff&b2<<8) | (0x00ff&b1); /* block boundary */ b1= in.readByte(); read_bytes++; b2= in.readByte(); read_bytes++; size= b2<<8 | b1; /* bit sample */ b1= in.readByte(); read_bytes++; b2= in.readByte(); read_bytes++; size= b2<<8 | b1; System.out.println("bit sample : "+size); sampling_bit= size; // extended size if(read_bytes [buffering_size]"); }else{ int WINDOW_SIZE=160; if(args.length>1){ try{ }catch(NumberFormatException e){ e.printStackTrace(); System.err.println("buffering_size="+WINDOW_SIZE); } } ReadWave rw= new ReadWave(WINDOW_SIZE); int ret = rw.openWavFile(args[0]); int size= rw.readWavHeader(); int sampling_rate = rw.getSamplingRate(); int mono_stereo = rw.getMonoStereo(); int sampling_bit = rw.getSamplingBit(); PlaySound pc= new PlaySound(sampling_rate, sampling_bit, mono_stereo); // read buffer byte[] buffer= rw.readWavData(); while(buffer!=null){ // play //buffer= sp.fft_ifft(buffer, WINDOW_SIZE); pc.play(buffer); // read buffer buffer= rw.readWavData(); } ret=rw.closeWavFile(); pc.end(); } } }