Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

read a wav file

Posted on 2006-04-06
7
Medium Priority
?
434 Views
Last Modified: 2012-08-14
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

0
Comment
Question by:lacroix_al
  • 3
  • 3
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 16397730
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));
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 16399083
>> nBitsPerSample += fileHeader[i]*powers[i-34];
instead of using powers array, you can use :
   nBitsPerSample += fileHeader[i]<<( (i-34)<<3 );
0
 

Author Comment

by:lacroix_al
ID: 16402628
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 92

Expert Comment

by:objects
ID: 16404626
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));
0
 

Author Comment

by:lacroix_al
ID: 16404787
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

0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 16404843
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));



0
 

Author Comment

by:lacroix_al
ID: 16483218
thanks
Al
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month20 days, 19 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question