Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a wav file

Status
Not open for further replies.

kentech1

Programmer
Apr 19, 2007
3
US
I have to write a java program to read the content of a wav file. I've been researching this project for a few months now, but for some reason I have not been able to find any sample code that could point me to the right direction. I pretty much understand the wav file format in theory. I would really appreciate if someone could send me a sample code to reading wav file or point me to the right direction.
 
kentech1 said:
I pretty much understand the wav file format in theory

Okay, that's good. So exactly how are you having trouble then? Can you post some succinct code which illustrates your difficulties? Are you having trouble with the java io classes or is it something further down the line?

Tim
 
The issue here is I have no experience working wave files. I've asked several programmers for help and none of them seems to know anything about wave files. At this point I would like to know how to view both the content of the header and the data chunk. The sample script below is just something I copied of the internet that I think could help Please any help would be greatly appreciated. I've been looking at this problem for about three months now.



import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;




/* Play a *.wav or *.au file */

public class test
{
/*Play a *.wav or *.au file
@param args args[0] on command line is name of file to play
*/
static int frameSample;
static int timeofFrame;
static int N;
static int runTimes;
static int bps;
static int channels;
static double times;
static int bufSize;
static int frameSize;
static int frameRate;
static long length;

public static void main(String[] args)
{
try
{
AudioInputStream ais = AudioSystem.getAudioInputStream(new File(args[0]));
AudioInputStream a;

File file = new File(args[0]); /*To get the file size*/
length = file.length();
System.out.println("File size : " + length);

AudioFormat af = ais.getFormat();
DataLine.Info info = new DataLine.Info(SourceDataLine.class, af);

if (!AudioSystem.isLineSupported(info))
{
System.out.println("unsupported line");
System.exit(0);
}

frameRate = (int)af.getFrameRate();
System.out.println("Frame Rate: " + frameRate);

frameSize = af.getFrameSize();
System.out.println("Frame Size: " + frameSize);

bufSize = frameRate * frameSize / 10;
System.out.println("Buffer Size: " + bufSize);

channels = af.getChannels();
System.out.println("Channels : " + channels);

bps = af.getSampleSizeInBits();
System.out.println("Bits per sample : " + bps);

times = (double)(length / (frameRate * channels * bps / 8));
System.out.println("Duration of the songs : " + times +" seconds");

byte[] data2 = new byte[bufSize];
int bytesRead2;
timeofFrame = 20; //20ms
frameSample = (timeofFrame * frameRate) / 1000;
N = frameSample;
runTimes = (int) (times * 1000) / 20;


byte[] data = new byte[bufSize];
int bytesRead;
/*SourceDataLine line = (SourceDataLine)
AudioSystem.getLine(info);
line.open(af, bufSize);
line.start();

while ((bytesRead = ais.read(data, 0, data.length)) != -1)
line.write(data, 0, bytesRead);

line.drain();
line.stop();

long time = line.getMicrosecondPosition();
System.out.println("time by playing " + time);
line.close();*/


int[][] freq = new int[runTimes][N];

int temp=0;


/* Want to read the data of the wav and do the DFT later, but i don't know how to read data and save it correctly?"
for (int i = 0; i < runTimes; i++)
//for(int j=0;j<N;j++)
{
a.read(freq, j, N);
j = N;
}


*/

FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;


fis = new FileInputStream(file);

// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);

// dis.available() returns 0 if the file does not have more lines.
while (dis.available() != 0) {

// this statement reads the line from the file and print it to the console.
for (int i = 0; i < 1; i++)
for(int j=0;j<N;j++)
{
freq[j] = (int)dis.readByte();

}
}
System.out.println(freq[0][0]);

// dispose all the resources after using them.
fis.close();
bis.close();
dis.close();

}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
/*for (int i = 0; i < 10; i++)
//for(int j=0;j<N;j++)
{
freq = a.read(file);
//j = N;
}
System.out.println(freq[0]);
*/

catch (Exception e)
{
System.out.println(e.toString());
}
System.exit(0);
}
}
 
Actually, here is the question that I'm trying to answer. That's why I thought the sample program above would be the right approach. Please let me know what you thoughts are.


Develop a working Java program that opens MDCK.WAV and searches its data area for the minimum (most negative) and maximum (most positive) samples. The hexadecimal address of the sample within the WAV file must also be displayed, as in this example (not actual) result:
The maximum value 7F hex is located at address 1E7A hex within the file.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top