// read_wav.java read_wav ok.wav // reads bytes one at a time as an int import java.io.*; public class read_wav { char riff[] = new char[4]; int sread, swrite; int fsize; char wave[] = new char[4]; char fmt[] = new char[4]; int nbytes; short ccode; short channels; int rate; int avgrate; short blockalign; short bps; // bits per sample char data[] = new char[4]; int csize[] = new int[50]; // max 50 chunks int ncsize = 0; int ibyte; // byte of cound char more[] = new char[4]; int smin = 0; int smax = 0; int savg; int nbread; // number of bytes read int outix; // number of output samples int A[]; // output sound amplitude, bigger than outix public read_wav(String file_in, String file_out, boolean ifwrite) { wav_read(file_in); if(ifwrite) wav_write(file_out); } void wav_read(String inp) { System.out.println("read_wav running, reading "+inp); outix = 0; try { FileInputStream ds = new FileInputStream(inp); sread = 4; for(int i=0; i127) ibyte = ibyte-256; A[outix] = ibyte; outix++; savg = savg + ibyte; if(i==0) {smin=ibyte; smax=ibyte;} smin = ibytesmax?ibyte:smax; if(i<10 || i>csize[ncsize]-10) System.out.println("sound byte ="+ibyte); } savg = savg / csize[ncsize]; System.out.println("smin="+smin+", smax="+smax+", savg="+savg); nbread = nbread+csize[ncsize]; System.out.println(nbread+" bytes read so far"); // read rest of chunks while(nbread+17127) ibyte = ibyte-256; A[outix] = ibyte; outix++; } nbread = nbread+8+csize[ncsize]; System.out.println(nbread+" bytes read so far"); } ds.close(); } catch(Exception e) { System.out.println("wav_read, some exception thrown"); } System.out.println("end wav_read"); } // end wav_read void wav_write(String outp) { System.out.println("read_write running, writing "+outp); int inix = 0; ncsize=0; try { FileOutputStream ds = new FileOutputStream(outp); sread = 4; for(int i=0; i=outix) break; } nbread = nbread+csize[ncsize]; System.out.println(nbread+" bytes written so far"); // write rest of chunks while(nbread+17=outix) break; for(int i=0; i<4; i++) ds.write(more[i]); System.out.println("wrote 4 bytes should be WAVE "+ more[0]+more[1]+more[2]+more[3]); smax = csize[ncsize]; for(int i=0; i<4; i++) { ds.write(smax%256); smax /= 256;} System.out.println("wrote chunk has "+csize[ncsize]+" bytes"); for(int i=0; i=outix) break; } nbread = nbread+8+csize[ncsize]; System.out.println(nbread+" bytes written so far"); } // ds.write(0); // on some files ds.close(); } catch(Exception e) { System.out.println("wav_write, some exception thrown"); } System.out.println("end wav_write"); } // end wav_write public static void main (String[] args) { String file_in = "ok.wav"; if(args.length>0) { file_in = args[0]; } String file_out = "junk.wav"; if(args.length>1) { file_out = args[1]; } new read_wav(file_in, file_out, true); } // end main } // end class read_wav of read_wav.java