473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how to read wav.file and put it into byte array and do the DFT?

hi, i am the student who should write a program about reading wav file and do the DFT.

actually i don't know how to read data of the wav song and save it into the array...

if i want to read 17640 every times., and i set byte read[]=new byte[17640],

i have the wav format paper, but i don't know how to read the data correctly into the byte array, and used for doing DFT....

here's my code, and i am little hurry...thanks for the helps first~

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[i], 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[i][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[i] = a.read(file);
//j = N;
}
System.out.println(freq[0]);
*/

catch (Exception e)
{
System.out.println(e.toString());
}
System.exit(0);
}
}
Mar 6 '07 #1
0 5735

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Michael Van Altena via .NET 247 | last post by:
I'm trying to figure out how to read a formatted binary file intoa structure definition in C#. I've tried using the"StructLayout" attribute with both LayoutKind.Explicit andLayoutKind.Sequential...
4
by: Lingyun Yang | last post by:
*** post for FREE via your newsreader at post.newsfeed.com *** Dear all, I have a file it's binary data viewed in UltraEdit is EF BB BF 0D 0A 3C ....... I want to read them into a int or long...
3
by: Shaun Merrill | last post by:
Hey, here is my code. I am trying to read a file into a byte array. Please show me how this can work. Obviously I need to read into a dynamic array because it could read any file. So why does...
23
by: ShaneO | last post by:
Hello, I wish to extract embedded string data from a file using a Binary Read method. The following code sample is used in VB.NET and similar code is used in VB6 - (Assume variable...
14
by: chance | last post by:
Hello, I have a file on disk called TEMP.ZIP and I would like to somehow get this into a memory stream so I can eventually do this: row = dataStream.ToArray() However, I am not sure of the...
11
by: Icemokka | last post by:
Hi, I'm need to upload a big file ( 600Mb+ ) to a BLOB field in MSSQL 2005. My code looks like this : fs = New FileStream(sFilePath, FileMode.Open) Dim ByteArray(fs.Length) As Byte...
5
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a...
3
by: sam | last post by:
same as subject?
1
by: tom thomas | last post by:
Dear sir, i am very new to c# and i request you to... please look into the problem The following is the code(please see the code below:) which i uses to open the binary file,currently it works...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.