read a wav file

Hi,
I have this code that reads a wav file and outputs the data.

import java.io.*;

public class WAVReader {
      private static long[] powers = {1,256,256*256,256*256*256};

      private String RIFFHeader = "";
      private long RIFFSize;

      private String WAVHeader = "";

      private String ckID = "";
      private long nChunkSize;
      private int wFormatTag;
      private int nChannels;
      private long nSamplesPerSec;
      private long nAvgBytesPerSec;
      private int nBlockAlign;
      private int nBitsPerSample;
        private long numSamples;

      private String dataTag = "";

      private BufferedInputStream wavStream;
      private int numBytes = 0;
      private int bytesRead = 0;

      private int[] fileHeader;
        private int headerSize=0;

      public int getNChannels() {
            return nChannels;
      }

      public byte[] getFileHeader() {
    byte[] head = new byte[headerSize];
    for (int i=0; i<headerSize; i++)
      head[i] = ((byte)fileHeader[i]);
            return head;
      }

      public long getSamplingRate() {
            return nSamplesPerSec;
      }

      public int getNBitsPerSample() {
            return nBitsPerSample;
      }

      public long getNumTotalSamples() {
            return numSamples;
      }

      public int getBytesPerSample() {
            return nBitsPerSample/8;
      }


      public void close() {
            try {
                  wavStream.close();
            }
            catch (IOException e) {
                  System.err.println(e);
            }
      }

      public WAVReader(InputStream in) {
            wavStream = new BufferedInputStream(in);
            try {
                  numBytes = wavStream.available();
            }
            catch (IOException e) {
                  System.err.println(e);
            }
            try {
                  fileHeader = new int[1024];

                  int i = 0;
                  bytesRead = 0;
                  
                  //read in the RIFF header
                  for (; i<4; ++i) {
                        fileHeader[i] = (int)wavStream.read();
                        RIFFHeader += (char)fileHeader[i];
                        ++bytesRead;
                  }

                  //read in RIFF size
                  for (; i<8; ++i) {
                        fileHeader[i] = (int)wavStream.read();
                        RIFFSize += fileHeader[i]*powers[i-4];
                        ++bytesRead;
                  }

                  //read in WAVE header
                  for (; i<12; ++i) {
                        fileHeader[i] = (int)wavStream.read();
                        WAVHeader += (char)fileHeader[i];
                        ++bytesRead;
                  }
                  
                  //read in chunk ID
                  for (; i<16; ++i) {
                        fileHeader[i] = (int)wavStream.read();
                        ckID += (char)fileHeader[i];
                        ++bytesRead;
                  }

                  for (; i<20; ++i) {
                        fileHeader[i] = (int)wavStream.read();
                        nChunkSize += fileHeader[i]*powers[i-16];
                        ++bytesRead;
                  }

                  for (; i<22; ++i) {
                        fileHeader[i] = (int)wavStream.read();
                        wFormatTag += fileHeader[i]*powers[i-20];
                        ++bytesRead;
                  }

                  for (; i<24; ++i) {
                        fileHeader[i] = (int)wavStream.read();
                        nChannels += fileHeader[i]*powers[i-22];
                        ++bytesRead;
                  }

                  for (; i<28; ++i) {
                        fileHeader[i] = (int)wavStream.read();
                        nSamplesPerSec += fileHeader[i]*powers[i-24];
                        ++bytesRead;
                  }

                  for (; i<32; ++i) {
                        fileHeader[i] = (int)wavStream.read();
                        nAvgBytesPerSec += fileHeader[i]*powers[i-28];
                        ++bytesRead;
                  }

                  for (; i<34; ++i) {
                        fileHeader[i] = (int)wavStream.read();
                        nBlockAlign += fileHeader[i]*powers[i-32];
                        ++bytesRead;
                  }

                  for (; i<36; ++i) {
                        fileHeader[i] = (int)wavStream.read();
                        nBitsPerSample += fileHeader[i]*powers[i-34];
                        ++bytesRead;
                  }

                  //have we read in the whole fmt chunk?
                  for(; i < nChunkSize+20; ++i) {
                        fileHeader[i] = (int)wavStream.read();
                        ++bytesRead;
                  }

                  //get the next chunk header (hope for data)
                  int di;
                  boolean isDataChunk = false;
                  String chunkName = "";
                  while(!isDataChunk) {
                        chunkName = "";
                        di = i+4;
                        for(; i<di; ++i) {
                              fileHeader[i] = (int)wavStream.read();
                              ++bytesRead;
                              chunkName += (char)fileHeader[i];
                        }
                        isDataChunk = (0 == chunkName.compareTo("data"));
                        
                        //System.out.print("ischunk: "+isDataChunk+"\n");
                        if(! isDataChunk) {
                          System.out.print("Ignoring chunk:"+ chunkName +"\n");
                              int nChkSz = 0;
                              di = i+4;
                              for (; i<di; ++i) {
                                    fileHeader[i] = (int)wavStream.read();
                                    
                                    //System.out.print(fileHeader[i]+ "\n");
                                    nChkSz += fileHeader[i]*powers[i-di+4];
                                    ++bytesRead;
                              }
                              
                              //System.out.print("chunkSize: " + nChkSz + "\n");
                              di = i+nChkSz;
                              for (; i<di; ++i) {
                                    fileHeader[i] = (int)wavStream.read();
                                    ++bytesRead;
                              }
                        }
                  }
                  
      numSamples = (RIFFSize - bytesRead + 8)/(nChannels * (nBitsPerSample/8));
      headerSize = bytesRead;
        dataTag = chunkName;

            }
            catch (IOException e) {
                  System.err.println(e);
            }

            System.out.print("RIFF Header: " + '"' + RIFFHeader + '"');
            System.out.print(" " + RIFFSize);
            System.out.println(" (File Size = " + numBytes + ")");

            System.out.println("WAVE string: " + '"' + WAVHeader + '"');

            System.out.println("ckID: " + '"' + ckID + '"');
            System.out.println("nChunkSize: " + nChunkSize);
            System.out.println("wFormatTag: " + wFormatTag);
            System.out.println("nChannels: " + nChannels);
            System.out.println("nSamplesPerSec: " + nSamplesPerSec);
            System.out.println("nAvgBytesPerSec: " + nAvgBytesPerSec);
            System.out.println("nBlockAlign: " + nBlockAlign);
            System.out.println("nBitsPerSample: " + nBitsPerSample);
            System.out.println("data begin: " + '"' + dataTag + '"');
            System.out.println("");
      }

