Go Premium for a chance to win a PS4. Enter to Win


Extracting audio data from WAV file byte array

Posted on 2003-10-30
Medium Priority
Last Modified: 2008-02-01
I am creating an audio player using the Java Sound API (using J2SDK 1.4.2). WAV files are stored in a database.  These WAV files are passed to me in the form of a byte array (headers included). Is there any easy way to extract the audio data without building a parser?

I've been creating an AudioInputStream as follows:

byte[] wavFileBytes = getByteArrayFromDataBase();
ByteArrayInputStream bais = new ByteArrayInputStream(wavFileBytes);
AudioInputStream ais = AudioSystem.getAudioInputStream(bais);

However, when I read the AudioInputStream ( ais.read(...) ) I get the WAV headers as well. Actually, I get back the original byte array (wavFileBytes). I was expecting the call to AudioSystem.getAudioInputStream() to be smart enough to know that headers were in the original byte array. I can actually play this data, but there is an annoying click at the start due to the headers.

I was reviewing how the Java Sound demo does it, but it reads a WAV file from the file system using:

File file = new File("<filepath>");AudioInputStream ais = AudioSystem.getAudioInputStream(file);

In this case, when the AudioInputStream is read only the audio bytes are received.

Does anyone know a way around this problem? Is there a way to treat the byte array as a file?
Question by:lizardpie
  • 3
  • 2
LVL 15

Expert Comment

ID: 9654636
I know nothing about audio, so this is just based on what I've found in the APIs ;-)

Could you use the following:

Get the length (in sample frames) from ais

Get the audio type and using this, get the size of a frame for that type

Mulitply the two together to calculate the size (in bytes) of the data in the audio

Remove (or skip) wavFileBytes.length - audioLength.

As I said, I've no idea if this will work or not, but it looks like it might.

Author Comment

ID: 9654717
The research I've done into wav files indicates that the data chunk doesn't have to be the last thing in the file.  There could be subsequent chunks.  Maybe this doesn't happen in practice, but you never know.  I know I could build a little parser that looks for the data chunk, which already contains the length of data, but apparently there are other considerations as well.  The fmt (format) could specify some form of compression used in the data.  I don't know all the ins and out of audio either.

However, I think I've solved my own problem.  I'm writing the byte array to a temporary file and then creating my AudioInputStream from it with AudioSystem.getAudioInputStream(File file). Now when I read the audioInputStream I just get the audio data and no headers.  I just tested it and the waveform now looks and sounds clean.
LVL 15

Expert Comment

ID: 9656395
File and data formats like this are never "easy" to deal with :-(

Thanks for the info about wavs.  I didn't know that :-)

Sorry I couldn't help more, but at least you've got your solution :-)


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Author Comment

ID: 9658261

Thanks for taking the time to respond.

LVL 15

Expert Comment

ID: 9658266
No problem.  It was quite interesting ;-)  It's always good to learn something new.

Accepted Solution

PashaMod earned 0 total points
ID: 9812055
Question closed and points refunded

CS Moderator

Featured Post

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Suggested Courses

885 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