      public byte getNextWAVByte() {
            try {
                  ++bytesRead;
                  return (byte)wavStream.read();
            }
            catch (IOException e) {
                  System.err.println(e);
            }
            return 0;
      }

      public boolean isBlocked() {
            if (bytesRead >= numBytes)
                  close();
            return bytesRead >= numBytes;
      }

      public long getNextWAVAmplitude() {
            long amp = 0;
            try {
                  for (int i=0; i<nBitsPerSample/8; ++i)
                        amp += wavStream.read()*powers[i];
                  if (nChannels > 1)
                        for (int i=0; i<(nChannels-1)*nBitsPerSample/8; ++i)
                              wavStream.read();
            }
            catch (IOException e) {
                  System.err.println(e);
            }
            if (nBitsPerSample == 16) {
                  if (amp > 32767)
                        amp -= 65536;
            }

            if (nBitsPerSample == 8)
                  amp -= 128;
            return amp;
      }

      public long[] getChunk(int size) {
            long[] chunk = new long[size];
            for (int i=0; i<size; ++i)
                  chunk[i] = getNextWAVAmplitude();
            return chunk;
      }


      public static void main(String[] args) {
            WAVReader a = null;
            try {
                  a = new WAVReader(new FileInputStream("C:/Documents and Settings/s118608/Desktop/School/CIS416_PROGRAMMING/Project/dsclip.wav")); //args[0]));
            }
            catch (FileNotFoundException e) {
                  System.err.println(e);
            }
                  for (int i=0; i<Integer.parseInt("100"); ++i){      //10010      
                  System.out.println(Long.toString(a.getNextWAVAmplitude(),16)+"\t"+"@"+Integer.toHexString(i));
                  //create array with info from a.getnextwavamplitue and search it for the hi and low and print
      
      }
}
}
  what I need it to do is scan the output of the data and return the largest and smallest value.
How do I go about doing that?
Al

lacroix_alAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
long maxx = Long.MIN_VALUE;
  long minx = Long.MAX_VALUE;
         for (int i=0; i<100; ++i){     //10010    
               long x = a.getNextWAVAmplitude(),16);
               minx = Math.min(x, minx);
               maxx = Math.max(x, maxx);

               System.out.println(x+"\t"+"@"+Integer.toHexString(i));
WebstormCommented:
>> nBitsPerSample += fileHeader[i]*powers[i-34];
instead of using powers array, you can use :
   nBitsPerSample += fileHeader[i]<<( (i-34)<<3 );
lacroix_alAuthor Commented:
Ojects

I put that code in but I get an error

<identifier> expected
               long x = a.getNextWAVAmplitude(),16;
it is pointing to the 16
I did remove a ")" after the 16 but it didn't fix it.
Al
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Mick BarryJava DeveloperCommented:
sorry, thats a typo

long maxx = Long.MIN_VALUE;
  long minx = Long.MAX_VALUE;
         for (int i=0; i<100; ++i){     //10010    
               long x = a.getNextWAVAmplitude();
               minx = Math.min(x, minx);
               maxx = Math.max(x, maxx);

               System.out.println(Long.toString(x, 16)+"\t"+"@"+Integer.toHexString(i));
lacroix_alAuthor Commented:
Ok
What I really need is to print out the largest and smallest values and the corrisponding addresses in HEX.
EX.
Max = 7e      @ address 2a
Max = 7e      @ address 2b
Max = 7e      @ address 2c
Min = -7e      @ address 2a
Min = -7e      @ address 2b
Min = -7e      @ address 2c

This is what I get when I run it
Snippit:

IFF Header: "RIFF" 10043 (File Size = 10051)
WAVE string: "WAVE"
ckID: "fmt "
nChunkSize: 18
wFormatTag: 7
nChannels: 1
nSamplesPerSec: 8000
nAvgBytesPerSec: 8000
nBlockAlign: 1
nBitsPerSample: 8
data begin: "data"

-6b     @0
-59     @1
-80     @2
-80     @3
6f      @4
69      @5
7f      @6
-e      @7

What do I do?
Al

Mick BarryJava DeveloperCommented:
to also store the address use:

long maxx = Long.MIN_VALUE;
  long minx = Long.MAX_VALUE;
int mini, maxi;
         for (int i=0; i<100; ++i){     //10010    
               long x = a.getNextWAVAmplitude();
               if (x<minx) {
                 minx = x;
                 mini = i;
               }
               if (x>maxx) {
                 maxx = x;
                 maxi = i;
               }

               System.out.println(Long.toString(x, 16)+"\t"+"@"+Integer.toHexString(i));



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lacroix_alAuthor Commented:
thanks
Al
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.